This repository was archived by the owner on Dec 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (75 loc) · 2.98 KB
/
index.js
File metadata and controls
93 lines (75 loc) · 2.98 KB
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
const UTF8 = 'utf-8'
const UTF16LE = 'utf-16le'
// https://encoding.spec.whatwg.org/#names-and-labels
const UTF8alias = ['utf8', 'unicode-1-1-utf-8', 'unicode11utf8', 'unicode20utf8', 'x-unicode20utf8']
const UTF16LEalias = ['utf-16', 'ucs-2', 'unicode', 'unicodefeff', 'iso-10646-ucs-2', 'csunicode'] // but not utf16
const normalizeEncoding = (encoding) => {
const lower = encoding.toLowerCase()
if (UTF8 === lower || UTF16LE === lower) return lower // fast path
if (UTF8alias.includes(lower)) return UTF8
if (UTF16LEalias.includes(lower)) return UTF16LE
return lower
}
const defineFinal = (obj, property, value) =>
Object.defineProperty(obj, property, { value, writable: false })
const assertUTF8 = (encoding) => {
if (encoding !== UTF8) {
throw new Error('only utf-8 is supported')
}
}
const assertUTF8orUTF16LE = (encoding) => {
// We don't include ascii because it's an alias to windows-1252 in TextDecoder and differs from Buffer ascii
// We don't include utf-16be because it's not supported by buffer package
if (encoding !== UTF8 && encoding !== UTF16LE) {
throw new Error('only utf-8 and utf-16le are supported')
}
}
const fromBufferSouce = (buf) => {
if (buf instanceof ArrayBuffer) return Buffer.from(buf)
if (ArrayBuffer.isView(buf)) return Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
if (globalThis.SharedArrayBuffer && buf instanceof globalThis.SharedArrayBuffer) {
return Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
}
throw new Error('argument must be a SharedArrayBuffer, ArrayBuffer or ArrayBufferView')
}
// encoding argument is non-standard but catches usage of 'text-encoding' npm package API
// Standard TextEncoder constructor doesn't have any arguments at all and is always utf-8
function TextEncoder(encoding = UTF8) {
encoding = normalizeEncoding(encoding)
assertUTF8(encoding)
defineFinal(this, 'encoding', encoding)
}
TextEncoder.prototype.encode = function (str) {
const buf = Buffer.from(str)
return new Uint8Array(buf.buffer, buf.byteOffset, buf.length)
}
TextEncoder.prototype.encodeInto = function () {
throw new Error('not supported')
}
function TextDecoder(encoding = UTF8, options = {}) {
encoding = normalizeEncoding(encoding)
assertUTF8orUTF16LE(encoding)
// Buffer.from will throw
const { fatal = true, ignoreBOM = false, stream = false } = options
if (fatal === false) {
throw new Error('disabling "fatal" mode is not supported')
}
if (ignoreBOM) {
throw new Error('option "ignoreBOM" is not supported')
}
if (stream) {
throw new Error('option "stream" is not supported')
}
// see: https://github.com/inexorabletash/text-encoding/blob/master/lib/encoding.js#L1049
defineFinal(this, 'encoding', encoding)
defineFinal(this, 'fatal', fatal)
defineFinal(this, 'ignoreBOM', ignoreBOM)
}
TextDecoder.prototype.decode = function (buf) {
if (buf === undefined) return ''
return fromBufferSouce(buf).toString(this.encoding)
}
module.exports = {
TextEncoder,
TextDecoder,
}