Skip to content

Commit

Permalink
chore: Bump build node version and refactor json bigint serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Feb 21, 2025
1 parent 524ee6d commit 6f033fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: 'Build @algorandfoundation/algorand-typescript'
uses: ./.github/workflows/node-ci.yml
with:
node-version: 20.x
node-version: 21.x
run-build: true
audit-script: npm run audit
working-directory: packages/algo-ts
Expand All @@ -37,7 +37,7 @@ jobs:
download-artifact-pattern: algo-*
download-artifact-path: ./packages-temp
pre-run-script: mv packages-temp/algo-ts packages/algo-ts/dist
node-version: 20.x
node-version: 21.x
run-build: true
run-commit-lint: ${{ inputs.run-commit-lint }}
build-script: npm run build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ jobs:
with:
fetch-depth: 0

- name: Use Node.js 20.x
- name: Use Node.js 21.x
uses: actions/setup-node@v4
with:
node-version: 20.x
node-version: 21.x

- run: npm ci --ignore-scripts

Expand Down
43 changes: 20 additions & 23 deletions src/awst/json-serialize-awst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ type JSONWithRaw = typeof JSON & {
rawJSON?(value: string): string
}

function serializeBigInt(value: bigint): unknown {
const jsonWithRaw = JSON as unknown as JSONWithRaw
if (jsonWithRaw.rawJSON) {
return jsonWithRaw.rawJSON(`${value}`)
}
if (value < 0n) {
if (value < Number.MIN_SAFE_INTEGER) {
throw new InternalError(`Cannot safely serialize ${value} to JSON`)
}
return Number(value)
} else {
if (value > Number.MAX_SAFE_INTEGER) {
return `${value}`
}
return Number(value)
}
}

export class SnakeCaseSerializer<T> {
constructor(private readonly spaces = 2) {}
public serialize(obj: T): string {
Expand All @@ -29,21 +47,7 @@ export class SnakeCaseSerializer<T> {

protected serializerFunction(key: string, value: unknown): unknown {
if (typeof value === 'bigint') {
const jsonWithRaw = JSON as unknown as JSONWithRaw
if (jsonWithRaw.rawJSON) {
return jsonWithRaw.rawJSON(`${value}`)
}
if (value < 0n) {
if (value < Number.MIN_SAFE_INTEGER) {
throw new InternalError(`Cannot safely serialize ${value} to JSON`)
}
return Number(value)
} else {
if (value > Number.MAX_SAFE_INTEGER) {
return `${value}`
}
return Number(value)
}
return serializeBigInt(value)
}
if (value instanceof Uint8Array) {
return this.b85.encode(value)
Expand Down Expand Up @@ -99,14 +103,7 @@ export class AwstSerializer extends SnakeCaseSerializer<RootNode[]> {
...(super.serializerFunction(key, value) as object),
immediates: value.immediates.map((i) => {
if (typeof i === 'bigint') {
if (i <= Number.MAX_SAFE_INTEGER) {
return Number(i)
} else {
throw new InternalError(
'Intrinsic call with integer immediate arg cannot be serialized as it is larger than Number.MAX_SAFE_INTEGER',
{ sourceLocation: value.sourceLocation },
)
}
return serializeBigInt(i)
}
return i
}),
Expand Down

0 comments on commit 6f033fd

Please sign in to comment.