-
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: implement TextEncoderStream and TextDecoderStream
Experimental as part of the web streams implementation Signed-off-by: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
5 changed files
with
443 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,232 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectDefineProperties, | ||
Symbol, | ||
} = primordials; | ||
|
||
const { | ||
TextDecoder, | ||
TextEncoder, | ||
} = require('internal/encoding'); | ||
|
||
const { | ||
TransformStream, | ||
} = require('internal/webstreams/transformstream'); | ||
|
||
const { | ||
codes: { | ||
ERR_INVALID_THIS, | ||
}, | ||
} = require('internal/errors'); | ||
|
||
const { | ||
inspect, | ||
} = require('internal/util/inspect'); | ||
|
||
const { | ||
customInspectSymbol: kInspect | ||
} = require('internal/util'); | ||
|
||
const kHandle = Symbol('kHandle'); | ||
const kTransform = Symbol('kTransform'); | ||
const kType = Symbol('kType'); | ||
|
||
/** | ||
* @typedef {import('./readablestream').ReadableStream} ReadableStream | ||
* @typedef {import('./writablestream').WritableStream} WritableStream | ||
*/ | ||
|
||
function isTextEncoderStream(value) { | ||
return typeof value?.[kHandle] === 'object' && | ||
value?.[kType] === 'TextEncoderStream'; | ||
} | ||
|
||
function isTextDecoderStream(value) { | ||
return typeof value?.[kHandle] === 'object' && | ||
value?.[kType] === 'TextDecoderStream'; | ||
} | ||
|
||
class TextEncoderStream { | ||
constructor() { | ||
this[kType] = 'TextEncoderStream'; | ||
this[kHandle] = new TextEncoder(); | ||
this[kTransform] = new TransformStream({ | ||
transform: (chunk, controller) => { | ||
const value = this[kHandle].encode(chunk); | ||
if (value) | ||
controller.enqueue(value); | ||
}, | ||
flush: (controller) => { | ||
const value = this[kHandle].encode(); | ||
if (value.byteLength > 0) | ||
controller.enqueue(value); | ||
controller.terminate(); | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
get encoding() { | ||
if (!isTextEncoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextEncoderStream'); | ||
return this[kHandle].encoding; | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {ReadableStream} | ||
*/ | ||
get readable() { | ||
if (!isTextEncoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextEncoderStream'); | ||
return this[kTransform].readable; | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {WritableStream} | ||
*/ | ||
get writable() { | ||
if (!isTextEncoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextEncoderStream'); | ||
return this[kTransform].writable; | ||
} | ||
|
||
[kInspect](depth, options) { | ||
if (!isTextEncoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextEncoderStream'); | ||
if (depth < 0) | ||
return this; | ||
|
||
const opts = { | ||
...options, | ||
depth: options.depth == null ? null : options.depth - 1 | ||
}; | ||
|
||
return `${this[kType]} ${inspect({ | ||
encoding: this[kHandle].encoding, | ||
readable: this[kTransform].readable, | ||
writable: this[kTransform].writable, | ||
}, opts)}`; | ||
} | ||
} | ||
|
||
class TextDecoderStream { | ||
/** | ||
* @param {string} [encoding] | ||
* @param {{ | ||
* fatal? : boolean, | ||
* ignoreBOM? : boolean, | ||
* }} [options] | ||
*/ | ||
constructor(encoding = 'utf-8', options = {}) { | ||
this[kType] = 'TextDecoderStream'; | ||
this[kHandle] = new TextDecoder(encoding, options); | ||
this[kTransform] = new TransformStream({ | ||
transform: (chunk, controller) => { | ||
const value = this[kHandle].decode(chunk, { stream: true }); | ||
if (value) | ||
controller.enqueue(value); | ||
}, | ||
flush: (controller) => { | ||
const value = this[kHandle].decode(); | ||
if (value) | ||
controller.enqueue(value); | ||
controller.terminate(); | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {string} | ||
*/ | ||
get encoding() { | ||
if (!isTextDecoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextDecoderStream'); | ||
return this[kHandle].encoding; | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {boolean} | ||
*/ | ||
get fatal() { | ||
if (!isTextDecoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextDecoderStream'); | ||
return this[kHandle].fatal; | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {boolean} | ||
*/ | ||
get ignoreBOM() { | ||
if (!isTextDecoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextDecoderStream'); | ||
return this[kHandle].ignoreBOM; | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {ReadableStream} | ||
*/ | ||
get readable() { | ||
if (!isTextDecoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextDecoderStream'); | ||
return this[kTransform].readable; | ||
} | ||
|
||
/** | ||
* @readonly | ||
* @type {WritableStream} | ||
*/ | ||
get writable() { | ||
if (!isTextDecoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextDecoderStream'); | ||
return this[kTransform].writable; | ||
} | ||
|
||
[kInspect](depth, options) { | ||
if (!isTextDecoderStream(this)) | ||
throw new ERR_INVALID_THIS('TextDecoderStream'); | ||
if (depth < 0) | ||
return this; | ||
|
||
const opts = { | ||
...options, | ||
depth: options.depth == null ? null : options.depth - 1 | ||
}; | ||
|
||
return `${this[kType]} ${inspect({ | ||
encoding: this[kHandle].encoding, | ||
fatal: this[kHandle].fatal, | ||
ignoreBOM: this[kHandle].ignoreBOM, | ||
readable: this[kTransform].readable, | ||
writable: this[kTransform].writable, | ||
}, opts)}`; | ||
} | ||
} | ||
|
||
ObjectDefineProperties(TextEncoderStream.prototype, { | ||
encoding: { enumerable: true }, | ||
readable: { enumerable: true }, | ||
writable: { enumerable: true }, | ||
}); | ||
|
||
ObjectDefineProperties(TextDecoderStream.prototype, { | ||
encoding: { enumerable: true }, | ||
fatal: { enumerable: true }, | ||
ignoreBOM: { enumerable: true }, | ||
readable: { enumerable: true }, | ||
writable: { enumerable: true }, | ||
}); | ||
|
||
module.exports = { | ||
TextEncoderStream, | ||
TextDecoderStream, | ||
}; |
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
Oops, something went wrong.