pino-std 0.0.0-stage → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,477 @@
1
+ 'use strict'
2
+
3
+ const { tspl } = require('@matteo.collina/tspl')
4
+ const http = require('node:http')
5
+ const { test } = require('node:test')
6
+ const serializers = require('../lib/req')
7
+ const { wrapRequestSerializer } = require('../')
8
+
9
+ test('maps request', async (t) => {
10
+ const p = tspl(t, { plan: 2 })
11
+
12
+ const server = http.createServer(handler)
13
+ server.unref()
14
+ server.listen(0, () => {
15
+ http.get(server.address(), () => {})
16
+ })
17
+
18
+ t.after(() => server.close())
19
+
20
+ function handler (req, res) {
21
+ const serialized = serializers.mapHttpRequest(req)
22
+ p.ok(serialized.req)
23
+ p.ok(serialized.req.method)
24
+ res.end()
25
+ }
26
+
27
+ await p.completed
28
+ })
29
+
30
+ test('does not return excessively long object', async (t) => {
31
+ const p = tspl(t, { plan: 1 })
32
+
33
+ const server = http.createServer(handler)
34
+ server.unref()
35
+ server.listen(0, () => {
36
+ http.get(server.address(), () => {})
37
+ })
38
+
39
+ t.after(() => server.close())
40
+
41
+ function handler (req, res) {
42
+ const serialized = serializers.reqSerializer(req)
43
+ p.strictEqual(Object.keys(serialized).length, 6)
44
+ res.end()
45
+ }
46
+
47
+ await p.completed
48
+ })
49
+
50
+ test('req.raw is available', async (t) => {
51
+ const p = tspl(t, { plan: 2 })
52
+
53
+ const server = http.createServer(handler)
54
+ server.unref()
55
+ server.listen(0, () => {
56
+ http.get(server.address(), () => {})
57
+ })
58
+
59
+ t.after(() => server.close())
60
+
61
+ function handler (req, res) {
62
+ req.foo = 'foo'
63
+ const serialized = serializers.reqSerializer(req)
64
+ p.ok(serialized.raw)
65
+ p.strictEqual(serialized.raw.foo, 'foo')
66
+ res.end()
67
+ }
68
+
69
+ await p.completed
70
+ })
71
+
72
+ test('req.raw will be obtained in from input request raw property if input request raw property is truthy', async (t) => {
73
+ const p = tspl(t, { plan: 2 })
74
+
75
+ const server = http.createServer(handler)
76
+ server.unref()
77
+ server.listen(0, () => {
78
+ http.get(server.address(), () => {})
79
+ })
80
+
81
+ t.after(() => server.close())
82
+
83
+ function handler (req, res) {
84
+ req.raw = { req: { foo: 'foo' }, res: {} }
85
+ const serialized = serializers.reqSerializer(req)
86
+ p.ok(serialized.raw)
87
+ p.strictEqual(serialized.raw.req.foo, 'foo')
88
+ res.end()
89
+ }
90
+
91
+ await p.completed
92
+ })
93
+
94
+ test('req.id defaults to undefined', async (t) => {
95
+ const p = tspl(t, { plan: 1 })
96
+
97
+ const server = http.createServer(handler)
98
+ server.unref()
99
+ server.listen(0, () => {
100
+ http.get(server.address(), () => {})
101
+ })
102
+
103
+ t.after(() => server.close())
104
+
105
+ function handler (req, res) {
106
+ const serialized = serializers.reqSerializer(req)
107
+ p.strictEqual(serialized.id, undefined)
108
+ res.end()
109
+ }
110
+
111
+ await p.completed
112
+ })
113
+
114
+ test('req.id has a non-function value', async (t) => {
115
+ const p = tspl(t, { plan: 1 })
116
+
117
+ const server = http.createServer(handler)
118
+ server.unref()
119
+ server.listen(0, () => {
120
+ http.get(server.address(), () => {})
121
+ })
122
+
123
+ t.after(() => server.close())
124
+
125
+ function handler (req, res) {
126
+ const serialized = serializers.reqSerializer(req)
127
+ p.strictEqual(typeof serialized.id === 'function', false)
128
+ res.end()
129
+ }
130
+
131
+ await p.completed
132
+ })
133
+
134
+ test('req.id will be obtained from input request info.id when input request id does not exist', async (t) => {
135
+ const p = tspl(t, { plan: 1 })
136
+
137
+ const server = http.createServer(handler)
138
+ server.unref()
139
+ server.listen(0, () => {
140
+ http.get(server.address(), () => {})
141
+ })
142
+
143
+ t.after(() => server.close())
144
+
145
+ function handler (req, res) {
146
+ req.info = { id: 'test' }
147
+ const serialized = serializers.reqSerializer(req)
148
+ p.strictEqual(serialized.id, 'test')
149
+ res.end()
150
+ }
151
+
152
+ await p.completed
153
+ })
154
+
155
+ test('req.id has a non-function value with custom id function', async (t) => {
156
+ const p = tspl(t, { plan: 2 })
157
+
158
+ const server = http.createServer(handler)
159
+ server.unref()
160
+ server.listen(0, () => {
161
+ http.get(server.address(), () => {})
162
+ })
163
+
164
+ t.after(() => server.close())
165
+
166
+ function handler (req, res) {
167
+ req.id = function () { return 42 }
168
+ const serialized = serializers.reqSerializer(req)
169
+ p.strictEqual(typeof serialized.id === 'function', false)
170
+ p.strictEqual(serialized.id, 42)
171
+ res.end()
172
+ }
173
+
174
+ await p.completed
175
+ })
176
+
177
+ test('req.url will be obtained from input request req.path when input request url is an object', async (t) => {
178
+ const p = tspl(t, { plan: 1 })
179
+
180
+ const server = http.createServer(handler)
181
+ server.unref()
182
+ server.listen(0, () => {
183
+ http.get(server.address(), () => {})
184
+ })
185
+
186
+ t.after(() => server.close())
187
+
188
+ function handler (req, res) {
189
+ req.path = '/test'
190
+ const serialized = serializers.reqSerializer(req)
191
+ p.strictEqual(serialized.url, '/test')
192
+ res.end()
193
+ }
194
+
195
+ await p.completed
196
+ })
197
+
198
+ test('req.url will be obtained from input request url.path when input request url is an object', async (t) => {
199
+ const p = tspl(t, { plan: 1 })
200
+
201
+ const server = http.createServer(handler)
202
+ server.unref()
203
+ server.listen(0, () => {
204
+ http.get(server.address(), () => {})
205
+ })
206
+
207
+ t.after(() => server.close())
208
+
209
+ function handler (req, res) {
210
+ req.url = { path: '/test' }
211
+ const serialized = serializers.reqSerializer(req)
212
+ p.strictEqual(serialized.url, '/test')
213
+ res.end()
214
+ }
215
+
216
+ await p.completed
217
+ })
218
+
219
+ test('req.url will be obtained from input request url when input request url is not an object', async (t) => {
220
+ const p = tspl(t, { plan: 1 })
221
+
222
+ const server = http.createServer(handler)
223
+ server.unref()
224
+ server.listen(0, () => {
225
+ http.get(server.address(), () => {})
226
+ })
227
+
228
+ t.after(() => server.close())
229
+
230
+ function handler (req, res) {
231
+ req.url = '/test'
232
+ const serialized = serializers.reqSerializer(req)
233
+ p.strictEqual(serialized.url, '/test')
234
+ res.end()
235
+ }
236
+
237
+ await p.completed
238
+ })
239
+
240
+ test('req.url will be empty when input request path and url are not defined', async (t) => {
241
+ const p = tspl(t, { plan: 1 })
242
+
243
+ const server = http.createServer(handler)
244
+ server.unref()
245
+ server.listen(0, () => {
246
+ http.get(server.address(), () => {})
247
+ })
248
+
249
+ t.after(() => server.close())
250
+
251
+ function handler (req, res) {
252
+ const serialized = serializers.reqSerializer(req)
253
+ p.strictEqual(serialized.url, '/')
254
+ res.end()
255
+ }
256
+
257
+ await p.completed
258
+ })
259
+
260
+ test('req.url will be obtained from input request originalUrl when available', async (t) => {
261
+ const p = tspl(t, { plan: 1 })
262
+
263
+ const server = http.createServer(handler)
264
+ server.unref()
265
+ server.listen(0, () => {
266
+ http.get(server.address(), () => {})
267
+ })
268
+
269
+ t.after(() => server.close())
270
+
271
+ function handler (req, res) {
272
+ req.originalUrl = '/test'
273
+ const serialized = serializers.reqSerializer(req)
274
+ p.strictEqual(serialized.url, '/test')
275
+ res.end()
276
+ }
277
+
278
+ await p.completed
279
+ })
280
+
281
+ test('req.url will be obtained from input request url when req path is a function', async (t) => {
282
+ const p = tspl(t, { plan: 1 })
283
+
284
+ const server = http.createServer(handler)
285
+ server.unref()
286
+ server.listen(0, () => {
287
+ http.get(server.address(), () => {})
288
+ })
289
+
290
+ t.after(() => server.close())
291
+
292
+ function handler (req, res) {
293
+ req.path = function () {
294
+ throw new Error('unexpected invocation')
295
+ }
296
+ req.url = '/test'
297
+ const serialized = serializers.reqSerializer(req)
298
+ p.strictEqual(serialized.url, '/test')
299
+ res.end()
300
+ }
301
+
302
+ await p.completed
303
+ })
304
+
305
+ test('req.url being undefined does not throw an error', async (t) => {
306
+ const p = tspl(t, { plan: 1 })
307
+
308
+ const server = http.createServer(handler)
309
+ server.unref()
310
+ server.listen(0, () => {
311
+ http.get(server.address(), () => {})
312
+ })
313
+
314
+ t.after(() => server.close())
315
+
316
+ function handler (req, res) {
317
+ req.url = undefined
318
+ const serialized = serializers.reqSerializer(req)
319
+ p.strictEqual(serialized.url, undefined)
320
+ res.end()
321
+ }
322
+
323
+ await p.completed
324
+ })
325
+
326
+ test('can wrap request serializers', async (t) => {
327
+ const p = tspl(t, { plan: 3 })
328
+
329
+ const server = http.createServer(handler)
330
+ server.unref()
331
+ server.listen(0, () => {
332
+ http.get(server.address(), () => {})
333
+ })
334
+
335
+ t.after(() => server.close())
336
+
337
+ const serailizer = wrapRequestSerializer(function (req) {
338
+ p.ok(req.method)
339
+ p.strictEqual(req.method, 'GET')
340
+ delete req.method
341
+ return req
342
+ })
343
+
344
+ function handler (req, res) {
345
+ const serialized = serailizer(req)
346
+ p.ok(!serialized.method)
347
+ res.end()
348
+ }
349
+
350
+ await p.completed
351
+ })
352
+
353
+ test('req.remoteAddress will be obtained from request socket.remoteAddress as fallback', async (t) => {
354
+ const p = tspl(t, { plan: 1 })
355
+
356
+ const server = http.createServer(handler)
357
+ server.unref()
358
+ server.listen(0, () => {
359
+ http.get(server.address(), () => {})
360
+ })
361
+
362
+ t.after(() => server.close())
363
+
364
+ function handler (req, res) {
365
+ req.socket = { remoteAddress: 'http://localhost' }
366
+ const serialized = serializers.reqSerializer(req)
367
+ p.strictEqual(serialized.remoteAddress, 'http://localhost')
368
+ res.end()
369
+ }
370
+
371
+ await p.completed
372
+ })
373
+
374
+ test('req.remoteAddress will be obtained from request info.remoteAddress if available', async (t) => {
375
+ const p = tspl(t, { plan: 1 })
376
+
377
+ const server = http.createServer(handler)
378
+ server.unref()
379
+ server.listen(0, () => {
380
+ http.get(server.address(), () => {})
381
+ })
382
+
383
+ t.after(() => server.close())
384
+
385
+ function handler (req, res) {
386
+ req.info = { remoteAddress: 'http://localhost' }
387
+ const serialized = serializers.reqSerializer(req)
388
+ p.strictEqual(serialized.remoteAddress, 'http://localhost')
389
+ res.end()
390
+ }
391
+
392
+ await p.completed
393
+ })
394
+
395
+ test('req.remotePort will be obtained from request socket.remotePort as fallback', async (t) => {
396
+ const p = tspl(t, { plan: 1 })
397
+
398
+ const server = http.createServer(handler)
399
+ server.unref()
400
+ server.listen(0, () => {
401
+ http.get(server.address(), () => {})
402
+ })
403
+
404
+ t.after(() => server.close())
405
+
406
+ function handler (req, res) {
407
+ req.socket = { remotePort: 3000 }
408
+ const serialized = serializers.reqSerializer(req)
409
+ p.strictEqual(serialized.remotePort, 3000)
410
+ res.end()
411
+ }
412
+
413
+ await p.completed
414
+ })
415
+
416
+ test('req.remotePort will be obtained from request info.remotePort if available', async (t) => {
417
+ const p = tspl(t, { plan: 1 })
418
+
419
+ const server = http.createServer(handler)
420
+ server.unref()
421
+ server.listen(0, () => {
422
+ http.get(server.address(), () => {})
423
+ })
424
+
425
+ t.after(() => server.close())
426
+
427
+ function handler (req, res) {
428
+ req.info = { remotePort: 3000 }
429
+ const serialized = serializers.reqSerializer(req)
430
+ p.strictEqual(serialized.remotePort, 3000)
431
+ res.end()
432
+ }
433
+
434
+ await p.completed
435
+ })
436
+
437
+ test('req.query is available', async (t) => {
438
+ const p = tspl(t, { plan: 1 })
439
+
440
+ const server = http.createServer(handler)
441
+ server.unref()
442
+ server.listen(0, () => {
443
+ http.get(server.address(), () => {})
444
+ })
445
+
446
+ t.after(() => server.close())
447
+
448
+ function handler (req, res) {
449
+ req.query = '/foo?bar=foobar&bar=foo'
450
+ const serialized = serializers.reqSerializer(req)
451
+ p.strictEqual(serialized.query, '/foo?bar=foobar&bar=foo')
452
+ res.end()
453
+ }
454
+
455
+ await p.completed
456
+ })
457
+
458
+ test('req.params is available', async (t) => {
459
+ const p = tspl(t, { plan: 1 })
460
+
461
+ const server = http.createServer(handler)
462
+ server.unref()
463
+ server.listen(0, () => {
464
+ http.get(server.address(), () => {})
465
+ })
466
+
467
+ t.after(() => server.close())
468
+
469
+ function handler (req, res) {
470
+ req.params = '/foo/bar'
471
+ const serialized = serializers.reqSerializer(req)
472
+ p.strictEqual(serialized.params, '/foo/bar')
473
+ res.end()
474
+ }
475
+
476
+ await p.completed
477
+ })
@@ -0,0 +1,120 @@
1
+ 'use strict'
2
+
3
+ /* eslint-disable no-prototype-builtins */
4
+
5
+ const { tspl } = require('@matteo.collina/tspl')
6
+ const http = require('node:http')
7
+ const { test } = require('node:test')
8
+ const serializers = require('../lib/res')
9
+ const { wrapResponseSerializer } = require('../')
10
+
11
+ test('res.raw is not enumerable', async (t) => {
12
+ const p = tspl(t, { plan: 1 })
13
+
14
+ const server = http.createServer(handler)
15
+ server.unref()
16
+ server.listen(0, () => {
17
+ http.get(server.address(), () => {})
18
+ })
19
+
20
+ t.after(() => server.close())
21
+
22
+ function handler (_req, res) {
23
+ const serialized = serializers.resSerializer(res)
24
+ p.strictEqual(serialized.propertyIsEnumerable('raw'), false)
25
+ res.end()
26
+ }
27
+
28
+ await p.completed
29
+ })
30
+
31
+ test('res.raw is available', async (t) => {
32
+ const p = tspl(t, { plan: 2 })
33
+
34
+ const server = http.createServer(handler)
35
+ server.unref()
36
+ server.listen(0, () => {
37
+ http.get(server.address(), () => {})
38
+ })
39
+
40
+ t.after(() => server.close())
41
+
42
+ function handler (_req, res) {
43
+ res.statusCode = 200
44
+ const serialized = serializers.resSerializer(res)
45
+ p.ok(serialized.raw)
46
+ p.strictEqual(serialized.raw.statusCode, 200)
47
+ res.end()
48
+ }
49
+
50
+ await p.completed
51
+ })
52
+
53
+ test('can wrap response serializers', async (t) => {
54
+ const p = tspl(t, { plan: 3 })
55
+
56
+ const server = http.createServer(handler)
57
+ server.unref()
58
+ server.listen(0, () => {
59
+ http.get(server.address(), () => {})
60
+ })
61
+
62
+ t.after(() => server.close())
63
+
64
+ const serializer = wrapResponseSerializer(function (res) {
65
+ p.ok(res.statusCode)
66
+ p.strictEqual(res.statusCode, 200)
67
+ delete res.statusCode
68
+ return res
69
+ })
70
+
71
+ function handler (_req, res) {
72
+ res.end()
73
+ res.statusCode = 200
74
+ const serialized = serializer(res)
75
+ p.ok(!serialized.statusCode)
76
+ }
77
+
78
+ await p.completed
79
+ })
80
+
81
+ test('res.headers is serialized', async (t) => {
82
+ const p = tspl(t, { plan: 1 })
83
+
84
+ const server = http.createServer(handler)
85
+ server.unref()
86
+ server.listen(0, () => {
87
+ http.get(server.address(), () => {})
88
+ })
89
+
90
+ t.after(() => server.close())
91
+
92
+ function handler (_req, res) {
93
+ res.setHeader('x-custom', 'y')
94
+ const serialized = serializers.resSerializer(res)
95
+ p.strictEqual(serialized.headers['x-custom'], 'y')
96
+ res.end()
97
+ }
98
+
99
+ await p.completed
100
+ })
101
+
102
+ test('req.url will be obtained from input request url when input request url is not an object', async (t) => {
103
+ const p = tspl(t, { plan: 1 })
104
+
105
+ const server = http.createServer(handler)
106
+ server.unref()
107
+ server.listen(0, () => {
108
+ http.get(server.address(), () => {})
109
+ })
110
+
111
+ t.after(() => server.close())
112
+
113
+ function handler (_req, res) {
114
+ const serialized = serializers.resSerializer(res)
115
+ p.strictEqual(serialized.statusCode, null)
116
+ res.end()
117
+ }
118
+
119
+ await p.completed
120
+ })
@@ -0,0 +1,71 @@
1
+ import {IncomingMessage, ServerResponse} from "http";
2
+ import {
3
+ err,
4
+ errWithCause,
5
+ req,
6
+ res,
7
+ SerializedError,
8
+ SerializedRequest,
9
+ wrapErrorSerializer,
10
+ wrapRequestSerializer,
11
+ wrapResponseSerializer,
12
+ SerializedResponse
13
+ } from '../../';
14
+
15
+ const customErrorSerializer = (error: SerializedError) => {
16
+ return {
17
+ myOwnError: {
18
+ data: `${error.type}-${error.message}\n\n${error.stack}`,
19
+ }
20
+ };
21
+ };
22
+
23
+ const customRequestSerializer = (req: SerializedRequest) => {
24
+ const {
25
+ headers,
26
+ id,
27
+ method,
28
+ raw,
29
+ remoteAddress,
30
+ remotePort,
31
+ url,
32
+ query,
33
+ params,
34
+ } = req;
35
+ return {
36
+ myOwnRequest: {
37
+ data: `${method}-${id}-${remoteAddress}-${remotePort}-${url}`,
38
+ headers,
39
+ raw,
40
+ }
41
+ };
42
+ };
43
+
44
+ const customResponseSerializer = (res: SerializedResponse) => {
45
+ const {headers, raw, statusCode} = res;
46
+ return {
47
+ myOwnResponse: {
48
+ data: statusCode,
49
+ headers,
50
+ raw,
51
+ }
52
+ };
53
+ };
54
+
55
+ const fakeError = new Error('A fake error for testing');
56
+ const serializedError: SerializedError = err(fakeError);
57
+ const mySerializer = wrapErrorSerializer(customErrorSerializer);
58
+
59
+ const fakeErrorWithCause = new Error('A fake error for testing with cause', { cause: new Error('An inner fake error') });
60
+ const serializedErrorWithCause: SerializedError = errWithCause(fakeError);
61
+
62
+ const request: IncomingMessage = {} as IncomingMessage
63
+ const serializedRequest: SerializedRequest = req(request);
64
+ const myReqSerializer = wrapRequestSerializer(customRequestSerializer);
65
+
66
+ const response: ServerResponse = {} as ServerResponse
67
+ const myResSerializer = wrapResponseSerializer(customResponseSerializer);
68
+ const serializedResponse = res(response);
69
+
70
+ myResSerializer(response)
71
+
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es6",
4
+ "lib": [ "es2022" ],
5
+ "module": "commonjs",
6
+ "noEmit": true,
7
+ "strict": true
8
+ },
9
+ "include": [
10
+ "./test/types/*.test-d.ts",
11
+ "./index.d.ts"
12
+ ]
13
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.