@revojs/server 0.2.32 → 0.2.33
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/dist/index.d.ts +5 -1
- package/dist/index.js +34 -7
- package/dist/package.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,10 @@ export interface ResponseConfig extends Record<string, unknown> {
|
|
|
23
23
|
statusText?: string;
|
|
24
24
|
headers: Headers;
|
|
25
25
|
}
|
|
26
|
+
export interface FetchOptions extends RequestInit {
|
|
27
|
+
json?: unknown;
|
|
28
|
+
forwardHeaders?: Array<string>;
|
|
29
|
+
}
|
|
26
30
|
export interface ServerContext {
|
|
27
31
|
states: Record<string, unknown>;
|
|
28
32
|
request: Request;
|
|
@@ -108,7 +112,7 @@ export declare function sendForbidden<T>(scope: Scope, body?: T | BodyInit, conf
|
|
|
108
112
|
export declare function sendNotFound<T>(scope: Scope, body?: T | BodyInit, config?: Mergeable<ResponseConfig>): Response;
|
|
109
113
|
export declare function getState<T>(scope: Scope, name: string): T;
|
|
110
114
|
export declare function setState<T>(scope: Scope, name: string, value: T): void;
|
|
111
|
-
export declare function fetch<T>(scope: Scope, input: string | URL, options?:
|
|
115
|
+
export declare function fetch<T>(scope: Scope, input: string | URL, options?: FetchOptions): Promise<T>;
|
|
112
116
|
export declare function invoke(scope: Scope, pipeline: Array<Middleware>, index?: number): Promise<Result>;
|
|
113
117
|
export declare function invokeWebSocket(webSocket: WebSocket, connection: WebSocketConnection, invoke: () => Awaitable<void>): Promise<void>;
|
|
114
118
|
export declare function createStaticMiddleware(resolve: (path: string | URL) => Awaitable<Blob>): {
|
package/dist/index.js
CHANGED
|
@@ -256,19 +256,31 @@ function getState(scope, name) {
|
|
|
256
256
|
function setState(scope, name, value) {
|
|
257
257
|
useStates(scope)[name] = value;
|
|
258
258
|
}
|
|
259
|
-
async function fetch(scope, input, options) {
|
|
259
|
+
async function fetch(scope, input, options = {}) {
|
|
260
|
+
const { json, forwardHeaders, ...init } = options;
|
|
261
|
+
const headers = new Headers(init.headers);
|
|
262
|
+
if (json !== void 0) {
|
|
263
|
+
if (init.body !== void 0 && init.body !== null) throw new TypeError("fetch accepts either json or body, not both");
|
|
264
|
+
init.body = JSON.stringify(json);
|
|
265
|
+
if (!headers.has("Content-Type")) headers.set("Content-Type", "application/json");
|
|
266
|
+
}
|
|
267
|
+
init.headers = headers;
|
|
260
268
|
let response;
|
|
261
269
|
if (isServer) {
|
|
262
270
|
const { states, request } = useServer(scope);
|
|
263
271
|
const url = new URL(input.toString(), request.url);
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const
|
|
272
|
+
if (url.origin === new URL(request.url).origin) {
|
|
273
|
+
const names = [...forwardHeaders ?? []];
|
|
274
|
+
if (init.credentials !== "omit") names.push("cookie");
|
|
275
|
+
for (const name of names) {
|
|
276
|
+
const value = request.headers.get(name);
|
|
277
|
+
if (value !== null && !headers.has(name)) headers.set(name, value);
|
|
278
|
+
}
|
|
279
|
+
const result = await (await import("#server")).default.fetch(await createServerScope(new Request(url, init), useVariables(scope), states));
|
|
268
280
|
if (isResponse(result)) response = result;
|
|
269
281
|
}
|
|
270
282
|
}
|
|
271
|
-
response ??= await globalThis.fetch(input,
|
|
283
|
+
response ??= await globalThis.fetch(input, init);
|
|
272
284
|
if (response.ok === false) throw response;
|
|
273
285
|
switch (response.headers.get("Content-Type")?.split(";").shift() ?? "") {
|
|
274
286
|
case "application/json": return response.json();
|
|
@@ -307,7 +319,22 @@ function createStaticMiddleware(resolve) {
|
|
|
307
319
|
return defineMiddleware({ async fetch(scope, next) {
|
|
308
320
|
const { pathname } = useUrl(scope);
|
|
309
321
|
const asset = assets[pathname];
|
|
310
|
-
if (asset)
|
|
322
|
+
if (asset) {
|
|
323
|
+
const { request } = useServer(scope);
|
|
324
|
+
let cacheControl = "public, max-age=0, must-revalidate";
|
|
325
|
+
if (pathname.startsWith("/assets/")) cacheControl = "public, max-age=31536000, immutable";
|
|
326
|
+
const headers = new Headers({
|
|
327
|
+
"Content-Type": asset.type,
|
|
328
|
+
"Cache-Control": cacheControl,
|
|
329
|
+
"ETag": asset.entityTag,
|
|
330
|
+
"Last-Modified": new Date(asset.lastModified).toUTCString()
|
|
331
|
+
});
|
|
332
|
+
for (const tag of request.headers.get("If-None-Match")?.split(",") ?? []) if (tag.trim() === asset.entityTag || tag.trim() === "*") return sendResponse(scope, null, {
|
|
333
|
+
status: 304,
|
|
334
|
+
headers
|
|
335
|
+
});
|
|
336
|
+
return sendOk(scope, await resolve(asset.relative), { headers });
|
|
337
|
+
}
|
|
311
338
|
return next?.();
|
|
312
339
|
} });
|
|
313
340
|
}
|
package/dist/package.js
CHANGED