expo 58.0.2 → 58.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +2 -2
- package/build/winter/runtime.native.js +0 -2
- package/build/winter/runtime.native.js.map +1 -1
- package/bundledNativeModules.json +31 -31
- package/package.json +15 -16
- package/scripts/spm/app-target.js +4 -2
- package/src/async-require/__tests__/loadBundle.test.ios.ts +2 -2
- package/src/winter/__tests__/{TextDecoderStream.test.ios.ts → TextDecoderStream.test.native.ts} +23 -2
- package/src/winter/runtime.native.ts +0 -2
- package/template.tgz +0 -0
- package/build/winter/TextDecoder.d.ts +0 -13
- package/build/winter/TextDecoder.js +0 -357
- package/build/winter/TextDecoder.js.map +0 -1
- package/src/winter/TextDecoder.ts +0 -427
- package/src/winter/__tests__/TextDecoder.test.native.ts +0 -549
|
@@ -1,549 +0,0 @@
|
|
|
1
|
-
describe('TextDecoder', () => {
|
|
2
|
-
// https://github.com/inexorabletash/text-encoding/blob/master/test/test-big5.js
|
|
3
|
-
|
|
4
|
-
it(`uses the Expo built-in APIs`, () => {
|
|
5
|
-
expect((TextDecoder as any)[Symbol.for('expo.builtin')]).toBe(true);
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L34C1-L47C18
|
|
9
|
-
it(`has expected attributes`, () => {
|
|
10
|
-
expect('encoding' in new TextEncoder()).toBe(true);
|
|
11
|
-
expect(new TextEncoder().encoding).toBe('utf-8');
|
|
12
|
-
|
|
13
|
-
expect('encoding' in new TextDecoder()).toBe(true);
|
|
14
|
-
|
|
15
|
-
expect(new TextDecoder().encoding).toBe('utf-8');
|
|
16
|
-
expect(new TextDecoder('utf-8').encoding).toBe('utf-8');
|
|
17
|
-
|
|
18
|
-
expect('fatal' in new TextDecoder()).toBe(true);
|
|
19
|
-
expect(new TextDecoder('utf-8').fatal).toBe(false);
|
|
20
|
-
expect(new TextDecoder('utf-8', { fatal: true }).fatal).toBe(true);
|
|
21
|
-
|
|
22
|
-
expect('ignoreBOM' in new TextDecoder()).toBe(true);
|
|
23
|
-
expect(new TextDecoder('utf-8').ignoreBOM).toBe(false);
|
|
24
|
-
expect(new TextDecoder('utf-8', { ignoreBOM: true }).ignoreBOM).toBe(true);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
describe(`supports utf8 labels`, () => {
|
|
28
|
-
[
|
|
29
|
-
'unicode-1-1-utf-8',
|
|
30
|
-
'unicode11utf8',
|
|
31
|
-
'unicode20utf8',
|
|
32
|
-
'utf-8',
|
|
33
|
-
'utf8',
|
|
34
|
-
'x-unicode20utf8',
|
|
35
|
-
// Always trim and lowercase the label
|
|
36
|
-
' UTF-8 ',
|
|
37
|
-
' X-uNIcode20utF8',
|
|
38
|
-
].forEach((label) => {
|
|
39
|
-
it(`should return utf-8 for label ${label}`, () => {
|
|
40
|
-
expect(new TextDecoder(label).encoding).toBe('utf-8');
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L49C1-L64C16
|
|
46
|
-
it(`handles bad data`, () => {
|
|
47
|
-
[
|
|
48
|
-
{ input: '\ud800', expected: '\ufffd' }, // Surrogate half
|
|
49
|
-
{ input: '\udc00', expected: '\ufffd' }, // Surrogate half
|
|
50
|
-
{ input: 'abc\ud800def', expected: 'abc\ufffddef' }, // Surrogate half
|
|
51
|
-
{ input: 'abc\udc00def', expected: 'abc\ufffddef' }, // Surrogate half
|
|
52
|
-
{ input: '\udc00\ud800', expected: '\ufffd\ufffd' }, // Wrong order
|
|
53
|
-
].forEach(({ input, expected }) => {
|
|
54
|
-
const encoded = new TextEncoder().encode(input);
|
|
55
|
-
const decoded = new TextDecoder().decode(encoded);
|
|
56
|
-
expect(expected).toBe(decoded);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L66C1-L91C18
|
|
61
|
-
describe('fatal flag', () => {
|
|
62
|
-
const bad = [
|
|
63
|
-
{ encoding: 'utf-8', input: [0xc0] }, // ends early
|
|
64
|
-
{ encoding: 'utf-8', input: [0xc0, 0x00] }, // invalid trail
|
|
65
|
-
{ encoding: 'utf-8', input: [0xc0, 0xc0] }, // invalid trail
|
|
66
|
-
{ encoding: 'utf-8', input: [0xe0] }, // ends early
|
|
67
|
-
{ encoding: 'utf-8', input: [0xe0, 0x00] }, // invalid trail
|
|
68
|
-
{ encoding: 'utf-8', input: [0xe0, 0xc0] }, // invalid trail
|
|
69
|
-
{ encoding: 'utf-8', input: [0xe0, 0x80, 0x00] }, // invalid trail
|
|
70
|
-
{ encoding: 'utf-8', input: [0xe0, 0x80, 0xc0] }, // invalid trail
|
|
71
|
-
{ encoding: 'utf-8', input: [0xfc, 0x80, 0x80, 0x80, 0x80, 0x80] }, // > 0x10FFFF
|
|
72
|
-
];
|
|
73
|
-
|
|
74
|
-
bad.forEach((t) => {
|
|
75
|
-
it(`should throw a TypeError for encoding ${t.encoding} and input ${t.input}`, () => {
|
|
76
|
-
expect(() => {
|
|
77
|
-
new TextDecoder(t.encoding, { fatal: true }).decode(new Uint8Array(t.input));
|
|
78
|
-
}).toThrow(TypeError);
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L93C1-L108C43
|
|
84
|
-
it('Encoding names are case insensitive', () => {
|
|
85
|
-
const encodings = [{ label: 'utf-8', encoding: 'utf-8' }];
|
|
86
|
-
|
|
87
|
-
encodings.forEach((test) => {
|
|
88
|
-
expect(new TextDecoder(test.label.toLowerCase()).encoding).toBe(test.encoding);
|
|
89
|
-
expect(new TextDecoder(test.label.toUpperCase()).encoding).toBe(test.encoding);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L110C1-L163C24
|
|
94
|
-
it('Byte-order marks', () => {
|
|
95
|
-
const utf8Bom = [0xef, 0xbb, 0xbf];
|
|
96
|
-
const utf8 = [
|
|
97
|
-
0x7a, 0xc2, 0xa2, 0xe6, 0xb0, 0xb4, 0xf0, 0x9d, 0x84, 0x9e, 0xf4, 0x8f, 0xbf, 0xbd,
|
|
98
|
-
];
|
|
99
|
-
|
|
100
|
-
const utf16leBom = [0xff, 0xfe];
|
|
101
|
-
|
|
102
|
-
const utf16beBom = [0xfe, 0xff];
|
|
103
|
-
|
|
104
|
-
const string = 'z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD'; // z, cent, CJK water, G-Clef, Private-use character
|
|
105
|
-
|
|
106
|
-
// missing BOMs
|
|
107
|
-
expect(new TextDecoder('utf-8').decode(new Uint8Array(utf8))).toBe(string);
|
|
108
|
-
|
|
109
|
-
// matching BOMs
|
|
110
|
-
expect(new TextDecoder('utf-8').decode(new Uint8Array([...utf8Bom, ...utf8]))).toBe(string);
|
|
111
|
-
|
|
112
|
-
// mismatching BOMs
|
|
113
|
-
expect(new TextDecoder('utf-8').decode(new Uint8Array([...utf16leBom, ...utf8]))).not.toBe(
|
|
114
|
-
string
|
|
115
|
-
);
|
|
116
|
-
expect(new TextDecoder('utf-8').decode(new Uint8Array([...utf16beBom, ...utf8]))).not.toBe(
|
|
117
|
-
string
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
// ignore BOMs
|
|
121
|
-
expect(
|
|
122
|
-
new TextDecoder('utf-8', { ignoreBOM: true }).decode(new Uint8Array([...utf8Bom, ...utf8]))
|
|
123
|
-
).toBe('\uFEFF' + string);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L165
|
|
127
|
-
describe('Encoding names', () => {
|
|
128
|
-
it('should return canonical case for utf-8', () => {
|
|
129
|
-
expect(new TextDecoder('utf-8').encoding).toBe('utf-8');
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L173
|
|
134
|
-
describe('Streaming Decode', () => {
|
|
135
|
-
const string = '\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF';
|
|
136
|
-
const cases = [
|
|
137
|
-
{
|
|
138
|
-
encoding: 'utf-8',
|
|
139
|
-
encoded: [
|
|
140
|
-
0, 49, 50, 51, 65, 66, 67, 97, 98, 99, 194, 128, 195, 191, 196, 128, 225, 128, 128, 239,
|
|
141
|
-
191, 189, 240, 144, 128, 128, 244, 143, 191, 191,
|
|
142
|
-
],
|
|
143
|
-
},
|
|
144
|
-
];
|
|
145
|
-
|
|
146
|
-
cases.forEach((c) => {
|
|
147
|
-
it(`should correctly stream decode ${c.encoding}`, () => {
|
|
148
|
-
for (let len = 1; len <= 5; ++len) {
|
|
149
|
-
let out = '';
|
|
150
|
-
const decoder = new TextDecoder(c.encoding);
|
|
151
|
-
for (let i = 0; i < c.encoded.length; i += len) {
|
|
152
|
-
const sub = c.encoded.slice(i, i + len);
|
|
153
|
-
out += decoder.decode(new Uint8Array(sub), { stream: true });
|
|
154
|
-
}
|
|
155
|
-
out += decoder.decode();
|
|
156
|
-
expect(out).toBe(string);
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L218
|
|
163
|
-
describe('Supersets of ASCII decode ASCII correctly', () => {
|
|
164
|
-
it.each(['utf-8'])('should decode ASCII correctly for encoding: %s', (encoding) => {
|
|
165
|
-
let string = '';
|
|
166
|
-
const bytes: number[] = [];
|
|
167
|
-
for (let i = 0; i < 128; ++i) {
|
|
168
|
-
// Encodings that have escape codes in 0x00-0x7F
|
|
169
|
-
if (encoding === 'iso-2022-jp' && (i === 0x0e || i === 0x0f || i === 0x1b)) {
|
|
170
|
-
continue;
|
|
171
|
-
}
|
|
172
|
-
string += String.fromCharCode(i);
|
|
173
|
-
bytes.push(i);
|
|
174
|
-
}
|
|
175
|
-
const ascii_encoded = new TextEncoder().encode(string);
|
|
176
|
-
expect(new TextDecoder(encoding).decode(ascii_encoded)).toBe(string);
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L236
|
|
181
|
-
describe('Non-fatal errors at EOF', () => {
|
|
182
|
-
it('should throw TypeError for fatal utf-8', () => {
|
|
183
|
-
expect(() =>
|
|
184
|
-
new TextDecoder('utf-8', { fatal: true }).decode(new Uint8Array([0xff]))
|
|
185
|
-
).toThrow(TypeError);
|
|
186
|
-
new TextDecoder('utf-8').decode(new Uint8Array([0xff]));
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L257
|
|
191
|
-
|
|
192
|
-
describe('Replacement encoding labels', () => {
|
|
193
|
-
const replacementEncodings = [
|
|
194
|
-
'csiso2022kr',
|
|
195
|
-
'hz-gb-2312',
|
|
196
|
-
'iso-2022-cn',
|
|
197
|
-
'iso-2022-cn-ext',
|
|
198
|
-
'iso-2022-kr',
|
|
199
|
-
];
|
|
200
|
-
|
|
201
|
-
it.each(replacementEncodings)('should handle replacement encoding %s correctly', (encoding) => {
|
|
202
|
-
expect(
|
|
203
|
-
new TextEncoder(
|
|
204
|
-
// @ts-expect-error
|
|
205
|
-
encoding
|
|
206
|
-
).encoding
|
|
207
|
-
).toBe('utf-8');
|
|
208
|
-
|
|
209
|
-
expect(() => new TextDecoder(encoding, { fatal: true })).toThrow(RangeError);
|
|
210
|
-
|
|
211
|
-
expect(() => new TextDecoder(encoding, { fatal: false })).toThrow(RangeError);
|
|
212
|
-
});
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L280
|
|
216
|
-
describe('ArrayBuffer, ArrayBufferView and buffer offsets', () => {
|
|
217
|
-
it('should decode correctly from various buffer views', () => {
|
|
218
|
-
const decoder = new TextDecoder();
|
|
219
|
-
const bytes = [65, 66, 97, 98, 99, 100, 101, 102, 103, 104, 67, 68, 69, 70, 71, 72];
|
|
220
|
-
const chars = 'ABabcdefghCDEFGH';
|
|
221
|
-
const buffer = new Uint8Array(bytes).buffer;
|
|
222
|
-
|
|
223
|
-
expect(decoder.decode(buffer)).toBe(chars);
|
|
224
|
-
|
|
225
|
-
const types = [
|
|
226
|
-
'Uint8Array',
|
|
227
|
-
'Int8Array',
|
|
228
|
-
'Uint8ClampedArray',
|
|
229
|
-
'Uint16Array',
|
|
230
|
-
'Int16Array',
|
|
231
|
-
'Uint32Array',
|
|
232
|
-
'Int32Array',
|
|
233
|
-
'Float32Array',
|
|
234
|
-
'Float64Array',
|
|
235
|
-
] as const;
|
|
236
|
-
|
|
237
|
-
types.forEach((typeName) => {
|
|
238
|
-
const TypeConstructor = globalThis[typeName];
|
|
239
|
-
const array = new TypeConstructor(buffer);
|
|
240
|
-
|
|
241
|
-
expect(decoder.decode(array)).toBe(chars);
|
|
242
|
-
|
|
243
|
-
const subset = new TypeConstructor(
|
|
244
|
-
buffer,
|
|
245
|
-
TypeConstructor.BYTES_PER_ELEMENT,
|
|
246
|
-
8 / TypeConstructor.BYTES_PER_ELEMENT
|
|
247
|
-
);
|
|
248
|
-
expect(decoder.decode(subset)).toBe(
|
|
249
|
-
chars.substring(TypeConstructor.BYTES_PER_ELEMENT, TypeConstructor.BYTES_PER_ELEMENT + 8)
|
|
250
|
-
);
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
expect(decoder.decode(new DataView(buffer, 2, 8))).toBe(chars.substring(2, 10));
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L305
|
|
258
|
-
describe('Invalid parameters', () => {
|
|
259
|
-
it('should throw errors for invalid parameters', () => {
|
|
260
|
-
expect(
|
|
261
|
-
() =>
|
|
262
|
-
// @ts-expect-error
|
|
263
|
-
new TextDecoder(null)
|
|
264
|
-
).toThrow(RangeError);
|
|
265
|
-
|
|
266
|
-
expect(
|
|
267
|
-
() =>
|
|
268
|
-
// @ts-expect-error
|
|
269
|
-
new TextDecoder('utf-8', '')
|
|
270
|
-
).toThrow(TypeError);
|
|
271
|
-
|
|
272
|
-
expect(() =>
|
|
273
|
-
// @ts-expect-error
|
|
274
|
-
new TextDecoder('utf-8').decode(null, '')
|
|
275
|
-
).toThrow(TypeError);
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
it('accepts callable options dictionaries', () => {
|
|
280
|
-
const options = () => {};
|
|
281
|
-
|
|
282
|
-
// @ts-expect-error Web IDL dictionaries accept callable objects
|
|
283
|
-
expect(new TextDecoder('utf-8', options).fatal).toBe(false);
|
|
284
|
-
// @ts-expect-error Web IDL dictionaries accept callable objects
|
|
285
|
-
expect(new TextDecoder().decode(new Uint8Array([0x61]), options)).toBe('a');
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
// https://github.com/inexorabletash/text-encoding/blob/3f330964c0e97e1ed344c2a3e963f4598610a7ad/test/test-misc.js#L383
|
|
289
|
-
it('encode() called with falsy arguments (polyfill bindings)', () => {
|
|
290
|
-
const encoder = new TextEncoder();
|
|
291
|
-
// @ts-expect-error
|
|
292
|
-
expect([...encoder.encode(false)]).toEqual([102, 97, 108, 115, 101]);
|
|
293
|
-
// @ts-expect-error
|
|
294
|
-
expect([...encoder.encode(0)]).toEqual([48]);
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
describe('indexed UTF-8 decoder', () => {
|
|
298
|
-
it('decodes valid sequences at each UTF-8 boundary', () => {
|
|
299
|
-
const bytes = new Uint8Array([
|
|
300
|
-
0x00, 0x7f, 0xc2, 0x80, 0xdf, 0xbf, 0xe0, 0xa0, 0x80, 0xed, 0x9f, 0xbf, 0xee, 0x80, 0x80,
|
|
301
|
-
0xf0, 0x90, 0x80, 0x80, 0xf4, 0x8f, 0xbf, 0xbf,
|
|
302
|
-
]);
|
|
303
|
-
|
|
304
|
-
expect(new TextDecoder().decode(bytes)).toBe(
|
|
305
|
-
'\u0000\u007f\u0080\u07ff\u0800\ud7ff\ue000\u{10000}\u{10ffff}'
|
|
306
|
-
);
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
it('reads the stream option once for malformed input', () => {
|
|
310
|
-
let reads = 0;
|
|
311
|
-
const options = {
|
|
312
|
-
get stream() {
|
|
313
|
-
reads++;
|
|
314
|
-
return false;
|
|
315
|
-
},
|
|
316
|
-
};
|
|
317
|
-
|
|
318
|
-
expect(new TextDecoder().decode(new Uint8Array([0xff]), options)).toBe('\ufffd');
|
|
319
|
-
expect(reads).toBe(1);
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
it('handles incomplete and malformed sequences across every chunk boundary', () => {
|
|
323
|
-
const NativeTextDecoder = require('node:util').TextDecoder;
|
|
324
|
-
const cases = [
|
|
325
|
-
[0xef, 0xbb, 0xbf, 0x61],
|
|
326
|
-
[0xc2, 0xa2, 0xe6, 0xb0, 0xb4, 0xf0, 0x9d, 0x84, 0x9e],
|
|
327
|
-
[0xe0, 0x80, 0x80, 0x61],
|
|
328
|
-
[0xed, 0xa0, 0x80, 0x61],
|
|
329
|
-
[0xf4, 0x90, 0x80, 0x80, 0x61],
|
|
330
|
-
[0xe1, 0x80, 0x41, 0xc2],
|
|
331
|
-
[0xff, 0xef, 0xbb, 0xbf],
|
|
332
|
-
[0x61, 0xef, 0xbb, 0xbf],
|
|
333
|
-
];
|
|
334
|
-
|
|
335
|
-
for (const input of cases) {
|
|
336
|
-
for (let split = 0; split <= input.length; split++) {
|
|
337
|
-
const decoder = new TextDecoder();
|
|
338
|
-
const nativeDecoder = new NativeTextDecoder();
|
|
339
|
-
const first = new Uint8Array(input.slice(0, split));
|
|
340
|
-
const second = new Uint8Array(input.slice(split));
|
|
341
|
-
const actual =
|
|
342
|
-
decoder.decode(first, { stream: true }) +
|
|
343
|
-
decoder.decode(second, { stream: true }) +
|
|
344
|
-
decoder.decode();
|
|
345
|
-
const expected =
|
|
346
|
-
nativeDecoder.decode(first, { stream: true }) +
|
|
347
|
-
nativeDecoder.decode(second, { stream: true }) +
|
|
348
|
-
nativeDecoder.decode();
|
|
349
|
-
|
|
350
|
-
expect(actual).toBe(expected);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
it('decodes large ASCII and multi-byte inputs', () => {
|
|
356
|
-
const value = 'a'.repeat(9000) + '水𝄞'.repeat(9000);
|
|
357
|
-
const encoded = new TextEncoder().encode(value);
|
|
358
|
-
|
|
359
|
-
expect(new TextDecoder().decode(encoded)).toBe(value);
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
it.each([0, 1, 31, 32, 33, 4095, 4096, 4097, 8193])(
|
|
363
|
-
'decodes %i ASCII bytes across conversion boundaries',
|
|
364
|
-
(length) => {
|
|
365
|
-
const bytes = new Uint8Array(length);
|
|
366
|
-
for (let i = 0; i < length; i++) bytes[i] = 32 + (i % 95);
|
|
367
|
-
|
|
368
|
-
const NativeTextDecoder = require('node:util').TextDecoder;
|
|
369
|
-
expect(new TextDecoder().decode(bytes)).toBe(new NativeTextDecoder().decode(bytes));
|
|
370
|
-
}
|
|
371
|
-
);
|
|
372
|
-
|
|
373
|
-
it('agrees with the native decoder for every two-byte input', () => {
|
|
374
|
-
const NativeTextDecoder = require('node:util').TextDecoder;
|
|
375
|
-
const bytes = new Uint8Array(2);
|
|
376
|
-
for (let first = 0; first <= 0xff; first++) {
|
|
377
|
-
bytes[0] = first;
|
|
378
|
-
for (let second = 0; second <= 0xff; second++) {
|
|
379
|
-
bytes[1] = second;
|
|
380
|
-
expect(new TextDecoder().decode(bytes)).toBe(new NativeTextDecoder().decode(bytes));
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
it('agrees with the native decoder for malformed inputs and all streaming splits', () => {
|
|
386
|
-
const NativeTextDecoder = require('node:util').TextDecoder;
|
|
387
|
-
let random = 0x12345678;
|
|
388
|
-
const nextByte = () => {
|
|
389
|
-
random = (Math.imul(random, 1664525) + 1013904223) >>> 0;
|
|
390
|
-
return random >>> 24;
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
for (let test = 0; test < 2000; test++) {
|
|
394
|
-
const input = new Uint8Array(test % 9);
|
|
395
|
-
for (let i = 0; i < input.length; i++) input[i] = nextByte();
|
|
396
|
-
expect(new TextDecoder().decode(input)).toBe(new NativeTextDecoder().decode(input));
|
|
397
|
-
|
|
398
|
-
for (let split = 0; split <= input.length; split++) {
|
|
399
|
-
const decoder = new TextDecoder();
|
|
400
|
-
const nativeDecoder = new NativeTextDecoder();
|
|
401
|
-
const actual =
|
|
402
|
-
decoder.decode(input.subarray(0, split), { stream: true }) +
|
|
403
|
-
decoder.decode(input.subarray(split), { stream: true }) +
|
|
404
|
-
decoder.decode();
|
|
405
|
-
const expected =
|
|
406
|
-
nativeDecoder.decode(input.subarray(0, split), { stream: true }) +
|
|
407
|
-
nativeDecoder.decode(input.subarray(split), { stream: true }) +
|
|
408
|
-
nativeDecoder.decode();
|
|
409
|
-
expect(actual).toBe(expected);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
it('starts a fresh BOM session after a completed decode', () => {
|
|
415
|
-
const decoder = new TextDecoder();
|
|
416
|
-
expect(decoder.decode(new Uint8Array([0x61]))).toBe('a');
|
|
417
|
-
expect(decoder.decode(new Uint8Array([0xef, 0xbb, 0xbf, 0x62]))).toBe('b');
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
it('recovers cleanly after a fatal streaming error', () => {
|
|
421
|
-
const decoder = new TextDecoder('utf-8', { fatal: true });
|
|
422
|
-
expect(decoder.decode(new Uint8Array([0xe2]), { stream: true })).toBe('');
|
|
423
|
-
expect(() => decoder.decode(new Uint8Array([0x41]), { stream: true })).toThrow(TypeError);
|
|
424
|
-
expect(decoder.decode(new Uint8Array([0x42]))).toBe('B');
|
|
425
|
-
});
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
// https://github.com/inexorabletash/text-encoding/blob/master/test/test-utf.js
|
|
429
|
-
describe('UTF-8 - Encode/Decode - reference sample', () => {
|
|
430
|
-
// z, cent, CJK water, G-Clef, Private-use character
|
|
431
|
-
const sample = 'z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD';
|
|
432
|
-
const cases = [
|
|
433
|
-
{
|
|
434
|
-
encoding: 'utf-8',
|
|
435
|
-
expected: [
|
|
436
|
-
0x7a, 0xc2, 0xa2, 0xe6, 0xb0, 0xb4, 0xf0, 0x9d, 0x84, 0x9e, 0xf4, 0x8f, 0xbf, 0xbd,
|
|
437
|
-
],
|
|
438
|
-
},
|
|
439
|
-
];
|
|
440
|
-
|
|
441
|
-
cases.forEach(function (t) {
|
|
442
|
-
it('expected equal decodings - ' + t.encoding, () => {
|
|
443
|
-
const decoded = new TextDecoder(t.encoding).decode(new Uint8Array(t.expected));
|
|
444
|
-
|
|
445
|
-
expect(decoded).toBe(sample);
|
|
446
|
-
});
|
|
447
|
-
});
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
it('UTF-8 - Encode/Decode - full roundtrip and agreement with encode/decodeURIComponent', () => {
|
|
451
|
-
function assertStringEquals(actual: string, expected: string, description: string) {
|
|
452
|
-
// short circuit success case
|
|
453
|
-
if (actual === expected) {
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
// length check
|
|
458
|
-
expect(actual.length).toBe(expected.length);
|
|
459
|
-
|
|
460
|
-
for (let i = 0; i < actual.length; i++) {
|
|
461
|
-
const a = actual.charCodeAt(i);
|
|
462
|
-
const b = expected.charCodeAt(i);
|
|
463
|
-
if (a !== b) {
|
|
464
|
-
throw new Error(
|
|
465
|
-
description +
|
|
466
|
-
': code unit ' +
|
|
467
|
-
i.toString() +
|
|
468
|
-
' unequal: ' +
|
|
469
|
-
cpname(a) +
|
|
470
|
-
' != ' +
|
|
471
|
-
cpname(b)
|
|
472
|
-
);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
// It should be impossible to get here, because the initial
|
|
477
|
-
// comparison failed, so either the length comparison or the
|
|
478
|
-
// codeunit-by-codeunit comparison should also fail.
|
|
479
|
-
throw new Error(description + ': failed to detect string difference');
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
// Inspired by:
|
|
483
|
-
// http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
|
|
484
|
-
function encodeUtf8(string: string): Uint8Array {
|
|
485
|
-
const utf8 = unescape(encodeURIComponent(string));
|
|
486
|
-
const octets = new Uint8Array(utf8.length);
|
|
487
|
-
for (let i = 0; i < utf8.length; i += 1) {
|
|
488
|
-
octets[i] = utf8.charCodeAt(i);
|
|
489
|
-
}
|
|
490
|
-
return octets;
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
function decodeUtf8(octets: number[]): string {
|
|
494
|
-
const utf8 = String.fromCharCode.apply(null, octets);
|
|
495
|
-
return decodeURIComponent(escape(utf8));
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
// Helpers for test_utf_roundtrip.
|
|
499
|
-
function cpname(n: number): string {
|
|
500
|
-
if (n + 0 !== n) return n.toString();
|
|
501
|
-
const w = n <= 0xffff ? 4 : 6;
|
|
502
|
-
return 'U+' + ('000000' + n.toString(16).toUpperCase()).slice(-w);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
function genblock(from: number, len: number, skip: number) {
|
|
506
|
-
let block = '';
|
|
507
|
-
for (let i = 0; i < len; i += skip) {
|
|
508
|
-
let cp = from + i;
|
|
509
|
-
if (0xd800 <= cp && cp <= 0xdfff) continue;
|
|
510
|
-
if (cp < 0x10000) {
|
|
511
|
-
block += String.fromCharCode(cp);
|
|
512
|
-
continue;
|
|
513
|
-
}
|
|
514
|
-
cp = cp - 0x10000;
|
|
515
|
-
block += String.fromCharCode(0xd800 + (cp >> 10));
|
|
516
|
-
block += String.fromCharCode(0xdc00 + (cp & 0x3ff));
|
|
517
|
-
}
|
|
518
|
-
return block;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
const MIN_CODEPOINT = 0;
|
|
522
|
-
const MAX_CODEPOINT = 0x10ffff;
|
|
523
|
-
const BLOCK_SIZE = 0x1000;
|
|
524
|
-
const SKIP_SIZE = 31;
|
|
525
|
-
|
|
526
|
-
const TE_U8 = new TextEncoder();
|
|
527
|
-
const TD_U8 = new TextDecoder('UTF-8');
|
|
528
|
-
|
|
529
|
-
for (let i = MIN_CODEPOINT; i < MAX_CODEPOINT; i += BLOCK_SIZE) {
|
|
530
|
-
const block_tag = cpname(i) + ' - ' + cpname(i + BLOCK_SIZE - 1);
|
|
531
|
-
const block = genblock(i, BLOCK_SIZE, SKIP_SIZE);
|
|
532
|
-
|
|
533
|
-
// test UTF-8 encodings against themselves
|
|
534
|
-
|
|
535
|
-
const encoded = TE_U8.encode(block);
|
|
536
|
-
const decoded = TD_U8.decode(encoded);
|
|
537
|
-
assertStringEquals(block, decoded, 'UTF-8 round trip ' + block_tag);
|
|
538
|
-
|
|
539
|
-
// test TextEncoder(UTF-8) against the older idiom
|
|
540
|
-
const expEncoded = encodeUtf8(block);
|
|
541
|
-
|
|
542
|
-
expect(encoded.length).toBe(expEncoded.length);
|
|
543
|
-
// assert_array_equals(encoded, expEncoded, 'UTF-8 reference encoding ' + block_tag);
|
|
544
|
-
|
|
545
|
-
const expDecoded = decodeUtf8([...expEncoded]);
|
|
546
|
-
assertStringEquals(decoded, expDecoded, 'UTF-8 reference decoding ' + block_tag);
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
});
|