repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(web): handle stdin epipe

a50a8b49a624be8d136ea5a40ac77be1091762a3

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

extensions/web/__tests__/web-fetch-contract.test.ts

Modified
1 -import { mkdtemp, readFile, writeFile } from "node:fs/promises";
1 +import { chmod, mkdtemp, readFile, writeFile } from "node:fs/promises";
2 2 import { tmpdir } from "node:os";
3 3 import path from "node:path";
4 4 import { afterEach, describe, expect, it, vi } from "vitest";
16 16 isPrivateOrReservedIp,
17 17 resolveRedirectUrl,
18 18 } from "../safety";
19 +import { spawnToText } from "../spawn";
19 20 import { platformStateDir, projectStoreDir, saveJsonResult } from "../store";
21 22 const originalFetch = globalThis.fetch;
45 +async function makeEarlyExitCommand(dir: string, name: string): Promise<void> {
46 + if (process.platform === "win32") {
47 + await writeFile(
48 + path.join(dir, `${name}.cmd`),
49 + "@echo off\r\nexit /b 0\r\n",
50 + );
51 + return;
52 + }
53 + const file = path.join(dir, name);
54 + await writeFile(file, "#!/bin/sh\nexit 0\n");
55 + await chmod(file, 0o755);
44 58 function pathEnv(dir: string): Record<string, string> {
45 59 return process.platform === "win32"
46 60 ? { PATH: dir, Path: dir, PATHEXT: ".COM;.EXE;.BAT;.CMD" }
88 102 expect(saved.output).toContain("x".repeat(10_000));
105 + it("handles commands that exit before stdin is written", async () => {
106 + const binDir = await mkdtemp(path.join(tmpdir(), "pi-web-early-exit-"));
107 + await makeEarlyExitCommand(binDir, "early");
108 + const oldPath = process.env.PATH;
109 + process.env.PATH = binDir;
110 + try {
111 + await expect(
112 + spawnToText("early", [], "x".repeat(10_000_000)),
113 + ).resolves.toBe("");
114 + } finally {
115 + process.env.PATH = oldPath;
117 + });
91 119 it("converts HTML with PATH-resolved pandoc and no Unix which", async () => {
92 120 globalThis.fetch = vi.fn(
93 121 async () =>

extensions/web/spawn.ts

Modified
28 28 proc.stdout.on("data", (d: Buffer) => (stdout += d));
29 29 proc.stderr.on("data", (d: Buffer) => (stderr += d));
30 + proc.stdin.on("error", (err: NodeJS.ErrnoException) => {
31 + if (err.code !== "EPIPE") fail(err);
32 + });
30 33 proc.on("close", (code) => {
31 34 if (settled) return;
32 35 settled = true;