cursedops 0.2.1 → 0.2.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/README.md CHANGED
@@ -11,7 +11,8 @@ bun add cursedops
11
11
  | `cursedops/roots` | finding a generation's roots, and a checkout's package root, without knowing a path |
12
12
  | `cursedops/launchd` | installing, replacing and removing a macOS launchd user agent |
13
13
  | `cursedops/smoke` | the scaffolding of a deployed smoke — the ledger, the fetch, the DNS hint, the exit code — and the one check no app owns: every address of a deployment serving the same built client |
14
- | `cursedops/serve` | the static tier's four helpers — the path-traversal guard, the MIME table, the hashed-asset test, the crash handlers — and the API floor: the predicates, the trailing-slash normaliser and the default 404 body that keep an unmatched `/api/...` from ever being answered with the app shell |
14
+ | `cursedops/serve` | the static tier's four helpers — the path-traversal guard, the MIME table, the hashed-asset test, the crash handlers |
15
+ | `cursedops/api-floor` | the rule that an unmatched `/api/...` is a phrase and never the app shell — the namespace predicates, the trailing-slash normaliser and the default 404 body. No `node:` import, so it mounts inside a Worker |
15
16
 
16
17
  Bun, zero runtime dependencies, ships TypeScript source. Nothing here knows an app's
17
18
  name, a hostname, a port or a route.
@@ -143,6 +144,41 @@ Nothing else. The checks are the part `desk` and `flix` wrote differently on pur
143
144
  and a shared kit that starts absorbing route lists and health-payload shapes is how the
144
145
  last one reached 277 files.
145
146
 
