Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-lambda-streaming): handle additional XMLHttpRequestBodyInit types #2616

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/presets/aws-lambda/runtime/aws-lambda-streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
normalizeLambdaOutgoingHeaders,
} from "nitropack/runtime/internal";
import { withQuery } from "ufo";
import { Readable } from "node:stream";

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
Expand Down Expand Up @@ -72,12 +73,28 @@ export const handler = awslambda.streamifyResponse(
},
};
if (r.body) {
const reader = r.body as ReadableStream;
const writer = awslambda.HttpResponseStream.from(
responseStream,
httpResponseMetadata
);
await streamToNodeStream(reader.getReader(), responseStream);

let reader;
if (r.body instanceof ReadableStream) {
reader = r.body.getReader();
} else if (typeof r.body === 'string') {
reader = Readable.toWeb(Readable.from(r.body)).getReader();
} else if (r.body instanceof Uint8Array || r.body instanceof ArrayBuffer) {
reader = Readable.toWeb(Readable.from(Buffer.from(r.body))).getReader();
} else if (r.body instanceof Blob) {
const arrayBuffer = await r.body.arrayBuffer();
reader = Readable.toWeb(Readable.from(Buffer.from(arrayBuffer))).getReader();
} else if (r.body instanceof FormData || r.body instanceof URLSearchParams) {
reader = Readable.toWeb(Readable.from(r.body.toString())).getReader();
} else {
reader = Readable.toWeb(Readable.from(JSON.stringify(r.body))).getReader();
}

await streamToNodeStream(reader, responseStream);
writer.end();
}
}
Expand Down