Skip to content

Commit

Permalink
Preserve 'constructor' keyword in json-bigint and add console.warn on…
Browse files Browse the repository at this point in the history
… decode error (#1109)

* WIP

* Set constructorAction: 'preserve' and use JSONBigNative const to get options

* Remove commented out code

* Add unit test

* Fix lint issue and unit test
  • Loading branch information
Alex-Tideman authored Feb 3, 2023
1 parent 0e61d87 commit e2b15bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/lib/utilities/decode-payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ const JsonObjectEncoded = {
data: 'eyAiVHJhbnNmb3JtZXIiOiAiT3B0aW11c1ByaW1lIiB9',
};

const JsonObjectEncodedWithConstructor = {
metadata: {
encoding: 'anNvbi9wbGFpbg==',
type: 'S2V5d29yZA==',
},
data: 'eyAiQ29uc3RydWN0b3JPdXRwdXQiOiAiT3B0aW11c1ByaW1lIiB9',
};

const JsonObjectDecoded = { Transformer: 'OptimusPrime' };
const JsonObjectDecodedWithConstructor = { ConstructorOutput: 'OptimusPrime' };

describe('decodePayload', () => {
it('Should not decode a payload with encoding binary/encrypted', () => {
Expand All @@ -106,6 +115,11 @@ describe('decodePayload', () => {
it('Should decode a json payload with encoding json/plain', () => {
expect(decodePayload(JsonObjectEncoded)).toEqual(JsonObjectDecoded);
});
it('Should decode a json payload with constructor keyword with encoding json/plain', () => {
expect(decodePayload(JsonObjectEncodedWithConstructor)).toEqual(
JsonObjectDecodedWithConstructor,
);
});
});

describe('decode all potential payloads', () => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/utilities/decode-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function decodePayload(
try {
return parseWithBigInt(atob(String(payload?.data ?? '')));
} catch (_e) {
console.warn('Could not parse payload: ', _e);
// Couldn't correctly decode this just let the user deal with the data as is
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/lib/utilities/parse-with-big-int.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import JSONbig from 'json-bigint';

JSONbig({
const JSONBigNative = JSONbig({
useNativeBigInt: true,
constructorAction: 'preserve',
});

export const parseWithBigInt = (content: string) => JSONbig.parse(content);
export const parseWithBigInt = (content: string) =>
JSONBigNative.parse(content);

export const stringifyWithBigInt = <T = unknown>(
value: T,
replacer?: (key: string, value: T) => T,
space?: string | number,
) => JSONbig.stringify(value, replacer, space);
) => JSONBigNative.stringify(value, replacer, space);

2 comments on commit e2b15bd

@vercel
Copy link

@vercel vercel bot commented on e2b15bd Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

holocene – ./

holocene.preview.thundergun.io
holocene-git-main.preview.thundergun.io

@vercel
Copy link

@vercel vercel bot commented on e2b15bd Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-lyart.vercel.app
ui-git-main.preview.thundergun.io
ui.preview.thundergun.io

Please sign in to comment.