Skip to content

Commit

Permalink
stream: use private properties for compression
Browse files Browse the repository at this point in the history
PR-URL: #47218
Reviewed-By: Erick Wendel <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
  • Loading branch information
anonrig authored and nodejs-github-bot committed Apr 13, 2023
1 parent 1323992 commit 4e93247
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 51 deletions.
68 changes: 21 additions & 47 deletions lib/internal/webstreams/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,119 +25,97 @@ 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);
}

/**
* @readonly
* @type {ReadableStream}
*/
get readable() {
if (!isCompressionStream(this))
throw new ERR_INVALID_THIS('CompressionStream');
return this[kTransform].readable;
return this.#transform.readable;
}

/**
* @readonly
* @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);
}

/**
* @readonly
* @type {ReadableStream}
*/
get readable() {
if (!isDecompressionStream(this))
throw new ERR_INVALID_THIS('DecompressionStream');
return this[kTransform].readable;
return this.#transform.readable;
}

/**
* @readonly
* @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,
});
}
}
Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-whatwg-webstreams-compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/,
});

0 comments on commit 4e93247

Please sign in to comment.