bare-module-traverse 2.5.0 → 2.5.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/index.d.ts CHANGED
@@ -35,6 +35,7 @@ type AliasableExtension =
35
35
 
36
36
  interface TraverseOptions extends ResolveOptions {
37
37
  defaultType?: number
38
+ artifacts?: boolean
38
39
  aliases?: Record<string, AliasableExtension>
39
40
  /**
40
41
  * @param entry - The import to resolve, as produced by `bare-module-lexer`.
@@ -83,7 +84,7 @@ declare function traverse(
83
84
  declare function traverse(
84
85
  entry: URL,
85
86
  readModule: (url: URL) => Promise<Buffer | string | null>,
86
- listPrefix?: (url: URL) => AsyncIterable<URL>,
87
+ listPrefix?: (url: URL, expand: boolean) => AsyncIterable<URL>,
87
88
  probeModule?: (url: URL) => Promise<boolean | undefined>,
88
89
  resolveModule?: (url: URL) => Promise<URL>
89
90
  ): AsyncIterable<Dependency>
@@ -92,7 +93,7 @@ declare function traverse(
92
93
  entry: URL,
93
94
  opts: TraverseOptions,
94
95
  readModule: (url: URL) => Buffer | string | null,
95
- listPrefix?: (url: URL) => Iterable<URL>,
96
+ listPrefix?: (url: URL, expand: true) => Iterable<URL>,
96
97
  probeModule?: (url: URL) => boolean | undefined,
97
98
  resolveModule?: (url: URL) => URL
98
99
  ): Iterable<Dependency>
@@ -101,7 +102,7 @@ declare function traverse(
101
102
  entry: URL,
102
103
  opts: TraverseOptions,
103
104
  readModule: (url: URL) => Promise<Buffer | string | null>,
104
- listPrefix?: (url: URL) => AsyncIterable<URL>,
105
+ listPrefix?: (url: URL, expand: true) => AsyncIterable<URL>,
105
106
  probeModule?: (url: URL) => Promise<boolean | undefined>,
106
107
  resolveModule?: (url: URL) => Promise<URL>
107
108
  ): AsyncIterable<Dependency>
package/index.js CHANGED
@@ -43,7 +43,7 @@ module.exports = exports = function traverse(
43
43
 
44
44
  return {
45
45
  *[Symbol.iterator]() {
46
- const artifacts = { addons: [], assets: [] }
46
+ const artifacts = opts.artifacts === false ? null : { addons: [], assets: [] }
47
47
 
48
48
  const visited = opts.visited || new Set()
49
49
 
@@ -66,9 +66,13 @@ module.exports = exports = function traverse(
66
66
  const result = []
67
67
 
68
68
  if (typeof listPrefix === 'function') {
69
- for (const url of listPrefix(value.prefix)) {
69
+ const listed = listPrefix(value.prefix, value.expand)
70
+
71
+ for (const url of listed) {
70
72
  result.push(url)
71
73
  }
74
+
75
+ result.resolved = listed.resolved === true
72
76
  } else {
73
77
  if (readModule(value.prefix) !== null) {
74
78
  result.push(value.prefix)
@@ -101,7 +105,7 @@ module.exports = exports = function traverse(
101
105
  },
102
106
 
103
107
  async *[Symbol.asyncIterator]() {
104
- const artifacts = { addons: [], assets: [] }
108
+ const artifacts = opts.artifacts === false ? null : { addons: [], assets: [] }
105
109
 
106
110
  const visited = opts.visited || new Set()
107
111
 
@@ -124,9 +128,13 @@ module.exports = exports = function traverse(
124
128
  const result = []
125
129
 
126
130
  if (typeof listPrefix === 'function') {
127
- for await (const url of listPrefix(value.prefix)) {
131
+ const listed = listPrefix(value.prefix, value.expand)
132
+
133
+ for await (const url of listed) {
128
134
  result.push(url)
129
135
  }
136
+
137
+ result.resolved = listed.resolved === true
130
138
  } else {
131
139
  if ((await readModule(value.prefix)) !== null) {
132
140
  result.push(value.prefix)
@@ -192,6 +200,9 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
192
200
 
193
201
  if (probed !== undefined) opts = { ...opts, probed: undefined }
194
202
 
203
+ if (opts.packages === undefined) opts = { ...opts, packages: new Map() }
204
+ if (opts.prefixes === undefined) opts = { ...opts, prefixes: new Map() }
205
+
195
206
  attributes = attributes || {}
196
207
 
197
208
  const artifact = asset === true || moduleType(url, attributes, null, opts) === constants.ADDON
@@ -225,20 +236,16 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
225
236
  let info = null
226
237
 
227
238
  if (url.protocol !== 'data:') {
228
- for (const packageURL of lookupPackageScope(url, opts)) {
229
- const source = yield { module: packageURL, artifact: false }
230
-
231
- if (source !== null) {
232
- info = JSON.parse(source)
239
+ const scope = yield* lookupPackage(url, opts)
233
240
 
234
- imports['#package'] = packageURL.href
241
+ if (scope !== null) {
242
+ info = scope.info
235
243
 
236
- yield {
237
- children: exports.package(packageURL, source, artifacts, visited, opts),
238
- deferred: false
239
- }
244
+ imports['#package'] = scope.url.href
240
245
 
241
- break
246
+ yield {
247
+ children: exports.package(scope.url, scope.source, artifacts, visited, opts),
248
+ deferred: false
242
249
  }
243
250
  }
244
251
  }
@@ -266,7 +273,7 @@ exports.module = function* (url, source, attributes, artifacts, visited, opts =
266
273
  ...opts,
267
274
  referrerType: type
268
275
  })
269
- } else if (type === constants.ADDON) {
276
+ } else if (type === constants.ADDON && artifacts !== null) {
270
277
  yield* exports.addons(url, artifacts, visited, opts)
271
278
  }
272
279
  }
@@ -308,7 +315,7 @@ exports.package = function* (url, source, artifacts, visited, opts = {}) {
308
315
  }
309
316
  }
310
317
 
311
- if (info.assets) {
318
+ if (info.assets && artifacts !== null) {
312
319
  yield {
313
320
  children: exports.assets(info.assets, url, artifacts, visited, opts),
314
321
  deferred: false
@@ -349,15 +356,17 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
349
356
  deferred: false
350
357
  }
351
358
  } else if (asset) {
352
- addURL(artifacts.assets, url)
353
-
354
- yield {
355
- children: exports.module(url, null, {}, artifacts, visited, {
356
- ...opts,
357
- asset: true,
358
- referrerType: type
359
- }),
360
- deferred: true
359
+ if (artifacts !== null) {
360
+ addURL(artifacts.assets, url)
361
+
362
+ yield {
363
+ children: exports.module(url, null, {}, artifacts, visited, {
364
+ ...opts,
365
+ asset: true,
366
+ referrerType: type
367
+ }),
368
+ deferred: true
369
+ }
361
370
  }
362
371
  } else if (
363
372
  url.protocol !== builtinProtocol &&
@@ -465,7 +474,8 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
465
474
  builtinProtocol = 'builtin:',
466
475
  linkedProtocol = 'linked:',
467
476
  deferredProtocol = 'deferred:',
468
- deferUnresolved = false
477
+ deferUnresolved = false,
478
+ prefixes = new Map()
469
479
  } = opts
470
480
 
471
481
  const matchedConditions = []
@@ -503,24 +513,56 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
503
513
  resolved = true
504
514
  } else if (condition === 'asset') {
505
515
  const prefix = url
516
+ const expand = artifacts !== null
506
517
 
507
- for (const url of yield { prefix }) {
508
- const resolution = yield* postresolve(url)
518
+ let expanded = prefixes.get(prefix.href)
509
519
 
510
- yield {
511
- children: exports.module(resolution, null, {}, artifacts, visited, {
512
- ...opts,
513
- asset: true
514
- }),
515
- deferred: true
520
+ if (expanded === undefined) {
521
+ const listed = yield { prefix, expand }
522
+
523
+ const urls = []
524
+
525
+ let prefixResolution = null
526
+ let found = false
527
+
528
+ for (const url of listed) {
529
+ found = true
530
+
531
+ if (expand === false) break
532
+
533
+ const resolution = listed.resolved === true ? url : yield* postresolve(url)
534
+
535
+ if (url.href === prefix.href) prefixResolution = resolution
536
+
537
+ yield {
538
+ children: exports.module(resolution, null, {}, artifacts, visited, {
539
+ ...opts,
540
+ asset: true
541
+ }),
542
+ deferred: true
543
+ }
544
+
545
+ urls.push(resolution)
516
546
  }
517
547
 
518
- addURL(artifacts.assets, resolution)
548
+ if (found) {
549
+ expanded = { resolution: prefixResolution || (yield* postresolve(prefix)), urls }
519
550
 
520
- resolved = true
551
+ prefixes.set(prefix.href, expanded)
552
+ }
521
553
  }
522
554
 
523
- if (resolved) addResolution(imports, specifier, matchedConditions, url)
555
+ if (expanded !== undefined) {
556
+ if (expand) {
557
+ for (const url of expanded.urls) addURL(artifacts.assets, url)
558
+ }
559
+
560
+ resolution = expanded.resolution
561
+
562
+ addResolution(imports, specifier, matchedConditions, resolution)
563
+
564
+ resolved = true
565
+ }
524
566
  } else if (
525
567
  condition === 'addon' ||
526
568
  moduleType(url, entry.attributes, null, opts) === constants.ADDON
@@ -578,7 +620,7 @@ function* resolveImport(entry, specifier, condition, parentURL, imports, artifac
578
620
  }
579
621
 
580
622
  if (resolved) {
581
- if (condition === 'addon') addURL(artifacts.addons, resolution)
623
+ if (condition === 'addon' && artifacts !== null) addURL(artifacts.addons, resolution)
582
624
 
583
625
  resolutions++
584
626
  }
@@ -624,8 +666,10 @@ exports.addons = function* (parentURL, artifacts, visited, opts = {}) {
624
666
 
625
667
  prefix.pathname = prefix.pathname.replace(ADDON_EXTENSION, '') + '/'
626
668
 
627
- for (const url of yield { prefix }) {
628
- const resolution = yield* postresolve(url)
669
+ const listed = yield { prefix, expand: true }
670
+
671
+ for (const url of listed) {
672
+ const resolution = listed.resolved === true ? url : yield* postresolve(url)
629
673
 
630
674
  yield {
631
675
  children: exports.module(resolution, null, {}, artifacts, visited, opts),
@@ -690,7 +734,7 @@ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, o
690
734
 
691
735
  const prefix = new URL(patternBase, parentURL)
692
736
 
693
- for (const url of yield { prefix }) {
737
+ for (const url of yield { prefix, expand: true }) {
694
738
  if (patternIndex === -1) {
695
739
  if (patternNegate) removeURL(matches, url)
696
740
  else addURL(matches, url)
@@ -727,6 +771,38 @@ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, o
727
771
  return matches
728
772
  }
729
773
 
774
+ function* lookupPackage(url, opts) {
775
+ const { packages = new Map() } = opts
776
+
777
+ const pending = []
778
+
779
+ for (const packageURL of lookupPackageScope(url, opts)) {
780
+ const scope = packages.get(packageURL.href)
781
+
782
+ if (scope !== undefined) {
783
+ for (const href of pending) packages.set(href, scope)
784
+
785
+ return scope
786
+ }
787
+
788
+ pending.push(packageURL.href)
789
+
790
+ const source = yield { module: packageURL, artifact: false }
791
+
792
+ if (source !== null) {
793
+ const scope = { url: packageURL, source, info: JSON.parse(source) }
794
+
795
+ for (const href of pending) packages.set(href, scope)
796
+
797
+ return scope
798
+ }
799
+ }
800
+
801
+ for (const href of pending) packages.set(href, null)
802
+
803
+ return null
804
+ }
805
+
730
806
  function* postresolve(url) {
731
807
  return (yield { resolution: url }) || url
732
808
  }
@@ -5,6 +5,7 @@ const runtime = require('../runtime')
5
5
  module.exports = function (entry, parentURL, opts = {}) {
6
6
  const { linked = false, host = runtime.host, hosts = [host] } = opts
7
7
 
8
+ let directories = false
8
9
  let extensions
9
10
  let conditions = hosts.map((host) => ['bare', 'node', ...host.split('-')])
10
11
 
@@ -23,6 +24,7 @@ module.exports = function (entry, parentURL, opts = {}) {
23
24
 
24
25
  if (entry.type & lex.constants.ASSET) {
25
26
  conditions = conditions.map((conditions) => ['asset', ...conditions])
27
+ directories = true
26
28
  } else {
27
29
  extensions = ['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.json', '.bare', '.node']
28
30
 
@@ -36,6 +38,7 @@ module.exports = function (entry, parentURL, opts = {}) {
36
38
  return resolve.module(entry.specifier, parentURL, {
37
39
  ...opts,
38
40
  extensions,
39
- conditions
41
+ conditions,
42
+ directories
40
43
  })
41
44
  }
@@ -5,6 +5,7 @@ const runtime = require('../runtime')
5
5
  module.exports = function (entry, parentURL, opts = {}) {
6
6
  const { host = runtime.host, hosts = [host] } = opts
7
7
 
8
+ let directories = false
8
9
  let extensions
9
10
  let conditions = hosts.map((host) => ['node', ...host.split('-')])
10
11
 
@@ -22,6 +23,7 @@ module.exports = function (entry, parentURL, opts = {}) {
22
23
 
23
24
  if (entry.type & lex.constants.ASSET) {
24
25
  conditions = conditions.map((conditions) => ['asset', ...conditions])
26
+ directories = true
25
27
  } else if (entry.type & lex.constants.REQUIRE) {
26
28
  extensions = ['.js', '.json', '.node']
27
29
  conditions = conditions.map((conditions) => ['require', ...conditions])
@@ -32,6 +34,7 @@ module.exports = function (entry, parentURL, opts = {}) {
32
34
  return resolve.module(entry.specifier, parentURL, {
33
35
  ...opts,
34
36
  extensions,
35
- conditions
37
+ conditions,
38
+ directories
36
39
  })
37
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.5.0",
3
+ "version": "2.5.2",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {