Skip to content

Commit 62e68f3

Browse files
fix(provider-core): isolate npm version caches by lookup environment (pingdotgg#34309)
1 parent e9df591 commit 62e68f3

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎packages/provider-core/src/server/maintenanceResolver.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,57 @@ it.layer(
421421
{ skip: !symlinksSupported || windowsHost },
422422
);
423423

424+
it.effect.skipIf(!symlinksSupported || windowsHost)(
425+
"keeps configured npm registries' latest versions apart for one install",
426+
() =>
427+
Effect.gen(function* () {
428+
const tempDir = yield* makeTempDir("t3-npm-registry-isolation");
429+
yield* Effect.addFinalizer(() =>
430+
Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true })),
431+
);
432+
const npmBinDir = NodePath.join(tempDir, "npm-bin");
433+
writeExecutable(NodePath.join(npmBinDir, "npm"));
434+
const binaryPath = linkIntoPackage(tempDir, "package-tool", [
435+
"lib",
436+
"node_modules",
437+
"@example",
438+
"package-tool",
439+
]);
440+
let probes = 0;
441+
const latest = (registry: string, version: string) =>
442+
resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
443+
binaryPath,
444+
env: { PATH: npmBinDir, NPM_CONFIG_REGISTRY: registry },
445+
}).pipe(
446+
Effect.provideService(
447+
ChildProcessSpawner.ChildProcessSpawner,
448+
stdoutSpawner(() => {
449+
probes++;
450+
return JSON.stringify(version);
451+
}),
452+
),
453+
Effect.tap((capabilities) =>
454+
Effect.sync(() => {
455+
expect(capabilities.latestVersionCacheKey ?? "").not.toContain("private-token");
456+
}),
457+
),
458+
Effect.flatMap(resolveLatestProviderVersion),
459+
);
460+
const first = "https://user:[email protected]";
461+
const second = "https://second.example.test";
462+
expect(yield* latest(first, "1.1.0")).toBe("1.1.0");
463+
expect(yield* latest(second, "2.0.0")).toBe("2.0.0");
464+
expect(yield* latest(first, "1.2.0")).toBe("1.1.0");
465+
expect(probes).toBe(2);
466+
}).pipe(
467+
Effect.provide(ProviderLatestVersions.layer),
468+
Effect.provideService(
469+
HttpClient.HttpClient,
470+
HttpClient.make(() => Effect.die("configured registries must not use public npm")),
471+
),
472+
),
473+
);
474+
424475
it("pins the advertised stable version in a pnpm-global update command", () => {
425476
const advisoryFor = (
426477
maintenanceCapabilities: ReturnType<typeof makeProviderMaintenanceCapabilities>,

‎packages/provider-core/src/server/maintenanceResolver.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,14 @@ const decodeNpmViewVersion = Schema.decodeUnknownOption(
938938
Schema.fromJsonString(Schema.Union([Schema.String, Schema.Tuple([Schema.String])])),
939939
);
940940

941+
function environmentCacheKey(env: NodeJS.ProcessEnv): string {
942+
return NodeCrypto.createHash("sha256")
943+
.update(
944+
JSON.stringify(Object.entries(env).toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))),
945+
)
946+
.digest("hex");
947+
}
948+
941949
const withConfiguredNpmVersionLookup = Effect.fn("withConfiguredNpmVersionLookup")(function* (
942950
capabilities: ProviderMaintenanceCapabilities,
943951
) {
@@ -981,6 +989,14 @@ const withConfiguredNpmVersionLookup = Effect.fn("withConfiguredNpmVersionLookup
981989
...capabilities,
982990
// npm owns registry, scoped auth and proxy configuration; failure must not retry public npm.
983991
latestVersion: null,
992+
latestVersionCacheKey: [
993+
"configured-npm",
994+
npmPath,
995+
packageName,
996+
prefix ?? "",
997+
environmentCacheKey(env),
998+
installerLatestVersionKey(capabilities) ?? "",
999+
].join("\0"),
9841000
lookupLatestVersion: capabilities.lookupLatestVersion
9851001
? capabilities.lookupLatestVersion.pipe(
9861002
Effect.filterOrElse(

0 commit comments

Comments
 (0)