bare-addon-resolve
Low-level addon resolution algorithm for Bare
bare-addon-resolve — Low-level addon resolution algorithm for Bare.
npm i bare-addon-resolveUsage
For synchronous resolution:
const resolve = require('bare-addon-resolve')
function readPackage(url) {
// Read and parse `url` if it exists, otherwise `null`
}
for (const resolution of resolve('./addon', new URL('file:///directory/'), readPackage)) {
console.log(resolution)
}For asynchronous resolution:
const resolve = require('bare-addon-resolve')
async function readPackage(url) {
// Read and parse `url` if it exists, otherwise `null`
}
for await (const resolution of resolve('./addon', new URL('file:///directory/'), readPackage)) {
console.log(resolution)
}API
Functions
resolve
resolve(specifier: string, parentURL: URL, readPackage?: (url: URL) => JSON | null): Iterable<URL>Resolve specifier relative to parentURL, which must be a WHATWG URL instance. readPackage is called with a URL instance for every package manifest to be read and must either return the parsed JSON package manifest, if it exists, or null. If readPackage returns a promise, synchronous iteration is not supported.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | The module specifier to resolve. |
parentURL | URL | — | The URL to resolve specifier relative to. |
readPackage? | (url: URL) => JSON | null | — | Called with the URL of each package manifest encountered; must return the parsed manifest or null. Returning a promise disables synchronous iteration. |
Returns Iterable<URL> — Yields candidate resolution URLs for the caller to test, in the order the algorithm tries them.
Throws
INVALID_ADDON_SPECIFIER— the addon specifier is not a valid package name or contains an invalid escape sequence.INVALID_PACKAGE_NAME— a package manifest'snamefield is invalid (for example contains__).
resolve.addon(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
One step of the resolution algorithm, exposed for fine-grained use: resolve specifier as an addon — a relative or absolute specifier resolves via resolve.file/resolve.directory, otherwise via resolve.package.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | The addon specifier to resolve. |
parentURL | URL | — | The URL to resolve specifier relative to. |
opts? | ResolveOptions | — | Options; see ResolveOptions. |
Returns Resolver — A Resolver yielding the candidate resolutions.
resolve.directory
resolve.directory(dirname: string, version: string, parentURL: URL, opts?: ResolveOptions): ResolverResolve dirname as a prebuilds directory addon candidate relative to parentURL.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
dirname | string | — | The prebuilds directory addon candidate. |
version | string | — | The package version, if the specifier carried one, else null. |
parentURL | URL | — | The URL to resolve dirname relative to. |
opts? | ResolveOptions | — | Options; see ResolveOptions. |
Returns Resolver — A Resolver yielding the candidate resolutions.
resolve.file(filename: string, parentURL: URL, opts?: ResolveOptions): Resolver
Resolve filename as a file addon candidate relative to parentURL, trying each of opts.extensions in turn.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
filename | string | — | The file addon candidate, without an extension. |
parentURL | URL | — | The URL to resolve filename relative to. |
opts? | ResolveOptions | — | Options; extensions lists the candidate extensions to try, in order. |
Returns Resolver — A Resolver yielding the candidate resolutions.
resolve.linked(name: string, version?: string, opts?: ResolveOptions): Resolver
Resolve name to a linked: specifier, for runtimes that link addons ahead of time by platform (for example iOS or Android) rather than resolving a prebuild at runtime.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The addon name to resolve to a linked: specifier. |
version? | string | — | The addon version, if any. |
opts? | ResolveOptions | — | Options; linked must not be false and hosts (or host) must be set, or resolution is skipped. linkedProtocol overrides the 'linked:' prefix. |
Returns Resolver — A Resolver yielding the candidate linked: resolutions.
resolve.package
resolve.package(packageSpecifier: string, packageVersion: string, parentURL: URL, opts?: ResolveOptions): ResolverResolve packageSpecifier (optionally @packageVersion) as a package name, locating the addon within it.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
packageSpecifier | string | — | The package name to resolve the addon within. |
packageVersion | string | — | The package version, if the specifier carried one, else null. |
parentURL | URL | — | The URL to resolve the package from. |
opts? | ResolveOptions | — | Options; see ResolveOptions. |
Returns Resolver — A Resolver yielding the candidate resolutions.
resolve.packageSelf
resolve.packageSelf(packageName: string, packageSubpath: string, packageVersion: string, parentURL: URL, opts?: ResolveOptions): ResolverResolve packageSubpath against the package named packageName, for when parentURL lies within that package's own scope — a package requiring its own addon by name.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
packageName | string | — | The package's own name, matched against each candidate scope's manifest. |
packageSubpath | string | — | The addon subpath to resolve within the matching package. |
packageVersion | string | — | The package version, if the specifier carried one, else null. |
parentURL | URL | — | A URL within the package's own scope to search upward from. |
opts? | ResolveOptions | — | Options; see ResolveOptions. |
Returns Resolver — A Resolver yielding the candidate resolutions.
resolve.url(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
Resolve specifier as an absolute URL, yielding it as a single candidate.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
specifier | string | — | An absolute URL specifier. |
parentURL | URL | — | Unused; accepted for a consistent step-function signature. |
opts? | ResolveOptions | — | Options; see ResolveOptions. |
Returns Resolver — A Resolver yielding the candidate resolution.
Constants and variables
resolve.constants
resolve.constants: {
UNRESOLVED: number
YIELDED: number
RESOLVED: number
}The generator status codes yielded by each resolution step: UNRESOLVED, YIELDED, and RESOLVED.
Types
resolve.Resolver
type Resolver = Generator<
{ resolution: URL } | { package: URL },
number,
void | boolean | JSON | null
>The shared generator type every resolve.* step function returns.
ResolveOptions
interface ResolveOptions {
builtinProtocol?: string
builtins?: Builtins
conditions?: Conditions
extensions?: string[]
host?: string
hosts?: string[]
linked?: boolean
linkedProtocol?: string
matchedConditions?: string[]
resolutions?: ResolutionsMap
}bare-addon-resolve/errors
AddonResolveError
AddonResolveError.INVALID_ADDON_SPECIFIER(msg: string): AddonResolveError
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns AddonResolveError — A new AddonResolveError with code INVALID_ADDON_SPECIFIER.
AddonResolveError.INVALID_PACKAGE_NAME(msg: string): AddonResolveError
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
msg | string | — | The error message. |
Returns AddonResolveError — A new AddonResolveError with code INVALID_PACKAGE_NAME.
code: string
See also
- Builds on
bare-module-resolveandbare-semver. - The resolved addon is the first candidate
resolveyields that exists as a file on the file system. - The
resolve.*step functions are subject to change between minor releases; if using them directly, specify a tilde range (for example~1.10.0) when declaring the module dependency. - Addons normally resolve through the runtime or are bundled by
bare-pack; reach for this module directly only when building tooling on the resolution algorithm itself, such asbare-module-traverse. - Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.