bare-module 6.4.0 → 7.0.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 +237 -270
- package/binding.c +623 -333
- package/index.d.ts +156 -63
- package/index.js +87 -1002
- package/lib/errors.js +27 -10
- package/lib/loader.d.ts +56 -0
- package/lib/loader.js +1192 -0
- package/lib/module/addon.js +7 -0
- package/lib/module/binary.js +7 -0
- package/lib/module/builtin.js +19 -0
- package/lib/module/bundle.js +47 -0
- package/lib/module/json.js +19 -0
- package/lib/module/script.js +23 -0
- package/lib/module/source-text.js +13 -0
- package/lib/module/synthetic.js +20 -0
- package/lib/module/text.js +7 -0
- package/lib/module/value.js +27 -0
- package/lib/module.js +392 -0
- package/lib/protocol.d.ts +56 -8
- package/lib/protocol.js +93 -26
- package/lib/source.js +18 -0
- package/lib/url.js +52 -0
- package/package.json +18 -8
- package/prebuilds/android-arm/bare-module.bare +0 -0
- package/prebuilds/android-arm64/bare-module.bare +0 -0
- package/prebuilds/android-ia32/bare-module.bare +0 -0
- package/prebuilds/android-x64/bare-module.bare +0 -0
- package/prebuilds/darwin-arm64/bare-module.bare +0 -0
- package/prebuilds/darwin-x64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-module.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-module.bare +0 -0
- package/prebuilds/linux-arm64/bare-module.bare +0 -0
- package/prebuilds/linux-x64/bare-module.bare +0 -0
- package/prebuilds/win32-arm64/bare-module.bare +0 -0
- package/prebuilds/win32-x64/bare-module.bare +0 -0
- package/lib/constants.d.ts +0 -19
- package/lib/constants.js +0 -17
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# bare-module
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The module system that powers Bare. It resolves and loads CommonJS and ECMAScript modules, as well as JSON, native addons, assets, bundles, binary, and text, and implements `package.json` resolution including the [`"exports"`](#exports), [`"imports"`](#imports), and [conditional](#conditional-exports) fields. Resolution and loading are driven by pluggable [protocols](#protocols), so modules can be served from somewhere other than the file system, such as a [`Hyperdrive`](https://github.com/holepunchto/hyperdrive) or a [`bare-bundle`](https://github.com/holepunchto/bare-bundle).
|
|
4
4
|
|
|
5
5
|
```
|
|
6
6
|
npm i bare-module
|
|
@@ -8,10 +8,34 @@ npm i bare-module
|
|
|
8
8
|
|
|
9
9
|
## Usage
|
|
10
10
|
|
|
11
|
+
A module is loaded by its WHATWG `URL`. The source may be read through the module's [protocol](#protocols) or passed in directly:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const Module = require('bare-module')
|
|
15
|
+
|
|
16
|
+
// Load a module directly from source, without it existing on disk.
|
|
17
|
+
const foo = await Module.load(
|
|
18
|
+
new URL('file:///foo.js'),
|
|
19
|
+
'module.exports = function add (a, b) { return a + b }'
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
foo.exports(2, 3)
|
|
23
|
+
// 5
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
To resolve and load specifiers relative to a directory, as `require()` does, create a `require()` bound to a parent URL. The default protocol has no backing store of its own and cannot read from the file system, so pass a [protocol](#protocols) that serves the source:
|
|
27
|
+
|
|
11
28
|
```js
|
|
12
29
|
const Module = require('bare-module')
|
|
30
|
+
|
|
31
|
+
const require = Module.createRequire('file:///directory/', { protocol })
|
|
32
|
+
|
|
33
|
+
// Resolves and loads `file:///directory/foo.js`, reading it through `protocol`.
|
|
34
|
+
const foo = require('./foo.js')
|
|
13
35
|
```
|
|
14
36
|
|
|
37
|
+
The same machinery backs the `require()` and `import` available to modules as they run; see [CommonJS modules](#commonjs-modules) and [ECMAScript modules](#ecmascript-modules) for what each exposes.
|
|
38
|
+
|
|
15
39
|
## Packages
|
|
16
40
|
|
|
17
41
|
A package is a directory with a `package.json` file.
|
|
@@ -260,205 +284,11 @@ The `"engines"` field defines the engine requirements of the package. During mod
|
|
|
260
284
|
|
|
261
285
|
## API
|
|
262
286
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
The flags for the current state of a module.
|
|
266
|
-
|
|
267
|
-
| Constant | Description |
|
|
268
|
-
| :------------ | :------------------------------------------- |
|
|
269
|
-
| `EVALUATED` | The module has been evaluated. |
|
|
270
|
-
| `SYNTHESIZED` | The module named exports have been detected. |
|
|
271
|
-
|
|
272
|
-
#### `Module.constants.types`
|
|
273
|
-
|
|
274
|
-
| Constant | Description |
|
|
275
|
-
| :------- | :--------------------------------------------------------------------------- |
|
|
276
|
-
| `SCRIPT` | The module is a CommonJS module. |
|
|
277
|
-
| `MODULE` | The module is a ECMAScript module. |
|
|
278
|
-
| `JSON` | The module is a JSON file. |
|
|
279
|
-
| `BUNDLE` | The module is a [`bare-bundle`](https://github.com/holepunchto/bare-bundle). |
|
|
280
|
-
| `ADDON` | The module is a native addon. |
|
|
281
|
-
| `BINARY` | The module is a binary file. |
|
|
282
|
-
| `TEXT` | The module is a text file. |
|
|
283
|
-
|
|
284
|
-
#### `Module.protocol`
|
|
285
|
-
|
|
286
|
-
The default `ModuleProtocol` class for resolving, reading and loading modules. See [Protocols](#protocols) for usage.
|
|
287
|
-
|
|
288
|
-
#### `Module.cache`
|
|
289
|
-
|
|
290
|
-
The global cache of loaded modules.
|
|
291
|
-
|
|
292
|
-
> [!WARNING]
|
|
293
|
-
> The cache may contain `Module` instances from several independent versions of `bare-module` as the cache is inherited through the module graph.
|
|
294
|
-
|
|
295
|
-
#### `const url = Module.resolve(specifier, parentURL[, options])`
|
|
296
|
-
|
|
297
|
-
Resolve the module `specifier` relative to the `parentURL`. `specifier` is a string and `parentURL` is a WHATWG `URL`.
|
|
298
|
-
|
|
299
|
-
Options include:
|
|
300
|
-
|
|
301
|
-
```js
|
|
302
|
-
options = {
|
|
303
|
-
// Whether the module is called via `import` or `import()`.
|
|
304
|
-
isImport: false,
|
|
305
|
-
// The referring module.
|
|
306
|
-
referrer: null,
|
|
307
|
-
// The type of the module. See Module.constants.types for possible values. The
|
|
308
|
-
// default is the equivalent constant of the `attributes`'s `type` property.
|
|
309
|
-
type,
|
|
310
|
-
// A list of file extensions to look for. The default is based on the `type`
|
|
311
|
-
// option.
|
|
312
|
-
extensions: [],
|
|
313
|
-
// The ModuleProtocol to resolve the specifier. Defaults to referrer's
|
|
314
|
-
// protocol if defined, otherwise defaults to Module.protocol
|
|
315
|
-
protocol,
|
|
316
|
-
// A default "imports" map to apply to all specifiers. Follows the same
|
|
317
|
-
// syntax and rules as the "imports" property defined in `package.json`.
|
|
318
|
-
imports,
|
|
319
|
-
// A map of preresolved imports with keys being serialized parent URLs and
|
|
320
|
-
// values being "imports" maps.
|
|
321
|
-
resolutions,
|
|
322
|
-
// A map of builtin module specifiers to loaded modules. If matched by the
|
|
323
|
-
// default resolver, the protocol of the resolved URL will be `builtin:`.
|
|
324
|
-
builtins,
|
|
325
|
-
// The supported import conditions. "default" is always recognized.
|
|
326
|
-
conditions: [],
|
|
327
|
-
// The import attributes, e.g. the `{ type: "json" }` in:
|
|
328
|
-
// `import foo from 'foo' with { type: "json" }`
|
|
329
|
-
// or in:
|
|
330
|
-
// `require('foo', { with: { type: "json" } })`
|
|
331
|
-
attributes
|
|
332
|
-
}
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
#### `const url = Module.asset(specifier, parentURL[, options])`
|
|
336
|
-
|
|
337
|
-
Get the asset URL by resolving `specifier` relative to `parentURL`. `specifier` is a string and `parentURL` is a WHATWG `URL`.
|
|
338
|
-
|
|
339
|
-
Options include:
|
|
340
|
-
|
|
341
|
-
```js
|
|
342
|
-
options = {
|
|
343
|
-
// The referring module.
|
|
344
|
-
referrer: null,
|
|
345
|
-
// The ModuleProtocol to use resolve the specifier. Defaults to referrer's
|
|
346
|
-
// protocol if defined, otherwise defaults to Module.protocol
|
|
347
|
-
protocol,
|
|
348
|
-
// A default "imports" map to apply to all specifiers. Follows the same
|
|
349
|
-
// syntax and rules as the "imports" property defined in `package.json`.
|
|
350
|
-
imports,
|
|
351
|
-
// A map of preresolved imports with keys being serialized parent URLs and
|
|
352
|
-
// values being "imports" maps.
|
|
353
|
-
resolutions,
|
|
354
|
-
// The supported import conditions. "default" is always recognized.
|
|
355
|
-
conditions
|
|
356
|
-
}
|
|
357
|
-
```
|
|
358
|
-
|
|
359
|
-
#### `const module = Module.load(url[, source][, options])`
|
|
360
|
-
|
|
361
|
-
Load a module with the provided `url`. `url` is a WHATWG `URL`. If provided, the `source` will be passed to the matching `extension` for the `url`.
|
|
362
|
-
|
|
363
|
-
Options include:
|
|
364
|
-
|
|
365
|
-
```js
|
|
366
|
-
options = {
|
|
367
|
-
// Whether the module is called via `import` or `import()`.
|
|
368
|
-
isImport: false,
|
|
369
|
-
// Whether the module is called via `import()`.
|
|
370
|
-
isDynamicImport: false,
|
|
371
|
-
// The referring module.
|
|
372
|
-
referrer: null,
|
|
373
|
-
// The type of the module. See Module.constants.types for possible values. The
|
|
374
|
-
// default is the equivalent constant of the `attributes`'s `type` property.
|
|
375
|
-
type,
|
|
376
|
-
// The assumed type of a module without a type using an ambiguous extension
|
|
377
|
-
// such as `.js`. See Module.constants.types. Inherited from `referrer` if it
|
|
378
|
-
// is defined.
|
|
379
|
-
defaultType: Module.constants.types.SCRIPT,
|
|
380
|
-
// Cache to use to load the Module. Defaults to `Module.cache`.
|
|
381
|
-
cache,
|
|
382
|
-
// The module representing the entry script where the program was launched.
|
|
383
|
-
main,
|
|
384
|
-
// The ModuleProtocol to use resolve the specifier. Defaults to referrer's
|
|
385
|
-
// `protocol` if defined, otherwise defaults to `Module.protocol`.
|
|
386
|
-
protocol,
|
|
387
|
-
// A default "imports" map to apply to all specifiers. Follows the same
|
|
388
|
-
// syntax and rules as the "imports" property defined in `package.json`.
|
|
389
|
-
imports,
|
|
390
|
-
// A map of preresolved imports with keys being serialized parent URLs and
|
|
391
|
-
// values being "imports" maps.
|
|
392
|
-
resolutions,
|
|
393
|
-
// A map of builtin module specifiers to loaded modules. If the `url`'s
|
|
394
|
-
// protocol is `builtin:`, the module's exports will be set to the matching
|
|
395
|
-
// value in the map for `url.pathname`.
|
|
396
|
-
builtins,
|
|
397
|
-
// The supported import conditions. "default" is always recognized.
|
|
398
|
-
conditions,
|
|
399
|
-
// The import attributes, e.g. the `{ type: "json" }` in:
|
|
400
|
-
// `import foo from 'foo' with { type: "json" }`
|
|
401
|
-
// or in:
|
|
402
|
-
// `require('foo', { with: { type: "json" } })`
|
|
403
|
-
attributes
|
|
404
|
-
}
|
|
405
|
-
```
|
|
406
|
-
|
|
407
|
-
#### `module.url`
|
|
408
|
-
|
|
409
|
-
The WHATWG `URL` identifier of the module.
|
|
410
|
-
|
|
411
|
-
#### `module.filename`
|
|
412
|
-
|
|
413
|
-
The file portion of `module.url`.
|
|
414
|
-
|
|
415
|
-
#### `module.dirname`
|
|
416
|
-
|
|
417
|
-
The directory portion of `module.url`.
|
|
418
|
-
|
|
419
|
-
#### `module.type`
|
|
420
|
-
|
|
421
|
-
The type of the module. See [`Module.constants.types`](#module.constants.types) for possible values.
|
|
422
|
-
|
|
423
|
-
#### `module.defaultType`
|
|
424
|
-
|
|
425
|
-
The assumed type of a module without a `type` using an ambiguous extension, such as `.js`. See [`Module.constants.types`](#module.constants.types) for possible values.
|
|
287
|
+
See the [`bare-module` reference](https://docs.pears.com/reference/bare/modules/bare-module).
|
|
426
288
|
|
|
427
|
-
|
|
289
|
+
## CommonJS modules
|
|
428
290
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
#### `module.main`
|
|
432
|
-
|
|
433
|
-
The module representing the entry script where the program was launched.
|
|
434
|
-
|
|
435
|
-
#### `module.exports`
|
|
436
|
-
|
|
437
|
-
The exports from the module.
|
|
438
|
-
|
|
439
|
-
#### `module.imports`
|
|
440
|
-
|
|
441
|
-
The import map when the module was loaded.
|
|
442
|
-
|
|
443
|
-
#### `module.resolutions`
|
|
444
|
-
|
|
445
|
-
A map of preresolved imports with keys being serialized parent URLs and values being `"imports"` maps. Resolutions performed while loading the module are cached in this map, keyed by the condition (`"import"`, `"require"`, or `"asset"`) that produced them, so that repeated resolutions of the same specifier can be served from the cache.
|
|
446
|
-
|
|
447
|
-
#### `module.builtins`
|
|
448
|
-
|
|
449
|
-
A map of builtin module specifiers mapped to the loaded module.
|
|
450
|
-
|
|
451
|
-
#### `module.conditions`
|
|
452
|
-
|
|
453
|
-
An array of conditions used to resolve dependencies while loading the module. See [Conditional Exports](#conditional-exports) for possible values.
|
|
454
|
-
|
|
455
|
-
#### `module.protocol`
|
|
456
|
-
|
|
457
|
-
The `ModuleProtocol` class used for resolving, reading, and loading modules. See [Protocols](#protocols).
|
|
458
|
-
|
|
459
|
-
### CommonJS modules
|
|
460
|
-
|
|
461
|
-
#### `require(specifier[, options])`
|
|
291
|
+
### `require(specifier[, options])`
|
|
462
292
|
|
|
463
293
|
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.
|
|
464
294
|
|
|
@@ -469,25 +299,26 @@ Options include:
|
|
|
469
299
|
```js
|
|
470
300
|
options = {
|
|
471
301
|
// The import attributes which instruct how the file or module should be loaded.
|
|
472
|
-
//
|
|
473
|
-
// `
|
|
302
|
+
// `type` is one of the strings `script`, `module`, `json`, `bundle`, `addon`,
|
|
303
|
+
// `binary` and `text`. A type that is not one of them is turned down rather
|
|
304
|
+
// than passed over, as is a `type` that is not a string.
|
|
474
305
|
with: { type: 'json' }
|
|
475
306
|
}
|
|
476
307
|
```
|
|
477
308
|
|
|
478
|
-
|
|
309
|
+
### `require.main`
|
|
479
310
|
|
|
480
|
-
The module representing the entry script where the program was launched.
|
|
311
|
+
The module representing the entry script where the program was launched.
|
|
481
312
|
|
|
482
|
-
|
|
313
|
+
### `require.cache`
|
|
483
314
|
|
|
484
|
-
|
|
315
|
+
The registry of loaded modules for this module's graph, keyed by URL href.
|
|
485
316
|
|
|
486
|
-
|
|
317
|
+
### `const path = require.resolve(specifier[, parentURL])`
|
|
487
318
|
|
|
488
319
|
Use the internal machinery of `require()` to resolve the `specifier` string relative to the URL `parentURL` and return the path string.
|
|
489
320
|
|
|
490
|
-
|
|
321
|
+
### `require.addon([specifier][, parentURL])`
|
|
491
322
|
|
|
492
323
|
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.
|
|
493
324
|
|
|
@@ -501,15 +332,15 @@ module.exports = require.addon()
|
|
|
501
332
|
|
|
502
333
|
See [`bare-addon`](https://github.com/holepunchto/bare-addon) for a template of building native addon modules.
|
|
503
334
|
|
|
504
|
-
|
|
335
|
+
### `require.addon.host`
|
|
505
336
|
|
|
506
337
|
Returns the string representation of the platform and architecture used when resolving addons with the pattern `<platform>-<arch>[-<environment>]`. Returns the same value as `Bare.Addon.host`.
|
|
507
338
|
|
|
508
|
-
|
|
339
|
+
### `const path = require.addon.resolve([specifier][, parentURL])`
|
|
509
340
|
|
|
510
341
|
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).
|
|
511
342
|
|
|
512
|
-
|
|
343
|
+
### `const path = require.asset(specifier[, parentURL])`
|
|
513
344
|
|
|
514
345
|
Resolve the `specifier` relative to the `parentURL` and return the path of the asset as a string.
|
|
515
346
|
|
|
@@ -520,136 +351,272 @@ const fs = require('bare-fs')
|
|
|
520
351
|
const contents = fs.readFileSync(require.asset('./foo.txt'))
|
|
521
352
|
```
|
|
522
353
|
|
|
523
|
-
|
|
354
|
+
## ECMAScript modules
|
|
524
355
|
|
|
525
|
-
|
|
356
|
+
### `import defaultExport, * as name, { export1, export2 as alias2, ... } from 'specifier' with { type: 'json' }`
|
|
526
357
|
|
|
527
358
|
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.
|
|
528
359
|
|
|
529
360
|
For more information on `import` syntax, see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import).
|
|
530
361
|
|
|
531
|
-
|
|
362
|
+
### `import.meta.url`
|
|
532
363
|
|
|
533
364
|
The string representation of the URL for the current module.
|
|
534
365
|
|
|
535
|
-
|
|
366
|
+
### `import.meta.main`
|
|
536
367
|
|
|
537
368
|
A boolean representing whether the current module is the entry script where the program was launched.
|
|
538
369
|
|
|
539
|
-
|
|
370
|
+
### `import.meta.cache`
|
|
540
371
|
|
|
541
|
-
|
|
372
|
+
The registry of loaded modules for this module's graph, keyed by URL href. The same value as `require.cache` for a CommonJS module of the same graph.
|
|
542
373
|
|
|
543
|
-
|
|
374
|
+
### `import.meta.dirname`
|
|
544
375
|
|
|
545
376
|
The directory name of the current module.
|
|
546
377
|
|
|
547
|
-
|
|
378
|
+
### `import.meta.filename`
|
|
548
379
|
|
|
549
380
|
The file name of the current module.
|
|
550
381
|
|
|
551
|
-
|
|
382
|
+
### `const href = import.meta.resolve(specifier[, parentURL])`
|
|
552
383
|
|
|
553
384
|
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.
|
|
554
385
|
|
|
555
|
-
|
|
386
|
+
### `import.meta.addon([specifier][, parentURL])`
|
|
556
387
|
|
|
557
388
|
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.
|
|
558
389
|
|
|
559
390
|
Returns the exported module contents.
|
|
560
391
|
|
|
561
|
-
|
|
392
|
+
### `import.meta.addon.host`
|
|
562
393
|
|
|
563
394
|
Returns the string representation of the platform and architecture used when resolving addons with the pattern `<platform>-<arch>[-<environment>]`. Returns the same value as `Bare.Addon.host`.
|
|
564
395
|
|
|
565
|
-
|
|
396
|
+
### `const href = import.meta.addon.resolve([specifier][, parentURL])`
|
|
566
397
|
|
|
567
398
|
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).
|
|
568
399
|
|
|
569
|
-
|
|
400
|
+
### `const href = import.meta.asset(specifier[, parentURL])`
|
|
570
401
|
|
|
571
402
|
Resolve the `specifier` relative to the `parentURL` and return the URL of the asset as a string.
|
|
572
403
|
|
|
573
|
-
|
|
404
|
+
## Custom `require()`
|
|
574
405
|
|
|
575
406
|
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.
|
|
576
407
|
|
|
577
|
-
|
|
408
|
+
### `const require = Module.createRequire(parentURL[, options])`
|
|
578
409
|
|
|
579
410
|
Options include:
|
|
580
411
|
|
|
581
412
|
```js
|
|
582
413
|
options = {
|
|
583
|
-
// The module
|
|
584
|
-
//
|
|
585
|
-
// options.
|
|
586
|
-
module: null,
|
|
587
|
-
// The referring module.
|
|
414
|
+
// The referring module. Supplies the loader, and with it the default for
|
|
415
|
+
// every option below that the loader carries.
|
|
588
416
|
referrer: null,
|
|
589
|
-
// The type of the module. See Module.constants.types for possible values.
|
|
590
|
-
type: Module.constants.types.SCRIPT,
|
|
591
417
|
// The assumed type of a module without a type using an ambiguous extension
|
|
592
|
-
// such as `.js`.
|
|
593
|
-
//
|
|
594
|
-
defaultType: Module.constants.
|
|
595
|
-
// A cache of loaded modules. Inherited from `referrer`
|
|
596
|
-
//
|
|
418
|
+
// such as `.js`. One of Module.constants. Inherited from `referrer` if it is
|
|
419
|
+
// defined, otherwise defaults to SCRIPT.
|
|
420
|
+
defaultType: Module.constants.SCRIPT,
|
|
421
|
+
// A cache of loaded modules. Inherited from `referrer` only while `protocol`
|
|
422
|
+
// and `builtins` are also inherited, as a cache holds the modules of a single
|
|
423
|
+
// graph; narrowing either starts a graph of its own with a fresh cache. Pass
|
|
424
|
+
// a cache object to share one, including a fresh object to share nothing.
|
|
597
425
|
cache,
|
|
598
|
-
// The
|
|
599
|
-
|
|
600
|
-
//
|
|
601
|
-
// referrer's protocol if defined, otherwise defaults to Module.protocol
|
|
426
|
+
// The ModuleProtocol used to resolve and read modules. Defaults to
|
|
427
|
+
// referrer's protocol if defined, otherwise to a protocol with no backing
|
|
428
|
+
// store.
|
|
602
429
|
protocol,
|
|
603
|
-
// A default "imports" map to apply to all specifiers.
|
|
604
|
-
// syntax and rules as the "imports" property defined in
|
|
430
|
+
// A default "imports" map to apply to all specifiers. An object or omitted,
|
|
431
|
+
// following the same syntax and rules as the "imports" property defined in
|
|
432
|
+
// `package.json`.
|
|
605
433
|
imports,
|
|
606
434
|
// A map of preresolved imports with keys being serialized parent URLs and
|
|
607
|
-
// values being "imports" maps.
|
|
435
|
+
// values being "imports" maps. Follows the cache, and like it is an object or
|
|
436
|
+
// omitted.
|
|
608
437
|
resolutions,
|
|
609
|
-
// A map of builtin module specifiers to loaded modules.
|
|
610
|
-
builtins,
|
|
611
|
-
//
|
|
612
|
-
|
|
438
|
+
// A map of builtin module specifiers to loaded modules, or omitted. Only the
|
|
439
|
+
// map's own keys are builtins. Inherited from `referrer` if it is defined,
|
|
440
|
+
// including when `protocol` narrows, so pass this too to narrow what the
|
|
441
|
+
// module reaches.
|
|
442
|
+
builtins
|
|
613
443
|
}
|
|
614
444
|
```
|
|
615
445
|
|
|
616
|
-
|
|
446
|
+
A module reaches as far as its `protocol` and its `builtins`, and a referrer hands on both. Narrowing one does not narrow the other, so code that should not reach what the referrer reaches must be given both:
|
|
447
|
+
|
|
448
|
+
```js
|
|
449
|
+
// The module reads only through `protocol`, but still reaches every builtin the
|
|
450
|
+
// referrer was given.
|
|
451
|
+
Module.createRequire(parentURL, { referrer, protocol })
|
|
452
|
+
|
|
453
|
+
// The module reaches no further than what is passed here.
|
|
454
|
+
Module.createRequire(parentURL, { referrer, protocol, builtins })
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
## Protocols
|
|
617
458
|
|
|
618
459
|
Protocols define how to resolve, access and load modules. Custom protocols can be defined to extend or replace how module are resolved and loaded to support things like loading modules via a [`Hyperdrive`](https://github.com/holepunchto/hyperdrive).
|
|
619
460
|
|
|
620
|
-
|
|
461
|
+
When no protocol is passed, modules are read through a bare `Module.Protocol`. It has no backing store of its own and in particular cannot read from the file system, so it finds nothing; pass a protocol to serve the source.
|
|
462
|
+
|
|
463
|
+
### `const protocol = new Module.Protocol(methods, context = null)`
|
|
621
464
|
|
|
622
465
|
Methods include:
|
|
623
466
|
|
|
624
467
|
```js
|
|
625
468
|
methods = {
|
|
626
|
-
// function (
|
|
627
|
-
// A function to
|
|
628
|
-
//
|
|
629
|
-
|
|
630
|
-
//
|
|
631
|
-
// A function to process the resolved URL. Can be used to convert file paths,
|
|
632
|
-
// etc.
|
|
633
|
-
postresolve,
|
|
634
|
-
// function* (specifier, parentURL, imports): [URL]
|
|
635
|
-
// A generator to resolve the `specifier` to a URL.
|
|
469
|
+
// function (url): URL | Promise<URL>
|
|
470
|
+
// A function to post-process a resolved URL before it is used, for example to
|
|
471
|
+
// canonicalize symlinks with `realpath` so a module reached through different
|
|
472
|
+
// symlinks dedupes against its real location. Defaults to the identity. May
|
|
473
|
+
// return a promise to resolve asynchronously.
|
|
636
474
|
resolve,
|
|
637
|
-
// function (url):
|
|
638
|
-
//
|
|
475
|
+
// function (url): URL
|
|
476
|
+
// The synchronous variant of `resolve`, used when a graph is linked
|
|
477
|
+
// synchronously. Defaults to calling `resolve` and throwing if it returns a
|
|
478
|
+
// promise.
|
|
479
|
+
resolveSync,
|
|
480
|
+
// function (url): boolean | Promise<boolean>
|
|
481
|
+
// A function that returns whether the URL exists as a boolean. Consulted before
|
|
482
|
+
// `read`, so a candidate that does not exist is never fetched. May return a
|
|
483
|
+
// promise to answer asynchronously.
|
|
639
484
|
exists,
|
|
640
|
-
// function (url):
|
|
641
|
-
//
|
|
642
|
-
//
|
|
485
|
+
// function (url): boolean
|
|
486
|
+
// The synchronous variant of `exists`. Defaults to calling `exists` and throwing
|
|
487
|
+
// if it returns a promise.
|
|
488
|
+
existsSync,
|
|
489
|
+
// function (url): string | Buffer | null | Promise<string | Buffer | null>
|
|
490
|
+
// A function that returns the source of a URL as a string or buffer, or `null`
|
|
491
|
+
// if it does not exist. May return a promise to serve the source asynchronously.
|
|
643
492
|
read,
|
|
644
|
-
// function (url):
|
|
645
|
-
//
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
//
|
|
649
|
-
asset
|
|
493
|
+
// function (url): string | Buffer | null
|
|
494
|
+
// The synchronous variant of `read`. Defaults to calling `read` and throwing if
|
|
495
|
+
// it returns a promise.
|
|
496
|
+
readSync,
|
|
497
|
+
// function* (url): Iterable<URL> | AsyncIterable<URL>
|
|
498
|
+
// A generator enumerating the URLs under a prefix, used for asset globbing.
|
|
499
|
+
// Defaults to listing nothing, in which case an asset is the prefix itself as
|
|
500
|
+
// `exists` has it, so a backing store need only provide `list` to support an
|
|
501
|
+
// asset naming a directory. Setting `resolved` on what it returns declares the
|
|
502
|
+
// URLs already resolved, sparing whoever drives the listing a `resolve` for
|
|
503
|
+
// every URL in it.
|
|
504
|
+
list,
|
|
505
|
+
// function* (url): Iterable<URL>
|
|
506
|
+
// The synchronous variant of `list`. Defaults to delegating to `list` and
|
|
507
|
+
// throwing if it returns an asynchronous iterable.
|
|
508
|
+
listSync
|
|
509
|
+
}
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
Each method comes in an asynchronous and a synchronous variant. The asynchronous variants may return a promise (or, for `list`, an asynchronous iterable) to serve modules asynchronously and are driven by the asynchronous `Module` statics (`Module.load` and `Module.resolve`) and [`Loader`](#loader) methods. The synchronous `*Sync` variants are driven by the synchronous entry points (`require()`, `Module.loadSync`, `Module.resolveSync`, `loader.linkSync`, and `loader.importSync`) and default to calling their asynchronous counterpart, throwing an `UNEXPECTED_PROMISE` error if it answers asynchronously. A protocol that supports both may implement the `*Sync` variants directly; for such a protocol a statically imported module is read through the asynchronous `read` while a `require()` with a computed specifier is read through `readSync`.
|
|
513
|
+
|
|
514
|
+
### `protocol[Symbol.for('bare.module.protocol.kind')]`
|
|
515
|
+
|
|
516
|
+
The version of the protocol interface. An instance reports the same version as `Module.Protocol`.
|
|
517
|
+
|
|
518
|
+
A protocol is the one capability a module system must be handed, and it travels between module systems that may be different copies of `bare-module`. A module system therefore checks this version before reading anything through a protocol, and throws a `PROTOCOL_INCOMPATIBLE` error if it does not match. Use `Module.Protocol.isProtocol(value)` to make the same check, or read the symbol directly to check against a version of your own.
|
|
519
|
+
|
|
520
|
+
### `const extended = protocol.extend(methods)`
|
|
521
|
+
|
|
522
|
+
Return a new `ModuleProtocol` that overrides the given `methods`, falling back to this protocol for any method not provided. Each method is passed this protocol as its first argument, ahead of the arguments the method normally takes, so an override can defer to what it extends.
|
|
523
|
+
|
|
524
|
+
A method and its `*Sync` variant answer the same question, so overriding either governs both and neither is inherited from this protocol. Overriding the asynchronous variant alone is usually what you want, as the synchronous variant then defaults to calling it. Overriding the synchronous variant alone leaves the asynchronous one at its default, which finds nothing; override both to serve both paths. An extension is therefore never half applied, and a protocol narrowed by one reaches no further than the extension allows on either path:
|
|
525
|
+
|
|
526
|
+
```js
|
|
527
|
+
// Reads nothing outside `/public/`, whether the graph is linked synchronously
|
|
528
|
+
// or asynchronously.
|
|
529
|
+
const restricted = protocol.extend({
|
|
530
|
+
exists(protocol, url) {
|
|
531
|
+
return url.href.startsWith('file:///public/') && protocol.exists(url)
|
|
532
|
+
},
|
|
533
|
+
|
|
534
|
+
read(protocol, url) {
|
|
535
|
+
return url.href.startsWith('file:///public/') ? protocol.read(url) : null
|
|
536
|
+
}
|
|
537
|
+
})
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
## Loader
|
|
541
|
+
|
|
542
|
+
A `Loader` owns a registry of module records keyed by URL and drives resolution and linking against a [protocol](#protocols). Where the `Module` statics link and evaluate in a single call, a loader exposes linking and evaluation as separate steps, as well as synchronous variants, so modules can be served from an asynchronous protocol such as one backed by a [`Hyperdrive`](https://github.com/holepunchto/hyperdrive).
|
|
543
|
+
|
|
544
|
+
Linking is split into two phases. First a graph is _linked_: every module reachable from the entry is read, lexed, and recorded, and its native module is created without running any code. Then it is _evaluated_: the recorded modules run. All IO happens during linking, so evaluation is synchronous regardless of how the graph was fetched.
|
|
545
|
+
|
|
546
|
+
### `const loader = new Module.Loader([options])`
|
|
547
|
+
|
|
548
|
+
Options include:
|
|
549
|
+
|
|
550
|
+
```js
|
|
551
|
+
options = {
|
|
552
|
+
// The ModuleProtocol used to resolve and read modules. Defaults to a
|
|
553
|
+
// protocol with no backing store of its own.
|
|
554
|
+
protocol,
|
|
555
|
+
// A map of builtin module specifiers to their exports, or omitted. Only the
|
|
556
|
+
// map's own keys are builtins; a name reached through its prototype chain is
|
|
557
|
+
// not.
|
|
558
|
+
builtins,
|
|
559
|
+
// The assumed type of a module without a type using an ambiguous extension
|
|
560
|
+
// such as `.js`. One of Module.constants.
|
|
561
|
+
defaultType,
|
|
562
|
+
// A default "imports" map to apply to all specifiers. An object or omitted,
|
|
563
|
+
// following the same syntax and rules as the "imports" property defined in
|
|
564
|
+
// `package.json`.
|
|
565
|
+
imports,
|
|
566
|
+
// The maximum number of module reads to perform concurrently while linking.
|
|
567
|
+
// A non-negative integer, defaulting to `0`, which applies no limit.
|
|
568
|
+
concurrency: 0,
|
|
569
|
+
// The module cache. Pass an object to share one, or omit for a fresh cache
|
|
570
|
+
// scoped to this loader. It is an object or nothing; there is no flag for
|
|
571
|
+
// either sharing or not sharing, because a cache holds the modules of a
|
|
572
|
+
// single graph and which graph that is comes of naming the object. A cache is
|
|
573
|
+
// claimed by the first loader to take it, and a later loader that reads
|
|
574
|
+
// through a different protocol or different builtins is turned down with a
|
|
575
|
+
// CACHE_INCOMPATIBLE error.
|
|
576
|
+
cache,
|
|
577
|
+
// A map of preresolved imports with keys being serialized parent URLs and
|
|
578
|
+
// values being "imports" maps. Like the cache it is an object or omitted, and
|
|
579
|
+
// omitting it gives a fresh map scoped to this loader.
|
|
580
|
+
resolutions
|
|
650
581
|
}
|
|
651
582
|
```
|
|
652
583
|
|
|
584
|
+
### `const module = await loader.link(entry[, source][, options])`
|
|
585
|
+
|
|
586
|
+
Link the module graph rooted at `entry`, a WHATWG `URL`, awaiting each read through the protocol so an asynchronous protocol can serve the source. If `source` is given, it is used instead of reading `entry` through the protocol. Returns the entry module, instantiated but not yet evaluated.
|
|
587
|
+
|
|
588
|
+
### `const module = loader.linkSync(entry[, source][, options])`
|
|
589
|
+
|
|
590
|
+
The synchronous equivalent of `loader.link()`. It drives the protocol's synchronous methods (`resolveSync`, `existsSync`, `readSync`, and `listSync`), which throw an `UNEXPECTED_PROMISE` error when the protocol can only answer asynchronously.
|
|
591
|
+
|
|
592
|
+
### `const exports = await loader.import(entry[, options])`
|
|
593
|
+
|
|
594
|
+
Link and evaluate the graph rooted at `entry`, returning its exports. Awaits the entry's evaluation, so a top-level `await` in the entry settles before the exports are returned.
|
|
595
|
+
|
|
596
|
+
### `const exports = loader.importSync(entry[, options])`
|
|
597
|
+
|
|
598
|
+
The synchronous equivalent of `loader.import()`. It cannot await, so a top-level `await` in the entry is unsupported.
|
|
599
|
+
|
|
600
|
+
### `const module = loader.get(url)`
|
|
601
|
+
|
|
602
|
+
Return the module record cached under `url`, a WHATWG `URL`, or `null` if none is loaded.
|
|
603
|
+
|
|
604
|
+
### `loader.main`
|
|
605
|
+
|
|
606
|
+
The graph's main module: the first entry linked. `null` until the first link.
|
|
607
|
+
|
|
608
|
+
### `loader.cache`
|
|
609
|
+
|
|
610
|
+
The registry of loaded modules, keyed by URL href.
|
|
611
|
+
|
|
612
|
+
### `loader.protocol`
|
|
613
|
+
|
|
614
|
+
The `ModuleProtocol` modules are resolved and read through, shared with every module of the loader's graph.
|
|
615
|
+
|
|
616
|
+
## Threat model
|
|
617
|
+
|
|
618
|
+
`bare-module` is one of the addons Bare compiles into its binary, so it inherits [Bare's threat model](https://github.com/holepunchto/bare/blob/main/docs/threat-model.md). See [`docs/threat-model.md`](docs/threat-model.md) for where this addon sits in it.
|
|
619
|
+
|
|
653
620
|
## License
|
|
654
621
|
|
|
655
622
|
Apache-2.0
|