bare-module 1.10.3 → 1.11.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/binding.c CHANGED
@@ -446,6 +446,38 @@ bare_module_exists (js_env_t *env, js_callback_info_t *info) {
446
446
  return result;
447
447
  }
448
448
 
449
+ static js_value_t *
450
+ bare_module_realpath (js_env_t *env, js_callback_info_t *info) {
451
+ int err;
452
+
453
+ uv_loop_t *loop;
454
+ err = js_get_env_loop(env, &loop);
455
+ assert(err == 0);
456
+
457
+ js_value_t *argv[1];
458
+ size_t argc = 1;
459
+
460
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
461
+ assert(err == 0);
462
+
463
+ assert(argc == 1);
464
+
465
+ utf8_t path[4096];
466
+ err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
467
+ assert(err == 0);
468
+
469
+ uv_fs_t req;
470
+ uv_fs_realpath(loop, &req, (char *) path, NULL);
471
+
472
+ js_value_t *result;
473
+ err = js_create_string_utf8(env, (utf8_t *) req.ptr, -1, &result);
474
+ assert(err == 0);
475
+
476
+ uv_fs_req_cleanup(&req);
477
+
478
+ return result;
479
+ }
480
+
449
481
  static js_value_t *
450
482
  bare_module_read (js_env_t *env, js_callback_info_t *info) {
451
483
  int err;
@@ -567,6 +599,11 @@ init (js_env_t *env, js_value_t *exports) {
567
599
  js_create_function(env, "exists", -1, bare_module_exists, NULL, &fn);
568
600
  js_set_named_property(env, exports, "exists", fn);
569
601
  }
602
+ {
603
+ js_value_t *fn;
604
+ js_create_function(env, "realpath", -1, bare_module_realpath, NULL, &fn);
605
+ js_set_named_property(env, exports, "realpath", fn);
606
+ }
570
607
  {
571
608
  js_value_t *fn;
572
609
  js_create_function(env, "read", -1, bare_module_read, NULL, &fn);
package/index.js CHANGED
@@ -23,6 +23,20 @@ const Module = module.exports = class Module {
23
23
  return path.dirname(this.filename)
24
24
  }
25
25
 
26
+ get importMap () {
27
+ return this._imports
28
+ }
29
+
30
+ // For Node.js compatibility
31
+ get id () {
32
+ return this.filename
33
+ }
34
+
35
+ // For Node.s compatibility
36
+ get path () {
37
+ return this.dirname
38
+ }
39
+
26
40
  static _context = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
27
41
 
28
42
  static _extensions = Object.create(null)
@@ -201,7 +215,7 @@ const Module = module.exports = class Module {
201
215
  throw errors.MODULE_NOT_FOUND(msg)
202
216
  }
203
217
 
204
- return resolved
218
+ return protocol.postresolve(resolved, dirname)
205
219
  }
206
220
 
207
221
  static * _resolve (specifier, dirname, protocol, imports) {
@@ -210,7 +224,7 @@ const Module = module.exports = class Module {
210
224
 
211
225
  protocol = this._protocolFor(specifier, protocol)
212
226
 
213
- specifier = protocol.map(specifier, dirname)
227
+ specifier = protocol.preresolve(specifier, dirname)
214
228
 
215
229
  if (protocol.resolve) {
216
230
  yield * protocol.resolve(specifier, dirname, imports)
@@ -231,15 +245,18 @@ const Module = module.exports = class Module {
231
245
  }
232
246
 
233
247
  static * _resolveFile (filename, protocol) {
234
- const f = filename
235
-
236
- if (protocol.exists(f)) yield f
237
- if (protocol.exists(f + '.js')) yield f + '.js'
238
- if (protocol.exists(f + '.cjs')) yield f + '.cjs'
239
- if (protocol.exists(f + '.mjs')) yield f + '.mjs'
240
- if (protocol.exists(f + '.json')) yield f + '.json'
241
- if (protocol.exists(f + '.bare')) yield f + '.bare'
242
- if (protocol.exists(f + '.node')) yield f + '.node'
248
+ const extensions = [
249
+ '.js',
250
+ '.cjs',
251
+ '.mjs',
252
+ '.json',
253
+ '.bare',
254
+ '.node'
255
+ ]
256
+
257
+ for (const candidate of [filename, ...extensions.map(ext => filename + ext)]) {
258
+ if (protocol.exists(candidate)) yield candidate
259
+ }
243
260
  }
244
261
 
245
262
  static * _resolveIndex (dirname, protocol) {
@@ -504,10 +521,14 @@ Module._extensions['.bundle'] = function (module, source, referrer, protocol, im
504
521
  }
505
522
 
506
523
  Module._protocols['file:'] = new Protocol({
507
- map (specifier) {
524
+ preresolve (specifier) {
508
525
  return specifier.replace(/^file:/, '')
509
526
  },
510
527
 
528
+ postresolve (specifier) {
529
+ return binding.realpath(specifier)
530
+ },
531
+
511
532
  exists (filename) {
512
533
  return binding.exists(filename)
513
534
  },
@@ -518,7 +539,7 @@ Module._protocols['file:'] = new Protocol({
518
539
  })
519
540
 
520
541
  Module._protocols['node:'] = new Protocol({
521
- map (specifier) {
542
+ preresolve (specifier) {
522
543
  return specifier.replace(/^node:/, '')
523
544
  }
524
545
  })
package/lib/protocol.js CHANGED
@@ -2,7 +2,8 @@ module.exports = class Protocol {
2
2
  constructor (opts = {}) {
3
3
  const {
4
4
  imports = Object.create(null),
5
- map = null,
5
+ preresolve = null,
6
+ postresolve = null,
6
7
  resolve = null,
7
8
  exists = null,
8
9
  read = null
@@ -10,14 +11,20 @@ module.exports = class Protocol {
10
11
 
11
12
  this.imports = imports
12
13
 
13
- if (map) this.map = map.bind(this)
14
+ if (preresolve) this.preresolve = preresolve.bind(this)
15
+ if (postresolve) this.postresolve = postresolve.bind(this)
16
+
14
17
  if (resolve) this.resolve = resolve.bind(this)
15
18
  else this.resolve = null
16
19
  if (exists) this.exists = exists.bind(this)
17
20
  if (read) this.read = read.bind(this)
18
21
  }
19
22
 
20
- map (specifier, dirname) {
23
+ preresolve (specifier, dirname) {
24
+ return specifier
25
+ }
26
+
27
+ postresolve (specifier, dirname) {
21
28
  return specifier
22
29
  }
23
30
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.10.3",
3
+ "version": "1.11.1",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [