bare-module 4.7.1 → 4.8.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/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.d.ts ADDED
@@ -0,0 +1,127 @@
1
+ import Buffer from 'bare-buffer'
2
+ import URL from 'bare-url'
3
+ import Bundle from 'bare-bundle'
4
+ import {
5
+ type Builtins,
6
+ type Conditions,
7
+ type ImportsMap,
8
+ type ResolutionsMap
9
+ } from 'bare-module-resolve'
10
+ import Protocol from './lib/protocol'
11
+ import constants from './lib/constants'
12
+
13
+ interface Cache {
14
+ [href: string]: Module
15
+ }
16
+
17
+ interface Attributes {
18
+ type: Lowercase<keyof typeof constants.types>
19
+ }
20
+
21
+ interface Options {
22
+ attributes?: Attributes
23
+ builtins?: Builtins
24
+ cache?: Cache
25
+ conditions?: Conditions
26
+ defaultType?: number
27
+ imports?: ImportsMap
28
+ main?: Module
29
+ protocol?: Protocol
30
+ referrer?: Module
31
+ resolutions?: ResolutionsMap
32
+ type?: number
33
+ }
34
+
35
+ interface LoadOptions extends Options {
36
+ isDynamicImport?: boolean
37
+ isImport?: boolean
38
+ }
39
+
40
+ interface ResolveOptions extends Options {
41
+ isImport?: boolean
42
+ }
43
+
44
+ interface Module {
45
+ readonly builtins: Builtins
46
+ readonly cache: Cache
47
+ readonly conditions: Conditions
48
+ readonly defaultType: number
49
+ readonly dirname: string
50
+ exports: unknown
51
+ readonly filename: string
52
+ readonly id: string
53
+ readonly imports: ImportsMap
54
+ readonly main: Module
55
+ readonly path: string
56
+ readonly protocol: Protocol
57
+ readonly resolutions: ResolutionsMap
58
+ readonly type: number
59
+ readonly url: URL
60
+
61
+ destroy(): void
62
+ }
63
+
64
+ declare class Module {
65
+ static readonly protocol: Protocol
66
+ static readonly cache: Cache
67
+
68
+ static load(url: URL, opts: LoadOptions): Module
69
+ static load(
70
+ url: URL,
71
+ source?: Buffer | string | Bundle | null,
72
+ opts?: LoadOptions
73
+ ): Module
74
+
75
+ static resolve(specifier: string, parentURL: URL, opts?: ResolveOptions): URL
76
+
77
+ static asset(specifier: string, parentURL: URL, opts?: Options): URL
78
+
79
+ constructor(url: URL)
80
+ }
81
+
82
+ declare namespace Module {
83
+ export {
84
+ type Attributes,
85
+ type Cache,
86
+ type Options,
87
+ type LoadOptions,
88
+ type ResolveOptions,
89
+ Protocol,
90
+ Bundle,
91
+ constants
92
+ }
93
+
94
+ export const builtinModules: Module[]
95
+
96
+ export function isBuiltin(): boolean
97
+
98
+ export interface CreateRequireOptions extends Options {
99
+ module?: Module
100
+ }
101
+
102
+ export interface RequireOptions {
103
+ with?: Attributes
104
+ }
105
+
106
+ export interface RequireAddon {
107
+ (specifier?: string, parentURL?: URL): string
108
+ host: string
109
+ resolve: (specifier: string, parentURL?: URL) => unknown
110
+ }
111
+
112
+ export interface Require {
113
+ (parentURL: string | URL, opts?: RequireOptions): unknown
114
+ main: Module
115
+ cache: Cache
116
+ resolve: (specifier: string, parentURL?: URL) => string
117
+ addon: RequireAddon
118
+ asset: (specifier: string, parentURL?: URL) => string
119
+ }
120
+
121
+ export function createRequire(
122
+ parentURL: string | URL,
123
+ opts?: CreateRequireOptions
124
+ ): Require
125
+ }
126
+
127
+ export = Module
package/index.js CHANGED
@@ -390,6 +390,7 @@ module.exports = exports = class Module {
390
390
 
391
391
  if (
392
392
  !ArrayBuffer.isView(source) &&
393
+ !Bundle.isBundle(source) &&
393
394
  typeof source !== 'string' &&
394
395
  source !== null
395
396
  ) {
@@ -448,9 +449,11 @@ module.exports = exports = class Module {
448
449
  canonicalExtensionForType(type) || path.extname(url.pathname)
449
450
 
450
451
  if (extension in self._extensions === false) {
451
- if (defaultType)
452
+ if (defaultType) {
452
453
  extension = canonicalExtensionForType(defaultType) || '.js'
453
- else extension = '.js'
454
+ } else {
455
+ extension = '.js'
456
+ }
454
457
  }
455
458
 
456
459
  self._extensions[extension](module, source, referrer)
@@ -513,7 +516,7 @@ module.exports = exports = class Module {
513
516
  case 'builtin:':
514
517
  return resolution
515
518
  default:
516
- if (protocol.exists(resolution)) {
519
+ if (protocol.exists(resolution, type)) {
517
520
  return protocol.postresolve(resolution)
518
521
  }
519
522
  }
@@ -524,7 +527,7 @@ module.exports = exports = class Module {
524
527
  )
525
528
 
526
529
  function readPackage(packageURL) {
527
- if (protocol.exists(packageURL)) {
530
+ if (protocol.exists(packageURL, constants.types.JSON)) {
528
531
  return Module.load(packageURL, { protocol })._exports
529
532
  }
530
533
 
@@ -566,7 +569,7 @@ module.exports = exports = class Module {
566
569
  },
567
570
  readPackage
568
571
  )) {
569
- if (protocol.exists(resolution)) {
572
+ if (protocol.exists(resolution, constants.types.ASSET)) {
570
573
  return protocol.postresolve(
571
574
  protocol.asset ? protocol.asset(resolution) : resolution
572
575
  )
@@ -578,7 +581,7 @@ module.exports = exports = class Module {
578
581
  )
579
582
 
580
583
  function readPackage(packageURL) {
581
- if (protocol.exists(packageURL)) {
584
+ if (protocol.exists(packageURL, constants.types.JSON)) {
582
585
  return Module.load(packageURL, { protocol })._exports
583
586
  }
584
587
 
@@ -799,7 +802,7 @@ Module._extensions['.js'] = function (module, source, referrer) {
799
802
  break
800
803
  }
801
804
 
802
- if (protocol.exists(packageURL)) {
805
+ if (protocol.exists(packageURL, constants.types.JSON)) {
803
806
  pkg = self.load(packageURL, { protocol })
804
807
  break
805
808
  }
@@ -905,10 +908,9 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
905
908
 
906
909
  referrer = module
907
910
 
908
- const bundle = (module._bundle = Bundle.from(source).mount(
909
- module._url.href + '/'
910
- ))
911
+ const bundle = Bundle.from(source).mount(module._url.href + '/')
911
912
 
913
+ module._bundle = bundle
912
914
  module._imports = bundle.imports
913
915
  module._resolutions = bundle.resolutions
914
916
 
@@ -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
  }
@@ -0,0 +1,19 @@
1
+ declare const constants: {
2
+ states: {
3
+ EVALUATED: number
4
+ SYNTHESIZED: number
5
+ DESTROYED: number
6
+ }
7
+ types: {
8
+ SCRIPT: number
9
+ MODULE: number
10
+ JSON: number
11
+ BUNDLE: number
12
+ ADDON: number
13
+ BINARY: number
14
+ TEXT: number
15
+ ASSET: number
16
+ }
17
+ }
18
+
19
+ export = constants
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
  }
@@ -0,0 +1,27 @@
1
+ import URL from 'bare-url'
2
+ import Buffer from 'bare-buffer'
3
+ import { type ImportsMap } from 'bare-module-resolve'
4
+
5
+ interface ModuleProtocol {
6
+ preresolve(specifier: string, parentURL: URL): string
7
+
8
+ postresolve(url: URL): URL
9
+
10
+ resolve(specifier: string, parentURL: URL, imports: ImportsMap): URL
11
+
12
+ exists(url: URL, type: number): boolean
13
+
14
+ read(url: URL): Buffer | string | null
15
+
16
+ addon(url: URL): URL
17
+
18
+ asset(url: URL): URL
19
+
20
+ extend(methods: Partial<ModuleProtocol>): ModuleProtocol
21
+ }
22
+
23
+ declare class ModuleProtocol {
24
+ constructor(methods?: Partial<ModuleProtocol>, context?: ModuleProtocol)
25
+ }
26
+
27
+ export = ModuleProtocol
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,10 +1,17 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "4.7.1",
3
+ "version": "4.8.1",
4
4
  "description": "Module support for JavaScript",
5
- "main": "index.js",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./index.d.ts",
8
+ "default": "./index.js"
9
+ },
10
+ "./package": "./package.json"
11
+ },
6
12
  "files": [
7
13
  "index.js",
14
+ "index.d.ts",
8
15
  "binding.c",
9
16
  "binding.js",
10
17
  "CMakeLists.txt",
@@ -33,9 +40,18 @@
33
40
  "bare-url": "^2.0.1"
34
41
  },
35
42
  "devDependencies": {
43
+ "bare-buffer": "^3.0.2",
36
44
  "brittle": "^3.1.1",
37
45
  "cmake-bare": "^1.1.6",
38
46
  "prettier": "^3.3.3",
39
47
  "prettier-config-standard": "^7.0.0"
48
+ },
49
+ "peerDependencies": {
50
+ "bare-buffer": "*"
51
+ },
52
+ "peerDependenciesMeta": {
53
+ "bare-buffer": {
54
+ "optional": true
55
+ }
40
56
  }
41
57
  }