@universal-middleware/express 0.4.24 → 0.4.25
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-OQZAACXZ.js +72 -0
- package/dist/index.d.ts +5 -61
- package/dist/index.js +16 -73
- package/dist/request-DIQC_37e.d.ts +65 -0
- package/dist/request.d.ts +4 -0
- package/dist/request.js +6 -0
- package/package.json +3 -2
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// src/const.ts
|
|
2
|
+
var requestSymbol = /* @__PURE__ */ Symbol.for("unRequest");
|
|
3
|
+
var pendingMiddlewaresSymbol = /* @__PURE__ */ Symbol.for("unPendingMiddlewares");
|
|
4
|
+
var wrappedResponseSymbol = /* @__PURE__ */ Symbol.for("unWrappedResponse");
|
|
5
|
+
var env = typeof globalThis.process?.env !== "undefined" ? globalThis.process.env : typeof import.meta?.env !== "undefined" ? import.meta.env : {};
|
|
6
|
+
|
|
7
|
+
// src/request.ts
|
|
8
|
+
var deno = typeof Deno !== "undefined";
|
|
9
|
+
var bun = typeof Bun !== "undefined";
|
|
10
|
+
function createRequestAdapter(options = {}) {
|
|
11
|
+
const { origin = env.ORIGIN, trustProxy = env.TRUST_PROXY === "1" } = options;
|
|
12
|
+
let { protocol: protocolOverride, host: hostOverride } = origin ? new URL(origin) : {};
|
|
13
|
+
if (protocolOverride) {
|
|
14
|
+
protocolOverride = protocolOverride.slice(0, -1);
|
|
15
|
+
}
|
|
16
|
+
let warned = false;
|
|
17
|
+
return function requestAdapter(req) {
|
|
18
|
+
if (req[requestSymbol]) {
|
|
19
|
+
return req[requestSymbol];
|
|
20
|
+
}
|
|
21
|
+
function parseForwardedHeader(name) {
|
|
22
|
+
return (headers[`x-forwarded-${name}`] || "").split(",", 1)[0].trim();
|
|
23
|
+
}
|
|
24
|
+
let headers = req.headers;
|
|
25
|
+
if (headers[":method"]) {
|
|
26
|
+
headers = Object.fromEntries(Object.entries(headers).filter(([key]) => !key.startsWith(":")));
|
|
27
|
+
}
|
|
28
|
+
const protocol = protocolOverride || trustProxy && parseForwardedHeader("proto") || req.protocol || // biome-ignore lint/suspicious/noExplicitAny: encrypted can exist in some express versions
|
|
29
|
+
req.socket?.encrypted && "https" || "http";
|
|
30
|
+
let host = hostOverride || trustProxy && parseForwardedHeader("host") || headers.host;
|
|
31
|
+
if (!host && !warned) {
|
|
32
|
+
console.warn(
|
|
33
|
+
"Could not automatically determine the origin host, using 'localhost'. Use the 'origin' option or the 'ORIGIN' environment variable to set the origin explicitly."
|
|
34
|
+
);
|
|
35
|
+
warned = true;
|
|
36
|
+
host = "localhost";
|
|
37
|
+
}
|
|
38
|
+
const request = new Request(`${protocol}://${host}${req.originalUrl ?? req.url}`, {
|
|
39
|
+
method: req.method,
|
|
40
|
+
headers,
|
|
41
|
+
body: convertBody(req),
|
|
42
|
+
// @ts-expect-error
|
|
43
|
+
duplex: "half"
|
|
44
|
+
});
|
|
45
|
+
req[requestSymbol] = request;
|
|
46
|
+
return request;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function convertBody(req) {
|
|
50
|
+
if (req.method === "GET" || req.method === "HEAD") {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (req.rawBody !== void 0) {
|
|
54
|
+
return req.rawBody;
|
|
55
|
+
}
|
|
56
|
+
if (!bun && !deno) {
|
|
57
|
+
return req;
|
|
58
|
+
}
|
|
59
|
+
return new ReadableStream({
|
|
60
|
+
start(controller) {
|
|
61
|
+
req.on("data", (chunk) => controller.enqueue(chunk));
|
|
62
|
+
req.on("end", () => controller.close());
|
|
63
|
+
req.on("error", (err) => controller.error(err));
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export {
|
|
69
|
+
pendingMiddlewaresSymbol,
|
|
70
|
+
wrappedResponseSymbol,
|
|
71
|
+
createRequestAdapter
|
|
72
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,68 +1,12 @@
|
|
|
1
|
+
import { Get, UniversalHandler, UniversalMiddleware, RuntimeAdapterTarget, UniversalRouter, UniversalRouterInterface, EnhancedMiddleware } from '@universal-middleware/core';
|
|
2
|
+
import { N as NodeAdapterHandlerOptions, a as NodeHandler, b as NodeAdapterMiddlewareOptions, c as NodeMiddleware, D as DecoratedRequest, d as DecoratedServerResponse } from './request-DIQC_37e.js';
|
|
3
|
+
export { e as createRequestAdapter } from './request-DIQC_37e.js';
|
|
1
4
|
import { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http';
|
|
2
|
-
import { Socket } from 'node:net';
|
|
3
|
-
import { contextSymbol, Awaitable, UniversalFn, UniversalHandler, UniversalMiddleware, Get, RuntimeAdapterTarget, UniversalRouter, UniversalRouterInterface, EnhancedMiddleware } from '@universal-middleware/core';
|
|
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';
|
|
8
|
+
import 'node:net';
|
|
7
9
|
|
|
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
|
-
declare const pendingMiddlewaresSymbol: unique symbol;
|
|
36
|
-
declare const wrappedResponseSymbol: unique symbol;
|
|
37
|
-
interface PossiblyEncryptedSocket extends Socket {
|
|
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
|
-
}
|
|
54
|
-
interface DecoratedServerResponse extends ServerResponse {
|
|
55
|
-
[pendingMiddlewaresSymbol]?: ((response: Response) => Awaitable<Response | undefined>)[];
|
|
56
|
-
[wrappedResponseSymbol]?: boolean;
|
|
57
|
-
}
|
|
58
|
-
/** Connect/Express style request listener/middleware */
|
|
59
|
-
type NodeMiddleware<In extends Universal.Context, Out extends Universal.Context> = UniversalFn<UniversalMiddleware<In, Out>, <R>(req: DecoratedRequest<In>, res: DecoratedServerResponse, next?: (err?: unknown) => void) => R>;
|
|
60
|
-
type NodeHandler<In extends Universal.Context> = UniversalFn<UniversalHandler<In>, <R>(req: DecoratedRequest<In>, res: DecoratedServerResponse, next?: (err?: unknown) => void) => R>;
|
|
61
|
-
/** Adapter options */
|
|
62
|
-
interface NodeAdapterHandlerOptions extends NodeRequestAdapterOptions {
|
|
63
|
-
}
|
|
64
|
-
interface NodeAdapterMiddlewareOptions extends NodeRequestAdapterOptions {
|
|
65
|
-
}
|
|
66
10
|
/**
|
|
67
11
|
* Creates a request handler to be passed to http.createServer() or used as a
|
|
68
12
|
* middleware in Connect-style frameworks like Express.
|
|
@@ -122,4 +66,4 @@ declare class UniversalExpressRouter<T extends App> extends UniversalRouter impl
|
|
|
122
66
|
}
|
|
123
67
|
declare function apply(app: Express, middlewares: EnhancedMiddlewareExpress[]): void;
|
|
124
68
|
|
|
125
|
-
export { type App, type ConnectMiddleware, type ConnectMiddlewareBoolean,
|
|
69
|
+
export { type App, type ConnectMiddleware, type ConnectMiddlewareBoolean, DecoratedRequest, DecoratedServerResponse, NodeHandler, NodeMiddleware, UniversalExpressRouter, type WebHandler, apply, connectToWeb, createHandler, createIncomingMessage, createMiddleware, createServerResponse, getContext, sendResponse };
|
package/dist/index.js
CHANGED
|
@@ -1,70 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRequestAdapter,
|
|
3
|
+
pendingMiddlewaresSymbol,
|
|
4
|
+
wrappedResponseSymbol
|
|
5
|
+
} from "./chunk-OQZAACXZ.js";
|
|
6
|
+
|
|
1
7
|
// src/common.ts
|
|
2
8
|
import { bindUniversal, contextSymbol, getAdapterRuntime, universalSymbol } from "@universal-middleware/core";
|
|
3
9
|
|
|
4
|
-
// src/request.ts
|
|
5
|
-
var deno = typeof Deno !== "undefined";
|
|
6
|
-
var bun = typeof Bun !== "undefined";
|
|
7
|
-
function createRequestAdapter(options = {}) {
|
|
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
|
-
}
|
|
64
|
-
|
|
65
10
|
// src/response.ts
|
|
66
11
|
import { nodeHeadersToWeb } from "@universal-middleware/core";
|
|
67
|
-
var
|
|
12
|
+
var deno = typeof Deno !== "undefined";
|
|
68
13
|
async function sendResponse(fetchResponse, nodeResponse) {
|
|
69
14
|
const fetchBody = fetchResponse.body;
|
|
70
15
|
let body = null;
|
|
@@ -74,7 +19,7 @@ async function sendResponse(fetchResponse, nodeResponse) {
|
|
|
74
19
|
body = fetchBody;
|
|
75
20
|
} else if (typeof fetchBody.pipeTo === "function") {
|
|
76
21
|
const { Readable: Readable2 } = await import("node:stream");
|
|
77
|
-
if (!
|
|
22
|
+
if (!deno && Readable2.fromWeb) {
|
|
78
23
|
body = Readable2.fromWeb(fetchBody);
|
|
79
24
|
} else {
|
|
80
25
|
const reader = fetchBody.getReader();
|
|
@@ -285,10 +230,6 @@ function setHeaders(fetchResponse, nodeResponse, mirror = false) {
|
|
|
285
230
|
}
|
|
286
231
|
|
|
287
232
|
// 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
233
|
function nextOr404(res, next) {
|
|
293
234
|
if (next) {
|
|
294
235
|
next();
|
|
@@ -396,6 +337,14 @@ function getRuntime(request, response) {
|
|
|
396
337
|
});
|
|
397
338
|
}
|
|
398
339
|
|
|
340
|
+
// src/router.ts
|
|
341
|
+
import {
|
|
342
|
+
apply as applyCore,
|
|
343
|
+
getUniversal,
|
|
344
|
+
UniversalRouter,
|
|
345
|
+
universalSymbol as universalSymbol2
|
|
346
|
+
} from "@universal-middleware/core";
|
|
347
|
+
|
|
399
348
|
// src/utils.ts
|
|
400
349
|
import { ServerResponse } from "node:http";
|
|
401
350
|
import { PassThrough, Readable } from "node:stream";
|
|
@@ -538,12 +487,6 @@ function isExpressV5(app) {
|
|
|
538
487
|
}
|
|
539
488
|
|
|
540
489
|
// src/router.ts
|
|
541
|
-
import {
|
|
542
|
-
apply as applyCore,
|
|
543
|
-
getUniversal,
|
|
544
|
-
UniversalRouter,
|
|
545
|
-
universalSymbol as universalSymbol2
|
|
546
|
-
} from "@universal-middleware/core";
|
|
547
490
|
var UniversalExpressRouter = class extends UniversalRouter {
|
|
548
491
|
#app;
|
|
549
492
|
constructor(app) {
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { Socket } from 'node:net';
|
|
3
|
+
import { contextSymbol, Awaitable, UniversalFn, UniversalHandler, UniversalMiddleware } from '@universal-middleware/core';
|
|
4
|
+
|
|
5
|
+
declare const requestSymbol: unique symbol;
|
|
6
|
+
declare const pendingMiddlewaresSymbol: unique symbol;
|
|
7
|
+
declare const wrappedResponseSymbol: unique symbol;
|
|
8
|
+
|
|
9
|
+
interface PossiblyEncryptedSocket extends Socket {
|
|
10
|
+
encrypted?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* `IncomingMessage` possibly augmented by Express-specific
|
|
14
|
+
* `ip` and `protocol` properties.
|
|
15
|
+
*/
|
|
16
|
+
interface DecoratedRequest<C extends Universal.Context = Universal.Context> extends Omit<IncomingMessage, "socket"> {
|
|
17
|
+
ip?: string;
|
|
18
|
+
protocol?: string;
|
|
19
|
+
socket?: PossiblyEncryptedSocket;
|
|
20
|
+
rawBody?: any;
|
|
21
|
+
originalUrl?: string;
|
|
22
|
+
params?: Record<string, string>;
|
|
23
|
+
[contextSymbol]?: C;
|
|
24
|
+
[requestSymbol]?: Request;
|
|
25
|
+
}
|
|
26
|
+
interface DecoratedServerResponse extends ServerResponse {
|
|
27
|
+
[pendingMiddlewaresSymbol]?: ((response: Response) => Awaitable<Response | undefined>)[];
|
|
28
|
+
[wrappedResponseSymbol]?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/** Connect/Express style request listener/middleware */
|
|
31
|
+
type NodeMiddleware<In extends Universal.Context, Out extends Universal.Context> = UniversalFn<UniversalMiddleware<In, Out>, <R>(req: DecoratedRequest<In>, res: DecoratedServerResponse, next?: (err?: unknown) => void) => R>;
|
|
32
|
+
type NodeHandler<In extends Universal.Context> = UniversalFn<UniversalHandler<In>, <R>(req: DecoratedRequest<In>, res: DecoratedServerResponse, next?: (err?: unknown) => void) => R>;
|
|
33
|
+
/** Adapter options */
|
|
34
|
+
interface NodeAdapterHandlerOptions extends NodeRequestAdapterOptions {
|
|
35
|
+
}
|
|
36
|
+
interface NodeAdapterMiddlewareOptions extends NodeRequestAdapterOptions {
|
|
37
|
+
}
|
|
38
|
+
/** Adapter options */
|
|
39
|
+
interface NodeRequestAdapterOptions {
|
|
40
|
+
/**
|
|
41
|
+
* Set the origin part of the URL to a constant value.
|
|
42
|
+
* It defaults to `process.env.ORIGIN`. If neither is set,
|
|
43
|
+
* the origin is computed from the protocol and hostname.
|
|
44
|
+
* To determine the protocol, `req.protocol` is tried first.
|
|
45
|
+
* If `trustProxy` is set, `X-Forwarded-Proto` header is used.
|
|
46
|
+
* Otherwise, `req.socket.encrypted` is used.
|
|
47
|
+
* To determine the hostname, `X-Forwarded-Host`
|
|
48
|
+
* (if `trustProxy` is set) or `Host` header is used.
|
|
49
|
+
*/
|
|
50
|
+
origin?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Whether to trust `X-Forwarded-*` headers. `X-Forwarded-Proto`
|
|
53
|
+
* and `X-Forwarded-Host` are used to determine the origin when
|
|
54
|
+
* `origin` and `process.env.ORIGIN` are not set. `X-Forwarded-For`
|
|
55
|
+
* is used to determine the IP address. The leftmost values are used
|
|
56
|
+
* if multiple values are set. Defaults to true if `process.env.TRUST_PROXY`
|
|
57
|
+
* is set to `1`, otherwise false.
|
|
58
|
+
*/
|
|
59
|
+
trustProxy?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Create a function that converts a Node HTTP request into a fetch API `Request` object */
|
|
63
|
+
declare function createRequestAdapter(options?: NodeRequestAdapterOptions): (req: DecoratedRequest) => Request;
|
|
64
|
+
|
|
65
|
+
export { type DecoratedRequest as D, type NodeAdapterHandlerOptions as N, type NodeHandler as a, type NodeAdapterMiddlewareOptions as b, type NodeMiddleware as c, type DecoratedServerResponse as d, createRequestAdapter as e };
|
package/dist/request.js
ADDED
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@universal-middleware/express",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.25",
|
|
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",
|