bare-module-traverse 1.0.1 → 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.
package/README.md CHANGED
@@ -50,6 +50,16 @@ for await (const dependency of traverse(new URL('file:///directory/file.js'), re
50
50
 
51
51
  Traverse the module graph rooted at `url`, which must be a WHATWG `URL` instance. `readModule` is called with a `URL` instance for every module to be read and must either return the module source, if it exists, or `null`. `listPrefix` is called with a `URL` instance of every prefix to be listed and must return a list of `URL` instances that have the specified `URL` as a prefix. If not provided, prefixes won't be traversed. If one or both of `readModule` or `listPrefix` returns a promise, synchronous iteration is not supported.
52
52
 
53
+ Options include:
54
+
55
+ ```js
56
+ {
57
+ resolve: resolve.default
58
+ }
59
+ ```
60
+
61
+ Options supported by <https://github.com/holepunchto/bare-module-resolve> and <https://github.com/holepunchto/bare-addon-resolve> may also be specified.
62
+
53
63
  #### `for (const dependency of dependencies)`
54
64
 
55
65
  Synchronously iterate the module graph. Each yielded dependency has the following shape:
@@ -68,6 +78,28 @@ Synchronously iterate the module graph. Each yielded dependency has the followin
68
78
 
69
79
  Asynchronously iterate the module graph. If one or both of `readModule` or `listPrefix` returns promises, these will be awaited. The same comments as `for (const dependency of dependencies)` apply.
70
80
 
81
+ ### Resolution
82
+
83
+ #### `resolve.module`
84
+
85
+ Convenience export from <https://github.com/holepunchto/bare-module-resolve>.
86
+
87
+ #### `resolve.addon`
88
+
89
+ Convenience export from <https://github.com/holepunchto/bare-addon-resolve>.
90
+
91
+ #### `resolve.default`
92
+
93
+ The default resolver, which simply forwards to <https://github.com/holepunchto/bare-module-resolve> and <https://github.com/holepunchto/bare-addon-resolve> with the literal options passed by the caller.
94
+
95
+ #### `resolve.bare`
96
+
97
+ The Bare resolver, which matches the options used by the Bare module system.
98
+
99
+ #### `resolve.node`
100
+
101
+ The Node.js resolver, which matches the options used by the Node.js module system.
102
+
71
103
  ### Algorithm
72
104
 
73
105
  The following generator functions implement the traversal algorithm. To drive the generator functions, a loop like the following can be used:
@@ -117,6 +149,8 @@ Options are the same as `traverse()` for all functions.
117
149
 
118
150
  #### `const generator = traverse.imports(parentURL, source, imports, artifacts, visited[, options])`
119
151
 
152
+ #### `const generator = traverse.prebuilds(packageURL, artifacts, visited[, options])`
153
+
120
154
  #### `const generator = traverse.assets(patterns, parentURL, artifacts, visited[, options])`
121
155
 
122
156
  ## License
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { lookupPackageScope } = require('bare-module-resolve')
2
+ const { lookupPrebuildsScope } = require('bare-addon-resolve')
2
3
  const lex = require('bare-module-lexer')
3
4
  const resolve = require('./lib/resolve')
4
5
  const errors = require('./lib/errors')
@@ -16,7 +17,7 @@ module.exports = exports = function traverse (entry, opts, readModule, listPrefi
16
17
 
17
18
  return {
18
19
  * [Symbol.iterator] () {
19
- const artifacts = createArtifacts()
20
+ const artifacts = { addons: [], assets: [] }
20
21
 
21
22
  const queue = [exports.module(entry, null, artifacts, new Set(), opts)]
22
23
 
@@ -45,7 +46,7 @@ module.exports = exports = function traverse (entry, opts, readModule, listPrefi
45
46
  },
46
47
 
47
48
  async * [Symbol.asyncIterator] () {
48
- const artifacts = createArtifacts()
49
+ const artifacts = { addons: [], assets: [] }
49
50
 
50
51
  const queue = [exports.module(entry, null, artifacts, new Set(), opts)]
51
52
 
@@ -79,19 +80,27 @@ function defaultListPrefix () {
79
80
  return []
80
81
  }
81
82
 
82
- function defaultResolve (entry, parentURL, opts) {
83
- if (entry.type & lex.constants.ADDON) {
84
- return resolve.addon(entry.specifier, parentURL, opts)
85
- } else {
86
- return resolve.module(entry.specifier, parentURL, opts)
83
+ function addURL (array, url) {
84
+ let lo = 0
85
+ let hi = array.length - 1
86
+
87
+ while (lo <= hi) {
88
+ const mid = lo + ((hi - lo) >> 1)
89
+ const found = array[mid]
90
+
91
+ if (found.href === url.href) return
92
+
93
+ if (found.href < url.href) {
94
+ lo = mid + 1
95
+ } else {
96
+ hi = mid - 1
97
+ }
87
98
  }
88
- }
89
99
 
90
- function createArtifacts () {
91
- return { addons: [], assets: [] }
100
+ array.splice(lo, 0, url)
92
101
  }
93
102
 
94
- function addURL (array, url) {
103
+ function removeURL (array, url) {
95
104
  let lo = 0
96
105
  let hi = array.length - 1
97
106
 
@@ -99,7 +108,7 @@ function addURL (array, url) {
99
108
  const mid = lo + ((hi - lo) >> 1)
100
109
  const found = array[mid]
101
110
 
102
- if (found.href === url.href) return
111
+ if (found.href === url.href) break
103
112
 
104
113
  if (found.href < url.href) {
105
114
  lo = mid + 1
@@ -108,7 +117,7 @@ function addURL (array, url) {
108
117
  }
109
118
  }
110
119
 
111
- array.splice(lo, 0, url)
120
+ if (array[lo].href === url.href) array.splice(lo, 1)
112
121
  }
113
122
 
114
123
  exports.resolve = resolve
@@ -163,6 +172,10 @@ exports.package = function * (url, source, artifacts, visited, opts = {}) {
163
172
  if (info) {
164
173
  yield { dependency: { url, source, imports: {} } }
165
174
 
175
+ if (info.addon) {
176
+ yield { children: exports.prebuilds(url, artifacts, visited, opts) }
177
+ }
178
+
166
179
  if (info.assets) {
167
180
  yield { children: exports.assets(info.assets, url, artifacts, visited, opts) }
168
181
  }
@@ -204,7 +217,7 @@ exports.preresolved = function * (url, source, resolutions, artifacts, visited,
204
217
  }
205
218
 
206
219
  exports.imports = function * (parentURL, source, imports, artifacts, visited, opts = {}) {
207
- const { resolve = defaultResolve } = opts
220
+ const { resolve = exports.resolve.default } = opts
208
221
 
209
222
  let yielded = false
210
223
 
@@ -258,13 +271,34 @@ exports.imports = function * (parentURL, source, imports, artifacts, visited, op
258
271
  return yielded
259
272
  }
260
273
 
274
+ exports.prebuilds = function * (packageURL, artifacts, visited, opts = {}) {
275
+ const [prefix = null] = lookupPrebuildsScope(packageURL, opts)
276
+
277
+ if (prefix === null) return false
278
+
279
+ let yielded = false
280
+
281
+ for (const url of yield { prefix }) {
282
+ const source = yield { module: url }
283
+
284
+ if (source !== null) {
285
+ addURL(artifacts.addons, url)
286
+
287
+ yield { children: exports.module(url, source, artifacts, visited, opts) }
288
+
289
+ yielded = true
290
+ }
291
+ }
292
+
293
+ return yielded
294
+ }
295
+
261
296
  exports.assets = function * (patterns, parentURL, artifacts, visited, opts = {}) {
262
- const matches = yield * exports.matches(patterns, parentURL, opts)
297
+ const matches = yield * exports.patternMatches(patterns, parentURL, [], opts)
263
298
 
264
299
  let yielded = false
265
300
 
266
- for (const href of matches) {
267
- const url = new URL(href)
301
+ for (const url of matches) {
268
302
  const source = yield { module: url }
269
303
 
270
304
  if (source !== null) {
@@ -279,10 +313,10 @@ exports.assets = function * (patterns, parentURL, artifacts, visited, opts = {})
279
313
  return yielded
280
314
  }
281
315
 
282
- exports.matches = function * (patterns, parentURL) {
283
- const matches = new Set()
316
+ exports.patternMatches = function * (pattern, parentURL, matches, opts = {}) {
317
+ const { conditions = [] } = opts
284
318
 
285
- for (let pattern of patterns) {
319
+ if (typeof pattern === 'string') {
286
320
  let patternNegate = false
287
321
  let patternBase
288
322
  let patternTrailer
@@ -306,12 +340,26 @@ exports.matches = function * (patterns, parentURL) {
306
340
 
307
341
  for (const url of yield { prefix }) {
308
342
  if (patternIndex === -1) {
309
- if (patternNegate) matches.delete(url.href)
310
- else matches.add(url.href)
343
+ if (patternNegate) removeURL(matches, url)
344
+ else addURL(matches, url)
311
345
  } else if (patternTrailer === '' || url.href.endsWith(patternTrailer)) {
312
- matches.add(url.href)
346
+ addURL(matches, url)
313
347
  } else if (patternNegate) {
314
- matches.delete(url.href)
348
+ removeURL(matches, url)
349
+ }
350
+ }
351
+ } else if (Array.isArray(pattern)) {
352
+ for (const patternValue of pattern) {
353
+ yield * exports.patternMatches(patternValue, parentURL, matches, opts)
354
+ }
355
+ } else if (typeof pattern === 'object' && pattern !== null) {
356
+ const keys = Object.keys(pattern)
357
+
358
+ for (const p of keys) {
359
+ if (p === 'default' || conditions.includes(p)) {
360
+ const patternValue = pattern[p]
361
+
362
+ return yield * exports.patternMatches(patternValue, parentURL, matches, opts)
315
363
  }
316
364
  }
317
365
  }
package/lib/resolve.js CHANGED
@@ -1,2 +1,68 @@
1
+ const lex = require('bare-module-lexer')
2
+
1
3
  exports.module = require('bare-module-resolve').module
2
4
  exports.addon = require('bare-addon-resolve').addon
5
+
6
+ exports.default = function resolve (entry, parentURL, opts) {
7
+ if (entry.type & lex.constants.ADDON) {
8
+ return exports.addon(entry.specifier, parentURL, opts)
9
+ }
10
+
11
+ return exports.module(entry.specifier, parentURL, opts)
12
+ }
13
+
14
+ exports.bare = function resolve (entry, parentURL, opts = {}) {
15
+ const runtime = require('#runtime')
16
+
17
+ const {
18
+ platform = runtime.platform,
19
+ arch = runtime.arch,
20
+ simulator = false
21
+ } = opts
22
+
23
+ let extensions
24
+ let conditions = ['bare', 'node', platform, arch]
25
+
26
+ if (simulator) conditions = [...conditions, 'simulator']
27
+
28
+ if (entry.type & lex.constants.ADDON) {
29
+ extensions = ['.bare', '.node']
30
+ conditions = ['addon', ...conditions]
31
+
32
+ return exports.addon(entry.specifier, parentURL, { extensions, conditions, ...opts })
33
+ }
34
+
35
+ if (entry.type & lex.constants.ASSET) {
36
+ conditions = ['asset', ...conditions]
37
+ } else {
38
+ extensions = ['.js', '.cjs', '.mjs', '.json', '.bare', '.node']
39
+
40
+ if (entry.type & lex.constants.REQUIRE) {
41
+ conditions = ['require', ...conditions]
42
+ } else if (entry.type & lex.constants.IMPORT) {
43
+ conditions = ['import', ...conditions]
44
+ }
45
+ }
46
+
47
+ return exports.module(entry.specifier, parentURL, { extensions, conditions, ...opts })
48
+ }
49
+
50
+ exports.node = function resolve (entry, parentURL, opts) {
51
+ let extensions
52
+ let conditions
53
+
54
+ if (entry.type & lex.constants.ADDON) {
55
+ extensions = ['.node']
56
+
57
+ return exports.addon(entry.specifier, parentURL, { extensions, conditions, ...opts })
58
+ }
59
+
60
+ if (entry.type & lex.constants.REQUIRE) {
61
+ extensions = ['.js', '.json', '.node']
62
+ conditions = ['node', 'require']
63
+ } else if (entry.type & lex.constants.IMPORT) {
64
+ conditions = ['node', 'import']
65
+ }
66
+
67
+ return exports.module(entry.specifier, parentURL, { extensions, conditions, ...opts })
68
+ }
@@ -0,0 +1,3 @@
1
+ /* global Bare */
2
+ exports.platform = Bare.platform
3
+ exports.arch = Bare.arch
@@ -0,0 +1,2 @@
1
+ exports.platform = process.platform
2
+ exports.arch = process.arch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -11,6 +11,12 @@
11
11
  "index.js",
12
12
  "lib"
13
13
  ],
14
+ "imports": {
15
+ "#runtime": {
16
+ "bare": "./lib/runtime/bare.js",
17
+ "node": "./lib/runtime/node.js"
18
+ }
19
+ },
14
20
  "scripts": {
15
21
  "test": "standard && bare test.js"
16
22
  },