147
+ ## `cursedops/api-floor`
148
+
149
+ ```ts
150
+ import { API_PREFIX, apiNotFoundBody, canonicalApiPath, canonicalApiRequest } from "cursedops/api-floor";
151
+
152
+ // AFTER the app's routes, BEFORE the static catch-all, and `all` not `get`.
153
+ const floor = (c) => {
154
+ const urlPath = new URL(c.req.url).pathname;
155
+ // A trailing slash is never meaningful under /api/ — re-dispatch, once.
156
+ if (canonicalApiPath(urlPath) !== null) return app.fetch(canonicalApiRequest(c.req.raw));
157
+ return Response.json(apiNotFoundBody(c.req.method, urlPath, "myapp", commit), { status: 404 });
158
+ };
159
+ app.all(API_PREFIX, floor); // `"/api/*"` alone does NOT match a bare `/api`
160
+ app.all(`${API_PREFIX}/*`, floor);
161
+ ```
162
+
163
+ 🔴 Measured 2026-09-17 on `station`: a page that had never loaded on any commit, because
164
+ its client asked for `/api/openclaw/`, Hono mounts a sub-app's `get("/")` at the mount
165
+ point without the trailing slash and matches strictly, and the miss fell into
166
+ `app.get("*")` and came back as 1.7 KB of `index.html` **with a 200**. The client read
167
+ that HTML honestly — *"an API older than this page"* — and sent the owner to deploy a
168
+ commit that could not have helped.
169
+
170
+ 🔴 Wire it **inside the router**, never as a wrapper around the host's `fetch`. A router
171
+ has more callers than `Bun.serve` — a Worker's `export default`, and every harness that
172
+ drives `app.fetch` — and a wrap installed at one of them is absent from the rest. That is
173
+ the exact fault the previous generation's artifact shipped for six days with a green
174
+ harness.
175
+
176
+ No `node:` import, no `Bun.` global, no `process` — asserted by `apiFloor.test.ts`, not
177
+ by this paragraph — so it mounts unchanged inside a Cloudflare Worker, which is where
178
+ `patterns` mounts it. `apiNotFoundBody` is the DEFAULT body and not the only one: an app
179
+ whose API namespace is not behind a gate keeps a terser phrase of its own and takes the
180
+ predicates.
181
+
146
182
  ## `cursedops/serve`
147
183
 
148
184
  ```ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cursedops",
3
- "version": "0.2.1",
4
- "description": "The build-and-ops answers this generation's apps wrote independently and identically: finding a generation's roots without knowing a path, macOS launchd agent install/replace/remove, the scaffolding of a deployed smoke, and the static-serving helpers eight apps copied — the path-traversal guard and the API floor among them. Mechanism only — no app knows its name from here. Bun, zero dependencies, ships source.",
3
+ "version": "0.2.3",
4
+ "description": "The build-and-ops answers this generation's apps wrote independently and identically: finding a generation's roots without knowing a path, macOS launchd agent install/replace/remove, the scaffolding of a deployed smoke, and the static-serving helpers eight apps copied — the path-traversal guard among them — and the API floor that keeps an unmatched /api/... from ever being answered with the app shell. Mechanism only — no app knows its name from here. Bun, zero dependencies, ships source.",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "typecheck": "tsc -p tsconfig.json --noEmit",
@@ -24,6 +24,12 @@
24
24
  "source": "./src/launchd.ts",
25
25
  "import": "./src/launchd.ts"
26
26
  },
27
+ "./api-floor": {
28
+ "types": "./src/apiFloor.ts",
29
+ "bun": "./src/apiFloor.ts",
30
+ "source": "./src/apiFloor.ts",
31
+ "import": "./src/apiFloor.ts"
32
+ },
27
33
  "./serve": {
28
34
  "types": "./src/serve.ts",
29
35
  "bun": "./src/serve.ts",
@@ -0,0 +1,139 @@
1
+ /**
2
+ * `cursedops/api-floor` — the rule that an unmatched `/api/...` is answered with a
3
+ * phrase and NEVER with the single-page app shell.
4
+ *
5
+ * It arrives by this library's own entry arithmetic, not by taste. `station` and
6
+ * `patterns` each wrote a floor — a rule that an unmatched `/api/...` is answered with a
7
+ * phrase and never with the single-page shell — independently, in different shapes, for
8
+ * the same incident. That is entry rule 1 twice over. Rule 2 holds because nothing below
9
+ * looks anything up: the app's name, its commit and the body it chooses to send all
10
+ * arrive as arguments, and there is no Hono type in this file, so the four lines of
11
+ * `app.all(...)` wiring stay in the app that owns the router. Rule 3 is the measurement:
12
+ *
13
+ * > 2026-09-17. `station`'s Openclaw page had never loaded on any commit. The client
14
+ * > asked for `/api/openclaw/`; Hono mounts that sub-app's `get("/")` at
15
+ * > `/api/openclaw` — `mergePath("/api/openclaw", "/")` drops the trailing slash — and
16
+ * > then matches strictly, so the request matched nothing, fell into `app.get("*")`,
17
+ * > and came back as 1.7 KB of `index.html` with a **200**. The client's honest reading
18
+ * > of an HTML body was *"that is what an API older than this page looks like — deploy
19
+ * > this commit and reload"*, which sent the owner at a deploy that could not have
20
+ * > helped.
21
+ *
22
+ * Both halves of that are here, because a floor without the normaliser fixes the symptom
23
+ * the owner did not have: {@link canonicalApiPath} makes the trailing slash a non-event,
24
+ * and {@link isApiPath} + {@link apiNotFoundBody} make every remaining miss a JSON 404
25
+ * that names the route, the app and the commit answering. A wrong path says "wrong path";
26
+ * a stale process says which commit it is.
27
+ *
28
+ * 🔴 **{@link apiNotFoundBody} is the DEFAULT body, not the only one.** `patterns` sends
29
+ * `{status, code, error}` through its own `reply.refuse` and deliberately says less —
30
+ * its floor faces the public internet, where the app's name and commit are one bit more
31
+ * than a scanner should get. An app that knows something this library cannot keeps its
32
+ * own body and takes the two predicates; that is the `cwip/asset-budget` shape, and it is
33
+ * what stops this growing into the kit it replaced.
34
+ *
35
+ * ## 🔴 Why this is its own subpath and not part of `cursedops/serve`
36
+ *
37
+ * Nothing below imports `node:` anything, and that is a requirement rather than an
38
+ * accident: `patterns` mounts its floor inside a Cloudflare Worker, where the static
39
+ * tier next door — `fileWithin`, `statSync`, a disk to read — does not exist and must
40
+ * not be dragged into the bundle to reach four pure functions. `nodejs_compat` would
41
+ * have RESOLVED the import and shipped a partial `node:fs` shim to the edge, so the
42
+ * failure would have been silent weight rather than a build error. One subpath per
43
+ * runtime requirement is what keeps that honest; `subpathsReachNoBunBuiltin`-shaped
44
+ * checks can then say so rather than a comment.
45
+ */
46
+
47
+ /** The API namespace. See the module header: nothing under it may answer with the shell. */
48
+ export const API_PREFIX = "/api";
49
+
50
+ /**
51
+ * Is this path inside the API namespace? `/api` itself counts; `/apiary` does not.
52
+ *
53
+ * 🔴 The namespace ITSELF is the case every hand-rolled floor missed. A Hono
54
+ * `app.all("/api/*")` does not match a bare `/api` — the pattern requires the slash — so
55
+ * the one path most likely to be typed by hand fell through to the shell in the two apps
56
+ * that already believed they had a floor.
57
+ */
58
+ export const isApiPath = (urlPath: string): boolean =>
59
+ urlPath === API_PREFIX || urlPath.startsWith(`${API_PREFIX}/`);
60
+
61
+ /**
62
+ * The path an API request SHOULD have asked for, or `null` when it already did.
63
+ *
64
+ * 🔴 One rule, and it is the one that broke the Openclaw page: a trailing slash on an API
65
+ * path is never meaningful. Hono mounts a sub-app's `get("/")` at the mount point WITHOUT
66
+ * a trailing slash and then matches strictly, so `/api/openclaw/` is a different path
67
+ * from `/api/openclaw` and matched nothing at all. Normalising once, in front of the
68
+ * router, means no caller anywhere can spell it the losing way again — which a fix in the
69
+ * one client module that happened to do it would not have bought.
70
+ *
71
+ * The query string is untouched: only the path is rewritten.
72
+ */
73
+ export function canonicalApiPath(urlPath: string): string | null {
74
+ if (!isApiPath(urlPath) || !urlPath.endsWith("/")) return null;
75
+ const trimmed = urlPath.replace(/\/+$/, "");
76
+ return trimmed === urlPath ? null : trimmed;
77
+ }
78
+
79
+ /**
80
+ * The same normalisation, applied to a whole request. Returns the request unchanged when
81
+ * there is nothing to normalise, so a caller that guards on {@link canonicalApiPath}
82
+ * allocates nothing.
83
+ *
84
+ * 🔴 **Call it from INSIDE the floor handler and re-dispatch, not from the host's
85
+ * `fetch`.** Both work; only one of them cannot be wired up wrong. A router has many
86
+ * callers — `Bun.serve`, a Worker's `export default`, and every test harness that drives
87
+ * `app.fetch` directly — and a wrap installed at one of them is absent from the others.
88
+ * That is the shape of the fault this whole family exists to close: the previous
89
+ * generation's artifact had a floor its harness mounted correctly and production did not,
90
+ * and it stayed green for six days. A floor that answers
91
+ *
92
+ * const canonical = canonicalApiPath(new URL(c.req.url).pathname);
93
+ * if (canonical !== null) return app.fetch(canonicalApiRequest(c.req.raw));
94
+ * return c.json(apiNotFoundBody(...), 404, ...);
95
+ *
96
+ * is reached by every caller there will ever be, costs nothing on the hot path — only a
97
+ * MISS gets here — and cannot loop, because the canonical path has no trailing slash left
98
+ * to strip and a second miss falls straight to the body.
99
+ *
100
+ * It must not be a Hono middleware either: Hono matches the route before the chain runs,
101
+ * so a middleware cannot change which handler answers, which is the entire job here.
102
+ */
103
+ export function canonicalApiRequest(request: Request): Request {
104
+ const url = new URL(request.url);
105
+ const canonical = canonicalApiPath(url.pathname);
106
+ if (canonical === null) return request;
107
+ url.pathname = canonical;
108
+ return new Request(url, request);
109
+ }
110
+
111
+ /**
112
+ * What an unmatched API path answers — a JSON 404 that names itself.
113
+ *
114
+ * 🔴 The commit is in the body on purpose. The two reasons a route is missing are "this
115
+ * path never existed" and "this process is older than the page that asked", and they need
116
+ * different hands; without the commit a client can only guess, and the guess one printed
117
+ * for the owner sent him to deploy a commit that would not have fixed anything.
118
+ *
119
+ * 🔴 Safe to publish only where the floor sits BEHIND the app's gate, which is where every
120
+ * caller puts it — an unauthenticated request is answered 401 by the gate, matched or not.
121
+ * An app whose API namespace is open sends its own, terser body instead; see the module
122
+ * header.
123
+ */
124
+ export function apiNotFoundBody(
125
+ method: string,
126
+ urlPath: string,
127
+ app: string,
128
+ commit: string | null | undefined,
129
+ ): { error: string; route: string; app: string; commit: string | null } {
130
+ return {
131
+ error:
132
+ `no such route: ${method} ${urlPath}. ${app} is serving ${commit ?? "an unreadable commit"} ` +
133
+ "and has no such API route — either the path is wrong, or this process is older than the page " +
134
+ "that asked for it. It is NOT the app shell: nothing under /api/ is ever answered with HTML.",
135
+ route: `${method} ${urlPath}`,
136
+ app,
137
+ commit: commit ?? null,
138
+ };
139
+ }
package/src/serve.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * The helpers every app's `src/server/serve.ts` wrote the same way — the
3
- * path-traversal guard among them, and the API floor.
2
+ * The four helpers every app's `src/server/serve.ts` wrote the same way — the
3
+ * path-traversal guard among them.
4
4
  *
