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

Relative handling, import.meta.url encouragement #5

Merged
merged 3 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ _Here's how this is different from worker_threads:_
```js
import Worker from 'web-worker';

const worker = new Worker('./worker.js');
const worker = new Worker(new URL('./worker.js', import.meta.url));

worker.addEventListener('message', e => {
console.log(e.data) // "hiya!"
Expand All @@ -55,6 +55,9 @@ addEventListener('message', e => {
</td></tr></tbody>
</table>

The pattern `new URL('./worker.js', import.meta.url)` is used above to load the worker relative to the current module instead of the application base URL. Without this, Worker URLs are relative to a document's URL, which in Node.js is interpreted to be `process.cwd()`.

Support for this pattern in build tools and test frameworks is still quite limited, but we are working on growing this support (tracking issue https://github.com/developit/web-worker/issues/4).

### Module Workers

Expand All @@ -68,7 +71,7 @@ In the browser, they can be used natively in Chrome 80+, or in all browsers via
```js
import Worker from 'web-worker';

const worker = new Worker('./worker.mjs', {
const worker = new Worker(new URL('./worker.mjs', import.meta.url), {
type: 'module'
});
worker.addEventListener('message', e => {
Expand Down
16 changes: 8 additions & 8 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ function Event(type, target) {
// thread boundary, but behaves differently in each context.
export default threads.isMainThread ? mainThread() : workerThread();

const baseUrl = URL.pathToFileURL(process.cwd() + '/');

function mainThread() {

/**
Expand All @@ -90,15 +92,13 @@ function mainThread() {
constructor(url, options) {
super();
const { name, type } = options || {};
// hack: grab the caller's filename from a stack trace
let relativeTo = process.cwd();
try {
relativeTo = Error().stack.split('\n')[2].match(/ \((.+):[^:]+:[^:]+\)$/)[1];
if (typeof url !== 'string') url = url.toString();
let mod;
if (/^data:/.test(url)) {
mod = url;
}
catch (e) {}
let mod = url;
if (!/^data:/.test(url)) {
mod = URL.fileURLToPath(new URL.URL(url, 'file://' + relativeTo));
else {
mod = URL.fileURLToPath(new URL.URL(url, baseUrl));
}
const worker = new threads.Worker(
__filename,
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test.after.always(t => {
});

test.serial('instantiation', async t => {
worker = createModuleWorker('./fixtures/worker.mjs');
worker = createModuleWorker('./test/fixtures/worker.mjs');
await sleep(500);
t.is(worker.events.length, 1, 'should have received a message event');
t.is(worker.events[0].data, 42);
Expand Down