@torpor/build 0.4.14 → 1.0.1
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 +20 -2
- package/dist/Jsonify-Dg6gChd3.d.mts +10 -0
- package/dist/Jsonify-Dg6gChd3.d.mts.map +1 -0
- package/dist/ParseRouteParams-DBmqlmLh.d.mts +42 -0
- package/dist/ParseRouteParams-DBmqlmLh.d.mts.map +1 -0
- package/dist/{RouteType-B6BMOyYw.mjs → Router-C4ADUfhv.mjs} +30 -42
- package/dist/Router-C4ADUfhv.mjs.map +1 -0
- package/dist/Server-BnjLJleZ.mjs +104 -0
- package/dist/Server-BnjLJleZ.mjs.map +1 -0
- package/dist/Server-Cler2w5O.d.mts +61 -0
- package/dist/Server-Cler2w5O.d.mts.map +1 -0
- package/dist/ServerEvent-CTx3Q-hk.d.mts +48 -0
- package/dist/ServerEvent-CTx3Q-hk.d.mts.map +1 -0
- package/dist/{ServerEvent-CxPwA4lH.mjs → ServerEvent-Dljn5i7A.mjs} +37 -11
- package/dist/ServerEvent-Dljn5i7A.mjs.map +1 -0
- package/dist/Site-CE0_o5Ci.d.mts +566 -0
- package/dist/Site-CE0_o5Ci.d.mts.map +1 -0
- package/dist/StandardSchema-D28L8_zZ.d.mts +100 -0
- package/dist/StandardSchema-D28L8_zZ.d.mts.map +1 -0
- package/dist/TypedResponse-6lznqN4U.d.mts +22 -0
- package/dist/TypedResponse-6lznqN4U.d.mts.map +1 -0
- package/dist/{_page-BQs4WvCO.mjs → _page-4cQKs_Dn.mjs} +3 -5
- package/dist/_page-4cQKs_Dn.mjs.map +1 -0
- package/dist/bin/index.d.mts +1 -1
- package/dist/bin/index.js +15 -11
- package/dist/bin/index.js.map +1 -1
- package/dist/clientEntry.d.mts +1 -0
- package/dist/clientEntry.mjs +59 -0
- package/dist/clientEntry.mjs.map +1 -0
- package/dist/clientEntryDev.d.mts +1 -0
- package/dist/clientEntryDev.mjs +7 -0
- package/dist/clientEntryDev.mjs.map +1 -0
- package/dist/dev.d.mts +3 -0
- package/dist/dev.mjs +3 -0
- package/dist/endpoint-EULRGCKA.mjs +108 -0
- package/dist/endpoint-EULRGCKA.mjs.map +1 -0
- package/dist/flattenHeaders-DM5qsSqO.mjs +13 -0
- package/dist/flattenHeaders-DM5qsSqO.mjs.map +1 -0
- package/dist/form.d.mts +35 -0
- package/dist/form.d.mts.map +1 -0
- package/dist/form.mjs +48 -0
- package/dist/form.mjs.map +1 -0
- package/dist/index.d.mts +72 -156
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +158 -26
- package/dist/index.mjs.map +1 -1
- package/dist/load-BntzvmwL.mjs +228 -0
- package/dist/load-BntzvmwL.mjs.map +1 -0
- package/dist/nav.d.mts +58 -5
- package/dist/nav.d.mts.map +1 -1
- package/dist/nav.mjs +63 -209
- package/dist/nav.mjs.map +1 -1
- package/dist/openapi.d.mts +144 -0
- package/dist/openapi.d.mts.map +1 -0
- package/dist/openapi.mjs +2 -0
- package/dist/pathTrie-D4Ax2Mu9.mjs +160 -0
- package/dist/pathTrie-D4Ax2Mu9.mjs.map +1 -0
- package/dist/plugin-D8Www11B.mjs +202 -0
- package/dist/plugin-D8Www11B.mjs.map +1 -0
- package/dist/{response-C5TtAsh1.mjs → response-Bg4w2S1q.mjs} +16 -39
- package/dist/response-Bg4w2S1q.mjs.map +1 -0
- package/dist/response.d.mts +309 -289
- package/dist/response.d.mts.map +1 -1
- package/dist/response.mjs +3 -4
- package/dist/run.d.mts +11 -4
- package/dist/run.d.mts.map +1 -1
- package/dist/run.mjs +2 -8
- package/dist/runOpenApi-DlOgF2Il.mjs +1419 -0
- package/dist/runOpenApi-DlOgF2Il.mjs.map +1 -0
- package/dist/schema.d.mts +32 -0
- package/dist/schema.d.mts.map +1 -0
- package/dist/schema.mjs +2 -0
- package/dist/server/Server.d.mts +2 -0
- package/dist/server/Server.mjs +2 -0
- package/dist/server.d.mts +11 -8
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +133 -4
- package/dist/server.mjs.map +1 -0
- package/dist/serverEntry-1qY3wZig.mjs +351 -0
- package/dist/serverEntry-1qY3wZig.mjs.map +1 -0
- package/dist/serverEntry-BAO8m03V.d.mts +6 -0
- package/dist/serverEntry-BAO8m03V.d.mts.map +1 -0
- package/dist/serverEntry.d.mts +2 -0
- package/dist/serverEntry.mjs +2 -0
- package/dist/state.d.mts.map +1 -1
- package/dist/state.mjs +2 -3
- package/dist/test.d.mts +8 -10
- package/dist/test.d.mts.map +1 -1
- package/dist/test.mjs +181 -79
- package/dist/test.mjs.map +1 -1
- package/dist/{seeOther-B4Yhu9iq.mjs → unprocessable-DAZbrDeR.mjs} +34 -13
- package/dist/unprocessable-DAZbrDeR.mjs.map +1 -0
- package/dist/validate-Ok4krlkX.mjs +40 -0
- package/dist/validate-Ok4krlkX.mjs.map +1 -0
- package/package.json +30 -18
- package/dist/RouteType-B6BMOyYw.mjs.map +0 -1
- package/dist/Server-CF5wDJp6.d.mts +0 -78
- package/dist/Server-CF5wDJp6.d.mts.map +0 -1
- package/dist/ServerEvent-CxPwA4lH.mjs.map +0 -1
- package/dist/Site-DgC6WWn1.d.mts +0 -78
- package/dist/Site-DgC6WWn1.d.mts.map +0 -1
- package/dist/_page-BQs4WvCO.mjs.map +0 -1
- package/dist/connectMiddleware-D7nehjj3.mjs +0 -252
- package/dist/connectMiddleware-D7nehjj3.mjs.map +0 -1
- package/dist/pathToRegex-TUAMc3zS.mjs +0 -11
- package/dist/pathToRegex-TUAMc3zS.mjs.map +0 -1
- package/dist/response-C5TtAsh1.mjs.map +0 -1
- package/dist/run-BPFtSYA3.mjs +0 -332
- package/dist/run-BPFtSYA3.mjs.map +0 -1
- package/dist/seeOther-B4Yhu9iq.mjs.map +0 -1
- package/src/bin/index.ts +0 -11
- package/src/index.ts +0 -30
- package/src/nav/formSubmit.ts +0 -56
- package/src/nav/load.ts +0 -9
- package/src/nav/loadData.ts +0 -134
- package/src/nav/navigate.ts +0 -164
- package/src/nav/reload.ts +0 -9
- package/src/nav.ts +0 -4
- package/src/response/badRequest.ts +0 -19
- package/src/response/created.ts +0 -20
- package/src/response/forbidden.ts +0 -21
- package/src/response/found.ts +0 -30
- package/src/response/methodNotAllowed.ts +0 -18
- package/src/response/movedPermanently.ts +0 -25
- package/src/response/notFound.ts +0 -24
- package/src/response/notModified.ts +0 -34
- package/src/response/ok.ts +0 -29
- package/src/response/permanentRedirect.ts +0 -30
- package/src/response/response.ts +0 -22
- package/src/response/seeOther.ts +0 -19
- package/src/response/serverError.ts +0 -24
- package/src/response/temporaryRedirect.ts +0 -31
- package/src/response/transfer.ts +0 -14
- package/src/response/unauthorized.ts +0 -21
- package/src/response/unprocessable.ts +0 -18
- package/src/response.ts +0 -37
- package/src/run/prepareTemplate.ts +0 -39
- package/src/run/run.ts +0 -62
- package/src/run/runBuild.ts +0 -77
- package/src/run/runDev.ts +0 -89
- package/src/run/runPreview.ts +0 -93
- package/src/run.ts +0 -6
- package/src/server/CookieHelper.ts +0 -36
- package/src/server/HeaderHelper.ts +0 -25
- package/src/server/Routerx.ts +0 -72
- package/src/server/Server.ts +0 -144
- package/src/server/ServerEvent.ts +0 -39
- package/src/server/connect/bufferToArrayBuffer.ts +0 -8
- package/src/server/connect/connectMiddleware.ts +0 -81
- package/src/server/connect/flattenHeaders.ts +0 -44
- package/src/server/connect/nodeMessageToNodeResponse.ts +0 -89
- package/src/server/connect/readableToBuffer.ts +0 -16
- package/src/server/connect/requestToNodeMessage.ts +0 -26
- package/src/server/contentType.ts +0 -85
- package/src/server/types/HttpMethod.ts +0 -12
- package/src/server/types/MiddlewareFunction.ts +0 -8
- package/src/server/types/ServerFunction.ts +0 -7
- package/src/server.ts +0 -7
- package/src/site/Router.ts +0 -164
- package/src/site/Site.ts +0 -214
- package/src/site/clientEntry.ts +0 -108
- package/src/site/clientEntryDev.ts +0 -3
- package/src/site/defaultAdapter.ts +0 -11
- package/src/site/manifest.ts +0 -54
- package/src/site/serverEntry.ts +0 -439
- package/src/state/$page.ts +0 -24
- package/src/state/client.ts +0 -15
- package/src/state.ts +0 -3
- package/src/test/runTest.ts +0 -350
- package/src/test.ts +0 -3
- package/src/types/Adapter.ts +0 -8
- package/src/types/ClientState.ts +0 -8
- package/src/types/InternalState.ts +0 -7
- package/src/types/LayoutHandler.ts +0 -5
- package/src/types/LayoutPath.ts +0 -10
- package/src/types/ManifestRoute.ts +0 -6
- package/src/types/PageEndPoint.ts +0 -39
- package/src/types/PageLoadEvent.ts +0 -14
- package/src/types/PageServerAction.ts +0 -7
- package/src/types/PageServerEndPoint.ts +0 -16
- package/src/types/PageServerLoad.ts +0 -7
- package/src/types/PageState.ts +0 -8
- package/src/types/Route.ts +0 -11
- package/src/types/RouteHandler.ts +0 -16
- package/src/types/RouteLayoutHandler.ts +0 -5
- package/src/types/RouteMatchResult.ts +0 -7
- package/src/types/RouteType.ts +0 -19
- package/src/types/ServerEndPoint.ts +0 -37
- package/src/types/ServerHook.ts +0 -11
- package/src/types/ServerLoadEvent.ts +0 -37
- package/src/types/ServerRequest.ts +0 -7
- package/src/utils/pathToRegex.ts +0 -15
- package/src/vite-env.d.ts +0 -2
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// From https://stackoverflow.com/a/79345620
|
|
2
|
-
// No idea why it is needed
|
|
3
|
-
export default function bufferToArrayBuffer(buffer: Buffer): ArrayBuffer {
|
|
4
|
-
return buffer.buffer.slice(
|
|
5
|
-
buffer.byteOffset,
|
|
6
|
-
buffer.byteOffset + buffer.byteLength,
|
|
7
|
-
) as ArrayBuffer;
|
|
8
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import type MiddlewareFunction from "../types/MiddlewareFunction";
|
|
3
|
-
import bufferToArrayBuffer from "./bufferToArrayBuffer";
|
|
4
|
-
import flattenHeaders from "./flattenHeaders";
|
|
5
|
-
import nodeMessageToNodeResponse from "./nodeMessageToNodeResponse";
|
|
6
|
-
import readableToBuffer from "./readableToBuffer";
|
|
7
|
-
import requestToNodeMessage from "./requestToNodeMessage";
|
|
8
|
-
|
|
9
|
-
// From https://github.com/vikejs/vike-node/blob/main/packages/vike-node/src/runtime/adapters/connectToWeb.ts
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Converts a Connect-style middleware to a web-compatible request handler.
|
|
13
|
-
*
|
|
14
|
-
* @param handler - The Connect-style middleware function to be converted.
|
|
15
|
-
* @returns A function that handles web requests and returns a Response or undefined.
|
|
16
|
-
*/
|
|
17
|
-
export default function connectMiddleware(
|
|
18
|
-
handler: ConnectMiddleware | ConnectMiddlewareBoolean,
|
|
19
|
-
): MiddlewareFunction {
|
|
20
|
-
return async (ev, next) => {
|
|
21
|
-
const req = requestToNodeMessage(ev.request);
|
|
22
|
-
const { res, onReadable } = nodeMessageToNodeResponse(req);
|
|
23
|
-
|
|
24
|
-
return new Promise<void>(async (resolve, reject) => {
|
|
25
|
-
onReadable(async ({ readable, headers, status }) => {
|
|
26
|
-
const responseBody = statusCodesWithoutBody.includes(status)
|
|
27
|
-
? null
|
|
28
|
-
: //(Readable.toWeb(readable) as ReadableStream);
|
|
29
|
-
// HACK: Chunked bodies are not being
|
|
30
|
-
// read correctly somewhere...
|
|
31
|
-
bufferToArrayBuffer(await readableToBuffer(readable));
|
|
32
|
-
ev.response = new Response(responseBody, {
|
|
33
|
-
status,
|
|
34
|
-
headers: flattenHeaders(headers),
|
|
35
|
-
});
|
|
36
|
-
resolve();
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const cnext = async (error?: unknown) => {
|
|
40
|
-
if (error) {
|
|
41
|
-
// eslint-disable-next-line no-base-to-string
|
|
42
|
-
reject(error instanceof Error ? error : new Error(String(error)));
|
|
43
|
-
} else {
|
|
44
|
-
// TODO: Can we nest the next functions less? This is a bit weird
|
|
45
|
-
await next();
|
|
46
|
-
resolve();
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
const handled = await handler(req, res, cnext);
|
|
52
|
-
if (handled === false) {
|
|
53
|
-
res.destroy();
|
|
54
|
-
resolve();
|
|
55
|
-
}
|
|
56
|
-
} catch (e) {
|
|
57
|
-
await cnext(e);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const statusCodesWithoutBody = [
|
|
64
|
-
100, // Continue
|
|
65
|
-
101, // Switching Protocols
|
|
66
|
-
102, // Processing (WebDAV)
|
|
67
|
-
103, // Early Hints
|
|
68
|
-
204, // No Content
|
|
69
|
-
205, // Reset Content
|
|
70
|
-
304, // Not Modified
|
|
71
|
-
];
|
|
72
|
-
|
|
73
|
-
type NextFunction = (err?: unknown) => void;
|
|
74
|
-
type ConnectMiddleware<
|
|
75
|
-
PlatformRequest extends IncomingMessage = IncomingMessage,
|
|
76
|
-
PlatformResponse extends ServerResponse = ServerResponse,
|
|
77
|
-
> = (req: PlatformRequest, res: PlatformResponse, next: NextFunction) => void | Promise<void>;
|
|
78
|
-
type ConnectMiddlewareBoolean<
|
|
79
|
-
PlatformRequest extends IncomingMessage = IncomingMessage,
|
|
80
|
-
PlatformResponse extends ServerResponse = ServerResponse,
|
|
81
|
-
> = (req: PlatformRequest, res: PlatformResponse, next: NextFunction) => boolean | Promise<boolean>;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { IncomingHttpHeaders } from "node:http";
|
|
2
|
-
import { OutgoingHttpHeaders } from "node:http2";
|
|
3
|
-
|
|
4
|
-
export default function flattenHeaders(
|
|
5
|
-
headers: IncomingHttpHeaders | OutgoingHttpHeaders,
|
|
6
|
-
): HeadersInit {
|
|
7
|
-
const flatHeaders = new Headers();
|
|
8
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
9
|
-
if (value === undefined || value === null) {
|
|
10
|
-
continue;
|
|
11
|
-
} else if (Array.isArray(value)) {
|
|
12
|
-
for (const v of value) {
|
|
13
|
-
if (v !== undefined && v !== null) {
|
|
14
|
-
value.forEach((v) => flatHeaders.append(key, v));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
} else {
|
|
18
|
-
flatHeaders.set(key, String(value));
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return flatHeaders;
|
|
22
|
-
|
|
23
|
-
/*
|
|
24
|
-
const flatHeaders: [string, string][] = [];
|
|
25
|
-
|
|
26
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
27
|
-
if (value === undefined || value === null) {
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (Array.isArray(value)) {
|
|
32
|
-
for (const v of value) {
|
|
33
|
-
if (v != null) {
|
|
34
|
-
flatHeaders.push([key, String(v)]);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
} else {
|
|
38
|
-
flatHeaders.push([key, String(value)]);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return flatHeaders;
|
|
43
|
-
*/
|
|
44
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type IncomingMessage,
|
|
3
|
-
type OutgoingHttpHeader,
|
|
4
|
-
type OutgoingHttpHeaders,
|
|
5
|
-
ServerResponse,
|
|
6
|
-
} from "node:http";
|
|
7
|
-
import { PassThrough, Readable } from "node:stream";
|
|
8
|
-
|
|
9
|
-
// From https://github.com/vikejs/vike-node/blob/main/packages/vike-node/src/runtime/adapters/createServerResponse.ts
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Creates a custom ServerResponse object that allows for intercepting and
|
|
13
|
-
* streaming the response.
|
|
14
|
-
*
|
|
15
|
-
* @param incomingMessage The incoming HTTP request message.
|
|
16
|
-
* @returns An object containing the custom ServerResponse and a function that
|
|
17
|
-
* takes a callback to invoke when the response is readable, providing an object
|
|
18
|
-
* with the readable stream, headers and status code.
|
|
19
|
-
*/
|
|
20
|
-
export default function nodeMessageToNodeResponse(
|
|
21
|
-
incomingMessage: IncomingMessage,
|
|
22
|
-
): ServerResponseResponse {
|
|
23
|
-
const res = new ServerResponse(incomingMessage);
|
|
24
|
-
const passThrough = new PassThrough();
|
|
25
|
-
let handled = false;
|
|
26
|
-
|
|
27
|
-
const onReadable = (
|
|
28
|
-
cb: (result: { readable: Readable; headers: OutgoingHttpHeaders; status: number }) => void,
|
|
29
|
-
) => {
|
|
30
|
-
const handleReadable = () => {
|
|
31
|
-
if (handled) return;
|
|
32
|
-
handled = true;
|
|
33
|
-
cb({
|
|
34
|
-
readable: Readable.from(passThrough),
|
|
35
|
-
headers: res.getHeaders(),
|
|
36
|
-
status: res.statusCode,
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
passThrough.once("readable", handleReadable);
|
|
41
|
-
passThrough.once("end", handleReadable);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
passThrough.once("finish", () => {
|
|
45
|
-
res.emit("finish");
|
|
46
|
-
});
|
|
47
|
-
passThrough.once("close", () => {
|
|
48
|
-
res.destroy();
|
|
49
|
-
res.emit("close");
|
|
50
|
-
});
|
|
51
|
-
passThrough.on("drain", () => {
|
|
52
|
-
res.emit("drain");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
res.write = passThrough.write.bind(passThrough);
|
|
56
|
-
res.end = passThrough.end.bind(passThrough) as any;
|
|
57
|
-
|
|
58
|
-
res.writeHead = function writeHead(
|
|
59
|
-
statusCode: number,
|
|
60
|
-
statusMessage?: string | OutgoingHttpHeaders | OutgoingHttpHeader[],
|
|
61
|
-
headers?: OutgoingHttpHeaders | OutgoingHttpHeader[],
|
|
62
|
-
): ServerResponse {
|
|
63
|
-
res.statusCode = statusCode;
|
|
64
|
-
if (typeof statusMessage === "object") {
|
|
65
|
-
headers = statusMessage;
|
|
66
|
-
statusMessage = undefined;
|
|
67
|
-
}
|
|
68
|
-
if (headers) {
|
|
69
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
70
|
-
if (value !== undefined) {
|
|
71
|
-
res.setHeader(key, value);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return res;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
res,
|
|
80
|
-
onReadable,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
interface ServerResponseResponse {
|
|
85
|
-
res: ServerResponse;
|
|
86
|
-
onReadable: (
|
|
87
|
-
cb: (result: { readable: Readable; headers: OutgoingHttpHeaders; status: number }) => void,
|
|
88
|
-
) => void;
|
|
89
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Readable } from "node:stream";
|
|
2
|
-
|
|
3
|
-
export default function readableToBuffer(req: Readable): Promise<Buffer> {
|
|
4
|
-
return new Promise<Buffer>((resolve, reject) => {
|
|
5
|
-
const chunks: Buffer[] = [];
|
|
6
|
-
req.on("data", (chunk: Buffer) => {
|
|
7
|
-
chunks.push(chunk);
|
|
8
|
-
});
|
|
9
|
-
req.on("end", () => {
|
|
10
|
-
resolve(Buffer.concat(chunks));
|
|
11
|
-
});
|
|
12
|
-
req.on("error", (err) => {
|
|
13
|
-
reject(err);
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { type IncomingMessage } from "node:http";
|
|
2
|
-
import { Readable } from "node:stream";
|
|
3
|
-
|
|
4
|
-
// From https://github.com/vikejs/vike-node/blob/main/packages/vike-node/src/runtime/adapters/connectToWeb.ts
|
|
5
|
-
|
|
6
|
-
const DUMMY_BASE_URL = "http://localhost";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Creates an IncomingMessage object from a web Request.
|
|
10
|
-
*
|
|
11
|
-
* @param {Request} request - The web Request object.
|
|
12
|
-
* @returns {IncomingMessage} An IncomingMessage-like object compatible with Node.js HTTP module.
|
|
13
|
-
*/
|
|
14
|
-
export default function requestToNodeMessage(request: Request): IncomingMessage {
|
|
15
|
-
const parsedUrl = new URL(request.url, DUMMY_BASE_URL);
|
|
16
|
-
const pathAndQuery = (parsedUrl.pathname || "") + (parsedUrl.search || "");
|
|
17
|
-
// HACK: Using Readable.fromWeb locks request.body so that the request cannot be used again
|
|
18
|
-
// As we're only using this for Vite middlewares, it's safe to discard the body for now?
|
|
19
|
-
const body = /*request.body ? Readable.fromWeb(request.body as any) :*/ Readable.from([]);
|
|
20
|
-
|
|
21
|
-
return Object.assign(body, {
|
|
22
|
-
url: pathAndQuery,
|
|
23
|
-
method: request.method,
|
|
24
|
-
headers: Object.fromEntries(request.headers),
|
|
25
|
-
}) as IncomingMessage;
|
|
26
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
// From https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types
|
|
2
|
-
|
|
3
|
-
export default function contentType(extension: string): string {
|
|
4
|
-
return types[extension] || "application/octet-stream";
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
const types: Record<string, string> = {
|
|
8
|
-
".aac": "audio/aac",
|
|
9
|
-
".abw": "application/x-abiword",
|
|
10
|
-
".apng": "image/apng",
|
|
11
|
-
".arc": "application/x-freearc",
|
|
12
|
-
".avif": "image/avif",
|
|
13
|
-
".avi": "video/x-msvideo",
|
|
14
|
-
".azw": "application/vnd.amazon.ebook",
|
|
15
|
-
".bin": "application/octet-stream",
|
|
16
|
-
".bmp": "image/bmp",
|
|
17
|
-
".bz": "application/x-bzip",
|
|
18
|
-
".bz2": "application/x-bzip2",
|
|
19
|
-
".cda": "application/x-cdf",
|
|
20
|
-
".csh": "application/x-csh",
|
|
21
|
-
".css": "text/css",
|
|
22
|
-
".csv": "text/csv",
|
|
23
|
-
".doc": "application/msword",
|
|
24
|
-
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
25
|
-
".eot": "application/vnd.ms-fontobject",
|
|
26
|
-
".epub": "application/epub+zip",
|
|
27
|
-
".gz": "application/gzip",
|
|
28
|
-
".gif": "image/gif",
|
|
29
|
-
".htm": "text/html",
|
|
30
|
-
".html": "text/html",
|
|
31
|
-
".ico": "image/vnd.microsoft.icon",
|
|
32
|
-
".ics": "text/calendar",
|
|
33
|
-
".jar": "application/java-archive",
|
|
34
|
-
".jpeg": "image/jpeg",
|
|
35
|
-
".jpg": "image/jpeg",
|
|
36
|
-
".js": "text/javascript",
|
|
37
|
-
".json": "application/json",
|
|
38
|
-
".jsonld": "application/ld+json",
|
|
39
|
-
".mid": "audio/midi",
|
|
40
|
-
".midi": "audio/midi",
|
|
41
|
-
".mjs": "text/javascript",
|
|
42
|
-
".mp3": "audio/mpeg",
|
|
43
|
-
".mp4": "video/mp4",
|
|
44
|
-
".mpeg": "video/mpeg",
|
|
45
|
-
".mpkg": "application/vnd.apple.installer+xml",
|
|
46
|
-
".odp": "application/vnd.oasis.opendocument.presentation",
|
|
47
|
-
".ods": "application/vnd.oasis.opendocument.spreadsheet",
|
|
48
|
-
".odt": "application/vnd.oasis.opendocument.text",
|
|
49
|
-
".oga": "audio/ogg",
|
|
50
|
-
".ogv": "video/ogg",
|
|
51
|
-
".ogx": "application/ogg",
|
|
52
|
-
".opus": "audio/ogg",
|
|
53
|
-
".otf": "font/otf",
|
|
54
|
-
".png": "image/png",
|
|
55
|
-
".pdf": "application/pdf",
|
|
56
|
-
".php": "application/x-httpd-php",
|
|
57
|
-
".ppt": "application/vnd.ms-powerpoint",
|
|
58
|
-
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
59
|
-
".rar": "application/vnd.rar",
|
|
60
|
-
".rtf": "application/rtf",
|
|
61
|
-
".sh": "application/x-sh",
|
|
62
|
-
".svg": "image/svg+xml",
|
|
63
|
-
".tar": "application/x-tar",
|
|
64
|
-
".tif": "image/tiff",
|
|
65
|
-
".tiff": "image/tiff",
|
|
66
|
-
".ts": "video/mp2t",
|
|
67
|
-
".ttf": "font/ttf",
|
|
68
|
-
".txt": "text/plain",
|
|
69
|
-
".vsd": "application/vnd.visio",
|
|
70
|
-
".wav": "audio/wav",
|
|
71
|
-
".weba": "audio/webm",
|
|
72
|
-
".webm": "video/webm",
|
|
73
|
-
".webp": "image/webp",
|
|
74
|
-
".woff": "font/woff",
|
|
75
|
-
".woff2": "font/woff2",
|
|
76
|
-
".xhtml": "application/xhtml+xml",
|
|
77
|
-
".xls": "application/vnd.ms-excel",
|
|
78
|
-
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
79
|
-
".xml": "application/xml",
|
|
80
|
-
".xul": "application/vnd.mozilla.xul+xml",
|
|
81
|
-
".zip": "application/zip",
|
|
82
|
-
".3gp": "video/3gpp",
|
|
83
|
-
".3g2": "video/3gpp2",
|
|
84
|
-
".7z": "application/x-7z-compressed",
|
|
85
|
-
};
|
package/src/server.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import CookieHelper from "./server/CookieHelper";
|
|
2
|
-
import HeaderHelper from "./server/HeaderHelper";
|
|
3
|
-
import Server from "./server/Server";
|
|
4
|
-
import ServerEvent from "./server/ServerEvent";
|
|
5
|
-
import connectMiddleware from "./server/connect/connectMiddleware";
|
|
6
|
-
|
|
7
|
-
export { Server, ServerEvent, CookieHelper, HeaderHelper, connectMiddleware };
|
package/src/site/Router.ts
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import type LayoutHandler from "../types/LayoutHandler";
|
|
2
|
-
import type ManifestRoute from "../types/ManifestRoute";
|
|
3
|
-
import type RouteHandler from "../types/RouteHandler";
|
|
4
|
-
import pathToRegex from "../utils/pathToRegex";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A router that handles file routes with layouts, hooks, etc.
|
|
8
|
-
*/
|
|
9
|
-
export default class Router {
|
|
10
|
-
routes: Route[] = [];
|
|
11
|
-
|
|
12
|
-
constructor() {}
|
|
13
|
-
|
|
14
|
-
//add(path: string, file: string) {
|
|
15
|
-
// // TODO: Call the specific addX method based on the file path
|
|
16
|
-
// this.addPage(route, file);
|
|
17
|
-
//}
|
|
18
|
-
|
|
19
|
-
addPages(routes: ManifestRoute[]): this {
|
|
20
|
-
for (let r of routes) {
|
|
21
|
-
this.routes.push(
|
|
22
|
-
new Route(r.path, {
|
|
23
|
-
path: r.path,
|
|
24
|
-
type: r.type,
|
|
25
|
-
endPoint: r.endPoint,
|
|
26
|
-
subFolder: r.subFolder,
|
|
27
|
-
}),
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
this.#sortRoutes();
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// TODO: Allow calling with the endpoint itself, so that you can setup an app with no scaffold
|
|
35
|
-
addPage(path: string, type: number, endPoint: () => Promise<any>, subFolder?: string): this {
|
|
36
|
-
this.routes.push(
|
|
37
|
-
new Route(path, {
|
|
38
|
-
path,
|
|
39
|
-
type,
|
|
40
|
-
endPoint,
|
|
41
|
-
subFolder,
|
|
42
|
-
}),
|
|
43
|
-
);
|
|
44
|
-
this.#sortRoutes();
|
|
45
|
-
return this;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/*
|
|
49
|
-
loadPage(ev: ServerEvent, file: string): Response {
|
|
50
|
-
let page = this.pages.get(file);
|
|
51
|
-
if (!page) {
|
|
52
|
-
page = {};
|
|
53
|
-
this.pages.set(file, page);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
match(path: string, query: URLSearchParams): RouteMatch | undefined {
|
|
59
|
-
for (let route of this.routes) {
|
|
60
|
-
let match = path.match(route.regex);
|
|
61
|
-
if (match) {
|
|
62
|
-
// Lazy load server endpoints and layouts
|
|
63
|
-
if (!route.handler.loaded) {
|
|
64
|
-
this.#loadHandler(route.handler, route.path);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
handler: route.handler,
|
|
69
|
-
params: match.groups,
|
|
70
|
-
query,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
#loadHandler(handler: RouteHandler, path: string) {
|
|
77
|
-
handler.layouts = this.#findLayouts(path, handler);
|
|
78
|
-
handler.serverEndPoint = this.#findServer(path);
|
|
79
|
-
handler.serverHook = this.#findServerHook(handler);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
#findLayouts(path: string, handler: RouteHandler): LayoutHandler[] | undefined {
|
|
83
|
-
let layouts: LayoutHandler[] = [];
|
|
84
|
-
let parts = path
|
|
85
|
-
// Strip the trailing`~/server` (so we don't look for `~/server/_layout`
|
|
86
|
-
// which will never exist)
|
|
87
|
-
.replace(/\/~server$/, "")
|
|
88
|
-
// The path will always start with / so splitting e.g. `/first/second`
|
|
89
|
-
// will result in ['', 'first', 'second'] and checks for `/_layout`,
|
|
90
|
-
// `/first/_layout` and `/second/_layout`
|
|
91
|
-
.split("/");
|
|
92
|
-
let basePath = "";
|
|
93
|
-
for (let i = 0; i < parts.length; i++) {
|
|
94
|
-
if (i > 0) {
|
|
95
|
-
basePath += "/" + parts[i];
|
|
96
|
-
}
|
|
97
|
-
const layoutPath = basePath + "/_layout";
|
|
98
|
-
const layoutRoute = this.routes.find((r) => r.path === layoutPath);
|
|
99
|
-
if (layoutRoute && layoutRoute.handler.subFolder === handler.subFolder) {
|
|
100
|
-
layouts.push({
|
|
101
|
-
path: layoutPath,
|
|
102
|
-
endPoint: layoutRoute.handler.endPoint,
|
|
103
|
-
serverEndPoint: this.#findServer(layoutPath),
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return layouts.length ? layouts : undefined;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
#findServer(path: string): (() => Promise<any>) | undefined {
|
|
111
|
-
const serverPath = path.replace(/\/$/, "") + "/~server";
|
|
112
|
-
const serverRoute = this.routes.find((r) => r.path === serverPath);
|
|
113
|
-
return serverRoute && serverRoute.handler.endPoint;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
#findServerHook(handler: RouteHandler): (() => Promise<any>) | undefined {
|
|
117
|
-
// TODO: Should this be a collection, like layouts?
|
|
118
|
-
let serverHookPath = "/_hook/~server";
|
|
119
|
-
if (handler.subFolder !== undefined) {
|
|
120
|
-
serverHookPath = handler.subFolder + serverHookPath;
|
|
121
|
-
}
|
|
122
|
-
const serverHookRoute = this.routes.find((r) => r.path === serverHookPath);
|
|
123
|
-
return serverHookRoute && serverHookRoute.handler.endPoint;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// HACK: We're doing this here as well as in Site, so that you can use the
|
|
127
|
-
// Router externally without Site
|
|
128
|
-
#sortRoutes() {
|
|
129
|
-
this.routes = this.routes.sort((a, b) => {
|
|
130
|
-
// Sort [param]s after paths
|
|
131
|
-
// There might be a quicker/easier way to do this
|
|
132
|
-
for (let i = 0; i < Math.min(a.path.length, b.path.length); i++) {
|
|
133
|
-
if (a.path[i] === "[" && b.path[i] !== "[") {
|
|
134
|
-
return 1;
|
|
135
|
-
} else if (b.path[i] === "[" && a.path[i] !== "[") {
|
|
136
|
-
return -1;
|
|
137
|
-
} else if (a.path[i] === b.path[i]) {
|
|
138
|
-
// Keep going...
|
|
139
|
-
} else {
|
|
140
|
-
return a.path[i].localeCompare(b.path[i]);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return a.path.length - b.path.length;
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
class Route {
|
|
149
|
-
path: string;
|
|
150
|
-
regex: RegExp;
|
|
151
|
-
handler: RouteHandler;
|
|
152
|
-
|
|
153
|
-
constructor(path: string, handler: RouteHandler) {
|
|
154
|
-
this.path = path;
|
|
155
|
-
this.regex = pathToRegex(path);
|
|
156
|
-
this.handler = handler;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
interface RouteMatch {
|
|
161
|
-
handler: RouteHandler;
|
|
162
|
-
params?: Record<string, string>;
|
|
163
|
-
query: URLSearchParams;
|
|
164
|
-
}
|