|
| 1 | +// A limited subset of TextEncoder / TextDecoder API |
| 2 | + |
| 3 | +// We can't return native TextDecoder if it's present, as Node.js one is broken on windows-1252 and we fix that |
| 4 | +// We are also faster than Node.js built-in on both TextEncoder and TextDecoder |
| 5 | + |
| 6 | +/* eslint-disable unicorn/text-encoding-identifier-case, @exodus/import/no-unresolved */ |
| 7 | + |
| 8 | +import { utf16toString, utf16toStringLoose } from '@exodus/bytes/utf16.js' |
| 9 | +import { utf8fromStringLoose, utf8toString, utf8toStringLoose } from '@exodus/bytes/utf8.js' |
| 10 | +import { windows1252toString } from '@exodus/bytes/windows1252.js' |
| 11 | + |
| 12 | +const Utf8 = 'utf-8' |
| 13 | +const Utf16LE = 'utf-16le' |
| 14 | +const Utf16BE = 'utf-16be' |
| 15 | +const Win1252 = 'windows-1252' |
| 16 | + |
| 17 | +// https://encoding.spec.whatwg.org/#names-and-labels |
| 18 | +// prettier-ignore |
| 19 | +const Utf8alias = new Set(['utf8', 'unicode-1-1-utf-8', 'unicode11utf8', 'unicode20utf8', 'x-unicode20utf8']) |
| 20 | +// prettier-ignore |
| 21 | +const Utf16LEalias = new Set(['utf-16', 'ucs-2', 'unicode', 'unicodefeff', 'iso-10646-ucs-2', 'csunicode']) // there is no 'utf16' |
| 22 | +const Utf16BEalias = new Set(['unicodefffe']) |
| 23 | +// prettier-ignore |
| 24 | +const Win1252alias = new Set([ |
| 25 | + 'ascii', 'latin1', 'l1', 'us-ascii', 'ansi_x3.4-1968', 'cp1252', 'cp819', 'csisolatin1', 'ibm819', |
| 26 | + 'iso-8859-1', 'iso-ir-100', 'iso8859-1', 'iso88591', 'iso_8859-1', 'iso_8859-1:1987', 'x-cp1252' |
| 27 | +]) |
| 28 | + |
| 29 | +const replacementChar = '\uFFFD' |
| 30 | + |
| 31 | +const normalizeEncoding = (encoding) => { |
| 32 | + const lower = `${encoding}`.trim().toLowerCase() |
| 33 | + if (Utf8 === lower || Utf16LE === lower || Utf16BE === lower || Win1252 === lower) return lower // fast path |
| 34 | + if (Utf8alias.has(lower)) return Utf8 |
| 35 | + if (Utf16LEalias.has(lower)) return Utf16LE |
| 36 | + if (Utf16BEalias.has(lower)) return Utf16BE |
| 37 | + if (Win1252alias.has(lower)) return Win1252 |
| 38 | + throw new RangeError('Only utf-8, utf-16le, utf-16be and windows-1252/latin1/ascii are supported') |
| 39 | +} |
| 40 | + |
| 41 | +const define = (obj, key, value) => Object.defineProperty(obj, key, { value, writable: false }) |
| 42 | + |
| 43 | +const fromSource = (x) => { |
| 44 | + if (x instanceof Uint8Array) return x |
| 45 | + if (x instanceof ArrayBuffer) return new Uint8Array(x) |
| 46 | + if (ArrayBuffer.isView(x)) return new Uint8Array(x.buffer, x.byteOffset, x.byteLength) |
| 47 | + if (globalThis.SharedArrayBuffer && x instanceof globalThis.SharedArrayBuffer) { |
| 48 | + return new Uint8Array(x.buffer, x.byteOffset, x.byteLength) |
| 49 | + } |
| 50 | + |
| 51 | + throw new TypeError('Argument must be a SharedArrayBuffer, ArrayBuffer or ArrayBufferView') |
| 52 | +} |
| 53 | + |
| 54 | +export function TextEncoder() { |
| 55 | + define(this, 'encoding', 'utf-8') |
| 56 | +} |
| 57 | + |
| 58 | +TextEncoder.prototype.encode = function (str = '') { |
| 59 | + const res = utf8fromStringLoose(str) |
| 60 | + return res.byteOffset === 0 ? res : res.slice(0) // Ensure 0-offset. TODO: do we need this? |
| 61 | +} |
| 62 | + |
| 63 | +// npmjs.com/text-encoding polyfill doesn't support this at all |
| 64 | +TextEncoder.prototype.encodeInto = function (str, target) { |
| 65 | + if (!(target instanceof Uint8Array)) throw new TypeError('Second argument must be an Uint8Array') |
| 66 | + const u8 = utf8fromStringLoose(str) |
| 67 | + if (target.length < u8.length) throw new RangeError('Truncation not supported') // TODO |
| 68 | + target.set(u8) // TODO: perf |
| 69 | + return { read: str.length, written: u8.length } |
| 70 | +} |
| 71 | + |
| 72 | +export function TextDecoder(encoding = Utf8, options = {}) { |
| 73 | + if (typeof options !== 'object') throw new TypeError('"options" argument must be of type object') |
| 74 | + const { fatal = false, ignoreBOM = false, stream = false } = options |
| 75 | + if (stream !== false) throw new TypeError('Option "stream" is not supported') |
| 76 | + |
| 77 | + define(this, 'encoding', normalizeEncoding(encoding)) |
| 78 | + define(this, 'fatal', fatal) |
| 79 | + define(this, 'ignoreBOM', ignoreBOM) |
| 80 | +} |
| 81 | + |
| 82 | +// TODO: test behavior on BOM for LE/BE |
| 83 | +TextDecoder.prototype.decode = function (input, { stream = false } = {}) { |
| 84 | + if (stream) throw new TypeError('Option "stream" is not supported') |
| 85 | + if (input === undefined) return '' |
| 86 | + let u = fromSource(input) |
| 87 | + let suffix = '' |
| 88 | + if (this.encoding === 'utf-8') { |
| 89 | + if (!this.ignoreBOM && u.byteLength >= 3 && u[0] === 0xef && u[1] === 0xbb && u[2] === 0xbf) { |
| 90 | + u = u.subarray(3) |
| 91 | + } |
| 92 | + |
| 93 | + return this.fatal ? utf8toString(u) : utf8toStringLoose(u) |
| 94 | + } |
| 95 | + |
| 96 | + if (this.encoding === 'utf-16le') { |
| 97 | + if (!this.ignoreBOM && u.byteLength >= 2 && u[0] === 0xff && u[1] === 0xfe) u = u.subarray(2) |
| 98 | + if (!this.fatal && u.byteLength % 2 !== 0) { |
| 99 | + u = u.subarray(0, -1) |
| 100 | + suffix = replacementChar |
| 101 | + } |
| 102 | + |
| 103 | + return (this.fatal ? utf16toString(u, 'uint8-le') : utf16toStringLoose(u, 'uint8-le')) + suffix |
| 104 | + } |
| 105 | + |
| 106 | + if (this.encoding === 'utf-16be') { |
| 107 | + if (!this.ignoreBOM && u.byteLength >= 2 && u[0] === 0xfe && u[1] === 0xff) u = u.subarray(2) |
| 108 | + if (!this.fatal && u.byteLength % 2 !== 0) { |
| 109 | + u = u.subarray(0, -1) |
| 110 | + suffix = replacementChar |
| 111 | + } |
| 112 | + |
| 113 | + return (this.fatal ? utf16toString(u, 'uint8-be') : utf16toStringLoose(u, 'uint8-be')) + suffix |
| 114 | + } |
| 115 | + |
| 116 | + if (this.encoding === 'windows-1252') return windows1252toString(u) // no BOM possible |
| 117 | + throw new RangeError('Unsupported encoding') |
| 118 | +} |
0 commit comments