bare-module 4.6.3 → 4.7.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 +265 -47
- package/binding.c +17 -17
- package/index.js +257 -147
- package/lib/errors.js +33 -13
- package/lib/protocol.js +9 -9
- package/package.json +4 -3
package/index.js
CHANGED
|
@@ -13,8 +13,8 @@ const isWindows = Bare.platform === 'win32'
|
|
|
13
13
|
|
|
14
14
|
const { startsWithWindowsDriveLetter } = resolve
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
constructor
|
|
16
|
+
module.exports = exports = class Module {
|
|
17
|
+
constructor(url) {
|
|
18
18
|
this._url = url
|
|
19
19
|
this._state = 0
|
|
20
20
|
this._type = 0
|
|
@@ -37,73 +37,73 @@ const Module = module.exports = exports = class Module {
|
|
|
37
37
|
Module._modules.add(this)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
get url
|
|
40
|
+
get url() {
|
|
41
41
|
return this._url
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
get filename
|
|
44
|
+
get filename() {
|
|
45
45
|
return urlToPath(this._url)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
get dirname
|
|
48
|
+
get dirname() {
|
|
49
49
|
return urlToDirname(this._url)
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
get type
|
|
52
|
+
get type() {
|
|
53
53
|
return this._type
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
get defaultType
|
|
56
|
+
get defaultType() {
|
|
57
57
|
return this._defaultType
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
get cache
|
|
60
|
+
get cache() {
|
|
61
61
|
return this._cache
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
get main
|
|
64
|
+
get main() {
|
|
65
65
|
return this._main
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
get exports
|
|
68
|
+
get exports() {
|
|
69
69
|
return this._exports
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
set exports
|
|
72
|
+
set exports(value) {
|
|
73
73
|
this._exports = value
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
get imports
|
|
76
|
+
get imports() {
|
|
77
77
|
return this._imports
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
get resolutions
|
|
80
|
+
get resolutions() {
|
|
81
81
|
return this._resolutions
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
get builtins
|
|
84
|
+
get builtins() {
|
|
85
85
|
return this._builtins
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
get conditions
|
|
88
|
+
get conditions() {
|
|
89
89
|
return Array.from(this._conditions)
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
get protocol
|
|
92
|
+
get protocol() {
|
|
93
93
|
return this._protocol
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
// For Node.js compatibility
|
|
97
|
-
get id
|
|
97
|
+
get id() {
|
|
98
98
|
return this.filename
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
// For Node.js compatibility
|
|
102
|
-
get path
|
|
102
|
+
get path() {
|
|
103
103
|
return this.dirname
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
destroy
|
|
106
|
+
destroy() {
|
|
107
107
|
this._state |= constants.states.DESTROYED
|
|
108
108
|
|
|
109
109
|
if (this._handle) {
|
|
@@ -114,11 +114,11 @@ const Module = module.exports = exports = class Module {
|
|
|
114
114
|
Module._modules.delete(this)
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
_run
|
|
117
|
+
_run() {
|
|
118
118
|
binding.runModule(this._handle, Module._handle, Module._onrun)
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
_transform
|
|
121
|
+
_transform(isImport, isDynamicImport) {
|
|
122
122
|
if (isDynamicImport) {
|
|
123
123
|
this._synthesize()
|
|
124
124
|
this._evaluate(true /* eagerRun */)
|
|
@@ -131,7 +131,7 @@ const Module = module.exports = exports = class Module {
|
|
|
131
131
|
return this
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
_synthesize
|
|
134
|
+
_synthesize() {
|
|
135
135
|
if ((this._state & constants.states.SYNTHESIZED) !== 0) return
|
|
136
136
|
|
|
137
137
|
this._state |= constants.states.SYNTHESIZED
|
|
@@ -159,9 +159,15 @@ const Module = module.exports = exports = class Module {
|
|
|
159
159
|
|
|
160
160
|
for (const { specifier, type } of result.imports) {
|
|
161
161
|
if (type & lex.constants.REEXPORT) {
|
|
162
|
-
const resolved = Module.resolve(specifier, referrer._url, {
|
|
162
|
+
const resolved = Module.resolve(specifier, referrer._url, {
|
|
163
|
+
isImport: true,
|
|
164
|
+
referrer
|
|
165
|
+
})
|
|
163
166
|
|
|
164
|
-
const module = Module.load(resolved, {
|
|
167
|
+
const module = Module.load(resolved, {
|
|
168
|
+
isImport: true,
|
|
169
|
+
referrer
|
|
170
|
+
})
|
|
165
171
|
|
|
166
172
|
if (module._names) {
|
|
167
173
|
for (const name of module._names) names.add(name)
|
|
@@ -188,10 +194,14 @@ const Module = module.exports = exports = class Module {
|
|
|
188
194
|
|
|
189
195
|
this._names = Array.from(names)
|
|
190
196
|
|
|
191
|
-
this._handle = binding.createSyntheticModule(
|
|
197
|
+
this._handle = binding.createSyntheticModule(
|
|
198
|
+
this._url.href,
|
|
199
|
+
this._names,
|
|
200
|
+
Module._handle
|
|
201
|
+
)
|
|
192
202
|
}
|
|
193
203
|
|
|
194
|
-
_evaluate
|
|
204
|
+
_evaluate(eagerRun = false) {
|
|
195
205
|
if ((this._state & constants.states.EVALUATED) !== 0) return
|
|
196
206
|
|
|
197
207
|
this._state |= constants.states.EVALUATED
|
|
@@ -221,7 +231,7 @@ const Module = module.exports = exports = class Module {
|
|
|
221
231
|
}
|
|
222
232
|
}
|
|
223
233
|
|
|
224
|
-
[Symbol.for('bare.inspect')]
|
|
234
|
+
[Symbol.for('bare.inspect')]() {
|
|
225
235
|
return {
|
|
226
236
|
__proto__: { constructor: Module },
|
|
227
237
|
|
|
@@ -243,23 +253,39 @@ const Module = module.exports = exports = class Module {
|
|
|
243
253
|
static _modules = new Set()
|
|
244
254
|
static _conditions = ['bare', 'node', Bare.platform, Bare.arch]
|
|
245
255
|
|
|
246
|
-
static _handle = binding.init(
|
|
256
|
+
static _handle = binding.init(
|
|
257
|
+
this,
|
|
258
|
+
this._onimport,
|
|
259
|
+
this._onevaluate,
|
|
260
|
+
this._onmeta
|
|
261
|
+
)
|
|
247
262
|
|
|
248
|
-
static _onimport
|
|
263
|
+
static _onimport(specifier, attributes, referrerHref, isDynamicImport) {
|
|
249
264
|
const referrer = this._cache[referrerHref] || null
|
|
250
265
|
|
|
251
266
|
if (referrer === null) {
|
|
252
|
-
throw errors.MODULE_NOT_FOUND(
|
|
267
|
+
throw errors.MODULE_NOT_FOUND(
|
|
268
|
+
`Cannot find referrer for module '${specifier}' imported from '${referrerHref}'`
|
|
269
|
+
)
|
|
253
270
|
}
|
|
254
271
|
|
|
255
|
-
const
|
|
272
|
+
const resolved = this.resolve(specifier, referrer._url, {
|
|
273
|
+
isImport: true,
|
|
274
|
+
referrer,
|
|
275
|
+
attributes
|
|
276
|
+
})
|
|
256
277
|
|
|
257
|
-
const module = this.load(
|
|
278
|
+
const module = this.load(resolved, {
|
|
279
|
+
isImport: true,
|
|
280
|
+
isDynamicImport,
|
|
281
|
+
referrer,
|
|
282
|
+
attributes
|
|
283
|
+
})
|
|
258
284
|
|
|
259
285
|
return module._handle
|
|
260
286
|
}
|
|
261
287
|
|
|
262
|
-
static _onevaluate
|
|
288
|
+
static _onevaluate(href) {
|
|
263
289
|
const module = this._cache[href] || null
|
|
264
290
|
|
|
265
291
|
if (module === null) {
|
|
@@ -272,11 +298,10 @@ const Module = module.exports = exports = class Module {
|
|
|
272
298
|
let value
|
|
273
299
|
|
|
274
300
|
if (
|
|
275
|
-
name === 'default' &&
|
|
276
|
-
|
|
301
|
+
name === 'default' &&
|
|
302
|
+
(typeof module._exports !== 'object' ||
|
|
277
303
|
module._exports === null ||
|
|
278
|
-
name in module._exports === false
|
|
279
|
-
)
|
|
304
|
+
name in module._exports === false)
|
|
280
305
|
) {
|
|
281
306
|
value = module._exports
|
|
282
307
|
} else {
|
|
@@ -287,7 +312,7 @@ const Module = module.exports = exports = class Module {
|
|
|
287
312
|
}
|
|
288
313
|
}
|
|
289
314
|
|
|
290
|
-
static _onmeta
|
|
315
|
+
static _onmeta(href, meta) {
|
|
291
316
|
const self = Module
|
|
292
317
|
|
|
293
318
|
const module = this._cache[href] || null
|
|
@@ -298,59 +323,76 @@ const Module = module.exports = exports = class Module {
|
|
|
298
323
|
|
|
299
324
|
const referrer = module
|
|
300
325
|
|
|
301
|
-
addon.host = Bare.Addon.host
|
|
302
|
-
|
|
303
326
|
meta.url = module._url.href
|
|
304
327
|
meta.main = module._main === module
|
|
305
328
|
meta.cache = module._cache
|
|
306
|
-
meta.resolve = resolve
|
|
307
|
-
meta.addon = addon
|
|
308
|
-
meta.asset = asset
|
|
309
|
-
|
|
310
|
-
function resolve (specifier, parentURL = referrer._url) {
|
|
311
|
-
const resolved = self.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
|
|
312
329
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}
|
|
330
|
+
meta.resolve = function resolve(specifier, parentURL = referrer._url) {
|
|
331
|
+
return self.resolve(specifier, toURL(parentURL, referrer._url), {
|
|
332
|
+
referrer
|
|
333
|
+
}).href
|
|
317
334
|
}
|
|
318
335
|
|
|
319
|
-
function addon
|
|
320
|
-
const resolved = Bare.Addon.resolve(
|
|
336
|
+
meta.addon = function addon(specifier = '.', parentURL = referrer._url) {
|
|
337
|
+
const resolved = Bare.Addon.resolve(
|
|
338
|
+
specifier,
|
|
339
|
+
toURL(parentURL, referrer._url),
|
|
340
|
+
{ referrer }
|
|
341
|
+
)
|
|
321
342
|
|
|
322
343
|
const addon = Bare.Addon.load(resolved, { referrer })
|
|
323
344
|
|
|
324
345
|
return addon._exports
|
|
325
346
|
}
|
|
326
347
|
|
|
327
|
-
|
|
328
|
-
|
|
348
|
+
meta.addon.resolve = function resolve(
|
|
349
|
+
specifier = '.',
|
|
350
|
+
parentURL = referrer._url
|
|
351
|
+
) {
|
|
352
|
+
return Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
|
|
353
|
+
referrer
|
|
354
|
+
}).href
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
meta.addon.host = Bare.Addon.host
|
|
358
|
+
|
|
359
|
+
meta.asset = function asset(specifier, parentURL = referrer._url) {
|
|
360
|
+
return self.asset(specifier, toURL(parentURL, referrer._url), {
|
|
361
|
+
referrer
|
|
362
|
+
}).href
|
|
329
363
|
}
|
|
330
364
|
}
|
|
331
365
|
|
|
332
|
-
static _onrun
|
|
366
|
+
static _onrun(reason, promise, err = reason) {
|
|
333
367
|
if (err) {
|
|
334
368
|
promise.catch(() => {}) // Don't leak the rejection
|
|
335
369
|
|
|
336
370
|
throw err
|
|
337
371
|
} else {
|
|
338
|
-
promise.catch((err) =>
|
|
372
|
+
promise.catch((err) =>
|
|
373
|
+
queueMicrotask(() => {
|
|
374
|
+
throw err
|
|
375
|
+
})
|
|
376
|
+
)
|
|
339
377
|
}
|
|
340
378
|
}
|
|
341
379
|
|
|
342
|
-
static get protocol
|
|
380
|
+
static get protocol() {
|
|
343
381
|
return this._protocol
|
|
344
382
|
}
|
|
345
383
|
|
|
346
|
-
static get cache
|
|
384
|
+
static get cache() {
|
|
347
385
|
return this._cache
|
|
348
386
|
}
|
|
349
387
|
|
|
350
|
-
static load
|
|
388
|
+
static load(url, source = null, opts = {}) {
|
|
351
389
|
const self = Module
|
|
352
390
|
|
|
353
|
-
if (
|
|
391
|
+
if (
|
|
392
|
+
!ArrayBuffer.isView(source) &&
|
|
393
|
+
typeof source !== 'string' &&
|
|
394
|
+
source !== null
|
|
395
|
+
) {
|
|
354
396
|
opts = source
|
|
355
397
|
source = null
|
|
356
398
|
}
|
|
@@ -376,7 +418,9 @@ const Module = module.exports = exports = class Module {
|
|
|
376
418
|
|
|
377
419
|
if (module !== null) {
|
|
378
420
|
if (type !== 0 && type !== module._type) {
|
|
379
|
-
throw errors.TYPE_INCOMPATIBLE(
|
|
421
|
+
throw errors.TYPE_INCOMPATIBLE(
|
|
422
|
+
`Module '${module.url.href}' is not of type '${nameOfType(type)}'`
|
|
423
|
+
)
|
|
380
424
|
}
|
|
381
425
|
|
|
382
426
|
return module._transform(isImport, isDynamicImport)
|
|
@@ -400,10 +444,12 @@ const Module = module.exports = exports = class Module {
|
|
|
400
444
|
module._builtins = builtins
|
|
401
445
|
module._conditions = conditions
|
|
402
446
|
|
|
403
|
-
let extension =
|
|
447
|
+
let extension =
|
|
448
|
+
canonicalExtensionForType(type) || path.extname(url.pathname)
|
|
404
449
|
|
|
405
450
|
if (extension in self._extensions === false) {
|
|
406
|
-
if (defaultType)
|
|
451
|
+
if (defaultType)
|
|
452
|
+
extension = canonicalExtensionForType(defaultType) || '.js'
|
|
407
453
|
else extension = '.js'
|
|
408
454
|
}
|
|
409
455
|
|
|
@@ -419,11 +465,13 @@ const Module = module.exports = exports = class Module {
|
|
|
419
465
|
}
|
|
420
466
|
}
|
|
421
467
|
|
|
422
|
-
static resolve
|
|
468
|
+
static resolve(specifier, parentURL, opts = {}) {
|
|
423
469
|
const self = Module
|
|
424
470
|
|
|
425
471
|
if (typeof specifier !== 'string') {
|
|
426
|
-
throw new TypeError(
|
|
472
|
+
throw new TypeError(
|
|
473
|
+
`Specifier must be a string. Received type ${typeof specifier} (${specifier})`
|
|
474
|
+
)
|
|
427
475
|
}
|
|
428
476
|
|
|
429
477
|
const {
|
|
@@ -446,18 +494,24 @@ const Module = module.exports = exports = class Module {
|
|
|
446
494
|
|
|
447
495
|
if (resolution) return protocol.postresolve(resolution)
|
|
448
496
|
|
|
449
|
-
for (const resolution of resolve(
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
497
|
+
for (const resolution of resolve(
|
|
498
|
+
resolved,
|
|
499
|
+
parentURL,
|
|
500
|
+
{
|
|
501
|
+
conditions: isImport
|
|
502
|
+
? ['import', ...conditions]
|
|
503
|
+
: ['require', ...conditions],
|
|
504
|
+
imports,
|
|
505
|
+
resolutions,
|
|
506
|
+
extensions,
|
|
507
|
+
builtins: builtins ? Object.keys(builtins) : [],
|
|
508
|
+
engines: Bare.versions
|
|
509
|
+
},
|
|
510
|
+
readPackage
|
|
511
|
+
)) {
|
|
459
512
|
switch (resolution.protocol) {
|
|
460
|
-
case 'builtin:':
|
|
513
|
+
case 'builtin:':
|
|
514
|
+
return resolution
|
|
461
515
|
default:
|
|
462
516
|
if (protocol.exists(resolution)) {
|
|
463
517
|
return protocol.postresolve(resolution)
|
|
@@ -465,9 +519,11 @@ const Module = module.exports = exports = class Module {
|
|
|
465
519
|
}
|
|
466
520
|
}
|
|
467
521
|
|
|
468
|
-
throw errors.MODULE_NOT_FOUND(
|
|
522
|
+
throw errors.MODULE_NOT_FOUND(
|
|
523
|
+
`Cannot find module '${specifier}' imported from '${parentURL.href}'`
|
|
524
|
+
)
|
|
469
525
|
|
|
470
|
-
function readPackage
|
|
526
|
+
function readPackage(packageURL) {
|
|
471
527
|
if (protocol.exists(packageURL)) {
|
|
472
528
|
return Module.load(packageURL, { protocol })._exports
|
|
473
529
|
}
|
|
@@ -476,11 +532,13 @@ const Module = module.exports = exports = class Module {
|
|
|
476
532
|
}
|
|
477
533
|
}
|
|
478
534
|
|
|
479
|
-
static asset
|
|
535
|
+
static asset(specifier, parentURL, opts = {}) {
|
|
480
536
|
const self = Module
|
|
481
537
|
|
|
482
538
|
if (typeof specifier !== 'string') {
|
|
483
|
-
throw new TypeError(
|
|
539
|
+
throw new TypeError(
|
|
540
|
+
`Specifier must be a string. Received type ${typeof specifier} (${specifier})`
|
|
541
|
+
)
|
|
484
542
|
}
|
|
485
543
|
|
|
486
544
|
const {
|
|
@@ -497,19 +555,29 @@ const Module = module.exports = exports = class Module {
|
|
|
497
555
|
|
|
498
556
|
if (resolution) return protocol.postresolve(resolution)
|
|
499
557
|
|
|
500
|
-
for (const resolution of resolve(
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
558
|
+
for (const resolution of resolve(
|
|
559
|
+
resolved,
|
|
560
|
+
parentURL,
|
|
561
|
+
{
|
|
562
|
+
conditions: ['asset', ...conditions],
|
|
563
|
+
imports,
|
|
564
|
+
resolutions,
|
|
565
|
+
engines: Bare.versions
|
|
566
|
+
},
|
|
567
|
+
readPackage
|
|
568
|
+
)) {
|
|
505
569
|
if (protocol.exists(resolution)) {
|
|
506
|
-
return protocol.postresolve(
|
|
570
|
+
return protocol.postresolve(
|
|
571
|
+
protocol.asset ? protocol.asset(resolution) : resolution
|
|
572
|
+
)
|
|
507
573
|
}
|
|
508
574
|
}
|
|
509
575
|
|
|
510
|
-
throw errors.ASSET_NOT_FOUND(
|
|
576
|
+
throw errors.ASSET_NOT_FOUND(
|
|
577
|
+
`Cannot find asset '${specifier}' imported from '${parentURL.href}'`
|
|
578
|
+
)
|
|
511
579
|
|
|
512
|
-
function readPackage
|
|
580
|
+
function readPackage(packageURL) {
|
|
513
581
|
if (protocol.exists(packageURL)) {
|
|
514
582
|
return Module.load(packageURL, { protocol })._exports
|
|
515
583
|
}
|
|
@@ -519,7 +587,9 @@ const Module = module.exports = exports = class Module {
|
|
|
519
587
|
}
|
|
520
588
|
}
|
|
521
589
|
|
|
522
|
-
|
|
590
|
+
const Module = exports
|
|
591
|
+
|
|
592
|
+
function extensionsForType(type) {
|
|
523
593
|
switch (type) {
|
|
524
594
|
case constants.types.SCRIPT:
|
|
525
595
|
return ['.js', '.cjs']
|
|
@@ -540,7 +610,7 @@ function extensionsForType (type) {
|
|
|
540
610
|
}
|
|
541
611
|
}
|
|
542
612
|
|
|
543
|
-
function canonicalExtensionForType
|
|
613
|
+
function canonicalExtensionForType(type) {
|
|
544
614
|
switch (type) {
|
|
545
615
|
case constants.types.SCRIPT:
|
|
546
616
|
return '.cjs'
|
|
@@ -561,7 +631,7 @@ function canonicalExtensionForType (type) {
|
|
|
561
631
|
}
|
|
562
632
|
}
|
|
563
633
|
|
|
564
|
-
function nameOfType
|
|
634
|
+
function nameOfType(type) {
|
|
565
635
|
switch (type) {
|
|
566
636
|
case constants.types.SCRIPT:
|
|
567
637
|
return 'script'
|
|
@@ -582,7 +652,7 @@ function nameOfType (type) {
|
|
|
582
652
|
}
|
|
583
653
|
}
|
|
584
654
|
|
|
585
|
-
function typeForAttributes
|
|
655
|
+
function typeForAttributes(attributes) {
|
|
586
656
|
if (typeof attributes !== 'object' || attributes === null) return 0
|
|
587
657
|
|
|
588
658
|
switch (attributes.type) {
|
|
@@ -614,11 +684,14 @@ exports.constants = constants
|
|
|
614
684
|
exports.builtinModules = []
|
|
615
685
|
|
|
616
686
|
// For Node.js compatibility
|
|
617
|
-
exports.isBuiltin = function isBuiltin
|
|
687
|
+
exports.isBuiltin = function isBuiltin() {
|
|
618
688
|
return false
|
|
619
689
|
}
|
|
620
690
|
|
|
621
|
-
const createRequire = exports.createRequire = function createRequire
|
|
691
|
+
const createRequire = (exports.createRequire = function createRequire(
|
|
692
|
+
parentURL,
|
|
693
|
+
opts = {}
|
|
694
|
+
) {
|
|
622
695
|
const self = Module
|
|
623
696
|
|
|
624
697
|
let {
|
|
@@ -652,47 +725,61 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
|
|
|
652
725
|
|
|
653
726
|
referrer = module
|
|
654
727
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
require.main = module._main
|
|
658
|
-
require.cache = module._cache
|
|
659
|
-
require.resolve = resolve
|
|
660
|
-
require.addon = addon
|
|
661
|
-
require.asset = asset
|
|
662
|
-
|
|
663
|
-
return require
|
|
664
|
-
|
|
665
|
-
function require (specifier, opts = {}) {
|
|
728
|
+
function require(specifier, opts = {}) {
|
|
666
729
|
const attributes = opts && opts.with
|
|
667
730
|
|
|
668
|
-
const resolved = self.resolve(specifier, referrer._url, {
|
|
731
|
+
const resolved = self.resolve(specifier, referrer._url, {
|
|
732
|
+
referrer,
|
|
733
|
+
attributes
|
|
734
|
+
})
|
|
669
735
|
|
|
670
736
|
const module = self.load(resolved, { referrer, attributes })
|
|
671
737
|
|
|
672
738
|
return module._exports
|
|
673
739
|
}
|
|
674
740
|
|
|
675
|
-
|
|
676
|
-
|
|
741
|
+
require.main = module._main
|
|
742
|
+
require.cache = module._cache
|
|
677
743
|
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
744
|
+
require.resolve = function resolve(specifier, parentURL = referrer._url) {
|
|
745
|
+
return urlToPath(
|
|
746
|
+
self.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
|
|
747
|
+
)
|
|
682
748
|
}
|
|
683
749
|
|
|
684
|
-
function addon
|
|
685
|
-
const resolved = Bare.Addon.resolve(
|
|
750
|
+
require.addon = function addon(specifier = '.', parentURL = referrer._url) {
|
|
751
|
+
const resolved = Bare.Addon.resolve(
|
|
752
|
+
specifier,
|
|
753
|
+
toURL(parentURL, referrer._url),
|
|
754
|
+
{ referrer }
|
|
755
|
+
)
|
|
686
756
|
|
|
687
757
|
const addon = Bare.Addon.load(resolved, { referrer })
|
|
688
758
|
|
|
689
759
|
return addon._exports
|
|
690
760
|
}
|
|
691
761
|
|
|
692
|
-
|
|
693
|
-
|
|
762
|
+
require.addon.host = Bare.Addon.host
|
|
763
|
+
|
|
764
|
+
require.addon.resolve = function resolve(
|
|
765
|
+
specifier = '.',
|
|
766
|
+
parentURL = referrer._url
|
|
767
|
+
) {
|
|
768
|
+
return urlToPath(
|
|
769
|
+
Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
|
|
770
|
+
referrer
|
|
771
|
+
})
|
|
772
|
+
)
|
|
694
773
|
}
|
|
695
|
-
|
|
774
|
+
|
|
775
|
+
require.asset = function asset(specifier, parentURL = referrer._url) {
|
|
776
|
+
return urlToPath(
|
|
777
|
+
self.asset(specifier, toURL(parentURL, referrer._url), { referrer })
|
|
778
|
+
)
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
return require
|
|
782
|
+
})
|
|
696
783
|
|
|
697
784
|
if (Bare.simulator) Module._conditions.push('simulator')
|
|
698
785
|
|
|
@@ -704,7 +791,9 @@ Module._extensions['.js'] = function (module, source, referrer) {
|
|
|
704
791
|
|
|
705
792
|
let pkg
|
|
706
793
|
|
|
707
|
-
for (const packageURL of resolve.lookupPackageScope(module._url, {
|
|
794
|
+
for (const packageURL of resolve.lookupPackageScope(module._url, {
|
|
795
|
+
resolutions
|
|
796
|
+
})) {
|
|
708
797
|
if (self._cache[packageURL.href]) {
|
|
709
798
|
pkg = self._cache[packageURL.href]
|
|
710
799
|
break
|
|
@@ -718,13 +807,11 @@ Module._extensions['.js'] = function (module, source, referrer) {
|
|
|
718
807
|
|
|
719
808
|
const info = (pkg && pkg._exports) || {}
|
|
720
809
|
|
|
721
|
-
const isESM =
|
|
810
|
+
const isESM =
|
|
722
811
|
// The default type is ES modules.
|
|
723
|
-
|
|
724
|
-
|
|
812
|
+
constants.types.MODULE === module._defaultType ||
|
|
725
813
|
// The package is explicitly declared as an ES module.
|
|
726
814
|
(info && info.type === 'module')
|
|
727
|
-
)
|
|
728
815
|
|
|
729
816
|
return self._extensions[isESM ? '.mjs' : '.cjs'](module, source, referrer)
|
|
730
817
|
}
|
|
@@ -741,7 +828,12 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
741
828
|
|
|
742
829
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
743
830
|
|
|
744
|
-
module._function = binding.createFunction(
|
|
831
|
+
module._function = binding.createFunction(
|
|
832
|
+
module._url.href,
|
|
833
|
+
['require', 'module', 'exports', '__filename', '__dirname'],
|
|
834
|
+
source,
|
|
835
|
+
0
|
|
836
|
+
)
|
|
745
837
|
}
|
|
746
838
|
}
|
|
747
839
|
|
|
@@ -759,7 +851,12 @@ Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
|
759
851
|
|
|
760
852
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
761
853
|
|
|
762
|
-
module._handle = binding.createModule(
|
|
854
|
+
module._handle = binding.createModule(
|
|
855
|
+
module._url.href,
|
|
856
|
+
source,
|
|
857
|
+
0,
|
|
858
|
+
self._handle
|
|
859
|
+
)
|
|
763
860
|
}
|
|
764
861
|
}
|
|
765
862
|
|
|
@@ -808,27 +905,33 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
808
905
|
|
|
809
906
|
referrer = module
|
|
810
907
|
|
|
811
|
-
const bundle = module._bundle = Bundle.from(source).mount(
|
|
908
|
+
const bundle = (module._bundle = Bundle.from(source).mount(
|
|
909
|
+
module._url.href + '/'
|
|
910
|
+
))
|
|
812
911
|
|
|
813
912
|
module._imports = bundle.imports
|
|
814
913
|
module._resolutions = bundle.resolutions
|
|
815
914
|
|
|
816
915
|
module._protocol = protocol.extend({
|
|
817
|
-
postresolve
|
|
916
|
+
postresolve(context, url) {
|
|
818
917
|
return bundle.exists(url.href) ? url : context.postresolve(url)
|
|
819
918
|
},
|
|
820
919
|
|
|
821
|
-
exists
|
|
920
|
+
exists(context, url) {
|
|
822
921
|
return bundle.exists(url.href) || context.exists(url)
|
|
823
922
|
},
|
|
824
923
|
|
|
825
|
-
read
|
|
924
|
+
read(context, url) {
|
|
826
925
|
return bundle.read(url.href) || context.read(url)
|
|
827
926
|
}
|
|
828
927
|
})
|
|
829
928
|
|
|
830
929
|
if (bundle.main) {
|
|
831
|
-
module._exports = self.load(
|
|
930
|
+
module._exports = self.load(
|
|
931
|
+
new URL(bundle.main),
|
|
932
|
+
bundle.read(bundle.main),
|
|
933
|
+
{ referrer }
|
|
934
|
+
)._exports
|
|
832
935
|
}
|
|
833
936
|
}
|
|
834
937
|
|
|
@@ -857,7 +960,7 @@ Module._extensions['.txt'] = function (module, source, referrer) {
|
|
|
857
960
|
}
|
|
858
961
|
|
|
859
962
|
Module._protocol = new Protocol({
|
|
860
|
-
postresolve
|
|
963
|
+
postresolve(url) {
|
|
861
964
|
switch (url.protocol) {
|
|
862
965
|
case 'file:':
|
|
863
966
|
return pathToFileURL(binding.realpath(fileURLToPath(url)))
|
|
@@ -866,7 +969,7 @@ Module._protocol = new Protocol({
|
|
|
866
969
|
}
|
|
867
970
|
},
|
|
868
971
|
|
|
869
|
-
exists
|
|
972
|
+
exists(url) {
|
|
870
973
|
switch (url.protocol) {
|
|
871
974
|
case 'file:':
|
|
872
975
|
return binding.exists(fileURLToPath(url))
|
|
@@ -875,7 +978,7 @@ Module._protocol = new Protocol({
|
|
|
875
978
|
}
|
|
876
979
|
},
|
|
877
980
|
|
|
878
|
-
read
|
|
981
|
+
read(url) {
|
|
879
982
|
switch (url.protocol) {
|
|
880
983
|
case 'file:':
|
|
881
984
|
return Buffer.from(binding.read(fileURLToPath(url)))
|
|
@@ -885,16 +988,15 @@ Module._protocol = new Protocol({
|
|
|
885
988
|
}
|
|
886
989
|
})
|
|
887
990
|
|
|
888
|
-
Bare
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
}
|
|
991
|
+
Bare.prependListener('teardown', () => {
|
|
992
|
+
for (const module of Module._modules) {
|
|
993
|
+
module.destroy()
|
|
994
|
+
}
|
|
893
995
|
|
|
894
|
-
|
|
895
|
-
|
|
996
|
+
binding.destroy(Module._handle)
|
|
997
|
+
})
|
|
896
998
|
|
|
897
|
-
function toURL
|
|
999
|
+
function toURL(value, base) {
|
|
898
1000
|
if (isURL(value)) return value
|
|
899
1001
|
|
|
900
1002
|
if (startsWithWindowsDriveLetter(value)) {
|
|
@@ -904,34 +1006,42 @@ function toURL (value, base) {
|
|
|
904
1006
|
return URL.parse(value, base) || pathToFileURL(value)
|
|
905
1007
|
}
|
|
906
1008
|
|
|
907
|
-
function urlToPath
|
|
1009
|
+
function urlToPath(url) {
|
|
908
1010
|
if (url.protocol === 'file:') return fileURLToPath(url)
|
|
909
1011
|
|
|
910
1012
|
if (isWindows) {
|
|
911
1013
|
if (/%2f|%5c/i.test(url.pathname)) {
|
|
912
|
-
throw errors.INVALID_URL_PATH(
|
|
1014
|
+
throw errors.INVALID_URL_PATH(
|
|
1015
|
+
'The URL path must not include encoded \\ or / characters'
|
|
1016
|
+
)
|
|
913
1017
|
}
|
|
914
1018
|
} else {
|
|
915
1019
|
if (/%2f/i.test(url.pathname)) {
|
|
916
|
-
throw errors.INVALID_URL_PATH(
|
|
1020
|
+
throw errors.INVALID_URL_PATH(
|
|
1021
|
+
'The URL path must not include encoded / characters'
|
|
1022
|
+
)
|
|
917
1023
|
}
|
|
918
1024
|
}
|
|
919
1025
|
|
|
920
1026
|
return decodeURIComponent(url.pathname)
|
|
921
1027
|
}
|
|
922
1028
|
|
|
923
|
-
function urlToDirname
|
|
1029
|
+
function urlToDirname(url) {
|
|
924
1030
|
if (url.protocol === 'file:') return path.dirname(fileURLToPath(url))
|
|
925
1031
|
|
|
926
1032
|
if (isWindows) {
|
|
927
1033
|
if (/%2f|%5c/i.test(url.pathname)) {
|
|
928
|
-
throw errors.INVALID_URL_PATH(
|
|
1034
|
+
throw errors.INVALID_URL_PATH(
|
|
1035
|
+
'The URL path must not include encoded \\ or / characters'
|
|
1036
|
+
)
|
|
929
1037
|
}
|
|
930
1038
|
} else {
|
|
931
1039
|
if (/%2f/i.test(url.pathname)) {
|
|
932
|
-
throw errors.INVALID_URL_PATH(
|
|
1040
|
+
throw errors.INVALID_URL_PATH(
|
|
1041
|
+
'The URL path must not include encoded / characters'
|
|
1042
|
+
)
|
|
933
1043
|
}
|
|
934
1044
|
}
|
|
935
1045
|
|
|
936
|
-
return decodeURIComponent(
|
|
1046
|
+
return decodeURIComponent(new URL('.', url).pathname).replace(/\/$/, '')
|
|
937
1047
|
}
|