Skip to content

Commit de80bb3

Browse files
committed
feat: Uint8Array<SharedArrayBuffer> is accepted in single-byte decode
1 parent 3ebd4de commit de80bb3

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

single-byte.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import type { Uint8ArrayBuffer } from './array.js';
6565
export function createSinglebyteDecoder(
6666
encoding: string,
6767
loose?: boolean
68-
): (arr: Uint8ArrayBuffer) => string;
68+
): (arr: Uint8Array) => string;
6969

7070
/**
7171
* Create an encoder for a supported one-byte `encoding`, given its lowercased name `encoding`.
@@ -101,7 +101,7 @@ export function createSinglebyteEncoder(
101101
* @param arr - The bytes to decode
102102
* @returns The decoded string
103103
*/
104-
export function latin1toString(arr: Uint8ArrayBuffer): string;
104+
export function latin1toString(arr: Uint8Array): string;
105105

106106
/**
107107
* Encode a string to `iso-8859-1` bytes.
@@ -131,7 +131,7 @@ export function latin1fromString(string: string): Uint8ArrayBuffer;
131131
* @param arr - The bytes to decode
132132
* @returns The decoded string
133133
*/
134-
export function windows1252toString(arr: Uint8ArrayBuffer): string;
134+
export function windows1252toString(arr: Uint8Array): string;
135135

