-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
streams: add CompressionStream and DecompressionStream
Signed-off-by: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
4 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectDefineProperties, | ||
Symbol, | ||
} = primordials; | ||
|
||
const { | ||
codes: { | ||
ERR_INVALID_ARG_VALUE, | ||
ERR_INVALID_THIS, | ||
}, | ||
} = require('internal/errors'); | ||
|
||
const { | ||
newReadableWritablePairFromDuplex, | ||
} = require('internal/webstreams/adapters'); | ||
|
||
const { | ||
customInspect, | ||
} = require('internal/webstreams/util'); | ||
|
||
const { | ||
customInspectSymbol: kInspect, | ||
} = require('internal/util'); | ||
|
||
const { | ||
createDeflate, | ||
createInflate, | ||
createGzip, | ||
createGunzip, | ||
} = require('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 { | ||
/** | ||
* @param {'deflate'|'gzip'} format | ||
*/ | ||
constructor(format) { | ||
this[kType] = 'CompressionStream'; | ||
switch (format) { | ||
case 'deflate': | ||
this[kHandle] = createDeflate(); | ||
break; | ||
case 'gzip': | ||
this[kHandle] = createGzip(); | ||
break; | ||
default: | ||
throw new ERR_INVALID_ARG_VALUE('format', format); | ||
} | ||
this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]); | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {ReadableStream} | ||
*/ | ||
get readable() { | ||
if (!isCompressionStream(this)) | ||
throw new ERR_INVALID_THIS('CompressionStream'); | ||
return this[kTransform].readable; | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {WritableStream} | ||
*/ | ||
get writable() { | ||
if (!isCompressionStream(this)) | ||
throw new ERR_INVALID_THIS('CompressionStream'); | ||
return this[kTransform].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, | ||
}); | ||
} | ||
} | ||
|
||
class DecompressionStream { | ||
/** | ||
* @param {'deflate'|'gzip'} format | ||
*/ | ||
constructor(format) { | ||
this[kType] = 'DecompressionStream'; | ||
switch (format) { | ||
case 'deflate': | ||
this[kHandle] = createInflate(); | ||
break; | ||
case 'gzip': | ||
this[kHandle] = createGunzip(); | ||
break; | ||
default: | ||
throw new ERR_INVALID_ARG_VALUE('format', format); | ||
} | ||
this[kTransform] = newReadableWritablePairFromDuplex(this[kHandle]); | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {ReadableStream} | ||
*/ | ||
get readable() { | ||
if (!isDecompressionStream(this)) | ||
throw new ERR_INVALID_THIS('DecompressionStream'); | ||
return this[kTransform].readable; | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {WritableStream} | ||
*/ | ||
get writable() { | ||
if (!isDecompressionStream(this)) | ||
throw new ERR_INVALID_THIS('DecompressionStream'); | ||
return this[kTransform].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, | ||
}); | ||
} | ||
} | ||
|
||
ObjectDefineProperties(CompressionStream.prototype, { | ||
readable: { enumerable: true }, | ||
writable: { enumerable: true }, | ||
}); | ||
|
||
ObjectDefineProperties(DecompressionStream.prototype, { | ||
readable: { enumerable: true }, | ||
writable: { enumerable: true }, | ||
}); | ||
|
||
module.exports = { | ||
CompressionStream, | ||
DecompressionStream, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Flags: --expose-internals --no-warnings | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const { | ||
CompressionStream, | ||
DecompressionStream, | ||
} = require('internal/webstreams/compression'); | ||
|
||
const assert = require('assert'); | ||
const dec = new TextDecoder(); | ||
|
||
async function test(format) { | ||
const gzip = new CompressionStream(format); | ||
const gunzip = new DecompressionStream(format); | ||
|
||
gzip.readable.pipeTo(gunzip.writable).then(common.mustCall()); | ||
|
||
const reader = gunzip.readable.getReader(); | ||
const writer = gzip.writable.getWriter(); | ||
|
||
await Promise.all([ | ||
reader.read().then(({ value, done }) => { | ||
assert.strictEqual(dec.decode(value), 'hello'); | ||
}), | ||
reader.read().then(({ done }) => assert(done)), | ||
writer.write('hello'), | ||
writer.close(), | ||
]); | ||
} | ||
|
||
Promise.all(['gzip', 'deflate'].map((i) => test(i))).then(common.mustCall()); | ||
|
||
[1, 'hello', false, {}].forEach((i) => { | ||
assert.throws(() => new CompressionStream(i), { | ||
code: 'ERR_INVALID_ARG_VALUE', | ||
}); | ||
assert.throws(() => new DecompressionStream(i), { | ||
code: 'ERR_INVALID_ARG_VALUE', | ||
}); | ||
}); | ||
|
||
assert.throws( | ||
() => Reflect.get(CompressionStream.prototype, 'readable', {}), { | ||
code: 'ERR_INVALID_THIS', | ||
}); | ||
assert.throws( | ||
() => Reflect.get(CompressionStream.prototype, 'writable', {}), { | ||
code: 'ERR_INVALID_THIS', | ||
}); | ||
assert.throws( | ||
() => Reflect.get(DecompressionStream.prototype, 'readable', {}), { | ||
code: 'ERR_INVALID_THIS', | ||
}); | ||
assert.throws( | ||
() => Reflect.get(DecompressionStream.prototype, 'writable', {}), { | ||
code: 'ERR_INVALID_THIS', | ||
}); |