bare-worker 4.1.6 → 4.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +16 -1
- package/lib/message-channel.js +0 -3
- package/lib/message-port.js +4 -3
- package/lib/worker-state.js +13 -23
- package/lib/worker-thread.js +2 -8
- package/package.json +6 -3
package/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const constants = require('./lib/constants')
|
|
|
7
7
|
|
|
8
8
|
const preloads = new Map()
|
|
9
9
|
|
|
10
|
+
let environmentData = new Map()
|
|
10
11
|
let parentPort = null
|
|
11
12
|
let workerData = null
|
|
12
13
|
|
|
@@ -17,6 +18,7 @@ if (WorkerState.parent) {
|
|
|
17
18
|
|
|
18
19
|
parentPort = WorkerState.parent.port
|
|
19
20
|
workerData = WorkerState.parent.data
|
|
21
|
+
environmentData = WorkerState.parent.environmentData
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
const worker = Thread.prepare(require.resolve('./lib/worker-thread'), { shared: true })
|
|
@@ -36,7 +38,8 @@ module.exports = exports = class Worker extends MessagePort {
|
|
|
36
38
|
source: Thread.prepare(entry, { shared: true }),
|
|
37
39
|
channel: channel.handle,
|
|
38
40
|
data: workerData,
|
|
39
|
-
preloads
|
|
41
|
+
preloads,
|
|
42
|
+
environmentData
|
|
40
43
|
}
|
|
41
44
|
})
|
|
42
45
|
|
|
@@ -59,6 +62,10 @@ module.exports = exports = class Worker extends MessagePort {
|
|
|
59
62
|
return this._terminating.promise
|
|
60
63
|
}
|
|
61
64
|
|
|
65
|
+
async [Symbol.asyncDispose]() {
|
|
66
|
+
await this.terminate()
|
|
67
|
+
}
|
|
68
|
+
|
|
62
69
|
[Symbol.for('bare.inspect')]() {
|
|
63
70
|
return {
|
|
64
71
|
__proto__: { constructor: Worker },
|
|
@@ -78,6 +85,14 @@ module.exports = exports = class Worker extends MessagePort {
|
|
|
78
85
|
}
|
|
79
86
|
}
|
|
80
87
|
|
|
88
|
+
exports.setEnvironmentData = function setEnvironmentData(key, value) {
|
|
89
|
+
environmentData.set(key, value)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
exports.getEnvironmentData = function getEnvironmentData(key) {
|
|
93
|
+
return environmentData.get(key)
|
|
94
|
+
}
|
|
95
|
+
|
|
81
96
|
exports.Worker = exports
|
|
82
97
|
|
|
83
98
|
exports.MessageChannel = MessageChannel
|
package/lib/message-channel.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
const Channel = require('bare-channel')
|
|
2
|
-
const WorkerState = require('./worker-state')
|
|
3
2
|
const MessagePort = require('./message-port')
|
|
4
3
|
|
|
5
4
|
module.exports = class MessageChannel {
|
|
@@ -10,5 +9,3 @@ module.exports = class MessageChannel {
|
|
|
10
9
|
this.port2 = new MessagePort(channel)
|
|
11
10
|
}
|
|
12
11
|
}
|
|
13
|
-
|
|
14
|
-
if (WorkerState.parent) module.exports = WorkerState.parent.MessageChannel
|
package/lib/message-port.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const EventEmitter = require('bare-events')
|
|
2
2
|
const Channel = require('bare-channel')
|
|
3
|
-
const WorkerState = require('./worker-state')
|
|
4
3
|
const constants = require('./constants')
|
|
5
4
|
const errors = require('./errors')
|
|
6
5
|
|
|
@@ -59,6 +58,10 @@ module.exports = exports = class MessagePort extends EventEmitter {
|
|
|
59
58
|
this._unref()
|
|
60
59
|
}
|
|
61
60
|
|
|
61
|
+
hasRef() {
|
|
62
|
+
return (this._state & constants.state.REFED) !== 0
|
|
63
|
+
}
|
|
64
|
+
|
|
62
65
|
[Symbol.for('bare.inspect')]() {
|
|
63
66
|
return {
|
|
64
67
|
__proto__: { constructor: MessagePort },
|
|
@@ -215,8 +218,6 @@ module.exports = exports = class MessagePort extends EventEmitter {
|
|
|
215
218
|
static _ports = new Set()
|
|
216
219
|
}
|
|
217
220
|
|
|
218
|
-
if (WorkerState.parent) module.exports = WorkerState.parent.MessagePort
|
|
219
|
-
|
|
220
221
|
Bare.on('beforeExit', (exitCode) => {
|
|
221
222
|
for (const port of exports._ports) port._beforeExit(exitCode)
|
|
222
223
|
})
|
package/lib/worker-state.js
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
1
|
const Channel = require('bare-channel')
|
|
2
|
-
const MessageChannel = require('./message-channel')
|
|
3
2
|
const MessagePort = require('./message-port')
|
|
4
3
|
|
|
5
4
|
const state = Symbol.for('bare.worker.state')
|
|
6
5
|
const kind = Symbol.for('bare.worker.state.kind')
|
|
7
6
|
|
|
8
|
-
class WorkerState {
|
|
7
|
+
module.exports = class WorkerState {
|
|
9
8
|
static get [kind]() {
|
|
10
9
|
return 0 // Compatibility version
|
|
11
10
|
}
|
|
12
11
|
|
|
12
|
+
static get parent() {
|
|
13
|
+
const parent = global[state]
|
|
14
|
+
|
|
15
|
+
if (typeof parent === 'object' && parent !== null && parent[kind] === WorkerState[kind]) {
|
|
16
|
+
return parent
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
|
|
13
22
|
constructor() {
|
|
14
|
-
const { source, preloads, data, channel: handle } = Bare.Thread.self.data
|
|
23
|
+
const { source, preloads, data, environmentData, channel: handle } = Bare.Thread.self.data
|
|
15
24
|
|
|
16
25
|
this.source = source
|
|
17
26
|
this.preloads = preloads
|
|
18
27
|
this.data = Bare.Thread.self.data = data
|
|
28
|
+
this.environmentData = environmentData
|
|
19
29
|
|
|
20
30
|
const channel = Channel.from(handle, { interfaces: [MessagePort] })
|
|
21
31
|
|
|
@@ -28,24 +38,4 @@ class WorkerState {
|
|
|
28
38
|
get [kind]() {
|
|
29
39
|
return WorkerState[kind]
|
|
30
40
|
}
|
|
31
|
-
|
|
32
|
-
get MessageChannel() {
|
|
33
|
-
return MessageChannel
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
get MessagePort() {
|
|
37
|
-
return MessagePort
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
module.exports = exports = WorkerState
|
|
42
|
-
|
|
43
|
-
if (
|
|
44
|
-
typeof global[state] === 'object' &&
|
|
45
|
-
global[state] !== null &&
|
|
46
|
-
global[state][kind] === WorkerState[kind]
|
|
47
|
-
) {
|
|
48
|
-
exports.parent = global[state]
|
|
49
|
-
} else {
|
|
50
|
-
exports.parent = null
|
|
51
41
|
}
|
package/lib/worker-thread.js
CHANGED
|
@@ -8,17 +8,11 @@ Bare.on('newListener', onnewlistener)
|
|
|
8
8
|
.on('uncaughtException', onerror)
|
|
9
9
|
.on('unhandledRejection', onerror)
|
|
10
10
|
|
|
11
|
-
const cache = Object.create(null)
|
|
12
|
-
|
|
13
11
|
for (const [, source] of state.preloads) {
|
|
14
|
-
Module.load(
|
|
15
|
-
new URL(`bare:/worker/preload-${Math.random().toString(16).slice(2)}.bundle`),
|
|
16
|
-
source,
|
|
17
|
-
{ cache }
|
|
18
|
-
)
|
|
12
|
+
Module.load(new URL(`bare:/worker/preload-${Math.random().toString(16).slice(2)}.bundle`), source)
|
|
19
13
|
}
|
|
20
14
|
|
|
21
|
-
Module.load(new URL('bare:/worker.bundle'), state.source
|
|
15
|
+
Module.load(new URL('bare:/worker.bundle'), state.source)
|
|
22
16
|
|
|
23
17
|
function onnewlistener(name, fn) {
|
|
24
18
|
if (fn === onremovelistener || fn === onerror) return
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-worker",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.9",
|
|
4
4
|
"description": "Higher-level worker threads for JavaScript",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
@@ -17,7 +17,9 @@
|
|
|
17
17
|
"lib"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"
|
|
20
|
+
"format": "prettier --write . && lunte --fix",
|
|
21
|
+
"lint": "prettier --check . && lunte",
|
|
22
|
+
"test": "brittle-bare --coverage test.js"
|
|
21
23
|
},
|
|
22
24
|
"repository": {
|
|
23
25
|
"type": "git",
|
|
@@ -33,10 +35,11 @@
|
|
|
33
35
|
"bare-channel": "^5.1.5",
|
|
34
36
|
"bare-events": "^2.2.1",
|
|
35
37
|
"bare-module": "^6.0.1",
|
|
36
|
-
"bare-thread": "^1.
|
|
38
|
+
"bare-thread": "^1.2.2"
|
|
37
39
|
},
|
|
38
40
|
"devDependencies": {
|
|
39
41
|
"brittle": "^3.2.1",
|
|
42
|
+
"lunte": "^1.8.0",
|
|
40
43
|
"prettier": "^3.4.1",
|
|
41
44
|
"prettier-config-holepunch": "^2.0.0"
|
|
42
45
|
}
|