Skip to content

Commit

Permalink
Remove in-between Transform for file (#1140)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Sep 24, 2021
1 parent 8261137 commit ba7241f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coverage: true
timeout: 480
check-coverage: false
12 changes: 1 addition & 11 deletions file.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
'use strict'

const { Transform, pipeline } = require('stream')
const pino = require('./pino')
const { once } = require('events')

module.exports = async function (opts = {}) {
const stream = new Transform({
objectMode: true,
autoDestroy: true,
transform (chunk, enc, cb) {
cb(null, chunk.toString())
}
})

const destination = pino.destination({ dest: opts.destination || 1, sync: false })
await once(destination, 'ready')
pipeline(stream, destination, () => {})
return stream
return destination
}
29 changes: 29 additions & 0 deletions test/fixtures/transport-many-lines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const pino = require('../..')
const transport = pino.transport({
targets: [{
level: 'info',
target: 'pino/file',
options: {
destination: process.argv[2]
}
}]
})
const logger = pino(transport)

const toWrite = 1000000
transport.on('ready', run)

let total = 0

function run () {
if (total++ === 8) {
return
}

for (let i = 0; i < toWrite; i++) {
logger.info(`hello ${i}`)
}
transport.once('drain', run)
}
29 changes: 29 additions & 0 deletions test/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const url = require('url')
const strip = require('strip-ansi')
const execa = require('execa')
const writer = require('flush-write-stream')
const { promisify } = require('util')
const stream = require('stream')
const { createReadStream } = require('fs')
const split = require('split2')

const pipeline = promisify(stream.pipeline)
const { Writable } = stream

const { pid } = process
const hostname = os.hostname()
Expand Down Expand Up @@ -478,3 +485,25 @@ test('transport options with target and stream', async ({ fail, equal }) => {
equal(err.message, 'only one of option.transport or stream can be specified')
}
})

test('eight million lines', async ({ equal, comment }) => {
const destination = join(
os.tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'transport-many-lines.js'), destination])

await once(child, 'exit')
const toWrite = 8 * 1000000
let count = 0
await pipeline(createReadStream(destination), split(), new Writable({
write (chunk, enc, cb) {
if (count % (toWrite / 10) === 0) {
comment(`read ${count}`)
}
count++
cb()
}
}))
equal(count, toWrite)
})

0 comments on commit ba7241f

Please sign in to comment.