diff --git a/src/lib/utilities/decode-payload.test.ts b/src/lib/utilities/decode-payload.test.ts index e8eb5e5ce..8f4a48cd4 100644 --- a/src/lib/utilities/decode-payload.test.ts +++ b/src/lib/utilities/decode-payload.test.ts @@ -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', () => { @@ -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', () => { diff --git a/src/lib/utilities/decode-payload.ts b/src/lib/utilities/decode-payload.ts index c1bbae43a..0b03bed96 100644 --- a/src/lib/utilities/decode-payload.ts +++ b/src/lib/utilities/decode-payload.ts @@ -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 } } diff --git a/src/lib/utilities/parse-with-big-int.ts b/src/lib/utilities/parse-with-big-int.ts index 2acef282d..8f0d43ee5 100644 --- a/src/lib/utilities/parse-with-big-int.ts +++ b/src/lib/utilities/parse-with-big-int.ts @@ -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 = ( value: T, replacer?: (key: string, value: T) => T, space?: string | number, -) => JSONbig.stringify(value, replacer, space); +) => JSONBigNative.stringify(value, replacer, space);