@tschk/moonshine-server 0.3.7 → 0.4.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/package.json +1 -1
- package/src/index.ts +7 -2
- package/src/pipeline.ts +49 -11
- package/src/static.ts +21 -1
- package/src/serialize.ts +0 -86
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
export { createRequestHandler } from "./pipeline.js";
|
|
2
2
|
export { callAction, callLoader, createRouteContext } from "./data.js";
|
|
3
3
|
export { redirect, json, errorResponse, Redirect } from "./errors.js";
|
|
4
|
-
export { serializeData } from "
|
|
5
|
-
export {
|
|
4
|
+
export { serializeData } from "@tschk/moonshine-framework";
|
|
5
|
+
export {
|
|
6
|
+
isContained,
|
|
7
|
+
resolveStaticPath,
|
|
8
|
+
staticFileHeaders,
|
|
9
|
+
tryServeStatic,
|
|
10
|
+
} from "./static.js";
|
|
6
11
|
export type {
|
|
7
12
|
Action,
|
|
8
13
|
ErrorBoundary,
|
package/src/pipeline.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { createRouteGraph, matchRoutes } from "@tschk/moonshine-router";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
RouteArtifact,
|
|
4
|
+
RouteDefinition,
|
|
5
|
+
} from "@tschk/moonshine-framework";
|
|
3
6
|
import type {
|
|
4
7
|
ErrorBoundary,
|
|
5
8
|
Middleware,
|
|
@@ -99,12 +102,16 @@ function mergeHeaders(
|
|
|
99
102
|
function finalizeResponse(
|
|
100
103
|
response: Response,
|
|
101
104
|
baseHeaders?: HeadersInit,
|
|
105
|
+
method?: string,
|
|
102
106
|
): Response {
|
|
103
107
|
const headers = new Headers(baseHeaders);
|
|
104
108
|
for (const [key, value] of response.headers.entries()) {
|
|
105
109
|
headers.set(key, value);
|
|
106
110
|
}
|
|
107
|
-
|
|
111
|
+
// HEAD must carry the GET headers and no body; shipping one is a protocol
|
|
112
|
+
// violation and mismatches Content-Length on runtimes that do not strip it.
|
|
113
|
+
const body = method === "HEAD" ? null : response.body;
|
|
114
|
+
return new Response(body, {
|
|
108
115
|
status: response.status,
|
|
109
116
|
statusText: response.statusText,
|
|
110
117
|
headers,
|
|
@@ -140,6 +147,14 @@ function resolveBaseHeaders(
|
|
|
140
147
|
);
|
|
141
148
|
}
|
|
142
149
|
|
|
150
|
+
// Derived from the leaf, never hardcoded: an Allow header that lists methods
|
|
151
|
+
// the route cannot serve sends clients down paths that will 405 again.
|
|
152
|
+
function allowedMethods(leaf: ModuleChainItem | undefined): string {
|
|
153
|
+
const methods = ["GET", "HEAD"];
|
|
154
|
+
if (leaf?.module.action) methods.push("POST", "PUT", "PATCH", "DELETE");
|
|
155
|
+
return methods.join(", ");
|
|
156
|
+
}
|
|
157
|
+
|
|
143
158
|
async function runCoreAndAfters(
|
|
144
159
|
ctx: RouteContext,
|
|
145
160
|
chain: ModuleChainItem[],
|
|
@@ -149,7 +164,18 @@ async function runCoreAndAfters(
|
|
|
149
164
|
const method = ctx.request.method;
|
|
150
165
|
const leaf = chain[chain.length - 1];
|
|
151
166
|
|
|
152
|
-
if (method !== "GET" && method !== "HEAD"
|
|
167
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
168
|
+
// Without an action there is nothing to serve a write method. Falling
|
|
169
|
+
// through would run the loaders — reachable by any verb, so a
|
|
170
|
+
// CSRF-shaped POST reaches loader side effects — and answer DELETE with
|
|
171
|
+
// 200, which every HTTP client reads as "deleted".
|
|
172
|
+
if (!leaf?.module.action) {
|
|
173
|
+
return new Response(null, {
|
|
174
|
+
status: 405,
|
|
175
|
+
headers: { allow: allowedMethods(leaf) },
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
153
179
|
const result = await callAction(leaf.module.action, ctx);
|
|
154
180
|
if (result instanceof Response) return result;
|
|
155
181
|
if (result !== undefined) {
|
|
@@ -180,13 +206,17 @@ async function runCoreAndAfters(
|
|
|
180
206
|
response = leafData instanceof Response ? leafData : json(ctx.data);
|
|
181
207
|
} else if (route.mode === "static" && options.staticDir) {
|
|
182
208
|
const pathname = new URL(ctx.request.url).pathname;
|
|
183
|
-
const staticRes = await tryServeStatic(
|
|
209
|
+
const staticRes = await tryServeStatic(
|
|
210
|
+
options.staticDir,
|
|
211
|
+
pathname,
|
|
212
|
+
options.defaultMeta?.headers,
|
|
213
|
+
);
|
|
184
214
|
if (staticRes) {
|
|
185
215
|
response = staticRes;
|
|
186
216
|
} else if (options.renderer) {
|
|
187
217
|
response = await options.renderer.render({
|
|
188
218
|
request: ctx.request,
|
|
189
|
-
route: route as
|
|
219
|
+
route: route as RouteArtifact,
|
|
190
220
|
params: ctx.params,
|
|
191
221
|
data: ctx.data,
|
|
192
222
|
signal: ctx.signal,
|
|
@@ -197,7 +227,7 @@ async function runCoreAndAfters(
|
|
|
197
227
|
} else if (options.renderer) {
|
|
198
228
|
response = await options.renderer.render({
|
|
199
229
|
request: ctx.request,
|
|
200
|
-
route: route as
|
|
230
|
+
route: route as RouteArtifact,
|
|
201
231
|
params: ctx.params,
|
|
202
232
|
data: ctx.data,
|
|
203
233
|
signal: ctx.signal,
|
|
@@ -250,7 +280,11 @@ export function createRequestHandler(
|
|
|
250
280
|
(method === "GET" || method === "HEAD") &&
|
|
251
281
|
pathname !== "/"
|
|
252
282
|
) {
|
|
253
|
-
const staticRes = await tryServeStatic(
|
|
283
|
+
const staticRes = await tryServeStatic(
|
|
284
|
+
options.staticDir,
|
|
285
|
+
pathname,
|
|
286
|
+
options.defaultMeta?.headers,
|
|
287
|
+
);
|
|
254
288
|
if (staticRes) return staticRes;
|
|
255
289
|
}
|
|
256
290
|
|
|
@@ -280,7 +314,7 @@ export function createRequestHandler(
|
|
|
280
314
|
const befores: Middleware[] = [];
|
|
281
315
|
for (const c of chain) befores.push(...(c.module.before ?? []));
|
|
282
316
|
const response = await runBefores(ctx, 0, befores, chain, options, route);
|
|
283
|
-
return finalizeResponse(response, baseHeaders);
|
|
317
|
+
return finalizeResponse(response, baseHeaders, method);
|
|
284
318
|
} catch (error) {
|
|
285
319
|
if (error instanceof Redirect) {
|
|
286
320
|
if (!isSafeRedirect(error.location, request)) {
|
|
@@ -297,14 +331,18 @@ export function createRequestHandler(
|
|
|
297
331
|
const boundary = findErrorBoundary(chain, route, options.modules ?? {});
|
|
298
332
|
if (boundary) {
|
|
299
333
|
const res = await boundary(ctx, error);
|
|
300
|
-
return finalizeResponse(res, baseHeaders);
|
|
334
|
+
return finalizeResponse(res, baseHeaders, method);
|
|
301
335
|
}
|
|
302
336
|
|
|
303
337
|
if (error instanceof Response) {
|
|
304
|
-
return finalizeResponse(error, baseHeaders);
|
|
338
|
+
return finalizeResponse(error, baseHeaders, method);
|
|
305
339
|
}
|
|
306
340
|
|
|
307
|
-
return finalizeResponse(
|
|
341
|
+
return finalizeResponse(
|
|
342
|
+
errorResponse(error, options.mode),
|
|
343
|
+
baseHeaders,
|
|
344
|
+
method,
|
|
345
|
+
);
|
|
308
346
|
}
|
|
309
347
|
};
|
|
310
348
|
}
|
package/src/static.ts
CHANGED
|
@@ -81,9 +81,28 @@ export function isContained(root: string, filePath: string): boolean {
|
|
|
81
81
|
return real === realRoot || real.startsWith(realRoot + sep);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
export function staticFileHeaders(
|
|
85
|
+
type: string,
|
|
86
|
+
customHeaders?: HeadersInit,
|
|
87
|
+
): Headers {
|
|
88
|
+
const headers = new Headers(customHeaders);
|
|
89
|
+
headers.set("content-type", type);
|
|
90
|
+
if (!headers.has("x-content-type-options")) {
|
|
91
|
+
headers.set("x-content-type-options", "nosniff");
|
|
92
|
+
}
|
|
93
|
+
if (!headers.has("x-frame-options")) {
|
|
94
|
+
headers.set("x-frame-options", "DENY");
|
|
95
|
+
}
|
|
96
|
+
if (!headers.has("referrer-policy")) {
|
|
97
|
+
headers.set("referrer-policy", "no-referrer");
|
|
98
|
+
}
|
|
99
|
+
return headers;
|
|
100
|
+
}
|
|
101
|
+
|
|
84
102
|
export async function tryServeStatic(
|
|
85
103
|
staticDir: string,
|
|
86
104
|
pathname: string,
|
|
105
|
+
customHeaders?: HeadersInit,
|
|
87
106
|
): Promise<Response | null> {
|
|
88
107
|
if (typeof Bun === "undefined") return null;
|
|
89
108
|
const filePath = resolveStaticPath(staticDir, pathname);
|
|
@@ -97,7 +116,8 @@ export async function tryServeStatic(
|
|
|
97
116
|
}
|
|
98
117
|
const type =
|
|
99
118
|
MIME[extOf(filePath)] ?? (file.type || "application/octet-stream");
|
|
119
|
+
|
|
100
120
|
return new Response(file, {
|
|
101
|
-
headers:
|
|
121
|
+
headers: staticFileHeaders(type, customHeaders),
|
|
102
122
|
});
|
|
103
123
|
}
|
package/src/serialize.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
const ESCAPE_PATTERN = /[<>&\u2028\u2029]/g;
|
|
2
|
-
|
|
3
|
-
function escapeJsonChar(char: string): string {
|
|
4
|
-
return `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function serializeData(value: unknown): string {
|
|
8
|
-
const seen = new Set<unknown>();
|
|
9
|
-
|
|
10
|
-
function visit(v: unknown): string {
|
|
11
|
-
if (v === undefined || v === null) return "null";
|
|
12
|
-
|
|
13
|
-
const type = typeof v;
|
|
14
|
-
|
|
15
|
-
if (type === "boolean" || type === "number") {
|
|
16
|
-
if (Number.isNaN(v as number) || !Number.isFinite(v as number)) {
|
|
17
|
-
return "null";
|
|
18
|
-
}
|
|
19
|
-
return String(v);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (type === "string") {
|
|
23
|
-
const raw = JSON.stringify(v);
|
|
24
|
-
if (raw === undefined)
|
|
25
|
-
throw new TypeError("serializeData: cannot serialize");
|
|
26
|
-
return raw;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (type === "bigint" || type === "function" || type === "symbol") {
|
|
30
|
-
throw new TypeError(`serializeData: cannot serialize ${type}`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (seen.has(v)) throw new TypeError("serializeData: cyclic reference");
|
|
34
|
-
|
|
35
|
-
if (Array.isArray(v)) {
|
|
36
|
-
seen.add(v);
|
|
37
|
-
try {
|
|
38
|
-
const parts: string[] = [];
|
|
39
|
-
for (const item of v) {
|
|
40
|
-
parts.push(visit(item));
|
|
41
|
-
}
|
|
42
|
-
return `[${parts.join(",")}]`;
|
|
43
|
-
} finally {
|
|
44
|
-
seen.delete(v);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const proto = Object.prototype.toString.call(v);
|
|
49
|
-
|
|
50
|
-
if (proto === "[object Date]" || proto === "[object RegExp]") {
|
|
51
|
-
seen.add(v);
|
|
52
|
-
try {
|
|
53
|
-
const raw = JSON.stringify(v);
|
|
54
|
-
if (raw === undefined)
|
|
55
|
-
throw new TypeError("serializeData: cannot serialize");
|
|
56
|
-
return raw;
|
|
57
|
-
} finally {
|
|
58
|
-
seen.delete(v);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const symbols = Object.getOwnPropertySymbols(v);
|
|
63
|
-
if (symbols.length) {
|
|
64
|
-
throw new TypeError("serializeData: cannot serialize symbol");
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
seen.add(v);
|
|
68
|
-
try {
|
|
69
|
-
const parts: string[] = [];
|
|
70
|
-
for (const [key, val] of Object.entries(v as Record<string, unknown>)) {
|
|
71
|
-
if (typeof val === "function" || typeof val === "symbol") {
|
|
72
|
-
throw new TypeError(
|
|
73
|
-
"serializeData: cannot serialize function/symbol",
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
parts.push(`${visit(key)}:${visit(val)}`);
|
|
77
|
-
}
|
|
78
|
-
return `{${parts.join(",")}}`;
|
|
79
|
-
} finally {
|
|
80
|
-
seen.delete(v);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const raw = visit(value);
|
|
85
|
-
return raw.replace(ESCAPE_PATTERN, escapeJsonChar);
|
|
86
|
-
}
|