web-worker
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -17,10 +17,6 @@ /** | ||
const URL = require('url'); | ||
const VM = require('vm'); | ||
const threads = require('worker_threads'); | ||
const WORKER = Symbol.for('worker'); | ||
const EVENTS = Symbol.for('events'); | ||
class EventTarget { | ||
@@ -32,6 +28,4 @@ constructor() { | ||
} | ||
dispatchEvent(event) { | ||
event.target = event.currentTarget = this; | ||
if (this['on' + event.type]) { | ||
@@ -44,3 +38,2 @@ try { | ||
} | ||
const list = this[EVENTS].get(event.type); | ||
@@ -56,3 +49,2 @@ if (list == null) return; | ||
} | ||
addEventListener(type, fn) { | ||
@@ -63,6 +55,4 @@ let events = this[EVENTS].get(type); | ||
} | ||
removeEventListener(type, fn) { | ||
let events = this[EVENTS].get(type); | ||
if (events) { | ||
@@ -73,5 +63,3 @@ const index = events.indexOf(fn); | ||
} | ||
} | ||
function Event(type, target) { | ||
@@ -81,9 +69,8 @@ this.type = type; | ||
this.target = this.currentTarget = this.data = null; | ||
} // this module is used self-referentially on both sides of the | ||
} | ||
// this module is used self-referentially on both sides of the | ||
// thread boundary, but behaves differently in each context. | ||
module.exports = threads.isMainThread ? mainThread() : workerThread(); | ||
const baseUrl = URL.pathToFileURL(process.cwd() + '/'); | ||
function mainThread() { | ||
@@ -111,3 +98,2 @@ /** | ||
let mod; | ||
if (/^data:/.test(url)) { | ||
@@ -118,3 +104,2 @@ mod = url; | ||
} | ||
const worker = new threads.Worker(__filename, { | ||
@@ -143,17 +128,12 @@ workerData: { | ||
} | ||
postMessage(data, transferList) { | ||
this[WORKER].postMessage(data, transferList); | ||
} | ||
terminate() { | ||
this[WORKER].terminate(); | ||
} | ||
} | ||
Worker.prototype.onmessage = Worker.prototype.onerror = Worker.prototype.onclose = null; | ||
return Worker; | ||
} | ||
function workerThread() { | ||
@@ -164,8 +144,10 @@ let { | ||
type | ||
} = threads.workerData; // turn global into a mock WorkerGlobalScope | ||
} = threads.workerData; | ||
if (!mod) return mainThread(); | ||
const self = global.self = global; // enqueue messages to dispatch after modules are loaded | ||
// turn global into a mock WorkerGlobalScope | ||
const self = global.self = global; | ||
// enqueue messages to dispatch after modules are loaded | ||
let q = []; | ||
function flush() { | ||
@@ -178,3 +160,2 @@ const buffered = q; | ||
} | ||
threads.parentPort.on('message', data => { | ||
@@ -189,15 +170,11 @@ const event = new Event('message'); | ||
}); | ||
class WorkerGlobalScope extends EventTarget { | ||
postMessage(data, transferList) { | ||
threads.parentPort.postMessage(data, transferList); | ||
} // Emulates https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/close | ||
} | ||
// Emulates https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/close | ||
close() { | ||
process.exit(); | ||
} | ||
} | ||
let proto = Object.getPrototypeOf(global); | ||
@@ -212,3 +189,2 @@ delete proto.constructor; | ||
const isDataUrl = /^data:/.test(mod); | ||
if (type === 'module') { | ||
@@ -220,3 +196,2 @@ import(mod).catch(err => { | ||
} | ||
console.error(err); | ||
@@ -234,7 +209,5 @@ }).then(flush); | ||
} | ||
Promise.resolve().then(flush); | ||
} | ||
} | ||
function evaluateDataUrl(url, name) { | ||
@@ -248,3 +221,2 @@ const { | ||
} | ||
function parseDataUrl(url) { | ||
@@ -257,3 +229,2 @@ let [m, type, encoding, data] = url.match(/^data: *([^;,]*)(?: *; *([^,]*))? *,(.*)$/) || []; | ||
break; | ||
default: | ||
@@ -260,0 +231,0 @@ throw Error('Unknown Data URL encoding "' + encoding + '"'); |
@@ -136,2 +136,3 @@ /** | ||
let { mod, name, type } = threads.workerData; | ||
if (!mod) return mainThread(); | ||
@@ -138,0 +139,0 @@ // turn global into a mock WorkerGlobalScope |
{ | ||
"name": "web-worker", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Consistent Web Workers in browser and Node.", | ||
"main": "./cjs/node.js", | ||
"browser": "./cjs/browser.js", | ||
"types": "./index.d.ts", | ||
"exports": { | ||
"browser": "./cjs/browser.js", | ||
"node": "./cjs/node.js", | ||
"default": "./cjs/node.js" | ||
"types": "./index.d.ts", | ||
"import": { | ||
"bun": "./browser.js", | ||
"browser": "./browser.js", | ||
"default": "./node.js" | ||
}, | ||
"module": { | ||
"bun": "./browser.js", | ||
"browser": "./browser.js", | ||
"default": "./node.js" | ||
}, | ||
"default": { | ||
"bun": "./cjs/browser.js", | ||
"browser": "./cjs/browser.js", | ||
"default": "./cjs/node.js" | ||
} | ||
}, | ||
"types": "index.d.ts", | ||
"files": [ | ||
@@ -48,12 +61,12 @@ "cjs", | ||
"devDependencies": { | ||
"@babel/cli": "^7.7.7", | ||
"@babel/core": "^7.7.7", | ||
"@babel/plugin-syntax-dynamic-import": "^7.7.4", | ||
"@babel/plugin-transform-modules-commonjs": "^7.7.5", | ||
"@babel/preset-env": "^7.7.7", | ||
"ava": "^2.4.0", | ||
"@babel/cli": "^7.21.0", | ||
"@babel/core": "^7.21.0", | ||
"@babel/plugin-syntax-dynamic-import": "^7.8.3", | ||
"@babel/plugin-transform-modules-commonjs": "7.21", | ||
"ava": "2.4", | ||
"babel-plugin-commonjs-lite": "gist:3e38560d5bed35da1fd708d635e4acb3", | ||
"eslint": "^6.8.0", | ||
"eslint-config-developit": "^1.1.1" | ||
"eslint": "^7.32.0", | ||
"eslint-config-developit": "^1.2.0", | ||
"eslint-plugin-compat": "^4.2.0" | ||
} | ||
} |
31054
457