diff --git a/packages/candid/src/idl.test.ts b/packages/candid/src/idl.test.ts index cc270a384..410e3d2e5 100644 --- a/packages/candid/src/idl.test.ts +++ b/packages/candid/src/idl.test.ts @@ -160,6 +160,7 @@ test('IDL encoding (array)', () => { '4449444c016d7c01000400010203', 'Array of Ints', ); + IDL.encode([IDL.Vec(IDL.Nat16)], [new Array(200000).fill(42)]); expect(() => IDL.encode([IDL.Vec(IDL.Int)], [BigInt(0)])).toThrow(/Invalid vec int argument/); expect(() => IDL.encode([IDL.Vec(IDL.Int)], [['fail']])).toThrow(/Invalid vec int argument/); }); diff --git a/packages/candid/src/idl.ts b/packages/candid/src/idl.ts index f340e6bd4..13f9d09a9 100644 --- a/packages/candid/src/idl.ts +++ b/packages/candid/src/idl.ts @@ -750,8 +750,13 @@ export class VecClass extends ConstructType { if (this._blobOptimization) { return concat(len, new Uint8Array(x as unknown as number[])); } - - return concat(len, ...x.map(d => this._type.encodeValue(d))); + const buf = new Pipe(new ArrayBuffer(len.byteLength + x.length), 0); + buf.write(len); + for (const d of x) { + const encoded = this._type.encodeValue(d); + buf.write(new Uint8Array(encoded)); + } + return buf.buffer; } public _buildTypeTableImpl(typeTable: TypeTable) {