bare-module 6.3.2 → 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.
Files changed (38) hide show
  1. package/README.md +237 -270
  2. package/binding.c +654 -331
  3. package/index.d.ts +156 -63
  4. package/index.js +88 -975
  5. package/lib/errors.js +27 -10
  6. package/lib/loader.d.ts +56 -0
  7. package/lib/loader.js +1192 -0
  8. package/lib/module/addon.js +7 -0
  9. package/lib/module/binary.js +7 -0
  10. package/lib/module/builtin.js +19 -0
  11. package/lib/module/bundle.js +47 -0
  12. package/lib/module/json.js +19 -0
  13. package/lib/module/script.js +23 -0
  14. package/lib/module/source-text.js +13 -0
  15. package/lib/module/synthetic.js +20 -0
  16. package/lib/module/text.js +7 -0
  17. package/lib/module/value.js +27 -0
  18. package/lib/module.js +392 -0
  19. package/lib/protocol.d.ts +56 -8
  20. package/lib/protocol.js +93 -26
  21. package/lib/source.js +18 -0
  22. package/lib/url.js +52 -0
  23. package/package.json +19 -8
  24. package/prebuilds/android-arm/bare-module.bare +0 -0
  25. package/prebuilds/android-arm64/bare-module.bare +0 -0
  26. package/prebuilds/android-ia32/bare-module.bare +0 -0
  27. package/prebuilds/android-x64/bare-module.bare +0 -0
  28. package/prebuilds/darwin-arm64/bare-module.bare +0 -0
  29. package/prebuilds/darwin-x64/bare-module.bare +0 -0
  30. package/prebuilds/ios-arm64/bare-module.bare +0 -0
  31. package/prebuilds/ios-arm64-simulator/bare-module.bare +0 -0
  32. package/prebuilds/ios-x64-simulator/bare-module.bare +0 -0
  33. package/prebuilds/linux-arm64/bare-module.bare +0 -0
  34. package/prebuilds/linux-x64/bare-module.bare +0 -0
  35. package/prebuilds/win32-arm64/bare-module.bare +0 -0
  36. package/prebuilds/win32-x64/bare-module.bare +0 -0
  37. package/lib/constants.d.ts +0 -19
  38. package/lib/constants.js +0 -17
package/index.js CHANGED
@@ -1,1037 +1,150 @@
1
- const path = require('bare-path')
2
- const resolve = require('bare-module-resolve')
3
- const lex = require('bare-module-lexer')
4
- const { isURL, fileURLToPath, pathToFileURL } = require('bare-url')
5
- const Bundle = require('bare-bundle')
6
- const Protocol = require('./lib/protocol')
7
- const constants = require('./lib/constants')
8
- const errors = require('./lib/errors')
9
- const binding = require('./binding')
1
+ const { isURL, pathToFileURL } = require('bare-url')
2
+ const { constants } = require('bare-module-traverse')
3
+ const Module = require('./lib/module')
4
+ const ModuleProtocol = require('./lib/protocol')
5
+ const ModuleLoader = require('./lib/loader')
6
+ const ModuleSource = require('./lib/source')
10
7
 
11
- const kind = Symbol.for('bare.module.kind')
8
+ module.exports = exports = Module
12
9
 
13
- const isWindows = Bare.platform === 'win32'
10
+ exports.Protocol = ModuleProtocol
11
+ exports.Loader = ModuleLoader
14
12
 
