noofpi 2.0.1 → 3.1.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/README.md +6 -2
- package/client.d.ts +2 -0
- package/dist/build-YRW44BZG.js +212 -0
- package/dist/{chunk-YJVMBHNL.js → chunk-JUVQEKLE.js} +43 -12
- package/dist/chunk-LJEKK4AK.js +51 -0
- package/dist/chunk-O5JRC252.js +245 -0
- package/dist/{chunk-G35DW4NA.js → chunk-PC3ZJN4D.js} +11 -1
- package/dist/{chunk-NHBXVKEQ.js → chunk-PL3CA2JF.js} +160 -86
- package/dist/{chunk-MN5PVHC5.js → chunk-W7ASRDAJ.js} +229 -7
- package/dist/{chunk-IE3WF5XF.js → chunk-YN4TKMDX.js} +87 -4
- package/dist/chunk-YN64WQ2B.js +562 -0
- package/dist/cli.js +21 -9
- package/dist/{dev-OAGCOGHA.js → dev-A7VK5V4H.js} +7 -6
- package/dist/{devtools-PLUN2DYV.js → devtools-C3PFU57C.js} +1 -1
- package/dist/handler-oYl9j0Qa.d.ts +26 -0
- package/dist/index.d.ts +26 -11
- package/dist/index.js +3 -2
- package/dist/payload-DfhHnXAn.d.ts +66 -0
- package/dist/{prepare-BN7NMCVQ.js → prepare-JT2NPD5S.js} +6 -5
- package/dist/runtime/entry-client.js +4 -4
- package/dist/runtime/entry-server.d.ts +6 -4
- package/dist/runtime/entry-server.js +88 -25
- package/dist/runtime/index.d.ts +24 -6
- package/dist/runtime/index.js +5 -3
- package/dist/server/bun.d.ts +3 -3
- package/dist/server/index.d.ts +35 -7
- package/dist/server/index.js +12 -7
- package/dist/server/node.d.ts +3 -3
- package/dist/session-CLvYihOU.d.ts +275 -0
- package/dist/{start-ITRND3BY.js → start-J6ZIUG66.js} +7 -6
- package/dist/{types-B6rQOOxS.d.ts → types-Czld0vOH.d.ts} +30 -5
- package/package.json +12 -7
- package/dist/build-WPHZ7TYZ.js +0 -49
- package/dist/chunk-MTXQR32N.js +0 -8
- package/dist/chunk-X3DYEZLK.js +0 -253
- package/dist/handler-C6NnwBfC.d.ts +0 -17
- package/dist/payload-CL6tYBin.d.ts +0 -121
- package/dist/route-rules-COilNsVT.d.ts +0 -127
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PageCache,
|
|
3
|
+
cacheControl,
|
|
4
|
+
cacheKey,
|
|
5
|
+
cacheState
|
|
6
|
+
} from "./chunk-LJEKK4AK.js";
|
|
7
|
+
import {
|
|
8
|
+
openSession,
|
|
9
|
+
resolveSessionSecrets
|
|
10
|
+
} from "./chunk-O5JRC252.js";
|
|
11
|
+
import {
|
|
12
|
+
applyCookies,
|
|
13
|
+
createCookieJar,
|
|
14
|
+
injectPayload,
|
|
15
|
+
isNoofpiError,
|
|
16
|
+
toErrorData
|
|
17
|
+
} from "./chunk-EZPJF4D4.js";
|
|
18
|
+
import {
|
|
19
|
+
PROXY_PREFIX,
|
|
20
|
+
SESSION_PATH,
|
|
21
|
+
SSR_OUTLET,
|
|
22
|
+
applyHead,
|
|
23
|
+
isStreamResult,
|
|
24
|
+
resolveHead
|
|
25
|
+
} from "./chunk-PC3ZJN4D.js";
|
|
26
|
+
import {
|
|
27
|
+
appendVary,
|
|
28
|
+
applyRouteSecurity,
|
|
29
|
+
applySecurityHeaders,
|
|
30
|
+
buildCspHeader,
|
|
31
|
+
corsHeaders,
|
|
32
|
+
generateNonce,
|
|
33
|
+
injectNonce,
|
|
34
|
+
isAllowedHost,
|
|
35
|
+
isCrossSiteWrite,
|
|
36
|
+
isPreflight,
|
|
37
|
+
isServerRendered,
|
|
38
|
+
limitRequestBody,
|
|
39
|
+
matchRedirect,
|
|
40
|
+
matchRouteRules,
|
|
41
|
+
matchesRoutePattern,
|
|
42
|
+
modifyHeaders,
|
|
43
|
+
usesFetchMode,
|
|
44
|
+
usesNonce
|
|
45
|
+
} from "./chunk-W7ASRDAJ.js";
|
|
46
|
+
|
|
47
|
+
// src/server/cookies.ts
|
|
48
|
+
var JAR = /* @__PURE__ */ Symbol.for("noofpi.request-cookies");
|
|
49
|
+
function bindRequestCookies(request, jar) {
|
|
50
|
+
request[JAR] = jar;
|
|
51
|
+
}
|
|
52
|
+
function getRequestCookies(request) {
|
|
53
|
+
const jar = request[JAR];
|
|
54
|
+
if (!jar)
|
|
55
|
+
throw new Error(
|
|
56
|
+
"[noofpi] getRequestCookies() needs the original Request of a request handled by the noofpi server"
|
|
57
|
+
);
|
|
58
|
+
return jar;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/server/session.ts
|
|
62
|
+
var SESSION = /* @__PURE__ */ Symbol.for("noofpi.request-session");
|
|
63
|
+
function bindRequestSession(request, session) {
|
|
64
|
+
request[SESSION] = session;
|
|
65
|
+
}
|
|
66
|
+
function findSession(request) {
|
|
67
|
+
return request[SESSION];
|
|
68
|
+
}
|
|
69
|
+
function getSession(request) {
|
|
70
|
+
const session = request[SESSION];
|
|
71
|
+
if (!session)
|
|
72
|
+
throw new Error(
|
|
73
|
+
"[noofpi] getSession() needs the `session` option in noofpi.config.ts and the original Request of a request handled by the noofpi server"
|
|
74
|
+
);
|
|
75
|
+
return session;
|
|
76
|
+
}
|
|
77
|
+
var toList = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [value];
|
|
78
|
+
var wantsPage = (request) => (request.method === "GET" || request.method === "HEAD") && (request.headers.get("accept") ?? "").includes("text/html");
|
|
79
|
+
function sessionGuard(options = {}) {
|
|
80
|
+
const match = toList(options.match);
|
|
81
|
+
const exclude = toList(options.exclude);
|
|
82
|
+
const param = options.redirectParam ?? "redirect";
|
|
83
|
+
if (options.redirect !== void 0 && (!options.redirect.startsWith("/") || options.redirect.startsWith("//")))
|
|
84
|
+
throw new Error(
|
|
85
|
+
`[noofpi] sessionGuard: redirect must be a path starting with "/" (got "${options.redirect}")`
|
|
86
|
+
);
|
|
87
|
+
return async ({ request, url }, next) => {
|
|
88
|
+
const guarded = (!match.length || match.some((p) => matchesRoutePattern(p, url.pathname))) && !exclude.some((p) => matchesRoutePattern(p, url.pathname));
|
|
89
|
+
if (!guarded) return next();
|
|
90
|
+
const session = getSession(request);
|
|
91
|
+
const allowed = options.allow ? await options.allow(session) : !session.isEmpty;
|
|
92
|
+
if (allowed) return next();
|
|
93
|
+
if (options.redirect && wantsPage(request)) {
|
|
94
|
+
const target = new URL(options.redirect, url);
|
|
95
|
+
target.searchParams.set(param, url.pathname + url.search);
|
|
96
|
+
return new Response(null, {
|
|
97
|
+
status: 302,
|
|
98
|
+
headers: { Location: target.pathname + target.search }
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return new Response(JSON.stringify({ message: "Unauthorized" }), {
|
|
102
|
+
status: 401,
|
|
103
|
+
headers: { "Content-Type": "application/json" }
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/server/proxy.ts
|
|
109
|
+
var FORWARDED_REQUEST_HEADERS = [
|
|
110
|
+
"accept",
|
|
111
|
+
"accept-language",
|
|
112
|
+
"authorization",
|
|
113
|
+
"cookie",
|
|
114
|
+
"content-type",
|
|
115
|
+
"if-none-match",
|
|
116
|
+
"if-modified-since",
|
|
117
|
+
"range"
|
|
118
|
+
];
|
|
119
|
+
var DROPPED_RESPONSE_HEADERS = [
|
|
120
|
+
"content-encoding",
|
|
121
|
+
"content-length",
|
|
122
|
+
"transfer-encoding",
|
|
123
|
+
"connection",
|
|
124
|
+
"keep-alive"
|
|
125
|
+
];
|
|
126
|
+
var json = (status, message) => new Response(JSON.stringify({ message }), {
|
|
127
|
+
status,
|
|
128
|
+
headers: { "Content-Type": "application/json" }
|
|
129
|
+
});
|
|
130
|
+
function resolveProxyTarget(pathname, search, base, root) {
|
|
131
|
+
if (!pathname.startsWith(PROXY_PREFIX + "/")) return null;
|
|
132
|
+
const rest = pathname.slice(PROXY_PREFIX.length);
|
|
133
|
+
let target;
|
|
134
|
+
try {
|
|
135
|
+
target = new URL(rest + search, base);
|
|
136
|
+
} catch {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const afterRoot = target.pathname.charAt(root.length);
|
|
140
|
+
const insideRoot = target.pathname.startsWith(root) && (afterRoot === "" || afterRoot === "." || afterRoot === "/");
|
|
141
|
+
return target.origin === new URL(base).origin && insideRoot ? target : null;
|
|
142
|
+
}
|
|
143
|
+
async function proxyRequest(request, options) {
|
|
144
|
+
const url = new URL(request.url);
|
|
145
|
+
const target = resolveProxyTarget(
|
|
146
|
+
url.pathname,
|
|
147
|
+
url.search,
|
|
148
|
+
options.base,
|
|
149
|
+
options.root
|
|
150
|
+
);
|
|
151
|
+
if (!target) return json(404, "Not found");
|
|
152
|
+
const headers = new Headers();
|
|
153
|
+
for (const name of FORWARDED_REQUEST_HEADERS) {
|
|
154
|
+
const value = request.headers.get(name);
|
|
155
|
+
if (value !== null) headers.set(name, value);
|
|
156
|
+
}
|
|
157
|
+
if (options.omitCookie) {
|
|
158
|
+
const kept = (headers.get("cookie") ?? "").split(";").filter((part) => part.split("=")[0].trim() !== options.omitCookie).join(";").trim();
|
|
159
|
+
if (kept) headers.set("cookie", kept);
|
|
160
|
+
else headers.delete("cookie");
|
|
161
|
+
}
|
|
162
|
+
const hooked = await options.hook?.({
|
|
163
|
+
request,
|
|
164
|
+
headers,
|
|
165
|
+
target,
|
|
166
|
+
session: findSession(request)
|
|
167
|
+
});
|
|
168
|
+
if (hooked instanceof Response) return hooked;
|
|
169
|
+
const hasBody = request.method !== "GET" && request.method !== "HEAD";
|
|
170
|
+
let upstream;
|
|
171
|
+
try {
|
|
172
|
+
upstream = await fetch(target, {
|
|
173
|
+
method: request.method,
|
|
174
|
+
headers,
|
|
175
|
+
// Body is streamed through untouched (binary uploads and JSON alike)
|
|
176
|
+
body: hasBody ? request.body : void 0,
|
|
177
|
+
redirect: "manual",
|
|
178
|
+
...hasBody ? { duplex: "half" } : {}
|
|
179
|
+
});
|
|
180
|
+
} catch {
|
|
181
|
+
return json(502, "Bad gateway");
|
|
182
|
+
}
|
|
183
|
+
const responseHeaders = new Headers(upstream.headers);
|
|
184
|
+
for (const name of DROPPED_RESPONSE_HEADERS) responseHeaders.delete(name);
|
|
185
|
+
return new Response(upstream.body, {
|
|
186
|
+
status: upstream.status,
|
|
187
|
+
headers: responseHeaders
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// src/server/bots.ts
|
|
192
|
+
var BOT = /bot|crawl|spider|slurp|bingpreview|facebookexternalhit|embedly|quora link preview|outbrain|pinterest|vkshare|whatsapp|telegram|discord|linkedin|twitter|prerender/i;
|
|
193
|
+
function isBot(userAgent) {
|
|
194
|
+
return !!userAgent && BOT.test(userAgent);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/server/handler.ts
|
|
198
|
+
var isUnder = (pathname, root) => pathname === root || pathname.startsWith(root + ".") || pathname.startsWith(root + "/");
|
|
199
|
+
function routeSecurity(mod, url) {
|
|
200
|
+
const rules = mod.config.routeRules;
|
|
201
|
+
return applyRouteSecurity(
|
|
202
|
+
mod.config.security,
|
|
203
|
+
rules && matchRouteRules(rules, url.pathname).security
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
var CACHE_HEADER = "X-Noofpi-Cache";
|
|
207
|
+
function createHandler(options) {
|
|
208
|
+
const encoder = new TextEncoder();
|
|
209
|
+
const pageCache = new PageCache();
|
|
210
|
+
let cachedFor;
|
|
211
|
+
let warnedNonceCache = false;
|
|
212
|
+
const reportError = (error) => {
|
|
213
|
+
options.onError?.(error);
|
|
214
|
+
if (!options.onError) console.error(error);
|
|
215
|
+
};
|
|
216
|
+
const renderPage = async (scope, streaming) => {
|
|
217
|
+
const { mod, request, url, env, cookies, security } = scope;
|
|
218
|
+
const template = await options.getTemplate(url.pathname + url.search);
|
|
219
|
+
let html = "";
|
|
220
|
+
let head = resolveHead(mod.config.head);
|
|
221
|
+
let payload;
|
|
222
|
+
let status = 200;
|
|
223
|
+
let stream;
|
|
224
|
+
const renderer = streaming && mod.renderStream ? mod.renderStream : mod.render;
|
|
225
|
+
if (renderer && isServerRendered(mod.config.ssr, mod.config.routeRules, url.pathname)) {
|
|
226
|
+
const context = {
|
|
227
|
+
origin: url.origin,
|
|
228
|
+
cookie: request.headers.get("cookie") ?? void 0,
|
|
229
|
+
cookies,
|
|
230
|
+
acceptLanguage: request.headers.get("accept-language") ?? void 0,
|
|
231
|
+
session: scope.session?.exposed(),
|
|
232
|
+
fetch: (input, init) => handler(new Request(input, init), env, scope.ctx)
|
|
233
|
+
};
|
|
234
|
+
const target = url.pathname + url.search;
|
|
235
|
+
const result = await renderer(target, context);
|
|
236
|
+
if (isStreamResult(result)) {
|
|
237
|
+
stream = result;
|
|
238
|
+
head = result.head;
|
|
239
|
+
status = result.status;
|
|
240
|
+
} else {
|
|
241
|
+
if (result.redirect)
|
|
242
|
+
return new Response(null, {
|
|
243
|
+
status: result.status,
|
|
244
|
+
headers: { Location: result.redirect }
|
|
245
|
+
});
|
|
246
|
+
if (result.error !== void 0) reportError(result.error);
|
|
247
|
+
html = result.html;
|
|
248
|
+
head = result.head ?? head;
|
|
249
|
+
payload = result.payload;
|
|
250
|
+
status = result.status;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
let shell = applyHead(template, head);
|
|
254
|
+
const { csp } = security ?? {};
|
|
255
|
+
const nonce = !options.prerender && usesNonce(csp) ? generateNonce() : void 0;
|
|
256
|
+
if (nonce) shell = injectNonce(shell, nonce);
|
|
257
|
+
const headers = new Headers({ "Content-Type": "text/html; charset=utf-8" });
|
|
258
|
+
if (csp && !options.prerender) {
|
|
259
|
+
const [name, value] = buildCspHeader(csp, nonce);
|
|
260
|
+
headers.set(name, value);
|
|
261
|
+
}
|
|
262
|
+
if (stream) {
|
|
263
|
+
headers.set("X-Accel-Buffering", "no");
|
|
264
|
+
return new Response(
|
|
265
|
+
streamBody(stream, shell, url, (error) => reportError(error)),
|
|
266
|
+
{ status, headers }
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
let body = shell.replace(SSR_OUTLET, () => html);
|
|
270
|
+
if (payload) body = injectPayload(body, payload);
|
|
271
|
+
return new Response(request.method === "HEAD" ? null : body, {
|
|
272
|
+
status,
|
|
273
|
+
headers
|
|
274
|
+
});
|
|
275
|
+
};
|
|
276
|
+
const streamBody = (result, shell, url, report) => {
|
|
277
|
+
const at = shell.indexOf(SSR_OUTLET);
|
|
278
|
+
const before = at < 0 ? shell : shell.slice(0, at);
|
|
279
|
+
const after = at < 0 ? "" : shell.slice(at + SSR_OUTLET.length);
|
|
280
|
+
let reader;
|
|
281
|
+
const tail = (failure) => {
|
|
282
|
+
let payload = { data: {}, errors: {} };
|
|
283
|
+
let error = failure;
|
|
284
|
+
try {
|
|
285
|
+
const done = result.finish();
|
|
286
|
+
payload = done.payload;
|
|
287
|
+
error ??= done.error;
|
|
288
|
+
} catch (finishError) {
|
|
289
|
+
error ??= finishError;
|
|
290
|
+
}
|
|
291
|
+
if (error !== void 0) {
|
|
292
|
+
if (!isNoofpiError(error)) report(error);
|
|
293
|
+
payload = { ...payload, error: toErrorData(error, url.pathname) };
|
|
294
|
+
}
|
|
295
|
+
return encoder.encode(injectPayload(after, payload));
|
|
296
|
+
};
|
|
297
|
+
return new ReadableStream({
|
|
298
|
+
async pull(controller) {
|
|
299
|
+
if (!reader) {
|
|
300
|
+
reader = result.body.getReader();
|
|
301
|
+
controller.enqueue(encoder.encode(before));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
try {
|
|
305
|
+
const { done, value } = await reader.read();
|
|
306
|
+
if (!done) {
|
|
307
|
+
controller.enqueue(value);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
controller.enqueue(tail());
|
|
311
|
+
} catch (failure) {
|
|
312
|
+
controller.enqueue(tail(failure));
|
|
313
|
+
}
|
|
314
|
+
controller.close();
|
|
315
|
+
},
|
|
316
|
+
cancel: (reason) => (reader ?? result.body).cancel(reason)
|
|
317
|
+
});
|
|
318
|
+
};
|
|
319
|
+
const fromCache = (request, page, rule, state) => {
|
|
320
|
+
const headers = new Headers(page.headers);
|
|
321
|
+
if (!headers.has("cache-control"))
|
|
322
|
+
headers.set("Cache-Control", cacheControl(rule));
|
|
323
|
+
headers.set(CACHE_HEADER, state);
|
|
324
|
+
return new Response(request.method === "HEAD" ? null : page.body, {
|
|
325
|
+
status: page.status,
|
|
326
|
+
headers
|
|
327
|
+
});
|
|
328
|
+
};
|
|
329
|
+
const cachedPage = async (scope, rule) => {
|
|
330
|
+
const { request, url, cookies } = scope;
|
|
331
|
+
const key = cacheKey(url, request, rule.vary);
|
|
332
|
+
const render = async (jar, source) => {
|
|
333
|
+
const queued = jar.toSetCookieHeaders().length;
|
|
334
|
+
const response2 = await renderPage(
|
|
335
|
+
{ ...scope, request: source, cookies: jar },
|
|
336
|
+
false
|
|
337
|
+
);
|
|
338
|
+
const page2 = {
|
|
339
|
+
body: await response2.text(),
|
|
340
|
+
status: response2.status,
|
|
341
|
+
headers: [...response2.headers],
|
|
342
|
+
storedAt: Date.now()
|
|
343
|
+
};
|
|
344
|
+
const cacheable2 = response2.status === 200 && jar.toSetCookieHeaders().length === queued;
|
|
345
|
+
if (cacheable2) pageCache.set(key, page2);
|
|
346
|
+
return { page: page2, cacheable: cacheable2 };
|
|
347
|
+
};
|
|
348
|
+
const stored = pageCache.get(key);
|
|
349
|
+
const state = stored && cacheState(stored, rule, Date.now());
|
|
350
|
+
if (stored && state === "fresh")
|
|
351
|
+
return fromCache(request, stored, rule, "HIT");
|
|
352
|
+
if (stored && state === "stale") {
|
|
353
|
+
if (!pageCache.isRendering(key)) {
|
|
354
|
+
const jar = createCookieJar({
|
|
355
|
+
cookie: request.headers.get("cookie"),
|
|
356
|
+
defaults: { secure: url.protocol === "https:" }
|
|
357
|
+
});
|
|
358
|
+
const refresh = pageCache.share(
|
|
359
|
+
key,
|
|
360
|
+
() => render(jar, new Request(request.url, { headers: request.headers }))
|
|
361
|
+
).catch(reportError);
|
|
362
|
+
scope.ctx?.waitUntil(refresh);
|
|
363
|
+
}
|
|
364
|
+
return fromCache(request, stored, rule, "STALE");
|
|
365
|
+
}
|
|
366
|
+
const { page, cacheable } = await pageCache.share(
|
|
367
|
+
key,
|
|
368
|
+
() => render(cookies, request)
|
|
369
|
+
);
|
|
370
|
+
if (cacheable) return fromCache(request, page, rule, "MISS");
|
|
371
|
+
const response = fromCache(request, page, rule, "MISS");
|
|
372
|
+
response.headers.set(CACHE_HEADER, "BYPASS");
|
|
373
|
+
response.headers.set("Cache-Control", "private, no-store");
|
|
374
|
+
return response;
|
|
375
|
+
};
|
|
376
|
+
const respond = async (scope) => {
|
|
377
|
+
const { mod, request, url, env, cookies, security, rule } = scope;
|
|
378
|
+
const { ssr, doofpi } = mod.config;
|
|
379
|
+
const isProxy = usesFetchMode(doofpi.fetch, mod.config.routeRules, "pfetch") && url.pathname.startsWith(PROXY_PREFIX + "/");
|
|
380
|
+
const isApi = !!mod.doofpi && isUnder(url.pathname, doofpi.root);
|
|
381
|
+
if (security?.csrf && (isProxy || isApi) && isCrossSiteWrite(request, url, security.csrf)) {
|
|
382
|
+
return new Response(JSON.stringify({ message: "Forbidden" }), {
|
|
383
|
+
status: 403,
|
|
384
|
+
headers: { "Content-Type": "application/json" }
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
if (isProxy) {
|
|
388
|
+
return await proxyRequest(request, {
|
|
389
|
+
base: doofpi.url,
|
|
390
|
+
root: doofpi.root,
|
|
391
|
+
hook: mod.proxyHook,
|
|
392
|
+
omitCookie: mod.config.session?.name
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
if (isApi) {
|
|
396
|
+
return await mod.doofpi.fetch(request, env);
|
|
397
|
+
}
|
|
398
|
+
if (scope.session && url.pathname === SESSION_PATH) {
|
|
399
|
+
if (request.method !== "GET" && request.method !== "HEAD")
|
|
400
|
+
return new Response("Method Not Allowed", { status: 405 });
|
|
401
|
+
return new Response(
|
|
402
|
+
request.method === "HEAD" ? null : JSON.stringify({ data: scope.session.exposed() }),
|
|
403
|
+
{
|
|
404
|
+
headers: {
|
|
405
|
+
"Content-Type": "application/json",
|
|
406
|
+
"Cache-Control": "no-store"
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
412
|
+
return new Response("Method Not Allowed", { status: 405 });
|
|
413
|
+
}
|
|
414
|
+
if (/\.[a-z0-9]+$/i.test(url.pathname))
|
|
415
|
+
return new Response("Not Found", { status: 404 });
|
|
416
|
+
const rendered = isServerRendered(ssr, mod.config.routeRules, url.pathname);
|
|
417
|
+
const cache = rule.cache || void 0;
|
|
418
|
+
if (cache && rendered && !options.prerender) {
|
|
419
|
+
const personal = !!scope.session && !scope.session.session.isEmpty;
|
|
420
|
+
if (usesNonce(security?.csp)) {
|
|
421
|
+
if (!warnedNonceCache) {
|
|
422
|
+
warnedNonceCache = true;
|
|
423
|
+
console.warn(
|
|
424
|
+
"[noofpi] routeRules `cache` is ignored for pages with a CSP nonce, because a cached page would reuse its nonce"
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
} else if (personal) {
|
|
428
|
+
const response = await renderPage(scope, false);
|
|
429
|
+
response.headers.set("Cache-Control", "private, no-store");
|
|
430
|
+
response.headers.set(CACHE_HEADER, "BYPASS");
|
|
431
|
+
return response;
|
|
432
|
+
} else if (request.method === "GET" && cache.server !== false)
|
|
433
|
+
return cachedPage(scope, cache);
|
|
434
|
+
else {
|
|
435
|
+
const response = await renderPage(scope, false);
|
|
436
|
+
if (response.status === 200)
|
|
437
|
+
response.headers.set("Cache-Control", cacheControl(cache));
|
|
438
|
+
return response;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
const streaming = !options.prerender && request.method === "GET" && rendered && (rule.stream ?? mod.config.streaming ?? false) && !isBot(request.headers.get("user-agent"));
|
|
442
|
+
return renderPage(scope, streaming);
|
|
443
|
+
};
|
|
444
|
+
const handler = async (incoming, env, ctx) => {
|
|
445
|
+
let request = incoming;
|
|
446
|
+
let tooLarge = () => false;
|
|
447
|
+
const payloadTooLarge = () => new Response("Payload Too Large", { status: 413 });
|
|
448
|
+
try {
|
|
449
|
+
const mod = await options.load();
|
|
450
|
+
if (cachedFor !== mod) {
|
|
451
|
+
pageCache.clear();
|
|
452
|
+
cachedFor = mod;
|
|
453
|
+
}
|
|
454
|
+
const middleware = mod.middleware ?? [];
|
|
455
|
+
const url = new URL(request.url);
|
|
456
|
+
if (!isAllowedHost(url, mod.config.security?.allowedHosts))
|
|
457
|
+
return new Response("Invalid Host", { status: 400 });
|
|
458
|
+
const security = routeSecurity(mod, url);
|
|
459
|
+
if (security?.bodyLimit) {
|
|
460
|
+
const limited = limitRequestBody(request, security.bodyLimit);
|
|
461
|
+
request = limited.request;
|
|
462
|
+
tooLarge = limited.tooLarge;
|
|
463
|
+
}
|
|
464
|
+
const rules = mod.config.routeRules;
|
|
465
|
+
const rule = rules ? matchRouteRules(rules, url.pathname) : {};
|
|
466
|
+
const finish = (response2) => {
|
|
467
|
+
const secured = applySecurityHeaders(
|
|
468
|
+
response2,
|
|
469
|
+
security?.headers ?? {},
|
|
470
|
+
url.protocol === "https:"
|
|
471
|
+
);
|
|
472
|
+
const cors = rule.cors ? corsHeaders(rule.cors, request) : void 0;
|
|
473
|
+
const hasCors = !!cors && !cors.keys().next().done;
|
|
474
|
+
if (!rule.headers && !hasCors) return secured;
|
|
475
|
+
return modifyHeaders(secured, (headers) => {
|
|
476
|
+
for (const [name, value] of Object.entries(rule.headers ?? {}))
|
|
477
|
+
headers.set(name, value);
|
|
478
|
+
for (const [name, value] of cors ?? [])
|
|
479
|
+
if (name.toLowerCase() === "vary") appendVary(headers, value);
|
|
480
|
+
else if (!headers.has(name)) headers.set(name, value);
|
|
481
|
+
});
|
|
482
|
+
};
|
|
483
|
+
if (tooLarge()) return finish(payloadTooLarge());
|
|
484
|
+
const redirect = rules && matchRedirect(rules, url.pathname);
|
|
485
|
+
if (redirect) {
|
|
486
|
+
const target = new URL(redirect.to, url);
|
|
487
|
+
if (!target.search) target.search = url.search;
|
|
488
|
+
return finish(
|
|
489
|
+
new Response(null, {
|
|
490
|
+
status: redirect.status,
|
|
491
|
+
headers: {
|
|
492
|
+
Location: redirect.to.startsWith("/") ? target.pathname + target.search : target.href
|
|
493
|
+
}
|
|
494
|
+
})
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
if (rule.cors && isPreflight(request))
|
|
498
|
+
return finish(
|
|
499
|
+
new Response(null, {
|
|
500
|
+
status: 204,
|
|
501
|
+
headers: corsHeaders(rule.cors, request, true)
|
|
502
|
+
})
|
|
503
|
+
);
|
|
504
|
+
const cookies = createCookieJar({
|
|
505
|
+
cookie: request.headers.get("cookie"),
|
|
506
|
+
defaults: { secure: url.protocol === "https:" }
|
|
507
|
+
});
|
|
508
|
+
bindRequestCookies(request, cookies);
|
|
509
|
+
let session;
|
|
510
|
+
if (mod.config.session) {
|
|
511
|
+
session = await openSession({
|
|
512
|
+
config: mod.config.session,
|
|
513
|
+
cookies,
|
|
514
|
+
secrets: options.prerender ? [] : resolveSessionSecrets(env)
|
|
515
|
+
});
|
|
516
|
+
bindRequestSession(request, session.session);
|
|
517
|
+
}
|
|
518
|
+
const scope = {
|
|
519
|
+
mod,
|
|
520
|
+
request,
|
|
521
|
+
url,
|
|
522
|
+
env,
|
|
523
|
+
ctx,
|
|
524
|
+
cookies,
|
|
525
|
+
session,
|
|
526
|
+
rule,
|
|
527
|
+
security
|
|
528
|
+
};
|
|
529
|
+
const run = async (index) => {
|
|
530
|
+
const current = middleware[index];
|
|
531
|
+
if (!current) return respond(scope);
|
|
532
|
+
const response2 = await current(
|
|
533
|
+
{ request, url, env, cookies, session: session?.session },
|
|
534
|
+
() => run(index + 1)
|
|
535
|
+
);
|
|
536
|
+
if (!(response2 instanceof Response))
|
|
537
|
+
throw new Error(
|
|
538
|
+
"[noofpi] Server middleware must return a Response (return next() to continue)."
|
|
539
|
+
);
|
|
540
|
+
return response2;
|
|
541
|
+
};
|
|
542
|
+
const result = await run(0);
|
|
543
|
+
await session?.commit();
|
|
544
|
+
const response = applyCookies(result, cookies);
|
|
545
|
+
return finish(tooLarge() ? payloadTooLarge() : response);
|
|
546
|
+
} catch (error) {
|
|
547
|
+
if (tooLarge()) return payloadTooLarge();
|
|
548
|
+
reportError(error);
|
|
549
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
return handler;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
export {
|
|
556
|
+
getRequestCookies,
|
|
557
|
+
getSession,
|
|
558
|
+
sessionGuard,
|
|
559
|
+
resolveProxyTarget,
|
|
560
|
+
proxyRequest,
|
|
561
|
+
createHandler
|
|
562
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -4,10 +4,12 @@ import {
|
|
|
4
4
|
import {
|
|
5
5
|
BUILD_PRESETS,
|
|
6
6
|
assertBuildPreset,
|
|
7
|
+
generatePreset,
|
|
7
8
|
resolveConfig
|
|
8
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-YN4TKMDX.js";
|
|
10
|
+
import "./chunk-O5JRC252.js";
|
|
9
11
|
import "./chunk-ELXCFUA4.js";
|
|
10
|
-
import "./chunk-
|
|
12
|
+
import "./chunk-W7ASRDAJ.js";
|
|
11
13
|
|
|
12
14
|
// src/cli.ts
|
|
13
15
|
import path2 from "path";
|
|
@@ -17,6 +19,7 @@ import { parseArgs } from "util";
|
|
|
17
19
|
var COMMANDS = [
|
|
18
20
|
"dev",
|
|
19
21
|
"build",
|
|
22
|
+
"generate",
|
|
20
23
|
"start",
|
|
21
24
|
"preview",
|
|
22
25
|
"prepare",
|
|
@@ -109,6 +112,7 @@ var USAGE = `noofpi <command> [options]
|
|
|
109
112
|
Commands:
|
|
110
113
|
dev Start the dev server with live reload
|
|
111
114
|
build Build for production into .output/
|
|
115
|
+
generate Prerender the pages into static HTML files in .output/public (github-pages | cloudflare-pages)
|
|
112
116
|
start Run the production build
|
|
113
117
|
preview Build, then run the production build
|
|
114
118
|
prepare Generate the .noofpi/ type declarations (for a postinstall script)
|
|
@@ -120,10 +124,11 @@ Options:
|
|
|
120
124
|
--host <host> Server host (dev, start, preview)
|
|
121
125
|
--open Open the browser once the server is ready (dev, start, preview)
|
|
122
126
|
--preset <name> Build target: ${BUILD_PRESETS.join(" | ")} (default: build.preset or node)
|
|
123
|
-
preview needs a preset that \`noofpi start\` can run: node | bun | docker
|
|
127
|
+
preview needs a preset that \`noofpi start\` can run: node | bun | docker-node | docker-bun
|
|
128
|
+
generate needs a static one: github-pages | cloudflare-pages (default: build.preset when static, else github-pages)
|
|
124
129
|
-h, --help Show this help
|
|
125
130
|
-v, --version Show the noofpi version`;
|
|
126
|
-
var PREVIEW_PRESETS = ["node", "bun", "docker"];
|
|
131
|
+
var PREVIEW_PRESETS = ["node", "bun", "docker-node", "docker-bun"];
|
|
127
132
|
async function loadEnvFiles(root, mode) {
|
|
128
133
|
const { loadEnv } = await import("vite");
|
|
129
134
|
const env = loadEnv(mode, root, "");
|
|
@@ -150,7 +155,7 @@ async function main() {
|
|
|
150
155
|
await loadEnvFiles(root, mode);
|
|
151
156
|
const config = await loadConfig(root);
|
|
152
157
|
if (command === "dev") {
|
|
153
|
-
const { startDev } = await import("./dev-
|
|
158
|
+
const { startDev } = await import("./dev-A7VK5V4H.js");
|
|
154
159
|
await startDev(root, config, overrides);
|
|
155
160
|
} else if (command === "build" || command === "preview") {
|
|
156
161
|
const preset = args.preset ? assertBuildPreset(args.preset, "--preset") : void 0;
|
|
@@ -161,17 +166,24 @@ async function main() {
|
|
|
161
166
|
`[noofpi] preview can not run the "${target}" preset (use ${PREVIEW_PRESETS.join(" | ")}, or build it and serve .output/ with its own tooling)`
|
|
162
167
|
);
|
|
163
168
|
}
|
|
164
|
-
const { buildApp } = await import("./build-
|
|
169
|
+
const { buildApp } = await import("./build-YRW44BZG.js");
|
|
165
170
|
await buildApp(root, config, preset);
|
|
166
171
|
if (command === "preview") {
|
|
167
|
-
const { startProd } = await import("./start-
|
|
172
|
+
const { startProd } = await import("./start-J6ZIUG66.js");
|
|
168
173
|
await startProd(root, config, overrides);
|
|
169
174
|
}
|
|
175
|
+
} else if (command === "generate") {
|
|
176
|
+
const preset = generatePreset(
|
|
177
|
+
args.preset ? assertBuildPreset(args.preset, "--preset") : void 0,
|
|
178
|
+
config.build.preset
|
|
179
|
+
);
|
|
180
|
+
const { buildApp } = await import("./build-YRW44BZG.js");
|
|
181
|
+
await buildApp(root, config, preset, { generate: true });
|
|
170
182
|
} else if (command === "start") {
|
|
171
|
-
const { startProd } = await import("./start-
|
|
183
|
+
const { startProd } = await import("./start-J6ZIUG66.js");
|
|
172
184
|
await startProd(root, config, overrides);
|
|
173
185
|
} else {
|
|
174
|
-
const { prepareApp } = await import("./prepare-
|
|
186
|
+
const { prepareApp } = await import("./prepare-JT2NPD5S.js");
|
|
175
187
|
await prepareApp(root, config);
|
|
176
188
|
if (command === "prepare") {
|
|
177
189
|
console.log("noofpi: generated .noofpi/");
|
|
@@ -16,13 +16,14 @@ import {
|
|
|
16
16
|
resolveCss,
|
|
17
17
|
resolveFonts,
|
|
18
18
|
startPlatformProxy
|
|
19
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-PL3CA2JF.js";
|
|
20
20
|
import "./chunk-OKNF6XDO.js";
|
|
21
|
-
import "./chunk-
|
|
21
|
+
import "./chunk-YN4TKMDX.js";
|
|
22
22
|
import {
|
|
23
23
|
createHandler
|
|
24
|
-
} from "./chunk-
|
|
25
|
-
import "./chunk-
|
|
24
|
+
} from "./chunk-YN64WQ2B.js";
|
|
25
|
+
import "./chunk-LJEKK4AK.js";
|
|
26
|
+
import "./chunk-O5JRC252.js";
|
|
26
27
|
import {
|
|
27
28
|
sendWebResponse,
|
|
28
29
|
toWebRequest
|
|
@@ -30,8 +31,8 @@ import {
|
|
|
30
31
|
import "./chunk-PPUFFGCH.js";
|
|
31
32
|
import "./chunk-ELXCFUA4.js";
|
|
32
33
|
import "./chunk-EZPJF4D4.js";
|
|
33
|
-
import "./chunk-
|
|
34
|
-
import "./chunk-
|
|
34
|
+
import "./chunk-PC3ZJN4D.js";
|
|
35
|
+
import "./chunk-W7ASRDAJ.js";
|
|
35
36
|
import "./chunk-Y3NBZ6DW.js";
|
|
36
37
|
|
|
37
38
|
// src/vite/dev.ts
|