thread-stream 0.11.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +2 -2
- package/.github/workflows/package-manager-ci.yml +59 -0
- package/README.md +22 -0
- package/lib/worker.js +19 -1
- package/package.json +2 -1
- package/test/commonjs-fallback.test.js +29 -0
package/.github/workflows/ci.yml
CHANGED
|
@@ -22,7 +22,7 @@ jobs:
|
|
|
22
22
|
- uses: actions/checkout@v2.3.4
|
|
23
23
|
|
|
24
24
|
- name: Use Node.js
|
|
25
|
-
uses: actions/setup-node@v2.
|
|
25
|
+
uses: actions/setup-node@v2.4.0
|
|
26
26
|
with:
|
|
27
27
|
node-version: ${{ matrix.node-version }}
|
|
28
28
|
|
|
@@ -55,6 +55,6 @@ jobs:
|
|
|
55
55
|
needs: test
|
|
56
56
|
runs-on: ubuntu-latest
|
|
57
57
|
steps:
|
|
58
|
-
- uses: fastify/github-action-merge-dependabot@v2.
|
|
58
|
+
- uses: fastify/github-action-merge-dependabot@v2.4.0
|
|
59
59
|
with:
|
|
60
60
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
name: package-manager-ci
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
paths-ignore:
|
|
5
|
+
- 'docs/**'
|
|
6
|
+
- '*.md'
|
|
7
|
+
pull_request:
|
|
8
|
+
paths-ignore:
|
|
9
|
+
- 'docs/**'
|
|
10
|
+
- '*.md'
|
|
11
|
+
jobs:
|
|
12
|
+
pnpm:
|
|
13
|
+
name: pnpm package manager on ${{ matrix.node-version }} ${{ matrix.os }}
|
|
14
|
+
runs-on: ${{ matrix.os }}
|
|
15
|
+
strategy:
|
|
16
|
+
matrix:
|
|
17
|
+
os: [macOS-latest, windows-latest]
|
|
18
|
+
node-version: [12, 14, 16]
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v2.3.4
|
|
21
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
22
|
+
uses: actions/setup-node@v2.4.0
|
|
23
|
+
with:
|
|
24
|
+
node-version: ${{ matrix.node-version }}
|
|
25
|
+
- name: Use pnpm
|
|
26
|
+
uses: pnpm/action-setup@v2.0.1
|
|
27
|
+
with:
|
|
28
|
+
version: ^6.0.0
|
|
29
|
+
- name: Install dependancies
|
|
30
|
+
run: pnpm install
|
|
31
|
+
- name: Tests
|
|
32
|
+
run: pnpm run test:ci
|
|
33
|
+
|
|
34
|
+
yarn-pnp:
|
|
35
|
+
name: yarn-pnp package manager on ${{ matrix.node-version }} ${{ matrix.os }}
|
|
36
|
+
runs-on: ${{ matrix.os }}
|
|
37
|
+
strategy:
|
|
38
|
+
matrix:
|
|
39
|
+
os: [macOS-latest]
|
|
40
|
+
node-version: [12, 14, 16]
|
|
41
|
+
steps:
|
|
42
|
+
- uses: actions/checkout@v2.3.4
|
|
43
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
44
|
+
uses: actions/setup-node@v2.4.0
|
|
45
|
+
with:
|
|
46
|
+
node-version: ${{ matrix.node-version }}
|
|
47
|
+
- name: Use yarn
|
|
48
|
+
run: |
|
|
49
|
+
npm install -g yarn
|
|
50
|
+
yarn set version berry
|
|
51
|
+
echo "nodeLinker: pnp" >> .yarnrc.yml
|
|
52
|
+
echo "pnpMode: loose" >> .yarnrc.yml
|
|
53
|
+
yarn add -D pino-elasticsearch@^6.0.0
|
|
54
|
+
yarn install
|
|
55
|
+
env:
|
|
56
|
+
# needed due the yarn.lock file in repository's .gitignore
|
|
57
|
+
YARN_ENABLE_IMMUTABLE_INSTALLS: false
|
|
58
|
+
- name: Tests
|
|
59
|
+
run: yarn run test:yarn
|
package/README.md
CHANGED
|
@@ -64,6 +64,28 @@ flag your stream classes.
|
|
|
64
64
|
|
|
65
65
|
The underlining worker is automatically closed if the stream is garbage collected.
|
|
66
66
|
|
|
67
|
+
|
|
68
|
+
### External modules
|
|
69
|
+
|
|
70
|
+
You may use this module within compatible external modules, that exports the `worker.js` interface.
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const ThreadStream = require('thread-stream')
|
|
74
|
+
|
|
75
|
+
const modulePath = require.resolve('pino-elasticsearch')
|
|
76
|
+
|
|
77
|
+
const stream = new ThreadStream({
|
|
78
|
+
filename: modulePath,
|
|
79
|
+
workerData: { node: 'http://localhost:9200' }
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
stream.write('log to elasticsearch!')
|
|
83
|
+
stream.flushSync()
|
|
84
|
+
stream.end()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
This module works with `yarn` in PnP (plug'n play) mode too!
|
|
88
|
+
|
|
67
89
|
## License
|
|
68
90
|
|
|
69
91
|
MIT
|
package/lib/worker.js
CHANGED
|
@@ -15,7 +15,25 @@ const state = new Int32Array(stateBuf)
|
|
|
15
15
|
const data = Buffer.from(dataBuf)
|
|
16
16
|
|
|
17
17
|
async function start () {
|
|
18
|
-
|
|
18
|
+
let fn
|
|
19
|
+
try {
|
|
20
|
+
fn = (await import(workerData.filename)).default
|
|
21
|
+
} catch (error) {
|
|
22
|
+
// A yarn user that tries to start a ThreadStream for an external module
|
|
23
|
+
// provides a filename pointing to a zip file.
|
|
24
|
+
// eg. require.resolve('pino-elasticsearch') // returns /foo/pino-elasticsearch-npm-6.1.0-0c03079478-6915435172.zip/bar.js
|
|
25
|
+
// The `import` will fail to try to load it.
|
|
26
|
+
// This catch block executes the `require` fallback to load the module correctly.
|
|
27
|
+
// In fact, yarn modifies the `require` function to manage the zipped path.
|
|
28
|
+
// More details at https://github.com/pinojs/pino/pull/1113
|
|
29
|
+
// The error codes may change based on the node.js version (ENOTDIR > 12, ERR_MODULE_NOT_FOUND <= 12 )
|
|
30
|
+
if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND') &&
|
|
31
|
+
workerData.filename.startsWith('file://')) {
|
|
32
|
+
fn = require(workerData.filename.replace('file://', ''))
|
|
33
|
+
} else {
|
|
34
|
+
throw error
|
|
35
|
+
}
|
|
36
|
+
}
|
|
19
37
|
destination = await fn(workerData.workerData)
|
|
20
38
|
|
|
21
39
|
destination.on('error', function (err) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "thread-stream",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"description": "A streaming way to send data to a Node.js Worker Thread",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"devDependencies": {
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"scripts": {
|
|
15
15
|
"test": "standard && tap --no-check-coverage test/*.test.*js",
|
|
16
16
|
"test:ci": "standard && tap \"test/**/*.test.*js\" --no-check-coverage --coverage-report=lcovonly",
|
|
17
|
+
"test:yarn": "tap \"test/**/*.test.js\" --no-check-coverage",
|
|
17
18
|
"prepare": "husky install"
|
|
18
19
|
},
|
|
19
20
|
"repository": {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const ThreadStream = require('..')
|
|
5
|
+
|
|
6
|
+
const isYarnPnp = process.versions.pnp !== undefined
|
|
7
|
+
|
|
8
|
+
test('yarn module resolution', { skip: !isYarnPnp }, t => {
|
|
9
|
+
t.plan(5)
|
|
10
|
+
|
|
11
|
+
const modulePath = require.resolve('pino-elasticsearch')
|
|
12
|
+
t.match(modulePath, /.*\.zip.*/)
|
|
13
|
+
|
|
14
|
+
const stream = new ThreadStream({
|
|
15
|
+
filename: modulePath,
|
|
16
|
+
workerData: { node: null },
|
|
17
|
+
sync: true
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
stream.on('error', (err) => {
|
|
21
|
+
t.pass('error emitted')
|
|
22
|
+
t.equal(err.message, 'Missing node(s) option', 'module custom error')
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
t.ok(stream.write('hello world\n'))
|
|
26
|
+
t.ok(stream.writable)
|
|
27
|
+
|
|
28
|
+
stream.end()
|
|
29
|
+
})
|