@universal-middleware/express 0.6.0 → 0.6.1
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.js +10 -11
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRequestAdapter, expressOrigin } from "./request.js";
|
|
2
|
-
import { UniversalRouter, apply as apply$1, bindUniversal, cancelReplacedBody, contextSymbol, getAdapterRuntime, getUniversal, universalSymbol } from "@universal-middleware/core";
|
|
2
|
+
import { UniversalRouter, apply as apply$1, bindUniversal, cancelReplacedBody, contextSymbol, getAdapterRuntime, getUniversal, universalSymbol, withMutableHeaders } from "@universal-middleware/core";
|
|
3
3
|
import { BadRequestError, requestSymbol, responseAdapter, sendResponse, setResponseHeaders } from "@universal-middleware/node";
|
|
4
4
|
import { EventEmitter, once } from "node:events";
|
|
5
5
|
import { ServerResponse } from "node:http";
|
|
@@ -200,6 +200,7 @@ function wrapResponse(nodeResponse, next) {
|
|
|
200
200
|
try {
|
|
201
201
|
response = responseAdapter(nodeResponse, captured.readable);
|
|
202
202
|
for (const middleware of middlewares) {
|
|
203
|
+
response = withMutableHeaders(response);
|
|
203
204
|
const tmp = await middleware(response);
|
|
204
205
|
cancelReplacedBody(response, tmp);
|
|
205
206
|
if (tmp) response = tmp;
|
|
@@ -389,19 +390,16 @@ const statusCodesWithoutBody = [
|
|
|
389
390
|
*/
|
|
390
391
|
function connectToWeb(handler) {
|
|
391
392
|
return async (request, context, runtime) => {
|
|
392
|
-
const
|
|
393
|
-
const req = realReq ?? createIncomingMessage(request);
|
|
393
|
+
const req = (runtime && "req" in runtime && runtime.req || request.runtime?.node?.req) ?? createIncomingMessage(request);
|
|
394
394
|
const callerContext = context ?? request.context;
|
|
395
395
|
if (callerContext) req[contextSymbol] = callerContext;
|
|
396
396
|
const { res, onReadable } = createServerResponse(req);
|
|
397
|
-
const stopForwardingAbort =
|
|
397
|
+
const stopForwardingAbort = forwardAbort(request.signal, req, res);
|
|
398
398
|
return new Promise(async (resolve, reject) => {
|
|
399
399
|
onReadable(({ readable, headers, statusCode, statusMessage }) => {
|
|
400
400
|
const hasBody = !statusCodesWithoutBody.includes(statusCode);
|
|
401
|
-
if (stopForwardingAbort)
|
|
402
|
-
|
|
403
|
-
else stopForwardingAbort();
|
|
404
|
-
}
|
|
401
|
+
if (hasBody) readable.once("close", stopForwardingAbort);
|
|
402
|
+
else stopForwardingAbort();
|
|
405
403
|
resolve(new Response(hasBody ? toWebStream(readable) : null, {
|
|
406
404
|
status: statusCode,
|
|
407
405
|
statusText: statusMessage,
|
|
@@ -409,14 +407,14 @@ function connectToWeb(handler) {
|
|
|
409
407
|
}));
|
|
410
408
|
});
|
|
411
409
|
const next = (error) => {
|
|
412
|
-
stopForwardingAbort
|
|
410
|
+
stopForwardingAbort();
|
|
413
411
|
if (error) reject(error instanceof Error ? error : new Error(String(error)));
|
|
414
412
|
else resolve(void 0);
|
|
415
413
|
};
|
|
416
414
|
try {
|
|
417
415
|
if (await handler(req, res, next) === false) {
|
|
418
416
|
res.destroy();
|
|
419
|
-
stopForwardingAbort
|
|
417
|
+
stopForwardingAbort();
|
|
420
418
|
resolve(void 0);
|
|
421
419
|
}
|
|
422
420
|
} catch (e) {
|
|
@@ -491,6 +489,7 @@ function createServerResponse(incomingMessage) {
|
|
|
491
489
|
});
|
|
492
490
|
res.write = passThrough.write.bind(passThrough);
|
|
493
491
|
res.end = passThrough.end.bind(passThrough);
|
|
492
|
+
Object.defineProperty(res, "writableEnded", { get: () => passThrough.writableEnded });
|
|
494
493
|
res.writeHead = function writeHead(statusCode, statusMessage, headers) {
|
|
495
494
|
setHead(res, statusCode, statusMessage, headers);
|
|
496
495
|
return res;
|
|
@@ -501,7 +500,7 @@ function createServerResponse(incomingMessage) {
|
|
|
501
500
|
};
|
|
502
501
|
}
|
|
503
502
|
/**
|
|
504
|
-
* Bridges a Web `AbortSignal` to
|
|
503
|
+
* Bridges a Web `AbortSignal` to `connectToWeb`'s `req`/`res`, and returns a function that stops it.
|
|
505
504
|
* The request adapter aborts `request.signal` on `res`'s "close", which a socketless `ServerResponse` never emits.
|
|
506
505
|
*/
|
|
507
506
|
function forwardAbort(signal, req, res) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@universal-middleware/express",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Express adapter for universal middlewares",
|
|
6
6
|
"files": [
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"node": ">=22"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@universal-middleware/core": "^0.7.
|
|
23
|
+
"@universal-middleware/core": "^0.7.2",
|
|
24
24
|
"@universal-middleware/node": "^0.2.9"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
"express": "^5.3.0",
|
|
31
31
|
"express4": "npm:express@^4.22.3",
|
|
32
32
|
"helmet": "^8.3.0",
|
|
33
|
-
"tsdown": "^0.23.
|
|
33
|
+
"tsdown": "^0.23.1",
|
|
34
34
|
"tsx": "^4.23.15",
|
|
35
35
|
"typescript": "^7.0.2",
|
|
36
36
|
"vite": "^8.3.4",
|
|
37
|
-
"vitest": "^5.0.
|
|
37
|
+
"vitest": "^5.0.5",
|
|
38
38
|
"@universal-middleware/tests": "0.0.2",
|
|
39
39
|
"@universal-middleware/tsdown-config": "0.0.0"
|
|
40
40
|
},
|