bare-module 6.4.0 → 7.0.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.
Files changed (38) hide show
  1. package/README.md +237 -270
  2. package/binding.c +623 -333
  3. package/index.d.ts +156 -63
  4. package/index.js +87 -1002
  5. package/lib/errors.js +27 -10
  6. package/lib/loader.d.ts +56 -0
  7. package/lib/loader.js +1192 -0
  8. package/lib/module/addon.js +7 -0
  9. package/lib/module/binary.js +7 -0
  10. package/lib/module/builtin.js +19 -0
  11. package/lib/module/bundle.js +47 -0
  12. package/lib/module/json.js +19 -0
  13. package/lib/module/script.js +23 -0
  14. package/lib/module/source-text.js +13 -0
  15. package/lib/module/synthetic.js +20 -0
  16. package/lib/module/text.js +7 -0
  17. package/lib/module/value.js +27 -0
  18. package/lib/module.js +392 -0
  19. package/lib/protocol.d.ts +56 -8
  20. package/lib/protocol.js +93 -26
  21. package/lib/source.js +18 -0
  22. package/lib/url.js +52 -0
  23. package/package.json +18 -8
  24. package/prebuilds/android-arm/bare-module.bare +0 -0
  25. package/prebuilds/android-arm64/bare-module.bare +0 -0
  26. package/prebuilds/android-ia32/bare-module.bare +0 -0
  27. package/prebuilds/android-x64/bare-module.bare +0 -0
  28. package/prebuilds/darwin-arm64/bare-module.bare +0 -0
  29. package/prebuilds/darwin-x64/bare-module.bare +0 -0
  30. package/prebuilds/ios-arm64/bare-module.bare +0 -0
  31. package/prebuilds/ios-arm64-simulator/bare-module.bare +0 -0
  32. package/prebuilds/ios-x64-simulator/bare-module.bare +0 -0
  33. package/prebuilds/linux-arm64/bare-module.bare +0 -0
  34. package/prebuilds/linux-x64/bare-module.bare +0 -0
  35. package/prebuilds/win32-arm64/bare-module.bare +0 -0
  36. package/prebuilds/win32-x64/bare-module.bare +0 -0
  37. package/lib/constants.d.ts +0 -19
  38. package/lib/constants.js +0 -17
package/index.d.ts CHANGED
@@ -1,116 +1,209 @@
1
1
  import Buffer from 'bare-buffer'
2
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'
3
+ import { constants } from 'bare-module-traverse'
10
4
  import Protocol from './lib/protocol'
11
- import constants from './lib/constants'
5
+ import Loader from './lib/loader'
12
6
 
7
+ /** A map of module URL `href`s to loaded modules. */
13
8
  interface Cache {
14
9
  [href: string]: Module
15
10
  }
16
11
 
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
- }
12
+ /** The import condition a specifier is resolved for. */
13
+ type Condition = 'require' | 'import' | 'asset' | 'addon'
34
14
 
