bare-module 7.4.0 → 7.5.1

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
@@ -303,11 +303,32 @@ options = {
303
303
  // `binary` and `text`. A type that is not one of them is turned down rather
304
304
  // than passed over, as is a `type` that is not a string. `imports` is the
305
305
  // specifier of an "imports" map to apply to the module and its dependencies,
306
- // resolved relative to the module calling `require()`.
306
+ // resolved relative to the module calling `require()`. `host` is the host to
307
+ // resolve the addons of the module and its dependencies for, and must be one
308
+ // that loads on any platform, such as `wasi-wasm32`. The conditions of the
309
+ // host, such as `wasi` and `wasm32`, may then also be targeted alongside
310
+ // those of the native host, in the order a package lists them. `wasi` is the
311
+ // specifier of a module providing the WASI capabilities of the WebAssembly
312
+ // addons of the module and its dependencies, in place of the loader's `wasi`
313
+ // option.
307
314
  with: { type: 'json' }
308
315
  }
309
316
  ```
310
317
 
318
+ A package's addons load in one context per loader, being the host and WASI provider it's first imported under. Importing it under another throws.
319
+
320
+ A WASI provider exports a function, as `module.exports` or its default export, which is called with the URL of each WebAssembly addon and returns the options for [`bare-wasi`](https://github.com/holepunchto/bare-wasi):
321
+
322
+ ```js
323
+ const { MemoryDirectory } = require('bare-wasi/memory')
324
+
325
+ module.exports = (url) => ({
326
+ version: 'preview1',
327
+ preopens: { '/data': new MemoryDirectory() },
328
+ random: (data) => crypto.getRandomValues(data)
329
+ })
330
+ ```
331
+
311
332
  ### `require.main`
312
333
 
313
334
  The module representing the entry script where the program was launched.
@@ -597,7 +618,12 @@ options = {
597
618
  // A map of preresolved imports with keys being serialized parent URLs and
598
619
  // values being "imports" maps. Like the cache it is an object or omitted, and
599
620
  // omitting it gives a fresh map scoped to this loader.
600
- resolutions
621
+ resolutions,
622
+ // A function returning the WASI options of a WebAssembly addon, given its
623
+ // URL, for addons not imported with a `wasi` attribute. Without it, they get
624
+ // no capabilities. The options are versioned, so `version` must be
625
+ // `'preview1'`. A cache is read with a single `wasi`, like a single protocol.
626
+ wasi
601
627
  }
602
628
  ```
603
629
 
package/index.d.ts CHANGED
@@ -29,6 +29,17 @@ interface Attributes {
29
29
  * relative to the parent URL.
30
30
  */
31
31
  imports?: string
32
+ /**
33
+ * The host to resolve the addons of the module and its dependencies for, such as `wasi-wasm32`.
34
+ * The conditions of the host may then also be targeted alongside those of the native host, in the
35
+ * order a package lists them.
36
+ */
37
+ host?: string
38
+ /**
39
+ * The specifier of a module providing the WASI capabilities of the WebAssembly addons of the
40
+ * module and its dependencies, resolved relative to the parent URL.
41
+ */
42
+ wasi?: string
32
43
  }
33
44
 
