bare-thread 1.0.0 → 1.1.1

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