Skip to content

Commit

Permalink
fix: console logger can serialize bigint values (rrweb-io#1403)
Browse files Browse the repository at this point in the history
* fix: console logger can serialize bigint values

* teach test jsdom is present

* add a changeset
  • Loading branch information
pauldambra authored and jxiwang committed Jul 31, 2024
1 parent 0c34ddd commit e9cfd9f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-mice-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb': patch
---

safely capture BigInt values with the console log plugin"
7 changes: 5 additions & 2 deletions packages/rrweb/src/plugins/console/record/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string, unknown> = {};
for (const eventKey in value) {
Expand Down Expand Up @@ -158,7 +161,7 @@ export function stringify(
return true;
}

// is function
// is function or bigint
if (typeof _obj === 'function') {
return true;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/rrweb/test/plugins/console/record.test.ts
Original file line number Diff line number Diff line change
@@ -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"');
});
});

0 comments on commit e9cfd9f

Please sign in to comment.