|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +if (!process.features.experimental_workers) { |
| 4 | + throw new Error('Experimental workers are disabled'); |
| 5 | +} |
| 6 | + |
| 7 | +const util = require('util'); |
| 8 | +const assert = require('assert'); |
| 9 | +const EventEmitter = require('events'); |
| 10 | +const WorkerContextBinding = process.binding('WorkerContext'); |
| 11 | +const JSONStringify = function(value) { |
| 12 | + if (value === undefined) value = null; |
| 13 | + return JSON.stringify(value); |
| 14 | +}; |
| 15 | +const JSONParse = JSON.parse; |
| 16 | +const EMPTY_ARRAY = []; |
| 17 | + |
| 18 | +const workerContextSymbol = Symbol('workerContext'); |
| 19 | +const installEventsSymbol = Symbol('installEvents'); |
| 20 | +const checkAliveSymbol = Symbol('checkAlive'); |
| 21 | +const initSymbol = WorkerContextBinding.initSymbol; |
| 22 | + |
| 23 | +const builtinErrorTypes = new Map([ |
| 24 | + Error, SyntaxError, RangeError, URIError, TypeError, EvalError, ReferenceError |
| 25 | +].map(function(Type) { |
| 26 | + return [Type.name, Type]; |
| 27 | +})); |
| 28 | + |
| 29 | +const Worker = WorkerContextBinding.JsConstructor; |
| 30 | +util.inherits(Worker, EventEmitter); |
| 31 | + |
| 32 | +Worker.prototype[initSymbol] = function(entryModulePath, options) { |
| 33 | + if (typeof entryModulePath !== 'string') |
| 34 | + throw new TypeError('entryModulePath must be a string'); |
| 35 | + EventEmitter.call(this); |
| 36 | + options = Object(options); |
| 37 | + var keepAlive = options.keepAlive === undefined ? true : !!options.keepAlive; |
| 38 | + this[workerContextSymbol] = |
| 39 | + new WorkerContextBinding.WorkerContext(entryModulePath, |
| 40 | + {keepAlive: keepAlive}); |
| 41 | + this[installEventsSymbol](); |
| 42 | +}; |
| 43 | + |
| 44 | +Worker.prototype[installEventsSymbol] = function() { |
| 45 | + const workerObject = this; |
| 46 | + const workerContext = this[workerContextSymbol]; |
| 47 | + |
| 48 | + const onerror = function(payload) { |
| 49 | + var ErrorConstructor = builtinErrorTypes.get(payload.builtinType); |
| 50 | + if (typeof ErrorConstructor !== 'function') |
| 51 | + ErrorConstructor = Error; |
| 52 | + const error = new ErrorConstructor(payload.message); |
| 53 | + error.stack = payload.stack; |
| 54 | + util._extend(error, payload.additionalProperties); |
| 55 | + workerObject.emit('error', error); |
| 56 | + }; |
| 57 | + |
| 58 | + workerContext._onexit = function(exitCode) { |
| 59 | + workerObject[workerContextSymbol] = null; |
| 60 | + workerObject.emit('exit', exitCode); |
| 61 | + }; |
| 62 | + |
| 63 | + workerContext._onmessage = function(payload, messageType) { |
| 64 | + payload = JSONParse(payload); |
| 65 | + switch (messageType) { |
| 66 | + case WorkerContextBinding.USER: |
| 67 | + return workerObject.emit('message', payload); |
| 68 | + case WorkerContextBinding.INTERNAL: |
| 69 | + assert.fail('unreachable'); |
| 70 | + case WorkerContextBinding.EXCEPTION: |
| 71 | + return onerror(payload); |
| 72 | + default: |
| 73 | + assert.fail('unreachable'); |
| 74 | + } |
| 75 | + }; |
| 76 | +}; |
| 77 | + |
| 78 | +Worker.prototype[checkAliveSymbol] = function() { |
| 79 | + if (!this[workerContextSymbol]) |
| 80 | + throw new RangeError('this worker has been terminated'); |
| 81 | +}; |
| 82 | + |
| 83 | +Worker.prototype.postMessage = function(payload) { |
| 84 | + this[checkAliveSymbol](); |
| 85 | + this[workerContextSymbol].postMessage(JSONStringify(payload), |
| 86 | + EMPTY_ARRAY, |
| 87 | + WorkerContextBinding.USER); |
| 88 | +}; |
| 89 | + |
| 90 | +Worker.prototype.terminate = function(callback) { |
| 91 | + this[checkAliveSymbol](); |
| 92 | + var context = this[workerContextSymbol]; |
| 93 | + this[workerContextSymbol] = null; |
| 94 | + if (typeof callback === 'function') { |
| 95 | + this.once('exit', function(exitCode) { |
| 96 | + callback(null, exitCode); |
| 97 | + }); |
| 98 | + } |
| 99 | + context.terminate(); |
| 100 | +}; |
| 101 | + |
| 102 | +Worker.prototype.ref = function() { |
| 103 | + this[checkAliveSymbol](); |
| 104 | + this[workerContextSymbol].ref(); |
| 105 | +}; |
| 106 | + |
| 107 | +Worker.prototype.unref = function() { |
| 108 | + this[checkAliveSymbol](); |
| 109 | + this[workerContextSymbol].unref(); |
| 110 | +}; |
| 111 | + |
| 112 | +if (process.isWorkerInstance) { |
| 113 | + const postMessage = function(payload, transferList, type) { |
| 114 | + if (!Array.isArray(transferList)) |
| 115 | + throw new Error('transferList must be an array'); |
| 116 | + |
| 117 | + WorkerContextBinding.workerWrapper._postMessage(JSONStringify(payload), |
| 118 | + transferList, |
| 119 | + type); |
| 120 | + }; |
| 121 | + const workerFatalError = function(er) { |
| 122 | + const defaultStack = null; |
| 123 | + const defaultMessage = '[toString() conversion failed]'; |
| 124 | + const defaultBuiltinType = 'Error'; |
| 125 | + |
| 126 | + var message = defaultMessage; |
| 127 | + var builtinType = defaultBuiltinType; |
| 128 | + var stack = defaultStack; |
| 129 | + var additionalProperties = {}; |
| 130 | + |
| 131 | + if (er instanceof Error) { |
| 132 | + try { |
| 133 | + builtinType = er.name; |
| 134 | + } catch (ignore) {} |
| 135 | + |
| 136 | + if (typeof builtinType !== 'string') |
| 137 | + builtinType = defaultBuiltinType; |
| 138 | + |
| 139 | + try { |
| 140 | + stack = er.stack; |
| 141 | + } catch (ignore) {} |
| 142 | + |
| 143 | + if (typeof stack !== 'string') |
| 144 | + stack = defaultStack; |
| 145 | + |
| 146 | + try { |
| 147 | + // Get inherited enumerable properties. |
| 148 | + // .name, .stack and .message are all non-enumerable |
| 149 | + for (var key in er) |
| 150 | + additionalProperties[key] = er[key]; |
| 151 | + // The message delivery must always succeed, otherwise the real cause |
| 152 | + // of this fatal error is masked. |
| 153 | + JSONStringify(additionalProperties); |
| 154 | + } catch (e) { |
| 155 | + additionalProperties = {}; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + try { |
| 160 | + if (er instanceof Error) { |
| 161 | + message = er.message; |
| 162 | + if (typeof message !== 'string') |
| 163 | + message = '' + er; |
| 164 | + } else { |
| 165 | + message = '' + er; |
| 166 | + } |
| 167 | + } catch (e) { |
| 168 | + message = defaultMessage; |
| 169 | + } |
| 170 | + |
| 171 | + postMessage({ |
| 172 | + message: message, |
| 173 | + stack: stack, |
| 174 | + additionalProperties: additionalProperties, |
| 175 | + builtinType: builtinType |
| 176 | + }, EMPTY_ARRAY, WorkerContextBinding.EXCEPTION); |
| 177 | + }; |
| 178 | + |
| 179 | + util._extend(Worker, EventEmitter.prototype); |
| 180 | + EventEmitter.call(Worker); |
| 181 | + |
| 182 | + WorkerContextBinding.workerWrapper._onmessage = |
| 183 | + function(payload, messageType) { |
| 184 | + payload = JSONParse(payload); |
| 185 | + switch (messageType) { |
| 186 | + case WorkerContextBinding.USER: |
| 187 | + return Worker.emit('message', payload); |
| 188 | + case WorkerContextBinding.INTERNAL: |
| 189 | + assert.fail('unreachable'); |
| 190 | + case WorkerContextBinding.EXCEPTION: |
| 191 | + assert.fail('unreachable'); |
| 192 | + default: |
| 193 | + assert.fail('unreachable'); |
| 194 | + } |
| 195 | + }; |
| 196 | + |
| 197 | + Worker.postMessage = function(payload) { |
| 198 | + postMessage(payload, EMPTY_ARRAY, WorkerContextBinding.USER); |
| 199 | + }; |
| 200 | + |
| 201 | + Object.defineProperty(Worker, '_workerFatalError', { |
| 202 | + configurable: true, |
| 203 | + writable: false, |
| 204 | + enumerable: false, |
| 205 | + value: workerFatalError |
| 206 | + }); |
| 207 | +} |
| 208 | + |
| 209 | + |
| 210 | +module.exports = Worker; |
0 commit comments