bare-module 6.0.0 → 6.1.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/index.d.ts CHANGED
@@ -64,11 +64,7 @@ declare class Module {
64
64
  static readonly cache: Cache
65
65
 
66
66
  static load(url: URL, opts: LoadOptions): Module
67
- static load(
68
- url: URL,
69
- source?: Buffer | string | Bundle | null,
70
- opts?: LoadOptions
71
- ): Module
67
+ static load(url: URL, source?: Buffer | string | Bundle | null, opts?: LoadOptions): Module
72
68
 
73
69
  static resolve(specifier: string, parentURL: URL, opts?: ResolveOptions): URL
74
70
 
@@ -115,10 +111,7 @@ declare namespace Module {
115
111
  asset: (specifier: string, parentURL?: URL) => string
116
112
  }
117
113
 
118
- export function createRequire(
119
- parentURL: string | URL,
120
- opts?: CreateRequireOptions
121
- ): Require
114
+ export function createRequire(parentURL: string | URL, opts?: CreateRequireOptions): Require
122
115
  }
123
116
 
124
117
  export = Module
package/index.js CHANGED
@@ -192,11 +192,7 @@ module.exports = exports = class Module {
192
192
 
193
193
  this._names = Array.from(names)
194
194
 
195
- this._handle = binding.createSyntheticModule(
196
- this._url.href,
197
- this._names,
198
- Module._handle
199
- )
195
+ this._handle = binding.createSyntheticModule(this._url.href, this._names, Module._handle)
200
196
  }
201
197
 
