Skip to content

Commit 33f45f0

Browse files
committed
fix RangeError in encoding BLOB (resolve #65)
1 parent aed070f commit 33f45f0

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Encoder.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export class Encoder {
7979
this.writeU8(object);
8080
} else if (object < 0x100) {
8181
// uint 8
82-
this.writeU8v(0xcc, object);
82+
this.writeU8(0xcc);
83+
this.writeU8(object);
8384
} else if (object < 0x10000) {
8485
// uint 16
8586
this.writeU8(0xcd);
@@ -199,7 +200,7 @@ export class Encoder {
199200
throw new Error(`Too large binary: ${size}`);
200201
}
201202
const bytes = ensureUint8Array(object);
202-
this.writeU8v(...bytes);
203+
this.writeU8a(bytes);
203204
}
204205

205206
encodeArray(object: Array<unknown>, depth: number) {
@@ -290,7 +291,7 @@ export class Encoder {
290291
throw new Error(`Too large extension object: ${size}`);
291292
}
292293
this.writeI8(ext.type);
293-
this.writeU8v(...ext.data);
294+
this.writeU8a(ext.data);
294295
}
295296

296297
writeU8(value: number) {
@@ -300,6 +301,9 @@ export class Encoder {
300301
this.pos++;
301302
}
302303

304+
/**
305+
* @deprecated No longer used. Will be removed in a semver-major
306+
*/
303307
writeU8v(...values: Array<number>) {
304308
const size = values.length;
305309
this.ensureBufferSizeToWrite(size);
@@ -311,6 +315,14 @@ export class Encoder {
311315
this.pos += size;
312316
}
313317

318+
writeU8a(values: ArrayLike<number>) {
319+
const size = values.length;
320+
this.ensureBufferSizeToWrite(size);
321+
322+
this.bytes.set(values, this.pos);
323+
this.pos += size;
324+
}
325+
314326
writeI8(value: number) {
315327
this.ensureBufferSizeToWrite(1);
316328

test/msgpack-test-suite.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ describe("msgpack-test-suite", () => {
9696
STR_INCLUDING_NUL: "foo\0bar\0",
9797
STR_BROKEN_FF: "\xff",
9898
BIN16: new Uint8Array(0x100).fill(0xff),
99-
BIN32: new Uint8Array(0x10000).fill(0xff),
99+
BIN32: new Uint8Array(0x10_000).fill(0xff),
100+
BIN32LARGE: new Uint8Array(0x100_000).fill(0xff), // regression: caused "RangeError: Maximum call stack size exceeded"
100101
ARRAY16: new Array<boolean>(0x100).fill(true),
101102
ARRAY32: new Array<boolean>(0x10000).fill(true),
102103
MAP16: new Array<null>(0x100).fill(null).reduce<Record<string, number>>((acc, _val, i) => {

0 commit comments

Comments
 (0)