@torpor/build 0.4.8 → 0.4.10
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/nav.mjs +3 -1
- package/dist/nav.mjs.map +1 -1
- package/package.json +7 -3
- package/CHANGELOG.md +0 -467
- package/scripts/postbuild.ts +0 -12
- package/src/bin/index.ts +0 -11
- package/src/index.ts +0 -30
- package/src/nav/formSubmit.ts +0 -49
- 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 -417
- 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
- package/test/api/_hook.server.ts +0 -3
- package/test/api/posts/+page.server.ts +0 -3
- package/test/api/posts/[id]/+page.server.ts +0 -3
- package/test/app.test.ts +0 -47
- package/test/router.test.ts +0 -78
- package/test/routes/+page.server.ts +0 -3
- package/test/routes/+page.ts +0 -3
- package/test/routes/_error.ts +0 -3
- package/test/routes/_hook.server.ts +0 -3
- package/test/routes/_layout.server.ts +0 -3
- package/test/routes/_layout.ts +0 -3
- package/test/routes/posts/+page.ts +0 -3
- package/test/routes/posts/[id]/+page.ts +0 -3
- package/test/site.test.ts +0 -40
- package/tsconfig.json +0 -9
- package/tsdown.config.ts +0 -18
- package/vitest.config.ts +0 -3
package/src/server/Server.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import pathToRegex from "../utils/pathToRegex";
|
|
2
|
-
import ServerEvent from "./ServerEvent";
|
|
3
|
-
import type MiddlewareFunction from "./types/MiddlewareFunction";
|
|
4
|
-
import type ServerFunction from "./types/ServerFunction";
|
|
5
|
-
|
|
6
|
-
const noop = () => {};
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* A Fetch API Request/Response server.
|
|
10
|
-
*/
|
|
11
|
-
export default class Server {
|
|
12
|
-
// TODO: We don't use any of the routes, just add("*") in runDev/runBuild...
|
|
13
|
-
routes: RouteHandler[] = [];
|
|
14
|
-
// TODO: We only use the Vite middleware here
|
|
15
|
-
middleware: MiddlewareFunction[] = [];
|
|
16
|
-
|
|
17
|
-
constructor() {
|
|
18
|
-
this.routes = [];
|
|
19
|
-
this.middleware = [];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The standard Fetch API method that should work in any runtime (including
|
|
24
|
-
* Node with polyfills).
|
|
25
|
-
* @returns
|
|
26
|
-
*/
|
|
27
|
-
fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
|
28
|
-
const request = new Request(input, init);
|
|
29
|
-
const url = new URL(request.url);
|
|
30
|
-
const match = this.match(url.pathname);
|
|
31
|
-
let ev = new ServerEvent(request, match?.params);
|
|
32
|
-
|
|
33
|
-
if (this.middleware.length) {
|
|
34
|
-
// TODO: Get middleware that applies to this route only?
|
|
35
|
-
await this.middleware[0](ev, buildNext(this.middleware, 0));
|
|
36
|
-
function buildNext(middleware: MiddlewareFunction[], i: number): () => void | Promise<void> {
|
|
37
|
-
if (ev.response) {
|
|
38
|
-
return noop;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (i < middleware.length - 1) {
|
|
42
|
-
let next = middleware[i + 1];
|
|
43
|
-
return async () => next(ev, buildNext(middleware, i + 1));
|
|
44
|
-
} else {
|
|
45
|
-
return match
|
|
46
|
-
? async () => {
|
|
47
|
-
ev.response = await match.fn(ev);
|
|
48
|
-
}
|
|
49
|
-
: noop;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
} else if (match) {
|
|
53
|
-
ev.response = await match.fn(ev);
|
|
54
|
-
} else {
|
|
55
|
-
return new Response(null, { status: 404 });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// TODO: Should I be returning 200 if there's no response? Because it's not an error??
|
|
59
|
-
ev.response ??= new Response(null, { status: 200 });
|
|
60
|
-
ev.addHeaders();
|
|
61
|
-
|
|
62
|
-
return ev.response;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Adds a server function at a route path.
|
|
67
|
-
* @param path The route path pattern.
|
|
68
|
-
* @param fn The function to call when the pattern is matched.
|
|
69
|
-
* @returns
|
|
70
|
-
*/
|
|
71
|
-
add(path: string, fn: ServerFunction): Server {
|
|
72
|
-
this.routes.push(new RouteHandler(path, fn));
|
|
73
|
-
this.#sortRoutes();
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
match(path: string): Match | undefined {
|
|
78
|
-
for (let handler of this.routes) {
|
|
79
|
-
let match = path.match(handler.regex);
|
|
80
|
-
if (match) {
|
|
81
|
-
return {
|
|
82
|
-
fn: handler.fn,
|
|
83
|
-
params: match.groups,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
console.log(`Server path not found: ${path}`);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// TODO:
|
|
91
|
-
//use(path: string, ...fn: MiddlewareFunction[])
|
|
92
|
-
use(...fn: MiddlewareFunction[]): Server {
|
|
93
|
-
this.middleware = this.middleware.concat(fn);
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
#sortRoutes() {
|
|
98
|
-
this.routes = this.routes.sort((a, b) => {
|
|
99
|
-
// Sort [param]s after paths
|
|
100
|
-
// There might be a quicker/easier way to do this
|
|
101
|
-
for (let i = 0; i < Math.min(a.path.length, b.path.length); i++) {
|
|
102
|
-
if (a.path[i] === "[" && b.path[i] !== "[") {
|
|
103
|
-
return 1;
|
|
104
|
-
} else if (b.path[i] === "[" && a.path[i] !== "[") {
|
|
105
|
-
return -1;
|
|
106
|
-
} else if (a.path[i] === b.path[i]) {
|
|
107
|
-
// Keep going...
|
|
108
|
-
} else {
|
|
109
|
-
return a.path[i].localeCompare(b.path[i]);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return a.path.length - b.path.length;
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
class RouteHandler {
|
|
118
|
-
path: string;
|
|
119
|
-
regex: RegExp;
|
|
120
|
-
fn: ServerFunction;
|
|
121
|
-
|
|
122
|
-
constructor(path: string, fn: ServerFunction) {
|
|
123
|
-
this.path = path;
|
|
124
|
-
this.regex = pathToRegex(path);
|
|
125
|
-
this.fn = fn;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
//class MiddlewareHandler {
|
|
130
|
-
// path: string;
|
|
131
|
-
// regex: RegExp;
|
|
132
|
-
// fn: MiddlewareFunction;
|
|
133
|
-
//
|
|
134
|
-
// constructor(path: string, fn: MiddlewareFunction) {
|
|
135
|
-
// this.path = path;
|
|
136
|
-
// this.regex = pathToRegex(path);
|
|
137
|
-
// this.fn = fn;
|
|
138
|
-
// }
|
|
139
|
-
//}
|
|
140
|
-
|
|
141
|
-
interface Match {
|
|
142
|
-
fn: ServerFunction;
|
|
143
|
-
params?: Record<string, string>;
|
|
144
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import CookieHelper from "./CookieHelper";
|
|
2
|
-
import HeaderHelper from "./HeaderHelper";
|
|
3
|
-
|
|
4
|
-
export default class ServerEvent {
|
|
5
|
-
request: Request;
|
|
6
|
-
response: Response | undefined | void;
|
|
7
|
-
// TODO: Should we cast types??
|
|
8
|
-
params?: Record<string, string>;
|
|
9
|
-
|
|
10
|
-
adapter: any;
|
|
11
|
-
|
|
12
|
-
cookies: CookieHelper;
|
|
13
|
-
headers: HeaderHelper;
|
|
14
|
-
|
|
15
|
-
constructor(request: Request, params?: Record<string, string>) {
|
|
16
|
-
this.request = request;
|
|
17
|
-
this.params = params;
|
|
18
|
-
this.cookies = new CookieHelper(request);
|
|
19
|
-
this.headers = new HeaderHelper(request);
|
|
20
|
-
|
|
21
|
-
// Adapters may have added the `adapter` property to globalThis
|
|
22
|
-
// @ts-ignore
|
|
23
|
-
this.adapter = globalThis.adapter;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
addHeaders(): void {
|
|
27
|
-
if (!this.response) {
|
|
28
|
-
throw new Error("Response not created yet");
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
for (let cookie of this.cookies.cookies.values()) {
|
|
32
|
-
this.response.headers.append("set-cookie", cookie);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
for (let header of this.headers.headers.entries()) {
|
|
36
|
-
this.response.headers.append(header[0], header[1]);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
@@ -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 };
|