diff --git a/lib/internal/webstreams/compression.js b/lib/internal/webstreams/compression.js index 2ba8e53fa28e73..6cbaa3f3250e73 100644 --- a/lib/internal/webstreams/compression.js +++ b/lib/internal/webstreams/compression.js @@ -2,14 +2,10 @@ const { ObjectDefineProperties, - Symbol, } = primordials; const { - codes: { - ERR_INVALID_ARG_VALUE, - ERR_INVALID_THIS, - }, + codes: { ERR_INVALID_ARG_VALUE }, } = require('internal/errors'); const { @@ -29,42 +25,30 @@ function lazyZlib() { return zlib; } -const kHandle = Symbol('kHandle'); -const kTransform = Symbol('kTransform'); -const kType = Symbol('kType'); - /** * @typedef {import('./readablestream').ReadableStream} ReadableStream * @typedef {import('./writablestream').WritableStream} WritableStream */ -function isCompressionStream(value) { - return typeof value?.[kHandle] === 'object' && - value?.[kType] === 'CompressionStream'; -} - -function isDecompressionStream(value) { - return typeof value?.[kHandle] === 'object' && - value?.[kType] === 'DecompressionStream'; -} - class CompressionStream { + #handle; + #transform; + /** * @param {'deflate'|'gzip'} format */ constructor(format) { - this[kType] = 'CompressionStream'; switch (format) { case 'deflate': - this[kHandle] = lazyZlib().createDeflate(); + this.#handle = lazyZlib().createDeflate(); break; case 'gzip': - this[kHandle] = lazyZlib().createGzip(); + this.#handle = lazyZlib().createGzip(); break; default: throw new ERR_INVALID_ARG_VALUE('format', format); } - this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]); + this.#transform = newReadableWritablePairFromDuplex(this.#handle); } /** @@ -72,9 +56,7 @@ class CompressionStream { * @type {ReadableStream} */ get readable() { - if (!isCompressionStream(this)) - throw new ERR_INVALID_THIS('CompressionStream'); - return this[kTransform].readable; + return this.#transform.readable; } /** @@ -82,38 +64,36 @@ class CompressionStream { * @type {WritableStream} */ get writable() { - if (!isCompressionStream(this)) - throw new ERR_INVALID_THIS('CompressionStream'); - return this[kTransform].writable; + return this.#transform.writable; } [kInspect](depth, options) { - if (!isCompressionStream(this)) - throw new ERR_INVALID_THIS('CompressionStream'); customInspect(depth, options, 'CompressionStream', { - readable: this[kTransform].readable, - writable: this[kTransform].writable, + readable: this.#transform.readable, + writable: this.#transform.writable, }); } } class DecompressionStream { + #handle; + #transform; + /** * @param {'deflate'|'gzip'} format */ constructor(format) { - this[kType] = 'DecompressionStream'; switch (format) { case 'deflate': - this[kHandle] = lazyZlib().createInflate(); + this.#handle = lazyZlib().createInflate(); break; case 'gzip': - this[kHandle] = lazyZlib().createGunzip(); + this.#handle = lazyZlib().createGunzip(); break; default: throw new ERR_INVALID_ARG_VALUE('format', format); } - this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]); + this.#transform = newReadableWritablePairFromDuplex(this.#handle); } /** @@ -121,9 +101,7 @@ class DecompressionStream { * @type {ReadableStream} */ get readable() { - if (!isDecompressionStream(this)) - throw new ERR_INVALID_THIS('DecompressionStream'); - return this[kTransform].readable; + return this.#transform.readable; } /** @@ -131,17 +109,13 @@ class DecompressionStream { * @type {WritableStream} */ get writable() { - if (!isDecompressionStream(this)) - throw new ERR_INVALID_THIS('DecompressionStream'); - return this[kTransform].writable; + return this.#transform.writable; } [kInspect](depth, options) { - if (!isDecompressionStream(this)) - throw new ERR_INVALID_THIS('DecompressionStream'); customInspect(depth, options, 'DecompressionStream', { - readable: this[kTransform].readable, - writable: this[kTransform].writable, + readable: this.#transform.readable, + writable: this.#transform.writable, }); } } diff --git a/test/parallel/test-whatwg-webstreams-compression.js b/test/parallel/test-whatwg-webstreams-compression.js index 859df4e07bed88..c527fc1f13d10e 100644 --- a/test/parallel/test-whatwg-webstreams-compression.js +++ b/test/parallel/test-whatwg-webstreams-compression.js @@ -51,17 +51,21 @@ Promise.all(['gzip', 'deflate'].map((i) => test(i))).then(common.mustCall()); assert.throws( () => Reflect.get(CompressionStream.prototype, 'readable', {}), { - code: 'ERR_INVALID_THIS', + name: 'TypeError', + message: /Cannot read private member/, }); assert.throws( () => Reflect.get(CompressionStream.prototype, 'writable', {}), { - code: 'ERR_INVALID_THIS', + name: 'TypeError', + message: /Cannot read private member/, }); assert.throws( () => Reflect.get(DecompressionStream.prototype, 'readable', {}), { - code: 'ERR_INVALID_THIS', + name: 'TypeError', + message: /Cannot read private member/, }); assert.throws( () => Reflect.get(DecompressionStream.prototype, 'writable', {}), { - code: 'ERR_INVALID_THIS', + name: 'TypeError', + message: /Cannot read private member/, });