@universal-middleware/express 0.4.18 → 0.4.20
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/index.d.ts +1 -1
- package/dist/index.js +27 -13
- package/package.json +8 -8
package/dist/index.d.ts
CHANGED
|
@@ -45,7 +45,7 @@ interface DecoratedRequest<C extends Universal.Context = Universal.Context> exte
|
|
|
45
45
|
ip?: string;
|
|
46
46
|
protocol?: string;
|
|
47
47
|
socket?: PossiblyEncryptedSocket;
|
|
48
|
-
rawBody?:
|
|
48
|
+
rawBody?: any;
|
|
49
49
|
originalUrl?: string;
|
|
50
50
|
params?: Record<string, string>;
|
|
51
51
|
[contextSymbol]?: C;
|
package/dist/index.js
CHANGED
|
@@ -22,7 +22,8 @@ function createRequestAdapter(options = {}) {
|
|
|
22
22
|
if (headers[":method"]) {
|
|
23
23
|
headers = Object.fromEntries(Object.entries(headers).filter(([key]) => !key.startsWith(":")));
|
|
24
24
|
}
|
|
25
|
-
const protocol = protocolOverride || req.protocol || trustProxy && parseForwardedHeader("proto") ||
|
|
25
|
+
const protocol = protocolOverride || req.protocol || trustProxy && parseForwardedHeader("proto") || // biome-ignore lint/suspicious/noExplicitAny: encrypted can exist in some express versions
|
|
26
|
+
req.socket?.encrypted && "https" || "http";
|
|
26
27
|
let host = hostOverride || trustProxy && parseForwardedHeader("host") || headers.host;
|
|
27
28
|
if (!host && !warned) {
|
|
28
29
|
console.warn(
|
|
@@ -35,7 +36,7 @@ function createRequestAdapter(options = {}) {
|
|
|
35
36
|
method: req.method,
|
|
36
37
|
headers,
|
|
37
38
|
body: convertBody(req),
|
|
38
|
-
// @ts-
|
|
39
|
+
// @ts-expect-error
|
|
39
40
|
duplex: "half"
|
|
40
41
|
});
|
|
41
42
|
req[requestSymbol] = request;
|
|
@@ -62,7 +63,6 @@ function convertBody(req) {
|
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
// src/response.ts
|
|
65
|
-
import { Readable } from "node:stream";
|
|
66
66
|
import { nodeHeadersToWeb } from "@universal-middleware/core";
|
|
67
67
|
var deno2 = typeof Deno !== "undefined";
|
|
68
68
|
async function sendResponse(fetchResponse, nodeResponse) {
|
|
@@ -73,11 +73,12 @@ async function sendResponse(fetchResponse, nodeResponse) {
|
|
|
73
73
|
} else if (typeof fetchBody.pipe === "function") {
|
|
74
74
|
body = fetchBody;
|
|
75
75
|
} else if (typeof fetchBody.pipeTo === "function") {
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
const { Readable: Readable2 } = await import("node:stream");
|
|
77
|
+
if (!deno2 && Readable2.fromWeb) {
|
|
78
|
+
body = Readable2.fromWeb(fetchBody);
|
|
78
79
|
} else {
|
|
79
80
|
const reader = fetchBody.getReader();
|
|
80
|
-
body = new
|
|
81
|
+
body = new Readable2({
|
|
81
82
|
async read() {
|
|
82
83
|
try {
|
|
83
84
|
const { done, value } = await reader.read();
|
|
@@ -96,7 +97,8 @@ async function sendResponse(fetchResponse, nodeResponse) {
|
|
|
96
97
|
});
|
|
97
98
|
}
|
|
98
99
|
} else if (fetchBody) {
|
|
99
|
-
|
|
100
|
+
const { Readable: Readable2 } = await import("node:stream");
|
|
101
|
+
body = Readable2.from(fetchBody);
|
|
100
102
|
}
|
|
101
103
|
setHeaders(fetchResponse, nodeResponse);
|
|
102
104
|
if (body) {
|
|
@@ -218,7 +220,7 @@ function wrapResponse(nodeResponse, next) {
|
|
|
218
220
|
}
|
|
219
221
|
const middlewares = nodeResponse[pendingMiddlewaresSymbol];
|
|
220
222
|
delete nodeResponse[pendingMiddlewaresSymbol];
|
|
221
|
-
let response
|
|
223
|
+
let response;
|
|
222
224
|
try {
|
|
223
225
|
response = responseAdapter(nodeResponse, reader1);
|
|
224
226
|
for (const middleware of middlewares) {
|
|
@@ -276,7 +278,9 @@ function setHeaders(fetchResponse, nodeResponse, mirror = false) {
|
|
|
276
278
|
nodeResponse.setHeader(key, value);
|
|
277
279
|
});
|
|
278
280
|
if (mirror) {
|
|
279
|
-
nodeResponseHeaders.forEach((key) =>
|
|
281
|
+
nodeResponseHeaders.forEach((key) => {
|
|
282
|
+
nodeResponse.removeHeader(key);
|
|
283
|
+
});
|
|
280
284
|
}
|
|
281
285
|
}
|
|
282
286
|
|
|
@@ -379,10 +383,14 @@ function getContext(req) {
|
|
|
379
383
|
function getRuntime(request, response) {
|
|
380
384
|
return getAdapterRuntime("express", {
|
|
381
385
|
params: request.params,
|
|
386
|
+
// biome-ignore lint/suspicious/noExplicitAny: cast
|
|
382
387
|
req: request,
|
|
388
|
+
// biome-ignore lint/suspicious/noExplicitAny: cast
|
|
383
389
|
res: response,
|
|
384
390
|
express: Object.freeze({
|
|
391
|
+
// biome-ignore lint/suspicious/noExplicitAny: cast
|
|
385
392
|
req: request,
|
|
393
|
+
// biome-ignore lint/suspicious/noExplicitAny: cast
|
|
386
394
|
res: response
|
|
387
395
|
})
|
|
388
396
|
});
|
|
@@ -390,7 +398,7 @@ function getRuntime(request, response) {
|
|
|
390
398
|
|
|
391
399
|
// src/utils.ts
|
|
392
400
|
import { ServerResponse } from "node:http";
|
|
393
|
-
import { PassThrough, Readable
|
|
401
|
+
import { PassThrough, Readable } from "node:stream";
|
|
394
402
|
var statusCodesWithoutBody = [
|
|
395
403
|
100,
|
|
396
404
|
// Continue
|
|
@@ -413,7 +421,13 @@ function connectToWeb(handler) {
|
|
|
413
421
|
const { res, onReadable } = createServerResponse(req);
|
|
414
422
|
return new Promise(async (resolve, reject) => {
|
|
415
423
|
onReadable(({ readable, headers, statusCode }) => {
|
|
416
|
-
const responseBody = statusCodesWithoutBody.includes(statusCode) ? null :
|
|
424
|
+
const responseBody = statusCodesWithoutBody.includes(statusCode) ? null : "from" in ReadableStream ? (
|
|
425
|
+
// biome-ignore lint/suspicious/noExplicitAny: definition clash between Web and Node
|
|
426
|
+
ReadableStream.from(readable)
|
|
427
|
+
) : (
|
|
428
|
+
// biome-ignore lint/suspicious/noExplicitAny: definition clash between Web and Node
|
|
429
|
+
Readable.toWeb(readable)
|
|
430
|
+
);
|
|
417
431
|
resolve(
|
|
418
432
|
new Response(responseBody, {
|
|
419
433
|
status: statusCode,
|
|
@@ -443,7 +457,7 @@ function connectToWeb(handler) {
|
|
|
443
457
|
function createIncomingMessage(request) {
|
|
444
458
|
const parsedUrl = new URL(request.url, "http://localhost");
|
|
445
459
|
const pathnameAndQuery = (parsedUrl.pathname || "") + (parsedUrl.search || "");
|
|
446
|
-
const body = request.body ?
|
|
460
|
+
const body = request.body ? Readable.fromWeb(request.body) : Readable.from([]);
|
|
447
461
|
return Object.assign(body, {
|
|
448
462
|
url: pathnameAndQuery,
|
|
449
463
|
method: request.method,
|
|
@@ -458,7 +472,7 @@ function createServerResponse(incomingMessage) {
|
|
|
458
472
|
const handleReadable = () => {
|
|
459
473
|
if (handled) return;
|
|
460
474
|
handled = true;
|
|
461
|
-
cb({ readable:
|
|
475
|
+
cb({ readable: Readable.from(passThrough), headers: res.getHeaders(), statusCode: res.statusCode });
|
|
462
476
|
};
|
|
463
477
|
passThrough.once("readable", handleReadable);
|
|
464
478
|
passThrough.once("end", handleReadable);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@universal-middleware/express",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.20",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Express adapter for universal middlewares",
|
|
6
6
|
"files": [
|
|
@@ -13,23 +13,23 @@
|
|
|
13
13
|
"repository": "https://github.com/magne4000/universal-middleware",
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@universal-middleware/core": "^0.4.
|
|
16
|
+
"@universal-middleware/core": "^0.4.13"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@swc/core": "^1.13.
|
|
19
|
+
"@swc/core": "^1.13.5",
|
|
20
20
|
"@types/express": "^5.0.3",
|
|
21
21
|
"@types/express4": "npm:@types/express@^4.17.23",
|
|
22
|
-
"@types/node": "^20.19.
|
|
22
|
+
"@types/node": "^20.19.21",
|
|
23
23
|
"express": "^5.1.0",
|
|
24
24
|
"express4": "npm:express@^4.21.2",
|
|
25
25
|
"helmet": "^8.1.0",
|
|
26
26
|
"rimraf": "^6.0.1",
|
|
27
27
|
"tsup": "^8.5.0",
|
|
28
|
-
"tsx": "^4.20.
|
|
29
|
-
"typescript": "^5.
|
|
30
|
-
"vite": "^7.
|
|
28
|
+
"tsx": "^4.20.6",
|
|
29
|
+
"typescript": "^5.9.3",
|
|
30
|
+
"vite": "^7.1.11",
|
|
31
31
|
"vitest": "^3.2.4",
|
|
32
|
-
"@universal-middleware/tests": "0.0.
|
|
32
|
+
"@universal-middleware/tests": "0.0.1"
|
|
33
33
|
},
|
|
34
34
|
"sideEffects": false,
|
|
35
35
|
"keywords": [
|