Skip to content

Commit cce04ac

Browse files
committed
don't transform payloads
1 parent b36af39 commit cce04ac

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

src/proxy.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { APIGatewayProxyResultV2 } from "aws-lambda";
1+
import { APIGatewayProxyResult } from "aws-lambda";
22
import axios, { AxiosResponseHeaders, RawAxiosResponseHeaders } from "axios";
33
import net from "net";
44
import { EndpointRequest, EndpointResponse } from "./types";
@@ -102,22 +102,16 @@ export const endpointProxy = async ({
102102
headers: rawHeaders,
103103
data: decodedBody,
104104
timeout: deadline,
105+
responseType: "arraybuffer",
105106
})
106107
.then((response) => {
107108
const { data: rawData, headers: rawHeaders } = response;
108109

109-
const body =
110-
rawData && typeof rawData === "string"
111-
? rawData
112-
: Buffer.from(rawData).toString("base64");
113-
114-
const isBase64Encoded = rawData && typeof rawData !== "string";
115-
116-
const payload: APIGatewayProxyResultV2 = {
110+
const payload: APIGatewayProxyResult = {
117111
statusCode: response.status,
118112
headers: convertHeaders(rawHeaders),
119-
body,
120-
isBase64Encoded,
113+
body: Buffer.from(rawData).toString("base64"),
114+
isBase64Encoded: true,
121115
};
122116

123117
return {

src/routing.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from "aws-lambda";
21
import axios from "axios";
32
import { EndpointRequest } from "./types";
43
import { endpointProxy } from "./proxy";
@@ -13,34 +12,37 @@ export const routeEvents = async (
1312
{
1413
// block indefinitely until a response is received
1514
timeout: 0,
15+
responseType: "text",
1616
}
1717
);
1818

19-
const requestId = headers["Lambda-Runtime-Aws-Request-Id"] as string;
19+
const requestId = headers["lambda-runtime-aws-request-id"];
20+
21+
if (!requestId) {
22+
throw new Error("No request ID found in response headers");
23+
}
2024

2125
console.log("Received request from Lambda Runtime API", { requestId });
2226

2327
const initialDeadline = Number.parseInt(
24-
headers["Lambda-Runtime-Deadline-Ms"]
28+
headers["lambda-runtime-deadline-ms"]
2529
);
2630

27-
// TODO handle V1 payloads
28-
const event = JSON.parse(data) as APIGatewayProxyEventV2;
29-
let payload: APIGatewayProxyResultV2 | undefined = undefined;
31+
let payload: any | undefined = undefined;
3032

3133
if (!endpoint) {
3234
const { execa } = await import("execa");
3335
// no endpoint, just exec the bin
3436
const { stdout } = await execa({
3537
stderr: ["inherit"],
36-
})`${bin} ${JSON.stringify(event)}`;
38+
})`${bin} ${data}`;
3739

38-
payload = JSON.parse(stdout) as APIGatewayProxyResultV2;
40+
payload = JSON.parse(stdout);
3941
} else {
4042
const request: EndpointRequest = {
4143
requestId,
4244
endpoint,
43-
event,
45+
event: JSON.parse(data),
4446
initialDeadline,
4547
};
4648

src/types.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from "aws-lambda";
2-
31
export type EndpointRequest = {
42
requestId: string;
53
endpoint: string;
6-
event: APIGatewayProxyEventV2;
4+
event: any;
75
initialDeadline: number;
86
};
97

108
export type EndpointResponse = {
119
requestId: string;
12-
payload: APIGatewayProxyResultV2;
10+
payload: any;
1311
};

0 commit comments

Comments
 (0)