bare-module 7.2.1 → 7.3.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/lib/loader.d.ts CHANGED
@@ -65,6 +65,12 @@ declare namespace ModuleLoader {
65
65
  * type: 'json' }`.
66
66
  */
67
67
  attributes?: Attributes
68
+
69
+ /**
70
+ * The URL to mount the entry at if it's a bundle, defaulting to the URL of the bundle followed
71
+ * by a `/`. The bundle serves only the modules under it, so `file:///` lets it serve any file.
72
+ */
73
+ mount?: URL | string
68
74
  }
69
75
 
70
76
  export interface ImportOptions extends LinkOptions {}
package/lib/loader.js CHANGED
@@ -190,10 +190,11 @@ module.exports = class ModuleLoader {
190
190
  this._resolutions = resolutionsFor(resolutions)
191
191
  this._manifests = Object.create(null)
192
192
  this._bundles = []
193
+ this._mounts = new Map()
193
194
  this._visited = new Set()
194
195
  this._packages = new Map()
195
196
  this._prefixes = new Map()
196
- this._answers = { listed: new Map(), resolved: new Map() }
197
+ this._answers = { listed: new Map(), resolved: new Map(), exists: new Map() }
197
198
  this._uninstantiated = []
198
199
  this._pending = null
199
200
 
@@ -302,6 +303,8 @@ module.exports = class ModuleLoader {
302
303
  async link(entry, source = null, opts = {}) {
303
304
  assertURL(entry, 'Entry')
304
305
 
306
+ if (opts.mount !== undefined) this._mounts.set(entry.href, mountFor(opts.mount))
307
+
305
308
  if (entry.protocol === 'builtin:') this._lookup(entry)
306
309
  else await this._link(this._root(entry, source, opts), this._protocolFor(entry))
307
310
 
@@ -313,6 +316,8 @@ module.exports = class ModuleLoader {
313
316
  linkSync(entry, source = null, opts = {}) {
314
317
  assertURL(entry, 'Entry')
315
318
 
319
+ if (opts.mount !== undefined) this._mounts.set(entry.href, mountFor(opts.mount))
320
+
316
321
  if (entry.protocol === 'builtin:') this._lookup(entry)
317
322
  else this._linkSync(this._root(entry, source, opts), this._protocolFor(entry))
318
323
 
@@ -405,35 +410,19 @@ module.exports = class ModuleLoader {
405
410
  },
406
411
 
407
412
  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)
413
+ return bundleOwns(bundle, url) ? true : context.exists(url)
415
414
  },
416
415
 
417
416
  existsSync(context, url) {
418
- if (bundleOwns(bundle, url)) return true
419
-
420
- return context.existsSync(url) || bundle.exists(url.href)
417
+ return bundleOwns(bundle, url) ? true : context.existsSync(url)
421
418
  },
422
419
 
423
420
  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)
421
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.read(url)
431
422
  },
432
423
 
433
424
  readSync(context, url) {
434
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
435
-
436
- return context.readSync(url) || bundle.read(url.href)
425
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.readSync(url)
437
426
  },
438
427
 
439
428
  list(context, url) {
@@ -446,9 +435,13 @@ module.exports = class ModuleLoader {
446
435
  })
447
436
  }
448
437
 
