Skip to content

Commit cb9d0d5

Browse files
committed
fix(core): honor npm registry config, use schema decoding, and compare versions semantically in dist-tag freshness check
1 parent 132739b commit cb9d0d5

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

‎packages/core/src/npm.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * as Npm from "./npm"
22

33
import path from "path"
44
import npa from "npm-package-arg"
5+
import semver from "semver"
56
import { Effect, Schema, Context, Layer, Option, FileSystem } from "effect"
67
import { NodeFileSystem } from "@effect/platform-node"
78
import { FSUtil } from "./fs-util"
@@ -117,19 +118,20 @@ const layer = Layer.effect(
117118
// so compare the cached version against the registry before reusing it.
118119
// Returns true when the cache is stale and a reinstall is needed; any
119120
// registry or read failure conservatively keeps the cached copy.
120-
const isDistTagOutdated = Effect.fn("Npm.isDistTagOutdated")(function* (name: string, target: string) {
121-
const cached = yield* afs.readJson(path.join(target, "package.json")).pipe(
122-
Effect.orElseSucceed(() => undefined),
123-
)
124-
const cachedVersion =
125-
typeof cached === "object" && cached !== null && typeof (cached as any).version === "string"
126-
? ((cached as any).version as string)
127-
: undefined
121+
const isDistTagOutdated = Effect.fn("Npm.isDistTagOutdated")(function* (dir: string, name: string, target: string) {
122+
const decodePackage = Schema.decodeUnknownOption(Schema.Struct({ version: Schema.String }))
123+
const json = yield* afs.readJson(path.join(target, "package.json")).pipe(Effect.option)
124+
if (Option.isNone(json)) return true
125+
const decoded = decodePackage(json.value)
126+
const cachedVersion = Option.isSome(decoded) ? decoded.value.version : undefined
128127
if (!cachedVersion) return true
129128

129+
// Resolve the registry from the same npm config `reify` uses so custom
130+
// registries and enterprise mirrors are honored.
131+
const base = yield* NpmConfig.registry(dir)
130132
const encoded = name.includes("/") ? name.replace("/", "%2f") : name
131133
const res = yield* Effect.promise(() =>
132-
fetch(`https://registry.npmjs.org/${encoded}/latest`, {
134+
fetch(`${base}/${encoded}/latest`, {
133135
headers: { Accept: "application/vnd.npm.install-v1+json" },
134136
signal: AbortSignal.timeout(10_000),
135137
}).catch(() => undefined),
@@ -143,6 +145,11 @@ const layer = Layer.effect(
143145
)
144146
const latestVersion = data?.version
145147
if (!latestVersion) return false
148+
// Semver-equivalent spellings ("v1.0.0", "1.0.0+build") must not trigger
149+
// pointless reinstalls.
150+
if (semver.valid(latestVersion) && semver.valid(cachedVersion)) {
151+
return !semver.eq(latestVersion, cachedVersion)
152+
}
146153
return latestVersion !== cachedVersion
147154
})
148155

@@ -160,7 +167,7 @@ const layer = Layer.effect(
160167

161168
if (yield* afs.existsSafe(target)) {
162169
if (parsed?.type === "tag") {
163-
if (!(yield* isDistTagOutdated(name, target))) return resolveEntryPoint(name, target)
170+
if (!(yield* isDistTagOutdated(dir, name, target))) return resolveEntryPoint(name, target)
164171
} else {
165172
return resolveEntryPoint(name, target)
166173
}

‎packages/core/test/npm.test.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe("Npm.add", () => {
8383
global.fetch = (() => {
8484
registryCalls++
8585
throw new Error("registry must not be contacted for exact versions")
86-
}) as typeof fetch
86+
}) as unknown as typeof fetch
8787

8888
try {
8989
const entry = await addWithCache(cache, spec)
@@ -105,7 +105,7 @@ describe("Npm.add", () => {
105105
new Response(JSON.stringify({ version: "1.0.0" }), {
106106
status: 200,
107107
headers: { "content-type": "application/json" },
108-
})) as typeof fetch
108+
})) as unknown as typeof fetch
109109

110110
try {
111111
const entry = await addWithCache(cache, "acme@latest")
@@ -124,10 +124,31 @@ describe("Npm.add", () => {
124124
const originalFetch = global.fetch
125125
global.fetch = (async () => {
126126
throw new Error("offline")
127-
}) as typeof fetch
127+
}) as unknown as typeof fetch
128128

129129
try {
130-
const entry = await addWithCache(cache, "acme@latest")
130+
const entry = await addWithCache(cache, spec)
131+
expect(entry.entrypoint).toBeDefined()
132+
} finally {
133+
global.fetch = originalFetch
134+
}
135+
})
136+
137+
test("treats semver-equivalent registry versions as up to date", async () => {
138+
await using tmp = await tmpdir()
139+
const cache = path.join(tmp.path, "cache")
140+
const spec = "acme@latest"
141+
await seedCachedPackage(cache, spec, "acme", "1.0.0")
142+
143+
const originalFetch = global.fetch
144+
global.fetch = (async () =>
145+
new Response(JSON.stringify({ version: "v1.0.0+build.1" }), {
146+
status: 200,
147+
headers: { "content-type": "application/json" },
148+
})) as unknown as typeof fetch
149+
150+
try {
151+
const entry = await addWithCache(cache, spec)
131152
expect(entry.entrypoint).toBeDefined()
132153
} finally {
133154
global.fetch = originalFetch

0 commit comments

Comments
 (0)