Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't use spread operator when encoding vec #561

Merged
merged 3 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/candid/src/idl.test.ts
Expand Up @@ -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/);
});
Expand Down
9 changes: 7 additions & 2 deletions packages/candid/src/idl.ts
Expand Up @@ -750,8 +750,13 @@ export class VecClass<T> extends ConstructType<T[]> {
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) {
Expand Down