sync mode on
This commit is contained in:
1 parent
1818932244
commit
1174b328e5
415 files changed
+44294
-1182
No files matched your search
+1
@@ -0,0 +1 @@
|
||||
../pino/bin.js
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
{
|
||||
"name": "mmpSearch",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"node_modules/@pinojs/redact": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@pinojs/redact/-/redact-0.4.0.tgz",
|
||||
"integrity": "sha512-k2ENnmBugE/rzQfEcdWHcCY+/FM3VLzH9cYEsbdsoqrvzAKRhUZeRNhAZvB8OitQJ1TBed3yqWtdjzS6wJKBwg=="
|
||||
},
|
||||
"node_modules/atomic-sleep": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz",
|
||||
"integrity": "sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==",
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/on-exit-leak-free": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/on-exit-leak-free/-/on-exit-leak-free-2.1.2.tgz",
|
||||
"integrity": "sha512-0eJJY6hXLGf1udHwfNftBqH+g73EU4B504nZeKpz1sYRKafAghwxEJunB2O7rDZkL4PGfsMVnTXZ2EjibbqcsA==",
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/pino": {
|
||||
"version": "10.1.0",
|
||||
"resolved": "https://registry.npmjs.org/pino/-/pino-10.1.0.tgz",
|
||||
"integrity": "sha512-0zZC2ygfdqvqK8zJIr1e+wT1T/L+LF6qvqvbzEQ6tiMAoTqEVK9a1K3YRu8HEUvGEvNqZyPJTtb2sNIoTkB83w==",
|
||||
"dependencies": {
|
||||
"@pinojs/redact": "^0.4.0",
|
||||
"atomic-sleep": "^1.0.0",
|
||||
"on-exit-leak-free": "^2.1.0",
|
||||
"pino-abstract-transport": "^2.0.0",
|
||||
"pino-std-serializers": "^7.0.0",
|
||||
"process-warning": "^5.0.0",
|
||||
"quick-format-unescaped": "^4.0.3",
|
||||
"real-require": "^0.2.0",
|
||||
"safe-stable-stringify": "^2.3.1",
|
||||
"sonic-boom": "^4.0.1",
|
||||
"thread-stream": "^3.0.0"
|
||||
},
|
||||
"bin": {
|
||||
"pino": "bin.js"
|
||||
}
|
||||
},
|
||||
"node_modules/pino-abstract-transport": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/pino-abstract-transport/-/pino-abstract-transport-2.0.0.tgz",
|
||||
"integrity": "sha512-F63x5tizV6WCh4R6RHyi2Ml+M70DNRXt/+HANowMflpgGFMAym/VKm6G7ZOQRjqN7XbGxK1Lg9t6ZrtzOaivMw==",
|
||||
"dependencies": {
|
||||
"split2": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/pino-std-serializers": {
|
||||
"version": "7.0.0",
|
||||
"resolved": "https://registry.npmjs.org/pino-std-serializers/-/pino-std-serializers-7.0.0.tgz",
|
||||
"integrity": "sha512-e906FRY0+tV27iq4juKzSYPbUj2do2X2JX4EzSca1631EB2QJQUqGbDuERal7LCtOpxl6x3+nvo9NPZcmjkiFA=="
|
||||
},
|
||||
"node_modules/process-warning": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "https://registry.npmjs.org/process-warning/-/process-warning-5.0.0.tgz",
|
||||
"integrity": "sha512-a39t9ApHNx2L4+HBnQKqxxHNs1r7KF+Intd8Q/g1bUh6q0WIp9voPXJ/x0j+ZL45KF1pJd9+q2jLIRMfvEshkA==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/fastify"
|
||||
},
|
||||
{
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/fastify"
|
||||
}
|
||||
]
|
||||
},
|
||||
"node_modules/quick-format-unescaped": {
|
||||
"version": "4.0.4",
|
||||
"resolved": "https://registry.npmjs.org/quick-format-unescaped/-/quick-format-unescaped-4.0.4.tgz",
|
||||
"integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg=="
|
||||
},
|
||||
"node_modules/real-require": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmjs.org/real-require/-/real-require-0.2.0.tgz",
|
||||
"integrity": "sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg==",
|
||||
"engines": {
|
||||
"node": ">= 12.13.0"
|
||||
}
|
||||
},
|
||||
"node_modules/safe-stable-stringify": {
|
||||
"version": "2.5.0",
|
||||
"resolved": "https://registry.npmjs.org/safe-stable-stringify/-/safe-stable-stringify-2.5.0.tgz",
|
||||
"integrity": "sha512-b3rppTKm9T+PsVCBEOUR46GWI7fdOs00VKZ1+9c1EWDaDMvjQc6tUwuFyIprgGgTcWoVHSKrU8H31ZHA2e0RHA==",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/sonic-boom": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-4.2.0.tgz",
|
||||
"integrity": "sha512-INb7TM37/mAcsGmc9hyyI6+QR3rR1zVRu36B0NeGXKnOOLiZOfER5SA+N7X7k3yUYRzLWafduTDvJAfDswwEww==",
|
||||
"dependencies": {
|
||||
"atomic-sleep": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/split2": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",
|
||||
"integrity": "sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==",
|
||||
"engines": {
|
||||
"node": ">= 10.x"
|
||||
}
|
||||
},
|
||||
"node_modules/thread-stream": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/thread-stream/-/thread-stream-3.1.0.tgz",
|
||||
"integrity": "sha512-OqyPZ9u96VohAyMfJykzmivOrY2wfMSf3C5TtFJVgN+Hm6aj+voFhlK+kZEIv2FBh1X6Xp3DlnCOfEQ3B2J86A==",
|
||||
"dependencies": {
|
||||
"real-require": "^0.2.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
open-pull-requests-limit: 10
|
||||
|
||||
- package-ecosystem: "npm"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
open-pull-requests-limit: 10
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- 'v*'
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
|
||||
# This allows a subsequently queued workflow run to interrupt previous runs
|
||||
concurrency:
|
||||
group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: ${{ matrix.node-version }} ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
permissions:
|
||||
contents: read
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [macOS-latest, windows-latest, ubuntu-latest]
|
||||
node-version: [18, 20, 22, 24]
|
||||
|
||||
steps:
|
||||
- name: Check out repo
|
||||
uses: actions/checkout@v5.0.0
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Setup Node ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm i --ignore-scripts
|
||||
|
||||
- name: Run tests
|
||||
run: npm run test
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
name: Publish release
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
description: 'The version number to tag and release'
|
||||
required: true
|
||||
type: string
|
||||
prerelease:
|
||||
description: 'Release as pre-release'
|
||||
required: false
|
||||
type: boolean
|
||||
default: false
|
||||
|
||||
jobs:
|
||||
release-npm:
|
||||
runs-on: ubuntu-latest
|
||||
environment: main
|
||||
permissions:
|
||||
contents: write
|
||||
id-token: write
|
||||
steps:
|
||||
- uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v4
|
||||
- uses: actions/setup-node@v5
|
||||
with:
|
||||
node-version: '22'
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
- run: npm install npm -g
|
||||
- run: npm install
|
||||
- name: Change version number and sync
|
||||
run: |
|
||||
node scripts/sync-version.mjs ${{ inputs.version }}
|
||||
- name: GIT commit and push all changed files
|
||||
run: |
|
||||
git config --global user.name "mcollina"
|
||||
git config --global user.email "hello@matteocollina.com"
|
||||
git commit -n -a -m "Bumped v${{ inputs.version }}"
|
||||
git push origin HEAD:${{ github.ref }}
|
||||
- run: npm publish --access public --tag ${{ inputs.prerelease == true && 'next' || 'latest' }}
|
||||
- name: 'Create release notes'
|
||||
run: |
|
||||
npx @matteo.collina/release-notes -a ${{ secrets.GITHUB_TOKEN }} -t v${{ inputs.version }} -r redact -o pinojs ${{ github.event.inputs.prerelease == 'true' && '-p' || '' }} -c ${{ github.ref }}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 pinojs contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+350
@@ -0,0 +1,350 @@
|
||||
# @pinojs/redact
|
||||
|
||||
> Smart object redaction for JavaScript applications - safe AND fast!
|
||||
|
||||
Redact JS objects with the same API as [fast-redact](https://github.com/davidmarkclements/fast-redact), but uses innovative **selective cloning** instead of mutating the original. This provides immutability guarantees with **performance competitive** to fast-redact for real-world usage patterns.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install @pinojs/redact
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const slowRedact = require('@pinojs/redact')
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['headers.cookie', 'headers.authorization', 'user.password']
|
||||
})
|
||||
|
||||
const obj = {
|
||||
headers: {
|
||||
cookie: 'secret-session-token',
|
||||
authorization: 'Bearer abc123',
|
||||
'x-forwarded-for': '192.168.1.1'
|
||||
},
|
||||
user: {
|
||||
name: 'john',
|
||||
password: 'secret123'
|
||||
}
|
||||
}
|
||||
|
||||
console.log(redact(obj))
|
||||
// Output: {"headers":{"cookie":"[REDACTED]","authorization":"[REDACTED]","x-forwarded-for":"192.168.1.1"},"user":{"name":"john","password":"[REDACTED]"}}
|
||||
|
||||
// Original object is completely unchanged:
|
||||
console.log(obj.headers.cookie) // 'secret-session-token'
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### slowRedact(options) → Function
|
||||
|
||||
Creates a redaction function with the specified options.
|
||||
|
||||
#### Options
|
||||
|
||||
- **paths** `string[]` (required): An array of strings describing the nested location of a key in an object
|
||||
- **censor** `any` (optional, default: `'[REDACTED]'`): The value to replace sensitive data with. Can be a static value or function.
|
||||
- **serialize** `Function|boolean` (optional, default: `JSON.stringify`): Serialization function. Set to `false` to return the redacted object.
|
||||
- **remove** `boolean` (optional, default: `false`): Remove redacted keys from serialized output
|
||||
- **strict** `boolean` (optional, default: `true`): Throw on non-object values or pass through primitives
|
||||
|
||||
#### Path Syntax
|
||||
|
||||
Supports the same path syntax as fast-redact:
|
||||
|
||||
- **Dot notation**: `'user.name'`, `'headers.cookie'`
|
||||
- **Bracket notation**: `'user["password"]'`, `'headers["X-Forwarded-For"]'`
|
||||
- **Array indices**: `'users[0].password'`, `'items[1].secret'`
|
||||
- **Wildcards**:
|
||||
- Terminal: `'users.*.password'` (redacts password for all users)
|
||||
- Intermediate: `'*.password'` (redacts password at any level)
|
||||
- Array wildcard: `'items.*'` (redacts all array elements)
|
||||
|
||||
#### Examples
|
||||
|
||||
**Custom censor value:**
|
||||
```js
|
||||
const redact = slowRedact({
|
||||
paths: ['password'],
|
||||
censor: '***HIDDEN***'
|
||||
})
|
||||
```
|
||||
|
||||
**Dynamic censor function:**
|
||||
```js
|
||||
const redact = slowRedact({
|
||||
paths: ['password'],
|
||||
censor: (value, path) => `REDACTED:${path}`
|
||||
})
|
||||
```
|
||||
|
||||
**Return object instead of JSON string:**
|
||||
```js
|
||||
const redact = slowRedact({
|
||||
paths: ['secret'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact({ secret: 'hidden', public: 'data' })
|
||||
console.log(result.secret) // '[REDACTED]'
|
||||
console.log(result.public) // 'data'
|
||||
|
||||
// Restore original values
|
||||
const restored = result.restore()
|
||||
console.log(restored.secret) // 'hidden'
|
||||
```
|
||||
|
||||
**Custom serialization:**
|
||||
```js
|
||||
const redact = slowRedact({
|
||||
paths: ['password'],
|
||||
serialize: obj => JSON.stringify(obj, null, 2)
|
||||
})
|
||||
```
|
||||
|
||||
**Remove keys instead of redacting:**
|
||||
```js
|
||||
const redact = slowRedact({
|
||||
paths: ['password', 'user.secret'],
|
||||
remove: true
|
||||
})
|
||||
|
||||
const obj = { username: 'john', password: 'secret123', user: { name: 'Jane', secret: 'hidden' } }
|
||||
console.log(redact(obj))
|
||||
// Output: {"username":"john","user":{"name":"Jane"}}
|
||||
// Note: 'password' and 'user.secret' are completely absent, not redacted
|
||||
```
|
||||
|
||||
**Wildcard patterns:**
|
||||
```js
|
||||
// Redact all properties in secrets object
|
||||
const redact1 = slowRedact({ paths: ['secrets.*'] })
|
||||
|
||||
// Redact password for any user
|
||||
const redact2 = slowRedact({ paths: ['users.*.password'] })
|
||||
|
||||
// Redact all items in an array
|
||||
const redact3 = slowRedact({ paths: ['items.*'] })
|
||||
|
||||
// Remove all secrets instead of redacting them
|
||||
const redact4 = slowRedact({ paths: ['secrets.*'], remove: true })
|
||||
```
|
||||
|
||||
## Key Differences from fast-redact
|
||||
|
||||
### Safety First
|
||||
- **No mutation**: Original objects are never modified
|
||||
- **Selective cloning**: Only clones paths that need redaction, shares references for everything else
|
||||
- **Restore capability**: Can restore original values when `serialize: false`
|
||||
|
||||
### Feature Compatibility
|
||||
- **Remove option**: Full compatibility with fast-redact's `remove: true` option to completely omit keys from output
|
||||
- **All path patterns**: Supports same syntax including wildcards, bracket notation, and array indices
|
||||
- **Censor functions**: Dynamic censoring with path information passed as arrays
|
||||
- **Serialization**: Custom serializers and `serialize: false` mode
|
||||
|
||||
### Smart Performance Approach
|
||||
- **Selective cloning**: Analyzes redaction paths and only clones necessary object branches
|
||||
- **Reference sharing**: Non-redacted properties maintain original object references
|
||||
- **Memory efficiency**: Dramatically reduced memory usage for large objects with minimal redaction
|
||||
- **Setup-time optimization**: Path analysis happens once during setup, not per redaction
|
||||
|
||||
### When to Use @pinojs/redact
|
||||
- When immutability is critical
|
||||
- When you need to preserve original objects
|
||||
- When objects are shared across multiple contexts
|
||||
- In functional programming environments
|
||||
- When debugging and you need to compare before/after
|
||||
- **Large objects with selective redaction** (now performance-competitive!)
|
||||
- When memory efficiency with reference sharing is important
|
||||
|
||||
### When to Use fast-redact
|
||||
- When absolute maximum performance is critical
|
||||
- In extremely high-throughput scenarios (>100,000 ops/sec)
|
||||
- When you control the object lifecycle and mutation is acceptable
|
||||
- Very small objects where setup overhead matters
|
||||
|
||||
## Performance Benchmarks
|
||||
|
||||
@pinojs/redact uses **selective cloning** that provides good performance while maintaining immutability guarantees:
|
||||
|
||||
### Performance Results
|
||||
|
||||
| Operation Type | @pinojs/redact | fast-redact | Performance Ratio |
|
||||
|---------------|-------------|-------------|-------------------|
|
||||
| **Small objects** | ~690ns | ~200ns | ~3.5x slower |
|
||||
| **Large objects (minimal redaction)** | **~18μs** | ~17μs | **~same performance** |
|
||||
| **Large objects (wildcards)** | **~48μs** | ~37μs | **~1.3x slower** |
|
||||
| **No redaction (large objects)** | **~18μs** | ~17μs | **~same performance** |
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
@pinojs/redact is performance-competitive with fast-redact for large objects.
|
||||
|
||||
1. **Selective cloning approach**: Only clones object paths that need redaction
|
||||
2. **Reference sharing**: Non-redacted properties share original object references
|
||||
3. **Setup-time optimization**: Path analysis happens once, not per redaction
|
||||
4. **Memory efficiency**: Dramatically reduced memory usage for typical use cases
|
||||
|
||||
### Benchmark Details
|
||||
|
||||
**Small Objects (~180 bytes)**:
|
||||
- @pinojs/redact: **690ns** per operation
|
||||
- fast-redact: **200ns** per operation
|
||||
- **Slight setup overhead for small objects**
|
||||
|
||||
**Large Objects (~18KB, minimal redaction)**:
|
||||
- @pinojs/redact: **18μs** per operation
|
||||
- fast-redact: **17μs** per operation
|
||||
- Near-identical performance
|
||||
|
||||
**Large Objects (~18KB, wildcard patterns)**:
|
||||
- @pinojs/redact: **48μs** per operation
|
||||
- fast-redact: **37μs** per operation
|
||||
- Competitive performance for complex patterns
|
||||
|
||||
**Memory Considerations**:
|
||||
- @pinojs/redact: **Selective reference sharing** (much lower memory usage than before)
|
||||
- fast-redact: Mutates in-place (lowest memory usage)
|
||||
- Large objects with few redacted paths now share most references
|
||||
|
||||
### When Performance Matters
|
||||
|
||||
Choose **fast-redact** when:
|
||||
- Absolute maximum performance is critical (>100,000 ops/sec)
|
||||
- Working with very small objects frequently
|
||||
- Mutation is acceptable and controlled
|
||||
- Every microsecond counts
|
||||
|
||||
Choose **@pinojs/redact** when:
|
||||
- Immutability is required (with competitive performance)
|
||||
- Objects are shared across contexts
|
||||
- Large objects with selective redaction
|
||||
- Memory efficiency through reference sharing is important
|
||||
- Safety and functionality are priorities
|
||||
- Most production applications (performance gap is minimal)
|
||||
|
||||
Run benchmarks yourself:
|
||||
```bash
|
||||
npm run bench
|
||||
```
|
||||
|
||||
## How Selective Cloning Works
|
||||
|
||||
@pinojs/redact uses an innovative **selective cloning** approach that provides immutability guarantees while dramatically improving performance:
|
||||
|
||||
### Traditional Approach (before optimization)
|
||||
```js
|
||||
// Old approach: Deep clone entire object, then redact
|
||||
const fullClone = deepClone(originalObject) // Clone everything
|
||||
redact(fullClone, paths) // Then redact specific paths
|
||||
```
|
||||
|
||||
### Selective Cloning Approach (current)
|
||||
```js
|
||||
// New approach: Analyze paths, clone only what's needed
|
||||
const pathStructure = buildPathStructure(paths) // One-time setup
|
||||
const selectiveClone = cloneOnlyNeededPaths(obj, pathStructure) // Smart cloning
|
||||
redact(selectiveClone, paths) // Redact pre-identified paths
|
||||
```
|
||||
|
||||
### Key Innovations
|
||||
|
||||
1. **Path Analysis**: Pre-processes redaction paths into an efficient tree structure
|
||||
2. **Selective Cloning**: Only creates new objects for branches that contain redaction targets
|
||||
3. **Reference Sharing**: Non-redacted properties maintain exact same object references
|
||||
4. **Setup Optimization**: Path parsing happens once during redactor creation, not per redaction
|
||||
|
||||
### Example: Reference Sharing in Action
|
||||
|
||||
```js
|
||||
const largeConfig = {
|
||||
database: { /* large config object */ },
|
||||
api: { /* another large config */ },
|
||||
secrets: { password: 'hidden', apiKey: 'secret' }
|
||||
}
|
||||
|
||||
const redact = slowRedact({ paths: ['secrets.password'] })
|
||||
const result = redact(largeConfig)
|
||||
|
||||
// Only secrets object is cloned, database and api share original references
|
||||
console.log(result.database === largeConfig.database) // true - shared reference!
|
||||
console.log(result.api === largeConfig.api) // true - shared reference!
|
||||
console.log(result.secrets === largeConfig.secrets) // false - cloned for redaction
|
||||
```
|
||||
|
||||
This approach provides **immutability where it matters** while **sharing references where it's safe**.
|
||||
|
||||
## Remove Option
|
||||
|
||||
The `remove: true` option provides full compatibility with fast-redact's key removal functionality:
|
||||
|
||||
```js
|
||||
const redact = slowRedact({
|
||||
paths: ['password', 'secrets.*', 'users.*.credentials'],
|
||||
remove: true
|
||||
})
|
||||
|
||||
const data = {
|
||||
username: 'john',
|
||||
password: 'secret123',
|
||||
secrets: { apiKey: 'abc', token: 'xyz' },
|
||||
users: [
|
||||
{ name: 'Alice', credentials: { password: 'pass1' } },
|
||||
{ name: 'Bob', credentials: { password: 'pass2' } }
|
||||
]
|
||||
}
|
||||
|
||||
console.log(redact(data))
|
||||
// Output: {"username":"john","secrets":{},"users":[{"name":"Alice"},{"name":"Bob"}]}
|
||||
```
|
||||
|
||||
### Remove vs Redact Behavior
|
||||
|
||||
| Option | Behavior | Output Example |
|
||||
|--------|----------|----------------|
|
||||
| Default (redact) | Replaces values with censor | `{"password":"[REDACTED]"}` |
|
||||
| `remove: true` | Completely omits keys | `{}` |
|
||||
|
||||
### Compatibility Notes
|
||||
|
||||
- **Same output as fast-redact**: Identical JSON output when using `remove: true`
|
||||
- **Wildcard support**: Works with all wildcard patterns (`*`, `users.*`, `items.*.secret`)
|
||||
- **Array handling**: Array items are set to `undefined` (omitted in JSON output)
|
||||
- **Nested paths**: Supports deep removal (`users.*.credentials.password`)
|
||||
- **Serialize compatibility**: Only works with `JSON.stringify` serializer (like fast-redact)
|
||||
|
||||
## Testing
|
||||
|
||||
```bash
|
||||
# Run unit tests
|
||||
npm test
|
||||
|
||||
# Run integration tests comparing with fast-redact
|
||||
npm run test:integration
|
||||
|
||||
# Run all tests (unit + integration)
|
||||
npm run test:all
|
||||
|
||||
# Run benchmarks
|
||||
npm run bench
|
||||
```
|
||||
|
||||
### Test Coverage
|
||||
|
||||
- **16 unit tests**: Core functionality and edge cases
|
||||
- **16 integration tests**: Output compatibility with fast-redact
|
||||
- **All major features**: Paths, wildcards, serialization, custom censors
|
||||
- **Performance benchmarks**: Direct comparison with fast-redact
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
## Contributing
|
||||
|
||||
Pull requests welcome! Please ensure all tests pass and add tests for new features.
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
const { bench, group, run } = require('mitata')
|
||||
const slowRedact = require('../index.js')
|
||||
const fastRedact = require('fast-redact')
|
||||
|
||||
// Test objects
|
||||
const smallObj = {
|
||||
user: { name: 'john', password: 'secret123' },
|
||||
headers: { cookie: 'session-token', authorization: 'Bearer abc123' }
|
||||
}
|
||||
|
||||
const largeObj = {
|
||||
users: [],
|
||||
metadata: {
|
||||
version: '1.0.0',
|
||||
secret: 'app-secret-key',
|
||||
database: {
|
||||
host: 'localhost',
|
||||
password: 'db-password'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Populate users array with for loop instead of Array.from
|
||||
for (let i = 0; i < 100; i++) {
|
||||
largeObj.users.push({
|
||||
id: i,
|
||||
name: `user${i}`,
|
||||
email: `user${i}@example.com`,
|
||||
password: `secret${i}`,
|
||||
profile: {
|
||||
age: 20 + (i % 50),
|
||||
preferences: {
|
||||
theme: 'dark',
|
||||
notifications: true,
|
||||
apiKey: `key-${i}-secret`
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Redaction configurations
|
||||
const basicSlowRedact = slowRedact({
|
||||
paths: ['user.password', 'headers.cookie']
|
||||
})
|
||||
|
||||
const basicFastRedact = fastRedact({
|
||||
paths: ['user.password', 'headers.cookie']
|
||||
})
|
||||
|
||||
const wildcardSlowRedact = slowRedact({
|
||||
paths: ['users.*.password', 'users.*.profile.preferences.apiKey']
|
||||
})
|
||||
|
||||
const wildcardFastRedact = fastRedact({
|
||||
paths: ['users.*.password', 'users.*.profile.preferences.apiKey']
|
||||
})
|
||||
|
||||
const deepSlowRedact = slowRedact({
|
||||
paths: ['metadata.secret', 'metadata.database.password']
|
||||
})
|
||||
|
||||
const deepFastRedact = fastRedact({
|
||||
paths: ['metadata.secret', 'metadata.database.password']
|
||||
})
|
||||
|
||||
group('Small Object Redaction - @pinojs/redact', () => {
|
||||
bench('basic paths', () => {
|
||||
basicSlowRedact(smallObj)
|
||||
})
|
||||
|
||||
bench('serialize: false', () => {
|
||||
const redact = slowRedact({
|
||||
paths: ['user.password'],
|
||||
serialize: false
|
||||
})
|
||||
redact(smallObj)
|
||||
})
|
||||
|
||||
bench('custom censor function', () => {
|
||||
const redact = slowRedact({
|
||||
paths: ['user.password'],
|
||||
censor: (value, path) => `HIDDEN:${path}`
|
||||
})
|
||||
redact(smallObj)
|
||||
})
|
||||
})
|
||||
|
||||
group('Small Object Redaction - fast-redact', () => {
|
||||
bench('basic paths', () => {
|
||||
basicFastRedact(smallObj)
|
||||
})
|
||||
|
||||
bench('serialize: false', () => {
|
||||
const redact = fastRedact({
|
||||
paths: ['user.password'],
|
||||
serialize: false
|
||||
})
|
||||
redact(smallObj)
|
||||
})
|
||||
|
||||
bench('custom censor function', () => {
|
||||
const redact = fastRedact({
|
||||
paths: ['user.password'],
|
||||
censor: (value, path) => `HIDDEN:${path}`
|
||||
})
|
||||
redact(smallObj)
|
||||
})
|
||||
})
|
||||
|
||||
group('Large Object Redaction - @pinojs/redact', () => {
|
||||
bench('wildcard patterns', () => {
|
||||
wildcardSlowRedact(largeObj)
|
||||
})
|
||||
|
||||
bench('deep nested paths', () => {
|
||||
deepSlowRedact(largeObj)
|
||||
})
|
||||
|
||||
bench('multiple wildcards', () => {
|
||||
const redact = slowRedact({
|
||||
paths: ['users.*.password', 'users.*.profile.preferences.*']
|
||||
})
|
||||
redact(largeObj)
|
||||
})
|
||||
})
|
||||
|
||||
group('Large Object Redaction - fast-redact', () => {
|
||||
bench('wildcard patterns', () => {
|
||||
wildcardFastRedact(largeObj)
|
||||
})
|
||||
|
||||
bench('deep nested paths', () => {
|
||||
deepFastRedact(largeObj)
|
||||
})
|
||||
|
||||
bench('multiple wildcards', () => {
|
||||
const redact = fastRedact({
|
||||
paths: ['users.*.password', 'users.*.profile.preferences.*']
|
||||
})
|
||||
redact(largeObj)
|
||||
})
|
||||
})
|
||||
|
||||
group('Direct Performance Comparison', () => {
|
||||
bench('@pinojs/redact - basic paths', () => {
|
||||
basicSlowRedact(smallObj)
|
||||
})
|
||||
|
||||
bench('fast-redact - basic paths', () => {
|
||||
basicFastRedact(smallObj)
|
||||
})
|
||||
|
||||
bench('@pinojs/redact - wildcards', () => {
|
||||
wildcardSlowRedact(largeObj)
|
||||
})
|
||||
|
||||
bench('fast-redact - wildcards', () => {
|
||||
wildcardFastRedact(largeObj)
|
||||
})
|
||||
})
|
||||
|
||||
group('Object Cloning Overhead', () => {
|
||||
bench('@pinojs/redact - no redaction (clone only)', () => {
|
||||
const redact = slowRedact({ paths: [] })
|
||||
redact(smallObj)
|
||||
})
|
||||
|
||||
bench('fast-redact - no redaction', () => {
|
||||
const redact = fastRedact({ paths: [] })
|
||||
redact(smallObj)
|
||||
})
|
||||
|
||||
bench('@pinojs/redact - large object clone', () => {
|
||||
const redact = slowRedact({ paths: [] })
|
||||
redact(largeObj)
|
||||
})
|
||||
|
||||
bench('fast-redact - large object', () => {
|
||||
const redact = fastRedact({ paths: [] })
|
||||
redact(largeObj)
|
||||
})
|
||||
})
|
||||
|
||||
run()
|
||||
+1
@@ -0,0 +1 @@
|
||||
module.exports = require('neostandard')()
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
export = F;
|
||||
|
||||
/**
|
||||
* When called without any options, or with a zero length paths array, @pinojs/redact will return JSON.stringify or the serialize option, if set.
|
||||
* @param redactOptions
|
||||
* @param redactOptions.paths An array of strings describing the nested location of a key in an object.
|
||||
* @param redactOptions.censor This is the value which overwrites redacted properties.
|
||||
* @param redactOptions.remove The remove option, when set to true will cause keys to be removed from the serialized output.
|
||||
* @param redactOptions.serialize The serialize option may either be a function or a boolean. If a function is supplied, this will be used to serialize the redacted object.
|
||||
* @param redactOptions.strict The strict option, when set to true, will cause the redactor function to throw if instead of an object it finds a primitive.
|
||||
* @returns Redacted value from input
|
||||
*/
|
||||
declare function F(
|
||||
redactOptions: F.RedactOptionsNoSerialize
|
||||
): F.redactFnNoSerialize;
|
||||
declare function F(redactOptions?: F.RedactOptions): F.redactFn;
|
||||
|
||||
declare namespace F {
|
||||
/** Redacts input */
|
||||
type redactFn = <T>(input: T) => string | T;
|
||||
|
||||
/** Redacts input without serialization */
|
||||
type redactFnNoSerialize = redactFn & {
|
||||
/** Method that allowing the redacted keys to be restored with the original data. Supplied only when serialize option set to false. */
|
||||
restore<T>(input: T): T;
|
||||
};
|
||||
|
||||
interface RedactOptions {
|
||||
/** An array of strings describing the nested location of a key in an object. */
|
||||
paths?: string[] | undefined;
|
||||
|
||||
/** This is the value which overwrites redacted properties. */
|
||||
censor?: string | ((v: any) => any) | undefined;
|
||||
|
||||
/** The remove option, when set to true will cause keys to be removed from the serialized output. */
|
||||
remove?: boolean | undefined;
|
||||
|
||||
/**
|
||||
* The serialize option may either be a function or a boolean. If a function is supplied, this will be used to serialize the redacted object.
|
||||
* The default serialize is the function JSON.stringify
|
||||
*/
|
||||
serialize?: boolean | ((v: any) => any) | undefined;
|
||||
|
||||
/** The strict option, when set to true, will cause the redactor function to throw if instead of an object it finds a primitive. */
|
||||
strict?: boolean | undefined;
|
||||
}
|
||||
|
||||
/** RedactOptions without serialization. Instead of the serialized object, the output of the redactor function will be the mutated object itself. */
|
||||
interface RedactOptionsNoSerialize extends RedactOptions {
|
||||
serialize: false;
|
||||
}
|
||||
}
|
||||
+529
@@ -0,0 +1,529 @@
|
||||
'use strict'
|
||||
|
||||
function deepClone (obj) {
|
||||
if (obj === null || typeof obj !== 'object') {
|
||||
return obj
|
||||
}
|
||||
|
||||
if (obj instanceof Date) {
|
||||
return new Date(obj.getTime())
|
||||
}
|
||||
|
||||
if (obj instanceof Array) {
|
||||
const cloned = []
|
||||
for (let i = 0; i < obj.length; i++) {
|
||||
cloned[i] = deepClone(obj[i])
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
if (typeof obj === 'object') {
|
||||
const cloned = Object.create(Object.getPrototypeOf(obj))
|
||||
for (const key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
cloned[key] = deepClone(obj[key])
|
||||
}
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
function parsePath (path) {
|
||||
const parts = []
|
||||
let current = ''
|
||||
let inBrackets = false
|
||||
let inQuotes = false
|
||||
let quoteChar = ''
|
||||
|
||||
for (let i = 0; i < path.length; i++) {
|
||||
const char = path[i]
|
||||
|
||||
if (!inBrackets && char === '.') {
|
||||
if (current) {
|
||||
parts.push(current)
|
||||
current = ''
|
||||
}
|
||||
} else if (char === '[') {
|
||||
if (current) {
|
||||
parts.push(current)
|
||||
current = ''
|
||||
}
|
||||
inBrackets = true
|
||||
} else if (char === ']' && inBrackets) {
|
||||
// Always push the current value when closing brackets, even if it's an empty string
|
||||
parts.push(current)
|
||||
current = ''
|
||||
inBrackets = false
|
||||
inQuotes = false
|
||||
} else if ((char === '"' || char === "'") && inBrackets) {
|
||||
if (!inQuotes) {
|
||||
inQuotes = true
|
||||
quoteChar = char
|
||||
} else if (char === quoteChar) {
|
||||
inQuotes = false
|
||||
quoteChar = ''
|
||||
} else {
|
||||
current += char
|
||||
}
|
||||
} else {
|
||||
current += char
|
||||
}
|
||||
}
|
||||
|
||||
if (current) {
|
||||
parts.push(current)
|
||||
}
|
||||
|
||||
return parts
|
||||
}
|
||||
|
||||
function setValue (obj, parts, value) {
|
||||
let current = obj
|
||||
|
||||
for (let i = 0; i < parts.length - 1; i++) {
|
||||
const key = parts[i]
|
||||
// Type safety: Check if current is an object before using 'in' operator
|
||||
if (typeof current !== 'object' || current === null || !(key in current)) {
|
||||
return false // Path doesn't exist, don't create it
|
||||
}
|
||||
if (typeof current[key] !== 'object' || current[key] === null) {
|
||||
return false // Path doesn't exist properly
|
||||
}
|
||||
current = current[key]
|
||||
}
|
||||
|
||||
const lastKey = parts[parts.length - 1]
|
||||
if (lastKey === '*') {
|
||||
if (Array.isArray(current)) {
|
||||
for (let i = 0; i < current.length; i++) {
|
||||
current[i] = value
|
||||
}
|
||||
} else if (typeof current === 'object' && current !== null) {
|
||||
for (const key in current) {
|
||||
if (Object.prototype.hasOwnProperty.call(current, key)) {
|
||||
current[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Type safety: Check if current is an object before using 'in' operator
|
||||
if (typeof current === 'object' && current !== null && lastKey in current && Object.prototype.hasOwnProperty.call(current, lastKey)) {
|
||||
current[lastKey] = value
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function removeKey (obj, parts) {
|
||||
let current = obj
|
||||
|
||||
for (let i = 0; i < parts.length - 1; i++) {
|
||||
const key = parts[i]
|
||||
// Type safety: Check if current is an object before using 'in' operator
|
||||
if (typeof current !== 'object' || current === null || !(key in current)) {
|
||||
return false // Path doesn't exist, don't create it
|
||||
}
|
||||
if (typeof current[key] !== 'object' || current[key] === null) {
|
||||
return false // Path doesn't exist properly
|
||||
}
|
||||
current = current[key]
|
||||
}
|
||||
|
||||
const lastKey = parts[parts.length - 1]
|
||||
if (lastKey === '*') {
|
||||
if (Array.isArray(current)) {
|
||||
// For arrays, we can't really "remove" all items as that would change indices
|
||||
// Instead, we set them to undefined which will be omitted by JSON.stringify
|
||||
for (let i = 0; i < current.length; i++) {
|
||||
current[i] = undefined
|
||||
}
|
||||
} else if (typeof current === 'object' && current !== null) {
|
||||
for (const key in current) {
|
||||
if (Object.prototype.hasOwnProperty.call(current, key)) {
|
||||
delete current[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Type safety: Check if current is an object before using 'in' operator
|
||||
if (typeof current === 'object' && current !== null && lastKey in current && Object.prototype.hasOwnProperty.call(current, lastKey)) {
|
||||
delete current[lastKey]
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Sentinel object to distinguish between undefined value and non-existent path
|
||||
const PATH_NOT_FOUND = Symbol('PATH_NOT_FOUND')
|
||||
|
||||
function getValueIfExists (obj, parts) {
|
||||
let current = obj
|
||||
|
||||
for (const part of parts) {
|
||||
if (current === null || current === undefined) {
|
||||
return PATH_NOT_FOUND
|
||||
}
|
||||
// Type safety: Check if current is an object before property access
|
||||
if (typeof current !== 'object' || current === null) {
|
||||
return PATH_NOT_FOUND
|
||||
}
|
||||
// Check if the property exists before accessing it
|
||||
if (!(part in current)) {
|
||||
return PATH_NOT_FOUND
|
||||
}
|
||||
current = current[part]
|
||||
}
|
||||
|
||||
return current
|
||||
}
|
||||
|
||||
function getValue (obj, parts) {
|
||||
let current = obj
|
||||
|
||||
for (const part of parts) {
|
||||
if (current === null || current === undefined) {
|
||||
return undefined
|
||||
}
|
||||
// Type safety: Check if current is an object before property access
|
||||
if (typeof current !== 'object' || current === null) {
|
||||
return undefined
|
||||
}
|
||||
current = current[part]
|
||||
}
|
||||
|
||||
return current
|
||||
}
|
||||
|
||||
function redactPaths (obj, paths, censor, remove = false) {
|
||||
for (const path of paths) {
|
||||
const parts = parsePath(path)
|
||||
|
||||
if (parts.includes('*')) {
|
||||
redactWildcardPath(obj, parts, censor, path, remove)
|
||||
} else {
|
||||
if (remove) {
|
||||
removeKey(obj, parts)
|
||||
} else {
|
||||
// Get value only if path exists - single traversal
|
||||
const value = getValueIfExists(obj, parts)
|
||||
if (value === PATH_NOT_FOUND) {
|
||||
continue
|
||||
}
|
||||
|
||||
const actualCensor = typeof censor === 'function'
|
||||
? censor(value, parts)
|
||||
: censor
|
||||
setValue(obj, parts, actualCensor)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function redactWildcardPath (obj, parts, censor, originalPath, remove = false) {
|
||||
const wildcardIndex = parts.indexOf('*')
|
||||
|
||||
if (wildcardIndex === parts.length - 1) {
|
||||
const parentParts = parts.slice(0, -1)
|
||||
let current = obj
|
||||
|
||||
for (const part of parentParts) {
|
||||
if (current === null || current === undefined) return
|
||||
// Type safety: Check if current is an object before property access
|
||||
if (typeof current !== 'object' || current === null) return
|
||||
current = current[part]
|
||||
}
|
||||
|
||||
if (Array.isArray(current)) {
|
||||
if (remove) {
|
||||
// For arrays, set all items to undefined which will be omitted by JSON.stringify
|
||||
for (let i = 0; i < current.length; i++) {
|
||||
current[i] = undefined
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < current.length; i++) {
|
||||
const indexPath = [...parentParts, i.toString()]
|
||||
const actualCensor = typeof censor === 'function'
|
||||
? censor(current[i], indexPath)
|
||||
: censor
|
||||
current[i] = actualCensor
|
||||
}
|
||||
}
|
||||
} else if (typeof current === 'object' && current !== null) {
|
||||
if (remove) {
|
||||
// Collect keys to delete to avoid issues with deleting during iteration
|
||||
const keysToDelete = []
|
||||
for (const key in current) {
|
||||
if (Object.prototype.hasOwnProperty.call(current, key)) {
|
||||
keysToDelete.push(key)
|
||||
}
|
||||
}
|
||||
for (const key of keysToDelete) {
|
||||
delete current[key]
|
||||
}
|
||||
} else {
|
||||
for (const key in current) {
|
||||
const keyPath = [...parentParts, key]
|
||||
const actualCensor = typeof censor === 'function'
|
||||
? censor(current[key], keyPath)
|
||||
: censor
|
||||
current[key] = actualCensor
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
redactIntermediateWildcard(obj, parts, censor, wildcardIndex, originalPath, remove)
|
||||
}
|
||||
}
|
||||
|
||||
function redactIntermediateWildcard (obj, parts, censor, wildcardIndex, originalPath, remove = false) {
|
||||
const beforeWildcard = parts.slice(0, wildcardIndex)
|
||||
const afterWildcard = parts.slice(wildcardIndex + 1)
|
||||
const pathArray = [] // Cached array to avoid allocations
|
||||
|
||||
function traverse (current, pathLength) {
|
||||
if (pathLength === beforeWildcard.length) {
|
||||
if (Array.isArray(current)) {
|
||||
for (let i = 0; i < current.length; i++) {
|
||||
pathArray[pathLength] = i.toString()
|
||||
traverse(current[i], pathLength + 1)
|
||||
}
|
||||
} else if (typeof current === 'object' && current !== null) {
|
||||
for (const key in current) {
|
||||
pathArray[pathLength] = key
|
||||
traverse(current[key], pathLength + 1)
|
||||
}
|
||||
}
|
||||
} else if (pathLength < beforeWildcard.length) {
|
||||
const nextKey = beforeWildcard[pathLength]
|
||||
// Type safety: Check if current is an object before using 'in' operator
|
||||
if (current && typeof current === 'object' && current !== null && nextKey in current) {
|
||||
pathArray[pathLength] = nextKey
|
||||
traverse(current[nextKey], pathLength + 1)
|
||||
}
|
||||
} else {
|
||||
// Check if afterWildcard contains more wildcards
|
||||
if (afterWildcard.includes('*')) {
|
||||
// Recursively handle remaining wildcards
|
||||
// Wrap censor to prepend current path context
|
||||
const wrappedCensor = typeof censor === 'function'
|
||||
? (value, path) => {
|
||||
const fullPath = [...pathArray.slice(0, pathLength), ...path]
|
||||
return censor(value, fullPath)
|
||||
}
|
||||
: censor
|
||||
redactWildcardPath(current, afterWildcard, wrappedCensor, originalPath, remove)
|
||||
} else {
|
||||
// No more wildcards, apply the redaction directly
|
||||
if (remove) {
|
||||
removeKey(current, afterWildcard)
|
||||
} else {
|
||||
const actualCensor = typeof censor === 'function'
|
||||
? censor(getValue(current, afterWildcard), [...pathArray.slice(0, pathLength), ...afterWildcard])
|
||||
: censor
|
||||
setValue(current, afterWildcard, actualCensor)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (beforeWildcard.length === 0) {
|
||||
traverse(obj, 0)
|
||||
} else {
|
||||
let current = obj
|
||||
for (let i = 0; i < beforeWildcard.length; i++) {
|
||||
const part = beforeWildcard[i]
|
||||
if (current === null || current === undefined) return
|
||||
// Type safety: Check if current is an object before property access
|
||||
if (typeof current !== 'object' || current === null) return
|
||||
current = current[part]
|
||||
pathArray[i] = part
|
||||
}
|
||||
if (current !== null && current !== undefined) {
|
||||
traverse(current, beforeWildcard.length)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function buildPathStructure (pathsToClone) {
|
||||
if (pathsToClone.length === 0) {
|
||||
return null // No paths to redact
|
||||
}
|
||||
|
||||
// Parse all paths and organize by depth
|
||||
const pathStructure = new Map()
|
||||
for (const path of pathsToClone) {
|
||||
const parts = parsePath(path)
|
||||
let current = pathStructure
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const part = parts[i]
|
||||
if (!current.has(part)) {
|
||||
current.set(part, new Map())
|
||||
}
|
||||
current = current.get(part)
|
||||
}
|
||||
}
|
||||
return pathStructure
|
||||
}
|
||||
|
||||
function selectiveClone (obj, pathStructure) {
|
||||
if (!pathStructure) {
|
||||
return obj // No paths to redact, return original
|
||||
}
|
||||
|
||||
function cloneSelectively (source, pathMap, depth = 0) {
|
||||
if (!pathMap || pathMap.size === 0) {
|
||||
return source // No more paths to clone, return reference
|
||||
}
|
||||
|
||||
if (source === null || typeof source !== 'object') {
|
||||
return source
|
||||
}
|
||||
|
||||
if (source instanceof Date) {
|
||||
return new Date(source.getTime())
|
||||
}
|
||||
|
||||
if (Array.isArray(source)) {
|
||||
const cloned = []
|
||||
for (let i = 0; i < source.length; i++) {
|
||||
const indexStr = i.toString()
|
||||
if (pathMap.has(indexStr) || pathMap.has('*')) {
|
||||
cloned[i] = cloneSelectively(source[i], pathMap.get(indexStr) || pathMap.get('*'))
|
||||
} else {
|
||||
cloned[i] = source[i] // Share reference for non-redacted items
|
||||
}
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
// Handle objects
|
||||
const cloned = Object.create(Object.getPrototypeOf(source))
|
||||
for (const key in source) {
|
||||
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
||||
if (pathMap.has(key) || pathMap.has('*')) {
|
||||
cloned[key] = cloneSelectively(source[key], pathMap.get(key) || pathMap.get('*'))
|
||||
} else {
|
||||
cloned[key] = source[key] // Share reference for non-redacted properties
|
||||
}
|
||||
}
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
return cloneSelectively(obj, pathStructure)
|
||||
}
|
||||
|
||||
function validatePath (path) {
|
||||
if (typeof path !== 'string') {
|
||||
throw new Error('Paths must be (non-empty) strings')
|
||||
}
|
||||
|
||||
if (path === '') {
|
||||
throw new Error('Invalid redaction path ()')
|
||||
}
|
||||
|
||||
// Check for double dots
|
||||
if (path.includes('..')) {
|
||||
throw new Error(`Invalid redaction path (${path})`)
|
||||
}
|
||||
|
||||
// Check for comma-separated paths (invalid syntax)
|
||||
if (path.includes(',')) {
|
||||
throw new Error(`Invalid redaction path (${path})`)
|
||||
}
|
||||
|
||||
// Check for unmatched brackets
|
||||
let bracketCount = 0
|
||||
let inQuotes = false
|
||||
let quoteChar = ''
|
||||
|
||||
for (let i = 0; i < path.length; i++) {
|
||||
const char = path[i]
|
||||
|
||||
if ((char === '"' || char === "'") && bracketCount > 0) {
|
||||
if (!inQuotes) {
|
||||
inQuotes = true
|
||||
quoteChar = char
|
||||
} else if (char === quoteChar) {
|
||||
inQuotes = false
|
||||
quoteChar = ''
|
||||
}
|
||||
} else if (char === '[' && !inQuotes) {
|
||||
bracketCount++
|
||||
} else if (char === ']' && !inQuotes) {
|
||||
bracketCount--
|
||||
if (bracketCount < 0) {
|
||||
throw new Error(`Invalid redaction path (${path})`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bracketCount !== 0) {
|
||||
throw new Error(`Invalid redaction path (${path})`)
|
||||
}
|
||||
}
|
||||
|
||||
function validatePaths (paths) {
|
||||
if (!Array.isArray(paths)) {
|
||||
throw new TypeError('paths must be an array')
|
||||
}
|
||||
|
||||
for (const path of paths) {
|
||||
validatePath(path)
|
||||
}
|
||||
}
|
||||
|
||||
function slowRedact (options = {}) {
|
||||
const {
|
||||
paths = [],
|
||||
censor = '[REDACTED]',
|
||||
serialize = JSON.stringify,
|
||||
strict = true,
|
||||
remove = false
|
||||
} = options
|
||||
|
||||
// Validate paths upfront to match fast-redact behavior
|
||||
validatePaths(paths)
|
||||
|
||||
// Build path structure once during setup, not on every call
|
||||
const pathStructure = buildPathStructure(paths)
|
||||
|
||||
return function redact (obj) {
|
||||
if (strict && (obj === null || typeof obj !== 'object')) {
|
||||
if (obj === null || obj === undefined) {
|
||||
return serialize ? serialize(obj) : obj
|
||||
}
|
||||
if (typeof obj !== 'object') {
|
||||
return serialize ? serialize(obj) : obj
|
||||
}
|
||||
}
|
||||
|
||||
// Only clone paths that need redaction
|
||||
const cloned = selectiveClone(obj, pathStructure)
|
||||
const original = obj // Keep reference to original for restore
|
||||
|
||||
let actualCensor = censor
|
||||
if (typeof censor === 'function') {
|
||||
actualCensor = censor
|
||||
}
|
||||
|
||||
redactPaths(cloned, paths, actualCensor, remove)
|
||||
|
||||
if (serialize === false) {
|
||||
cloned.restore = function () {
|
||||
return deepClone(original) // Full clone only when restore is called
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
if (typeof serialize === 'function') {
|
||||
return serialize(cloned)
|
||||
}
|
||||
|
||||
return JSON.stringify(cloned)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = slowRedact
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
import { expectType, expectAssignable } from "tsd";
|
||||
import slowRedact from ".";
|
||||
import type { redactFn, redactFnNoSerialize } from ".";
|
||||
|
||||
// should return redactFn
|
||||
expectType<redactFn>(slowRedact());
|
||||
expectType<redactFn>(slowRedact({ paths: [] }));
|
||||
expectType<redactFn>(slowRedact({ paths: ["some.path"] }));
|
||||
expectType<redactFn>(slowRedact({ paths: [], censor: "[REDACTED]" }));
|
||||
expectType<redactFn>(slowRedact({ paths: [], strict: true }));
|
||||
expectType<redactFn>(slowRedact({ paths: [], serialize: JSON.stringify }));
|
||||
expectType<redactFn>(slowRedact({ paths: [], serialize: true }));
|
||||
expectType<redactFnNoSerialize>(slowRedact({ paths: [], serialize: false }));
|
||||
expectType<redactFn>(slowRedact({ paths: [], remove: true }));
|
||||
|
||||
// should return string
|
||||
expectType<string>(slowRedact()(""));
|
||||
|
||||
// should return string or T
|
||||
expectAssignable<string | { someField: string }>(
|
||||
slowRedact()({ someField: "someValue" })
|
||||
);
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@pinojs/redact",
|
||||
"version": "0.4.0",
|
||||
"description": "Redact JS objects",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"scripts": {
|
||||
"test": "node --test && npm run test:types",
|
||||
"test:integration": "node --test test/integration.test.js",
|
||||
"test:types": "tsd",
|
||||
"test:all": "node --test test/*.test.js",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix",
|
||||
"bench": "node benchmarks/basic.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/pinojs/redact.git"
|
||||
},
|
||||
"keywords": [
|
||||
"redact"
|
||||
],
|
||||
"author": "Matteo Collina <hello@matteocollina.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pinojs/redact/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pinojs/redact#readme",
|
||||
"devDependencies": {
|
||||
"eslint": "^9.36.0",
|
||||
"fast-redact": "^3.5.0",
|
||||
"mitata": "^1.0.34",
|
||||
"neostandard": "^0.12.2",
|
||||
"tsd": "^0.33.0",
|
||||
"typescript": "^5.9.2"
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
const packageJsonPath = path.resolve(import.meta.dirname, '../package.json')
|
||||
let { version } = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
|
||||
|
||||
let passedVersion = process.argv[2]
|
||||
|
||||
if (passedVersion) {
|
||||
passedVersion = passedVersion.trim().replace(/^v/, '')
|
||||
if (version !== passedVersion) {
|
||||
console.log(`Syncing version from ${version} to ${passedVersion}`)
|
||||
version = passedVersion
|
||||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
|
||||
packageJson.version = version
|
||||
fs.writeFileSync(path.resolve('./package.json'), JSON.stringify(packageJson, null, 2) + '\n', { encoding: 'utf-8' })
|
||||
}
|
||||
} else {
|
||||
throw new Error('Version argument is required')
|
||||
}
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
'use strict'
|
||||
|
||||
// Node.js test comparing @pinojs/redact vs fast-redact for multiple wildcard patterns
|
||||
// This test validates that @pinojs/redact correctly handles 3+ consecutive wildcards
|
||||
// matching the behavior of fast-redact
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { strict: assert } = require('node:assert')
|
||||
const fastRedact = require('fast-redact')
|
||||
const slowRedact = require('../index.js')
|
||||
|
||||
// Helper function to test redaction and track which values were censored
|
||||
function testRedactDirect (library, pattern, testData = {}) {
|
||||
const matches = []
|
||||
const redactor = library === '@pinojs/redact' ? slowRedact : fastRedact
|
||||
|
||||
try {
|
||||
const redact = redactor({
|
||||
paths: [pattern],
|
||||
censor: (value, path) => {
|
||||
if (
|
||||
value !== undefined &&
|
||||
value !== null &&
|
||||
typeof value === 'string' &&
|
||||
value.includes('secret')
|
||||
) {
|
||||
matches.push({
|
||||
value,
|
||||
path: path ? path.join('.') : 'unknown'
|
||||
})
|
||||
}
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
redact(JSON.parse(JSON.stringify(testData)))
|
||||
|
||||
return {
|
||||
library,
|
||||
pattern,
|
||||
matches,
|
||||
success: true,
|
||||
testData
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
library,
|
||||
pattern,
|
||||
matches: [],
|
||||
success: false,
|
||||
error: error.message,
|
||||
testData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function testSlowRedactDirect (pattern, testData) {
|
||||
return testRedactDirect('@pinojs/redact', pattern, testData)
|
||||
}
|
||||
|
||||
function testFastRedactDirect (pattern, testData) {
|
||||
return testRedactDirect('fast-redact', pattern, testData)
|
||||
}
|
||||
|
||||
test('@pinojs/redact: *.password (2 levels)', () => {
|
||||
const result = testSlowRedactDirect('*.password', {
|
||||
simple: { password: 'secret-2-levels' }
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'secret-2-levels')
|
||||
})
|
||||
|
||||
test('@pinojs/redact: *.*.password (3 levels)', () => {
|
||||
const result = testSlowRedactDirect('*.*.password', {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } }
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'secret-3-levels')
|
||||
})
|
||||
|
||||
test('@pinojs/redact: *.*.*.password (4 levels)', () => {
|
||||
const result = testSlowRedactDirect('*.*.*.password', {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } }
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'secret-4-levels')
|
||||
})
|
||||
|
||||
test('@pinojs/redact: *.*.*.*.password (5 levels)', () => {
|
||||
const result = testSlowRedactDirect('*.*.*.*.password', {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } },
|
||||
config: {
|
||||
user: { auth: { settings: { password: 'secret-5-levels' } } }
|
||||
}
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'secret-5-levels')
|
||||
})
|
||||
|
||||
test('@pinojs/redact: *.*.*.*.*.password (6 levels)', () => {
|
||||
const result = testSlowRedactDirect('*.*.*.*.*.password', {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } },
|
||||
config: {
|
||||
user: { auth: { settings: { password: 'secret-5-levels' } } }
|
||||
},
|
||||
data: {
|
||||
reqConfig: {
|
||||
data: {
|
||||
credentials: {
|
||||
settings: {
|
||||
password: 'real-secret-6-levels'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'real-secret-6-levels')
|
||||
})
|
||||
|
||||
test('fast-redact: *.password (2 levels)', () => {
|
||||
const result = testFastRedactDirect('*.password', {
|
||||
simple: { password: 'secret-2-levels' }
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'secret-2-levels')
|
||||
})
|
||||
|
||||
test('fast-redact: *.*.password (3 levels)', () => {
|
||||
const result = testFastRedactDirect('*.*.password', {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } }
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'secret-3-levels')
|
||||
})
|
||||
|
||||
test('fast-redact: *.*.*.password (4 levels)', () => {
|
||||
const result = testFastRedactDirect('*.*.*.password', {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } }
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'secret-4-levels')
|
||||
})
|
||||
|
||||
test('fast-redact: *.*.*.*.password (5 levels)', () => {
|
||||
const result = testFastRedactDirect('*.*.*.*.password', {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } },
|
||||
config: {
|
||||
user: { auth: { settings: { password: 'secret-5-levels' } } }
|
||||
}
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'secret-5-levels')
|
||||
})
|
||||
|
||||
test('fast-redact: *.*.*.*.*.password (6 levels)', () => {
|
||||
const result = testFastRedactDirect('*.*.*.*.*.password', {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } },
|
||||
config: {
|
||||
user: { auth: { settings: { password: 'secret-5-levels' } } }
|
||||
},
|
||||
data: {
|
||||
reqConfig: {
|
||||
data: {
|
||||
credentials: {
|
||||
settings: {
|
||||
password: 'real-secret-6-levels'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
assert.strictEqual(result.success, true)
|
||||
assert.strictEqual(result.matches.length, 1)
|
||||
assert.strictEqual(result.matches[0].value, 'real-secret-6-levels')
|
||||
})
|
||||
+824
@@ -0,0 +1,824 @@
|
||||
const { test } = require('node:test')
|
||||
const { strict: assert } = require('node:assert')
|
||||
const slowRedact = require('../index.js')
|
||||
|
||||
test('basic path redaction', () => {
|
||||
const obj = {
|
||||
headers: {
|
||||
cookie: 'secret-cookie',
|
||||
authorization: 'Bearer token'
|
||||
},
|
||||
body: { message: 'hello' }
|
||||
}
|
||||
|
||||
const redact = slowRedact({ paths: ['headers.cookie'] })
|
||||
const result = redact(obj)
|
||||
|
||||
// Original object should remain unchanged
|
||||
assert.strictEqual(obj.headers.cookie, 'secret-cookie')
|
||||
|
||||
// Result should have redacted path
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.headers.cookie, '[REDACTED]')
|
||||
assert.strictEqual(parsed.headers.authorization, 'Bearer token')
|
||||
assert.strictEqual(parsed.body.message, 'hello')
|
||||
})
|
||||
|
||||
test('multiple paths redaction', () => {
|
||||
const obj = {
|
||||
user: { name: 'john', password: 'secret' },
|
||||
session: { token: 'abc123' }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['user.password', 'session.token']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
// Original unchanged
|
||||
assert.strictEqual(obj.user.password, 'secret')
|
||||
assert.strictEqual(obj.session.token, 'abc123')
|
||||
|
||||
// Result redacted
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.user.password, '[REDACTED]')
|
||||
assert.strictEqual(parsed.session.token, '[REDACTED]')
|
||||
assert.strictEqual(parsed.user.name, 'john')
|
||||
})
|
||||
|
||||
test('custom censor value', () => {
|
||||
const obj = { secret: 'hidden' }
|
||||
const redact = slowRedact({
|
||||
paths: ['secret'],
|
||||
censor: '***'
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.secret, '***')
|
||||
})
|
||||
|
||||
test('serialize: false returns object with restore method', () => {
|
||||
const obj = { secret: 'hidden' }
|
||||
const redact = slowRedact({
|
||||
paths: ['secret'],
|
||||
serialize: false
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
// Should be object, not string
|
||||
assert.strictEqual(typeof result, 'object')
|
||||
assert.strictEqual(result.secret, '[REDACTED]')
|
||||
|
||||
// Should have restore method
|
||||
assert.strictEqual(typeof result.restore, 'function')
|
||||
|
||||
const restored = result.restore()
|
||||
assert.strictEqual(restored.secret, 'hidden')
|
||||
})
|
||||
|
||||
test('bracket notation paths', () => {
|
||||
const obj = {
|
||||
'weird-key': { 'another-weird': 'secret' },
|
||||
normal: 'public'
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['["weird-key"]["another-weird"]']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed['weird-key']['another-weird'], '[REDACTED]')
|
||||
assert.strictEqual(parsed.normal, 'public')
|
||||
})
|
||||
|
||||
test('array paths', () => {
|
||||
const obj = {
|
||||
users: [
|
||||
{ name: 'john', password: 'secret1' },
|
||||
{ name: 'jane', password: 'secret2' }
|
||||
]
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['users[0].password', 'users[1].password']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.users[0].password, '[REDACTED]')
|
||||
assert.strictEqual(parsed.users[1].password, '[REDACTED]')
|
||||
assert.strictEqual(parsed.users[0].name, 'john')
|
||||
assert.strictEqual(parsed.users[1].name, 'jane')
|
||||
})
|
||||
|
||||
test('wildcard at end of path', () => {
|
||||
const obj = {
|
||||
secrets: {
|
||||
key1: 'secret1',
|
||||
key2: 'secret2'
|
||||
},
|
||||
public: 'data'
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['secrets.*']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.secrets.key1, '[REDACTED]')
|
||||
assert.strictEqual(parsed.secrets.key2, '[REDACTED]')
|
||||
assert.strictEqual(parsed.public, 'data')
|
||||
})
|
||||
|
||||
test('wildcard with arrays', () => {
|
||||
const obj = {
|
||||
items: ['secret1', 'secret2', 'secret3']
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['items.*']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.items[0], '[REDACTED]')
|
||||
assert.strictEqual(parsed.items[1], '[REDACTED]')
|
||||
assert.strictEqual(parsed.items[2], '[REDACTED]')
|
||||
})
|
||||
|
||||
test('intermediate wildcard', () => {
|
||||
const obj = {
|
||||
users: {
|
||||
user1: { password: 'secret1' },
|
||||
user2: { password: 'secret2' }
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['users.*.password']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.users.user1.password, '[REDACTED]')
|
||||
assert.strictEqual(parsed.users.user2.password, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('censor function', () => {
|
||||
const obj = { secret: 'hidden' }
|
||||
const redact = slowRedact({
|
||||
paths: ['secret'],
|
||||
censor: (value, path) => `REDACTED:${path.join('.')}`
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.secret, 'REDACTED:secret')
|
||||
})
|
||||
|
||||
test('custom serialize function', () => {
|
||||
const obj = { secret: 'hidden', public: 'data' }
|
||||
const redact = slowRedact({
|
||||
paths: ['secret'],
|
||||
serialize: (obj) => `custom:${JSON.stringify(obj)}`
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
assert(result.startsWith('custom:'))
|
||||
const parsed = JSON.parse(result.slice(7))
|
||||
assert.strictEqual(parsed.secret, '[REDACTED]')
|
||||
assert.strictEqual(parsed.public, 'data')
|
||||
})
|
||||
|
||||
test('nested paths', () => {
|
||||
const obj = {
|
||||
level1: {
|
||||
level2: {
|
||||
level3: {
|
||||
secret: 'hidden'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['level1.level2.level3.secret']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.level1.level2.level3.secret, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('non-existent paths are ignored', () => {
|
||||
const obj = { existing: 'value' }
|
||||
const redact = slowRedact({
|
||||
paths: ['nonexistent.path']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.existing, 'value')
|
||||
assert.strictEqual(parsed.nonexistent, undefined)
|
||||
})
|
||||
|
||||
test('null and undefined handling', () => {
|
||||
const obj = {
|
||||
nullValue: null,
|
||||
undefinedValue: undefined,
|
||||
nested: {
|
||||
nullValue: null
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['nullValue', 'nested.nullValue']
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.nullValue, '[REDACTED]')
|
||||
assert.strictEqual(parsed.nested.nullValue, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('original object remains unchanged', () => {
|
||||
const original = {
|
||||
secret: 'hidden',
|
||||
nested: { secret: 'hidden2' }
|
||||
}
|
||||
const copy = JSON.parse(JSON.stringify(original))
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['secret', 'nested.secret']
|
||||
})
|
||||
redact(original)
|
||||
|
||||
// Original should be completely unchanged
|
||||
assert.deepStrictEqual(original, copy)
|
||||
})
|
||||
|
||||
test('strict mode with primitives', () => {
|
||||
const redact = slowRedact({
|
||||
paths: ['test'],
|
||||
strict: true
|
||||
})
|
||||
|
||||
const stringResult = redact('primitive')
|
||||
assert.strictEqual(stringResult, '"primitive"')
|
||||
|
||||
const numberResult = redact(42)
|
||||
assert.strictEqual(numberResult, '42')
|
||||
})
|
||||
|
||||
// Path validation tests to match fast-redact behavior
|
||||
test('path validation - non-string paths should throw', () => {
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: [123] })
|
||||
}, {
|
||||
message: 'Paths must be (non-empty) strings'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: [null] })
|
||||
}, {
|
||||
message: 'Paths must be (non-empty) strings'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: [undefined] })
|
||||
}, {
|
||||
message: 'Paths must be (non-empty) strings'
|
||||
})
|
||||
})
|
||||
|
||||
test('path validation - empty string should throw', () => {
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: [''] })
|
||||
}, {
|
||||
message: 'Invalid redaction path ()'
|
||||
})
|
||||
})
|
||||
|
||||
test('path validation - double dots should throw', () => {
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['invalid..path'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (invalid..path)'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['a..b..c'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (a..b..c)'
|
||||
})
|
||||
})
|
||||
|
||||
test('path validation - unmatched brackets should throw', () => {
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['invalid[unclosed'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (invalid[unclosed)'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['invalid]unopened'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (invalid]unopened)'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['nested[a[b]'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (nested[a[b])'
|
||||
})
|
||||
})
|
||||
|
||||
test('path validation - comma-separated paths should throw', () => {
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['req,headers.cookie'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (req,headers.cookie)'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['user,profile,name'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (user,profile,name)'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['a,b'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (a,b)'
|
||||
})
|
||||
})
|
||||
|
||||
test('path validation - mixed valid and invalid should throw', () => {
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['valid.path', 123, 'another.valid'] })
|
||||
}, {
|
||||
message: 'Paths must be (non-empty) strings'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['valid.path', 'invalid..path'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (invalid..path)'
|
||||
})
|
||||
|
||||
assert.throws(() => {
|
||||
slowRedact({ paths: ['valid.path', 'req,headers.cookie'] })
|
||||
}, {
|
||||
message: 'Invalid redaction path (req,headers.cookie)'
|
||||
})
|
||||
})
|
||||
|
||||
test('path validation - valid paths should work', () => {
|
||||
// These should not throw
|
||||
assert.doesNotThrow(() => {
|
||||
slowRedact({ paths: [] })
|
||||
})
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
slowRedact({ paths: ['valid.path'] })
|
||||
})
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
slowRedact({ paths: ['user.password', 'data[0].secret'] })
|
||||
})
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
slowRedact({ paths: ['["quoted-key"].value'] })
|
||||
})
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
slowRedact({ paths: ["['single-quoted'].value"] })
|
||||
})
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
slowRedact({ paths: ['array[0]', 'object.property', 'wildcard.*'] })
|
||||
})
|
||||
})
|
||||
|
||||
// fast-redact compatibility tests
|
||||
test('censor function receives path as array (fast-redact compatibility)', () => {
|
||||
const obj = {
|
||||
headers: {
|
||||
authorization: 'Bearer token',
|
||||
'x-api-key': 'secret-key'
|
||||
}
|
||||
}
|
||||
|
||||
const pathsReceived = []
|
||||
const redact = slowRedact({
|
||||
paths: ['headers.authorization', 'headers["x-api-key"]'],
|
||||
censor: (value, path) => {
|
||||
pathsReceived.push(path)
|
||||
assert(Array.isArray(path), 'Path should be an array')
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
redact(obj)
|
||||
|
||||
// Verify paths are arrays
|
||||
assert.strictEqual(pathsReceived.length, 2)
|
||||
assert.deepStrictEqual(pathsReceived[0], ['headers', 'authorization'])
|
||||
assert.deepStrictEqual(pathsReceived[1], ['headers', 'x-api-key'])
|
||||
})
|
||||
|
||||
test('censor function with nested paths receives correct array', () => {
|
||||
const obj = {
|
||||
user: {
|
||||
profile: {
|
||||
credentials: {
|
||||
password: 'secret123'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let receivedPath
|
||||
const redact = slowRedact({
|
||||
paths: ['user.profile.credentials.password'],
|
||||
censor: (value, path) => {
|
||||
receivedPath = path
|
||||
assert.strictEqual(value, 'secret123')
|
||||
assert(Array.isArray(path))
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
redact(obj)
|
||||
|
||||
assert.deepStrictEqual(receivedPath, ['user', 'profile', 'credentials', 'password'])
|
||||
})
|
||||
|
||||
test('censor function with wildcards receives correct array paths', () => {
|
||||
const obj = {
|
||||
users: {
|
||||
user1: { password: 'secret1' },
|
||||
user2: { password: 'secret2' }
|
||||
}
|
||||
}
|
||||
|
||||
const pathsReceived = []
|
||||
const redact = slowRedact({
|
||||
paths: ['users.*.password'],
|
||||
censor: (value, path) => {
|
||||
pathsReceived.push([...path]) // copy the array
|
||||
assert(Array.isArray(path))
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
redact(obj)
|
||||
|
||||
assert.strictEqual(pathsReceived.length, 2)
|
||||
assert.deepStrictEqual(pathsReceived[0], ['users', 'user1', 'password'])
|
||||
assert.deepStrictEqual(pathsReceived[1], ['users', 'user2', 'password'])
|
||||
})
|
||||
|
||||
test('censor function with array wildcard receives correct array paths', () => {
|
||||
const obj = {
|
||||
items: [
|
||||
{ secret: 'value1' },
|
||||
{ secret: 'value2' }
|
||||
]
|
||||
}
|
||||
|
||||
const pathsReceived = []
|
||||
const redact = slowRedact({
|
||||
paths: ['items.*.secret'],
|
||||
censor: (value, path) => {
|
||||
pathsReceived.push([...path])
|
||||
assert(Array.isArray(path))
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
redact(obj)
|
||||
|
||||
assert.strictEqual(pathsReceived.length, 2)
|
||||
assert.deepStrictEqual(pathsReceived[0], ['items', '0', 'secret'])
|
||||
assert.deepStrictEqual(pathsReceived[1], ['items', '1', 'secret'])
|
||||
})
|
||||
|
||||
test('censor function with end wildcard receives correct array paths', () => {
|
||||
const obj = {
|
||||
secrets: {
|
||||
key1: 'secret1',
|
||||
key2: 'secret2'
|
||||
}
|
||||
}
|
||||
|
||||
const pathsReceived = []
|
||||
const redact = slowRedact({
|
||||
paths: ['secrets.*'],
|
||||
censor: (value, path) => {
|
||||
pathsReceived.push([...path])
|
||||
assert(Array.isArray(path))
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
redact(obj)
|
||||
|
||||
assert.strictEqual(pathsReceived.length, 2)
|
||||
// Sort paths for consistent testing since object iteration order isn't guaranteed
|
||||
pathsReceived.sort((a, b) => a[1].localeCompare(b[1]))
|
||||
assert.deepStrictEqual(pathsReceived[0], ['secrets', 'key1'])
|
||||
assert.deepStrictEqual(pathsReceived[1], ['secrets', 'key2'])
|
||||
})
|
||||
|
||||
test('type safety: accessing properties on primitive values should not throw', () => {
|
||||
// Test case from GitHub issue #5
|
||||
const redactor = slowRedact({ paths: ['headers.authorization'] })
|
||||
const data = {
|
||||
headers: 123 // primitive value
|
||||
}
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
const result = redactor(data)
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.headers, 123) // Should remain unchanged
|
||||
})
|
||||
|
||||
// Test wildcards with primitives
|
||||
const redactor2 = slowRedact({ paths: ['data.*.nested'] })
|
||||
const data2 = {
|
||||
data: {
|
||||
item1: 123, // primitive, trying to access .nested on it
|
||||
item2: { nested: 'secret' }
|
||||
}
|
||||
}
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
const result2 = redactor2(data2)
|
||||
const parsed2 = JSON.parse(result2)
|
||||
assert.strictEqual(parsed2.data.item1, 123) // Primitive unchanged
|
||||
assert.strictEqual(parsed2.data.item2.nested, '[REDACTED]') // Object property redacted
|
||||
})
|
||||
|
||||
// Test deep nested access on primitives
|
||||
const redactor3 = slowRedact({ paths: ['user.name.first.charAt'] })
|
||||
const data3 = {
|
||||
user: {
|
||||
name: 'John' // string primitive
|
||||
}
|
||||
}
|
||||
|
||||
assert.doesNotThrow(() => {
|
||||
const result3 = redactor3(data3)
|
||||
const parsed3 = JSON.parse(result3)
|
||||
assert.strictEqual(parsed3.user.name, 'John') // Should remain unchanged
|
||||
})
|
||||
})
|
||||
|
||||
// Remove option tests
|
||||
test('remove option: basic key removal', () => {
|
||||
const obj = { username: 'john', password: 'secret123' }
|
||||
const redact = slowRedact({ paths: ['password'], remove: true })
|
||||
const result = redact(obj)
|
||||
|
||||
// Original object should remain unchanged
|
||||
assert.strictEqual(obj.password, 'secret123')
|
||||
|
||||
// Result should have password completely removed
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.username, 'john')
|
||||
assert.strictEqual('password' in parsed, false)
|
||||
assert.strictEqual(parsed.password, undefined)
|
||||
})
|
||||
|
||||
test('remove option: multiple paths removal', () => {
|
||||
const obj = {
|
||||
user: { name: 'john', password: 'secret' },
|
||||
session: { token: 'abc123', id: 'session1' }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['user.password', 'session.token'],
|
||||
remove: true
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
// Original unchanged
|
||||
assert.strictEqual(obj.user.password, 'secret')
|
||||
assert.strictEqual(obj.session.token, 'abc123')
|
||||
|
||||
// Result has keys completely removed
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.user.name, 'john')
|
||||
assert.strictEqual(parsed.session.id, 'session1')
|
||||
assert.strictEqual('password' in parsed.user, false)
|
||||
assert.strictEqual('token' in parsed.session, false)
|
||||
})
|
||||
|
||||
test('remove option: wildcard removal', () => {
|
||||
const obj = {
|
||||
secrets: {
|
||||
key1: 'secret1',
|
||||
key2: 'secret2'
|
||||
},
|
||||
public: 'data'
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['secrets.*'],
|
||||
remove: true
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.public, 'data')
|
||||
assert.deepStrictEqual(parsed.secrets, {}) // All keys removed
|
||||
})
|
||||
|
||||
test('remove option: array wildcard removal', () => {
|
||||
const obj = {
|
||||
items: ['secret1', 'secret2', 'secret3'],
|
||||
meta: 'data'
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['items.*'],
|
||||
remove: true
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.meta, 'data')
|
||||
// Array items set to undefined are omitted by JSON.stringify
|
||||
assert.deepStrictEqual(parsed.items, [null, null, null])
|
||||
})
|
||||
|
||||
test('remove option: intermediate wildcard removal', () => {
|
||||
const obj = {
|
||||
users: {
|
||||
user1: { password: 'secret1', name: 'john' },
|
||||
user2: { password: 'secret2', name: 'jane' }
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['users.*.password'],
|
||||
remove: true
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.users.user1.name, 'john')
|
||||
assert.strictEqual(parsed.users.user2.name, 'jane')
|
||||
assert.strictEqual('password' in parsed.users.user1, false)
|
||||
assert.strictEqual('password' in parsed.users.user2, false)
|
||||
})
|
||||
|
||||
test('remove option: serialize false returns object with removed keys', () => {
|
||||
const obj = { secret: 'hidden', public: 'data' }
|
||||
const redact = slowRedact({
|
||||
paths: ['secret'],
|
||||
remove: true,
|
||||
serialize: false
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
// Should be object, not string
|
||||
assert.strictEqual(typeof result, 'object')
|
||||
assert.strictEqual(result.public, 'data')
|
||||
assert.strictEqual('secret' in result, false)
|
||||
|
||||
// Should have restore method
|
||||
assert.strictEqual(typeof result.restore, 'function')
|
||||
|
||||
const restored = result.restore()
|
||||
assert.strictEqual(restored.secret, 'hidden')
|
||||
})
|
||||
|
||||
test('remove option: non-existent paths are ignored', () => {
|
||||
const obj = { existing: 'value' }
|
||||
const redact = slowRedact({
|
||||
paths: ['nonexistent.path'],
|
||||
remove: true
|
||||
})
|
||||
const result = redact(obj)
|
||||
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed.existing, 'value')
|
||||
assert.strictEqual(parsed.nonexistent, undefined)
|
||||
})
|
||||
|
||||
// Test for Issue #13: Empty string bracket notation paths not being redacted correctly
|
||||
test('empty string bracket notation path', () => {
|
||||
const obj = { '': { c: 'sensitive-data' } }
|
||||
const redact = slowRedact({ paths: ["[''].c"] })
|
||||
const result = redact(obj)
|
||||
|
||||
// Original object should remain unchanged
|
||||
assert.strictEqual(obj[''].c, 'sensitive-data')
|
||||
|
||||
// Result should have redacted path
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed[''].c, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('empty string bracket notation with double quotes', () => {
|
||||
const obj = { '': { c: 'sensitive-data' } }
|
||||
const redact = slowRedact({ paths: ['[""].c'] })
|
||||
const result = redact(obj)
|
||||
|
||||
// Original object should remain unchanged
|
||||
assert.strictEqual(obj[''].c, 'sensitive-data')
|
||||
|
||||
// Result should have redacted path
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed[''].c, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('empty string key with nested bracket notation', () => {
|
||||
const obj = { '': { '': { secret: 'value' } } }
|
||||
const redact = slowRedact({ paths: ["[''][''].secret"] })
|
||||
const result = redact(obj)
|
||||
|
||||
// Original object should remain unchanged
|
||||
assert.strictEqual(obj[''][''].secret, 'value')
|
||||
|
||||
// Result should have redacted path
|
||||
const parsed = JSON.parse(result)
|
||||
assert.strictEqual(parsed[''][''].secret, '[REDACTED]')
|
||||
})
|
||||
|
||||
// Test for Pino issue #2313: censor should only be called when path exists
|
||||
test('censor function not called for non-existent paths', () => {
|
||||
let censorCallCount = 0
|
||||
const censorCalls = []
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['a.b.c', 'req.authorization', 'url'],
|
||||
serialize: false,
|
||||
censor (value, path) {
|
||||
censorCallCount++
|
||||
censorCalls.push({ value, path: path.slice() })
|
||||
return '***'
|
||||
}
|
||||
})
|
||||
|
||||
// Test case 1: { req: { id: 'test' } }
|
||||
// req.authorization doesn't exist, censor should not be called for it
|
||||
censorCallCount = 0
|
||||
censorCalls.length = 0
|
||||
redact({ req: { id: 'test' } })
|
||||
|
||||
// Should not have been called for any path since none exist
|
||||
assert.strictEqual(censorCallCount, 0, 'censor should not be called when paths do not exist')
|
||||
|
||||
// Test case 2: { a: { d: 'test' } }
|
||||
// a.b.c doesn't exist (a.d exists, but not a.b.c)
|
||||
censorCallCount = 0
|
||||
redact({ a: { d: 'test' } })
|
||||
assert.strictEqual(censorCallCount, 0)
|
||||
|
||||
// Test case 3: paths that do exist should still call censor
|
||||
censorCallCount = 0
|
||||
censorCalls.length = 0
|
||||
const result = redact({ req: { authorization: 'bearer token' } })
|
||||
assert.strictEqual(censorCallCount, 1, 'censor should be called when path exists')
|
||||
assert.deepStrictEqual(censorCalls[0].path, ['req', 'authorization'])
|
||||
assert.strictEqual(censorCalls[0].value, 'bearer token')
|
||||
assert.strictEqual(result.req.authorization, '***')
|
||||
})
|
||||
|
||||
test('censor function not called for non-existent nested paths', () => {
|
||||
let censorCallCount = 0
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['headers.authorization'],
|
||||
serialize: false,
|
||||
censor (value, path) {
|
||||
censorCallCount++
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
// headers exists but authorization doesn't
|
||||
censorCallCount = 0
|
||||
const result1 = redact({ headers: { 'content-type': 'application/json' } })
|
||||
assert.strictEqual(censorCallCount, 0)
|
||||
assert.deepStrictEqual(result1.headers, { 'content-type': 'application/json' })
|
||||
|
||||
// headers doesn't exist at all
|
||||
censorCallCount = 0
|
||||
const result2 = redact({ body: 'data' })
|
||||
assert.strictEqual(censorCallCount, 0)
|
||||
assert.strictEqual(result2.body, 'data')
|
||||
assert.strictEqual(typeof result2.restore, 'function')
|
||||
|
||||
// headers.authorization exists - should call censor
|
||||
censorCallCount = 0
|
||||
const result3 = redact({ headers: { authorization: 'Bearer token' } })
|
||||
assert.strictEqual(censorCallCount, 1)
|
||||
assert.strictEqual(result3.headers.authorization, '[REDACTED]')
|
||||
})
|
||||
+390
@@ -0,0 +1,390 @@
|
||||
const { test } = require('node:test')
|
||||
const { strict: assert } = require('node:assert')
|
||||
const slowRedact = require('../index.js')
|
||||
const fastRedact = require('fast-redact')
|
||||
|
||||
test('integration: basic path redaction matches fast-redact', () => {
|
||||
const obj = {
|
||||
headers: {
|
||||
cookie: 'secret-cookie',
|
||||
authorization: 'Bearer token'
|
||||
},
|
||||
body: { message: 'hello' }
|
||||
}
|
||||
|
||||
const slowResult = slowRedact({ paths: ['headers.cookie'] })(obj)
|
||||
const fastResult = fastRedact({ paths: ['headers.cookie'] })(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: multiple paths match fast-redact', () => {
|
||||
const obj = {
|
||||
user: { name: 'john', password: 'secret' },
|
||||
session: { token: 'abc123' }
|
||||
}
|
||||
|
||||
const paths = ['user.password', 'session.token']
|
||||
const slowResult = slowRedact({ paths })(obj)
|
||||
const fastResult = fastRedact({ paths })(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: custom censor value matches fast-redact', () => {
|
||||
const obj = { secret: 'hidden' }
|
||||
const options = { paths: ['secret'], censor: '***' }
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: bracket notation matches fast-redact', () => {
|
||||
const obj = {
|
||||
'weird-key': { 'another-weird': 'secret' },
|
||||
normal: 'public'
|
||||
}
|
||||
|
||||
const options = { paths: ['["weird-key"]["another-weird"]'] }
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: array paths match fast-redact', () => {
|
||||
const obj = {
|
||||
users: [
|
||||
{ name: 'john', password: 'secret1' },
|
||||
{ name: 'jane', password: 'secret2' }
|
||||
]
|
||||
}
|
||||
|
||||
const options = { paths: ['users[0].password', 'users[1].password'] }
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: wildcard at end matches fast-redact', () => {
|
||||
const obj = {
|
||||
secrets: {
|
||||
key1: 'secret1',
|
||||
key2: 'secret2'
|
||||
},
|
||||
public: 'data'
|
||||
}
|
||||
|
||||
const options = { paths: ['secrets.*'] }
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: wildcard with arrays matches fast-redact', () => {
|
||||
const obj = {
|
||||
items: ['secret1', 'secret2', 'secret3']
|
||||
}
|
||||
|
||||
const options = { paths: ['items.*'] }
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: intermediate wildcard matches fast-redact', () => {
|
||||
const obj = {
|
||||
users: {
|
||||
user1: { password: 'secret1' },
|
||||
user2: { password: 'secret2' }
|
||||
}
|
||||
}
|
||||
|
||||
const options = { paths: ['users.*.password'] }
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: custom serialize function matches fast-redact', () => {
|
||||
const obj = { secret: 'hidden', public: 'data' }
|
||||
const options = {
|
||||
paths: ['secret'],
|
||||
serialize: (obj) => `custom:${JSON.stringify(obj)}`
|
||||
}
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: nested paths match fast-redact', () => {
|
||||
const obj = {
|
||||
level1: {
|
||||
level2: {
|
||||
level3: {
|
||||
secret: 'hidden'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const options = { paths: ['level1.level2.level3.secret'] }
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: non-existent paths match fast-redact', () => {
|
||||
const obj = { existing: 'value' }
|
||||
const options = { paths: ['nonexistent.path'] }
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: null and undefined handling - legitimate difference', () => {
|
||||
const obj = {
|
||||
nullValue: null,
|
||||
undefinedValue: undefined,
|
||||
nested: {
|
||||
nullValue: null
|
||||
}
|
||||
}
|
||||
|
||||
const options = { paths: ['nullValue', 'nested.nullValue'] }
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
// This is a legitimate behavioral difference:
|
||||
// @pinojs/redact redacts null values, fast-redact doesn't
|
||||
const slowParsed = JSON.parse(slowResult)
|
||||
const fastParsed = JSON.parse(fastResult)
|
||||
|
||||
// @pinojs/redact redacts nulls
|
||||
assert.strictEqual(slowParsed.nullValue, '[REDACTED]')
|
||||
assert.strictEqual(slowParsed.nested.nullValue, '[REDACTED]')
|
||||
|
||||
// fast-redact preserves nulls
|
||||
assert.strictEqual(fastParsed.nullValue, null)
|
||||
assert.strictEqual(fastParsed.nested.nullValue, null)
|
||||
})
|
||||
|
||||
test('integration: strict mode with primitives - different error handling', () => {
|
||||
const options = { paths: ['test'], strict: true }
|
||||
|
||||
const slowRedactFn = slowRedact(options)
|
||||
const fastRedactFn = fastRedact(options)
|
||||
|
||||
// @pinojs/redact handles primitives gracefully
|
||||
const stringSlowResult = slowRedactFn('primitive')
|
||||
assert.strictEqual(stringSlowResult, '"primitive"')
|
||||
|
||||
const numberSlowResult = slowRedactFn(42)
|
||||
assert.strictEqual(numberSlowResult, '42')
|
||||
|
||||
// fast-redact throws an error for primitives in strict mode
|
||||
assert.throws(() => {
|
||||
fastRedactFn('primitive')
|
||||
}, /primitives cannot be redacted/)
|
||||
|
||||
assert.throws(() => {
|
||||
fastRedactFn(42)
|
||||
}, /primitives cannot be redacted/)
|
||||
})
|
||||
|
||||
test('integration: serialize false behavior difference', () => {
|
||||
const slowObj = { secret: 'hidden' }
|
||||
const fastObj = { secret: 'hidden' }
|
||||
const options = { paths: ['secret'], serialize: false }
|
||||
|
||||
const slowResult = slowRedact(options)(slowObj)
|
||||
const fastResult = fastRedact(options)(fastObj)
|
||||
|
||||
// Both should redact the secret
|
||||
assert.strictEqual(slowResult.secret, '[REDACTED]')
|
||||
assert.strictEqual(fastResult.secret, '[REDACTED]')
|
||||
|
||||
// @pinojs/redact always has restore method
|
||||
assert.strictEqual(typeof slowResult.restore, 'function')
|
||||
|
||||
// @pinojs/redact should restore to original value
|
||||
assert.strictEqual(slowResult.restore().secret, 'hidden')
|
||||
|
||||
// Key difference: original object state
|
||||
// fast-redact mutates the original, @pinojs/redact doesn't
|
||||
assert.strictEqual(slowObj.secret, 'hidden') // @pinojs/redact preserves original
|
||||
assert.strictEqual(fastObj.secret, '[REDACTED]') // fast-redact mutates original
|
||||
})
|
||||
|
||||
test('integration: censor function behavior', () => {
|
||||
const obj = { secret: 'hidden' }
|
||||
const options = {
|
||||
paths: ['secret'],
|
||||
censor: (value, path) => `REDACTED:${path}`
|
||||
}
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: complex object with mixed patterns', () => {
|
||||
const obj = {
|
||||
users: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'john',
|
||||
credentials: { password: 'secret1', apiKey: 'key1' }
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'jane',
|
||||
credentials: { password: 'secret2', apiKey: 'key2' }
|
||||
}
|
||||
],
|
||||
config: {
|
||||
database: { password: 'db-secret' },
|
||||
api: { keys: ['key1', 'key2', 'key3'] }
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
paths: [
|
||||
'users.*.credentials.password',
|
||||
'users.*.credentials.apiKey',
|
||||
'config.database.password',
|
||||
'config.api.keys.*'
|
||||
]
|
||||
}
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
// Remove option integration tests - comparing with fast-redact
|
||||
test('integration: remove option basic comparison with fast-redact', () => {
|
||||
const obj = { username: 'john', password: 'secret123' }
|
||||
const options = { paths: ['password'], remove: true }
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
|
||||
// Verify the key is actually removed
|
||||
const parsed = JSON.parse(slowResult)
|
||||
assert.strictEqual(parsed.username, 'john')
|
||||
assert.strictEqual('password' in parsed, false)
|
||||
})
|
||||
|
||||
test('integration: remove option multiple paths comparison with fast-redact', () => {
|
||||
const obj = {
|
||||
user: { name: 'john', password: 'secret' },
|
||||
session: { token: 'abc123', id: 'session1' }
|
||||
}
|
||||
|
||||
const options = {
|
||||
paths: ['user.password', 'session.token'],
|
||||
remove: true
|
||||
}
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: remove option wildcard comparison with fast-redact', () => {
|
||||
const obj = {
|
||||
secrets: {
|
||||
key1: 'secret1',
|
||||
key2: 'secret2'
|
||||
},
|
||||
public: 'data'
|
||||
}
|
||||
|
||||
const options = {
|
||||
paths: ['secrets.*'],
|
||||
remove: true
|
||||
}
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: remove option intermediate wildcard comparison with fast-redact', () => {
|
||||
const obj = {
|
||||
users: {
|
||||
user1: { password: 'secret1', name: 'john' },
|
||||
user2: { password: 'secret2', name: 'jane' }
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
paths: ['users.*.password'],
|
||||
remove: true
|
||||
}
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
})
|
||||
|
||||
test('integration: remove option with custom censor comparison with fast-redact', () => {
|
||||
const obj = { secret: 'hidden', public: 'data' }
|
||||
const options = {
|
||||
paths: ['secret'],
|
||||
censor: '***',
|
||||
remove: true
|
||||
}
|
||||
|
||||
const slowResult = slowRedact(options)(obj)
|
||||
const fastResult = fastRedact(options)(obj)
|
||||
|
||||
assert.strictEqual(slowResult, fastResult)
|
||||
|
||||
// With remove: true, censor value should be ignored
|
||||
const parsed = JSON.parse(slowResult)
|
||||
assert.strictEqual('secret' in parsed, false)
|
||||
assert.strictEqual(parsed.public, 'data')
|
||||
})
|
||||
|
||||
test('integration: remove option serialize false behavior - @pinojs/redact only', () => {
|
||||
// fast-redact doesn't support remove option with serialize: false
|
||||
// so we test @pinojs/redact's behavior only
|
||||
const obj = { secret: 'hidden', public: 'data' }
|
||||
const options = { paths: ['secret'], remove: true, serialize: false }
|
||||
|
||||
const result = slowRedact(options)(obj)
|
||||
|
||||
// Should have the key removed
|
||||
assert.strictEqual('secret' in result, false)
|
||||
assert.strictEqual(result.public, 'data')
|
||||
|
||||
// Should have restore method
|
||||
assert.strictEqual(typeof result.restore, 'function')
|
||||
|
||||
// Original object should be preserved
|
||||
assert.strictEqual(obj.secret, 'hidden')
|
||||
|
||||
// Restore should bring back the removed key
|
||||
const restored = result.restore()
|
||||
assert.strictEqual(restored.secret, 'hidden')
|
||||
})
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { strict: assert } = require('node:assert')
|
||||
const slowRedact = require('../index.js')
|
||||
|
||||
// Tests for Issue #2319: @pinojs/redact fails to redact patterns with 3+ consecutive wildcards
|
||||
test('three consecutive wildcards: *.*.*.password (4 levels deep)', () => {
|
||||
const obj = {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.password']
|
||||
})
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// Only the 4-level deep password should be redacted
|
||||
assert.strictEqual(parsed.simple.password, 'secret-2-levels', '2-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.user.auth.password, 'secret-3-levels', '3-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.nested.deep.auth.password, '[REDACTED]', '4-level password SHOULD be redacted')
|
||||
})
|
||||
|
||||
test('four consecutive wildcards: *.*.*.*.password (5 levels deep)', () => {
|
||||
const obj = {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } },
|
||||
config: { user: { auth: { settings: { password: 'secret-5-levels' } } } }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.*.password']
|
||||
})
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// Only the 5-level deep password should be redacted
|
||||
assert.strictEqual(parsed.simple.password, 'secret-2-levels', '2-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.user.auth.password, 'secret-3-levels', '3-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.nested.deep.auth.password, 'secret-4-levels', '4-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.config.user.auth.settings.password, '[REDACTED]', '5-level password SHOULD be redacted')
|
||||
})
|
||||
|
||||
test('five consecutive wildcards: *.*.*.*.*.password (6 levels deep)', () => {
|
||||
const obj = {
|
||||
simple: { password: 'secret-2-levels' },
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
nested: { deep: { auth: { password: 'secret-4-levels' } } },
|
||||
config: { user: { auth: { settings: { password: 'secret-5-levels' } } } },
|
||||
data: {
|
||||
reqConfig: {
|
||||
data: {
|
||||
credentials: {
|
||||
settings: {
|
||||
password: 'secret-6-levels'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.*.*.password']
|
||||
})
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// Only the 6-level deep password should be redacted
|
||||
assert.strictEqual(parsed.simple.password, 'secret-2-levels', '2-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.user.auth.password, 'secret-3-levels', '3-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.nested.deep.auth.password, 'secret-4-levels', '4-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.config.user.auth.settings.password, 'secret-5-levels', '5-level password should NOT be redacted')
|
||||
assert.strictEqual(parsed.data.reqConfig.data.credentials.settings.password, '[REDACTED]', '6-level password SHOULD be redacted')
|
||||
})
|
||||
|
||||
test('three wildcards with censor function receives correct values', () => {
|
||||
const obj = {
|
||||
nested: { deep: { auth: { password: 'secret-value' } } }
|
||||
}
|
||||
|
||||
const censorCalls = []
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.password'],
|
||||
censor: (value, path) => {
|
||||
censorCalls.push({ value, path: [...path] })
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// Should have been called exactly once with the correct value
|
||||
assert.strictEqual(censorCalls.length, 1, 'censor should be called once')
|
||||
assert.strictEqual(censorCalls[0].value, 'secret-value', 'censor should receive the actual value')
|
||||
assert.deepStrictEqual(censorCalls[0].path, ['nested', 'deep', 'auth', 'password'], 'censor should receive correct path')
|
||||
assert.strictEqual(parsed.nested.deep.auth.password, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('three wildcards with multiple matches', () => {
|
||||
const obj = {
|
||||
api1: { v1: { auth: { token: 'token1' } } },
|
||||
api2: { v2: { auth: { token: 'token2' } } },
|
||||
api3: { v1: { auth: { token: 'token3' } } }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.token']
|
||||
})
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// All three tokens should be redacted
|
||||
assert.strictEqual(parsed.api1.v1.auth.token, '[REDACTED]')
|
||||
assert.strictEqual(parsed.api2.v2.auth.token, '[REDACTED]')
|
||||
assert.strictEqual(parsed.api3.v1.auth.token, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('three wildcards with remove option', () => {
|
||||
const obj = {
|
||||
nested: { deep: { auth: { password: 'secret', username: 'admin' } } }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.password'],
|
||||
remove: true
|
||||
})
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// Password should be removed entirely
|
||||
assert.strictEqual('password' in parsed.nested.deep.auth, false, 'password key should be removed')
|
||||
assert.strictEqual(parsed.nested.deep.auth.username, 'admin', 'username should remain')
|
||||
})
|
||||
|
||||
test('mixed: two and three wildcards in same redactor', () => {
|
||||
const obj = {
|
||||
user: { auth: { password: 'secret-3-levels' } },
|
||||
config: { deep: { auth: { password: 'secret-4-levels' } } }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.password', '*.*.*.password']
|
||||
})
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// Both should be redacted
|
||||
assert.strictEqual(parsed.user.auth.password, '[REDACTED]', '3-level should be redacted by *.*.password')
|
||||
assert.strictEqual(parsed.config.deep.auth.password, '[REDACTED]', '4-level should be redacted by *.*.*.password')
|
||||
})
|
||||
|
||||
test('three wildcards should not call censor for non-existent paths', () => {
|
||||
const obj = {
|
||||
shallow: { data: 'value' },
|
||||
nested: { deep: { auth: { password: 'secret' } } }
|
||||
}
|
||||
|
||||
let censorCallCount = 0
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.password'],
|
||||
censor: (value, path) => {
|
||||
censorCallCount++
|
||||
return '[REDACTED]'
|
||||
}
|
||||
})
|
||||
|
||||
redact(obj)
|
||||
|
||||
// Should only be called once for the path that exists
|
||||
assert.strictEqual(censorCallCount, 1, 'censor should only be called for existing paths')
|
||||
})
|
||||
|
||||
test('three wildcards with arrays', () => {
|
||||
const obj = {
|
||||
users: [
|
||||
{ auth: { password: 'secret1' } },
|
||||
{ auth: { password: 'secret2' } }
|
||||
]
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.password']
|
||||
})
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// Both passwords should be redacted (users[0].auth.password is 4 levels)
|
||||
assert.strictEqual(parsed.users[0].auth.password, '[REDACTED]')
|
||||
assert.strictEqual(parsed.users[1].auth.password, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('four wildcards with authorization header (real-world case)', () => {
|
||||
const obj = {
|
||||
requests: {
|
||||
api1: {
|
||||
config: {
|
||||
headers: {
|
||||
authorization: 'Bearer secret-token'
|
||||
}
|
||||
}
|
||||
},
|
||||
api2: {
|
||||
config: {
|
||||
headers: {
|
||||
authorization: 'Bearer another-token'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['*.*.*.*.authorization']
|
||||
})
|
||||
const result = redact(obj)
|
||||
const parsed = JSON.parse(result)
|
||||
|
||||
// Both authorization headers should be redacted
|
||||
assert.strictEqual(parsed.requests.api1.config.headers.authorization, '[REDACTED]')
|
||||
assert.strictEqual(parsed.requests.api2.config.headers.authorization, '[REDACTED]')
|
||||
})
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
const { test } = require('node:test')
|
||||
const { strict: assert } = require('node:assert')
|
||||
const slowRedact = require('../index.js')
|
||||
|
||||
/* eslint-disable no-proto */
|
||||
|
||||
test('prototype pollution: __proto__ path should not pollute Object prototype', () => {
|
||||
const obj = {
|
||||
user: { name: 'john' },
|
||||
__proto__: { isAdmin: true }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['__proto__.isAdmin'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should not pollute Object.prototype
|
||||
assert.strictEqual(Object.prototype.isAdmin, undefined)
|
||||
assert.strictEqual({}.isAdmin, undefined)
|
||||
|
||||
// Should redact the __proto__ property if it exists as a regular property
|
||||
assert.strictEqual(result.__proto__.isAdmin, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('prototype pollution: constructor.prototype path should not pollute', () => {
|
||||
const obj = {
|
||||
user: { name: 'john' },
|
||||
constructor: {
|
||||
prototype: { isAdmin: true }
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['constructor.prototype.isAdmin'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should not pollute Object.prototype
|
||||
assert.strictEqual(Object.prototype.isAdmin, undefined)
|
||||
assert.strictEqual({}.isAdmin, undefined)
|
||||
|
||||
// Should redact the constructor.prototype property if it exists as a regular property
|
||||
assert.strictEqual(result.constructor.prototype.isAdmin, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('prototype pollution: nested __proto__ should not pollute', () => {
|
||||
const obj = {
|
||||
user: {
|
||||
settings: {
|
||||
__proto__: { isAdmin: true }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['user.settings.__proto__.isAdmin'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should not pollute Object.prototype
|
||||
assert.strictEqual(Object.prototype.isAdmin, undefined)
|
||||
assert.strictEqual({}.isAdmin, undefined)
|
||||
|
||||
// Should redact the nested __proto__ property
|
||||
assert.strictEqual(result.user.settings.__proto__.isAdmin, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('prototype pollution: bracket notation __proto__ should not pollute', () => {
|
||||
const obj = {
|
||||
user: { name: 'john' },
|
||||
__proto__: { isAdmin: true }
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['["__proto__"]["isAdmin"]'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should not pollute Object.prototype
|
||||
assert.strictEqual(Object.prototype.isAdmin, undefined)
|
||||
assert.strictEqual({}.isAdmin, undefined)
|
||||
|
||||
// Should redact the __proto__ property when accessed via bracket notation
|
||||
assert.strictEqual(result.__proto__.isAdmin, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('prototype pollution: wildcard with __proto__ should not pollute', () => {
|
||||
const obj = {
|
||||
users: {
|
||||
__proto__: { isAdmin: true },
|
||||
user1: { name: 'john' },
|
||||
user2: { name: 'jane' }
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['users.*'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should not pollute Object.prototype
|
||||
assert.strictEqual(Object.prototype.isAdmin, undefined)
|
||||
assert.strictEqual({}.isAdmin, undefined)
|
||||
|
||||
// Should redact only own properties
|
||||
assert.strictEqual(result.users.user1, '[REDACTED]')
|
||||
assert.strictEqual(result.users.user2, '[REDACTED]')
|
||||
|
||||
// __proto__ should only be redacted if it's an own property, not inherited
|
||||
if (Object.prototype.hasOwnProperty.call(obj.users, '__proto__')) {
|
||||
assert.strictEqual(result.users.__proto__, '[REDACTED]')
|
||||
}
|
||||
})
|
||||
|
||||
test('prototype pollution: malicious JSON payload should not pollute', () => {
|
||||
// Simulate a malicious payload that might come from JSON.parse
|
||||
const maliciousObj = JSON.parse('{"user": {"name": "john"}, "__proto__": {"isAdmin": true}}')
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['__proto__.isAdmin'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(maliciousObj)
|
||||
|
||||
// Should not pollute Object.prototype
|
||||
assert.strictEqual(Object.prototype.isAdmin, undefined)
|
||||
assert.strictEqual({}.isAdmin, undefined)
|
||||
|
||||
// The malicious payload should have been redacted
|
||||
assert.strictEqual(result.__proto__.isAdmin, '[REDACTED]')
|
||||
})
|
||||
|
||||
test('prototype pollution: verify prototype chain is preserved', () => {
|
||||
function CustomClass () {
|
||||
this.data = 'test'
|
||||
}
|
||||
CustomClass.prototype.method = function () { return 'original' }
|
||||
|
||||
const obj = new CustomClass()
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['data'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should redact the data property
|
||||
assert.strictEqual(result.data, '[REDACTED]')
|
||||
|
||||
// Should preserve the original prototype chain
|
||||
assert.strictEqual(result.method(), 'original')
|
||||
assert.strictEqual(Object.getPrototypeOf(result), CustomClass.prototype)
|
||||
})
|
||||
|
||||
test('prototype pollution: setValue should not create prototype pollution', () => {
|
||||
const obj = { user: { name: 'john' } }
|
||||
|
||||
// Try to pollute via non-existent path that could create __proto__
|
||||
const redact = slowRedact({
|
||||
paths: ['__proto__.isAdmin'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should not pollute Object.prototype
|
||||
assert.strictEqual(Object.prototype.isAdmin, undefined)
|
||||
assert.strictEqual({}.isAdmin, undefined)
|
||||
|
||||
// Should not create the path if it doesn't exist
|
||||
// The __proto__ property may exist due to Object.create, but should not contain our redacted value
|
||||
if (result.__proto__) {
|
||||
assert.strictEqual(result.__proto__.isAdmin, undefined)
|
||||
}
|
||||
})
|
||||
|
||||
test('prototype pollution: deep nested prototype properties should not pollute', () => {
|
||||
const obj = {
|
||||
level1: {
|
||||
level2: {
|
||||
level3: {
|
||||
__proto__: { isAdmin: true },
|
||||
constructor: {
|
||||
prototype: { isEvil: true }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: [
|
||||
'level1.level2.level3.__proto__.isAdmin',
|
||||
'level1.level2.level3.constructor.prototype.isEvil'
|
||||
],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should not pollute Object.prototype
|
||||
assert.strictEqual(Object.prototype.isAdmin, undefined)
|
||||
assert.strictEqual(Object.prototype.isEvil, undefined)
|
||||
assert.strictEqual({}.isAdmin, undefined)
|
||||
assert.strictEqual({}.isEvil, undefined)
|
||||
|
||||
// Should redact the deep nested properties
|
||||
assert.strictEqual(result.level1.level2.level3.__proto__.isAdmin, '[REDACTED]')
|
||||
assert.strictEqual(result.level1.level2.level3.constructor.prototype.isEvil, '[REDACTED]')
|
||||
})
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
const { test } = require('node:test')
|
||||
const { strict: assert } = require('node:assert')
|
||||
const slowRedact = require('../index.js')
|
||||
|
||||
test('selective cloning shares references for non-redacted paths', () => {
|
||||
const sharedObject = { unchanged: 'data' }
|
||||
const obj = {
|
||||
toRedact: 'secret',
|
||||
shared: sharedObject,
|
||||
nested: {
|
||||
toRedact: 'secret2',
|
||||
shared: sharedObject
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['toRedact', 'nested.toRedact'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Redacted values should be different
|
||||
assert.strictEqual(result.toRedact, '[REDACTED]')
|
||||
assert.strictEqual(result.nested.toRedact, '[REDACTED]')
|
||||
|
||||
// Non-redacted references should be shared (same object reference)
|
||||
assert.strictEqual(result.shared, obj.shared)
|
||||
assert.strictEqual(result.nested.shared, obj.nested.shared)
|
||||
|
||||
// The shared object should be the exact same reference
|
||||
assert.strictEqual(result.shared, sharedObject)
|
||||
assert.strictEqual(result.nested.shared, sharedObject)
|
||||
})
|
||||
|
||||
test('selective cloning works with arrays', () => {
|
||||
const sharedItem = { unchanged: 'data' }
|
||||
const obj = {
|
||||
items: [
|
||||
{ secret: 'hidden1', shared: sharedItem },
|
||||
{ secret: 'hidden2', shared: sharedItem },
|
||||
sharedItem
|
||||
]
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['items.*.secret'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Secrets should be redacted
|
||||
assert.strictEqual(result.items[0].secret, '[REDACTED]')
|
||||
assert.strictEqual(result.items[1].secret, '[REDACTED]')
|
||||
|
||||
// Shared references should be preserved where possible
|
||||
// Note: array items with secrets will be cloned, but their shared properties should still reference the original
|
||||
assert.strictEqual(result.items[0].shared, sharedItem)
|
||||
assert.strictEqual(result.items[1].shared, sharedItem)
|
||||
|
||||
// The third item gets cloned due to wildcard, but should have the same content
|
||||
assert.deepStrictEqual(result.items[2], sharedItem)
|
||||
// Note: Due to wildcard '*', all array items are cloned, even if they don't need redaction
|
||||
// This is still a significant optimization for object properties that aren't in wildcard paths
|
||||
})
|
||||
|
||||
test('selective cloning with no paths returns original object', () => {
|
||||
const obj = { data: 'unchanged' }
|
||||
const redact = slowRedact({
|
||||
paths: [],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Should return the exact same object reference
|
||||
assert.strictEqual(result, obj)
|
||||
})
|
||||
|
||||
test('selective cloning performance - large objects with minimal redaction', () => {
|
||||
// Create a large object with mostly shared data
|
||||
const sharedData = { large: 'data'.repeat(1000) }
|
||||
const obj = {
|
||||
secret: 'hidden',
|
||||
shared1: sharedData,
|
||||
shared2: sharedData,
|
||||
nested: {
|
||||
secret: 'hidden2',
|
||||
shared3: sharedData,
|
||||
deep: {
|
||||
shared4: sharedData,
|
||||
moreShared: sharedData
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const redact = slowRedact({
|
||||
paths: ['secret', 'nested.secret'],
|
||||
serialize: false
|
||||
})
|
||||
|
||||
const result = redact(obj)
|
||||
|
||||
// Verify redaction worked
|
||||
assert.strictEqual(result.secret, '[REDACTED]')
|
||||
assert.strictEqual(result.nested.secret, '[REDACTED]')
|
||||
|
||||
// Verify shared references are preserved
|
||||
assert.strictEqual(result.shared1, sharedData)
|
||||
assert.strictEqual(result.shared2, sharedData)
|
||||
assert.strictEqual(result.nested.shared3, sharedData)
|
||||
assert.strictEqual(result.nested.deep.shared4, sharedData)
|
||||
assert.strictEqual(result.nested.deep.moreShared, sharedData)
|
||||
})
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictNullChecks": true,
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"index.test-d.ts"
|
||||
]
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
language: node_js
|
||||
sudo: false
|
||||
node_js:
|
||||
- 6
|
||||
- 8
|
||||
- 10
|
||||
- 11
|
||||
- 12
|
||||
- 13
|
||||
script:
|
||||
- npm run ci
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
Copyright (c) 2020 David Mark Clements
|
||||
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
||||
OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
/* global SharedArrayBuffer, Atomics */
|
||||
|
||||
if (typeof SharedArrayBuffer !== 'undefined' && typeof Atomics !== 'undefined') {
|
||||
const nil = new Int32Array(new SharedArrayBuffer(4))
|
||||
|
||||
function sleep (ms) {
|
||||
// also filters out NaN, non-number types, including empty strings, but allows bigints
|
||||
const valid = ms > 0 && ms < Infinity
|
||||
if (valid === false) {
|
||||
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
|
||||
throw TypeError('sleep: ms must be a number')
|
||||
}
|
||||
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
|
||||
}
|
||||
|
||||
Atomics.wait(nil, 0, 0, Number(ms))
|
||||
}
|
||||
module.exports = sleep
|
||||
} else {
|
||||
|
||||
function sleep (ms) {
|
||||
// also filters out NaN, non-number types, including empty strings, but allows bigints
|
||||
const valid = ms > 0 && ms < Infinity
|
||||
if (valid === false) {
|
||||
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
|
||||
throw TypeError('sleep: ms must be a number')
|
||||
}
|
||||
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
|
||||
}
|
||||
const target = Date.now() + Number(ms)
|
||||
while (target > Date.now()){}
|
||||
}
|
||||
|
||||
module.exports = sleep
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "atomic-sleep",
|
||||
"version": "1.0.0",
|
||||
"description": "Zero CPU overhead, zero dependency, true event-loop blocking sleep",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "tap -R classic- -j1 test",
|
||||
"lint": "standard",
|
||||
"ci": "npm run lint && npm test"
|
||||
},
|
||||
"keywords": [
|
||||
"sleep",
|
||||
"pause",
|
||||
"wait",
|
||||
"performance",
|
||||
"atomics"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
},
|
||||
"author": "David Mark Clements (@davidmarkclem)",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"standard": "^14.3.1",
|
||||
"tap": "^14.10.6",
|
||||
"tape": "^4.13.2"
|
||||
},
|
||||
"dependencies": {},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/davidmarkclements/atomic-sleep.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/davidmarkclements/atomic-sleep/issues"
|
||||
},
|
||||
"homepage": "https://github.com/davidmarkclements/atomic-sleep#readme"
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
<h1 align="center">Welcome to atomic-sleep ⏱️</h1>
|
||||
<p>
|
||||
<img alt="Version" src="https://img.shields.io/badge/version-1.0.0-blue.svg?cacheSeconds=2592000" />
|
||||
<a href="#" target="_blank">
|
||||
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" />
|
||||
</a>
|
||||
<a href="https://twitter.com/davidmarkclem" target="_blank">
|
||||
<img alt="Twitter: davidmarkclem" src="https://img.shields.io/twitter/follow/davidmarkclem.svg?style=social" />
|
||||
</a>
|
||||
</p>
|
||||
|
||||
> Zero CPU overhead, zero dependency, true event-loop blocking sleep
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const sleep = require('atomic-sleep')
|
||||
|
||||
console.time('sleep')
|
||||
setTimeout(() => { console.timeEnd('sleep') }, 100)
|
||||
sleep(1000)
|
||||
```
|
||||
|
||||
The `console.time` will report a time of just over 1000ms despite the `setTimeout`
|
||||
being 100ms. This is because the event loop is paused for 1000ms and the setTimeout
|
||||
fires immediately after the event loop is no longer blocked (as more than 100ms have passed).
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install
|
||||
```
|
||||
|
||||
## Run tests
|
||||
|
||||
```sh
|
||||
npm test
|
||||
```
|
||||
|
||||
## Support
|
||||
|
||||
Node and Browser versions that support both `SharedArrayBuffer` and `Atomics` will have (virtually) zero CPU overhead sleep.
|
||||
|
||||
For Node, Atomic Sleep can provide zero CPU overhead sleep from Node 8 and up.
|
||||
|
||||
For browser support see https://caniuse.com/#feat=sharedarraybuffer and https://caniuse.com/#feat=mdn-javascript_builtins_atomics.
|
||||
|
||||
|
||||
For older Node versions and olders browsers we fall back to blocking the event loop in a way that will cause a CPU spike.
|
||||
|
||||
|
||||
|
||||
## Author
|
||||
|
||||
👤 **David Mark Clements (@davidmarkclem)**
|
||||
|
||||
* Twitter: [@davidmarkclem](https://twitter.com/davidmarkclem)
|
||||
* Github: [@davidmarkclements](https://github.com/davidmarkclements)
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
'use strict'
|
||||
const test = require('tape')
|
||||
const sleep = require('.')
|
||||
|
||||
test('blocks event loop for given amount of milliseconds', ({ is, end }) => {
|
||||
const now = Date.now()
|
||||
setTimeout(() => {
|
||||
const delta = Date.now() - now
|
||||
const fuzzyDelta = Math.floor(delta / 10) * 10 // allow up to 10ms of execution lag
|
||||
is(fuzzyDelta, 1000)
|
||||
end()
|
||||
}, 100)
|
||||
sleep(1000)
|
||||
})
|
||||
|
||||
if (typeof BigInt !== 'undefined') {
|
||||
|
||||
test('allows ms to be supplied as a BigInt number', ({ is, end }) => {
|
||||
const now = Date.now()
|
||||
setTimeout(() => {
|
||||
const delta = Date.now() - now
|
||||
const fuzzyDelta = Math.floor(delta / 10) * 10 // allow up to 10ms of execution lag
|
||||
is(fuzzyDelta, 1000)
|
||||
end()
|
||||
}, 100)
|
||||
sleep(BigInt(1000)) // avoiding n notation as this will error on legacy node/browsers
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
test('throws range error if ms less than 0', ({ throws, end }) => {
|
||||
throws(() => sleep(-1), RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity'))
|
||||
end()
|
||||
})
|
||||
|
||||
test('throws range error if ms is Infinity', ({ throws, end }) => {
|
||||
throws(() => sleep(Infinity), RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity'))
|
||||
end()
|
||||
})
|
||||
|
||||
test('throws range error if ms is not a number or bigint', ({ throws, end }) => {
|
||||
throws(() => sleep('Infinity'), TypeError('sleep: ms must be a number'))
|
||||
throws(() => sleep('foo'), TypeError('sleep: ms must be a number'))
|
||||
throws(() => sleep({a: 1}), TypeError('sleep: ms must be a number'))
|
||||
throws(() => sleep([1,2,3]), TypeError('sleep: ms must be a number'))
|
||||
end()
|
||||
})
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: github-actions
|
||||
directory: '/'
|
||||
schedule:
|
||||
interval: daily
|
||||
open-pull-requests-limit: 10
|
||||
- package-ecosystem: npm
|
||||
directory: '/'
|
||||
schedule:
|
||||
interval: daily
|
||||
open-pull-requests-limit: 10
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
name: CI
|
||||
on:
|
||||
push:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [14, 16, 18, 20]
|
||||
os: [macos-latest, ubuntu-latest, windows-latest]
|
||||
exclude:
|
||||
- node-version: 14
|
||||
os: windows-latest
|
||||
|
||||
steps:
|
||||
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Use Node.js
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install
|
||||
run: |
|
||||
npm install --ignore-scripts
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
npm run test
|
||||
|
||||
automerge:
|
||||
needs: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: fastify/github-action-merge-dependabot@v3.9
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Matteo Collina
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
# on-exit-leak-free
|
||||
|
||||
This module helps dispose of an object gracefully when the Node.js process exits.
|
||||
It executes a function with a given parameter
|
||||
on [`'exit'`](https://nodejs.org/api/process.html#event-exit) without leaking memory,
|
||||
cleaning things up appropriately if the object is garbage collected.
|
||||
|
||||
Requires `WeakRef` and `FinalizationRegistry`, i.e. use Node v14+.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm i on-exit-leak-free
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```js
|
||||
'use strict'
|
||||
|
||||
const { register, unregister } = require('on-exit-leak-free')
|
||||
const assert = require('assert')
|
||||
|
||||
function setup () {
|
||||
// This object can be safely garbage collected,
|
||||
// and the resulting shutdown function will not be called.
|
||||
// There are no leaks.
|
||||
const obj = { foo: 'bar' }
|
||||
register(obj, shutdown)
|
||||
// use registerBeforeExit(obj, shutdown) to execute the function only
|
||||
// on beforeExit
|
||||
// call unregister(obj) to remove
|
||||
}
|
||||
|
||||
let shutdownCalled = false
|
||||
|
||||
// Please make sure that the function passed to register()
|
||||
// does not create a closure around unnecessary objects.
|
||||
function shutdown (obj, eventName) {
|
||||
console.log(eventName) // beforeExit
|
||||
shutdownCalled = true
|
||||
assert.strictEqual(obj.foo, 'bar')
|
||||
}
|
||||
|
||||
setup()
|
||||
|
||||
process.on('exit', function () {
|
||||
assert.strictEqual(shutdownCalled, true)
|
||||
})
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
'use strict'
|
||||
|
||||
const refs = {
|
||||
exit: [],
|
||||
beforeExit: []
|
||||
}
|
||||
const functions = {
|
||||
exit: onExit,
|
||||
beforeExit: onBeforeExit
|
||||
}
|
||||
|
||||
let registry
|
||||
|
||||
function ensureRegistry () {
|
||||
if (registry === undefined) {
|
||||
registry = new FinalizationRegistry(clear)
|
||||
}
|
||||
}
|
||||
|
||||
function install (event) {
|
||||
if (refs[event].length > 0) {
|
||||
return
|
||||
}
|
||||
|
||||
process.on(event, functions[event])
|
||||
}
|
||||
|
||||
function uninstall (event) {
|
||||
if (refs[event].length > 0) {
|
||||
return
|
||||
}
|
||||
process.removeListener(event, functions[event])
|
||||
if (refs.exit.length === 0 && refs.beforeExit.length === 0) {
|
||||
registry = undefined
|
||||
}
|
||||
}
|
||||
|
||||
function onExit () {
|
||||
callRefs('exit')
|
||||
}
|
||||
|
||||
function onBeforeExit () {
|
||||
callRefs('beforeExit')
|
||||
}
|
||||
|
||||
function callRefs (event) {
|
||||
for (const ref of refs[event]) {
|
||||
const obj = ref.deref()
|
||||
const fn = ref.fn
|
||||
|
||||
// This should always happen, however GC is
|
||||
// undeterministic so it might not happen.
|
||||
/* istanbul ignore else */
|
||||
if (obj !== undefined) {
|
||||
fn(obj, event)
|
||||
}
|
||||
}
|
||||
refs[event] = []
|
||||
}
|
||||
|
||||
function clear (ref) {
|
||||
for (const event of ['exit', 'beforeExit']) {
|
||||
const index = refs[event].indexOf(ref)
|
||||
refs[event].splice(index, index + 1)
|
||||
uninstall(event)
|
||||
}
|
||||
}
|
||||
|
||||
function _register (event, obj, fn) {
|
||||
if (obj === undefined) {
|
||||
throw new Error('the object can\'t be undefined')
|
||||
}
|
||||
install(event)
|
||||
const ref = new WeakRef(obj)
|
||||
ref.fn = fn
|
||||
|
||||
ensureRegistry()
|
||||
registry.register(obj, ref)
|
||||
refs[event].push(ref)
|
||||
}
|
||||
|
||||
function register (obj, fn) {
|
||||
_register('exit', obj, fn)
|
||||
}
|
||||
|
||||
function registerBeforeExit (obj, fn) {
|
||||
_register('beforeExit', obj, fn)
|
||||
}
|
||||
|
||||
function unregister (obj) {
|
||||
if (registry === undefined) {
|
||||
return
|
||||
}
|
||||
registry.unregister(obj)
|
||||
for (const event of ['exit', 'beforeExit']) {
|
||||
refs[event] = refs[event].filter((ref) => {
|
||||
const _obj = ref.deref()
|
||||
return _obj && _obj !== obj
|
||||
})
|
||||
uninstall(event)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
register,
|
||||
registerBeforeExit,
|
||||
unregister
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "on-exit-leak-free",
|
||||
"version": "2.1.2",
|
||||
"description": "Execute a function on exit without leaking memory, allowing all objects to be garbage collected",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "standard | snazzy && tap test/*.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/mcollina/on-exit-or-gc.git"
|
||||
},
|
||||
"keywords": [
|
||||
"weak",
|
||||
"reference",
|
||||
"finalization",
|
||||
"registry",
|
||||
"process",
|
||||
"exit",
|
||||
"garbage",
|
||||
"collector"
|
||||
],
|
||||
"author": "Matteo Collina <hello@matteocollina.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mcollina/on-exit-or-gc/issues"
|
||||
},
|
||||
"homepage": "https://github.com/mcollina/on-exit-or-gc#readme",
|
||||
"devDependencies": {
|
||||
"snazzy": "^9.0.0",
|
||||
"standard": "^17.0.0",
|
||||
"tap": "^16.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { fork } = require('child_process')
|
||||
const { join } = require('path')
|
||||
const { once } = require('events')
|
||||
const { register } = require('..')
|
||||
|
||||
const files = [
|
||||
'close.js',
|
||||
'beforeExit',
|
||||
'gc-not-close.js',
|
||||
'unregister.js'
|
||||
]
|
||||
|
||||
for (const file of files) {
|
||||
test(file, async ({ equal }) => {
|
||||
const child = fork(join(__dirname, 'fixtures', file), [], {
|
||||
execArgv: ['--expose-gc']
|
||||
})
|
||||
|
||||
const [code] = await once(child, 'close')
|
||||
|
||||
equal(code, 0)
|
||||
})
|
||||
}
|
||||
|
||||
test('undefined', async ({ throws }) => {
|
||||
throws(() => register(undefined))
|
||||
})
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const t = require('tap')
|
||||
const { register, unregister } = require('..')
|
||||
|
||||
process.on('warning', () => {
|
||||
t.fail('warning emitted')
|
||||
})
|
||||
|
||||
const objs = []
|
||||
for (let i = 0; i < 20; i++) {
|
||||
const obj = { i }
|
||||
objs.push(obj)
|
||||
register(obj, shutdown)
|
||||
}
|
||||
|
||||
for (const obj of objs) {
|
||||
unregister(obj)
|
||||
}
|
||||
|
||||
t.pass('completed')
|
||||
|
||||
function shutdown () {}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
'use strict'
|
||||
|
||||
const { unregister, registerBeforeExit } = require('../..')
|
||||
const assert = require('assert')
|
||||
|
||||
function setup () {
|
||||
const obj = { foo: 'bar' }
|
||||
registerBeforeExit(obj, shutdown)
|
||||
}
|
||||
|
||||
let shutdownCalled = false
|
||||
let timeoutFinished = false
|
||||
function shutdown (obj, event) {
|
||||
shutdownCalled = true
|
||||
if (event === 'beforeExit') {
|
||||
setTimeout(function () {
|
||||
timeoutFinished = true
|
||||
assert.strictEqual(obj.foo, 'bar')
|
||||
unregister(obj)
|
||||
}, 100)
|
||||
process.on('beforeExit', function () {
|
||||
assert.strictEqual(timeoutFinished, true)
|
||||
})
|
||||
} else {
|
||||
throw new Error('different event')
|
||||
}
|
||||
}
|
||||
|
||||
setup()
|
||||
|
||||
process.on('exit', function () {
|
||||
assert.strictEqual(shutdownCalled, true)
|
||||
})
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
'use strict'
|
||||
|
||||
const { register } = require('../..')
|
||||
const assert = require('assert')
|
||||
|
||||
function setup () {
|
||||
const obj = { foo: 'bar' }
|
||||
register(obj, shutdown)
|
||||
}
|
||||
|
||||
let shutdownCalled = false
|
||||
function shutdown (obj) {
|
||||
shutdownCalled = true
|
||||
assert.strictEqual(obj.foo, 'bar')
|
||||
}
|
||||
|
||||
setup()
|
||||
|
||||
process.on('exit', function () {
|
||||
assert.strictEqual(shutdownCalled, true)
|
||||
})
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
'use strict'
|
||||
|
||||
const { register } = require('../..')
|
||||
const assert = require('assert')
|
||||
|
||||
function setup () {
|
||||
let obj = { foo: 'bar' }
|
||||
register(obj, shutdown)
|
||||
setImmediate(function () {
|
||||
obj = undefined
|
||||
gc() // eslint-disable-line
|
||||
})
|
||||
}
|
||||
|
||||
let shutdownCalled = false
|
||||
function shutdown (obj) {
|
||||
shutdownCalled = true
|
||||
}
|
||||
|
||||
setup()
|
||||
|
||||
process.on('exit', function () {
|
||||
assert.strictEqual(shutdownCalled, false)
|
||||
})
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
'use strict'
|
||||
|
||||
const { register, unregister } = require('../..')
|
||||
const assert = require('assert')
|
||||
|
||||
function setup () {
|
||||
const obj = { foo: 'bar' }
|
||||
register(obj, shutdown)
|
||||
setImmediate(function () {
|
||||
unregister(obj)
|
||||
unregister(obj) // twice, this should not throw
|
||||
})
|
||||
}
|
||||
|
||||
let shutdownCalled = false
|
||||
function shutdown (obj) {
|
||||
shutdownCalled = true
|
||||
}
|
||||
|
||||
setup()
|
||||
|
||||
process.on('exit', function () {
|
||||
assert.strictEqual(shutdownCalled, false)
|
||||
})
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
open-pull-requests-limit: 10
|
||||
|
||||
- package-ecosystem: "npm"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
open-pull-requests-limit: 10
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
|
||||
# This allows a subsequently queued workflow run to interrupt previous runs
|
||||
concurrency:
|
||||
group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
dependency-review:
|
||||
name: Dependency Review
|
||||
if: github.event_name == 'pull_request'
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Check out repo
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Dependency review
|
||||
uses: actions/dependency-review-action@v3
|
||||
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ${{ matrix.os }}
|
||||
permissions:
|
||||
contents: read
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [18, 20, 22]
|
||||
os: [macos-latest, ubuntu-latest, windows-latest]
|
||||
|
||||
steps:
|
||||
- name: Check out repo
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Setup Node ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Restore cached dependencies
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: node_modules
|
||||
key: node-modules-${{ hashFiles('package.json') }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm i --ignore-scripts
|
||||
|
||||
- name: Run Tests
|
||||
run: npm run test-ci
|
||||
|
||||
- name: Coveralls Parallel
|
||||
uses: coverallsapp/github-action@v2.1.2
|
||||
with:
|
||||
github-token: ${{ secrets.github_token }}
|
||||
parallel: true
|
||||
flag-name: run-${{ matrix.node-version }}-${{ matrix.os }}
|
||||
|
||||
coverage:
|
||||
needs: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Coveralls Finished
|
||||
uses: coverallsapp/github-action@v2.1.2
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
parallel-finished: true
|
||||
|
||||
automerge:
|
||||
name: Automerge Dependabot PRs
|
||||
if: >
|
||||
github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.user.login == 'dependabot[bot]'
|
||||
needs: test
|
||||
permissions:
|
||||
pull-requests: write
|
||||
contents: write
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: fastify/github-action-merge-dependabot@v3
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env sh
|
||||
. "$(dirname -- "$0")/_/husky.sh"
|
||||
|
||||
npm test
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 pino
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
# pino-abstract-transport
|
||||
[](https://www.npmjs.com/package/pino-abstract-transport)
|
||||
[](https://github.com/pinojs/pino-abstract-transport/actions)
|
||||
[](https://coveralls.io/github/pinojs/pino-abstract-transport?branch=main)
|
||||
[](https://standardjs.com/)
|
||||
|
||||
Write Pino transports easily.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm i pino-abstract-transport
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import build from 'pino-abstract-transport'
|
||||
|
||||
export default async function (opts) {
|
||||
return build(async function (source) {
|
||||
for await (let obj of source) {
|
||||
console.log(obj)
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
or in CommonJS and streams:
|
||||
|
||||
```js
|
||||
'use strict'
|
||||
|
||||
const build = require('pino-abstract-transport')
|
||||
|
||||
module.exports = function (opts) {
|
||||
return build(function (source) {
|
||||
source.on('data', function (obj) {
|
||||
console.log(obj)
|
||||
})
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
## Typescript usage
|
||||
|
||||
Install the type definitions for node. Make sure the major version of the type definitions matches the node version you are using.
|
||||
|
||||
#### Node 16
|
||||
|
||||
```sh
|
||||
npm i -D @types/node@16
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### build(fn, opts) => Stream
|
||||
|
||||
Create a [`split2`](http://npm.im/split2) instance and returns it.
|
||||
This same instance is also passed to the given function, which is called
|
||||
synchronously.
|
||||
|
||||
If `opts.transform` is `true`, `pino-abstract-transform` will
|
||||
wrap the split2 instance and the returned stream using [`duplexify`](https://www.npmjs.com/package/duplexify),
|
||||
so they can be concatenated into multiple transports.
|
||||
|
||||
#### Events emitted
|
||||
|
||||
In addition to all events emitted by a [`Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable)
|
||||
stream, it emits the following events:
|
||||
|
||||
* `unknown` where an unparsable line is found, both the line and optional error is emitted.
|
||||
|
||||
#### Options
|
||||
|
||||
* `parse` an option to change to data format passed to build function. When this option is set to `lines`,
|
||||
the data is passed as a string, otherwise the data is passed as an object. Default: `undefined`.
|
||||
|
||||
* `close(err, cb)` a function that is called to shutdown the transport. It's called both on error and non-error shutdowns.
|
||||
It can also return a promise. In this case discard the the `cb` argument.
|
||||
|
||||
* `parseLine(line)` a function that is used to parse line received from `pino`.
|
||||
|
||||
* `expectPinoConfig` a boolean that indicates if the transport expects Pino to add some of its configuration to the stream. Default: `false`.
|
||||
|
||||
## Example
|
||||
|
||||
### custom parseLine
|
||||
|
||||
You can allow custom `parseLine` from users while providing a simple and safe default parseLine.
|
||||
|
||||
```js
|
||||
'use strict'
|
||||
|
||||
const build = require('pino-abstract-transport')
|
||||
|
||||
function defaultParseLine (line) {
|
||||
const obj = JSON.parse(line)
|
||||
// property foo will be added on each line
|
||||
obj.foo = 'bar'
|
||||
return obj
|
||||
}
|
||||
|
||||
module.exports = function (opts) {
|
||||
const parseLine = typeof opts.parseLine === 'function' ? opts.parseLine : defaultParseLine
|
||||
return build(function (source) {
|
||||
source.on('data', function (obj) {
|
||||
console.log(obj)
|
||||
})
|
||||
}, {
|
||||
parseLine: parseLine
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### Stream concatenation / pipeline
|
||||
|
||||
You can pipeline multiple transports:
|
||||
|
||||
```js
|
||||
const build = require('pino-abstract-transport')
|
||||
const { Transform, pipeline } = require('stream')
|
||||
|
||||
function buildTransform () {
|
||||
return build(function (source) {
|
||||
return new Transform({
|
||||
objectMode: true,
|
||||
autoDestroy: true,
|
||||
transform (line, enc, cb) {
|
||||
line.service = 'bob'
|
||||
cb(null, JSON.stringify(line))
|
||||
}
|
||||
})
|
||||
}, { enablePipelining: true })
|
||||
}
|
||||
|
||||
function buildDestination () {
|
||||
return build(function (source) {
|
||||
source.on('data', function (obj) {
|
||||
console.log(obj)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pipeline(process.stdin, buildTransform(), buildDestination(), function (err) {
|
||||
console.log('pipeline completed!', err)
|
||||
})
|
||||
```
|
||||
|
||||
### Using pino config
|
||||
|
||||
Setting `expectPinoConfig` to `true` will make the transport wait for pino to send its configuration before starting to process logs. It will add `levels`, `messageKey` and `errorKey` to the stream.
|
||||
|
||||
When used with an incompatible version of pino, the stream will immediately error.
|
||||
|
||||
```js
|
||||
import build from 'pino-abstract-transport'
|
||||
|
||||
export default function (opts) {
|
||||
return build(async function (source) {
|
||||
for await (const obj of source) {
|
||||
console.log(`[${source.levels.labels[obj.level]}]: ${obj[source.messageKey]}`)
|
||||
}
|
||||
}, {
|
||||
expectPinoConfig: true
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
// Type definitions for pino-abstract-transport 0.4.0
|
||||
// Project: https://github.com/pinojs/pino-abstract-transport#readme
|
||||
// Definitions by: Diyar Oktay <https://github.com/windupbird144>
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
import { Transform } from "stream";
|
||||
|
||||
type BuildOptions = {
|
||||
/**
|
||||
* `parseLine(line)` a function that is used to parse line received from pino.
|
||||
* @default JSON.parse
|
||||
*/
|
||||
parseLine?: (line: string) => unknown;
|
||||
|
||||
/**
|
||||
* `parse` an option to change to data format passed to build function.
|
||||
* @default undefined
|
||||
*
|
||||
*/
|
||||
parse?: "lines";
|
||||
|
||||
/**
|
||||
* `close(err, cb)` a function that is called to shutdown the transport.
|
||||
* It's called both on error and non-error shutdowns. It can also return
|
||||
* a promise. In this case discard the the cb argument.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* {
|
||||
* close: function (err, cb) {
|
||||
* process.nextTick(cb, err)
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
* */
|
||||
close?: (err: Error, cb: Function) => void | Promise<void>;
|
||||
|
||||
/**
|
||||
* `metadata` If set to false, do not add metadata properties to the returned stream
|
||||
*/
|
||||
metadata?: false;
|
||||
|
||||
/**
|
||||
* `expectPinoConfig` If set to true, the transport will wait for pino to send its
|
||||
* configuration before starting to process logs.
|
||||
*/
|
||||
expectPinoConfig?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pass these options to wrap the split2 stream and
|
||||
* the returned stream into a Duplex
|
||||
*/
|
||||
type EnablePipelining = BuildOptions & {
|
||||
enablePipelining: true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a split2 instance and returns it. This same instance is also passed
|
||||
* to the given function, which is called after pino has sent its configuration.
|
||||
*
|
||||
* @returns {Promise<Transform>} the split2 instance
|
||||
*/
|
||||
declare function build(
|
||||
fn: (transform: Transform & build.OnUnknown) => void | Promise<void>,
|
||||
opts: BuildOptions & { expectPinoConfig: true }
|
||||
): Promise<Transform & build.OnUnknown>;
|
||||
|
||||
/**
|
||||
* Create a split2 instance and returns it. This same instance is also passed
|
||||
* to the given function, which is called synchronously.
|
||||
*
|
||||
* @returns {Transform} the split2 instance
|
||||
*/
|
||||
declare function build(
|
||||
fn: (transform: Transform & build.OnUnknown) => void | Promise<void>,
|
||||
opts?: BuildOptions
|
||||
): Transform & build.OnUnknown;
|
||||
|
||||
/**
|
||||
* Creates a split2 instance and passes it to the given function, which is called
|
||||
* after pino has sent its configuration. Then wraps the split2 instance and
|
||||
* the returned stream into a Duplex, so they can be concatenated into multiple
|
||||
* transports.
|
||||
*
|
||||
* @returns {Promise<Transform>} the wrapped split2 instance
|
||||
*/
|
||||
declare function build(
|
||||
fn: (transform: Transform & build.OnUnknown) => Transform & build.OnUnknown,
|
||||
opts: EnablePipelining & { expectPinoConfig: true }
|
||||
): Promise<Transform>;
|
||||
|
||||
/**
|
||||
* Creates a split2 instance and passes it to the given function, which is called
|
||||
* synchronously. Then wraps the split2 instance and the returned stream into a
|
||||
* Duplex, so they can be concatenated into multiple transports.
|
||||
*
|
||||
* @returns {Transform} the wrapped split2 instance
|
||||
*/
|
||||
declare function build(
|
||||
fn: (transform: Transform & build.OnUnknown) => Transform & build.OnUnknown,
|
||||
opts: EnablePipelining
|
||||
): Transform;
|
||||
|
||||
declare namespace build {
|
||||
export interface OnUnknown {
|
||||
/**
|
||||
* `unknown` is the event emitted where an unparsable line is found
|
||||
*
|
||||
* @param event 'unknown'
|
||||
* @param line the unparsable line
|
||||
* @param error the error that was thrown when parsing the line
|
||||
*/
|
||||
on(
|
||||
event: "unknown",
|
||||
listener: (line: string, error: unknown) => void
|
||||
): void;
|
||||
}
|
||||
}
|
||||
|
||||
export = build;
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
'use strict'
|
||||
|
||||
const metadata = Symbol.for('pino.metadata')
|
||||
const split = require('split2')
|
||||
const { Duplex } = require('stream')
|
||||
const { parentPort, workerData } = require('worker_threads')
|
||||
|
||||
function createDeferred () {
|
||||
let resolve
|
||||
let reject
|
||||
const promise = new Promise((_resolve, _reject) => {
|
||||
resolve = _resolve
|
||||
reject = _reject
|
||||
})
|
||||
promise.resolve = resolve
|
||||
promise.reject = reject
|
||||
return promise
|
||||
}
|
||||
|
||||
module.exports = function build (fn, opts = {}) {
|
||||
const waitForConfig = opts.expectPinoConfig === true && workerData?.workerData?.pinoWillSendConfig === true
|
||||
const parseLines = opts.parse === 'lines'
|
||||
const parseLine = typeof opts.parseLine === 'function' ? opts.parseLine : JSON.parse
|
||||
const close = opts.close || defaultClose
|
||||
const stream = split(function (line) {
|
||||
let value
|
||||
|
||||
try {
|
||||
value = parseLine(line)
|
||||
} catch (error) {
|
||||
this.emit('unknown', line, error)
|
||||
return
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
this.emit('unknown', line, 'Null value ignored')
|
||||
return
|
||||
}
|
||||
|
||||
if (typeof value !== 'object') {
|
||||
value = {
|
||||
data: value,
|
||||
time: Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
if (stream[metadata]) {
|
||||
stream.lastTime = value.time
|
||||
stream.lastLevel = value.level
|
||||
stream.lastObj = value
|
||||
}
|
||||
|
||||
if (parseLines) {
|
||||
return line
|
||||
}
|
||||
|
||||
return value
|
||||
}, { autoDestroy: true })
|
||||
|
||||
stream._destroy = function (err, cb) {
|
||||
const promise = close(err, cb)
|
||||
if (promise && typeof promise.then === 'function') {
|
||||
promise.then(cb, cb)
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.expectPinoConfig === true && workerData?.workerData?.pinoWillSendConfig !== true) {
|
||||
setImmediate(() => {
|
||||
stream.emit('error', new Error('This transport is not compatible with the current version of pino. Please upgrade pino to the latest version.'))
|
||||
})
|
||||
}
|
||||
|
||||
if (opts.metadata !== false) {
|
||||
stream[metadata] = true
|
||||
stream.lastTime = 0
|
||||
stream.lastLevel = 0
|
||||
stream.lastObj = null
|
||||
}
|
||||
|
||||
if (waitForConfig) {
|
||||
let pinoConfig = {}
|
||||
const configReceived = createDeferred()
|
||||
parentPort.on('message', function handleMessage (message) {
|
||||
if (message.code === 'PINO_CONFIG') {
|
||||
pinoConfig = message.config
|
||||
configReceived.resolve()
|
||||
parentPort.off('message', handleMessage)
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperties(stream, {
|
||||
levels: {
|
||||
get () { return pinoConfig.levels }
|
||||
},
|
||||
messageKey: {
|
||||
get () { return pinoConfig.messageKey }
|
||||
},
|
||||
errorKey: {
|
||||
get () { return pinoConfig.errorKey }
|
||||
}
|
||||
})
|
||||
|
||||
return configReceived.then(finish)
|
||||
}
|
||||
|
||||
return finish()
|
||||
|
||||
function finish () {
|
||||
let res = fn(stream)
|
||||
|
||||
if (res && typeof res.catch === 'function') {
|
||||
res.catch((err) => {
|
||||
stream.destroy(err)
|
||||
})
|
||||
|
||||
// set it to null to not retain a reference to the promise
|
||||
res = null
|
||||
} else if (opts.enablePipelining && res) {
|
||||
return Duplex.from({ writable: stream, readable: res })
|
||||
}
|
||||
|
||||
return stream
|
||||
}
|
||||
}
|
||||
|
||||
function defaultClose (err, cb) {
|
||||
process.nextTick(cb, err)
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "pino-abstract-transport",
|
||||
"version": "2.0.0",
|
||||
"description": "Write Pino transports easily",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"prepare": "husky install",
|
||||
"test": "standard | snazzy && tap test/*.test.js && tsd",
|
||||
"test-ci": "standard | snazzy && tap test/*.test.js --coverage-report=lcovonly && tsd"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/pinojs/pino-abstract-transport.git"
|
||||
},
|
||||
"keywords": [
|
||||
"pino",
|
||||
"transport"
|
||||
],
|
||||
"author": "Matteo Collina <hello@matteocollina.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pinojs/pino-abstract-transport/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pinojs/pino-abstract-transport#readme",
|
||||
"dependencies": {
|
||||
"split2": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.1.0",
|
||||
"husky": "^9.0.6",
|
||||
"snazzy": "^9.0.0",
|
||||
"standard": "^17.0.0",
|
||||
"tap": "^16.0.0",
|
||||
"thread-stream": "^2.6.0",
|
||||
"tsd": "^0.31.0"
|
||||
},
|
||||
"tsd": {
|
||||
"directory": "./test/types"
|
||||
}
|
||||
}
|
||||
+445
@@ -0,0 +1,445 @@
|
||||
'use strict'
|
||||
|
||||
const { once } = require('events')
|
||||
const { Transform, pipeline } = require('stream')
|
||||
|
||||
const { test } = require('tap')
|
||||
const build = require('../')
|
||||
|
||||
test('parse newlined delimited JSON', ({ same, plan }) => {
|
||||
plan(2)
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
same(expected.shift(), line)
|
||||
})
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('parse newlined delimited JSON', ({ same, plan }) => {
|
||||
plan(2)
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
same(expected.shift(), line)
|
||||
})
|
||||
}, { parse: 'json' })
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('null support', ({ same, plan }) => {
|
||||
plan(1)
|
||||
const stream = build(function (source) {
|
||||
source.on('unknown', function (line) {
|
||||
same('null', line)
|
||||
})
|
||||
})
|
||||
|
||||
stream.write('null\n')
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('broken json', ({ match, same, plan }) => {
|
||||
plan(2)
|
||||
const expected = '{ "truncated'
|
||||
const stream = build(function (source) {
|
||||
source.on('unknown', function (line, error) {
|
||||
same(expected, line)
|
||||
const regex = /^(Unexpected end of JSON input|Unterminated string in JSON at position 12)( \(line 1 column 13\))?$/
|
||||
match(error.message, regex)
|
||||
})
|
||||
})
|
||||
|
||||
stream.write(expected + '\n')
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('pure values', ({ same, ok, plan }) => {
|
||||
plan(3)
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
same(line.data, 42)
|
||||
ok(line.time)
|
||||
same(new Date(line.time).getTime(), line.time)
|
||||
})
|
||||
})
|
||||
|
||||
stream.write('42\n')
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('support async iteration', ({ same, plan }) => {
|
||||
plan(2)
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(async function (source) {
|
||||
for await (const line of source) {
|
||||
same(expected.shift(), line)
|
||||
}
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('rejecting errors the stream', async ({ same, plan }) => {
|
||||
const stream = build(async function (source) {
|
||||
throw new Error('kaboom')
|
||||
})
|
||||
|
||||
const [err] = await once(stream, 'error')
|
||||
same(err.message, 'kaboom')
|
||||
})
|
||||
|
||||
test('emits an error if the transport expects pino to send the config, but pino is not going to', async function ({ plan, same }) {
|
||||
plan(1)
|
||||
const stream = build(() => {}, { expectPinoConfig: true })
|
||||
const [err] = await once(stream, 'error')
|
||||
same(err.message, 'This transport is not compatible with the current version of pino. Please upgrade pino to the latest version.')
|
||||
})
|
||||
|
||||
test('set metadata', ({ same, plan, equal }) => {
|
||||
plan(9)
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
const obj = expected.shift()
|
||||
same(this.lastLevel, obj.level)
|
||||
same(this.lastTime, obj.time)
|
||||
same(this.lastObj, obj)
|
||||
same(obj, line)
|
||||
})
|
||||
}, { metadata: true })
|
||||
|
||||
equal(stream[Symbol.for('pino.metadata')], true)
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('parse lines', ({ same, plan, equal }) => {
|
||||
plan(9)
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
const obj = expected.shift()
|
||||
same(this.lastLevel, obj.level)
|
||||
same(this.lastTime, obj.time)
|
||||
same(this.lastObj, obj)
|
||||
same(JSON.stringify(obj), line)
|
||||
})
|
||||
}, { metadata: true, parse: 'lines' })
|
||||
|
||||
equal(stream[Symbol.for('pino.metadata')], true)
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('custom parse line function', ({ same, plan, equal }) => {
|
||||
plan(11)
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
let num = 0
|
||||
|
||||
function parseLine (str) {
|
||||
const obj = JSON.parse(str)
|
||||
same(expected[num], obj)
|
||||
return obj
|
||||
}
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
const obj = expected[num]
|
||||
same(this.lastLevel, obj.level)
|
||||
same(this.lastTime, obj.time)
|
||||
same(this.lastObj, obj)
|
||||
same(obj, line)
|
||||
num++
|
||||
})
|
||||
}, { metadata: true, parseLine })
|
||||
|
||||
equal(stream[Symbol.for('pino.metadata')], true)
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('set metadata (default)', ({ same, plan, equal }) => {
|
||||
plan(9)
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
const obj = expected.shift()
|
||||
same(this.lastLevel, obj.level)
|
||||
same(this.lastTime, obj.time)
|
||||
same(this.lastObj, obj)
|
||||
same(obj, line)
|
||||
})
|
||||
})
|
||||
|
||||
equal(stream[Symbol.for('pino.metadata')], true)
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('do not set metadata', ({ same, plan, equal }) => {
|
||||
plan(9)
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
const obj = expected.shift()
|
||||
same(this.lastLevel, undefined)
|
||||
same(this.lastTime, undefined)
|
||||
same(this.lastObj, undefined)
|
||||
same(obj, line)
|
||||
})
|
||||
}, { metadata: false })
|
||||
|
||||
equal(stream[Symbol.for('pino.metadata')], undefined)
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('close logic', ({ same, plan, pass }) => {
|
||||
plan(3)
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
same(expected.shift(), line)
|
||||
})
|
||||
}, {
|
||||
close (err, cb) {
|
||||
pass('close called')
|
||||
process.nextTick(cb, err)
|
||||
}
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('close with promises', ({ same, plan, pass }) => {
|
||||
plan(3)
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const stream = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
same(expected.shift(), line)
|
||||
})
|
||||
}, {
|
||||
async close () {
|
||||
pass('close called')
|
||||
}
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test('support Transform streams', ({ same, plan, error }) => {
|
||||
plan(7)
|
||||
|
||||
const expected1 = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const expected2 = []
|
||||
|
||||
const stream1 = build(function (source) {
|
||||
const transform = new Transform({
|
||||
objectMode: true,
|
||||
autoDestroy: true,
|
||||
transform (chunk, enc, cb) {
|
||||
same(expected1.shift(), chunk)
|
||||
chunk.service = 'from transform'
|
||||
expected2.push(chunk)
|
||||
cb(null, JSON.stringify(chunk) + '\n')
|
||||
}
|
||||
})
|
||||
|
||||
pipeline(source, transform, () => {})
|
||||
|
||||
return transform
|
||||
}, { enablePipelining: true })
|
||||
|
||||
const stream2 = build(function (source) {
|
||||
source.on('data', function (line) {
|
||||
same(expected2.shift(), line)
|
||||
})
|
||||
})
|
||||
|
||||
pipeline(stream1, stream2, function (err) {
|
||||
error(err)
|
||||
same(expected1, [])
|
||||
same(expected2, [])
|
||||
})
|
||||
|
||||
const lines = expected1.map(JSON.stringify).join('\n')
|
||||
stream1.write(lines)
|
||||
stream1.end()
|
||||
})
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const build = require('../..')
|
||||
|
||||
module.exports = async function (threadStreamOpts) {
|
||||
const { port, opts = {} } = threadStreamOpts
|
||||
return build(
|
||||
async function (source) {
|
||||
for await (const obj of source) {
|
||||
port.postMessage({
|
||||
data: obj,
|
||||
pinoConfig: {
|
||||
levels: source.levels,
|
||||
messageKey: source.messageKey,
|
||||
errorKey: source.errorKey
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
opts
|
||||
)
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const build = require('../..')
|
||||
|
||||
module.exports = async function (threadStreamOpts) {
|
||||
const { port, opts = {} } = threadStreamOpts
|
||||
return build(
|
||||
function (source) {
|
||||
source.on('data', function (line) {
|
||||
port.postMessage({
|
||||
data: line,
|
||||
pinoConfig: {
|
||||
levels: source.levels,
|
||||
messageKey: source.messageKey,
|
||||
errorKey: source.errorKey
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
opts
|
||||
)
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
'use strict'
|
||||
|
||||
const { Transform, pipeline } = require('stream')
|
||||
const build = require('../..')
|
||||
|
||||
module.exports = function (threadStreamOpts) {
|
||||
const { opts = {} } = threadStreamOpts
|
||||
return build(function (source) {
|
||||
const transform = new Transform({
|
||||
objectMode: true,
|
||||
autoDestroy: true,
|
||||
transform (chunk, enc, cb) {
|
||||
chunk.service = 'from transform'
|
||||
chunk.level = `${source.levels.labels[chunk.level]}(${chunk.level})`
|
||||
chunk[source.messageKey] = chunk[source.messageKey].toUpperCase()
|
||||
cb(null, JSON.stringify(chunk) + '\n')
|
||||
}
|
||||
})
|
||||
|
||||
pipeline(source, transform, () => {})
|
||||
|
||||
return transform
|
||||
}, { ...opts, enablePipelining: true })
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
'use strict'
|
||||
|
||||
const { pipeline, PassThrough } = require('stream')
|
||||
|
||||
module.exports = async function ({ targets }) {
|
||||
const streams = await Promise.all(targets.map(async (t) => {
|
||||
const fn = require(t.target)
|
||||
const stream = await fn(t.options)
|
||||
return stream
|
||||
}))
|
||||
|
||||
const stream = new PassThrough()
|
||||
pipeline(stream, ...streams, () => {})
|
||||
return stream
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import build, { OnUnknown } from "../../index";
|
||||
import { expectType } from "tsd";
|
||||
import { Transform } from "stream";
|
||||
|
||||
/**
|
||||
* If enablePipelining is set to true, the function passed as an argument
|
||||
* must return a transform. The unknown event should be listened to on the
|
||||
* stream passed in the first argument.
|
||||
*/
|
||||
expectType<Transform>(build((source) => source, { enablePipelining: true }));
|
||||
|
||||
/**
|
||||
* If expectPinoConfig is set with enablePipelining, build returns a promise
|
||||
*/
|
||||
expectType<(Promise<Transform>)>(build((source) => source, { enablePipelining: true, expectPinoConfig: true }));
|
||||
|
||||
/**
|
||||
* If enablePipelining is not set the unknown event can be listened to on
|
||||
* the returned stream.
|
||||
*/
|
||||
expectType<Transform & OnUnknown>(build((source) => {}));
|
||||
|
||||
/**
|
||||
* If expectPinoConfig is set, build returns a promise
|
||||
*/
|
||||
expectType<(Promise<Transform & OnUnknown>)>(build((source) => {}, { expectPinoConfig: true }));
|
||||
|
||||
/**
|
||||
* build also accepts an async function
|
||||
*/
|
||||
expectType<Transform & OnUnknown>(build(async (source) => {}));
|
||||
+357
@@ -0,0 +1,357 @@
|
||||
'use strict'
|
||||
|
||||
const { once } = require('events')
|
||||
const { join } = require('path')
|
||||
const ThreadStream = require('thread-stream')
|
||||
const { MessageChannel } = require('worker_threads')
|
||||
const { test } = require('tap')
|
||||
|
||||
workerTest('transport-on-data.js')
|
||||
workerTest('transport-async-iteration.js', ' when using async iteration')
|
||||
|
||||
function workerTest (filename, description = '') {
|
||||
test(`does not wait for pino to send config by default${description}`, function ({ same, plan }) {
|
||||
plan(4)
|
||||
const { port1, port2 } = new MessageChannel()
|
||||
const stream = new ThreadStream({
|
||||
filename: join(__dirname, 'fixtures', filename),
|
||||
workerData: { port: port1 },
|
||||
workerOpts: {
|
||||
transferList: [port1]
|
||||
}
|
||||
})
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const emptyPinoConfig = {
|
||||
levels: undefined,
|
||||
messageKey: undefined,
|
||||
errorKey: undefined
|
||||
}
|
||||
|
||||
port2.on('message', function (message) {
|
||||
same(expected.shift(), message.data)
|
||||
same(emptyPinoConfig, message.pinoConfig)
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test(`does not wait for pino to send config if transport is not expecting it${description}`, function ({ same, plan }) {
|
||||
plan(4)
|
||||
const { port1, port2 } = new MessageChannel()
|
||||
const stream = new ThreadStream({
|
||||
filename: join(__dirname, 'fixtures', filename),
|
||||
workerData: {
|
||||
port: port1,
|
||||
pinoWillSendConfig: true
|
||||
},
|
||||
workerOpts: {
|
||||
transferList: [port1]
|
||||
}
|
||||
})
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const emptyPinoConfig = {
|
||||
levels: undefined,
|
||||
messageKey: undefined,
|
||||
errorKey: undefined
|
||||
}
|
||||
|
||||
const pinoConfig = {
|
||||
levels: {
|
||||
labels: { 30: 'info' },
|
||||
values: { info: 30 }
|
||||
},
|
||||
messageKey: 'msg',
|
||||
errorKey: 'err'
|
||||
}
|
||||
|
||||
stream.emit('message', { code: 'PINO_CONFIG', config: pinoConfig })
|
||||
|
||||
port2.on('message', function (message) {
|
||||
same(expected.shift(), message.data)
|
||||
same(emptyPinoConfig, message.pinoConfig)
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test(`waits for the pino config when pino intends to send it and the transport requests it${description}`, function ({ same, plan }) {
|
||||
plan(4)
|
||||
const { port1, port2 } = new MessageChannel()
|
||||
const stream = new ThreadStream({
|
||||
filename: join(__dirname, 'fixtures', filename),
|
||||
workerData: {
|
||||
port: port1,
|
||||
pinoWillSendConfig: true,
|
||||
opts: {
|
||||
expectPinoConfig: true
|
||||
}
|
||||
},
|
||||
workerOpts: {
|
||||
transferList: [port1]
|
||||
}
|
||||
})
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const pinoConfig = {
|
||||
levels: {
|
||||
labels: { 30: 'info' },
|
||||
values: { info: 30 }
|
||||
},
|
||||
messageKey: 'msg',
|
||||
errorKey: 'err'
|
||||
}
|
||||
|
||||
port2.on('message', function (message) {
|
||||
same(expected.shift(), message.data)
|
||||
same(pinoConfig, message.pinoConfig)
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.emit('message', { code: 'PINO_CONFIG', config: pinoConfig })
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test(`continues to listen if it receives a message that is not PINO_CONFIG${description}`, function ({ same, plan }) {
|
||||
plan(4)
|
||||
const { port1, port2 } = new MessageChannel()
|
||||
const stream = new ThreadStream({
|
||||
filename: join(__dirname, 'fixtures', 'transport-on-data.js'),
|
||||
workerData: {
|
||||
port: port1,
|
||||
pinoWillSendConfig: true,
|
||||
opts: {
|
||||
expectPinoConfig: true
|
||||
}
|
||||
},
|
||||
workerOpts: {
|
||||
transferList: [port1]
|
||||
}
|
||||
})
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const pinoConfig = {
|
||||
levels: {
|
||||
labels: { 30: 'info' },
|
||||
values: { info: 30 }
|
||||
},
|
||||
messageKey: 'msg',
|
||||
errorKey: 'err'
|
||||
}
|
||||
|
||||
port2.on('message', function (message) {
|
||||
same(expected.shift(), message.data)
|
||||
same(pinoConfig, message.pinoConfig)
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.emit('message', 'not a PINO_CONFIG')
|
||||
stream.emit('message', { code: 'NOT_PINO_CONFIG', config: { levels: 'foo', messageKey: 'bar', errorKey: 'baz' } })
|
||||
stream.emit('message', { code: 'PINO_CONFIG', config: pinoConfig })
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test(`waits for the pino config even if it is sent after write${description}`, function ({ same, plan }) {
|
||||
plan(4)
|
||||
const { port1, port2 } = new MessageChannel()
|
||||
const stream = new ThreadStream({
|
||||
filename: join(__dirname, 'fixtures', filename),
|
||||
workerData: {
|
||||
port: port1,
|
||||
pinoWillSendConfig: true,
|
||||
opts: {
|
||||
expectPinoConfig: true
|
||||
}
|
||||
},
|
||||
workerOpts: {
|
||||
transferList: [port1]
|
||||
}
|
||||
})
|
||||
|
||||
const expected = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}]
|
||||
|
||||
const pinoConfig = {
|
||||
levels: {
|
||||
labels: { 30: 'info' },
|
||||
values: { info: 30 }
|
||||
},
|
||||
messageKey: 'msg',
|
||||
errorKey: 'err'
|
||||
}
|
||||
|
||||
port2.on('message', function (message) {
|
||||
same(expected.shift(), message.data)
|
||||
same(pinoConfig, message.pinoConfig)
|
||||
})
|
||||
|
||||
const lines = expected.map(JSON.stringify).join('\n')
|
||||
stream.write(lines)
|
||||
stream.emit('message', { code: 'PINO_CONFIG', config: pinoConfig })
|
||||
stream.end()
|
||||
})
|
||||
|
||||
test(`emits an error if the transport expects pino to send the config, but pino is not going to${description}`, async function ({ plan, same, ok }) {
|
||||
plan(2)
|
||||
const stream = new ThreadStream({
|
||||
filename: join(__dirname, 'fixtures', filename),
|
||||
workerData: {
|
||||
opts: {
|
||||
expectPinoConfig: true
|
||||
}
|
||||
}
|
||||
})
|
||||
const [err] = await once(stream, 'error')
|
||||
same(err.message, 'This transport is not compatible with the current version of pino. Please upgrade pino to the latest version.')
|
||||
ok(stream.destroyed)
|
||||
})
|
||||
}
|
||||
|
||||
test('waits for the pino config when pipelining', function ({ same, plan }) {
|
||||
plan(2)
|
||||
const { port1, port2 } = new MessageChannel()
|
||||
const stream = new ThreadStream({
|
||||
filename: join(__dirname, 'fixtures', 'worker-pipeline.js'),
|
||||
workerData: {
|
||||
pinoWillSendConfig: true,
|
||||
targets: [{
|
||||
target: './transport-transform.js',
|
||||
options: {
|
||||
opts: { expectPinoConfig: true }
|
||||
}
|
||||
}, {
|
||||
target: './transport-on-data.js',
|
||||
options: {
|
||||
port: port1
|
||||
}
|
||||
}]
|
||||
},
|
||||
workerOpts: {
|
||||
transferList: [port1]
|
||||
}
|
||||
})
|
||||
|
||||
const expected = [{
|
||||
level: 'info(30)',
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'HELLO WORLD',
|
||||
service: 'from transform'
|
||||
}, {
|
||||
level: 'info(30)',
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'ANOTHER MESSAGE',
|
||||
prop: 42,
|
||||
service: 'from transform'
|
||||
}]
|
||||
|
||||
const lines = [{
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'hello world'
|
||||
}, {
|
||||
level: 30,
|
||||
time: 1617955768092,
|
||||
pid: 2942,
|
||||
hostname: 'MacBook-Pro.local',
|
||||
msg: 'another message',
|
||||
prop: 42
|
||||
}].map(JSON.stringify).join('\n')
|
||||
|
||||
const pinoConfig = {
|
||||
levels: {
|
||||
labels: { 30: 'info' },
|
||||
values: { info: 30 }
|
||||
},
|
||||
messageKey: 'msg',
|
||||
errorKey: 'err'
|
||||
}
|
||||
|
||||
port2.on('message', function (message) {
|
||||
same(expected.shift(), message.data)
|
||||
})
|
||||
|
||||
stream.emit('message', { code: 'PINO_CONFIG', config: pinoConfig })
|
||||
stream.write(lines)
|
||||
stream.end()
|
||||
})
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
# [*.md]
|
||||
# trim_trailing_whitespace = false
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
index.d.ts
|
||||
test/types/index.test-d.ts
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
open-pull-requests-limit: 10
|
||||
|
||||
- package-ecosystem: "npm"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
open-pull-requests-limit: 10
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
|
||||
# This allows a subsequently queued workflow run to interrupt previous runs
|
||||
concurrency:
|
||||
group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
dependency-review:
|
||||
name: Dependency Review
|
||||
if: github.event_name == 'pull_request'
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- name: Check out repo
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Dependency review
|
||||
uses: actions/dependency-review-action@v4
|
||||
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
node-version: [18, 20]
|
||||
steps:
|
||||
- name: Check out repo
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Setup Node ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm install --ignore-scripts
|
||||
env:
|
||||
NODE_ENV: development
|
||||
|
||||
- name: Lint-CI
|
||||
run: npm run lint-ci
|
||||
|
||||
- name: Test-Types
|
||||
run: npm run test-types
|
||||
|
||||
- name: Test-CI
|
||||
run: npm run test-ci
|
||||
|
||||
automerge:
|
||||
name: Automerge Dependabot PRs
|
||||
if: >
|
||||
github.event_name == 'pull_request' &&
|
||||
github.event.pull_request.user.login == 'dependabot[bot]'
|
||||
needs: test
|
||||
permissions:
|
||||
pull-requests: write
|
||||
contents: write
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: fastify/github-action-merge-dependabot@v3
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
Copyright Mateo Collina, David Mark Clements, James Sumners
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
# pino-std-serializers [](https://github.com/pinojs/pino-std-serializers/actions?query=workflow%3ACI)
|
||||
|
||||
This module provides a set of standard object serializers for the
|
||||
[Pino](https://getpino.io) logger.
|
||||
|
||||
## Serializers
|
||||
|
||||
### `exports.err(error)`
|
||||
Serializes an `Error` like object. Returns an object:
|
||||
|
||||
```js
|
||||
{
|
||||
type: 'string', // The name of the object's constructor.
|
||||
message: 'string', // The supplied error message.
|
||||
stack: 'string', // The stack when the error was generated.
|
||||
raw: Error // Non-enumerable, i.e. will not be in the output, original
|
||||
// Error object. This is available for subsequent serializers
|
||||
// to use.
|
||||
[...any additional Enumerable property the original Error had]
|
||||
}
|
||||
```
|
||||
|
||||
Any other extra properties, e.g. `statusCode`, that have been attached to the
|
||||
object will also be present on the serialized object.
|
||||
|
||||
If the error object has a [`cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) property, the `cause`'s `message` and `stack` will be appended to the top-level `message` and `stack`. All other parameters that belong to the `error.cause` object will be omitted.
|
||||
|
||||
Example:
|
||||
|
||||
```js
|
||||
const serializer = require('pino-std-serializers').err;
|
||||
|
||||
const innerError = new Error("inner error");
|
||||
innerError.isInner = true;
|
||||
const outerError = new Error("outer error", { cause: innerError });
|
||||
outerError.isInner = false;
|
||||
|
||||
const serialized = serializer(outerError);
|
||||
/* Result:
|
||||
{
|
||||
"type": "Error",
|
||||
"message": "outer error: inner error",
|
||||
"isInner": false,
|
||||
"stack": "Error: outer error
|
||||
at <...omitted..>
|
||||
caused by: Error: inner error
|
||||
at <...omitted..>
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
### `exports.errWithCause(error)`
|
||||
Serializes an `Error` like object, including any `error.cause`. Returns an object:
|
||||
|
||||
```js
|
||||
{
|
||||
type: 'string', // The name of the object's constructor.
|
||||
message: 'string', // The supplied error message.
|
||||
stack: 'string', // The stack when the error was generated.
|
||||
cause?: Error, // If the original error had an error.cause, it will be serialized here
|
||||
raw: Error // Non-enumerable, i.e. will not be in the output, original
|
||||
// Error object. This is available for subsequent serializers
|
||||
// to use.
|
||||
[...any additional Enumerable property the original Error had]
|
||||
}
|
||||
```
|
||||
|
||||
Any other extra properties, e.g. `statusCode`, that have been attached to the object will also be present on the serialized object.
|
||||
|
||||
Example:
|
||||
```javascript
|
||||
const serializer = require('pino-std-serializers').errWithCause;
|
||||
|
||||
const innerError = new Error("inner error");
|
||||
innerError.isInner = true;
|
||||
const outerError = new Error("outer error", { cause: innerError });
|
||||
outerError.isInner = false;
|
||||
|
||||
const serialized = serializer(outerError);
|
||||
/* Result:
|
||||
{
|
||||
"type": "Error",
|
||||
"message": "outer error",
|
||||
"isInner": false,
|
||||
"stack": "Error: outer error
|
||||
at <...omitted..>",
|
||||
"cause": {
|
||||
"type": "Error",
|
||||
"message": "inner error",
|
||||
"isInner": true,
|
||||
"stack": "Error: inner error
|
||||
at <...omitted..>"
|
||||
},
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
### `exports.mapHttpResponse(response)`
|
||||
Used internally by Pino for general response logging. Returns an object:
|
||||
|
||||
```js
|
||||
{
|
||||
res: {}
|
||||
}
|
||||
```
|
||||
|
||||
Where `res` is the `response` as serialized by the standard response serializer.
|
||||
|
||||
### `exports.mapHttpRequest(request)`
|
||||
Used internall by Pino for general request logging. Returns an object:
|
||||
|
||||
```js
|
||||
{
|
||||
req: {}
|
||||
}
|
||||
```
|
||||
|
||||
Where `req` is the `request` as serialized by the standard request serializer.
|
||||
|
||||
### `exports.req(request)`
|
||||
The default `request` serializer. Returns an object:
|
||||
|
||||
```js
|
||||
{
|
||||
id: 'string', // Defaults to `undefined`, unless there is an `id` property
|
||||
// already attached to the `request` object or to the `request.info`
|
||||
// object. Attach a synchronous function
|
||||
// to the `request.id` that returns an identifier to have
|
||||
// the value filled.
|
||||
method: 'string',
|
||||
url: 'string', // the request pathname (as per req.url in core HTTP)
|
||||
query: 'object', // the request query (as per req.query in express or hapi)
|
||||
params: 'object', // the request params (as per req.params in express or hapi)
|
||||
headers: Object, // a reference to the `headers` object from the request
|
||||
// (as per req.headers in core HTTP)
|
||||
remoteAddress: 'string',
|
||||
remotePort: Number,
|
||||
raw: Object // Non-enumerable, i.e. will not be in the output, original
|
||||
// request object. This is available for subsequent serializers
|
||||
// to use. In cases where the `request` input already has
|
||||
// a `raw` property this will replace the original `request.raw`
|
||||
// property
|
||||
}
|
||||
```
|
||||
|
||||
### `exports.res(response)`
|
||||
The default `response` serializer. Returns an object:
|
||||
|
||||
```js
|
||||
{
|
||||
statusCode: Number, // Response status code, will be null before headers are flushed
|
||||
headers: Object, // The headers to be sent in the response.
|
||||
raw: Object // Non-enumerable, i.e. will not be in the output, original
|
||||
// response object. This is available for subsequent serializers
|
||||
// to use.
|
||||
}
|
||||
```
|
||||
|
||||
### `exports.wrapErrorSerializer(customSerializer)`
|
||||
A utility method for wrapping the default error serializer. This allows
|
||||
custom serializers to work with the already serialized object.
|
||||
|
||||
The `customSerializer` accepts one parameter — the newly serialized error
|
||||
object — and returns the new (or updated) error object.
|
||||
|
||||
### `exports.wrapRequestSerializer(customSerializer)`
|
||||
A utility method for wrapping the default request serializer. This allows
|
||||
custom serializers to work with the already serialized object.
|
||||
|
||||
The `customSerializer` accepts one parameter — the newly serialized request
|
||||
object — and returns the new (or updated) request object.
|
||||
|
||||
### `exports.wrapResponseSerializer(customSerializer)`
|
||||
A utility method for wrapping the default response serializer. This allows
|
||||
custom serializers to work with the already serialized object.
|
||||
|
||||
The `customSerializer` accepts one parameter — the newly serialized response
|
||||
object — and returns the new (or updated) response object.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
// Type definitions for pino-std-serializers 2.4
|
||||
// Definitions by: Connor Fitzgerald <https://github.com/connorjayfitzgerald>
|
||||
// Igor Savin <https://github.com/kibertoad>
|
||||
// TypeScript Version: 2.7
|
||||
|
||||
/// <reference types="node" />
|
||||
import { IncomingMessage, ServerResponse } from 'http';
|
||||
|
||||
export interface SerializedError {
|
||||
/**
|
||||
* The name of the object's constructor.
|
||||
*/
|
||||
type: string;
|
||||
/**
|
||||
* The supplied error message.
|
||||
*/
|
||||
message: string;
|
||||
/**
|
||||
* The stack when the error was generated.
|
||||
*/
|
||||
stack: string;
|
||||
/**
|
||||
* Non-enumerable. The original Error object. This will not be included in the logged output.
|
||||
* This is available for subsequent serializers to use.
|
||||
*/
|
||||
raw: Error;
|
||||
/**
|
||||
* `cause` is never included in the log output, if you need the `cause`, use {@link raw.cause}
|
||||
*/
|
||||
cause?: never;
|
||||
/**
|
||||
* Any other extra properties that have been attached to the object will also be present on the serialized object.
|
||||
*/
|
||||
[key: string]: any;
|
||||
[key: number]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes an Error object. Does not serialize "err.cause" fields (will append the err.cause.message to err.message
|
||||
* and err.cause.stack to err.stack)
|
||||
*/
|
||||
export function err(err: Error): SerializedError;
|
||||
|
||||
/**
|
||||
* Serializes an Error object, including full serialization for any err.cause fields recursively.
|
||||
*/
|
||||
export function errWithCause(err: Error): SerializedError;
|
||||
|
||||
export interface SerializedRequest {
|
||||
/**
|
||||
* Defaults to `undefined`, unless there is an `id` property already attached to the `request` object or
|
||||
* to the `request.info` object. Attach a synchronous function to the `request.id` that returns an
|
||||
* identifier to have the value filled.
|
||||
*/
|
||||
id: string | undefined;
|
||||
/**
|
||||
* HTTP method.
|
||||
*/
|
||||
method: string;
|
||||
/**
|
||||
* Request pathname (as per req.url in core HTTP).
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* Reference to the `headers` object from the request (as per req.headers in core HTTP).
|
||||
*/
|
||||
headers: Record<string, string>;
|
||||
remoteAddress: string;
|
||||
remotePort: number;
|
||||
params: Record<string, string>;
|
||||
query: Record<string, string>;
|
||||
|
||||
/**
|
||||
* Non-enumerable, i.e. will not be in the output, original request object. This is available for subsequent
|
||||
* serializers to use. In cases where the `request` input already has a `raw` property this will
|
||||
* replace the original `request.raw` property.
|
||||
*/
|
||||
raw: IncomingMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes a Request object.
|
||||
*/
|
||||
export function req(req: IncomingMessage): SerializedRequest;
|
||||
|
||||
/**
|
||||
* Used internally by Pino for general request logging.
|
||||
*/
|
||||
export function mapHttpRequest(req: IncomingMessage): {
|
||||
req: SerializedRequest
|
||||
};
|
||||
|
||||
export interface SerializedResponse {
|
||||
/**
|
||||
* HTTP status code.
|
||||
*/
|
||||
statusCode: number;
|
||||
/**
|
||||
* The headers to be sent in the response.
|
||||
*/
|
||||
headers: Record<string, string>;
|
||||
/**
|
||||
* Non-enumerable, i.e. will not be in the output, original response object. This is available for subsequent serializers to use.
|
||||
*/
|
||||
raw: ServerResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes a Response object.
|
||||
*/
|
||||
export function res(res: ServerResponse): SerializedResponse;
|
||||
|
||||
/**
|
||||
* Used internally by Pino for general response logging.
|
||||
*/
|
||||
export function mapHttpResponse(res: ServerResponse): {
|
||||
res: SerializedResponse
|
||||
};
|
||||
|
||||
export type CustomErrorSerializer = (err: SerializedError) => Record<string, any>;
|
||||
|
||||
/**
|
||||
* A utility method for wrapping the default error serializer.
|
||||
* This allows custom serializers to work with the already serialized object.
|
||||
* The customSerializer accepts one parameter — the newly serialized error object — and returns the new (or updated) error object.
|
||||
*/
|
||||
export function wrapErrorSerializer(customSerializer: CustomErrorSerializer): (err: Error) => Record<string, any>;
|
||||
|
||||
export type CustomRequestSerializer = (req: SerializedRequest) => Record<string, any>;
|
||||
|
||||
/**
|
||||
* A utility method for wrapping the default request serializer.
|
||||
* This allows custom serializers to work with the already serialized object.
|
||||
* The customSerializer accepts one parameter — the newly serialized request object — and returns the new (or updated) request object.
|
||||
*/
|
||||
export function wrapRequestSerializer(customSerializer: CustomRequestSerializer): (req: IncomingMessage) => Record<string, any>;
|
||||
|
||||
export type CustomResponseSerializer = (res: SerializedResponse) => Record<string, any>;
|
||||
|
||||
/**
|
||||
* A utility method for wrapping the default response serializer.
|
||||
* This allows custom serializers to work with the already serialized object.
|
||||
* The customSerializer accepts one parameter — the newly serialized response object — and returns the new (or updated) response object.
|
||||
*/
|
||||
export function wrapResponseSerializer(customSerializer: CustomResponseSerializer): (res: ServerResponse) => Record<string, any>;
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
'use strict'
|
||||
|
||||
const errSerializer = require('./lib/err')
|
||||
const errWithCauseSerializer = require('./lib/err-with-cause')
|
||||
const reqSerializers = require('./lib/req')
|
||||
const resSerializers = require('./lib/res')
|
||||
|
||||
module.exports = {
|
||||
err: errSerializer,
|
||||
errWithCause: errWithCauseSerializer,
|
||||
mapHttpRequest: reqSerializers.mapHttpRequest,
|
||||
mapHttpResponse: resSerializers.mapHttpResponse,
|
||||
req: reqSerializers.reqSerializer,
|
||||
res: resSerializers.resSerializer,
|
||||
|
||||
wrapErrorSerializer: function wrapErrorSerializer (customSerializer) {
|
||||
if (customSerializer === errSerializer) return customSerializer
|
||||
return function wrapErrSerializer (err) {
|
||||
return customSerializer(errSerializer(err))
|
||||
}
|
||||
},
|
||||
|
||||
wrapRequestSerializer: function wrapRequestSerializer (customSerializer) {
|
||||
if (customSerializer === reqSerializers.reqSerializer) return customSerializer
|
||||
return function wrappedReqSerializer (req) {
|
||||
return customSerializer(reqSerializers.reqSerializer(req))
|
||||
}
|
||||
},
|
||||
|
||||
wrapResponseSerializer: function wrapResponseSerializer (customSerializer) {
|
||||
if (customSerializer === resSerializers.resSerializer) return customSerializer
|
||||
return function wrappedResSerializer (res) {
|
||||
return customSerializer(resSerializers.resSerializer(res))
|
||||
}
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
'use strict'
|
||||
|
||||
// **************************************************************
|
||||
// * Code initially copied/adapted from "pony-cause" npm module *
|
||||
// * Please upstream improvements there *
|
||||
// **************************************************************
|
||||
|
||||
const isErrorLike = (err) => {
|
||||
return err && typeof err.message === 'string'
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Error|{ cause?: unknown|(()=>err)}} err
|
||||
* @returns {Error|Object|undefined}
|
||||
*/
|
||||
const getErrorCause = (err) => {
|
||||
if (!err) return
|
||||
|
||||
/** @type {unknown} */
|
||||
// @ts-ignore
|
||||
const cause = err.cause
|
||||
|
||||
// VError / NError style causes
|
||||
if (typeof cause === 'function') {
|
||||
// @ts-ignore
|
||||
const causeResult = err.cause()
|
||||
|
||||
return isErrorLike(causeResult)
|
||||
? causeResult
|
||||
: undefined
|
||||
} else {
|
||||
return isErrorLike(cause)
|
||||
? cause
|
||||
: undefined
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method that keeps a track of which error we have already added, to avoid circular recursion
|
||||
*
|
||||
* @private
|
||||
* @param {Error} err
|
||||
* @param {Set<Error>} seen
|
||||
* @returns {string}
|
||||
*/
|
||||
const _stackWithCauses = (err, seen) => {
|
||||
if (!isErrorLike(err)) return ''
|
||||
|
||||
const stack = err.stack || ''
|
||||
|
||||
// Ensure we don't go circular or crazily deep
|
||||
if (seen.has(err)) {
|
||||
return stack + '\ncauses have become circular...'
|
||||
}
|
||||
|
||||
const cause = getErrorCause(err)
|
||||
|
||||
if (cause) {
|
||||
seen.add(err)
|
||||
return (stack + '\ncaused by: ' + _stackWithCauses(cause, seen))
|
||||
} else {
|
||||
return stack
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Error} err
|
||||
* @returns {string}
|
||||
*/
|
||||
const stackWithCauses = (err) => _stackWithCauses(err, new Set())
|
||||
|
||||
/**
|
||||
* Internal method that keeps a track of which error we have already added, to avoid circular recursion
|
||||
*
|
||||
* @private
|
||||
* @param {Error} err
|
||||
* @param {Set<Error>} seen
|
||||
* @param {boolean} [skip]
|
||||
* @returns {string}
|
||||
*/
|
||||
const _messageWithCauses = (err, seen, skip) => {
|
||||
if (!isErrorLike(err)) return ''
|
||||
|
||||
const message = skip ? '' : (err.message || '')
|
||||
|
||||
// Ensure we don't go circular or crazily deep
|
||||
if (seen.has(err)) {
|
||||
return message + ': ...'
|
||||
}
|
||||
|
||||
const cause = getErrorCause(err)
|
||||
|
||||
if (cause) {
|
||||
seen.add(err)
|
||||
|
||||
// @ts-ignore
|
||||
const skipIfVErrorStyleCause = typeof err.cause === 'function'
|
||||
|
||||
return (message +
|
||||
(skipIfVErrorStyleCause ? '' : ': ') +
|
||||
_messageWithCauses(cause, seen, skipIfVErrorStyleCause))
|
||||
} else {
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Error} err
|
||||
* @returns {string}
|
||||
*/
|
||||
const messageWithCauses = (err) => _messageWithCauses(err, new Set())
|
||||
|
||||
module.exports = {
|
||||
isErrorLike,
|
||||
getErrorCause,
|
||||
stackWithCauses,
|
||||
messageWithCauses
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
'use strict'
|
||||
|
||||
const seen = Symbol('circular-ref-tag')
|
||||
const rawSymbol = Symbol('pino-raw-err-ref')
|
||||
|
||||
const pinoErrProto = Object.create({}, {
|
||||
type: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: undefined
|
||||
},
|
||||
message: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: undefined
|
||||
},
|
||||
stack: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: undefined
|
||||
},
|
||||
aggregateErrors: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: undefined
|
||||
},
|
||||
raw: {
|
||||
enumerable: false,
|
||||
get: function () {
|
||||
return this[rawSymbol]
|
||||
},
|
||||
set: function (val) {
|
||||
this[rawSymbol] = val
|
||||
}
|
||||
}
|
||||
})
|
||||
Object.defineProperty(pinoErrProto, rawSymbol, {
|
||||
writable: true,
|
||||
value: {}
|
||||
})
|
||||
|
||||
module.exports = {
|
||||
pinoErrProto,
|
||||
pinoErrorSymbols: {
|
||||
seen,
|
||||
rawSymbol
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = errWithCauseSerializer
|
||||
|
||||
const { isErrorLike } = require('./err-helpers')
|
||||
const { pinoErrProto, pinoErrorSymbols } = require('./err-proto')
|
||||
const { seen } = pinoErrorSymbols
|
||||
|
||||
const { toString } = Object.prototype
|
||||
|
||||
function errWithCauseSerializer (err) {
|
||||
if (!isErrorLike(err)) {
|
||||
return err
|
||||
}
|
||||
|
||||
err[seen] = undefined // tag to prevent re-looking at this
|
||||
const _err = Object.create(pinoErrProto)
|
||||
_err.type = toString.call(err.constructor) === '[object Function]'
|
||||
? err.constructor.name
|
||||
: err.name
|
||||
_err.message = err.message
|
||||
_err.stack = err.stack
|
||||
|
||||
if (Array.isArray(err.errors)) {
|
||||
_err.aggregateErrors = err.errors.map(err => errWithCauseSerializer(err))
|
||||
}
|
||||
|
||||
if (isErrorLike(err.cause) && !Object.prototype.hasOwnProperty.call(err.cause, seen)) {
|
||||
_err.cause = errWithCauseSerializer(err.cause)
|
||||
}
|
||||
|
||||
for (const key in err) {
|
||||
if (_err[key] === undefined) {
|
||||
const val = err[key]
|
||||
if (isErrorLike(val)) {
|
||||
if (!Object.prototype.hasOwnProperty.call(val, seen)) {
|
||||
_err[key] = errWithCauseSerializer(val)
|
||||
}
|
||||
} else {
|
||||
_err[key] = val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete err[seen] // clean up tag in case err is serialized again later
|
||||
_err.raw = err
|
||||
return _err
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = errSerializer
|
||||
|
||||
const { messageWithCauses, stackWithCauses, isErrorLike } = require('./err-helpers')
|
||||
const { pinoErrProto, pinoErrorSymbols } = require('./err-proto')
|
||||
const { seen } = pinoErrorSymbols
|
||||
|
||||
const { toString } = Object.prototype
|
||||
|
||||
function errSerializer (err) {
|
||||
if (!isErrorLike(err)) {
|
||||
return err
|
||||
}
|
||||
|
||||
err[seen] = undefined // tag to prevent re-looking at this
|
||||
const _err = Object.create(pinoErrProto)
|
||||
_err.type = toString.call(err.constructor) === '[object Function]'
|
||||
? err.constructor.name
|
||||
: err.name
|
||||
_err.message = messageWithCauses(err)
|
||||
_err.stack = stackWithCauses(err)
|
||||
|
||||
if (Array.isArray(err.errors)) {
|
||||
_err.aggregateErrors = err.errors.map(err => errSerializer(err))
|
||||
}
|
||||
|
||||
for (const key in err) {
|
||||
if (_err[key] === undefined) {
|
||||
const val = err[key]
|
||||
if (isErrorLike(val)) {
|
||||
// We append cause messages and stacks to _err, therefore skipping causes here
|
||||
if (key !== 'cause' && !Object.prototype.hasOwnProperty.call(val, seen)) {
|
||||
_err[key] = errSerializer(val)
|
||||
}
|
||||
} else {
|
||||
_err[key] = val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete err[seen] // clean up tag in case err is serialized again later
|
||||
_err.raw = err
|
||||
return _err
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
mapHttpRequest,
|
||||
reqSerializer
|
||||
}
|
||||
|
||||
const rawSymbol = Symbol('pino-raw-req-ref')
|
||||
const pinoReqProto = Object.create({}, {
|
||||
id: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: ''
|
||||
},
|
||||
method: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: ''
|
||||
},
|
||||
url: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: ''
|
||||
},
|
||||
query: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: ''
|
||||
},
|
||||
params: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: ''
|
||||
},
|
||||
headers: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: {}
|
||||
},
|
||||
remoteAddress: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: ''
|
||||
},
|
||||
remotePort: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: ''
|
||||
},
|
||||
raw: {
|
||||
enumerable: false,
|
||||
get: function () {
|
||||
return this[rawSymbol]
|
||||
},
|
||||
set: function (val) {
|
||||
this[rawSymbol] = val
|
||||
}
|
||||
}
|
||||
})
|
||||
Object.defineProperty(pinoReqProto, rawSymbol, {
|
||||
writable: true,
|
||||
value: {}
|
||||
})
|
||||
|
||||
function reqSerializer (req) {
|
||||
// req.info is for hapi compat.
|
||||
const connection = req.info || req.socket
|
||||
const _req = Object.create(pinoReqProto)
|
||||
_req.id = (typeof req.id === 'function' ? req.id() : (req.id || (req.info ? req.info.id : undefined)))
|
||||
_req.method = req.method
|
||||
// req.originalUrl is for expressjs compat.
|
||||
if (req.originalUrl) {
|
||||
_req.url = req.originalUrl
|
||||
} else {
|
||||
const path = req.path
|
||||
// path for safe hapi compat.
|
||||
_req.url = typeof path === 'string' ? path : (req.url ? req.url.path || req.url : undefined)
|
||||
}
|
||||
|
||||
if (req.query) {
|
||||
_req.query = req.query
|
||||
}
|
||||
|
||||
if (req.params) {
|
||||
_req.params = req.params
|
||||
}
|
||||
|
||||
_req.headers = req.headers
|
||||
_req.remoteAddress = connection && connection.remoteAddress
|
||||
_req.remotePort = connection && connection.remotePort
|
||||
// req.raw is for hapi compat/equivalence
|
||||
_req.raw = req.raw || req
|
||||
return _req
|
||||
}
|
||||
|
||||
function mapHttpRequest (req) {
|
||||
return {
|
||||
req: reqSerializer(req)
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
mapHttpResponse,
|
||||
resSerializer
|
||||
}
|
||||
|
||||
const rawSymbol = Symbol('pino-raw-res-ref')
|
||||
const pinoResProto = Object.create({}, {
|
||||
statusCode: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: 0
|
||||
},
|
||||
headers: {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: ''
|
||||
},
|
||||
raw: {
|
||||
enumerable: false,
|
||||
get: function () {
|
||||
return this[rawSymbol]
|
||||
},
|
||||
set: function (val) {
|
||||
this[rawSymbol] = val
|
||||
}
|
||||
}
|
||||
})
|
||||
Object.defineProperty(pinoResProto, rawSymbol, {
|
||||
writable: true,
|
||||
value: {}
|
||||
})
|
||||
|
||||
function resSerializer (res) {
|
||||
const _res = Object.create(pinoResProto)
|
||||
_res.statusCode = res.headersSent ? res.statusCode : null
|
||||
_res.headers = res.getHeaders ? res.getHeaders() : res._headers
|
||||
_res.raw = res
|
||||
return _res
|
||||
}
|
||||
|
||||
function mapHttpResponse (res) {
|
||||
return {
|
||||
res: resSerializer(res)
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"name": "pino-std-serializers",
|
||||
"version": "7.0.0",
|
||||
"description": "A collection of standard object serializers for Pino",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
"types": "index.d.ts",
|
||||
"scripts": {
|
||||
"lint": "standard | snazzy",
|
||||
"lint-ci": "standard",
|
||||
"test": "borp -p 'test/**/*.js'",
|
||||
"test-ci": "borp --coverage -p 'test/**/*.js'",
|
||||
"test-types": "tsc && tsd"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@github.com/pinojs/pino-std-serializers.git"
|
||||
},
|
||||
"keywords": [
|
||||
"pino",
|
||||
"logging"
|
||||
],
|
||||
"author": "James Sumners <james.sumners@gmail.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pinojs/pino-std-serializers/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pinojs/pino-std-serializers#readme",
|
||||
"precommit": [
|
||||
"lint",
|
||||
"test",
|
||||
"test-types"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@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.1.0",
|
||||
"tsd": "^0.31.0",
|
||||
"typescript": "^5.3.3"
|
||||
},
|
||||
"tsd": {
|
||||
"directory": "test/types"
|
||||
}
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const serializer = require('../lib/err-with-cause')
|
||||
const { wrapErrorSerializer } = require('../')
|
||||
|
||||
test('serializes Error objects', () => {
|
||||
const serialized = serializer(Error('foo'))
|
||||
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', () => {
|
||||
const err = Error('foo')
|
||||
err.statusCode = 500
|
||||
const serialized = serializer(err)
|
||||
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"', () => {
|
||||
class MyError extends Error {}
|
||||
|
||||
const err = new MyError('foo')
|
||||
const serialized = serializer(err)
|
||||
assert.strictEqual(serialized.type, 'MyError')
|
||||
})
|
||||
|
||||
test('serializes nested errors', () => {
|
||||
const err = Error('foo')
|
||||
err.inner = Error('bar')
|
||||
const serialized = serializer(err)
|
||||
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', () => {
|
||||
const innerErr = Error('inner')
|
||||
const middleErr = Error('middle')
|
||||
middleErr.cause = innerErr
|
||||
const outerErr = Error('outer')
|
||||
outerErr.cause = middleErr
|
||||
|
||||
const serialized = serializer(outerErr)
|
||||
|
||||
assert.strictEqual(serialized.type, 'Error')
|
||||
assert.strictEqual(serialized.message, 'outer')
|
||||
assert.match(serialized.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:/)
|
||||
|
||||
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', () => {
|
||||
const err = Error('foo')
|
||||
err.cause = 'abc'
|
||||
const serialized = serializer(err)
|
||||
assert.strictEqual(serialized.type, 'Error')
|
||||
assert.strictEqual(serialized.message, 'foo')
|
||||
assert.strictEqual(serialized.cause, 'abc')
|
||||
})
|
||||
|
||||
test('prevents infinite recursion', () => {
|
||||
const err = Error('foo')
|
||||
err.inner = err
|
||||
const serialized = serializer(err)
|
||||
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', () => {
|
||||
const err = Error('foo')
|
||||
const bar = Error('bar')
|
||||
err.inner = bar
|
||||
bar.inner = err
|
||||
|
||||
serializer(err)
|
||||
const serialized = serializer(err)
|
||||
|
||||
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', () => {
|
||||
const err = Error('foo')
|
||||
const serialized = serializer(err)
|
||||
assert.strictEqual(serialized.raw, err)
|
||||
})
|
||||
|
||||
test('redefined err.constructor doesnt crash serializer', () => {
|
||||
function check (a, name) {
|
||||
assert.strictEqual(a.type, name)
|
||||
assert.strictEqual(a.message, 'foo')
|
||||
}
|
||||
|
||||
const err1 = TypeError('foo')
|
||||
err1.constructor = '10'
|
||||
|
||||
const err2 = TypeError('foo')
|
||||
err2.constructor = undefined
|
||||
|
||||
const err3 = Error('foo')
|
||||
err3.constructor = null
|
||||
|
||||
const err4 = Error('foo')
|
||||
err4.constructor = 10
|
||||
|
||||
class MyError extends Error {}
|
||||
|
||||
const err5 = new MyError('foo')
|
||||
err5.constructor = undefined
|
||||
|
||||
check(serializer(err1), 'TypeError')
|
||||
check(serializer(err2), 'TypeError')
|
||||
check(serializer(err3), 'Error')
|
||||
check(serializer(err4), 'Error')
|
||||
// We do not expect 'MyError' because err5.constructor has been blown away.
|
||||
// `err5.name` is 'Error' from the base class prototype.
|
||||
check(serializer(err5), 'Error')
|
||||
})
|
||||
|
||||
test('pass through anything that does not look like an Error', () => {
|
||||
function check (a) {
|
||||
assert.strictEqual(serializer(a), a)
|
||||
}
|
||||
|
||||
check('foo')
|
||||
check({ hello: 'world' })
|
||||
check([1, 2])
|
||||
})
|
||||
|
||||
test('can wrap err serializers', () => {
|
||||
const err = Error('foo')
|
||||
err.foo = 'foo'
|
||||
const serializer = wrapErrorSerializer(function (err) {
|
||||
delete err.foo
|
||||
err.bar = 'bar'
|
||||
return err
|
||||
})
|
||||
const serialized = serializer(err)
|
||||
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 }, () => {
|
||||
const foo = new Error('foo')
|
||||
const bar = new Error('bar')
|
||||
for (const aggregate of [
|
||||
new AggregateError([foo, bar], 'aggregated message'), // eslint-disable-line no-undef
|
||||
{ errors: [foo, bar], message: 'aggregated message', stack: 'err-with-cause.test.js:' }
|
||||
]) {
|
||||
const serialized = serializer(aggregate)
|
||||
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:/)
|
||||
}
|
||||
})
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('node:assert')
|
||||
const { test } = require('node:test')
|
||||
const serializer = require('../lib/err')
|
||||
const { wrapErrorSerializer } = require('../')
|
||||
|
||||
test('serializes Error objects', () => {
|
||||
const serialized = serializer(Error('foo'))
|
||||
assert.strictEqual(serialized.type, 'Error')
|
||||
assert.strictEqual(serialized.message, 'foo')
|
||||
assert.match(serialized.stack, /err\.test\.js:/)
|
||||
})
|
||||
|
||||
test('serializes Error objects with extra properties', () => {
|
||||
const err = Error('foo')
|
||||
err.statusCode = 500
|
||||
const serialized = serializer(err)
|
||||
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"', () => {
|
||||
class MyError extends Error {}
|
||||
const err = new MyError('foo')
|
||||
const serialized = serializer(err)
|
||||
assert.strictEqual(serialized.type, 'MyError')
|
||||
})
|
||||
|
||||
test('serializes nested errors', () => {
|
||||
const err = Error('foo')
|
||||
err.inner = Error('bar')
|
||||
const serialized = serializer(err)
|
||||
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', () => {
|
||||
for (const cause of [
|
||||
Error('bar'),
|
||||
{ message: 'bar', stack: 'Error: bar: err.test.js:' }
|
||||
]) {
|
||||
const err = Error('foo')
|
||||
err.cause = cause
|
||||
err.cause.cause = Error('abc')
|
||||
const serialized = serializer(err)
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
test('serializes error causes with VError support', function (t) {
|
||||
// Fake VError-style setup
|
||||
const err = Error('foo: bar')
|
||||
err.foo = 'abc'
|
||||
err.cause = function () {
|
||||
const err = Error('bar')
|
||||
err.cause = Error(this.foo)
|
||||
return err
|
||||
}
|
||||
const serialized = serializer(err)
|
||||
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', () => {
|
||||
const err = Error('foo')
|
||||
err.cause = 'abc'
|
||||
const serialized = serializer(err)
|
||||
assert.strictEqual(serialized.type, 'Error')
|
||||
assert.strictEqual(serialized.message, 'foo')
|
||||
assert.strictEqual(serialized.cause, 'abc')
|
||||
})
|
||||
|
||||
test('prevents infinite recursion', () => {
|
||||
const err = Error('foo')
|
||||
err.inner = err
|
||||
const serialized = serializer(err)
|
||||
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', () => {
|
||||
const err = Error('foo')
|
||||
const bar = Error('bar')
|
||||
err.inner = bar
|
||||
bar.inner = err
|
||||
|
||||
serializer(err)
|
||||
const serialized = serializer(err)
|
||||
|
||||
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', () => {
|
||||
const err = Error('foo')
|
||||
const serialized = serializer(err)
|
||||
assert.strictEqual(serialized.raw, err)
|
||||
})
|
||||
|
||||
test('redefined err.constructor doesnt crash serializer', () => {
|
||||
function check (a, name) {
|
||||
assert.strictEqual(a.type, name)
|
||||
assert.strictEqual(a.message, 'foo')
|
||||
}
|
||||
|
||||
const err1 = TypeError('foo')
|
||||
err1.constructor = '10'
|
||||
|
||||
const err2 = TypeError('foo')
|
||||
err2.constructor = undefined
|
||||
|
||||
const err3 = Error('foo')
|
||||
err3.constructor = null
|
||||
|
||||
const err4 = Error('foo')
|
||||
err4.constructor = 10
|
||||
|
||||
class MyError extends Error {}
|
||||
const err5 = new MyError('foo')
|
||||
err5.constructor = undefined
|
||||
|
||||
check(serializer(err1), 'TypeError')
|
||||
check(serializer(err2), 'TypeError')
|
||||
check(serializer(err3), 'Error')
|
||||
check(serializer(err4), 'Error')
|
||||
// We do not expect 'MyError' because err5.constructor has been blown away.
|
||||
// `err5.name` is 'Error' from the base class prototype.
|
||||
check(serializer(err5), 'Error')
|
||||
})
|
||||
|
||||
test('pass through anything that does not look like an Error', () => {
|
||||
function check (a) {
|
||||
assert.strictEqual(serializer(a), a)
|
||||
}
|
||||
|
||||
check('foo')
|
||||
check({ hello: 'world' })
|
||||
check([1, 2])
|
||||
})
|
||||
|
||||
test('can wrap err serializers', () => {
|
||||
const err = Error('foo')
|
||||
err.foo = 'foo'
|
||||
const serializer = wrapErrorSerializer(function (err) {
|
||||
delete err.foo
|
||||
err.bar = 'bar'
|
||||
return err
|
||||
})
|
||||
const serialized = serializer(err)
|
||||
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 }, () => {
|
||||
const foo = new Error('foo')
|
||||
const bar = new Error('bar')
|
||||
for (const aggregate of [
|
||||
new AggregateError([foo, bar], 'aggregated message'), // eslint-disable-line no-undef
|
||||
{ errors: [foo, bar], message: 'aggregated message', stack: 'err.test.js:' }
|
||||
]) {
|
||||
const serialized = serializer(aggregate)
|
||||
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:/)
|
||||
}
|
||||
})
|
||||
+477
@@ -0,0 +1,477 @@
|
||||
'use strict'
|
||||
|
||||
const { tspl } = require('@matteo.collina/tspl')
|
||||
const http = require('node:http')
|
||||
const { test } = require('node:test')
|
||||
const serializers = require('../lib/req')
|
||||
const { wrapRequestSerializer } = require('../')
|
||||
|
||||
test('maps request', async (t) => {
|
||||
const p = tspl(t, { plan: 2 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
const serialized = serializers.mapHttpRequest(req)
|
||||
p.ok(serialized.req)
|
||||
p.ok(serialized.req.method)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('does not return excessively long object', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(Object.keys(serialized).length, 6)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.raw is available', async (t) => {
|
||||
const p = tspl(t, { plan: 2 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.foo = 'foo'
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
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', async (t) => {
|
||||
const p = tspl(t, { plan: 2 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.raw = { req: { foo: 'foo' }, res: {} }
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.ok(serialized.raw)
|
||||
p.strictEqual(serialized.raw.req.foo, 'foo')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.id defaults to undefined', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.id, undefined)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.id has a non-function value', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
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', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.info = { id: 'test' }
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.id, 'test')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.id has a non-function value with custom id function', async (t) => {
|
||||
const p = tspl(t, { plan: 2 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.id = function () { return 42 }
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
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', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.path = '/test'
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
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', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.url = { path: '/test' }
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
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', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.url = '/test'
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
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', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.url, '/')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.url will be obtained from input request originalUrl when available', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.originalUrl = '/test'
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
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', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.path = function () {
|
||||
throw new Error('unexpected invocation')
|
||||
}
|
||||
req.url = '/test'
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.url, '/test')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.url being undefined does not throw an error', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.url = undefined
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.url, undefined)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('can wrap request serializers', async (t) => {
|
||||
const p = tspl(t, { plan: 3 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
const serailizer = wrapRequestSerializer(function (req) {
|
||||
p.ok(req.method)
|
||||
p.strictEqual(req.method, 'GET')
|
||||
delete req.method
|
||||
return req
|
||||
})
|
||||
|
||||
function handler (req, res) {
|
||||
const serialized = serailizer(req)
|
||||
p.ok(!serialized.method)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.remoteAddress will be obtained from request socket.remoteAddress as fallback', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.socket = { remoteAddress: 'http://localhost' }
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.remoteAddress, 'http://localhost')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.remoteAddress will be obtained from request info.remoteAddress if available', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.info = { remoteAddress: 'http://localhost' }
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.remoteAddress, 'http://localhost')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.remotePort will be obtained from request socket.remotePort as fallback', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.socket = { remotePort: 3000 }
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.remotePort, 3000)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.remotePort will be obtained from request info.remotePort if available', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.info = { remotePort: 3000 }
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.remotePort, 3000)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.query is available', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.query = '/foo?bar=foobar&bar=foo'
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.query, '/foo?bar=foobar&bar=foo')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('req.params is available', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (req, res) {
|
||||
req.params = '/foo/bar'
|
||||
const serialized = serializers.reqSerializer(req)
|
||||
p.strictEqual(serialized.params, '/foo/bar')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint-disable no-prototype-builtins */
|
||||
|
||||
const { tspl } = require('@matteo.collina/tspl')
|
||||
const http = require('node:http')
|
||||
const { test } = require('node:test')
|
||||
const serializers = require('../lib/res')
|
||||
const { wrapResponseSerializer } = require('../')
|
||||
|
||||
test('res.raw is not enumerable', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (_req, res) {
|
||||
const serialized = serializers.resSerializer(res)
|
||||
p.strictEqual(serialized.propertyIsEnumerable('raw'), false)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('res.raw is available', async (t) => {
|
||||
const p = tspl(t, { plan: 2 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (_req, res) {
|
||||
res.statusCode = 200
|
||||
const serialized = serializers.resSerializer(res)
|
||||
p.ok(serialized.raw)
|
||||
p.strictEqual(serialized.raw.statusCode, 200)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('can wrap response serializers', async (t) => {
|
||||
const p = tspl(t, { plan: 3 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
const serializer = wrapResponseSerializer(function (res) {
|
||||
p.ok(res.statusCode)
|
||||
p.strictEqual(res.statusCode, 200)
|
||||
delete res.statusCode
|
||||
return res
|
||||
})
|
||||
|
||||
function handler (_req, res) {
|
||||
res.end()
|
||||
res.statusCode = 200
|
||||
const serialized = serializer(res)
|
||||
p.ok(!serialized.statusCode)
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
test('res.headers is serialized', async (t) => {
|
||||
const p = tspl(t, { plan: 1 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (_req, res) {
|
||||
res.setHeader('x-custom', 'y')
|
||||
const serialized = serializers.resSerializer(res)
|
||||
p.strictEqual(serialized.headers['x-custom'], 'y')
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
|
||||
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 })
|
||||
|
||||
const server = http.createServer(handler)
|
||||
server.unref()
|
||||
server.listen(0, () => {
|
||||
http.get(server.address(), () => {})
|
||||
})
|
||||
|
||||
t.after(() => server.close())
|
||||
|
||||
function handler (_req, res) {
|
||||
const serialized = serializers.resSerializer(res)
|
||||
p.strictEqual(serialized.statusCode, null)
|
||||
res.end()
|
||||
}
|
||||
|
||||
await p.completed
|
||||
})
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
import {IncomingMessage, ServerResponse} from "http";
|
||||
import {
|
||||
err,
|
||||
errWithCause,
|
||||
req,
|
||||
res,
|
||||
SerializedError,
|
||||
SerializedRequest,
|
||||
wrapErrorSerializer,
|
||||
wrapRequestSerializer,
|
||||
wrapResponseSerializer,
|
||||
SerializedResponse
|
||||
} from '../../';
|
||||
|
||||
const customErrorSerializer = (error: SerializedError) => {
|
||||
return {
|
||||
myOwnError: {
|
||||
data: `${error.type}-${error.message}\n\n${error.stack}`,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const customRequestSerializer = (req: SerializedRequest) => {
|
||||
const {
|
||||
headers,
|
||||
id,
|
||||
method,
|
||||
raw,
|
||||
remoteAddress,
|
||||
remotePort,
|
||||
url,
|
||||
query,
|
||||
params,
|
||||
} = req;
|
||||
return {
|
||||
myOwnRequest: {
|
||||
data: `${method}-${id}-${remoteAddress}-${remotePort}-${url}`,
|
||||
headers,
|
||||
raw,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const customResponseSerializer = (res: SerializedResponse) => {
|
||||
const {headers, raw, statusCode} = res;
|
||||
return {
|
||||
myOwnResponse: {
|
||||
data: statusCode,
|
||||
headers,
|
||||
raw,
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const fakeError = new Error('A fake error for testing');
|
||||
const serializedError: SerializedError = err(fakeError);
|
||||
const mySerializer = wrapErrorSerializer(customErrorSerializer);
|
||||
|
||||
const fakeErrorWithCause = new Error('A fake error for testing with cause', { cause: new Error('An inner fake error') });
|
||||
const serializedErrorWithCause: SerializedError = errWithCause(fakeError);
|
||||
|
||||
const request: IncomingMessage = {} as IncomingMessage
|
||||
const serializedRequest: SerializedRequest = req(request);
|
||||
const myReqSerializer = wrapRequestSerializer(customRequestSerializer);
|
||||
|
||||
const response: ServerResponse = {} as ServerResponse
|
||||
const myResSerializer = wrapResponseSerializer(customResponseSerializer);
|
||||
const serializedResponse = res(response);
|
||||
|
||||
myResSerializer(response)
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"lib": [ "es2022" ],
|
||||
"module": "commonjs",
|
||||
"noEmit": true,
|
||||
"strict": true
|
||||
},
|
||||
"include": [
|
||||
"./test/types/*.test-d.ts",
|
||||
"./index.d.ts"
|
||||
]
|
||||
}
|
||||
Whitespace-only changes.
+1
@@ -0,0 +1 @@
|
||||
*
|
||||
+1
@@ -0,0 +1 @@
|
||||
getpino.io
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
# Pino is an OPEN Open Source Project
|
||||
|
||||
## What?
|
||||
|
||||
Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
|
||||
|
||||
## Rules
|
||||
|
||||
Before you start coding, please read [Contributing to projects with git](https://jrfom.com/posts/2017/03/08/a-primer-on-contributing-to-projects-with-git/).
|
||||
|
||||
Notice that as long as you don't have commit-access to the project, you have to fork the project and open PRs from the feature branches of the forked project.
|
||||
|
||||
There are a few basic ground-rules for contributors:
|
||||
|
||||
1. **No `--force` pushes** on `master` or modifying the Git history in any way after a PR has been merged.
|
||||
1. **Non-master branches** ought to be used for ongoing work.
|
||||
1. **Non-trivial changes** ought to be subject to an **internal pull-request** to solicit feedback from other contributors.
|
||||
1. All pull-requests for new features **must** target the `master` branch. PRs to fix bugs in LTS releases are also allowed.
|
||||
1. Contributors should attempt to adhere to the prevailing code-style.
|
||||
1. 100% code coverage
|
||||
|
||||
## Releases
|
||||
|
||||
Declaring formal releases remains the prerogative of the project maintainer.
|
||||
|
||||
## Changes to this arrangement
|
||||
|
||||
This is an experiment and feedback is welcome! This document may also be subject to pull-requests or changes by contributors where you believe you have something valuable to add or change.
|
||||
|
||||
-----------------------------------------
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016-2025 Matteo Collina, David Mark Clements and the Pino contributors listed at <https://github.com/pinojs/pino#the-team> and in the README file.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+177
@@ -0,0 +1,177 @@
|
||||

|
||||
|
||||
# pino
|
||||
[](https://www.npmjs.com/package/pino)
|
||||
[](https://github.com/pinojs/pino/actions)
|
||||
[](https://standardjs.com/)
|
||||
|
||||
[Very low overhead](#low-overhead) JavaScript logger.
|
||||
|
||||
## Documentation
|
||||
|
||||
* [Benchmarks ⇗](/docs/benchmarks.md)
|
||||
* [API ⇗](/docs/api.md)
|
||||
* [Browser API ⇗](/docs/browser.md)
|
||||
* [Redaction ⇗](/docs/redaction.md)
|
||||
* [Child Loggers ⇗](/docs/child-loggers.md)
|
||||
* [Transports ⇗](/docs/transports.md)
|
||||
* [Diagnostics ⇗](/docs/diagnostics.md)
|
||||
* [Web Frameworks ⇗](/docs/web.md)
|
||||
* [Pretty Printing ⇗](/docs/pretty.md)
|
||||
* [Asynchronous Logging ⇗](/docs/asynchronous.md)
|
||||
* [Ecosystem ⇗](/docs/ecosystem.md)
|
||||
* [Help ⇗](/docs/help.md)
|
||||
* [Long Term Support Policy ⇗](/docs/lts.md)
|
||||
|
||||
## Runtimes
|
||||
|
||||
### Node.js
|
||||
|
||||
Pino is built to run on [Node.js](http://nodejs.org).
|
||||
|
||||
### Bare
|
||||
|
||||
Pino works on [Bare](https://github.com/holepunchto/bare) with the [`pino-bare`](https://github.com/pinojs/pino-bare) compatability module.
|
||||
|
||||
### Pear
|
||||
|
||||
Pino works on [Pear](https://docs.pears.com), which is built on [Bare](https://github.com/holepunchto/bare), with the [`pino-bare`](https://github.com/pinojs/pino-bare) compatibility module.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
Using NPM:
|
||||
```
|
||||
$ npm install pino
|
||||
```
|
||||
|
||||
Using YARN:
|
||||
```
|
||||
$ yarn add pino
|
||||
```
|
||||
|
||||
If you would like to install pino v6, refer to https://github.com/pinojs/pino/tree/v6.x.
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const logger = require('pino')()
|
||||
|
||||
logger.info('hello world')
|
||||
|
||||
const child = logger.child({ a: 'property' })
|
||||
child.info('hello child!')
|
||||
```
|
||||
|
||||
This produces:
|
||||
|
||||
```
|
||||
{"level":30,"time":1531171074631,"msg":"hello world","pid":657,"hostname":"Davids-MBP-3.fritz.box"}
|
||||
{"level":30,"time":1531171082399,"msg":"hello child!","pid":657,"hostname":"Davids-MBP-3.fritz.box","a":"property"}
|
||||
```
|
||||
|
||||
For using Pino with a web framework see:
|
||||
|
||||
* [Pino with Fastify](docs/web.md#fastify)
|
||||
* [Pino with Express](docs/web.md#express)
|
||||
* [Pino with Hapi](docs/web.md#hapi)
|
||||
* [Pino with Restify](docs/web.md#restify)
|
||||
* [Pino with Koa](docs/web.md#koa)
|
||||
* [Pino with Node core `http`](docs/web.md#http)
|
||||
* [Pino with Nest](docs/web.md#nest)
|
||||
* [Pino with Hono](docs/web.md#hono)
|
||||
|
||||
<a name="essentials"></a>
|
||||
## Essentials
|
||||
|
||||
### Development Formatting
|
||||
|
||||
The [`pino-pretty`](https://github.com/pinojs/pino-pretty) module can be used to
|
||||
format logs during development:
|
||||
|
||||

|
||||
|
||||
### Transports & Log Processing
|
||||
|
||||
Due to Node's single-threaded event-loop, it's highly recommended that sending,
|
||||
alert triggering, reformatting, and all forms of log processing
|
||||
are conducted in a separate process or thread.
|
||||
|
||||
In Pino terminology, we call all log processors "transports" and recommend that the
|
||||
transports be run in a worker thread using our `pino.transport` API.
|
||||
|
||||
For more details see our [Transports⇗](docs/transports.md) document.
|
||||
|
||||
### Low overhead
|
||||
|
||||
Using minimum resources for logging is very important. Log messages
|
||||
tend to get added over time and this can lead to a throttling effect
|
||||
on applications – such as reduced requests per second.
|
||||
|
||||
In many cases, Pino is over 5x faster than alternatives.
|
||||
|
||||
See the [Benchmarks](docs/benchmarks.md) document for comparisons.
|
||||
|
||||
### Bundling support
|
||||
|
||||
Pino supports being bundled using tools like webpack or esbuild.
|
||||
|
||||
See [Bundling](docs/bundling.md) document for more information.
|
||||
|
||||
<a name="team"></a>
|
||||
## The Team
|
||||
|
||||
### Matteo Collina
|
||||
|
||||
<https://github.com/mcollina>
|
||||
|
||||
<https://www.npmjs.com/~matteo.collina>
|
||||
|
||||
<https://twitter.com/matteocollina>
|
||||
|
||||
### David Mark Clements
|
||||
|
||||
<https://github.com/davidmarkclements>
|
||||
|
||||
<https://www.npmjs.com/~davidmarkclements>
|
||||
|
||||
<https://twitter.com/davidmarkclem>
|
||||
|
||||
### James Sumners
|
||||
|
||||
<https://github.com/jsumners>
|
||||
|
||||
<https://www.npmjs.com/~jsumners>
|
||||
|
||||
<https://twitter.com/jsumners79>
|
||||
|
||||
### Thomas Watson Steen
|
||||
|
||||
<https://github.com/watson>
|
||||
|
||||
<https://www.npmjs.com/~watson>
|
||||
|
||||
<https://twitter.com/wa7son>
|
||||
|
||||
## Contributing
|
||||
|
||||
Pino is an **OPEN Open Source Project**. This means that:
|
||||
|
||||
> Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
|
||||
|
||||
See the [CONTRIBUTING.md](https://github.com/pinojs/pino/blob/main/CONTRIBUTING.md) file for more details.
|
||||
|
||||
<a name="acknowledgments"></a>
|
||||
## Acknowledgments
|
||||
|
||||
This project was kindly sponsored by [nearForm](https://nearform.com).
|
||||
This project is kindly sponsored by [Platformatic](https://platformatic.dev).
|
||||
|
||||
Logo and identity designed by Cosmic Fox Design: https://www.behance.net/cosmicfox.
|
||||
|
||||
## License
|
||||
|
||||
Licensed under [MIT](./LICENSE).
|
||||
|
||||
[elasticsearch]: https://www.elastic.co/products/elasticsearch
|
||||
[kibana]: https://www.elastic.co/products/kibana
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
# Security Policy
|
||||
|
||||
This document describes the management of vulnerabilities for the
|
||||
Pino project and all modules within the Pino organization.
|
||||
|
||||
## Reporting vulnerabilities
|
||||
|
||||
Individuals who find potential vulnerabilities in Pino are invited
|
||||
to report them via email at matteo.collina@gmail.com.
|
||||
|
||||
### Strict measures when reporting vulnerabilities
|
||||
|
||||
Avoid creating new "informative" reports. Only create new
|
||||
report a potential vulnerability if you are absolutely sure this
|
||||
should be tagged as an actual vulnerability. Be careful on the maintainers time.
|
||||
|
||||
## Handling vulnerability reports
|
||||
|
||||
When a potential vulnerability is reported, the following actions are taken:
|
||||
|
||||
### Triage
|
||||
|
||||
**Delay:** 5 business days
|
||||
|
||||
Within 5 business days, a member of the security team provides a first answer to the
|
||||
individual who submitted the potential vulnerability. The possible responses
|
||||
can be:
|
||||
|
||||
* Acceptance: what was reported is considered as a new vulnerability
|
||||
* Rejection: what was reported is not considered as a new vulnerability
|
||||
* Need more information: the security team needs more information in order to evaluate what was reported.
|
||||
|
||||
Triaging should include updating issue fields:
|
||||
* Asset - set/create the module affected by the report
|
||||
* Severity - TBD, currently left empty
|
||||
|
||||
### Correction follow-up
|
||||
|
||||
**Delay:** 90 days
|
||||
|
||||
When a vulnerability is confirmed, a member of the security team volunteers to follow
|
||||
up on this report.
|
||||
|
||||
With the help of the individual who reported the vulnerability, they contact
|
||||
the maintainers of the vulnerable package to make them aware of the
|
||||
vulnerability. The maintainers can be invited as participants to the reported issue.
|
||||
|
||||
With the package maintainer, they define a release date for the publication
|
||||
of the vulnerability. Ideally, this release date should not happen before
|
||||
the package has been patched.
|
||||
|
||||
The report's vulnerable versions upper limit should be set to:
|
||||
* `*` if there is no fixed version available by the time of publishing the report.
|
||||
* the last vulnerable version. For example: `<=1.2.3` if a fix exists in `1.2.4`
|
||||
|
||||
### Publication
|
||||
|
||||
**Delay:** 90 days
|
||||
|
||||
Within 90 days after the triage date, the vulnerability must be made public.
|
||||
|
||||
**Severity**: Vulnerability severity is assessed using [CVSS v.3](https://www.first.org/cvss/user-guide).
|
||||
|
||||
If the package maintainer is actively developing a patch, an additional delay
|
||||
can be added with the approval of the security team and the individual who
|
||||
reported the vulnerability.
|
||||
|
||||
At this point, a CVE will be requested by the team.
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
const bunyan = require('bunyan')
|
||||
const bole = require('bole')('bench')
|
||||
const winston = require('winston')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const loglevel = require('./utils/wrap-log-level')(dest)
|
||||
const plogNodeStream = pino(dest)
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogMinLength = require('../')(pino.destination({ dest: '/dev/null', minLength: 4096 }))
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogDest = require('../')(pino.destination('/dev/null'))
|
||||
|
||||
process.env.DEBUG = 'dlog'
|
||||
const debug = require('debug')
|
||||
const dlog = debug('dlog')
|
||||
dlog.log = function (s) { dest.write(s) }
|
||||
|
||||
const max = 10
|
||||
const blog = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{
|
||||
level: 'trace',
|
||||
stream: dest
|
||||
}]
|
||||
})
|
||||
|
||||
require('bole').output({
|
||||
level: 'info',
|
||||
stream: dest
|
||||
}).setFastTime(true)
|
||||
|
||||
const chill = winston.createLogger({
|
||||
transports: [
|
||||
new winston.transports.Stream({
|
||||
stream: fs.createWriteStream('/dev/null')
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
const run = bench([
|
||||
function benchBunyan (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchWinston (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
chill.log('info', 'hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBole (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchDebug (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
dlog('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchLogLevel (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
loglevel.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPino (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLength (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStream (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
const bunyan = require('bunyan')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plogNodeStream = pino(dest).child({ a: 'property' }).child({ sub: 'child' })
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogDest = require('../')(pino.destination('/dev/null')).child({ a: 'property' }).child({ sub: 'child' })
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogMinLength = require('../')(pino.destination({ dest: '/dev/null', sync: false, minLength: 4096 }))
|
||||
.child({ a: 'property' })
|
||||
.child({ sub: 'child' })
|
||||
|
||||
const max = 10
|
||||
const blog = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{
|
||||
level: 'trace',
|
||||
stream: dest
|
||||
}]
|
||||
}).child({ a: 'property' }).child({ sub: 'child' })
|
||||
|
||||
const run = bench([
|
||||
function benchBunyanChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
const bunyan = require('bunyan')
|
||||
const bole = require('bole')('bench')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plogNodeStream = pino(dest)
|
||||
const plogDest = pino(pino.destination(('/dev/null')))
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogMinLength = require('../')(pino.destination({ dest: '/dev/null', sync: false, minLength: 4096 }))
|
||||
|
||||
const max = 10
|
||||
const blog = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{
|
||||
level: 'trace',
|
||||
stream: dest
|
||||
}]
|
||||
})
|
||||
|
||||
require('bole').output({
|
||||
level: 'info',
|
||||
stream: dest
|
||||
}).setFastTime(true)
|
||||
|
||||
const run = bench([
|
||||
function benchBunyanCreation (cb) {
|
||||
const child = blog.child({ a: 'property' })
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBoleCreation (cb) {
|
||||
const child = bole('child')
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoCreation (cb) {
|
||||
const child = plogDest.child({ a: 'property' })
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthCreation (cb) {
|
||||
const child = plogMinLength.child({ a: 'property' })
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamCreation (cb) {
|
||||
const child = plogNodeStream.child({ a: 'property' })
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoCreationWithOption (cb) {
|
||||
const child = plogDest.child({ a: 'property' }, { redact: [] })
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
const bunyan = require('bunyan')
|
||||
const bole = require('bole')('bench')('child')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plogNodeStream = pino(dest).child({ a: 'property' })
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogDest = require('../')(pino.destination('/dev/null')).child({ a: 'property' })
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogMinLength = require('../')(pino.destination({ dest: '/dev/null', sync: false, minLength: 4096 }))
|
||||
|
||||
const max = 10
|
||||
const blog = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{
|
||||
level: 'trace',
|
||||
stream: dest
|
||||
}]
|
||||
}).child({ a: 'property' })
|
||||
|
||||
require('bole').output({
|
||||
level: 'info',
|
||||
stream: dest
|
||||
}).setFastTime(true)
|
||||
|
||||
const run = bench([
|
||||
function benchBunyanChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBoleChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
const bunyan = require('bunyan')
|
||||
const bole = require('bole')('bench')
|
||||
const winston = require('winston')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plogNodeStream = pino(dest)
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogDest = require('../')(pino.destination('/dev/null'))
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogMinLength = require('../')(pino.destination({ dest: '/dev/null', sync: false, minLength: 4096 }))
|
||||
delete require.cache[require.resolve('../')]
|
||||
|
||||
const loglevel = require('./utils/wrap-log-level')(dest)
|
||||
|
||||
const deep = Object.assign({}, require('../package.json'), { level: 'info' })
|
||||
|
||||
const max = 10
|
||||
const blog = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{
|
||||
level: 'trace',
|
||||
stream: dest
|
||||
}]
|
||||
})
|
||||
|
||||
require('bole').output({
|
||||
level: 'info',
|
||||
stream: dest
|
||||
}).setFastTime(true)
|
||||
|
||||
const chill = winston.createLogger({
|
||||
transports: [
|
||||
new winston.transports.Stream({
|
||||
stream: fs.createWriteStream('/dev/null')
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
const run = bench([
|
||||
function benchBunyanDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchWinstonDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
chill.log(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBoleDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchLogLevelDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
loglevel.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
'use strict'
|
||||
|
||||
const formatters = {
|
||||
level (label, number) {
|
||||
return {
|
||||
log: {
|
||||
level: label
|
||||
}
|
||||
}
|
||||
},
|
||||
bindings (bindings) {
|
||||
return {
|
||||
process: {
|
||||
pid: bindings.pid
|
||||
},
|
||||
host: {
|
||||
name: bindings.hostname
|
||||
}
|
||||
}
|
||||
},
|
||||
log (obj) {
|
||||
return { foo: 'bar', ...obj }
|
||||
}
|
||||
}
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
delete require.cache[require.resolve('../')]
|
||||
const pinoNoFormatters = require('../')(pino.destination('/dev/null'))
|
||||
delete require.cache[require.resolve('../')]
|
||||
const pinoFormatters = require('../')({ formatters }, pino.destination('/dev/null'))
|
||||
|
||||
const max = 10
|
||||
|
||||
const run = bench([
|
||||
function benchPinoNoFormatters (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
pinoNoFormatters.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoFormatters (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
pinoFormatters.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../../')
|
||||
|
||||
const base = pino(pino.destination('/dev/null'))
|
||||
const baseCl = pino({
|
||||
customLevels: { foo: 31 }
|
||||
}, pino.destination('/dev/null'))
|
||||
const child = base.child({})
|
||||
const childCl = base.child({
|
||||
customLevels: { foo: 31 }
|
||||
})
|
||||
const childOfBaseCl = baseCl.child({})
|
||||
|
||||
const max = 100
|
||||
|
||||
const run = bench([
|
||||
function benchPinoNoCustomLevel (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
base.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoCustomLevel (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
baseCl.foo({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchChildNoCustomLevel (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildCustomLevel (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
childCl.foo({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildInheritedCustomLevel (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
childOfBaseCl.foo({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildCreation (cb) {
|
||||
const child = base.child({})
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildCreationCustomLevel (cb) {
|
||||
const child = base.child({
|
||||
customLevels: { foo: 31 }
|
||||
})
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.foo({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../../')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plog = pino(dest)
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogDest = require('../../')(pino.destination('/dev/null'))
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogAsync = require('../../')(pino.destination({ dest: '/dev/null', sync: false }))
|
||||
const deep = require('../../package.json')
|
||||
deep.deep = JSON.parse(JSON.stringify(deep))
|
||||
deep.deep.deep = JSON.parse(JSON.stringify(deep))
|
||||
const longStr = JSON.stringify(deep)
|
||||
|
||||
const max = 10
|
||||
|
||||
const run = bench([
|
||||
function benchPinoLongString (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestLongString (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncLongString (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncDeepObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info(deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info('hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info('hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 1000)
|
||||
|
||||
run(run)
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../../')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plog = pino(dest)
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogDest = require('../../')(pino.destination('/dev/null'))
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogAsync = require('../../')(pino.destination({ dest: '/dev/null', sync: false }))
|
||||
const plogChild = plog.child({ a: 'property' })
|
||||
const plogDestChild = plogDest.child({ a: 'property' })
|
||||
const plogAsyncChild = plogAsync.child({ a: 'property' })
|
||||
const plogChildChild = plog.child({ a: 'property' }).child({ sub: 'child' })
|
||||
const plogDestChildChild = plogDest.child({ a: 'property' }).child({ sub: 'child' })
|
||||
const plogAsyncChildChild = plogAsync.child({ a: 'property' }).child({ sub: 'child' })
|
||||
|
||||
const max = 10
|
||||
|
||||
const run = bench([
|
||||
function benchPino (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDest (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoExtreme (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDestChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsyncChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogChildChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDestChildChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsyncChildChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildCreation (cb) {
|
||||
const child = plog.child({ a: 'property' })
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestChildCreation (cb) {
|
||||
const child = plogDest.child({ a: 'property' })
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMulti (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info('hello', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestMulti (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncMulti (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info('hello', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info('hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info('hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info('hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoDestInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../../')
|
||||
|
||||
const base = pino(pino.destination('/dev/null'))
|
||||
const child = base.child({})
|
||||
const childChild = child.child({})
|
||||
const childChildChild = childChild.child({})
|
||||
const childChildChildChild = childChildChild.child({})
|
||||
const child2 = base.child({})
|
||||
const baseSerializers = pino(pino.destination('/dev/null'))
|
||||
const baseSerializersChild = baseSerializers.child({})
|
||||
const baseSerializersChildSerializers = baseSerializers.child({})
|
||||
|
||||
const max = 100
|
||||
|
||||
const run = bench([
|
||||
function benchPinoBase (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
base.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
child.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
childChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
childChildChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChildChildChildChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
childChildChildChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoChild2 (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
child2.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoBaseSerializers (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
baseSerializers.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoBaseSerializersChild (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
baseSerializersChild.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoBaseSerializersChildSerializers (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
baseSerializersChildSerializers.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../../')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plog = pino(dest)
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogAsync = require('../../')(pino.destination({ dest: '/dev/null', sync: false }))
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogUnsafe = require('../../')({ safe: false }, dest)
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogUnsafeAsync = require('../../')(
|
||||
{ safe: false },
|
||||
pino.destination({ dest: '/dev/null', sync: false })
|
||||
)
|
||||
const plogRedact = pino({ redact: ['a.b.c'] }, dest)
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogAsyncRedact = require('../../')(
|
||||
{ redact: ['a.b.c'] },
|
||||
pino.destination({ dest: '/dev/null', sync: false })
|
||||
)
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogUnsafeRedact = require('../../')({ redact: ['a.b.c'], safe: false }, dest)
|
||||
delete require.cache[require.resolve('../../')]
|
||||
const plogUnsafeAsyncRedact = require('../../')(
|
||||
{ redact: ['a.b.c'], safe: false },
|
||||
pino.destination({ dest: '/dev/null', sync: false })
|
||||
)
|
||||
|
||||
const max = 10
|
||||
|
||||
// note that "redact me." is the same amount of bytes as the censor: "[Redacted]"
|
||||
|
||||
const run = bench([
|
||||
function benchPinoNoRedact (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plog.info({ a: { b: { c: 'redact me.', d: 'leave me' } } })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoRedact (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogRedact.info({ a: { b: { c: 'redact me.', d: 'leave me' } } })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoUnsafeNoRedact (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogUnsafe.info({ a: { b: { c: 'redact me.', d: 'leave me' } } })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoUnsafeRedact (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogUnsafeRedact.info({ a: { b: { c: 'redact me.', d: 'leave me' } } })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncNoRedact (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsync.info({ a: { b: { c: 'redact me.', d: 'leave me' } } })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoAsyncRedact (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogAsyncRedact.info({ a: { b: { c: 'redact me.', d: 'leave me' } } })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoUnsafeAsyncNoRedact (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogUnsafeAsync.info({ a: { b: { c: 'redact me.', d: 'leave me' } } })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoUnsafeAsyncRedact (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogUnsafeAsyncRedact.info({ a: { b: { c: 'redact me.', d: 'leave me' } } })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
const bunyan = require('bunyan')
|
||||
const bole = require('bole')('bench')
|
||||
const winston = require('winston')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plogNodeStream = pino(dest)
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogDest = require('../')(pino.destination('/dev/null'))
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogMinLength = require('../')(pino.destination({ dest: '/dev/null', sync: false, minLength: 4096 }))
|
||||
|
||||
const crypto = require('crypto')
|
||||
|
||||
const longStr = crypto.randomBytes(2000).toString()
|
||||
|
||||
const max = 10
|
||||
const blog = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{
|
||||
level: 'trace',
|
||||
stream: dest
|
||||
}]
|
||||
})
|
||||
|
||||
require('bole').output({
|
||||
level: 'info',
|
||||
stream: dest
|
||||
}).setFastTime(true)
|
||||
|
||||
const chill = winston.createLogger({
|
||||
transports: [
|
||||
new winston.transports.Stream({
|
||||
stream: fs.createWriteStream('/dev/null')
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
const run = bench([
|
||||
function benchBunyan (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchWinston (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
chill.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBole (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPino (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLength (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStream (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info(longStr)
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 1000)
|
||||
|
||||
run(run)
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
const bunyan = require('bunyan')
|
||||
const bole = require('bole')('bench')
|
||||
const winston = require('winston')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const plogNodeStream = pino(dest)
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogDest = require('../')(pino.destination('/dev/null'))
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogMinLength = require('../')(pino.destination({ dest: '/dev/null', sync: false, minLength: 4096 }))
|
||||
delete require.cache[require.resolve('../')]
|
||||
|
||||
const deep = require('../package.json')
|
||||
deep.deep = Object.assign({}, JSON.parse(JSON.stringify(deep)))
|
||||
deep.deep.deep = Object.assign({}, JSON.parse(JSON.stringify(deep)))
|
||||
deep.deep.deep.deep = Object.assign({}, JSON.parse(JSON.stringify(deep)))
|
||||
|
||||
const blog = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{
|
||||
level: 'trace',
|
||||
stream: dest
|
||||
}]
|
||||
})
|
||||
|
||||
require('bole').output({
|
||||
level: 'info',
|
||||
stream: dest
|
||||
}).setFastTime(true)
|
||||
|
||||
const chill = winston.createLogger({
|
||||
transports: [
|
||||
new winston.transports.Stream({
|
||||
stream: fs.createWriteStream('/dev/null')
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
const max = 10
|
||||
|
||||
const run = bench([
|
||||
function benchBunyanInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info('hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchWinstonInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
chill.log('info', 'hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBoleInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info('hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info('hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamInterpolate (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info('hello %s', 'world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBunyanInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info('hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
|
||||
function benchWinstonInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
chill.log('info', 'hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBoleInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info('hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info('hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamInterpolateAll (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info('hello %s %j %d', 'world', { obj: true }, 4)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBunyanInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchWinstonInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
chill.log('info', 'hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBoleInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamInterpolateExtra (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info('hello %s %j %d', 'world', { obj: true }, 4, { another: 'obj' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBunyanInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info('hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchWinstonInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
chill.log('info', 'hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBoleInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info('hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info('hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info('hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamInterpolateDeep (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info('hello %j', deep)
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const bunyan = require('bunyan')
|
||||
const pino = require('../')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
|
||||
const tenStreams = [
|
||||
{ stream: dest },
|
||||
{ stream: dest },
|
||||
{ stream: dest },
|
||||
{ stream: dest },
|
||||
{ stream: dest },
|
||||
{ level: 'debug', stream: dest },
|
||||
{ level: 'debug', stream: dest },
|
||||
{ level: 'trace', stream: dest },
|
||||
{ level: 'warn', stream: dest },
|
||||
{ level: 'fatal', stream: dest }
|
||||
]
|
||||
const pinomsTen = pino({ level: 'debug' }, pino.multistream(tenStreams))
|
||||
|
||||
const fourStreams = [
|
||||
{ stream: dest },
|
||||
{ stream: dest },
|
||||
{ level: 'debug', stream: dest },
|
||||
{ level: 'trace', stream: dest }
|
||||
]
|
||||
const pinomsFour = pino({ level: 'debug' }, pino.multistream(fourStreams))
|
||||
|
||||
const pinomsOne = pino({ level: 'info' }, pino.multistream(dest))
|
||||
const blogOne = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{ stream: dest }]
|
||||
})
|
||||
|
||||
const blogTen = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: tenStreams
|
||||
})
|
||||
const blogFour = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: fourStreams
|
||||
})
|
||||
|
||||
const max = 10
|
||||
const run = bench([
|
||||
function benchBunyanTen (cb) {
|
||||
for (let i = 0; i < max; i++) {
|
||||
blogTen.info('hello world')
|
||||
blogTen.debug('hello world')
|
||||
blogTen.trace('hello world')
|
||||
blogTen.warn('hello world')
|
||||
blogTen.fatal('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMSTen (cb) {
|
||||
for (let i = 0; i < max; i++) {
|
||||
pinomsTen.info('hello world')
|
||||
pinomsTen.debug('hello world')
|
||||
pinomsTen.trace('hello world')
|
||||
pinomsTen.warn('hello world')
|
||||
pinomsTen.fatal('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBunyanFour (cb) {
|
||||
for (let i = 0; i < max; i++) {
|
||||
blogFour.info('hello world')
|
||||
blogFour.debug('hello world')
|
||||
blogFour.trace('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMSFour (cb) {
|
||||
for (let i = 0; i < max; i++) {
|
||||
pinomsFour.info('hello world')
|
||||
pinomsFour.debug('hello world')
|
||||
pinomsFour.trace('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBunyanOne (cb) {
|
||||
for (let i = 0; i < max; i++) {
|
||||
blogOne.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMSOne (cb) {
|
||||
for (let i = 0; i < max; i++) {
|
||||
pinomsOne.info('hello world')
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run()
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
'use strict'
|
||||
|
||||
const bench = require('fastbench')
|
||||
const pino = require('../')
|
||||
const bunyan = require('bunyan')
|
||||
const bole = require('bole')('bench')
|
||||
const winston = require('winston')
|
||||
const fs = require('node:fs')
|
||||
const dest = fs.createWriteStream('/dev/null')
|
||||
const loglevel = require('./utils/wrap-log-level')(dest)
|
||||
const plogNodeStream = pino(dest)
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogDest = require('../')(pino.destination('/dev/null'))
|
||||
delete require.cache[require.resolve('../')]
|
||||
const plogMinLength = require('../')(pino.destination({ dest: '/dev/null', sync: false, minLength: 4096 }))
|
||||
const blog = bunyan.createLogger({
|
||||
name: 'myapp',
|
||||
streams: [{
|
||||
level: 'trace',
|
||||
stream: dest
|
||||
}]
|
||||
})
|
||||
require('bole').output({
|
||||
level: 'info',
|
||||
stream: dest
|
||||
}).setFastTime(true)
|
||||
const chill = winston.createLogger({
|
||||
transports: [
|
||||
new winston.transports.Stream({
|
||||
stream: fs.createWriteStream('/dev/null')
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
const max = 10
|
||||
|
||||
const run = bench([
|
||||
function benchBunyanObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
blog.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchWinstonObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
chill.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchBoleObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
bole.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchLogLevelObject (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
loglevel.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogDest.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoMinLengthObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogMinLength.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
},
|
||||
function benchPinoNodeStreamObj (cb) {
|
||||
for (var i = 0; i < max; i++) {
|
||||
plogNodeStream.info({ hello: 'world' })
|
||||
}
|
||||
setImmediate(cb)
|
||||
}
|
||||
], 10000)
|
||||
|
||||
run(run)
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
'use strict'
|
||||
const { join } = require('node:path')
|
||||
const { execSync } = require('node:child_process')
|
||||
|
||||
const run = (type) => {
|
||||
process.stderr.write(`benchmarking ${type}\n`)
|
||||
return execSync(`node ${join(__dirname, 'runbench')} ${type} -q`)
|
||||
}
|
||||
|
||||
console.log(`
|
||||
# Benchmarks
|
||||
|
||||
\`pino.info('hello world')\`:
|
||||
|
||||
\`\`\`
|
||||
${run('basic')}
|
||||
\`\`\`
|
||||
|
||||
\`pino.info({'hello': 'world'})\`:
|
||||
|
||||
\`\`\`
|
||||
${run('object')}
|
||||
\`\`\`
|
||||
|
||||
\`pino.info(aBigDeeplyNestedObject)\`:
|
||||
|
||||
\`\`\`
|
||||
${run('deep-object')}
|
||||
\`\`\`
|
||||
|
||||
\`pino.info('hello %s %j %d', 'world', {obj: true}, 4, {another: 'obj'})\`:
|
||||
|
||||
For a fair comparison, [LogLevel](http://npm.im/loglevel) was extended
|
||||
to include a timestamp and [bole](http://npm.im/bole) had
|
||||
\`fastTime\` mode switched on.
|
||||
`)
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
'use strict'
|
||||
|
||||
const { type, platform, arch, release, cpus } = require('node:os')
|
||||
const { resolve, join } = require('node:path')
|
||||
const spawn = require('node:child_process').spawn
|
||||
const pump = require('pump')
|
||||
const split = require('split2')
|
||||
const through = require('through2')
|
||||
const steed = require('steed')
|
||||
|
||||
function usage () {
|
||||
console.log(`
|
||||
Pino Benchmarks
|
||||
|
||||
To run a benchmark, specify which to run:
|
||||
|
||||
・all ⁃ run all benchmarks (takes a while)
|
||||
・basic ⁃ log a simple string
|
||||
・object ⁃ logging a basic object
|
||||
・deep-object ⁃ logging a large object
|
||||
・multi-arg ⁃ multiple log method arguments
|
||||
・child ⁃ child from a parent
|
||||
・child-child ⁃ child from a child
|
||||
・child-creation ⁃ child constructor
|
||||
・formatters ⁃ difference between with or without formatters
|
||||
|
||||
Example:
|
||||
|
||||
node runbench basic
|
||||
`)
|
||||
}
|
||||
|
||||
if (!process.argv[2]) {
|
||||
usage()
|
||||
process.exit()
|
||||
}
|
||||
|
||||
const quiet = process.argv[3] === '-q'
|
||||
|
||||
const selectedBenchmark = process.argv[2].toLowerCase()
|
||||
const benchmarkDir = resolve(__dirname, '..')
|
||||
const benchmarks = {
|
||||
basic: 'basic.bench.js',
|
||||
object: 'object.bench.js',
|
||||
'deep-object': 'deep-object.bench.js',
|
||||
'multi-arg': 'multi-arg.bench.js',
|
||||
'long-string': 'long-string.bench.js',
|
||||
child: 'child.bench.js',
|
||||
'child-child': 'child-child.bench.js',
|
||||
'child-creation': 'child-creation.bench.js',
|
||||
formatters: 'formatters.bench.js'
|
||||
}
|
||||
|
||||
function runBenchmark (name, done) {
|
||||
const benchmarkResults = {}
|
||||
benchmarkResults[name] = {}
|
||||
|
||||
const processor = through(function (line, enc, cb) {
|
||||
const [label, time] = ('' + line).split(': ')
|
||||
const [target, iterations] = label.split('*')
|
||||
const logger = target.replace('bench', '')
|
||||
|
||||
if (!benchmarkResults[name][logger]) benchmarkResults[name][logger] = []
|
||||
|
||||
benchmarkResults[name][logger].push({
|
||||
time: time.replace('ms', ''),
|
||||
iterations: iterations.replace(':', '')
|
||||
})
|
||||
|
||||
cb()
|
||||
})
|
||||
|
||||
if (quiet === false) console.log(`Running ${name.toUpperCase()} benchmark\n`)
|
||||
|
||||
const benchmark = spawn(
|
||||
process.argv[0],
|
||||
[join(benchmarkDir, benchmarks[name])]
|
||||
)
|
||||
|
||||
if (quiet === false) {
|
||||
benchmark.stdout.pipe(process.stdout)
|
||||
}
|
||||
|
||||
pump(benchmark.stdout, split(), processor)
|
||||
|
||||
benchmark.on('exit', () => {
|
||||
console.log()
|
||||
if (done && typeof done === 'function') done(null, benchmarkResults)
|
||||
})
|
||||
}
|
||||
|
||||
function sum (arr) {
|
||||
let result = 0
|
||||
for (var i = 0; i < arr.length; i += 1) {
|
||||
result += Number.parseFloat(arr[i].time)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function displayResults (results) {
|
||||
if (quiet === false) console.log('==========')
|
||||
const benchNames = Object.keys(results)
|
||||
for (var i = 0; i < benchNames.length; i += 1) {
|
||||
console.log(`${benchNames[i].toUpperCase()} benchmark averages`)
|
||||
const benchmark = results[benchNames[i]]
|
||||
const loggers = Object.keys(benchmark)
|
||||
for (var j = 0; j < loggers.length; j += 1) {
|
||||
const logger = benchmark[loggers[j]]
|
||||
const average = sum(logger) / logger.length
|
||||
console.log(`${loggers[j]} average: ${average.toFixed(3)}ms`)
|
||||
}
|
||||
}
|
||||
if (quiet === false) {
|
||||
console.log('==========')
|
||||
console.log(
|
||||
`System: ${type()}/${platform()} ${arch()} ${release()}`,
|
||||
`~ ${cpus()[0].model} (cores/threads: ${cpus().length})`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function toBench (done) {
|
||||
runBenchmark(this.name, done)
|
||||
}
|
||||
|
||||
const benchQueue = []
|
||||
if (selectedBenchmark !== 'all') {
|
||||
benchQueue.push(toBench.bind({ name: selectedBenchmark }))
|
||||
} else {
|
||||
const keys = Object.keys(benchmarks)
|
||||
for (var i = 0; i < keys.length; i += 1) {
|
||||
benchQueue.push(toBench.bind({ name: keys[i] }))
|
||||
}
|
||||
}
|
||||
steed.series(benchQueue, function (err, results) {
|
||||
if (err) return console.error(err.message)
|
||||
results.forEach(displayResults)
|
||||
})
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
'use strict'
|
||||
|
||||
const { readFileSync } = require('node:fs')
|
||||
const vm = require('vm')
|
||||
const { join } = require('node:path')
|
||||
const code = readFileSync(
|
||||
join(__dirname, '..', '..', 'node_modules', 'loglevel', 'lib', 'loglevel.js')
|
||||
)
|
||||
const { Console } = require('console')
|
||||
|
||||
function build (dest) {
|
||||
const sandbox = {
|
||||
module: {},
|
||||
console: new Console(dest, dest)
|
||||
}
|
||||
const context = vm.createContext(sandbox)
|
||||
|
||||
const script = new vm.Script(code)
|
||||
script.runInContext(context)
|
||||
|
||||
const loglevel = sandbox.log
|
||||
|
||||
const originalFactory = loglevel.methodFactory
|
||||
loglevel.methodFactory = function (methodName, logLevel, loggerName) {
|
||||
const rawMethod = originalFactory(methodName, logLevel, loggerName)
|
||||
|
||||
return function () {
|
||||
const time = new Date()
|
||||
let array
|
||||
if (typeof arguments[0] === 'string') {
|
||||
arguments[0] = '[' + time.toISOString() + '] ' + arguments[0]
|
||||
rawMethod.apply(null, arguments)
|
||||
} else {
|
||||
array = new Array(arguments.length + 1)
|
||||
array[0] = '[' + time.toISOString() + ']'
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
array[i + 1] = arguments[i]
|
||||
}
|
||||
rawMethod.apply(null, array)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loglevel.setLevel(loglevel.levels.INFO)
|
||||
return loglevel
|
||||
}
|
||||
|
||||
module.exports = build
|
||||
|
||||
if (require.main === module) {
|
||||
const loglevel = build(process.stdout)
|
||||
loglevel.info('hello')
|
||||
loglevel.info({ hello: 'world' })
|
||||
loglevel.info('hello %j', { hello: 'world' })
|
||||
}
|
||||
Loaded 100 of 415 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user