@torpor/build 0.1.7 → 0.1.9
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/CHANGELOG.md +14 -0
- package/dist/{Site-Df1k0TuQ.d.cts → Server-B45PD5Ys.d.cts} +2 -27
- package/dist/{Site-Df1k0TuQ.d.ts → Server-B45PD5Ys.d.ts} +2 -27
- package/dist/Site-PxPCXGZ-.d.cts +29 -0
- package/dist/Site-Qpcmx0Xq.d.ts +29 -0
- package/dist/bin/index.cjs +21 -5
- package/dist/bin/index.cjs.map +1 -1
- package/dist/bin/index.js +3 -2
- package/dist/bin/index.js.map +1 -1
- package/dist/chunk-65Z6BF2N.js +355 -0
- package/dist/chunk-65Z6BF2N.js.map +1 -0
- package/dist/chunk-T5K52YVJ.js +12 -0
- package/dist/chunk-T5K52YVJ.js.map +1 -0
- package/dist/{chunk-GQ4JCTRP.js → chunk-ZSWD4UBH.js} +8 -170
- package/dist/chunk-ZSWD4UBH.js.map +1 -0
- package/dist/index.cjs +104 -142
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -3
- package/dist/index.d.ts +49 -3
- package/dist/index.js +129 -3
- package/dist/index.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/dist/metafile-esm.json +1 -1
- package/dist/run.cjs +21 -5
- package/dist/run.cjs.map +1 -1
- package/dist/run.d.cts +2 -1
- package/dist/run.d.ts +2 -1
- package/dist/run.js +3 -2
- package/dist/server.cjs +387 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +17 -0
- package/dist/server.d.ts +17 -0
- package/dist/server.js +16 -0
- package/dist/server.js.map +1 -0
- package/package.json +9 -14
- package/src/index.ts +2 -2
- package/src/{site → server}/Routerx.ts +4 -4
- package/src/server/Server.ts +22 -2
- package/src/server/connect/{createMiddlewareHandler.ts → connectMiddleware.ts} +2 -2
- package/src/{types → server/types}/MiddlewareFunction.ts +1 -1
- package/src/server/{ServerFunction.ts → types/ServerFunction.ts} +1 -1
- package/src/server.ts +7 -0
- package/src/site/Router.ts +35 -12
- package/src/site/manifest.ts +2 -2
- package/src/site/runDev.ts +2 -2
- package/src/types/ManifestRoute.ts +0 -1
- package/tsup.config.ts +8 -1
- package/dist/chunk-GQ4JCTRP.js.map +0 -1
- package/dist/chunk-L3QBUPXY.js +0 -176
- package/dist/chunk-L3QBUPXY.js.map +0 -1
- package/src/types/RouteHandlerCollection.ts +0 -9
- /package/src/{types → server/types}/HttpMethod.ts +0 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import {
|
|
2
|
+
pathToRegex
|
|
3
|
+
} from "./chunk-T5K52YVJ.js";
|
|
4
|
+
|
|
5
|
+
// src/server/CookieHelper.ts
|
|
6
|
+
import { parse, serialize } from "cookie";
|
|
7
|
+
var CookieHelper = class {
|
|
8
|
+
request;
|
|
9
|
+
cookies = /* @__PURE__ */ new Map();
|
|
10
|
+
constructor(request) {
|
|
11
|
+
this.request = request;
|
|
12
|
+
}
|
|
13
|
+
get(name) {
|
|
14
|
+
if (this.cookies.has(name)) {
|
|
15
|
+
return this.cookies.get(name);
|
|
16
|
+
}
|
|
17
|
+
const header = this.request.headers.get("cookie");
|
|
18
|
+
return parse(header ?? "")[name];
|
|
19
|
+
}
|
|
20
|
+
set(name, value, options) {
|
|
21
|
+
options ??= {};
|
|
22
|
+
options.httpOnly ??= true;
|
|
23
|
+
options.secure ??= true;
|
|
24
|
+
options.sameSite ??= "lax";
|
|
25
|
+
this.cookies.set(name, serialize(name, value, options));
|
|
26
|
+
}
|
|
27
|
+
delete(name, options) {
|
|
28
|
+
options ??= {};
|
|
29
|
+
options.expires = /* @__PURE__ */ new Date(0);
|
|
30
|
+
this.cookies.set(name, serialize(name, "", options));
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// src/server/HeaderHelper.ts
|
|
35
|
+
var HeaderHelper = class {
|
|
36
|
+
request;
|
|
37
|
+
headers = /* @__PURE__ */ new Map();
|
|
38
|
+
constructor(request) {
|
|
39
|
+
this.request = request;
|
|
40
|
+
}
|
|
41
|
+
get(name) {
|
|
42
|
+
if (this.headers.has(name)) {
|
|
43
|
+
return this.headers.get(name);
|
|
44
|
+
}
|
|
45
|
+
return this.request.headers.get(name) ?? void 0;
|
|
46
|
+
}
|
|
47
|
+
set(name, value) {
|
|
48
|
+
this.headers.set(name, value);
|
|
49
|
+
}
|
|
50
|
+
delete(name) {
|
|
51
|
+
this.headers.delete(name);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// src/server/ServerEvent.ts
|
|
56
|
+
var ServerEvent = class {
|
|
57
|
+
request;
|
|
58
|
+
response;
|
|
59
|
+
// TODO: Should we cast types??
|
|
60
|
+
params;
|
|
61
|
+
cookies;
|
|
62
|
+
headers;
|
|
63
|
+
constructor(request, params) {
|
|
64
|
+
this.request = request;
|
|
65
|
+
this.params = params;
|
|
66
|
+
this.cookies = new CookieHelper(request);
|
|
67
|
+
this.headers = new HeaderHelper(request);
|
|
68
|
+
}
|
|
69
|
+
addHeaders() {
|
|
70
|
+
if (!this.response) {
|
|
71
|
+
throw new Error("Response not created yet");
|
|
72
|
+
}
|
|
73
|
+
for (let cookie of this.cookies.cookies.values()) {
|
|
74
|
+
this.response.headers.append("set-cookie", cookie);
|
|
75
|
+
}
|
|
76
|
+
for (let header of this.headers.headers.entries()) {
|
|
77
|
+
this.response.headers.append(header[0], header[1]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/server/Server.ts
|
|
83
|
+
var noop = () => {
|
|
84
|
+
};
|
|
85
|
+
var Server = class {
|
|
86
|
+
// TODO: We don't use any of the routes, just add("*") in runDev/runBuild...
|
|
87
|
+
routes = [];
|
|
88
|
+
// TODO: We only use the Vite middleware here
|
|
89
|
+
middleware = [];
|
|
90
|
+
constructor() {
|
|
91
|
+
this.routes = [];
|
|
92
|
+
this.middleware = [];
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The standard Fetch API method that should work in any runtime (including
|
|
96
|
+
* Node with polyfills).
|
|
97
|
+
* @returns
|
|
98
|
+
*/
|
|
99
|
+
fetch = async (input, init) => {
|
|
100
|
+
const request = new Request(input, init);
|
|
101
|
+
const url = new URL(request.url);
|
|
102
|
+
const match = this.match(url.pathname);
|
|
103
|
+
let ev = new ServerEvent(request, match?.params);
|
|
104
|
+
if (this.middleware.length) {
|
|
105
|
+
let buildNext2 = function(middleware, i) {
|
|
106
|
+
if (ev.response) {
|
|
107
|
+
return noop;
|
|
108
|
+
}
|
|
109
|
+
if (i < middleware.length - 1) {
|
|
110
|
+
let next = middleware[i + 1];
|
|
111
|
+
return async () => next(ev, buildNext2(middleware, i + 1));
|
|
112
|
+
} else {
|
|
113
|
+
return match ? async () => {
|
|
114
|
+
ev.response = await match.fn(ev);
|
|
115
|
+
} : noop;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var buildNext = buildNext2;
|
|
119
|
+
await this.middleware[0](ev, buildNext2(this.middleware, 0));
|
|
120
|
+
} else if (match) {
|
|
121
|
+
ev.response = await match.fn(ev);
|
|
122
|
+
} else {
|
|
123
|
+
return new Response(null, { status: 404 });
|
|
124
|
+
}
|
|
125
|
+
ev.response ??= new Response(null, { status: 200 });
|
|
126
|
+
ev.addHeaders();
|
|
127
|
+
return ev.response;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Adds a server function at a route path.
|
|
131
|
+
* @param path The route path pattern.
|
|
132
|
+
* @param fn The function to call when the pattern is matched.
|
|
133
|
+
* @returns
|
|
134
|
+
*/
|
|
135
|
+
add(path, fn) {
|
|
136
|
+
this.routes.push(new RouteHandler(path, fn));
|
|
137
|
+
this.#sortRoutes();
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
match(path) {
|
|
141
|
+
for (let handler of this.routes) {
|
|
142
|
+
let match = path.match(handler.regex);
|
|
143
|
+
if (match) {
|
|
144
|
+
return {
|
|
145
|
+
fn: handler.fn,
|
|
146
|
+
params: match.groups
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
console.log(`Server path not found: ${path}`);
|
|
151
|
+
}
|
|
152
|
+
// TODO:
|
|
153
|
+
//use(path: string, ...fn: MiddlewareFunction[])
|
|
154
|
+
use(...fn) {
|
|
155
|
+
this.middleware = this.middleware.concat(fn);
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
#sortRoutes() {
|
|
159
|
+
this.routes = this.routes.sort((a, b) => {
|
|
160
|
+
for (let i = 0; i < Math.min(a.path.length, b.path.length); i++) {
|
|
161
|
+
if (a.path[i] === "[" && b.path[i] !== "[") {
|
|
162
|
+
return 1;
|
|
163
|
+
} else if (b.path[i] === "[" && a.path[i] !== "[") {
|
|
164
|
+
return -1;
|
|
165
|
+
} else if (a.path[i] === b.path[i]) {
|
|
166
|
+
} else {
|
|
167
|
+
return a.path[i].localeCompare(b.path[i]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return a.path.length - b.path.length;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
var RouteHandler = class {
|
|
175
|
+
path;
|
|
176
|
+
regex;
|
|
177
|
+
fn;
|
|
178
|
+
constructor(path, fn) {
|
|
179
|
+
this.path = path;
|
|
180
|
+
this.regex = pathToRegex(path);
|
|
181
|
+
this.fn = fn;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/server/connect/flattenHeaders.ts
|
|
186
|
+
function flattenHeaders(headers) {
|
|
187
|
+
const flatHeaders = new Headers();
|
|
188
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
189
|
+
if (value === void 0 || value === null) {
|
|
190
|
+
continue;
|
|
191
|
+
} else if (Array.isArray(value)) {
|
|
192
|
+
for (const v of value) {
|
|
193
|
+
if (v !== void 0 && v !== null) {
|
|
194
|
+
value.forEach((v2) => flatHeaders.append(key, v2));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
flatHeaders.set(key, String(value));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return flatHeaders;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// src/server/connect/nodeMessageToNodeResponse.ts
|
|
205
|
+
import {
|
|
206
|
+
ServerResponse
|
|
207
|
+
} from "node:http";
|
|
208
|
+
import { PassThrough, Readable } from "node:stream";
|
|
209
|
+
function nodeMessageToNodeResponse(incomingMessage) {
|
|
210
|
+
const res = new ServerResponse(incomingMessage);
|
|
211
|
+
const passThrough = new PassThrough();
|
|
212
|
+
let handled = false;
|
|
213
|
+
const onReadable = (cb) => {
|
|
214
|
+
const handleReadable = () => {
|
|
215
|
+
if (handled) return;
|
|
216
|
+
handled = true;
|
|
217
|
+
cb({
|
|
218
|
+
readable: Readable.from(passThrough),
|
|
219
|
+
headers: res.getHeaders(),
|
|
220
|
+
status: res.statusCode
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
passThrough.once("readable", handleReadable);
|
|
224
|
+
passThrough.once("end", handleReadable);
|
|
225
|
+
};
|
|
226
|
+
passThrough.once("finish", () => {
|
|
227
|
+
res.emit("finish");
|
|
228
|
+
});
|
|
229
|
+
passThrough.once("close", () => {
|
|
230
|
+
res.destroy();
|
|
231
|
+
res.emit("close");
|
|
232
|
+
});
|
|
233
|
+
passThrough.on("drain", () => {
|
|
234
|
+
res.emit("drain");
|
|
235
|
+
});
|
|
236
|
+
res.write = passThrough.write.bind(passThrough);
|
|
237
|
+
res.end = passThrough.end.bind(passThrough);
|
|
238
|
+
res.writeHead = function writeHead(statusCode, statusMessage, headers) {
|
|
239
|
+
res.statusCode = statusCode;
|
|
240
|
+
if (typeof statusMessage === "object") {
|
|
241
|
+
headers = statusMessage;
|
|
242
|
+
statusMessage = void 0;
|
|
243
|
+
}
|
|
244
|
+
if (headers) {
|
|
245
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
246
|
+
if (value !== void 0) {
|
|
247
|
+
res.setHeader(key, value);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return res;
|
|
252
|
+
};
|
|
253
|
+
return {
|
|
254
|
+
res,
|
|
255
|
+
onReadable
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/server/connect/readableToBuffer.ts
|
|
260
|
+
function readableToBuffer(req) {
|
|
261
|
+
return new Promise((resolve, reject) => {
|
|
262
|
+
const chunks = [];
|
|
263
|
+
req.on("data", (chunk) => {
|
|
264
|
+
chunks.push(chunk);
|
|
265
|
+
});
|
|
266
|
+
req.on("end", () => {
|
|
267
|
+
resolve(Buffer.concat(chunks));
|
|
268
|
+
});
|
|
269
|
+
req.on("error", (err) => {
|
|
270
|
+
reject(err);
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// src/server/connect/requestToNodeMessage.ts
|
|
276
|
+
import { Readable as Readable2 } from "node:stream";
|
|
277
|
+
var DUMMY_BASE_URL = "http://localhost";
|
|
278
|
+
function requestToNodeMessage(request) {
|
|
279
|
+
const parsedUrl = new URL(request.url, DUMMY_BASE_URL);
|
|
280
|
+
const pathAndQuery = (parsedUrl.pathname || "") + (parsedUrl.search || "");
|
|
281
|
+
const body = (
|
|
282
|
+
/*request.body ? Readable.fromWeb(request.body as any) :*/
|
|
283
|
+
Readable2.from([])
|
|
284
|
+
);
|
|
285
|
+
return Object.assign(body, {
|
|
286
|
+
url: pathAndQuery,
|
|
287
|
+
method: request.method,
|
|
288
|
+
headers: Object.fromEntries(request.headers)
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// src/server/connect/connectMiddleware.ts
|
|
293
|
+
function connectMiddleware(handler) {
|
|
294
|
+
return async (ev, next) => {
|
|
295
|
+
const req = requestToNodeMessage(ev.request);
|
|
296
|
+
const { res, onReadable } = nodeMessageToNodeResponse(req);
|
|
297
|
+
return new Promise(async (resolve, reject) => {
|
|
298
|
+
onReadable(async ({ readable, headers, status }) => {
|
|
299
|
+
const responseBody = statusCodesWithoutBody.includes(status) ? null : (
|
|
300
|
+
//(Readable.toWeb(readable) as ReadableStream);
|
|
301
|
+
// HACK: Chunked bodies are not being
|
|
302
|
+
// read correctly somewhere...
|
|
303
|
+
await readableToBuffer(readable)
|
|
304
|
+
);
|
|
305
|
+
ev.response = new Response(responseBody, {
|
|
306
|
+
status,
|
|
307
|
+
headers: flattenHeaders(headers)
|
|
308
|
+
});
|
|
309
|
+
resolve();
|
|
310
|
+
});
|
|
311
|
+
const cnext = async (error) => {
|
|
312
|
+
if (error) {
|
|
313
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
314
|
+
} else {
|
|
315
|
+
await next();
|
|
316
|
+
resolve();
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
try {
|
|
320
|
+
const handled = await handler(req, res, cnext);
|
|
321
|
+
if (handled === false) {
|
|
322
|
+
res.destroy();
|
|
323
|
+
resolve();
|
|
324
|
+
}
|
|
325
|
+
} catch (e) {
|
|
326
|
+
cnext(e);
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
var statusCodesWithoutBody = [
|
|
332
|
+
100,
|
|
333
|
+
// Continue
|
|
334
|
+
101,
|
|
335
|
+
// Switching Protocols
|
|
336
|
+
102,
|
|
337
|
+
// Processing (WebDAV)
|
|
338
|
+
103,
|
|
339
|
+
// Early Hints
|
|
340
|
+
204,
|
|
341
|
+
// No Content
|
|
342
|
+
205,
|
|
343
|
+
// Reset Content
|
|
344
|
+
304
|
|
345
|
+
// Not Modified
|
|
346
|
+
];
|
|
347
|
+
|
|
348
|
+
export {
|
|
349
|
+
CookieHelper,
|
|
350
|
+
HeaderHelper,
|
|
351
|
+
ServerEvent,
|
|
352
|
+
Server,
|
|
353
|
+
connectMiddleware
|
|
354
|
+
};
|
|
355
|
+
//# sourceMappingURL=chunk-65Z6BF2N.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server/CookieHelper.ts","../src/server/HeaderHelper.ts","../src/server/ServerEvent.ts","../src/server/Server.ts","../src/server/connect/flattenHeaders.ts","../src/server/connect/nodeMessageToNodeResponse.ts","../src/server/connect/readableToBuffer.ts","../src/server/connect/requestToNodeMessage.ts","../src/server/connect/connectMiddleware.ts"],"sourcesContent":["import { SerializeOptions, parse, serialize } from \"cookie\";\n\nexport default class CookieHelper {\n\trequest: Request;\n\tcookies: Map<string, string> = new Map();\n\n\tconstructor(request: Request) {\n\t\tthis.request = request;\n\t}\n\n\tget(name: string): string | undefined {\n\t\t// If it's been set, return the updated value\n\t\tif (this.cookies.has(name)) {\n\t\t\treturn this.cookies.get(name);\n\t\t}\n\t\t// Otherwise, return the cookie value from the `Cookie` header\n\t\tconst header = this.request.headers.get(\"cookie\");\n\t\treturn parse(header ?? \"\")[name];\n\t}\n\n\tset(name: string, value: string, options?: SerializeOptions) {\n\t\t// Set a cookie header with some default values for security\n\t\toptions ??= {};\n\t\toptions.httpOnly ??= true;\n\t\toptions.secure ??= true;\n\t\toptions.sameSite ??= \"lax\";\n\t\tthis.cookies.set(name, serialize(name, value, options));\n\t}\n\n\tdelete(name: string, options?: SerializeOptions) {\n\t\t// Set a cookie header with an immediate expiry date\n\t\toptions ??= {};\n\t\toptions.expires = new Date(0);\n\t\tthis.cookies.set(name, serialize(name, \"\", options));\n\t}\n}\n","export default class HeaderHelper {\n\trequest: Request;\n\theaders: Map<string, string> = new Map();\n\n\tconstructor(request: Request) {\n\t\tthis.request = request;\n\t}\n\n\tget(name: string): string | undefined {\n\t\t// If it's been set, return the updated value\n\t\tif (this.headers.has(name)) {\n\t\t\treturn this.headers.get(name);\n\t\t}\n\t\t// Otherwise, return the value from the header\n\t\treturn this.request.headers.get(name) ?? undefined;\n\t}\n\n\tset(name: string, value: string) {\n\t\tthis.headers.set(name, value);\n\t}\n\n\tdelete(name: string) {\n\t\tthis.headers.delete(name);\n\t}\n}\n","import CookieHelper from \"./CookieHelper\";\nimport HeaderHelper from \"./HeaderHelper\";\n\nexport default class ServerEvent {\n\trequest: Request;\n\tresponse?: Response;\n\t// TODO: Should we cast types??\n\tparams?: { [key: string]: string };\n\n\tcookies: CookieHelper;\n\theaders: HeaderHelper;\n\n\tconstructor(request: Request, params?: { [key: string]: string }) {\n\t\tthis.request = request;\n\t\tthis.params = params;\n\t\tthis.cookies = new CookieHelper(request);\n\t\tthis.headers = new HeaderHelper(request);\n\t}\n\n\taddHeaders() {\n\t\tif (!this.response) {\n\t\t\tthrow new Error(\"Response not created yet\");\n\t\t}\n\n\t\tfor (let cookie of this.cookies.cookies.values()) {\n\t\t\tthis.response.headers.append(\"set-cookie\", cookie);\n\t\t}\n\n\t\tfor (let header of this.headers.headers.entries()) {\n\t\t\tthis.response.headers.append(header[0], header[1]);\n\t\t}\n\t}\n}\n","import pathToRegex from \"../site/pathToRegex\";\nimport ServerEvent from \"./ServerEvent\";\nimport type MiddlewareFunction from \"./types/MiddlewareFunction\";\nimport type ServerFunction from \"./types/ServerFunction\";\n\nconst noop = () => {};\n\n/**\n * A Fetch API Request/Response server.\n */\nexport default class Server {\n\t// TODO: We don't use any of the routes, just add(\"*\") in runDev/runBuild...\n\troutes: RouteHandler[] = [];\n\t// TODO: We only use the Vite middleware here\n\tmiddleware: MiddlewareFunction[] = [];\n\n\tconstructor() {\n\t\tthis.routes = [];\n\t\tthis.middleware = [];\n\t}\n\n\t/**\n\t * The standard Fetch API method that should work in any runtime (including\n\t * Node with polyfills).\n\t * @returns\n\t */\n\tfetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {\n\t\tconst request = new Request(input, init);\n\t\tconst url = new URL(request.url);\n\t\tconst match = this.match(url.pathname);\n\t\tlet ev = new ServerEvent(request, match?.params);\n\n\t\tif (this.middleware.length) {\n\t\t\t// TODO: Get middleware that applies to this route only?\n\t\t\tawait this.middleware[0](ev, buildNext(this.middleware, 0));\n\t\t\tfunction buildNext(middleware: MiddlewareFunction[], i: number): () => void | Promise<void> {\n\t\t\t\tif (ev.response) {\n\t\t\t\t\treturn noop;\n\t\t\t\t}\n\n\t\t\t\tif (i < middleware.length - 1) {\n\t\t\t\t\tlet next = middleware[i + 1];\n\t\t\t\t\treturn async () => next(ev, buildNext(middleware, i + 1));\n\t\t\t\t} else {\n\t\t\t\t\treturn match\n\t\t\t\t\t\t? async () => {\n\t\t\t\t\t\t\t\tev.response = await match.fn(ev);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: noop;\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (match) {\n\t\t\tev.response = await match.fn(ev);\n\t\t} else {\n\t\t\treturn new Response(null, { status: 404 });\n\t\t}\n\n\t\t// TODO: Should I be returning 200 if there's no response? Because it's not an error??\n\t\tev.response ??= new Response(null, { status: 200 });\n\t\tev.addHeaders();\n\n\t\treturn ev.response;\n\t};\n\n\t/**\n\t * Adds a server function at a route path.\n\t * @param path The route path pattern.\n\t * @param fn The function to call when the pattern is matched.\n\t * @returns\n\t */\n\tadd(path: string, fn: ServerFunction): Server {\n\t\tthis.routes.push(new RouteHandler(path, fn));\n\t\tthis.#sortRoutes();\n\t\treturn this;\n\t}\n\n\tmatch(path: string) {\n\t\tfor (let handler of this.routes) {\n\t\t\tlet match = path.match(handler.regex);\n\t\t\tif (match) {\n\t\t\t\treturn {\n\t\t\t\t\tfn: handler.fn,\n\t\t\t\t\tparams: match.groups,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\tconsole.log(`Server path not found: ${path}`);\n\t}\n\n\t// TODO:\n\t//use(path: string, ...fn: MiddlewareFunction[])\n\tuse(...fn: MiddlewareFunction[]): Server {\n\t\tthis.middleware = this.middleware.concat(fn);\n\t\treturn this;\n\t}\n\n\t#sortRoutes() {\n\t\tthis.routes = this.routes.sort((a, b) => {\n\t\t\t// Sort [param]s after paths\n\t\t\t// There might be a quicker/easier way to do this\n\t\t\tfor (let i = 0; i < Math.min(a.path.length, b.path.length); i++) {\n\t\t\t\tif (a.path[i] === \"[\" && b.path[i] !== \"[\") {\n\t\t\t\t\treturn 1;\n\t\t\t\t} else if (b.path[i] === \"[\" && a.path[i] !== \"[\") {\n\t\t\t\t\treturn -1;\n\t\t\t\t} else if (a.path[i] === b.path[i]) {\n\t\t\t\t\t// Keep going...\n\t\t\t\t} else {\n\t\t\t\t\treturn a.path[i].localeCompare(b.path[i]);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn a.path.length - b.path.length;\n\t\t});\n\t}\n}\n\nclass RouteHandler {\n\tpath: string;\n\tregex: RegExp;\n\tfn: ServerFunction;\n\n\tconstructor(path: string, fn: ServerFunction) {\n\t\tthis.path = path;\n\t\tthis.regex = pathToRegex(path);\n\t\tthis.fn = fn;\n\t}\n}\n\n//class MiddlewareHandler {\n//\tpath: string;\n//\tregex: RegExp;\n//\tfn: MiddlewareFunction;\n//\n//\tconstructor(path: string, fn: MiddlewareFunction) {\n//\t\tthis.path = path;\n//\t\tthis.regex = pathToRegex(path);\n//\t\tthis.fn = fn;\n//\t}\n//}\n","import { IncomingHttpHeaders } from \"node:http\";\nimport { OutgoingHttpHeaders } from \"node:http2\";\n\nexport default function flattenHeaders(\n\theaders: IncomingHttpHeaders | OutgoingHttpHeaders,\n): HeadersInit {\n\tconst flatHeaders = new Headers();\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tif (value === undefined || value === null) {\n\t\t\tcontinue;\n\t\t} else if (Array.isArray(value)) {\n\t\t\tfor (const v of value) {\n\t\t\t\tif (v !== undefined && v !== null) {\n\t\t\t\t\tvalue.forEach((v) => flatHeaders.append(key, v));\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tflatHeaders.set(key, String(value));\n\t\t}\n\t}\n\treturn flatHeaders;\n\n\t/*\n const flatHeaders: [string, string][] = [];\n\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tif (value === undefined || value === null) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (Array.isArray(value)) {\n\t\t\tfor (const v of value) {\n\t\t\t\tif (v != null) {\n\t\t\t\t\tflatHeaders.push([key, String(v)]);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tflatHeaders.push([key, String(value)]);\n\t\t}\n\t}\n\n\treturn flatHeaders;\n */\n}\n","import {\n\ttype IncomingMessage,\n\ttype OutgoingHttpHeader,\n\ttype OutgoingHttpHeaders,\n\tServerResponse,\n} from \"node:http\";\nimport { PassThrough, Readable } from \"node:stream\";\n\n// From https://github.com/vikejs/vike-node/blob/main/packages/vike-node/src/runtime/adapters/createServerResponse.ts\n\n/**\n * Creates a custom ServerResponse object that allows for intercepting and\n * streaming the response.\n *\n * @param incomingMessage The incoming HTTP request message.\n * @returns An object containing the custom ServerResponse and a function that\n * takes a callback to invoke when the response is readable, providing an object\n * with the readable stream, headers and status code.\n */\nexport default function nodeMessageToNodeResponse(\n\tincomingMessage: IncomingMessage,\n): ServerResponseResponse {\n\tconst res = new ServerResponse(incomingMessage);\n\tconst passThrough = new PassThrough();\n\tlet handled = false;\n\n\tconst onReadable = (\n\t\tcb: (result: { readable: Readable; headers: OutgoingHttpHeaders; status: number }) => void,\n\t) => {\n\t\tconst handleReadable = () => {\n\t\t\tif (handled) return;\n\t\t\thandled = true;\n\t\t\tcb({\n\t\t\t\treadable: Readable.from(passThrough),\n\t\t\t\theaders: res.getHeaders(),\n\t\t\t\tstatus: res.statusCode,\n\t\t\t});\n\t\t};\n\n\t\tpassThrough.once(\"readable\", handleReadable);\n\t\tpassThrough.once(\"end\", handleReadable);\n\t};\n\n\tpassThrough.once(\"finish\", () => {\n\t\tres.emit(\"finish\");\n\t});\n\tpassThrough.once(\"close\", () => {\n\t\tres.destroy();\n\t\tres.emit(\"close\");\n\t});\n\tpassThrough.on(\"drain\", () => {\n\t\tres.emit(\"drain\");\n\t});\n\n\tres.write = passThrough.write.bind(passThrough);\n\tres.end = passThrough.end.bind(passThrough) as any;\n\n\tres.writeHead = function writeHead(\n\t\tstatusCode: number,\n\t\tstatusMessage?: string | OutgoingHttpHeaders | OutgoingHttpHeader[],\n\t\theaders?: OutgoingHttpHeaders | OutgoingHttpHeader[],\n\t): ServerResponse {\n\t\tres.statusCode = statusCode;\n\t\tif (typeof statusMessage === \"object\") {\n\t\t\theaders = statusMessage;\n\t\t\tstatusMessage = undefined;\n\t\t}\n\t\tif (headers) {\n\t\t\tfor (const [key, value] of Object.entries(headers)) {\n\t\t\t\tif (value !== undefined) {\n\t\t\t\t\tres.setHeader(key, value);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn res;\n\t};\n\n\treturn {\n\t\tres,\n\t\tonReadable,\n\t};\n}\n\ntype ServerResponseResponse = {\n\tres: ServerResponse;\n\tonReadable: (\n\t\tcb: (result: { readable: Readable; headers: OutgoingHttpHeaders; status: number }) => void,\n\t) => void;\n};\n","import { Readable } from \"node:stream\";\n\nexport default function readableToBuffer(req: Readable): Promise<Buffer> {\n\treturn new Promise<Buffer>((resolve, reject) => {\n\t\tconst chunks: Buffer[] = [];\n\t\treq.on(\"data\", (chunk: Buffer) => {\n\t\t\tchunks.push(chunk);\n\t\t});\n\t\treq.on(\"end\", () => {\n\t\t\tresolve(Buffer.concat(chunks));\n\t\t});\n\t\treq.on(\"error\", (err) => {\n\t\t\treject(err);\n\t\t});\n\t});\n}\n","import { type IncomingMessage } from \"node:http\";\nimport { Readable } from \"node:stream\";\n\n// From https://github.com/vikejs/vike-node/blob/main/packages/vike-node/src/runtime/adapters/connectToWeb.ts\n\nconst DUMMY_BASE_URL = \"http://localhost\";\n\n/**\n * Creates an IncomingMessage object from a web Request.\n *\n * @param {Request} request - The web Request object.\n * @returns {IncomingMessage} An IncomingMessage-like object compatible with Node.js HTTP module.\n */\nexport default function requestToNodeMessage(request: Request): IncomingMessage {\n\tconst parsedUrl = new URL(request.url, DUMMY_BASE_URL);\n\tconst pathAndQuery = (parsedUrl.pathname || \"\") + (parsedUrl.search || \"\");\n\t// HACK: Using Readable.fromWeb locks request.body so that the request cannot be used again\n\t// As we're only using this for Vite middlewares, it's safe to discard the body for now?\n\tconst body = /*request.body ? Readable.fromWeb(request.body as any) :*/ Readable.from([]);\n\n\treturn Object.assign(body, {\n\t\turl: pathAndQuery,\n\t\tmethod: request.method,\n\t\theaders: Object.fromEntries(request.headers),\n\t}) as IncomingMessage;\n}\n","import { IncomingMessage, ServerResponse } from \"node:http\";\nimport type MiddlewareFunction from \"../types/MiddlewareFunction\";\nimport flattenHeaders from \"./flattenHeaders\";\nimport nodeMessageToNodeResponse from \"./nodeMessageToNodeResponse\";\nimport readableToBuffer from \"./readableToBuffer\";\nimport requestToNodeMessage from \"./requestToNodeMessage\";\n\n// From https://github.com/vikejs/vike-node/blob/main/packages/vike-node/src/runtime/adapters/connectToWeb.ts\n\n/**\n * Converts a Connect-style middleware to a web-compatible request handler.\n *\n * @param handler - The Connect-style middleware function to be converted.\n * @returns A function that handles web requests and returns a Response or undefined.\n */\nexport default function connectMiddleware(\n\thandler: ConnectMiddleware | ConnectMiddlewareBoolean,\n): MiddlewareFunction {\n\treturn async (ev, next) => {\n\t\tconst req = requestToNodeMessage(ev.request);\n\t\tconst { res, onReadable } = nodeMessageToNodeResponse(req);\n\n\t\treturn new Promise<void>(async (resolve, reject) => {\n\t\t\tonReadable(async ({ readable, headers, status }) => {\n\t\t\t\tconst responseBody = statusCodesWithoutBody.includes(status)\n\t\t\t\t\t? null\n\t\t\t\t\t: //(Readable.toWeb(readable) as ReadableStream);\n\t\t\t\t\t\t// HACK: Chunked bodies are not being\n\t\t\t\t\t\t// read correctly somewhere...\n\t\t\t\t\t\tawait readableToBuffer(readable);\n\t\t\t\tev.response = new Response(responseBody, {\n\t\t\t\t\tstatus,\n\t\t\t\t\theaders: flattenHeaders(headers),\n\t\t\t\t});\n\t\t\t\tresolve();\n\t\t\t});\n\n\t\t\tconst cnext = async (error?: unknown) => {\n\t\t\t\tif (error) {\n\t\t\t\t\treject(error instanceof Error ? error : new Error(String(error)));\n\t\t\t\t} else {\n\t\t\t\t\t// TODO: Can we nest the next functions less? This is a bit weird\n\t\t\t\t\tawait next();\n\t\t\t\t\tresolve();\n\t\t\t\t}\n\t\t\t};\n\n\t\t\ttry {\n\t\t\t\tconst handled = await handler(req, res, cnext);\n\t\t\t\tif (handled === false) {\n\t\t\t\t\tres.destroy();\n\t\t\t\t\tresolve();\n\t\t\t\t}\n\t\t\t} catch (e) {\n\t\t\t\tcnext(e);\n\t\t\t}\n\t\t});\n\t};\n}\n\nconst statusCodesWithoutBody = [\n\t100, // Continue\n\t101, // Switching Protocols\n\t102, // Processing (WebDAV)\n\t103, // Early Hints\n\t204, // No Content\n\t205, // Reset Content\n\t304, // Not Modified\n];\n\ntype NextFunction = (err?: unknown) => void;\ntype ConnectMiddleware<\n\tPlatformRequest extends IncomingMessage = IncomingMessage,\n\tPlatformResponse extends ServerResponse = ServerResponse,\n> = (req: PlatformRequest, res: PlatformResponse, next: NextFunction) => void | Promise<void>;\ntype ConnectMiddlewareBoolean<\n\tPlatformRequest extends IncomingMessage = IncomingMessage,\n\tPlatformResponse extends ServerResponse = ServerResponse,\n> = (req: PlatformRequest, res: PlatformResponse, next: NextFunction) => boolean | Promise<boolean>;\n"],"mappings":";;;;;AAAA,SAA2B,OAAO,iBAAiB;AAEnD,IAAqB,eAArB,MAAkC;AAAA,EACjC;AAAA,EACA,UAA+B,oBAAI,IAAI;AAAA,EAEvC,YAAY,SAAkB;AAC7B,SAAK,UAAU;AAAA,EAChB;AAAA,EAEA,IAAI,MAAkC;AAErC,QAAI,KAAK,QAAQ,IAAI,IAAI,GAAG;AAC3B,aAAO,KAAK,QAAQ,IAAI,IAAI;AAAA,IAC7B;AAEA,UAAM,SAAS,KAAK,QAAQ,QAAQ,IAAI,QAAQ;AAChD,WAAO,MAAM,UAAU,EAAE,EAAE,IAAI;AAAA,EAChC;AAAA,EAEA,IAAI,MAAc,OAAe,SAA4B;AAE5D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AO,YAAY,QAAQ,OAAO;AAAA,EAC5C,CAAC;AACF;;;ACVe,SAAR,kBACN,SACqB;AACrB,SAAO,OAAO,IAAI,SAAS;AAC1B,UAAM,MAAM,qBAAqB,GAAG,OAAO;AAC3C,UAAM,EAAE,KAAK,WAAW,IAAI,0BAA0B,GAAG;AAEzD,WAAO,IAAI,QAAc,OAAO,SAAS,WAAW;AACnD,iBAAW,OAAO,EAAE,UAAU,SAAS,OAAO,MAAM;AACnD,cAAM,eAAe,uBAAuB,SAAS,MAAM,IACxD;AAAA;AAAA;AAAA;AAAA,UAID,MAAM,iBAAiB,QAAQ;AAAA;AACjC,WAAG,WAAW,IAAI,SAAS,cAAc;AAAA,UACxC;AAAA,UACA,SAAS,eAAe,OAAO;AAAA,QAChC,CAAC;AACD,gBAAQ;AAAA,MACT,CAAC;AAED,YAAM,QAAQ,OAAO,UAAoB;AACxC,YAAI,OAAO;AACV,iBAAO,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC,CAAC;AAAA,QACjE,OAAO;AAEN,gBAAM,KAAK;AACX,kBAAQ;AAAA,QACT;AAAA,MACD;AAEA,UAAI;AACH,cAAM,UAAU,MAAM,QAAQ,KAAK,KAAK,KAAK;AAC7C,YAAI,YAAY,OAAO;AACtB,cAAI,QAAQ;AACZ,kBAAQ;AAAA,QACT;AAAA,MACD,SAAS,GAAG;AACX,cAAM,CAAC;AAAA,MACR;AAAA,IACD,CAAC;AAAA,EACF;AACD;AAEA,IAAM,yBAAyB;AAAA,EAC9B;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AACD;","names":["buildNext","v","Readable"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/site/pathToRegex.ts
|
|
2
|
+
function pathToRegex(path) {
|
|
3
|
+
const pattern = path.split("/").map((p) => {
|
|
4
|
+
return p.replace(/\[([^\/]+?)\]/, "(?<$1>[^\\/]+?)");
|
|
5
|
+
}).join("\\/").replaceAll("*", ".*");
|
|
6
|
+
return new RegExp(`^${pattern}$`);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export {
|
|
10
|
+
pathToRegex
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=chunk-T5K52YVJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/site/pathToRegex.ts"],"sourcesContent":["export default function pathToRegex(path: string): RegExp {\n\tconst pattern = path\n\t\t.split(\"/\")\n\t\t.map((p) => {\n\t\t\treturn p.replace(/\\[([^\\/]+?)\\]/, \"(?<$1>[^\\\\/]+?)\");\n\t\t})\n\t\t.join(\"\\\\/\")\n\t\t.replaceAll(\"*\", \".*\");\n\treturn new RegExp(`^${pattern}$`);\n}\n"],"mappings":";AAAe,SAAR,YAA6B,MAAsB;AACzD,QAAM,UAAU,KACd,MAAM,GAAG,EACT,IAAI,CAAC,MAAM;AACX,WAAO,EAAE,QAAQ,iBAAiB,iBAAiB;AAAA,EACpD,CAAC,EACA,KAAK,KAAK,EACV,WAAW,KAAK,IAAI;AACtB,SAAO,IAAI,OAAO,IAAI,OAAO,GAAG;AACjC;","names":[]}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Server
|
|
3
|
-
} from "./chunk-L3QBUPXY.js";
|
|
4
1
|
import {
|
|
5
2
|
serverError
|
|
6
3
|
} from "./chunk-VAGHGWM5.js";
|
|
4
|
+
import {
|
|
5
|
+
Server,
|
|
6
|
+
connectMiddleware
|
|
7
|
+
} from "./chunk-65Z6BF2N.js";
|
|
7
8
|
|
|
8
9
|
// src/site/runBuild.ts
|
|
9
10
|
import torpor from "@torpor/unplugin/vite";
|
|
@@ -36,10 +37,10 @@ export default {
|
|
|
36
37
|
routes: [
|
|
37
38
|
${site.routes.map((r) => {
|
|
38
39
|
if (serverRequest || !/server\.(ts|js)$/.test(r.file)) {
|
|
39
|
-
return `{ path: "${r.path}",
|
|
40
|
+
return `{ path: "${r.path}", type: ${r.type}, endPoint: ${serverRequest || !/server\.(ts|js)$/.test(r.file) ? `() => import(/* @vite-ignore */ "${path.join(site.root, r.file)}")` : "undefined"} }`;
|
|
40
41
|
} else {
|
|
41
42
|
const haveLoad = readFileSync(path.join(site.root, r.file)).includes("load:");
|
|
42
|
-
return `{ path: "${r.path}",
|
|
43
|
+
return `{ path: "${r.path}", type: ${r.type}, endPoint: ${haveLoad ? "() => load" : "undefined"} }`;
|
|
43
44
|
}
|
|
44
45
|
}).join(",\n ")}
|
|
45
46
|
],
|
|
@@ -118,169 +119,6 @@ import { promises as fs2 } from "node:fs";
|
|
|
118
119
|
import path3 from "node:path";
|
|
119
120
|
import { createServer as createViteServer } from "vite";
|
|
120
121
|
|
|
121
|
-
// src/server/connect/flattenHeaders.ts
|
|
122
|
-
function flattenHeaders(headers) {
|
|
123
|
-
const flatHeaders = new Headers();
|
|
124
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
125
|
-
if (value === void 0 || value === null) {
|
|
126
|
-
continue;
|
|
127
|
-
} else if (Array.isArray(value)) {
|
|
128
|
-
for (const v of value) {
|
|
129
|
-
if (v !== void 0 && v !== null) {
|
|
130
|
-
value.forEach((v2) => flatHeaders.append(key, v2));
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
} else {
|
|
134
|
-
flatHeaders.set(key, String(value));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return flatHeaders;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// src/server/connect/nodeMessageToNodeResponse.ts
|
|
141
|
-
import {
|
|
142
|
-
ServerResponse
|
|
143
|
-
} from "node:http";
|
|
144
|
-
import { PassThrough, Readable } from "node:stream";
|
|
145
|
-
function nodeMessageToNodeResponse(incomingMessage) {
|
|
146
|
-
const res = new ServerResponse(incomingMessage);
|
|
147
|
-
const passThrough = new PassThrough();
|
|
148
|
-
let handled = false;
|
|
149
|
-
const onReadable = (cb) => {
|
|
150
|
-
const handleReadable = () => {
|
|
151
|
-
if (handled) return;
|
|
152
|
-
handled = true;
|
|
153
|
-
cb({
|
|
154
|
-
readable: Readable.from(passThrough),
|
|
155
|
-
headers: res.getHeaders(),
|
|
156
|
-
status: res.statusCode
|
|
157
|
-
});
|
|
158
|
-
};
|
|
159
|
-
passThrough.once("readable", handleReadable);
|
|
160
|
-
passThrough.once("end", handleReadable);
|
|
161
|
-
};
|
|
162
|
-
passThrough.once("finish", () => {
|
|
163
|
-
res.emit("finish");
|
|
164
|
-
});
|
|
165
|
-
passThrough.once("close", () => {
|
|
166
|
-
res.destroy();
|
|
167
|
-
res.emit("close");
|
|
168
|
-
});
|
|
169
|
-
passThrough.on("drain", () => {
|
|
170
|
-
res.emit("drain");
|
|
171
|
-
});
|
|
172
|
-
res.write = passThrough.write.bind(passThrough);
|
|
173
|
-
res.end = passThrough.end.bind(passThrough);
|
|
174
|
-
res.writeHead = function writeHead(statusCode, statusMessage, headers) {
|
|
175
|
-
res.statusCode = statusCode;
|
|
176
|
-
if (typeof statusMessage === "object") {
|
|
177
|
-
headers = statusMessage;
|
|
178
|
-
statusMessage = void 0;
|
|
179
|
-
}
|
|
180
|
-
if (headers) {
|
|
181
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
182
|
-
if (value !== void 0) {
|
|
183
|
-
res.setHeader(key, value);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return res;
|
|
188
|
-
};
|
|
189
|
-
return {
|
|
190
|
-
res,
|
|
191
|
-
onReadable
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// src/server/connect/readableToBuffer.ts
|
|
196
|
-
function readableToBuffer(req) {
|
|
197
|
-
return new Promise((resolve, reject) => {
|
|
198
|
-
const chunks = [];
|
|
199
|
-
req.on("data", (chunk) => {
|
|
200
|
-
chunks.push(chunk);
|
|
201
|
-
});
|
|
202
|
-
req.on("end", () => {
|
|
203
|
-
resolve(Buffer.concat(chunks));
|
|
204
|
-
});
|
|
205
|
-
req.on("error", (err) => {
|
|
206
|
-
reject(err);
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// src/server/connect/requestToNodeMessage.ts
|
|
212
|
-
import { Readable as Readable2 } from "node:stream";
|
|
213
|
-
var DUMMY_BASE_URL = "http://localhost";
|
|
214
|
-
function requestToNodeMessage(request) {
|
|
215
|
-
const parsedUrl = new URL(request.url, DUMMY_BASE_URL);
|
|
216
|
-
const pathAndQuery = (parsedUrl.pathname || "") + (parsedUrl.search || "");
|
|
217
|
-
const body = (
|
|
218
|
-
/*request.body ? Readable.fromWeb(request.body as any) :*/
|
|
219
|
-
Readable2.from([])
|
|
220
|
-
);
|
|
221
|
-
return Object.assign(body, {
|
|
222
|
-
url: pathAndQuery,
|
|
223
|
-
method: request.method,
|
|
224
|
-
headers: Object.fromEntries(request.headers)
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// src/server/connect/createMiddlewareHandler.ts
|
|
229
|
-
function createMiddlewareHandler(handler) {
|
|
230
|
-
return async (ev, next) => {
|
|
231
|
-
const req = requestToNodeMessage(ev.request);
|
|
232
|
-
const { res, onReadable } = nodeMessageToNodeResponse(req);
|
|
233
|
-
return new Promise(async (resolve, reject) => {
|
|
234
|
-
onReadable(async ({ readable, headers, status }) => {
|
|
235
|
-
const responseBody = statusCodesWithoutBody.includes(status) ? null : (
|
|
236
|
-
//(Readable.toWeb(readable) as ReadableStream);
|
|
237
|
-
// HACK: Chunked bodies are not being
|
|
238
|
-
// read correctly somewhere...
|
|
239
|
-
await readableToBuffer(readable)
|
|
240
|
-
);
|
|
241
|
-
ev.response = new Response(responseBody, {
|
|
242
|
-
status,
|
|
243
|
-
headers: flattenHeaders(headers)
|
|
244
|
-
});
|
|
245
|
-
resolve();
|
|
246
|
-
});
|
|
247
|
-
const cnext = async (error) => {
|
|
248
|
-
if (error) {
|
|
249
|
-
reject(error instanceof Error ? error : new Error(String(error)));
|
|
250
|
-
} else {
|
|
251
|
-
await next();
|
|
252
|
-
resolve();
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
try {
|
|
256
|
-
const handled = await handler(req, res, cnext);
|
|
257
|
-
if (handled === false) {
|
|
258
|
-
res.destroy();
|
|
259
|
-
resolve();
|
|
260
|
-
}
|
|
261
|
-
} catch (e) {
|
|
262
|
-
cnext(e);
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
var statusCodesWithoutBody = [
|
|
268
|
-
100,
|
|
269
|
-
// Continue
|
|
270
|
-
101,
|
|
271
|
-
// Switching Protocols
|
|
272
|
-
102,
|
|
273
|
-
// Processing (WebDAV)
|
|
274
|
-
103,
|
|
275
|
-
// Early Hints
|
|
276
|
-
204,
|
|
277
|
-
// No Content
|
|
278
|
-
205,
|
|
279
|
-
// Reset Content
|
|
280
|
-
304
|
|
281
|
-
// Not Modified
|
|
282
|
-
];
|
|
283
|
-
|
|
284
122
|
// src/site/prepareTemplate.ts
|
|
285
123
|
function prepareTemplate(template, clientScript) {
|
|
286
124
|
let contentStart = regexIndexOf(template, /\<div\s+id=("app"|'app'|app)\s+/);
|
|
@@ -316,7 +154,7 @@ async function runDev(site) {
|
|
|
316
154
|
let clientScript = path3.join(siteFolder, "clientEntry.ts");
|
|
317
155
|
let serverScript = path3.join(siteFolder, "serverEntry.ts");
|
|
318
156
|
template = prepareTemplate(template, clientScript);
|
|
319
|
-
server.use(
|
|
157
|
+
server.use(connectMiddleware(vite.middlewares));
|
|
320
158
|
server.add("*", async (ev) => {
|
|
321
159
|
try {
|
|
322
160
|
const { load } = await vite.ssrLoadModule(serverScript);
|
|
@@ -526,4 +364,4 @@ export {
|
|
|
526
364
|
runPreview,
|
|
527
365
|
run
|
|
528
366
|
};
|
|
529
|
-
//# sourceMappingURL=chunk-
|
|
367
|
+
//# sourceMappingURL=chunk-ZSWD4UBH.js.map
|