bare-thread 1.0.0 → 1.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.
Files changed (2) hide show
  1. package/index.js +35 -26
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -5,34 +5,12 @@ const { imports, resolutions, protocol } = module
5
5
 
6
6
  module.exports = exports = class Thread {
7
7
  constructor(entry, opts = {}) {
8
- entry = new URL(entry, module.url)
9
-
10
- const bundle = new Bundle()
11
-
12
- for (const dependency of traverse(
13
- entry,
14
- {
15
- imports,
16
- resolutions,
17
- resolve: traverse.resolve.bare
18
- },
19
- readModule
20
- )) {
21
- const { url, source, imports } = dependency
22
-
23
- bundle.write(url.href, source, {
24
- main: url.href === entry.href,
25
- imports
26
- })
27
- }
28
-
29
- this._thread = new Bare.Thread('bare:/thread.bundle', { ...opts, source: bundle.toBuffer() })
8
+ let source
30
9
 
31
- function readModule(url) {
32
- if (protocol.exists(url)) return protocol.read(url)
10
+ if (Buffer.isBuffer(entry)) source = entry
11
+ else source = Thread.prepare(entry)
33
12
 
34
- return null
35
- }
13
+ this._thread = new Bare.Thread('bare:/thread.bundle', { ...opts, source })
36
14
  }
37
15
 
38
16
  get joined() {
@@ -71,3 +49,34 @@ module.exports = exports = class Thread {
71
49
  exports.isMainThread = Bare.Thread.isMainThread
72
50
 
73
51
  exports.self = Bare.Thread.self
52
+
53
+ exports.prepare = function prepare(entry) {
54
+ entry = new URL(entry, module.url)
55
+
56
+ const bundle = new Bundle()
57
+
58
+ for (const dependency of traverse(
59
+ entry,
60
+ {
61
+ imports,
62
+ resolutions,
63
+ resolve: traverse.resolve.bare
64
+ },
65
+ readModule
66
+ )) {
67
+ const { url, source, imports } = dependency
68
+
69
+ bundle.write(url.href, source, {
70
+ main: url.href === entry.href,
71
+ imports
72
+ })
73
+ }
74
+
75
+ return bundle.toBuffer()
76
+ }
77
+
78
+ function readModule(url) {
79
+ if (protocol.exists(url)) return protocol.read(url)
80
+
81
+ return null
82
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-thread",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Thread support for Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",