@geostrategists/react-router-aws 2.1.0 → 2.2.0

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/biome.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/2.2.2/schema.json",
3
+ "vcs": {
4
+ "enabled": true,
5
+ "clientKind": "git",
6
+ "useIgnoreFile": true
7
+ },
8
+ "files": {
9
+ "ignoreUnknown": false
10
+ },
11
+ "formatter": {
12
+ "enabled": true,
13
+ "indentStyle": "space",
14
+ "lineWidth": 120
15
+ },
16
+ "linter": {
17
+ "enabled": true,
18
+ "rules": {
19
+ "recommended": true
20
+ }
21
+ },
22
+ "javascript": {
23
+ "formatter": {
24
+ "quoteStyle": "double"
25
+ }
26
+ },
27
+ "assist": {
28
+ "enabled": true,
29
+ "actions": {
30
+ "source": {
31
+ "organizeImports": "on"
32
+ }
33
+ }
34
+ },
35
+ "overrides": [
36
+ {
37
+ "includes": ["test/**"],
38
+ "linter": {
39
+ "rules": {
40
+ "suspicious": {
41
+ "noExplicitAny": "off"
42
+ }
43
+ }
44
+ }
45
+ }
46
+ ]
47
+ }
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { UNSAFE_MiddlewareEnabled, unstable_InitialContext, AppLoadContext, ServerBuild } from 'react-router';
1
+ import { UNSAFE_MiddlewareEnabled, RouterContextProvider, AppLoadContext, ServerBuild } from 'react-router';
2
2
  import { ALBEvent, ALBHandler, APIGatewayProxyEvent, APIGatewayProxyHandler, APIGatewayProxyEventV2, APIGatewayProxyHandlerV2, LambdaFunctionURLEvent, LambdaFunctionURLHandler, Handler, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, ALBResult } from 'aws-lambda';
3
3
  import { StreamifyHandler } from 'aws-lambda/handler';
4
4
 
@@ -10,7 +10,7 @@ type MaybePromise<T> = T | Promise<T>;
10
10
  * You can think of this as an escape hatch that allows you to pass
11
11
  * environment/platform-specific values through to your loader/action.
12
12
  */
13
- type GetLoadContextFunction<E> = (event: E) => UNSAFE_MiddlewareEnabled extends true ? MaybePromise<unstable_InitialContext> : MaybePromise<AppLoadContext>;
13
+ type GetLoadContextFunction<E> = (event: E) => UNSAFE_MiddlewareEnabled extends true ? MaybePromise<RouterContextProvider> : MaybePromise<AppLoadContext>;
14
14
  type CreateRequestHandlerArgs<T> = {
15
15
  build: ServerBuild;
16
16
  getLoadContext?: GetLoadContextFunction<T>;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { UNSAFE_MiddlewareEnabled, unstable_InitialContext, AppLoadContext, ServerBuild } from 'react-router';
1
+ import { UNSAFE_MiddlewareEnabled, RouterContextProvider, AppLoadContext, ServerBuild } from 'react-router';
2
2
  import { ALBEvent, ALBHandler, APIGatewayProxyEvent, APIGatewayProxyHandler, APIGatewayProxyEventV2, APIGatewayProxyHandlerV2, LambdaFunctionURLEvent, LambdaFunctionURLHandler, Handler, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2, ALBResult } from 'aws-lambda';
3
3
  import { StreamifyHandler } from 'aws-lambda/handler';
4
4
 
@@ -10,7 +10,7 @@ type MaybePromise<T> = T | Promise<T>;
10
10
  * You can think of this as an escape hatch that allows you to pass
11
11
  * environment/platform-specific values through to your loader/action.
12
12
  */
13
- type GetLoadContextFunction<E> = (event: E) => UNSAFE_MiddlewareEnabled extends true ? MaybePromise<unstable_InitialContext> : MaybePromise<AppLoadContext>;
13
+ type GetLoadContextFunction<E> = (event: E) => UNSAFE_MiddlewareEnabled extends true ? MaybePromise<RouterContextProvider> : MaybePromise<AppLoadContext>;
14
14
  type CreateRequestHandlerArgs<T> = {
15
15
  build: ServerBuild;
16
16
  getLoadContext?: GetLoadContextFunction<T>;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @geostrategists/react-router-aws v2.1.0
2
+ * @geostrategists/react-router-aws v2.2.0
3
3
  *
4
4
  * Copyright (c) Geostrategists Consulting GmbH
5
5
  *
@@ -9,11 +9,9 @@
9
9
  * @license MIT
10
10
  */
11
11
  "use strict";
12
- var __create = Object.create;
13
12
  var __defProp = Object.defineProperty;
14
13
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
15
14
  var __getOwnPropNames = Object.getOwnPropertyNames;
16
- var __getProtoOf = Object.getPrototypeOf;
17
15
  var __hasOwnProp = Object.prototype.hasOwnProperty;
18
16
  var __export = (target, all) => {
19
17
  for (var name in all)
@@ -27,14 +25,6 @@ var __copyProps = (to, from, except, desc) => {
27
25
  }
28
26
  return to;
29
27
  };
30
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
31
- // If the importer is in node compatibility mode or this is not an ESM
32
- // file that has been converted to a CommonJS file using a Babel-
33
- // compatible transform (i.e. "__esModule" has not been set), then set
34
- // "default" to the CommonJS "module.exports" for node compatibility.
35
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
36
- mod
37
- ));
38
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
39
29
 
40
30
  // src/index.ts
@@ -149,28 +139,29 @@ function createReactRouterHeadersAPIGatewayV2(requestHeaders, requestCookies) {
149
139
  }
150
140
  return headers;
151
141
  }