35
- interface LoadOptions extends Options {
36
- isDynamicImport?: boolean
37
- isImport?: boolean
38
- }
39
-
40
- interface ResolveOptions extends Options {
41
- isImport?: boolean
15
+ /**
16
+ * Import attributes instructing how a module should be loaded. A `type` that is not one of the
17
+ * known strings is turned down rather than passed over.
18
+ */
19
+ interface Attributes {
20
+ /**
21
+ * How the module should be loaded: one of `'script'`, `'module'`, `'json'`, `'bundle'`,
22
+ * `'addon'`, `'binary'`, or `'text'`.
23
+ */
24
+ type: Lowercase<keyof typeof constants>
42
25
  }
43
26
 
44
27
  interface Module {
45
- readonly builtins: Builtins
46
- readonly cache: Cache
47
- readonly conditions: Conditions
48
- readonly defaultType: number
28
+ /** The directory portion of `module.url`. */
49
29
  readonly dirname: string
30
+ /** The exports from the module. */
50
31
  exports: unknown
32
+ /** The file portion of `module.url`. */
51
33
  readonly filename: string
52
34
  readonly id: string
53
- readonly imports: ImportsMap
54
- readonly main: Module
55
35
  readonly path: string
36
+ /**
37
+ * The `ModuleProtocol` class for resolving, reading and loading modules. See
38
+ * [Protocols](https://github.com/holepunchto/bare-module#protocols) for usage.
39
+ */
56
40
  readonly protocol: Protocol
57
- readonly resolutions: ResolutionsMap
58
- readonly type: number
41
+ /** The WHATWG `URL` identifier of the module. */
59
42
  readonly url: URL
60
43
  }
61
44
 
62
45
  declare class Module {
63
- static readonly protocol: Protocol
64
- static readonly cache: Cache
65
-
66
- static load(url: URL, opts: LoadOptions): Module
67
- static load(url: URL, source?: Buffer | string | Bundle | null, opts?: LoadOptions): Module
68
-
69
- static resolve(specifier: string, parentURL: URL, opts?: ResolveOptions): URL
70
-
71
- static asset(specifier: string, parentURL: URL, opts?: Options): URL
72
-
73
46
  constructor(url: URL)
74
47
  }
75
48
 
76
49
  declare namespace Module {
77
- export {
78
- type Attributes,
79
- type Cache,
80
- type Options,
81
- type LoadOptions,
82
- type ResolveOptions,
83
- Protocol,
84
- constants
85
- }
50
+ export { type Attributes, type Cache, type Condition, Loader, Protocol, constants }
86
51
 
87
- export const builtinModules: Module[]
52
+ export interface LoadOptions extends Loader.Options, Loader.LinkOptions {
53
+ /** The referring module. */
54
+ referrer?: Module
55
+ }
88
56
 
89
- export function isBuiltin(): boolean
57
+ export interface ResolveOptions extends Loader.Options {
58
+ referrer?: Module
59
+ }
90
60
 
91
- export interface CreateRequireOptions extends Options {
92
- module?: Module
61
+ export interface CreateRequireOptions extends Loader.Options {
62
+ referrer?: Module
93
63
  }
94
64
 
65
+ /** Options for `require()`; `with` holds the import attributes. */
95
66
  export interface RequireOptions {
96
67
  with?: Attributes
97
68
  }
98
69
 
70
+ /** The `require.addon` function: imports addon modules, with `host` and `resolve` attached. */
99
71
  export interface RequireAddon {
100
72
  (specifier?: string, parentURL?: URL): string
73
+ /**
74
+ * The platform and architecture used when resolving addons, following the pattern
75
+ * `<platform>-<arch>[-<environment>]`.
76
+ */
101
77
  host: string
78
+ /**
79
+ * @returns The WHATWG `URL` that `specifier` resolves to.
80
+ * @throws {MODULE_NOT_FOUND} no module matching `specifier` could be found relative to
81
+ * `parentURL`.
82
+ * @throws {TypeError} `specifier` is not a string.
83
+ */
102
84
  resolve: (specifier: string, parentURL?: URL) => unknown
103
85
  }
104
86
 
87
+ /**
88
+ * The function returned by `Module.createRequire()`: resolves and loads modules relative to its
89
+ * parent URL, with `main`, `cache`, `resolve`, `addon`, and `asset` attached.
90
+ */
105
91
  export interface Require {
106
92
  (parentURL: string | URL, opts?: RequireOptions): unknown
107
93
  main: Module
108
94
  cache: Cache
109
95
  resolve: (specifier: string, parentURL?: URL) => string
96
+ /**
97
+ * Resolve `specifier` relative to `parentURL` and evaluate the matching addon.
98
+ * @param specifier - The addon specifier to resolve. Defaults to `'.'`.
99
+ * @param parentURL - The WHATWG `URL` to resolve `specifier` relative to. Defaults to the URL
100
+ * of the module that `require()` was created for.
101
+ * @returns The exports of the addon.
102
+ * @throws {MODULE_NOT_FOUND} no addon matching `specifier` could be found relative to
103
+ * `parentURL`.
104
+ */
110
105
  addon: RequireAddon
106
+ /**
107
+ * @returns The WHATWG `URL` of the resolved asset.
108
+ * @throws {ASSET_NOT_FOUND} no asset matching `specifier` could be found relative to
109
+ * `parentURL`.
110
+ * @throws {TypeError} `specifier` is not a string.
111
+ */
111
112
  asset: (specifier: string, parentURL?: URL) => string
112
113
  }
113
114
 
115
+ /**
116
+ * Load a module with the provided `url`. `url` is a WHATWG `URL`. If provided, the `source` will
117
+ * be passed to the matching `extension` for the `url`.
118
+ * @param url - The WHATWG `URL` of the module to load.
119
+ * @param opts - Load options; may carry a `source` to load directly instead of reading it through
120
+ * the protocol.
121
+ * @returns The loaded `Module`, reusing the cached instance if `url` was already loaded.
122
+ * @throws {TYPE_INCOMPATIBLE} a module is already cached for `url` with a type incompatible with
123
+ * the requested `type`.
124
+ */
125
+ export function load(url: URL, opts: LoadOptions): Promise<Module>
126
+ export function load(
127
+ url: URL,
128
+ source?: Buffer | string | null,
129
+ opts?: LoadOptions
130
+ ): Promise<Module>
131
+
132
+ /**
133
+ * As `load()`, but synchronous, for a caller that cannot await one. The protocol is driven
134
+ * through its `*Sync` methods and so must answer synchronously. A module with a top-level
135
+ * `await` is returned as soon as its evaluation is started rather than once it has finished, as
136
+ * there is nothing to wait on it here.
137
+ * @param url - The WHATWG `URL` of the module to load.
138
+ * @param opts - Load options; may carry a `source` to load directly instead of reading it through
139
+ * the protocol.
140
+ * @returns The loaded `Module`, reusing the cached instance if `url` was already loaded.
141
+ * @throws {UNEXPECTED_PROMISE} the protocol answered asynchronously.
142
+ * @throws {TYPE_INCOMPATIBLE} a module is already cached for `url` with a type incompatible with
143
+ * the requested `type`.
144
+ */
145
+ export function loadSync(url: URL, opts: LoadOptions): Module
146
+ export function loadSync(url: URL, source?: Buffer | string | null, opts?: LoadOptions): Module
147
+
148
+ /**
149
+ * Resolve the module `specifier` relative to the `parentURL`. `specifier` is a string and
150
+ * `parentURL` is a WHATWG `URL`. `condition` is an optional import condition, defaulting to
151
+ * `'require'`; pass `'asset'` to resolve an asset rather than a module.
152
+ * @param specifier - The module specifier to resolve.
153
+ * @param parentURL - The WHATWG `URL` to resolve `specifier` relative to.
154
+ * @param condition - The import condition to resolve for.
155
+ * @param opts - Resolution options.
156
+ * @returns The WHATWG `URL` that `specifier` resolves to.
157
+ * @throws {MODULE_NOT_FOUND} no module matching `specifier` could be found relative to
158
+ * `parentURL`.
159
+ * @throws {ASSET_NOT_FOUND} the `'asset'` condition was used and no matching asset could be
160
+ * found.
161
+ * @throws {ADDON_NOT_FOUND} the `'addon'` condition was used and no matching addon could be
162
+ * found.
163
+ * @throws {TypeError} `specifier` is not a string, `parentURL` is not a WHATWG `URL`, or
164
+ * `condition` is not a known condition.
165
+ */
166
+ export function resolve(specifier: string, parentURL: URL, opts?: ResolveOptions): Promise<URL>
167
+ export function resolve(
168
+ specifier: string,
169
+ parentURL: URL,
170
+ condition: Condition,
171
+ opts?: ResolveOptions
172
+ ): Promise<URL>
173
+
174
+ /**
175
+ * As `resolve()`, but synchronous, for a caller that cannot await one. The protocol is driven
176
+ * through its `*Sync` methods and so must answer synchronously.
177
+ * @param specifier - The module specifier to resolve.
178
+ * @param parentURL - The WHATWG `URL` to resolve `specifier` relative to.
179
+ * @param condition - The import condition to resolve for.
180
+ * @param opts - Resolution options.
181
+ * @returns The WHATWG `URL` that `specifier` resolves to.
182
+ * @throws {UNEXPECTED_PROMISE} the protocol answered asynchronously.
183
+ * @throws {MODULE_NOT_FOUND} no module matching `specifier` could be found relative to
184
+ * `parentURL`.
185
+ * @throws {TypeError} `specifier` is not a string, `parentURL` is not a WHATWG `URL`, or
186
+ * `condition` is not a known condition.
187
+ */
188
+ export function resolveSync(specifier: string, parentURL: URL, opts?: ResolveOptions): URL
189
+ export function resolveSync(
190
+ specifier: string,
191
+ parentURL: URL,
192
+ condition: Condition,
193
+ opts?: ResolveOptions
194
+ ): URL
195
+
196
+ /**
197
+ * Create a preconfigured `require()` bound to `parentURL`, so specifiers resolve and load
198
+ * relative to it.
199
+ * @param parentURL - The parent URL that the returned `require()` resolves and loads specifiers
200
+ * relative to.
201
+ * @param opts - Options for the created `require()`, such as its `protocol` and `cache`.
202
+ * @returns A `require()` bound to `parentURL`, with `main`, `cache`, `resolve`, `addon`, and
203
+ * `asset` attached.
204
+ * @throws {TypeError} `parentURL` is neither a string nor a WHATWG `URL`, and no `referrer` was
205
+ * given.
206
+ */
114
207
  export function createRequire(parentURL: string | URL, opts?: CreateRequireOptions): Require
115
208
  }
116
209