bare-worker 4.0.0 → 4.1.0
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/global.js +4 -0
- package/index.js +11 -8
- package/lib/errors.js +4 -5
- package/lib/message-port.js +3 -11
- package/lib/preloads.js +5 -3
- package/lib/worker-thread.js +6 -13
- package/package.json +5 -4
package/global.js
ADDED
package/index.js
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
|
+
const Thread = require('bare-thread')
|
|
1
2
|
const Channel = require('bare-channel')
|
|
2
3
|
const MessageChannel = require('./lib/message-channel')
|
|
3
4
|
const MessagePort = require('./lib/message-port')
|
|
4
5
|
const constants = require('./lib/constants')
|
|
5
6
|
const preloads = require('./lib/preloads')
|
|
6
|
-
|
|
7
|
+
|
|
8
|
+
const worker = Thread.prepare(require.resolve('./lib/worker-thread'), { shared: true })
|
|
7
9
|
|
|
8
10
|
module.exports = exports = class Worker extends MessagePort {
|
|
9
|
-
constructor(
|
|
11
|
+
constructor(entry, opts = {}) {
|
|
12
|
+
const { workerData } = opts
|
|
13
|
+
|
|
10
14
|
const channel = new Channel({ interfaces: [MessagePort] })
|
|
11
15
|
|
|
12
16
|
super(channel)
|
|
13
17
|
|
|
14
18
|
this._state = constants.state.REFED
|
|
15
19
|
|
|
16
|
-
this._thread = new Thread(
|
|
20
|
+
this._thread = new Thread(worker, {
|
|
17
21
|
data: {
|
|
22
|
+
source: Thread.prepare(entry, { shared: true }),
|
|
18
23
|
channel: channel.handle,
|
|
19
|
-
|
|
20
|
-
data: opts.workerData,
|
|
21
|
-
imports: module.imports,
|
|
24
|
+
workerData,
|
|
22
25
|
preloads
|
|
23
26
|
}
|
|
24
27
|
})
|
|
@@ -72,6 +75,6 @@ exports.parentPort = null
|
|
|
72
75
|
|
|
73
76
|
exports.workerData = null
|
|
74
77
|
|
|
75
|
-
exports.preload = function preload(
|
|
76
|
-
preloads.
|
|
78
|
+
exports.preload = function preload(entry) {
|
|
79
|
+
preloads.set(entry, Thread.prepare(entry, { shared: true }))
|
|
77
80
|
}
|
package/lib/errors.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
module.exports = class WorkerError extends Error {
|
|
2
|
-
constructor(msg,
|
|
2
|
+
constructor(msg, fn = WorkerError, code = fn.name) {
|
|
3
3
|
super(`${code}: ${msg}`)
|
|
4
|
+
|
|
4
5
|
this.code = code
|
|
5
6
|
|
|
6
|
-
if (Error.captureStackTrace)
|
|
7
|
-
Error.captureStackTrace(this, fn)
|
|
8
|
-
}
|
|
7
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, fn)
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
get name() {
|
|
@@ -13,6 +12,6 @@ module.exports = class WorkerError extends Error {
|
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
static ALREADY_STARTED(msg) {
|
|
16
|
-
return new WorkerError(msg,
|
|
15
|
+
return new WorkerError(msg, WorkerError.ALREADY_STARTED)
|
|
17
16
|
}
|
|
18
17
|
}
|
package/lib/message-port.js
CHANGED
|
@@ -13,10 +13,7 @@ module.exports = exports = class MessagePort extends EventEmitter {
|
|
|
13
13
|
this._port = null
|
|
14
14
|
this._exitCode = 0
|
|
15
15
|
|
|
16
|
-
this.on('newListener', this._onnewlistener).on(
|
|
17
|
-
'removeListener',
|
|
18
|
-
this._onremovelistener
|
|
19
|
-
)
|
|
16
|
+
this.on('newListener', this._onnewlistener).on('removeListener', this._onremovelistener)
|
|
20
17
|
}
|
|
21
18
|
|
|
22
19
|
get detached() {
|
|
@@ -44,10 +41,7 @@ module.exports = exports = class MessagePort extends EventEmitter {
|
|
|
44
41
|
}
|
|
45
42
|
|
|
46
43
|
postMessage(message, transferList) {
|
|
47
|
-
this._write(
|
|
48
|
-
{ type: constants.message.MESSAGE, value: message },
|
|
49
|
-
{ transfer: transferList }
|
|
50
|
-
)
|
|
44
|
+
this._write({ type: constants.message.MESSAGE, value: message }, { transfer: transferList })
|
|
51
45
|
}
|
|
52
46
|
|
|
53
47
|
close() {
|
|
@@ -74,9 +68,7 @@ module.exports = exports = class MessagePort extends EventEmitter {
|
|
|
74
68
|
|
|
75
69
|
[Symbol.for('bare.detach')]() {
|
|
76
70
|
if (this._state & constants.state.STARTED) {
|
|
77
|
-
throw errors.ALREADY_STARTED(
|
|
78
|
-
'Worker has already started receiving messages'
|
|
79
|
-
)
|
|
71
|
+
throw errors.ALREADY_STARTED('Worker has already started receiving messages')
|
|
80
72
|
}
|
|
81
73
|
|
|
82
74
|
this._state |= constants.state.DETACHED
|
package/lib/preloads.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const
|
|
1
|
+
const Thread = require('bare-thread')
|
|
2
2
|
|
|
3
|
-
const preloads = new
|
|
3
|
+
const preloads = new Map()
|
|
4
4
|
|
|
5
5
|
if (
|
|
6
6
|
Thread.self &&
|
|
@@ -9,7 +9,9 @@ if (
|
|
|
9
9
|
typeof Thread.self.data.preloads === 'object' &&
|
|
10
10
|
Thread.self.data.preloads !== null
|
|
11
11
|
) {
|
|
12
|
-
for (const
|
|
12
|
+
for (const [entry, source] of Thread.self.data.preloads) {
|
|
13
|
+
preloads.set(entry, source)
|
|
14
|
+
}
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
module.exports = preloads
|
package/lib/worker-thread.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
+
const Thread = require('bare-thread')
|
|
1
2
|
const Channel = require('bare-channel')
|
|
2
3
|
const Module = require('bare-module')
|
|
3
|
-
const os = require('bare-os')
|
|
4
|
-
const url = require('bare-url')
|
|
5
4
|
const MessagePort = require('./message-port')
|
|
6
5
|
const worker = require('..')
|
|
7
|
-
const { Thread } = Bare
|
|
8
6
|
|
|
9
|
-
const { channel: handle,
|
|
10
|
-
|
|
11
|
-
for (const preload of preloads) require(preload)
|
|
7
|
+
const { source, channel: handle, workerData, preloads } = Thread.self.data
|
|
12
8
|
|
|
13
9
|
const channel = Channel.from(handle, { interfaces: [MessagePort] })
|
|
14
10
|
|
|
@@ -20,14 +16,11 @@ Bare.on('newListener', onnewlistener)
|
|
|
20
16
|
worker.parentPort = new MessagePort(channel)
|
|
21
17
|
worker.parentPort._online()
|
|
22
18
|
|
|
23
|
-
worker.workerData =
|
|
19
|
+
worker.workerData = workerData
|
|
20
|
+
|
|
21
|
+
for (const [entry, source] of preloads) Module.load(new URL(entry), source)
|
|
24
22
|
|
|
25
|
-
Module.load(
|
|
26
|
-
Module.resolve(filename, url.pathToFileURL(os.cwd() + '/'), {
|
|
27
|
-
imports
|
|
28
|
-
}),
|
|
29
|
-
{ imports }
|
|
30
|
-
)
|
|
23
|
+
Module.load(new URL('bare:/worker.bundle'), source)
|
|
31
24
|
|
|
32
25
|
function onnewlistener(name, fn) {
|
|
33
26
|
if (fn === onremovelistener || fn === onerror) return
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-worker",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Higher-level worker threads for JavaScript",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
7
7
|
"./package": "./package.json",
|
|
8
|
+
"./global": "./global.js",
|
|
8
9
|
"./constants": "./lib/constants.js",
|
|
9
10
|
"./errors": "./lib/errors.js",
|
|
10
11
|
"./message-channel": "./lib/message-channel.js",
|
|
@@ -12,6 +13,7 @@
|
|
|
12
13
|
},
|
|
13
14
|
"files": [
|
|
14
15
|
"index.js",
|
|
16
|
+
"global.js",
|
|
15
17
|
"lib"
|
|
16
18
|
],
|
|
17
19
|
"scripts": {
|
|
@@ -31,12 +33,11 @@
|
|
|
31
33
|
"bare-channel": "^5.1.5",
|
|
32
34
|
"bare-events": "^2.2.1",
|
|
33
35
|
"bare-module": "^6.0.1",
|
|
34
|
-
"bare-
|
|
35
|
-
"bare-url": "^2.0.1"
|
|
36
|
+
"bare-thread": "^1.1.3"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"brittle": "^3.2.1",
|
|
39
40
|
"prettier": "^3.4.1",
|
|
40
|
-
"prettier-config-
|
|
41
|
+
"prettier-config-holepunch": "^2.0.0"
|
|
41
42
|
}
|
|
42
43
|
}
|