fix: multi-repo local dev (file:../ deps, package exports, missing deps)
This commit is contained in:
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
# bare-addon-resolve
|
||||
|
||||
Low-level addon resolution algorithm for Bare. The algorithm is implemented as a generator function that yields either package manifests to be read or resolution candidates to be tested by the caller. As a convenience, the main export is a synchronous and asynchronous iterable that relies on package manifests being read by a callback. For asynchronous iteration, the callback may return promises which will be awaited before being passed to the generator.
|
||||
|
||||
```
|
||||
npm i bare-addon-resolve
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
For synchronous resolution:
|
||||
|
||||
```js
|
||||
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:
|
||||
|
||||
```js
|
||||
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
|
||||
|
||||
#### `const resolver = resolve(specifier, parentURL[, options][, readPackage])`
|
||||
|
||||
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.
|
||||
|
||||
Options include:
|
||||
|
||||
```js
|
||||
options = {
|
||||
// A list of builtin addon specifiers. If matched, the protocol of the
|
||||
// resolved URL will be `builtinProtocol`.
|
||||
builtins: [],
|
||||
// The protocol to use for resolved builtin addon specifiers.
|
||||
builtinProtocol: 'builtin:',
|
||||
// Whether or not addons linked ahead-of-time should be resolved.
|
||||
linked: true,
|
||||
// The protocol to use for addons linked ahead-of-time.
|
||||
linkedProtocol: 'linked:',
|
||||
// The supported import conditions. "default" is always recognized.
|
||||
conditions: [],
|
||||
// An array reference which will contain the matched conditions when yielding
|
||||
// resolutions.
|
||||
matchedConditions: [],
|
||||
// The `<platform>-<arch>` combinations to look for when resolving dynamic
|
||||
// addons. If empty, only builtin specifiers can be resolved. In Bare,
|
||||
// pass `[Bare.Addon.host]`.
|
||||
hosts: [],
|
||||
// The file extensions to look for when resolving dynamic addons.
|
||||
extensions: [],
|
||||
// A map of preresolved imports with keys being serialized directory URLs and
|
||||
// values being "imports" maps.
|
||||
resolutions
|
||||
}
|
||||
```
|
||||
|
||||
#### `for (const resolution of resolver)`
|
||||
|
||||
Synchronously iterate the addon resolution candidates. The resolved addon is the first candidate that exists as a file on the file system.
|
||||
|
||||
#### `for await (const resolution of resolver)`
|
||||
|
||||
Asynchronously iterate the addon resolution candidates. If `readPackage` returns promises, these will be awaited. The same comments as `for (const resolution of resolver)` apply.
|
||||
|
||||
### Algorithm
|
||||
|
||||
The following generator functions implement the resolution algorithm. The yielded values have the following shape:
|
||||
|
||||
**Package manifest**
|
||||
|
||||
```js
|
||||
next.value = {
|
||||
package: URL
|
||||
}
|
||||
```
|
||||
|
||||
If the package manifest identified by `next.value.package` exists, `generator.next()` must be passed the parsed JSON value of the manifest. If it does not exist, pass `null` instead.
|
||||
|
||||
**Resolution candidate**
|
||||
|
||||
```js
|
||||
next.value = {
|
||||
resolution: URL
|
||||
}
|
||||
```
|
||||
|
||||
If the addon identified by `next.value.resolution` exists, `generator.next()` may be passed `true` to signal that the resolution for the current set of conditions has been identified. If it does not exist, pass `false` instead.
|
||||
|
||||
To drive the generator functions, a loop like the following can be used:
|
||||
|
||||
```js
|
||||
const generator = resolve.addon(specifier, parentURL)
|
||||
|
||||
let next = generator.next()
|
||||
|
||||
while (next.done !== true) {
|
||||
const value = next.value
|
||||
|
||||
if (value.package) {
|
||||
// Read and parse `value.package` if it exists, otherwise `null`
|
||||
let info
|
||||
|
||||
next = generator.next(info)
|
||||
} else {
|
||||
const resolution = value.resolution
|
||||
|
||||
// `true` if `resolution` was the correct candidate, otherwise `false`
|
||||
let resolved
|
||||
|
||||
next = generator.next(resolved)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Options are the same as `resolve()` for all functions.
|
||||
|
||||
> [!WARNING]
|
||||
> These functions are currently subject to change between minor releases. If using them directly, make sure to specify a tilde range (`~1.2.3`) when declaring the module dependency.
|
||||
|
||||
#### `const generator = resolve.addon(specifier, parentURL[, options])`
|
||||
|
||||
#### `const generator = resolve.url(url, parentURL[, options])`
|
||||
|
||||
#### `const generator = resolve.package(packageSpecifier, packageVersion, parentURL[, options])`
|
||||
|
||||
#### `const generator = resolve.packageSelf(packageName, packageSubpath, packageVersion, parentURL[, options])`
|
||||
|
||||
#### `const generator = resolve.preresolved(directoryURL, resolutions[, options])`
|
||||
|
||||
#### `const generator = resolve.file(filename, parentURL[, options])`
|
||||
|
||||
#### `const generator = resolve.directory(dirname, version, parentURL[, options])`
|
||||
|
||||
#### `const generator = resolve.linked(name, version[, options])`
|
||||
|
||||
## License
|
||||
|
||||
Apache-2.0
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
import URL from 'bare-url'
|
||||
import { constants, type Builtins, type Conditions, type ResolutionsMap } from 'bare-module-resolve'
|
||||
|
||||
type JSON = string | number | boolean | JSON[] | { [key: string]: JSON }
|
||||
|
||||
interface ResolveOptions {
|
||||
builtinProtocol?: string
|
||||
builtins?: Builtins
|
||||
conditions?: Conditions
|
||||
extensions?: string[]
|
||||
host?: string
|
||||
hosts?: string[]
|
||||
linked?: boolean
|
||||
linkedProtocol?: string
|
||||
matchedConditions?: string[]
|
||||
resolutions?: ResolutionsMap
|
||||
}
|
||||
|
||||
declare function resolve(
|
||||
specifier: string,
|
||||
parentURL: URL,
|
||||
readPackage?: (url: URL) => JSON | null
|
||||
): Iterable<URL>
|
||||
|
||||
declare function resolve(
|
||||
specifier: string,
|
||||
parentURL: URL,
|
||||
readPackage: (url: URL) => Promise<JSON | null>
|
||||
): AsyncIterable<URL>
|
||||
|
||||
declare function resolve(
|
||||
specifier: string,
|
||||
parentURL: URL,
|
||||
opts: ResolveOptions,
|
||||
readPackage?: (url: URL) => JSON | null
|
||||
): Iterable<URL>
|
||||
|
||||
declare function resolve(
|
||||
specifier: string,
|
||||
parentURL: URL,
|
||||
opts: ResolveOptions,
|
||||
readPackage: (url: URL) => Promise<JSON | null>
|
||||
): AsyncIterable<URL>
|
||||
|
||||
declare namespace resolve {
|
||||
export { constants, type ResolveOptions }
|
||||
|
||||
export type Resolver = Generator<
|
||||
{ resolution: URL } | { package: URL },
|
||||
number,
|
||||
void | boolean | JSON | null
|
||||
>
|
||||
|
||||
export function addon(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
|
||||
|
||||
export function url(specifier: string, parentURL: URL, opts?: ResolveOptions): Resolver
|
||||
|
||||
export function package(
|
||||
packageSpecifier: string,
|
||||
packageVersion: string,
|
||||
parentURL: URL,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function packageSelf(
|
||||
packageName: string,
|
||||
packageSubpath: string,
|
||||
packageVersion: string,
|
||||
parentURL: URL,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function file(filename: string, parentURL: URL, opts?: ResolveOptions): Resolver
|
||||
|
||||
export function directory(
|
||||
dirname: string,
|
||||
version: string,
|
||||
parentURL: URL,
|
||||
opts?: ResolveOptions
|
||||
): Resolver
|
||||
|
||||
export function linked(name: string, version?: string, opts?: ResolveOptions): Resolver
|
||||
}
|
||||
|
||||
export = resolve
|
||||
+471
@@ -0,0 +1,471 @@
|
||||
const resolve = require('bare-module-resolve')
|
||||
const { Version } = require('bare-semver')
|
||||
const errors = require('./lib/errors')
|
||||
|
||||
module.exports = exports = function resolve(specifier, parentURL, opts, readPackage) {
|
||||
if (typeof opts === 'function') {
|
||||
readPackage = opts
|
||||
opts = {}
|
||||
} else if (typeof readPackage !== 'function') {
|
||||
readPackage = defaultReadPackage
|
||||
}
|
||||
|
||||
return {
|
||||
*[Symbol.iterator]() {
|
||||
const generator = exports.addon(specifier, parentURL, opts)
|
||||
|
||||
let next = generator.next()
|
||||
|
||||
while (next.done !== true) {
|
||||
const value = next.value
|
||||
|
||||
if (value.package) {
|
||||
next = generator.next(readPackage(value.package))
|
||||
} else {
|
||||
next = generator.next(yield value.resolution)
|
||||
}
|
||||
}
|
||||
|
||||
return next.value
|
||||
},
|
||||
|
||||
async *[Symbol.asyncIterator]() {
|
||||
const generator = exports.addon(specifier, parentURL, opts)
|
||||
|
||||
let next = generator.next()
|
||||
|
||||
while (next.done !== true) {
|
||||
const value = next.value
|
||||
|
||||
if (value.package) {
|
||||
next = generator.next(await readPackage(value.package))
|
||||
} else {
|
||||
next = generator.next(yield value.resolution)
|
||||
}
|
||||
}
|
||||
|
||||
return next.value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function defaultReadPackage() {
|
||||
return null
|
||||
}
|
||||
|
||||
const { UNRESOLVED, YIELDED, RESOLVED } = resolve.constants
|
||||
|
||||
exports.constants = {
|
||||
UNRESOLVED,
|
||||
YIELDED,
|
||||
RESOLVED
|
||||
}
|
||||
|
||||
exports.addon = function* (specifier, parentURL, opts = {}) {
|
||||
const { resolutions = null } = opts
|
||||
|
||||
if (exports.startsWithWindowsDriveLetter(specifier)) {
|
||||
specifier = '/' + specifier
|
||||
}
|
||||
|
||||
let status
|
||||
|
||||
if (resolutions) {
|
||||
status = yield* resolve.preresolved(specifier, resolutions, parentURL, opts)
|
||||
|
||||
if (status) return status
|
||||
}
|
||||
|
||||
status = yield* exports.url(specifier, parentURL, opts)
|
||||
|
||||
if (status) return status
|
||||
|
||||
let version = null
|
||||
|
||||
const i = specifier.lastIndexOf('@')
|
||||
|
||||
if (i > 0) {
|
||||
version = specifier.substring(i + 1)
|
||||
|
||||
try {
|
||||
Version.parse(version)
|
||||
|
||||
specifier = specifier.substring(0, i)
|
||||
} catch {
|
||||
version = null
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
specifier === '.' ||
|
||||
specifier === '..' ||
|
||||
specifier[0] === '/' ||
|
||||
specifier[0] === '\\' ||
|
||||
specifier.startsWith('./') ||
|
||||
specifier.startsWith('.\\') ||
|
||||
specifier.startsWith('../') ||
|
||||
specifier.startsWith('..\\')
|
||||
) {
|
||||
status = yield* exports.file(specifier, parentURL, opts)
|
||||
|
||||
if (status === RESOLVED) return status
|
||||
|
||||
return yield* exports.directory(specifier, version, parentURL, opts)
|
||||
}
|
||||
|
||||
return yield* exports.package(specifier, version, parentURL, opts)
|
||||
}
|
||||
|
||||
exports.url = function* (url, parentURL, opts = {}) {
|
||||
let resolution
|
||||
try {
|
||||
resolution = new URL(url)
|
||||
} catch {
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
const resolved = yield { resolution }
|
||||
|
||||
return resolved ? RESOLVED : YIELDED
|
||||
}
|
||||
|
||||
exports.package = function* (packageSpecifier, packageVersion, parentURL, opts = {}) {
|
||||
if (packageSpecifier === '') {
|
||||
throw errors.INVALID_ADDON_SPECIFIER(
|
||||
`Addon specifier '${packageSpecifier}' is not a valid package name`
|
||||
)
|
||||
}
|
||||
|
||||
let packageName
|
||||
|
||||
if (packageSpecifier[0] !== '@') {
|
||||
packageName = packageSpecifier.split('/', 1).join()
|
||||
} else {
|
||||
if (!packageSpecifier.includes('/')) {
|
||||
throw errors.INVALID_ADDON_SPECIFIER(
|
||||
`Addon specifier '${packageSpecifier}' is not a valid package name`
|
||||
)
|
||||
}
|
||||
|
||||
packageName = packageSpecifier.split('/', 2).join('/')
|
||||
}
|
||||
|
||||
if (packageName[0] === '.' || packageName.includes('\\') || packageName.includes('%')) {
|
||||
throw errors.INVALID_ADDON_SPECIFIER(
|
||||
`Addon specifier '${packageSpecifier}' is not a valid package name`
|
||||
)
|
||||
}
|
||||
|
||||
const packageSubpath = '.' + packageSpecifier.substring(packageName.length)
|
||||
|
||||
const status = yield* exports.packageSelf(
|
||||
packageName,
|
||||
packageSubpath,
|
||||
packageVersion,
|
||||
parentURL,
|
||||
opts
|
||||
)
|
||||
|
||||
if (status) return status
|
||||
|
||||
parentURL = new URL(parentURL.href)
|
||||
|
||||
do {
|
||||
const packageURL = new URL('node_modules/' + packageName + '/', parentURL)
|
||||
|
||||
parentURL.pathname = parentURL.pathname.substring(0, parentURL.pathname.lastIndexOf('/'))
|
||||
|
||||
const info = yield { package: new URL('package.json', packageURL) }
|
||||
|
||||
if (info) {
|
||||
return yield* exports.directory(packageSubpath, packageVersion, packageURL, opts)
|
||||
}
|
||||
} while (parentURL.pathname !== '' && parentURL.pathname !== '/')
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.packageSelf = function* (
|
||||
packageName,
|
||||
packageSubpath,
|
||||
packageVersion,
|
||||
parentURL,
|
||||
opts = {}
|
||||
) {
|
||||
for (const packageURL of resolve.lookupPackageScope(parentURL, opts)) {
|
||||
const info = yield { package: packageURL }
|
||||
|
||||
if (info) {
|
||||
if (info.name === packageName) {
|
||||
return yield* exports.directory(packageSubpath, packageVersion, packageURL, opts)
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.lookupPrebuildsScope = function* lookupPrebuildsScope(url, opts = {}) {
|
||||
const scopeURL = new URL(url.href)
|
||||
|
||||
do {
|
||||
yield new URL('prebuilds/', scopeURL)
|
||||
|
||||
scopeURL.pathname = scopeURL.pathname.substring(0, scopeURL.pathname.lastIndexOf('/'))
|
||||
|
||||
if (
|
||||
scopeURL.pathname.length === 3 &&
|
||||
exports.isWindowsDriveLetter(scopeURL.pathname.substring(1))
|
||||
) {
|
||||
break
|
||||
}
|
||||
} while (scopeURL.pathname !== '' && scopeURL.pathname !== '/')
|
||||
}
|
||||
|
||||
exports.file = function* (filename, parentURL, opts = {}) {
|
||||
if (
|
||||
filename === '.' ||
|
||||
filename === '..' ||
|
||||
filename[filename.length - 1] === '/' ||
|
||||
filename[filename.length - 1] === '\\'
|
||||
) {
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
if (parentURL.protocol === 'file:' && /%2f|%5c/i.test(filename)) {
|
||||
throw errors.INVALID_ADDON_SPECIFIER(`Addon specifier '${filename}' is invalid`)
|
||||
}
|
||||
|
||||
const { extensions = [] } = opts
|
||||
|
||||
let status = UNRESOLVED
|
||||
|
||||
for (let ext of extensions) {
|
||||
if (filename.endsWith(ext)) ext = ''
|
||||
|
||||
if (yield { resolution: new URL(filename + ext, parentURL) }) {
|
||||
return RESOLVED
|
||||
}
|
||||
|
||||
status = YIELDED
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
exports.directory = function* (dirname, version, parentURL, opts = {}) {
|
||||
const {
|
||||
host = null, // Shorthand for single host resolution
|
||||
hosts = host !== null ? [host] : [],
|
||||
builtins = [],
|
||||
matchedConditions = []
|
||||
} = opts
|
||||
|
||||
let directoryURL
|
||||
|
||||
if (dirname[dirname.length - 1] === '/' || dirname[dirname.length - 1] === '\\') {
|
||||
directoryURL = new URL(dirname, parentURL)
|
||||
} else {
|
||||
directoryURL = new URL(dirname + '/', parentURL)
|
||||
}
|
||||
|
||||
const unversioned = version === null
|
||||
|
||||
let name = null
|
||||
|
||||
const info = yield { package: new URL('package.json', directoryURL) }
|
||||
|
||||
if (info) {
|
||||
if (typeof info.name === 'string' && info.name !== '') {
|
||||
if (info.name.includes('__')) {
|
||||
throw errors.INVALID_PACKAGE_NAME(`Package name '${info.name}' is invalid`)
|
||||
}
|
||||
|
||||
name = info.name.replace(/\//g, '__').replace(/^@/, '')
|
||||
} else {
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
if (typeof info.version === 'string' && info.version !== '') {
|
||||
if (version !== null && info.version !== version) return UNRESOLVED
|
||||
|
||||
version = info.version
|
||||
}
|
||||
} else {
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
let status
|
||||
|
||||
status = yield* resolve.builtinTarget(name, version, builtins, opts)
|
||||
|
||||
if (status) return status
|
||||
|
||||
for (const prebuildsURL of exports.lookupPrebuildsScope(directoryURL, opts)) {
|
||||
status = UNRESOLVED
|
||||
|
||||
for (const host of hosts) {
|
||||
const conditions = host.split('-')
|
||||
|
||||
const universal = supportsUniversalPrebuilds(host)
|
||||
? conditions.with(1, 'universal').join('-')
|
||||
: null
|
||||
|
||||
matchedConditions.push(...conditions)
|
||||
|
||||
if (version !== null) {
|
||||
status |= yield* exports.file(host + '/' + name + '@' + version, prebuildsURL, opts)
|
||||
|
||||
if (universal) {
|
||||
status |= yield* exports.file(universal + '/' + name + '@' + version, prebuildsURL, opts)
|
||||
}
|
||||
}
|
||||
|
||||
if (unversioned) {
|
||||
status |= yield* exports.file(host + '/' + name, prebuildsURL, opts)
|
||||
|
||||
if (universal) {
|
||||
status |= yield* exports.file(universal + '/' + name, prebuildsURL, opts)
|
||||
}
|
||||
}
|
||||
|
||||
for (const _ of conditions) matchedConditions.pop()
|
||||
}
|
||||
|
||||
if (status === RESOLVED) return status
|
||||
}
|
||||
|
||||
return yield* exports.linked(name, version, opts)
|
||||
}
|
||||
|
||||
exports.linked = function* (name, version = null, opts = {}) {
|
||||
const {
|
||||
linked = true,
|
||||
host = null, // Shorthand for single host resolution
|
||||
hosts = host !== null ? [host] : [],
|
||||
matchedConditions = []
|
||||
} = opts
|
||||
|
||||
if (linked === false || hosts.length === 0) return UNRESOLVED
|
||||
|
||||
let status = UNRESOLVED
|
||||
|
||||
for (const host of hosts) {
|
||||
const [platform = null] = host.split('-', 1)
|
||||
|
||||
if (platform === null) continue
|
||||
|
||||
matchedConditions.push(platform)
|
||||
|
||||
status |= yield* platformArtefact(name, version, platform, opts)
|
||||
|
||||
matchedConditions.pop()
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
function* platformArtefact(name, version = null, platform, opts = {}) {
|
||||
const { linkedProtocol = 'linked:' } = opts
|
||||
|
||||
if (platform === 'darwin' || platform === 'ios') {
|
||||
if (version !== null) {
|
||||
if (
|
||||
yield {
|
||||
resolution: new URL(`${linkedProtocol}${name}.${version}.framework/${name}.${version}`)
|
||||
}
|
||||
) {
|
||||
return RESOLVED
|
||||
}
|
||||
|
||||
if (platform === 'darwin') {
|
||||
if (
|
||||
yield {
|
||||
resolution: new URL(`${linkedProtocol}lib${name}.${version}.dylib`)
|
||||
}
|
||||
) {
|
||||
return RESOLVED
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
yield {
|
||||
resolution: new URL(`${linkedProtocol}${name}.framework/${name}`)
|
||||
}
|
||||
) {
|
||||
return RESOLVED
|
||||
}
|
||||
|
||||
if (platform === 'darwin') {
|
||||
if (
|
||||
yield {
|
||||
resolution: new URL(`${linkedProtocol}lib${name}.dylib`)
|
||||
}
|
||||
) {
|
||||
return RESOLVED
|
||||
}
|
||||
}
|
||||
|
||||
return YIELDED
|
||||
}
|
||||
|
||||
if (platform === 'linux' || platform === 'android') {
|
||||
if (version !== null) {
|
||||
if (
|
||||
yield {
|
||||
resolution: new URL(`${linkedProtocol}lib${name}.${version}.so`)
|
||||
}
|
||||
) {
|
||||
return RESOLVED
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
yield {
|
||||
resolution: new URL(`${linkedProtocol}lib${name}.so`)
|
||||
}
|
||||
) {
|
||||
return RESOLVED
|
||||
}
|
||||
|
||||
return YIELDED
|
||||
}
|
||||
|
||||
if (platform === 'win32') {
|
||||
if (version !== null) {
|
||||
if (
|
||||
yield {
|
||||
resolution: new URL(`${linkedProtocol}${name}-${version}.dll`)
|
||||
}
|
||||
) {
|
||||
return RESOLVED
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
yield {
|
||||
resolution: new URL(`${linkedProtocol}${name}.dll`)
|
||||
}
|
||||
) {
|
||||
return RESOLVED
|
||||
}
|
||||
}
|
||||
|
||||
return UNRESOLVED
|
||||
}
|
||||
|
||||
exports.isWindowsDriveLetter = resolve.isWindowsDriveLetter
|
||||
|
||||
exports.startsWithWindowsDriveLetter = resolve.startsWithWindowsDriveLetter
|
||||
|
||||
function supportsUniversalPrebuilds(host) {
|
||||
return (
|
||||
host === 'darwin-arm64' ||
|
||||
host === 'darwin-x64' ||
|
||||
host === 'ios-arm64-simulator' ||
|
||||
host === 'ios-x64-simulator'
|
||||
)
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
declare class AddonResolveError extends Error {
|
||||
readonly code: string
|
||||
|
||||
static INVALID_ADDON_SPECIFIER(msg: string): AddonResolveError
|
||||
static INVALID_PACKAGE_NAME(msg: string): AddonResolveError
|
||||
}
|
||||
|
||||
export = AddonResolveError
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
module.exports = class AddonResolveError extends Error {
|
||||
constructor(msg, code, fn = AddonResolveError) {
|
||||
super(`${code}: ${msg}`)
|
||||
this.code = code
|
||||
|
||||
if (Error.captureStackTrace) {
|
||||
Error.captureStackTrace(this, fn)
|
||||
}
|
||||
}
|
||||
|
||||
get name() {
|
||||
return 'AddonResolveError'
|
||||
}
|
||||
|
||||
static INVALID_ADDON_SPECIFIER(msg) {
|
||||
return new AddonResolveError(
|
||||
msg,
|
||||
'INVALID_ADDON_SPECIFIER',
|
||||
AddonResolveError.INVALID_ADDON_SPECIFIER
|
||||
)
|
||||
}
|
||||
|
||||
static INVALID_PACKAGE_NAME(msg) {
|
||||
return new AddonResolveError(
|
||||
msg,
|
||||
'INVALID_PACKAGE_NAME',
|
||||
AddonResolveError.INVALID_PACKAGE_NAME
|
||||
)
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"name": "bare-addon-resolve",
|
||||
"version": "1.10.0",
|
||||
"description": "Low-level addon resolution algorithm for Bare",
|
||||
"exports": {
|
||||
"./package": "./package.json",
|
||||
".": {
|
||||
"types": "./index.d.ts",
|
||||
"default": "./index.js"
|
||||
},
|
||||
"./errors": {
|
||||
"types": "./lib/errors.d.ts",
|
||||
"default": "./lib/errors.js"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts",
|
||||
"lib"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "prettier . --check && bare test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/holepunchto/bare-addon-resolve.git"
|
||||
},
|
||||
"author": "Holepunch",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": {
|
||||
"url": "https://github.com/holepunchto/bare-addon-resolve/issues"
|
||||
},
|
||||
"homepage": "https://github.com/holepunchto/bare-addon-resolve#readme",
|
||||
"dependencies": {
|
||||
"bare-module-resolve": "^1.10.0",
|
||||
"bare-semver": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bare-url": "^2.1.3",
|
||||
"brittle": "^3.2.1",
|
||||
"prettier": "^3.3.3",
|
||||
"prettier-config-holepunch": "^2.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bare-url": "*"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bare-url": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user