pino-std-serializers
Advanced tools
Comparing version 6.2.2 to 7.0.0
{ | ||
"name": "pino-std-serializers", | ||
"version": "6.2.2", | ||
"version": "7.0.0", | ||
"description": "A collection of standard object serializers for Pino", | ||
@@ -11,4 +11,4 @@ "main": "index.js", | ||
"lint-ci": "standard", | ||
"test": "tap --no-cov", | ||
"test-ci": "tap --cov --no-check-coverage --coverage-report=text", | ||
"test": "borp -p 'test/**/*.js'", | ||
"test-ci": "borp --coverage -p 'test/**/*.js'", | ||
"test-types": "tsc && tsd" | ||
@@ -36,9 +36,10 @@ }, | ||
"devDependencies": { | ||
"@types/node": "^20.1.0", | ||
"@matteo.collina/tspl": "^0.1.1", | ||
"@types/node": "^20.11.17", | ||
"borp": "^0.9.1", | ||
"pre-commit": "^1.2.2", | ||
"snazzy": "^9.0.0", | ||
"standard": "^17.0.0", | ||
"tap": "^15.0.10", | ||
"tsd": "^0.28.0", | ||
"typescript": "^5.0.2" | ||
"standard": "^17.1.0", | ||
"tsd": "^0.31.0", | ||
"typescript": "^5.3.3" | ||
}, | ||
@@ -45,0 +46,0 @@ "tsd": { |
@@ -50,2 +50,3 @@ # pino-std-serializers [![CI](https://github.com/pinojs/pino-std-serializers/workflows/CI/badge.svg)](https://github.com/pinojs/pino-std-serializers/actions?query=workflow%3ACI) | ||
*/ | ||
``` | ||
@@ -52,0 +53,0 @@ ### `exports.errWithCause(error)` |
'use strict' | ||
const test = require('tap').test | ||
const { test } = require('node:test') | ||
const assert = require('node:assert') | ||
const serializer = require('../lib/err-with-cause') | ||
const wrapErrorSerializer = require('../').wrapErrorSerializer | ||
const { wrapErrorSerializer } = require('../') | ||
test('serializes Error objects', function (t) { | ||
t.plan(3) | ||
test('serializes Error objects', () => { | ||
const serialized = serializer(Error('foo')) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
}) | ||
test('serializes Error objects with extra properties', function (t) { | ||
t.plan(5) | ||
test('serializes Error objects with extra properties', () => { | ||
const err = Error('foo') | ||
err.statusCode = 500 | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.ok(serialized.statusCode) | ||
t.equal(serialized.statusCode, 500) | ||
t.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.ok(serialized.statusCode) | ||
assert.strictEqual(serialized.statusCode, 500) | ||
assert.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
}) | ||
test('serializes Error objects with subclass "type"', function (t) { | ||
t.plan(1) | ||
test('serializes Error objects with subclass "type"', () => { | ||
class MyError extends Error {} | ||
@@ -34,20 +31,19 @@ | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'MyError') | ||
assert.strictEqual(serialized.type, 'MyError') | ||
}) | ||
test('serializes nested errors', function (t) { | ||
t.plan(7) | ||
test('serializes nested errors', () => { | ||
const err = Error('foo') | ||
err.inner = Error('bar') | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
t.equal(serialized.inner.type, 'Error') | ||
t.equal(serialized.inner.message, 'bar') | ||
t.match(serialized.inner.stack, /Error: bar/) | ||
t.match(serialized.inner.stack, /err-with-cause\.test\.js:/) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
assert.strictEqual(serialized.inner.type, 'Error') | ||
assert.strictEqual(serialized.inner.message, 'bar') | ||
assert.match(serialized.inner.stack, /Error: bar/) | ||
assert.match(serialized.inner.stack, /err-with-cause\.test\.js:/) | ||
}) | ||
test('serializes error causes', function (t) { | ||
test('serializes error causes', () => { | ||
const innerErr = Error('inner') | ||
@@ -61,40 +57,35 @@ const middleErr = Error('middle') | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'outer') | ||
t.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'outer') | ||
assert.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
t.equal(serialized.cause.type, 'Error') | ||
t.equal(serialized.cause.message, 'middle') | ||
t.match(serialized.cause.stack, /err-with-cause\.test\.js:/) | ||
assert.strictEqual(serialized.cause.type, 'Error') | ||
assert.strictEqual(serialized.cause.message, 'middle') | ||
assert.match(serialized.cause.stack, /err-with-cause\.test\.js:/) | ||
t.equal(serialized.cause.cause.type, 'Error') | ||
t.equal(serialized.cause.cause.message, 'inner') | ||
t.match(serialized.cause.cause.stack, /err-with-cause\.test\.js:/) | ||
t.end() | ||
assert.strictEqual(serialized.cause.cause.type, 'Error') | ||
assert.strictEqual(serialized.cause.cause.message, 'inner') | ||
assert.match(serialized.cause.cause.stack, /err-with-cause\.test\.js:/) | ||
}) | ||
test('keeps non-error cause', function (t) { | ||
t.plan(3) | ||
test('keeps non-error cause', () => { | ||
const err = Error('foo') | ||
err.cause = 'abc' | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.equal(serialized.cause, 'abc') | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.strictEqual(serialized.cause, 'abc') | ||
}) | ||
test('prevents infinite recursion', function (t) { | ||
t.plan(4) | ||
test('prevents infinite recursion', () => { | ||
const err = Error('foo') | ||
err.inner = err | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
t.notOk(serialized.inner) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
assert.ok(!serialized.inner) | ||
}) | ||
test('cleans up infinite recursion tracking', function (t) { | ||
t.plan(8) | ||
test('cleans up infinite recursion tracking', () => { | ||
const err = Error('foo') | ||
@@ -108,25 +99,22 @@ const bar = Error('bar') | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
t.ok(serialized.inner) | ||
t.equal(serialized.inner.type, 'Error') | ||
t.equal(serialized.inner.message, 'bar') | ||
t.match(serialized.inner.stack, /Error: bar/) | ||
t.notOk(serialized.inner.inner) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
assert.ok(serialized.inner) | ||
assert.strictEqual(serialized.inner.type, 'Error') | ||
assert.strictEqual(serialized.inner.message, 'bar') | ||
assert.match(serialized.inner.stack, /Error: bar/) | ||
assert.ok(!serialized.inner.inner) | ||
}) | ||
test('err.raw is available', function (t) { | ||
t.plan(1) | ||
test('err.raw is available', () => { | ||
const err = Error('foo') | ||
const serialized = serializer(err) | ||
t.equal(serialized.raw, err) | ||
assert.strictEqual(serialized.raw, err) | ||
}) | ||
test('redefined err.constructor doesnt crash serializer', function (t) { | ||
t.plan(10) | ||
test('redefined err.constructor doesnt crash serializer', () => { | ||
function check (a, name) { | ||
t.equal(a.type, name) | ||
t.equal(a.message, 'foo') | ||
assert.strictEqual(a.type, name) | ||
assert.strictEqual(a.message, 'foo') | ||
} | ||
@@ -160,7 +148,5 @@ | ||
test('pass through anything that does not look like an Error', function (t) { | ||
t.plan(3) | ||
test('pass through anything that does not look like an Error', () => { | ||
function check (a) { | ||
t.equal(serializer(a), a) | ||
assert.strictEqual(serializer(a), a) | ||
} | ||
@@ -173,4 +159,3 @@ | ||
test('can wrap err serializers', function (t) { | ||
t.plan(5) | ||
test('can wrap err serializers', () => { | ||
const err = Error('foo') | ||
@@ -184,11 +169,10 @@ err.foo = 'foo' | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
t.notOk(serialized.foo) | ||
t.equal(serialized.bar, 'bar') | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
assert.ok(!serialized.foo) | ||
assert.strictEqual(serialized.bar, 'bar') | ||
}) | ||
test('serializes aggregate errors', { skip: !global.AggregateError }, function (t) { | ||
t.plan(14) | ||
test('serializes aggregate errors', { skip: !global.AggregateError }, () => { | ||
const foo = new Error('foo') | ||
@@ -201,10 +185,10 @@ const bar = new Error('bar') | ||
const serialized = serializer(aggregate) | ||
t.equal(serialized.message, 'aggregated message') | ||
t.equal(serialized.aggregateErrors.length, 2) | ||
t.equal(serialized.aggregateErrors[0].message, 'foo') | ||
t.equal(serialized.aggregateErrors[1].message, 'bar') | ||
t.match(serialized.aggregateErrors[0].stack, /^Error: foo/) | ||
t.match(serialized.aggregateErrors[1].stack, /^Error: bar/) | ||
t.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
assert.strictEqual(serialized.message, 'aggregated message') | ||
assert.strictEqual(serialized.aggregateErrors.length, 2) | ||
assert.strictEqual(serialized.aggregateErrors[0].message, 'foo') | ||
assert.strictEqual(serialized.aggregateErrors[1].message, 'bar') | ||
assert.match(serialized.aggregateErrors[0].stack, /^Error: foo/) | ||
assert.match(serialized.aggregateErrors[1].stack, /^Error: bar/) | ||
assert.match(serialized.stack, /err-with-cause\.test\.js:/) | ||
} | ||
}) |
'use strict' | ||
const test = require('tap').test | ||
const assert = require('node:assert') | ||
const { test } = require('node:test') | ||
const serializer = require('../lib/err') | ||
const wrapErrorSerializer = require('../').wrapErrorSerializer | ||
const { wrapErrorSerializer } = require('../') | ||
test('serializes Error objects', function (t) { | ||
t.plan(3) | ||
test('serializes Error objects', () => { | ||
const serialized = serializer(Error('foo')) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
}) | ||
test('serializes Error objects with extra properties', function (t) { | ||
t.plan(5) | ||
test('serializes Error objects with extra properties', () => { | ||
const err = Error('foo') | ||
err.statusCode = 500 | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.ok(serialized.statusCode) | ||
t.equal(serialized.statusCode, 500) | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.ok(serialized.statusCode) | ||
assert.strictEqual(serialized.statusCode, 500) | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
}) | ||
test('serializes Error objects with subclass "type"', function (t) { | ||
t.plan(1) | ||
test('serializes Error objects with subclass "type"', () => { | ||
class MyError extends Error {} | ||
const err = new MyError('foo') | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'MyError') | ||
assert.strictEqual(serialized.type, 'MyError') | ||
}) | ||
test('serializes nested errors', function (t) { | ||
t.plan(7) | ||
test('serializes nested errors', () => { | ||
const err = Error('foo') | ||
err.inner = Error('bar') | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
t.equal(serialized.inner.type, 'Error') | ||
t.equal(serialized.inner.message, 'bar') | ||
t.match(serialized.inner.stack, /Error: bar/) | ||
t.match(serialized.inner.stack, /err\.test\.js:/) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
assert.strictEqual(serialized.inner.type, 'Error') | ||
assert.strictEqual(serialized.inner.message, 'bar') | ||
assert.match(serialized.inner.stack, /Error: bar/) | ||
assert.match(serialized.inner.stack, /err\.test\.js:/) | ||
}) | ||
test('serializes error causes', function (t) { | ||
t.plan(14) | ||
test('serializes error causes', () => { | ||
for (const cause of [ | ||
@@ -59,9 +55,9 @@ Error('bar'), | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo: bar: abc') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
t.match(serialized.stack, /Error: foo/) | ||
t.match(serialized.stack, /Error: bar/) | ||
t.match(serialized.stack, /Error: abc/) | ||
t.notOk(serialized.cause) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo: bar: abc') | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
assert.match(serialized.stack, /Error: foo/) | ||
assert.match(serialized.stack, /Error: bar/) | ||
assert.match(serialized.stack, /Error: abc/) | ||
assert.ok(!serialized.cause) | ||
} | ||
@@ -71,3 +67,2 @@ }) | ||
test('serializes error causes with VError support', function (t) { | ||
t.plan(6) | ||
// Fake VError-style setup | ||
@@ -82,33 +77,30 @@ const err = Error('foo: bar') | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo: bar: abc') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
t.match(serialized.stack, /Error: foo/) | ||
t.match(serialized.stack, /Error: bar/) | ||
t.match(serialized.stack, /Error: abc/) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo: bar: abc') | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
assert.match(serialized.stack, /Error: foo/) | ||
assert.match(serialized.stack, /Error: bar/) | ||
assert.match(serialized.stack, /Error: abc/) | ||
}) | ||
test('keeps non-error cause', function (t) { | ||
t.plan(3) | ||
test('keeps non-error cause', () => { | ||
const err = Error('foo') | ||
err.cause = 'abc' | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.equal(serialized.cause, 'abc') | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.strictEqual(serialized.cause, 'abc') | ||
}) | ||
test('prevents infinite recursion', function (t) { | ||
t.plan(4) | ||
test('prevents infinite recursion', () => { | ||
const err = Error('foo') | ||
err.inner = err | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
t.notOk(serialized.inner) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
assert.ok(!serialized.inner) | ||
}) | ||
test('cleans up infinite recursion tracking', function (t) { | ||
t.plan(8) | ||
test('cleans up infinite recursion tracking', () => { | ||
const err = Error('foo') | ||
@@ -122,25 +114,22 @@ const bar = Error('bar') | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
t.ok(serialized.inner) | ||
t.equal(serialized.inner.type, 'Error') | ||
t.equal(serialized.inner.message, 'bar') | ||
t.match(serialized.inner.stack, /Error: bar/) | ||
t.notOk(serialized.inner.inner) | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
assert.ok(serialized.inner) | ||
assert.strictEqual(serialized.inner.type, 'Error') | ||
assert.strictEqual(serialized.inner.message, 'bar') | ||
assert.match(serialized.inner.stack, /Error: bar/) | ||
assert.ok(!serialized.inner.inner) | ||
}) | ||
test('err.raw is available', function (t) { | ||
t.plan(1) | ||
test('err.raw is available', () => { | ||
const err = Error('foo') | ||
const serialized = serializer(err) | ||
t.equal(serialized.raw, err) | ||
assert.strictEqual(serialized.raw, err) | ||
}) | ||
test('redefined err.constructor doesnt crash serializer', function (t) { | ||
t.plan(10) | ||
test('redefined err.constructor doesnt crash serializer', () => { | ||
function check (a, name) { | ||
t.equal(a.type, name) | ||
t.equal(a.message, 'foo') | ||
assert.strictEqual(a.type, name) | ||
assert.strictEqual(a.message, 'foo') | ||
} | ||
@@ -173,7 +162,5 @@ | ||
test('pass through anything that does not look like an Error', function (t) { | ||
t.plan(3) | ||
test('pass through anything that does not look like an Error', () => { | ||
function check (a) { | ||
t.equal(serializer(a), a) | ||
assert.strictEqual(serializer(a), a) | ||
} | ||
@@ -186,4 +173,3 @@ | ||
test('can wrap err serializers', function (t) { | ||
t.plan(5) | ||
test('can wrap err serializers', () => { | ||
const err = Error('foo') | ||
@@ -197,11 +183,10 @@ err.foo = 'foo' | ||
const serialized = serializer(err) | ||
t.equal(serialized.type, 'Error') | ||
t.equal(serialized.message, 'foo') | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
t.notOk(serialized.foo) | ||
t.equal(serialized.bar, 'bar') | ||
assert.strictEqual(serialized.type, 'Error') | ||
assert.strictEqual(serialized.message, 'foo') | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
assert.ok(!serialized.foo) | ||
assert.strictEqual(serialized.bar, 'bar') | ||
}) | ||
test('serializes aggregate errors', { skip: !global.AggregateError }, function (t) { | ||
t.plan(14) | ||
test('serializes aggregate errors', { skip: !global.AggregateError }, () => { | ||
const foo = new Error('foo') | ||
@@ -214,10 +199,10 @@ const bar = new Error('bar') | ||
const serialized = serializer(aggregate) | ||
t.equal(serialized.message, 'aggregated message') | ||
t.equal(serialized.aggregateErrors.length, 2) | ||
t.equal(serialized.aggregateErrors[0].message, 'foo') | ||
t.equal(serialized.aggregateErrors[1].message, 'bar') | ||
t.match(serialized.aggregateErrors[0].stack, /^Error: foo/) | ||
t.match(serialized.aggregateErrors[1].stack, /^Error: bar/) | ||
t.match(serialized.stack, /err\.test\.js:/) | ||
assert.strictEqual(serialized.message, 'aggregated message') | ||
assert.strictEqual(serialized.aggregateErrors.length, 2) | ||
assert.strictEqual(serialized.aggregateErrors[0].message, 'foo') | ||
assert.strictEqual(serialized.aggregateErrors[1].message, 'bar') | ||
assert.match(serialized.aggregateErrors[0].stack, /^Error: foo/) | ||
assert.match(serialized.aggregateErrors[1].stack, /^Error: bar/) | ||
assert.match(serialized.stack, /err\.test\.js:/) | ||
} | ||
}) |
'use strict' | ||
const http = require('http') | ||
const test = require('tap').test | ||
const { tspl } = require('@matteo.collina/tspl') | ||
const http = require('node:http') | ||
const { test } = require('node:test') | ||
const serializers = require('../lib/req') | ||
const wrapRequestSerializer = require('../').wrapRequestSerializer | ||
const { wrapRequestSerializer } = require('../') | ||
test('maps request', function (t) { | ||
t.plan(2) | ||
test('maps request', async (t) => { | ||
const p = tspl(t, { plan: 2 }) | ||
@@ -17,15 +18,16 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
const serialized = serializers.mapHttpRequest(req) | ||
t.ok(serialized.req) | ||
t.ok(serialized.req.method) | ||
t.end() | ||
p.ok(serialized.req) | ||
p.ok(serialized.req.method) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('does not return excessively long object', function (t) { | ||
t.plan(1) | ||
test('does not return excessively long object', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -38,13 +40,15 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(Object.keys(serialized).length, 6) | ||
p.strictEqual(Object.keys(serialized).length, 6) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.raw is available', function (t) { | ||
t.plan(2) | ||
test('req.raw is available', async (t) => { | ||
const p = tspl(t, { plan: 2 }) | ||
@@ -57,3 +61,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -63,10 +67,12 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.ok(serialized.raw) | ||
t.equal(serialized.raw.foo, 'foo') | ||
p.ok(serialized.raw) | ||
p.strictEqual(serialized.raw.foo, 'foo') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.raw will be obtained in from input request raw property if input request raw property is truthy', function (t) { | ||
t.plan(2) | ||
test('req.raw will be obtained in from input request raw property if input request raw property is truthy', async (t) => { | ||
const p = tspl(t, { plan: 2 }) | ||
@@ -79,3 +85,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -85,10 +91,12 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.ok(serialized.raw) | ||
t.equal(serialized.raw.req.foo, 'foo') | ||
p.ok(serialized.raw) | ||
p.strictEqual(serialized.raw.req.foo, 'foo') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.id defaults to undefined', function (t) { | ||
t.plan(1) | ||
test('req.id defaults to undefined', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -101,13 +109,15 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.id, undefined) | ||
p.strictEqual(serialized.id, undefined) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.id has a non-function value', function (t) { | ||
t.plan(1) | ||
test('req.id has a non-function value', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -120,13 +130,15 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(typeof serialized.id === 'function', false) | ||
p.strictEqual(typeof serialized.id === 'function', false) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.id will be obtained from input request info.id when input request id does not exist', function (t) { | ||
t.plan(1) | ||
test('req.id will be obtained from input request info.id when input request id does not exist', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -139,3 +151,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -145,9 +157,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.id, 'test') | ||
p.strictEqual(serialized.id, 'test') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.id has a non-function value with custom id function', function (t) { | ||
t.plan(2) | ||
test('req.id has a non-function value with custom id function', async (t) => { | ||
const p = tspl(t, { plan: 2 }) | ||
@@ -160,3 +174,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -166,10 +180,12 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(typeof serialized.id === 'function', false) | ||
t.equal(serialized.id, 42) | ||
p.strictEqual(typeof serialized.id === 'function', false) | ||
p.strictEqual(serialized.id, 42) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.url will be obtained from input request req.path when input request url is an object', function (t) { | ||
t.plan(1) | ||
test('req.url will be obtained from input request req.path when input request url is an object', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -182,3 +198,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -188,9 +204,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.url, '/test') | ||
p.strictEqual(serialized.url, '/test') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.url will be obtained from input request url.path when input request url is an object', function (t) { | ||
t.plan(1) | ||
test('req.url will be obtained from input request url.path when input request url is an object', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -203,3 +221,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -209,9 +227,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.url, '/test') | ||
p.strictEqual(serialized.url, '/test') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.url will be obtained from input request url when input request url is not an object', function (t) { | ||
t.plan(1) | ||
test('req.url will be obtained from input request url when input request url is not an object', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -224,3 +244,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -230,9 +250,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.url, '/test') | ||
p.strictEqual(serialized.url, '/test') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.url will be empty when input request path and url are not defined', function (t) { | ||
t.plan(1) | ||
test('req.url will be empty when input request path and url are not defined', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -245,13 +267,15 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.url, '/') | ||
p.strictEqual(serialized.url, '/') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.url will be obtained from input request originalUrl when available', function (t) { | ||
t.plan(1) | ||
test('req.url will be obtained from input request originalUrl when available', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -264,3 +288,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -270,9 +294,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.url, '/test') | ||
p.strictEqual(serialized.url, '/test') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.url will be obtained from input request url when req path is a function', function (t) { | ||
t.plan(1) | ||
test('req.url will be obtained from input request url when req path is a function', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -285,3 +311,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -294,9 +320,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.url, '/test') | ||
p.strictEqual(serialized.url, '/test') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.url being undefined does not throw an error', function (t) { | ||
t.plan(1) | ||
test('req.url being undefined does not throw an error', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -309,3 +337,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -315,9 +343,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.url, undefined) | ||
p.strictEqual(serialized.url, undefined) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('can wrap request serializers', function (t) { | ||
t.plan(3) | ||
test('can wrap request serializers', async (t) => { | ||
const p = tspl(t, { plan: 3 }) | ||
@@ -330,7 +360,7 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
const serailizer = wrapRequestSerializer(function (req) { | ||
t.ok(req.method) | ||
t.equal(req.method, 'GET') | ||
p.ok(req.method) | ||
p.strictEqual(req.method, 'GET') | ||
delete req.method | ||
@@ -342,9 +372,11 @@ return req | ||
const serialized = serailizer(req) | ||
t.notOk(serialized.method) | ||
p.ok(!serialized.method) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.remoteAddress will be obtained from request socket.remoteAddress as fallback', function (t) { | ||
t.plan(1) | ||
test('req.remoteAddress will be obtained from request socket.remoteAddress as fallback', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -357,3 +389,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -363,9 +395,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.remoteAddress, 'http://localhost') | ||
p.strictEqual(serialized.remoteAddress, 'http://localhost') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.remoteAddress will be obtained from request info.remoteAddress if available', function (t) { | ||
t.plan(1) | ||
test('req.remoteAddress will be obtained from request info.remoteAddress if available', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -378,3 +412,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -384,9 +418,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.remoteAddress, 'http://localhost') | ||
p.strictEqual(serialized.remoteAddress, 'http://localhost') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.remotePort will be obtained from request socket.remotePort as fallback', function (t) { | ||
t.plan(1) | ||
test('req.remotePort will be obtained from request socket.remotePort as fallback', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -399,3 +435,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -405,9 +441,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.remotePort, 3000) | ||
p.strictEqual(serialized.remotePort, 3000) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.remotePort will be obtained from request info.remotePort if available', function (t) { | ||
t.plan(1) | ||
test('req.remotePort will be obtained from request info.remotePort if available', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -420,3 +458,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -426,9 +464,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.remotePort, 3000) | ||
p.strictEqual(serialized.remotePort, 3000) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.query is available', function (t) { | ||
t.plan(1) | ||
test('req.query is available', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -441,3 +481,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -447,9 +487,11 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.query, '/foo?bar=foobar&bar=foo') | ||
p.strictEqual(serialized.query, '/foo?bar=foobar&bar=foo') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('req.params is available', function (t) { | ||
t.plan(1) | ||
test('req.params is available', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -462,3 +504,3 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
@@ -468,5 +510,7 @@ function handler (req, res) { | ||
const serialized = serializers.reqSerializer(req) | ||
t.equal(serialized.params, '/foo/bar') | ||
p.strictEqual(serialized.params, '/foo/bar') | ||
res.end() | ||
} | ||
await p.completed | ||
}) |
@@ -5,9 +5,10 @@ 'use strict' | ||
const http = require('http') | ||
const test = require('tap').test | ||
const { tspl } = require('@matteo.collina/tspl') | ||
const http = require('node:http') | ||
const { test } = require('node:test') | ||
const serializers = require('../lib/res') | ||
const wrapResponseSerializer = require('../').wrapResponseSerializer | ||
const { wrapResponseSerializer } = require('../') | ||
test('res.raw is not enumerable', function (t) { | ||
t.plan(1) | ||
test('res.raw is not enumerable', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -20,13 +21,15 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
function handler (_req, res) { | ||
const serialized = serializers.resSerializer(res) | ||
t.equal(serialized.propertyIsEnumerable('raw'), false) | ||
p.strictEqual(serialized.propertyIsEnumerable('raw'), false) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('res.raw is available', function (t) { | ||
t.plan(2) | ||
test('res.raw is available', async (t) => { | ||
const p = tspl(t, { plan: 2 }) | ||
@@ -39,15 +42,17 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
function handler (_req, res) { | ||
res.statusCode = 200 | ||
const serialized = serializers.resSerializer(res) | ||
t.ok(serialized.raw) | ||
t.equal(serialized.raw.statusCode, 200) | ||
p.ok(serialized.raw) | ||
p.strictEqual(serialized.raw.statusCode, 200) | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('can wrap response serializers', function (t) { | ||
t.plan(3) | ||
test('can wrap response serializers', async (t) => { | ||
const p = tspl(t, { plan: 3 }) | ||
@@ -60,7 +65,7 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
const serializer = wrapResponseSerializer(function (res) { | ||
t.ok(res.statusCode) | ||
t.equal(res.statusCode, 200) | ||
p.ok(res.statusCode) | ||
p.strictEqual(res.statusCode, 200) | ||
delete res.statusCode | ||
@@ -70,12 +75,14 @@ return res | ||
function handler (req, res) { | ||
function handler (_req, res) { | ||
res.end() | ||
res.statusCode = 200 | ||
const serialized = serializer(res) | ||
t.notOk(serialized.statusCode) | ||
p.ok(!serialized.statusCode) | ||
} | ||
await p.completed | ||
}) | ||
test('res.headers is serialized', function (t) { | ||
t.plan(1) | ||
test('res.headers is serialized', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -88,14 +95,16 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
function handler (_req, res) { | ||
res.setHeader('x-custom', 'y') | ||
const serialized = serializers.resSerializer(res) | ||
t.equal(serialized.headers['x-custom'], 'y') | ||
p.strictEqual(serialized.headers['x-custom'], 'y') | ||
res.end() | ||
} | ||
await p.completed | ||
}) | ||
test('res.statusCode is null before headers are flushed', function (t) { | ||
t.plan(1) | ||
test('req.url will be obtained from input request url when input request url is not an object', async (t) => { | ||
const p = tspl(t, { plan: 1 }) | ||
@@ -108,9 +117,11 @@ const server = http.createServer(handler) | ||
t.teardown(() => server.close()) | ||
t.after(() => server.close()) | ||
function handler (req, res) { | ||
function handler (_req, res) { | ||
const serialized = serializers.resSerializer(res) | ||
t.equal(serialized.statusCode, null) | ||
p.strictEqual(serialized.statusCode, null) | ||
res.end() | ||
} | ||
await p.completed | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
54011
1379
183
0
8
21