bare-module 7.2.0 → 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
@@ -23,6 +23,8 @@ const engines = Bare.versions
23
23
 
24
24
  const cacheOwners = new WeakMap()
25
25
 
26
+ const defaultProtocol = new ModuleProtocol()
27
+
26
28
  function assertProtocol(protocol) {
27
29
  if (ModuleProtocol.isProtocol(protocol)) return protocol
28
30
 
@@ -178,7 +180,7 @@ module.exports = class ModuleLoader {
178
180
  concurrency
179
181
  } = opts
180
182
 
181
- this._protocol = protocol === null ? new ModuleProtocol() : assertProtocol(protocol)
183
+ this._protocol = protocol === null ? defaultProtocol : assertProtocol(protocol)
182
184
  this._defaultType = defaultTypeFor(defaultType)
183
185
  this._builtins = builtinsFor(builtins)
184
186
  this._imports = importsFor(imports)
@@ -188,11 +190,10 @@ module.exports = class ModuleLoader {
188
190
  this._resolutions = resolutionsFor(resolutions)
189
191
  this._manifests = Object.create(null)
190
192
  this._bundles = []
191
- this._patches = null
192
193
  this._visited = new Set()
193
194
  this._packages = new Map()
194
195
  this._prefixes = new Map()
195
- this._answers = { listed: new Map(), resolved: new Map() }
196
+ this._answers = { listed: new Map(), resolved: new Map(), exists: new Map() }
196
197
  this._uninstantiated = []
197
198
  this._pending = null
198
199
 
@@ -202,7 +203,7 @@ module.exports = class ModuleLoader {
202
203
  // other, protocol and builtins and all. The cache is therefore claimed
203
204
  // before anything is read into it, and what it already holds is checked
204
205
  // against the claim.
205
- claimCache(this)
206
+ this._graph = claimCache(this)
206
207
 
207
208
  for (const href in this._cache) {
208
209
  const record = this._cache[href]
@@ -230,9 +231,11 @@ module.exports = class ModuleLoader {
230
231
  }
231
232
 
232
233
  patch(bundle) {
233
- if (this._patches === null) {
234
- this._patches = new Map()
235
- this._protocol = patching(this._protocol, this._patches)
234
+ const graph = this._graph
235
+
236
+ if (graph.patches === null) {
237
+ graph.patches = new Map()
238
+ graph.reader = patching(graph.protocol, graph.patches)
236
239
  }
237
240
 
238
241
  const patched = []
@@ -240,7 +243,7 @@ module.exports = class ModuleLoader {
240
243
  for (const href of bundle.keys()) {
241
244
  const url = new URL(href)
242
245
 
243
- this._patches.set(href, bundle)
246
+ graph.patches.set(href, bundle)
244
247
 
245
248
  this._forget(url)
246
249
 
@@ -384,15 +387,15 @@ module.exports = class ModuleLoader {
384
387
  }
385
388
 
386
389
  _answersFor(protocol) {
387
- return protocol === this._protocol ? this._answers : null
390
+ return protocol === this._graph.reader ? this._answers : null
388
391
  }
389
392
 
390
393
  _protocolFor(url) {
391
394
  const bundle = this._bundleFor(url)
392
395
 
393
- if (bundle === null) return this._protocol
396
+ if (bundle === null) return this._graph.reader
394
397
 
395
- return this._protocol.extend({
398
+ return this._graph.reader.extend({
396
399
  resolve(context, url) {
397
400
  return bundleOwns(bundle, url) ? url : context.resolve(url)
398
401
  },
@@ -402,35 +405,19 @@ module.exports = class ModuleLoader {
402
405
  },
403
406
 
404
407
  exists(context, url) {
405
- if (bundleOwns(bundle, url)) return true
406
-
407
- const exists = context.exists(url)
408
-
409
- if (isThenable(exists)) return exists.then((found) => found || bundle.exists(url.href))
410
-
411
- return exists || bundle.exists(url.href)
408
+ return bundleOwns(bundle, url) ? true : context.exists(url)
412
409
  },
413
410
 
414
411
  existsSync(context, url) {
415
- if (bundleOwns(bundle, url)) return true
416
-
417
- return context.existsSync(url) || bundle.exists(url.href)
412
+ return bundleOwns(bundle, url) ? true : context.existsSync(url)
418
413
  },
419
414
 
420
415
  read(context, url) {
421
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
422
-
423
- const source = context.read(url)
424
-
425
- if (isThenable(source)) return source.then((found) => found || bundle.read(url.href))
426
-
427
- return source || bundle.read(url.href)
416
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.read(url)
428
417
  },
429
418
 
430
419
  readSync(context, url) {
431
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
432
-
433
- return context.readSync(url) || bundle.read(url.href)
420
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.readSync(url)
434
421
  },
435
422
 
436
423
  list(context, url) {
@@ -445,7 +432,7 @@ module.exports = class ModuleLoader {
445
432
 
446
433
  _bundleFor(url) {
447
434
  for (const bundle of this._bundles) {
448
- if (bundle.exists(url.href)) return bundle
435
+ if (bundleOwns(bundle, url)) return bundle
449
436
  }
450
437
 
451
438
  return null
@@ -485,6 +472,7 @@ module.exports = class ModuleLoader {
485
472
 
486
473
  this._answers.listed.delete(href)
487
474
  this._answers.resolved.delete(href)
475
+ this._answers.exists.delete(href)
488
476
 
489
477
  if (this._main !== null && this._main.url.href === href) this._main = null
490
478
 
@@ -561,7 +549,7 @@ module.exports = class ModuleLoader {
561
549
  return traverse.module(entry, source, attributes, null, this._visited, opts)
562
550
  }
563
551
 
564
- *_drive(generator, onChild) {
552
+ *_drive(generator, onChild, onBundle) {
565
553
  let next = generator.next()
566
554
  let probed = null
567
555
 
@@ -594,7 +582,9 @@ module.exports = class ModuleLoader {
594
582
 
595
583
  next = generator.next()
596
584
  } else {
597
- this._ingest(value.dependency)
585
+ const record = this._ingest(value.dependency)
586
+
587
+ if (record instanceof BundleModule) onBundle(record)
598
588
 
599
589
  next = generator.next()
600
590
  }
@@ -606,19 +596,25 @@ module.exports = class ModuleLoader {
606
596
 
607
597
  const queue = [root]
608
598
  const deferred = []
599
+ const bundles = []
609
600
 
610
601
  const onChild = (children, isDeferred) => {
611
602
  if (isDeferred) deferred.push(children)
612
603
  else queue.push(children)
613
604
  }
614
605
 
615
- 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)
616
610
 
617
611
  while (queue.length > 0 || deferred.length > 0) {
618
612
  const generator = queue.length > 0 ? queue.pop() : deferred.shift()
619
613
 
620
- driveSync(this._drive(generator, onChild), protocol, driveLink, answers)
614
+ driveSync(this._drive(generator, onChild, onBundle), protocol, driveLink, answers)
621
615
  }
616
+
617
+ for (const record of bundles) record._mount()
622
618
  }
623
619
 
624
620
  async _link(root, protocol) {
@@ -627,6 +623,9 @@ module.exports = class ModuleLoader {
627
623
  const semaphore = this._concurrency > 0 ? new Semaphore(this._concurrency) : null
628
624
 
629
625
  const deferred = []
626
+ const bundles = []
627
+
628
+ const onBundle = (record) => bundles.push(record)
630
629
 
631
630
  const collect = async (generator) => {
632
631
  const queue = []
@@ -637,9 +636,15 @@ module.exports = class ModuleLoader {
637
636
  }
638
637
 
639
638
  const driveLink = (link) =>
640
- drive(this._drive(link, onChild), protocol, driveLink, semaphore, answers)
641
-
642
- 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
+ )
643
648
 
644
649
  if (queue.length > 0) await Promise.all(queue.map(collect))
645
650
  }
@@ -649,58 +654,50 @@ module.exports = class ModuleLoader {
649
654
  while (deferred.length > 0) {
650
655
  await Promise.all(deferred.splice(0, deferred.length).map(collect))
651
656
  }
657
+
658
+ for (const record of bundles) await record._mountAsync()
652
659
  }
653
660
 
654
- _linkBundle(bundle) {
661
+ _addBundle(bundle) {
655
662
  this._bundles.push(bundle)
656
663
 
657
664
  for (const href in bundle.resolutions) {
665
+ if (bundleCovers(bundle, href) === false) continue
666
+
658
667
  if (href in this._resolutions === false) this._resolutions[href] = bundle.resolutions[href]
659
668
  }
669
+ }
660
670
 
661
- const protocol = this._protocol.extend({
662
- exists(context, url) {
663
- if (bundleOwns(bundle, url)) return true
671
+ _linkBundleSync(bundle) {
672
+ this._linkSync(this._bundleRoot(bundle), this._bundleProtocol(bundle))
673
+ }
664
674
 
665
- const exists = context.exists(url)
675
+ async _linkBundle(bundle) {
676
+ await this._link(this._bundleRoot(bundle), this._bundleProtocol(bundle))
677
+ }
666
678
 
667
- if (isThenable(exists)) return bundle.exists(url.href)
679
+ _bundleRoot(bundle) {
680
+ return this._root(new URL(bundle.main), null, { imports: bundle.imports })
681
+ }
668
682
 
669
- 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)
670
687
  },
671
688
 
672
689
  existsSync(context, url) {
673
- if (bundleOwns(bundle, url)) return true
674
-
675
- const exists = context.existsSync(url)
676
-
677
- if (isThenable(exists)) return bundle.exists(url.href)
678
-
679
- return exists || bundle.exists(url.href)
690
+ return bundleOwns(bundle, url) ? true : context.existsSync(url)
680
691
  },
681
692
 
682
693
  read(context, url) {
683
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
684
-
685
- const source = context.read(url)
686
-
687
- if (isThenable(source)) return bundle.read(url.href)
688
-
689
- return source || bundle.read(url.href)
694
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.read(url)
690
695
  },
691
696
 
692
697
  readSync(context, url) {
693
- if (bundleOwns(bundle, url)) return bundle.read(url.href)
694
-
695
- const source = context.readSync(url)
696
-
697
- if (isThenable(source)) return bundle.read(url.href)
698
-
699
- return source || bundle.read(url.href)
698
+ return bundleOwns(bundle, url) ? bundle.read(url.href) : context.readSync(url)
700
699
  }
701
700
  })
702
-
703
- this._linkSync(this._root(new URL(bundle.main), null, { imports: bundle.imports }), protocol)
704
701
  }
705
702
 
706
703
  _ingest(dependency) {
@@ -860,6 +857,7 @@ module.exports = class ModuleLoader {
860
857
 
861
858
  _resolveSync(specifier, parentURL, condition) {
862
859
  const protocol = this._protocolFor(parentURL)
860
+ const answers = this._answersFor(protocol)
863
861
 
864
862
  const resolver = traverse.resolve.bare(
865
863
  resolveEntry(specifier, condition),
@@ -882,7 +880,7 @@ module.exports = class ModuleLoader {
882
880
  if (manifest === undefined) {
883
881
  const record = this.get(value.package)
884
882
 
885
- const source = record ? record._source.bytes : readSync(protocol, value.package)
883
+ const source = record ? record._source.bytes : readSync(protocol, value.package, answers)
886
884
 
887
885
  manifest = this._manifests[href] = source === null ? null : JSON.parse(source.toString())
888
886
  }
@@ -899,7 +897,7 @@ module.exports = class ModuleLoader {
899
897
  return url
900
898
  }
901
899
 
902
- if (existsSync(protocol, url, condition)) {
900
+ if (existsSync(protocol, url, condition, answers)) {
903
901
  const resolution = protocol.resolveSync(url)
904
902
 
905
903
  this._cacheResolution(parentURL, specifier, condition, resolution)
@@ -1101,9 +1099,9 @@ async function drive(generator, protocol, driveLink, semaphore, answers = null)
1101
1099
 
1102
1100
  try {
1103
1101
  if ('module' in command) {
1104
- value = read(protocol, command.module)
1102
+ value = read(protocol, command.module, answers)
1105
1103
  } else if ('probe' in command) {
1106
- value = protocol.exists(command.probe)
1104
+ value = answer(answers, 'exists', command.probe, protocol.exists(command.probe))
1107
1105
  } else if ('resolution' in command) {
1108
1106
  value = resolve(protocol, command.resolution, answers)
1109
1107
  } else {
@@ -1145,12 +1143,16 @@ function claimCache(loader) {
1145
1143
  const owner = cacheOwners.get(loader._cache)
1146
1144
 
1147
1145
  if (owner === undefined) {
1148
- cacheOwners.set(loader._cache, {
1146
+ const graph = {
1149
1147
  protocol: loader._protocol,
1150
- builtins: loader._builtins
1151
- })
1148
+ builtins: loader._builtins,
1149
+ patches: null,
1150
+ reader: loader._protocol
1151
+ }
1152
+
1153
+ cacheOwners.set(loader._cache, graph)
1152
1154
 
1153
- return
1155
+ return graph
1154
1156
  }
1155
1157
 
1156
1158
  if (owner.protocol !== loader._protocol) {
@@ -1160,6 +1162,8 @@ function claimCache(loader) {
1160
1162
  if (owner.builtins !== loader._builtins) {
1161
1163
  throw errors.CACHE_INCOMPATIBLE('Cache is read with different builtins')
1162
1164
  }
1165
+
1166
+ return owner
1163
1167
  }
1164
1168
 
1165
1169
  function assertCompatible(loader, href, record) {
@@ -1176,14 +1180,14 @@ function assertCompatible(loader, href, record) {
1176
1180
  }
1177
1181
  }
1178
1182
 
1179
- function readSync(protocol, url) {
1180
- if (protocol.existsSync(url) === false) return null
1183
+ function readSync(protocol, url, answers = null) {
1184
+ if (probeSync(protocol, url, answers) === false) return null
1181
1185
 
1182
1186
  return protocol.readSync(url)
1183
1187
  }
1184
1188
 
1185
- function read(protocol, url) {
1186
- const exists = protocol.exists(url)
1189
+ function read(protocol, url, answers = null) {
1190
+ const exists = answer(answers, 'exists', url, protocol.exists(url))
1187
1191
 
1188
1192
  if (isThenable(exists)) {
1189
1193
  return exists.then((exists) => (exists === false ? null : protocol.read(url)))
@@ -1192,10 +1196,32 @@ function read(protocol, url) {
1192
1196
  return exists === false ? null : protocol.read(url)
1193
1197
  }
1194
1198
 
1195
- function existsSync(protocol, url, condition) {
1196
- 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)
1197
1201
 
1198
- return protocol.existsSync(url)
1202
+ try {
1203
+ return listFirstSync(protocol, url) !== null
1204
+ } catch (err) {
1205
+ const known = recall(err, answers, 'listed', url)
1206
+
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
1199
1225
  }
1200
1226
 
1201
1227
  async function exists(protocol, url, condition) {
@@ -1360,12 +1386,14 @@ function toURL(value, base) {
1360
1386
  return URL.parse(value, base) || pathToFileURL(value)
1361
1387
  }
1362
1388
 
1363
- function bundleOwns(bundle, url) {
1389
+ function bundleCovers(bundle, href) {
1364
1390
  const root = bundle.root
1365
1391
 
1366
- if (typeof root === 'string' && url.href.startsWith(root) === false) return false
1392
+ return typeof root !== 'string' || href.startsWith(root)
1393
+ }
1367
1394
 
1368
- return bundle.exists(url.href)
1395
+ function bundleOwns(bundle, url) {
1396
+ return bundleCovers(bundle, url.href) && bundle.exists(url.href)
1369
1397
  }
1370
1398
 
1371
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.0",
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",