bare-worker 3.1.1 → 3.2.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/index.js CHANGED
@@ -2,6 +2,7 @@ const Channel = require('bare-channel')
2
2
  const MessageChannel = require('./lib/message-channel')
3
3
  const MessagePort = require('./lib/message-port')
4
4
  const constants = require('./lib/constants')
5
+ const preloads = require('./lib/preloads')
5
6
  const { Thread } = Bare
6
7
 
7
8
  module.exports = exports = class Worker extends MessagePort {
@@ -17,7 +18,8 @@ module.exports = exports = class Worker extends MessagePort {
17
18
  channel: channel.handle,
18
19
  filename,
19
20
  data: opts.workerData,
20
- imports: module.imports
21
+ imports: module.imports,
22
+ preloads
21
23
  }
22
24
  })
23
25
 
@@ -69,3 +71,7 @@ exports.isMainThread = Thread.isMainThread
69
71
  exports.parentPort = null
70
72
 
71
73
  exports.workerData = null
74
+
75
+ exports.preload = function preload(filename) {
76
+ preloads.add(filename)
77
+ }
@@ -0,0 +1,15 @@
1
+ const { Thread } = Bare
2
+
3
+ const preloads = new Set()
4
+
5
+ if (
6
+ Thread.self &&
7
+ typeof Thread.self.data === 'object' &&
8
+ Thread.self.data !== null &&
9
+ typeof Thread.self.data.preloads === 'object' &&
10
+ Thread.self.data.preloads !== null
11
+ ) {
12
+ for (const filename of Thread.self.data.preloads) preloads.add(filename)
13
+ }
14
+
15
+ module.exports = preloads
@@ -6,7 +6,9 @@ const MessagePort = require('./message-port')
6
6
  const worker = require('..')
7
7
  const { Thread } = Bare
8
8
 
9
- const { channel: handle, filename, data, imports } = Thread.self.data
9
+ const { channel: handle, filename, data, imports, preloads } = Thread.self.data
10
+
11
+ for (const preload of preloads) require(preload)
10
12
 
11
13
  const channel = Channel.from(handle, { interfaces: [MessagePort] })
12
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-worker",
3
- "version": "3.1.1",
3
+ "version": "3.2.0",
4
4
  "description": "Higher-level worker threads for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "homepage": "https://github.com/holepunchto/bare-worker#readme",
30
30
  "dependencies": {
31
- "bare-channel": "^5.0.5",
31
+ "bare-channel": "^5.1.5",
32
32
  "bare-events": "^2.2.1",
33
33
  "bare-module": "^5.0.0",
34
34
  "bare-os": "^3.0.1",