rulesync 13.0.0 → 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cli/index.cjs +913 -24
- package/dist/cli/index.js +913 -24
- package/dist/cli/index.js.map +1 -1
- package/dist/{import-CMSHVMqU.js → import-CSnC5vl7.js} +194 -14
- package/dist/import-CSnC5vl7.js.map +1 -0
- package/dist/{import-nbmxtKYi.cjs → import-Dh9PCQKq.cjs} +212 -26
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +13 -13
- package/dist/import-CMSHVMqU.js.map +0 -1
package/dist/cli/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const require_import = require("../import-
|
|
2
|
+
const require_import = require("../import-Dh9PCQKq.cjs");
|
|
3
3
|
let commander = require("commander");
|
|
4
4
|
let zod_mini = require("zod/mini");
|
|
5
5
|
let node_path = require("node:path");
|
|
@@ -14,6 +14,7 @@ let es_toolkit_promise = require("es-toolkit/promise");
|
|
|
14
14
|
let _octokit_request_error = require("@octokit/request-error");
|
|
15
15
|
let _octokit_rest = require("@octokit/rest");
|
|
16
16
|
let node_child_process = require("node:child_process");
|
|
17
|
+
let node_zlib = require("node:zlib");
|
|
17
18
|
let fastmcp = require("fastmcp");
|
|
18
19
|
let node_fs = require("node:fs");
|
|
19
20
|
node_fs = require_import.__toESM(node_fs, 1);
|
|
@@ -1242,6 +1243,7 @@ const DERIVED_PATHS_NOT_GITIGNORED = /* @__PURE__ */ new Set([
|
|
|
1242
1243
|
"**/.grok/config.toml",
|
|
1243
1244
|
"**/.vibe/config.toml",
|
|
1244
1245
|
"**/reasonix.toml",
|
|
1246
|
+
"**/.vscode/settings.json",
|
|
1245
1247
|
"**/.zed/settings.json",
|
|
1246
1248
|
"**/kilo.json",
|
|
1247
1249
|
"**/kilo.jsonc",
|
|
@@ -3548,6 +3550,516 @@ async function walkDirectory(dir, outputRoot, depth = 0, ctx = {
|
|
|
3548
3550
|
}
|
|
3549
3551
|
return results;
|
|
3550
3552
|
}
|
|
3553
|
+
const DEFAULT_NPM_TOKEN_ENV = "NPM_TOKEN";
|
|
3554
|
+
/** Abbreviated packument media type (install metadata only). */
|
|
3555
|
+
const PACKUMENT_ACCEPT_HEADER = "application/vnd.npm.install-v1+json";
|
|
3556
|
+
/** Timeout for registry HTTP requests (60 seconds). */
|
|
3557
|
+
const NPM_FETCH_TIMEOUT_MS = 6e4;
|
|
3558
|
+
/** Maximum accepted tarball size (compressed), aligned with the extraction cap. */
|
|
3559
|
+
const MAX_TARBALL_SIZE = 100 * 1024 * 1024;
|
|
3560
|
+
/**
|
|
3561
|
+
* npm package name rules (scoped or unscoped, URL-safe characters only).
|
|
3562
|
+
* This also guards against URL path injection into registry requests.
|
|
3563
|
+
*/
|
|
3564
|
+
const NPM_PACKAGE_NAME_REGEX = /^(@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/i;
|
|
3565
|
+
const MAX_NPM_PACKAGE_NAME_LENGTH = 214;
|
|
3566
|
+
const INTEGRITY_ALGORITHM_PREFERENCE = [
|
|
3567
|
+
"sha512",
|
|
3568
|
+
"sha384",
|
|
3569
|
+
"sha256",
|
|
3570
|
+
"sha1"
|
|
3571
|
+
];
|
|
3572
|
+
var NpmClientError = class extends Error {
|
|
3573
|
+
statusCode;
|
|
3574
|
+
constructor(message, options) {
|
|
3575
|
+
super(message, { cause: options?.cause });
|
|
3576
|
+
this.name = "NpmClientError";
|
|
3577
|
+
this.statusCode = options?.statusCode;
|
|
3578
|
+
}
|
|
3579
|
+
};
|
|
3580
|
+
function validateNpmPackageName(name) {
|
|
3581
|
+
if (name.length > MAX_NPM_PACKAGE_NAME_LENGTH || !NPM_PACKAGE_NAME_REGEX.test(name)) throw new NpmClientError(`Invalid npm package name: "${name}". Expected "name" or "@scope/name".`);
|
|
3582
|
+
}
|
|
3583
|
+
function validateNpmRegistryUrl(url, options) {
|
|
3584
|
+
const ctrl = require_import.findControlCharacter(url);
|
|
3585
|
+
if (ctrl) throw new NpmClientError(`Registry URL contains control character ${ctrl.hex} at position ${ctrl.position}`);
|
|
3586
|
+
if (!url.startsWith("https://") && !url.startsWith("http://")) throw new NpmClientError(`Unsupported registry URL: "${url}". Use https:// (or http://).`);
|
|
3587
|
+
if (url.startsWith("http://")) options?.logger?.warn(`Registry URL "${url}" uses an unencrypted protocol. Consider using https:// instead.`);
|
|
3588
|
+
}
|
|
3589
|
+
/**
|
|
3590
|
+
* Resolve the registry token from the environment. When `tokenEnv` is set it
|
|
3591
|
+
* must name an existing environment variable; otherwise `NPM_TOKEN` is used
|
|
3592
|
+
* when present. The token value itself is never logged.
|
|
3593
|
+
*/
|
|
3594
|
+
function resolveNpmToken(params) {
|
|
3595
|
+
const { tokenEnv } = params;
|
|
3596
|
+
if (tokenEnv !== void 0) {
|
|
3597
|
+
const value = process.env[tokenEnv];
|
|
3598
|
+
if (value === void 0 || value === "") throw new NpmClientError(`Environment variable "${tokenEnv}" (from tokenEnv) is not set. Export it or remove the tokenEnv field.`);
|
|
3599
|
+
return value;
|
|
3600
|
+
}
|
|
3601
|
+
const fallback = process.env[DEFAULT_NPM_TOKEN_ENV];
|
|
3602
|
+
return fallback === void 0 || fallback === "" ? void 0 : fallback;
|
|
3603
|
+
}
|
|
3604
|
+
/** Build the packument URL for a (possibly scoped) package on a registry. */
|
|
3605
|
+
function buildPackumentUrl(params) {
|
|
3606
|
+
const { registryUrl, packageName } = params;
|
|
3607
|
+
validateNpmPackageName(packageName);
|
|
3608
|
+
const base = registryUrl.endsWith("/") ? registryUrl : `${registryUrl}/`;
|
|
3609
|
+
const encodedName = packageName.replaceAll("/", "%2F");
|
|
3610
|
+
return new URL(encodedName, base).toString();
|
|
3611
|
+
}
|
|
3612
|
+
async function fetchWithTimeout(url, headers) {
|
|
3613
|
+
try {
|
|
3614
|
+
return await fetch(url, {
|
|
3615
|
+
headers,
|
|
3616
|
+
redirect: "follow",
|
|
3617
|
+
signal: AbortSignal.timeout(NPM_FETCH_TIMEOUT_MS)
|
|
3618
|
+
});
|
|
3619
|
+
} catch (error) {
|
|
3620
|
+
throw new NpmClientError(`Network error while requesting ${url}`, { cause: error });
|
|
3621
|
+
}
|
|
3622
|
+
}
|
|
3623
|
+
/**
|
|
3624
|
+
* Fetch the (abbreviated) packument for a package from a registry.
|
|
3625
|
+
*/
|
|
3626
|
+
async function fetchPackument(params) {
|
|
3627
|
+
const { registryUrl, packageName, token } = params;
|
|
3628
|
+
validateNpmPackageName(packageName);
|
|
3629
|
+
const url = buildPackumentUrl({
|
|
3630
|
+
registryUrl,
|
|
3631
|
+
packageName
|
|
3632
|
+
});
|
|
3633
|
+
const headers = { Accept: PACKUMENT_ACCEPT_HEADER };
|
|
3634
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
3635
|
+
const response = await fetchWithTimeout(url, headers);
|
|
3636
|
+
if (!response.ok) throw new NpmClientError(`Failed to fetch package metadata for "${packageName}" from ${registryUrl}: HTTP ${response.status}`, { statusCode: response.status });
|
|
3637
|
+
try {
|
|
3638
|
+
return await response.json();
|
|
3639
|
+
} catch (error) {
|
|
3640
|
+
throw new NpmClientError(`Failed to parse package metadata for "${packageName}" from ${registryUrl}`, { cause: error });
|
|
3641
|
+
}
|
|
3642
|
+
}
|
|
3643
|
+
/**
|
|
3644
|
+
* Resolve a requested version or dist-tag against a packument. Only exact
|
|
3645
|
+
* versions and dist-tags are supported — semver ranges are intentionally out
|
|
3646
|
+
* of scope (no semver dependency).
|
|
3647
|
+
*/
|
|
3648
|
+
function resolvePackumentVersion(params) {
|
|
3649
|
+
const { packument, packageName, requested } = params;
|
|
3650
|
+
const versions = packument.versions ?? {};
|
|
3651
|
+
if (Object.prototype.hasOwnProperty.call(versions, requested)) return requested;
|
|
3652
|
+
const distTags = packument["dist-tags"] ?? {};
|
|
3653
|
+
const tagged = Object.prototype.hasOwnProperty.call(distTags, requested) ? distTags[requested] : void 0;
|
|
3654
|
+
if (tagged !== void 0 && Object.prototype.hasOwnProperty.call(versions, tagged)) return tagged;
|
|
3655
|
+
throw new NpmClientError(`Could not resolve "${packageName}@${requested}": not an exact published version or dist-tag. Note: semver ranges are not supported by the npm transport.`);
|
|
3656
|
+
}
|
|
3657
|
+
/** Get the dist metadata (tarball URL, integrity) for a resolved version. */
|
|
3658
|
+
function getPackumentVersionDist(params) {
|
|
3659
|
+
const { packument, packageName, version } = params;
|
|
3660
|
+
const versions = packument.versions ?? {};
|
|
3661
|
+
const dist = (Object.prototype.hasOwnProperty.call(versions, version) ? versions[version] : void 0)?.dist;
|
|
3662
|
+
if (!dist?.tarball) throw new NpmClientError(`Registry metadata for "${packageName}@${version}" is missing the dist.tarball URL.`);
|
|
3663
|
+
return dist;
|
|
3664
|
+
}
|
|
3665
|
+
/**
|
|
3666
|
+
* Download a package tarball. The Authorization header is only attached when
|
|
3667
|
+
* the tarball is hosted on the same origin (scheme + host) as the registry,
|
|
3668
|
+
* so the token never leaks to third-party CDNs or plaintext downgrades.
|
|
3669
|
+
*/
|
|
3670
|
+
async function fetchTarball(params) {
|
|
3671
|
+
const { tarballUrl, registryUrl, token } = params;
|
|
3672
|
+
const maxSize = params.maxSize ?? MAX_TARBALL_SIZE;
|
|
3673
|
+
if (!tarballUrl.startsWith("https://") && !tarballUrl.startsWith("http://")) throw new NpmClientError(`Unsupported tarball URL: "${tarballUrl}". Use https:// (or http://).`);
|
|
3674
|
+
const headers = {};
|
|
3675
|
+
if (token && isSameOrigin(tarballUrl, registryUrl)) headers.Authorization = `Bearer ${token}`;
|
|
3676
|
+
const response = await fetchWithTimeout(tarballUrl, headers);
|
|
3677
|
+
if (!response.ok) throw new NpmClientError(`Failed to download tarball ${tarballUrl}: HTTP ${response.status}`, { statusCode: response.status });
|
|
3678
|
+
const contentLength = Number.parseInt(response.headers.get("content-length") ?? "", 10);
|
|
3679
|
+
if (Number.isFinite(contentLength) && contentLength > maxSize) throw new NpmClientError(oversizedTarballMessage(tarballUrl, maxSize));
|
|
3680
|
+
return await readBodyWithLimit({
|
|
3681
|
+
response,
|
|
3682
|
+
tarballUrl,
|
|
3683
|
+
maxSize
|
|
3684
|
+
});
|
|
3685
|
+
}
|
|
3686
|
+
function oversizedTarballMessage(tarballUrl, maxSize) {
|
|
3687
|
+
return `Tarball ${tarballUrl} exceeds max size of ${maxSize / 1024 / 1024}MB.`;
|
|
3688
|
+
}
|
|
3689
|
+
/**
|
|
3690
|
+
* Read a response body incrementally, aborting as soon as the size cap is
|
|
3691
|
+
* exceeded. content-length can be absent or forged, so the streaming check is
|
|
3692
|
+
* the actual enforcement of the cap.
|
|
3693
|
+
*/
|
|
3694
|
+
async function readBodyWithLimit(params) {
|
|
3695
|
+
const { response, tarballUrl, maxSize } = params;
|
|
3696
|
+
const reader = response.body?.getReader();
|
|
3697
|
+
if (!reader) {
|
|
3698
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
3699
|
+
if (arrayBuffer.byteLength > maxSize) throw new NpmClientError(oversizedTarballMessage(tarballUrl, maxSize));
|
|
3700
|
+
return Buffer.from(arrayBuffer);
|
|
3701
|
+
}
|
|
3702
|
+
const chunks = [];
|
|
3703
|
+
let totalBytes = 0;
|
|
3704
|
+
while (true) {
|
|
3705
|
+
const { done, value } = await reader.read();
|
|
3706
|
+
if (done) break;
|
|
3707
|
+
totalBytes += value.byteLength;
|
|
3708
|
+
if (totalBytes > maxSize) {
|
|
3709
|
+
await reader.cancel();
|
|
3710
|
+
throw new NpmClientError(oversizedTarballMessage(tarballUrl, maxSize));
|
|
3711
|
+
}
|
|
3712
|
+
chunks.push(Buffer.from(value));
|
|
3713
|
+
}
|
|
3714
|
+
return Buffer.concat(chunks);
|
|
3715
|
+
}
|
|
3716
|
+
function isSameOrigin(urlA, urlB) {
|
|
3717
|
+
try {
|
|
3718
|
+
return new URL(urlA).origin === new URL(urlB).origin;
|
|
3719
|
+
} catch {
|
|
3720
|
+
return false;
|
|
3721
|
+
}
|
|
3722
|
+
}
|
|
3723
|
+
/**
|
|
3724
|
+
* Convert a hex sha1 shasum to the SRI form used by `verifyTarballIntegrity`.
|
|
3725
|
+
* Rejects malformed shasum values so a broken value is never recorded in the
|
|
3726
|
+
* lockfile as a seemingly valid SRI string.
|
|
3727
|
+
*/
|
|
3728
|
+
function shasumToSri(shasum) {
|
|
3729
|
+
if (!/^[0-9a-f]{40}$/i.test(shasum)) throw new NpmClientError(`Malformed sha1 shasum in registry metadata: "${shasum}"`);
|
|
3730
|
+
return `sha1-${Buffer.from(shasum, "hex").toString("base64")}`;
|
|
3731
|
+
}
|
|
3732
|
+
/**
|
|
3733
|
+
* Verify a downloaded tarball against registry integrity metadata.
|
|
3734
|
+
* Prefers the strongest supported algorithm in the SRI `integrity` string and
|
|
3735
|
+
* falls back to the legacy sha1 `shasum`. An `integrity` string that is
|
|
3736
|
+
* present but cannot be parsed fails closed; a warning is only logged when
|
|
3737
|
+
* the registry provides no integrity metadata at all.
|
|
3738
|
+
*/
|
|
3739
|
+
function verifyTarballIntegrity(params) {
|
|
3740
|
+
const { tarball, integrity, shasum, context, logger } = params;
|
|
3741
|
+
if (integrity !== void 0) {
|
|
3742
|
+
const sri = pickStrongestSriEntry(integrity);
|
|
3743
|
+
if (!sri) throw new NpmClientError(`Unsupported or malformed integrity metadata for ${context}. Expected an SRI string with sha512/sha384/sha256/sha1.`);
|
|
3744
|
+
const actual = (0, node_crypto.createHash)(sri.algorithm).update(tarball).digest("base64");
|
|
3745
|
+
if (actual !== sri.digest) throw new NpmClientError(`Integrity verification failed for ${context}: expected ${sri.algorithm}-${sri.digest}, got ${sri.algorithm}-${actual}. The tarball may have been tampered with.`);
|
|
3746
|
+
return;
|
|
3747
|
+
}
|
|
3748
|
+
if (shasum) {
|
|
3749
|
+
const actual = (0, node_crypto.createHash)("sha1").update(tarball).digest("hex");
|
|
3750
|
+
if (actual !== shasum.toLowerCase()) throw new NpmClientError(`Integrity verification failed for ${context}: expected sha1 ${shasum}, got ${actual}. The tarball may have been tampered with.`);
|
|
3751
|
+
return;
|
|
3752
|
+
}
|
|
3753
|
+
logger?.warn(`No integrity metadata available for ${context}; skipping tarball verification.`);
|
|
3754
|
+
}
|
|
3755
|
+
function pickStrongestSriEntry(integrity) {
|
|
3756
|
+
if (!integrity) return;
|
|
3757
|
+
const entries = integrity.split(/\s+/).map((entry) => {
|
|
3758
|
+
const separatorIndex = entry.indexOf("-");
|
|
3759
|
+
if (separatorIndex === -1) return void 0;
|
|
3760
|
+
const algorithm = entry.slice(0, separatorIndex);
|
|
3761
|
+
const digest = entry.slice(separatorIndex + 1).split("?")[0] ?? "";
|
|
3762
|
+
const known = INTEGRITY_ALGORITHM_PREFERENCE.find((a) => a === algorithm);
|
|
3763
|
+
if (!known || digest.length === 0) return void 0;
|
|
3764
|
+
return {
|
|
3765
|
+
algorithm: known,
|
|
3766
|
+
digest
|
|
3767
|
+
};
|
|
3768
|
+
}).filter((entry) => Boolean(entry));
|
|
3769
|
+
for (const algorithm of INTEGRITY_ALGORITHM_PREFERENCE) {
|
|
3770
|
+
const match = entries.find((entry) => entry.algorithm === algorithm);
|
|
3771
|
+
if (match) return match;
|
|
3772
|
+
}
|
|
3773
|
+
}
|
|
3774
|
+
/**
|
|
3775
|
+
* Log contextual hints for NpmClientError to help users troubleshoot
|
|
3776
|
+
* authentication problems without ever logging the token itself.
|
|
3777
|
+
*/
|
|
3778
|
+
function logNpmAuthHints(params) {
|
|
3779
|
+
const { error, logger } = params;
|
|
3780
|
+
if (error.statusCode === 401 || error.statusCode === 403) logger.info("Hint: The registry rejected the request. Set NPM_TOKEN (or the per-source tokenEnv variable) to a token with read access. Note: .npmrc files are not read by the npm transport.");
|
|
3781
|
+
else if (error.statusCode === 404) logger.info("Hint: Package not found. Check the package name and the registry URL. Some registries also return 404 for unauthorized requests.");
|
|
3782
|
+
}
|
|
3783
|
+
const NpmLockedSkillSchema = zod_mini.z.object({ integrity: zod_mini.z.string() });
|
|
3784
|
+
/**
|
|
3785
|
+
* Schema for a single locked npm source entry.
|
|
3786
|
+
*/
|
|
3787
|
+
const NpmLockedSourceSchema = zod_mini.z.object({
|
|
3788
|
+
registry: (0, zod_mini.optional)(zod_mini.z.string()),
|
|
3789
|
+
requestedVersion: (0, zod_mini.optional)(zod_mini.z.string()),
|
|
3790
|
+
resolvedVersion: zod_mini.z.string(),
|
|
3791
|
+
/** SRI integrity of the package tarball as reported by the registry. */
|
|
3792
|
+
integrity: (0, zod_mini.optional)(zod_mini.z.string()),
|
|
3793
|
+
resolvedAt: (0, zod_mini.optional)(zod_mini.z.string()),
|
|
3794
|
+
skills: zod_mini.z.record(zod_mini.z.string(), NpmLockedSkillSchema)
|
|
3795
|
+
});
|
|
3796
|
+
const NpmSourcesLockSchema = zod_mini.z.object({
|
|
3797
|
+
lockfileVersion: zod_mini.z.number(),
|
|
3798
|
+
sources: zod_mini.z.record(zod_mini.z.string(), NpmLockedSourceSchema)
|
|
3799
|
+
});
|
|
3800
|
+
/**
|
|
3801
|
+
* Create an empty npm lockfile structure.
|
|
3802
|
+
*/
|
|
3803
|
+
function createEmptyNpmLock() {
|
|
3804
|
+
return {
|
|
3805
|
+
lockfileVersion: 1,
|
|
3806
|
+
sources: {}
|
|
3807
|
+
};
|
|
3808
|
+
}
|
|
3809
|
+
/**
|
|
3810
|
+
* Read the npm lockfile from disk.
|
|
3811
|
+
* @returns The parsed lockfile, or an empty lockfile if it doesn't exist or is invalid.
|
|
3812
|
+
*/
|
|
3813
|
+
async function readNpmLockFile(params) {
|
|
3814
|
+
const { logger } = params;
|
|
3815
|
+
const lockPath = (0, node_path.join)(params.projectRoot, require_import.RULESYNC_NPM_SOURCES_LOCK_RELATIVE_FILE_PATH);
|
|
3816
|
+
if (!await require_import.fileExists(lockPath)) {
|
|
3817
|
+
logger.debug("No npm sources lockfile found, starting fresh.");
|
|
3818
|
+
return createEmptyNpmLock();
|
|
3819
|
+
}
|
|
3820
|
+
try {
|
|
3821
|
+
const content = await require_import.readFileContent(lockPath);
|
|
3822
|
+
const result = NpmSourcesLockSchema.safeParse(JSON.parse(content));
|
|
3823
|
+
if (result.success) return result.data;
|
|
3824
|
+
logger.warn(`Invalid npm sources lockfile format (${require_import.RULESYNC_NPM_SOURCES_LOCK_RELATIVE_FILE_PATH}). Starting fresh.`);
|
|
3825
|
+
return createEmptyNpmLock();
|
|
3826
|
+
} catch {
|
|
3827
|
+
logger.warn(`Failed to read npm sources lockfile (${require_import.RULESYNC_NPM_SOURCES_LOCK_RELATIVE_FILE_PATH}). Starting fresh.`);
|
|
3828
|
+
return createEmptyNpmLock();
|
|
3829
|
+
}
|
|
3830
|
+
}
|
|
3831
|
+
/**
|
|
3832
|
+
* Write the npm lockfile to disk.
|
|
3833
|
+
*/
|
|
3834
|
+
async function writeNpmLockFile(params) {
|
|
3835
|
+
const { logger } = params;
|
|
3836
|
+
const lockPath = (0, node_path.join)(params.projectRoot, require_import.RULESYNC_NPM_SOURCES_LOCK_RELATIVE_FILE_PATH);
|
|
3837
|
+
await require_import.writeFileContent(lockPath, JSON.stringify(params.lock, null, 2) + "\n");
|
|
3838
|
+
logger.debug(`Wrote npm sources lockfile to ${lockPath}`);
|
|
3839
|
+
}
|
|
3840
|
+
/**
|
|
3841
|
+
* Normalize an npm source key (package name) for lockfile lookups.
|
|
3842
|
+
*/
|
|
3843
|
+
function normalizeNpmSourceKey(source) {
|
|
3844
|
+
return source.trim();
|
|
3845
|
+
}
|
|
3846
|
+
/**
|
|
3847
|
+
* Get the locked entry for an npm source key, if it exists.
|
|
3848
|
+
*/
|
|
3849
|
+
function getNpmLockedSource(lock, sourceKey) {
|
|
3850
|
+
const normalized = normalizeNpmSourceKey(sourceKey);
|
|
3851
|
+
return Object.prototype.hasOwnProperty.call(lock.sources, normalized) ? lock.sources[normalized] : void 0;
|
|
3852
|
+
}
|
|
3853
|
+
/**
|
|
3854
|
+
* Set (or update) a locked entry for an npm source key (immutable).
|
|
3855
|
+
*/
|
|
3856
|
+
function setNpmLockedSource(lock, sourceKey, entry) {
|
|
3857
|
+
return {
|
|
3858
|
+
lockfileVersion: lock.lockfileVersion,
|
|
3859
|
+
sources: {
|
|
3860
|
+
...lock.sources,
|
|
3861
|
+
[normalizeNpmSourceKey(sourceKey)]: entry
|
|
3862
|
+
}
|
|
3863
|
+
};
|
|
3864
|
+
}
|
|
3865
|
+
/**
|
|
3866
|
+
* Get the skill names from a locked npm source entry.
|
|
3867
|
+
*/
|
|
3868
|
+
function getNpmLockedSkillNames(entry) {
|
|
3869
|
+
return Object.keys(entry.skills);
|
|
3870
|
+
}
|
|
3871
|
+
//#endregion
|
|
3872
|
+
//#region src/lib/npm-tar.ts
|
|
3873
|
+
/**
|
|
3874
|
+
* Minimal, hardened tar reader for npm package tarballs (EXPERIMENTAL npm
|
|
3875
|
+
* transport). Intentionally supports only the subset of the (pax-extended)
|
|
3876
|
+
* ustar format that npm-compatible registries produce:
|
|
3877
|
+
*
|
|
3878
|
+
* - regular files (typeflag "0" / "\0")
|
|
3879
|
+
* - directories (typeflag "5") — skipped; directories are created implicitly
|
|
3880
|
+
* - pax extended headers (typeflag "x") — only the `path` override is honored
|
|
3881
|
+
* - GNU long names (typeflag "L")
|
|
3882
|
+
*
|
|
3883
|
+
* Everything else (symlinks, hardlinks, devices, FIFOs, ...) is skipped and
|
|
3884
|
+
* never materialized. Extraction is bounded by file-count and total-byte caps
|
|
3885
|
+
* to prevent decompression bombs, and every entry path is validated against
|
|
3886
|
+
* traversal (absolute paths, `..` segments, backslashes).
|
|
3887
|
+
*/
|
|
3888
|
+
const BLOCK_SIZE = 512;
|
|
3889
|
+
var NpmTarError = class extends Error {
|
|
3890
|
+
constructor(message, cause) {
|
|
3891
|
+
super(message, { cause });
|
|
3892
|
+
this.name = "NpmTarError";
|
|
3893
|
+
}
|
|
3894
|
+
};
|
|
3895
|
+
/**
|
|
3896
|
+
* Gunzip and extract a npm package tarball into an in-memory file list.
|
|
3897
|
+
* Throws {@link NpmTarError} on malformed archives, traversal attempts, or
|
|
3898
|
+
* resource-limit violations.
|
|
3899
|
+
*/
|
|
3900
|
+
function extractPackageTarball(params) {
|
|
3901
|
+
const { tarball, onSkippedEntry } = params;
|
|
3902
|
+
const maxFiles = params.maxFiles ?? 1e4;
|
|
3903
|
+
const maxTotalBytes = params.maxTotalBytes ?? 104857600;
|
|
3904
|
+
let tar;
|
|
3905
|
+
try {
|
|
3906
|
+
tar = (0, node_zlib.gunzipSync)(tarball, { maxOutputLength: maxTotalBytes + maxFiles * 3 * BLOCK_SIZE + 2 * BLOCK_SIZE });
|
|
3907
|
+
} catch (error) {
|
|
3908
|
+
throw new NpmTarError("Failed to gunzip package tarball", error);
|
|
3909
|
+
}
|
|
3910
|
+
return parseTarBuffer({
|
|
3911
|
+
tar,
|
|
3912
|
+
maxFiles,
|
|
3913
|
+
maxTotalBytes,
|
|
3914
|
+
onSkippedEntry
|
|
3915
|
+
});
|
|
3916
|
+
}
|
|
3917
|
+
function parseTarBuffer(params) {
|
|
3918
|
+
const { tar, maxFiles, maxTotalBytes, onSkippedEntry } = params;
|
|
3919
|
+
const files = [];
|
|
3920
|
+
let totalBytes = 0;
|
|
3921
|
+
let offset = 0;
|
|
3922
|
+
let pendingLongName;
|
|
3923
|
+
let pendingPaxPath;
|
|
3924
|
+
while (offset + BLOCK_SIZE <= tar.length) {
|
|
3925
|
+
const header = tar.subarray(offset, offset + BLOCK_SIZE);
|
|
3926
|
+
if (isZeroBlock(header)) break;
|
|
3927
|
+
verifyHeaderChecksum(header);
|
|
3928
|
+
const size = parseOctalField(header, 124, 12, "size");
|
|
3929
|
+
const typeflag = String.fromCharCode(header[156] ?? 0);
|
|
3930
|
+
const dataStart = offset + BLOCK_SIZE;
|
|
3931
|
+
const dataEnd = dataStart + size;
|
|
3932
|
+
if (dataEnd > tar.length) throw new NpmTarError("Truncated tar archive: entry data extends past end of archive");
|
|
3933
|
+
switch (typeflag) {
|
|
3934
|
+
case "x": {
|
|
3935
|
+
const records = parsePaxRecords(tar.subarray(dataStart, dataEnd));
|
|
3936
|
+
if (records.has("size")) throw new NpmTarError("Unsupported tar archive: pax size override is not supported");
|
|
3937
|
+
pendingPaxPath = records.get("path") ?? pendingPaxPath;
|
|
3938
|
+
break;
|
|
3939
|
+
}
|
|
3940
|
+
case "L":
|
|
3941
|
+
pendingLongName = trimAtFirstNul(tar.toString("utf8", dataStart, dataEnd));
|
|
3942
|
+
break;
|
|
3943
|
+
case "g": {
|
|
3944
|
+
const records = parsePaxRecords(tar.subarray(dataStart, dataEnd));
|
|
3945
|
+
if (records.has("size") || records.has("path")) throw new NpmTarError("Unsupported tar archive: global pax size/path overrides are not supported");
|
|
3946
|
+
break;
|
|
3947
|
+
}
|
|
3948
|
+
case "0":
|
|
3949
|
+
case "\0": {
|
|
3950
|
+
const rawName = resolveEntryName({
|
|
3951
|
+
header,
|
|
3952
|
+
pendingLongName,
|
|
3953
|
+
pendingPaxPath
|
|
3954
|
+
});
|
|
3955
|
+
pendingLongName = void 0;
|
|
3956
|
+
pendingPaxPath = void 0;
|
|
3957
|
+
const relativePath = toSafeRelativePath(rawName);
|
|
3958
|
+
if (relativePath !== null) {
|
|
3959
|
+
if (files.length + 1 > maxFiles) throw new NpmTarError(`Package tarball exceeds max file count of ${maxFiles}. Aborting to prevent resource exhaustion.`);
|
|
3960
|
+
totalBytes += size;
|
|
3961
|
+
if (totalBytes > maxTotalBytes) throw new NpmTarError(`Package tarball exceeds max total size of ${maxTotalBytes / 1024 / 1024}MB. Aborting to prevent resource exhaustion.`);
|
|
3962
|
+
files.push({
|
|
3963
|
+
relativePath,
|
|
3964
|
+
content: Buffer.from(tar.subarray(dataStart, dataEnd))
|
|
3965
|
+
});
|
|
3966
|
+
}
|
|
3967
|
+
break;
|
|
3968
|
+
}
|
|
3969
|
+
case "5":
|
|
3970
|
+
pendingLongName = void 0;
|
|
3971
|
+
pendingPaxPath = void 0;
|
|
3972
|
+
break;
|
|
3973
|
+
default: {
|
|
3974
|
+
const rawName = resolveEntryName({
|
|
3975
|
+
header,
|
|
3976
|
+
pendingLongName,
|
|
3977
|
+
pendingPaxPath
|
|
3978
|
+
});
|
|
3979
|
+
pendingLongName = void 0;
|
|
3980
|
+
pendingPaxPath = void 0;
|
|
3981
|
+
onSkippedEntry?.(`Skipping unsupported tar entry type "${typeflag}" for "${rawName}" (only regular files are extracted).`);
|
|
3982
|
+
break;
|
|
3983
|
+
}
|
|
3984
|
+
}
|
|
3985
|
+
offset = dataStart + Math.ceil(size / BLOCK_SIZE) * BLOCK_SIZE;
|
|
3986
|
+
}
|
|
3987
|
+
return files;
|
|
3988
|
+
}
|
|
3989
|
+
function isZeroBlock(block) {
|
|
3990
|
+
return block.every((byte) => byte === 0);
|
|
3991
|
+
}
|
|
3992
|
+
/** Cut a string at its first NUL character (tar fields are NUL-terminated). */
|
|
3993
|
+
function trimAtFirstNul(value) {
|
|
3994
|
+
const nulIndex = value.indexOf("\0");
|
|
3995
|
+
return nulIndex === -1 ? value : value.substring(0, nulIndex);
|
|
3996
|
+
}
|
|
3997
|
+
function parseOctalField(block, offset, length, field) {
|
|
3998
|
+
if (((block[offset] ?? 0) & 128) !== 0) throw new NpmTarError(`Unsupported tar archive: base-256 ${field} field is not supported`);
|
|
3999
|
+
const text = trimAtFirstNul(block.toString("latin1", offset, offset + length)).trim();
|
|
4000
|
+
if (text === "") return 0;
|
|
4001
|
+
if (!/^[0-7]+$/.test(text)) throw new NpmTarError(`Invalid tar header: malformed octal ${field} field`);
|
|
4002
|
+
return Number.parseInt(text, 8);
|
|
4003
|
+
}
|
|
4004
|
+
/**
|
|
4005
|
+
* Verify the ustar header checksum: the unsigned byte sum of the 512-byte
|
|
4006
|
+
* header with the checksum field itself treated as ASCII spaces.
|
|
4007
|
+
*/
|
|
4008
|
+
function verifyHeaderChecksum(header) {
|
|
4009
|
+
const stored = parseOctalField(header, 148, 8, "checksum");
|
|
4010
|
+
let sum = 0;
|
|
4011
|
+
for (let i = 0; i < BLOCK_SIZE; i++) sum += i >= 148 && i < 156 ? 32 : header[i] ?? 0;
|
|
4012
|
+
if (sum !== stored) throw new NpmTarError("Invalid tar header: checksum mismatch");
|
|
4013
|
+
}
|
|
4014
|
+
function readCString(block, offset, length) {
|
|
4015
|
+
const end = block.indexOf(0, offset);
|
|
4016
|
+
const stop = end === -1 || end > offset + length ? offset + length : end;
|
|
4017
|
+
return block.toString("utf8", offset, stop);
|
|
4018
|
+
}
|
|
4019
|
+
function resolveEntryName(params) {
|
|
4020
|
+
const { header, pendingLongName, pendingPaxPath } = params;
|
|
4021
|
+
if (pendingPaxPath !== void 0) return pendingPaxPath;
|
|
4022
|
+
if (pendingLongName !== void 0) return pendingLongName;
|
|
4023
|
+
const name = readCString(header, 0, 100);
|
|
4024
|
+
const prefix = header.toString("latin1", 257, 262) === "ustar" ? readCString(header, 345, 155) : "";
|
|
4025
|
+
return prefix.length > 0 ? `${prefix}/${name}` : name;
|
|
4026
|
+
}
|
|
4027
|
+
/**
|
|
4028
|
+
* Parse pax extended header records of the form `<len> <key>=<value>\n`,
|
|
4029
|
+
* where `<len>` is the decimal length of the whole record.
|
|
4030
|
+
*/
|
|
4031
|
+
function parsePaxRecords(data) {
|
|
4032
|
+
const records = /* @__PURE__ */ new Map();
|
|
4033
|
+
let offset = 0;
|
|
4034
|
+
while (offset < data.length) {
|
|
4035
|
+
if (data[offset] === 0) break;
|
|
4036
|
+
const spaceIndex = data.indexOf(32, offset);
|
|
4037
|
+
if (spaceIndex === -1) throw new NpmTarError("Invalid pax header: missing length delimiter");
|
|
4038
|
+
const recordLength = Number.parseInt(data.toString("utf8", offset, spaceIndex), 10);
|
|
4039
|
+
if (!Number.isInteger(recordLength) || recordLength <= 0 || offset + recordLength > data.length) throw new NpmTarError("Invalid pax header: malformed record length");
|
|
4040
|
+
const record = data.toString("utf8", spaceIndex + 1, offset + recordLength - 1);
|
|
4041
|
+
const equalsIndex = record.indexOf("=");
|
|
4042
|
+
if (equalsIndex !== -1) records.set(record.slice(0, equalsIndex), record.slice(equalsIndex + 1));
|
|
4043
|
+
offset += recordLength;
|
|
4044
|
+
}
|
|
4045
|
+
return records;
|
|
4046
|
+
}
|
|
4047
|
+
/**
|
|
4048
|
+
* Validate a tar entry path and convert it to a package-root-relative path
|
|
4049
|
+
* with the first component stripped. Returns null for entries that resolve to
|
|
4050
|
+
* the package root itself (e.g. the `package/` folder entry). Throws on
|
|
4051
|
+
* traversal attempts.
|
|
4052
|
+
*/
|
|
4053
|
+
function toSafeRelativePath(rawPath) {
|
|
4054
|
+
if (rawPath.includes("\0")) throw new NpmTarError(`Unsafe tar entry path (NUL byte): "${rawPath}"`);
|
|
4055
|
+
if (rawPath.includes("\\")) throw new NpmTarError(`Unsafe tar entry path (backslash): "${rawPath}"`);
|
|
4056
|
+
if (rawPath.startsWith("/")) throw new NpmTarError(`Unsafe tar entry path (absolute): "${rawPath}"`);
|
|
4057
|
+
const segments = rawPath.split("/").filter((segment) => segment !== "" && segment !== ".");
|
|
4058
|
+
if (segments.includes("..")) throw new NpmTarError(`Unsafe tar entry path (".." segment): "${rawPath}"`);
|
|
4059
|
+
segments.shift();
|
|
4060
|
+
if (segments.length === 0) return null;
|
|
4061
|
+
return segments.join("/");
|
|
4062
|
+
}
|
|
3551
4063
|
/**
|
|
3552
4064
|
* Schema for a single locked skill entry with content integrity.
|
|
3553
4065
|
*/
|
|
@@ -3737,37 +4249,41 @@ async function resolveAndFetchSources(params) {
|
|
|
3737
4249
|
projectRoot,
|
|
3738
4250
|
logger
|
|
3739
4251
|
});
|
|
4252
|
+
let npmLock = options.updateSources ? createEmptyNpmLock() : await readNpmLockFile({
|
|
4253
|
+
projectRoot,
|
|
4254
|
+
logger
|
|
4255
|
+
});
|
|
3740
4256
|
if (options.frozen) assertFrozenLockCoversSources({
|
|
3741
4257
|
lock,
|
|
4258
|
+
npmLock,
|
|
3742
4259
|
sources
|
|
3743
4260
|
});
|
|
3744
4261
|
const originalLockJson = JSON.stringify(lock);
|
|
4262
|
+
const originalNpmLockJson = JSON.stringify(npmLock);
|
|
3745
4263
|
const client = new GitHubClient({ token: GitHubClient.resolveToken(options.token) });
|
|
3746
4264
|
const localSkillNames = await require_import.getLocalSkillDirNames(projectRoot);
|
|
3747
4265
|
let totalSkillCount = 0;
|
|
3748
4266
|
const allFetchedSkillNames = /* @__PURE__ */ new Set();
|
|
3749
4267
|
for (const sourceEntry of sources) try {
|
|
3750
|
-
const
|
|
4268
|
+
const result = await fetchSingleSource({
|
|
3751
4269
|
sourceEntry,
|
|
3752
4270
|
client,
|
|
3753
4271
|
projectRoot,
|
|
3754
4272
|
lock,
|
|
4273
|
+
npmLock,
|
|
3755
4274
|
localSkillNames,
|
|
3756
4275
|
alreadyFetchedSkillNames: allFetchedSkillNames,
|
|
3757
4276
|
updateSources: options.updateSources ?? false,
|
|
3758
4277
|
frozen: options.frozen ?? false,
|
|
3759
4278
|
logger
|
|
3760
4279
|
});
|
|
3761
|
-
lock =
|
|
3762
|
-
|
|
3763
|
-
|
|
4280
|
+
lock = result.lock;
|
|
4281
|
+
npmLock = result.npmLock;
|
|
4282
|
+
totalSkillCount += result.skillCount;
|
|
4283
|
+
for (const name of result.fetchedSkillNames) allFetchedSkillNames.add(name);
|
|
3764
4284
|
} catch (error) {
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
error,
|
|
3768
|
-
logger
|
|
3769
|
-
});
|
|
3770
|
-
else if (error instanceof GitClientError) logGitClientHints({
|
|
4285
|
+
logSourceFetchFailure({
|
|
4286
|
+
sourceEntry,
|
|
3771
4287
|
error,
|
|
3772
4288
|
logger
|
|
3773
4289
|
});
|
|
@@ -3777,18 +4293,100 @@ async function resolveAndFetchSources(params) {
|
|
|
3777
4293
|
sources,
|
|
3778
4294
|
logger
|
|
3779
4295
|
});
|
|
3780
|
-
|
|
4296
|
+
npmLock = pruneStaleNpmLockEntries({
|
|
4297
|
+
npmLock,
|
|
4298
|
+
sources,
|
|
4299
|
+
logger
|
|
4300
|
+
});
|
|
4301
|
+
await writeLockFilesIfChanged({
|
|
3781
4302
|
projectRoot,
|
|
3782
4303
|
lock,
|
|
4304
|
+
npmLock,
|
|
4305
|
+
originalLockJson,
|
|
4306
|
+
originalNpmLockJson,
|
|
4307
|
+
frozen: options.frozen ?? false,
|
|
3783
4308
|
logger
|
|
3784
4309
|
});
|
|
3785
|
-
else logger.debug("Lockfile unchanged, skipping write.");
|
|
3786
4310
|
return {
|
|
3787
4311
|
fetchedSkillCount: totalSkillCount,
|
|
3788
4312
|
sourcesProcessed: sources.length
|
|
3789
4313
|
};
|
|
3790
4314
|
}
|
|
3791
4315
|
/**
|
|
4316
|
+
* Dispatch a single source to the npm fetcher or the git/github fetcher,
|
|
4317
|
+
* returning the (possibly) updated lock objects for both lockfiles.
|
|
4318
|
+
*/
|
|
4319
|
+
async function fetchSingleSource(params) {
|
|
4320
|
+
const { sourceEntry, lock, npmLock } = params;
|
|
4321
|
+
if ((sourceEntry.transport ?? "github") === "npm") {
|
|
4322
|
+
const result = await fetchSourceViaNpm({
|
|
4323
|
+
sourceEntry,
|
|
4324
|
+
projectRoot: params.projectRoot,
|
|
4325
|
+
npmLock,
|
|
4326
|
+
localSkillNames: params.localSkillNames,
|
|
4327
|
+
alreadyFetchedSkillNames: params.alreadyFetchedSkillNames,
|
|
4328
|
+
updateSources: params.updateSources,
|
|
4329
|
+
logger: params.logger
|
|
4330
|
+
});
|
|
4331
|
+
return {
|
|
4332
|
+
skillCount: result.skillCount,
|
|
4333
|
+
fetchedSkillNames: result.fetchedSkillNames,
|
|
4334
|
+
lock,
|
|
4335
|
+
npmLock: result.updatedLock
|
|
4336
|
+
};
|
|
4337
|
+
}
|
|
4338
|
+
const result = await fetchSourceByTransport({
|
|
4339
|
+
sourceEntry,
|
|
4340
|
+
client: params.client,
|
|
4341
|
+
projectRoot: params.projectRoot,
|
|
4342
|
+
lock,
|
|
4343
|
+
localSkillNames: params.localSkillNames,
|
|
4344
|
+
alreadyFetchedSkillNames: params.alreadyFetchedSkillNames,
|
|
4345
|
+
updateSources: params.updateSources,
|
|
4346
|
+
frozen: params.frozen,
|
|
4347
|
+
logger: params.logger
|
|
4348
|
+
});
|
|
4349
|
+
return {
|
|
4350
|
+
skillCount: result.skillCount,
|
|
4351
|
+
fetchedSkillNames: result.fetchedSkillNames,
|
|
4352
|
+
lock: result.updatedLock,
|
|
4353
|
+
npmLock
|
|
4354
|
+
};
|
|
4355
|
+
}
|
|
4356
|
+
/** Log a per-source fetch failure with transport-specific troubleshooting hints. */
|
|
4357
|
+
function logSourceFetchFailure(params) {
|
|
4358
|
+
const { sourceEntry, error, logger } = params;
|
|
4359
|
+
logger.error(`Failed to fetch source "${sourceEntry.source}": ${require_import.formatError(error)}`);
|
|
4360
|
+
if (error instanceof GitHubClientError) logGitHubAuthHints({
|
|
4361
|
+
error,
|
|
4362
|
+
logger
|
|
4363
|
+
});
|
|
4364
|
+
else if (error instanceof GitClientError) logGitClientHints({
|
|
4365
|
+
error,
|
|
4366
|
+
logger
|
|
4367
|
+
});
|
|
4368
|
+
else if (error instanceof NpmClientError) logNpmAuthHints({
|
|
4369
|
+
error,
|
|
4370
|
+
logger
|
|
4371
|
+
});
|
|
4372
|
+
}
|
|
4373
|
+
/** Write each lockfile only when it changed (and never in frozen mode). */
|
|
4374
|
+
async function writeLockFilesIfChanged(params) {
|
|
4375
|
+
const { projectRoot, lock, npmLock, originalLockJson, originalNpmLockJson, frozen, logger } = params;
|
|
4376
|
+
if (!frozen && JSON.stringify(lock) !== originalLockJson) await writeLockFile({
|
|
4377
|
+
projectRoot,
|
|
4378
|
+
lock,
|
|
4379
|
+
logger
|
|
4380
|
+
});
|
|
4381
|
+
else logger.debug("Lockfile unchanged, skipping write.");
|
|
4382
|
+
if (!frozen && JSON.stringify(npmLock) !== originalNpmLockJson) await writeNpmLockFile({
|
|
4383
|
+
projectRoot,
|
|
4384
|
+
lock: npmLock,
|
|
4385
|
+
logger
|
|
4386
|
+
});
|
|
4387
|
+
else logger.debug("npm lockfile unchanged, skipping write.");
|
|
4388
|
+
}
|
|
4389
|
+
/**
|
|
3792
4390
|
* Log contextual hints for GitClientError to help users troubleshoot.
|
|
3793
4391
|
*/
|
|
3794
4392
|
function logGitClientHints(params) {
|
|
@@ -3797,13 +4395,15 @@ function logGitClientHints(params) {
|
|
|
3797
4395
|
else logger.info("Hint: Check your git credentials (SSH keys, credential helper, or access token).");
|
|
3798
4396
|
}
|
|
3799
4397
|
/**
|
|
3800
|
-
* Frozen mode: validate the
|
|
3801
|
-
* remediation guidance listing any uncovered source keys.
|
|
4398
|
+
* Frozen mode: validate the lockfiles cover every declared source. Throws with
|
|
4399
|
+
* remediation guidance listing any uncovered source keys. npm-transport
|
|
4400
|
+
* sources are checked against the npm lockfile; everything else against the
|
|
4401
|
+
* main sources lockfile.
|
|
3802
4402
|
*/
|
|
3803
4403
|
function assertFrozenLockCoversSources(params) {
|
|
3804
|
-
const { lock, sources } = params;
|
|
4404
|
+
const { lock, npmLock, sources } = params;
|
|
3805
4405
|
const missingKeys = [];
|
|
3806
|
-
for (const source of sources) if (!getLockedSource(lock, source.source)) missingKeys.push(source.source);
|
|
4406
|
+
for (const source of sources) if (!((source.transport ?? "github") === "npm" ? getNpmLockedSource(npmLock, source.source) : getLockedSource(lock, source.source))) missingKeys.push(source.source);
|
|
3807
4407
|
if (missingKeys.length > 0) throw new Error(`Frozen install failed: lockfile is missing entries for: ${missingKeys.join(", ")}. Run 'rulesync install' to update the lockfile.`);
|
|
3808
4408
|
}
|
|
3809
4409
|
/**
|
|
@@ -3839,7 +4439,7 @@ async function fetchSourceByTransport(params) {
|
|
|
3839
4439
|
*/
|
|
3840
4440
|
function pruneStaleLockEntries(params) {
|
|
3841
4441
|
const { lock, sources, logger } = params;
|
|
3842
|
-
const sourceKeys = new Set(sources.map((s) => normalizeSourceKey(s.source)));
|
|
4442
|
+
const sourceKeys = new Set(sources.filter((s) => (s.transport ?? "github") !== "npm").map((s) => normalizeSourceKey(s.source)));
|
|
3843
4443
|
const prunedSources = {};
|
|
3844
4444
|
for (const [key, value] of Object.entries(lock.sources)) if (sourceKeys.has(normalizeSourceKey(key))) prunedSources[key] = value;
|
|
3845
4445
|
else logger.debug(`Pruned stale lockfile entry: ${key}`);
|
|
@@ -3849,6 +4449,21 @@ function pruneStaleLockEntries(params) {
|
|
|
3849
4449
|
};
|
|
3850
4450
|
}
|
|
3851
4451
|
/**
|
|
4452
|
+
* Prune stale npm lockfile entries whose keys are not in the current
|
|
4453
|
+
* npm-transport sources (immutable — returns a fresh lock object).
|
|
4454
|
+
*/
|
|
4455
|
+
function pruneStaleNpmLockEntries(params) {
|
|
4456
|
+
const { npmLock, sources, logger } = params;
|
|
4457
|
+
const sourceKeys = new Set(sources.filter((s) => (s.transport ?? "github") === "npm").map((s) => normalizeNpmSourceKey(s.source)));
|
|
4458
|
+
const prunedSources = {};
|
|
4459
|
+
for (const [key, value] of Object.entries(npmLock.sources)) if (sourceKeys.has(normalizeNpmSourceKey(key))) prunedSources[key] = value;
|
|
4460
|
+
else logger.debug(`Pruned stale npm lockfile entry: ${key}`);
|
|
4461
|
+
return {
|
|
4462
|
+
lockfileVersion: npmLock.lockfileVersion,
|
|
4463
|
+
sources: prunedSources
|
|
4464
|
+
};
|
|
4465
|
+
}
|
|
4466
|
+
/**
|
|
3852
4467
|
* Check if all locked skills exist on disk in the curated directory.
|
|
3853
4468
|
*/
|
|
3854
4469
|
async function checkLockedSkillsExist(curatedDir, skillNames) {
|
|
@@ -3916,16 +4531,31 @@ async function writeSkillAndComputeIntegrity(params) {
|
|
|
3916
4531
|
return { integrity };
|
|
3917
4532
|
}
|
|
3918
4533
|
/**
|
|
4534
|
+
* Merge back locked skills that still exist in the remote but were skipped
|
|
4535
|
+
* during fetching (due to local precedence, already-fetched, etc.). Skills no
|
|
4536
|
+
* longer present in the remote (e.g. renamed or deleted upstream) are
|
|
4537
|
+
* intentionally dropped. Shared by the git/github and npm lock updates.
|
|
4538
|
+
*/
|
|
4539
|
+
function mergeFetchedWithLockedSkills(params) {
|
|
4540
|
+
const { fetchedSkills, lockedSkills, remoteSkillNames } = params;
|
|
4541
|
+
const remoteSet = new Set(remoteSkillNames);
|
|
4542
|
+
const mergedSkills = { ...fetchedSkills };
|
|
4543
|
+
if (lockedSkills) {
|
|
4544
|
+
for (const [skillName, skillEntry] of Object.entries(lockedSkills)) if (!(skillName in mergedSkills) && remoteSet.has(skillName)) mergedSkills[skillName] = skillEntry;
|
|
4545
|
+
}
|
|
4546
|
+
return mergedSkills;
|
|
4547
|
+
}
|
|
4548
|
+
/**
|
|
3919
4549
|
* Merge newly fetched skills with existing locked skills and update the lockfile.
|
|
3920
4550
|
*/
|
|
3921
4551
|
function buildLockUpdate(params) {
|
|
3922
4552
|
const { lock, sourceKey, fetchedSkills, locked, requestedRef, resolvedSha, remoteSkillNames, logger } = params;
|
|
3923
4553
|
const fetchedNames = Object.keys(fetchedSkills);
|
|
3924
|
-
const
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
}
|
|
4554
|
+
const mergedSkills = mergeFetchedWithLockedSkills({
|
|
4555
|
+
fetchedSkills,
|
|
4556
|
+
lockedSkills: locked?.skills,
|
|
4557
|
+
remoteSkillNames
|
|
4558
|
+
});
|
|
3929
4559
|
const updatedLock = setLockedSource(lock, sourceKey, {
|
|
3930
4560
|
requestedRef,
|
|
3931
4561
|
resolvedRef: resolvedSha,
|
|
@@ -4366,6 +4996,265 @@ async function fetchSourceViaGit(params) {
|
|
|
4366
4996
|
updatedLock: result.updatedLock
|
|
4367
4997
|
};
|
|
4368
4998
|
}
|
|
4999
|
+
/**
|
|
5000
|
+
* Select the skill files inside an extracted npm package, mirroring the git
|
|
5001
|
+
* transport's discovery: files under `skills/` (or the configured `path`) are
|
|
5002
|
+
* grouped per subdirectory; a package whose `SKILL.md` sits at the package
|
|
5003
|
+
* root is installed as a single skill (root fallback via
|
|
5004
|
+
* {@link shouldUseRootFallback}), named after the requested skill or, for
|
|
5005
|
+
* wildcard fetches, after the package's base name.
|
|
5006
|
+
*/
|
|
5007
|
+
function selectNpmSkillFiles(params) {
|
|
5008
|
+
const { allFiles, skillsPath, skillFilter, isWildcard, packageName } = params;
|
|
5009
|
+
const normalizedBase = node_path.posix.normalize(skillsPath.replace(/\\/g, "/")).replace(/\/+$/, "");
|
|
5010
|
+
if (normalizedBase === "" || normalizedBase === ".") return {
|
|
5011
|
+
remoteFiles: allFiles,
|
|
5012
|
+
skillFilter,
|
|
5013
|
+
isWildcard
|
|
5014
|
+
};
|
|
5015
|
+
const prefix = `${normalizedBase}/`;
|
|
5016
|
+
const filesUnderBase = allFiles.filter((file) => file.relativePath.startsWith(prefix)).map((file) => ({
|
|
5017
|
+
relativePath: file.relativePath.substring(prefix.length),
|
|
5018
|
+
content: file.content
|
|
5019
|
+
}));
|
|
5020
|
+
if (filesUnderBase.length > 0) return {
|
|
5021
|
+
remoteFiles: filesUnderBase,
|
|
5022
|
+
skillFilter,
|
|
5023
|
+
isWildcard
|
|
5024
|
+
};
|
|
5025
|
+
const hasRootSkillFile = allFiles.some((file) => file.relativePath === require_import.SKILL_FILE_NAME);
|
|
5026
|
+
const fallbackFilter = isWildcard ? [npmPackageBaseName(packageName)] : skillFilter;
|
|
5027
|
+
const [singleSkillName] = fallbackFilter;
|
|
5028
|
+
if (fallbackFilter.length === 1 && singleSkillName !== void 0 && shouldUseRootFallback({
|
|
5029
|
+
skillFilter: fallbackFilter,
|
|
5030
|
+
isWildcard: false,
|
|
5031
|
+
hasRootSkillFile,
|
|
5032
|
+
hasRequestedSkillDir: false
|
|
5033
|
+
})) return {
|
|
5034
|
+
remoteFiles: allFiles,
|
|
5035
|
+
skillFilter: fallbackFilter,
|
|
5036
|
+
isWildcard: false
|
|
5037
|
+
};
|
|
5038
|
+
return {
|
|
5039
|
+
remoteFiles: filesUnderBase,
|
|
5040
|
+
skillFilter,
|
|
5041
|
+
isWildcard
|
|
5042
|
+
};
|
|
5043
|
+
}
|
|
5044
|
+
/** Base name of an npm package: `@scope/name` -> `name`. */
|
|
5045
|
+
function npmPackageBaseName(packageName) {
|
|
5046
|
+
const slashIndex = packageName.indexOf("/");
|
|
5047
|
+
return slashIndex === -1 ? packageName : packageName.substring(slashIndex + 1);
|
|
5048
|
+
}
|
|
5049
|
+
/**
|
|
5050
|
+
* Resolve the version to fetch for an npm source: the locked version when
|
|
5051
|
+
* available (deterministic re-fetch), otherwise the declared `ref` (exact
|
|
5052
|
+
* version or dist-tag, defaulting to "latest") resolved via the packument.
|
|
5053
|
+
*/
|
|
5054
|
+
function resolveNpmFetchVersion(params) {
|
|
5055
|
+
const { sourceEntry, locked, updateSources } = params;
|
|
5056
|
+
if (locked && !updateSources) return {
|
|
5057
|
+
lockedVersion: locked.resolvedVersion,
|
|
5058
|
+
requestedVersion: locked.requestedVersion
|
|
5059
|
+
};
|
|
5060
|
+
return {
|
|
5061
|
+
lockedVersion: void 0,
|
|
5062
|
+
requestedVersion: sourceEntry.ref ?? "latest"
|
|
5063
|
+
};
|
|
5064
|
+
}
|
|
5065
|
+
/**
|
|
5066
|
+
* Resolve the package version via the registry packument, download the
|
|
5067
|
+
* tarball, and verify it against the registry (and, when re-fetching a locked
|
|
5068
|
+
* version, the locked) integrity metadata.
|
|
5069
|
+
*/
|
|
5070
|
+
async function downloadVerifiedNpmTarball(params) {
|
|
5071
|
+
const { packageName, registryUrl, token, lockedVersion, requestedVersion, locked, logger } = params;
|
|
5072
|
+
const packument = await fetchPackument({
|
|
5073
|
+
registryUrl,
|
|
5074
|
+
packageName,
|
|
5075
|
+
token
|
|
5076
|
+
});
|
|
5077
|
+
const resolvedVersion = lockedVersion ?? resolvePackumentVersion({
|
|
5078
|
+
packument,
|
|
5079
|
+
packageName,
|
|
5080
|
+
requested: requestedVersion ?? "latest"
|
|
5081
|
+
});
|
|
5082
|
+
logger.debug(`Resolved ${packageName}@${requestedVersion ?? "latest"} to ${resolvedVersion}`);
|
|
5083
|
+
const dist = getPackumentVersionDist({
|
|
5084
|
+
packument,
|
|
5085
|
+
packageName,
|
|
5086
|
+
version: resolvedVersion
|
|
5087
|
+
});
|
|
5088
|
+
const tarball = await fetchTarball({
|
|
5089
|
+
tarballUrl: dist.tarball,
|
|
5090
|
+
registryUrl,
|
|
5091
|
+
token
|
|
5092
|
+
});
|
|
5093
|
+
const context = `${packageName}@${resolvedVersion}`;
|
|
5094
|
+
verifyTarballIntegrity({
|
|
5095
|
+
tarball,
|
|
5096
|
+
integrity: dist.integrity,
|
|
5097
|
+
shasum: dist.shasum,
|
|
5098
|
+
context,
|
|
5099
|
+
logger
|
|
5100
|
+
});
|
|
5101
|
+
if (locked?.integrity && locked.resolvedVersion === resolvedVersion) verifyTarballIntegrity({
|
|
5102
|
+
tarball,
|
|
5103
|
+
integrity: locked.integrity,
|
|
5104
|
+
context,
|
|
5105
|
+
logger
|
|
5106
|
+
});
|
|
5107
|
+
return {
|
|
5108
|
+
resolvedVersion,
|
|
5109
|
+
dist,
|
|
5110
|
+
tarball
|
|
5111
|
+
};
|
|
5112
|
+
}
|
|
5113
|
+
/**
|
|
5114
|
+
* Extract a verified npm tarball in memory and convert its entries into
|
|
5115
|
+
* remote skill files, skipping any file above MAX_FILE_SIZE.
|
|
5116
|
+
*/
|
|
5117
|
+
function extractNpmRemoteFiles(params) {
|
|
5118
|
+
const { tarball, logger } = params;
|
|
5119
|
+
const extracted = extractPackageTarball({
|
|
5120
|
+
tarball,
|
|
5121
|
+
onSkippedEntry: (message) => logger.warn(message)
|
|
5122
|
+
});
|
|
5123
|
+
const allFiles = [];
|
|
5124
|
+
for (const entry of extracted) {
|
|
5125
|
+
if (entry.content.length > 10485760) {
|
|
5126
|
+
logger.warn(`Skipping file "${entry.relativePath}" (${(entry.content.length / 1024 / 1024).toFixed(2)}MB exceeds ${require_import.MAX_FILE_SIZE / 1024 / 1024}MB limit).`);
|
|
5127
|
+
continue;
|
|
5128
|
+
}
|
|
5129
|
+
allFiles.push({
|
|
5130
|
+
relativePath: entry.relativePath,
|
|
5131
|
+
content: entry.content.toString("utf8")
|
|
5132
|
+
});
|
|
5133
|
+
}
|
|
5134
|
+
return allFiles;
|
|
5135
|
+
}
|
|
5136
|
+
/** Build the npm lockfile entry for a fetched source. */
|
|
5137
|
+
function buildNpmLockEntry(params) {
|
|
5138
|
+
const { sourceEntry, requestedVersion, resolvedVersion, dist, mergedSkills } = params;
|
|
5139
|
+
const integrity = dist.integrity ?? (dist.shasum !== void 0 ? shasumToSri(dist.shasum) : void 0);
|
|
5140
|
+
return {
|
|
5141
|
+
...sourceEntry.registry !== void 0 && { registry: sourceEntry.registry },
|
|
5142
|
+
...requestedVersion !== void 0 && { requestedVersion },
|
|
5143
|
+
resolvedVersion,
|
|
5144
|
+
...integrity !== void 0 && { integrity },
|
|
5145
|
+
resolvedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
5146
|
+
skills: mergedSkills
|
|
5147
|
+
};
|
|
5148
|
+
}
|
|
5149
|
+
/**
|
|
5150
|
+
* Fetch skills from a single npm-transport source (EXPERIMENTAL): resolve the
|
|
5151
|
+
* package version via the registry packument, download and verify the
|
|
5152
|
+
* tarball, extract it in-memory with the hardened tar reader, and install the
|
|
5153
|
+
* discovered skills into the curated directory.
|
|
5154
|
+
*/
|
|
5155
|
+
async function fetchSourceViaNpm(params) {
|
|
5156
|
+
const { sourceEntry, projectRoot, npmLock, localSkillNames, alreadyFetchedSkillNames, updateSources, logger } = params;
|
|
5157
|
+
const packageName = sourceEntry.source;
|
|
5158
|
+
validateNpmPackageName(packageName);
|
|
5159
|
+
const registryUrl = sourceEntry.registry ?? "https://registry.npmjs.org";
|
|
5160
|
+
validateNpmRegistryUrl(registryUrl, { logger });
|
|
5161
|
+
const token = resolveNpmToken({ tokenEnv: sourceEntry.tokenEnv });
|
|
5162
|
+
const sourceKey = packageName;
|
|
5163
|
+
const locked = getNpmLockedSource(npmLock, sourceKey);
|
|
5164
|
+
const lockedSkillNames = locked ? getNpmLockedSkillNames(locked) : [];
|
|
5165
|
+
const curatedDir = (0, node_path.join)(projectRoot, require_import.RULESYNC_CURATED_SKILLS_RELATIVE_DIR_PATH);
|
|
5166
|
+
const { lockedVersion, requestedVersion } = resolveNpmFetchVersion({
|
|
5167
|
+
sourceEntry,
|
|
5168
|
+
locked,
|
|
5169
|
+
updateSources
|
|
5170
|
+
});
|
|
5171
|
+
if (lockedVersion !== void 0) {
|
|
5172
|
+
if (await checkLockedSkillsExist(curatedDir, lockedSkillNames)) {
|
|
5173
|
+
logger.debug(`Version unchanged for ${sourceKey}, skipping re-fetch.`);
|
|
5174
|
+
return {
|
|
5175
|
+
skillCount: 0,
|
|
5176
|
+
fetchedSkillNames: lockedSkillNames,
|
|
5177
|
+
updatedLock: npmLock
|
|
5178
|
+
};
|
|
5179
|
+
}
|
|
5180
|
+
}
|
|
5181
|
+
const { resolvedVersion, dist, tarball } = await downloadVerifiedNpmTarball({
|
|
5182
|
+
packageName,
|
|
5183
|
+
registryUrl,
|
|
5184
|
+
token,
|
|
5185
|
+
lockedVersion,
|
|
5186
|
+
requestedVersion,
|
|
5187
|
+
locked,
|
|
5188
|
+
logger
|
|
5189
|
+
});
|
|
5190
|
+
const allFiles = extractNpmRemoteFiles({
|
|
5191
|
+
tarball,
|
|
5192
|
+
logger
|
|
5193
|
+
});
|
|
5194
|
+
const declaredFilter = sourceEntry.skills ?? ["*"];
|
|
5195
|
+
const declaredWildcard = declaredFilter.length === 1 && declaredFilter[0] === "*";
|
|
5196
|
+
const { remoteFiles, skillFilter, isWildcard } = selectNpmSkillFiles({
|
|
5197
|
+
allFiles,
|
|
5198
|
+
skillsPath: sourceEntry.path ?? "skills",
|
|
5199
|
+
skillFilter: declaredFilter,
|
|
5200
|
+
isWildcard: declaredWildcard,
|
|
5201
|
+
packageName
|
|
5202
|
+
});
|
|
5203
|
+
const skillFileMap = groupRemoteFilesBySkillRoot({
|
|
5204
|
+
remoteFiles,
|
|
5205
|
+
skillFilter,
|
|
5206
|
+
isWildcard
|
|
5207
|
+
});
|
|
5208
|
+
const allNames = [...skillFileMap.keys()];
|
|
5209
|
+
const filteredNames = isWildcard ? allNames : allNames.filter((n) => skillFilter.includes(n));
|
|
5210
|
+
if (locked) await cleanPreviousCuratedSkills({
|
|
5211
|
+
curatedDir,
|
|
5212
|
+
lockedSkillNames,
|
|
5213
|
+
logger
|
|
5214
|
+
});
|
|
5215
|
+
const lockedForIntegrityCheck = locked ? {
|
|
5216
|
+
resolvedRef: locked.resolvedVersion,
|
|
5217
|
+
skills: locked.skills
|
|
5218
|
+
} : void 0;
|
|
5219
|
+
const fetchedSkills = {};
|
|
5220
|
+
for (const skillName of filteredNames) {
|
|
5221
|
+
if (shouldSkipSkill({
|
|
5222
|
+
skillName,
|
|
5223
|
+
sourceKey,
|
|
5224
|
+
localSkillNames,
|
|
5225
|
+
alreadyFetchedSkillNames,
|
|
5226
|
+
logger
|
|
5227
|
+
})) continue;
|
|
5228
|
+
fetchedSkills[skillName] = await writeSkillAndComputeIntegrity({
|
|
5229
|
+
skillName,
|
|
5230
|
+
files: skillFileMap.get(skillName) ?? [],
|
|
5231
|
+
curatedDir,
|
|
5232
|
+
locked: lockedForIntegrityCheck,
|
|
5233
|
+
resolvedSha: resolvedVersion,
|
|
5234
|
+
sourceKey,
|
|
5235
|
+
logger
|
|
5236
|
+
});
|
|
5237
|
+
logger.debug(`Fetched skill "${skillName}" from ${sourceKey}`);
|
|
5238
|
+
}
|
|
5239
|
+
const fetchedNames = Object.keys(fetchedSkills);
|
|
5240
|
+
const updatedLock = setNpmLockedSource(npmLock, sourceKey, buildNpmLockEntry({
|
|
5241
|
+
sourceEntry,
|
|
5242
|
+
requestedVersion,
|
|
5243
|
+
resolvedVersion,
|
|
5244
|
+
dist,
|
|
5245
|
+
mergedSkills: mergeFetchedWithLockedSkills({
|
|
5246
|
+
fetchedSkills,
|
|
5247
|
+
lockedSkills: locked?.skills,
|
|
5248
|
+
remoteSkillNames: filteredNames
|
|
5249
|
+
})
|
|
5250
|
+
}));
|
|
5251
|
+
logger.info(`Fetched ${fetchedNames.length} skill(s) from ${sourceKey}: ${fetchedNames.join(", ") || "(none)"}`);
|
|
5252
|
+
return {
|
|
5253
|
+
skillCount: fetchedNames.length,
|
|
5254
|
+
fetchedSkillNames: fetchedNames,
|
|
5255
|
+
updatedLock
|
|
5256
|
+
};
|
|
5257
|
+
}
|
|
4369
5258
|
//#endregion
|
|
4370
5259
|
//#region src/cli/commands/install.ts
|
|
4371
5260
|
const INSTALL_MODES = [
|
|
@@ -6795,7 +7684,7 @@ function wrapCommand$1({ name, errorCode, handler, getVersion, loggerFactory = c
|
|
|
6795
7684
|
}
|
|
6796
7685
|
//#endregion
|
|
6797
7686
|
//#region src/cli/index.ts
|
|
6798
|
-
const getVersion = () => "
|
|
7687
|
+
const getVersion = () => "14.0.1";
|
|
6799
7688
|
function wrapCommand(name, errorCode, handler) {
|
|
6800
7689
|
return wrapCommand$1({
|
|
6801
7690
|
name,
|