bare-module 4.4.0 → 4.5.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 CHANGED
@@ -212,11 +212,14 @@ Options include:
212
212
  {
213
213
  isImport = false,
214
214
  referrer = null,
215
+ type,
216
+ extensions,
215
217
  protocol,
216
218
  imports,
217
219
  resolutions,
218
220
  builtins,
219
- conditions
221
+ conditions,
222
+ attributes
220
223
  }
221
224
  ```
222
225
 
@@ -235,7 +238,8 @@ Options include:
235
238
  imports,
236
239
  resolutions,
237
240
  builtins,
238
- conditions
241
+ conditions,
242
+ attributes
239
243
  }
240
244
  ```
241
245
 
package/index.js CHANGED
@@ -112,6 +112,10 @@ const Module = module.exports = exports = class Module {
112
112
  Module._modules.delete(this)
113
113
  }
114
114
 
115
+ _run () {
116
+ binding.runModule(this._handle, Module._handle, Module._onrun)
117
+ }
118
+
115
119
  _transform (isImport, isDynamicImport) {
116
120
  if (isDynamicImport) {
117
121
  this._synthesize()
@@ -183,10 +187,6 @@ const Module = module.exports = exports = class Module {
183
187
  this._handle = binding.createSyntheticModule(this._url.href, this._names, Module._handle)
184
188
  }
185
189
 
186
- _run () {
187
- binding.runModule(this._handle, Module._handle, Module._onrun)
188
- }
189
-
190
190
  _evaluate (eagerRun = false) {
191
191
  if ((this._state & constants.states.EVALUATED) !== 0) return
192
192
 
@@ -208,16 +208,12 @@ const Module = module.exports = exports = class Module {
208
208
  )
209
209
 
210
210
  if (eagerRun) this._run()
211
- }
212
-
213
- if (this._type === constants.types.MODULE) {
211
+ } else if (this._type === constants.types.MODULE) {
214
212
  this._run()
215
213
 
216
214
  this._exports = binding.getNamespace(this._handle)
217
- }
218
-
219
- if (this._type === constants.types.ADDON) {
220
- if (eagerRun) this._run()
215
+ } else if (eagerRun) {
216
+ this._run()
221
217
  }
222
218
  }
223
219
 
@@ -245,7 +241,7 @@ const Module = module.exports = exports = class Module {
245
241
 
246
242
  static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
247
243
 
248
- static _onimport (href, assertions, referrerHref, isDynamicImport) {
244
+ static _onimport (href, attributes, referrerHref, isDynamicImport) {
249
245
  const referrer = this._cache[referrerHref] || null
250
246
 
251
247
  if (referrer === null) {
@@ -256,28 +252,9 @@ const Module = module.exports = exports = class Module {
256
252
  throw errors.MODULE_NOT_FOUND(msg)
257
253
  }
258
254
 
259
- const url = this.resolve(href, referrer._url, {
260
- isImport: true,
261
- referrer
262
- })
263
-
264
- let type
255
+ const url = this.resolve(href, referrer._url, { isImport: true, referrer, attributes })
265
256
 
266
- switch (assertions.type) {
267
- case 'module':
268
- type = constants.types.MODULE
269
- break
270
- case 'json':
271
- type = constants.types.JSON
272
- break
273
- }
274
-
275
- const module = this.load(url, {
276
- isImport: true,
277
- isDynamicImport,
278
- referrer,
279
- type
280
- })
257
+ const module = this.load(url, { isImport: true, isDynamicImport, referrer, attributes })
281
258
 
282
259
  return module._handle
283
260
  }
@@ -331,7 +308,7 @@ const Module = module.exports = exports = class Module {
331
308
  meta.asset = asset
332
309
 
333
310
  function resolve (specifier, parentURL = referrer._url) {
334
- const resolved = self.resolve(specifier, toURL(parentURL), { referrer })
311
+ const resolved = self.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
335
312
 
336
313
  switch (resolved.protocol) {
337
314
  case 'builtin:': return resolved.pathname
@@ -340,7 +317,7 @@ const Module = module.exports = exports = class Module {
340
317
  }
341
318
 
342
319
  function addon (specifier = '.', parentURL = referrer._url) {
343
- const resolved = Bare.Addon.resolve(specifier, toURL(parentURL), { referrer })
320
+ const resolved = Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
344
321
 
345
322
  const addon = Bare.Addon.load(resolved, { referrer })
346
323
 
@@ -348,7 +325,7 @@ const Module = module.exports = exports = class Module {
348
325
  }
349
326
 
350
327
  function asset (specifier, parentURL = referrer._url) {
351
- return self.asset(specifier, toURL(parentURL), { referrer }).href
328
+ return self.asset(specifier, toURL(parentURL, referrer._url), { referrer }).href
352
329
  }
353
330
  }
354
331
 
@@ -383,7 +360,8 @@ const Module = module.exports = exports = class Module {
383
360
  isDynamicImport = false,
384
361
 
385
362
  referrer = null,
386
- type = 0,
363
+ attributes,
364
+ type = typeForAttributes(attributes),
387
365
  defaultType = referrer ? referrer._defaultType : 0,
388
366
  cache = referrer ? referrer._cache : self._cache,
389
367
  main = referrer ? referrer._main : null,
@@ -414,10 +392,10 @@ const Module = module.exports = exports = class Module {
414
392
  module._builtins = builtins
415
393
  module._conditions = conditions
416
394
 
417
- let extension = self._extensionFor(type) || path.extname(url.pathname)
395
+ let extension = canonicalExtensionForType(type) || path.extname(url.pathname)
418
396
 
419
397
  if (extension in self._extensions === false) {
420
- if (defaultType) extension = self._extensionFor(defaultType) || '.js'
398
+ if (defaultType) extension = canonicalExtensionForType(defaultType) || '.js'
421
399
  else extension = '.js'
422
400
  }
423
401
 
@@ -444,6 +422,9 @@ const Module = module.exports = exports = class Module {
444
422
  isImport = false,
445
423
 
446
424
  referrer = null,
425
+ attributes,
426
+ type = typeForAttributes(attributes),
427
+ extensions = extensionsForType(type),
447
428
  protocol = referrer ? referrer._protocol : self._protocol,
448
429
  imports = referrer ? referrer._imports : null,
449
430
  resolutions = referrer ? referrer._resolutions : null,
@@ -461,18 +442,11 @@ const Module = module.exports = exports = class Module {
461
442
  conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
462
443
  imports,
463
444
  resolutions,
445
+ extensions,
464
446
  builtins: builtins ? Object.keys(builtins) : [],
465
447
  engines: {
466
448
  ...Bare.versions
467
- },
468
- extensions: [
469
- '.js',
470
- '.cjs',
471
- '.mjs',
472
- '.json',
473
- '.bare',
474
- '.node'
475
- ]
449
+ }
476
450
  }, readPackage)) {
477
451
  switch (resolution.protocol) {
478
452
  case 'builtin:': return resolution
@@ -537,22 +511,70 @@ const Module = module.exports = exports = class Module {
537
511
  return null
538
512
  }
539
513
  }
514
+ }
540
515
 
541
- static _extensionFor (type) {
542
- switch (type) {
543
- case constants.types.SCRIPT:
544
- return '.cjs'
545
- case constants.types.MODULE:
546
- return '.esm'
547
- case constants.types.JSON:
548
- return '.json'
549
- case constants.types.BUNDLE:
550
- return '.bundle'
551
- case constants.types.ADDON:
552
- return '.bare'
553
- default:
554
- return null
555
- }
516
+ function extensionsForType (type) {
517
+ switch (type) {
518
+ case constants.types.SCRIPT:
519
+ return ['.js', '.cjs']
520
+ case constants.types.MODULE:
521
+ return ['.js', '.mjs']
522
+ case constants.types.JSON:
523
+ return ['.json']
524
+ case constants.types.BUNDLE:
525
+ return ['.json']
526
+ case constants.types.ADDON:
527
+ return ['.bare', '.node']
528
+ case constants.types.BINARY:
529
+ return ['.bin']
530
+ case constants.types.TEXT:
531
+ return ['.txt']
532
+ default:
533
+ return ['.js', '.cjs', '.mjs', '.json', '.bare', '.node']
534
+ }
535
+ }
536
+
537
+ function canonicalExtensionForType (type) {
538
+ switch (type) {
539
+ case constants.types.SCRIPT:
540
+ return '.cjs'
541
+ case constants.types.MODULE:
542
+ return '.esm'
543
+ case constants.types.JSON:
544
+ return '.json'
545
+ case constants.types.BUNDLE:
546
+ return '.bundle'
547
+ case constants.types.ADDON:
548
+ return '.bare'
549
+ case constants.types.BINARY:
550
+ return '.bin'
551
+ case constants.types.TEXT:
552
+ return '.txt'
553
+ default:
554
+ return null
555
+ }
556
+ }
557
+
558
+ function typeForAttributes (attributes) {
559
+ if (typeof attributes !== 'object' || attributes === null) return 0
560
+
561
+ switch (attributes.type) {
562
+ case 'script':
563
+ return constants.types.SCRIPT
564
+ case 'module':
565
+ return constants.types.MODULE
566
+ case 'json':
567
+ return constants.types.JSON
568
+ case 'bundle':
569
+ return constants.types.BUNDLE
570
+ case 'addon':
571
+ return constants.types.ADDON
572
+ case 'binary':
573
+ return constants.types.BINARY
574
+ case 'text':
575
+ return constants.types.TEXT
576
+ default:
577
+ return 0
556
578
  }
557
579
  }
558
580
 
@@ -613,16 +635,18 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
613
635
 
614
636
  return require
615
637
 
616
- function require (specifier) {
617
- const resolved = self.resolve(specifier, referrer._url, { referrer })
638
+ function require (specifier, opts = {}) {
639
+ const attributes = opts && opts.with
640
+
641
+ const resolved = self.resolve(specifier, referrer._url, { referrer, attributes })
618
642
 
619
- const module = self.load(resolved, { referrer })
643
+ const module = self.load(resolved, { referrer, attributes })
620
644
 
621
645
  return module._exports
622
646
  }
623
647
 
624
648
  function resolve (specifier, parentURL = referrer._url) {
625
- const resolved = self.resolve(specifier, toURL(parentURL), { referrer })
649
+ const resolved = self.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
626
650
 
627
651
  switch (resolved.protocol) {
628
652
  case 'builtin:': return resolved.pathname
@@ -631,7 +655,7 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
631
655
  }
632
656
 
633
657
  function addon (specifier = '.', parentURL = referrer._url) {
634
- const resolved = Bare.Addon.resolve(specifier, toURL(parentURL), { referrer })
658
+ const resolved = Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
635
659
 
636
660
  const addon = Bare.Addon.load(resolved, { referrer })
637
661
 
@@ -639,7 +663,7 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
639
663
  }
640
664
 
641
665
  function asset (specifier, parentURL = referrer._url) {
642
- return urlToPath(self.asset(specifier, toURL(parentURL), { referrer }))
666
+ return urlToPath(self.asset(specifier, toURL(parentURL, referrer._url), { referrer }))
643
667
  }
644
668
  }
645
669
 
@@ -781,6 +805,30 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
781
805
  }
782
806
  }
783
807
 
808
+ Module._extensions['.bin'] = function (module, source, referrer) {
809
+ const protocol = module._protocol
810
+
811
+ module._type = constants.types.BINARY
812
+
813
+ if (source === null) source = protocol.read(module._url)
814
+
815
+ if (typeof source === 'string') source = Buffer.from(source)
816
+
817
+ module._exports = source
818
+ }
819
+
820
+ Module._extensions['.txt'] = function (module, source, referrer) {
821
+ const protocol = module._protocol
822
+
823
+ module._type = constants.types.TEXT
824
+
825
+ if (source === null) source = protocol.read(module._url)
826
+
827
+ if (typeof source !== 'string') source = Buffer.coerce(source).toString()
828
+
829
+ module._exports = source
830
+ }
831
+
784
832
  Module._protocol = new Protocol({
785
833
  postresolve (url) {
786
834
  switch (url.protocol) {
@@ -819,14 +867,14 @@ Bare
819
867
  binding.destroy(Module._handle)
820
868
  })
821
869
 
822
- function toURL (value) {
870
+ function toURL (value, base) {
823
871
  if (isURL(value)) return value
824
872
 
825
873
  if (startsWithWindowsDriveLetter(value)) {
826
874
  return pathToFileURL(value)
827
875
  }
828
876
 
829
- return URL.parse(value) || pathToFileURL(value)
877
+ return URL.parse(value, base) || pathToFileURL(value)
830
878
  }
831
879
 
832
880
  function urlToPath (url) {
package/lib/constants.js CHANGED
@@ -10,6 +10,8 @@ module.exports = {
10
10
  MODULE: 2,
11
11
  JSON: 3,
12
12
  BUNDLE: 4,
13
- ADDON: 5
13
+ ADDON: 5,
14
+ BINARY: 6,
15
+ TEXT: 7
14
16
  }
15
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "4.4.0",
3
+ "version": "4.5.1",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [