Skip to content

Commit ba0c724

Browse files
committed
fix: handle big integers in incoming events
An event may have data that contains a BigInt. The builtin `JSON` parser for JavaScript does not handle the `BigInt` types. The introduced `json-bigint` dependency (34k) does. Fixes: #489 Signed-off-by: Lance Ball <[email protected]>
1 parent d9ee0e0 commit ba0c724

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

package-lock.json

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"dependencies": {
113113
"ajv": "^8.6.3",
114114
"ajv-formats": "^2.1.1",
115+
"json-bigint": "^1.0.0",
115116
"util": "^0.12.4",
116117
"uuid": "^8.3.2"
117118
},
@@ -120,6 +121,7 @@
120121
"@types/chai": "^4.2.11",
121122
"@types/cucumber": "^6.0.1",
122123
"@types/got": "^9.6.11",
124+
"@types/json-bigint": "^1.0.1",
123125
"@types/mocha": "^7.0.2",
124126
"@types/node": "^14.14.10",
125127
"@types/superagent": "^4.1.10",

src/message/http/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { types } from "util";
7+
import JSON from "json-bigint";
78

89
import { CloudEvent, CloudEventV1, CONSTANTS, Mode, Version } from "../..";
910
import { Message, Headers, Binding } from "..";

src/parsers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import JSON from "json-bigint";
67
import CONSTANTS from "./constants";
78
import { isString, isDefinedOrThrow, isStringOrObjectOrThrow, ValidationError } from "./event/validation";
89

test/integration/message_test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ const imageData = new Uint32Array(fs.readFileSync(path.join(process.cwd(), "test
4141
const image_base64 = asBase64(imageData);
4242

4343
describe("HTTP transport", () => {
44+
it("Handles big integers in structured mode", () => {
45+
const ce = HTTP.toEvent({
46+
headers: { "content-type": "application/cloudevents+json" },
47+
body: `{"data": 1524831183200260097}`
48+
}) as CloudEvent;
49+
expect(String(ce.data)).to.equal(String(1524831183200260097n));
50+
});
51+
52+
it("Handles big integers in binary mode", () => {
53+
const ce = HTTP.toEvent({
54+
headers: { "content-type": "application/json", "ce-id": "1234" },
55+
body: `{"data": 1524831183200260097}`
56+
}) as CloudEvent<Record<string, never>>;
57+
console.error(ce.data);
58+
expect(String((ce.data as Record<string, never>).data)).to.equal(String(1524831183200260097n));
59+
});
60+
4461
it("Handles events with no content-type and no datacontenttype", () => {
4562
const body = "{Something[Not:valid}JSON";
4663
const message: Message<undefined> = {

test/integration/parser_test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ describe("JSON Event Format Parser", () => {
5656
const parser = new Parser();
5757

5858
// TODO: Should the parser catch the SyntaxError and re-throw a ValidationError?
59-
expect(parser.parse.bind(parser, payload)).to.throw(SyntaxError, "Unexpected token g in JSON at position 1");
59+
try {
60+
parser.parse(payload);
61+
} catch (error: any) {
62+
expect(error.message).to.equal("Bad string");
63+
}
6064
});
6165

6266
it("Accepts a string as valid JSON", () => {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
3+
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
44
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
55
"allowJs": true, /* Allow javascript files to be compiled. */
66
"checkJs": false, /* Report errors in .js files. */

0 commit comments

Comments
 (0)