34
45
  interface Module {
package/lib/errors.js CHANGED
@@ -65,6 +65,14 @@ module.exports = class ModuleError extends Error {
65
65
  return new ModuleError(msg, ModuleError.UNEXPECTED_PROMISE)
66
66
  }
67
67
 
68
+ static ADDON_HOST_INCOMPATIBLE(msg) {
69
+ return new ModuleError(msg, ModuleError.ADDON_HOST_INCOMPATIBLE)
70
+ }
71
+
72
+ static ADDON_WASI_INCOMPATIBLE(msg) {
73
+ return new ModuleError(msg, ModuleError.ADDON_WASI_INCOMPATIBLE)
74
+ }
75
+
68
76
  static UNKNOWN_MODULE_TYPE(msg) {
69
77
  return new ModuleError(msg, ModuleError.UNKNOWN_MODULE_TYPE)
70
78
  }
package/lib/loader.d.ts CHANGED
@@ -57,6 +57,7 @@ declare namespace ModuleLoader {
57
57
  imports?: ImportsMap
58
58
  protocol?: Protocol
59
59
  resolutions?: ResolutionsMap
60
+ wasi?: (url: URL) => object
60
61
  }
61
62
 
62
63
  export interface LinkOptions {
package/lib/loader.js CHANGED
@@ -2,6 +2,7 @@ const Semaphore = require('promaphore')
2
2
  const traverse = require('bare-module-traverse')
3
3
  const { startsWithWindowsDriveLetter } = require('bare-module-resolve')
4
4
  const lex = require('bare-module-lexer')
5
+ const hostParser = require('bare-host-parser')
5
6
  const { isURL, pathToFileURL } = require('bare-url')
6
7
  const { urlToPath } = require('./url')
7
8
  const ModuleProtocol = require('./protocol')
@@ -66,6 +67,14 @@ function builtinsFor(builtins) {
66
67
  throw new TypeError(`Builtins must be an object. Received type ${typeof builtins} (${builtins})`)
67
68
  }
68
69
 
70
+ function wasiFor(wasi) {
71
+ if (wasi === undefined || wasi === null) return null
72
+
73
+ if (typeof wasi === 'function') return wasi
74
+
75
+ throw new TypeError(`WASI must be a function. Received type ${typeof wasi} (${wasi})`)
76
+ }
77
+
69
78
  function importsFor(imports) {
70
79
  if (imports === undefined || imports === null) return null
71
80
 
@@ -108,6 +117,23 @@ function* eachURL(urls) {
108
117
  }
109
118
  }
110
119
 
120
+ function withContext(attributes, context) {
121
+ const { host, wasi } = context
122
+
123
+ if (host === null && wasi === null) return attributes
124
+
125
+ attributes = { ...attributes }
126
+
127
+ if (host !== null) attributes.host = host
128
+ if (wasi !== null) attributes.wasi = wasi
129
+
130
+ return attributes
131
+ }
132
+
133
+ function sameContext(a, b) {
134
+ return a.host === b.host && a.wasi === b.wasi
135
+ }
136
+
111
137
  function typeAttributeFor(attributes) {
112
138
  if (attributes === undefined || attributes === null) return undefined
113
139
 
@@ -133,7 +159,8 @@ const loaderOptions = [
133
159
  'defaultType',
134
160
  'imports',
135
161
  'protocol',
136
- 'resolutions'
162
+ 'resolutions',
163
+ 'wasi'
137
164
  ]
138
165
 
139
166
  const recordClasses = {
@@ -177,13 +204,15 @@ module.exports = class ModuleLoader {
177
204
  imports,
178
205
  resolutions,
179
206
  cache,
180
- concurrency
207
+ concurrency,
208
+ wasi
181
209
  } = opts
182
210
 
183
211
  this._protocol = protocol === null ? defaultProtocol : assertProtocol(protocol)
184
212
  this._defaultType = defaultTypeFor(defaultType)
185
213
  this._builtins = builtinsFor(builtins)
186
214
  this._imports = importsFor(imports)
215
+ this._wasi = wasiFor(wasi)
187
216
  this._concurrency = concurrencyFor(concurrency)
188
217
  this._main = null
189
218
  this._cache = cacheFor(cache)
@@ -192,6 +221,9 @@ module.exports = class ModuleLoader {
192
221
  this._bundles = []
193
222
  this._mounts = new Map()
194
223
  this._visited = new Set()
224
+ this._defaultContext = { host: null, wasi: null, visited: this._visited }
225
+ this._contexts = new Map([[JSON.stringify([null, null]), this._defaultContext]])
226
+ this._addonContexts = new Map()
195
227
  this._packages = new Map()
196
228
  this._prefixes = new Map()
197
229
  this._answers = { listed: new Map(), resolved: new Map(), exists: new Map() }
@@ -359,7 +391,10 @@ module.exports = class ModuleLoader {
359
391
  // loader read belongs to what it could reach, so a fork that reaches
360
392
  // elsewhere gets neither the cache it read into nor the resolutions it
361
393
  // resolved through.
362
- const reaches = options.protocol === this._protocol && options.builtins === this._builtins
394
+ const reaches =
395
+ options.protocol === this._protocol &&
396
+ options.builtins === this._builtins &&
397
+ options.wasi === this._wasi
363
398
 
364
399
  if (!reaches) options.cache = opts.cache
365
400
 
@@ -476,9 +511,10 @@ module.exports = class ModuleLoader {
476
511
 
477
512
  delete this._cache[href]
478
513
 
479
- this._visited.delete(href)
480
514
  this._packages.delete(href)
481
515
 
516
+ for (const { visited } of this._contexts.values()) visited.delete(href)
517
+
482
518
  this._answers.listed.delete(href)
483
519
  this._answers.resolved.delete(href)
484
520
  this._answers.exists.delete(href)
@@ -510,13 +546,14 @@ module.exports = class ModuleLoader {
510
546
  return record
511
547
  }
512
548
 
513
- _addonRecord(url) {
549
+ _addonRecord(url, wasi = null) {
514
550
  const source = new ModuleSource({
515
551
  url,
516
552
  type: type.ADDON,
517
553
  source: null,
518
554
  imports: {},
519
- lexer: { imports: [], exports: [] }
555
+ lexer: { imports: [], exports: [] },
556
+ wasi
520
557
  })
521
558
 
522
559
  const record = new AddonModule(this, source)
@@ -544,7 +581,10 @@ module.exports = class ModuleLoader {
544
581
  resolutions: this._resolutions,
545
582
  packages: this._packages,
546
583
  prefixes: this._prefixes,
547
- resolve: traverse.resolve.bare
584
+ contexts: this._contexts,
585
+ addonContexts: this._addonContexts,
586
+ resolve: traverse.resolve.bare,
587
+ loadable: isLoadable
548
588
  }
549
589
 
550
590
  if (this._builtins !== null) opts.builtins = Object.keys(this._builtins)
@@ -618,12 +658,12 @@ module.exports = class ModuleLoader {
618
658
  driveSync(this._drive(link, onChild, onBundle), protocol, driveLink, answers)
619
659
 
620
660
  while (queue.length > 0 || deferred.length > 0) {
621
- const generator = queue.length > 0 ? queue.pop() : deferred.shift()
661
+ const generator = queue.length > 0 ? queue.shift() : deferred.shift()
622
662
 
623
663
  driveSync(this._drive(generator, onChild, onBundle), protocol, driveLink, answers)
624
664
  }
625
665
 
626
- for (const record of bundles) record._mount()
666
+ for (const record of bundles) record._mountSync()
627
667
  }
628
668
 
629
669
  async _link(root, protocol) {
@@ -631,40 +671,32 @@ module.exports = class ModuleLoader {
631
671
 
632
672
  const semaphore = this._concurrency > 0 ? new Semaphore(this._concurrency) : null
633
673
 
674
+ const queue = [root]
634
675
  const deferred = []
635
676
  const bundles = []
636
677
 
637
- const onBundle = (record) => bundles.push(record)
638
-
639
- const collect = async (generator) => {
640
- const queue = []
641
-
642
- const onChild = (children, isDeferred) => {
643
- if (isDeferred) deferred.push(children)
644
- else queue.push(children)
645
- }
678
+ const onChild = (children, isDeferred) => {
679
+ if (isDeferred) deferred.push(children)
680
+ else queue.push(children)
681
+ }
646
682
 
647
- const driveLink = (link) =>
648
- drive(this._drive(link, onChild, onBundle), protocol, driveLink, semaphore, answers)
683
+ const onBundle = (record) => bundles.push(record)
649
684
 
650
- await drive(
651
- this._drive(generator, onChild, onBundle),
652
- protocol,
653
- driveLink,
654
- semaphore,
655
- answers
656
- )
685
+ const driveLink = (link) =>
686
+ drive(this._drive(link, onChild, onBundle), protocol, driveLink, semaphore, answers)
657
687
 
658
- if (queue.length > 0) await Promise.all(queue.map(collect))
659
- }
688
+ const collect = (generator) =>
689
+ drive(this._drive(generator, onChild, onBundle), protocol, driveLink, semaphore, answers)
660
690
 
661
- await collect(root)
691
+ // Each round is driven concurrently, but only once the one before it is
692
+ // done, so that children are linked in the order they were reached.
693
+ while (queue.length > 0 || deferred.length > 0) {
694
+ const round = queue.length > 0 ? queue.splice(0) : deferred.splice(0)
662
695
 
663
- while (deferred.length > 0) {
664
- await Promise.all(deferred.splice(0, deferred.length).map(collect))
696
+ await Promise.all(round.map(collect))
665
697
  }
666
698
 
667
- for (const record of bundles) await record._mountAsync()
699
+ for (const record of bundles) await record._mount()
668
700
  }
669
701
 
670
702
  _addBundle(bundle) {
@@ -776,6 +808,8 @@ module.exports = class ModuleLoader {
776
808
 
777
809
  if (record === null) {
778
810
  record = loader._requireSync(specifier, referrer.url, attributes)
811
+ } else {
812
+ loader._linkForSync(url, referrer.url, attributes, lex.constants.REQUIRE)
779
813
  }
780
814
 
781
815
  loader._assertType(record, attributes)
@@ -810,30 +844,17 @@ module.exports = class ModuleLoader {
810
844
  _importSync(specifier, parentURL) {
811
845
  const url = this._resolveSync(specifier, parentURL, 'import')
812
846
 
813
- let record = this._lookup(url)
847
+ this._linkForSync(url, parentURL, null, lex.constants.IMPORT)
814
848
 
815
- if (record === null) {
816
- this.linkSync(url, null, { importType: lex.constants.IMPORT })
817
-
818
- record = this.get(url)
819
- }
820
-
821
- return record
849
+ return this._lookup(url)
822
850
  }
823
851
 
824
852
  _requireSync(specifier, parentURL, attributes = null) {
825
853
  const url = this._resolveSync(specifier, parentURL, 'require')
826
854
 
827
- let record = this._lookup(url)
828
-
829
- if (record === null) {
830
- this.linkSync(url, null, {
831
- attributes: this._resolveAttributesSync(attributes, parentURL),
832
- importType: lex.constants.REQUIRE
833
- })
855
+ this._linkForSync(url, parentURL, attributes, lex.constants.REQUIRE)
834
856
 
835
- record = this.get(url)
836
- }
857
+ const record = this._lookup(url)
837
858
 
838
859
  if (record === null) {
839
860
  throw errors.MODULE_NOT_FOUND(
@@ -846,6 +867,99 @@ module.exports = class ModuleLoader {
846
867
  return record
847
868
  }
848
869
 
870
+ _linkForSync(url, parentURL, attributes, importType) {
871
+ attributes = this._resolveAttributeSync(attributes, 'wasi', parentURL)
872
+
873
+ const contexts = this._unlinkedContexts(url, parentURL, attributes)
874
+
875
+ if (contexts.length === 0) return
876
+
877
+ attributes = this._resolveAttributeSync(attributes, 'imports', parentURL)
878
+
879
+ if (hasAttribute(attributes, 'wasi')) {
880
+ this._linkForSync(new URL(attributes.wasi), parentURL, null, 0)
881
+ }
882
+
883
+ for (const context of contexts) {
884
+ this.linkSync(url, null, { attributes: withContext(attributes, context), importType })
885
+ }
886
+ }
887
+
888
+ async _linkFor(url, parentURL, attributes, importType) {
889
+ attributes = await this._resolveAttribute(attributes, 'wasi', parentURL)
890
+
891
+ const contexts = this._unlinkedContexts(url, parentURL, attributes)
892
+
893
+ if (contexts.length === 0) return
894
+
895
+ attributes = await this._resolveAttribute(attributes, 'imports', parentURL)
896
+
897
+ if (hasAttribute(attributes, 'wasi')) {
898
+ await this._linkFor(new URL(attributes.wasi), parentURL, null, 0)
899
+ }
900
+
901
+ for (const context of contexts) {
902
+ await this.link(url, null, { attributes: withContext(attributes, context), importType })
903
+ }
904
+ }
905
+
906
+ _unlinkedContexts(url, parentURL, attributes) {
907
+ const { host, wasi } = attributes || {}
908
+
909
+ const linked = this._lookup(url) === null ? [] : this._contextsOf(url)
910
+
911
+ const unlinked = []
912
+
913
+ for (const context of this._contextsOf(parentURL)) {
914
+ const requested = {
915
+ host: host === undefined ? context.host : host,
916
+ wasi: wasi === undefined ? context.wasi : wasi
917
+ }
918
+
919
+ const known = (other) => sameContext(other, requested)
920
+
921
+ if (linked.some(known) || unlinked.some(known)) continue
922
+
923
+ unlinked.push(requested)
924
+ }
925
+
926
+ return unlinked
927
+ }
928
+
929
+ _contextsOf(url) {
930
+ const contexts = []
931
+
932
+ for (const context of this._contexts.values()) {
933
+ if (context.visited.has(url.href)) contexts.push(context)
934
+ }
935
+
936
+ return contexts.length === 0 ? [this._defaultContext] : contexts
937
+ }
938
+
939
+ _hostOf(url) {
940
+ for (const { host, visited } of this._contexts.values()) {
941
+ if (host !== null && visited.has(url.href)) return host
942
+ }
943
+
944
+ return null
945
+ }
946
+
947
+ _wasiFor(record) {
948
+ const { wasi } = record._source
949
+
950
+ if (wasi === null) return this._wasi === null ? null : this._wasi(record.url)
951
+
952
+ let provider = this._lookup(new URL(wasi))._evaluate()
953
+
954
+ if (typeof provider === 'object' && provider !== null) provider = provider.default
955
+
956
+ if (typeof provider !== 'function') {
957
+ throw new TypeError(`WASI provider '${wasi}' must export a function`)
958
+ }
959
+
960
+ return provider(record.url)
961
+ }
962
+
849
963
  _cacheResolution(parentURL, specifier, condition, url) {
850
964
  const imports = this._resolutions[parentURL.href]
851
965
 
@@ -856,22 +970,28 @@ module.exports = class ModuleLoader {
856
970
  imports[specifier] = { [condition]: url.href }
857
971
  }
858
972
 
859
- _resolveOptions() {
973
+ _resolveOptions(host, condition) {
860
974
  const opts = { resolutions: this._resolutions, engines: Bare.versions }
861
975
 
976
+ if (host !== null) {
977
+ opts.conditions = hostParser.conditions(host)
978
+
979
+ if (condition === 'addon') opts.hosts = [host]
980
+ }
981
+
862
982
  if (this._builtins !== null) opts.builtins = Object.keys(this._builtins)
863
983
 
864
984
  return opts
865
985
  }
866
986
 
867
- _resolveSync(specifier, parentURL, condition) {
987
+ _resolveSync(specifier, parentURL, condition, host = this._hostOf(parentURL)) {
868
988
  const protocol = this._protocolFor(parentURL)
869
989
  const answers = this._answersFor(protocol)
870
990
 
871
991
  const resolver = traverse.resolve.bare(
872
992
  resolveEntry(specifier, condition),
873
993
  parentURL,
874
- this._resolveOptions()
994
+ this._resolveOptions(host, condition)
875
995
  )
876
996
 
877
997
  const candidates = []
@@ -909,9 +1029,11 @@ module.exports = class ModuleLoader {
909
1029
  if (existsSync(protocol, url, condition, answers)) {
910
1030
  const resolution = protocol.resolveSync(url)
911
1031
 
912
- this._cacheResolution(parentURL, specifier, condition, resolution)
1032
+ if (condition !== 'addon' || isLoadable(resolution)) {
1033
+ this._cacheResolution(parentURL, specifier, condition, resolution)
913
1034
 
914
- return resolution
1035
+ return resolution
1036
+ }
915
1037
  }
916
1038
 
917
1039
  next = resolver.next(false)
@@ -921,13 +1043,13 @@ module.exports = class ModuleLoader {
921
1043
  throw notFound(condition, specifier, parentURL, candidates)
922
1044
  }
923
1045
 
924
- async _resolve(specifier, parentURL, condition) {
1046
+ async _resolve(specifier, parentURL, condition, host = this._hostOf(parentURL)) {
925
1047
  const protocol = this._protocolFor(parentURL)
926
1048
 
927
1049
  const resolver = traverse.resolve.bare(
928
1050
  resolveEntry(specifier, condition),
929
1051
  parentURL,
930
- this._resolveOptions()
1052
+ this._resolveOptions(host, condition)
931
1053
  )
932
1054
 
933
1055
  const candidates = []
@@ -975,9 +1097,11 @@ module.exports = class ModuleLoader {
975
1097
 
976
1098
  if (isThenable(resolution)) resolution = await resolution
977
1099
 
978
- this._cacheResolution(parentURL, specifier, condition, resolution)
1100
+ if (condition !== 'addon' || isLoadable(resolution)) {
1101
+ this._cacheResolution(parentURL, specifier, condition, resolution)
979
1102
 
980
- return resolution
1103
+ return resolution
1104
+ }
981
1105
  }
982
1106
 
983
1107
  next = resolver.next(false)
@@ -995,7 +1119,7 @@ module.exports = class ModuleLoader {
995
1119
 
996
1120
  parentURL = parentURLFor(referrer, parentURL)
997
1121
 
998
- const url = this._resolveSync(specifier, parentURL, condition)
1122
+ const url = this._resolveSync(specifier, parentURL, condition, this._hostOf(referrer.url))
999
1123
 
1000
1124
  const attributes = opts && opts.with
1001
1125
 
@@ -1007,36 +1131,36 @@ module.exports = class ModuleLoader {
1007
1131
  lex.constants.RESOLVE |
1008
1132
  (condition === 'require' ? lex.constants.REQUIRE : lex.constants.IMPORT)
1009
1133
 
1010
- this._linkResolved(url, parentURL, attributes, importType)
1134
+ this._linkForSync(url, parentURL, attributes, importType)
1011
1135
  }
1012
1136
 
1013
1137
  return url
1014
1138
  }
1015
1139
 
1016
- _linkResolved(url, parentURL, attributes, importType) {
1017
- if (this._lookup(url) !== null) return
1140
+ _resolveAttributeSync(attributes, name, parentURL) {
1141
+ if (!hasAttribute(attributes, name)) return attributes
1018
1142
 
1019
- attributes = this._resolveAttributesSync(attributes, parentURL)
1143
+ const url =
1144
+ this._resolveRecorded(attributes[name], parentURL) ||
1145
+ this._resolveSync(attributes[name], parentURL, 'default')
1020
1146
 
1021
- this._linkSync(this._root(url, null, { attributes, importType }), this._protocolFor(url))
1022
-
1023
- this._instantiate()
1147
+ return { ...attributes, [name]: url.href }
1024
1148
  }
1025
1149
 
1026
- _resolveAttributesSync(attributes, parentURL) {
1027
- if (!hasImportsAttribute(attributes)) return attributes
1150
+ async _resolveAttribute(attributes, name, parentURL) {
1151
+ if (!hasAttribute(attributes, name)) return attributes
1028
1152
 
1029
- const url = this._resolveSync(attributes.imports, parentURL, 'default')
1153
+ const url =
1154
+ this._resolveRecorded(attributes[name], parentURL) ||
1155
+ (await this._resolve(attributes[name], parentURL, 'default'))
1030
1156
 
1031
- return { ...attributes, imports: url.href }
1157
+ return { ...attributes, [name]: url.href }
1032
1158
  }
1033
1159
 
1034
- async _resolveAttributes(attributes, parentURL) {
1035
- if (!hasImportsAttribute(attributes)) return attributes
1160
+ _resolveRecorded(specifier, parentURL) {
1161
+ const parent = this._lookup(parentURL)
1036
1162
 
1037
- const url = await this._resolve(attributes.imports, parentURL, 'default')
1038
-
1039
- return { ...attributes, imports: url.href }
1163
+ return parent === null ? null : parent._resolve(specifier, 'default')
1040
1164
  }
1041
1165
 
1042
1166
  _resolveArtifact(referrer, specifier, condition, parentURL) {
@@ -1044,13 +1168,43 @@ module.exports = class ModuleLoader {
1044
1168
 
1045
1169
  const url = parentURL === referrer.url ? referrer._resolve(specifier, condition) : null
1046
1170
 
1047
- return url === null ? this._resolveSync(specifier, parentURL, condition) : url
1171
+ if (url !== null) return url
1172
+
1173
+ if (condition === 'addon') return this._resolveAddon(referrer, specifier, parentURL)
1174
+
1175
+ return this._resolveSync(specifier, parentURL, condition, this._hostOf(referrer.url))
1176
+ }
1177
+
1178
+ _resolveAddon(referrer, specifier, parentURL) {
1179
+ const contexts = this._contextsOf(referrer.url)
1180
+
1181
+ if (contexts.length > 1) {
1182
+ throw errors.ADDON_HOST_INCOMPATIBLE(
1183
+ `Cannot resolve addon '${specifier}' from '${referrer.url.href}', which is linked in several contexts`
1184
+ )
1185
+ }
1186
+
1187
+ const [context] = contexts
1188
+
1189
+ const url = this._resolveSync(specifier, parentURL, 'addon', context.host)
1190
+
1191
+ const imports = this._resolutions[referrer.url.href]
1192
+
1193
+ traverse.assertAddonContext(
1194
+ url,
1195
+ (imports && imports['#package']) || referrer.url.href,
1196
+ context,
1197
+ this._addonContexts
1198
+ )
1199
+
1200
+ return url
1048
1201
  }
1049
1202
 
1050
1203
  _addon(referrer, specifier, parentURL) {
1051
1204
  const url = this._resolveArtifact(referrer, specifier, 'addon', parentURL)
1052
1205
 
1053
- const record = this._lookup(url) || this._addonRecord(url)
1206
+ const record =
1207
+ this._lookup(url) || this._addonRecord(url, this._contextsOf(referrer.url)[0].wasi)
1054
1208
 
1055
1209
  return record._evaluate()
1056
1210
  }
@@ -1155,6 +1309,7 @@ function claimCache(loader) {
1155
1309
  const graph = {
1156
1310
  protocol: loader._protocol,
1157
1311
  builtins: loader._builtins,
1312
+ wasi: loader._wasi,
1158
1313
  patches: null,
1159
1314
  reader: loader._protocol
1160
1315
  }
@@ -1172,6 +1327,10 @@ function claimCache(loader) {
1172
1327
  throw errors.CACHE_INCOMPATIBLE('Cache is read with different builtins')
1173
1328
  }
1174
1329
 
1330
+ if (owner.wasi !== loader._wasi) {
1331
+ throw errors.CACHE_INCOMPATIBLE('Cache is read with different WASI capabilities')
1332
+ }
1333
+
1175
1334
  return owner
1176
1335
  }
1177
1336
 
@@ -1187,6 +1346,12 @@ function assertCompatible(loader, href, record) {
1187
1346
  if (other._builtins !== loader._builtins) {
1188
1347
  throw errors.CACHE_INCOMPATIBLE(`Cache holds '${href}', which was read with different builtins`)
1189
1348
  }
1349
+
1350
+ if (other._wasi !== loader._wasi) {
1351
+ throw errors.CACHE_INCOMPATIBLE(
1352
+ `Cache holds '${href}', which was read with different WASI capabilities`
1353
+ )
1354
+ }
1190
1355
  }
1191
1356
 
1192
1357
  function readSync(protocol, url, answers = null) {
@@ -1423,9 +1588,9 @@ function isOptions(value) {
1423
1588
  return typeof value === 'object' && value !== null && !URL.isURL(value)
1424
1589
  }
1425
1590
 
1426
- function hasImportsAttribute(attributes) {
1591
+ function hasAttribute(attributes, name) {
1427
1592
  return (
1428
- typeof attributes === 'object' && attributes !== null && typeof attributes.imports === 'string'
1593
+ typeof attributes === 'object' && attributes !== null && typeof attributes[name] === 'string'
1429
1594
  )
1430
1595
  }
1431
1596
 
@@ -1437,6 +1602,13 @@ function isTerminalResolution(url, condition) {
1437
1602
  return protocol === 'linked:' && condition === 'addon'
1438
1603
  }
1439
1604
 
1605
+ // Once sealed, only native addons that were already loaded can be loaded.
1606
+ function isLoadable(url) {
1607
+ if (url.pathname.endsWith('.wasm') || Bare.Addon.sealed !== true) return true
1608
+
1609
+ return Bare.Addon.loaded(url)
1610
+ }
1611
+
1440
1612
  function isThenable(value) {
1441
1613
  return value !== null && typeof value === 'object' && typeof value.then === 'function'
1442
1614
  }
@@ -1,7 +1,14 @@
1
+ const instantiate = require('bare-wasm-abi')
1
2
  const SyntheticModule = require('./synthetic')
2
3
 
3
4
  module.exports = class AddonModule extends SyntheticModule {
4
5
  _execute() {
5
- this.exports = new Bare.Addon(this.url).exports
6
+ if (this.url.pathname.endsWith('.wasm')) {
7
+ this.exports = instantiate(this._source.bytes || this.protocol.readSync(this.url), {
8
+ wasi: this._loader._wasiFor(this)
9
+ })
10
+ } else {
11
+ this.exports = new Bare.Addon(this.url).exports
12
+ }
6
13
  }
7
14
  }
@@ -27,7 +27,7 @@ module.exports = class BundleModule extends SyntheticModule {
27
27
  return bundle
28
28
  }
29
29
 
30
- _mount() {
30
+ _mountSync() {
31
31
  if (this._linked) return
32
32
 
33
33
  if (this._open().main) this._loader._linkBundleSync(this._bundle)
@@ -35,17 +35,17 @@ module.exports = class BundleModule extends SyntheticModule {
35
35
  this._linked = true
36
36
  }
37
37
 
38
- _mountAsync() {
39
- if (this._linking === null) this._linking = this._linkAsync()
38
+ _mount() {
39
+ if (this._linking === null) this._linking = this._link()
40
40
 
41
41
  return this._linking
42
42
  }
43
43
 
44
44
  _ensureMounted() {
45
- if (this._linking === null) this._mount()
45
+ if (this._linking === null) this._mountSync()
46
46
  }
47
47
 
48
- async _linkAsync() {
48
+ async _link() {
49
49
  if (this._open().main) await this._loader._linkBundle(this._bundle)
50
50
 
51
51
  this._linked = true
package/lib/module.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const lex = require('bare-module-lexer')
2
2
  const { conditionMatches } = require('bare-module-resolve')
3
3
  const strip = require('bare-type-stripper')
4
+ const hostParser = require('bare-host-parser')
4
5
  const binding = require('../binding')
5
6
  const { urlToPath, urlToDirname } = require('./url')
6
7
  const errors = require('./errors')
@@ -9,7 +10,7 @@ const { REEXPORT, ADDON, ASSET } = lex.constants
9
10
 
10
11
  const DEFERRED_PROTOCOL = 'deferred:'
11
12
 
12
- const hostConditions = ['bare', 'node', ...Bare.Addon.host.split('-')]
13
+ const nativeConditions = hostParser.conditions(Bare.Addon.host)
13
14
 
14
15
  const UNLINKED = 0
15
16
  const LINKED = 1
@@ -101,7 +102,10 @@ module.exports = exports = class Module {
101
102
 
102
103
  if (entry === undefined) return null
103
104
 
104
- const href = pickCondition(entry, condition)
105
+ const href =
106
+ typeof entry === 'string'
107
+ ? entry
108
+ : pickCondition(entry, conditionsFor(condition, this._loader._hostOf(this.url)))
105
109
 
106
110
  if (href === null) return null
107
111
 
@@ -279,18 +283,13 @@ async function ondynamicimport(specifier, attributes, referrerName, id) {
279
283
 
280
284
  let url = referrer._resolve(specifier, 'import')
281
285
 
282
- let record = loader._lookup(url)
286
+ if (loader._lookup(url) === null) url = await loader._resolve(specifier, referrer.url, 'import')
283
287
 
284
- if (record === null) {
285
- url = await loader._resolve(specifier, referrer.url, 'import')
288
+ await loader._linkFor(url, referrer.url, attributes, lex.constants.IMPORT | lex.constants.DYNAMIC)
286
289
 
287
- record = await loader.link(url, null, {
288
- attributes: await loader._resolveAttributes(attributes, referrer.url),
289
- importType: lex.constants.IMPORT | lex.constants.DYNAMIC
290
- })
291
- } else {
292
- record._instantiate()
293
- }
290
+ const record = loader._lookup(url)
291
+
292
+ record._instantiate()
294
293
 
295
294
  loader._assertType(record, attributes)
296
295
 
@@ -370,13 +369,30 @@ function onrun(reason, promise, err = reason) {
370
369
  }
371
370
  }
372
371
 
373
- function pickCondition(entry, condition) {
372
+ function conditionsFor(condition, host) {
373
+ const base = [condition, 'bare', 'node']
374
+
375
+ if (host === null) {
376
+ return [
377
+ [...base, ...nativeConditions],
378
+ [...base, 'wasi', 'wasm32']
379
+ ]
380
+ }
381
+
382
+ if (condition === 'addon') return [[...base, ...hostParser.conditions(host)]]
383
+
384
+ return [[...base, ...nativeConditions, ...hostParser.conditions(host)]]
385
+ }
386
+
387
+ function pickCondition(entry, conditions) {
374
388
  if (typeof entry === 'string') return entry
375
389
 
376
390
  if (entry === null || typeof entry !== 'object') return null
377
391
 
378
- for (const [, target] of conditionMatches(entry, [condition, ...hostConditions])) {
379
- return pickCondition(target, condition)
392
+ for (const [, target, subset] of conditionMatches(entry, conditions)) {
393
+ const href = pickCondition(target, subset)
394
+
395
+ if (href !== null) return href
380
396
  }
381
397
 
382
398
  return null
package/lib/source.js CHANGED
@@ -6,6 +6,7 @@ module.exports = class ModuleSource {
6
6
  this.bytes = toBuffer(dependency.source)
7
7
  this.imports = dependency.imports
8
8
  this.lexer = dependency.lexer
9
+ this.wasi = dependency.wasi || null
9
10
  }
10
11
 
11
12
  get exports() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "7.4.0",
3
+ "version": "7.5.1",
4
4
  "description": "The module system that powers Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -43,16 +43,18 @@
43
43
  },
44
44
  "homepage": "https://github.com/holepunchto/bare-module#readme",
45
45
  "engines": {
46
- "bare": ">=1.32.0"
46
+ "bare": ">=1.35.0"
47
47
  },
48
48
  "dependencies": {
49
49
  "bare-bundle": "^1.11.0",
50
+ "bare-host-parser": "^1.0.0",
50
51
  "bare-module-lexer": "^1.7.0",
51
52
  "bare-module-resolve": "^1.12.6",
52
- "bare-module-traverse": "~2.6.0",
53
+ "bare-module-traverse": "~2.7.0",
53
54
  "bare-path": "^3.0.0",
54
55
  "bare-type-stripper": "^0.1.2",
55
56
  "bare-url": "^2.0.1",
57
+ "bare-wasm-abi": "^0.1.0",
56
58
  "promaphore": "^1.0.0"
57
59
  },
58
60
  "devDependencies": {