All files / lib/internal/webstreams encoding.js

100% Statements 247/247
100% Branches 59/59
100% Functions 16/16
100% Lines 247/247

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 24821x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 64x 64x 64x 64x 21x 186x 186x 186x 186x 21x 21x 21x 30x 30x 30x 30x 30x 47x 47x 47x 47x 111x 111x 111x 21x 21x 21x 15x 15x 15x 6x 6x 111x 24x 24x 24x 111x 1x 1x 1x 71x 71x 46x 30x 30x 30x 30x 30x 25x 25x 3x 3x 30x 30x 30x 21x 21x 21x 21x 21x 21x 2x 2x 1x 2x 21x 21x 21x 21x 21x 21x 30x 30x 29x 30x 21x 21x 21x 21x 21x 21x 30x 30x 29x 30x 21x 21x 2x 2x 1x 1x 1x 1x 1x 2x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 113x 113x 113x 113x 123x 123x 123x 113x 113x 60x 60x 60x 57x 113x 113x 113x 21x 21x 21x 21x 21x 21x 6x 6x 5x 6x 21x 21x 21x 21x 21x 21x 12x 12x 11x 12x 21x 21x 21x 21x 21x 21x 12x 12x 11x 12x 21x 21x 21x 21x 21x 21x 77x 77x 76x 77x 21x 21x 21x 21x 21x 21x 77x 77x 76x 77x 21x 21x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x  
'use strict';
 
const {
  ObjectDefineProperties,
  String,
  StringPrototypeCharCodeAt,
  Symbol,
  Uint8Array,
} = primordials;
 
const {
  TextDecoder,
  TextEncoder,
} = require('internal/encoding');
 
const {
  TransformStream,
} = require('internal/webstreams/transformstream');
 
const { customInspect } = require('internal/webstreams/util');
 
const {
  codes: {
    ERR_INVALID_THIS,
  },
} = require('internal/errors');
 
const {
  customInspectSymbol: kInspect,
  kEmptyObject,
  kEnumerableProperty,
} = require('internal/util');
 
const kHandle = Symbol('kHandle');
const kTransform = Symbol('kTransform');
const kType = Symbol('kType');
const kPendingHighSurrogate = Symbol('kPendingHighSurrogate');
 
/**
 * @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[kPendingHighSurrogate] = null;
    this[kType] = 'TextEncoderStream';
    this[kHandle] = new TextEncoder();
    this[kTransform] = new TransformStream({
      transform: (chunk, controller) => {
        // https://encoding.spec.whatwg.org/#encode-and-enqueue-a-chunk
        chunk = String(chunk);
        let finalChunk = '';
        for (let i = 0; i < chunk.length; i++) {
          const item = chunk[i];
          const codeUnit = StringPrototypeCharCodeAt(item, 0);
          if (this[kPendingHighSurrogate] !== null) {
            const highSurrogate = this[kPendingHighSurrogate];
            this[kPendingHighSurrogate] = null;
            if (0xDC00 <= codeUnit && codeUnit <= 0xDFFF) {
              finalChunk += highSurrogate + item;
              continue;
            }
            finalChunk += '\uFFFD';
          }
          if (0xD800 <= codeUnit && codeUnit <= 0xDBFF) {
            this[kPendingHighSurrogate] = item;
            continue;
          }
          if (0xDC00 <= codeUnit && codeUnit <= 0xDFFF) {
            finalChunk += '\uFFFD';
            continue;
          }
          finalChunk += item;
        }
        if (finalChunk) {
          const value = this[kHandle].encode(finalChunk);
          controller.enqueue(value);
        }
      },
      flush: (controller) => {
        // https://encoding.spec.whatwg.org/#encode-and-flush
        if (this[kPendingHighSurrogate] !== null) {
          controller.enqueue(new Uint8Array([0xEF, 0xBF, 0xBD]));
        }
      },
    });
  }
 
  /**
   * @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');
    return customInspect(depth, options, 'TextEncoderStream', {
      encoding: this[kHandle].encoding,
      readable: this[kTransform].readable,
      writable: this[kTransform].writable,
    });
  }
}
 
class TextDecoderStream {
  /**
   * @param {string} [encoding]
   * @param {{
   *   fatal? : boolean,
   *   ignoreBOM? : boolean,
   * }} [options]
   */
  constructor(encoding = 'utf-8', options = kEmptyObject) {
    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');
    return customInspect(depth, options, 'TextDecoderStream', {
      encoding: this[kHandle].encoding,
      fatal: this[kHandle].fatal,
      ignoreBOM: this[kHandle].ignoreBOM,
      readable: this[kTransform].readable,
      writable: this[kTransform].writable,
    });
  }
}
 
ObjectDefineProperties(TextEncoderStream.prototype, {
  encoding: kEnumerableProperty,
  readable: kEnumerableProperty,
  writable: kEnumerableProperty,
});
 
ObjectDefineProperties(TextDecoderStream.prototype, {
  encoding: kEnumerableProperty,
  fatal: kEnumerableProperty,
  ignoreBOM: kEnumerableProperty,
  readable: kEnumerableProperty,
  writable: kEnumerableProperty,
});
 
module.exports = {
  TextEncoderStream,
  TextDecoderStream,
};