@universal-middleware/express 0.2.3 → 0.2.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Joël Charles
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -69,7 +69,7 @@ declare function createHandler<T extends unknown[]>(handlerFactory: Get<T, Unive
69
69
  * Creates a middleware to be passed to Connect-style frameworks like Express
70
70
  */
71
71
  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<OutContext>>;
72
- declare function getContext<InContext extends Universal.Context = Universal.Context>(req: DecoratedRequest): InContext | undefined;
72
+ declare function getContext<InContext extends Universal.Context = Universal.Context>(req: DecoratedRequest): InContext;
73
73
 
74
74
  /**
75
75
  * Send a fetch API Response into a Node.js HTTP response stream.
package/dist/index.js CHANGED
@@ -13,13 +13,11 @@ function createRequestAdapter(options = {}) {
13
13
  return req[requestSymbol];
14
14
  }
15
15
  function parseForwardedHeader(name) {
16
- return (headers["x-forwarded-" + name] || "").split(",", 1)[0].trim();
16
+ return (headers[`x-forwarded-${name}`] || "").split(",", 1)[0].trim();
17
17
  }
18
18
  let headers = req.headers;
19
19
  if (headers[":method"]) {
20
- headers = Object.fromEntries(
21
- Object.entries(headers).filter(([key]) => !key.startsWith(":"))
22
- );
20
+ headers = Object.fromEntries(Object.entries(headers).filter(([key]) => !key.startsWith(":")));
23
21
  }
24
22
  const protocol = protocolOverride || req.protocol || trustProxy && parseForwardedHeader("proto") || req.socket?.encrypted && "https" || "http";
25
23
  let host = hostOverride || trustProxy && parseForwardedHeader("host") || headers.host;
@@ -30,16 +28,13 @@ function createRequestAdapter(options = {}) {
30
28
  warned = true;
31
29
  host = "localhost";
32
30
  }
33
- const request = new Request(
34
- protocol + "://" + host + (req.originalUrl ?? req.url),
35
- {
36
- method: req.method,
37
- headers,
38
- body: convertBody(req),
39
- // @ts-ignore
40
- duplex: "half"
41
- }
42
- );
31
+ const request = new Request(`${protocol}://${host}${req.originalUrl ?? req.url}`, {
32
+ method: req.method,
33
+ headers,
34
+ body: convertBody(req),
35
+ // @ts-ignore
36
+ duplex: "half"
37
+ });
43
38
  req[requestSymbol] = request;
44
39
  return request;
45
40
  };
@@ -137,9 +132,7 @@ function wrapResponse(nodeResponse) {
137
132
  )
138
133
  );
139
134
  if (response.body) {
140
- throw new Error(
141
- "Replacing the Response body is currently not supported for connect-like servers"
142
- );
135
+ throw new Error("Replacing the Response body is currently not supported for connect-like servers");
143
136
  }
144
137
  setHeaders(response, nodeResponse, true);
145
138
  resolve();
@@ -227,7 +220,8 @@ function createMiddleware(middlewareFactory, options = {}) {
227
220
  );
228
221
  if (!response) {
229
222
  return next?.();
230
- } else if (typeof response === "function") {
223
+ }
224
+ if (typeof response === "function") {
231
225
  if (res.headersSent) {
232
226
  throw new Error(
233
227
  "Universal Middleware called after headers have been sent. Please open an issue at https://github.com/magne4000/universal-handler"
@@ -237,7 +231,8 @@ function createMiddleware(middlewareFactory, options = {}) {
237
231
  res[pendingMiddlewaresSymbol] ??= [];
238
232
  res[pendingMiddlewaresSymbol].push(response);
239
233
  return next?.();
240
- } else if (response instanceof Response) {
234
+ }
235
+ if (response instanceof Response) {
241
236
  await sendResponse(response, res);
242
237
  } else {
243
238
  req[contextSymbol] = response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@universal-middleware/express",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "type": "module",
5
5
  "description": "Express adapter for universal middlewares",
6
6
  "files": [
@@ -12,34 +12,23 @@
12
12
  "author": "Joël Charles <joel.charles91@gmail.com>",
13
13
  "repository": "https://github.com/magne4000/universal-handler",
14
14
  "license": "MIT",
15
- "scripts": {
16
- "build": "rimraf dist && tsup",
17
- "dev": "tsup --watch",
18
- "prepack": "pnpm build",
19
- "test": "vitest run",
20
- "test:run-express:node": "tsx tests/entry-express.ts",
21
- "test:run-express:bun": "bun tests/entry-express.ts",
22
- "test:run-express:deno": "deno run --unstable-byonm --unstable-sloppy-imports -A tests/entry-express.ts",
23
- "test:typecheck": "tsc -p tsconfig.json --noEmit",
24
- "release": "LANG=en_US release-me patch",
25
- "release:minor": "LANG=en_US release-me minor",
26
- "release:commit": "LANG=en_US release-me commit"
27
- },
28
15
  "dependencies": {
29
- "@universal-middleware/core": "^0.2.6"
16
+ "@universal-middleware/core": "^0.2.8"
30
17
  },
31
18
  "devDependencies": {
32
19
  "@brillout/release-me": "^0.4.0",
33
20
  "@swc/core": "^1.7.11",
34
- "@types/node": "^20.14.10",
35
- "@universal-middleware/tests": "workspace:*",
21
+ "@types/express": "^4.17.21",
22
+ "@types/node": "^20.16.5",
36
23
  "express": "^4.19.2",
37
24
  "helmet": "^7.1.0",
38
25
  "rimraf": "^6.0.0",
39
26
  "tsup": "^8.2.4",
40
27
  "tsx": "^4.19.0",
41
28
  "typescript": "^5.5.4",
42
- "vitest": "^2.0.5"
29
+ "vite": "^5.4.3",
30
+ "vitest": "^2.0.5",
31
+ "@universal-middleware/tests": "0.0.0"
43
32
  },
44
33
  "sideEffects": false,
45
34
  "keywords": [
@@ -52,5 +41,17 @@
52
41
  "universal-server",
53
42
  "context-middleware",
54
43
  "request-handling"
55
- ]
56
- }
44
+ ],
45
+ "scripts": {
46
+ "build": "rimraf dist && tsup",
47
+ "dev": "tsup --watch",
48
+ "test": "vitest run",
49
+ "test:run-express:node": "tsx tests/entry-express.ts",
50
+ "test:run-express:bun": "bun tests/entry-express.ts",
51
+ "test:run-express:deno": "deno run --unstable-byonm --unstable-sloppy-imports -A tests/entry-express.ts",
52
+ "test:typecheck": "tsc -p tsconfig.json --noEmit",
53
+ "release": "LANG=en_US release-me patch",
54
+ "release:minor": "LANG=en_US release-me minor",
55
+ "release:commit": "LANG=en_US release-me commit"
56
+ }
57
+ }