bare-worker 4.1.0 → 4.1.2
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/lib/worker-thread.js +41 -13
- package/package.json +1 -1
package/lib/worker-thread.js
CHANGED
|
@@ -1,26 +1,54 @@
|
|
|
1
|
-
const Thread = require('bare-thread')
|
|
2
1
|
const Channel = require('bare-channel')
|
|
3
2
|
const Module = require('bare-module')
|
|
4
|
-
const
|
|
5
|
-
const worker = require('..')
|
|
3
|
+
const Bundle = require('bare-bundle')
|
|
6
4
|
|
|
7
|
-
const { source, channel: handle, workerData, preloads } = Thread.self.data
|
|
5
|
+
const { source, channel: handle, workerData, preloads } = Bare.Thread.self.data
|
|
8
6
|
|
|
9
|
-
const
|
|
7
|
+
const bundle = Bundle.from(source)
|
|
8
|
+
|
|
9
|
+
const protocol = module.protocol.extend({
|
|
10
|
+
postresolve(context, url) {
|
|
11
|
+
return bundle.exists(url.href) ? url : context.postresolve(url)
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
exists(context, url, type) {
|
|
15
|
+
return bundle.exists(url.href) || context.exists(url, type)
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
read(context, url) {
|
|
19
|
+
return bundle.read(url.href) || context.read(url)
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const { main, imports, resolutions } = bundle
|
|
24
|
+
|
|
25
|
+
const cache = Object.create(null)
|
|
26
|
+
|
|
27
|
+
let Worker
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const resolved = Module.resolve('bare-worker', new URL(main), { protocol, imports, resolutions })
|
|
31
|
+
|
|
32
|
+
Worker = Module.load(resolved, { protocol, cache }).exports
|
|
33
|
+
} catch {
|
|
34
|
+
Worker = require('..')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const channel = Channel.from(handle, { interfaces: [Worker.MessagePort] })
|
|
38
|
+
|
|
39
|
+
Worker.parentPort = new Worker.MessagePort(channel)
|
|
40
|
+
Worker.parentPort._online()
|
|
41
|
+
|
|
42
|
+
Worker.workerData = Bare.Thread.self.data = workerData
|
|
10
43
|
|
|
11
44
|
Bare.on('newListener', onnewlistener)
|
|
12
45
|
.on('removeListener', onremovelistener)
|
|
13
46
|
.on('uncaughtException', onerror)
|
|
14
47
|
.on('unhandledRejection', onerror)
|
|
15
48
|
|
|
16
|
-
|
|
17
|
-
worker.parentPort._online()
|
|
18
|
-
|
|
19
|
-
worker.workerData = workerData
|
|
20
|
-
|
|
21
|
-
for (const [entry, source] of preloads) Module.load(new URL(entry), source)
|
|
49
|
+
for (const [entry, source] of preloads) Module.load(new URL(entry), source, { cache })
|
|
22
50
|
|
|
23
|
-
Module.load(new URL('bare:/worker.bundle'),
|
|
51
|
+
Module.load(new URL('bare:/worker.bundle'), bundle, { cache })
|
|
24
52
|
|
|
25
53
|
function onnewlistener(name, fn) {
|
|
26
54
|
if (fn === onremovelistener || fn === onerror) return
|
|
@@ -43,7 +71,7 @@ function onremovelistener(name, fn) {
|
|
|
43
71
|
}
|
|
44
72
|
|
|
45
73
|
async function onerror(error) {
|
|
46
|
-
await
|
|
74
|
+
await Worker.parentPort._error(error)
|
|
47
75
|
|
|
48
76
|
Bare.exitCode = 1
|
|
49
77
|
}
|