Skip to content

Commit

Permalink
CBOR: parse safe uint64 as number (#3481)
Browse files Browse the repository at this point in the history
* parse safe uint64 as number

* rename test
  • Loading branch information
dholms authored Jan 31, 2025
1 parent 7f52e67 commit 52c687a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/three-papayas-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/common": patch
---

Parse safe uint64 as number
8 changes: 7 additions & 1 deletion packages/common/src/ipld-multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ cborx.addExtension({
},
})

const decoder = new cborx.Decoder({
// @ts-ignore
int64AsNumber: true, // not in types for some reason
useRecords: false,
})

export const cborDecodeMulti = (encoded: Uint8Array): unknown[] => {
const decoded: unknown[] = []
cborx.decodeMultiple(encoded, (value) => {
decoder.decodeMultiple(encoded, (value) => {
decoded.push(value)
})
return decoded
Expand Down
9 changes: 9 additions & 0 deletions packages/common/tests/ipld-multi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ describe('ipld decode multi', () => {
expect(decoded[0]).toEqual(one)
expect(decoded[1]).toEqual(two)
})

it('parses safe ints as number', async () => {
const one = {
test: Number.MAX_SAFE_INTEGER,
}
const encoded = cborEncode(one)
const decoded = cborDecodeMulti(encoded)
expect(Number.isInteger(decoded[0]?.['test'])).toBe(true)
})
})

0 comments on commit 52c687a

Please sign in to comment.