Skip to content

Commit

Permalink
fix: handle big integers in incoming events
Browse files Browse the repository at this point in the history
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 <lball@redhat.com>
  • Loading branch information
lance committed Jun 14, 2022
1 parent d9ee0e0 commit ba0c724
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"dependencies": {
"ajv": "^8.6.3",
"ajv-formats": "^2.1.1",
"json-bigint": "^1.0.0",
"util": "^0.12.4",
"uuid": "^8.3.2"
},
Expand All @@ -120,6 +121,7 @@
"@types/chai": "^4.2.11",
"@types/cucumber": "^6.0.1",
"@types/got": "^9.6.11",
"@types/json-bigint": "^1.0.1",
"@types/mocha": "^7.0.2",
"@types/node": "^14.14.10",
"@types/superagent": "^4.1.10",
Expand Down
1 change: 1 addition & 0 deletions src/message/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { types } from "util";
import JSON from "json-bigint";

import { CloudEvent, CloudEventV1, CONSTANTS, Mode, Version } from "../..";
import { Message, Headers, Binding } from "..";
Expand Down
1 change: 1 addition & 0 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SPDX-License-Identifier: Apache-2.0
*/

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

Expand Down
17 changes: 17 additions & 0 deletions test/integration/message_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ const imageData = new Uint32Array(fs.readFileSync(path.join(process.cwd(), "test
const image_base64 = asBase64(imageData);

describe("HTTP transport", () => {
it("Handles big integers in structured mode", () => {
const ce = HTTP.toEvent({
headers: { "content-type": "application/cloudevents+json" },
body: `{"data": 1524831183200260097}`
}) as CloudEvent;
expect(String(ce.data)).to.equal(String(1524831183200260097n));
});

it("Handles big integers in binary mode", () => {
const ce = HTTP.toEvent({
headers: { "content-type": "application/json", "ce-id": "1234" },
body: `{"data": 1524831183200260097}`
}) as CloudEvent<Record<string, never>>;
console.error(ce.data);
expect(String((ce.data as Record<string, never>).data)).to.equal(String(1524831183200260097n));
});

it("Handles events with no content-type and no datacontenttype", () => {
const body = "{Something[Not:valid}JSON";
const message: Message<undefined> = {
Expand Down
6 changes: 5 additions & 1 deletion test/integration/parser_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ describe("JSON Event Format Parser", () => {
const parser = new Parser();

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

it("Accepts a string as valid JSON", () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES2016", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"allowJs": true, /* Allow javascript files to be compiled. */
"checkJs": false, /* Report errors in .js files. */
Expand Down

0 comments on commit ba0c724

Please sign in to comment.