15
- const { startsWithWindowsDriveLetter } = resolve
16
-
17
- module.exports = exports = class Module {
18
- static get [kind]() {
19
- return 1 // Compatibility version
20
- }
21
-
22
- constructor(url) {
23
- this._url = url
24
- this._state = 0
25
- this._type = 0
26
- this._defaultType = 0
27
- this._main = null
28
- this._exports = null
29
- this._imports = null
30
- this._resolutions = null
31
- this._builtins = null
32
- this._conditions = null
33
- this._protocol = null
34
- this._cache = null
35
- this._source = null
36
- this._function = null
37
- this._names = null
38
- this._promise = null
39
- this._handle = null
40
-
41
- Object.preventExtensions(this)
42
- }
43
-
44
- get [kind]() {
45
- return Module[kind]
46
- }
47
-
48
- get url() {
49
- return this._url
50
- }
51
-
52
- get type() {
53
- return this._type
54
- }
55
-
56
- get defaultType() {
57
- return this._defaultType
58
- }
59
-
60
- get main() {
61
- return this._main
62
- }
63
-
64
- get exports() {
65
- return this._exports
66
- }
67
-
68
- set exports(value) {
69
- this._exports = value
70
- }
71
-
72
- get imports() {
73
- return this._imports
74
- }
75
-
76
- get resolutions() {
77
- return this._resolutions
78
- }
79
-
80
- get builtins() {
81
- return this._builtins
82
- }
83
-
84
- get conditions() {
85
- return Array.from(this._conditions)
86
- }
87
-
88
- get protocol() {
89
- return this._protocol
90
- }
91
-
92
- get cache() {
93
- return this._cache
94
- }
95
-
96
- // For Node.js compatibility
97
- get filename() {
98
- return urlToPath(this._url)
99
- }
100
-
101
- // For Node.js compatibility
102
- get dirname() {
103
- return urlToDirname(this._url)
104
- }
105
-
106
- // For Node.js compatibility
107
- get id() {
108
- return this.filename
109
- }
110
-
111
- // For Node.js compatibility
112
- get path() {
113
- return this.dirname
114
- }
115
-
116
- _transform(isImport, isDynamicImport) {
117
- if (isDynamicImport) {
118
- this._run()
119
- } else if (isImport) {
120
- this._synthesize()
121
- } else {
122
- this._evaluate()
123
- }
124
-
125
- return this
126
- }
127
-
128
- _synthesize() {
129
- if ((this._state & constants.states.SYNTHESIZED) !== 0) return
130
-
131
- this._state |= constants.states.SYNTHESIZED
132
-
133
- if (this._type === constants.types.MODULE) return
134
-
135
- const names = new Set(['default'])
136
- const queue = [this]
137
- const seen = new Set()
138
-
139
- while (queue.length) {
140
- const module = queue.pop()
141
-
142
- if (seen.has(module)) continue
143
-
144
- seen.add(module)
145
-
146
- switch (module._type) {
147
- case constants.types.SCRIPT: {
148
- const result = lex(module._source)
149
-
150
- for (const { name } of result.exports) names.add(name)
151
-
152
- const referrer = module
153
-
154
- for (const { specifier, type } of result.imports) {
155
- if (
156
- (type & lex.constants.REEXPORT) !== 0 &&
157
- (type & lex.constants.ADDON) === 0 &&
158
- (type & lex.constants.ASSET) === 0
159
- ) {
160
- const resolved = Module.resolve(specifier, referrer._url, {
161
- isImport: true,
162
- referrer
163
- })
164
-
165
- const module = Module.load(resolved, {
166
- isImport: true,
167
- referrer
168
- })
169
-
170
- if (module._names) {
171
- for (const name of module._names) names.add(name)
172
- } else {
173
- queue.push(module)
174
- }
175
- }
176
- }
177
-
178
- break
179
- }
180
-
181
- case constants.types.MODULE:
182
- module._evaluate()
183
-
184
- for (const name of Object.keys(module._exports)) names.add(name)
185
-
186
- break
187
-
188
- case constants.types.JSON:
189
- for (const name of Object.keys(module._exports)) names.add(name)
190
- }
191
- }
192
-
193
- this._names = Array.from(names)
194
-
195
- this._handle = binding.createSyntheticModule(this._url.href, this._names, Module._handle)
196
- }
197
-
198
- _evaluate() {
199
- if ((this._state & constants.states.EVALUATED) !== 0) return
200
-
201
- this._state |= constants.states.EVALUATED
202
-
203
- if (this._type === constants.types.SCRIPT) {
204
- const require = exports.createRequire(this._url, { module: this })
205
-
206
- this._exports = {}
207
-
208
- const fn = this._function // Bind to variable to ensure proper stack trace
209
-
210
- fn(require, this, this._exports, urlToPath(this._url), urlToDirname(this._url))
211
- } else if (this._type === constants.types.MODULE) {
212
- this._run()
213
-
214
- this._exports = binding.getNamespace(this._handle)
215
- }
216
- }
217
-
218
- _run() {
219
- if ((this._state & constants.states.RUN) !== 0) return
220
-
221
- this._state |= constants.states.RUN
222
-
223
- this._synthesize()
224
-
225
- this._promise = binding.runModule(this._handle, Module._handle, Module._onrun)
226
- }
227
-
228
- [Symbol.for('bare.inspect')]() {
229
- return {
230
- __proto__: { constructor: Module },
231
-
232
- url: this.url,
233
- type: this.type,
234
- defaultType: this.defaultType,
235
- main: this.main,
236
- exports: this.exports,
237
- imports: this.imports,
238
- resolutions: this.resolutions,
239
- builtins: this.builtins,
240
- conditions: this.conditions
241
- }
242
- }
243
-
244
- static _extensions = Object.create(null)
245
-
246
- static _protocol = null
247
-
248
- static _cache =
249
- module[kind] === Module[kind] ? module.cache || Object.create(null) : Object.create(null)
250
-
251
- static _conditions = ['bare', 'node', ...Bare.Addon.host.split('-')]
252
-
253
- static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
254
-
255
- static _onimport(specifier, attributes, referrerHref, isDynamicImport) {
256
- const referrer = this._cache[referrerHref] || null
257
-
258
- if (referrer === null) {
259
- throw errors.MODULE_NOT_FOUND(
260
- `Cannot find referrer for module '${specifier}' imported from '${referrerHref}'`,
261
- specifier,
262
- referrer
263
- )
264
- }
265
-
266
- const resolved = this.resolve(specifier, referrer._url, {
267
- isImport: true,
268
- referrer,
269
- attributes
270
- })
271
-
272
- const module = this.load(resolved, {
273
- isImport: true,
274
- isDynamicImport,
275
- referrer,
276
- attributes
277
- })
278
-
279
- return isDynamicImport
280
- ? module._promise.then(() => binding.getNamespace(module._handle))
281
- : module._handle
282
- }
283
-
284
- static _onevaluate(href) {
285
- const module = this._cache[href] || null
286
-
287
- if (module === null) {
288
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`, href)
289
- }
290
-
291
- module._evaluate()
292
-
293
- for (const name of module._names) {
294
- let value
295
-
296
- if (
297
- name === 'default' &&
298
- (typeof module._exports !== 'object' ||
299
- module._exports === null ||
300
- name in module._exports === false)
301
- ) {
302
- value = module._exports
303
- } else {
304
- value = module._exports[name]
305
- }
306
-
307
- binding.setExport(module._handle, name, value)
308
- }
309
- }
310
-
311
- static _onmeta(href, meta) {
312
- const self = Module
313
-
314
- const module = this._cache[href] || null
315
-
316
- if (module === null) {
317
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`, href)
318
- }
319
-
320
- const referrer = module
321
-
322
- meta.url = module._url.href
323
- meta.main = module._main === module
324
- meta.cache = module._cache
325
-
326
- meta.dirname = module.dirname // For Node.js compatibility
327
- meta.filename = module.filename // For Node.js compatibility
328
-
329
- meta.resolve = function resolve(specifier, parentURL = referrer._url) {
330
- return self.resolve(specifier, toURL(parentURL, referrer._url), {
331
- referrer
332
- }).href
333
- }
334
-
335
- meta.addon = function addon(specifier = '.', parentURL = referrer._url) {
336
- const resolved = Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
337
-
338
- const addon = Bare.Addon.load(resolved, { referrer })
339
-
340
- return addon._exports
341
- }
342
-
343
- meta.addon.resolve = function resolve(specifier = '.', parentURL = referrer._url) {
344
- return Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
345
- referrer
346
- }).href
347
- }
348
-
349
- meta.addon.host = Bare.Addon.host
350
-
351
- meta.asset = function asset(specifier, parentURL = referrer._url) {
352
- return self.asset(specifier, toURL(parentURL, referrer._url), {
353
- referrer
354
- }).href
355
- }
356
- }
357
-
358
- static _onrun(reason, promise, err = reason) {
359
- if (err) {
360
- promise.catch(() => {}) // Don't leak the rejection
361
-
362
- throw err
363
- } else {
364
- promise.catch((err) =>
365
- queueMicrotask(() => {
366
- throw err
367
- })
368
- )
369
- }
370
- }
371
-
372
- static get protocol() {
373
- return this._protocol
374
- }
375
-
376
- static get cache() {
377
- return this._cache
378
- }
379
-
380
- static load(url, source = null, opts = {}) {
381
- const self = Module
382
-
383
- if (
384
- !ArrayBuffer.isView(source) &&
385
- !Bundle.isBundle(source) &&
386
- typeof source !== 'string' &&
387
- source !== null
388
- ) {
389
- opts = source
390
- source = null
391
- }
392
-
393
- const {
394
- isImport = false,
395
- isDynamicImport = false,
396
- referrer = null,
397
- attributes,
398
- type = typeForAttributes(attributes),
399
- defaultType = referrer ? referrer._defaultType : 0,
400
- cache = referrer ? referrer._cache : self._cache,
401
- main = referrer ? referrer._main : null,
402
- protocol = referrer ? referrer._protocol : self._protocol,
403
- imports = referrer ? referrer._imports : null,
404
- resolutions = referrer ? referrer._resolutions : null,
405
- builtins = referrer ? referrer._builtins : null,
406
- conditions = referrer ? referrer._conditions : self._conditions
407
- } = opts
408
-
409
- let module = cache[url.href] || null
410
-
411
- if (module !== null) {
412
- if (type !== 0 && type !== module._type) {
413
- throw errors.TYPE_INCOMPATIBLE(
414
- `Module '${module.url.href}' is not of type '${nameOfType(type)}'`
415
- )
416
- }
417
-
418
- return module._transform(isImport, isDynamicImport)
419
- }
420
-
421
- module = cache[url.href] = new Module(url)
422
-
423
- try {
424
- switch (url.protocol) {
425
- case 'builtin:':
426
- module._exports = builtins[url.pathname]
427
- break
428
-
429
- default: {
430
- module._defaultType = defaultType
431
- module._cache = cache
432
- module._main = main || module
433
- module._protocol = protocol
434
- module._imports = imports
435
- module._resolutions = resolutions
436
- module._builtins = builtins
437
- module._conditions = conditions
438
-
439
- if (
440
- typeof attributes === 'object' &&
441
- attributes !== null &&
442
- typeof attributes.imports === 'string'
443
- ) {
444
- const resolved = self.resolve(attributes.imports, referrer._url, {
445
- referrer: module
446
- })
447
-
448
- const imports = self.load(resolved, {
449
- referrer: module,
450
- type: constants.types.JSON
451
- })
452
-
453
- module._imports = mixinImports(module._imports, imports._exports, resolved)
454
- }
455
-
456
- let extension = canonicalExtensionForType(type) || path.extname(url.pathname)
457
-
458
- if (extension in self._extensions === false) {
459
- if (defaultType) {
460
- extension = canonicalExtensionForType(defaultType) || '.js'
461
- } else {
462
- extension = '.js'
463
- }
464
- }
465
-
466
- self._extensions[extension](module, source, referrer)
467
- }
468
- }
13
+ exports.constants = constants
469
14
 
470
- return module._transform(isImport, isDynamicImport)
471
- } catch (err) {
472
- delete cache[url.href]
15
+ exports.load = async function load(url, source = null, opts = {}) {
16
+ if (typeof url === 'string') url = new URL(url)
473
17
 
474
- throw err
475
- }
18
+ if (source !== null && typeof source !== 'string' && !ArrayBuffer.isView(source)) {
19
+ opts = source
20
+ source = null
476
21
  }
477
22
 
478
- static resolve(specifier, parentURL, opts = {}) {
479
- const self = Module
480
-
481
- if (typeof specifier !== 'string') {
482
- throw new TypeError(
483
- `Specifier must be a string. Received type ${typeof specifier} (${specifier})`
484
- )
485
- }
486
-
487
- const {
488
- isImport = false,
489
- referrer = null,
490
- attributes,
491
- type = typeForAttributes(attributes),
492
- extensions = extensionsForType(type),
493
- protocol = referrer ? referrer._protocol : self._protocol,
494
- imports = referrer ? referrer._imports : null,
495
- resolutions = referrer ? referrer._resolutions : null,
496
- builtins = referrer ? referrer._builtins : null,
497
- conditions = referrer ? referrer._conditions : self._conditions
498
- } = opts
499
-
500
- const resolved = protocol.preresolve(specifier, parentURL)
501
-
502
- const [resolution] = protocol.resolve(resolved, parentURL, imports)
503
-
504
- if (resolution) return protocol.postresolve(resolution)
505
-
506
- const candidates = []
23
+ opts = optionsFor(opts)
507
24
 
508
- for (const resolution of resolve(
509
- resolved,
510
- parentURL,
511
- {
512
- conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
513
- imports,
514
- resolutions,
515
- extensions,
516
- builtins: builtins ? Object.keys(builtins) : [],
517
- engines: Bare.versions
518
- },
519
- readPackage
520
- )) {
521
- candidates.push(resolution)
25
+ const loader = loaderFor(opts)
522
26
 
523
- switch (resolution.protocol) {
524
- case 'builtin:':
525
- return resolution
526
- default:
527
- if (protocol.exists(resolution, type)) {
528
- return protocol.postresolve(resolution)
529
- }
530
- }
531
- }
532
-
533
- let message = `Cannot find module '${specifier}' imported from '${parentURL.href}'`
534
-
535
- if (candidates.length > 0) {
536
- message += '\nCandidates:'
537
- message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
538
- }
27
+ const record = await loader.link(url, source, opts)
539
28
 
540
- throw errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
29
+ return loader._evaluate(record)
30
+ }
541
31
 
542
- function readPackage(packageURL) {
543
- if (protocol.exists(packageURL, constants.types.JSON)) {
544
- return Module.load(packageURL, { protocol })._exports
545
- }
32
+ exports.loadSync = function loadSync(url, source = null, opts = {}) {
33
+ if (typeof url === 'string') url = new URL(url)
546
34
 
547
- return null
548
- }
35
+ if (source !== null && typeof source !== 'string' && !ArrayBuffer.isView(source)) {
36
+ opts = source
37
+ source = null
549
38
  }
550
39
 
551
- static asset(specifier, parentURL, opts = {}) {
552
- const self = Module
553
-
554
- if (typeof specifier !== 'string') {
555
- throw new TypeError(
556
- `Specifier must be a string. Received type ${typeof specifier} (${specifier})`
557
- )
558
- }
559
-
560
- const {
561
- referrer = null,
562
- protocol = referrer ? referrer._protocol : self._protocol,
563
- imports = referrer ? referrer._imports : null,
564
- resolutions = referrer ? referrer._resolutions : null,
565
- conditions = referrer ? referrer._conditions : self._conditions
566
- } = opts
567
-
568
- const resolved = protocol.preresolve(specifier, parentURL)
569
-
570
- const [resolution] = protocol.resolve(resolved, parentURL, imports)
571
-
572
- if (resolution) return protocol.postresolve(resolution)
573
-
574
- const candidates = []
40
+ opts = optionsFor(opts)
575
41
 
576
- for (const resolution of resolve(
577
- resolved,
578
- parentURL,
579
- {
580
- conditions: ['asset', ...conditions],
581
- imports,
582
- resolutions,
583
- engines: Bare.versions
584
- },
585
- readPackage
586
- )) {
587
- candidates.push(resolution)
588
-
589
- if (protocol.exists(resolution, constants.types.ASSET)) {
590
- return protocol.postresolve(protocol.asset ? protocol.asset(resolution) : resolution)
591
- }
592
- }
593
-
594
- let message = `Cannot find asset '${specifier}' imported from '${parentURL.href}'`
595
-
596
- if (candidates.length > 0) {
597
- message += '\nCandidates:'
598
- message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
599
- }
600
-
601
- throw errors.ASSET_NOT_FOUND(message, specifier, parentURL, candidates)
602
-
603
- function readPackage(packageURL) {
604
- if (protocol.exists(packageURL, constants.types.JSON)) {
605
- return Module.load(packageURL, { protocol })._exports
606
- }
607
-
608
- return null
609
- }
610
- }
611
- }
42
+ const loader = loaderFor(opts)
612
43
 
613
- const Module = exports
44
+ const record = loader.linkSync(url, source, opts)
614
45
 
615
- function extensionsForType(type) {
616
- switch (type) {
617
- case constants.types.SCRIPT:
618
- return ['.js', '.cjs']
619
- case constants.types.MODULE:
620
- return ['.js', '.mjs']
621
- case constants.types.JSON:
622
- return ['.json']
623
- case constants.types.BUNDLE:
624
- return ['.bundle']
625
- case constants.types.ADDON:
626
- return ['.bare', '.node']
627
- case constants.types.BINARY:
628
- return ['.bin']
629
- case constants.types.TEXT:
630
- return ['.txt']
631
- default:
632
- return ['.js', '.cjs', '.mjs', '.json', '.bare', '.node']
633
- }
634
- }
46
+ record._evaluate()
635
47
 
636
- function canonicalExtensionForType(type) {
637
- switch (type) {
638
- case constants.types.SCRIPT:
639
- return '.cjs'
640
- case constants.types.MODULE:
641
- return '.esm'
642
- case constants.types.JSON:
643
- return '.json'
644
- case constants.types.BUNDLE:
645
- return '.bundle'
646
- case constants.types.ADDON:
647
- return '.bare'
648
- case constants.types.BINARY:
649
- return '.bin'
650
- case constants.types.TEXT:
651
- return '.txt'
652
- default:
653
- return null
654
- }
48
+ return record
655
49
  }
656
50
 
657
- function nameOfType(type) {
658
- switch (type) {
659
- case constants.types.SCRIPT:
660
- return 'script'
661
- case constants.types.MODULE:
662
- return 'module'
663
- case constants.types.JSON:
664
- return 'json'
665
- case constants.types.BUNDLE:
666
- return 'bundle'
667
- case constants.types.ADDON:
668
- return 'bare'
669
- case constants.types.BINARY:
670
- return 'binary'
671
- case constants.types.TEXT:
672
- return 'text'
673
- default:
674
- return null
51
+ exports.resolve = async function resolve(specifier, parentURL, condition = 'require', opts = {}) {
52
+ if (typeof condition === 'object' && condition !== null) {
53
+ opts = condition
54
+ condition = 'require'
675
55
  }
676
- }
677
56
 
678
- function typeForAttributes(attributes) {
679
- if (typeof attributes !== 'object' || attributes === null) return 0
57
+ const { loader, parent } = resolveWith(specifier, parentURL, condition, optionsFor(opts))
680
58
 
681
- switch (attributes.type) {
682
- case 'script':
683
- return constants.types.SCRIPT
684
- case 'module':
685
- return constants.types.MODULE
686
- case 'json':
687
- return constants.types.JSON
688
- case 'bundle':
689
- return constants.types.BUNDLE
690
- case 'addon':
691
- return constants.types.ADDON
692
- case 'binary':
693
- return constants.types.BINARY
694
- case 'text':
695
- return constants.types.TEXT
696
- default:
697
- return 0
698
- }
59
+ return await loader._resolve(specifier, parent, condition)
699
60
  }
700
61
 
701
- function mixinImports(target, imports, url) {
702
- if (typeof imports === 'object' && imports !== null && 'imports' in imports) {
703
- imports = imports.imports
704
- }
705
-
706
- if (typeof imports !== 'object' || imports === null) {
707
- throw errors.INVALID_IMPORTS_MAP(`Imports map at '${url.href}' is not valid`)
62
+ exports.resolveSync = function resolveSync(specifier, parentURL, condition = 'require', opts = {}) {
63
+ if (typeof condition === 'object' && condition !== null) {
64
+ opts = condition
65
+ condition = 'require'
708
66
  }
709
67
 
710
- return { ...target, ...imports }
711
- }
712
-
713
- exports.Protocol = Protocol
714
-
715
- exports.constants = constants
716
-
717
- // For Node.js compatibility
718
- exports.builtinModules = []
68
+ const { loader, parent } = resolveWith(specifier, parentURL, condition, optionsFor(opts))
719
69
 
720
- // For Node.js compatibility
721
- exports.isBuiltin = function isBuiltin() {
722
- return false
70
+ return loader._resolveSync(specifier, parent, condition)
723
71
  }
724
72
 
725
- exports.createRequire = function createRequire(parentURL, opts = {}) {
726
- const self = Module
727
-
728
- let {
729
- module = null,
730
- referrer = null,
731
- type = constants.types.SCRIPT,
732
- defaultType = referrer ? referrer._defaultType : constants.types.SCRIPT,
733
- cache = referrer ? referrer._cache : self._cache,
734
- main = referrer ? referrer._main : null,
735
- protocol = referrer ? referrer._protocol : self._protocol,
736
- imports = referrer ? referrer._imports : null,
737
- resolutions = referrer ? referrer._resolutions : null,
738
- builtins = referrer ? referrer._builtins : null,
739
- conditions = referrer ? referrer._conditions : self._conditions
740
- } = opts
741
-
742
- if (module === null) {
743
- module = new Module(toURL(parentURL))
744
-
745
- module._type = type
746
- module._defaultType = defaultType
747
- module._cache = cache
748
- module._main = main || module
749
- module._protocol = protocol
750
- module._imports = imports
751
- module._resolutions = resolutions
752
- module._builtins = builtins
753
- module._conditions = conditions
754
- }
755
-
756
- referrer = module
757
-
758
- function require(specifier, opts = {}) {
759
- const attributes = opts && opts.with
760
-
761
- const resolved = self.resolve(specifier, referrer._url, {
762
- referrer,
763
- attributes
764
- })
765
-
766
- const module = self.load(resolved, { referrer, attributes })
767
-
768
- return module._exports
769
- }
770
-
771
- require.main = module._main
772
- require.cache = module._cache
773
-
774
- require.resolve = function resolve(specifier, parentURL = referrer._url) {
775
- return urlToPath(self.resolve(specifier, toURL(parentURL, referrer._url), { referrer }))
776
- }
777
-
778
- require.addon = function addon(specifier = '.', parentURL = referrer._url) {
779
- const resolved = Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
780
-
781
- const addon = Bare.Addon.load(resolved, { referrer })
782
-
783
- return addon._exports
784
- }
73
+ const conditions = ['require', 'import', 'asset', 'addon']
785
74
 
786
- require.addon.host = Bare.Addon.host
787
-
788
- require.addon.resolve = function resolve(specifier = '.', parentURL = referrer._url) {
789
- return urlToPath(
790
- Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
791
- referrer
792
- })
75
+ function resolveWith(specifier, parentURL, condition, opts) {
76
+ if (typeof specifier !== 'string') {
77
+ throw new TypeError(
78
+ `Specifier must be a string. Received type ${typeof specifier} (${specifier})`
793
79
  )
794
80
  }
795
81
 
796
- require.asset = function asset(specifier, parentURL = referrer._url) {
797
- return urlToPath(self.asset(specifier, toURL(parentURL, referrer._url), { referrer }))
82
+ if (conditions.includes(condition) === false) {
83
+ throw new TypeError(
84
+ `Condition must be one of ${conditions.join(', ')}. Received type ${typeof condition} (${condition})`
85
+ )
798
86
  }
799
87
 
800
- return require
801
- }
88
+ const referrer = opts.referrer || null
802
89
 
803
- Module._extensions['.js'] = function (module, source, referrer) {
804
- const self = Module
805
-
806
- const protocol = module._protocol
807
- const resolutions = module._resolutions
808
-
809
- let pkg
810
-
811
- for (const packageURL of resolve.lookupPackageScope(module._url, {
812
- resolutions
813
- })) {
814
- if (self._cache[packageURL.href]) {
815
- pkg = self._cache[packageURL.href]
816
- break
817
- }
818
-
819
- if (protocol.exists(packageURL, constants.types.JSON)) {
820
- pkg = self.load(packageURL, { protocol })
821
- break
822
- }
90
+ if (parentURL === undefined || parentURL === null) {
91
+ parentURL = referrer ? referrer.url : pathToFileURL('./')
92
+ } else if (typeof parentURL === 'string') {
93
+ parentURL = new URL(parentURL)
823
94
  }
824
95
 
825
- const info = (pkg && pkg._exports) || {}
826
-
827
- const isESM =
828
- // The default type is ES modules.
829
- constants.types.MODULE === module._defaultType ||
830
- // The package is explicitly declared as an ES module.
831
- (info && info.type === 'module')
832
-
833
- return self._extensions[isESM ? '.mjs' : '.cjs'](module, source, referrer)
834
- }
835
-
836
- Module._extensions['.cjs'] = function (module, source, referrer) {
837
- const protocol = module._protocol
838
-
839
- module._type = constants.types.SCRIPT
840
-
841
- if (source === null) source = protocol.read(module._url)
842
-
843
- if (typeof source === 'string') source = Buffer.from(source)
844
-
845
- module._source = source
846
-
847
- module._function = binding.createFunction(
848
- module._url.href,
849
- ['require', 'module', 'exports', '__filename', '__dirname'],
850
- source.toString(),
851
- 0
852
- )
853
- }
854
-
855
- Module._extensions['.mjs'] = function (module, source, referrer) {
856
- const self = Module
857
-
858
- const protocol = module._protocol
859
-
860
- module._type = constants.types.MODULE
861
-
862
- if (source === null) source = protocol.read(module._url)
863
-
864
- if (typeof source === 'string') source = Buffer.from(source)
865
-
866
- module._source = source
867
-
868
- module._handle = binding.createModule(module._url.href, source.toString(), 0, self._handle)
869
- }
870
-
871
- Module._extensions['.json'] = function (module, source, referrer) {
872
- const protocol = module._protocol
873
-
874
- module._type = constants.types.JSON
875
-
876
- if (source === null) source = protocol.read(module._url)
877
-
878
- if (typeof source === 'string') source = Buffer.from(source)
879
-
880
- module._source = source
881
- module._exports = JSON.parse(source.toString())
96
+ return { loader: loaderFor(opts), parent: assertURL(parentURL, 'Parent URL') }
882
97
  }
883
98
 
884
- Module._extensions['.bare'] = function (module, source, referrer) {
885
- module._type = constants.types.ADDON
886
-
887
- referrer = module
99
+ function assertURL(url, name) {
100
+ if (isURL(url)) return url
888
101
 
889
- module._exports = Bare.Addon.load(module._url, { referrer }).exports
102
+ throw new TypeError(`${name} must be a URL. Received type ${typeof url} (${url})`)
890
103
  }
891
104
 
892
- Module._extensions['.node'] = function (module, source, referrer) {
893
- module._type = constants.types.ADDON
105
+ function optionsFor(opts) {
106
+ if (opts === undefined || opts === null) return {}
894
107
 
895
- referrer = module
108
+ if (typeof opts === 'object') return opts
896
109
 
897
- module._exports = Bare.Addon.load(module._url, { referrer }).exports
110
+ throw new TypeError(`Options must be an object. Received type ${typeof opts} (${opts})`)
898
111
  }
899
112
 
900
- Module._extensions['.bundle'] = function (module, source, referrer) {
901
- const self = Module
902
-
903
- const protocol = module._protocol
904
-
905
- module._type = constants.types.BUNDLE
906
-
907
- if (source === null) source = protocol.read(module._url)
908
-
909
- if (typeof source === 'string') source = Buffer.from(source)
910
-
911
- referrer = module
912
-
913
- const bundle = Bundle.from(source).mount(module._url.href + '/')
914
-
915
- module._source = source
916
- module._imports = bundle.imports
917
- module._resolutions = bundle.resolutions
918
-
919
- module._protocol = protocol.extend({
920
- postresolve(context, url) {
921
- return bundle.exists(url.href) ? url : context.postresolve(url)
922
- },
923
-
924
- exists(context, url, type) {
925
- return bundle.exists(url.href) || context.exists(url, type)
926
- },
927
-
928
- read(context, url) {
929
- return bundle.read(url.href) || context.read(url)
930
- }
931
- })
932
-
933
- if (bundle.main) {
934
- module._exports = self.load(new URL(bundle.main), bundle.read(bundle.main), {
935
- referrer
936
- })._exports
937
- }
938
- }
939
-
940
- Module._extensions['.bin'] = function (module, source, referrer) {
941
- const protocol = module._protocol
942
-
943
- module._type = constants.types.BINARY
944
-
945
- if (source === null) source = protocol.read(module._url)
946
-
947
- if (typeof source === 'string') source = Buffer.from(source)
948
-
949
- module._source = module._exports = source
950
- }
951
-
952
- Module._extensions['.txt'] = function (module, source, referrer) {
953
- const protocol = module._protocol
954
-
955
- module._type = constants.types.TEXT
956
-
957
- if (source === null) source = protocol.read(module._url)
958
-
959
- if (typeof source === 'string') source = Buffer.from(source)
113
+ exports.createRequire = function createRequire(parentURL, opts = {}) {
114
+ opts = optionsFor(opts)
960
115
 
961
- module._source = source
962
- module._exports = source.toString()
963
- }
116
+ let referrer = opts.referrer || null
964
117
 
965
- Module._protocol = new Protocol({
966
- postresolve(url) {
967
- switch (url.protocol) {
968
- case 'file:':
969
- return pathToFileURL(binding.realpath(path.toNamespacedPath(fileURLToPath(url))))
970
- default:
971
- return url
972
- }
973
- },
118
+ const loader = loaderFor(opts)
974
119
 
975
- exists(url, type = 0) {
976
- switch (url.protocol) {
977
- case 'file:':
978
- return binding.exists(
979
- path.toNamespacedPath(fileURLToPath(url)),
980
- type === constants.types.ASSET ? binding.FILE | binding.DIR : binding.FILE
981
- )
982
- default:
983
- return false
120
+ if (referrer === null) {
121
+ if (typeof parentURL === 'string') {
122
+ parentURL = URL.parse(parentURL) || pathToFileURL(parentURL)
984
123
  }
985
- },
986
124
 
987
- read(url) {
988
- switch (url.protocol) {
989
- case 'file:':
990
- return Buffer.from(binding.read(path.toNamespacedPath(fileURLToPath(url))))
991
- default:
992
- throw errors.UNKNOWN_PROTOCOL(`Cannot load module '${url.href}'`)
993
- }
994
- }
995
- })
125
+ assertURL(parentURL, 'Parent URL')
996
126
 
997
- function toURL(value, base) {
998
- if (isURL(value)) return value
999
-
1000
- if (startsWithWindowsDriveLetter(value)) {
1001
- return pathToFileURL(value)
1002
- }
1003
-
1004
- return URL.parse(value, base) || pathToFileURL(value)
1005
- }
1006
-
1007
- function urlToPath(url) {
1008
- if (url.protocol === 'file:') return fileURLToPath(url)
127
+ referrer = new Module(
128
+ loader,
129
+ new ModuleSource({
130
+ url: parentURL,
131
+ type: 0,
132
+ source: Buffer.alloc(0),
133
+ imports: {},
134
+ lexer: { imports: [], exports: [] }
135
+ })
136
+ )
1009
137
 
1010
- if (isWindows) {
1011
- if (/%2f|%5c/i.test(url.pathname)) {
1012
- throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
1013
- }
1014
- } else {
1015
- if (/%2f/i.test(url.pathname)) {
1016
- throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
1017
- }
138
+ if (loader.main === null) loader._main = referrer
1018
139
  }
1019
140
 
1020
- return decodeURIComponent(url.pathname)
141
+ return loader._createRequire(referrer)
1021
142
  }
1022
143
 
1023
- function urlToDirname(url) {
1024
- if (url.protocol === 'file:') return path.dirname(fileURLToPath(url))
144
+ function loaderFor(opts) {
145
+ const referrer = opts.referrer || null
1025
146
 
1026
- if (isWindows) {
1027
- if (/%2f|%5c/i.test(url.pathname)) {
1028
- throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
1029
- }
1030
- } else {
1031
- if (/%2f/i.test(url.pathname)) {
1032
- throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
1033
- }
1034
- }
147
+ if (referrer === null) return new ModuleLoader(opts)
1035
148
 
1036
- return decodeURIComponent(new URL('.', url).pathname).replace(/\/$/, '')
149
+ return referrer._loader._fork(opts)
1037
150
  }