Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace fast-safe-stringify with json-stringify-safe #1066

Merged
merged 1 commit into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/tools.js
Original file line number Diff line number Diff line change
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]"'
davidmarkclements marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 }) => {
kibertoad marked this conversation as resolved.
Show resolved Hide resolved
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