bare-module 1.12.1 → 1.13.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.
Files changed (3) hide show
  1. package/index.js +124 -49
  2. package/lib/constants.js +11 -6
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -5,25 +5,49 @@ const constants = require('./lib/constants')
5
5
  const errors = require('./lib/errors')
6
6
  const binding = require('./binding')
7
7
 
8
- const Module = module.exports = class Module {
8
+ module.exports = exports = class Module {
9
9
  constructor (filename, main) {
10
- this.filename = filename
11
- this.main = main || this
12
- this.exports = null
13
-
14
- this._type = null
15
- this._info = null
16
10
  this._state = 0
17
- this._protocol = null
11
+ this._type = 0
12
+ this._defaultType = this._type
13
+ this._filename = filename
14
+ this._main = main || this
15
+ this._exports = null
18
16
  this._imports = null
17
+ this._info = null
18
+ this._protocol = null
19
19
  this._handle = null
20
20
  }
21
21
 
22
+ get type () {
23
+ return this._type
24
+ }
25
+
26
+ get defaultType () {
27
+ return this._defaultType
28
+ }
29
+
30
+ get filename () {
31
+ return this._filename
32
+ }
33
+
22
34
  get dirname () {
23
- return path.dirname(this.filename)
35
+ return path.dirname(this._filename)
24
36
  }
25
37
 
26
- get importMap () {
38
+ get main () {
39
+ return this._main
40
+ }
41
+
42
+ get exports () {
43
+ return this._exports
44
+ }
45
+
46
+ set exports (value) {
47
+ this._exports = value
48
+ }
49
+
50
+ get imports () {
27
51
  return this._imports
28
52
  }
29
53
 
@@ -37,6 +61,20 @@ const Module = module.exports = class Module {
37
61
  return this.dirname
38
62
  }
39
63
 
64
+ [Symbol.for('bare.inspect')] () {
65
+ return {
66
+ __proto__: { constructor: Module },
67
+
68
+ type: this.type,
69
+ defaultType: this.defaultType,
70
+ filename: this.filename,
71
+ dirname: this.dirname,
72
+ main: this.main,
73
+ exports: this.exports,
74
+ imports: this.imports
75
+ }
76
+ }
77
+
40
78
  static _context = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
41
79
 
42
80
  static _extensions = Object.create(null)
@@ -65,11 +103,23 @@ const Module = module.exports = class Module {
65
103
  specifier = this.resolve(specifier)
66
104
  }
67
105
 
106
+ let type
107
+
108
+ switch (assertions.type) {
109
+ case 'module':
110
+ type = constants.types.MODULE
111
+ break
112
+ case 'json':
113
+ type = constants.types.JSON
114
+ break
115
+ }
116
+
68
117
  const module = this.load(specifier, {
69
118
  protocol: this._protocolFor(specifier, protocol),
70
119
  imports,
71
120
  referrer,
72
- dynamic
121
+ dynamic,
122
+ type
73
123
  })
74
124
 
75
125
  return module._handle
@@ -103,6 +153,7 @@ const Module = module.exports = class Module {
103
153
 
104
154
  static Protocol = Protocol
105
155
  static Bundle = Bundle
156
+ static constants = constants
106
157
 
107
158
  static get cache () {
108
159
  return this._cache
@@ -152,7 +203,9 @@ const Module = module.exports = class Module {
152
203
  protocol = this._protocolFor(specifier, this._protocols['file:']),
153
204
  referrer = null,
154
205
  dynamic = false,
155
- main = referrer ? referrer.main : null
206
+ main = referrer ? referrer._main : null,
207
+ type = 0,
208
+ defaultType = referrer ? referrer._defaultType : 0
156
209
  } = opts
157
210
 
158
211
  if (this._cache[specifier]) return this._transform(this._cache[specifier], referrer, dynamic)
@@ -175,6 +228,8 @@ const Module = module.exports = class Module {
175
228
 
176
229
  const module = this._cache[specifier] = new this(specifier, main)
177
230
 
231
+ module._defaultType = defaultType
232
+
178
233
  let dirname = module.dirname
179
234
  do {
180
235
  const pkg = path.join(dirname, 'package.json')
@@ -192,9 +247,12 @@ const Module = module.exports = class Module {
192
247
  if (specifier in this._builtins) {
193
248
  module.exports = this._builtins[specifier]
194
249
  } else {
195
- let extension = path.extname(specifier)
250
+ let extension = this._extensionFor(type) || path.extname(specifier)
196
251
 
197
- if (extension in this._extensions === false) extension = '.js'
252
+ if (extension in this._extensions === false) {
253
+ if (defaultType) extension = this._extensionFor(defaultType) || '.js'
254
+ else extension = '.js'
255
+ }
198
256
 
199
257
  this._extensions[extension].call(this, module, source, referrer, protocol, imports)
200
258
  }
@@ -331,6 +389,23 @@ const Module = module.exports = class Module {
331
389
  }
332
390
  }
333
391
 
392
+ static _extensionFor (type) {
393
+ switch (type) {
394
+ case constants.types.SCRIPT:
395
+ return '.cjs'
396
+ case constants.types.MODULE:
397
+ return '.esm'
398
+ case constants.types.JSON:
399
+ return '.json'
400
+ case constants.types.BUNDLE:
401
+ return '.bundle'
402
+ case constants.types.ADDON:
403
+ return '.bare'
404
+ default:
405
+ return null
406
+ }
407
+ }
408
+
334
409
  static _protocolFor (specifier, fallback = null) {
335
410
  let protocol = fallback
336
411
 
@@ -390,20 +465,15 @@ const Module = module.exports = class Module {
390
465
 
391
466
  static _transform (module, referrer = null, dynamic = false) {
392
467
  if (dynamic) {
393
- if (module._type !== constants.TYPE_ESM && module._handle === null) {
394
- this._synthesize(module)
395
- }
396
-
468
+ if (module._type !== constants.types.MODULE) this._synthesize(module)
397
469
  this._evaluate(module)
398
470
  } else if (referrer) {
399
- if (referrer._type === constants.TYPE_ESM) {
400
- if (module._type !== constants.TYPE_ESM && module._handle === null) {
401
- this._synthesize(module)
402
- }
403
- } else if (module._type === constants.TYPE_ESM) {
471
+ if (referrer._type === constants.types.MODULE) {
472
+ if (module._type !== constants.types.MODULE) this._synthesize(module)
473
+ } else if (module._type === constants.types.MODULE) {
404
474
  this._evaluate(module)
405
475
  }
406
- } else if (module._type === constants.TYPE_ESM) {
476
+ } else if (module._type === constants.types.MODULE) {
407
477
  this._evaluate(module)
408
478
  }
409
479
 
@@ -411,18 +481,20 @@ const Module = module.exports = class Module {
411
481
  }
412
482
 
413
483
  static _evaluate (module) {
414
- if ((module._state & constants.STATE_EVALUATED) !== 0) return
484
+ if ((module._state & constants.states.EVALUATED) !== 0) return
415
485
 
416
486
  binding.runModule(module._handle, this._context)
417
487
 
418
- if (module._type === constants.TYPE_ESM) {
488
+ if (module._type === constants.types.MODULE) {
419
489
  module.exports = binding.getNamespace(module._handle)
420
490
  }
421
491
 
422
- module._state |= constants.STATE_EVALUATED
492
+ module._state |= constants.states.EVALUATED
423
493
  }
424
494
 
425
495
  static _synthesize (module) {
496
+ if ((module._state & constants.states.SYNTHESIZED) !== 0) return
497
+
426
498
  const names = ['default']
427
499
 
428
500
  for (const key of Object.keys(module.exports)) {
@@ -431,17 +503,20 @@ const Module = module.exports = class Module {
431
503
 
432
504
  module._handle = binding.createSyntheticModule(module.filename, names, this._context)
433
505
 
434
- module._state &= ~constants.STATE_EVALUATED
506
+ module._state |= constants.states.SYNTHESIZED
435
507
  }
436
508
  }
437
509
 
438
- Module._extensions['.js'] = function (module, source, referrer, protocol, imports) {
510
+ exports._extensions['.js'] = function (module, source, referrer, protocol, imports) {
439
511
  const isESM = (
512
+ // The default type is ES modules.
513
+ (module._defaultType === constants.types.MODULE) ||
514
+
440
515
  // The package is explicitly declared as an ES module.
441
516
  (module._info && module._info.type === 'module') ||
442
517
 
443
518
  // The source is a data: URI and the referrer is itself an ES module.
444
- (protocol === this._protocols['data:'] && referrer && referrer._type === constants.TYPE_ESM)
519
+ (protocol === this._protocols['data:'] && referrer && referrer._type === constants.types.MODULE)
445
520
  )
446
521
 
447
522
  const loader = this._extensions[isESM ? '.mjs' : '.cjs']
@@ -449,7 +524,7 @@ Module._extensions['.js'] = function (module, source, referrer, protocol, import
449
524
  return loader.call(this, module, source, referrer, protocol, imports)
450
525
  }
451
526
 
452
- Module._extensions['.cjs'] = function (module, source, referrer, protocol, imports) {
527
+ exports._extensions['.cjs'] = function (module, source, referrer, protocol, imports) {
453
528
  if (source === null) source = protocol.read(module.filename)
454
529
 
455
530
  if (typeof source !== 'string') source = Buffer.coerce(source).toString()
@@ -474,7 +549,7 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
474
549
  return module.exports
475
550
  }
476
551
 
477
- module._type = constants.TYPE_CJS
552
+ module._type = constants.types.SCRIPT
478
553
  module._protocol = protocol
479
554
  module._imports = imports
480
555
 
@@ -493,65 +568,65 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
493
568
  )
494
569
  }
495
570
 
496
- Module._extensions['.mjs'] = function (module, source, referrer, protocol, imports) {
571
+ exports._extensions['.mjs'] = function (module, source, referrer, protocol, imports) {
497
572
  if (source === null) source = protocol.read(module.filename)
498
573
 
499
574
  if (typeof source !== 'string') source = Buffer.coerce(source).toString()
500
575
 
501
- module._type = constants.TYPE_ESM
576
+ module._type = constants.types.MODULE
502
577
  module._protocol = protocol
503
578
  module._imports = imports
504
579
 
505
580
  module._handle = binding.createModule(module.filename, source, 0, this._context)
506
581
  }
507
582
 
508
- Module._extensions['.json'] = function (module, source, referrer, protocol, imports) {
583
+ exports._extensions['.json'] = function (module, source, referrer, protocol, imports) {
509
584
  if (source === null) source = protocol.read(module.filename)
510
585
 
511
586
  if (typeof source !== 'string') source = Buffer.coerce(source).toString()
512
587
 
513
- module._type = constants.TYPE_JSON
588
+ module._type = constants.types.JSON
514
589
  module._protocol = protocol
515
590
  module._imports = imports
516
591
 
517
592
  module.exports = JSON.parse(source)
518
593
  }
519
594
 
520
- Module._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
521
- module._type = constants.TYPE_ADDON
595
+ exports._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
596
+ module._type = constants.types.ADDON
522
597
  module._protocol = protocol
523
598
  module._imports = imports
524
599
 
525
600
  module.exports = process.addon(module.filename)
526
601
  }
527
602
 
528
- Module._extensions['.node'] = function (module, source, referrer, protocol, imports) {
529
- module._type = constants.TYPE_ADDON
603
+ exports._extensions['.node'] = function (module, source, referrer, protocol, imports) {
604
+ module._type = constants.types.ADDON
530
605
  module._protocol = protocol
531
606
  module._imports = imports
532
607
 
533
608
  module.exports = process.addon(module.filename)
534
609
  }
535
610
 
536
- Module._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
611
+ exports._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
537
612
  if (typeof source === 'string') source = Buffer.from(source)
538
613
 
539
614
  const bundle = this._bundleFor(module.filename, protocol, source)
540
615
 
541
- module._type = constants.TYPE_BUNDLE
616
+ module._type = constants.types.BUNDLE
542
617
  module._protocol = protocol
543
618
  module._imports = imports
544
619
 
545
- module.exports = Module.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer }).exports
620
+ module.exports = exports.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer }).exports
546
621
  }
547
622
 
548
- Module._protocols['file:'] = new Protocol({
623
+ exports._protocols['file:'] = new Protocol({
549
624
  preresolve (specifier) {
550
625
  return specifier.replace(/^file:/, '')
551
626
  },
552
627
 
553
628
  postresolve (specifier) {
554
- if (Module.isBuiltin(specifier)) return specifier
629
+ if (exports.isBuiltin(specifier)) return specifier
555
630
  return binding.realpath(specifier)
556
631
  },
557
632
 
@@ -564,13 +639,13 @@ Module._protocols['file:'] = new Protocol({
564
639
  }
565
640
  })
566
641
 
567
- Module._protocols['node:'] = new Protocol({
642
+ exports._protocols['node:'] = new Protocol({
568
643
  preresolve (specifier) {
569
644
  return specifier.replace(/^node:/, '')
570
645
  }
571
646
  })
572
647
 
573
- Module._protocols['data:'] = new Protocol({
648
+ exports._protocols['data:'] = new Protocol({
574
649
  * resolve (specifier) {
575
650
  yield specifier
576
651
  },
@@ -582,8 +657,8 @@ Module._protocols['data:'] = new Protocol({
582
657
  }
583
658
  })
584
659
 
585
- process.on('exit', () => binding.destroy(Module._context))
660
+ process.on('exit', () => binding.destroy(exports._context))
586
661
 
587
662
  if (process.thread) {
588
- process.thread.on('exit', () => binding.destroy(Module._context))
663
+ process.thread.on('exit', () => binding.destroy(exports._context))
589
664
  }
package/lib/constants.js CHANGED
@@ -1,9 +1,14 @@
1
1
  module.exports = {
2
- STATE_EVALUATED: 1,
2
+ states: {
3
+ EVALUATED: 1,
4
+ SYNTHESIZED: 2
5
+ },
3
6
 
4
- TYPE_ESM: 'esm',
5
- TYPE_CJS: 'cjs',
6
- TYPE_JSON: 'json',
7
- TYPE_BUNDLE: 'bundle',
8
- TYPE_ADDON: 'addon'
7
+ types: {
8
+ SCRIPT: 1,
9
+ MODULE: 2,
10
+ JSON: 3,
11
+ BUNDLE: 4,
12
+ ADDON: 5
13
+ }
9
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.12.1",
3
+ "version": "1.13.1",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [