/
polyfill.js
192 lines (180 loc) · 4.48 KB
/
polyfill.js
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
/* eslint-disable prefer-destructuring */
'use strict'
const REPLACEMENT = '\ufffd'
const BYTES = {
// 0b00_000 - 0b01_111: ASCII
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
7: 0,
8: 0,
9: 0,
10: 0,
11: 0,
12: 0,
13: 0,
14: 0,
15: 0,
// 0b10_000 - 0b10_111: Continuation
16: -1,
17: -1,
18: -1,
19: -1,
20: -1,
21: -1,
22: -1,
23: -1,
// 0b110_00 - 0b110_11: Two bytes
24: 1,
25: 1,
26: 1,
27: 1,
// 0b1110_0 - 0b1110_1: Three bytes
28: 2,
29: 2,
// 0b11110: Four bytes
30: 3,
// 0b11111: Invalid
31: -2,
}
const ERR_MSG = '[ERR_ENCODING_INVALID_ENCODED_DATA]: ' +
'The encoded data was not valid for encoding utf-8'
function utf8Decode(buf, fatal, state) {
if (!state) {
state = {cur: 0, left: 0}
}
let res = ''
for (const b of buf) {
const bytes = BYTES[(b & 0xf8) >> 3]
switch (bytes) {
case -2:
// Top 5 bits all set
state.cur = 0
state.left = 0
if (fatal) {
const err = new TypeError(ERR_MSG)
err.code = 'ERR_ENCODING_INVALID_ENCODED_DATA'
err.errno = 12
throw err
} else {
res += REPLACEMENT
}
break
case -1:
state.left--
if (state.left < 0) {
// Too many continuation bytes
state.cur = 0
state.left = 0
if (fatal) {
const err = new TypeError(ERR_MSG)
err.code = 'ERR_ENCODING_INVALID_ENCODED_DATA'
err.errno = 12
throw err
} else {
res += REPLACEMENT
}
} else {
state.cur = (state.cur << 6) | (b & 0x3f)
if (state.left === 0) {
res += String.fromCodePoint(state.cur)
state.cur = 0
}
}
break
case 0: // One ASCII7 byte
if ((state.cur !== 0) || (state.left !== 0)) {
// Not enough continuation bytes
state.cur = 0
state.left = 0
if (fatal) {
const err = new TypeError(ERR_MSG)
err.code = 'ERR_ENCODING_INVALID_ENCODED_DATA'
err.errno = 12
throw err
} else {
res += REPLACEMENT
}
}
res += String.fromCharCode(b)
break
default:
if ((state.cur !== 0) || (state.left !== 0)) {
// Not enough continuation bytes
state.cur = 0
state.left = 0
if (fatal) {
const err = new TypeError(ERR_MSG)
err.code = 'ERR_ENCODING_INVALID_ENCODED_DATA'
err.errno = 12
throw err
} else {
res += REPLACEMENT
}
}
state.left = bytes
state.cur = b & (0xff >> (bytes + 2))
break
}
}
return [res, state]
}
class TextDecoderPolyfill {
constructor(utfLabel, options) {
this.utfLabel = (utfLabel || 'utf-8').toLowerCase()
if ((this.utfLabel !== 'utf-8') && (this.utfLabel !== 'utf8')) {
const err = new RangeError('The "' + utfLabel + '" encoding is not supported')
err.code = 'ERR_ENCODING_NOT_SUPPORTED'
throw err
}
options = options || {}
this.fatal = Boolean(options.fatal)
this.ignoreBOM = Boolean(options.ignoreBOM)
this.state = null
}
decode(input, options) {
if (!(input instanceof Uint8Array)) {
if (input instanceof ArrayBuffer) {
input = new Uint8Array(input)
} else if (ArrayBuffer.isView(input)) {
input = new Uint8Array(input.buffer, input.byteOffset, input.byteLength)
} else {
const typ = typeof input
const err = new TypeError('The "input" argument must be an instance of ArrayBuffer or ArrayBufferView. Received type ' + typ)
err.code = 'ERR_INVALID_ARG_TYPE'
throw err
}
}
const str_state = utf8Decode(input, this.fatal, this.state)
let str = str_state[0]
const state = str_state[1]
if (options && options.stream) {
this.state = state
} else {
this.state = null
if (state.left !== 0) {
// Truncated
if (this.fatal) {
const err = new TypeError(ERR_MSG)
err.code = 'ERR_ENCODING_INVALID_ENCODED_DATA'
err.errno = 11
throw err
} else {
str += REPLACEMENT
}
}
}
if (!this.ignoreBOM) {
// U+FEFF: BYTE ORDER MARK
if (str.codePointAt(0) === 0xFEFF) {
return str.slice(1)
}
}
return str
}
}
module.exports = TextDecoderPolyfill