bare-module 6.0.1 → 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')]() {
@@ -260,25 +246,20 @@ module.exports = exports = class Module {
260
246
  static _protocol = null
261
247
 
262
248
  static _cache =
263
- module[kind] === Module[kind]
264
- ? module.cache || Object.create(null)
265
- : Object.create(null)
249
+ module[kind] === Module[kind] ? module.cache || Object.create(null) : Object.create(null)
266
250
 
267
251
  static _conditions = ['bare', 'node', Bare.platform, Bare.arch]
268
252
 
269
- static _handle = binding.init(
270
- this,
271
- this._onimport,
272
- this._onevaluate,
273
- this._onmeta
274
- )
253
+ static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
275
254
 
276
255
  static _onimport(specifier, attributes, referrerHref, isDynamicImport) {
277
256
  const referrer = this._cache[referrerHref] || null
278
257
 
279
258
  if (referrer === null) {
280
259
  throw errors.MODULE_NOT_FOUND(
281
- `Cannot find referrer for module '${specifier}' imported from '${referrerHref}'`
260
+ `Cannot find referrer for module '${specifier}' imported from '${referrerHref}'`,
261
+ specifier,
262
+ referrer
282
263
  )
283
264
  }
284
265
 
@@ -304,7 +285,7 @@ module.exports = exports = class Module {
304
285
  const module = this._cache[href] || null
305
286
 
306
287
  if (module === null) {
307
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`)
288
+ throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`, href)
308
289
  }
309
290
 
310
291
  module._evaluate()
@@ -333,7 +314,7 @@ module.exports = exports = class Module {
333
314
  const module = this._cache[href] || null
334
315
 
335
316
  if (module === null) {
336
- throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`)
317
+ throw errors.MODULE_NOT_FOUND(`Cannot find module '${href}'`, href)
337
318
  }
338
319
 
339
320
  const referrer = module
@@ -349,21 +330,14 @@ module.exports = exports = class Module {
349
330
  }
350
331
 
351
332
  meta.addon = function addon(specifier = '.', parentURL = referrer._url) {
352
- const resolved = Bare.Addon.resolve(
353
- specifier,
354
- toURL(parentURL, referrer._url),
355
- { referrer }
356
- )
333
+ const resolved = Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
357
334
 
358
335
  const addon = Bare.Addon.load(resolved, { referrer })
359
336
 
360
337
  return addon._exports
361
338
  }
362
339
 
363
- meta.addon.resolve = function resolve(
364
- specifier = '.',
365
- parentURL = referrer._url
366
- ) {
340
+ meta.addon.resolve = function resolve(specifier = '.', parentURL = referrer._url) {
367
341
  return Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
368
342
  referrer
369
343
  }).href
@@ -473,15 +447,10 @@ module.exports = exports = class Module {
473
447
  type: constants.types.JSON
474
448
  })
475
449
 
476
- module._imports = mixinImports(
477
- module._imports,
478
- imports._exports,
479
- resolved
480
- )
450
+ module._imports = mixinImports(module._imports, imports._exports, resolved)
481
451
  }
482
452
 
483
- let extension =
484
- canonicalExtensionForType(type) || path.extname(url.pathname)
453
+ let extension = canonicalExtensionForType(type) || path.extname(url.pathname)
485
454
 
486
455
  if (extension in self._extensions === false) {
487
456
  if (defaultType) {
@@ -537,9 +506,7 @@ module.exports = exports = class Module {
537
506
  resolved,
538
507
  parentURL,
539
508
  {
540
- conditions: isImport
541
- ? ['import', ...conditions]
542
- : ['require', ...conditions],
509
+ conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
543
510
  imports,
544
511
  resolutions,
545
512
  extensions,
@@ -567,7 +534,7 @@ module.exports = exports = class Module {
567
534
  message += '\n' + candidates.map((url) => '- ' + url.href).join('\n')
568
535
  }
569
536
 
570
- throw errors.MODULE_NOT_FOUND(message, candidates)
537
+ throw errors.MODULE_NOT_FOUND(message, specifier, parentURL, candidates)
571
538
 
572
539
  function readPackage(packageURL) {
573
540
  if (protocol.exists(packageURL, constants.types.JSON)) {
@@ -601,6 +568,8 @@ module.exports = exports = class Module {
601
568
 
602
569
  if (resolution) return protocol.postresolve(resolution)
603
570
 
571
+ const candidates = []
572
+
604
573
  for (const resolution of resolve(
605
574
  resolved,
606
575
  parentURL,
@@ -612,16 +581,21 @@ module.exports = exports = class Module {
612
581
  },
613
582
  readPackage
614
583
  )) {
584
+ candidates.push(resolution)
585
+
615
586
  if (protocol.exists(resolution, constants.types.ASSET)) {
616
- return protocol.postresolve(
617
- protocol.asset ? protocol.asset(resolution) : resolution
618
- )
587
+ return protocol.postresolve(protocol.asset ? protocol.asset(resolution) : resolution)
619
588
  }
620
589
  }
621
590
 
622
- throw errors.ASSET_NOT_FOUND(
623
- `Cannot find asset '${specifier}' imported from '${parentURL.href}'`
624
- )
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)
625
599
 
626
600
  function readPackage(packageURL) {
627
601
  if (protocol.exists(packageURL, constants.types.JSON)) {
@@ -727,9 +701,7 @@ function mixinImports(target, imports, url) {
727
701
  }
728
702
 
729
703
  if (typeof imports !== 'object' || imports === null) {
730
- throw errors.INVALID_IMPORTS_MAP(
731
- `Imports map at '${url.href}' is not valid`
732
- )
704
+ throw errors.INVALID_IMPORTS_MAP(`Imports map at '${url.href}' is not valid`)
733
705
  }
734
706
 
735
707
  return { ...target, ...imports }
@@ -797,17 +769,11 @@ exports.createRequire = function createRequire(parentURL, opts = {}) {
797
769
  require.cache = module._cache
798
770
 
799
771
  require.resolve = function resolve(specifier, parentURL = referrer._url) {
800
- return urlToPath(
801
- self.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
802
- )
772
+ return urlToPath(self.resolve(specifier, toURL(parentURL, referrer._url), { referrer }))
803
773
  }
804
774
 
805
775
  require.addon = function addon(specifier = '.', parentURL = referrer._url) {
806
- const resolved = Bare.Addon.resolve(
807
- specifier,
808
- toURL(parentURL, referrer._url),
809
- { referrer }
810
- )
776
+ const resolved = Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), { referrer })
811
777
 
812
778
  const addon = Bare.Addon.load(resolved, { referrer })
813
779
 
@@ -816,10 +782,7 @@ exports.createRequire = function createRequire(parentURL, opts = {}) {
816
782
 
817
783
  require.addon.host = Bare.Addon.host
818
784
 
819
- require.addon.resolve = function resolve(
820
- specifier = '.',
821
- parentURL = referrer._url
822
- ) {
785
+ require.addon.resolve = function resolve(specifier = '.', parentURL = referrer._url) {
823
786
  return urlToPath(
824
787
  Bare.Addon.resolve(specifier, toURL(parentURL, referrer._url), {
825
788
  referrer
@@ -828,9 +791,7 @@ exports.createRequire = function createRequire(parentURL, opts = {}) {
828
791
  }
829
792
 
830
793
  require.asset = function asset(specifier, parentURL = referrer._url) {
831
- return urlToPath(
832
- self.asset(specifier, toURL(parentURL, referrer._url), { referrer })
833
- )
794
+ return urlToPath(self.asset(specifier, toURL(parentURL, referrer._url), { referrer }))
834
795
  }
835
796
 
836
797
  return require
@@ -903,12 +864,7 @@ Module._extensions['.mjs'] = function (module, source, referrer) {
903
864
 
904
865
  module._source = source
905
866
 
906
- module._handle = binding.createModule(
907
- module._url.href,
908
- source.toString(),
909
- 0,
910
- self._handle
911
- )
867
+ module._handle = binding.createModule(module._url.href, source.toString(), 0, self._handle)
912
868
  }
913
869
 
914
870
  Module._extensions['.json'] = function (module, source, referrer) {
@@ -974,11 +930,9 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
974
930
  })
975
931
 
976
932
  if (bundle.main) {
977
- module._exports = self.load(
978
- new URL(bundle.main),
979
- bundle.read(bundle.main),
980
- { referrer }
981
- )._exports
933
+ module._exports = self.load(new URL(bundle.main), bundle.read(bundle.main), {
934
+ referrer
935
+ })._exports
982
936
  }
983
937
  }
984
938
 
@@ -1011,9 +965,7 @@ Module._protocol = new Protocol({
1011
965
  postresolve(url) {
1012
966
  switch (url.protocol) {
1013
967
  case 'file:':
1014
- return pathToFileURL(
1015
- binding.realpath(path.toNamespacedPath(fileURLToPath(url)))
1016
- )
968
+ return pathToFileURL(binding.realpath(path.toNamespacedPath(fileURLToPath(url))))
1017
969
  default:
1018
970
  return url
1019
971
  }
@@ -1024,9 +976,7 @@ Module._protocol = new Protocol({
1024
976
  case 'file:':
1025
977
  return binding.exists(
1026
978
  path.toNamespacedPath(fileURLToPath(url)),
1027
- type === constants.types.ASSET
1028
- ? binding.FILE | binding.DIR
1029
- : binding.FILE
979
+ type === constants.types.ASSET ? binding.FILE | binding.DIR : binding.FILE
1030
980
  )
1031
981
  default:
1032
982
  return false
@@ -1036,9 +986,7 @@ Module._protocol = new Protocol({
1036
986
  read(url) {
1037
987
  switch (url.protocol) {
1038
988
  case 'file:':
1039
- return Buffer.from(
1040
- binding.read(path.toNamespacedPath(fileURLToPath(url)))
1041
- )
989
+ return Buffer.from(binding.read(path.toNamespacedPath(fileURLToPath(url))))
1042
990
  default:
1043
991
  throw errors.UNKNOWN_PROTOCOL(`Cannot load module '${url.href}'`)
1044
992
  }
@@ -1060,15 +1008,11 @@ function urlToPath(url) {
1060
1008
 
1061
1009
  if (isWindows) {
1062
1010
  if (/%2f|%5c/i.test(url.pathname)) {
1063
- throw errors.INVALID_URL_PATH(
1064
- 'The URL path must not include encoded \\ or / characters'
1065
- )
1011
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
1066
1012
  }
1067
1013
  } else {
1068
1014
  if (/%2f/i.test(url.pathname)) {
1069
- throw errors.INVALID_URL_PATH(
1070
- 'The URL path must not include encoded / characters'
1071
- )
1015
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
1072
1016
  }
1073
1017
  }
1074
1018
 
@@ -1080,15 +1024,11 @@ function urlToDirname(url) {
1080
1024
 
1081
1025
  if (isWindows) {
1082
1026
  if (/%2f|%5c/i.test(url.pathname)) {
1083
- throw errors.INVALID_URL_PATH(
1084
- 'The URL path must not include encoded \\ or / characters'
1085
- )
1027
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
1086
1028
  }
1087
1029
  } else {
1088
1030
  if (/%2f/i.test(url.pathname)) {
1089
- throw errors.INVALID_URL_PATH(
1090
- 'The URL path must not include encoded / characters'
1091
- )
1031
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
1092
1032
  }
1093
1033
  }
1094
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.1",
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": "*"