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

Use json-bigint for parsing server response to avoid truncation of large integers #333

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

janfh
Copy link
Contributor

@janfh janfh commented Feb 20, 2025

Fixes #324

JSON.parse will parse integers to Number (IEEE 754 64-bit float) and is limited to values less than what might be returned from the server. This will cause truncation of values less than -9007199254740991 and greater than 9007199254740991 (Number.MAX_SAFE_INTEGER).

This PR changes SQLJob to use json-bigint for parsing the server response. Values will be deserialized to BigInt instead of Number.

Before change:
image

After change:
image

@worksofliam
Copy link
Contributor

@janfh Out of curiosity, is a new package needed for this? Can't we use the built-in BigInt support?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility

@janfh
Copy link
Contributor Author

janfh commented Feb 20, 2025

@janfh Out of curiosity, is a new package needed for this? Can't we use the built-in BigInt support?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility

@worksofliam : I can try a different approach

@janfh
Copy link
Contributor Author

janfh commented Feb 21, 2025

Using BigInt causes other problems like JSON.stringify does not support serializing it.

It looks like this is working without the json-bigint package:

                let response: ServerResponse = JSON.parse(thisMsg, (_key, value, context) => {
                  if (typeof value === 'number' && ('' + value !== context.source)) {
                    return context.source;
                  }
                  return value;
                });

The context parameter on the reviver function is quite new, but it seems to work fine even if vscode complains about the syntax.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#browser_compatibility

@worksofliam : what do you think?

@worksofliam
Copy link
Contributor

@janfh Sadly Reviver has context parameter is not available until Node.js 21, and VS Code uses version 20 internally, so we will have to wait.

image

Personally, I think we should not merge this PR, and perhaps wait for VS Code to upgrade Node.js version instead of adding a temp work around. I do think an issue should be opened to track this, and have it fixed soon after the update.

What do you think?

@janfh
Copy link
Contributor Author

janfh commented Feb 21, 2025

Personally, I think we should not merge this PR, and perhaps wait for VS Code to upgrade Node.js version instead of adding a temp work around. I do think an issue should be opened to track this, and have it fixed soon after the update.

What do you think?

We can wait and see, and maybe there is another way of solving this. I guess it's kind of unfortunate that the current release will return wrong values without warning if it's big enough, but maybe it doesn't happen that often?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Big number and DB2 for i
2 participants