@prisma/compute-sdk 0.45.0 → 0.46.0
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 +39 -5
- package/dist/service/launcher.mjs +179 -99
- package/dist/service/settings/resolve.d.ts.map +1 -1
- package/dist/service/settings/resolve.js +5 -1
- package/dist/service/settings/resolve.js.map +1 -1
- package/dist/service/settings/start-order.d.ts +2 -0
- package/dist/service/settings/start-order.d.ts.map +1 -1
- package/dist/service/settings/start-order.js +7 -5
- package/dist/service/settings/start-order.js.map +1 -1
- package/package.json +1 -1
- package/src/service/launcher/bun-shell.ts +8 -2
- package/src/service/launcher/listening-ports.ts +9 -5
- package/src/service/launcher/main.ts +19 -2
- package/src/service/launcher/package-manager-shims.ts +61 -16
- package/src/service/launcher/proc.ts +5 -0
- package/src/service/launcher/stop-signals.ts +79 -0
- package/src/service/settings/resolve.ts +5 -1
- package/src/service/settings/start-order.ts +12 -3
package/README.md
CHANGED
|
@@ -482,7 +482,7 @@ import { resolveServiceBuildSettings } from "@prisma/compute-sdk/service";
|
|
|
482
482
|
const settings = await resolveServiceBuildSettings((path) => files[path] ?? null);
|
|
483
483
|
// settings.install → { value: "npm ci", from: "lockfile" }
|
|
484
484
|
// settings.build → { value: "npm run build", from: "package.json" }
|
|
485
|
-
// settings.start → { value: { kind: "command", command: "
|
|
485
|
+
// settings.start → { value: { kind: "command", command: "npm run start" }, from: "package.json" }
|
|
486
486
|
```
|
|
487
487
|
|
|
488
488
|
| Setting | Rule |
|
|
@@ -497,7 +497,7 @@ const settings = await resolveServiceBuildSettings((path) => files[path] ?? null
|
|
|
497
497
|
The start is the first match of:
|
|
498
498
|
|
|
499
499
|
1. `start` or `static` in the Service config.
|
|
500
|
-
2. The `start:prod`, `start:production` or `start` script, unless it runs a development server.
|
|
500
|
+
2. The `start:prod`, `start:production` or `start` script, unless it runs a development server. The start is `<package manager> run <script>`, so the script's `pre` script runs before it. See [Scripts](#scripts).
|
|
501
501
|
3. A server file in a folder the build created: `server/index.mjs`, `server/entry.mjs`, `index.js`, `main.js`, `server.js` or `index.mjs`.
|
|
502
502
|
4. A folder the build created that holds `index.html` and the scripts and styles it links. It is served as static files.
|
|
503
503
|
5. An entry file in the source: `main` or `module` from `package.json`, `index.ts`, `server.ts`, `src/index.ts`, `src/server.ts`, or their `.js` forms.
|
|
@@ -570,7 +570,7 @@ const built = await buildService({
|
|
|
570
570
|
});
|
|
571
571
|
|
|
572
572
|
if (built.isOk()) {
|
|
573
|
-
built.value.start; // { value: { kind: "command", command: "
|
|
573
|
+
built.value.start; // { value: { kind: "command", command: "npm run start" }, from: "package.json" }
|
|
574
574
|
built.value.steps; // [{ name: "install", command: "npm ci", seconds: 12, ok: true }, { name: "build", ... }]
|
|
575
575
|
built.value.artifact; // { dir: "/repo", entrypoint: ".prisma/launcher.mjs" }
|
|
576
576
|
}
|
|
@@ -630,7 +630,7 @@ Without a `PORT` from the caller, the Launcher uses `port` from the Service conf
|
|
|
630
630
|
|
|
631
631
|
```ts
|
|
632
632
|
await buildService({ dir: ".", config: { port: 3000 } });
|
|
633
|
-
// .prisma/plan.json → { "kind": "command", "command": "
|
|
633
|
+
// .prisma/plan.json → { "kind": "command", "command": "npm run start", "port": 3000 }
|
|
634
634
|
```
|
|
635
635
|
|
|
636
636
|
A Service that ignores `PORT` listens on its own port. The Launcher's `listening` event then names that port. Nothing in this package changes the routing.
|
|
@@ -640,7 +640,7 @@ A Service that ignores `PORT` listens on its own port. The Launcher's `listening
|
|
|
640
640
|
Compute starts the Launcher with Bun, and the Launcher starts the Service. It exists because the Runtime has no shell: it runs the start command with Bun's shell, makes `node`, `npm run <script>`, `npx` and the bins in `node_modules/.bin` work, and serves static files itself. It writes one line per event to the Service's log:
|
|
641
641
|
|
|
642
642
|
```text
|
|
643
|
-
[prisma] {"event":"start","command":"
|
|
643
|
+
[prisma] {"event":"start","command":"npm run start","port":8080}
|
|
644
644
|
[prisma] {"event":"listening","port":8080}
|
|
645
645
|
[prisma] {"event":"exit","code":1}
|
|
646
646
|
```
|
|
@@ -649,6 +649,40 @@ Compute starts the Launcher with Bun, and the Launcher starts the Service. It ex
|
|
|
649
649
|
|
|
650
650
|
When it serves static files, the Launcher does not serve a file or folder whose name starts with a dot, except `.well-known`.
|
|
651
651
|
|
|
652
|
+
#### Scripts
|
|
653
|
+
|
|
654
|
+
The Launcher runs a `package.json` script the way npm does: `pre<script>` first, then the script, then `post<script>`. A script that fails ends the run with its exit code, and the scripts after it do not run. `npm`, `pnpm`, `yarn` and `bun` all run a script this way on the Runtime.
|
|
655
|
+
|
|
656
|
+
The start script runs the same way, because the start command names the script.
|
|
657
|
+
|
|
658
|
+
```ts
|
|
659
|
+
// package.json: { "scripts": { "prestart": "prisma migrate deploy", "start": "node dist/index.js" } }
|
|
660
|
+
settings.start; // { value: { kind: "command", command: "npm run start" }, from: "package.json" }
|
|
661
|
+
// On the Runtime: "prisma migrate deploy" runs, then "node dist/index.js".
|
|
662
|
+
// When "prisma migrate deploy" fails, the Service does not start.
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
A `start` in the Service config runs as written. `start: "node dist/index.js"` runs no `prestart`, and `start: "npm run start"` does.
|
|
666
|
+
|
|
667
|
+
#### Stop signals
|
|
668
|
+
|
|
669
|
+
The Launcher is the process Compute starts, so a `SIGTERM` or `SIGINT` for the Service arrives at the Launcher. The Launcher sends the signal on to each command the start command runs, and to each command a script runs for `npm run <script>`. It then waits for the start command to end and exits with its exit code.
|
|
670
|
+
|
|
671
|
+
```text
|
|
672
|
+
[prisma] {"event":"start","command":"npm run start","port":8080}
|
|
673
|
+
[prisma] {"event":"listening","port":8080}
|
|
674
|
+
the Service closed its connections
|
|
675
|
+
[prisma] {"event":"exit","code":0}
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
A Service that does not handle the signal ends with exit code 143. A process that a Service process starts itself gets no signal from the Launcher.
|
|
679
|
+
|
|
680
|
+
A signal that arrives when no command runs, for example between `prestart` and `start`, goes to the next command as soon as it starts. Two signals in that time kill the next command.
|
|
681
|
+
|
|
682
|
+
A second signal kills every process the Launcher started, so a Service that ignores the first signal cannot keep the Launcher running. The exit code is then 137.
|
|
683
|
+
|
|
684
|
+
When it serves static files, the Launcher stops its server and exits with 0.
|
|
685
|
+
|
|
652
686
|
## Error handling
|
|
653
687
|
|
|
654
688
|
All `ComputeClient` methods return `Result<T, E>` from the [`better-result`](https://www.npmjs.com/package/better-result) library instead of throwing exceptions. This gives you exhaustive, type-safe error handling.
|
|
@@ -15,7 +15,7 @@ var START_PLAN_FILE = ".prisma/plan.json";
|
|
|
15
15
|
var DEFAULT_PORT = 8080;
|
|
16
16
|
|
|
17
17
|
// src/service/launcher/package-manager-shims.ts
|
|
18
|
-
import { existsSync as existsSync2, readFileSync as
|
|
18
|
+
import { existsSync as existsSync2, readFileSync as readFileSync3 } from "fs";
|
|
19
19
|
import { join as join3 } from "path";
|
|
20
20
|
|
|
21
21
|
// src/service/launcher/bin-wrappers.ts
|
|
@@ -140,99 +140,8 @@ await import(${path});
|
|
|
140
140
|
`;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
// src/service/launcher/bun-shell.ts
|
|
144
|
-
function runInBunShell(command, options = {}) {
|
|
145
|
-
return Bun.spawn([BUN_BINARY, "exec", command], {
|
|
146
|
-
...options,
|
|
147
|
-
stdio: ["inherit", "inherit", "inherit"]
|
|
148
|
-
}).exited;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// src/service/launcher/package-manager-shims.ts
|
|
152
|
-
var SHIM_BEHAVIOURS = {
|
|
153
|
-
npm: "run script",
|
|
154
|
-
pnpm: "run script",
|
|
155
|
-
yarn: "run script",
|
|
156
|
-
bun: "run script or Bun",
|
|
157
|
-
npx: "run command",
|
|
158
|
-
pnpx: "run command",
|
|
159
|
-
bunx: "run command"
|
|
160
|
-
};
|
|
161
|
-
var SHIM_VARIABLE = "PRISMA_SHIM";
|
|
162
|
-
var RUN_WORDS = ["run", "run-script"];
|
|
163
|
-
var YES_FLAGS = ["-y", "--yes"];
|
|
164
|
-
function writePackageManagerShims(wrapperDir, launcherFile) {
|
|
165
|
-
for (const name of Object.keys(SHIM_BEHAVIOURS)) {
|
|
166
|
-
writeBunScript(join3(wrapperDir, name), shimSource(name, launcherFile));
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
function shimSource(name, launcherFile) {
|
|
170
|
-
return `process.env.${SHIM_VARIABLE} = ${JSON.stringify(name)};
|
|
171
|
-
await import(${JSON.stringify(launcherFile)});
|
|
172
|
-
`;
|
|
173
|
-
}
|
|
174
|
-
function takeShimRequest(env) {
|
|
175
|
-
const name = env[SHIM_VARIABLE];
|
|
176
|
-
delete env[SHIM_VARIABLE];
|
|
177
|
-
return isShimName(name) ? name : null;
|
|
178
|
-
}
|
|
179
|
-
async function actAsPackageManager(shim, args) {
|
|
180
|
-
const behaviour = SHIM_BEHAVIOURS[shim];
|
|
181
|
-
if (behaviour === "run command") {
|
|
182
|
-
return runProgram(withoutYesFlags(args));
|
|
183
|
-
}
|
|
184
|
-
const request = parseScriptRequest(args);
|
|
185
|
-
const script = readScript(process.cwd(), request.script);
|
|
186
|
-
if (script !== null) {
|
|
187
|
-
return runScript(script, request.args);
|
|
188
|
-
}
|
|
189
|
-
if (behaviour === "run script or Bun") {
|
|
190
|
-
return runProgram([BUN_BINARY, ...args]);
|
|
191
|
-
}
|
|
192
|
-
console.error(`${shim}: no script named "${request.script}" in package.json`);
|
|
193
|
-
return 1;
|
|
194
|
-
}
|
|
195
|
-
function parseScriptRequest(args) {
|
|
196
|
-
const nameIndex = RUN_WORDS.includes(args[0] ?? "") ? 1 : 0;
|
|
197
|
-
return { script: args[nameIndex], args: args.slice(nameIndex + 1) };
|
|
198
|
-
}
|
|
199
|
-
function withoutYesFlags(args) {
|
|
200
|
-
return args.filter((arg) => !YES_FLAGS.includes(arg));
|
|
201
|
-
}
|
|
202
|
-
function quoteForShell(arg) {
|
|
203
|
-
return `'${arg.replaceAll("'", "'\\''")}'`;
|
|
204
|
-
}
|
|
205
|
-
function isShimName(name) {
|
|
206
|
-
return name !== undefined && Object.hasOwn(SHIM_BEHAVIOURS, name);
|
|
207
|
-
}
|
|
208
|
-
function readScript(dir, name) {
|
|
209
|
-
const file = join3(dir, "package.json");
|
|
210
|
-
if (name === undefined || !existsSync2(file)) {
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
const script = readScripts(file)[name];
|
|
214
|
-
return typeof script === "string" ? script : null;
|
|
215
|
-
}
|
|
216
|
-
function readScripts(file) {
|
|
217
|
-
try {
|
|
218
|
-
return JSON.parse(readFileSync2(file, "utf8"))?.scripts ?? {};
|
|
219
|
-
} catch {
|
|
220
|
-
return {};
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
function runScript(script, args) {
|
|
224
|
-
return runInBunShell([script, ...args.map(quoteForShell)].join(" "));
|
|
225
|
-
}
|
|
226
|
-
async function runProgram(argv) {
|
|
227
|
-
return Bun.spawn(argv, { stdio: ["inherit", "inherit", "inherit"] }).exited;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// src/service/launcher/run-command.ts
|
|
231
|
-
import { rmSync as rmSync2 } from "fs";
|
|
232
|
-
import { dirname as dirname2 } from "path";
|
|
233
|
-
|
|
234
143
|
// src/service/launcher/proc.ts
|
|
235
|
-
import { readdirSync as readdirSync2, readFileSync as
|
|
144
|
+
import { readdirSync as readdirSync2, readFileSync as readFileSync2, readlinkSync } from "fs";
|
|
236
145
|
var TCP_LISTEN_STATE = "0A";
|
|
237
146
|
function parseListeningSockets(text) {
|
|
238
147
|
const sockets = [];
|
|
@@ -271,6 +180,9 @@ function readParentPid(pid) {
|
|
|
271
180
|
const stat = readProcFile(`/proc/${pid}/stat`);
|
|
272
181
|
return stat === null ? null : parseParentPid(stat);
|
|
273
182
|
}
|
|
183
|
+
function readChildPids(pid) {
|
|
184
|
+
return readProcessIds().filter((child) => readParentPid(child) === pid);
|
|
185
|
+
}
|
|
274
186
|
function readSocketInodes(pid) {
|
|
275
187
|
try {
|
|
276
188
|
return readdirSync2(`/proc/${pid}/fd`).map((fd) => parseSocketInode(readlinkSync(`/proc/${pid}/fd/${fd}`))).filter((inode) => inode !== null);
|
|
@@ -283,7 +195,7 @@ function readSocketInodes(pid) {
|
|
|
283
195
|
}
|
|
284
196
|
function readProcFile(file) {
|
|
285
197
|
try {
|
|
286
|
-
return
|
|
198
|
+
return readFileSync2(file, "utf8");
|
|
287
199
|
} catch (error) {
|
|
288
200
|
if (isGoneOrForbidden(error)) {
|
|
289
201
|
return null;
|
|
@@ -329,10 +241,7 @@ function portToReport(ports, routedPort) {
|
|
|
329
241
|
function portsOfProcessesStartedBy(launcherPid) {
|
|
330
242
|
const portByInode = readListeningSockets();
|
|
331
243
|
const ports = new Set;
|
|
332
|
-
for (const pid of
|
|
333
|
-
if (pid === launcherPid || !isStartedBy(pid, launcherPid, readParentPid)) {
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
244
|
+
for (const pid of processesStartedBy(launcherPid)) {
|
|
336
245
|
for (const inode of readSocketInodes(pid)) {
|
|
337
246
|
const port = portByInode.get(inode);
|
|
338
247
|
if (port !== undefined) {
|
|
@@ -342,6 +251,9 @@ function portsOfProcessesStartedBy(launcherPid) {
|
|
|
342
251
|
}
|
|
343
252
|
return [...ports];
|
|
344
253
|
}
|
|
254
|
+
function processesStartedBy(launcherPid) {
|
|
255
|
+
return readProcessIds().filter((pid) => pid !== launcherPid && isStartedBy(pid, launcherPid, readParentPid));
|
|
256
|
+
}
|
|
345
257
|
function isStartedBy(pid, ancestorPid, parentOf) {
|
|
346
258
|
let current = pid;
|
|
347
259
|
for (let step = 0;step < MAX_PARENT_STEPS && current !== null && current > 1; step++) {
|
|
@@ -353,7 +265,164 @@ function isStartedBy(pid, ancestorPid, parentOf) {
|
|
|
353
265
|
return false;
|
|
354
266
|
}
|
|
355
267
|
|
|
268
|
+
// src/service/launcher/stop-signals.ts
|
|
269
|
+
var STOP_SIGNALS = ["SIGTERM", "SIGINT"];
|
|
270
|
+
var RETRY_MS = 50;
|
|
271
|
+
var receivers = () => [];
|
|
272
|
+
var killRequested = false;
|
|
273
|
+
function onStopSignal(handle) {
|
|
274
|
+
let received = 0;
|
|
275
|
+
for (const signal of STOP_SIGNALS) {
|
|
276
|
+
process.on(signal, () => {
|
|
277
|
+
received += 1;
|
|
278
|
+
handle(signal, received === 1);
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function passStopSignalsOn() {
|
|
283
|
+
onStopSignal((signal, isFirst) => {
|
|
284
|
+
if (isFirst) {
|
|
285
|
+
sendToNextReceivers(signal);
|
|
286
|
+
} else {
|
|
287
|
+
killRequested = true;
|
|
288
|
+
sendSignal("SIGKILL", processesStartedBy(process.pid));
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
function sendStopSignalsTo(pids) {
|
|
293
|
+
receivers = pids;
|
|
294
|
+
}
|
|
295
|
+
function sendToNextReceivers(signal) {
|
|
296
|
+
const pids = receivers();
|
|
297
|
+
if (pids.length === 0) {
|
|
298
|
+
setTimeout(() => sendToNextReceivers(signal), RETRY_MS).unref();
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (killRequested) {
|
|
302
|
+
sendSignal("SIGKILL", processesStartedBy(process.pid));
|
|
303
|
+
} else {
|
|
304
|
+
sendSignal(signal, pids);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
function sendSignal(signal, pids) {
|
|
308
|
+
for (const pid of pids) {
|
|
309
|
+
try {
|
|
310
|
+
process.kill(pid, signal);
|
|
311
|
+
} catch {}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// src/service/launcher/bun-shell.ts
|
|
316
|
+
function runInBunShell(command, options = {}) {
|
|
317
|
+
const shell = Bun.spawn([BUN_BINARY, "exec", command], {
|
|
318
|
+
...options,
|
|
319
|
+
stdio: ["inherit", "inherit", "inherit"]
|
|
320
|
+
});
|
|
321
|
+
sendStopSignalsTo(() => readChildPids(shell.pid));
|
|
322
|
+
return shell.exited;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/service/launcher/package-manager-shims.ts
|
|
326
|
+
var SHIM_BEHAVIOURS = {
|
|
327
|
+
npm: "run script",
|
|
328
|
+
pnpm: "run script",
|
|
329
|
+
yarn: "run script",
|
|
330
|
+
bun: "run script or Bun",
|
|
331
|
+
npx: "run command",
|
|
332
|
+
pnpx: "run command",
|
|
333
|
+
bunx: "run command"
|
|
334
|
+
};
|
|
335
|
+
var SHIM_VARIABLE = "PRISMA_SHIM";
|
|
336
|
+
var RUN_WORDS = ["run", "run-script"];
|
|
337
|
+
var BUN_COMMANDS = new Set("a add audit build c check completions create dedupe discord exec fmt getcompletes help i info init install link list login logout outdated patch patch-commit pm prune publish r remove repl rm test uninstall unlink up update upgrade whoami why x".split(" "));
|
|
338
|
+
var YES_FLAGS = ["-y", "--yes"];
|
|
339
|
+
function writePackageManagerShims(wrapperDir, launcherFile) {
|
|
340
|
+
for (const name of Object.keys(SHIM_BEHAVIOURS)) {
|
|
341
|
+
writeBunScript(join3(wrapperDir, name), shimSource(name, launcherFile));
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function shimSource(name, launcherFile) {
|
|
345
|
+
return `process.env.${SHIM_VARIABLE} = ${JSON.stringify(name)};
|
|
346
|
+
await import(${JSON.stringify(launcherFile)});
|
|
347
|
+
`;
|
|
348
|
+
}
|
|
349
|
+
function takeShimRequest(env) {
|
|
350
|
+
const name = env[SHIM_VARIABLE];
|
|
351
|
+
delete env[SHIM_VARIABLE];
|
|
352
|
+
return isShimName(name) ? name : null;
|
|
353
|
+
}
|
|
354
|
+
async function actAsPackageManager(shim, args) {
|
|
355
|
+
const behaviour = SHIM_BEHAVIOURS[shim];
|
|
356
|
+
if (behaviour === "run command") {
|
|
357
|
+
return runProgram(withoutYesFlags(args));
|
|
358
|
+
}
|
|
359
|
+
if (behaviour === "run script or Bun" && BUN_COMMANDS.has(args[0] ?? "")) {
|
|
360
|
+
return runProgram([BUN_BINARY, ...args]);
|
|
361
|
+
}
|
|
362
|
+
const request = parseScriptRequest(args);
|
|
363
|
+
const commands = scriptCommands(readScripts(process.cwd()), request.script, request.args);
|
|
364
|
+
if (commands !== null) {
|
|
365
|
+
return runInOrder(commands);
|
|
366
|
+
}
|
|
367
|
+
if (behaviour === "run script or Bun") {
|
|
368
|
+
return runProgram([BUN_BINARY, ...args]);
|
|
369
|
+
}
|
|
370
|
+
console.error(`${shim}: no script named "${request.script}" in package.json`);
|
|
371
|
+
return 1;
|
|
372
|
+
}
|
|
373
|
+
function parseScriptRequest(args) {
|
|
374
|
+
const nameIndex = RUN_WORDS.includes(args[0] ?? "") ? 1 : 0;
|
|
375
|
+
return { script: args[nameIndex], args: args.slice(nameIndex + 1) };
|
|
376
|
+
}
|
|
377
|
+
function withoutYesFlags(args) {
|
|
378
|
+
return args.filter((arg) => !YES_FLAGS.includes(arg));
|
|
379
|
+
}
|
|
380
|
+
function quoteForShell(arg) {
|
|
381
|
+
return `'${arg.replaceAll("'", "'\\''")}'`;
|
|
382
|
+
}
|
|
383
|
+
function isShimName(name) {
|
|
384
|
+
return name !== undefined && Object.hasOwn(SHIM_BEHAVIOURS, name);
|
|
385
|
+
}
|
|
386
|
+
function scriptCommands(scripts, name, args) {
|
|
387
|
+
const script = name === undefined ? undefined : scripts[name];
|
|
388
|
+
if (typeof script !== "string") {
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
return [
|
|
392
|
+
scripts[`pre${name}`],
|
|
393
|
+
[script, ...args.map(quoteForShell)].join(" "),
|
|
394
|
+
scripts[`post${name}`]
|
|
395
|
+
].filter((command) => typeof command === "string");
|
|
396
|
+
}
|
|
397
|
+
function readScripts(dir) {
|
|
398
|
+
const file = join3(dir, "package.json");
|
|
399
|
+
if (!existsSync2(file)) {
|
|
400
|
+
return {};
|
|
401
|
+
}
|
|
402
|
+
try {
|
|
403
|
+
return JSON.parse(readFileSync3(file, "utf8"))?.scripts ?? {};
|
|
404
|
+
} catch {
|
|
405
|
+
return {};
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
async function runInOrder(commands) {
|
|
409
|
+
for (const command of commands) {
|
|
410
|
+
const exitCode = await runInBunShell(command);
|
|
411
|
+
if (exitCode !== 0) {
|
|
412
|
+
return exitCode;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return 0;
|
|
416
|
+
}
|
|
417
|
+
async function runProgram(argv) {
|
|
418
|
+
const program = Bun.spawn(argv, { stdio: ["inherit", "inherit", "inherit"] });
|
|
419
|
+
sendStopSignalsTo(() => [program.pid]);
|
|
420
|
+
return program.exited;
|
|
421
|
+
}
|
|
422
|
+
|
|
356
423
|
// src/service/launcher/run-command.ts
|
|
424
|
+
import { rmSync as rmSync2 } from "fs";
|
|
425
|
+
import { dirname as dirname2 } from "path";
|
|
357
426
|
async function runCommand(options) {
|
|
358
427
|
const { command, serviceDir, port, launcherFile } = options;
|
|
359
428
|
writeWrappers(serviceDir, WRAPPER_DIR, launcherFile);
|
|
@@ -461,6 +530,7 @@ var shim = takeShimRequest(process.env);
|
|
|
461
530
|
if (shim === null) {
|
|
462
531
|
await startService();
|
|
463
532
|
} else {
|
|
533
|
+
passStopSignalsOn();
|
|
464
534
|
process.exit(await actAsPackageManager(shim, process.argv.slice(2)));
|
|
465
535
|
}
|
|
466
536
|
async function startService() {
|
|
@@ -468,12 +538,22 @@ async function startService() {
|
|
|
468
538
|
const plan = readStartPlan(serviceDir);
|
|
469
539
|
const port = routedPort(plan);
|
|
470
540
|
if (plan.kind === "static") {
|
|
471
|
-
serveStatic(resolve(serviceDir, plan.dir), port);
|
|
541
|
+
stopOnSignal(serveStatic(resolve(serviceDir, plan.dir), port));
|
|
472
542
|
console.log(formatLauncherEvent({ event: "listening", port }));
|
|
473
543
|
return;
|
|
474
544
|
}
|
|
545
|
+
passStopSignalsOn();
|
|
475
546
|
process.exit(await runCommand({ command: plan.command, serviceDir, port, launcherFile }));
|
|
476
547
|
}
|
|
548
|
+
function stopOnSignal(server) {
|
|
549
|
+
onStopSignal((_signal, isFirst) => {
|
|
550
|
+
if (isFirst) {
|
|
551
|
+
server.stop().then(() => process.exit(0));
|
|
552
|
+
} else {
|
|
553
|
+
process.exit(0);
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
}
|
|
477
557
|
function readStartPlan(serviceDir) {
|
|
478
558
|
return JSON.parse(readFileSync4(join5(serviceDir, START_PLAN_FILE), "utf8"));
|
|
479
559
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../../../src/service/settings/resolve.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,oBAAoB,EACpB,aAAa,EACb,KAAK,EACN,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,wBAAsB,2BAA2B,CAC/C,IAAI,EAAE,QAAQ,EACd,MAAM,GAAE,aAAkB,EAC1B,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,oBAAoB,CAAC,
|
|
1
|
+
{"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../../../src/service/settings/resolve.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,oBAAoB,EACpB,aAAa,EACb,KAAK,EACN,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,wBAAsB,2BAA2B,CAC/C,IAAI,EAAE,QAAQ,EACd,MAAM,GAAE,aAAkB,EAC1B,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,oBAAoB,CAAC,CAoB/B;AAED,0FAA0F;AAC1F,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,QAAQ,EACd,MAAM,EAAE,WAAW,EACnB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAQjC"}
|
|
@@ -11,7 +11,11 @@ export async function resolveServiceBuildSettings(read, config = {}, signal) {
|
|
|
11
11
|
const { packageJson, malformed } = await readPackageJson(read);
|
|
12
12
|
const scripts = packageJson.scripts ?? {};
|
|
13
13
|
const choice = await choosePackageManager(read, packageJson);
|
|
14
|
-
const { start, notes } = startBeforeBuild({
|
|
14
|
+
const { start, notes } = startBeforeBuild({
|
|
15
|
+
config,
|
|
16
|
+
scripts,
|
|
17
|
+
packageManager: choice.packageManager,
|
|
18
|
+
});
|
|
15
19
|
return {
|
|
16
20
|
packageManager: choice.packageManager,
|
|
17
21
|
node: await pinnedNodeVersion(read, packageJson),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve.js","sourceRoot":"","sources":["../../../src/service/settings/resolve.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAUrE;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC/C,IAAc,EACd,SAAwB,EAAE,EAC1B,MAAoB;IAEpB,MAAM,EAAE,cAAc,EAAE,CAAC;IACzB,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,MAAM,eAAe,CAAC,IAAI,CAAC,CAAC;IAC/D,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,IAAI,EAAE,CAAC;IAC1C,MAAM,MAAM,GAAG,MAAM,oBAAoB,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IAC7D,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,
|
|
1
|
+
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AC,KAAK;KACnE,CAAC;AACJ,CAAC;AAED,0FAA0F;AAC1F,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,IAAc,EACd,MAAmB,EACnB,MAAoB;IAEpB,MAAM,EAAE,cAAc,EAAE,CAAC;IAEzB,OAAO,eAAe,CAAC;QACrB,IAAI;QACJ,MAAM;QACN,WAAW,EAAE,CAAC,MAAM,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW;KACvD,CAAC,CAAC;AACL,CAAC;AAED,MAAM,2BAA2B,GAC/B,+CAA+C,CAAC;AAElD,4FAA4F;AAC5F,SAAS,YAAY,CACnB,OAA+B,EAC/B,cAA8B,EAC9B,MAAqB;IAErB,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IACjD,CAAC;IAED,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACrB,OAAO,EAAE,KAAK,EAAE,GAAG,cAAc,YAAY,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC;IACxE,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;AAC1C,CAAC;AAED,0EAA0E;AAC1E,SAAS,WAAW,CAAC,MAAqB;IACxC,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACrE,CAAC"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { PackageManager } from "../../package-manager.ts";
|
|
1
2
|
import type { BuildOutput, PackageJson, ReadFile, Resolved, ServiceConfig, Start } from "./types.ts";
|
|
2
3
|
/** What a rule may look at before the build runs. */
|
|
3
4
|
interface ServiceBeforeBuild {
|
|
4
5
|
config: ServiceConfig;
|
|
5
6
|
scripts: Record<string, string>;
|
|
7
|
+
packageManager: PackageManager;
|
|
6
8
|
}
|
|
7
9
|
/** What a rule may look at once the build has run. */
|
|
8
10
|
interface ServiceAfterBuild {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"start-order.d.ts","sourceRoot":"","sources":["../../../src/service/settings/start-order.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"start-order.d.ts","sourceRoot":"","sources":["../../../src/service/settings/start-order.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAE/D,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,aAAa,EAEb,KAAK,EACN,MAAM,YAAY,CAAC;AAEpB,qDAAqD;AACrD,UAAU,kBAAkB;IAC1B,MAAM,EAAE,aAAa,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,cAAc,EAAE,cAAc,CAAC;CAChC;AAED,sDAAsD;AACtD,UAAU,iBAAiB;IACzB,IAAI,EAAE,QAAQ,CAAC;IACf,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,WAAW,CAAC;CACrB;AA6CD,gGAAgG;AAChG,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,kBAAkB,GAAG;IAC7D,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,GAAG,aAAa,CAAC;IACvC,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,CASA;AAED,2FAA2F;AAC3F,wBAAsB,eAAe,CACnC,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CASjC"}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// The start order: the rules that decide how a Service starts. The first rule that matches wins.
|
|
2
|
-
// The user's own statement comes first, then the user's own script, then what the build made, then source files.
|
|
3
1
|
import { isDevCommand } from "./dev-command.js";
|
|
4
2
|
const RULES_BEFORE_BUILD = [
|
|
5
3
|
statedInServiceConfig,
|
|
@@ -73,13 +71,17 @@ function statedInServiceConfig({ config, }) {
|
|
|
73
71
|
}
|
|
74
72
|
return null;
|
|
75
73
|
}
|
|
76
|
-
/**
|
|
77
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Rule 2: the repository's own start script, unless it runs a development server.
|
|
76
|
+
* The start names the script and does not copy its text, so the script runs as the package manager runs it:
|
|
77
|
+
* its `pre` script before it and its `post` script after it. The Launcher does that for `<package manager> run <script>`.
|
|
78
|
+
*/
|
|
79
|
+
function startScript({ scripts, packageManager, }) {
|
|
78
80
|
const script = findStartScript(scripts);
|
|
79
81
|
if (script === null || isDevCommand(script.command, scripts)) {
|
|
80
82
|
return null;
|
|
81
83
|
}
|
|
82
|
-
return command(script.
|
|
84
|
+
return command(`${packageManager} run ${script.name}`, "package.json");
|
|
83
85
|
}
|
|
84
86
|
/** Rule 3: a server file in a folder the build created. */
|
|
85
87
|
async function serverFileFromBuild({ read, output, }) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"start-order.js","sourceRoot":"","sources":["../../../src/service/settings/start-order.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"start-order.js","sourceRoot":"","sources":["../../../src/service/settings/start-order.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AA+BhD,MAAM,kBAAkB,GAAsB;IAC5C,qBAAqB;IACrB,WAAW;CACZ,CAAC;AAEF,MAAM,iBAAiB,GAAqB;IAC1C,mBAAmB;IACnB,mBAAmB;IACnB,iBAAiB;IACjB,wBAAwB;CACzB,CAAC;AAEF,yEAAyE;AACzE,MAAM,aAAa,GAAG,CAAC,YAAY,EAAE,kBAAkB,EAAE,OAAO,CAAC,CAAC;AAElE,qFAAqF;AACrF,MAAM,uBAAuB,GAAG;IAC9B,kBAAkB;IAClB,kBAA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AAC,MAAM,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC;AACrC,CAAC;AAED,SAAS,OAAO,CAAC,IAAY,EAAE,IAAY;IACzC,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC;AAC7D,CAAC;AAED,SAAS,WAAW,CAAC,GAAW,EAAE,IAAY;IAC5C,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC;AAClD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Runs a command line through Bun's shell, because the Runtime has no /bin/sh.
|
|
2
2
|
import { BUN_BINARY } from "./bin-wrappers.ts";
|
|
3
|
+
import { readChildPids } from "./proc.ts";
|
|
4
|
+
import { sendStopSignalsTo } from "./stop-signals.ts";
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Runs `command` in a new Bun process and returns its exit code.
|
|
@@ -10,8 +12,12 @@ export function runInBunShell(
|
|
|
10
12
|
command: string,
|
|
11
13
|
options: { cwd?: string; env?: Record<string, string | undefined> } = {},
|
|
12
14
|
): Promise<number> {
|
|
13
|
-
|
|
15
|
+
const shell = Bun.spawn([BUN_BINARY, "exec", command], {
|
|
14
16
|
...options,
|
|
15
17
|
stdio: ["inherit", "inherit", "inherit"],
|
|
16
|
-
})
|
|
18
|
+
});
|
|
19
|
+
// Bun's shell ends on SIGTERM and leaves its commands running. So the commands get the signal, and the shell reports their exit code.
|
|
20
|
+
sendStopSignalsTo(() => readChildPids(shell.pid));
|
|
21
|
+
|
|
22
|
+
return shell.exited;
|
|
17
23
|
}
|
|
@@ -70,11 +70,7 @@ export function portsOfProcessesStartedBy(launcherPid: number): number[] {
|
|
|
70
70
|
const portByInode = readListeningSockets();
|
|
71
71
|
const ports = new Set<number>();
|
|
72
72
|
|
|
73
|
-
for (const pid of
|
|
74
|
-
if (pid === launcherPid || !isStartedBy(pid, launcherPid, readParentPid)) {
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
|
|
73
|
+
for (const pid of processesStartedBy(launcherPid)) {
|
|
78
74
|
for (const inode of readSocketInodes(pid)) {
|
|
79
75
|
const port = portByInode.get(inode);
|
|
80
76
|
|
|
@@ -87,6 +83,14 @@ export function portsOfProcessesStartedBy(launcherPid: number): number[] {
|
|
|
87
83
|
return [...ports];
|
|
88
84
|
}
|
|
89
85
|
|
|
86
|
+
/** Every process that `launcherPid` started, directly or through other processes. */
|
|
87
|
+
export function processesStartedBy(launcherPid: number): number[] {
|
|
88
|
+
return readProcessIds().filter(
|
|
89
|
+
(pid) =>
|
|
90
|
+
pid !== launcherPid && isStartedBy(pid, launcherPid, readParentPid),
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
90
94
|
/** True when `ancestorPid` started `pid`, directly or through other processes. */
|
|
91
95
|
export function isStartedBy(
|
|
92
96
|
pid: number,
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
} from "./package-manager-shims.ts";
|
|
16
16
|
import { runCommand } from "./run-command.ts";
|
|
17
17
|
import { serveStatic } from "./serve-static.ts";
|
|
18
|
+
import { onStopSignal, passStopSignalsOn } from "./stop-signals.ts";
|
|
18
19
|
|
|
19
20
|
const launcherFile = fileURLToPath(import.meta.url);
|
|
20
21
|
|
|
@@ -23,10 +24,14 @@ const shim = takeShimRequest(process.env);
|
|
|
23
24
|
if (shim === null) {
|
|
24
25
|
await startService();
|
|
25
26
|
} else {
|
|
27
|
+
passStopSignalsOn();
|
|
26
28
|
process.exit(await actAsPackageManager(shim, process.argv.slice(2)));
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* Reads the Start plan, then serves the static folder or runs the start command. Exits with the command's exit code when it ends.
|
|
33
|
+
* A stop signal goes on to the Service. In static mode it stops the server.
|
|
34
|
+
*/
|
|
30
35
|
async function startService(): Promise<void> {
|
|
31
36
|
// The Launcher is at .prisma/launcher.mjs in the Artifact, so the Service's folder is one level up.
|
|
32
37
|
const serviceDir = resolve(dirname(launcherFile), "..");
|
|
@@ -34,16 +39,28 @@ async function startService(): Promise<void> {
|
|
|
34
39
|
const port = routedPort(plan);
|
|
35
40
|
|
|
36
41
|
if (plan.kind === "static") {
|
|
37
|
-
serveStatic(resolve(serviceDir, plan.dir), port);
|
|
42
|
+
stopOnSignal(serveStatic(resolve(serviceDir, plan.dir), port));
|
|
38
43
|
console.log(formatLauncherEvent({ event: "listening", port }));
|
|
39
44
|
return;
|
|
40
45
|
}
|
|
41
46
|
|
|
47
|
+
passStopSignalsOn();
|
|
42
48
|
process.exit(
|
|
43
49
|
await runCommand({ command: plan.command, serviceDir, port, launcherFile }),
|
|
44
50
|
);
|
|
45
51
|
}
|
|
46
52
|
|
|
53
|
+
/** The first stop signal lets the responses that are under way finish, then the Launcher exits. A later one does not wait for them. */
|
|
54
|
+
function stopOnSignal(server: Bun.Server<undefined>): void {
|
|
55
|
+
onStopSignal((_signal, isFirst) => {
|
|
56
|
+
if (isFirst) {
|
|
57
|
+
void server.stop().then(() => process.exit(0));
|
|
58
|
+
} else {
|
|
59
|
+
process.exit(0);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
47
64
|
function readStartPlan(serviceDir: string): StartPlan {
|
|
48
65
|
return JSON.parse(readFileSync(join(serviceDir, START_PLAN_FILE), "utf8"));
|
|
49
66
|
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// Shims for the package manager commands that a start command or a package.json script can call.
|
|
2
2
|
// `npm run x`, `yarn x`, `bun run x` and `npx x` cannot run on the Runtime, because `bun run` needs /bin/sh.
|
|
3
3
|
// A shim sets one environment variable and starts the Launcher file again. The Launcher then runs the script or the command itself.
|
|
4
|
+
// A script runs with its `pre` and `post` scripts: see `scriptCommands`.
|
|
4
5
|
import { existsSync, readFileSync } from "node:fs";
|
|
5
6
|
import { join } from "node:path";
|
|
6
7
|
import { BUN_BINARY, writeBunScript } from "./bin-wrappers.ts";
|
|
7
8
|
import { runInBunShell } from "./bun-shell.ts";
|
|
9
|
+
import { sendStopSignalsTo } from "./stop-signals.ts";
|
|
8
10
|
|
|
9
11
|
/** What a shim does with its arguments. */
|
|
10
12
|
type ShimBehaviour =
|
|
@@ -34,6 +36,13 @@ const SHIM_VARIABLE = "PRISMA_SHIM";
|
|
|
34
36
|
/** The words that can stand before the script name, as in `npm run build`. `yarn build` has none. */
|
|
35
37
|
const RUN_WORDS = ["run", "run-script"];
|
|
36
38
|
|
|
39
|
+
/** Bun's own commands. Bun runs them even when package.json has a script with the same name, so `bun build` is always the bundler. */
|
|
40
|
+
const BUN_COMMANDS = new Set(
|
|
41
|
+
"a add audit build c check completions create dedupe discord exec fmt getcompletes help i info init install link list login logout outdated patch patch-commit pm prune publish r remove repl rm test uninstall unlink up update upgrade whoami why x".split(
|
|
42
|
+
" ",
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
|
|
37
46
|
/** The flags that tell `npx` not to ask before a download. The Launcher downloads nothing, so they mean nothing here. */
|
|
38
47
|
const YES_FLAGS = ["-y", "--yes"];
|
|
39
48
|
|
|
@@ -80,11 +89,19 @@ export async function actAsPackageManager(
|
|
|
80
89
|
return runProgram(withoutYesFlags(args));
|
|
81
90
|
}
|
|
82
91
|
|
|
83
|
-
|
|
84
|
-
|
|
92
|
+
if (behaviour === "run script or Bun" && BUN_COMMANDS.has(args[0] ?? "")) {
|
|
93
|
+
return runProgram([BUN_BINARY, ...args]);
|
|
94
|
+
}
|
|
85
95
|
|
|
86
|
-
|
|
87
|
-
|
|
96
|
+
const request = parseScriptRequest(args);
|
|
97
|
+
const commands = scriptCommands(
|
|
98
|
+
readScripts(process.cwd()),
|
|
99
|
+
request.script,
|
|
100
|
+
request.args,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (commands !== null) {
|
|
104
|
+
return runInOrder(commands);
|
|
88
105
|
}
|
|
89
106
|
|
|
90
107
|
if (behaviour === "run script or Bun") {
|
|
@@ -115,20 +132,37 @@ function isShimName(name: string | undefined): name is ShimName {
|
|
|
115
132
|
return name !== undefined && Object.hasOwn(SHIM_BEHAVIOURS, name);
|
|
116
133
|
}
|
|
117
134
|
|
|
118
|
-
/**
|
|
119
|
-
|
|
120
|
-
|
|
135
|
+
/**
|
|
136
|
+
* The command lines that run the script `name`, in order, or null when `scripts` has no such script.
|
|
137
|
+
* `pre<name>` runs before the script and `post<name>` after it, when `scripts` has them. Only the script itself gets the arguments.
|
|
138
|
+
* npm, pnpm, Yarn 1 and Bun all run a script this way, so every shim does.
|
|
139
|
+
*/
|
|
140
|
+
export function scriptCommands(
|
|
141
|
+
scripts: Record<string, unknown>,
|
|
142
|
+
name: string | undefined,
|
|
143
|
+
args: string[],
|
|
144
|
+
): string[] | null {
|
|
145
|
+
const script = name === undefined ? undefined : scripts[name];
|
|
121
146
|
|
|
122
|
-
if (
|
|
147
|
+
if (typeof script !== "string") {
|
|
123
148
|
return null;
|
|
124
149
|
}
|
|
125
150
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
151
|
+
return [
|
|
152
|
+
scripts[`pre${name}`],
|
|
153
|
+
[script, ...args.map(quoteForShell)].join(" "),
|
|
154
|
+
scripts[`post${name}`],
|
|
155
|
+
].filter((command) => typeof command === "string");
|
|
129
156
|
}
|
|
130
157
|
|
|
131
|
-
|
|
158
|
+
/** The scripts in the package.json of `dir`. A folder without a package.json has none. */
|
|
159
|
+
function readScripts(dir: string): Record<string, unknown> {
|
|
160
|
+
const file = join(dir, "package.json");
|
|
161
|
+
|
|
162
|
+
if (!existsSync(file)) {
|
|
163
|
+
return {};
|
|
164
|
+
}
|
|
165
|
+
|
|
132
166
|
try {
|
|
133
167
|
return JSON.parse(readFileSync(file, "utf8"))?.scripts ?? {};
|
|
134
168
|
} catch {
|
|
@@ -137,11 +171,22 @@ function readScripts(file: string): Record<string, unknown> {
|
|
|
137
171
|
}
|
|
138
172
|
}
|
|
139
173
|
|
|
140
|
-
/** Runs
|
|
141
|
-
function
|
|
142
|
-
|
|
174
|
+
/** Runs the command lines one after the other. The first one that fails ends the run with its exit code. */
|
|
175
|
+
async function runInOrder(commands: string[]): Promise<number> {
|
|
176
|
+
for (const command of commands) {
|
|
177
|
+
const exitCode = await runInBunShell(command);
|
|
178
|
+
|
|
179
|
+
if (exitCode !== 0) {
|
|
180
|
+
return exitCode;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return 0;
|
|
143
185
|
}
|
|
144
186
|
|
|
145
187
|
async function runProgram(argv: string[]): Promise<number> {
|
|
146
|
-
|
|
188
|
+
const program = Bun.spawn(argv, { stdio: ["inherit", "inherit", "inherit"] });
|
|
189
|
+
sendStopSignalsTo(() => [program.pid]);
|
|
190
|
+
|
|
191
|
+
return program.exited;
|
|
147
192
|
}
|
|
@@ -69,6 +69,11 @@ export function readParentPid(pid: number): number | null {
|
|
|
69
69
|
return stat === null ? null : parseParentPid(stat);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/** The processes that `pid` started itself. */
|
|
73
|
+
export function readChildPids(pid: number): number[] {
|
|
74
|
+
return readProcessIds().filter((child) => readParentPid(child) === pid);
|
|
75
|
+
}
|
|
76
|
+
|
|
72
77
|
/** The inodes of the sockets a process holds open. */
|
|
73
78
|
export function readSocketInodes(pid: number): string[] {
|
|
74
79
|
try {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Passes a stop signal on to the Service. The Launcher is the process Compute starts, so a signal for the Service arrives at the Launcher alone.
|
|
2
|
+
import { processesStartedBy } from "./listening-ports.ts";
|
|
3
|
+
|
|
4
|
+
/** The signals that ask a process to stop. A platform sends SIGTERM, and Ctrl+C in a terminal sends SIGINT. */
|
|
5
|
+
const STOP_SIGNALS = ["SIGTERM", "SIGINT"] as const;
|
|
6
|
+
|
|
7
|
+
/** How often a stop signal that no process could get yet looks for the processes of the next command. */
|
|
8
|
+
const RETRY_MS = 50;
|
|
9
|
+
|
|
10
|
+
/** Returns the processes that get a stop signal. */
|
|
11
|
+
type Receivers = () => number[];
|
|
12
|
+
|
|
13
|
+
/** A Launcher process runs one command at a time, so one variable names who gets the next stop signal. */
|
|
14
|
+
let receivers: Receivers = () => [];
|
|
15
|
+
|
|
16
|
+
/** True after the second stop signal. A first signal that still waits for a command then kills that command. */
|
|
17
|
+
let killRequested = false;
|
|
18
|
+
|
|
19
|
+
/** Calls `handle` for each stop signal this process gets. `isFirst` is true for the first one only. */
|
|
20
|
+
export function onStopSignal(
|
|
21
|
+
handle: (signal: NodeJS.Signals, isFirst: boolean) => void,
|
|
22
|
+
): void {
|
|
23
|
+
let received = 0;
|
|
24
|
+
|
|
25
|
+
for (const signal of STOP_SIGNALS) {
|
|
26
|
+
process.on(signal, () => {
|
|
27
|
+
received += 1;
|
|
28
|
+
handle(signal, received === 1);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* From now on a stop signal does not end this process. The first one goes to the command that runs,
|
|
35
|
+
* and this process keeps waiting for the command to end, so it can exit with the command's exit code.
|
|
36
|
+
* When no command runs yet, the signal waits for the next one. A stop request is never lost.
|
|
37
|
+
* A later one kills every process this process started. The wait then ends at once.
|
|
38
|
+
*/
|
|
39
|
+
export function passStopSignalsOn(): void {
|
|
40
|
+
onStopSignal((signal, isFirst) => {
|
|
41
|
+
if (isFirst) {
|
|
42
|
+
sendToNextReceivers(signal);
|
|
43
|
+
} else {
|
|
44
|
+
killRequested = true;
|
|
45
|
+
sendSignal("SIGKILL", processesStartedBy(process.pid));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Names the processes of the command that runs now. They get the next stop signal. */
|
|
51
|
+
export function sendStopSignalsTo(pids: Receivers): void {
|
|
52
|
+
receivers = pids;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Sends the signal to the command that runs, or to the next command as soon as its processes exist. */
|
|
56
|
+
function sendToNextReceivers(signal: NodeJS.Signals): void {
|
|
57
|
+
const pids = receivers();
|
|
58
|
+
|
|
59
|
+
if (pids.length === 0) {
|
|
60
|
+
setTimeout(() => sendToNextReceivers(signal), RETRY_MS).unref();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (killRequested) {
|
|
65
|
+
sendSignal("SIGKILL", processesStartedBy(process.pid));
|
|
66
|
+
} else {
|
|
67
|
+
sendSignal(signal, pids);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function sendSignal(signal: NodeJS.Signals, pids: number[]): void {
|
|
72
|
+
for (const pid of pids) {
|
|
73
|
+
try {
|
|
74
|
+
process.kill(pid, signal);
|
|
75
|
+
} catch {
|
|
76
|
+
// The process ended after /proc listed it, so it needs no signal.
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -26,7 +26,11 @@ export async function resolveServiceBuildSettings(
|
|
|
26
26
|
const { packageJson, malformed } = await readPackageJson(read);
|
|
27
27
|
const scripts = packageJson.scripts ?? {};
|
|
28
28
|
const choice = await choosePackageManager(read, packageJson);
|
|
29
|
-
const { start, notes } = startBeforeBuild({
|
|
29
|
+
const { start, notes } = startBeforeBuild({
|
|
30
|
+
config,
|
|
31
|
+
scripts,
|
|
32
|
+
packageManager: choice.packageManager,
|
|
33
|
+
});
|
|
30
34
|
|
|
31
35
|
return {
|
|
32
36
|
packageManager: choice.packageManager,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// The start order: the rules that decide how a Service starts. The first rule that matches wins.
|
|
2
2
|
// The user's own statement comes first, then the user's own script, then what the build made, then source files.
|
|
3
|
+
import type { PackageManager } from "../../package-manager.ts";
|
|
3
4
|
import { isDevCommand } from "./dev-command.ts";
|
|
4
5
|
import type {
|
|
5
6
|
BuildOutput,
|
|
@@ -15,6 +16,7 @@ import type {
|
|
|
15
16
|
interface ServiceBeforeBuild {
|
|
16
17
|
config: ServiceConfig;
|
|
17
18
|
scripts: Record<string, string>;
|
|
19
|
+
packageManager: PackageManager;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
/** What a rule may look at once the build has run. */
|
|
@@ -123,14 +125,21 @@ function statedInServiceConfig({
|
|
|
123
125
|
return null;
|
|
124
126
|
}
|
|
125
127
|
|
|
126
|
-
/**
|
|
127
|
-
|
|
128
|
+
/**
|
|
129
|
+
* Rule 2: the repository's own start script, unless it runs a development server.
|
|
130
|
+
* The start names the script and does not copy its text, so the script runs as the package manager runs it:
|
|
131
|
+
* its `pre` script before it and its `post` script after it. The Launcher does that for `<package manager> run <script>`.
|
|
132
|
+
*/
|
|
133
|
+
function startScript({
|
|
134
|
+
scripts,
|
|
135
|
+
packageManager,
|
|
136
|
+
}: ServiceBeforeBuild): Resolved<Start> | null {
|
|
128
137
|
const script = findStartScript(scripts);
|
|
129
138
|
if (script === null || isDevCommand(script.command, scripts)) {
|
|
130
139
|
return null;
|
|
131
140
|
}
|
|
132
141
|
|
|
133
|
-
return command(script.
|
|
142
|
+
return command(`${packageManager} run ${script.name}`, "package.json");
|
|
134
143
|
}
|
|
135
144
|
|
|
136
145
|
/** Rule 3: a server file in a folder the build created. */
|