Skip to content

Commit

Permalink
Replace fast-safe-stringify with json-stringify-safe
Browse files Browse the repository at this point in the history
Fixes #1062
  • Loading branch information
mcollina committed Jul 11, 2021
1 parent 089c8fa commit 19f368e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/tools.js
Expand Up @@ -5,7 +5,7 @@
const format = require('quick-format-unescaped')
const { mapHttpRequest, mapHttpResponse } = require('pino-std-serializers')
const SonicBoom = require('sonic-boom')
const stringifySafe = require('fast-safe-stringify')
const stringifySafe = require('json-stringify-safe')
const {
lsCacheSym,
chindingsSym,
Expand Down Expand Up @@ -437,7 +437,11 @@ function stringify (obj) {
try {
return JSON.stringify(obj)
} catch (_) {
return stringifySafe(obj)
try {
return stringifySafe(obj)
} catch (_) {
return '"[unable to serialize, circular reference is too complex to analyze]"'
}
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -104,6 +104,7 @@
"on-exit-leak-free": "^0.1.0",
"pino-abstract-transport": "^0.2.0",
"pino-std-serializers": "^4.0.0",
"json-stringify-safe": "^5.0.1",
"quick-format-unescaped": "^4.0.3",
"sonic-boom": "^2.1.0",
"thread-stream": "^0.11.0"
Expand Down
9 changes: 4 additions & 5 deletions test/basic.test.js
Expand Up @@ -593,19 +593,18 @@ test('children with same names render in correct order', async ({ equal }) => {
equal(a, 3, 'last logged object takes precedence')
})

// https://github.com/pinojs/pino/pull/251 - use this.stringify
test('use `fast-safe-stringify` to avoid circular dependencies', async ({ same }) => {
test('use `json-stringify-safe` to avoid circular dependencies', async ({ same }) => {
const stream = sink()
const root = pino(stream)
// circular depth
const obj = {}
obj.a = obj
root.info(obj)
const { a } = await once(stream, 'data')
same(a, { a: '[Circular]' })
same(a, { a: '[Circular ~]' })
})

test('fast-safe-stringify must be used when interpolating', async (t) => {
test('json-stringify-safe must be used when interpolating', async (t) => {
const stream = sink()
const instance = pino(stream)

Expand All @@ -614,7 +613,7 @@ test('fast-safe-stringify must be used when interpolating', async (t) => {
instance.info('test %j', o)

const { msg } = await once(stream, 'data')
t.equal(msg, 'test {"a":{"b":{"c":"[Circular]"}}}')
t.equal(msg, 'test {"a":{"b":{"c":"[Circular ~.a.b]"}}}')
})

test('throws when setting useOnlyCustomLevels without customLevels', async ({ throws }) => {
Expand Down

0 comments on commit 19f368e

Please sign in to comment.