From af0962cc6c80b693bbc622520032d17342685cf6 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Thu, 25 Jan 2024 15:55:52 +0000 Subject: [PATCH] fix: console logger can serialize bigint values (#1403) * fix: console logger can serialize bigint values * teach test jsdom is present * add a changeset --- .changeset/breezy-mice-breathe.md | 5 +++++ .../rrweb/src/plugins/console/record/stringify.ts | 7 +++++-- packages/rrweb/test/plugins/console/record.test.ts | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .changeset/breezy-mice-breathe.md create mode 100644 packages/rrweb/test/plugins/console/record.test.ts diff --git a/.changeset/breezy-mice-breathe.md b/.changeset/breezy-mice-breathe.md new file mode 100644 index 0000000000..b3b564243b --- /dev/null +++ b/.changeset/breezy-mice-breathe.md @@ -0,0 +1,5 @@ +--- +'rrweb': patch +--- + +safely capture BigInt values with the console log plugin" diff --git a/packages/rrweb/src/plugins/console/record/stringify.ts b/packages/rrweb/src/plugins/console/record/stringify.ts index a767793e8f..00b7245e32 100644 --- a/packages/rrweb/src/plugins/console/record/stringify.ts +++ b/packages/rrweb/src/plugins/console/record/stringify.ts @@ -91,7 +91,7 @@ export function stringify( const keys: unknown[] = []; return JSON.stringify( obj, - function (key, value: string | object | null | undefined) { + function (key, value: string | bigint | object | null | undefined) { /** * forked from https://github.com/moll/json-stringify-safe/blob/master/stringify.js * to deCycle the object @@ -120,6 +120,9 @@ export function stringify( if (shouldIgnore(value as object)) { return toString(value as object); } + if (typeof value === 'bigint') { + return value.toString() + 'n'; + } if (value instanceof Event) { const eventResult: Record = {}; for (const eventKey in value) { @@ -158,7 +161,7 @@ export function stringify( return true; } - // is function + // is function or bigint if (typeof _obj === 'function') { return true; } diff --git a/packages/rrweb/test/plugins/console/record.test.ts b/packages/rrweb/test/plugins/console/record.test.ts new file mode 100644 index 0000000000..a864aaf178 --- /dev/null +++ b/packages/rrweb/test/plugins/console/record.test.ts @@ -0,0 +1,11 @@ +/** + * @jest-environment jsdom + */ + +import { stringify } from '../../../src/plugins/console/record/stringify'; + +describe('console record plugin', () => { + it('can stringify bigint', () => { + expect(stringify(BigInt(1))).toEqual('"1n"'); + }); +});