5
5
  * ## Why this is a library and not eight copies
6
6
  *
@@ -48,37 +48,11 @@
48
48
  * `serve.test.ts` drives both halves, including the symlink escape, because a guard
49
49
  * that has only ever been seen passing is a guard nobody has proved can fail.
50
50
  *
51
- * ## 🔴 The API floor, added 2026-09-18 — and why it is here rather than in an app
52
- *
53
- * The second family in this file, and it arrives by the same arithmetic. `station` and
54
- * `patterns` each wrote a floor — a rule that an unmatched `/api/...` is answered with a
55
- * phrase and never with the single-page shell — independently, in different shapes, for
56
- * the same incident. That is entry rule 1 twice over. Rule 2 holds because nothing below
57
- * looks anything up: the app's name, its commit and the body it chooses to send all
58
- * arrive as arguments, and there is no Hono type in this file, so the three lines of
59
- * `app.all(...)` wiring stay in the app that owns the router. Rule 3 is the measurement:
60
- *
61
- * > 2026-09-17. `station`'s Openclaw page had never loaded on any commit. The client
62
- * > asked for `/api/openclaw/`; Hono mounts that sub-app's `get("/")` at
63
- * > `/api/openclaw` — `mergePath("/api/openclaw", "/")` drops the trailing slash — and
64
- * > then matches strictly, so the request matched nothing, fell into `app.get("*")`,
65
- * > and came back as 1.7 KB of `index.html` with a **200**. The client's honest reading
66
- * > of an HTML body was *"that is what an API older than this page looks like — deploy
67
- * > this commit and reload"*, which sent the owner at a deploy that could not have
68
- * > helped.
69
- *
70
- * Both halves of that are here, because a floor without the normaliser fixes the symptom
71
- * the owner did not have: {@link canonicalApiPath} makes the trailing slash a non-event,
72
- * and {@link isApiPath} + {@link apiNotFoundBody} make every remaining miss a JSON 404
73
- * that names the route, the app and the commit answering. A wrong path says "wrong path";
74
- * a stale process says which commit it is.
75
- *
76
- * 🔴 **{@link apiNotFoundBody} is the DEFAULT body, not the only one.** `patterns` sends
77
- * `{status, code, error}` through its own `reply.refuse` and deliberately says less —
78
- * its floor faces the public internet, where the app's name and commit are one bit more
79
- * than a scanner should get. An app that knows something this library cannot keeps its
80
- * own body and takes the two predicates; that is the `cwip/asset-budget` shape, and it is
81
- * what stops this growing into the kit it replaced.
51
+ * 🔴 **The API floor is deliberately NOT here — it is `cursedops/api-floor`.** It is the
52
+ * same family of answer (an app's HTTP tier, written the same way twice) and it has a
53
+ * requirement this module cannot meet: `patterns` mounts its floor inside a Cloudflare
54
+ * Worker, and everything in this file exists to read a disk. Splitting the subpath is
55
+ * what stops `node:fs` being bundled to the edge to reach four pure functions.
82
56
  */
83
57
 
84
58
  import { existsSync, realpathSync, statSync } from "node:fs";
@@ -173,86 +147,6 @@ export function fileWithin(root: string, urlPath: string): string | null {
173
147
  * the whole of rule 2: the only app-shaped thing these four helpers touch arrives
174
148
  * as an argument.
175
149
  */
176
- /** The API namespace. See the module header: nothing under it may answer with the shell. */
177
- export const API_PREFIX = "/api";
178
-
179
- /**
180
- * Is this path inside the API namespace? `/api` itself counts; `/apiary` does not.
181
- *
182
- * 🔴 The namespace ITSELF is the case every hand-rolled floor missed. A Hono
183
- * `app.all("/api/*")` does not match a bare `/api` — the pattern requires the slash — so
184
- * the one path most likely to be typed by hand fell through to the shell in the two apps
185
- * that already believed they had a floor.
186
- */
187
- export const isApiPath = (urlPath: string): boolean =>
188
- urlPath === API_PREFIX || urlPath.startsWith(`${API_PREFIX}/`);
189
-
190
- /**
191
- * The path an API request SHOULD have asked for, or `null` when it already did.
192
- *
193
- * 🔴 One rule, and it is the one that broke the Openclaw page: a trailing slash on an API
194
- * path is never meaningful. Hono mounts a sub-app's `get("/")` at the mount point WITHOUT
195
- * a trailing slash and then matches strictly, so `/api/openclaw/` is a different path
196
- * from `/api/openclaw` and matched nothing at all. Normalising once, in front of the
197
- * router, means no caller anywhere can spell it the losing way again — which a fix in the
198
- * one client module that happened to do it would not have bought.
199
- *
200
- * The query string is untouched: only the path is rewritten.
201
- */
202
- export function canonicalApiPath(urlPath: string): string | null {
203
- if (!isApiPath(urlPath) || !urlPath.endsWith("/")) return null;
204
- const trimmed = urlPath.replace(/\/+$/, "");
205
- return trimmed === urlPath ? null : trimmed;
206
- }
207
-
208
- /**
209
- * The same normalisation, applied to a whole request. Returns the request unchanged when
210
- * there is nothing to normalise, so the hot path allocates nothing.
211
- *
212
- * 🔴 Wrap it around the ONE function every response leaves through — the `fetch` a host
213
- * hands `Bun.serve` or exports from a Worker — and never as a Hono middleware. Hono runs
214
- * middleware in registration order against handlers registered AFTER it, and an app's
215
- * routes are mounted before the host is built; a middleware added at that point runs for
216
- * none of them. It also cannot change which route matches, which is the entire job here.
217
- */
218
- export function canonicalApiRequest(request: Request): Request {
219
- const url = new URL(request.url);
220
- const canonical = canonicalApiPath(url.pathname);
221
- if (canonical === null) return request;
222
- url.pathname = canonical;
223
- return new Request(url, request);
224
- }
225
-
226
- /**
227
- * What an unmatched API path answers — a JSON 404 that names itself.
228
- *
229
- * 🔴 The commit is in the body on purpose. The two reasons a route is missing are "this
230
- * path never existed" and "this process is older than the page that asked", and they need
231
- * different hands; without the commit a client can only guess, and the guess one printed
232
- * for the owner sent him to deploy a commit that would not have fixed anything.
233
- *
234
- * 🔴 Safe to publish only where the floor sits BEHIND the app's gate, which is where every
235
- * caller puts it — an unauthenticated request is answered 401 by the gate, matched or not.
236
- * An app whose API namespace is open sends its own, terser body instead; see the module
237
- * header.
238
- */
239
- export function apiNotFoundBody(
240
- method: string,
241
- urlPath: string,
242
- app: string,
243
- commit: string | null | undefined,
244
- ): { error: string; route: string; app: string; commit: string | null } {
245
- return {
246
- error:
247
- `no such route: ${method} ${urlPath}. ${app} is serving ${commit ?? "an unreadable commit"} ` +
248
- "and has no such API route — either the path is wrong, or this process is older than the page " +
249
- "that asked for it. It is NOT the app shell: nothing under /api/ is ever answered with HTML.",
250
- route: `${method} ${urlPath}`,
251
- app,
252
- commit: commit ?? null,
253
- };
254
- }
255
-
256
150
  export function installCrashHandlers(name: string): void {
257
151
  process.on("uncaughtException", (error) => {
258
152
  console.error(`[${name}] uncaught exception — exiting`, error);