202
198
  _evaluate() {
@@ -211,13 +207,7 @@ module.exports = exports = class Module {
211
207
 
212
208
  const fn = this._function // Bind to variable to ensure proper stack trace
213
209
 
214
- fn(
215
- require,
216
- this,
217
- this._exports,
218
- urlToPath(this._url),
219
- urlToDirname(this._url)
220
- )
210
+ fn(require, this, this._exports, urlToPath(this._url), urlToDirname(this._url))
221
211
  } else if (this._type === constants.types.MODULE) {
222
212
  this._run()
223
213
 
@@ -232,11 +222,7 @@ module.exports = exports = class Module {
232
222
 
233
223
  this._synthesize()
234
224
 
235
- this._promise = binding.runModule(
236
- this._handle,
237
- Module._handle,
238
- Module._onrun
239
- )
225
+ this._promise = binding.runModule(this._handle, Module._handle, Module._onrun)
240
226
  }
241
227
 
242
228
  [Symbol.for('bare.inspect')]() {
@@ -256,23 +242,24 @@ module.exports = exports = class Module {
256
242
  }
257
243
 
258
244
  static _extensions = Object.create(null)
245
+
259
246
  static _protocol = null
260
- static _cache = module.cache || Object.create(null)
247
+
248
+ static _cache =
249
+ module[kind] === Module[kind] ? module.cache || Object.create(null) : Object.create(null)
250
+
261
251
  static _conditions = ['bare', 'node', Bare.platform, Bare.arch]
262
252
 
263
- static _handle = binding.init(
264
- this,
265
- this._onimport,
266
- this._onevaluate,
267
- this._onmeta
268
- )
253
+ static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
269
254
 
270
255
  static _onimport(specifier, attributes, referrerHref, isDynamicImport) {
271
256
  const referrer = this._cache[referrerHref] || null
272
257
 
273
258
  if (referrer === null) {
274
259
  throw errors.MODULE_NOT_FOUND(
275
- `Cannot find referrer for module '${specifier}' imported from '${referrerHref}'`
260
+ `Cannot find referrer for module '${specifier}' imported from '${referrerHref}'`,
261
+ specifier,
262
+ referrer
276
263
  )
277
264
  }
278
265
 
@@ -298,7 +285,7 @@ module.exports = exports = class Module {
298
285
  const module = this._cache[href] || null
299
286
 
300
287
  if (module === null) {
301
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`)
288
+ throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`, href)
302
289
  }
303
290
 
304
291
  module._evaluate()
@@ -327,7 +314,7 @@ module.exports = exports = class Module {
327
314
  const module = this._cache[href] || null
328
315
 
329
316
  if (module === null) {
330
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`)
317
+ throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`, href)
331
318
  }
332
319
 
333
320
  const referrer = module
@@ -343,21 +330,14 @@ module.exports = exports = class Module {
343
330
  }
344
331
 
345
332
  meta.addon = function addon(specifier = '.', parentURL = referrer._url) {
346
- const resolved = Bare.Addon.resolve(
347
- specifier,
348
- toURL(parentURL, referrer._url),
349
- { referrer }
350
- )
333
+ const resolved = Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
351
334
 
352
335
  const addon = Bare.Addon.load(resolved, { referrer })
353
336
 
354
337
  return addon._exports
355
338
  }
356
339
 
357
- meta.addon.resolve = function resolve(
358
- specifier = '.',
359
- parentURL = referrer._url
360
- ) {
340
+ meta.addon.resolve = function resolve(specifier = '.', parentURL = referrer._url) {
361
341
  return Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
362
342
  referrer
363
343
  }).href
@@ -467,15 +447,10 @@ module.exports = exports = class Module {
467
447
  type: constants.types.JSON
468
448
  })
469
449
 
470
- module._imports = mixinImports(
471
- module._imports,
472
- imports._exports,
473
- resolved
474
- )
450
+ module._imports = mixinImports(module._imports, imports._exports, resolved)
475
451
  }
476
452
 
477
- let extension =
478
- canonicalExtensionForType(type) || path.extname(url.pathname)
453
+ let extension = canonicalExtensionForType(type) || path.extname(url.pathname)
479
454
 
480
455
  if (extension in self._extensions === false) {
481
456
  if (defaultType) {
@@ -531,9 +506,7 @@ module.exports = exports = class Module {
531
506
  resolved,
532
507
  parentURL,
533
508
  {
534
- conditions: isImport
535
- ? ['import', ...conditions]
536
- : ['require', ...conditions],
509
+ conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
537
510
  imports,
538
511
  resolutions,
539
512
  extensions,
@@ -561,7 +534,7 @@ module.exports = exports = class Module {
561
534
  message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
562
535
  }
563
536
 
564
- throw errors.MODULE_NOT_FOUND(message, candidates)
537
+ throw errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
565
538
 
566
539
  function readPackage(packageURL) {
567
540
  if (protocol.exists(packageURL, constants.types.JSON)) {
@@ -595,6 +568,8 @@ module.exports = exports = class Module {
595
568
 
596
569
  if (resolution) return protocol.postresolve(resolution)
597
570
 
571
+ const candidates = []
572
+
598
573
  for (const resolution of resolve(
599
574
  resolved,
600
575
  parentURL,
@@ -606,16 +581,21 @@ module.exports = exports = class Module {
606
581
  },
607
582
  readPackage
608
583
  )) {
584
+ candidates.push(resolution)
585
+
609
586
  if (protocol.exists(resolution, constants.types.ASSET)) {
610
- return protocol.postresolve(
611
- protocol.asset ? protocol.asset(resolution) : resolution
612
- )
587
+ return protocol.postresolve(protocol.asset ? protocol.asset(resolution) : resolution)
613
588
  }
614
589
  }
615
590
 
616
- throw errors.ASSET_NOT_FOUND(
617
- `Cannot find asset '${specifier}' imported from '${parentURL.href}'`
618
- )
591
+ let message = `Cannot find asset '${specifier}' imported from '${parentURL.href}'`
592
+
593
+ if (candidates.length > 0) {
594
+ message += '\nCandidates:'
595
+ message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
596
+ }
597
+
598
+ throw errors.ASSET_NOT_FOUND(message, specifier, parentURL, candidates)
619
599
 
620
600
  function readPackage(packageURL) {
621
601
  if (protocol.exists(packageURL, constants.types.JSON)) {
@@ -721,9 +701,7 @@ function mixinImports(target, imports, url) {
721
701
  }
722
702
 
723
703
  if (typeof imports !== 'object' || imports === null) {
724
- throw errors.INVALID_IMPORTS_MAP(
725
- `Imports map at '${url.href}' is not valid`
726
- )
704
+ throw errors.INVALID_IMPORTS_MAP(`Imports map at '${url.href}' is not valid`)
727
705
  }
728
706
 
729
707
  return { ...target, ...imports }
@@ -791,17 +769,11 @@ exports.createRequire = function createRequire(parentURL, opts = {}) {
791
769
  require.cache = module._cache
792
770
 
793
771
  require.resolve = function resolve(specifier, parentURL = referrer._url) {
794
- return urlToPath(
795
- self.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
796
- )
772
+ return urlToPath(self.resolve(specifier, toURL(parentURL, referrer._url), { referrer }))
797
773
  }
798
774
 
799
775
  require.addon = function addon(specifier = '.', parentURL = referrer._url) {
800
- const resolved = Bare.Addon.resolve(
801
- specifier,
802
- toURL(parentURL, referrer._url),
803
- { referrer }
804
- )
776
+ const resolved = Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
805
777
 
806
778
  const addon = Bare.Addon.load(resolved, { referrer })
807
779
 
@@ -810,10 +782,7 @@ exports.createRequire = function createRequire(parentURL, opts = {}) {
810
782
 
811
783
  require.addon.host = Bare.Addon.host
812
784
 
813
- require.addon.resolve = function resolve(
814
- specifier = '.',
815
- parentURL = referrer._url
816
- ) {
785
+ require.addon.resolve = function resolve(specifier = '.', parentURL = referrer._url) {
817
786
  return urlToPath(
818
787
  Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
819
788
  referrer
@@ -822,9 +791,7 @@ exports.createRequire = function createRequire(parentURL, opts = {}) {
822
791
  }
823
792
 
824
793
  require.asset = function asset(specifier, parentURL = referrer._url) {
825
- return urlToPath(
826
- self.asset(specifier, toURL(parentURL, referrer._url), { referrer })
827
- )
794
+ return urlToPath(self.asset(specifier, toURL(parentURL, referrer._url), { referrer }))
828
795
  }
829
796
 
830
797
  return require
@@ -897,12 +864,7 @@ Module._extensions['.mjs'] = function (module, source, referrer) {
897
864
 
898
865
  module._source = source
899
866
 
900
- module._handle = binding.createModule(
901
- module._url.href,
902
- source.toString(),
903
- 0,
904
- self._handle
905
- )
867
+ module._handle = binding.createModule(module._url.href, source.toString(), 0, self._handle)
906
868
  }
907
869
 
908
870
  Module._extensions['.json'] = function (module, source, referrer) {
@@ -968,11 +930,9 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
968
930
  })
969
931
 
970
932
  if (bundle.main) {
971
- module._exports = self.load(
972
- new URL(bundle.main),
973
- bundle.read(bundle.main),
974
- { referrer }
975
- )._exports
933
+ module._exports = self.load(new URL(bundle.main), bundle.read(bundle.main), {
934
+ referrer
935
+ })._exports
976
936
  }
977
937
  }
978
938
 
@@ -1005,9 +965,7 @@ Module._protocol = new Protocol({
1005
965
  postresolve(url) {
1006
966
  switch (url.protocol) {
1007
967
  case 'file:':
1008
- return pathToFileURL(
1009
- binding.realpath(path.toNamespacedPath(fileURLToPath(url)))
1010
- )
968
+ return pathToFileURL(binding.realpath(path.toNamespacedPath(fileURLToPath(url))))
1011
969
  default:
1012
970
  return url
1013
971
  }
@@ -1018,9 +976,7 @@ Module._protocol = new Protocol({
1018
976
  case 'file:':
1019
977
  return binding.exists(
1020
978
  path.toNamespacedPath(fileURLToPath(url)),
1021
- type === constants.types.ASSET
1022
- ? binding.FILE | binding.DIR
1023
- : binding.FILE
979
+ type === constants.types.ASSET ? binding.FILE | binding.DIR : binding.FILE
1024
980
  )
1025
981
  default:
1026
982
  return false
@@ -1030,9 +986,7 @@ Module._protocol = new Protocol({
1030
986
  read(url) {
1031
987
  switch (url.protocol) {
1032
988
  case 'file:':
1033
- return Buffer.from(
1034
- binding.read(path.toNamespacedPath(fileURLToPath(url)))
1035
- )
989
+ return Buffer.from(binding.read(path.toNamespacedPath(fileURLToPath(url))))
1036
990
  default:
1037
991
  throw errors.UNKNOWN_PROTOCOL(`Cannot load module '${url.href}'`)
1038
992
  }
@@ -1054,15 +1008,11 @@ function urlToPath(url) {
1054
1008
 
1055
1009
  if (isWindows) {
1056
1010
  if (/%2f|%5c/i.test(url.pathname)) {
1057
- throw errors.INVALID_URL_PATH(
1058
- 'The URL path must not include encoded \\ or / characters'
1059
- )
1011
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
1060
1012
  }
1061
1013
  } else {
1062
1014
  if (/%2f/i.test(url.pathname)) {
1063
- throw errors.INVALID_URL_PATH(
1064
- 'The URL path must not include encoded / characters'
1065
- )
1015
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
1066
1016
  }
1067
1017
  }
1068
1018
 
@@ -1074,15 +1024,11 @@ function urlToDirname(url) {
1074
1024
 
1075
1025
  if (isWindows) {
1076
1026
  if (/%2f|%5c/i.test(url.pathname)) {
1077
- throw errors.INVALID_URL_PATH(
1078
- 'The URL path must not include encoded \\ or / characters'
1079
- )
1027
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
1080
1028
  }
1081
1029
  } else {
1082
1030
  if (/%2f/i.test(url.pathname)) {
1083
- throw errors.INVALID_URL_PATH(
1084
- 'The URL path must not include encoded / characters'
1085
- )
1031
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
1086
1032
  }
1087
1033
  }
1088
1034
 
package/lib/errors.js CHANGED
@@ -1,5 +1,5 @@
1
1
  module.exports = class ModuleError extends Error {
2
- constructor(msg, code, fn = ModuleError) {
2
+ constructor(msg, fn = ModuleError, code = fn.name) {
3
3
  super(`${code}: ${msg}`)
4
4
  this.code = code
5
5
 
@@ -12,59 +12,43 @@ module.exports = class ModuleError extends Error {
12
12
  return 'ModuleError'
13
13
  }
14
14
 
15
- static MODULE_NOT_FOUND(msg, candidates = []) {
16
- const err = new ModuleError(
17
- msg,
18
- 'MODULE_NOT_FOUND',
19
- ModuleError.MODULE_NOT_FOUND
20
- )
15
+ static MODULE_NOT_FOUND(msg, specifier, referrer = null, candidates = []) {
16
+ const err = new ModuleError(msg, ModuleError.MODULE_NOT_FOUND)
21
17
 
18
+ err.specifier = specifier
19
+ err.referrer = referrer
22
20
  err.candidates = candidates
23
21
 
24
22
  return err
25
23
  }
26
24
 
27
- static ASSET_NOT_FOUND(msg) {
28
- return new ModuleError(msg, 'ASSET_NOT_FOUND', ModuleError.ASSET_NOT_FOUND)
25
+ static ASSET_NOT_FOUND(msg, specifier, referrer = null, candidates = []) {
26
+ const err = new ModuleError(msg, ModuleError.ASSET_NOT_FOUND)
27
+
28
+ err.specifier = specifier
29
+ err.referrer = referrer
30
+ err.candidates = candidates
31
+
32
+ return err
29
33
  }
30
34
 
31
35
  static UNKNOWN_PROTOCOL(msg) {
32
- return new ModuleError(
33
- msg,
34
- 'UNKNOWN_PROTOCOL',
35
- ModuleError.UNKNOWN_PROTOCOL
36
- )
36
+ return new ModuleError(msg, ModuleError.UNKNOWN_PROTOCOL)
37
37
  }
38
38
 
39
39
  static INVALID_BUNDLE_EXTENSION(msg) {
40
- return new ModuleError(
41
- msg,
42
- 'INVALID_BUNDLE_EXTENSION',
43
- ModuleError.INVALID_BUNDLE_EXTENSION
44
- )
40
+ return new ModuleError(msg, ModuleError.INVALID_BUNDLE_EXTENSION)
45
41
  }
46
42
 
47
43
  static INVALID_URL_PATH(msg) {
48
- return new ModuleError(
49
- msg,
50
- 'INVALID_URL_PATH',
51
- ModuleError.INVALID_URL_PATH
52
- )
44
+ return new ModuleError(msg, ModuleError.INVALID_URL_PATH)
53
45
  }
54
46
 
55
47
  static INVALID_IMPORTS_MAP(msg) {
56
- return new ModuleError(
57
- msg,
58
- 'INVALID_IMPORTS_MAP',
59
- ModuleError.INVALID_IMPORTS_MAP
60
- )
48
+ return new ModuleError(msg, ModuleError.INVALID_IMPORTS_MAP)
61
49
  }
62
50
 
63
51
  static TYPE_INCOMPATIBLE(msg) {
64
- return new ModuleError(
65
- msg,
66
- 'TYPE_INCOMPATIBLE',
67
- ModuleError.TYPE_INCOMPATIBLE
68
- )
52
+ return new ModuleError(msg, ModuleError.TYPE_INCOMPATIBLE)
69
53
  }
70
54
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "exports": {
6
6
  ".": {
@@ -47,7 +47,7 @@
47
47
  "brittle": "^3.1.1",
48
48
  "cmake-bare": "^1.1.6",
49
49
  "prettier": "^3.3.3",
50
- "prettier-config-standard": "^7.0.0"
50
+ "prettier-config-holepunch": "^2.0.0"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "bare-buffer": "*"