@decocms/tanstack 7.85.1 → 7.85.3
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/package.json +4 -9
- package/src/vite/plugin.js +9 -58
- package/src/daemon/auth.ts +0 -204
- package/src/daemon/fs.ts +0 -238
- package/src/daemon/index.ts +0 -8
- package/src/daemon/middleware.ts +0 -156
- package/src/daemon/tunnel.ts +0 -129
- package/src/daemon/volumes.ts +0 -366
- package/src/daemon/watch.ts +0 -272
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/tanstack",
|
|
3
|
-
"version": "7.85.
|
|
3
|
+
"version": "7.85.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Deco framework binding for TanStack Start + Cloudflare Workers",
|
|
6
6
|
"repository": {
|
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
"exports": {
|
|
13
13
|
".": "./src/index.ts",
|
|
14
14
|
"./vite": "./src/vite/plugin.js",
|
|
15
|
-
"./daemon": "./src/daemon/index.ts",
|
|
16
15
|
"./sdk/createInvoke": "./src/sdk/createInvoke.ts",
|
|
17
16
|
"./sdk/cookiePassthrough": "./src/sdk/cookiePassthrough.ts",
|
|
18
17
|
"./sdk/deferredSectionLoader": "./src/sdk/deferredSectionLoader.ts",
|
|
@@ -27,12 +26,9 @@
|
|
|
27
26
|
"lint:unused": "knip"
|
|
28
27
|
},
|
|
29
28
|
"dependencies": {
|
|
30
|
-
"@decocms/blocks": "7.85.
|
|
31
|
-
"@decocms/blocks-admin": "7.85.
|
|
32
|
-
"@decocms/blocks-cli": "7.85.
|
|
33
|
-
"@deco-cx/warp-node": "^0.3.16",
|
|
34
|
-
"fast-json-patch": "^3.1.0",
|
|
35
|
-
"ws": "^8.18.0"
|
|
29
|
+
"@decocms/blocks": "7.85.3",
|
|
30
|
+
"@decocms/blocks-admin": "7.85.3",
|
|
31
|
+
"@decocms/blocks-cli": "7.85.3"
|
|
36
32
|
},
|
|
37
33
|
"peerDependencies": {
|
|
38
34
|
"@tanstack/react-query": ">=5.0.0",
|
|
@@ -47,7 +43,6 @@
|
|
|
47
43
|
"@tanstack/store": "^0.9.1",
|
|
48
44
|
"@types/react": "^19.0.0",
|
|
49
45
|
"@types/react-dom": "^19.0.0",
|
|
50
|
-
"@types/ws": "^8.18.0",
|
|
51
46
|
"knip": "^5.86.0",
|
|
52
47
|
"typescript": "^5.9.0",
|
|
53
48
|
"vite": ">=6.0.0"
|
package/src/vite/plugin.js
CHANGED
|
@@ -498,13 +498,13 @@ export function decoVitePlugin({ fastDeploy = "auto", metaAsset = false } = {})
|
|
|
498
498
|
// out above), so this plugin's no-invalidation delta machinery below
|
|
499
499
|
// stays the TanStack dev-reload path until that's fixed upstream.
|
|
500
500
|
//
|
|
501
|
-
// Generator is loaded lazily via tsImport
|
|
502
|
-
//
|
|
501
|
+
// Generator is loaded lazily via tsImport so we don't depend on the
|
|
502
|
+
// consumer's TS loader.
|
|
503
503
|
const cwd = process.cwd();
|
|
504
504
|
|
|
505
505
|
// Vite's CORS middleware ends every preflight itself and, since Vite 6,
|
|
506
506
|
// allows only localhost origins, so a cross-origin admin read that needs
|
|
507
|
-
// one (Studio's conditional `/live/_meta`
|
|
507
|
+
// one (Studio's conditional `/live/_meta`) never
|
|
508
508
|
// reaches the worker's own answer. Vite installs it before running
|
|
509
509
|
// configureServer, hence the front of the stack rather than `use()`.
|
|
510
510
|
// That stack is connect's, not Vite's API: without it, only this
|
|
@@ -527,9 +527,8 @@ export function decoVitePlugin({ fastDeploy = "auto", metaAsset = false } = {})
|
|
|
527
527
|
const jsonFile = outFile.replace(/\.ts$/, ".json");
|
|
528
528
|
|
|
529
529
|
// Lazily load the block generator module (generateBlocks for the cold-start
|
|
530
|
-
// bootstrap, readBlockDelta for live edits).
|
|
531
|
-
//
|
|
532
|
-
// registering a global hook.
|
|
530
|
+
// bootstrap, readBlockDelta for live edits). tsImport keeps `tsx` scoped to
|
|
531
|
+
// this single import instead of registering a global hook.
|
|
533
532
|
let genModule;
|
|
534
533
|
const loadGenModule = () => {
|
|
535
534
|
if (genModule) return Promise.resolve(genModule);
|
|
@@ -913,53 +912,6 @@ export function decoVitePlugin({ fastDeploy = "auto", metaAsset = false } = {})
|
|
|
913
912
|
server.watcher.on("add", (file) => {
|
|
914
913
|
if (isSchemaSource(file)) scheduleScaffoldGen();
|
|
915
914
|
});
|
|
916
|
-
|
|
917
|
-
// Tunnel + daemon: connect local dev to admin.deco.cx
|
|
918
|
-
// Activated only when both DECO_SITE_NAME and DECO_ENV_NAME are set.
|
|
919
|
-
// Omitting DECO_ENV_NAME runs Vite fully local (no tunnel registration),
|
|
920
|
-
// since DECO_SITE_NAME alone is also consumed by site builds via vite's
|
|
921
|
-
// `define` for `process.env.DECO_SITE_NAME` and shouldn't force a tunnel.
|
|
922
|
-
const siteName = process.env.DECO_SITE_NAME;
|
|
923
|
-
const envName = process.env.DECO_ENV_NAME;
|
|
924
|
-
if (siteName && envName) {
|
|
925
|
-
// Daemon files are .ts and live inside node_modules. Node's
|
|
926
|
-
// experimental strip-types refuses to transpile node_modules, so
|
|
927
|
-
// a plain dynamic `import()` blows up under `vite dev`. Use tsx's
|
|
928
|
-
// ad-hoc loader (`tsImport`) — scoped to this import, doesn't
|
|
929
|
-
// register a global hook.
|
|
930
|
-
const loadDaemon = (specifier) =>
|
|
931
|
-
import("tsx/esm/api").then(({ tsImport }) => tsImport(specifier, import.meta.url));
|
|
932
|
-
|
|
933
|
-
// Add daemon middleware (x-daemon-api interception + auth + volumes + SSE + admin routes)
|
|
934
|
-
loadDaemon("../daemon/middleware.ts")
|
|
935
|
-
.then(({ createDaemonMiddleware }) => {
|
|
936
|
-
server.middlewares.use(createDaemonMiddleware({ site: siteName, server }));
|
|
937
|
-
})
|
|
938
|
-
.catch((err) => {
|
|
939
|
-
console.warn("[deco] Failed to load daemon middleware:", err.message);
|
|
940
|
-
});
|
|
941
|
-
|
|
942
|
-
// Start tunnel after HTTP server is listening (so we know the real port)
|
|
943
|
-
server.httpServer?.once("listening", async () => {
|
|
944
|
-
const addr = server.httpServer?.address();
|
|
945
|
-
const port = typeof addr === "object" && addr ? addr.port : 5173;
|
|
946
|
-
try {
|
|
947
|
-
const { startTunnel } = await loadDaemon("../daemon/tunnel.ts");
|
|
948
|
-
const tunnel = await startTunnel({
|
|
949
|
-
site: siteName,
|
|
950
|
-
env: envName,
|
|
951
|
-
port,
|
|
952
|
-
// Default to the .deco.host relay (matches startTunnel's documented
|
|
953
|
-
// default). Set DECO_HOST=false to opt back into the legacy
|
|
954
|
-
// simpletunnel.deco.site relay.
|
|
955
|
-
decoHost: process.env.DECO_HOST !== "false",
|
|
956
|
-
});
|
|
957
|
-
server.httpServer?.on("close", () => tunnel.close());
|
|
958
|
-
} catch (err) {
|
|
959
|
-
console.warn("[deco] Failed to start tunnel:", err.message);
|
|
960
|
-
}
|
|
961
|
-
});
|
|
962
|
-
}
|
|
963
915
|
},
|
|
964
916
|
|
|
965
917
|
config(_cfg, { command }) {
|
|
@@ -996,13 +948,12 @@ export function decoVitePlugin({ fastDeploy = "auto", metaAsset = false } = {})
|
|
|
996
948
|
};
|
|
997
949
|
}
|
|
998
950
|
|
|
999
|
-
//
|
|
1000
|
-
//
|
|
1001
|
-
//
|
|
1002
|
-
// boilerplate.
|
|
951
|
+
// Let Studio's hosts through Vite's host check. .deco.studio is the admin
|
|
952
|
+
// frontend; multiple real-world Deco sites duplicated this list to include
|
|
953
|
+
// it — bundling it here removes that boilerplate.
|
|
1003
954
|
if (process.env.DECO_SITE_NAME) {
|
|
1004
955
|
cfg.server = {
|
|
1005
|
-
allowedHosts: [".
|
|
956
|
+
allowedHosts: [".decocdn.com", ".deco.studio"],
|
|
1006
957
|
};
|
|
1007
958
|
}
|
|
1008
959
|
|
package/src/daemon/auth.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JWT verification for admin.deco.cx requests.
|
|
3
|
-
* Uses Web Crypto only — no external dependencies.
|
|
4
|
-
*
|
|
5
|
-
* Ported from: deco-cx/deco daemon/auth.ts + commons/jwt/*
|
|
6
|
-
*/
|
|
7
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
8
|
-
|
|
9
|
-
// ---------------------------------------------------------------------------
|
|
10
|
-
// Public key — same key used by all sites (from commons/jwt/trusted.ts)
|
|
11
|
-
// ---------------------------------------------------------------------------
|
|
12
|
-
|
|
13
|
-
const ADMIN_PUBLIC_KEY =
|
|
14
|
-
process.env.DECO_ADMIN_PUBLIC_KEY ??
|
|
15
|
-
"eyJrdHkiOiJSU0EiLCJhbGciOiJSUzI1NiIsIm4iOiJ1N0Y3UklDN19Zc3ljTFhEYlBvQ1pUQnM2elZ6VjVPWkhXQ0M4akFZeFdPUnByem9WNDJDQ1JBVkVOVjJldzk1MnJOX2FTMmR3WDlmVGRvdk9zWl9jX2RVRXctdGlPN3hJLXd0YkxsanNUbUhoNFpiYXU0aUVoa0o1VGNHc2VaelhFYXNOSEhHdUo4SzY3WHluRHJSX0h4Ym9kQ2YxNFFJTmc5QnJjT3FNQmQyMUl4eUctVVhQampBTnRDTlNici1rXzFKeTZxNmtPeVJ1ZmV2Mjl0djA4Ykh5WDJQenp5Tnp3RWpjY0lROWpmSFdMN0JXX2tzdFpOOXU3TUtSLWJ4bjlSM0FKMEpZTHdXR3VnZGpNdVpBRnk0dm5BUXZzTk5Cd3p2YnFzMnZNd0dDTnF1ZE1tVmFudlNzQTJKYkE3Q0JoazI5TkRFTXRtUS1wbmo1cUlYSlEiLCJlIjoiQVFBQiIsImtleV9vcHMiOlsidmVyaWZ5Il0sImV4dCI6dHJ1ZX0";
|
|
16
|
-
|
|
17
|
-
const BYPASS_JWT =
|
|
18
|
-
process.env.DANGEROUSLY_ALLOW_PUBLIC_ACCESS === "true";
|
|
19
|
-
|
|
20
|
-
// ---------------------------------------------------------------------------
|
|
21
|
-
// JWT types
|
|
22
|
-
// ---------------------------------------------------------------------------
|
|
23
|
-
|
|
24
|
-
export interface JwtPayload {
|
|
25
|
-
[key: string]: unknown;
|
|
26
|
-
iss?: string;
|
|
27
|
-
sub?: string;
|
|
28
|
-
aud?: string | string[];
|
|
29
|
-
exp?: number;
|
|
30
|
-
nbf?: number;
|
|
31
|
-
iat?: number;
|
|
32
|
-
jti?: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// ---------------------------------------------------------------------------
|
|
36
|
-
// Crypto helpers — ported from commons/jwt/keys.ts
|
|
37
|
-
// ---------------------------------------------------------------------------
|
|
38
|
-
|
|
39
|
-
const ALG = "RSASSA-PKCS1-v1_5";
|
|
40
|
-
const HASH = "SHA-256";
|
|
41
|
-
|
|
42
|
-
function parseJWK(b64: string): JsonWebKey {
|
|
43
|
-
return JSON.parse(atob(b64));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
let cachedKey: Promise<CryptoKey> | null = null;
|
|
47
|
-
|
|
48
|
-
function getAdminPublicKey(): Promise<CryptoKey> {
|
|
49
|
-
cachedKey ??= crypto.subtle.importKey(
|
|
50
|
-
"jwk",
|
|
51
|
-
parseJWK(ADMIN_PUBLIC_KEY),
|
|
52
|
-
{ name: ALG, hash: HASH },
|
|
53
|
-
false,
|
|
54
|
-
["verify"],
|
|
55
|
-
);
|
|
56
|
-
return cachedKey;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// ---------------------------------------------------------------------------
|
|
60
|
-
// JWT verification — ported from commons/jwt/jwt.ts
|
|
61
|
-
// ---------------------------------------------------------------------------
|
|
62
|
-
|
|
63
|
-
function base64UrlDecode(str: string): Uint8Array {
|
|
64
|
-
const b64 = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
65
|
-
const pad = b64.length % 4 === 0 ? "" : "=".repeat(4 - (b64.length % 4));
|
|
66
|
-
const binary = atob(b64 + pad);
|
|
67
|
-
const bytes = new Uint8Array(binary.length);
|
|
68
|
-
for (let i = 0; i < binary.length; i++) {
|
|
69
|
-
bytes[i] = binary.charCodeAt(i);
|
|
70
|
-
}
|
|
71
|
-
return bytes;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export async function verifyAdminJwt(
|
|
75
|
-
token: string,
|
|
76
|
-
): Promise<JwtPayload | null> {
|
|
77
|
-
const parts = token.split(".");
|
|
78
|
-
if (parts.length !== 3) return null;
|
|
79
|
-
|
|
80
|
-
const [headerB64, payloadB64, signatureB64] = parts;
|
|
81
|
-
const signingInput = new TextEncoder().encode(
|
|
82
|
-
`${headerB64}.${payloadB64}`,
|
|
83
|
-
);
|
|
84
|
-
const signature = base64UrlDecode(signatureB64);
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
const key = await getAdminPublicKey();
|
|
88
|
-
const valid = await crypto.subtle.verify(
|
|
89
|
-
ALG,
|
|
90
|
-
key,
|
|
91
|
-
new Uint8Array(signature),
|
|
92
|
-
new Uint8Array(signingInput),
|
|
93
|
-
);
|
|
94
|
-
if (!valid) return null;
|
|
95
|
-
} catch {
|
|
96
|
-
return null;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
const payload: JwtPayload = JSON.parse(
|
|
101
|
-
new TextDecoder().decode(base64UrlDecode(payloadB64)),
|
|
102
|
-
);
|
|
103
|
-
return payload;
|
|
104
|
-
} catch {
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// ---------------------------------------------------------------------------
|
|
110
|
-
// URN matching — ported from commons/jwt/engine.ts
|
|
111
|
-
// ---------------------------------------------------------------------------
|
|
112
|
-
|
|
113
|
-
function matchPart(urnPart: string, otherPart: string): boolean {
|
|
114
|
-
return urnPart === "*" || otherPart === urnPart;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function matchParts(urn: string[], resource: string[]): boolean {
|
|
118
|
-
return urn.every((part, idx) => matchPart(part, resource[idx]));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function matches(urnParts: string[]) {
|
|
122
|
-
return (resourceUrn: string) => {
|
|
123
|
-
const resourceParts = resourceUrn.split(":");
|
|
124
|
-
if (resourceParts.length > urnParts.length) return false;
|
|
125
|
-
const lastIdx = resourceParts.length - 1;
|
|
126
|
-
return resourceParts.every((part, idx) => {
|
|
127
|
-
if (part === "*") return true;
|
|
128
|
-
if (lastIdx === idx) {
|
|
129
|
-
return matchParts(part.split("/"), urnParts[idx].split("/"));
|
|
130
|
-
}
|
|
131
|
-
return part === urnParts[idx];
|
|
132
|
-
});
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export function tokenIsValid(site: string, jwt: JwtPayload): boolean {
|
|
137
|
-
const { iss, sub, exp } = jwt;
|
|
138
|
-
if (!iss || !sub) return false;
|
|
139
|
-
if (exp && exp * 1000 <= Date.now()) return false;
|
|
140
|
-
const siteUrn = `urn:deco:site:*:${site}:deployment/*`;
|
|
141
|
-
return matches(sub.split(":"))(siteUrn);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// ---------------------------------------------------------------------------
|
|
145
|
-
// Auth middleware for Connect (Vite dev server)
|
|
146
|
-
// ---------------------------------------------------------------------------
|
|
147
|
-
|
|
148
|
-
function extractToken(req: IncomingMessage): string | null {
|
|
149
|
-
const auth = req.headers.authorization;
|
|
150
|
-
if (auth) {
|
|
151
|
-
const parts = auth.split(/\s+/);
|
|
152
|
-
if (parts.length === 2) return parts[1];
|
|
153
|
-
}
|
|
154
|
-
// Fallback: ?token= query param
|
|
155
|
-
try {
|
|
156
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
157
|
-
const t = url.searchParams.get("token");
|
|
158
|
-
if (t) return t;
|
|
159
|
-
} catch {
|
|
160
|
-
// ignore
|
|
161
|
-
}
|
|
162
|
-
return null;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export type NextFn = () => void;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Returns a Connect-style middleware that verifies JWT on every request.
|
|
169
|
-
* If invalid, responds 401/403. If valid (or bypass enabled), calls next().
|
|
170
|
-
*/
|
|
171
|
-
export function createAuthMiddleware(site: string) {
|
|
172
|
-
return async (
|
|
173
|
-
req: IncomingMessage,
|
|
174
|
-
res: ServerResponse,
|
|
175
|
-
next: NextFn,
|
|
176
|
-
): Promise<void> => {
|
|
177
|
-
if (BYPASS_JWT) {
|
|
178
|
-
next();
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const token = extractToken(req);
|
|
183
|
-
if (!token) {
|
|
184
|
-
res.writeHead(401);
|
|
185
|
-
res.end();
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const jwt = await verifyAdminJwt(token);
|
|
190
|
-
if (!jwt) {
|
|
191
|
-
res.writeHead(401);
|
|
192
|
-
res.end();
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (!tokenIsValid(site, jwt)) {
|
|
197
|
-
res.writeHead(403);
|
|
198
|
-
res.end();
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
next();
|
|
203
|
-
};
|
|
204
|
-
}
|
package/src/daemon/fs.ts
DELETED
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Filesystem REST API — read, patch, delete .deco/ files.
|
|
3
|
-
*
|
|
4
|
-
* The admin UI reads individual files via GET /fs/file/<path>.
|
|
5
|
-
* This is separate from the volumes/realtime WebSocket API.
|
|
6
|
-
*
|
|
7
|
-
* Ported from: deco-cx/deco daemon/fs/api.ts
|
|
8
|
-
*/
|
|
9
|
-
import { readFile, writeFile, rm, stat, mkdir } from "node:fs/promises";
|
|
10
|
-
import { join, resolve, sep } from "node:path";
|
|
11
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
12
|
-
import fjp from "fast-json-patch";
|
|
13
|
-
import type { Operation } from "fast-json-patch";
|
|
14
|
-
import { inferMetadata, broadcastFSEvent, type Metadata } from "./watch";
|
|
15
|
-
|
|
16
|
-
const cwd = process.cwd();
|
|
17
|
-
const toPosix = (p: string) => p.replaceAll(sep, "/");
|
|
18
|
-
|
|
19
|
-
// ---------------------------------------------------------------------------
|
|
20
|
-
// Helpers
|
|
21
|
-
// ---------------------------------------------------------------------------
|
|
22
|
-
|
|
23
|
-
function safePath(untrusted: string): string | null {
|
|
24
|
-
const resolved = resolve(cwd, untrusted.startsWith("/") ? `.${untrusted}` : untrusted);
|
|
25
|
-
if (!resolved.startsWith(cwd + sep) && resolved !== cwd) return null;
|
|
26
|
-
return resolved;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function extractFilePath(url: string): string {
|
|
30
|
-
// URL: /fs/file/.deco/blocks/site.json
|
|
31
|
-
const [, ...segments] = url.split("/file");
|
|
32
|
-
return segments.join("/file") || "/";
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function readBody(req: IncomingMessage): Promise<string> {
|
|
36
|
-
return new Promise((resolve, reject) => {
|
|
37
|
-
const chunks: Buffer[] = [];
|
|
38
|
-
req.on("data", (chunk: Buffer) => chunks.push(chunk));
|
|
39
|
-
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
40
|
-
req.on("error", reject);
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async function mtimeFor(filepath: string): Promise<number> {
|
|
45
|
-
try {
|
|
46
|
-
const stats = await stat(filepath);
|
|
47
|
-
return stats.mtimeMs;
|
|
48
|
-
} catch {
|
|
49
|
-
return Date.now();
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// ---------------------------------------------------------------------------
|
|
54
|
-
// Patch application — matches daemon/fs/common.ts
|
|
55
|
-
// ---------------------------------------------------------------------------
|
|
56
|
-
|
|
57
|
-
interface Patch {
|
|
58
|
-
type: "json" | "text";
|
|
59
|
-
payload: Operation[];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function applyPatch(
|
|
63
|
-
content: string | null,
|
|
64
|
-
patch: Patch,
|
|
65
|
-
): { conflict: boolean; content?: string } {
|
|
66
|
-
try {
|
|
67
|
-
if (patch.type === "json") {
|
|
68
|
-
const result = patch.payload.reduce(
|
|
69
|
-
fjp.applyReducer,
|
|
70
|
-
JSON.parse(content ?? "{}"),
|
|
71
|
-
);
|
|
72
|
-
return { conflict: false, content: JSON.stringify(result, null, 2) };
|
|
73
|
-
}
|
|
74
|
-
if (patch.type === "text") {
|
|
75
|
-
const result = patch.payload.reduce(
|
|
76
|
-
fjp.applyReducer,
|
|
77
|
-
content?.split("\n") ?? [],
|
|
78
|
-
);
|
|
79
|
-
return { conflict: false, content: (result as string[]).join("\n") };
|
|
80
|
-
}
|
|
81
|
-
} catch (err: unknown) {
|
|
82
|
-
if (
|
|
83
|
-
err instanceof fjp.JsonPatchError &&
|
|
84
|
-
err.name === "TEST_OPERATION_FAILED"
|
|
85
|
-
) {
|
|
86
|
-
return { conflict: true };
|
|
87
|
-
}
|
|
88
|
-
throw err;
|
|
89
|
-
}
|
|
90
|
-
return { conflict: true };
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// ---------------------------------------------------------------------------
|
|
94
|
-
// Handler
|
|
95
|
-
// ---------------------------------------------------------------------------
|
|
96
|
-
|
|
97
|
-
export function createFSHandler() {
|
|
98
|
-
return async (
|
|
99
|
-
req: IncomingMessage,
|
|
100
|
-
res: ServerResponse,
|
|
101
|
-
next: () => void,
|
|
102
|
-
): Promise<void> => {
|
|
103
|
-
const url = new URL(req.url ?? "/", "http://localhost");
|
|
104
|
-
const { pathname } = url;
|
|
105
|
-
|
|
106
|
-
// Only handle /fs/file/* paths
|
|
107
|
-
if (!pathname.startsWith("/fs/file")) {
|
|
108
|
-
// Also handle /fs/grep (admin search)
|
|
109
|
-
if (pathname === "/fs/grep") {
|
|
110
|
-
// Minimal grep stub — return empty results
|
|
111
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
112
|
-
res.end(JSON.stringify({ matches: [], totalMatches: 0 }));
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
next();
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const filePath = extractFilePath(pathname);
|
|
120
|
-
const systemPath = safePath(filePath);
|
|
121
|
-
|
|
122
|
-
if (!systemPath) {
|
|
123
|
-
res.writeHead(403, { "Content-Type": "application/json" });
|
|
124
|
-
res.end(JSON.stringify({ error: "Path traversal denied" }));
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// GET /fs/file/* — read file
|
|
129
|
-
if (req.method === "GET") {
|
|
130
|
-
try {
|
|
131
|
-
const [content, metadata, mtime] = await Promise.all([
|
|
132
|
-
readFile(systemPath, "utf-8"),
|
|
133
|
-
inferMetadata(systemPath),
|
|
134
|
-
mtimeFor(systemPath),
|
|
135
|
-
]);
|
|
136
|
-
|
|
137
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
138
|
-
res.end(JSON.stringify({ content, metadata, timestamp: mtime }));
|
|
139
|
-
} catch (err: unknown) {
|
|
140
|
-
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
|
|
141
|
-
res.writeHead(404, { "Content-Type": "application/json" });
|
|
142
|
-
res.end(JSON.stringify({ timestamp: Date.now() }));
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
throw err;
|
|
146
|
-
}
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// PATCH /fs/file/* — apply JSON patch
|
|
151
|
-
if (req.method === "PATCH") {
|
|
152
|
-
const raw = await readBody(req);
|
|
153
|
-
let body: { patch: Patch; timestamp: number };
|
|
154
|
-
try {
|
|
155
|
-
body = JSON.parse(raw);
|
|
156
|
-
} catch {
|
|
157
|
-
res.writeHead(400, { "Content-Type": "application/json" });
|
|
158
|
-
res.end(JSON.stringify({ error: "Invalid JSON" }));
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const mtimeBefore = await mtimeFor(systemPath);
|
|
163
|
-
let content: string | null;
|
|
164
|
-
try {
|
|
165
|
-
content = await readFile(systemPath, "utf-8");
|
|
166
|
-
} catch {
|
|
167
|
-
content = null;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const result = applyPatch(content, body.patch);
|
|
171
|
-
|
|
172
|
-
if (!result.conflict && result.content != null) {
|
|
173
|
-
const dir = join(systemPath, "..");
|
|
174
|
-
await mkdir(dir, { recursive: true });
|
|
175
|
-
await writeFile(systemPath, result.content, "utf-8");
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const [metadata, mtimeAfter] = await Promise.all([
|
|
179
|
-
inferMetadata(systemPath),
|
|
180
|
-
mtimeFor(systemPath),
|
|
181
|
-
]);
|
|
182
|
-
|
|
183
|
-
// Broadcast change for SSE listeners
|
|
184
|
-
broadcastFSEvent({
|
|
185
|
-
type: "fs-sync",
|
|
186
|
-
detail: {
|
|
187
|
-
metadata,
|
|
188
|
-
timestamp: mtimeAfter,
|
|
189
|
-
filepath: toPosix(systemPath.replace(cwd, "")),
|
|
190
|
-
},
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
const update = result.conflict
|
|
194
|
-
? { conflict: true, metadata, timestamp: mtimeAfter, content }
|
|
195
|
-
: {
|
|
196
|
-
conflict: false,
|
|
197
|
-
metadata,
|
|
198
|
-
timestamp: mtimeAfter,
|
|
199
|
-
content: mtimeBefore !== body.timestamp ? result.content : undefined,
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
203
|
-
res.end(JSON.stringify(update));
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// DELETE /fs/file/*
|
|
208
|
-
if (req.method === "DELETE") {
|
|
209
|
-
try {
|
|
210
|
-
await rm(systemPath, { force: true });
|
|
211
|
-
} catch {
|
|
212
|
-
// ignore
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
broadcastFSEvent({
|
|
216
|
-
type: "fs-sync",
|
|
217
|
-
detail: {
|
|
218
|
-
metadata: null,
|
|
219
|
-
timestamp: Date.now(),
|
|
220
|
-
filepath: toPosix(systemPath.replace(cwd, "")),
|
|
221
|
-
},
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
res.writeHead(200, { "Content-Type": "application/json" });
|
|
225
|
-
res.end(
|
|
226
|
-
JSON.stringify({
|
|
227
|
-
conflict: false,
|
|
228
|
-
metadata: null,
|
|
229
|
-
timestamp: Date.now(),
|
|
230
|
-
}),
|
|
231
|
-
);
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
res.writeHead(405);
|
|
236
|
-
res.end();
|
|
237
|
-
};
|
|
238
|
-
}
|
package/src/daemon/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export { startTunnel } from "./tunnel";
|
|
2
|
-
export type { TunnelOptions, TunnelConnection } from "./tunnel";
|
|
3
|
-
export { createAuthMiddleware, verifyAdminJwt, tokenIsValid } from "./auth";
|
|
4
|
-
export type { JwtPayload } from "./auth";
|
|
5
|
-
export { createDaemonMiddleware } from "./middleware";
|
|
6
|
-
export type { DaemonOptions } from "./middleware";
|
|
7
|
-
export { createVolumesHandler } from "./volumes";
|
|
8
|
-
export { createWatchHandler, watchFS, broadcastFSEvent } from "./watch";
|