|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ObjectDefineProperties, |
| 5 | + Symbol, |
| 6 | +} = primordials; |
| 7 | + |
| 8 | +const { |
| 9 | + TextDecoder, |
| 10 | + TextEncoder, |
| 11 | +} = require('internal/encoding'); |
| 12 | + |
| 13 | +const { |
| 14 | + TransformStream, |
| 15 | +} = require('internal/webstreams/transformstream'); |
| 16 | + |
| 17 | +const { |
| 18 | + customInspect, |
| 19 | + kEnumerableProperty, |
| 20 | +} = require('internal/webstreams/util'); |
| 21 | + |
| 22 | +const { |
| 23 | + codes: { |
| 24 | + ERR_INVALID_THIS, |
| 25 | + }, |
| 26 | +} = require('internal/errors'); |
| 27 | + |
| 28 | +const { |
| 29 | + customInspectSymbol: kInspect |
| 30 | +} = require('internal/util'); |
| 31 | + |
| 32 | +const kHandle = Symbol('kHandle'); |
| 33 | +const kTransform = Symbol('kTransform'); |
| 34 | +const kType = Symbol('kType'); |
| 35 | + |
| 36 | +/** |
| 37 | + * @typedef {import('./readablestream').ReadableStream} ReadableStream |
| 38 | + * @typedef {import('./writablestream').WritableStream} WritableStream |
| 39 | + */ |
| 40 | + |
| 41 | +function isTextEncoderStream(value) { |
| 42 | + return typeof value?.[kHandle] === 'object' && |
| 43 | + value?.[kType] === 'TextEncoderStream'; |
| 44 | +} |
| 45 | + |
| 46 | +function isTextDecoderStream(value) { |
| 47 | + return typeof value?.[kHandle] === 'object' && |
| 48 | + value?.[kType] === 'TextDecoderStream'; |
| 49 | +} |
| 50 | + |
| 51 | +class TextEncoderStream { |
| 52 | + constructor() { |
| 53 | + this[kType] = 'TextEncoderStream'; |
| 54 | + this[kHandle] = new TextEncoder(); |
| 55 | + this[kTransform] = new TransformStream({ |
| 56 | + transform: (chunk, controller) => { |
| 57 | + const value = this[kHandle].encode(chunk); |
| 58 | + if (value) |
| 59 | + controller.enqueue(value); |
| 60 | + }, |
| 61 | + flush: (controller) => { |
| 62 | + const value = this[kHandle].encode(); |
| 63 | + if (value.byteLength > 0) |
| 64 | + controller.enqueue(value); |
| 65 | + controller.terminate(); |
| 66 | + }, |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @readonly |
| 72 | + * @type {string} |
| 73 | + */ |
| 74 | + get encoding() { |
| 75 | + if (!isTextEncoderStream(this)) |
| 76 | + throw new ERR_INVALID_THIS('TextEncoderStream'); |
| 77 | + return this[kHandle].encoding; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @readonly |
| 82 | + * @type {ReadableStream} |
| 83 | + */ |
| 84 | + get readable() { |
| 85 | + if (!isTextEncoderStream(this)) |
| 86 | + throw new ERR_INVALID_THIS('TextEncoderStream'); |
| 87 | + return this[kTransform].readable; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @readonly |
| 92 | + * @type {WritableStream} |
| 93 | + */ |
| 94 | + get writable() { |
| 95 | + if (!isTextEncoderStream(this)) |
| 96 | + throw new ERR_INVALID_THIS('TextEncoderStream'); |
| 97 | + return this[kTransform].writable; |
| 98 | + } |
| 99 | + |
| 100 | + [kInspect](depth, options) { |
| 101 | + if (!isTextEncoderStream(this)) |
| 102 | + throw new ERR_INVALID_THIS('TextEncoderStream'); |
| 103 | + return customInspect(depth, options, 'TextEncoderStream', { |
| 104 | + encoding: this[kHandle].encoding, |
| 105 | + readable: this[kTransform].readable, |
| 106 | + writable: this[kTransform].writable, |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +class TextDecoderStream { |
| 112 | + /** |
| 113 | + * @param {string} [encoding] |
| 114 | + * @param {{ |
| 115 | + * fatal? : boolean, |
| 116 | + * ignoreBOM? : boolean, |
| 117 | + * }} [options] |
| 118 | + */ |
| 119 | + constructor(encoding = 'utf-8', options = {}) { |
| 120 | + this[kType] = 'TextDecoderStream'; |
| 121 | + this[kHandle] = new TextDecoder(encoding, options); |
| 122 | + this[kTransform] = new TransformStream({ |
| 123 | + transform: (chunk, controller) => { |
| 124 | + const value = this[kHandle].decode(chunk, { stream: true }); |
| 125 | + if (value) |
| 126 | + controller.enqueue(value); |
| 127 | + }, |
| 128 | + flush: (controller) => { |
| 129 | + const value = this[kHandle].decode(); |
| 130 | + if (value) |
| 131 | + controller.enqueue(value); |
| 132 | + controller.terminate(); |
| 133 | + }, |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @readonly |
| 139 | + * @type {string} |
| 140 | + */ |
| 141 | + get encoding() { |
| 142 | + if (!isTextDecoderStream(this)) |
| 143 | + throw new ERR_INVALID_THIS('TextDecoderStream'); |
| 144 | + return this[kHandle].encoding; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @readonly |
| 149 | + * @type {boolean} |
| 150 | + */ |
| 151 | + get fatal() { |
| 152 | + if (!isTextDecoderStream(this)) |
| 153 | + throw new ERR_INVALID_THIS('TextDecoderStream'); |
| 154 | + return this[kHandle].fatal; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @readonly |
| 159 | + * @type {boolean} |
| 160 | + */ |
| 161 | + get ignoreBOM() { |
| 162 | + if (!isTextDecoderStream(this)) |
| 163 | + throw new ERR_INVALID_THIS('TextDecoderStream'); |
| 164 | + return this[kHandle].ignoreBOM; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @readonly |
| 169 | + * @type {ReadableStream} |
| 170 | + */ |
| 171 | + get readable() { |
| 172 | + if (!isTextDecoderStream(this)) |
| 173 | + throw new ERR_INVALID_THIS('TextDecoderStream'); |
| 174 | + return this[kTransform].readable; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @readonly |
| 179 | + * @type {WritableStream} |
| 180 | + */ |
| 181 | + get writable() { |
| 182 | + if (!isTextDecoderStream(this)) |
| 183 | + throw new ERR_INVALID_THIS('TextDecoderStream'); |
| 184 | + return this[kTransform].writable; |
| 185 | + } |
| 186 | + |
| 187 | + [kInspect](depth, options) { |
| 188 | + if (!isTextDecoderStream(this)) |
| 189 | + throw new ERR_INVALID_THIS('TextDecoderStream'); |
| 190 | + return customInspect(depth, options, 'TextDecoderStream', { |
| 191 | + encoding: this[kHandle].encoding, |
| 192 | + fatal: this[kHandle].fatal, |
| 193 | + ignoreBOM: this[kHandle].ignoreBOM, |
| 194 | + readable: this[kTransform].readable, |
| 195 | + writable: this[kTransform].writable, |
| 196 | + }); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +ObjectDefineProperties(TextEncoderStream.prototype, { |
| 201 | + encoding: kEnumerableProperty, |
| 202 | + readable: kEnumerableProperty, |
| 203 | + writable: kEnumerableProperty, |
| 204 | +}); |
| 205 | + |
| 206 | +ObjectDefineProperties(TextDecoderStream.prototype, { |
| 207 | + encoding: kEnumerableProperty, |
| 208 | + fatal: kEnumerableProperty, |
| 209 | + ignoreBOM: kEnumerableProperty, |
| 210 | + readable: kEnumerableProperty, |
| 211 | + writable: kEnumerableProperty, |
| 212 | +}); |
| 213 | + |
| 214 | +module.exports = { |
| 215 | + TextEncoderStream, |
| 216 | + TextDecoderStream, |
| 217 | +}; |
0 commit comments