Skip to content

Commit

Permalink
fix(logs): safely serialize objects with cyclic refs
Browse files Browse the repository at this point in the history
  • Loading branch information
emirotin committed Jul 29, 2018
1 parent 62b9ba6 commit 270a7e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/core/botpress/src/logger/db-transport.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import winston from 'winston'
import ms from 'ms'
import Promise from 'bluebird'

import { isEmpty } from 'lodash'

import helpers from '../database/helpers'
import { safeStringify } from '../util'

const LOGS_FLUSH_INTERVAL = ms('2s')
const LOGS_CHUNK_SIZE = 1000
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class DbTransport extends winston.Transport {

log(level, message, meta, callback) {
if (!isEmpty(meta)) {
message += ` (meta=${JSON.stringify(meta)})`
message += ` (meta=${safeStringify(meta)})`
}

this.db
Expand Down
18 changes: 17 additions & 1 deletion packages/core/botpress/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ const getPackageName = pkg => {
}
}

const getCircularReplacer = () => {
const seen = new WeakSet()
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
return '[cyclic reference]'
}
seen.add(value)
}
return value
}
}

const safeStringify = o => JSON.stringify(o, getCircularReplacer())

module.exports = {
print,
resolveFromDir,
Expand All @@ -136,5 +151,6 @@ module.exports = {
getInMemoryDb,
safeId,
isBotpressPackage,
getModuleShortname
getModuleShortname,
safeStringify
}

0 comments on commit 270a7e4

Please sign in to comment.