Comparing version 3.0.0 to 4.0.0
@@ -5,4 +5,3 @@ module.exports = ` | ||
self.removeEventListener('message', init); | ||
const __init_uvm = e && (e.__init_uvm || (e.data && e.data.__init_uvm)); | ||
// eslint-disable-next-line no-eval | ||
const __init_uvm = e?.data?.__init_uvm; | ||
(typeof __init_uvm === 'string') && eval(__init_uvm); | ||
@@ -9,0 +8,0 @@ }; |
@@ -30,3 +30,3 @@ const Flatted = require('flatted'), | ||
}.bind(null, __self.postMessage.bind(__self)); | ||
__uvm_addEventListener = __self.addEventListener.bind(__self); | ||
__uvm_on = __self.on.bind(__self); | ||
@@ -36,4 +36,4 @@ ${bridgeClientCode()} | ||
(function (emit, id) { | ||
__uvm_addEventListener("message", function (e) { | ||
const { __emit_uvm, __id_uvm } = e?.data || e || {}; | ||
__uvm_on("message", function (e) { | ||
const { __emit_uvm, __id_uvm } = e || {}; | ||
if (typeof __emit_uvm === 'string' && __id_uvm === id) { | ||
@@ -45,3 +45,3 @@ emit(__emit_uvm); | ||
__uvm_dispatch = null; delete __uvm_dispatch; | ||
__uvm_addEventListener = null; delete __uvm_addEventListener; | ||
__uvm_on = null; delete __uvm_on; | ||
@@ -73,7 +73,7 @@ (function (self, bridge, setTimeout) { | ||
const id = UVM_ID_ + randomNumber(), | ||
{ bootCode, debug, bootTimeout, _sandbox } = options, | ||
// function to forward messages emitted | ||
forwardEmits = (e) => { | ||
// e.data for Web Worker (MessageEvent.data) | ||
const { __emit_uvm, __id_uvm } = e?.data || e || /* istanbul ignore next */ {}; | ||
const { __emit_uvm, __id_uvm } = e; | ||
@@ -107,13 +107,11 @@ /* istanbul ignore next-line */ | ||
// remove event listeners for this sandbox | ||
worker.removeEventListener(MESSAGE, forwardEmits); | ||
worker.removeEventListener(ERROR, forwardErrors); | ||
worker.removeEventListener(MESSAGE_ERROR, forwardErrors); | ||
worker.removeEventListener(EXIT, forwardExit); | ||
worker.off(MESSAGE, forwardEmits); | ||
worker.off(ERROR, forwardErrors); | ||
worker.off(MESSAGE_ERROR, forwardErrors); | ||
worker.off(EXIT, forwardExit); | ||
const terminate = !options._sandbox ? | ||
worker.terminate.bind(worker, callback) : | ||
callback; | ||
(_sandbox ? Promise.resolve() : worker.terminate()) | ||
.then(() => { callback(); }); | ||
worker = null; | ||
terminate && terminate(); | ||
}; | ||
@@ -142,3 +140,3 @@ | ||
// get firmware code string with boot code | ||
firmwareCode = sandboxFirmware(options.bootCode, id, options.debug); | ||
firmwareCode = sandboxFirmware(bootCode, id, debug); | ||
@@ -148,15 +146,14 @@ // start boot timer, stops once we get the load signal, terminate otherwise | ||
terminateWorker(); | ||
callback(new Error(`uvm: boot timed out after ${options.bootTimeout}ms.`)); | ||
}, options.bootTimeout); | ||
callback(new Error(`uvm: boot timed out after ${bootTimeout}ms.`)); | ||
}, bootTimeout); | ||
// if sandbox worker is provided, we simply need to init with firmware code | ||
// @todo validate sandbox type or APIs | ||
if (options._sandbox) { | ||
worker = options._sandbox; | ||
if (_sandbox) { | ||
worker = _sandbox; | ||
/* istanbul ignore else */ | ||
if (typeof worker.addEventListener !== 'function') { | ||
// add event listener methods for Node.js worker | ||
worker.addEventListener = worker.on.bind(worker); | ||
worker.removeEventListener = worker.off.bind(worker); | ||
// add event listener methods for Web worker | ||
/* istanbul ignore next-line */ | ||
if (typeof worker.on !== 'function') { | ||
Worker.attachNodeStyleListener(worker); | ||
} | ||
@@ -177,6 +174,6 @@ | ||
// from worker (is removed on disconnect) | ||
worker.addEventListener(MESSAGE, forwardEmits); | ||
worker.addEventListener(ERROR, forwardErrors); | ||
worker.addEventListener(MESSAGE_ERROR, forwardErrors); | ||
worker.addEventListener(EXIT, forwardExit); | ||
worker.on(MESSAGE, forwardEmits); | ||
worker.on(ERROR, forwardErrors); | ||
worker.on(MESSAGE_ERROR, forwardErrors); | ||
worker.on(EXIT, forwardExit); | ||
@@ -183,0 +180,0 @@ // equip bridge to disconnect (i.e. terminate the worker) |
@@ -13,25 +13,50 @@ /* istanbul ignore file */ | ||
window.URL.revokeObjectURL(firmwareObjectURL); | ||
WebWorker.attachNodeStyleListener(this, this.worker); | ||
} | ||
addEventListener (name, listener) { | ||
this.worker.addEventListener(name, listener); | ||
postMessage (data) { | ||
this.worker.postMessage(data); | ||
} | ||
removeEventListener (name, listener) { | ||
this.worker.removeEventListener(name, listener); | ||
} | ||
terminate () { | ||
if (this.worker) { | ||
this.worker.terminate(); | ||
this.worker = null; | ||
} | ||
postMessage (data) { | ||
this.worker.postMessage(data); | ||
return Promise.resolve(); | ||
} | ||
terminate (callback) { | ||
if (!this.worker) { return; } | ||
static attachNodeStyleListener (self, worker) { | ||
function extract (name, event) { | ||
switch (name) { | ||
case 'message': | ||
return event.data; | ||
case 'error': | ||
return event.error || new Error(event.message); | ||
case 'unhandledrejection': | ||
return event.reason; | ||
default: | ||
throw new Error('uvm: Unknown event type'); | ||
} | ||
} | ||
this.worker.terminate(); | ||
this.worker = null; | ||
callback && callback(); | ||
if (!worker) { | ||
worker = self; | ||
} | ||
self.on = function (name, listener) { | ||
worker.addEventListener(name, function (event) { | ||
listener(extract(name, event)); | ||
}); | ||
}; | ||
self.off = worker.removeEventListener.bind(worker); | ||
} | ||
static __self = 'self'; | ||
static __self = `(function () { | ||
function ${WebWorker.attachNodeStyleListener.toString()} | ||
attachNodeStyleListener(self); | ||
return self; | ||
}())`; | ||
@@ -53,2 +78,3 @@ static __exceptionHandler = ` | ||
// Instance of PromiseRejectionEvent | ||
if (event.reason) { | ||
@@ -62,2 +88,3 @@ event.preventDefault(); | ||
// Not using 'self.on' since we need the original event object in 'onError'. | ||
self.addEventListener('error', onError); | ||
@@ -64,0 +91,0 @@ self.addEventListener('unhandledrejection', onError); |
const { Worker } = require('worker_threads'); | ||
class NodeWorker { | ||
class NodeWorker extends Worker { | ||
constructor (bootCode, options) { | ||
this.worker = new Worker(bootCode, { | ||
super(bootCode, { | ||
eval: true, | ||
stdout: !options?.debug, | ||
stderr: !options?.debug | ||
stderr: !options?.debug, | ||
execArgv: [ | ||
// TODO: To be removed when support for Node.js v18 is dropped | ||
'--experimental-global-webcrypto' | ||
] | ||
}); | ||
} | ||
addEventListener (name, listener) { | ||
this.worker.on(name, listener); | ||
} | ||
removeEventListener (name, listener) { | ||
this.worker.off(name, listener); | ||
} | ||
postMessage (data) { | ||
this.worker.postMessage(data); | ||
} | ||
terminate (callback) { | ||
/* istanbul ignore next-line */ | ||
if (!this.worker) { return; } | ||
this.worker.terminate().then(() => { | ||
callback && callback(); | ||
}); | ||
this.worker = null; | ||
} | ||
static __self = 'require("worker_threads").parentPort'; | ||
@@ -35,0 +17,0 @@ |
{ | ||
"name": "uvm", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Universal Virtual Machine for Node and Browser", | ||
@@ -49,3 +49,3 @@ "author": "Postman Inc.", | ||
"eslint-plugin-jsdoc": "^47.0.2", | ||
"eslint-plugin-mocha": "^10.4.3", | ||
"eslint-plugin-mocha": "^10.5.0", | ||
"eslint-plugin-n": "^16.6.2", | ||
@@ -55,3 +55,3 @@ "eslint-plugin-security": "^2.1.1", | ||
"jsdoc": "^4.0.3", | ||
"karma": "^6.4.0", | ||
"karma": "^6.4.4", | ||
"karma-browserify": "^8.1.0", | ||
@@ -61,3 +61,3 @@ "karma-chrome-launcher": "^3.1.1", | ||
"karma-mocha-reporter": "^2.2.5", | ||
"mocha": "^10.4.0", | ||
"mocha": "^10.7.3", | ||
"nyc": "^15.1.0", | ||
@@ -71,4 +71,4 @@ "packity": "^0.3.4", | ||
"engines": { | ||
"node": ">=16" | ||
"node": ">=18" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40323
527