-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker-state.js
294 lines (244 loc) · 8.32 KB
/
worker-state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"use strict"
/**
* This is the actual logic for the child. Note that anything here will not
* propagate to the parent without the use of IPC.
*
* Minimal error handling is done beyond invoking the method itself and catching
* `require` errors, because internal errors cannot be reasonably handled.
*
* Note that the numerous seemingly magic numbers really just correspond to
* positional parameter indices (0-based). Each relevant method has descriptive
* names to explain each of the positional parameters. It's to reduce IPC memory
* overhead by saving a lot of JSON space.
*/
const M = require("./enums").Messages
const serializer = require("./serializer")
const ReturnWrap = require("./worker-api").ReturnWrap
const util = require("./util")
// Simple bit mask
const StateError = 0x1
const StateDefer = 0x2
const StateLocked = 0x4
function invoke(method, args) {
util.check(typeof method === "function")
// V8 likes consistent numbers of arguments.
if (args == null) return method()
switch (args.length) {
case 0: return util.check(false)
case 1: return method(args[0])
case 2: return method(args[0], args[1])
case 3: return method(args[0], args[1], args[2])
case 4: return method(args[0], args[1], args[2], args[3])
default: return method.apply(undefined, args)
}
}
function load(state, name) {
util.check(typeof state === "object" && state != null)
util.check(typeof name === "string")
const raw = state.require(name)
if (raw == null || typeof raw !== "function" && typeof raw !== "object") {
state.uncache(name)
throw new TypeError(
`Module '${name}' must only export an object or function value!`
)
}
return raw
}
function emit(state, type, id, value) {
util.check(typeof state === "object" && state != null)
util.check(typeof type === "number")
util.check(typeof id === "number")
if (value instanceof ReturnWrap) {
serializer.send(state, type, id, value.result, value.options)
} else {
serializer.send(state, type, id, value)
}
}
function tryLoad(state, name) {
util.check(typeof state === "object" && state != null)
util.check(typeof name === "string")
try {
const raw = load(state, name)
const exports = Object.create(null)
const lengths = Object.create(null)
for (const key of Object.keys(raw)) {
const method = raw[key]
if (typeof method === "function") {
exports[key] = method
lengths[key] = method.length
}
}
return {exports, lengths}
} catch (e) {
state.mask |= StateError
return {value: e}
}
}
// -> [_, id, module]
// <- [M.Load, id, {...[method]: length}]
// <- [M.Load, id]
// <- [M.Throw, id, type, value?]
// This is deferred to avoid blocking later call requests.
function initLoad(state, id, sendMethods) {
util.check(typeof state === "object" && state != null)
util.check(typeof id === "number")
util.check(typeof sendMethods === "boolean")
state.mask = 0
const name = state.calls[id]
delete state.calls[id]
delete state.timers[id]
const result = tryLoad(state, name)
if (state.mask & StateError) {
emit(state, M.Throw, id, result.value)
} else {
state.cache[name] = result.exports
if (sendMethods) {
state.send([M.Load, id, result.lengths])
} else {
state.send([M.Load, id])
}
}
}
/**
* This is all untrusted, thus the enclosing `try`/`catch`.
*/
function tryInvoke(state, call, id) {
util.check(typeof state === "object" && state != null)
util.check(typeof call === "object" && call != null)
util.check(typeof id === "number")
try {
const result = invoke(call.method, call.args)
const then = result.then
if (typeof result.then === "function") {
then.call(result,
value => emit(state, M.Return, id, value),
err => emit(state, M.Throw, id, err))
state.mask |= StateDefer
return undefined
}
return result
} catch (e) {
state.mask |= StateError
return e
}
}
// -> [M.Invoke, id]
// <- [M.Return, id, type, value?]
// <- [M.Throw, id, type, value?]
function initInvoke(state, id) {
util.check(typeof state === "object" && state != null)
util.check(typeof id === "number")
const call = state.calls[id]
delete state.calls[id]
delete state.timers[id]
state.mask = 0
const result = tryInvoke(state, call, id)
if (state.mask & StateError) {
emit(state, M.Throw, id, result)
} else if (!(state.mask & StateDefer)) {
emit(state, M.Return, id, result)
}
}
// -> [M.Cancel, id]
// <- [M.Cancel, id]
function handleCancel(state, id) {
util.check(typeof state === "object" && state != null)
util.check(typeof id === "number")
state.clear(state.calls[id])
delete state.calls[id]
delete state.timers[id]
state.send([M.Cancel, id])
}
// -> [M.Add, id, type, value?] + socket?
// <- [M.Next, id]
function handleAdd(state, id, message, socket) {
util.check(typeof state === "object" && state != null)
util.check(typeof id === "number")
util.check(Array.isArray(message))
const call = state.calls[id]
if (call == null) return
if (call.args == null) {
call.args = [serializer.read(message, socket)]
} else {
call.args.push(serializer.read(message, socket))
}
state.send([M.Next, id])
}
/**
* A state state manager. Note that this is intended to be subclassed, with the
* following abstract methods:
*
* - `send(message, socket?, options?)` - Use a custom `process.send`.
* - `defer(func)` - Use a custom `setImmediate` to defer execution.
* - `clear(func)` - Use a custom `clearImmediate`.
* - `require(module)` - Use a custom `require`.
* - `uncache(module)` - Uncache a failed `require`.
*/
module.exports = class State {
constructor() {
this.mask = 0
this.cache = Object.create(null)
this.calls = Object.create(null)
this.timers = Object.create(null)
}
send() { throw new ReferenceError("state.send is abstract!") }
defer() { throw new ReferenceError("state.defer is abstract!") }
clear() { throw new ReferenceError("state.clear is abstract!") }
require() { throw new ReferenceError("state.require is abstract!") }
uncache() { throw new ReferenceError("state.uncache is abstract!") }
// die(err) {
// this.mask = StateLocked
// emit(this, M.Error, 0, err)
// }
invoke(message, socket) {
if (this.mask === StateLocked) return
util.check(Array.isArray(message))
// These are standard fields present on all requests
const type = message[0]
const id = message[1]
util.check(typeof type === "number")
util.check(typeof id === "number")
switch (type) {
// -> [M.Cancel, id]
// <- [M.Cancel, id]
case M.Cancel:
handleCancel(this, id)
break
// -> [M.Load, id, module]
// <- [M.Load, id, {...[name]: length}]
// <- [M.Throw, id, type, value?]
case M.Load:
this.calls[id] = message[2]
this.timers[id] = this.defer(() => initLoad(this, id, true))
break
// -> [M.LateLoad, id, module]
// <- [M.LateLoad, id, {...[name]: length}]
// <- [M.Throw, id, type, value?]
case M.LateLoad:
this.calls[id] = message[2]
this.timers[id] = this.defer(() => initLoad(this, id, false))
break
// -> [M.Init, id, module, method]
// <- [M.Next, id]
case M.Init:
this.calls[id] = {
method: this.cache[message[2]][message[3]],
args: undefined,
}
this.send([M.Next, id])
break
// -> [M.Add, id, type, value?] + socket?
// <- [M.Next, id]
case M.Add:
handleAdd(this, id, message, socket)
break
// -> [M.Invoke, id]
// <- [M.Return, id, type, value?]
// <- [M.Throw, id, type, value?]
case M.Invoke:
this.timers[id] = this.defer(() => initInvoke(this, id))
break
default: throw new TypeError(`Invalid message type: ${type}`)
}
}
}