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

wip: bigint decoding #765

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/agent/src/cbor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,43 @@ function buf2hex(buffer: Uint8Array) {
// join the elements.
return Array.prototype.map.call(buffer, x => ('00' + x.toString(16)).slice(-2)).join('');
}

describe('encode + decode numbers', () => {
it('should handle 0', () => {
expect(decode(encode(0))).toBe(0);
});
it('should handle 1', () => {
expect(decode(encode(1))).toBe(1);
});
it('should handle -1', () => {
expect(decode(encode(-1))).toBe(-1);
});
it('should handle 255', () => {
expect(decode(encode(255))).toBe(255);
});
it('should handle 256', () => {
expect(decode(encode(256))).toBe(256);
});
it('should handle 65535', () => {
expect(decode(encode(65535))).toBe(65535);
});
it('should handle 65536', () => {
expect(decode(encode(65536))).toBe(65536);
});
it('should handle 4294967295', () => {
expect(decode(encode(4294967295))).toBe(4294967295);
});
it('should handle 4294967296', () => {
expect(decode(encode(4294967296))).toBe(4294967296);
});
it('should handle 18446744073709551615n', () => {
expect(decode(encode(BigInt('18446744073709551615')))).toBe(BigInt('18446744073709551615'));
});
it('should encode 0n', () => {
expect(decode(encode(0n))).toBe(0n);
});
it('should encode 1n', () => {
// Is this valid?
expect(decode(encode(1n))).toBe(1);
});
});
25 changes: 21 additions & 4 deletions packages/agent/src/cbor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BufferEncoder implements CborEncoder<ArrayBuffer> {
}
}

class BigIntEncoder implements CborEncoder<BigInt> {
class BigIntEncoder implements CborEncoder<bigint> {
public get name() {
return 'BigInt';
}
Expand All @@ -64,13 +64,30 @@ class BigIntEncoder implements CborEncoder<BigInt> {
}

public encode(v: bigint): cbor.CborValue {
// Always use a bigint encoding.
if (v > BigInt(0)) {
// Use a bigint encoding for large values
if (v > BigInt(Number.MAX_SAFE_INTEGER)) {
return cbor.value.tagged(2, cbor.value.bytes(fromHex(v.toString(16))));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to fix the padding here to make it even length?

} else {
}
// If zero, use the zero encoding.
else if (v === BigInt(0)) {
return cbor.value.tagged(2, cbor.value.bytes(new Uint8Array([])));
}
// Use Number serialization for safe numbers
else if (v >= BigInt(-1 * Number.MAX_SAFE_INTEGER) && v <= BigInt(Number.MAX_SAFE_INTEGER)) {
const serialized = serializer.serialize(Number(v)) as cbor.CborValue;
// Add CBOR tag
Object.defineProperty(serialized, '__brand', { value: 'CBOR' });

return serialized;
} else if (v < BigInt(Number.MAX_SAFE_INTEGER)) {
return cbor.value.tagged(3, cbor.value.bytes(fromHex((BigInt('-1') * v).toString(16))));
} else {
this.#never();
}
}
#never(): never {
throw new Error('Should never happen');
}
}

const serializer = SelfDescribeCborSerializer.withDefaultEncoders(true);
Expand Down
Loading