bare-module 6.4.0 → 7.0.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/README.md +237 -270
- package/binding.c +623 -333
- package/index.d.ts +156 -63
- package/index.js +87 -1002
- package/lib/errors.js +27 -10
- package/lib/loader.d.ts +56 -0
- package/lib/loader.js +1192 -0
- package/lib/module/addon.js +7 -0
- package/lib/module/binary.js +7 -0
- package/lib/module/builtin.js +19 -0
- package/lib/module/bundle.js +47 -0
- package/lib/module/json.js +19 -0
- package/lib/module/script.js +23 -0
- package/lib/module/source-text.js +13 -0
- package/lib/module/synthetic.js +20 -0
- package/lib/module/text.js +7 -0
- package/lib/module/value.js +27 -0
- package/lib/module.js +392 -0
- package/lib/protocol.d.ts +56 -8
- package/lib/protocol.js +93 -26
- package/lib/source.js +18 -0
- package/lib/url.js +52 -0
- package/package.json +18 -8
- package/prebuilds/android-arm/bare-module.bare +0 -0
- package/prebuilds/android-arm64/bare-module.bare +0 -0
- package/prebuilds/android-ia32/bare-module.bare +0 -0
- package/prebuilds/android-x64/bare-module.bare +0 -0
- package/prebuilds/darwin-arm64/bare-module.bare +0 -0
- package/prebuilds/darwin-x64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-module.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-module.bare +0 -0
- package/prebuilds/linux-arm64/bare-module.bare +0 -0
- package/prebuilds/linux-x64/bare-module.bare +0 -0
- package/prebuilds/win32-arm64/bare-module.bare +0 -0
- package/prebuilds/win32-x64/bare-module.bare +0 -0
- package/lib/constants.d.ts +0 -19
- package/lib/constants.js +0 -17
package/lib/loader.js
ADDED
|
@@ -0,0 +1,1192 @@
|
|
|
1
|
+
const Semaphore = require('promaphore')
|
|
2
|
+
const traverse = require('bare-module-traverse')
|
|
3
|
+
const { startsWithWindowsDriveLetter } = require('bare-module-resolve')
|
|
4
|
+
const lex = require('bare-module-lexer')
|
|
5
|
+
const { isURL, pathToFileURL } = require('bare-url')
|
|
6
|
+
const { urlToPath } = require('./url')
|
|
7
|
+
const ModuleProtocol = require('./protocol')
|
|
8
|
+
const ModuleSource = require('./source')
|
|
9
|
+
const SourceTextModule = require('./module/source-text')
|
|
10
|
+
const ScriptModule = require('./module/script')
|
|
11
|
+
const JSONModule = require('./module/json')
|
|
12
|
+
const TextModule = require('./module/text')
|
|
13
|
+
const BinaryModule = require('./module/binary')
|
|
14
|
+
const AddonModule = require('./module/addon')
|
|
15
|
+
const BundleModule = require('./module/bundle')
|
|
16
|
+
const BuiltinModule = require('./module/builtin')
|
|
17
|
+
const errors = require('./errors')
|
|
18
|
+
|
|
19
|
+
const protocolKind = Symbol.for('bare.module.protocol.kind')
|
|
20
|
+
|
|
21
|
+
const type = traverse.constants
|
|
22
|
+
const engines = Bare.versions
|
|
23
|
+
|
|
24
|
+
const cacheOwners = new WeakMap()
|
|
25
|
+
|
|
26
|
+
function assertProtocol(protocol) {
|
|
27
|
+
if (ModuleProtocol.isProtocol(protocol)) return protocol
|
|
28
|
+
|
|
29
|
+
const version =
|
|
30
|
+
typeof protocol === 'object' && protocol !== null ? protocol[protocolKind] : undefined
|
|
31
|
+
|
|
32
|
+
if (version === undefined) {
|
|
33
|
+
throw errors.PROTOCOL_INCOMPATIBLE('Protocol is not a module protocol')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
throw errors.PROTOCOL_INCOMPATIBLE(
|
|
37
|
+
`Protocol is of version ${version}, but this module system reads version ${ModuleProtocol[protocolKind]}`
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function cacheFor(cache) {
|
|
42
|
+
if (cache === undefined) return Object.create(null)
|
|
43
|
+
|
|
44
|
+
if (typeof cache === 'object' && cache !== null) return cache
|
|
45
|
+
|
|
46
|
+
throw new TypeError(`Cache must be an object. Received type ${typeof cache} (${cache})`)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function resolutionsFor(resolutions) {
|
|
50
|
+
if (resolutions === undefined) return Object.create(null)
|
|
51
|
+
|
|
52
|
+
if (typeof resolutions === 'object' && resolutions !== null) return resolutions
|
|
53
|
+
|
|
54
|
+
throw new TypeError(
|
|
55
|
+
`Resolutions must be an object. Received type ${typeof resolutions} (${resolutions})`
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function builtinsFor(builtins) {
|
|
60
|
+
if (builtins === undefined || builtins === null) return null
|
|
61
|
+
|
|
62
|
+
if (typeof builtins === 'object') return builtins
|
|
63
|
+
|
|
64
|
+
throw new TypeError(`Builtins must be an object. Received type ${typeof builtins} (${builtins})`)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function importsFor(imports) {
|
|
68
|
+
if (imports === undefined || imports === null) return null
|
|
69
|
+
|
|
70
|
+
if (typeof imports === 'object') return imports
|
|
71
|
+
|
|
72
|
+
throw new TypeError(`Imports must be an object. Received type ${typeof imports} (${imports})`)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function defaultTypeFor(defaultType) {
|
|
76
|
+
if (defaultType === undefined || defaultType === 0) return 0
|
|
77
|
+
|
|
78
|
+
if (recordClasses[defaultType] !== undefined) return defaultType
|
|
79
|
+
|
|
80
|
+
throw new TypeError(
|
|
81
|
+
`Default type must be a module type. Received type ${typeof defaultType} (${defaultType})`
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function concurrencyFor(concurrency) {
|
|
86
|
+
if (concurrency === undefined) return 0
|
|
87
|
+
|
|
88
|
+
if (Number.isInteger(concurrency) && concurrency >= 0) return concurrency
|
|
89
|
+
|
|
90
|
+
throw new TypeError(
|
|
91
|
+
`Concurrency must be a non-negative integer. Received type ${typeof concurrency} (${concurrency})`
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function assertURL(url, name) {
|
|
96
|
+
if (isURL(url)) return url
|
|
97
|
+
|
|
98
|
+
throw new TypeError(`${name} must be a URL. Received type ${typeof url} (${url})`)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function typeAttributeFor(attributes) {
|
|
102
|
+
if (attributes === undefined || attributes === null) return undefined
|
|
103
|
+
|
|
104
|
+
if (typeof attributes !== 'object') {
|
|
105
|
+
throw new TypeError(
|
|
106
|
+
`Attributes must be an object. Received type ${typeof attributes} (${attributes})`
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const type = attributes.type
|
|
111
|
+
|
|
112
|
+
if (type === undefined || typeof type === 'string') return type
|
|
113
|
+
|
|
114
|
+
throw new TypeError(`Module type must be a string. Received type ${typeof type} (${type})`)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Must name every option the constructor takes, as it decides both what a fork
|
|
118
|
+
// inherits and what it may narrow.
|
|
119
|
+
const loaderOptions = [
|
|
120
|
+
'builtins',
|
|
121
|
+
'cache',
|
|
122
|
+
'concurrency',
|
|
123
|
+
'defaultType',
|
|
124
|
+
'imports',
|
|
125
|
+
'protocol',
|
|
126
|
+
'resolutions'
|
|
127
|
+
]
|
|
128
|
+
|
|
129
|
+
const recordClasses = {
|
|
130
|
+
__proto__: null,
|
|
131
|
+
|
|
132
|
+
[type.MODULE]: SourceTextModule,
|
|
133
|
+
[type.SCRIPT]: ScriptModule,
|
|
134
|
+
[type.JSON]: JSONModule,
|
|
135
|
+
[type.TEXT]: TextModule,
|
|
136
|
+
[type.BINARY]: BinaryModule,
|
|
137
|
+
[type.ADDON]: AddonModule,
|
|
138
|
+
[type.BUNDLE]: BundleModule
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const typeByAttribute = {
|
|
142
|
+
__proto__: null,
|
|
143
|
+
|
|
144
|
+
module: type.MODULE,
|
|
145
|
+
script: type.SCRIPT,
|
|
146
|
+
json: type.JSON,
|
|
147
|
+
text: type.TEXT,
|
|
148
|
+
binary: type.BINARY,
|
|
149
|
+
addon: type.ADDON,
|
|
150
|
+
bundle: type.BUNDLE
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const attributeByType = {
|
|
154
|
+
__proto__: null,
|
|
155
|
+
|
|
156
|
+
...Object.fromEntries(
|
|
157
|
+
Object.entries(typeByAttribute).map(([attribute, type]) => [type, attribute])
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
module.exports = class ModuleLoader {
|
|
162
|
+
constructor(opts = {}) {
|
|
163
|
+
const {
|
|
164
|
+
protocol = null,
|
|
165
|
+
builtins,
|
|
166
|
+
defaultType,
|
|
167
|
+
imports,
|
|
168
|
+
resolutions,
|
|
169
|
+
cache,
|
|
170
|
+
concurrency
|
|
171
|
+
} = opts
|
|
172
|
+
|
|
173
|
+
this._protocol = protocol === null ? new ModuleProtocol() : assertProtocol(protocol)
|
|
174
|
+
this._defaultType = defaultTypeFor(defaultType)
|
|
175
|
+
this._builtins = builtinsFor(builtins)
|
|
176
|
+
this._imports = importsFor(imports)
|
|
177
|
+
this._concurrency = concurrencyFor(concurrency)
|
|
178
|
+
this._main = null
|
|
179
|
+
this._cache = cacheFor(cache)
|
|
180
|
+
this._resolutions = resolutionsFor(resolutions)
|
|
181
|
+
this._manifests = Object.create(null)
|
|
182
|
+
this._bundles = []
|
|
183
|
+
this._visited = new Set()
|
|
184
|
+
this._packages = new Map()
|
|
185
|
+
this._prefixes = new Map()
|
|
186
|
+
this._answers = { listed: new Map(), resolved: new Map() }
|
|
187
|
+
this._uninstantiated = []
|
|
188
|
+
this._pending = null
|
|
189
|
+
|
|
190
|
+
// A cache holds the modules of a single graph, and every record in it is a
|
|
191
|
+
// handle to the loader that read it. Reusing one across loaders that don't
|
|
192
|
+
// reach as far as each other would hand the modules of one graph to the
|
|
193
|
+
// other, protocol and builtins and all. The cache is therefore claimed
|
|
194
|
+
// before anything is read into it, and what it already holds is checked
|
|
195
|
+
// against the claim.
|
|
196
|
+
claimCache(this)
|
|
197
|
+
|
|
198
|
+
for (const href in this._cache) {
|
|
199
|
+
const record = this._cache[href]
|
|
200
|
+
|
|
201
|
+
if (record === null || record === undefined) continue
|
|
202
|
+
|
|
203
|
+
assertCompatible(this, href, record)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
get protocol() {
|
|
208
|
+
return this._protocol
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
get cache() {
|
|
212
|
+
return this._cache
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
get main() {
|
|
216
|
+
return this._main
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
get(url) {
|
|
220
|
+
return this._cached(assertURL(url, 'URL').href) || null
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async link(entry, source = null, opts = {}) {
|
|
224
|
+
assertURL(entry, 'Entry')
|
|
225
|
+
|
|
226
|
+
if (entry.protocol === 'builtin:') this._lookup(entry)
|
|
227
|
+
else await this._link(this._root(entry, source, opts), this._protocolFor(entry))
|
|
228
|
+
|
|
229
|
+
this._instantiate()
|
|
230
|
+
|
|
231
|
+
return this._entry(entry, opts)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
linkSync(entry, source = null, opts = {}) {
|
|
235
|
+
assertURL(entry, 'Entry')
|
|
236
|
+
|
|
237
|
+
if (entry.protocol === 'builtin:') this._lookup(entry)
|
|
238
|
+
else this._linkSync(this._root(entry, source, opts), this._protocolFor(entry))
|
|
239
|
+
|
|
240
|
+
this._instantiate()
|
|
241
|
+
|
|
242
|
+
return this._entry(entry, opts)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async import(entry, opts = {}) {
|
|
246
|
+
const record = await this.link(entry, null, opts)
|
|
247
|
+
|
|
248
|
+
await this._evaluate(record)
|
|
249
|
+
|
|
250
|
+
return record.exports
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
importSync(entry, opts = {}) {
|
|
254
|
+
return this.linkSync(entry, null, opts)._evaluate()
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
[Symbol.for('bare.inspect')]() {
|
|
258
|
+
return {
|
|
259
|
+
__proto__: { constructor: ModuleLoader },
|
|
260
|
+
|
|
261
|
+
main: this.main
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
_fork(opts) {
|
|
266
|
+
if (loaderOptions.every((name) => opts[name] === undefined)) return this
|
|
267
|
+
|
|
268
|
+
const options = {}
|
|
269
|
+
|
|
270
|
+
for (const name of loaderOptions) {
|
|
271
|
+
options[name] = opts[name] !== undefined ? opts[name] : this['_' + name]
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Narrowing what a module may reach starts a graph of its own. What this
|
|
275
|
+
// loader read belongs to what it could reach, so a fork that reaches
|
|
276
|
+
// elsewhere gets neither the cache it read into nor the resolutions it
|
|
277
|
+
// resolved through.
|
|
278
|
+
const reaches = options.protocol === this._protocol && options.builtins === this._builtins
|
|
279
|
+
|
|
280
|
+
if (!reaches) options.cache = opts.cache
|
|
281
|
+
|
|
282
|
+
const shares = reaches && cacheFor(options.cache) === this._cache
|
|
283
|
+
|
|
284
|
+
if (!shares) options.resolutions = opts.resolutions
|
|
285
|
+
|
|
286
|
+
const forked = new ModuleLoader(options)
|
|
287
|
+
|
|
288
|
+
// Which module the program was launched from doesn't change with the
|
|
289
|
+
// options another one is loaded with, so long as the two still share a
|
|
290
|
+
// graph. Handing it over otherwise would hand over a record of this graph.
|
|
291
|
+
if (shares) forked._main = this._main
|
|
292
|
+
|
|
293
|
+
return forked
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async _evaluate(record) {
|
|
297
|
+
const pending = (this._pending = [])
|
|
298
|
+
|
|
299
|
+
try {
|
|
300
|
+
record._evaluate()
|
|
301
|
+
} finally {
|
|
302
|
+
this._pending = null
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (pending.length > 0) await Promise.all(pending)
|
|
306
|
+
|
|
307
|
+
return record
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
_answersFor(protocol) {
|
|
311
|
+
return protocol === this._protocol ? this._answers : null
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
_protocolFor(url) {
|
|
315
|
+
const bundle = this._bundleFor(url)
|
|
316
|
+
|
|
317
|
+
if (bundle === null) return this._protocol
|
|
318
|
+
|
|
319
|
+
return this._protocol.extend({
|
|
320
|
+
resolve(context, url) {
|
|
321
|
+
return bundleOwns(bundle, url) ? url : context.resolve(url)
|
|
322
|
+
},
|
|
323
|
+
|
|
324
|
+
resolveSync(context, url) {
|
|
325
|
+
return bundleOwns(bundle, url) ? url : context.resolveSync(url)
|
|
326
|
+
},
|
|
327
|
+
|
|
328
|
+
exists(context, url) {
|
|
329
|
+
if (bundleOwns(bundle, url)) return true
|
|
330
|
+
|
|
331
|
+
const exists = context.exists(url)
|
|
332
|
+
|
|
333
|
+
if (isThenable(exists)) return exists.then((found) => found || bundle.exists(url.href))
|
|
334
|
+
|
|
335
|
+
return exists || bundle.exists(url.href)
|
|
336
|
+
},
|
|
337
|
+
|
|
338
|
+
existsSync(context, url) {
|
|
339
|
+
if (bundleOwns(bundle, url)) return true
|
|
340
|
+
|
|
341
|
+
return context.existsSync(url) || bundle.exists(url.href)
|
|
342
|
+
},
|
|
343
|
+
|
|
344
|
+
read(context, url) {
|
|
345
|
+
if (bundleOwns(bundle, url)) return bundle.read(url.href)
|
|
346
|
+
|
|
347
|
+
const source = context.read(url)
|
|
348
|
+
|
|
349
|
+
if (isThenable(source)) return source.then((found) => found || bundle.read(url.href))
|
|
350
|
+
|
|
351
|
+
return source || bundle.read(url.href)
|
|
352
|
+
},
|
|
353
|
+
|
|
354
|
+
readSync(context, url) {
|
|
355
|
+
if (bundleOwns(bundle, url)) return bundle.read(url.href)
|
|
356
|
+
|
|
357
|
+
return context.readSync(url) || bundle.read(url.href)
|
|
358
|
+
},
|
|
359
|
+
|
|
360
|
+
list(context, url) {
|
|
361
|
+
return [] // TODO
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
listSync(context, url) {
|
|
365
|
+
return [] // TODO
|
|
366
|
+
}
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
_bundleFor(url) {
|
|
371
|
+
for (const bundle of this._bundles) {
|
|
372
|
+
if (bundle.exists(url.href)) return bundle
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return null
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
_cached(href) {
|
|
379
|
+
const record = this._cache[href]
|
|
380
|
+
|
|
381
|
+
if (record === null || record === undefined) return record
|
|
382
|
+
|
|
383
|
+
if (record._loader !== this) assertCompatible(this, href, record)
|
|
384
|
+
|
|
385
|
+
return record
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
_lookup(url) {
|
|
389
|
+
if (url === null) return null
|
|
390
|
+
|
|
391
|
+
const record = this._cached(url.href)
|
|
392
|
+
|
|
393
|
+
if (record !== undefined) return record
|
|
394
|
+
|
|
395
|
+
if (url.protocol === 'builtin:') return this._builtinRecord(url)
|
|
396
|
+
|
|
397
|
+
return null
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
_builtinRecord(url) {
|
|
401
|
+
const name = url.pathname
|
|
402
|
+
|
|
403
|
+
if (this._builtins === null || Object.hasOwn(this._builtins, name) === false) return null
|
|
404
|
+
|
|
405
|
+
const source = new ModuleSource({
|
|
406
|
+
url,
|
|
407
|
+
type: 0,
|
|
408
|
+
source: null,
|
|
409
|
+
imports: {},
|
|
410
|
+
lexer: { imports: [], exports: [] }
|
|
411
|
+
})
|
|
412
|
+
|
|
413
|
+
const record = new BuiltinModule(this, source, this._builtins[name])
|
|
414
|
+
|
|
415
|
+
this._cache[url.href] = record
|
|
416
|
+
|
|
417
|
+
this._uninstantiated.push(record)
|
|
418
|
+
|
|
419
|
+
return record
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
_addonRecord(url) {
|
|
423
|
+
const source = new ModuleSource({
|
|
424
|
+
url,
|
|
425
|
+
type: type.ADDON,
|
|
426
|
+
source: null,
|
|
427
|
+
imports: {},
|
|
428
|
+
lexer: { imports: [], exports: [] }
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
const record = new AddonModule(this, source)
|
|
432
|
+
|
|
433
|
+
this._cache[url.href] = record
|
|
434
|
+
|
|
435
|
+
return record
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
_entry(entry, opts) {
|
|
439
|
+
const record = this._lookup(traverse.alias(entry, opts))
|
|
440
|
+
|
|
441
|
+
if (this._main === null) this._main = record
|
|
442
|
+
|
|
443
|
+
return record
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
_root(entry, source, opts) {
|
|
447
|
+
opts = {
|
|
448
|
+
defaultType: this._defaultType || type.SCRIPT,
|
|
449
|
+
imports: this._imports,
|
|
450
|
+
engines,
|
|
451
|
+
...opts,
|
|
452
|
+
deferUnresolved: true,
|
|
453
|
+
packages: this._packages,
|
|
454
|
+
prefixes: this._prefixes,
|
|
455
|
+
resolve: traverse.resolve.bare
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (this._builtins !== null) opts.builtins = Object.keys(this._builtins)
|
|
459
|
+
|
|
460
|
+
const attributes = opts.attributes || {}
|
|
461
|
+
|
|
462
|
+
// Turned down here as well as at the type check, as this is where a type
|
|
463
|
+
// reaches the traversal and a graph is read for it.
|
|
464
|
+
typeAttributeFor(attributes)
|
|
465
|
+
|
|
466
|
+
return traverse.module(entry, source, attributes, null, this._visited, opts)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
*_drive(generator, onChild) {
|
|
470
|
+
let next = generator.next()
|
|
471
|
+
let probed = null
|
|
472
|
+
|
|
473
|
+
while (next.done !== true) {
|
|
474
|
+
const value = next.value
|
|
475
|
+
|
|
476
|
+
if (value.module) {
|
|
477
|
+
const source =
|
|
478
|
+
value.artifact || value.module.href === probed ? null : yield { module: value.module }
|
|
479
|
+
|
|
480
|
+
probed = null
|
|
481
|
+
|
|
482
|
+
next = generator.next(source)
|
|
483
|
+
} else if (value.probe) {
|
|
484
|
+
const exists = yield { probe: value.probe }
|
|
485
|
+
|
|
486
|
+
probed = exists ? value.probe.href : null
|
|
487
|
+
|
|
488
|
+
next = generator.next(exists)
|
|
489
|
+
} else if (value.resolution) {
|
|
490
|
+
next = generator.next(yield { resolution: value.resolution })
|
|
491
|
+
} else if (value.prefix) {
|
|
492
|
+
next = generator.next(yield { prefix: value.prefix, expand: value.expand })
|
|
493
|
+
} else if (value.links) {
|
|
494
|
+
yield { links: value.links }
|
|
495
|
+
|
|
496
|
+
next = generator.next()
|
|
497
|
+
} else if (value.children) {
|
|
498
|
+
onChild(value.children, value.deferred)
|
|
499
|
+
|
|
500
|
+
next = generator.next()
|
|
501
|
+
} else {
|
|
502
|
+
this._ingest(value.dependency)
|
|
503
|
+
|
|
504
|
+
next = generator.next()
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
_linkSync(root, protocol) {
|
|
510
|
+
const answers = this._answersFor(protocol)
|
|
511
|
+
|
|
512
|
+
const queue = [root]
|
|
513
|
+
const deferred = []
|
|
514
|
+
|
|
515
|
+
const onChild = (children, isDeferred) => {
|
|
516
|
+
if (isDeferred) deferred.push(children)
|
|
517
|
+
else queue.push(children)
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
const driveLink = (link) => driveSync(this._drive(link, onChild), protocol, driveLink, answers)
|
|
521
|
+
|
|
522
|
+
while (queue.length > 0 || deferred.length > 0) {
|
|
523
|
+
const generator = queue.length > 0 ? queue.pop() : deferred.shift()
|
|
524
|
+
|
|
525
|
+
driveSync(this._drive(generator, onChild), protocol, driveLink, answers)
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
async _link(root, protocol) {
|
|
530
|
+
const answers = this._answersFor(protocol)
|
|
531
|
+
|
|
532
|
+
const semaphore = this._concurrency > 0 ? new Semaphore(this._concurrency) : null
|
|
533
|
+
|
|
534
|
+
const deferred = []
|
|
535
|
+
|
|
536
|
+
const collect = async (generator) => {
|
|
537
|
+
const queue = []
|
|
538
|
+
|
|
539
|
+
const onChild = (children, isDeferred) => {
|
|
540
|
+
if (isDeferred) deferred.push(children)
|
|
541
|
+
else queue.push(children)
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const driveLink = (link) =>
|
|
545
|
+
drive(this._drive(link, onChild), protocol, driveLink, semaphore, answers)
|
|
546
|
+
|
|
547
|
+
await drive(this._drive(generator, onChild), protocol, driveLink, semaphore, answers)
|
|
548
|
+
|
|
549
|
+
if (queue.length > 0) await Promise.all(queue.map(collect))
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
await collect(root)
|
|
553
|
+
|
|
554
|
+
while (deferred.length > 0) {
|
|
555
|
+
await Promise.all(deferred.splice(0, deferred.length).map(collect))
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
_linkBundle(bundle) {
|
|
560
|
+
this._bundles.push(bundle)
|
|
561
|
+
|
|
562
|
+
const protocol = this._protocol.extend({
|
|
563
|
+
exists(context, url) {
|
|
564
|
+
if (bundleOwns(bundle, url)) return true
|
|
565
|
+
|
|
566
|
+
const exists = context.exists(url)
|
|
567
|
+
|
|
568
|
+
if (isThenable(exists)) return bundle.exists(url.href)
|
|
569
|
+
|
|
570
|
+
return exists || bundle.exists(url.href)
|
|
571
|
+
},
|
|
572
|
+
|
|
573
|
+
existsSync(context, url) {
|
|
574
|
+
if (bundleOwns(bundle, url)) return true
|
|
575
|
+
|
|
576
|
+
const exists = context.existsSync(url)
|
|
577
|
+
|
|
578
|
+
if (isThenable(exists)) return bundle.exists(url.href)
|
|
579
|
+
|
|
580
|
+
return exists || bundle.exists(url.href)
|
|
581
|
+
},
|
|
582
|
+
|
|
583
|
+
read(context, url) {
|
|
584
|
+
if (bundleOwns(bundle, url)) return bundle.read(url.href)
|
|
585
|
+
|
|
586
|
+
const source = context.read(url)
|
|
587
|
+
|
|
588
|
+
if (isThenable(source)) return bundle.read(url.href)
|
|
589
|
+
|
|
590
|
+
return source || bundle.read(url.href)
|
|
591
|
+
},
|
|
592
|
+
|
|
593
|
+
readSync(context, url) {
|
|
594
|
+
if (bundleOwns(bundle, url)) return bundle.read(url.href)
|
|
595
|
+
|
|
596
|
+
const source = context.readSync(url)
|
|
597
|
+
|
|
598
|
+
if (isThenable(source)) return bundle.read(url.href)
|
|
599
|
+
|
|
600
|
+
return source || bundle.read(url.href)
|
|
601
|
+
}
|
|
602
|
+
})
|
|
603
|
+
|
|
604
|
+
this._linkSync(
|
|
605
|
+
this._root(new URL(bundle.main), null, {
|
|
606
|
+
imports: bundle.imports,
|
|
607
|
+
resolutions: bundle.resolutions
|
|
608
|
+
}),
|
|
609
|
+
protocol
|
|
610
|
+
)
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
_ingest(dependency) {
|
|
614
|
+
const existing = this._cached(dependency.url.href)
|
|
615
|
+
|
|
616
|
+
if (existing) return existing
|
|
617
|
+
|
|
618
|
+
if (dependency.source === null && dependency.type !== type.ADDON) return null
|
|
619
|
+
|
|
620
|
+
const Class = recordClasses[dependency.type]
|
|
621
|
+
|
|
622
|
+
if (Class === undefined) {
|
|
623
|
+
throw errors.UNKNOWN_MODULE_TYPE(
|
|
624
|
+
`Module type '${dependency.type}' at '${dependency.url.href}' is not supported`
|
|
625
|
+
)
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const record = new Class(this, new ModuleSource(dependency))
|
|
629
|
+
|
|
630
|
+
this._cache[dependency.url.href] = record
|
|
631
|
+
|
|
632
|
+
this._uninstantiated.push(record)
|
|
633
|
+
|
|
634
|
+
this._resolutions[dependency.url.href] = dependency.imports
|
|
635
|
+
|
|
636
|
+
return record
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
_instantiate() {
|
|
640
|
+
const pending = this._uninstantiated
|
|
641
|
+
|
|
642
|
+
while (pending.length > 0) pending.pop()._instantiate()
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
_assertType(record, attributes) {
|
|
646
|
+
const { type: actual, naturalType } = record._source
|
|
647
|
+
|
|
648
|
+
const requested = typeAttributeFor(attributes)
|
|
649
|
+
|
|
650
|
+
if (requested === undefined) {
|
|
651
|
+
// An import attribute somewhere else may have changed the type. An import
|
|
652
|
+
// that asks for no type in particular should get the module as it is, or
|
|
653
|
+
// an error, but never the type someone else asked for.
|
|
654
|
+
if (naturalType !== 0 && naturalType !== actual) {
|
|
655
|
+
throw errors.TYPE_INCOMPATIBLE(
|
|
656
|
+
`Module '${record.url.href}' is only of type '${attributeByType[actual]}' in this graph`
|
|
657
|
+
)
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
return
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const expected = typeByAttribute[requested]
|
|
664
|
+
|
|
665
|
+
if (expected === undefined) {
|
|
666
|
+
throw errors.UNKNOWN_MODULE_TYPE(
|
|
667
|
+
`Module type '${requested}' at '${record.url.href}' is not supported`
|
|
668
|
+
)
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
if (expected !== actual) {
|
|
672
|
+
throw errors.TYPE_INCOMPATIBLE(`Module '${record.url.href}' is not of type '${requested}'`)
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
_createRequire(referrer) {
|
|
677
|
+
const loader = this
|
|
678
|
+
|
|
679
|
+
function require(specifier, opts = {}) {
|
|
680
|
+
const attributes = opts && opts.with
|
|
681
|
+
|
|
682
|
+
const url = referrer._resolve(specifier, 'require')
|
|
683
|
+
|
|
684
|
+
let record = loader._lookup(url)
|
|
685
|
+
|
|
686
|
+
if (record === null) {
|
|
687
|
+
record = loader._requireSync(specifier, referrer.url, attributes)
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
loader._assertType(record, attributes)
|
|
691
|
+
|
|
692
|
+
return record._evaluate()
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
require.main = loader.main
|
|
696
|
+
require.cache = loader.cache
|
|
697
|
+
|
|
698
|
+
require.resolve = function (specifier, parentURL = referrer.url) {
|
|
699
|
+
return urlToPath(loader._resolveSync(specifier, toURL(parentURL, referrer.url), 'require'))
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
require.asset = function (specifier, parentURL = referrer.url) {
|
|
703
|
+
return urlToPath(
|
|
704
|
+
loader._resolveArtifact(referrer, specifier, 'asset', toURL(parentURL, referrer.url))
|
|
705
|
+
)
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
require.addon = function (specifier = '.', parentURL = referrer.url) {
|
|
709
|
+
return loader._addon(referrer, specifier, toURL(parentURL, referrer.url))
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
require.addon.resolve = function (specifier = '.', parentURL = referrer.url) {
|
|
713
|
+
return urlToPath(
|
|
714
|
+
loader._resolveArtifact(referrer, specifier, 'addon', toURL(parentURL, referrer.url))
|
|
715
|
+
)
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
require.addon.host = Bare.Addon.host
|
|
719
|
+
|
|
720
|
+
return require
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
_importSync(specifier, parentURL) {
|
|
724
|
+
const url = this._resolveSync(specifier, parentURL, 'import')
|
|
725
|
+
|
|
726
|
+
let record = this._lookup(url)
|
|
727
|
+
|
|
728
|
+
if (record === null) {
|
|
729
|
+
this.linkSync(url, null, { importType: lex.constants.IMPORT })
|
|
730
|
+
|
|
731
|
+
record = this.get(url)
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
return record
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
_requireSync(specifier, parentURL, attributes = null) {
|
|
738
|
+
const url = this._resolveSync(specifier, parentURL, 'require')
|
|
739
|
+
|
|
740
|
+
let record = this._lookup(url)
|
|
741
|
+
|
|
742
|
+
if (record === null) {
|
|
743
|
+
this.linkSync(url, null, { attributes, importType: lex.constants.REQUIRE })
|
|
744
|
+
|
|
745
|
+
record = this.get(url)
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
if (record === null) {
|
|
749
|
+
throw errors.MODULE_NOT_FOUND(`Cannot find module '${specifier}'`, specifier, parentURL)
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
return record
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
_cacheResolution(parentURL, specifier, condition, url) {
|
|
756
|
+
let imports = this._resolutions[parentURL.href]
|
|
757
|
+
|
|
758
|
+
if (typeof imports !== 'object' || imports === null) {
|
|
759
|
+
imports = this._resolutions[parentURL.href] = Object.create(null)
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
if (specifier in imports) return
|
|
763
|
+
|
|
764
|
+
imports[specifier] = { [condition]: url.href }
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
_resolveOptions() {
|
|
768
|
+
const opts = { resolutions: this._resolutions, engines: Bare.versions }
|
|
769
|
+
|
|
770
|
+
if (this._builtins !== null) opts.builtins = Object.keys(this._builtins)
|
|
771
|
+
|
|
772
|
+
return opts
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
_resolveSync(specifier, parentURL, condition) {
|
|
776
|
+
const protocol = this._protocolFor(parentURL)
|
|
777
|
+
|
|
778
|
+
const resolver = traverse.resolve.bare(
|
|
779
|
+
resolveEntry(specifier, condition),
|
|
780
|
+
parentURL,
|
|
781
|
+
this._resolveOptions()
|
|
782
|
+
)
|
|
783
|
+
|
|
784
|
+
let next = resolver.next()
|
|
785
|
+
|
|
786
|
+
while (next.done !== true) {
|
|
787
|
+
const value = next.value
|
|
788
|
+
|
|
789
|
+
if (value.package) {
|
|
790
|
+
const href = value.package.href
|
|
791
|
+
|
|
792
|
+
let manifest = this._manifests[href]
|
|
793
|
+
|
|
794
|
+
if (manifest === undefined) {
|
|
795
|
+
const record = this.get(value.package)
|
|
796
|
+
|
|
797
|
+
const source = record ? record._source.bytes : readSync(protocol, value.package)
|
|
798
|
+
|
|
799
|
+
manifest = this._manifests[href] = source === null ? null : JSON.parse(source.toString())
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
next = resolver.next(manifest)
|
|
803
|
+
} else {
|
|
804
|
+
const url = value.resolution
|
|
805
|
+
|
|
806
|
+
if (url.protocol === 'builtin:' || url.protocol === 'data:' || this.get(url) !== null) {
|
|
807
|
+
this._cacheResolution(parentURL, specifier, condition, url)
|
|
808
|
+
|
|
809
|
+
return url
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
if (existsSync(protocol, url, condition)) {
|
|
813
|
+
const resolution = protocol.resolveSync(url)
|
|
814
|
+
|
|
815
|
+
this._cacheResolution(parentURL, specifier, condition, resolution)
|
|
816
|
+
|
|
817
|
+
return resolution
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
next = resolver.next(false)
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
throw notFound(condition, specifier, parentURL)
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
async _resolve(specifier, parentURL, condition) {
|
|
828
|
+
const protocol = this._protocolFor(parentURL)
|
|
829
|
+
|
|
830
|
+
const resolver = traverse.resolve.bare(
|
|
831
|
+
resolveEntry(specifier, condition),
|
|
832
|
+
parentURL,
|
|
833
|
+
this._resolveOptions()
|
|
834
|
+
)
|
|
835
|
+
|
|
836
|
+
let next = resolver.next()
|
|
837
|
+
|
|
838
|
+
while (next.done !== true) {
|
|
839
|
+
const value = next.value
|
|
840
|
+
|
|
841
|
+
if (value.package) {
|
|
842
|
+
const href = value.package.href
|
|
843
|
+
|
|
844
|
+
let manifest = this._manifests[href]
|
|
845
|
+
|
|
846
|
+
if (manifest === undefined) {
|
|
847
|
+
const record = this.get(value.package)
|
|
848
|
+
|
|
849
|
+
let source
|
|
850
|
+
|
|
851
|
+
if (record) {
|
|
852
|
+
source = record._source.bytes
|
|
853
|
+
} else {
|
|
854
|
+
source = read(protocol, value.package)
|
|
855
|
+
|
|
856
|
+
if (isThenable(source)) source = await source
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
manifest = this._manifests[href] = source === null ? null : JSON.parse(source.toString())
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
next = resolver.next(manifest)
|
|
863
|
+
} else {
|
|
864
|
+
const url = value.resolution
|
|
865
|
+
|
|
866
|
+
if (url.protocol === 'builtin:' || url.protocol === 'data:' || this.get(url) !== null) {
|
|
867
|
+
this._cacheResolution(parentURL, specifier, condition, url)
|
|
868
|
+
|
|
869
|
+
return url
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
if (await exists(protocol, url, condition)) {
|
|
873
|
+
let resolution = protocol.resolve(url)
|
|
874
|
+
|
|
875
|
+
if (isThenable(resolution)) resolution = await resolution
|
|
876
|
+
|
|
877
|
+
this._cacheResolution(parentURL, specifier, condition, resolution)
|
|
878
|
+
|
|
879
|
+
return resolution
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
next = resolver.next(false)
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
throw notFound(condition, specifier, parentURL)
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
_resolveArtifact(referrer, specifier, condition, parentURL = referrer.url) {
|
|
890
|
+
const url = parentURL === referrer.url ? referrer._resolve(specifier, condition) : null
|
|
891
|
+
|
|
892
|
+
return url === null ? this._resolveSync(specifier, parentURL, condition) : url
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
_addon(referrer, specifier, parentURL = referrer.url) {
|
|
896
|
+
const url = this._resolveArtifact(referrer, specifier, 'addon', parentURL)
|
|
897
|
+
|
|
898
|
+
const record = this._lookup(url) || this._addonRecord(url)
|
|
899
|
+
|
|
900
|
+
return record._evaluate()
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
function driveSync(generator, protocol, driveLink, answers = null) {
|
|
905
|
+
let next = generator.next()
|
|
906
|
+
|
|
907
|
+
while (next.done !== true) {
|
|
908
|
+
const command = next.value
|
|
909
|
+
|
|
910
|
+
if ('links' in command) {
|
|
911
|
+
for (const link of command.links) driveLink(link)
|
|
912
|
+
|
|
913
|
+
next = generator.next()
|
|
914
|
+
|
|
915
|
+
continue
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
let value
|
|
919
|
+
|
|
920
|
+
if ('module' in command) {
|
|
921
|
+
value = readSync(protocol, command.module)
|
|
922
|
+
} else if ('probe' in command) {
|
|
923
|
+
value = protocol.existsSync(command.probe)
|
|
924
|
+
} else if ('resolution' in command) {
|
|
925
|
+
value = resolveSync(protocol, command.resolution, answers)
|
|
926
|
+
} else {
|
|
927
|
+
value = listSync(protocol, command.prefix, command.expand, answers)
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
next = generator.next(value)
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
return next.value
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
async function drive(generator, protocol, driveLink, semaphore, answers = null) {
|
|
937
|
+
let next = generator.next()
|
|
938
|
+
|
|
939
|
+
while (next.done !== true) {
|
|
940
|
+
const command = next.value
|
|
941
|
+
|
|
942
|
+
if ('links' in command) {
|
|
943
|
+
await Promise.all(command.links.map(driveLink))
|
|
944
|
+
|
|
945
|
+
next = generator.next()
|
|
946
|
+
|
|
947
|
+
continue
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
if (semaphore !== null) await semaphore.wait()
|
|
951
|
+
|
|
952
|
+
let value
|
|
953
|
+
|
|
954
|
+
try {
|
|
955
|
+
if ('module' in command) {
|
|
956
|
+
value = read(protocol, command.module)
|
|
957
|
+
} else if ('probe' in command) {
|
|
958
|
+
value = protocol.exists(command.probe)
|
|
959
|
+
} else if ('resolution' in command) {
|
|
960
|
+
value = resolve(protocol, command.resolution, answers)
|
|
961
|
+
} else {
|
|
962
|
+
value = list(protocol, command.prefix, command.expand, answers)
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
if (isThenable(value)) value = await value
|
|
966
|
+
} finally {
|
|
967
|
+
if (semaphore !== null) semaphore.signal()
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
next = generator.next(value)
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
return next.value
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
function notFound(condition, specifier, parentURL) {
|
|
977
|
+
switch (condition) {
|
|
978
|
+
case 'addon':
|
|
979
|
+
return errors.ADDON_NOT_FOUND(`Cannot find addon '${specifier}'`, specifier, parentURL)
|
|
980
|
+
case 'asset':
|
|
981
|
+
return errors.ASSET_NOT_FOUND(`Cannot find asset '${specifier}'`, specifier, parentURL)
|
|
982
|
+
default:
|
|
983
|
+
return errors.MODULE_NOT_FOUND(`Cannot find module '${specifier}'`, specifier, parentURL)
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
function claimCache(loader) {
|
|
988
|
+
const owner = cacheOwners.get(loader._cache)
|
|
989
|
+
|
|
990
|
+
if (owner === undefined) {
|
|
991
|
+
cacheOwners.set(loader._cache, {
|
|
992
|
+
protocol: loader._protocol,
|
|
993
|
+
builtins: loader._builtins
|
|
994
|
+
})
|
|
995
|
+
|
|
996
|
+
return
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
if (owner.protocol !== loader._protocol) {
|
|
1000
|
+
throw errors.CACHE_INCOMPATIBLE('Cache is read through a different protocol')
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
if (owner.builtins !== loader._builtins) {
|
|
1004
|
+
throw errors.CACHE_INCOMPATIBLE('Cache is read with different builtins')
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
function assertCompatible(loader, href, record) {
|
|
1009
|
+
const other = record._loader
|
|
1010
|
+
|
|
1011
|
+
if (other._protocol !== loader._protocol) {
|
|
1012
|
+
throw errors.CACHE_INCOMPATIBLE(
|
|
1013
|
+
`Cache holds '${href}', which was read through a different protocol`
|
|
1014
|
+
)
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
if (other._builtins !== loader._builtins) {
|
|
1018
|
+
throw errors.CACHE_INCOMPATIBLE(`Cache holds '${href}', which was read with different builtins`)
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
function readSync(protocol, url) {
|
|
1023
|
+
if (protocol.existsSync(url) === false) return null
|
|
1024
|
+
|
|
1025
|
+
return protocol.readSync(url)
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
function read(protocol, url) {
|
|
1029
|
+
const exists = protocol.exists(url)
|
|
1030
|
+
|
|
1031
|
+
if (isThenable(exists)) {
|
|
1032
|
+
return exists.then((exists) => (exists === false ? null : protocol.read(url)))
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
return exists === false ? null : protocol.read(url)
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
function existsSync(protocol, url, condition) {
|
|
1039
|
+
if (condition === 'asset') return listFirstSync(protocol, url) !== null
|
|
1040
|
+
|
|
1041
|
+
return protocol.existsSync(url)
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
async function exists(protocol, url, condition) {
|
|
1045
|
+
if (condition === 'asset') return (await listFirst(protocol, url)) !== null
|
|
1046
|
+
|
|
1047
|
+
return protocol.exists(url)
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
function listFirstSync(protocol, url) {
|
|
1051
|
+
for (const found of protocol.listSync(url)) return found
|
|
1052
|
+
|
|
1053
|
+
return protocol.existsSync(url) ? url : null
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
async function listFirst(protocol, url) {
|
|
1057
|
+
for await (const found of protocol.list(url)) return found
|
|
1058
|
+
|
|
1059
|
+
return (await protocol.exists(url)) ? url : null
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
function answered(answers, kind, url) {
|
|
1063
|
+
return answers === null ? undefined : answers[kind].get(url.href)
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
function answeredSync(answers, kind, url) {
|
|
1067
|
+
const value = answered(answers, kind, url)
|
|
1068
|
+
|
|
1069
|
+
return isThenable(value) ? undefined : value
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
function answer(answers, kind, url, value) {
|
|
1073
|
+
if (answers === null) return value
|
|
1074
|
+
|
|
1075
|
+
const cache = answers[kind]
|
|
1076
|
+
|
|
1077
|
+
if (isThenable(value)) {
|
|
1078
|
+
value = value.then((value) => {
|
|
1079
|
+
cache.set(url.href, value)
|
|
1080
|
+
|
|
1081
|
+
return value
|
|
1082
|
+
})
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
cache.set(url.href, value)
|
|
1086
|
+
|
|
1087
|
+
return value
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
function resolveSync(protocol, url, answers) {
|
|
1091
|
+
const known = answeredSync(answers, 'resolved', url)
|
|
1092
|
+
|
|
1093
|
+
if (known !== undefined) return known
|
|
1094
|
+
|
|
1095
|
+
return answer(answers, 'resolved', url, protocol.resolveSync(url))
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
function resolve(protocol, url, answers) {
|
|
1099
|
+
const known = answered(answers, 'resolved', url)
|
|
1100
|
+
|
|
1101
|
+
if (known !== undefined) return known
|
|
1102
|
+
|
|
1103
|
+
return answer(answers, 'resolved', url, protocol.resolve(url))
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
function listSync(protocol, url, expand, answers) {
|
|
1107
|
+
if (expand === false) {
|
|
1108
|
+
const known = answeredSync(answers, 'listed', url)
|
|
1109
|
+
|
|
1110
|
+
if (known !== undefined) return known
|
|
1111
|
+
|
|
1112
|
+
const found = listFirstSync(protocol, url)
|
|
1113
|
+
|
|
1114
|
+
return answer(answers, 'listed', url, found === null ? [] : [found])
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
const listing = protocol.listSync(url)
|
|
1118
|
+
|
|
1119
|
+
const urls = []
|
|
1120
|
+
|
|
1121
|
+
for (const found of listing) urls.push(found)
|
|
1122
|
+
|
|
1123
|
+
if (urls.length === 0) return protocol.existsSync(url) ? [url] : []
|
|
1124
|
+
|
|
1125
|
+
return resolved(urls, listing)
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
function list(protocol, url, expand, answers) {
|
|
1129
|
+
if (expand === false) {
|
|
1130
|
+
const known = answered(answers, 'listed', url)
|
|
1131
|
+
|
|
1132
|
+
if (known !== undefined) return known
|
|
1133
|
+
|
|
1134
|
+
return answer(
|
|
1135
|
+
answers,
|
|
1136
|
+
'listed',
|
|
1137
|
+
url,
|
|
1138
|
+
listFirst(protocol, url).then((found) => (found === null ? [] : [found]))
|
|
1139
|
+
)
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
return collect(protocol, url)
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
async function collect(protocol, url) {
|
|
1146
|
+
const listing = protocol.list(url)
|
|
1147
|
+
|
|
1148
|
+
const urls = []
|
|
1149
|
+
|
|
1150
|
+
for await (const found of listing) urls.push(found)
|
|
1151
|
+
|
|
1152
|
+
if (urls.length === 0) return (await protocol.exists(url)) ? [url] : []
|
|
1153
|
+
|
|
1154
|
+
return resolved(urls, listing)
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
function resolved(urls, listing) {
|
|
1158
|
+
urls.resolved = listing.resolved === true
|
|
1159
|
+
|
|
1160
|
+
return urls
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
function resolveEntry(specifier, condition) {
|
|
1164
|
+
let type = 0
|
|
1165
|
+
|
|
1166
|
+
if (condition === 'require') type = lex.constants.REQUIRE
|
|
1167
|
+
else if (condition === 'import') type = lex.constants.IMPORT
|
|
1168
|
+
else if (condition === 'asset') type = lex.constants.ASSET
|
|
1169
|
+
else if (condition === 'addon') type = lex.constants.ADDON
|
|
1170
|
+
|
|
1171
|
+
return { type, specifier, names: [], attributes: {}, position: [0, 0, 0] }
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
function toURL(value, base) {
|
|
1175
|
+
if (isURL(value)) return value
|
|
1176
|
+
|
|
1177
|
+
if (startsWithWindowsDriveLetter(value)) return pathToFileURL(value)
|
|
1178
|
+
|
|
1179
|
+
return URL.parse(value, base) || pathToFileURL(value)
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
function bundleOwns(bundle, url) {
|
|
1183
|
+
const root = bundle.root
|
|
1184
|
+
|
|
1185
|
+
if (typeof root === 'string' && url.href.startsWith(root) === false) return false
|
|
1186
|
+
|
|
1187
|
+
return bundle.exists(url.href)
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
function isThenable(value) {
|
|
1191
|
+
return value !== null && typeof value === 'object' && typeof value.then === 'function'
|
|
1192
|
+
}
|