node/lib/internal/webstreams/compression.js
Yagiz Nizipli 4e93247079 stream: use private properties for compression
PR-URL: https://github.com/nodejs/node/pull/47218
Reviewed-By: Erick Wendel <erick.workspace@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
2023-04-13 15:22:47 +00:00

137 lines
2.7 KiB
JavaScript

'use strict';
const {
ObjectDefineProperties,
} = primordials;
const {
codes: { ERR_INVALID_ARG_VALUE },
} = require('internal/errors');
const {
newReadableWritablePairFromDuplex,
} = require('internal/webstreams/adapters');
const { customInspect } = require('internal/webstreams/util');
const {
customInspectSymbol: kInspect,
kEnumerableProperty,
} = require('internal/util');
let zlib;
function lazyZlib() {
zlib ??= require('zlib');
return zlib;
}
/**
* @typedef {import('./readablestream').ReadableStream} ReadableStream
* @typedef {import('./writablestream').WritableStream} WritableStream
*/
class CompressionStream {
#handle;
#transform;
/**
* @param {'deflate'|'gzip'} format
*/
constructor(format) {
switch (format) {
case 'deflate':
this.#handle = lazyZlib().createDeflate();
break;
case 'gzip':
this.#handle = lazyZlib().createGzip();
break;
default:
throw new ERR_INVALID_ARG_VALUE('format', format);
}
this.#transform = newReadableWritablePairFromDuplex(this.#handle);
}
/**
* @readonly
* @type {ReadableStream}
*/
get readable() {
return this.#transform.readable;
}
/**
* @readonly
* @type {WritableStream}
*/
get writable() {
return this.#transform.writable;
}
[kInspect](depth, options) {
customInspect(depth, options, 'CompressionStream', {
readable: this.#transform.readable,
writable: this.#transform.writable,
});
}
}
class DecompressionStream {
#handle;
#transform;
/**
* @param {'deflate'|'gzip'} format
*/
constructor(format) {
switch (format) {
case 'deflate':
this.#handle = lazyZlib().createInflate();
break;
case 'gzip':
this.#handle = lazyZlib().createGunzip();
break;
default:
throw new ERR_INVALID_ARG_VALUE('format', format);
}
this.#transform = newReadableWritablePairFromDuplex(this.#handle);
}
/**
* @readonly
* @type {ReadableStream}
*/
get readable() {
return this.#transform.readable;
}
/**
* @readonly
* @type {WritableStream}
*/
get writable() {
return this.#transform.writable;
}
[kInspect](depth, options) {
customInspect(depth, options, 'DecompressionStream', {
readable: this.#transform.readable,
writable: this.#transform.writable,
});
}
}
ObjectDefineProperties(CompressionStream.prototype, {
readable: kEnumerableProperty,
writable: kEnumerableProperty,
});
ObjectDefineProperties(DecompressionStream.prototype, {
readable: kEnumerableProperty,
writable: kEnumerableProperty,
});
module.exports = {
CompressionStream,
DecompressionStream,
};