bare-module 1.11.0 → 1.11.2

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,43 @@ 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
+ if (req.result < 0) {
473
+ js_throw_error(env, uv_err_name(err), uv_strerror(err));
474
+ return NULL;
475
+ }
476
+
477
+ js_value_t *result;
478
+ err = js_create_string_utf8(env, (utf8_t *) req.ptr, -1, &result);
479
+ assert(err == 0);
480
+
481
+ uv_fs_req_cleanup(&req);
482
+
483
+ return result;
484
+ }
485
+
449
486
  static js_value_t *
450
487
  bare_module_read (js_env_t *env, js_callback_info_t *info) {
451
488
  int err;
@@ -567,6 +604,11 @@ init (js_env_t *env, js_value_t *exports) {
567
604
  js_create_function(env, "exists", -1, bare_module_exists, NULL, &fn);
568
605
  js_set_named_property(env, exports, "exists", fn);
569
606
  }
607
+ {
608
+ js_value_t *fn;
609
+ js_create_function(env, "realpath", -1, bare_module_realpath, NULL, &fn);
610
+ js_set_named_property(env, exports, "realpath", fn);
611
+ }
570
612
  {
571
613
  js_value_t *fn;
572
614
  js_create_function(env, "read", -1, bare_module_read, NULL, &fn);
package/index.js CHANGED
@@ -27,6 +27,16 @@ const Module = module.exports = class Module {
27
27
  return this._imports
28
28
  }
29
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
+
30
40
  static _context = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
31
41
 
32
42
  static _extensions = Object.create(null)
@@ -205,7 +215,7 @@ const Module = module.exports = class Module {
205
215
  throw errors.MODULE_NOT_FOUND(msg)
206
216
  }
207
217
 
208
- return resolved
218
+ return protocol.postresolve(resolved, dirname)
209
219
  }
210
220
 
211
221
  static * _resolve (specifier, dirname, protocol, imports) {
@@ -214,7 +224,7 @@ const Module = module.exports = class Module {
214
224
 
215
225
  protocol = this._protocolFor(specifier, protocol)
216
226
 
217
- specifier = protocol.map(specifier, dirname)
227
+ specifier = protocol.preresolve(specifier, dirname)
218
228
 
219
229
  if (protocol.resolve) {
220
230
  yield * protocol.resolve(specifier, dirname, imports)
@@ -235,15 +245,18 @@ const Module = module.exports = class Module {
235
245
  }
236
246
 
237
247
  static * _resolveFile (filename, protocol) {
238
- const f = filename
239
-
240
- if (protocol.exists(f)) yield f
241
- if (protocol.exists(f + '.js')) yield f + '.js'
242
- if (protocol.exists(f + '.cjs')) yield f + '.cjs'
243
- if (protocol.exists(f + '.mjs')) yield f + '.mjs'
244
- if (protocol.exists(f + '.json')) yield f + '.json'
245
- if (protocol.exists(f + '.bare')) yield f + '.bare'
246
- 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
+ }
247
260
  }
248
261
 
249
262
  static * _resolveIndex (dirname, protocol) {
@@ -508,10 +521,14 @@ Module._extensions['.bundle'] = function (module, source, referrer, protocol, im
508
521
  }
509
522
 
510
523
  Module._protocols['file:'] = new Protocol({
511
- map (specifier) {
524
+ preresolve (specifier) {
512
525
  return specifier.replace(/^file:/, '')
513
526
  },
514
527
 
528
+ postresolve (specifier) {
529
+ return binding.realpath(specifier)
530
+ },
531
+
515
532
  exists (filename) {
516
533
  return binding.exists(filename)
517
534
  },
@@ -522,7 +539,7 @@ Module._protocols['file:'] = new Protocol({
522
539
  })
523
540
 
524
541
  Module._protocols['node:'] = new Protocol({
525
- map (specifier) {
542
+ preresolve (specifier) {
526
543
  return specifier.replace(/^node:/, '')
527
544
  }
528
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.11.0",
3
+ "version": "1.11.2",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [