Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require fallback #34

Merged
merged 8 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions .github/workflows/package-manager-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 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
12 changes: 11 additions & 1 deletion lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ 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) {
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"desm": "^1.1.0",
"fastbench": "^1.0.1",
"husky": "^7.0.0",
"pino-elasticsearch": "^6.1.0",
"sonic-boom": "^2.0.1",
"standard": "^16.0.3",
"tap": "^15.0.0"
},
"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()
})