438
+ _mountFor(url) {
439
+ return this._mounts.get(url.href) || url.href + '/'
440
+ }
441
+
449
442
  _bundleFor(url) {
450
443
  for (const bundle of this._bundles) {
451
- if (bundle.exists(url.href)) return bundle
444
+ if (bundleOwns(bundle, url)) return bundle
452
445
  }
453
446
 
454
447
  return null
@@ -488,6 +481,7 @@ module.exports = class ModuleLoader {
488
481
 
489
482
  this._answers.listed.delete(href)
490
483
  this._answers.resolved.delete(href)
484
+ this._answers.exists.delete(href)
491
485
 
492
486
  if (this._main !== null && this._main.url.href === href) this._main = null
493
487
 
@@ -564,7 +558,7 @@ module.exports = class ModuleLoader {
564
558
  return traverse.module(entry, source, attributes, null, this._visited, opts)
565
559
  }
566
560
 
567
- *_drive(generator, onChild) {
561
+ *_drive(generator, onChild, onBundle) {
568
562
  let next = generator.next()
569
563
  let probed = null
570
564
 
@@ -597,7 +591,9 @@ module.exports = class ModuleLoader {
597
591
 
598
592
  next = generator.next()
599
593
  } else {
600
- this._ingest(value.dependency)
594
+ const record = this._ingest(value.dependency)
595
+
596
+ if (record instanceof BundleModule) onBundle(record)
601
597
 
602
598
  next = generator.next()
603
599
  }
@@ -609,19 +605,25 @@ module.exports = class ModuleLoader {
609
605
 
610
606
  const queue = [root]
611
607
  const deferred = []
608
+ const bundles = []
612
609
 
613
610
  const onChild = (children, isDeferred) => {
614
611
  if (isDeferred) deferred.push(children)
615
612
  else queue.push(children)
616
613
  }
617
614
 
618
- const driveLink = (link) => driveSync(this._drive(link, onChild), protocol, driveLink, answers)
615
+ const onBundle = (record) => bundles.push(record)
616
+
617
+ const driveLink = (link) =>
618
+ driveSync(this._drive(link, onChild, onBundle), protocol, driveLink, answers)
619
619
 
620
620
  while (queue.length > 0 || deferred.length > 0) {
621
621
  const generator = queue.length > 0 ? queue.pop() : deferred.shift()
622
622
 
623
- driveSync(this._drive(generator, onChild), protocol, driveLink, answers)
623
+ driveSync(this._drive(generator, onChild, onBundle), protocol, driveLink, answers)
624
624
  }
625
+
626
+ for (const record of bundles) record._mount()
625
627
  }
626
628
 
627
629
  async _link(root, protocol) {
@@ -630,6 +632,9 @@ module.exports = class ModuleLoader {
630
632
  const semaphore = this._concurrency > 0 ? new Semaphore(this._concurrency) : null
631
633
 
632
634
  const deferred = []
635
+ const bundles = []
636
+
637
+ const onBundle = (record) => bundles.push(record)
633
638
 
634
639
  const collect = async (generator) => {
635
640
  const queue = []
@@ -640,9 +645,15 @@ module.exports = class ModuleLoader {
640
645
  }
641
646
 
642
647
  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)
648
+ drive(this._drive(link, onChild, onBundle), protocol, driveLink, semaphore, answers)
649
+
650
+ await drive(
651
+ this._drive(generator, onChild, onBundle),
652
+ protocol,
653
+ driveLink,
654
+ semaphore,
655
+ answers
656
+ )
646
657
 
647
658
  if (queue.length > 0) await Promise.all(queue.map(collect))
648
659
  }
@@ -652,58 +663,50 @@ module.exports = class ModuleLoader {
652
663
  while (deferred.length > 0) {
653
664
  await Promise.all(deferred.splice(0, deferred.length).map(collect))
654
665
  }
666
+
667
+ for (const record of bundles) await record._mountAsync()
655
668
  }
656
669
 
657
- _linkBundle(bundle) {
670
+ _addBundle(bundle) {
658
671
  this._bundles.push(bundle)
659
672
 
660
673
  for (const href in bundle.resolutions) {
674
+ if (bundleCovers(bundle, href) === false) continue
675
+
661
676
  if (href in this._resolutions === false) this._resolutions[href] = bundle.resolutions[href]
662
677
  }
678
+ }
663
679
 
664
- const protocol = this._graph.reader.extend({
665
- exists(context, url) {
666
- if (bundleOwns(bundle, url)) return true
680
+ _linkBundleSync(bundle) {
681
+ this._linkSync(this._bundleRoot(bundle), this._bundleProtocol(bundle))
682
+ }
667
683
 
668
- const exists = context.exists(url)
684
+ async _linkBundle(bundle) {
685
+ await this._link(this._bundleRoot(bundle), this._bundleProtocol(bundle))
686
+ }
669
687
 
670
- if (isThenable(exists)) return bundle.exists(url.href)
688
+ _bundleRoot(bundle) {
689
+ return this._root(new URL(bundle.main), null, { imports: bundle.imports })
690
+ }
671
691
 
672
- return exists || bundle.exists(url.href)
692
+ _bundleProtocol(bundle) {
693
+ return this._graph.reader.extend({
694
+ exists(context, url) {
695
+ return bundleOwns(bundle, url) ? true : context.exists(url)
673
696
  },
674
697
 
675
698
  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)
699
+ return bundleOwns(bundle, url) ? true : context.existsSync(url)
683
700
  },
684
701
 
685
702
  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)
703
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.read(url)
693
704
  },
694
705
 
695
706
  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)
707
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.readSync(url)
703
708
  }
704
709
  })
705
-
706
- this._linkSync(this._root(new URL(bundle.main), null, { imports: bundle.imports }), protocol)
707
710
  }
708
711
 
709
712
  _ingest(dependency) {
@@ -863,6 +866,7 @@ module.exports = class ModuleLoader {
863
866
 
864
867
  _resolveSync(specifier, parentURL, condition) {
865
868
  const protocol = this._protocolFor(parentURL)
869
+ const answers = this._answersFor(protocol)
866
870
 
867
871
  const resolver = traverse.resolve.bare(
868
872
  resolveEntry(specifier, condition),
@@ -885,7 +889,7 @@ module.exports = class ModuleLoader {
885
889
  if (manifest === undefined) {
886
890
  const record = this.get(value.package)
887
891
 
888
- const source = record ? record._source.bytes : readSync(protocol, value.package)
892
+ const source = record ? record._source.bytes : readSync(protocol, value.package, answers)
889
893
 
890
894
  manifest = this._manifests[href] = source === null ? null : JSON.parse(source.toString())
891
895
  }
@@ -902,7 +906,7 @@ module.exports = class ModuleLoader {
902
906
  return url
903
907
  }
904
908
 
905
- if (existsSync(protocol, url, condition)) {
909
+ if (existsSync(protocol, url, condition, answers)) {
906
910
  const resolution = protocol.resolveSync(url)
907
911
 
908
912
  this._cacheResolution(parentURL, specifier, condition, resolution)
@@ -1104,9 +1108,9 @@ async function drive(generator, protocol, driveLink, semaphore, answers = null)
1104
1108
 
1105
1109
  try {
1106
1110
  if ('module' in command) {
1107
- value = read(protocol, command.module)
1111
+ value = read(protocol, command.module, answers)
1108
1112
  } else if ('probe' in command) {
1109
- value = protocol.exists(command.probe)
1113
+ value = answer(answers, 'exists', command.probe, protocol.exists(command.probe))
1110
1114
  } else if ('resolution' in command) {
1111
1115
  value = resolve(protocol, command.resolution, answers)
1112
1116
  } else {
@@ -1185,14 +1189,14 @@ function assertCompatible(loader, href, record) {
1185
1189
  }
1186
1190
  }
1187
1191
 
1188
- function readSync(protocol, url) {
1189
- if (protocol.existsSync(url) === false) return null
1192
+ function readSync(protocol, url, answers = null) {
1193
+ if (probeSync(protocol, url, answers) === false) return null
1190
1194
 
1191
1195
  return protocol.readSync(url)
1192
1196
  }
1193
1197
 
1194
- function read(protocol, url) {
1195
- const exists = protocol.exists(url)
1198
+ function read(protocol, url, answers = null) {
1199
+ const exists = answer(answers, 'exists', url, protocol.exists(url))
1196
1200
 
1197
1201
  if (isThenable(exists)) {
1198
1202
  return exists.then((exists) => (exists === false ? null : protocol.read(url)))
@@ -1201,10 +1205,32 @@ function read(protocol, url) {
1201
1205
  return exists === false ? null : protocol.read(url)
1202
1206
  }
1203
1207
 
1204
- function existsSync(protocol, url, condition) {
1205
- if (condition === 'asset') return listFirstSync(protocol, url) !== null
1208
+ function existsSync(protocol, url, condition, answers = null) {
1209
+ if (condition !== 'asset') return probeSync(protocol, url, answers)
1210
+
1211
+ try {
1212
+ return listFirstSync(protocol, url) !== null
1213
+ } catch (err) {
1214
+ const known = recall(err, answers, 'listed', url)
1206
1215
 
1207
- return protocol.existsSync(url)
1216
+ return known.length > 0
1217
+ }
1218
+ }
1219
+
1220
+ function probeSync(protocol, url, answers) {
1221
+ try {
1222
+ return protocol.existsSync(url)
1223
+ } catch (err) {
1224
+ return recall(err, answers, 'exists', url)
1225
+ }
1226
+ }
1227
+
1228
+ function recall(err, answers, kind, url) {
1229
+ const known = err.code === 'UNEXPECTED_PROMISE' ? answeredSync(answers, kind, url) : undefined
1230
+
1231
+ if (known === undefined) throw err
1232
+
1233
+ return known
1208
1234
  }
1209
1235
 
1210
1236
  async function exists(protocol, url, condition) {
@@ -1369,12 +1395,24 @@ function toURL(value, base) {
1369
1395
  return URL.parse(value, base) || pathToFileURL(value)
1370
1396
  }
1371
1397
 
1372
- function bundleOwns(bundle, url) {
1398
+ function mountFor(mount) {
1399
+ const url = typeof mount === 'string' ? URL.parse(mount) : mount
1400
+
1401
+ if (!isURL(url)) {
1402
+ throw new TypeError(`Mount must be a URL. Received type ${typeof mount} (${mount})`)
1403
+ }
1404
+
1405
+ return url.href.endsWith('/') ? url.href : url.href + '/'
1406
+ }
1407
+
1408
+ function bundleCovers(bundle, href) {
1373
1409
  const root = bundle.root
1374
1410
 
1375
- if (typeof root === 'string' && url.href.startsWith(root) === false) return false
1411
+ return typeof root !== 'string' || href.startsWith(root)
1412
+ }
1376
1413
 
1377
- return bundle.exists(url.href)
1414
+ function bundleOwns(bundle, url) {
1415
+ return bundleCovers(bundle, url.href) && bundle.exists(url.href)
1378
1416
  }
1379
1417
 
1380
1418
  function parentURLFor(referrer, parentURL) {
@@ -7,20 +7,48 @@ 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
- const bundle = Bundle.from(this._source.bytes).mount(this.url.href + '/')
17
+ const bundle = Bundle.from(this._source.bytes).mount(this._loader._mountFor(this.url))
16
18
 
17
19
  this._bundle = bundle
18
20
 
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.3.0",
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",