bare-module 4.7.1 → 4.8.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/README.md CHANGED
@@ -458,6 +458,112 @@ The `ModuleProtocol` class used for resolving, reading and loading modules. See
458
458
 
459
459
  Unloads the module.
460
460
 
461
+ ### CommonJS modules
462
+
463
+ #### `require(specifier[, options])`
464
+
465
+ Used to import JavaScript or JSON modules and local files. Relative paths such as `./`, `./foo`, `./bar/baz`, and `../foo` will be resolved against the directory named by `__dirname`. POSIX style paths are resolved in an OS independent fashion, meaning that the examples above will work on Windows in the same way they would on POSIX systems.
466
+
467
+ Returns the exported module contents.
468
+
469
+ Options include:
470
+
471
+ ```js
472
+ options = {
473
+ // The import attributes which instruct how the file or module should be loaded.
474
+ // Possible values for `type` are `script`, `module`, `json`, `bundle`,
475
+ // `addon`, `binary` and `text`.
476
+ with: { type: 'json' }
477
+ }
478
+ ```
479
+
480
+ #### `require.main`
481
+
482
+ The module representing the entry script where the program was launched. The same value as [`module.main`](#modulemain) for the current module.
483
+
484
+ #### `require.cache`
485
+
486
+ A cache of loaded modules for this module. The same value as `module.cache` for the current module.
487
+
488
+ #### `const path = require.resolve(specifier[, parentURL])`
489
+
490
+ Use the internal machinery of `require()` to resolve the `specifier` string relative to the URL `parentURL` and return the path string.
491
+
492
+ #### `require.addon([specifier][, parentURL])`
493
+
494
+ Also used to import modules but specifically loads only addon modules. `specifier` is resolved relative to `parentURL` using the [addon resolution](https://github.com/holepunchto/bare-addon-resolve#algorithm) algorithm.
495
+
496
+ Returns the exported module contents.
497
+
498
+ A common pattern for writing an addon module is to use `require.addon()` as the JavaScript module exports:
499
+
500
+ ```js
501
+ module.exports = require.addon()
502
+ ```
503
+
504
+ See [`bare-addon`](https://github.com/holepunchto/bare-addon) for a template of building native addon modules.
505
+
506
+ #### `require.addon.host`
507
+
508
+ Returns the string representation of the platform and architecture used when resolving addons with the pattern `<platform>-<arch>[-simulator]`. Returns the same value as `Bare.Addon.host`.
509
+
510
+ #### `const path = require.addon.resolve([specifier][, parentURL])`
511
+
512
+ Resolve the `specifier` string relative to the URL `parentURL` as an addon and returns the path string. The `specifier` is resolved using the [addon resolution algorithm](https://github.com/holepunchto/bare-addon-resolve#algorithm).
513
+
514
+ #### `const path = require.asset(specifier[, parentURL])`
515
+
516
+ Resolve the `specifier` relative to the `parentURL` and return the path of the asset as a string.
517
+
518
+ Can be used to load assets, for example the following loads `./foo.txt` from the local files:
519
+
520
+ ```js
521
+ const fs = require('bare-fs')
522
+ const contents = fs.readFileSync(require.asset('./foo.txt'))
523
+ ```
524
+
525
+ ### ECMAScript modules
526
+
527
+ #### `import defaultExport, * as name, { export1, export2 as alias2, ... } from 'specifier' with { type: 'json' }`
528
+
529
+ The static `import` declaration is used to import read-only live bindings that are exported by another module. The imported bindings are called _live_ bindings because they are updated by the module that exported the binding, but cannot be re-assigned by the importing module. In brief, you can import what is exported from another module.
530
+
531
+ For more information on `import` syntax, see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import).
532
+
533
+ #### `import.meta.url`
534
+
535
+ The string representation of the URL for the current module.
536
+
537
+ #### `import.meta.main`
538
+
539
+ A boolean representing whether the current module is the entry script where the program was launched.
540
+
541
+ #### `import.meta.cache`
542
+
543
+ A cache of loaded modules for this module. The same value as `module.cache` for the current module.
544
+
545
+ #### `const href = import.meta.resolve(specifier[, parentURL])`
546
+
547
+ A module-relative resolution function which returns the URL string for the module. The `specifier` is a string which is resolved relative to the `parentURL` which is a WHATWG URL.
548
+
549
+ #### `import.meta.addon([specifier][, parentURL])`
550
+
551
+ Also used to import modules but specifically loads only addon modules. `specifier` is resolved relative to `parentURL` using the [addon resolution](https://github.com/holepunchto/bare-addon-resolve#algorithm) algorithm.
552
+
553
+ Returns the exported module contents.
554
+
555
+ #### `import.meta.addon.host`
556
+
557
+ Returns the string representation of the platform and architecture used when resolving addons with the pattern `<platform>-<arch>[-simulator]`. Returns the same value as `Bare.Addon.host`.
558
+
559
+ #### `const href = import.meta.addon.resolve([specifier][, parentURL])`
560
+
561
+ Resolve the `specifier` string relative to the URL `parentURL` as an addon and returns the URL string. The `specifier` is resolved using the [addon resolution algorithm](https://github.com/holepunchto/bare-addon-resolve#algorithm).
562
+
563
+ #### `const href = import.meta.asset(specifier[, parentURL])`
564
+
565
+ Resolve the `specifier` relative to the `parentURL` and return the URL of the asset as a string.
566
+
461
567
  ### Custom `require()`
462
568
 
463
569
  Creating a custom require allows one to create a preconfigured `require()`. This can be useful in scenarios such as a Read-Evaluate-Print-Loop (REPL) where the parent URL is set to a directory so requiring relative paths to work correctly.
package/binding.c CHANGED
@@ -529,25 +529,29 @@ bare_module_exists(js_env_t *env, js_callback_info_t *info) {
529
529
  err = js_get_env_loop(env, &loop);
530
530
  assert(err == 0);
531
531
 
532
- js_value_t *argv[1];
533
- size_t argc = 1;
532
+ js_value_t *argv[2];
533
+ size_t argc = 2;
534
534
 
535
535
  err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
536
536
  assert(err == 0);
537
537
 
538
- assert(argc == 1);
538
+ assert(argc == 2);
539
539
 
540
540
  utf8_t path[4096];
541
541
  err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
542
542
  assert(err == 0);
543
543
 
544
+ uint32_t mode;
545
+ err = js_get_value_uint32(env, argv[1], &mode);
546
+ assert(err == 0);
547
+
544
548
  uv_fs_t req;
545
549
  uv_fs_stat(loop, &req, (char *) path, NULL);
546
550
 
547
551
  uv_stat_t *st = req.result < 0 ? NULL : req.ptr;
548
552
 
549
553
  js_value_t *result;
550
- err = js_get_boolean(env, st && st->st_mode & S_IFREG, &result);
554
+ err = js_get_boolean(env, st && st->st_mode & mode, &result);
551
555
  assert(err == 0);
552
556
 
553
557
  uv_fs_req_cleanup(&req);
@@ -707,6 +711,19 @@ bare_module_exports(js_env_t *env, js_value_t *exports) {
707
711
  V("read", bare_module_read)
708
712
  #undef V
709
713
 
714
+ #define V(name, n) \
715
+ { \
716
+ js_value_t *val; \
717
+ err = js_create_uint32(env, n, &val); \
718
+ assert(err == 0); \
719
+ err = js_set_named_property(env, exports, name, val); \
720
+ assert(err == 0); \
721
+ }
722
+
723
+ V("FILE", S_IFREG)
724
+ V("DIR", S_IFDIR)
725
+ #undef V
726
+
710
727
  return exports;
711
728
  }
712
729
 
package/index.js CHANGED
@@ -448,9 +448,11 @@ module.exports = exports = class Module {
448
448
  canonicalExtensionForType(type) || path.extname(url.pathname)
449
449
 
450
450
  if (extension in self._extensions === false) {
451
- if (defaultType)
451
+ if (defaultType) {
452
452
  extension = canonicalExtensionForType(defaultType) || '.js'
453
- else extension = '.js'
453
+ } else {
454
+ extension = '.js'
455
+ }
454
456
  }
455
457
 
456
458
  self._extensions[extension](module, source, referrer)
@@ -513,7 +515,7 @@ module.exports = exports = class Module {
513
515
  case 'builtin:':
514
516
  return resolution
515
517
  default:
516
- if (protocol.exists(resolution)) {
518
+ if (protocol.exists(resolution, type)) {
517
519
  return protocol.postresolve(resolution)
518
520
  }
519
521
  }
@@ -524,7 +526,7 @@ module.exports = exports = class Module {
524
526
  )
525
527
 
526
528
  function readPackage(packageURL) {
527
- if (protocol.exists(packageURL)) {
529
+ if (protocol.exists(packageURL, constants.types.JSON)) {
528
530
  return Module.load(packageURL, { protocol })._exports
529
531
  }
530
532
 
@@ -566,7 +568,7 @@ module.exports = exports = class Module {
566
568
  },
567
569
  readPackage
568
570
  )) {
569
- if (protocol.exists(resolution)) {
571
+ if (protocol.exists(resolution, constants.types.ASSET)) {
570
572
  return protocol.postresolve(
571
573
  protocol.asset ? protocol.asset(resolution) : resolution
572
574
  )
@@ -578,7 +580,7 @@ module.exports = exports = class Module {
578
580
  )
579
581
 
580
582
  function readPackage(packageURL) {
581
- if (protocol.exists(packageURL)) {
583
+ if (protocol.exists(packageURL, constants.types.JSON)) {
582
584
  return Module.load(packageURL, { protocol })._exports
583
585
  }
584
586
 
@@ -799,7 +801,7 @@ Module._extensions['.js'] = function (module, source, referrer) {
799
801
  break
800
802
  }
801
803
 
802
- if (protocol.exists(packageURL)) {
804
+ if (protocol.exists(packageURL, constants.types.JSON)) {
803
805
  pkg = self.load(packageURL, { protocol })
804
806
  break
805
807
  }
@@ -969,10 +971,15 @@ Module._protocol = new Protocol({
969
971
  }
970
972
  },
971
973
 
972
- exists(url) {
974
+ exists(url, type = 0) {
973
975
  switch (url.protocol) {
974
976
  case 'file:':
975
- return binding.exists(fileURLToPath(url))
977
+ return binding.exists(
978
+ fileURLToPath(url),
979
+ type === constants.types.ASSET
980
+ ? binding.FILE | binding.DIR
981
+ : binding.FILE
982
+ )
976
983
  default:
977
984
  return false
978
985
  }
package/lib/constants.js CHANGED
@@ -12,6 +12,7 @@ module.exports = {
12
12
  BUNDLE: 4,
13
13
  ADDON: 5,
14
14
  BINARY: 6,
15
- TEXT: 7
15
+ TEXT: 7,
16
+ ASSET: 8
16
17
  }
17
18
  }
package/lib/protocol.js CHANGED
@@ -34,7 +34,7 @@ module.exports = class ModuleProtocol {
34
34
 
35
35
  *resolve(specifier, parentURL, imports) {}
36
36
 
37
- exists(url) {
37
+ exists(url, type) {
38
38
  return false
39
39
  }
40
40
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "4.7.1",
3
+ "version": "4.8.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [