bare-module 4.7.0 → 4.8.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 +371 -47
- package/binding.c +38 -21
- package/index.js +259 -134
- package/lib/constants.js +2 -1
- package/lib/errors.js +33 -13
- package/lib/protocol.js +9 -9
- package/package.json +4 -3
- 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/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 resolved = this.resolve(specifier, referrer._url, {
|
|
272
|
+
const resolved = this.resolve(specifier, referrer._url, {
|
|
273
|
+
isImport: true,
|
|
274
|
+
referrer,
|
|
275
|
+
attributes
|
|
276
|
+
})
|
|
256
277
|
|
|
257
|
-
const module = this.load(resolved, {
|
|
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
|
|
@@ -302,51 +327,72 @@ const Module = module.exports = exports = class Module {
|
|
|
302
327
|
meta.main = module._main === module
|
|
303
328
|
meta.cache = module._cache
|
|
304
329
|
|
|
305
|
-
meta.resolve = function resolve
|
|
306
|
-
return self.resolve(specifier, toURL(parentURL, referrer._url), {
|
|
330
|
+
meta.resolve = function resolve(specifier, parentURL = referrer._url) {
|
|
331
|
+
return self.resolve(specifier, toURL(parentURL, referrer._url), {
|
|
332
|
+
referrer
|
|
333
|
+
}).href
|
|
307
334
|
}
|
|
308
335
|
|
|
309
|
-
meta.addon = function addon
|
|
310
|
-
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
|
+
)
|
|
311
342
|
|
|
312
343
|
const addon = Bare.Addon.load(resolved, { referrer })
|
|
313
344
|
|
|
314
345
|
return addon._exports
|
|
315
346
|
}
|
|
316
347
|
|
|
317
|
-
meta.addon.resolve = function resolve
|
|
318
|
-
|
|
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
|
|
319
355
|
}
|
|
320
356
|
|
|
321
357
|
meta.addon.host = Bare.Addon.host
|
|
322
358
|
|
|
323
|
-
meta.asset = function asset
|
|
324
|
-
return self.asset(specifier, toURL(parentURL, referrer._url), {
|
|
359
|
+
meta.asset = function asset(specifier, parentURL = referrer._url) {
|
|
360
|
+
return self.asset(specifier, toURL(parentURL, referrer._url), {
|
|
361
|
+
referrer
|
|
362
|
+
}).href
|
|
325
363
|
}
|
|
326
364
|
}
|
|
327
365
|
|
|
328
|
-
static _onrun
|
|
366
|
+
static _onrun(reason, promise, err = reason) {
|
|
329
367
|
if (err) {
|
|
330
368
|
promise.catch(() => {}) // Don't leak the rejection
|
|
331
369
|
|
|
332
370
|
throw err
|
|
333
371
|
} else {
|
|
334
|
-
promise.catch((err) =>
|
|
372
|
+
promise.catch((err) =>
|
|
373
|
+
queueMicrotask(() => {
|
|
374
|
+
throw err
|
|
375
|
+
})
|
|
376
|
+
)
|
|
335
377
|
}
|
|
336
378
|
}
|
|
337
379
|
|
|
338
|
-
static get protocol
|
|
380
|
+
static get protocol() {
|
|
339
381
|
return this._protocol
|
|
340
382
|
}
|
|
341
383
|
|
|
342
|
-
static get cache
|
|
384
|
+
static get cache() {
|
|
343
385
|
return this._cache
|
|
344
386
|
}
|
|
345
387
|
|
|
346
|
-
static load
|
|
388
|
+
static load(url, source = null, opts = {}) {
|
|
347
389
|
const self = Module
|
|
348
390
|
|
|
349
|
-
if (
|
|
391
|
+
if (
|
|
392
|
+
!ArrayBuffer.isView(source) &&
|
|
393
|
+
typeof source !== 'string' &&
|
|
394
|
+
source !== null
|
|
395
|
+
) {
|
|
350
396
|
opts = source
|
|
351
397
|
source = null
|
|
352
398
|
}
|
|
@@ -372,7 +418,9 @@ const Module = module.exports = exports = class Module {
|
|
|
372
418
|
|
|
373
419
|
if (module !== null) {
|
|
374
420
|
if (type !== 0 && type !== module._type) {
|
|
375
|
-
throw errors.TYPE_INCOMPATIBLE(
|
|
421
|
+
throw errors.TYPE_INCOMPATIBLE(
|
|
422
|
+
`Module '${module.url.href}' is not of type '${nameOfType(type)}'`
|
|
423
|
+
)
|
|
376
424
|
}
|
|
377
425
|
|
|
378
426
|
return module._transform(isImport, isDynamicImport)
|
|
@@ -396,11 +444,15 @@ const Module = module.exports = exports = class Module {
|
|
|
396
444
|
module._builtins = builtins
|
|
397
445
|
module._conditions = conditions
|
|
398
446
|
|
|
399
|
-
let extension =
|
|
447
|
+
let extension =
|
|
448
|
+
canonicalExtensionForType(type) || path.extname(url.pathname)
|
|
400
449
|
|
|
401
450
|
if (extension in self._extensions === false) {
|
|
402
|
-
if (defaultType)
|
|
403
|
-
|
|
451
|
+
if (defaultType) {
|
|
452
|
+
extension = canonicalExtensionForType(defaultType) || '.js'
|
|
453
|
+
} else {
|
|
454
|
+
extension = '.js'
|
|
455
|
+
}
|
|
404
456
|
}
|
|
405
457
|
|
|
406
458
|
self._extensions[extension](module, source, referrer)
|
|
@@ -415,11 +467,13 @@ const Module = module.exports = exports = class Module {
|
|
|
415
467
|
}
|
|
416
468
|
}
|
|
417
469
|
|
|
418
|
-
static resolve
|
|
470
|
+
static resolve(specifier, parentURL, opts = {}) {
|
|
419
471
|
const self = Module
|
|
420
472
|
|
|
421
473
|
if (typeof specifier !== 'string') {
|
|
422
|
-
throw new TypeError(
|
|
474
|
+
throw new TypeError(
|
|
475
|
+
`Specifier must be a string. Received type ${typeof specifier} (${specifier})`
|
|
476
|
+
)
|
|
423
477
|
}
|
|
424
478
|
|
|
425
479
|
const {
|
|
@@ -442,29 +496,37 @@ const Module = module.exports = exports = class Module {
|
|
|
442
496
|
|
|
443
497
|
if (resolution) return protocol.postresolve(resolution)
|
|
444
498
|
|
|
445
|
-
for (const resolution of resolve(
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
499
|
+
for (const resolution of resolve(
|
|
500
|
+
resolved,
|
|
501
|
+
parentURL,
|
|
502
|
+
{
|
|
503
|
+
conditions: isImport
|
|
504
|
+
? ['import', ...conditions]
|
|
505
|
+
: ['require', ...conditions],
|
|
506
|
+
imports,
|
|
507
|
+
resolutions,
|
|
508
|
+
extensions,
|
|
509
|
+
builtins: builtins ? Object.keys(builtins) : [],
|
|
510
|
+
engines: Bare.versions
|
|
511
|
+
},
|
|
512
|
+
readPackage
|
|
513
|
+
)) {
|
|
455
514
|
switch (resolution.protocol) {
|
|
456
|
-
case 'builtin:':
|
|
515
|
+
case 'builtin:':
|
|
516
|
+
return resolution
|
|
457
517
|
default:
|
|
458
|
-
if (protocol.exists(resolution)) {
|
|
518
|
+
if (protocol.exists(resolution, type)) {
|
|
459
519
|
return protocol.postresolve(resolution)
|
|
460
520
|
}
|
|
461
521
|
}
|
|
462
522
|
}
|
|
463
523
|
|
|
464
|
-
throw errors.MODULE_NOT_FOUND(
|
|
524
|
+
throw errors.MODULE_NOT_FOUND(
|
|
525
|
+
`Cannot find module '${specifier}' imported from '${parentURL.href}'`
|
|
526
|
+
)
|
|
465
527
|
|
|
466
|
-
function readPackage
|
|
467
|
-
if (protocol.exists(packageURL)) {
|
|
528
|
+
function readPackage(packageURL) {
|
|
529
|
+
if (protocol.exists(packageURL, constants.types.JSON)) {
|
|
468
530
|
return Module.load(packageURL, { protocol })._exports
|
|
469
531
|
}
|
|
470
532
|
|
|
@@ -472,11 +534,13 @@ const Module = module.exports = exports = class Module {
|
|
|
472
534
|
}
|
|
473
535
|
}
|
|
474
536
|
|
|
475
|
-
static asset
|
|
537
|
+
static asset(specifier, parentURL, opts = {}) {
|
|
476
538
|
const self = Module
|
|
477
539
|
|
|
478
540
|
if (typeof specifier !== 'string') {
|
|
479
|
-
throw new TypeError(
|
|
541
|
+
throw new TypeError(
|
|
542
|
+
`Specifier must be a string. Received type ${typeof specifier} (${specifier})`
|
|
543
|
+
)
|
|
480
544
|
}
|
|
481
545
|
|
|
482
546
|
const {
|
|
@@ -493,20 +557,30 @@ const Module = module.exports = exports = class Module {
|
|
|
493
557
|
|
|
494
558
|
if (resolution) return protocol.postresolve(resolution)
|
|
495
559
|
|
|
496
|
-
for (const resolution of resolve(
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
560
|
+
for (const resolution of resolve(
|
|
561
|
+
resolved,
|
|
562
|
+
parentURL,
|
|
563
|
+
{
|
|
564
|
+
conditions: ['asset', ...conditions],
|
|
565
|
+
imports,
|
|
566
|
+
resolutions,
|
|
567
|
+
engines: Bare.versions
|
|
568
|
+
},
|
|
569
|
+
readPackage
|
|
570
|
+
)) {
|
|
571
|
+
if (protocol.exists(resolution, constants.types.ASSET)) {
|
|
572
|
+
return protocol.postresolve(
|
|
573
|
+
protocol.asset ? protocol.asset(resolution) : resolution
|
|
574
|
+
)
|
|
503
575
|
}
|
|
504
576
|
}
|
|
505
577
|
|
|
506
|
-
throw errors.ASSET_NOT_FOUND(
|
|
578
|
+
throw errors.ASSET_NOT_FOUND(
|
|
579
|
+
`Cannot find asset '${specifier}' imported from '${parentURL.href}'`
|
|
580
|
+
)
|
|
507
581
|
|
|
508
|
-
function readPackage
|
|
509
|
-
if (protocol.exists(packageURL)) {
|
|
582
|
+
function readPackage(packageURL) {
|
|
583
|
+
if (protocol.exists(packageURL, constants.types.JSON)) {
|
|
510
584
|
return Module.load(packageURL, { protocol })._exports
|
|
511
585
|
}
|
|
512
586
|
|
|
@@ -515,7 +589,9 @@ const Module = module.exports = exports = class Module {
|
|
|
515
589
|
}
|
|
516
590
|
}
|
|
517
591
|
|
|
518
|
-
|
|
592
|
+
const Module = exports
|
|
593
|
+
|
|
594
|
+
function extensionsForType(type) {
|
|
519
595
|
switch (type) {
|
|
520
596
|
case constants.types.SCRIPT:
|
|
521
597
|
return ['.js', '.cjs']
|
|
@@ -536,7 +612,7 @@ function extensionsForType (type) {
|
|
|
536
612
|
}
|
|
537
613
|
}
|
|
538
614
|
|
|
539
|
-
function canonicalExtensionForType
|
|
615
|
+
function canonicalExtensionForType(type) {
|
|
540
616
|
switch (type) {
|
|
541
617
|
case constants.types.SCRIPT:
|
|
542
618
|
return '.cjs'
|
|
@@ -557,7 +633,7 @@ function canonicalExtensionForType (type) {
|
|
|
557
633
|
}
|
|
558
634
|
}
|
|
559
635
|
|
|
560
|
-
function nameOfType
|
|
636
|
+
function nameOfType(type) {
|
|
561
637
|
switch (type) {
|
|
562
638
|
case constants.types.SCRIPT:
|
|
563
639
|
return 'script'
|
|
@@ -578,7 +654,7 @@ function nameOfType (type) {
|
|
|
578
654
|
}
|
|
579
655
|
}
|
|
580
656
|
|
|
581
|
-
function typeForAttributes
|
|
657
|
+
function typeForAttributes(attributes) {
|
|
582
658
|
if (typeof attributes !== 'object' || attributes === null) return 0
|
|
583
659
|
|
|
584
660
|
switch (attributes.type) {
|
|
@@ -610,11 +686,14 @@ exports.constants = constants
|
|
|
610
686
|
exports.builtinModules = []
|
|
611
687
|
|
|
612
688
|
// For Node.js compatibility
|
|
613
|
-
exports.isBuiltin = function isBuiltin
|
|
689
|
+
exports.isBuiltin = function isBuiltin() {
|
|
614
690
|
return false
|
|
615
691
|
}
|
|
616
692
|
|
|
617
|
-
const createRequire = exports.createRequire = function createRequire
|
|
693
|
+
const createRequire = (exports.createRequire = function createRequire(
|
|
694
|
+
parentURL,
|
|
695
|
+
opts = {}
|
|
696
|
+
) {
|
|
618
697
|
const self = Module
|
|
619
698
|
|
|
620
699
|
let {
|
|
@@ -648,10 +727,13 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
|
|
|
648
727
|
|
|
649
728
|
referrer = module
|
|
650
729
|
|
|
651
|
-
function require
|
|
730
|
+
function require(specifier, opts = {}) {
|
|
652
731
|
const attributes = opts && opts.with
|
|
653
732
|
|
|
654
|
-
const resolved = self.resolve(specifier, referrer._url, {
|
|
733
|
+
const resolved = self.resolve(specifier, referrer._url, {
|
|
734
|
+
referrer,
|
|
735
|
+
attributes
|
|
736
|
+
})
|
|
655
737
|
|
|
656
738
|
const module = self.load(resolved, { referrer, attributes })
|
|
657
739
|
|
|
@@ -661,12 +743,18 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
|
|
|
661
743
|
require.main = module._main
|
|
662
744
|
require.cache = module._cache
|
|
663
745
|
|
|
664
|
-
require.resolve = function resolve
|
|
665
|
-
return urlToPath(
|
|
746
|
+
require.resolve = function resolve(specifier, parentURL = referrer._url) {
|
|
747
|
+
return urlToPath(
|
|
748
|
+
self.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
|
|
749
|
+
)
|
|
666
750
|
}
|
|
667
751
|
|
|
668
|
-
require.addon = function addon
|
|
669
|
-
const resolved = Bare.Addon.resolve(
|
|
752
|
+
require.addon = function addon(specifier = '.', parentURL = referrer._url) {
|
|
753
|
+
const resolved = Bare.Addon.resolve(
|
|
754
|
+
specifier,
|
|
755
|
+
toURL(parentURL, referrer._url),
|
|
756
|
+
{ referrer }
|
|
757
|
+
)
|
|
670
758
|
|
|
671
759
|
const addon = Bare.Addon.load(resolved, { referrer })
|
|
672
760
|
|
|
@@ -675,16 +763,25 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
|
|
|
675
763
|
|
|
676
764
|
require.addon.host = Bare.Addon.host
|
|
677
765
|
|
|
678
|
-
require.addon.resolve = function resolve
|
|
679
|
-
|
|
766
|
+
require.addon.resolve = function resolve(
|
|
767
|
+
specifier = '.',
|
|
768
|
+
parentURL = referrer._url
|
|
769
|
+
) {
|
|
770
|
+
return urlToPath(
|
|
771
|
+
Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
|
|
772
|
+
referrer
|
|
773
|
+
})
|
|
774
|
+
)
|
|
680
775
|
}
|
|
681
776
|
|
|
682
|
-
require.asset = function asset
|
|
683
|
-
return urlToPath(
|
|
777
|
+
require.asset = function asset(specifier, parentURL = referrer._url) {
|
|
778
|
+
return urlToPath(
|
|
779
|
+
self.asset(specifier, toURL(parentURL, referrer._url), { referrer })
|
|
780
|
+
)
|
|
684
781
|
}
|
|
685
782
|
|
|
686
783
|
return require
|
|
687
|
-
}
|
|
784
|
+
})
|
|
688
785
|
|
|
689
786
|
if (Bare.simulator) Module._conditions.push('simulator')
|
|
690
787
|
|
|
@@ -696,13 +793,15 @@ Module._extensions['.js'] = function (module, source, referrer) {
|
|
|
696
793
|
|
|
697
794
|
let pkg
|
|
698
795
|
|
|
699
|
-
for (const packageURL of resolve.lookupPackageScope(module._url, {
|
|
796
|
+
for (const packageURL of resolve.lookupPackageScope(module._url, {
|
|
797
|
+
resolutions
|
|
798
|
+
})) {
|
|
700
799
|
if (self._cache[packageURL.href]) {
|
|
701
800
|
pkg = self._cache[packageURL.href]
|
|
702
801
|
break
|
|
703
802
|
}
|
|
704
803
|
|
|
705
|
-
if (protocol.exists(packageURL)) {
|
|
804
|
+
if (protocol.exists(packageURL, constants.types.JSON)) {
|
|
706
805
|
pkg = self.load(packageURL, { protocol })
|
|
707
806
|
break
|
|
708
807
|
}
|
|
@@ -710,13 +809,11 @@ Module._extensions['.js'] = function (module, source, referrer) {
|
|
|
710
809
|
|
|
711
810
|
const info = (pkg && pkg._exports) || {}
|
|
712
811
|
|
|
713
|
-
const isESM =
|
|
812
|
+
const isESM =
|
|
714
813
|
// The default type is ES modules.
|
|
715
|
-
|
|
716
|
-
|
|
814
|
+
constants.types.MODULE === module._defaultType ||
|
|
717
815
|
// The package is explicitly declared as an ES module.
|
|
718
816
|
(info && info.type === 'module')
|
|
719
|
-
)
|
|
720
817
|
|
|
721
818
|
return self._extensions[isESM ? '.mjs' : '.cjs'](module, source, referrer)
|
|
722
819
|
}
|
|
@@ -733,7 +830,12 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
733
830
|
|
|
734
831
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
735
832
|
|
|
736
|
-
module._function = binding.createFunction(
|
|
833
|
+
module._function = binding.createFunction(
|
|
834
|
+
module._url.href,
|
|
835
|
+
['require', 'module', 'exports', '__filename', '__dirname'],
|
|
836
|
+
source,
|
|
837
|
+
0
|
|
838
|
+
)
|
|
737
839
|
}
|
|
738
840
|
}
|
|
739
841
|
|
|
@@ -751,7 +853,12 @@ Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
|
751
853
|
|
|
752
854
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
753
855
|
|
|
754
|
-
module._handle = binding.createModule(
|
|
856
|
+
module._handle = binding.createModule(
|
|
857
|
+
module._url.href,
|
|
858
|
+
source,
|
|
859
|
+
0,
|
|
860
|
+
self._handle
|
|
861
|
+
)
|
|
755
862
|
}
|
|
756
863
|
}
|
|
757
864
|
|
|
@@ -800,27 +907,33 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
800
907
|
|
|
801
908
|
referrer = module
|
|
802
909
|
|
|
803
|
-
const bundle = module._bundle = Bundle.from(source).mount(
|
|
910
|
+
const bundle = (module._bundle = Bundle.from(source).mount(
|
|
911
|
+
module._url.href + '/'
|
|
912
|
+
))
|
|
804
913
|
|
|
805
914
|
module._imports = bundle.imports
|
|
806
915
|
module._resolutions = bundle.resolutions
|
|
807
916
|
|
|
808
917
|
module._protocol = protocol.extend({
|
|
809
|
-
postresolve
|
|
918
|
+
postresolve(context, url) {
|
|
810
919
|
return bundle.exists(url.href) ? url : context.postresolve(url)
|
|
811
920
|
},
|
|
812
921
|
|
|
813
|
-
exists
|
|
922
|
+
exists(context, url) {
|
|
814
923
|
return bundle.exists(url.href) || context.exists(url)
|
|
815
924
|
},
|
|
816
925
|
|
|
817
|
-
read
|
|
926
|
+
read(context, url) {
|
|
818
927
|
return bundle.read(url.href) || context.read(url)
|
|
819
928
|
}
|
|
820
929
|
})
|
|
821
930
|
|
|
822
931
|
if (bundle.main) {
|
|
823
|
-
module._exports = self.load(
|
|
932
|
+
module._exports = self.load(
|
|
933
|
+
new URL(bundle.main),
|
|
934
|
+
bundle.read(bundle.main),
|
|
935
|
+
{ referrer }
|
|
936
|
+
)._exports
|
|
824
937
|
}
|
|
825
938
|
}
|
|
826
939
|
|
|
@@ -849,7 +962,7 @@ Module._extensions['.txt'] = function (module, source, referrer) {
|
|
|
849
962
|
}
|
|
850
963
|
|
|
851
964
|
Module._protocol = new Protocol({
|
|
852
|
-
postresolve
|
|
965
|
+
postresolve(url) {
|
|
853
966
|
switch (url.protocol) {
|
|
854
967
|
case 'file:':
|
|
855
968
|
return pathToFileURL(binding.realpath(fileURLToPath(url)))
|
|
@@ -858,16 +971,21 @@ Module._protocol = new Protocol({
|
|
|
858
971
|
}
|
|
859
972
|
},
|
|
860
973
|
|
|
861
|
-
exists
|
|
974
|
+
exists(url, type = 0) {
|
|
862
975
|
switch (url.protocol) {
|
|
863
976
|
case 'file:':
|
|
864
|
-
return binding.exists(
|
|
977
|
+
return binding.exists(
|
|
978
|
+
fileURLToPath(url),
|
|
979
|
+
type === constants.types.ASSET
|
|
980
|
+
? binding.FILE | binding.DIR
|
|
981
|
+
: binding.FILE
|
|
982
|
+
)
|
|
865
983
|
default:
|
|
866
984
|
return false
|
|
867
985
|
}
|
|
868
986
|
},
|
|
869
987
|
|
|
870
|
-
read
|
|
988
|
+
read(url) {
|
|
871
989
|
switch (url.protocol) {
|
|
872
990
|
case 'file:':
|
|
873
991
|
return Buffer.from(binding.read(fileURLToPath(url)))
|
|
@@ -877,16 +995,15 @@ Module._protocol = new Protocol({
|
|
|
877
995
|
}
|
|
878
996
|
})
|
|
879
997
|
|
|
880
|
-
Bare
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
}
|
|
998
|
+
Bare.prependListener('teardown', () => {
|
|
999
|
+
for (const module of Module._modules) {
|
|
1000
|
+
module.destroy()
|
|
1001
|
+
}
|
|
885
1002
|
|
|
886
|
-
|
|
887
|
-
|
|
1003
|
+
binding.destroy(Module._handle)
|
|
1004
|
+
})
|
|
888
1005
|
|
|
889
|
-
function toURL
|
|
1006
|
+
function toURL(value, base) {
|
|
890
1007
|
if (isURL(value)) return value
|
|
891
1008
|
|
|
892
1009
|
if (startsWithWindowsDriveLetter(value)) {
|
|
@@ -896,34 +1013,42 @@ function toURL (value, base) {
|
|
|
896
1013
|
return URL.parse(value, base) || pathToFileURL(value)
|
|
897
1014
|
}
|
|
898
1015
|
|
|
899
|
-
function urlToPath
|
|
1016
|
+
function urlToPath(url) {
|
|
900
1017
|
if (url.protocol === 'file:') return fileURLToPath(url)
|
|
901
1018
|
|
|
902
1019
|
if (isWindows) {
|
|
903
1020
|
if (/%2f|%5c/i.test(url.pathname)) {
|
|
904
|
-
throw errors.INVALID_URL_PATH(
|
|
1021
|
+
throw errors.INVALID_URL_PATH(
|
|
1022
|
+
'The URL path must not include encoded \\ or / characters'
|
|
1023
|
+
)
|
|
905
1024
|
}
|
|
906
1025
|
} else {
|
|
907
1026
|
if (/%2f/i.test(url.pathname)) {
|
|
908
|
-
throw errors.INVALID_URL_PATH(
|
|
1027
|
+
throw errors.INVALID_URL_PATH(
|
|
1028
|
+
'The URL path must not include encoded / characters'
|
|
1029
|
+
)
|
|
909
1030
|
}
|
|
910
1031
|
}
|
|
911
1032
|
|
|
912
1033
|
return decodeURIComponent(url.pathname)
|
|
913
1034
|
}
|
|
914
1035
|
|
|
915
|
-
function urlToDirname
|
|
1036
|
+
function urlToDirname(url) {
|
|
916
1037
|
if (url.protocol === 'file:') return path.dirname(fileURLToPath(url))
|
|
917
1038
|
|
|
918
1039
|
if (isWindows) {
|
|
919
1040
|
if (/%2f|%5c/i.test(url.pathname)) {
|
|
920
|
-
throw errors.INVALID_URL_PATH(
|
|
1041
|
+
throw errors.INVALID_URL_PATH(
|
|
1042
|
+
'The URL path must not include encoded \\ or / characters'
|
|
1043
|
+
)
|
|
921
1044
|
}
|
|
922
1045
|
} else {
|
|
923
1046
|
if (/%2f/i.test(url.pathname)) {
|
|
924
|
-
throw errors.INVALID_URL_PATH(
|
|
1047
|
+
throw errors.INVALID_URL_PATH(
|
|
1048
|
+
'The URL path must not include encoded / characters'
|
|
1049
|
+
)
|
|
925
1050
|
}
|
|
926
1051
|
}
|
|
927
1052
|
|
|
928
|
-
return decodeURIComponent(
|
|
1053
|
+
return decodeURIComponent(new URL('.', url).pathname).replace(/\/$/, '')
|
|
929
1054
|
}
|