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

feat: Support bundlers. #48

Merged
merged 2 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const registry = new FinalizationRegistry((worker) => {
function createWorker (stream, opts) {
const { filename, workerData } = opts

const toExecute = join(__dirname, 'lib', 'worker.js')
const bundlerOverrides = '__bundlerPathsOverrides' in globalThis ? globalThis.__bundlerPathsOverrides : {}
const toExecute = bundlerOverrides['thread-stream-worker'] || join(__dirname, 'lib', 'worker.js')

const worker = new Worker(toExecute, {
...opts.workerOpts,
Expand Down
5 changes: 3 additions & 2 deletions lib/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { realImport, realRequire } = require('real-require')
const { workerData, parentPort } = require('worker_threads')
const { WRITE_INDEX, READ_INDEX } = require('./indexes')
const { waitDiff } = require('./wait')
Expand All @@ -17,7 +18,7 @@ const data = Buffer.from(dataBuf)
async function start () {
let fn
try {
fn = (await import(workerData.filename)).default
fn = (await realImport(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.
Expand All @@ -29,7 +30,7 @@ async function start () {
// 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://', ''))
fn = realRequire(workerData.filename.replace('file://', ''))
} else {
throw error
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.12.1",
"description": "A streaming way to send data to a Node.js Worker Thread",
"main": "index.js",
"dependencies": {
"real-require": "^0.1.0"
},
"devDependencies": {
"desm": "^1.1.0",
"fastbench": "^1.0.1",
Expand Down