fix(update): accept npm 12 release metadata (#1138)
* fix(update): accept npm 12 release metadata (cherry picked from commit aa400d18c95e36029c5c654c396ec66924e1358f) * fix(update): classify npm release metadata without a spawn mock Keep the npm 11 string and npm 12 one-element array accepted by the cherry-picked lookup, and separate invalid metadata from a failed registry command. --------- Co-authored-by: Dapo Olaomo <penielgold@gmail.com>
This commit is contained in:
@@ -3,13 +3,14 @@ import { homedir } from "node:os";
|
||||
import { basename, dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import spawn from "cross-spawn";
|
||||
import { z } from "zod";
|
||||
|
||||
import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { safeParseJson } from "../domain/safeJson.js";
|
||||
import {
|
||||
updateInstallCommand,
|
||||
type NpmInstallation,
|
||||
type ReleaseLookupFailure,
|
||||
type UpdateHost,
|
||||
type UpdateOutput,
|
||||
} from "./Update.js";
|
||||
@@ -116,6 +117,60 @@ export const detectNpmInstallation = async (
|
||||
return { prefix: dirname(library), packageRoot: canonicalPackageRoot };
|
||||
};
|
||||
|
||||
const INVALID_NPM_RELEASE_METADATA =
|
||||
"Invalid npm release metadata: expected a version string or a single-element version array.";
|
||||
|
||||
/** npm view arguments for dist-tags.latest, scoped to the owning prefix when known. */
|
||||
export const npmLatestVersionCommand = (
|
||||
installation: NpmInstallation | undefined,
|
||||
): readonly string[] => [
|
||||
"npm",
|
||||
"view",
|
||||
"--global",
|
||||
...(installation === undefined ? [] : ["--prefix", installation.prefix]),
|
||||
PRODUCT_IDENTITY.packageName,
|
||||
"dist-tags.latest",
|
||||
"--json",
|
||||
"--fetch-retries=0",
|
||||
"--fetch-timeout=10000",
|
||||
];
|
||||
|
||||
/**
|
||||
* Read one latest version from `npm view --json`.
|
||||
* npm 11 emits a JSON string. npm 12 emits that string as a one-element array.
|
||||
*/
|
||||
export const parseNpmLatestVersion = (
|
||||
metadata: string,
|
||||
): Result<string, string> => {
|
||||
const decoded = safeParseJson(metadata);
|
||||
if (!decoded.ok) return err(decoded.error);
|
||||
const version = readNpmLatestVersion(decoded.value);
|
||||
return version === undefined
|
||||
? err(INVALID_NPM_RELEASE_METADATA)
|
||||
: ok(version);
|
||||
};
|
||||
|
||||
const readNpmLatestVersion = (value: unknown): string | undefined => {
|
||||
if (typeof value === "string") return nonemptyVersion(value);
|
||||
if (!Array.isArray(value) || value.length !== 1) return undefined;
|
||||
const only = value[0];
|
||||
return typeof only === "string" ? nonemptyVersion(only) : undefined;
|
||||
};
|
||||
|
||||
const nonemptyVersion = (value: string): string | undefined =>
|
||||
value.length === 0 ? undefined : value;
|
||||
|
||||
/** Classify npm view output as a version or a lookup failure. */
|
||||
export const npmReleaseLookup = (
|
||||
response: Result<string, string>,
|
||||
): Result<string, ReleaseLookupFailure> => {
|
||||
if (!response.ok) return err({ kind: "unavailable", detail: response.error });
|
||||
const parsed = parseNpmLatestVersion(response.value);
|
||||
return parsed.ok
|
||||
? ok(parsed.value)
|
||||
: err({ kind: "invalid-metadata", detail: parsed.error });
|
||||
};
|
||||
|
||||
/** Create npm, registry, verification, and read-only maintenance effects. */
|
||||
export const systemUpdateHost = (
|
||||
packageRoot = fileURLToPath(new URL("../..", import.meta.url)),
|
||||
@@ -127,28 +182,10 @@ export const systemUpdateHost = (
|
||||
globalRoot: () => requireCommandOutput(["npm", "root", "--global"]),
|
||||
globalPrefix: () => requireCommandOutput(["npm", "prefix", "--global"]),
|
||||
}),
|
||||
latestVersion: async (installation) => {
|
||||
const response = await runUpdateCommand([
|
||||
"npm",
|
||||
"view",
|
||||
"--global",
|
||||
...(installation === undefined ? [] : ["--prefix", installation.prefix]),
|
||||
PRODUCT_IDENTITY.packageName,
|
||||
"dist-tags.latest",
|
||||
"--json",
|
||||
"--fetch-retries=0",
|
||||
"--fetch-timeout=10000",
|
||||
]);
|
||||
if (!response.ok) return response;
|
||||
try {
|
||||
const parsed = z.string().min(1).safeParse(JSON.parse(response.value));
|
||||
return parsed.success
|
||||
? ok(parsed.data)
|
||||
: err(`Invalid npm release metadata: ${parsed.error.message}`);
|
||||
} catch (cause: unknown) {
|
||||
return err(cause instanceof Error ? cause.message : String(cause));
|
||||
}
|
||||
},
|
||||
latestVersion: async (installation) =>
|
||||
npmReleaseLookup(
|
||||
await runUpdateCommand(npmLatestVersionCommand(installation)),
|
||||
),
|
||||
installVersion: async (installation, version, output) => {
|
||||
const result = await runUpdateCommand(
|
||||
updateInstallCommand(installation, version),
|
||||
|
||||
Reference in New Issue
Block a user