Skip to content

Commit ac5a3f9

Browse files
committed
fix(server): backfill cached runtime compatibility launchers
1 parent 8095c65 commit ac5a3f9

3 files changed

Lines changed: 98 additions & 51 deletions

File tree

‎apps/server/src/cloud/pinnedRuntime.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,40 @@ it.layer(NodeServices.layer)("ensurePinnedRuntimeInstalled", (it) => {
223223
}),
224224
);
225225

226+
it.effect("backfills a cached archive without downloading or replacing it", () =>
227+
Effect.gen(function* () {
228+
const fs = yield* FileSystem.FileSystem;
229+
const path = yield* Path.Path;
230+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pinned-legacy-cache-" });
231+
const cached = pinnedRuntimePaths(path, baseDir, version, "linux");
232+
const legacyEntry = path.join(cached.versionDir, "node_modules/t3/dist/bin.mjs");
233+
yield* fs.makeDirectory(cached.versionDir, { recursive: true });
234+
yield* fs.writeFileString(cached.entryPath, "cached executable\n");
235+
yield* fs.writeFileString(cached.sentinelPath, `${version}\n`);
236+
const requests: string[] = [];
237+
const commands: string[] = [];
238+
yield* ensurePinnedRuntimeInstalled({
239+
baseDir,
240+
version,
241+
fs,
242+
path,
243+
platform: "linux",
244+
arch: "x64",
245+
httpClient: releaseHttpClient(yield* validChecksums, requests),
246+
runner: extractingRunner(fs, path, commands),
247+
validate: () =>
248+
fs.exists(legacyEntry).pipe(
249+
Effect.flatMap((exists) => (exists ? Effect.void : Effect.die("missing legacy entry"))),
250+
Effect.orDie,
251+
),
252+
});
253+
assert.deepEqual(requests, []);
254+
assert.deepEqual(commands, []);
255+
assert.equal(yield* fs.readFileString(cached.entryPath), "cached executable\n");
256+
assert.equal(yield* fs.readFileString(cached.sentinelPath), `${version}\n`);
257+
}),
258+
);
259+
226260
it.effect("preserves a completed runtime when validation fails", () =>
227261
Effect.gen(function* () {
228262
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/cloud/pinnedRuntime.ts‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(
230230
input: PinnedRuntimeInstallInput,
231231
) {
232232
const { fs } = input;
233+
// Old service launchers still use the npm entry point, including when an
234+
// archive was cached before this compatibility wrapper existed.
235+
const ensureLegacyEntry = Effect.fn("cloud.pinned_runtime.ensure_legacy_entry")(
236+
function* (versionDir: string) {
237+
const legacyDir = input.path.join(versionDir, "node_modules", "t3", "dist");
238+
const entryPath = input.path.join(legacyDir, "bin.mjs");
239+
if (yield* fs.exists(entryPath)) return;
240+
yield* fs.makeDirectory(legacyDir, { recursive: true });
241+
yield* fs.writeFileString(entryPath, legacyCliLauncherScript("archive"));
242+
},
243+
Effect.mapError(
244+
(cause) =>
245+
new PinnedRuntimeInstallError({
246+
step: "writing the legacy service entry point",
247+
cause,
248+
}),
249+
),
250+
);
233251
const paths = pinnedRuntimePaths(input.path, input.baseDir, input.version, input.platform);
234252
const [versionDirExists, entryExists, sentinel] = yield* Effect.all([
235253
fs.exists(paths.versionDir),
@@ -243,6 +261,7 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(
243261
const alreadyPinned =
244262
entryExists && Option.isSome(sentinel) && sentinel.value.trim() === input.version;
245263
if (alreadyPinned) {
264+
yield* ensureLegacyEntry(paths.versionDir);
246265
yield* input.validate(paths);
247266
return paths;
248267
}
@@ -291,21 +310,7 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(
291310
return yield* Effect.gen(function* () {
292311
yield* installFromArchive(input, stagingDir);
293312

294-
// A launcher already running the old npm layout cannot discover the
295-
// archive entry point. Keep its path working on subsequent updates too.
296-
const legacyDir = input.path.join(stagingDir, "node_modules", "t3", "dist");
297-
yield* fs.makeDirectory(legacyDir, { recursive: true }).pipe(
298-
Effect.andThen(
299-
fs.writeFileString(
300-
input.path.join(legacyDir, "bin.mjs"),
301-
legacyCliLauncherScript("archive"),
302-
),
303-
),
304-
Effect.mapError(
305-
(cause) =>
306-
new PinnedRuntimeInstallError({ step: "writing the legacy service entry point", cause }),
307-
),
308-
);
313+
yield* ensureLegacyEntry(stagingDir);
309314

310315
yield* input.validate(stagingPaths);
311316
yield* fs
@@ -345,7 +350,10 @@ const installPinnedRuntime = Effect.fn("cloud.pinned_runtime.ensure_installed")(
345350
),
346351
),
347352
);
348-
if (!published) yield* input.validate(paths);
353+
if (!published) {
354+
yield* ensureLegacyEntry(paths.versionDir);
355+
yield* input.validate(paths);
356+
}
349357
return paths;
350358
}).pipe(
351359
Effect.ensuring(fs.remove(stagingDir, { recursive: true, force: true }).pipe(Effect.ignore)),

‎scripts/build-npm-platform-packages.test.ts‎

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -186,42 +186,47 @@ it.layer(NodeServices.layer)("build-npm-platform-packages", (it) => {
186186
const hostPlatform = yield* HostProcessPlatform;
187187
const hostArch = yield* HostProcessArchitecture;
188188
const env = { ...process.env, NODE_PATH: fixture.outputDir } as Record<string, string>;
189-
const passthrough = yield* run(process.execPath, ["bin/t3.js", "serve", "--port", "1234"], {
190-
cwd: launcherDir,
191-
env,
192-
});
193-
assert.equal(passthrough.stdout.trim(), `stub ${hostPlatform}-${hostArch} serve --port 1234`);
194-
assert.equal(passthrough.exitCode, 7);
189+
if (KEYS.some((key) => key === `${hostPlatform}-${hostArch}`)) {
190+
const passthrough = yield* run(process.execPath, ["bin/t3.js", "serve", "--port", "1234"], {
191+
cwd: launcherDir,
192+
env,
193+
});
194+
assert.equal(
195+
passthrough.stdout.trim(),
196+
`stub ${hostPlatform}-${hostArch} serve --port 1234`,
197+
);
198+
assert.equal(passthrough.exitCode, 7);
195199

196-
// Run the entry point used by already-installed service updaters from
197-
// the published tarball, including their preflight arguments.
198-
const installedLauncher = path.join(fixture.root, "installed-launcher");
199-
yield* fs.makeDirectory(installedLauncher);
200-
const unpack = yield* run(
201-
"tar",
202-
["-xf", path.join(fixture.outputDir, "t3.tgz"), "-C", installedLauncher],
203-
{
204-
cwd: fixture.root,
205-
},
206-
);
207-
assert.equal(unpack.exitCode, 0, unpack.stderr);
208-
const legacy = yield* run(
209-
process.execPath,
210-
[
211-
"dist/bin.mjs",
212-
"__service-preflight",
213-
"--database-path",
214-
"a database.sqlite",
215-
"--launcher-protocol",
216-
"2",
217-
],
218-
{ cwd: path.join(installedLauncher, "package"), env },
219-
);
220-
assert.equal(
221-
legacy.stdout.trim(),
222-
`stub ${hostPlatform}-${hostArch} __service-preflight --database-path a database.sqlite --launcher-protocol 2`,
223-
);
224-
assert.equal(legacy.exitCode, 7);
200+
// Run the entry point used by already-installed service updaters from
201+
// the published tarball, including their preflight arguments.
202+
const installedLauncher = path.join(fixture.root, "installed-launcher");
203+
yield* fs.makeDirectory(installedLauncher);
204+
const unpack = yield* run(
205+
"tar",
206+
["-xf", path.join(fixture.outputDir, "t3.tgz"), "-C", installedLauncher],
207+
{
208+
cwd: fixture.root,
209+
},
210+
);
211+
assert.equal(unpack.exitCode, 0, unpack.stderr);
212+
const legacy = yield* run(
213+
process.execPath,
214+
[
215+
"dist/bin.mjs",
216+
"__service-preflight",
217+
"--database-path",
218+
"a database.sqlite",
219+
"--launcher-protocol",
220+
"2",
221+
],
222+
{ cwd: path.join(installedLauncher, "package"), env },
223+
);
224+
assert.equal(
225+
legacy.stdout.trim(),
226+
`stub ${hostPlatform}-${hostArch} __service-preflight --database-path a database.sqlite --launcher-protocol 2`,
227+
);
228+
assert.equal(legacy.exitCode, 7);
229+
}
225230

226231
const unsupported = yield* run(process.execPath, ["bin/t3.js", "--version"], {
227232
cwd: launcherDir,

0 commit comments

Comments
 (0)