136136
/**
137137
* Encode a string to `windows-1252` bytes.

tests/single-byte.test.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import { singleByte } from './encoding/fixtures/indexes.cjs'
99
const encodings = Object.keys(encodingsObject)
1010
const nonWhatwg = new Set(['iso-8859-1', 'iso-8859-9', 'iso-8859-11'])
1111

12+
const SharedArrayBuffer = globalThis.SharedArrayBuffer ?? ArrayBuffer
13+
const toShared = (u8) => {
14+
const res = new Uint8Array(new SharedArrayBuffer(u8.length))
15+
res.set(u8)
16+
return res
17+
}
18+
1219
// See also tests/encoding/single-byte.tables.test.js for similar TextDecoder tests
1320

1421
describe('single-byte encodings are supersets of ascii', () => {
@@ -26,7 +33,7 @@ describe('single-byte encodings are supersets of ascii', () => {
2633

2734
t.assert.strictEqual(str.length, 1, i)
2835
t.assert.strictEqual(str.codePointAt(0), i, i)
29-
36+
t.assert.strictEqual(decoder(toShared(Uint8Array.of(i))), str)
3037
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(i))
3138
}
3239
})
@@ -41,6 +48,7 @@ describe('single-byte encodings match fallback', () => {
4148
const fallback = encodingDecoder(encoding)
4249
for (let i = 0; i < 256; i++) {
4350
const u8 = Uint8Array.of(i)
51+
const s8 = toShared(u8)
4452
let found = false
4553
let str
4654
try {
@@ -52,14 +60,18 @@ describe('single-byte encodings match fallback', () => {
5260
t.assert.strictEqual(str.length, 1)
5361
t.assert.notEqual(str, '\uFFFD')
5462
t.assert.strictEqual(decoderLoose(u8), str)
63+
t.assert.strictEqual(decoder(s8), str)
64+
t.assert.strictEqual(decoderLoose(s8), str)
5565
t.assert.strictEqual(fallback(u8), str)
5666
t.assert.strictEqual(fallback(u8, true), str)
5767
} else {
5868
t.assert.ok(i >= 128)
69+
t.assert.throws(() => decoder(s8))
5970
t.assert.throws(() => fallback(u8))
6071
str = decoderLoose(u8)
6172
t.assert.strictEqual(str.length, 1)
6273
t.assert.strictEqual(str, '\uFFFD')
74+
t.assert.strictEqual(decoderLoose(s8), str)
6375
t.assert.strictEqual(fallback(u8, true), str)
6476
}
6577
}
@@ -125,6 +137,7 @@ describe('single-byte encodings index: Unicode', () => {
125137

126138
if (code === undefined) {
127139
t.assert.throws(() => decoder(Uint8Array.of(byte)))
140+
t.assert.throws(() => decoder(toShared(Uint8Array.of(byte))))
128141
try {
129142
str = decoderLoose(Uint8Array.of(byte))
130143
} catch (cause) {
@@ -133,6 +146,7 @@ describe('single-byte encodings index: Unicode', () => {
133146

134147
t.assert.strictEqual(str.length, 1)
135148
t.assert.strictEqual(str.codePointAt(0), 0xff_fd)
149+
t.assert.strictEqual(decoderLoose(toShared(Uint8Array.of(byte))), str)
136150
} else {
137151
try {
138152
str = decoder(Uint8Array.of(byte))
@@ -142,8 +156,9 @@ describe('single-byte encodings index: Unicode', () => {
142156

143157
t.assert.strictEqual(str.length, 1, byte)
144158
t.assert.strictEqual(str.codePointAt(0), code, byte)
145-
t.assert.strictEqual(str, decoderLoose(Uint8Array.of(byte)))
146-
159+
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), str)
160+
t.assert.strictEqual(decoder(toShared(Uint8Array.of(byte))), str)
161+
t.assert.strictEqual(decoderLoose(toShared(Uint8Array.of(byte))), str)
147162
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(byte))
148163
}
149164
}
@@ -197,10 +212,12 @@ describe('single-byte encodings index: WHATWG', () => {
197212
t.assert.strictEqual(str.length, 1, row.description)
198213
t.assert.strictEqual(str.codePointAt(0), row.code, row.description)
199214
t.assert.strictEqual(str, decoderLoose(Uint8Array.of(byte)))
200-
215+
t.assert.strictEqual(decoder(toShared(Uint8Array.of(byte))), str)
216+
t.assert.strictEqual(decoderLoose(toShared(Uint8Array.of(byte))), str)
201217
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(byte))
202218
} else {
203219
t.assert.throws(() => decoder(Uint8Array.of(byte)))
220+
t.assert.throws(() => decoder(toShared(Uint8Array.of(byte))))
204221
try {
205222
str = decoderLoose(Uint8Array.of(byte))
206223
} catch (cause) {
@@ -209,6 +226,7 @@ describe('single-byte encodings index: WHATWG', () => {
209226

210227
t.assert.strictEqual(str.length, 1)
211228
t.assert.strictEqual(str.codePointAt(0), 0xff_fd)
229+
t.assert.strictEqual(decoderLoose(toShared(Uint8Array.of(byte))), str)
212230
}
213231
}
214232
})
@@ -243,10 +261,14 @@ describe('single-byte encodings index: WHATWG non-normative indexes.json', () =>
243261
t.assert.ok(data[i] <= 0xff_ff)
244262
t.assert.strictEqual(decoder(Uint8Array.of(byte)), str)
245263
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), str)
264+
t.assert.strictEqual(decoder(toShared(Uint8Array.of(byte))), str)
265+
t.assert.strictEqual(decoderLoose(toShared(Uint8Array.of(byte))), str)
246266
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(byte))
247267
} else {
248268
t.assert.throws(() => decoder(Uint8Array.of(byte)))
269+
t.assert.throws(() => decoder(toShared(Uint8Array.of(byte))))
249270
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), '\uFFFD')
271+
t.assert.strictEqual(decoderLoose(toShared(Uint8Array.of(byte))), '\uFFFD')
250272
}
251273
}
252274
})
@@ -263,6 +285,8 @@ describe('x-user-defined', () => {
263285
const str = String.fromCodePoint(byte >= 0x80 ? 0xf7_80 + byte - 0x80 : byte)
264286
t.assert.strictEqual(decoder(Uint8Array.of(byte)), str, byte)
265287
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), str, byte)
288+
t.assert.strictEqual(decoder(toShared(Uint8Array.of(byte))), str, byte)
289+
t.assert.strictEqual(decoderLoose(toShared(Uint8Array.of(byte))), str, byte)
266290
}
267291
})
268292

0 commit comments

Comments
 (0)