• Stars
    star
    229
  • Rank 174,666 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A streaming way to send data to a Node.js Worker Thread

thread-stream

npm version Build Status js-standard-style

A streaming way to send data to a Node.js Worker Thread.

install

npm i thread-stream

Usage

'use strict'

const ThreadStream = require('thread-stream')
const { join } = require('path')

const stream = new ThreadStream({
  filename: join(__dirname, 'worker.js'),
  workerData: { dest },
  workerOpts: {}, // Other options to be passed to Worker
  sync: false, // default
})

stream.write('hello')

// Asynchronous flushing
stream.flush(function () {
  stream.write(' ')
  stream.write('world')

  // Synchronous flushing
  stream.flushSync()
  stream.end()
})

In worker.js:

'use strict'

const fs = require('fs')
const { once } = require('events')

async function run (opts) {
  const stream = fs.createWriteStream(opts.dest)
  await once(stream, 'open')
  return stream
}

module.exports = run

Make sure that the stream emits 'close' when the stream completes. This can usually be achieved by passing the autoDestroy: true flag your stream classes.

The underlining worker is automatically closed if the stream is garbage collected.

External modules

You may use this module within compatible external modules, that exports the worker.js interface.

const ThreadStream = require('thread-stream')

const modulePath = require.resolve('pino-elasticsearch')

const stream = new ThreadStream({
  filename: modulePath,
  workerData: { node: 'http://localhost:9200' }
})

stream.write('log to elasticsearch!')
stream.flushSync()
stream.end()

This module works with yarn in PnP (plug'n play) mode too!

Emit events

You can emit events on the ThreadStream from your worker using worker.parentPort.postMessage(). The message (JSON object) must have the following data structure:

parentPort.postMessage({
  code: 'EVENT',
  name: 'eventName',
  args: ['list', 'of', 'args', 123, new Error('Boom')]
})

On your ThreadStream, you can add a listener function for this event name:

const stream = new ThreadStream({
  filename: join(__dirname, 'worker.js'),
  workerData: {},
})
stream.on('eventName', function (a, b, c, n, err) {
  console.log('received:', a, b, c, n, err) // received: list of args 123 Error: Boom
})

License

MIT

More Repositories

1

pino

๐ŸŒฒ super fast, all natural json logger
JavaScript
14,160
star
2

pino-pretty

๐ŸŒฒBasic prettifier for Pino log lines
JavaScript
1,212
star
3

pino-http

๐ŸŒฒ high-speed HTTP logger for Node.js
JavaScript
535
star
4

sonic-boom

Extremely fast utf8 only stream implementation
JavaScript
266
star
5

express-pino-logger

๐ŸŒฒ an express middleware to log with pino
JavaScript
199
star
6

pino-elasticsearch

๐ŸŒฒ load pino logs into Elasticsearch
JavaScript
176
star
7

pino-debug

๐ŸŒฒhigh performance debug logging ๐Ÿž
JavaScript
147
star
8

koa-pino-logger

๐ŸŒฒ pino logging koa middleware
JavaScript
91
star
9

pino-tee

๐ŸŒฒ tee pino logs into a file, with multiple levels
JavaScript
89
star
10

pino-multi-stream

๐ŸŒฒ A wrapper for Pino to provide Bunyan's multiple stream API
JavaScript
69
star
11

pino-nextjs-example

JavaScript
66
star
12

pino-noir

๐ŸŒฒ pino log redaction ๐Ÿท
JavaScript
66
star
13

pino-std-serializers

๐ŸŒฒ A list of standard object serializers for the Pino logger
JavaScript
58
star
14

pino-caller

๐ŸŒฒ Include call site of pino log messages
JavaScript
56
star
15

pino-mongodb

๐ŸŒฒ Insert JSON from stdin into MongoDB
JavaScript
54
star
16

pino-socket

๐ŸŒฒ A transport for sending pino logs to network sockets
JavaScript
42
star
17

pino-abstract-transport

Write Pino transports easily
JavaScript
34
star
18

pino-syslog

๐ŸŒฒ A transport for reformatting pino logs into standard syslog format
JavaScript
29
star
19

pino-arborsculpture

๐ŸŒฒ Change Pino log levels in a running process
JavaScript
24
star
20

pino-opentelemetry-transport

OpenTelemetry transport for Pino
JavaScript
21
star
21

pino-webpack-plugin

JavaScript
19
star
22

quick-format-unescaped

Solves a problem with util.format
JavaScript
17
star
23

pino-gelf

๐ŸŒฒ Convert Pino logs to GELF format and send to Graylog
JavaScript
13
star
24

restify-pino-logger

๐ŸŒฒ pino logging restify middleware
JavaScript
12
star
25

pino-inspector

Send your pino logs to the node inspector!
JavaScript
12
star
26

pino-http-print

๐ŸŒฒ debug HTTP printer for pino
JavaScript
9
star
27

pino-filter

๐ŸŒฒ A transport to filter log lines in the manner of the `debug` module
JavaScript
9
star
28

real-require

Keep require and import consistent after bundling or transpiling.
JavaScript
8
star
29

pino-clf

๐ŸŒฒ Transport which transforms Pino HTTP logs into Common Log Format
JavaScript
5
star
30

getpino.io

The website for pino
CSS
5
star
31

pino-toke

๐ŸŒฒ Transform Pino HTTP log messages with a format string
JavaScript
5
star
32

pino-test

JavaScript
2
star
33

pino-sapling

๐ŸŒฒ seed template for creating pino plugins/libraries/transports/utilities/modules
JavaScript
2
star
34

community

๐ŸŒฒ data on the members of the pino community
JavaScript
1
star
35

rill-pino-logger

๐ŸŒฒpino logging rill middleware
JavaScript
1
star