bare-module 7.2.1 → 7.2.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/lib/loader.js CHANGED
@@ -193,7 +193,7 @@ module.exports = class ModuleLoader {
193
193
  this._visited = new Set()
194
194
  this._packages = new Map()
195
195
  this._prefixes = new Map()
196
- this._answers = { listed: new Map(), resolved: new Map() }
196
+ this._answers = { listed: new Map(), resolved: new Map(), exists: new Map() }
197
197
  this._uninstantiated = []
198
198
  this._pending = null
199
199
 
@@ -405,35 +405,19 @@ module.exports = class ModuleLoader {
405
405
  },
406
406
 
407
407
  exists(context, url) {
408
- if (bundleOwns(bundle, url)) return true
409
-
410
- const exists = context.exists(url)
411
-
412
- if (isThenable(exists)) return exists.then((found) => found || bundle.exists(url.href))
413
-
414
- return exists || bundle.exists(url.href)
408
+ return bundleOwns(bundle, url) ? true : context.exists(url)
415
409
  },
416
410
 
417
411
  existsSync(context, url) {
418
- if (bundleOwns(bundle, url)) return true
419
-
420
- return context.existsSync(url) || bundle.exists(url.href)
412
+ return bundleOwns(bundle, url) ? true : context.existsSync(url)
421
413
  },
422
414
 
423
415
  read(context, url) {
424
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
425
-
426
- const source = context.read(url)
427
-
428
- if (isThenable(source)) return source.then((found) => found || bundle.read(url.href))
429
-
430
- return source || bundle.read(url.href)
416
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.read(url)
431
417
  },
432
418
 
433
419
  readSync(context, url) {
434
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
435
-
436
- return context.readSync(url) || bundle.read(url.href)
420
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.readSync(url)
437
421
  },
438
422
 
439
423
  list(context, url) {
@@ -448,7 +432,7 @@ module.exports = class ModuleLoader {
448
432
 
449
433
  _bundleFor(url) {
450
434
  for (const bundle of this._bundles) {
451
- if (bundle.exists(url.href)) return bundle
435
+ if (bundleOwns(bundle, url)) return bundle
452
436
  }
453
437
 
454
438
  return null
@@ -488,6 +472,7 @@ module.exports = class ModuleLoader {
488
472
 
489
473
  this._answers.listed.delete(href)
490
474
  this._answers.resolved.delete(href)
475
+ this._answers.exists.delete(href)
491
476
 
492
477
  if (this._main !== null && this._main.url.href === href) this._main = null
493
478
 
@@ -564,7 +549,7 @@ module.exports = class ModuleLoader {
564
549
  return traverse.module(entry, source, attributes, null, this._visited, opts)
565
550
  }
566
551
 
567
- *_drive(generator, onChild) {
552
+ *_drive(generator, onChild, onBundle) {
568
553
  let next = generator.next()
569
554
  let probed = null
570
555
 
@@ -597,7 +582,9 @@ module.exports = class ModuleLoader {
597
582
 
598
583
  next = generator.next()
599
584
  } else {
600
- this._ingest(value.dependency)
585
+ const record = this._ingest(value.dependency)
586
+
587
+ if (record instanceof BundleModule) onBundle(record)
601
588
 
602
589
  next = generator.next()
603
590
  }
@@ -609,19 +596,25 @@ module.exports = class ModuleLoader {
609
596
 
610
597
  const queue = [root]
611
598
  const deferred = []
599
+ const bundles = []
612
600
 
613
601
  const onChild = (children, isDeferred) => {
614
602
  if (isDeferred) deferred.push(children)
615
603
  else queue.push(children)
616
604
  }
617
605
 
618
- const driveLink = (link) => driveSync(this._drive(link, onChild), protocol, driveLink, answers)
606
+ const onBundle = (record) => bundles.push(record)
607
+
608
+ const driveLink = (link) =>
609
+ driveSync(this._drive(link, onChild, onBundle), protocol, driveLink, answers)
619
610
 
620
611
  while (queue.length > 0 || deferred.length > 0) {
621
612
  const generator = queue.length > 0 ? queue.pop() : deferred.shift()
622
613
 
623
- driveSync(this._drive(generator, onChild), protocol, driveLink, answers)
614
+ driveSync(this._drive(generator, onChild, onBundle), protocol, driveLink, answers)
624
615
  }
616
+
617
+ for (const record of bundles) record._mount()
625
618
  }
626
619
 
627
620
  async _link(root, protocol) {
@@ -630,6 +623,9 @@ module.exports = class ModuleLoader {
630
623
  const semaphore = this._concurrency > 0 ? new Semaphore(this._concurrency) : null
631
624
 
632
625
  const deferred = []
626
+ const bundles = []
627
+
628
+ const onBundle = (record) => bundles.push(record)
633
629
 
634
630
  const collect = async (generator) => {
635
631
  const queue = []
@@ -640,9 +636,15 @@ module.exports = class ModuleLoader {
640
636
  }
641
637
 
642
638
  const driveLink = (link) =>
643
- drive(this._drive(link, onChild), protocol, driveLink, semaphore, answers)
644
-
645
- await drive(this._drive(generator, onChild), protocol, driveLink, semaphore, answers)
639
+ drive(this._drive(link, onChild, onBundle), protocol, driveLink, semaphore, answers)
640
+
641
+ await drive(
642
+ this._drive(generator, onChild, onBundle),
643
+ protocol,
644
+ driveLink,
645
+ semaphore,
646
+ answers
647
+ )
646
648
 
647
649
  if (queue.length > 0) await Promise.all(queue.map(collect))
648
650
  }
@@ -652,58 +654,50 @@ module.exports = class ModuleLoader {
652
654
  while (deferred.length > 0) {
653
655
  await Promise.all(deferred.splice(0, deferred.length).map(collect))
654
656
  }
657
+
658
+ for (const record of bundles) await record._mountAsync()
655
659
  }
656
660
 
657
- _linkBundle(bundle) {
661
+ _addBundle(bundle) {
658
662
  this._bundles.push(bundle)
659
663
 
660
664
  for (const href in bundle.resolutions) {
665
+ if (bundleCovers(bundle, href) === false) continue
666
+
661
667
  if (href in this._resolutions === false) this._resolutions[href] = bundle.resolutions[href]
662
668
  }
669
+ }
663
670
 
664
- const protocol = this._graph.reader.extend({
665
- exists(context, url) {
666
- if (bundleOwns(bundle, url)) return true
671
+ _linkBundleSync(bundle) {
672
+ this._linkSync(this._bundleRoot(bundle), this._bundleProtocol(bundle))
673
+ }
667
674
 
668
- const exists = context.exists(url)
675
+ async _linkBundle(bundle) {
676
+ await this._link(this._bundleRoot(bundle), this._bundleProtocol(bundle))
677
+ }
669
678
 
670
- if (isThenable(exists)) return bundle.exists(url.href)
679
+ _bundleRoot(bundle) {
680
+ return this._root(new URL(bundle.main), null, { imports: bundle.imports })
681
+ }
671
682
 
672
- return exists || bundle.exists(url.href)
683
+ _bundleProtocol(bundle) {
684
+ return this._graph.reader.extend({
685
+ exists(context, url) {
686
+ return bundleOwns(bundle, url) ? true : context.exists(url)
673
687
  },
674
688
 
675
689
  existsSync(context, url) {
676
- if (bundleOwns(bundle, url)) return true
677
-
678
- const exists = context.existsSync(url)
679
-
680
- if (isThenable(exists)) return bundle.exists(url.href)
681
-
682
- return exists || bundle.exists(url.href)
690
+ return bundleOwns(bundle, url) ? true : context.existsSync(url)
683
691
  },
684
692
 
685
693
  read(context, url) {
686
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
687
-
688
- const source = context.read(url)
689
-
690
- if (isThenable(source)) return bundle.read(url.href)
691
-
692
- return source || bundle.read(url.href)
694
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.read(url)
693
695
  },
694
696
 
695
697
  readSync(context, url) {
696
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
697
-
698
- const source = context.readSync(url)
699
-
700
- if (isThenable(source)) return bundle.read(url.href)
701
-
702
- return source || bundle.read(url.href)
698
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.readSync(url)
703
699
  }
704
700
  })
705
-
706
- this._linkSync(this._root(new URL(bundle.main), null, { imports: bundle.imports }), protocol)
707
701
  }
708
702
 
709
703
  _ingest(dependency) {
@@ -863,6 +857,7 @@ module.exports = class ModuleLoader {
863
857
 
864
858
  _resolveSync(specifier, parentURL, condition) {
865
859
  const protocol = this._protocolFor(parentURL)
860
+ const answers = this._answersFor(protocol)
866
861
 
867
862
  const resolver = traverse.resolve.bare(
868
863
  resolveEntry(specifier, condition),
@@ -885,7 +880,7 @@ module.exports = class ModuleLoader {
885
880
  if (manifest === undefined) {
886
881
  const record = this.get(value.package)
887
882
 
888
- const source = record ? record._source.bytes : readSync(protocol, value.package)
883
+ const source = record ? record._source.bytes : readSync(protocol, value.package, answers)
889
884
 
890
885
  manifest = this._manifests[href] = source === null ? null : JSON.parse(source.toString())
891
886
  }
@@ -902,7 +897,7 @@ module.exports = class ModuleLoader {
902
897
  return url
903
898
  }
904
899
 
905
- if (existsSync(protocol, url, condition)) {
900
+ if (existsSync(protocol, url, condition, answers)) {
906
901
  const resolution = protocol.resolveSync(url)
907
902
 
908
903
  this._cacheResolution(parentURL, specifier, condition, resolution)
@@ -1104,9 +1099,9 @@ async function drive(generator, protocol, driveLink, semaphore, answers = null)
1104
1099
 
1105
1100
  try {
1106
1101
  if ('module' in command) {
1107
- value = read(protocol, command.module)
1102
+ value = read(protocol, command.module, answers)
1108
1103
  } else if ('probe' in command) {
1109
- value = protocol.exists(command.probe)
1104
+ value = answer(answers, 'exists', command.probe, protocol.exists(command.probe))
1110
1105
  } else if ('resolution' in command) {
1111
1106
  value = resolve(protocol, command.resolution, answers)
1112
1107
  } else {
@@ -1185,14 +1180,14 @@ function assertCompatible(loader, href, record) {
1185
1180
  }
1186
1181
  }
1187
1182
 
1188
- function readSync(protocol, url) {
1189
- if (protocol.existsSync(url) === false) return null
1183
+ function readSync(protocol, url, answers = null) {
1184
+ if (probeSync(protocol, url, answers) === false) return null
1190
1185
 
1191
1186
  return protocol.readSync(url)
1192
1187
  }
1193
1188
 
1194
- function read(protocol, url) {
1195
- const exists = protocol.exists(url)
1189
+ function read(protocol, url, answers = null) {
1190
+ const exists = answer(answers, 'exists', url, protocol.exists(url))
1196
1191
 
1197
1192
  if (isThenable(exists)) {
1198
1193
  return exists.then((exists) => (exists === false ? null : protocol.read(url)))
@@ -1201,10 +1196,32 @@ function read(protocol, url) {
1201
1196
  return exists === false ? null : protocol.read(url)
1202
1197
  }
1203
1198
 
1204
- function existsSync(protocol, url, condition) {
1205
- if (condition === 'asset') return listFirstSync(protocol, url) !== null
1199
+ function existsSync(protocol, url, condition, answers = null) {
1200
+ if (condition !== 'asset') return probeSync(protocol, url, answers)
1201
+
1202
+ try {
1203
+ return listFirstSync(protocol, url) !== null
1204
+ } catch (err) {
1205
+ const known = recall(err, answers, 'listed', url)
1206
1206
 
1207
- return protocol.existsSync(url)
1207
+ return known.length > 0
1208
+ }
1209
+ }
1210
+
1211
+ function probeSync(protocol, url, answers) {
1212
+ try {
1213
+ return protocol.existsSync(url)
1214
+ } catch (err) {
1215
+ return recall(err, answers, 'exists', url)
1216
+ }
1217
+ }
1218
+
1219
+ function recall(err, answers, kind, url) {
1220
+ const known = err.code === 'UNEXPECTED_PROMISE' ? answeredSync(answers, kind, url) : undefined
1221
+
1222
+ if (known === undefined) throw err
1223
+
1224
+ return known
1208
1225
  }
1209
1226
 
1210
1227
  async function exists(protocol, url, condition) {
@@ -1369,12 +1386,14 @@ function toURL(value, base) {
1369
1386
  return URL.parse(value, base) || pathToFileURL(value)
1370
1387
  }
1371
1388
 
1372
- function bundleOwns(bundle, url) {
1389
+ function bundleCovers(bundle, href) {
1373
1390
  const root = bundle.root
1374
1391
 
1375
- if (typeof root === 'string' && url.href.startsWith(root) === false) return false
1392
+ return typeof root !== 'string' || href.startsWith(root)
1393
+ }
1376
1394
 
1377
- return bundle.exists(url.href)
1395
+ function bundleOwns(bundle, url) {
1396
+ return bundleCovers(bundle, url.href) && bundle.exists(url.href)
1378
1397
  }
1379
1398
 
1380
1399
  function parentURLFor(referrer, parentURL) {
@@ -7,10 +7,12 @@ module.exports = class BundleModule extends SyntheticModule {
7
7
 
8
8
  this._bundle = null
9
9
  this._mainURL = null
10
+ this._linked = false
11
+ this._linking = null
10
12
  }
11
13
 
12
- _mount() {
13
- if (this._bundle !== null) return
14
+ _open() {
15
+ if (this._bundle !== null) return this._bundle
14
16
 
15
17
  const bundle = Bundle.from(this._source.bytes).mount(this.url.href + '/')
16
18
 
@@ -19,8 +21,34 @@ module.exports = class BundleModule extends SyntheticModule {
19
21
  if (bundle.main) {
20
22
  this._mainURL = new URL(bundle.main)
21
23
 
22
- this._loader._linkBundle(bundle)
24
+ this._loader._addBundle(bundle)
23
25
  }
26
+
27
+ return bundle
28
+ }
29
+
30
+ _mount() {
31
+ if (this._linked) return
32
+
33
+ if (this._open().main) this._loader._linkBundleSync(this._bundle)
34
+
35
+ this._linked = true
36
+ }
37
+
38
+ _mountAsync() {
39
+ if (this._linking === null) this._linking = this._linkAsync()
40
+
41
+ return this._linking
42
+ }
43
+
44
+ _ensureMounted() {
45
+ if (this._linking === null) this._mount()
46
+ }
47
+
48
+ async _linkAsync() {
49
+ if (this._open().main) await this._loader._linkBundle(this._bundle)
50
+
51
+ this._linked = true
24
52
  }
25
53
 
26
54
  _main() {
@@ -28,7 +56,7 @@ module.exports = class BundleModule extends SyntheticModule {
28
56
  }
29
57
 
30
58
  _exportNames(seen = new Set()) {
31
- this._mount()
59
+ this._ensureMounted()
32
60
 
33
61
  const main = this._main()
34
62
 
@@ -36,7 +64,7 @@ module.exports = class BundleModule extends SyntheticModule {
36
64
  }
37
65
 
38
66
  _initialize() {
39
- this._mount()
67
+ this._ensureMounted()
40
68
  }
41
69
 
42
70
  _execute() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "7.2.1",
3
+ "version": "7.2.2",
4
4
  "description": "The module system that powers Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -48,8 +48,8 @@
48
48
  "dependencies": {
49
49
  "bare-bundle": "^1.11.0",
50
50
  "bare-module-lexer": "^1.7.0",
51
- "bare-module-resolve": "^1.12.3",
52
- "bare-module-traverse": "~2.5.7",
51
+ "bare-module-resolve": "^1.12.6",
52
+ "bare-module-traverse": "~2.6.0",
53
53
  "bare-path": "^3.0.0",
54
54
  "bare-type-stripper": "^0.1.2",
55
55
  "bare-url": "^2.0.1",