@universal-middleware/express 0.4.24 → 0.4.26
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/chunk-N42HUXDK.js +8 -0
- package/dist/index.d.ts +7 -53
- package/dist/index.js +18 -175
- package/dist/request.d.ts +1 -0
- package/dist/request.js +10 -0
- package/package.json +7 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,56 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { Awaitable, UniversalFn, UniversalHandler, UniversalMiddleware, Get, RuntimeAdapterTarget, UniversalRouter, UniversalRouterInterface, EnhancedMiddleware } from '@universal-middleware/core';
|
|
2
|
+
import { ServerResponse, IncomingMessage, OutgoingHttpHeaders } from 'node:http';
|
|
3
|
+
import { DecoratedRequest, NodeRequestAdapterOptions } from '@universal-middleware/node';
|
|
4
|
+
export { DecoratedRequest, createRequestAdapter, sendResponse } from '@universal-middleware/node';
|
|
4
5
|
import { Readable } from 'node:stream';
|
|
5
6
|
import { Express as Express$2 } from 'express';
|
|
6
7
|
import { Express as Express$1 } from 'express4';
|
|
7
8
|
|
|
8
|
-
/** Adapter options */
|
|
9
|
-
interface NodeRequestAdapterOptions {
|
|
10
|
-
/**
|
|
11
|
-
* Set the origin part of the URL to a constant value.
|
|
12
|
-
* It defaults to `process.env.ORIGIN`. If neither is set,
|
|
13
|
-
* the origin is computed from the protocol and hostname.
|
|
14
|
-
* To determine the protocol, `req.protocol` is tried first.
|
|
15
|
-
* If `trustProxy` is set, `X-Forwarded-Proto` header is used.
|
|
16
|
-
* Otherwise, `req.socket.encrypted` is used.
|
|
17
|
-
* To determine the hostname, `X-Forwarded-Host`
|
|
18
|
-
* (if `trustProxy` is set) or `Host` header is used.
|
|
19
|
-
*/
|
|
20
|
-
origin?: string;
|
|
21
|
-
/**
|
|
22
|
-
* Whether to trust `X-Forwarded-*` headers. `X-Forwarded-Proto`
|
|
23
|
-
* and `X-Forwarded-Host` are used to determine the origin when
|
|
24
|
-
* `origin` and `process.env.ORIGIN` are not set. `X-Forwarded-For`
|
|
25
|
-
* is used to determine the IP address. The leftmost values are used
|
|
26
|
-
* if multiple values are set. Defaults to true if `process.env.TRUST_PROXY`
|
|
27
|
-
* is set to `1`, otherwise false.
|
|
28
|
-
*/
|
|
29
|
-
trustProxy?: boolean;
|
|
30
|
-
}
|
|
31
|
-
/** Create a function that converts a Node HTTP request into a fetch API `Request` object */
|
|
32
|
-
declare function createRequestAdapter(options?: NodeRequestAdapterOptions): (req: DecoratedRequest) => Request;
|
|
33
|
-
|
|
34
|
-
declare const requestSymbol: unique symbol;
|
|
35
9
|
declare const pendingMiddlewaresSymbol: unique symbol;
|
|
36
10
|
declare const wrappedResponseSymbol: unique symbol;
|
|
37
|
-
|
|
38
|
-
encrypted?: boolean;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* `IncomingMessage` possibly augmented by Express-specific
|
|
42
|
-
* `ip` and `protocol` properties.
|
|
43
|
-
*/
|
|
44
|
-
interface DecoratedRequest<C extends Universal.Context = Universal.Context> extends Omit<IncomingMessage, "socket"> {
|
|
45
|
-
ip?: string;
|
|
46
|
-
protocol?: string;
|
|
47
|
-
socket?: PossiblyEncryptedSocket;
|
|
48
|
-
rawBody?: any;
|
|
49
|
-
originalUrl?: string;
|
|
50
|
-
params?: Record<string, string>;
|
|
51
|
-
[contextSymbol]?: C;
|
|
52
|
-
[requestSymbol]?: Request;
|
|
53
|
-
}
|
|
11
|
+
|
|
54
12
|
interface DecoratedServerResponse extends ServerResponse {
|
|
55
13
|
[pendingMiddlewaresSymbol]?: ((response: Response) => Awaitable<Response | undefined>)[];
|
|
56
14
|
[wrappedResponseSymbol]?: boolean;
|
|
@@ -63,6 +21,7 @@ interface NodeAdapterHandlerOptions extends NodeRequestAdapterOptions {
|
|
|
63
21
|
}
|
|
64
22
|
interface NodeAdapterMiddlewareOptions extends NodeRequestAdapterOptions {
|
|
65
23
|
}
|
|
24
|
+
|
|
66
25
|
/**
|
|
67
26
|
* Creates a request handler to be passed to http.createServer() or used as a
|
|
68
27
|
* middleware in Connect-style frameworks like Express.
|
|
@@ -74,11 +33,6 @@ declare function createHandler<T extends unknown[], InContext extends Universal.
|
|
|
74
33
|
declare function createMiddleware<T extends unknown[], InContext extends Universal.Context, OutContext extends Universal.Context>(middlewareFactory: Get<T, UniversalMiddleware<InContext, OutContext>>, options?: NodeAdapterMiddlewareOptions): Get<T, NodeMiddleware<InContext, OutContext>>;
|
|
75
34
|
declare function getContext<InContext extends Universal.Context = Universal.Context>(req: DecoratedRequest): InContext;
|
|
76
35
|
|
|
77
|
-
/**
|
|
78
|
-
* Send a fetch API Response into a Node.js HTTP response stream.
|
|
79
|
-
*/
|
|
80
|
-
declare function sendResponse(fetchResponse: Response, nodeResponse: DecoratedServerResponse): Promise<void>;
|
|
81
|
-
|
|
82
36
|
type Express = Express$1 | Express$2;
|
|
83
37
|
type ConnectMiddleware = (req: IncomingMessage, res: ServerResponse, next?: (err?: unknown) => void) => void | Promise<void>;
|
|
84
38
|
type ConnectMiddlewareBoolean = (req: IncomingMessage, res: ServerResponse, next?: (err?: unknown) => void) => boolean | Promise<boolean>;
|
|
@@ -122,4 +76,4 @@ declare class UniversalExpressRouter<T extends App> extends UniversalRouter impl
|
|
|
122
76
|
}
|
|
123
77
|
declare function apply(app: Express, middlewares: EnhancedMiddlewareExpress[]): void;
|
|
124
78
|
|
|
125
|
-
export { type App, type ConnectMiddleware, type ConnectMiddlewareBoolean, type
|
|
79
|
+
export { type App, type ConnectMiddleware, type ConnectMiddlewareBoolean, type DecoratedServerResponse, type NodeHandler, type NodeMiddleware, UniversalExpressRouter, type WebHandler, apply, connectToWeb, createHandler, createIncomingMessage, createMiddleware, createServerResponse, getContext };
|
package/dist/index.js
CHANGED
|
@@ -1,127 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRequestAdapter
|
|
3
|
+
} from "./chunk-N42HUXDK.js";
|
|
4
|
+
|
|
1
5
|
// src/common.ts
|
|
2
6
|
import { bindUniversal, contextSymbol, getAdapterRuntime, universalSymbol } from "@universal-middleware/core";
|
|
3
7
|
|
|
4
|
-
// src/
|
|
5
|
-
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
const { origin = env.ORIGIN, trustProxy = env.TRUST_PROXY === "1" } = options;
|
|
9
|
-
let { protocol: protocolOverride, host: hostOverride } = origin ? new URL(origin) : {};
|
|
10
|
-
if (protocolOverride) {
|
|
11
|
-
protocolOverride = protocolOverride.slice(0, -1);
|
|
12
|
-
}
|
|
13
|
-
let warned = false;
|
|
14
|
-
return function requestAdapter(req) {
|
|
15
|
-
if (req[requestSymbol]) {
|
|
16
|
-
return req[requestSymbol];
|
|
17
|
-
}
|
|
18
|
-
function parseForwardedHeader(name) {
|
|
19
|
-
return (headers[`x-forwarded-${name}`] || "").split(",", 1)[0].trim();
|
|
20
|
-
}
|
|
21
|
-
let headers = req.headers;
|
|
22
|
-
if (headers[":method"]) {
|
|
23
|
-
headers = Object.fromEntries(Object.entries(headers).filter(([key]) => !key.startsWith(":")));
|
|
24
|
-
}
|
|
25
|
-
const protocol = protocolOverride || trustProxy && parseForwardedHeader("proto") || req.protocol || // biome-ignore lint/suspicious/noExplicitAny: encrypted can exist in some express versions
|
|
26
|
-
req.socket?.encrypted && "https" || "http";
|
|
27
|
-
let host = hostOverride || trustProxy && parseForwardedHeader("host") || headers.host;
|
|
28
|
-
if (!host && !warned) {
|
|
29
|
-
console.warn(
|
|
30
|
-
"Could not automatically determine the origin host, using 'localhost'. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly."
|
|
31
|
-
);
|
|
32
|
-
warned = true;
|
|
33
|
-
host = "localhost";
|
|
34
|
-
}
|
|
35
|
-
const request = new Request(`${protocol}://${host}${req.originalUrl ?? req.url}`, {
|
|
36
|
-
method: req.method,
|
|
37
|
-
headers,
|
|
38
|
-
body: convertBody(req),
|
|
39
|
-
// @ts-expect-error
|
|
40
|
-
duplex: "half"
|
|
41
|
-
});
|
|
42
|
-
req[requestSymbol] = request;
|
|
43
|
-
return request;
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
function convertBody(req) {
|
|
47
|
-
if (req.method === "GET" || req.method === "HEAD") {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
if (req.rawBody !== void 0) {
|
|
51
|
-
return req.rawBody;
|
|
52
|
-
}
|
|
53
|
-
if (!bun && !deno) {
|
|
54
|
-
return req;
|
|
55
|
-
}
|
|
56
|
-
return new ReadableStream({
|
|
57
|
-
start(controller) {
|
|
58
|
-
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
59
|
-
req.on("end", () => controller.close());
|
|
60
|
-
req.on("error", (err) => controller.error(err));
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}
|
|
8
|
+
// src/const.ts
|
|
9
|
+
import { env, requestSymbol } from "@universal-middleware/node";
|
|
10
|
+
var pendingMiddlewaresSymbol = /* @__PURE__ */ Symbol.for("unPendingMiddlewares");
|
|
11
|
+
var wrappedResponseSymbol = /* @__PURE__ */ Symbol.for("unWrappedResponse");
|
|
64
12
|
|
|
65
13
|
// src/response.ts
|
|
66
|
-
import {
|
|
67
|
-
var deno2 = typeof Deno !== "undefined";
|
|
68
|
-
async function sendResponse(fetchResponse, nodeResponse) {
|
|
69
|
-
const fetchBody = fetchResponse.body;
|
|
70
|
-
let body = null;
|
|
71
|
-
if (!fetchBody) {
|
|
72
|
-
body = null;
|
|
73
|
-
} else if (typeof fetchBody.pipe === "function") {
|
|
74
|
-
body = fetchBody;
|
|
75
|
-
} else if (typeof fetchBody.pipeTo === "function") {
|
|
76
|
-
const { Readable: Readable2 } = await import("node:stream");
|
|
77
|
-
if (!deno2 && Readable2.fromWeb) {
|
|
78
|
-
body = Readable2.fromWeb(fetchBody);
|
|
79
|
-
} else {
|
|
80
|
-
const reader = fetchBody.getReader();
|
|
81
|
-
body = new Readable2({
|
|
82
|
-
async read() {
|
|
83
|
-
try {
|
|
84
|
-
const { done, value } = await reader.read();
|
|
85
|
-
if (done) {
|
|
86
|
-
this.push(null);
|
|
87
|
-
} else {
|
|
88
|
-
const canContinue = this.push(value);
|
|
89
|
-
if (!canContinue) {
|
|
90
|
-
reader.releaseLock();
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
} catch (e) {
|
|
94
|
-
this.destroy(e);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
} else if (fetchBody) {
|
|
100
|
-
const { Readable: Readable2 } = await import("node:stream");
|
|
101
|
-
body = Readable2.from(fetchBody);
|
|
102
|
-
}
|
|
103
|
-
setHeaders(fetchResponse, nodeResponse);
|
|
104
|
-
if (body) {
|
|
105
|
-
body.pipe(nodeResponse);
|
|
106
|
-
await new Promise((resolve, reject) => {
|
|
107
|
-
body.on("error", (err) => {
|
|
108
|
-
nodeResponse.destroy(err);
|
|
109
|
-
reject(err);
|
|
110
|
-
});
|
|
111
|
-
nodeResponse.on("error", (err) => {
|
|
112
|
-
body.destroy(err);
|
|
113
|
-
reject(err);
|
|
114
|
-
});
|
|
115
|
-
nodeResponse.on("finish", resolve);
|
|
116
|
-
nodeResponse.on("drain", () => {
|
|
117
|
-
body.resume();
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
} else {
|
|
121
|
-
nodeResponse.setHeader("content-length", "0");
|
|
122
|
-
nodeResponse.end();
|
|
123
|
-
}
|
|
124
|
-
}
|
|
14
|
+
import { responseAdapter, sendResponse, setResponseHeaders } from "@universal-middleware/node";
|
|
125
15
|
function createTransformStream() {
|
|
126
16
|
const textEncoder = new TextEncoder();
|
|
127
17
|
return new TransformStream({
|
|
@@ -179,30 +69,6 @@ function overrideWriteHead(nodeResponse, callback) {
|
|
|
179
69
|
}
|
|
180
70
|
};
|
|
181
71
|
}
|
|
182
|
-
function getFullUrl(pathnameOrFull, req) {
|
|
183
|
-
try {
|
|
184
|
-
return new URL(pathnameOrFull).href;
|
|
185
|
-
} catch {
|
|
186
|
-
const protocol = req.socket?.encrypted || req.headers["x-forwarded-proto"] === "https" ? "https" : "http";
|
|
187
|
-
const host = req.headers["x-forwarded-host"] || req.headers.host || "localhost";
|
|
188
|
-
const baseUrl = `${protocol}://${host}`;
|
|
189
|
-
return new URL(pathnameOrFull, baseUrl).href;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
function responseAdapter(nodeResponse, bodyInit) {
|
|
193
|
-
if ([301, 302, 303, 307, 308].includes(nodeResponse.statusCode) && nodeResponse.req) {
|
|
194
|
-
const location = nodeResponse.getHeader("location");
|
|
195
|
-
if (location) {
|
|
196
|
-
const fullUrl = getFullUrl(location, nodeResponse.req);
|
|
197
|
-
return Response.redirect(fullUrl, nodeResponse.statusCode);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return new Response([204, 304].includes(nodeResponse.statusCode) ? null : bodyInit, {
|
|
201
|
-
status: nodeResponse.statusCode,
|
|
202
|
-
statusText: nodeResponse.statusMessage,
|
|
203
|
-
headers: nodeHeadersToWeb(nodeResponse.getHeaders())
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
72
|
function wrapResponse(nodeResponse, next) {
|
|
207
73
|
if (nodeResponse[wrappedResponseSymbol]) return;
|
|
208
74
|
nodeResponse[wrappedResponseSymbol] = true;
|
|
@@ -241,7 +107,7 @@ function wrapResponse(nodeResponse, next) {
|
|
|
241
107
|
}
|
|
242
108
|
if (!response) return;
|
|
243
109
|
const readableToOriginal = response.body ?? reader2;
|
|
244
|
-
|
|
110
|
+
setResponseHeaders(response, nodeResponse, true);
|
|
245
111
|
original.writeHead.restore();
|
|
246
112
|
nodeResponse.flushHeaders();
|
|
247
113
|
const wait = readableToOriginal.pipeTo(
|
|
@@ -262,33 +128,8 @@ function wrapResponse(nodeResponse, next) {
|
|
|
262
128
|
original.end.restore();
|
|
263
129
|
}
|
|
264
130
|
}
|
|
265
|
-
function setHeaders(fetchResponse, nodeResponse, mirror = false) {
|
|
266
|
-
nodeResponse.statusCode = fetchResponse.status;
|
|
267
|
-
if (fetchResponse.statusText) {
|
|
268
|
-
nodeResponse.statusMessage = fetchResponse.statusText;
|
|
269
|
-
}
|
|
270
|
-
const nodeResponseHeaders = new Set(Object.keys(nodeResponse.getHeaders()));
|
|
271
|
-
const setCookie = fetchResponse.headers.getSetCookie();
|
|
272
|
-
for (const cookie of setCookie) {
|
|
273
|
-
nodeResponse.appendHeader("set-cookie", cookie);
|
|
274
|
-
}
|
|
275
|
-
fetchResponse.headers.forEach((value, key) => {
|
|
276
|
-
nodeResponseHeaders.delete(key);
|
|
277
|
-
if (key === "set-cookie") return;
|
|
278
|
-
nodeResponse.setHeader(key, value);
|
|
279
|
-
});
|
|
280
|
-
if (mirror) {
|
|
281
|
-
nodeResponseHeaders.forEach((key) => {
|
|
282
|
-
nodeResponse.removeHeader(key);
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
131
|
|
|
287
132
|
// src/common.ts
|
|
288
|
-
var requestSymbol = /* @__PURE__ */ Symbol.for("unRequest");
|
|
289
|
-
var pendingMiddlewaresSymbol = /* @__PURE__ */ Symbol.for("unPendingMiddlewares");
|
|
290
|
-
var wrappedResponseSymbol = /* @__PURE__ */ Symbol.for("unWrappedResponse");
|
|
291
|
-
var env = typeof globalThis.process?.env !== "undefined" ? globalThis.process.env : typeof import.meta?.env !== "undefined" ? import.meta.env : {};
|
|
292
133
|
function nextOr404(res, next) {
|
|
293
134
|
if (next) {
|
|
294
135
|
next();
|
|
@@ -396,6 +237,14 @@ function getRuntime(request, response) {
|
|
|
396
237
|
});
|
|
397
238
|
}
|
|
398
239
|
|
|
240
|
+
// src/router.ts
|
|
241
|
+
import {
|
|
242
|
+
apply as applyCore,
|
|
243
|
+
getUniversal,
|
|
244
|
+
UniversalRouter,
|
|
245
|
+
universalSymbol as universalSymbol2
|
|
246
|
+
} from "@universal-middleware/core";
|
|
247
|
+
|
|
399
248
|
// src/utils.ts
|
|
400
249
|
import { ServerResponse } from "node:http";
|
|
401
250
|
import { PassThrough, Readable } from "node:stream";
|
|
@@ -538,12 +387,6 @@ function isExpressV5(app) {
|
|
|
538
387
|
}
|
|
539
388
|
|
|
540
389
|
// src/router.ts
|
|
541
|
-
import {
|
|
542
|
-
apply as applyCore,
|
|
543
|
-
getUniversal,
|
|
544
|
-
UniversalRouter,
|
|
545
|
-
universalSymbol as universalSymbol2
|
|
546
|
-
} from "@universal-middleware/core";
|
|
547
390
|
var UniversalExpressRouter = class extends UniversalRouter {
|
|
548
391
|
#app;
|
|
549
392
|
constructor(app) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DecoratedRequest, NodeRequestAdapterOptions, PossiblyEncryptedSocket, createRequestAdapter, env, requestSymbol } from '@universal-middleware/node';
|
package/dist/request.js
ADDED
package/package.json
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@universal-middleware/express",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.26",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Express adapter for universal middlewares",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist"
|
|
8
8
|
],
|
|
9
9
|
"exports": {
|
|
10
|
-
".": "./dist/index.js"
|
|
10
|
+
".": "./dist/index.js",
|
|
11
|
+
"./request": "./dist/request.js"
|
|
11
12
|
},
|
|
12
13
|
"author": "Joël Charles <joel.charles91@gmail.com>",
|
|
13
14
|
"repository": "https://github.com/magne4000/universal-middleware",
|
|
14
15
|
"license": "MIT",
|
|
15
16
|
"dependencies": {
|
|
16
|
-
"@universal-middleware/core": "^0.4.17"
|
|
17
|
+
"@universal-middleware/core": "^0.4.17",
|
|
18
|
+
"@universal-middleware/node": "^0.1.0"
|
|
17
19
|
},
|
|
18
20
|
"devDependencies": {
|
|
19
|
-
"@swc/core": "^1.15.
|
|
21
|
+
"@swc/core": "^1.15.18",
|
|
20
22
|
"@types/express": "^5.0.6",
|
|
21
23
|
"@types/express4": "npm:@types/express@^4.17.25",
|
|
22
|
-
"@types/node": "^20.19.
|
|
24
|
+
"@types/node": "^20.19.35",
|
|
23
25
|
"express": "^5.2.1",
|
|
24
26
|
"express4": "npm:express@^4.22.1",
|
|
25
27
|
"helmet": "^8.1.0",
|