bare-addon-resolve
Advanced tools
Comparing version 1.7.0 to 1.7.1
196
index.js
const resolve = require('bare-module-resolve') | ||
const errors = require('./lib/errors') | ||
const semver = require('./lib/semver') | ||
module.exports = exports = function resolve (specifier, parentURL, opts, readPackage) { | ||
module.exports = exports = function resolve( | ||
specifier, | ||
parentURL, | ||
opts, | ||
readPackage | ||
) { | ||
if (typeof opts === 'function') { | ||
@@ -13,3 +19,3 @@ readPackage = opts | ||
return { | ||
* [Symbol.iterator] () { | ||
*[Symbol.iterator]() { | ||
const generator = exports.addon(specifier, parentURL, opts) | ||
@@ -33,3 +39,3 @@ | ||
async * [Symbol.asyncIterator] () { | ||
async *[Symbol.asyncIterator]() { | ||
const generator = exports.addon(specifier, parentURL, opts) | ||
@@ -55,7 +61,7 @@ | ||
function defaultReadPackage () { | ||
function defaultReadPackage() { | ||
return null | ||
} | ||
exports.addon = function * (specifier, parentURL, opts = {}) { | ||
exports.addon = function* (specifier, parentURL, opts = {}) { | ||
const { resolutions = null } = opts | ||
@@ -68,3 +74,3 @@ | ||
if (resolutions) { | ||
if (yield * resolve.preresolved(specifier, resolutions, parentURL, opts)) { | ||
if (yield* resolve.preresolved(specifier, resolutions, parentURL, opts)) { | ||
return true | ||
@@ -74,3 +80,3 @@ } | ||
if (yield * exports.url(specifier, parentURL, opts)) { | ||
if (yield* exports.url(specifier, parentURL, opts)) { | ||
return true | ||
@@ -81,17 +87,26 @@ } | ||
const i = specifier.lastIndexOf('@') | ||
const match = specifier.match(semver) | ||
if (i > 0) { | ||
version = specifier.substring(i + 1) | ||
specifier = specifier.substring(0, i) | ||
if (match !== null) { | ||
version = match[0].substring(1) | ||
specifier = specifier.substring(0, specifier.length - (version.length + 1)) | ||
} | ||
if (specifier === '.' || specifier === '..' || specifier[0] === '/' || specifier[0] === '\\' || specifier.startsWith('./') || specifier.startsWith('.\\') || specifier.startsWith('../') || specifier.startsWith('..\\')) { | ||
return yield * exports.directory(specifier, version, parentURL, opts) | ||
if ( | ||
specifier === '.' || | ||
specifier === '..' || | ||
specifier[0] === '/' || | ||
specifier[0] === '\\' || | ||
specifier.startsWith('./') || | ||
specifier.startsWith('.\\') || | ||
specifier.startsWith('../') || | ||
specifier.startsWith('..\\') | ||
) { | ||
return yield* exports.directory(specifier, version, parentURL, opts) | ||
} | ||
return yield * exports.package(specifier, version, parentURL, opts) | ||
return yield* exports.package(specifier, version, parentURL, opts) | ||
} | ||
exports.url = function * (url, parentURL, opts = {}) { | ||
exports.url = function* (url, parentURL, opts = {}) { | ||
let resolution | ||
@@ -109,5 +124,12 @@ try { | ||
exports.package = function * (packageSpecifier, packageVersion, parentURL, opts = {}) { | ||
exports.package = function* ( | ||
packageSpecifier, | ||
packageVersion, | ||
parentURL, | ||
opts = {} | ||
) { | ||
if (packageSpecifier === '') { | ||
throw errors.INVALID_ADDON_SPECIFIER(`Addon specifier '${packageSpecifier}' is not a valid package name`) | ||
throw errors.INVALID_ADDON_SPECIFIER( | ||
`Addon specifier '${packageSpecifier}' is not a valid package name` | ||
) | ||
} | ||
@@ -121,3 +143,5 @@ | ||
if (!packageSpecifier.includes('/')) { | ||
throw errors.INVALID_ADDON_SPECIFIER(`Addon specifier '${packageSpecifier}' is not a valid package name`) | ||
throw errors.INVALID_ADDON_SPECIFIER( | ||
`Addon specifier '${packageSpecifier}' is not a valid package name` | ||
) | ||
} | ||
@@ -128,4 +152,10 @@ | ||
if (packageName[0] === '.' || packageName.includes('\\') || packageName.includes('%')) { | ||
throw errors.INVALID_ADDON_SPECIFIER(`Addon specifier '${packageSpecifier}' is not a valid package name`) | ||
if ( | ||
packageName[0] === '.' || | ||
packageName.includes('\\') || | ||
packageName.includes('%') | ||
) { | ||
throw errors.INVALID_ADDON_SPECIFIER( | ||
`Addon specifier '${packageSpecifier}' is not a valid package name` | ||
) | ||
} | ||
@@ -135,3 +165,11 @@ | ||
if (yield * exports.packageSelf(packageName, packageSubpath, packageVersion, parentURL, opts)) { | ||
if ( | ||
yield* exports.packageSelf( | ||
packageName, | ||
packageSubpath, | ||
packageVersion, | ||
parentURL, | ||
opts | ||
) | ||
) { | ||
return true | ||
@@ -145,3 +183,6 @@ } | ||
parentURL.pathname = parentURL.pathname.substring(0, parentURL.pathname.lastIndexOf('/')) | ||
parentURL.pathname = parentURL.pathname.substring( | ||
0, | ||
parentURL.pathname.lastIndexOf('/') | ||
) | ||
@@ -151,3 +192,8 @@ const info = yield { package: new URL('package.json', packageURL) } | ||
if (info) { | ||
return yield * exports.directory(packageSubpath, packageVersion, packageURL, opts) | ||
return yield* exports.directory( | ||
packageSubpath, | ||
packageVersion, | ||
packageURL, | ||
opts | ||
) | ||
} | ||
@@ -159,3 +205,9 @@ } while (parentURL.pathname !== '' && parentURL.pathname !== '/') | ||
exports.packageSelf = function * (packageName, packageSubpath, packageVersion, parentURL, opts = {}) { | ||
exports.packageSelf = function* ( | ||
packageName, | ||
packageSubpath, | ||
packageVersion, | ||
parentURL, | ||
opts = {} | ||
) { | ||
for (const packageURL of resolve.lookupPackageScope(parentURL, opts)) { | ||
@@ -166,3 +218,8 @@ const info = yield { package: packageURL } | ||
if (info.name === packageName) { | ||
return yield * exports.directory(packageSubpath, packageVersion, packageURL, opts) | ||
return yield* exports.directory( | ||
packageSubpath, | ||
packageVersion, | ||
packageURL, | ||
opts | ||
) | ||
} | ||
@@ -177,7 +234,12 @@ | ||
exports.lookupPrebuildsScope = function * lookupPrebuildsScope (url, opts = {}) { | ||
exports.lookupPrebuildsScope = function* lookupPrebuildsScope(url, opts = {}) { | ||
const { resolutions = null, host = null } = opts | ||
if (resolutions) { | ||
for (const { resolution } of resolve.preresolved('#prebuilds', resolutions, url, opts)) { | ||
for (const { resolution } of resolve.preresolved( | ||
'#prebuilds', | ||
resolutions, | ||
url, | ||
opts | ||
)) { | ||
if (resolution) return yield resolution | ||
@@ -194,11 +256,20 @@ } | ||
scopeURL.pathname = scopeURL.pathname.substring(0, scopeURL.pathname.lastIndexOf('/')) | ||
scopeURL.pathname = scopeURL.pathname.substring( | ||
0, | ||
scopeURL.pathname.lastIndexOf('/') | ||
) | ||
if (scopeURL.pathname.length === 3 && exports.isWindowsDriveLetter(scopeURL.pathname.substring(1))) break | ||
if ( | ||
scopeURL.pathname.length === 3 && | ||
exports.isWindowsDriveLetter(scopeURL.pathname.substring(1)) | ||
) | ||
break | ||
} while (scopeURL.pathname !== '' && scopeURL.pathname !== '/') | ||
} | ||
exports.file = function * (filename, parentURL, opts = {}) { | ||
exports.file = function* (filename, parentURL, opts = {}) { | ||
if (parentURL.protocol === 'file:' && /%2f|%5c/i.test(filename)) { | ||
throw errors.INVALID_ADDON_SPECIFIER(`Addon specifier '${filename}' is invalid`) | ||
throw errors.INVALID_ADDON_SPECIFIER( | ||
`Addon specifier '${filename}' is invalid` | ||
) | ||
} | ||
@@ -215,3 +286,3 @@ | ||
exports.directory = function * (dirname, version, parentURL, opts = {}) { | ||
exports.directory = function* (dirname, version, parentURL, opts = {}) { | ||
const { resolutions = null, builtins = [] } = opts | ||
@@ -221,3 +292,6 @@ | ||
if (dirname[dirname.length - 1] === '/' || dirname[dirname.length - 1] === '\\') { | ||
if ( | ||
dirname[dirname.length - 1] === '/' || | ||
dirname[dirname.length - 1] === '\\' | ||
) { | ||
directoryURL = new URL(dirname, parentURL) | ||
@@ -231,3 +305,5 @@ } else { | ||
if (resolutions) { | ||
if (yield * resolve.preresolved('bare:addon', resolutions, directoryURL, opts)) { | ||
if ( | ||
yield* resolve.preresolved('bare:addon', resolutions, directoryURL, opts) | ||
) { | ||
return true | ||
@@ -259,3 +335,3 @@ } | ||
if (yield * resolve.builtinTarget(name, version, builtins, opts)) { | ||
if (yield* resolve.builtinTarget(name, version, builtins, opts)) { | ||
return true | ||
@@ -268,3 +344,3 @@ } | ||
if (version !== null) { | ||
if (yield * exports.file(name + '@' + version, prebuildsURL, opts)) { | ||
if (yield* exports.file(name + '@' + version, prebuildsURL, opts)) { | ||
yielded = true | ||
@@ -275,3 +351,3 @@ } | ||
if (unversioned) { | ||
if (yield * exports.file(name, prebuildsURL, opts)) { | ||
if (yield* exports.file(name, prebuildsURL, opts)) { | ||
yielded = true | ||
@@ -282,3 +358,3 @@ } | ||
if (yield * exports.linked(name, version, opts)) { | ||
if (yield* exports.linked(name, version, opts)) { | ||
yielded = true | ||
@@ -290,3 +366,3 @@ } | ||
exports.linked = function * (name, version = null, opts = {}) { | ||
exports.linked = function* (name, version = null, opts = {}) { | ||
const { linked = true, linkedProtocol = 'linked:', host = null } = opts | ||
@@ -298,4 +374,19 @@ | ||
if (version !== null) { | ||
yield { resolution: new URL(linkedProtocol + 'lib' + name + '.' + version + '.dylib') } | ||
yield { resolution: new URL(linkedProtocol + name + '.' + version + '.framework/' + name + '.' + version) } | ||
yield { | ||
resolution: new URL( | ||
linkedProtocol + 'lib' + name + '.' + version + '.dylib' | ||
) | ||
} | ||
yield { | ||
resolution: new URL( | ||
linkedProtocol + | ||
name + | ||
'.' + | ||
version + | ||
'.framework/' + | ||
name + | ||
'.' + | ||
version | ||
) | ||
} | ||
} | ||
@@ -311,3 +402,14 @@ | ||
if (version !== null) { | ||
yield { resolution: new URL(linkedProtocol + name + '.' + version + '.framework/' + name + '.' + version) } | ||
yield { | ||
resolution: new URL( | ||
linkedProtocol + | ||
name + | ||
'.' + | ||
version + | ||
'.framework/' + | ||
name + | ||
'.' + | ||
version | ||
) | ||
} | ||
} | ||
@@ -322,3 +424,7 @@ | ||
if (version !== null) { | ||
yield { resolution: new URL(linkedProtocol + 'lib' + name + '.' + version + '.so') } | ||
yield { | ||
resolution: new URL( | ||
linkedProtocol + 'lib' + name + '.' + version + '.so' | ||
) | ||
} | ||
} | ||
@@ -333,3 +439,5 @@ | ||
if (version !== null) { | ||
yield { resolution: new URL(linkedProtocol + name + '-' + version + '.dll') } | ||
yield { | ||
resolution: new URL(linkedProtocol + name + '-' + version + '.dll') | ||
} | ||
} | ||
@@ -336,0 +444,0 @@ |
module.exports = class AddonResolveError extends Error { | ||
constructor (msg, code, fn = AddonResolveError) { | ||
constructor(msg, code, fn = AddonResolveError) { | ||
super(`${code}: ${msg}`) | ||
@@ -11,9 +11,13 @@ this.code = code | ||
get name () { | ||
get name() { | ||
return 'AddonResolveError' | ||
} | ||
static INVALID_ADDON_SPECIFIER (msg) { | ||
return new AddonResolveError(msg, 'INVALID_ADDON_SPECIFIER', AddonResolveError.INVALID_ADDON_SPECIFIER) | ||
static INVALID_ADDON_SPECIFIER(msg) { | ||
return new AddonResolveError( | ||
msg, | ||
'INVALID_ADDON_SPECIFIER', | ||
AddonResolveError.INVALID_ADDON_SPECIFIER | ||
) | ||
} | ||
} |
{ | ||
"name": "bare-addon-resolve", | ||
"version": "1.7.0", | ||
"version": "1.7.1", | ||
"description": "Low-level addon resolution algorithm for Bare", | ||
@@ -15,3 +15,3 @@ "exports": { | ||
"scripts": { | ||
"test": "standard && bare test.js" | ||
"test": "prettier . --check && bare test.js" | ||
}, | ||
@@ -33,4 +33,5 @@ "repository": { | ||
"brittle": "^3.2.1", | ||
"standard": "^17.0.0" | ||
"prettier": "^3.3.3", | ||
"prettier-config-standard": "^7.0.0" | ||
} | ||
} |
@@ -13,10 +13,14 @@ # bare-addon-resolve | ||
``` js | ||
```js | ||
const resolve = require('bare-addon-resolve') | ||
function readPackage (url) { | ||
function readPackage(url) { | ||
// Read and parse `url` if it exists, otherwise `null` | ||
} | ||
for (const resolution of resolve('./addon', new URL('file:///directory/'), readPackage)) { | ||
for (const resolution of resolve( | ||
'./addon', | ||
new URL('file:///directory/'), | ||
readPackage | ||
)) { | ||
console.log(resolution) | ||
@@ -28,10 +32,14 @@ } | ||
``` js | ||
```js | ||
const resolve = require('bare-addon-resolve') | ||
async function readPackage (url) { | ||
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)) { | ||
for await (const resolution of resolve( | ||
'./addon', | ||
new URL('file:///directory/'), | ||
readPackage | ||
)) { | ||
console.log(resolution) | ||
@@ -38,0 +46,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26671
6
364
134
3