likec4 1.48.0 → 1.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -1
- package/__app__/src/likec4.js +5595 -4127
- package/__app__/src/routes/index.js +3 -153
- package/__app__/src/routes/projects.js +21 -1087
- package/__app__/src/routes/single.js +1221 -16
- package/__app__/src/style.css +1 -1
- package/__app__/src/vendors.js +3507 -894
- package/__app__/src/webcomponent.js +1 -1
- package/config/schema.json +155 -143
- package/dist/THIRD-PARTY-LICENSES.md +1739 -0
- package/dist/_chunks/GraphvizBinaryAdapter.mjs +72 -0
- package/dist/_chunks/filenames.mjs +14 -0
- package/dist/_chunks/index.d.mts +224 -129
- package/dist/_chunks/index2.d.mts +2033 -1908
- package/dist/_chunks/libs/@chevrotain/gast.mjs +1 -3969
- package/dist/_chunks/libs/@chevrotain/regexp-to-ast.mjs +9 -785
- package/dist/_chunks/libs/@chevrotain/utils.mjs +1 -37
- package/dist/_chunks/libs/@hono/mcp.mjs +34 -0
- package/dist/_chunks/libs/@hono/node-server.mjs +1 -436
- package/dist/_chunks/libs/@logtape/logtape.d.mts +741 -0
- package/dist/_chunks/libs/@logtape/logtape.mjs +6 -1354
- package/dist/_chunks/libs/@lume/kiwi.mjs +1 -1355
- package/dist/_chunks/libs/@modelcontextprotocol/sdk.d.mts +14 -14
- package/dist/_chunks/libs/@modelcontextprotocol/sdk.mjs +12 -25105
- package/dist/_chunks/libs/@msgpack/msgpack.mjs +1 -1503
- package/dist/_chunks/libs/@nanostores/react.mjs +1 -30
- package/dist/_chunks/libs/@smithy/util-base64.mjs +1 -100
- package/dist/_chunks/libs/ajv.mjs +1 -777
- package/dist/_chunks/libs/atomically.mjs +1 -362
- package/dist/_chunks/libs/birpc.mjs +1 -201
- package/dist/_chunks/libs/chevrotain-allstar.mjs +2 -850
- package/dist/_chunks/libs/chevrotain.mjs +55 -6229
- package/dist/_chunks/libs/conf.mjs +1 -2258
- package/dist/_chunks/libs/defu.mjs +1 -42
- package/dist/_chunks/libs/esm-env.mjs +1 -5
- package/dist/_chunks/libs/eventemitter3.mjs +1 -243
- package/dist/_chunks/libs/fast-equals.mjs +1 -446
- package/dist/_chunks/libs/find-up-simple.mjs +1 -24
- package/dist/_chunks/libs/get-port.mjs +1 -107
- package/dist/_chunks/libs/is-docker.mjs +1 -26
- package/dist/_chunks/libs/is-error-instance.mjs +1 -26
- package/dist/_chunks/libs/is-inside-container.mjs +1 -20
- package/dist/_chunks/libs/is-plain-obj.mjs +1 -9
- package/dist/_chunks/libs/isexe.mjs +1 -127
- package/dist/_chunks/libs/json5.mjs +14 -959
- package/dist/_chunks/libs/khroma.mjs +1 -605
- package/dist/_chunks/libs/ky.mjs +2 -807
- package/dist/_chunks/libs/langium.d.mts +2880 -2844
- package/dist/_chunks/libs/langium.mjs +32 -20351
- package/dist/_chunks/libs/merge-error-cause.mjs +2 -746
- package/dist/_chunks/libs/nanostores.mjs +1 -198
- package/dist/_chunks/libs/p-limit.mjs +1 -120
- package/dist/_chunks/libs/p-queue.mjs +1 -449
- package/dist/_chunks/libs/package-manager-detector.mjs +1 -559
- package/dist/_chunks/libs/package-up.mjs +1 -10
- package/dist/_chunks/libs/parse-ms.mjs +1 -36
- package/dist/_chunks/libs/pathe.mjs +1 -0
- package/dist/_chunks/libs/picomatch.mjs +1 -1673
- package/dist/_chunks/libs/pretty-ms.mjs +1 -80
- package/dist/_chunks/libs/remeda.mjs +1 -690
- package/dist/_chunks/libs/safe-stringify.mjs +1 -21
- package/dist/_chunks/libs/strip-indent.mjs +1 -15
- package/dist/_chunks/libs/tinyrainbow.mjs +1 -88
- package/dist/_chunks/libs/ts-graphviz.mjs +4 -725
- package/dist/_chunks/libs/ufo.mjs +1 -240
- package/dist/_chunks/libs/which.mjs +1 -84
- package/dist/_chunks/libs/word-wrap.mjs +12 -43
- package/dist/_chunks/node.mjs +481 -0
- package/dist/_chunks/plugin.mjs +98 -772
- package/dist/_chunks/rolldown-runtime.mjs +1 -48
- package/dist/_chunks/sequence-view.mjs +1 -575
- package/dist/cli/index.mjs +127 -1846
- package/dist/config/index.d.mts +2 -2
- package/dist/config/index.mjs +1 -6
- package/dist/index.d.mts +148 -7
- package/dist/index.mjs +1 -21
- package/dist/model/builder.mjs +1 -3
- package/dist/model/index.d.mts +57 -3
- package/dist/model/index.mjs +1 -5
- package/dist/vite-plugin/index.d.mts +4 -3
- package/dist/vite-plugin/index.mjs +1 -22
- package/dist/vite-plugin/internal.d.mts +5 -5
- package/dist/vite-plugin/internal.mjs +1 -68
- package/package.json +60 -41
- package/react/{index.d.ts → index.d.mts} +112 -70
- package/react/{index.js → index.mjs} +21361 -22064
- package/react/package.json +2 -5
- package/vite-plugin-modules.d.ts +5 -5
- package/dist/_chunks/LikeC4.d.mts +0 -121
- package/dist/_chunks/LikeC4.mjs +0 -202
- package/dist/_chunks/config-app.prod.d.mts +0 -18
- package/dist/_chunks/config-app.prod.mjs +0 -188
- package/dist/_chunks/config-webcomponent.prod.mjs +0 -71
- package/dist/_chunks/define-config.mjs +0 -409
- package/dist/_chunks/index3.d.mts +0 -60
- package/dist/_chunks/index4.d.mts +0 -1
- package/dist/_chunks/libs/@smithy/is-array-buffer.mjs +0 -10
- package/dist/_chunks/libs/hono.mjs +0 -1829
- package/dist/_chunks/libs/nanoid.mjs +0 -29
- package/dist/_chunks/model.mjs +0 -12
- package/dist/_chunks/module.d.mts +0 -71
- package/dist/_chunks/module.mjs +0 -18657
- package/dist/_chunks/vite-build.mjs +0 -69
- package/dist/_chunks/vite-dev.mjs +0 -79
- package/dist/_chunks/vite-preview.mjs +0 -27
- package/dist/language/module.d.mts +0 -5
- package/dist/language/module.mjs +0 -20
- package/dist/vite/vite-build.d.mts +0 -26
- package/dist/vite/vite-build.mjs +0 -27
- package/dist/vite/vite-dev.d.mts +0 -34
- package/dist/vite/vite-dev.mjs +0 -29
- package/dist/vite/vite-preview.d.mts +0 -20
- package/dist/vite/vite-preview.mjs +0 -26
|
@@ -1,436 +1 @@
|
|
|
1
|
-
import crypto from "crypto";
|
|
2
|
-
import { createServer } from "http";
|
|
3
|
-
import { Http2ServerRequest } from "http2";
|
|
4
|
-
import { Readable } from "stream";
|
|
5
|
-
|
|
6
|
-
//#region ../../node_modules/.pnpm/@hono+node-server@1.19.9_hono@4.11.6/node_modules/@hono/node-server/dist/index.mjs
|
|
7
|
-
var RequestError = class extends Error {
|
|
8
|
-
constructor(message, options) {
|
|
9
|
-
super(message, options);
|
|
10
|
-
this.name = "RequestError";
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
var toRequestError = (e) => {
|
|
14
|
-
if (e instanceof RequestError) return e;
|
|
15
|
-
return new RequestError(e.message, { cause: e });
|
|
16
|
-
};
|
|
17
|
-
var GlobalRequest = global.Request;
|
|
18
|
-
var Request = class extends GlobalRequest {
|
|
19
|
-
constructor(input, options) {
|
|
20
|
-
if (typeof input === "object" && getRequestCache in input) input = input[getRequestCache]();
|
|
21
|
-
if (typeof options?.body?.getReader !== "undefined") options.duplex ??= "half";
|
|
22
|
-
super(input, options);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
var newHeadersFromIncoming = (incoming) => {
|
|
26
|
-
const headerRecord = [];
|
|
27
|
-
const rawHeaders = incoming.rawHeaders;
|
|
28
|
-
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
29
|
-
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
30
|
-
if (key.charCodeAt(0) !== 58) headerRecord.push([key, value]);
|
|
31
|
-
}
|
|
32
|
-
return new Headers(headerRecord);
|
|
33
|
-
};
|
|
34
|
-
var wrapBodyStream = Symbol("wrapBodyStream");
|
|
35
|
-
var newRequestFromIncoming = (method, url, headers, incoming, abortController) => {
|
|
36
|
-
const init = {
|
|
37
|
-
method,
|
|
38
|
-
headers,
|
|
39
|
-
signal: abortController.signal
|
|
40
|
-
};
|
|
41
|
-
if (method === "TRACE") {
|
|
42
|
-
init.method = "GET";
|
|
43
|
-
const req = new Request(url, init);
|
|
44
|
-
Object.defineProperty(req, "method", { get() {
|
|
45
|
-
return "TRACE";
|
|
46
|
-
} });
|
|
47
|
-
return req;
|
|
48
|
-
}
|
|
49
|
-
if (!(method === "GET" || method === "HEAD")) if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) init.body = new ReadableStream({ start(controller) {
|
|
50
|
-
controller.enqueue(incoming.rawBody);
|
|
51
|
-
controller.close();
|
|
52
|
-
} });
|
|
53
|
-
else if (incoming[wrapBodyStream]) {
|
|
54
|
-
let reader;
|
|
55
|
-
init.body = new ReadableStream({ async pull(controller) {
|
|
56
|
-
try {
|
|
57
|
-
reader ||= Readable.toWeb(incoming).getReader();
|
|
58
|
-
const { done, value } = await reader.read();
|
|
59
|
-
if (done) controller.close();
|
|
60
|
-
else controller.enqueue(value);
|
|
61
|
-
} catch (error) {
|
|
62
|
-
controller.error(error);
|
|
63
|
-
}
|
|
64
|
-
} });
|
|
65
|
-
} else init.body = Readable.toWeb(incoming);
|
|
66
|
-
return new Request(url, init);
|
|
67
|
-
};
|
|
68
|
-
var getRequestCache = Symbol("getRequestCache");
|
|
69
|
-
var requestCache = Symbol("requestCache");
|
|
70
|
-
var incomingKey = Symbol("incomingKey");
|
|
71
|
-
var urlKey = Symbol("urlKey");
|
|
72
|
-
var headersKey = Symbol("headersKey");
|
|
73
|
-
var abortControllerKey = Symbol("abortControllerKey");
|
|
74
|
-
var requestPrototype = {
|
|
75
|
-
get method() {
|
|
76
|
-
return this[incomingKey].method || "GET";
|
|
77
|
-
},
|
|
78
|
-
get url() {
|
|
79
|
-
return this[urlKey];
|
|
80
|
-
},
|
|
81
|
-
get headers() {
|
|
82
|
-
return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]);
|
|
83
|
-
},
|
|
84
|
-
[Symbol("getAbortController")]() {
|
|
85
|
-
this[getRequestCache]();
|
|
86
|
-
return this[abortControllerKey];
|
|
87
|
-
},
|
|
88
|
-
[getRequestCache]() {
|
|
89
|
-
this[abortControllerKey] ||= new AbortController();
|
|
90
|
-
return this[requestCache] ||= newRequestFromIncoming(this.method, this[urlKey], this.headers, this[incomingKey], this[abortControllerKey]);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
[
|
|
94
|
-
"body",
|
|
95
|
-
"bodyUsed",
|
|
96
|
-
"cache",
|
|
97
|
-
"credentials",
|
|
98
|
-
"destination",
|
|
99
|
-
"integrity",
|
|
100
|
-
"mode",
|
|
101
|
-
"redirect",
|
|
102
|
-
"referrer",
|
|
103
|
-
"referrerPolicy",
|
|
104
|
-
"signal",
|
|
105
|
-
"keepalive"
|
|
106
|
-
].forEach((k) => {
|
|
107
|
-
Object.defineProperty(requestPrototype, k, { get() {
|
|
108
|
-
return this[getRequestCache]()[k];
|
|
109
|
-
} });
|
|
110
|
-
});
|
|
111
|
-
[
|
|
112
|
-
"arrayBuffer",
|
|
113
|
-
"blob",
|
|
114
|
-
"clone",
|
|
115
|
-
"formData",
|
|
116
|
-
"json",
|
|
117
|
-
"text"
|
|
118
|
-
].forEach((k) => {
|
|
119
|
-
Object.defineProperty(requestPrototype, k, { value: function() {
|
|
120
|
-
return this[getRequestCache]()[k]();
|
|
121
|
-
} });
|
|
122
|
-
});
|
|
123
|
-
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
124
|
-
var newRequest = (incoming, defaultHostname) => {
|
|
125
|
-
const req = Object.create(requestPrototype);
|
|
126
|
-
req[incomingKey] = incoming;
|
|
127
|
-
const incomingUrl = incoming.url || "";
|
|
128
|
-
if (incomingUrl[0] !== "/" && (incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) {
|
|
129
|
-
if (incoming instanceof Http2ServerRequest) throw new RequestError("Absolute URL for :path is not allowed in HTTP/2");
|
|
130
|
-
try {
|
|
131
|
-
req[urlKey] = new URL(incomingUrl).href;
|
|
132
|
-
} catch (e) {
|
|
133
|
-
throw new RequestError("Invalid absolute URL", { cause: e });
|
|
134
|
-
}
|
|
135
|
-
return req;
|
|
136
|
-
}
|
|
137
|
-
const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
|
|
138
|
-
if (!host) throw new RequestError("Missing host header");
|
|
139
|
-
let scheme;
|
|
140
|
-
if (incoming instanceof Http2ServerRequest) {
|
|
141
|
-
scheme = incoming.scheme;
|
|
142
|
-
if (!(scheme === "http" || scheme === "https")) throw new RequestError("Unsupported scheme");
|
|
143
|
-
} else scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http";
|
|
144
|
-
const url = new URL(`${scheme}://${host}${incomingUrl}`);
|
|
145
|
-
if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) throw new RequestError("Invalid host header");
|
|
146
|
-
req[urlKey] = url.href;
|
|
147
|
-
return req;
|
|
148
|
-
};
|
|
149
|
-
var responseCache = Symbol("responseCache");
|
|
150
|
-
var getResponseCache = Symbol("getResponseCache");
|
|
151
|
-
var cacheKey = Symbol("cache");
|
|
152
|
-
var GlobalResponse = global.Response;
|
|
153
|
-
var Response2 = class _Response {
|
|
154
|
-
#body;
|
|
155
|
-
#init;
|
|
156
|
-
[getResponseCache]() {
|
|
157
|
-
delete this[cacheKey];
|
|
158
|
-
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
159
|
-
}
|
|
160
|
-
constructor(body, init) {
|
|
161
|
-
let headers;
|
|
162
|
-
this.#body = body;
|
|
163
|
-
if (init instanceof _Response) {
|
|
164
|
-
const cachedGlobalResponse = init[responseCache];
|
|
165
|
-
if (cachedGlobalResponse) {
|
|
166
|
-
this.#init = cachedGlobalResponse;
|
|
167
|
-
this[getResponseCache]();
|
|
168
|
-
return;
|
|
169
|
-
} else {
|
|
170
|
-
this.#init = init.#init;
|
|
171
|
-
headers = new Headers(init.#init.headers);
|
|
172
|
-
}
|
|
173
|
-
} else this.#init = init;
|
|
174
|
-
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
175
|
-
headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
176
|
-
this[cacheKey] = [
|
|
177
|
-
init?.status || 200,
|
|
178
|
-
body,
|
|
179
|
-
headers
|
|
180
|
-
];
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
get headers() {
|
|
184
|
-
const cache = this[cacheKey];
|
|
185
|
-
if (cache) {
|
|
186
|
-
if (!(cache[2] instanceof Headers)) cache[2] = new Headers(cache[2]);
|
|
187
|
-
return cache[2];
|
|
188
|
-
}
|
|
189
|
-
return this[getResponseCache]().headers;
|
|
190
|
-
}
|
|
191
|
-
get status() {
|
|
192
|
-
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
193
|
-
}
|
|
194
|
-
get ok() {
|
|
195
|
-
const status = this.status;
|
|
196
|
-
return status >= 200 && status < 300;
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
[
|
|
200
|
-
"body",
|
|
201
|
-
"bodyUsed",
|
|
202
|
-
"redirected",
|
|
203
|
-
"statusText",
|
|
204
|
-
"trailers",
|
|
205
|
-
"type",
|
|
206
|
-
"url"
|
|
207
|
-
].forEach((k) => {
|
|
208
|
-
Object.defineProperty(Response2.prototype, k, { get() {
|
|
209
|
-
return this[getResponseCache]()[k];
|
|
210
|
-
} });
|
|
211
|
-
});
|
|
212
|
-
[
|
|
213
|
-
"arrayBuffer",
|
|
214
|
-
"blob",
|
|
215
|
-
"clone",
|
|
216
|
-
"formData",
|
|
217
|
-
"json",
|
|
218
|
-
"text"
|
|
219
|
-
].forEach((k) => {
|
|
220
|
-
Object.defineProperty(Response2.prototype, k, { value: function() {
|
|
221
|
-
return this[getResponseCache]()[k]();
|
|
222
|
-
} });
|
|
223
|
-
});
|
|
224
|
-
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
225
|
-
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
226
|
-
async function readWithoutBlocking(readPromise) {
|
|
227
|
-
return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]);
|
|
228
|
-
}
|
|
229
|
-
function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) {
|
|
230
|
-
const cancel = (error) => {
|
|
231
|
-
reader.cancel(error).catch(() => {});
|
|
232
|
-
};
|
|
233
|
-
writable.on("close", cancel);
|
|
234
|
-
writable.on("error", cancel);
|
|
235
|
-
(currentReadPromise ?? reader.read()).then(flow, handleStreamError);
|
|
236
|
-
return reader.closed.finally(() => {
|
|
237
|
-
writable.off("close", cancel);
|
|
238
|
-
writable.off("error", cancel);
|
|
239
|
-
});
|
|
240
|
-
function handleStreamError(error) {
|
|
241
|
-
if (error) writable.destroy(error);
|
|
242
|
-
}
|
|
243
|
-
function onDrain() {
|
|
244
|
-
reader.read().then(flow, handleStreamError);
|
|
245
|
-
}
|
|
246
|
-
function flow({ done, value }) {
|
|
247
|
-
try {
|
|
248
|
-
if (done) writable.end();
|
|
249
|
-
else if (!writable.write(value)) writable.once("drain", onDrain);
|
|
250
|
-
else return reader.read().then(flow, handleStreamError);
|
|
251
|
-
} catch (e) {
|
|
252
|
-
handleStreamError(e);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
function writeFromReadableStream(stream, writable) {
|
|
257
|
-
if (stream.locked) throw new TypeError("ReadableStream is locked.");
|
|
258
|
-
else if (writable.destroyed) return;
|
|
259
|
-
return writeFromReadableStreamDefaultReader(stream.getReader(), writable);
|
|
260
|
-
}
|
|
261
|
-
var buildOutgoingHttpHeaders = (headers) => {
|
|
262
|
-
const res = {};
|
|
263
|
-
if (!(headers instanceof Headers)) headers = new Headers(headers ?? void 0);
|
|
264
|
-
const cookies = [];
|
|
265
|
-
for (const [k, v] of headers) if (k === "set-cookie") cookies.push(v);
|
|
266
|
-
else res[k] = v;
|
|
267
|
-
if (cookies.length > 0) res["set-cookie"] = cookies;
|
|
268
|
-
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
269
|
-
return res;
|
|
270
|
-
};
|
|
271
|
-
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
272
|
-
if (typeof global.crypto === "undefined") global.crypto = crypto;
|
|
273
|
-
var outgoingEnded = Symbol("outgoingEnded");
|
|
274
|
-
var handleRequestError = () => new Response(null, { status: 400 });
|
|
275
|
-
var handleFetchError = (e) => new Response(null, { status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500 });
|
|
276
|
-
var handleResponseError = (e, outgoing) => {
|
|
277
|
-
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
278
|
-
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") console.info("The user aborted a request.");
|
|
279
|
-
else {
|
|
280
|
-
console.error(e);
|
|
281
|
-
if (!outgoing.headersSent) outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
282
|
-
outgoing.end(`Error: ${err.message}`);
|
|
283
|
-
outgoing.destroy(err);
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
var flushHeaders = (outgoing) => {
|
|
287
|
-
if ("flushHeaders" in outgoing && outgoing.writable) outgoing.flushHeaders();
|
|
288
|
-
};
|
|
289
|
-
var responseViaCache = async (res, outgoing) => {
|
|
290
|
-
let [status, body, header] = res[cacheKey];
|
|
291
|
-
if (header instanceof Headers) header = buildOutgoingHttpHeaders(header);
|
|
292
|
-
if (typeof body === "string") header["Content-Length"] = Buffer.byteLength(body);
|
|
293
|
-
else if (body instanceof Uint8Array) header["Content-Length"] = body.byteLength;
|
|
294
|
-
else if (body instanceof Blob) header["Content-Length"] = body.size;
|
|
295
|
-
outgoing.writeHead(status, header);
|
|
296
|
-
if (typeof body === "string" || body instanceof Uint8Array) outgoing.end(body);
|
|
297
|
-
else if (body instanceof Blob) outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
298
|
-
else {
|
|
299
|
-
flushHeaders(outgoing);
|
|
300
|
-
await writeFromReadableStream(body, outgoing)?.catch((e) => handleResponseError(e, outgoing));
|
|
301
|
-
}
|
|
302
|
-
outgoing[outgoingEnded]?.();
|
|
303
|
-
};
|
|
304
|
-
var isPromise = (res) => typeof res.then === "function";
|
|
305
|
-
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
306
|
-
if (isPromise(res)) if (options.errorHandler) try {
|
|
307
|
-
res = await res;
|
|
308
|
-
} catch (err) {
|
|
309
|
-
const errRes = await options.errorHandler(err);
|
|
310
|
-
if (!errRes) return;
|
|
311
|
-
res = errRes;
|
|
312
|
-
}
|
|
313
|
-
else res = await res.catch(handleFetchError);
|
|
314
|
-
if (cacheKey in res) return responseViaCache(res, outgoing);
|
|
315
|
-
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
316
|
-
if (res.body) {
|
|
317
|
-
const reader = res.body.getReader();
|
|
318
|
-
const values = [];
|
|
319
|
-
let done = false;
|
|
320
|
-
let currentReadPromise = void 0;
|
|
321
|
-
if (resHeaderRecord["transfer-encoding"] !== "chunked") {
|
|
322
|
-
let maxReadCount = 2;
|
|
323
|
-
for (let i = 0; i < maxReadCount; i++) {
|
|
324
|
-
currentReadPromise ||= reader.read();
|
|
325
|
-
const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => {
|
|
326
|
-
console.error(e);
|
|
327
|
-
done = true;
|
|
328
|
-
});
|
|
329
|
-
if (!chunk) {
|
|
330
|
-
if (i === 1) {
|
|
331
|
-
await new Promise((resolve) => setTimeout(resolve));
|
|
332
|
-
maxReadCount = 3;
|
|
333
|
-
continue;
|
|
334
|
-
}
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
currentReadPromise = void 0;
|
|
338
|
-
if (chunk.value) values.push(chunk.value);
|
|
339
|
-
if (chunk.done) {
|
|
340
|
-
done = true;
|
|
341
|
-
break;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
if (done && !("content-length" in resHeaderRecord)) resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0);
|
|
345
|
-
}
|
|
346
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
347
|
-
values.forEach((value) => {
|
|
348
|
-
outgoing.write(value);
|
|
349
|
-
});
|
|
350
|
-
if (done) outgoing.end();
|
|
351
|
-
else {
|
|
352
|
-
if (values.length === 0) flushHeaders(outgoing);
|
|
353
|
-
await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise);
|
|
354
|
-
}
|
|
355
|
-
} else if (resHeaderRecord[X_ALREADY_SENT]) {} else {
|
|
356
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
357
|
-
outgoing.end();
|
|
358
|
-
}
|
|
359
|
-
outgoing[outgoingEnded]?.();
|
|
360
|
-
};
|
|
361
|
-
var getRequestListener = (fetchCallback, options = {}) => {
|
|
362
|
-
const autoCleanupIncoming = options.autoCleanupIncoming ?? true;
|
|
363
|
-
if (options.overrideGlobalObjects !== false && global.Request !== Request) {
|
|
364
|
-
Object.defineProperty(global, "Request", { value: Request });
|
|
365
|
-
Object.defineProperty(global, "Response", { value: Response2 });
|
|
366
|
-
}
|
|
367
|
-
return async (incoming, outgoing) => {
|
|
368
|
-
let res, req;
|
|
369
|
-
try {
|
|
370
|
-
req = newRequest(incoming, options.hostname);
|
|
371
|
-
let incomingEnded = !autoCleanupIncoming || incoming.method === "GET" || incoming.method === "HEAD";
|
|
372
|
-
if (!incomingEnded) {
|
|
373
|
-
incoming[wrapBodyStream] = true;
|
|
374
|
-
incoming.on("end", () => {
|
|
375
|
-
incomingEnded = true;
|
|
376
|
-
});
|
|
377
|
-
if (incoming instanceof Http2ServerRequest) outgoing[outgoingEnded] = () => {
|
|
378
|
-
if (!incomingEnded) setTimeout(() => {
|
|
379
|
-
if (!incomingEnded) setTimeout(() => {
|
|
380
|
-
incoming.destroy();
|
|
381
|
-
outgoing.destroy();
|
|
382
|
-
});
|
|
383
|
-
});
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
outgoing.on("close", () => {
|
|
387
|
-
if (req[abortControllerKey]) {
|
|
388
|
-
if (incoming.errored) req[abortControllerKey].abort(incoming.errored.toString());
|
|
389
|
-
else if (!outgoing.writableFinished) req[abortControllerKey].abort("Client connection prematurely closed.");
|
|
390
|
-
}
|
|
391
|
-
if (!incomingEnded) setTimeout(() => {
|
|
392
|
-
if (!incomingEnded) setTimeout(() => {
|
|
393
|
-
incoming.destroy();
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
});
|
|
397
|
-
res = fetchCallback(req, {
|
|
398
|
-
incoming,
|
|
399
|
-
outgoing
|
|
400
|
-
});
|
|
401
|
-
if (cacheKey in res) return responseViaCache(res, outgoing);
|
|
402
|
-
} catch (e) {
|
|
403
|
-
if (!res) if (options.errorHandler) {
|
|
404
|
-
res = await options.errorHandler(req ? e : toRequestError(e));
|
|
405
|
-
if (!res) return;
|
|
406
|
-
} else if (!req) res = handleRequestError();
|
|
407
|
-
else res = handleFetchError(e);
|
|
408
|
-
else return handleResponseError(e, outgoing);
|
|
409
|
-
}
|
|
410
|
-
try {
|
|
411
|
-
return await responseViaResponseObject(res, outgoing, options);
|
|
412
|
-
} catch (e) {
|
|
413
|
-
return handleResponseError(e, outgoing);
|
|
414
|
-
}
|
|
415
|
-
};
|
|
416
|
-
};
|
|
417
|
-
var createAdaptorServer = (options) => {
|
|
418
|
-
const fetchCallback = options.fetch;
|
|
419
|
-
const requestListener = getRequestListener(fetchCallback, {
|
|
420
|
-
hostname: options.hostname,
|
|
421
|
-
overrideGlobalObjects: options.overrideGlobalObjects,
|
|
422
|
-
autoCleanupIncoming: options.autoCleanupIncoming
|
|
423
|
-
});
|
|
424
|
-
return (options.createServer || createServer)(options.serverOptions || {}, requestListener);
|
|
425
|
-
};
|
|
426
|
-
var serve = (options, listeningListener) => {
|
|
427
|
-
const server = createAdaptorServer(options);
|
|
428
|
-
server.listen(options?.port ?? 3e3, options.hostname, () => {
|
|
429
|
-
const serverInfo = server.address();
|
|
430
|
-
listeningListener && listeningListener(serverInfo);
|
|
431
|
-
});
|
|
432
|
-
return server;
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
//#endregion
|
|
436
|
-
export { serve as t };
|
|
1
|
+
import crypto from"crypto";import{createServer}from"http";import{Http2ServerRequest}from"http2";import{Readable}from"stream";var RequestError=class extends Error{constructor(e,t){super(e,t),this.name=`RequestError`}},toRequestError=e=>e instanceof RequestError?e:new RequestError(e.message,{cause:e}),GlobalRequest=global.Request,Request=class extends GlobalRequest{constructor(e,t){typeof e==`object`&&getRequestCache in e&&(e=e[getRequestCache]()),t?.body?.getReader!==void 0&&(t.duplex??=`half`),super(e,t)}},newHeadersFromIncoming=e=>{let t=[],n=e.rawHeaders;for(let e=0;e<n.length;e+=2){let{[e]:r,[e+1]:i}=n;r.charCodeAt(0)!==58&&t.push([r,i])}return new Headers(t)},wrapBodyStream=Symbol(`wrapBodyStream`),newRequestFromIncoming=(e,t,n,i,a)=>{let o={method:e,headers:n,signal:a.signal};if(e===`TRACE`){o.method=`GET`;let e=new Request(t,o);return Object.defineProperty(e,`method`,{get(){return`TRACE`}}),e}if(!(e===`GET`||e===`HEAD`))if(`rawBody`in i&&i.rawBody instanceof Buffer)o.body=new ReadableStream({start(e){e.enqueue(i.rawBody),e.close()}});else if(i[wrapBodyStream]){let e;o.body=new ReadableStream({async pull(t){try{e||=Readable.toWeb(i).getReader();let{done:n,value:a}=await e.read();n?t.close():t.enqueue(a)}catch(e){t.error(e)}}})}else o.body=Readable.toWeb(i);return new Request(t,o)},getRequestCache=Symbol(`getRequestCache`),requestCache=Symbol(`requestCache`),incomingKey=Symbol(`incomingKey`),urlKey=Symbol(`urlKey`),headersKey=Symbol(`headersKey`),abortControllerKey=Symbol(`abortControllerKey`),requestPrototype={get method(){return this[incomingKey].method||`GET`},get url(){return this[urlKey]},get headers(){return this[headersKey]||=newHeadersFromIncoming(this[incomingKey])},[Symbol(`getAbortController`)](){return this[getRequestCache](),this[abortControllerKey]},[getRequestCache](){return this[abortControllerKey]||=new AbortController,this[requestCache]||=newRequestFromIncoming(this.method,this[urlKey],this.headers,this[incomingKey],this[abortControllerKey])}};[`body`,`bodyUsed`,`cache`,`credentials`,`destination`,`integrity`,`mode`,`redirect`,`referrer`,`referrerPolicy`,`signal`,`keepalive`].forEach(e=>{Object.defineProperty(requestPrototype,e,{get(){return this[getRequestCache]()[e]}})}),[`arrayBuffer`,`blob`,`clone`,`formData`,`json`,`text`].forEach(e=>{Object.defineProperty(requestPrototype,e,{value:function(){return this[getRequestCache]()[e]()}})}),Object.setPrototypeOf(requestPrototype,Request.prototype);var newRequest=(e,t)=>{let r=Object.create(requestPrototype);r[incomingKey]=e;let a=e.url||``;if(a[0]!==`/`&&(a.startsWith(`http://`)||a.startsWith(`https://`))){if(e instanceof Http2ServerRequest)throw new RequestError(`Absolute URL for :path is not allowed in HTTP/2`);try{r[urlKey]=new URL(a).href}catch(e){throw new RequestError(`Invalid absolute URL`,{cause:e})}return r}let o=(e instanceof Http2ServerRequest?e.authority:e.headers.host)||t;if(!o)throw new RequestError(`Missing host header`);let s;if(e instanceof Http2ServerRequest){if(s=e.scheme,!(s===`http`||s===`https`))throw new RequestError(`Unsupported scheme`)}else s=e.socket&&e.socket.encrypted?`https`:`http`;let c=new URL(`${s}://${o}${a}`);if(c.hostname.length!==o.length&&c.hostname!==o.replace(/:\d+$/,``))throw new RequestError(`Invalid host header`);return r[urlKey]=c.href,r},responseCache=Symbol(`responseCache`),getResponseCache=Symbol(`getResponseCache`),cacheKey=Symbol(`cache`),GlobalResponse=global.Response,Response2=class e{#e;#t;[getResponseCache](){return delete this[cacheKey],this[responseCache]||=new GlobalResponse(this.#e,this.#t)}constructor(t,n){let r;if(this.#e=t,n instanceof e){let e=n[responseCache];if(e){this.#t=e,this[getResponseCache]();return}else this.#t=n.#t,r=new Headers(n.#t.headers)}else this.#t=n;(typeof t==`string`||t?.getReader!==void 0||t instanceof Blob||t instanceof Uint8Array)&&(r||=n?.headers||{"content-type":`text/plain; charset=UTF-8`},this[cacheKey]=[n?.status||200,t,r])}get headers(){let e=this[cacheKey];return e?(e[2]instanceof Headers||(e[2]=new Headers(e[2])),e[2]):this[getResponseCache]().headers}get status(){return this[cacheKey]?.[0]??this[getResponseCache]().status}get ok(){let e=this.status;return e>=200&&e<300}};[`body`,`bodyUsed`,`redirected`,`statusText`,`trailers`,`type`,`url`].forEach(e=>{Object.defineProperty(Response2.prototype,e,{get(){return this[getResponseCache]()[e]}})}),[`arrayBuffer`,`blob`,`clone`,`formData`,`json`,`text`].forEach(e=>{Object.defineProperty(Response2.prototype,e,{value:function(){return this[getResponseCache]()[e]()}})}),Object.setPrototypeOf(Response2,GlobalResponse),Object.setPrototypeOf(Response2.prototype,GlobalResponse.prototype);async function readWithoutBlocking(e){return Promise.race([e,Promise.resolve().then(()=>Promise.resolve(void 0))])}function writeFromReadableStreamDefaultReader(e,t,n){let r=t=>{e.cancel(t).catch(()=>{})};return t.on(`close`,r),t.on(`error`,r),(n??e.read()).then(o,i),e.closed.finally(()=>{t.off(`close`,r),t.off(`error`,r)});function i(e){e&&t.destroy(e)}function a(){e.read().then(o,i)}function o({done:n,value:r}){try{if(n)t.end();else if(!t.write(r))t.once(`drain`,a);else return e.read().then(o,i)}catch(e){i(e)}}}function writeFromReadableStream(e,t){if(e.locked)throw TypeError(`ReadableStream is locked.`);return t.destroyed?void 0:writeFromReadableStreamDefaultReader(e.getReader(),t)}var buildOutgoingHttpHeaders=e=>{let t={};e instanceof Headers||(e=new Headers(e??void 0));let n=[];for(let[r,i]of e)r===`set-cookie`?n.push(i):t[r]=i;return n.length>0&&(t[`set-cookie`]=n),t[`content-type`]??=`text/plain; charset=UTF-8`,t},X_ALREADY_SENT=`x-hono-already-sent`;global.crypto===void 0&&(global.crypto=crypto);var outgoingEnded=Symbol(`outgoingEnded`),handleRequestError=()=>new Response(null,{status:400}),handleFetchError=e=>new Response(null,{status:e instanceof Error&&(e.name===`TimeoutError`||e.constructor.name===`TimeoutError`)?504:500}),handleResponseError=(e,t)=>{let n=e instanceof Error?e:Error(`unknown error`,{cause:e});n.code===`ERR_STREAM_PREMATURE_CLOSE`?console.info(`The user aborted a request.`):(console.error(e),t.headersSent||t.writeHead(500,{"Content-Type":`text/plain`}),t.end(`Error: ${n.message}`),t.destroy(n))},flushHeaders=e=>{`flushHeaders`in e&&e.writable&&e.flushHeaders()},responseViaCache=async(e,t)=>{let[n,r,i]=e[cacheKey];i instanceof Headers&&(i=buildOutgoingHttpHeaders(i)),typeof r==`string`?i[`Content-Length`]=Buffer.byteLength(r):r instanceof Uint8Array?i[`Content-Length`]=r.byteLength:r instanceof Blob&&(i[`Content-Length`]=r.size),t.writeHead(n,i),typeof r==`string`||r instanceof Uint8Array?t.end(r):r instanceof Blob?t.end(new Uint8Array(await r.arrayBuffer())):(flushHeaders(t),await writeFromReadableStream(r,t)?.catch(e=>handleResponseError(e,t))),t[outgoingEnded]?.()},isPromise=e=>typeof e.then==`function`,responseViaResponseObject=async(e,t,n={})=>{if(isPromise(e))if(n.errorHandler)try{e=await e}catch(t){let r=await n.errorHandler(t);if(!r)return;e=r}else e=await e.catch(handleFetchError);if(cacheKey in e)return responseViaCache(e,t);let r=buildOutgoingHttpHeaders(e.headers);if(e.body){let n=e.body.getReader(),i=[],a=!1,o;if(r[`transfer-encoding`]!==`chunked`){let e=2;for(let t=0;t<e;t++){o||=n.read();let r=await readWithoutBlocking(o).catch(e=>{console.error(e),a=!0});if(!r){if(t===1){await new Promise(e=>setTimeout(e)),e=3;continue}break}if(o=void 0,r.value&&i.push(r.value),r.done){a=!0;break}}a&&!(`content-length`in r)&&(r[`content-length`]=i.reduce((e,t)=>e+t.length,0))}t.writeHead(e.status,r),i.forEach(e=>{t.write(e)}),a?t.end():(i.length===0&&flushHeaders(t),await writeFromReadableStreamDefaultReader(n,t,o))}else r[X_ALREADY_SENT]||(t.writeHead(e.status,r),t.end());t[outgoingEnded]?.()},getRequestListener=(e,t={})=>{let r=t.autoCleanupIncoming??!0;return t.overrideGlobalObjects!==!1&&global.Request!==Request&&(Object.defineProperty(global,`Request`,{value:Request}),Object.defineProperty(global,`Response`,{value:Response2})),async(i,o)=>{let s,c;try{c=newRequest(i,t.hostname);let a=!r||i.method===`GET`||i.method===`HEAD`;if(a||(i[wrapBodyStream]=!0,i.on(`end`,()=>{a=!0}),i instanceof Http2ServerRequest&&(o[outgoingEnded]=()=>{a||setTimeout(()=>{a||setTimeout(()=>{i.destroy(),o.destroy()})})})),o.on(`close`,()=>{c[abortControllerKey]&&(i.errored?c[abortControllerKey].abort(i.errored.toString()):o.writableFinished||c[abortControllerKey].abort(`Client connection prematurely closed.`)),a||setTimeout(()=>{a||setTimeout(()=>{i.destroy()})})}),s=e(c,{incoming:i,outgoing:o}),cacheKey in s)return responseViaCache(s,o)}catch(e){if(s)return handleResponseError(e,o);if(t.errorHandler){if(s=await t.errorHandler(c?e:toRequestError(e)),!s)return}else s=c?handleFetchError(e):handleRequestError()}try{return await responseViaResponseObject(s,o,t)}catch(e){return handleResponseError(e,o)}}},createAdaptorServer=e=>{let n=e.fetch,r=getRequestListener(n,{hostname:e.hostname,overrideGlobalObjects:e.overrideGlobalObjects,autoCleanupIncoming:e.autoCleanupIncoming});return(e.createServer||createServer)(e.serverOptions||{},r)},serve=(e,t)=>{let n=createAdaptorServer(e);return n.listen(e?.port??3e3,e.hostname,()=>{let e=n.address();t&&t(e)}),n};export{serve as t};
|