Skip to content

Commit

Permalink
Add pino-browser support for hapi-pino (#612)
Browse files Browse the repository at this point in the history
This changes browser.js to expose symbols and serializer methods used
by hapi-pino.

This is extremely niche use case, but some people want to run Hapi.js
(HTTP server) in WebExtension context using chrome.sockets APIs.

Ref. ipfs/ipfs-companion#664
  • Loading branch information
lidel authored and mcollina committed Mar 20, 2019
1 parent 232d903 commit 7d2ba19
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
6 changes: 6 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module.exports = pino

var _console = global.console || {}
var stdSerializers = {
mapHttpRequest: mock,
mapHttpResponse: mock,
wrapRequestSerializer: passthrough,
wrapResponseSerializer: passthrough,
req: mock,
res: mock,
err: asErrValue
Expand Down Expand Up @@ -157,6 +161,7 @@ pino.levels = {
}

pino.stdSerializers = stdSerializers
pino.symbols = require('./lib/symbols')

function set (opts, logger, level, fallback) {
var proto = Object.getPrototypeOf(logger)
Expand Down Expand Up @@ -296,4 +301,5 @@ function asErrValue (err) {
}

function mock () { return {} }
function passthrough (a) { return a }
function noop () {}
30 changes: 23 additions & 7 deletions test/browser.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'
const test = require('tape')
const fresh = require('fresh-require')
const pinoStdSerializers = require('pino-std-serializers')
const pinoSymbols = require('../lib/symbols')
const pino = require('../browser')

levelTest('fatal')
Expand Down Expand Up @@ -101,22 +103,36 @@ test('exposes LOG_VERSION', ({ end, is }) => {

test('exposes faux stdSerializers', ({ end, ok, same }) => {
ok(pino.stdSerializers)
ok(pino.stdSerializers.req)
ok(pino.stdSerializers.res)
ok(pino.stdSerializers.err)
// make sure faux stdSerializers match pino-std-serializers
for (let serializer in pinoStdSerializers) {
ok(pino.stdSerializers[serializer], `pino.stdSerializers.${serializer}`)
}
// confirm faux methods return empty objects
same(pino.stdSerializers.req(), {})
same(pino.stdSerializers.mapHttpRequest(), {})
same(pino.stdSerializers.mapHttpResponse(), {})
same(pino.stdSerializers.res(), {})

// confirm wrapping function is a passthrough
const noChange = { foo: 'bar', fuz: 42 }
same(pino.stdSerializers.wrapRequestSerializer(noChange), noChange)
same(pino.stdSerializers.wrapResponseSerializer(noChange), noChange)
end()
})

test('exposes err stdSerializer', ({ end, ok }) => {
ok(pino.stdSerializers)
ok(pino.stdSerializers.req)
ok(pino.stdSerializers.res)
ok(pino.stdSerializers.err)
ok(pino.stdSerializers.err(Error()))
end()
})

test('exposes real symbols', ({ end, ok, same }) => {
ok(pino.symbols)
// confirm every symbol is present
for (let symbol in pinoSymbols) {
ok(pino.symbols[symbol], `pino.symbols.${symbol}`)
}
// confirm real symbols are used
same(pino.symbols, pinoSymbols)
end()
})

Expand Down

0 comments on commit 7d2ba19

Please sign in to comment.