@marko/run 0.5.17 → 0.6.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/dist/.tsbuildinfo +1 -1
- package/dist/adapter/index.cjs +71 -193
- package/dist/adapter/index.js +71 -193
- package/dist/adapter/middleware.cjs +70 -202
- package/dist/adapter/middleware.js +70 -192
- package/dist/cli/index.mjs +30 -31
- package/dist/runtime/internal.cjs +1 -1
- package/dist/runtime/internal.js +1 -1
- package/dist/vite/index.cjs +30 -31
- package/dist/vite/index.js +30 -31
- package/package.json +12 -13
- package/dist/adapter/polyfill.d.ts +0 -11
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/adapter/middleware.ts
|
|
@@ -42,76 +32,6 @@ var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
|
|
|
42
32
|
|
|
43
33
|
// src/adapter/middleware.ts
|
|
44
34
|
var import_node_stream = require("node:stream");
|
|
45
|
-
|
|
46
|
-
// src/adapter/polyfill.ts
|
|
47
|
-
var import_crypto = require("crypto");
|
|
48
|
-
var import_http = require("http");
|
|
49
|
-
var webStream = __toESM(require("stream/web"), 1);
|
|
50
|
-
var undici = __toESM(require("undici"), 1);
|
|
51
|
-
globalThis.crypto ?? (globalThis.crypto = import_crypto.webcrypto);
|
|
52
|
-
globalThis.fetch ?? (globalThis.fetch = undici.fetch);
|
|
53
|
-
globalThis.Response ?? (globalThis.Response = undici.Response);
|
|
54
|
-
globalThis.Request ?? (globalThis.Request = undici.Request);
|
|
55
|
-
globalThis.Headers ?? (globalThis.Headers = undici.Headers);
|
|
56
|
-
globalThis.ReadableStream ?? (globalThis.ReadableStream = webStream.ReadableStream);
|
|
57
|
-
globalThis.TransformStream ?? (globalThis.TransformStream = webStream.TransformStream);
|
|
58
|
-
globalThis.WritableStream ?? (globalThis.WritableStream = webStream.WritableStream);
|
|
59
|
-
globalThis.FormData ?? (globalThis.FormData = undici.FormData);
|
|
60
|
-
globalThis.File ?? (globalThis.File = undici.File);
|
|
61
|
-
var appendHeader = import_http.ServerResponse.prototype.appendHeader ? appendHeader_platform : appendHeader_fallback;
|
|
62
|
-
function appendHeader_platform(response, name, value) {
|
|
63
|
-
response.appendHeader(name, value);
|
|
64
|
-
}
|
|
65
|
-
function appendHeader_fallback(response, name, value) {
|
|
66
|
-
const existing = response.getHeader(name);
|
|
67
|
-
if (existing === void 0) {
|
|
68
|
-
response.setHeader(name, value);
|
|
69
|
-
} else if (Array.isArray(existing)) {
|
|
70
|
-
response.setHeader(name, existing.concat(value));
|
|
71
|
-
} else {
|
|
72
|
-
response.setHeader(name, [String(existing)].concat(value));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
var getSetCookie = Headers.prototype.getSetCookie ? getSetCookie_platform : getSetCookie_fallback;
|
|
76
|
-
function getSetCookie_platform(headers) {
|
|
77
|
-
return headers.getSetCookie();
|
|
78
|
-
}
|
|
79
|
-
var inExpiresDateRgs = /Expires\s*=\s*(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*$/i;
|
|
80
|
-
function getSetCookie_fallback(headers) {
|
|
81
|
-
const value = headers.get("set-cookie");
|
|
82
|
-
if (!value) return void 0;
|
|
83
|
-
let sepIndex = value.indexOf(",") + 1;
|
|
84
|
-
if (!sepIndex) return value;
|
|
85
|
-
let index = 0;
|
|
86
|
-
let setCookie = void 0;
|
|
87
|
-
let setCookies = void 0;
|
|
88
|
-
do {
|
|
89
|
-
const valuePart = value.slice(index, sepIndex - 1);
|
|
90
|
-
if (!inExpiresDateRgs.test(valuePart)) {
|
|
91
|
-
if (setCookies) {
|
|
92
|
-
setCookies.push(valuePart);
|
|
93
|
-
} else if (setCookie) {
|
|
94
|
-
setCookies = [setCookie, valuePart];
|
|
95
|
-
} else {
|
|
96
|
-
setCookie = valuePart;
|
|
97
|
-
}
|
|
98
|
-
index = sepIndex;
|
|
99
|
-
while (value.charCodeAt(index) === 32) index++;
|
|
100
|
-
}
|
|
101
|
-
sepIndex = value.indexOf(",", sepIndex) + 1;
|
|
102
|
-
} while (sepIndex);
|
|
103
|
-
if (index) {
|
|
104
|
-
const valuePart = value.slice(index);
|
|
105
|
-
if (setCookies) {
|
|
106
|
-
setCookies.push(valuePart);
|
|
107
|
-
return setCookies;
|
|
108
|
-
}
|
|
109
|
-
return [setCookie, valuePart];
|
|
110
|
-
}
|
|
111
|
-
return value;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// src/adapter/middleware.ts
|
|
115
35
|
function getForwardedHeader(req, name) {
|
|
116
36
|
const value = req.headers["x-forwarded-" + name];
|
|
117
37
|
if (value) {
|
|
@@ -126,7 +46,7 @@ function getOrigin(req, trustProxy) {
|
|
|
126
46
|
const protocol = trustProxy && getForwardedHeader(req, "proto") || req.socket.encrypted && "https" || req.protocol || "http";
|
|
127
47
|
let host = trustProxy && getForwardedHeader(req, "host") || req.headers.host;
|
|
128
48
|
if (!host) {
|
|
129
|
-
if (process.env.NODE_ENV
|
|
49
|
+
if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") {
|
|
130
50
|
host = "localhost";
|
|
131
51
|
console.warn(
|
|
132
52
|
`Could not automatically determine the origin host, using 'localhost'. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
|
|
@@ -145,147 +65,95 @@ function copyResponseHeaders(response, headers) {
|
|
|
145
65
|
response.setHeader(key, value);
|
|
146
66
|
}
|
|
147
67
|
}
|
|
148
|
-
const setCookies = getSetCookie(
|
|
68
|
+
const setCookies = headers.getSetCookie();
|
|
149
69
|
if (setCookies == null ? void 0 : setCookies.length) {
|
|
150
|
-
appendHeader(
|
|
70
|
+
response.appendHeader("set-cookie", setCookies);
|
|
151
71
|
}
|
|
152
72
|
}
|
|
153
|
-
function
|
|
154
|
-
if (error.cause && !error.message) {
|
|
155
|
-
error.message = error.cause.message;
|
|
156
|
-
error.stack || (error.stack = error.cause.stack);
|
|
157
|
-
}
|
|
158
|
-
return error;
|
|
159
|
-
}
|
|
160
|
-
function createMiddleware(fetch2, options) {
|
|
73
|
+
function createMiddleware(fetch, options) {
|
|
161
74
|
const {
|
|
162
75
|
origin = process.env.ORIGIN,
|
|
163
76
|
trustProxy = process.env.TRUST_PROXY === "1",
|
|
164
77
|
createPlatform = (platform) => platform
|
|
165
78
|
} = options ?? (options = {});
|
|
166
79
|
return async (req, res, next) => {
|
|
167
|
-
var _a;
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
res.on("close", onErrorOrClose);
|
|
175
|
-
signal.addEventListener("abort", onSignalAborted);
|
|
176
|
-
function onErrorOrClose(err) {
|
|
177
|
-
req.off("error", onErrorOrClose);
|
|
178
|
-
req.socket.off("error", onErrorOrClose);
|
|
179
|
-
res.off("error", onErrorOrClose);
|
|
180
|
-
res.off("close", onErrorOrClose);
|
|
181
|
-
if (err) {
|
|
182
|
-
signal.removeEventListener("abort", onSignalAborted);
|
|
183
|
-
controller.abort(err);
|
|
80
|
+
var _a, _b, _c, _d;
|
|
81
|
+
try {
|
|
82
|
+
if ((!process.env.NODE_ENV || process.env.NODE_ENV === "development") && globalThis.__marko_run_dev__ && ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html"))) {
|
|
83
|
+
var devWebSocket;
|
|
84
|
+
globalThis.__marko_run_dev__.onClient(res, (ws) => {
|
|
85
|
+
devWebSocket = ws;
|
|
86
|
+
});
|
|
184
87
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
88
|
+
let body;
|
|
89
|
+
switch (req.method) {
|
|
90
|
+
case "POST":
|
|
91
|
+
case "PUT":
|
|
92
|
+
case "PATCH":
|
|
93
|
+
if (import_node_stream.Readable.isDisturbed(req)) {
|
|
94
|
+
body = bodyConsumedErrorStream;
|
|
95
|
+
} else {
|
|
96
|
+
body = req;
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
195
99
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
function sendError() {
|
|
205
|
-
const { message, stack = "" } = normalizeError(signal.reason);
|
|
206
|
-
ws.send(
|
|
207
|
-
JSON.stringify({
|
|
208
|
-
type: "error",
|
|
209
|
-
err: { message, stack }
|
|
210
|
-
})
|
|
211
|
-
);
|
|
212
|
-
}
|
|
100
|
+
const url = new URL(req.url, origin || getOrigin(req, trustProxy));
|
|
101
|
+
const request = new Request(url, {
|
|
102
|
+
method: req.method,
|
|
103
|
+
headers: req.headers,
|
|
104
|
+
body,
|
|
105
|
+
// @ts-expect-error: Node requires this for streams
|
|
106
|
+
duplex: "half"
|
|
213
107
|
});
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
108
|
+
const platform = createPlatform({
|
|
109
|
+
request: req,
|
|
110
|
+
response: res
|
|
111
|
+
});
|
|
112
|
+
const response = await fetch(request, platform);
|
|
113
|
+
if (res.destroyed || res.headersSent) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (response) {
|
|
117
|
+
res.statusCode = response.status;
|
|
118
|
+
copyResponseHeaders(res, response.headers);
|
|
119
|
+
if (response.body) {
|
|
120
|
+
for await (const chunk of response.body) {
|
|
121
|
+
if (res.destroyed) return;
|
|
122
|
+
res.write(chunk);
|
|
123
|
+
(_b = res.flush) == null ? void 0 : _b.call(res);
|
|
124
|
+
}
|
|
125
|
+
} else if (!response.headers.has("content-length")) {
|
|
126
|
+
res.setHeader("content-length", "0");
|
|
224
127
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
method: req.method,
|
|
229
|
-
headers: req.headers,
|
|
230
|
-
body,
|
|
231
|
-
// @ts-expect-error: Node requires this for streams
|
|
232
|
-
duplex: "half",
|
|
233
|
-
signal
|
|
234
|
-
});
|
|
235
|
-
const platform = createPlatform({
|
|
236
|
-
request: req,
|
|
237
|
-
response: res
|
|
238
|
-
});
|
|
239
|
-
let response;
|
|
240
|
-
try {
|
|
241
|
-
response = await fetch2(request, platform);
|
|
242
|
-
} catch (err) {
|
|
243
|
-
normalizeError(err);
|
|
244
|
-
if (next) {
|
|
245
|
-
next(err);
|
|
128
|
+
res.end();
|
|
129
|
+
} else if (next) {
|
|
130
|
+
next();
|
|
246
131
|
} else {
|
|
247
|
-
|
|
132
|
+
(_c = res.socket) == null ? void 0 : _c.destroySoon();
|
|
248
133
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
134
|
+
} catch (err) {
|
|
135
|
+
const error = err;
|
|
136
|
+
if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") {
|
|
137
|
+
if (error.cause && !error.message) {
|
|
138
|
+
error.message = error.cause.message;
|
|
139
|
+
error.stack || (error.stack = error.cause.stack);
|
|
140
|
+
}
|
|
141
|
+
devWebSocket == null ? void 0 : devWebSocket.send(
|
|
142
|
+
JSON.stringify({
|
|
143
|
+
type: "error",
|
|
144
|
+
error: { message: error.message, stack: error.stack }
|
|
145
|
+
})
|
|
146
|
+
);
|
|
254
147
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (!response.headers.has("content-length")) {
|
|
261
|
-
res.setHeader("content-length", "0");
|
|
148
|
+
if (next) {
|
|
149
|
+
next(error);
|
|
150
|
+
} else {
|
|
151
|
+
(_d = res.socket) == null ? void 0 : _d.destroySoon();
|
|
152
|
+
console.error(error);
|
|
262
153
|
}
|
|
263
|
-
res.end();
|
|
264
|
-
return;
|
|
265
|
-
} else if (res.destroyed) {
|
|
266
|
-
controller.abort(new Error("Response stream destroyed"));
|
|
267
|
-
return;
|
|
268
154
|
}
|
|
269
|
-
writeResponse(response.body.getReader(), res, controller);
|
|
270
155
|
};
|
|
271
156
|
}
|
|
272
|
-
async function writeResponse(reader, res, controller) {
|
|
273
|
-
try {
|
|
274
|
-
while (!controller.signal.aborted) {
|
|
275
|
-
const { done, value } = await reader.read();
|
|
276
|
-
if (done) {
|
|
277
|
-
res.end();
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
res.write(value);
|
|
281
|
-
if (res.flush) {
|
|
282
|
-
res.flush();
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
} catch (err) {
|
|
286
|
-
controller.abort(err);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
157
|
var bodyConsumedErrorStream = new ReadableStream({
|
|
290
158
|
pull(controller) {
|
|
291
159
|
controller.error(
|
|
@@ -1,75 +1,5 @@
|
|
|
1
1
|
// src/adapter/middleware.ts
|
|
2
2
|
import { Readable } from "node:stream";
|
|
3
|
-
|
|
4
|
-
// src/adapter/polyfill.ts
|
|
5
|
-
import { webcrypto } from "crypto";
|
|
6
|
-
import { ServerResponse } from "http";
|
|
7
|
-
import * as webStream from "stream/web";
|
|
8
|
-
import * as undici from "undici";
|
|
9
|
-
globalThis.crypto ?? (globalThis.crypto = webcrypto);
|
|
10
|
-
globalThis.fetch ?? (globalThis.fetch = undici.fetch);
|
|
11
|
-
globalThis.Response ?? (globalThis.Response = undici.Response);
|
|
12
|
-
globalThis.Request ?? (globalThis.Request = undici.Request);
|
|
13
|
-
globalThis.Headers ?? (globalThis.Headers = undici.Headers);
|
|
14
|
-
globalThis.ReadableStream ?? (globalThis.ReadableStream = webStream.ReadableStream);
|
|
15
|
-
globalThis.TransformStream ?? (globalThis.TransformStream = webStream.TransformStream);
|
|
16
|
-
globalThis.WritableStream ?? (globalThis.WritableStream = webStream.WritableStream);
|
|
17
|
-
globalThis.FormData ?? (globalThis.FormData = undici.FormData);
|
|
18
|
-
globalThis.File ?? (globalThis.File = undici.File);
|
|
19
|
-
var appendHeader = ServerResponse.prototype.appendHeader ? appendHeader_platform : appendHeader_fallback;
|
|
20
|
-
function appendHeader_platform(response, name, value) {
|
|
21
|
-
response.appendHeader(name, value);
|
|
22
|
-
}
|
|
23
|
-
function appendHeader_fallback(response, name, value) {
|
|
24
|
-
const existing = response.getHeader(name);
|
|
25
|
-
if (existing === void 0) {
|
|
26
|
-
response.setHeader(name, value);
|
|
27
|
-
} else if (Array.isArray(existing)) {
|
|
28
|
-
response.setHeader(name, existing.concat(value));
|
|
29
|
-
} else {
|
|
30
|
-
response.setHeader(name, [String(existing)].concat(value));
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
var getSetCookie = Headers.prototype.getSetCookie ? getSetCookie_platform : getSetCookie_fallback;
|
|
34
|
-
function getSetCookie_platform(headers) {
|
|
35
|
-
return headers.getSetCookie();
|
|
36
|
-
}
|
|
37
|
-
var inExpiresDateRgs = /Expires\s*=\s*(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*$/i;
|
|
38
|
-
function getSetCookie_fallback(headers) {
|
|
39
|
-
const value = headers.get("set-cookie");
|
|
40
|
-
if (!value) return void 0;
|
|
41
|
-
let sepIndex = value.indexOf(",") + 1;
|
|
42
|
-
if (!sepIndex) return value;
|
|
43
|
-
let index = 0;
|
|
44
|
-
let setCookie = void 0;
|
|
45
|
-
let setCookies = void 0;
|
|
46
|
-
do {
|
|
47
|
-
const valuePart = value.slice(index, sepIndex - 1);
|
|
48
|
-
if (!inExpiresDateRgs.test(valuePart)) {
|
|
49
|
-
if (setCookies) {
|
|
50
|
-
setCookies.push(valuePart);
|
|
51
|
-
} else if (setCookie) {
|
|
52
|
-
setCookies = [setCookie, valuePart];
|
|
53
|
-
} else {
|
|
54
|
-
setCookie = valuePart;
|
|
55
|
-
}
|
|
56
|
-
index = sepIndex;
|
|
57
|
-
while (value.charCodeAt(index) === 32) index++;
|
|
58
|
-
}
|
|
59
|
-
sepIndex = value.indexOf(",", sepIndex) + 1;
|
|
60
|
-
} while (sepIndex);
|
|
61
|
-
if (index) {
|
|
62
|
-
const valuePart = value.slice(index);
|
|
63
|
-
if (setCookies) {
|
|
64
|
-
setCookies.push(valuePart);
|
|
65
|
-
return setCookies;
|
|
66
|
-
}
|
|
67
|
-
return [setCookie, valuePart];
|
|
68
|
-
}
|
|
69
|
-
return value;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// src/adapter/middleware.ts
|
|
73
3
|
function getForwardedHeader(req, name) {
|
|
74
4
|
const value = req.headers["x-forwarded-" + name];
|
|
75
5
|
if (value) {
|
|
@@ -84,7 +14,7 @@ function getOrigin(req, trustProxy) {
|
|
|
84
14
|
const protocol = trustProxy && getForwardedHeader(req, "proto") || req.socket.encrypted && "https" || req.protocol || "http";
|
|
85
15
|
let host = trustProxy && getForwardedHeader(req, "host") || req.headers.host;
|
|
86
16
|
if (!host) {
|
|
87
|
-
if (process.env.NODE_ENV
|
|
17
|
+
if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") {
|
|
88
18
|
host = "localhost";
|
|
89
19
|
console.warn(
|
|
90
20
|
`Could not automatically determine the origin host, using 'localhost'. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly.`
|
|
@@ -103,147 +33,95 @@ function copyResponseHeaders(response, headers) {
|
|
|
103
33
|
response.setHeader(key, value);
|
|
104
34
|
}
|
|
105
35
|
}
|
|
106
|
-
const setCookies = getSetCookie(
|
|
36
|
+
const setCookies = headers.getSetCookie();
|
|
107
37
|
if (setCookies == null ? void 0 : setCookies.length) {
|
|
108
|
-
appendHeader(
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
function normalizeError(error) {
|
|
112
|
-
if (error.cause && !error.message) {
|
|
113
|
-
error.message = error.cause.message;
|
|
114
|
-
error.stack || (error.stack = error.cause.stack);
|
|
38
|
+
response.appendHeader("set-cookie", setCookies);
|
|
115
39
|
}
|
|
116
|
-
return error;
|
|
117
40
|
}
|
|
118
|
-
function createMiddleware(
|
|
41
|
+
function createMiddleware(fetch, options) {
|
|
119
42
|
const {
|
|
120
43
|
origin = process.env.ORIGIN,
|
|
121
44
|
trustProxy = process.env.TRUST_PROXY === "1",
|
|
122
45
|
createPlatform = (platform) => platform
|
|
123
46
|
} = options ?? (options = {});
|
|
124
47
|
return async (req, res, next) => {
|
|
125
|
-
var _a;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
res.on("close", onErrorOrClose);
|
|
133
|
-
signal.addEventListener("abort", onSignalAborted);
|
|
134
|
-
function onErrorOrClose(err) {
|
|
135
|
-
req.off("error", onErrorOrClose);
|
|
136
|
-
req.socket.off("error", onErrorOrClose);
|
|
137
|
-
res.off("error", onErrorOrClose);
|
|
138
|
-
res.off("close", onErrorOrClose);
|
|
139
|
-
if (err) {
|
|
140
|
-
signal.removeEventListener("abort", onSignalAborted);
|
|
141
|
-
controller.abort(err);
|
|
48
|
+
var _a, _b, _c, _d;
|
|
49
|
+
try {
|
|
50
|
+
if ((!process.env.NODE_ENV || process.env.NODE_ENV === "development") && globalThis.__marko_run_dev__ && ((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html"))) {
|
|
51
|
+
var devWebSocket;
|
|
52
|
+
globalThis.__marko_run_dev__.onClient(res, (ws) => {
|
|
53
|
+
devWebSocket = ws;
|
|
54
|
+
});
|
|
142
55
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
56
|
+
let body;
|
|
57
|
+
switch (req.method) {
|
|
58
|
+
case "POST":
|
|
59
|
+
case "PUT":
|
|
60
|
+
case "PATCH":
|
|
61
|
+
if (Readable.isDisturbed(req)) {
|
|
62
|
+
body = bodyConsumedErrorStream;
|
|
63
|
+
} else {
|
|
64
|
+
body = req;
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
153
67
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
function sendError() {
|
|
163
|
-
const { message, stack = "" } = normalizeError(signal.reason);
|
|
164
|
-
ws.send(
|
|
165
|
-
JSON.stringify({
|
|
166
|
-
type: "error",
|
|
167
|
-
err: { message, stack }
|
|
168
|
-
})
|
|
169
|
-
);
|
|
170
|
-
}
|
|
68
|
+
const url = new URL(req.url, origin || getOrigin(req, trustProxy));
|
|
69
|
+
const request = new Request(url, {
|
|
70
|
+
method: req.method,
|
|
71
|
+
headers: req.headers,
|
|
72
|
+
body,
|
|
73
|
+
// @ts-expect-error: Node requires this for streams
|
|
74
|
+
duplex: "half"
|
|
171
75
|
});
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
76
|
+
const platform = createPlatform({
|
|
77
|
+
request: req,
|
|
78
|
+
response: res
|
|
79
|
+
});
|
|
80
|
+
const response = await fetch(request, platform);
|
|
81
|
+
if (res.destroyed || res.headersSent) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (response) {
|
|
85
|
+
res.statusCode = response.status;
|
|
86
|
+
copyResponseHeaders(res, response.headers);
|
|
87
|
+
if (response.body) {
|
|
88
|
+
for await (const chunk of response.body) {
|
|
89
|
+
if (res.destroyed) return;
|
|
90
|
+
res.write(chunk);
|
|
91
|
+
(_b = res.flush) == null ? void 0 : _b.call(res);
|
|
92
|
+
}
|
|
93
|
+
} else if (!response.headers.has("content-length")) {
|
|
94
|
+
res.setHeader("content-length", "0");
|
|
182
95
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
method: req.method,
|
|
187
|
-
headers: req.headers,
|
|
188
|
-
body,
|
|
189
|
-
// @ts-expect-error: Node requires this for streams
|
|
190
|
-
duplex: "half",
|
|
191
|
-
signal
|
|
192
|
-
});
|
|
193
|
-
const platform = createPlatform({
|
|
194
|
-
request: req,
|
|
195
|
-
response: res
|
|
196
|
-
});
|
|
197
|
-
let response;
|
|
198
|
-
try {
|
|
199
|
-
response = await fetch2(request, platform);
|
|
200
|
-
} catch (err) {
|
|
201
|
-
normalizeError(err);
|
|
202
|
-
if (next) {
|
|
203
|
-
next(err);
|
|
96
|
+
res.end();
|
|
97
|
+
} else if (next) {
|
|
98
|
+
next();
|
|
204
99
|
} else {
|
|
205
|
-
|
|
100
|
+
(_c = res.socket) == null ? void 0 : _c.destroySoon();
|
|
206
101
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
102
|
+
} catch (err) {
|
|
103
|
+
const error = err;
|
|
104
|
+
if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") {
|
|
105
|
+
if (error.cause && !error.message) {
|
|
106
|
+
error.message = error.cause.message;
|
|
107
|
+
error.stack || (error.stack = error.cause.stack);
|
|
108
|
+
}
|
|
109
|
+
devWebSocket == null ? void 0 : devWebSocket.send(
|
|
110
|
+
JSON.stringify({
|
|
111
|
+
type: "error",
|
|
112
|
+
error: { message: error.message, stack: error.stack }
|
|
113
|
+
})
|
|
114
|
+
);
|
|
212
115
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (!response.headers.has("content-length")) {
|
|
219
|
-
res.setHeader("content-length", "0");
|
|
116
|
+
if (next) {
|
|
117
|
+
next(error);
|
|
118
|
+
} else {
|
|
119
|
+
(_d = res.socket) == null ? void 0 : _d.destroySoon();
|
|
120
|
+
console.error(error);
|
|
220
121
|
}
|
|
221
|
-
res.end();
|
|
222
|
-
return;
|
|
223
|
-
} else if (res.destroyed) {
|
|
224
|
-
controller.abort(new Error("Response stream destroyed"));
|
|
225
|
-
return;
|
|
226
122
|
}
|
|
227
|
-
writeResponse(response.body.getReader(), res, controller);
|
|
228
123
|
};
|
|
229
124
|
}
|
|
230
|
-
async function writeResponse(reader, res, controller) {
|
|
231
|
-
try {
|
|
232
|
-
while (!controller.signal.aborted) {
|
|
233
|
-
const { done, value } = await reader.read();
|
|
234
|
-
if (done) {
|
|
235
|
-
res.end();
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
res.write(value);
|
|
239
|
-
if (res.flush) {
|
|
240
|
-
res.flush();
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
} catch (err) {
|
|
244
|
-
controller.abort(err);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
125
|
var bodyConsumedErrorStream = new ReadableStream({
|
|
248
126
|
pull(controller) {
|
|
249
127
|
controller.error(
|