152
- async function sendReactRouterResponseAPIGatewayV2(nodeResponse) {
142
+ function extractAPIGatewayV2ResponseMetadata(nodeResponse) {
153
143
  const cookies = nodeResponse.headers.getSetCookie();
154
144
  if (cookies.length) {
155
145
  nodeResponse.headers.delete("Set-Cookie");
156
146
  }
147
+ return {
148
+ statusCode: nodeResponse.status,
149
+ headers: Object.fromEntries(nodeResponse.headers.entries()),
150
+ cookies
151
+ };
152
+ }
153
+ async function sendReactRouterResponseAPIGatewayV2(nodeResponse) {
154
+ const result = extractAPIGatewayV2ResponseMetadata(nodeResponse);
157
155
  const contentType = nodeResponse.headers.get("Content-Type");
158
- const isBase64Encoded = isBinaryType(contentType);
159
- let body;
156
+ result.isBase64Encoded = isBinaryType(contentType);
160
157
  if (nodeResponse.body) {
161
- if (isBase64Encoded) {
162
- body = await (0, import_node.readableStreamToString)(nodeResponse.body, "base64");
158
+ if (result.isBase64Encoded) {
159
+ result.body = await (0, import_node.readableStreamToString)(nodeResponse.body, "base64");
163
160
  } else {
164
- body = await nodeResponse.text();
161
+ result.body = await nodeResponse.text();
165
162
  }
166
163
  }
167
- return {
168
- statusCode: nodeResponse.status,
169
- headers: Object.fromEntries(nodeResponse.headers.entries()),
170
- cookies,
171
- body,
172
- isBase64Encoded
173
- };
164
+ return result;
174
165
  }
175
166
  var apiGatewayV2Adapter = {
176
167
  wrapHandler: (handler) => (e) => handler(e),
@@ -283,21 +274,24 @@ var applicationLoadBalancerAdapter = {
283
274
 
284
275
  // src/adapters/function-url-streaming.ts
285
276
  var import_node4 = require("@react-router/node");
286
- var import_promises = __toESM(require("stream/promises"));
277
+ var emptyStream = () => new ReadableStream({
278
+ start(controller) {
279
+ controller.enqueue("");
280
+ controller.close();
281
+ }
282
+ });
287
283
  var sendReactRouterResponseFunctionUrlStreaming = async (response, responseStream) => {
288
- const httpResponseStream = awslambda.HttpResponseStream.from(responseStream, {
289
- statusCode: response.status,
290
- headers: Object.fromEntries(response.headers.entries())
291
- });
292
- if (response.body) {
293
- await (0, import_node4.writeReadableStreamToWritable)(response.body, httpResponseStream);
294
- } else {
295
- await import_promises.default.finished(httpResponseStream);
284
+ const metadata = extractAPIGatewayV2ResponseMetadata(response);
285
+ let body = response.body;
286
+ if (!body) {
287
+ body = emptyStream();
296
288
  }
289
+ const httpResponseStream = awslambda.HttpResponseStream.from(responseStream, metadata);
290
+ await (0, import_node4.writeReadableStreamToWritable)(body, httpResponseStream);
297
291
  };
298
292
  var functionUrlStreamingAdapter = {
299
293
  wrapHandler: awslambda.streamifyResponse,
300
- createReactRouterRequest: apiGatewayV2Adapter.createReactRouterRequest,
294
+ createReactRouterRequest: createReactRouterRequestAPIGateywayV2,
301
295
  sendReactRouterResponse: sendReactRouterResponseFunctionUrlStreaming
302
296
  };
303
297
 
@@ -369,7 +363,7 @@ function createRequestHandler(options) {
369
363
  return assertNever(awsProxy, `Unsupported buffered AWS Proxy type: ${awsProxy}`);
370
364
  }
371
365
  }
372
- function assertNever(x, message) {
366
+ function assertNever(_x, message) {
373
367
  throw new Error(message);
374
368
  }
375
369
  // Annotate the CommonJS export names for ESM import in node:
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @geostrategists/react-router-aws v2.1.0
2
+ * @geostrategists/react-router-aws v2.2.0
3
3
  *
4
4
  * Copyright (c) Geostrategists Consulting GmbH
5
5
  *
@@ -110,28 +110,29 @@ function createReactRouterHeadersAPIGatewayV2(requestHeaders, requestCookies) {
110
110
  }
111
111
  return headers;
112
112
  }
113
- async function sendReactRouterResponseAPIGatewayV2(nodeResponse) {
113
+ function extractAPIGatewayV2ResponseMetadata(nodeResponse) {
114
114
  const cookies = nodeResponse.headers.getSetCookie();
115
115
  if (cookies.length) {
116
116
  nodeResponse.headers.delete("Set-Cookie");
117
117
  }
118
+ return {
119
+ statusCode: nodeResponse.status,
120
+ headers: Object.fromEntries(nodeResponse.headers.entries()),
121
+ cookies
122
+ };
123
+ }
124
+ async function sendReactRouterResponseAPIGatewayV2(nodeResponse) {
125
+ const result = extractAPIGatewayV2ResponseMetadata(nodeResponse);
118
126
  const contentType = nodeResponse.headers.get("Content-Type");
119
- const isBase64Encoded = isBinaryType(contentType);
120
- let body;
127
+ result.isBase64Encoded = isBinaryType(contentType);
121
128
  if (nodeResponse.body) {
122
- if (isBase64Encoded) {
123
- body = await readableStreamToString(nodeResponse.body, "base64");
129
+ if (result.isBase64Encoded) {
130
+ result.body = await readableStreamToString(nodeResponse.body, "base64");
124
131
  } else {
125
- body = await nodeResponse.text();
132
+ result.body = await nodeResponse.text();
126
133
  }
127
134
  }
128
- return {
129
- statusCode: nodeResponse.status,
130
- headers: Object.fromEntries(nodeResponse.headers.entries()),
131
- cookies,
132
- body,
133
- isBase64Encoded
134
- };
135
+ return result;
135
136
  }
136
137
  var apiGatewayV2Adapter = {
137
138
  wrapHandler: (handler) => (e) => handler(e),
@@ -244,21 +245,24 @@ var applicationLoadBalancerAdapter = {
244
245
 
245
246
  // src/adapters/function-url-streaming.ts
246
247
  import { writeReadableStreamToWritable } from "@react-router/node";
247
- import stream from "node:stream/promises";
248
+ var emptyStream = () => new ReadableStream({
249
+ start(controller) {
250
+ controller.enqueue("");
251
+ controller.close();
252
+ }
253
+ });
248
254
  var sendReactRouterResponseFunctionUrlStreaming = async (response, responseStream) => {
249
- const httpResponseStream = awslambda.HttpResponseStream.from(responseStream, {
250
- statusCode: response.status,
251
- headers: Object.fromEntries(response.headers.entries())
252
- });
253
- if (response.body) {
254
- await writeReadableStreamToWritable(response.body, httpResponseStream);
255
- } else {
256
- await stream.finished(httpResponseStream);
255
+ const metadata = extractAPIGatewayV2ResponseMetadata(response);
256
+ let body = response.body;
257
+ if (!body) {
258
+ body = emptyStream();
257
259
  }
260
+ const httpResponseStream = awslambda.HttpResponseStream.from(responseStream, metadata);
261
+ await writeReadableStreamToWritable(body, httpResponseStream);
258
262
  };
259
263
  var functionUrlStreamingAdapter = {
260
264
  wrapHandler: awslambda.streamifyResponse,
261
- createReactRouterRequest: apiGatewayV2Adapter.createReactRouterRequest,
265
+ createReactRouterRequest: createReactRouterRequestAPIGateywayV2,
262
266
  sendReactRouterResponse: sendReactRouterResponseFunctionUrlStreaming
263
267
  };
264
268
 
@@ -330,7 +334,7 @@ function createRequestHandler(options) {
330
334
  return assertNever(awsProxy, `Unsupported buffered AWS Proxy type: ${awsProxy}`);
331
335
  }
332
336
  }
333
- function assertNever(x, message) {
337
+ function assertNever(_x, message) {
334
338
  throw new Error(message);
335
339
  }
336
340
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geostrategists/react-router-aws",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "AWS adapter for React Router v7",
5
5
  "license": "MIT",
6
6
  "bugs": {
@@ -29,37 +29,46 @@
29
29
  "./package.json": "./package.json"
30
30
  },
31
31
  "scripts": {
32
- "lint": "eslint .",
33
- "format": "prettier --write .",
34
- "format:check": "prettier --check .",
32
+ "lint": "biome lint .",
33
+ "format": "biome format --write .",
34
+ "format:check": "biome format .",
35
35
  "typecheck": "tsc --noEmit",
36
36
  "build": "tsc && tsup",
37
37
  "prepack": "yarn build && yarn npmignore --auto",
38
38
  "prepare": "husky",
39
- "release": "semantic-release"
39
+ "release": "semantic-release",
40
+ "test": "vitest run",
41
+ "test:watch": "vitest"
40
42
  },
41
43
  "peerDependencies": {
42
- "@react-router/dev": "^7.3.0",
43
- "@react-router/node": "^7.3.0",
44
- "react-router": "^7.3.0"
44
+ "@react-router/dev": "^7.9.1",
45
+ "@react-router/node": "^7.9.1",
46
+ "react-router": "^7.9.1"
45
47
  },
46
48
  "devDependencies": {
49
+ "@biomejs/biome": "^2.2.2",
47
50
  "@eslint/js": "^9.22.0",
48
- "@react-router/dev": "^7.3.0",
49
- "@react-router/node": "^7.3.0",
51
+ "@react-router/dev": "^7.9.1",
52
+ "@react-router/node": "^7.9.1",
50
53
  "@semantic-release/exec": "^7.0.3",
51
54
  "@types/aws-lambda": "^8.10.152",
52
- "@types/node": "^20",
55
+ "@types/node": "^20.19.17",
56
+ "@types/react": "^19",
57
+ "@types/react-dom": "^19",
58
+ "@vitest/coverage-v8": "^3.2.4",
53
59
  "eslint": "^9.22.0",
54
60
  "husky": "^9.1.7",
55
61
  "lint-staged": "^15.5.0",
56
62
  "npmignore": "^0.3.1",
57
63
  "prettier": "3.5.3",
58
- "react-router": "^7.3.0",
64
+ "react": "^19.1.1",
65
+ "react-dom": "^19.1.1",
66
+ "react-router": "^7.9.1",
59
67
  "semantic-release": "^24.2.3",
60
68
  "tsup": "^8.4.0",
61
69
  "typescript": "^5.9.2",
62
- "typescript-eslint": "^8.41.0"
70
+ "typescript-eslint": "^8.41.0",
71
+ "vitest": "^3.2.4"
63
72
  },
64
73
  "packageManager": "yarn@4.9.4+sha512.7b1cb0b62abba6a537b3a2ce00811a843bea02bcf53138581a6ae5b1bf563f734872bd47de49ce32a9ca9dcaff995aa789577ffb16811da7c603dcf69e73750b",
65
74
  "release": {
@@ -0,0 +1,95 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { htmlResponse, redirectResponse, invokeHandlerWithRRMock } from "./utils";
3
+ import type { ALBEvent } from "aws-lambda";
4
+
5
+ function albEvent(path: string, method = "GET", headers: Record<string, string> = {}): ALBEvent {
6
+ return {
7
+ requestContext: {} as ALBEvent["requestContext"],
8
+ httpMethod: method,
9
+ path,
10
+ queryStringParameters: {},
11
+ headers: {
12
+ Host: "example.com",
13
+ "x-forwarded-proto": "https",
14
+ ...headers,
15
+ },
16
+ body: null,
17
+ isBase64Encoded: false,
18
+ };
19
+ }
20
+
21
+ describe("ALB request handling", () => {
22
+ it("parses ALB event", async () => {
23
+ await invokeHandlerWithRRMock(
24
+ "createALBRequestHandler",
25
+ async (request: Request) => {
26
+ expect(request.url).toBe("https://example.com/test");
27
+ expect(request.method).toBe("POST");
28
+ expect(request.headers.get("x-custom-header")).toBe("a");
29
+ return new Response("ok");
30
+ },
31
+ albEvent("/test", "POST", { "x-custom-header": "a" }),
32
+ );
33
+ });
34
+ });
35
+
36
+ describe("ALB response handling", () => {
37
+ it("html without cookie", async () => {
38
+ const res = await invokeHandlerWithRRMock("createALBRequestHandler", () => htmlResponse(), albEvent("/html"));
39
+
40
+ expect(res).toStrictEqual({
41
+ statusCode: 200,
42
+ headers: {
43
+ "content-type": "text/html",
44
+ },
45
+ body: "<html>ok</html>",
46
+ isBase64Encoded: false,
47
+ });
48
+ });
49
+
50
+ it("html with cookie", async () => {
51
+ const res = await invokeHandlerWithRRMock(
52
+ "createALBRequestHandler",
53
+ () => htmlResponse("a=1; Path=/"),
54
+ albEvent("/html"),
55
+ );
56
+ expect(res).toStrictEqual({
57
+ statusCode: 200,
58
+ headers: {
59
+ "content-type": "text/html",
60
+ "set-cookie": "a=1; Path=/",
61
+ },
62
+ body: "<html>ok</html>",
63
+ isBase64Encoded: false,
64
+ });
65
+ });
66
+
67
+ it("redirect without cookie", async () => {
68
+ const res = await invokeHandlerWithRRMock("createALBRequestHandler", () => redirectResponse(), albEvent("/redir"));
69
+ expect(res).toStrictEqual({
70
+ statusCode: 302,
71
+ headers: {
72
+ location: "https://example.com/next",
73
+ },
74
+ body: "",
75
+ isBase64Encoded: false,
76
+ });
77
+ });
78
+
79
+ it("redirect with cookie", async () => {
80
+ const res = await invokeHandlerWithRRMock(
81
+ "createALBRequestHandler",
82
+ () => redirectResponse("b=2; Path=/"),
83
+ albEvent("/redir"),
84
+ );
85
+ expect(res).toStrictEqual({
86
+ statusCode: 302,
87
+ headers: {
88
+ location: "https://example.com/next",
89
+ "set-cookie": "b=2; Path=/",
90
+ },
91
+ body: "",
92
+ isBase64Encoded: false,
93
+ });
94
+ });
95
+ });
@@ -0,0 +1,101 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { htmlResponse, redirectResponse, invokeHandlerWithRRMock } from "./utils";
3
+ import type { APIGatewayProxyEvent } from "aws-lambda";
4
+
5
+ function apiGatewayV1Event(path: string, method = "GET", headers: Record<string, string> = {}): APIGatewayProxyEvent {
6
+ return {
7
+ requestContext: { httpMethod: method } as APIGatewayProxyEvent["requestContext"],
8
+ path,
9
+ queryStringParameters: {},
10
+ headers: {
11
+ Host: "example.com",
12
+ "x-forwarded-proto": "https",
13
+ ...headers,
14
+ },
15
+ body: null,
16
+ isBase64Encoded: false,
17
+ } as Partial<APIGatewayProxyEvent> as APIGatewayProxyEvent;
18
+ }
19
+
20
+ describe("API Gateway v1 request handling", () => {
21
+ it("parses API Gateway v1 event", async () => {
22
+ await invokeHandlerWithRRMock(
23
+ "createAPIGatewayV1RequestHandler",
24
+ (request) => {
25
+ expect(request.url).toBe("https://example.com/test");
26
+ expect(request.method).toBe("POST");
27
+ expect(request.headers.get("x-custom-header")).toBe("a");
28
+ return new Response("ok");
29
+ },
30
+ apiGatewayV1Event("/test", "POST", { "x-custom-header": "a" }),
31
+ );
32
+ });
33
+ });
34
+
35
+ describe("API Gateway v1 response handling", () => {
36
+ it("html without cookie", async () => {
37
+ const res = await invokeHandlerWithRRMock(
38
+ "createAPIGatewayV1RequestHandler",
39
+ () => htmlResponse(),
40
+ apiGatewayV1Event("/html"),
41
+ );
42
+ expect(res).toStrictEqual({
43
+ statusCode: 200,
44
+ headers: {
45
+ "content-type": "text/html",
46
+ },
47
+ body: "<html>ok</html>",
48
+ isBase64Encoded: false,
49
+ });
50
+ });
51
+
52
+ it("html with cookie", async () => {
53
+ const res = await invokeHandlerWithRRMock(
54
+ "createAPIGatewayV1RequestHandler",
55
+ () => htmlResponse("a=1; Path=/"),
56
+ apiGatewayV1Event("/html"),
57
+ );
58
+ expect(res).toStrictEqual({
59
+ statusCode: 200,
60
+ headers: {
61
+ "content-type": "text/html",
62
+ "set-cookie": "a=1; Path=/",
63
+ },
64
+ body: "<html>ok</html>",
65
+ isBase64Encoded: false,
66
+ });
67
+ });
68
+
69
+ it("redirect without cookie", async () => {
70
+ const res = await invokeHandlerWithRRMock(
71
+ "createAPIGatewayV1RequestHandler",
72
+ () => redirectResponse(),
73
+ apiGatewayV1Event("/redir"),
74
+ );
75
+ expect(res).toStrictEqual({
76
+ statusCode: 302,
77
+ headers: {
78
+ location: "https://example.com/next",
79
+ },
80
+ body: "",
81
+ isBase64Encoded: false,
82
+ });
83
+ });
84
+
85
+ it("redirect with cookie", async () => {
86
+ const res = await invokeHandlerWithRRMock(
87
+ "createAPIGatewayV1RequestHandler",
88
+ () => redirectResponse("b=2; Path=/"),
89
+ apiGatewayV1Event("/redir"),
90
+ );
91
+ expect(res).toStrictEqual({
92
+ statusCode: 302,
93
+ headers: {
94
+ location: "https://example.com/next",
95
+ "set-cookie": "b=2; Path=/",
96
+ },
97
+ body: "",
98
+ isBase64Encoded: false,
99
+ });
100
+ });
101
+ });
@@ -0,0 +1,107 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { htmlResponse, invokeHandlerWithRRMock, redirectResponse } from "./utils";
3
+ import type { APIGatewayProxyEventV2 } from "aws-lambda";
4
+
5
+ function apiGatewayV2Event(
6
+ path: string,
7
+ method = "GET",
8
+ headers: Record<string, string> = {},
9
+ cookies?: string[],
10
+ ): APIGatewayProxyEventV2 {
11
+ return {
12
+ requestContext: { http: { method } } as APIGatewayProxyEventV2["requestContext"],
13
+ rawPath: path,
14
+ rawQueryString: "",
15
+ headers: {
16
+ host: "example.com",
17
+ "x-forwarded-proto": "https",
18
+ ...headers,
19
+ },
20
+ cookies,
21
+ body: undefined,
22
+ isBase64Encoded: false,
23
+ } as Partial<APIGatewayProxyEventV2> as APIGatewayProxyEventV2;
24
+ }
25
+
26
+ describe("API Gateway v2 request handling", () => {
27
+ it("parses API Gateway v2 event", async () => {
28
+ await invokeHandlerWithRRMock(
29
+ "createAPIGatewayV2RequestHandler",
30
+ (request) => {
31
+ expect(request.url).toBe("https://example.com/test");
32
+ expect(request.method).toBe("POST");
33
+ expect(request.headers.get("x-custom-header")).toBe("a");
34
+ return new Response("ok");
35
+ },
36
+ apiGatewayV2Event("/test", "POST", { "x-custom-header": "a" }),
37
+ );
38
+ });
39
+ });
40
+
41
+ describe("API Gateway v2 response handling", () => {
42
+ it("html without cookie", async () => {
43
+ const res = await invokeHandlerWithRRMock(
44
+ "createAPIGatewayV2RequestHandler",
45
+ () => htmlResponse(),
46
+ apiGatewayV2Event("/html"),
47
+ );
48
+ expect(res).toStrictEqual({
49
+ statusCode: 200,
50
+ headers: {
51
+ "content-type": "text/html",
52
+ },
53
+ body: "<html>ok</html>",
54
+ isBase64Encoded: false,
55
+ cookies: [],
56
+ });
57
+ });
58
+
59
+ it("html with cookie", async () => {
60
+ const res = await invokeHandlerWithRRMock(
61
+ "createAPIGatewayV2RequestHandler",
62
+ () => htmlResponse("a=1; Path=/"),
63
+ apiGatewayV2Event("/html"),
64
+ );
65
+ expect(res).toStrictEqual({
66
+ statusCode: 200,
67
+ headers: {
68
+ "content-type": "text/html",
69
+ },
70
+ body: "<html>ok</html>",
71
+ isBase64Encoded: false,
72
+ cookies: ["a=1; Path=/"],
73
+ });
74
+ });
75
+
76
+ it("redirect without cookie", async () => {
77
+ const res = await invokeHandlerWithRRMock(
78
+ "createAPIGatewayV2RequestHandler",
79
+ () => redirectResponse(),
80
+ apiGatewayV2Event("/redir"),
81
+ );
82
+ expect(res).toStrictEqual({
83
+ statusCode: 302,
84
+ headers: {
85
+ location: "https://example.com/next",
86
+ },
87
+ isBase64Encoded: false,
88
+ cookies: [],
89
+ });
90
+ });
91
+
92
+ it("redirect with cookie", async () => {
93
+ const res = await invokeHandlerWithRRMock(
94
+ "createAPIGatewayV2RequestHandler",
95
+ () => redirectResponse("b=2; Path=/"),
96
+ apiGatewayV2Event("/redir"),
97
+ );
98
+ expect(res).toStrictEqual({
99
+ statusCode: 302,
100
+ headers: {
101
+ location: "https://example.com/next",
102
+ },
103
+ isBase64Encoded: false,
104
+ cookies: ["b=2; Path=/"],
105
+ });
106
+ });
107
+ });
@@ -0,0 +1,107 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { htmlResponse, invokeHandlerWithRRMock, redirectResponse } from "./utils";
3
+ import type { LambdaFunctionURLEvent } from "aws-lambda";
4
+
5
+ function lambdaFunctionUrlEvent(
6
+ path: string,
7
+ method = "GET",
8
+ headers: Record<string, string> = {},
9
+ cookies?: string[],
10
+ ): LambdaFunctionURLEvent {
11
+ return {
12
+ requestContext: { http: { method } } as LambdaFunctionURLEvent["requestContext"],
13
+ rawPath: path,
14
+ rawQueryString: "",
15
+ headers: {
16
+ host: "example.com",
17
+ "x-forwarded-proto": "https",
18
+ ...headers,
19
+ },
20
+ cookies,
21
+ body: undefined,
22
+ isBase64Encoded: false,
23
+ } as Partial<LambdaFunctionURLEvent> as LambdaFunctionURLEvent;
24
+ }
25
+
26
+ describe("Function URL streaming request handling", () => {
27
+ it("parses Function URL event", async () => {
28
+ await invokeHandlerWithRRMock(
29
+ "createFunctionURLStreamingRequestHandler",
30
+ (request) => {
31
+ expect(request.url).toBe("https://example.com/test");
32
+ expect(request.method).toBe("POST");
33
+ expect(request.headers.get("x-custom-header")).toBe("a");
34
+ return new Response("ok");
35
+ },
36
+ lambdaFunctionUrlEvent("/test", "POST", { "x-custom-header": "a" }),
37
+ );
38
+ });
39
+ });
40
+
41
+ describe("Function URL streaming response handling", () => {
42
+ it("html without cookie", async () => {
43
+ const res = await invokeHandlerWithRRMock(
44
+ "createFunctionURLStreamingRequestHandler",
45
+ () => htmlResponse(),
46
+ lambdaFunctionUrlEvent("/html"),
47
+ );
48
+ expect(res).toStrictEqual({
49
+ statusCode: 200,
50
+ headers: {
51
+ "content-type": "text/html",
52
+ },
53
+ body: "<html>ok</html>",
54
+ cookies: [],
55
+ });
56
+ });
57
+
58
+ it("html with cookie", async () => {
59
+ const res = await invokeHandlerWithRRMock(
60
+ "createFunctionURLStreamingRequestHandler",
61
+ () => htmlResponse("a=1; Path=/"),
62
+ lambdaFunctionUrlEvent("/html"),
63
+ );
64
+ expect(res).toStrictEqual({
65
+ statusCode: 200,
66
+ headers: {
67
+ "content-type": "text/html",
68
+ },
69
+ body: "<html>ok</html>",
70
+ cookies: ["a=1; Path=/"],
71
+ });
72
+ });
73
+
74
+ it("redirect without cookie", async () => {
75
+ const res = await invokeHandlerWithRRMock(
76
+ "createFunctionURLStreamingRequestHandler",
77
+ () => redirectResponse(),
78
+ lambdaFunctionUrlEvent("/redir"),
79
+ );
80
+ expect(res).toStrictEqual({
81
+ statusCode: 302,
82
+ headers: {
83
+ location: "https://example.com/next",
84
+ },
85
+ cookies: [],
86
+ // empty body gets added to provoke a write on the stream
87
+ body: "",
88
+ });
89
+ });
90
+
91
+ it("redirect with cookie", async () => {
92
+ const res = await invokeHandlerWithRRMock(
93
+ "createFunctionURLStreamingRequestHandler",
94
+ () => redirectResponse("b=2; Path=/"),
95
+ lambdaFunctionUrlEvent("/redir"),
96
+ );
97
+ expect(res).toStrictEqual({
98
+ statusCode: 302,
99
+ headers: {
100
+ location: "https://example.com/next",
101
+ },
102
+ cookies: ["b=2; Path=/"],
103
+ // empty body gets added to provoke a write on the stream
104
+ body: "",
105
+ });
106
+ });
107
+ });
@@ -0,0 +1,107 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { htmlResponse, invokeHandlerWithRRMock, redirectResponse } from "./utils";
3
+ import type { LambdaFunctionURLEvent } from "aws-lambda";
4
+
5
+ function lambdaFunctionUrlEvent(
6
+ path: string,
7
+ method = "GET",
8
+ headers: Record<string, string> = {},
9
+ cookies?: string[],
10
+ ): LambdaFunctionURLEvent {
11
+ return {
12
+ requestContext: { http: { method } } as LambdaFunctionURLEvent["requestContext"],
13
+ rawPath: path,
14
+ rawQueryString: "",
15
+ headers: {
16
+ host: "example.com",
17
+ "x-forwarded-proto": "https",
18
+ ...headers,
19
+ },
20
+ cookies,
21
+ body: undefined,
22
+ isBase64Encoded: false,
23
+ } as Partial<LambdaFunctionURLEvent> as LambdaFunctionURLEvent;
24
+ }
25
+
26
+ describe("Function URL request handling", () => {
27
+ it("parses Function URL event", async () => {
28
+ await invokeHandlerWithRRMock(
29
+ "createFunctionURLRequestHandler",
30
+ (request) => {
31
+ expect(request.url).toBe("https://example.com/test");
32
+ expect(request.method).toBe("POST");
33
+ expect(request.headers.get("x-custom-header")).toBe("a");
34
+ return new Response("ok");
35
+ },
36
+ lambdaFunctionUrlEvent("/test", "POST", { "x-custom-header": "a" }),
37
+ );
38
+ });
39
+ });
40
+
41
+ describe("Function URL buffered response handling", () => {
42
+ it("html without cookie", async () => {
43
+ const res = await invokeHandlerWithRRMock(
44
+ "createFunctionURLRequestHandler",
45
+ () => htmlResponse(),
46
+ lambdaFunctionUrlEvent("/html"),
47
+ );
48
+ expect(res).toStrictEqual({
49
+ statusCode: 200,
50
+ headers: {
51
+ "content-type": "text/html",
52
+ },
53
+ body: "<html>ok</html>",
54
+ isBase64Encoded: false,
55
+ cookies: [],
56
+ });
57
+ });
58
+
59
+ it("html with cookie", async () => {
60
+ const res = await invokeHandlerWithRRMock(
61
+ "createFunctionURLRequestHandler",
62
+ () => htmlResponse("a=1; Path=/"),
63
+ lambdaFunctionUrlEvent("/html"),
64
+ );
65
+ expect(res).toStrictEqual({
66
+ statusCode: 200,
67
+ headers: {
68
+ "content-type": "text/html",
69
+ },
70
+ body: "<html>ok</html>",
71
+ isBase64Encoded: false,
72
+ cookies: ["a=1; Path=/"],
73
+ });
74
+ });
75
+
76
+ it("redirect without cookie", async () => {
77
+ const res = await invokeHandlerWithRRMock(
78
+ "createFunctionURLRequestHandler",
79
+ () => redirectResponse(),
80
+ lambdaFunctionUrlEvent("/redir"),
81
+ );
82
+ expect(res).toStrictEqual({
83
+ statusCode: 302,
84
+ headers: {
85
+ location: "https://example.com/next",
86
+ },
87
+ isBase64Encoded: false,
88
+ cookies: [],
89
+ });
90
+ });
91
+
92
+ it("redirect with cookie", async () => {
93
+ const res = await invokeHandlerWithRRMock(
94
+ "createFunctionURLRequestHandler",
95
+ () => redirectResponse("b=2; Path=/"),
96
+ lambdaFunctionUrlEvent("/redir"),
97
+ );
98
+ expect(res).toStrictEqual({
99
+ statusCode: 302,
100
+ headers: {
101
+ location: "https://example.com/next",
102
+ },
103
+ isBase64Encoded: false,
104
+ cookies: ["b=2; Path=/"],
105
+ });
106
+ });
107
+ });
@@ -0,0 +1,34 @@
1
+ // biome-ignore-all lint: keep close to upstream
2
+ /**
3
+ * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
+ *
5
+ * HttpResponseStream is NOT used by the runtime.
6
+ * It is only exposed in the `awslambda` variable for customers to use.
7
+ */
8
+
9
+ // based on https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/main/src/HttpResponseStream.js
10
+ // which is licensed under Apache License 2.0
11
+
12
+ import type { ResponseStream } from "./ResponseStream";
13
+
14
+ const METADATA_PRELUDE_CONTENT_TYPE = "application/vnd.awslambda.http-integration-response";
15
+ const DELIMITER_LEN = 8;
16
+
17
+ // Implements the application/vnd.awslambda.http-integration-response content type.
18
+ export class HttpResponseStream {
19
+ static from(underlyingStream: ResponseStream, prelude: unknown) {
20
+ underlyingStream.setContentType(METADATA_PRELUDE_CONTENT_TYPE);
21
+
22
+ // JSON.stringify is required. NULL byte is not allowed in metadataPrelude.
23
+ const metadataPrelude = JSON.stringify(prelude);
24
+
25
+ underlyingStream._onBeforeFirstWrite = (write) => {
26
+ write(metadataPrelude);
27
+
28
+ // Write 8 null bytes after the JSON prelude.
29
+ write(new Uint8Array(DELIMITER_LEN));
30
+ };
31
+
32
+ return underlyingStream;
33
+ }
34
+ }
@@ -0,0 +1,44 @@
1
+ import { Stream } from "node:stream";
2
+
3
+ // based on https://github.com/astuyve/lambda-stream/blob/main/src/ResponseStream.ts
4
+ // which is MIT licensed according to https://www.npmjs.com/package/lambda-stream
5
+
6
+ export class ResponseStream extends Stream.Writable {
7
+ private response: Buffer[];
8
+ _contentType?: string;
9
+ _isBase64Encoded?: boolean;
10
+ _onBeforeFirstWrite?: (write: (chunk: string | Uint8Array) => void) => void;
11
+ _firstWriteHappened?: boolean = false;
12
+
13
+ constructor() {
14
+ super();
15
+ this.response = [];
16
+ }
17
+
18
+ write(chunk: any, encoding: any, callback?: any): boolean {
19
+ if (!this._firstWriteHappened) {
20
+ this._firstWriteHappened = true;
21
+ this._onBeforeFirstWrite?.(super.write.bind(this));
22
+ }
23
+ return super.write(chunk, encoding, callback);
24
+ }
25
+
26
+ // @param chunk Chunk of data to unshift onto the read queue. For streams not operating in object mode, `chunk` must be a string, `Buffer`, `Uint8Array` or `null`. For object mode
27
+ // streams, `chunk` may be any JavaScript value.
28
+ _write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void {
29
+ this.response.push(Buffer.from(chunk, encoding));
30
+ callback();
31
+ }
32
+
33
+ getBufferedData(): Buffer {
34
+ return Buffer.concat(this.response);
35
+ }
36
+
37
+ setContentType(contentType: string) {
38
+ this._contentType = contentType;
39
+ }
40
+
41
+ setIsBase64Encoded(isBase64Encoded: boolean) {
42
+ this._isBase64Encoded = isBase64Encoded;
43
+ }
44
+ }
@@ -0,0 +1,56 @@
1
+ // based on https://github.com/astuyve/lambda-stream/blob/main/src/index.ts
2
+ // which is MIT licensed according to https://www.npmjs.com/package/lambda-stream
3
+
4
+ import type { StreamifyHandler } from "aws-lambda";
5
+ import { ResponseStream } from "./ResponseStream";
6
+ import { HttpResponseStream } from "./HttpResponseStream";
7
+
8
+ export function streamifyResponse<TEvent = any, TResult = void>(
9
+ handler: StreamifyHandler<TEvent, TResult>,
10
+ ): StreamifyHandler<TEvent, TResult> {
11
+ // Check for global awslambda
12
+ return new Proxy(handler, {
13
+ apply: async (target, _, argList: Parameters<StreamifyHandler<TEvent, TResult>>) => {
14
+ const responseStream: ResponseStream = patchArgs(argList);
15
+ await target(...argList);
16
+
17
+ const bodyAndPrelude = responseStream._isBase64Encoded
18
+ ? responseStream.getBufferedData().toString("base64")
19
+ : responseStream.getBufferedData().toString();
20
+
21
+ const delim = "\0".repeat(8);
22
+ const delimPos = bodyAndPrelude.indexOf(delim);
23
+ if (delimPos === -1) {
24
+ return {
25
+ statusCode: 200,
26
+ headers: {
27
+ "content-type": responseStream._contentType ?? "application/json",
28
+ },
29
+ cookies: [],
30
+ body: bodyAndPrelude,
31
+ };
32
+ } else {
33
+ const prelude = bodyAndPrelude.slice(0, delimPos);
34
+ const body = bodyAndPrelude.slice(delimPos + delim.length);
35
+ const parsedPrelude = JSON.parse(prelude);
36
+ return {
37
+ ...parsedPrelude,
38
+ body,
39
+ };
40
+ }
41
+ },
42
+ });
43
+ }
44
+
45
+ function patchArgs(argList: any[]): ResponseStream {
46
+ if (!(argList[1] instanceof ResponseStream)) {
47
+ const responseStream = new ResponseStream();
48
+ argList.splice(1, 0, responseStream);
49
+ }
50
+ return argList[1];
51
+ }
52
+
53
+ export const awslambda = {
54
+ HttpResponseStream: HttpResponseStream,
55
+ streamifyResponse: streamifyResponse,
56
+ };
package/test/setup.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { vi } from "vitest";
2
+ import { awslambda } from "./lambda-stream";
3
+
4
+ vi.mock("react", () => ({}));
5
+ vi.mock("react/jsx-runtime", () => ({}));
6
+
7
+ vi.stubGlobal("awslambda", awslambda);
package/test/utils.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { vi } from "vitest";
2
+ import type { Context, Handler } from "aws-lambda";
3
+
4
+ import type { ServerBuild } from "react-router";
5
+
6
+ let currentRRHandler: RRHandler | null = null;
7
+ export function setReactRouterHandler(fn: RRHandler) {
8
+ currentRRHandler = fn;
9
+ }
10
+ vi.mock("react-router", () => {
11
+ return {
12
+ createRequestHandler: () => {
13
+ if (!currentRRHandler) throw new Error("React Router handler not set");
14
+ return currentRRHandler;
15
+ },
16
+ };
17
+ });
18
+
19
+ type ReactRouterAws = typeof import("../src");
20
+ type GatewayHandlers = Pick<
21
+ ReactRouterAws,
22
+ | "createALBRequestHandler"
23
+ | "createAPIGatewayV1RequestHandler"
24
+ | "createAPIGatewayV2RequestHandler"
25
+ | "createFunctionURLRequestHandler"
26
+ | "createFunctionURLStreamingRequestHandler"
27
+ >;
28
+
29
+ type RRHandler = (request: Request) => Response | Promise<Response>;
30
+
31
+ export async function createHandlerWithRRMock<T extends keyof GatewayHandlers>(
32
+ gatewayHandler: T,
33
+ handler: RRHandler,
34
+ ): Promise<GatewayHandlers[T]> {
35
+ setReactRouterHandler(handler);
36
+ const handlerFactory = (await import("../src"))[gatewayHandler] as any;
37
+ return handlerFactory({ build: {} as unknown as ServerBuild });
38
+ }
39
+
40
+ export async function invokeHandlerWithRRMock<T extends keyof GatewayHandlers>(
41
+ gatewayHandler: T,
42
+ rrHandler: RRHandler,
43
+ event: Parameters<ReturnType<GatewayHandlers[T]>>[0],
44
+ ) {
45
+ const handler = await createHandlerWithRRMock(gatewayHandler, rrHandler);
46
+ return invokeHandler(handler as any, event);
47
+ }
48
+
49
+ export async function invokeHandler<E, R>(handler: Handler<E, R>, event: E): Promise<R> {
50
+ return (await handler(event, {} as unknown as Context, () => {
51
+ throw new Error("Callback not supported");
52
+ })) as R;
53
+ }
54
+
55
+ export function htmlResponse(setCookie?: string) {
56
+ const headers = new Headers({ "Content-Type": "text/html" });
57
+ if (setCookie) {
58
+ headers.append("Set-Cookie", setCookie);
59
+ }
60
+ return new Response("<html>ok</html>", { status: 200, headers });
61
+ }
62
+
63
+ export function redirectResponse(setCookie?: string) {
64
+ const headers = new Headers({ Location: "https://example.com/next" });
65
+ if (setCookie) {
66
+ headers.append("Set-Cookie", setCookie);
67
+ }
68
+ return new Response(null, { status: 302, headers });
69
+ }
@@ -0,0 +1,12 @@
1
+ import { defineConfig } from "vitest/config";
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: "node",
6
+ include: ["test/**/*.test.ts"],
7
+ setupFiles: ["./test/setup.ts"],
8
+ coverage: {
9
+ reporter: ["text", "lcov"],
10
+ },
11
+ },
12
+ });
package/eslint.config.mjs DELETED
@@ -1,12 +0,0 @@
1
- // @ts-check
2
-
3
- import eslint from "@eslint/js";
4
- import tseslint from "typescript-eslint";
5
-
6
- export default tseslint.config(
7
- {
8
- ignores: ["dist", "lib"],
9
- },
10
- eslint.configs.recommended,
11
- tseslint.configs.recommended,
12
- );