@hienlh/ppm 0.18.7 → 0.18.8
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/CHANGELOG.md +7 -0
- package/assets/skills/ppm/SKILL.md +1 -1
- package/assets/skills/ppm/references/cli-reference.md +4 -4
- package/assets/skills/ppm/references/http-api.md +2 -1
- package/dist/web/assets/{ai-resource-editor-DZQUyrsU.js → ai-resource-editor-BydGrxjb.js} +1 -1
- package/dist/web/assets/{audio-preview-GGksv6U8.js → audio-preview-DzBGq9Lk.js} +1 -1
- package/dist/web/assets/{chat-tab-Xd2WMjdX.js → chat-tab-pBGQ9SCY.js} +3 -3
- package/dist/web/assets/{code-editor-DqXYJWAB.js → code-editor-XviNXdzi.js} +2 -2
- package/dist/web/assets/{conflict-editor-Dg_JIcKq.js → conflict-editor-CRConSGG.js} +1 -1
- package/dist/web/assets/{csv-preview-DsJ_a2Op.js → csv-preview-BtCGfmrq.js} +1 -1
- package/dist/web/assets/{database-viewer-CzjnrnMz.js → database-viewer-2M5sKRqb.js} +1 -1
- package/dist/web/assets/{diff-viewer-DUNkO-ll.js → diff-viewer-BMr7vOvZ.js} +1 -1
- package/dist/web/assets/{docx-preview-DujpUDkE.js → docx-preview-BpG2kr_-.js} +1 -1
- package/dist/web/assets/{explorer-body-DSDJUQs7.js → explorer-body-DXufyj-9.js} +1 -1
- package/dist/web/assets/{explorer-window-content-CWYBapkj.js → explorer-window-content-B3krk4G1.js} +1 -1
- package/dist/web/assets/{extension-webview-DBTFznQp.js → extension-webview-12M45CGq.js} +1 -1
- package/dist/web/assets/{git-log-panel-D8ZQdoYl.js → git-log-panel-Cp4k3tX_.js} +1 -1
- package/dist/web/assets/{glide-data-grid-uIjdTT7c.js → glide-data-grid-B0BB0VDl.js} +1 -1
- package/dist/web/assets/{group-chat-tab-B3sryFdA.js → group-chat-tab-CcG_NXIS.js} +1 -1
- package/dist/web/assets/{image-preview-B0VXSpYu.js → image-preview-3HfuxA0N.js} +1 -1
- package/dist/web/assets/{index-DgbOF3xa.js → index-BAVBuKmT.js} +4 -4
- package/dist/web/assets/{keybindings-store-FyolpNND.js → keybindings-store-oMJmtpH4.js} +1 -1
- package/dist/web/assets/{markdown-renderer-BaBQ6Wm6.js → markdown-renderer-DPcoPjmO.js} +1 -1
- package/dist/web/assets/{markdown-renderer-feiuhJ9n.js → markdown-renderer-Dg5HE9Zq.js} +1 -1
- package/dist/web/assets/media-url-DlYsHq0S.js +1 -0
- package/dist/web/assets/{mobile-explorer-sheet-Zxjl-e4c.js → mobile-explorer-sheet-CEVcU964.js} +1 -1
- package/dist/web/assets/{notification-store-DnGpxgkF.js → notification-store-B8AqxwRT.js} +1 -1
- package/dist/web/assets/{pdf-preview-xzAA4-kD.js → pdf-preview-BslNoDFw.js} +1 -1
- package/dist/web/assets/{postgres-viewer-Dkec6GyK.js → postgres-viewer-BtSXQ3S4.js} +1 -1
- package/dist/web/assets/{settings-tab-Bd8e6iqY.js → settings-tab-COSUzGl8.js} +1 -1
- package/dist/web/assets/{sql-query-editor-Bz1XrUqI.js → sql-query-editor-CkvpmXyA.js} +1 -1
- package/dist/web/assets/{sqlite-viewer-D5-NZw6b.js → sqlite-viewer-CJbP6ruh.js} +1 -1
- package/dist/web/assets/{system-monitor-tab-A0jvt5Qw.js → system-monitor-tab-DThAFSd_.js} +1 -1
- package/dist/web/assets/{team-member-sheet-5Oaqm8I_.js → team-member-sheet-M83B4znI.js} +2 -2
- package/dist/web/assets/{team-member-window-content-DSpZ2zhL.js → team-member-window-content-Cxh-HF2V.js} +1 -1
- package/dist/web/assets/{terminal-tab-DXazqejW.js → terminal-tab-gQCs_fyx.js} +1 -1
- package/dist/web/assets/{tool-cards-Bgp8iceS.js → tool-cards-BG2fz83z.js} +2 -2
- package/dist/web/assets/{use-monaco-theme-CpF3A7JL.js → use-monaco-theme-DaqI7O-a.js} +1 -1
- package/dist/web/assets/{use-open-team-member-CUvV02fA.js → use-open-team-member-DKpstvLS.js} +1 -1
- package/dist/web/assets/{use-websocket-TnL2I1io.js → use-websocket-KVhDtbwK.js} +1 -1
- package/dist/web/assets/video-preview-CVEUDZBj.js +1 -0
- package/dist/web/index.html +1 -1
- package/dist/web/sw.js +1 -1
- package/package.json +1 -1
- package/src/server/helpers/media-route-handlers.ts +22 -3
- package/src/server/routes/files.ts +5 -2
- package/src/server/routes/fs-browse.ts +5 -2
- package/src/services/edge-forwarder.ts +191 -191
- package/src/services/fs-path-guard.service.ts +17 -1
- package/src/services/media-transcode/transcode-stream.ts +27 -5
- package/src/services/windows-process-tree.ts +351 -351
- package/src/web/components/editor/video-player/video-player.tsx +54 -22
- package/src/web/lib/media-url.ts +30 -4
- package/.release-run.sh +0 -5
- package/dist/web/assets/media-url-CUpN5vI1.js +0 -1
- package/dist/web/assets/video-preview-CwtLPELG.js +0 -1
- package/packages/vscode-compat/node_modules/.bin/tsc.bunx +0 -0
- package/packages/vscode-compat/node_modules/.bin/tsc.exe +0 -0
- package/packages/vscode-compat/node_modules/.bin/tsserver.bunx +0 -0
- package/packages/vscode-compat/node_modules/.bin/tsserver.exe +0 -0
|
@@ -1,191 +1,191 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Edge forwarder — owns the public port and pipes raw TCP to the server's
|
|
3
|
-
* current loopback port.
|
|
4
|
-
*
|
|
5
|
-
* ─── CRITICAL INVARIANT: THIS PROCESS MUST NEVER SPAWN A CHILD ───
|
|
6
|
-
*
|
|
7
|
-
* On Windows a child spawned with fd stdio inherits every inheritable handle,
|
|
8
|
-
* including a listening socket. When such a descendant is orphaned it keeps the
|
|
9
|
-
* socket open, so the port stays in LISTEN under a dead PID and can never be
|
|
10
|
-
* rebound — a "zombie port". The server hits this constantly because it spawns
|
|
11
|
-
* chat/tool/MCP subprocesses; those inherit its listener and wedge its port.
|
|
12
|
-
*
|
|
13
|
-
* The edge exists precisely because it spawns nothing, so its socket cannot be
|
|
14
|
-
* inherited and its port cannot zombie. cloudflared can therefore stay pinned
|
|
15
|
-
* to it forever and the public URL stops rotating. Adding any child-process
|
|
16
|
-
* call to this file reintroduces the exact bug the edge was built to remove.
|
|
17
|
-
*
|
|
18
|
-
* Raw TCP, never HTTP: WebSocket upgrades and SSE streams pass through
|
|
19
|
-
* untouched, and forwarded requests keep the headers cloudflared set.
|
|
20
|
-
*/
|
|
21
|
-
import net from "node:net";
|
|
22
|
-
import { resolveTargetPort, _resetTargetCache } from "./edge-target-resolver.ts";
|
|
23
|
-
|
|
24
|
-
/** Delay between attempts while waiting for the server to come back. */
|
|
25
|
-
const CONNECT_RETRY_MS = 250;
|
|
26
|
-
/**
|
|
27
|
-
* How long a client connection waits for a reachable server before giving up.
|
|
28
|
-
* During an upgrade the server is briefly absent; refusing instantly would
|
|
29
|
-
* surface as a broken page rather than a slow one.
|
|
30
|
-
*/
|
|
31
|
-
const CONNECT_WINDOW_MS = 5000;
|
|
32
|
-
|
|
33
|
-
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
34
|
-
|
|
35
|
-
/** Resolve once the socket is connected; reject on the first connect error. */
|
|
36
|
-
function connectUpstream(port: number): Promise<net.Socket> {
|
|
37
|
-
return new Promise((resolvePromise, reject) => {
|
|
38
|
-
const upstream = net.connect({
|
|
39
|
-
port,
|
|
40
|
-
host: "127.0.0.1", // never taken from status.json — loopback only
|
|
41
|
-
allowHalfOpen: true,
|
|
42
|
-
});
|
|
43
|
-
const onError = (err: Error) => {
|
|
44
|
-
upstream.destroy();
|
|
45
|
-
reject(err);
|
|
46
|
-
};
|
|
47
|
-
upstream.once("error", onError);
|
|
48
|
-
upstream.once("connect", () => {
|
|
49
|
-
upstream.removeListener("error", onError);
|
|
50
|
-
resolvePromise(upstream);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Largest amount of client data held while waiting for the server. Bounds the
|
|
57
|
-
* memory a slow upload can pin during an upgrade window.
|
|
58
|
-
*/
|
|
59
|
-
const MAX_PENDING_BYTES = 8 * 1024 * 1024;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Pipe one client connection to the server.
|
|
63
|
-
*
|
|
64
|
-
* Resolving the upstream is async, so the first bytes usually arrive before
|
|
65
|
-
* there is anywhere to send them. They must be captured by a `data` listener
|
|
66
|
-
* attached synchronously on this very tick and replayed once the upstream
|
|
67
|
-
* exists.
|
|
68
|
-
*
|
|
69
|
-
* `socket.pause()` is NOT a substitute — verified against Bun 1.3.13: bytes
|
|
70
|
-
* that arrive while paused are lost, and a later `pipe()`/`resume()` does not
|
|
71
|
-
* bring them back. A socket with no `data` listener drops them outright too.
|
|
72
|
-
* Both variants silently swallowed the first request in testing.
|
|
73
|
-
*/
|
|
74
|
-
async function handleConnection(
|
|
75
|
-
client: net.Socket,
|
|
76
|
-
connectWindowMs: number,
|
|
77
|
-
): Promise<void> {
|
|
78
|
-
const pending: Buffer[] = [];
|
|
79
|
-
let pendingBytes = 0;
|
|
80
|
-
let clientEnded = false;
|
|
81
|
-
const onEarlyData = (chunk: Buffer) => {
|
|
82
|
-
pendingBytes += chunk.length;
|
|
83
|
-
if (pendingBytes > MAX_PENDING_BYTES) {
|
|
84
|
-
client.destroy();
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
pending.push(chunk);
|
|
88
|
-
};
|
|
89
|
-
client.on("data", onEarlyData);
|
|
90
|
-
client.once("end", () => { clientEnded = true; });
|
|
91
|
-
|
|
92
|
-
let upstream: net.Socket | null = null;
|
|
93
|
-
let clientGone = false;
|
|
94
|
-
|
|
95
|
-
// A socket error with no listener is an unhandled 'error' event, which would
|
|
96
|
-
// take the whole edge process — and the public port — down with it.
|
|
97
|
-
const teardown = () => {
|
|
98
|
-
upstream?.destroy();
|
|
99
|
-
client.destroy();
|
|
100
|
-
};
|
|
101
|
-
client.on("error", teardown);
|
|
102
|
-
client.once("close", () => { clientGone = true; });
|
|
103
|
-
|
|
104
|
-
const deadline = Date.now() + connectWindowMs;
|
|
105
|
-
while (!clientGone && Date.now() < deadline) {
|
|
106
|
-
const port = resolveTargetPort();
|
|
107
|
-
if (port !== null) {
|
|
108
|
-
try {
|
|
109
|
-
upstream = await connectUpstream(port);
|
|
110
|
-
break;
|
|
111
|
-
} catch {
|
|
112
|
-
// Server not up yet (or moved mid-restart) — re-resolve and retry.
|
|
113
|
-
_resetTargetCache();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
await sleep(CONNECT_RETRY_MS);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (clientGone) return;
|
|
120
|
-
if (!upstream) {
|
|
121
|
-
client.destroy();
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
upstream.on("error", teardown);
|
|
126
|
-
|
|
127
|
-
// Replay what arrived during the connect, then hand both directions to the
|
|
128
|
-
// stream machinery. Removing the listener and piping happen in the same tick,
|
|
129
|
-
// so no chunk can slip through the gap.
|
|
130
|
-
client.off("data", onEarlyData);
|
|
131
|
-
for (const chunk of pending) upstream.write(chunk);
|
|
132
|
-
pending.length = 0;
|
|
133
|
-
|
|
134
|
-
client.pipe(upstream);
|
|
135
|
-
upstream.pipe(client);
|
|
136
|
-
|
|
137
|
-
// The client may have half-closed while we were still connecting.
|
|
138
|
-
if (clientEnded) upstream.end();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export interface EdgeForwarderOptions {
|
|
142
|
-
publicPort: number;
|
|
143
|
-
host?: string;
|
|
144
|
-
/** How long a client waits for a reachable server. Defaults to 5s. */
|
|
145
|
-
connectWindowMs?: number;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Bind the public port and start forwarding. Resolves once listening so the
|
|
150
|
-
* caller can fail fast when the port is unavailable.
|
|
151
|
-
*/
|
|
152
|
-
export function startEdgeForwarder(
|
|
153
|
-
opts: EdgeForwarderOptions,
|
|
154
|
-
): Promise<net.Server> {
|
|
155
|
-
const host = opts.host ?? "0.0.0.0";
|
|
156
|
-
const connectWindowMs = opts.connectWindowMs ?? CONNECT_WINDOW_MS;
|
|
157
|
-
return new Promise((resolvePromise, reject) => {
|
|
158
|
-
// allowHalfOpen keeps the other direction alive when one side sends FIN —
|
|
159
|
-
// long-lived chat WS and SSE streams depend on it.
|
|
160
|
-
const server = net.createServer({ allowHalfOpen: true }, (client) => {
|
|
161
|
-
void handleConnection(client, connectWindowMs);
|
|
162
|
-
});
|
|
163
|
-
server.once("error", reject);
|
|
164
|
-
server.listen(opts.publicPort, host, () => {
|
|
165
|
-
server.removeListener("error", reject);
|
|
166
|
-
resolvePromise(server);
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// ─── Process entry ─────────────────────────────────────────────────────
|
|
172
|
-
// Mirrors the `__serve__` guard in src/server/index.ts: the supervisor
|
|
173
|
-
// re-invokes the binary as `<bin> __edge__ <publicPort> <host>`.
|
|
174
|
-
if (process.argv.includes("__edge__")) {
|
|
175
|
-
const idx = process.argv.indexOf("__edge__");
|
|
176
|
-
const publicPort = parseInt(process.argv[idx + 1] ?? "", 10);
|
|
177
|
-
const host = process.argv[idx + 2] ?? "0.0.0.0";
|
|
178
|
-
|
|
179
|
-
if (!Number.isInteger(publicPort) || publicPort <= 0 || publicPort > 65535) {
|
|
180
|
-
process.stderr.write(`[edge] Invalid public port: ${process.argv[idx + 1]}\n`);
|
|
181
|
-
process.exit(2);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
try {
|
|
185
|
-
await startEdgeForwarder({ publicPort, host });
|
|
186
|
-
process.stderr.write(`[edge] Listening on ${host}:${publicPort}\n`);
|
|
187
|
-
} catch (err) {
|
|
188
|
-
process.stderr.write(`[edge] Failed to bind ${host}:${publicPort}: ${err}\n`);
|
|
189
|
-
process.exit(1);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Edge forwarder — owns the public port and pipes raw TCP to the server's
|
|
3
|
+
* current loopback port.
|
|
4
|
+
*
|
|
5
|
+
* ─── CRITICAL INVARIANT: THIS PROCESS MUST NEVER SPAWN A CHILD ───
|
|
6
|
+
*
|
|
7
|
+
* On Windows a child spawned with fd stdio inherits every inheritable handle,
|
|
8
|
+
* including a listening socket. When such a descendant is orphaned it keeps the
|
|
9
|
+
* socket open, so the port stays in LISTEN under a dead PID and can never be
|
|
10
|
+
* rebound — a "zombie port". The server hits this constantly because it spawns
|
|
11
|
+
* chat/tool/MCP subprocesses; those inherit its listener and wedge its port.
|
|
12
|
+
*
|
|
13
|
+
* The edge exists precisely because it spawns nothing, so its socket cannot be
|
|
14
|
+
* inherited and its port cannot zombie. cloudflared can therefore stay pinned
|
|
15
|
+
* to it forever and the public URL stops rotating. Adding any child-process
|
|
16
|
+
* call to this file reintroduces the exact bug the edge was built to remove.
|
|
17
|
+
*
|
|
18
|
+
* Raw TCP, never HTTP: WebSocket upgrades and SSE streams pass through
|
|
19
|
+
* untouched, and forwarded requests keep the headers cloudflared set.
|
|
20
|
+
*/
|
|
21
|
+
import net from "node:net";
|
|
22
|
+
import { resolveTargetPort, _resetTargetCache } from "./edge-target-resolver.ts";
|
|
23
|
+
|
|
24
|
+
/** Delay between attempts while waiting for the server to come back. */
|
|
25
|
+
const CONNECT_RETRY_MS = 250;
|
|
26
|
+
/**
|
|
27
|
+
* How long a client connection waits for a reachable server before giving up.
|
|
28
|
+
* During an upgrade the server is briefly absent; refusing instantly would
|
|
29
|
+
* surface as a broken page rather than a slow one.
|
|
30
|
+
*/
|
|
31
|
+
const CONNECT_WINDOW_MS = 5000;
|
|
32
|
+
|
|
33
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
34
|
+
|
|
35
|
+
/** Resolve once the socket is connected; reject on the first connect error. */
|
|
36
|
+
function connectUpstream(port: number): Promise<net.Socket> {
|
|
37
|
+
return new Promise((resolvePromise, reject) => {
|
|
38
|
+
const upstream = net.connect({
|
|
39
|
+
port,
|
|
40
|
+
host: "127.0.0.1", // never taken from status.json — loopback only
|
|
41
|
+
allowHalfOpen: true,
|
|
42
|
+
});
|
|
43
|
+
const onError = (err: Error) => {
|
|
44
|
+
upstream.destroy();
|
|
45
|
+
reject(err);
|
|
46
|
+
};
|
|
47
|
+
upstream.once("error", onError);
|
|
48
|
+
upstream.once("connect", () => {
|
|
49
|
+
upstream.removeListener("error", onError);
|
|
50
|
+
resolvePromise(upstream);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Largest amount of client data held while waiting for the server. Bounds the
|
|
57
|
+
* memory a slow upload can pin during an upgrade window.
|
|
58
|
+
*/
|
|
59
|
+
const MAX_PENDING_BYTES = 8 * 1024 * 1024;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Pipe one client connection to the server.
|
|
63
|
+
*
|
|
64
|
+
* Resolving the upstream is async, so the first bytes usually arrive before
|
|
65
|
+
* there is anywhere to send them. They must be captured by a `data` listener
|
|
66
|
+
* attached synchronously on this very tick and replayed once the upstream
|
|
67
|
+
* exists.
|
|
68
|
+
*
|
|
69
|
+
* `socket.pause()` is NOT a substitute — verified against Bun 1.3.13: bytes
|
|
70
|
+
* that arrive while paused are lost, and a later `pipe()`/`resume()` does not
|
|
71
|
+
* bring them back. A socket with no `data` listener drops them outright too.
|
|
72
|
+
* Both variants silently swallowed the first request in testing.
|
|
73
|
+
*/
|
|
74
|
+
async function handleConnection(
|
|
75
|
+
client: net.Socket,
|
|
76
|
+
connectWindowMs: number,
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
const pending: Buffer[] = [];
|
|
79
|
+
let pendingBytes = 0;
|
|
80
|
+
let clientEnded = false;
|
|
81
|
+
const onEarlyData = (chunk: Buffer) => {
|
|
82
|
+
pendingBytes += chunk.length;
|
|
83
|
+
if (pendingBytes > MAX_PENDING_BYTES) {
|
|
84
|
+
client.destroy();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
pending.push(chunk);
|
|
88
|
+
};
|
|
89
|
+
client.on("data", onEarlyData);
|
|
90
|
+
client.once("end", () => { clientEnded = true; });
|
|
91
|
+
|
|
92
|
+
let upstream: net.Socket | null = null;
|
|
93
|
+
let clientGone = false;
|
|
94
|
+
|
|
95
|
+
// A socket error with no listener is an unhandled 'error' event, which would
|
|
96
|
+
// take the whole edge process — and the public port — down with it.
|
|
97
|
+
const teardown = () => {
|
|
98
|
+
upstream?.destroy();
|
|
99
|
+
client.destroy();
|
|
100
|
+
};
|
|
101
|
+
client.on("error", teardown);
|
|
102
|
+
client.once("close", () => { clientGone = true; });
|
|
103
|
+
|
|
104
|
+
const deadline = Date.now() + connectWindowMs;
|
|
105
|
+
while (!clientGone && Date.now() < deadline) {
|
|
106
|
+
const port = resolveTargetPort();
|
|
107
|
+
if (port !== null) {
|
|
108
|
+
try {
|
|
109
|
+
upstream = await connectUpstream(port);
|
|
110
|
+
break;
|
|
111
|
+
} catch {
|
|
112
|
+
// Server not up yet (or moved mid-restart) — re-resolve and retry.
|
|
113
|
+
_resetTargetCache();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
await sleep(CONNECT_RETRY_MS);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (clientGone) return;
|
|
120
|
+
if (!upstream) {
|
|
121
|
+
client.destroy();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
upstream.on("error", teardown);
|
|
126
|
+
|
|
127
|
+
// Replay what arrived during the connect, then hand both directions to the
|
|
128
|
+
// stream machinery. Removing the listener and piping happen in the same tick,
|
|
129
|
+
// so no chunk can slip through the gap.
|
|
130
|
+
client.off("data", onEarlyData);
|
|
131
|
+
for (const chunk of pending) upstream.write(chunk);
|
|
132
|
+
pending.length = 0;
|
|
133
|
+
|
|
134
|
+
client.pipe(upstream);
|
|
135
|
+
upstream.pipe(client);
|
|
136
|
+
|
|
137
|
+
// The client may have half-closed while we were still connecting.
|
|
138
|
+
if (clientEnded) upstream.end();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface EdgeForwarderOptions {
|
|
142
|
+
publicPort: number;
|
|
143
|
+
host?: string;
|
|
144
|
+
/** How long a client waits for a reachable server. Defaults to 5s. */
|
|
145
|
+
connectWindowMs?: number;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Bind the public port and start forwarding. Resolves once listening so the
|
|
150
|
+
* caller can fail fast when the port is unavailable.
|
|
151
|
+
*/
|
|
152
|
+
export function startEdgeForwarder(
|
|
153
|
+
opts: EdgeForwarderOptions,
|
|
154
|
+
): Promise<net.Server> {
|
|
155
|
+
const host = opts.host ?? "0.0.0.0";
|
|
156
|
+
const connectWindowMs = opts.connectWindowMs ?? CONNECT_WINDOW_MS;
|
|
157
|
+
return new Promise((resolvePromise, reject) => {
|
|
158
|
+
// allowHalfOpen keeps the other direction alive when one side sends FIN —
|
|
159
|
+
// long-lived chat WS and SSE streams depend on it.
|
|
160
|
+
const server = net.createServer({ allowHalfOpen: true }, (client) => {
|
|
161
|
+
void handleConnection(client, connectWindowMs);
|
|
162
|
+
});
|
|
163
|
+
server.once("error", reject);
|
|
164
|
+
server.listen(opts.publicPort, host, () => {
|
|
165
|
+
server.removeListener("error", reject);
|
|
166
|
+
resolvePromise(server);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ─── Process entry ─────────────────────────────────────────────────────
|
|
172
|
+
// Mirrors the `__serve__` guard in src/server/index.ts: the supervisor
|
|
173
|
+
// re-invokes the binary as `<bin> __edge__ <publicPort> <host>`.
|
|
174
|
+
if (process.argv.includes("__edge__")) {
|
|
175
|
+
const idx = process.argv.indexOf("__edge__");
|
|
176
|
+
const publicPort = parseInt(process.argv[idx + 1] ?? "", 10);
|
|
177
|
+
const host = process.argv[idx + 2] ?? "0.0.0.0";
|
|
178
|
+
|
|
179
|
+
if (!Number.isInteger(publicPort) || publicPort <= 0 || publicPort > 65535) {
|
|
180
|
+
process.stderr.write(`[edge] Invalid public port: ${process.argv[idx + 1]}\n`);
|
|
181
|
+
process.exit(2);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
await startEdgeForwarder({ publicPort, host });
|
|
186
|
+
process.stderr.write(`[edge] Listening on ${host}:${publicPort}\n`);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
process.stderr.write(`[edge] Failed to bind ${host}:${publicPort}: ${err}\n`);
|
|
189
|
+
process.exit(1);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -2,6 +2,7 @@ import { resolve, sep } from "node:path";
|
|
|
2
2
|
import { lstat, realpath } from "node:fs/promises";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { getPpmDir } from "./ppm-dir.ts";
|
|
5
|
+
import { getUploadsDir } from "./chat-upload-storage.service.ts";
|
|
5
6
|
import { realPathOrSelf, realPathOrSelfSync } from "./fs-ops/fs-real-path.ts";
|
|
6
7
|
|
|
7
8
|
// Re-exported so callers reach both the guards and the resolver they build on
|
|
@@ -84,13 +85,28 @@ export function isPpmDirPath(resolved: string): boolean {
|
|
|
84
85
|
return isInside(resolved, getPpmDir());
|
|
85
86
|
}
|
|
86
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Chat attachments the user uploaded through the UI. They sit under the PPM
|
|
90
|
+
* dir so chat history keeps resolving them across reboots, but they are
|
|
91
|
+
* ordinary user content, not credential material: the transcript has to render
|
|
92
|
+
* the very image the assistant just read back from that directory.
|
|
93
|
+
*/
|
|
94
|
+
export function isChatUploadPath(resolved: string): boolean {
|
|
95
|
+
return isInside(resolved, getUploadsDir());
|
|
96
|
+
}
|
|
97
|
+
|
|
87
98
|
/**
|
|
88
99
|
* Refuse the PPM directory subtree on read-style doors. It stores the config
|
|
89
100
|
* database with provider credentials and auth tokens, which must never be
|
|
90
101
|
* downloadable through a generic file route.
|
|
102
|
+
*
|
|
103
|
+
* Chat uploads are the one exception. Every caller applies this to the
|
|
104
|
+
* requested path *and* to its real path, so a symlink parked in the uploads
|
|
105
|
+
* directory still fails on the second call and cannot reach the rest of the
|
|
106
|
+
* PPM dir through the exception.
|
|
91
107
|
*/
|
|
92
108
|
export function assertNotPpmDir(resolved: string): void {
|
|
93
|
-
if (isPpmDirPath(resolved)) {
|
|
109
|
+
if (isPpmDirPath(resolved) && !isChatUploadPath(resolved)) {
|
|
94
110
|
throw Object.assign(new Error("Access denied"), { status: 403, code: "EDENIED" });
|
|
95
111
|
}
|
|
96
112
|
}
|
|
@@ -8,21 +8,36 @@
|
|
|
8
8
|
* by re-requesting with `start`, which becomes `-ss` *before* `-i` (fast keyframe
|
|
9
9
|
* seek; exact-frame seeking would decode from the file start every time).
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* Process lifetime is controlled explicitly, not only by the connection: a job is
|
|
12
|
+
* keyed by the player's `sessionId`, a new request for the same session kills the
|
|
13
|
+
* previous job, and the player sends a stop when it unmounts. Relying on the client
|
|
14
|
+
* disconnect alone is not enough — Cloudflare Tunnel (and other proxies) keep the
|
|
15
|
+
* origin request open and drain the body after the browser has gone, which left
|
|
16
|
+
* every seek's ffmpeg running to the end of the file and exhausted the job slots.
|
|
17
|
+
* Only the job's own PID is ever killed — never a blanket kill by image name.
|
|
14
18
|
*/
|
|
15
19
|
import { encoderArgs, getFfmpegCapabilities } from "./ffmpeg-capabilities.ts";
|
|
16
20
|
|
|
17
|
-
/** Parallel ffmpeg jobs allowed
|
|
18
|
-
export const MAX_CONCURRENT_TRANSCODES =
|
|
21
|
+
/** Parallel ffmpeg jobs allowed across all players (one per player after session replacement). */
|
|
22
|
+
export const MAX_CONCURRENT_TRANSCODES = 4;
|
|
19
23
|
let active = 0;
|
|
20
24
|
|
|
25
|
+
/** Live jobs by player session so a seek or unmount can stop the right one. */
|
|
26
|
+
const jobsBySession = new Map<string, () => void>();
|
|
27
|
+
|
|
21
28
|
/** How many transcodes are running right now (exposed for tests/diagnostics). */
|
|
22
29
|
export function activeTranscodeCount(): number {
|
|
23
30
|
return active;
|
|
24
31
|
}
|
|
25
32
|
|
|
33
|
+
/** Kill the job a player owns. Returns false when nothing was running for it. */
|
|
34
|
+
export function stopTranscode(sessionId: string): boolean {
|
|
35
|
+
const kill = jobsBySession.get(sessionId);
|
|
36
|
+
if (!kill) return false;
|
|
37
|
+
kill();
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
26
41
|
export class TranscodeUnavailableError extends Error {
|
|
27
42
|
constructor(msg = "ffmpeg is not installed or has no working H.264 encoder") {
|
|
28
43
|
super(msg);
|
|
@@ -42,6 +57,8 @@ export interface TranscodeOptions {
|
|
|
42
57
|
start?: number;
|
|
43
58
|
/** Aborts the job when the HTTP request is aborted. */
|
|
44
59
|
signal?: AbortSignal;
|
|
60
|
+
/** Player instance id; a new job for the same id replaces (kills) the previous one. */
|
|
61
|
+
sessionId?: string;
|
|
45
62
|
}
|
|
46
63
|
|
|
47
64
|
/** Build the ffmpeg argv; pure so tests can assert on it without spawning. */
|
|
@@ -72,6 +89,8 @@ export interface TranscodeJob {
|
|
|
72
89
|
export async function startTranscode(absPath: string, opts: TranscodeOptions = {}): Promise<TranscodeJob> {
|
|
73
90
|
const caps = await getFfmpegCapabilities();
|
|
74
91
|
if (!caps.ffmpeg || !caps.encoder) throw new TranscodeUnavailableError();
|
|
92
|
+
// A seek is a replacement, not an addition: free this player's slot before counting.
|
|
93
|
+
if (opts.sessionId) stopTranscode(opts.sessionId);
|
|
75
94
|
if (active >= MAX_CONCURRENT_TRANSCODES) throw new TranscodeBusyError();
|
|
76
95
|
|
|
77
96
|
const proc = Bun.spawn(buildTranscodeArgs(caps.ffmpeg, absPath, caps.encoder, opts.start), {
|
|
@@ -85,6 +104,8 @@ export async function startTranscode(absPath: string, opts: TranscodeOptions = {
|
|
|
85
104
|
if (finished) return;
|
|
86
105
|
finished = true;
|
|
87
106
|
active--;
|
|
107
|
+
// Only drop the registration if it is still ours — a replacement may already own the key.
|
|
108
|
+
if (opts.sessionId && jobsBySession.get(opts.sessionId) === kill) jobsBySession.delete(opts.sessionId);
|
|
88
109
|
};
|
|
89
110
|
|
|
90
111
|
// Drain stderr so ffmpeg never blocks on a full pipe; keep the tail for the log.
|
|
@@ -102,6 +123,7 @@ export async function startTranscode(absPath: string, opts: TranscodeOptions = {
|
|
|
102
123
|
finish();
|
|
103
124
|
};
|
|
104
125
|
opts.signal?.addEventListener("abort", kill, { once: true });
|
|
126
|
+
if (opts.sessionId) jobsBySession.set(opts.sessionId, kill);
|
|
105
127
|
|
|
106
128
|
// Wrap stdout so a cancelled response body (client went away) kills this process.
|
|
107
129
|
//
|