Skip to content

Commit

Permalink
feat(serializers): wrap breaking changes in future
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardolima committed Dec 9, 2024
1 parent ebd84c3 commit da9a16f
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 131 deletions.
9 changes: 7 additions & 2 deletions lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function write (_obj, msg, num) {
const responseKey = this[responseKeySym]
const messageKey = this[messageKeySym]
const mixinMergeStrategy = this[mixinMergeStrategySym] || defaultMixinMergeStrategy
const future = this[futureSym]
let obj

if (_obj === undefined || _obj === null) {
Expand All @@ -205,9 +206,13 @@ function write (_obj, msg, num) {
msg = _obj.message
}
} else if (_obj.method && _obj.headers && _obj.socket) {
obj = { [requestKey]: _obj }
if (future.skipUnconditionalStdSerializers) {
obj = { [requestKey]: _obj }
}
} else if (typeof _obj.setHeader === 'function') {
obj = { [responseKey]: _obj }
if (future.skipUnconditionalStdSerializers) {
obj = { [responseKey]: _obj }
}
} else {
obj = _obj
if (msg === undefined && _obj[messageKey] === undefined && _obj[errorKey]) {
Expand Down
6 changes: 3 additions & 3 deletions lib/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ const endSym = Symbol('pino.end')
const formatOptsSym = Symbol('pino.formatOpts')
const messageKeySym = Symbol('pino.messageKey')
const errorKeySym = Symbol('pino.errorKey')
const requestKeySym = Symbol('pino.requestKey')
const responseKeySym = Symbol('pino.responseKey')
const nestedKeySym = Symbol('pino.nestedKey')
const nestedKeyStrSym = Symbol('pino.nestedKeyStr')
const mixinMergeStrategySym = Symbol('pino.mixinMergeStrategy')
const msgPrefixSym = Symbol('pino.msgPrefix')
const responseKeySym = Symbol('pino.responseKey')
const requestKeySym = Symbol('pino.requestKey')
const futureSym = Symbol('pino.future')

const wildcardFirstSym = Symbol('pino.wildcardFirst')
Expand Down Expand Up @@ -65,8 +65,8 @@ module.exports = {
formatOptsSym,
messageKeySym,
errorKeySym,
responseKeySym,
requestKeySym,
responseKeySym,
nestedKeySym,
wildcardFirstSym,
needsMetadataGsym,
Expand Down
21 changes: 17 additions & 4 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint no-prototype-builtins: 0 */

const format = require('quick-format-unescaped')
const { mapHttpRequest, mapHttpResponse } = require('pino-std-serializers')
const SonicBoom = require('sonic-boom')
const onExit = require('on-exit-leak-free')
const {
Expand All @@ -23,10 +24,12 @@ const {
requestKeySym,
responseKeySym,
nestedKeyStrSym,
msgPrefixSym
msgPrefixSym,
futureSym
} = require('./symbols')
const { isMainThread } = require('worker_threads')
const transport = require('./transport')
const { warning } = require('./deprecations')

function noop () {
}
Expand All @@ -41,6 +44,16 @@ function genLog (level, hook) {
function LOG (o, ...n) {
if (typeof o === 'object') {
let msg = o
if (!this[futureSym].skipUnconditionalStdSerializers) {
warning.PINODEP010()
if (o !== null) {
if (o.method && o.headers && o.socket) {
o = mapHttpRequest(o)
} else if (typeof o.setHeader === 'function') {
o = mapHttpResponse(o)
}
}
}
let formatParams
if (msg === null && n.length === 0) {
formatParams = [null]
Expand Down Expand Up @@ -128,9 +141,9 @@ function asJson (obj, msg, num, time) {
value = serializers[key](value)
} else if (key === errorKey && serializers.err) {
value = serializers.err(value)
} else if (key === requestKey && serializers.req) {
} else if (key === requestKey && serializers.req && this[futureSym].skipUnconditionalStdSerializers) {
value = serializers.req(value)
} else if (key === responseKey && serializers.res) {
} else if (key === responseKey && serializers.res && this[futureSym].skipUnconditionalStdSerializers) {
value = serializers.res(value)
}

Expand Down Expand Up @@ -316,7 +329,7 @@ function createArgsNormalizer (defaultOptions) {
stream = transport({ caller, ...opts.transport, levels: customLevels })
}
opts = Object.assign({}, defaultOptions, opts)
opts.serializers = Object.assign({}, defaultOptions.serializers, opts.serializers)

opts.formatters = Object.assign({}, defaultOptions.formatters, opts.formatters)

if (opts.prettyPrint) {
Expand Down
31 changes: 14 additions & 17 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ const {
const { epochTime, nullTime } = time
const { pid } = process
const hostname = os.hostname()
const defaultErrorSerializer = stdSerializers.err
const defaultRequestSerializer = stdSerializers.req
const defaultResponseSerializer = stdSerializers.res

const defaultOptions = {
level: 'info',
levelComparison: SORTING_ORDER.ASC,
Expand All @@ -67,11 +65,6 @@ const defaultOptions = {
nestedKey: null,
enabled: true,
base: { pid, hostname },
serializers: Object.assign(Object.create(null), {
err: defaultErrorSerializer,
req: defaultRequestSerializer,
res: defaultResponseSerializer
}),
formatters: Object.assign(Object.create(null), {
bindings (bindings) {
return bindings
Expand All @@ -95,8 +88,6 @@ const defaultOptions = {

const normalize = createArgsNormalizer(defaultOptions)

const serializers = Object.assign(Object.create(null), stdSerializers)

function pino (...args) {
const instance = {}
const { opts, stream } = normalize(instance, caller(), ...args)
Expand All @@ -110,8 +101,8 @@ function pino (...args) {
timestamp,
messageKey,
errorKey,
requestKey,
responseKey,
requestKey,
nestedKey,
base,
name,
Expand All @@ -130,7 +121,13 @@ function pino (...args) {
future
} = opts

const futureSafe = Object.assign(Object.create(null), defaultFuture, future)
// future is made immutable, as it impacts other settings
const futureSafe = Object.freeze(Object.assign(Object.create(null), defaultFuture, future))

const defaultSerializers = futureSafe.skipUnconditionalStdSerializers
? { err: stdSerializers.err, req: stdSerializers.req, res: stdSerializers.res }
: { err: stdSerializers.err }
const serializersSafe = Object.assign(Object.create(null), defaultSerializers, serializers)

const stringifySafe = configure({
maximumDepth: depthLimit,
Expand All @@ -153,7 +150,7 @@ function pino (...args) {
const end = '}' + (crlf ? '\r\n' : '\n')
const coreChindings = asChindings.bind(null, {
[chindingsSym]: '',
[serializersSym]: serializers,
[serializersSym]: serializersSafe,
[stringifiersSym]: stringifiers,
[stringifySym]: stringify,
[stringifySafeSym]: stringifySafe,
Expand Down Expand Up @@ -202,12 +199,12 @@ function pino (...args) {
[formatOptsSym]: formatOpts,
[messageKeySym]: messageKey,
[errorKeySym]: errorKey,
[requestKeySym]: requestKey,
[responseKeySym]: responseKey,
[requestKeySym]: requestKey,
[nestedKeySym]: nestedKey,
// protect against injection
[nestedKeyStrSym]: nestedKey ? `,${JSON.stringify(nestedKey)}:{` : '',
[serializersSym]: serializers,
[serializersSym]: serializersSafe,
[mixinSym]: mixin,
[mixinMergeStrategySym]: mixinMergeStrategy,
[chindingsSym]: chindings,
Expand All @@ -216,7 +213,7 @@ function pino (...args) {
silent: noop,
onChild,
[msgPrefixSym]: msgPrefix,
[futureSym]: Object.freeze(futureSafe) // future is set immutable to each Pino top-instance, as it affects behavior of other settings
[futureSym]: futureSafe
})

Object.setPrototypeOf(instance, proto())
Expand All @@ -243,7 +240,7 @@ module.exports.transport = require('./lib/transport')
module.exports.multistream = require('./lib/multistream')

module.exports.levels = mappings()
module.exports.stdSerializers = serializers
module.exports.stdSerializers = Object.assign(Object.create(null), stdSerializers)
module.exports.stdTimeFunctions = Object.assign({}, time)
module.exports.symbols = symbols
module.exports.version = version
Expand Down
Loading

0 comments on commit da9a16f

Please sign in to comment.