Skip to content

Commit

Permalink
require fallback (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm authored Sep 3, 2021
1 parent e88be6e commit 0a01728
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 1 deletion.
59 changes: 59 additions & 0 deletions .github/workflows/package-manager-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: package-manager-ci
on:
push:
paths-ignore:
- 'docs/**'
- '*.md'
pull_request:
paths-ignore:
- 'docs/**'
- '*.md'
jobs:
pnpm:
name: pnpm package manager on ${{ matrix.node-version }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macOS-latest, windows-latest]
node-version: [12, 14, 16]
steps:
- uses: actions/[email protected]
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
- name: Use pnpm
uses: pnpm/[email protected]
with:
version: ^6.0.0
- name: Install dependancies
run: pnpm install
- name: Tests
run: pnpm run test:ci

yarn-pnp:
name: yarn-pnp package manager on ${{ matrix.node-version }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macOS-latest]
node-version: [12, 14, 16]
steps:
- uses: actions/[email protected]
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
- name: Use yarn
run: |
npm install -g yarn
yarn set version berry
echo "nodeLinker: pnp" >> .yarnrc.yml
echo "pnpMode: loose" >> .yarnrc.yml
yarn add -D pino-elasticsearch@^6.0.0
yarn install
env:
# needed due the yarn.lock file in repository's .gitignore
YARN_ENABLE_IMMUTABLE_INSTALLS: false
- name: Tests
run: yarn run test:yarn
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ typings/
# Optional npm cache directory
.npm

# Yarn files
.yarn
.yarnrc.yml
.pnp.cjs
yarn.lock

# Optional eslint cache
.eslintcache

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ 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.

```js
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!

## License

MIT
20 changes: 19 additions & 1 deletion lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,25 @@ const state = new Int32Array(stateBuf)
const data = Buffer.from(dataBuf)

async function start () {
const fn = (await import(workerData.filename)).default
let fn
try {
fn = (await import(workerData.filename)).default
} catch (error) {
// A yarn user that tries to start a ThreadStream for an external module
// provides a filename pointing to a zip file.
// eg. require.resolve('pino-elasticsearch') // returns /foo/pino-elasticsearch-npm-6.1.0-0c03079478-6915435172.zip/bar.js
// The `import` will fail to try to load it.
// This catch block executes the `require` fallback to load the module correctly.
// In fact, yarn modifies the `require` function to manage the zipped path.
// More details at https://github.com/pinojs/pino/pull/1113
// The error codes may change based on the node.js version (ENOTDIR > 12, ERR_MODULE_NOT_FOUND <= 12 )
if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND') &&
workerData.filename.startsWith('file://')) {
fn = require(workerData.filename.replace('file://', ''))
} else {
throw error
}
}
destination = await fn(workerData.workerData)

destination.on('error', function (err) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"scripts": {
"test": "standard && tap --no-check-coverage test/*.test.*js",
"test:ci": "standard && tap \"test/**/*.test.*js\" --no-check-coverage --coverage-report=lcovonly",
"test:yarn": "tap \"test/**/*.test.js\" --no-check-coverage",
"prepare": "husky install"
},
"repository": {
Expand Down
29 changes: 29 additions & 0 deletions test/commonjs-fallback.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const { test } = require('tap')
const ThreadStream = require('..')

const isYarnPnp = process.versions.pnp !== undefined

test('yarn module resolution', { skip: !isYarnPnp }, t => {
t.plan(5)

const modulePath = require.resolve('pino-elasticsearch')
t.match(modulePath, /.*\.zip.*/)

const stream = new ThreadStream({
filename: modulePath,
workerData: { node: null },
sync: true
})

stream.on('error', (err) => {
t.pass('error emitted')
t.equal(err.message, 'Missing node(s) option', 'module custom error')
})

t.ok(stream.write('hello world\n'))
t.ok(stream.writable)

stream.end()
})

0 comments on commit 0a01728

Please sign in to comment.