-
Notifications
You must be signed in to change notification settings - Fork 156
add aws-lambda-compressed #819
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@opennextjs/aws": patch | ||
--- | ||
|
||
Add aws-lambda-compressed wrapper | ||
|
||
New wrapper called `aws-lambda-compressed`. The compression quality for brotli can be configured using the `BROTLI_QUALITY` environment variable. If not set, it defaults to 6. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/open-next/src/overrides/wrappers/aws-lambda-compressed.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Readable, type Transform, Writable } from "node:stream"; | ||
import type { ReadableStream } from "node:stream/web"; | ||
import zlib from "node:zlib"; | ||
|
||
import type { AwsLambdaEvent, AwsLambdaReturn } from "types/aws-lambda"; | ||
import type { InternalResult, StreamCreator } from "types/open-next"; | ||
import type { WrapperHandler } from "types/overrides"; | ||
import { error } from "../../adapters/logger"; | ||
import { formatWarmerResponse } from "./aws-lambda"; | ||
|
||
const handler: WrapperHandler = | ||
async (handler, converter) => | ||
async (event: AwsLambdaEvent): Promise<AwsLambdaReturn> => { | ||
// Handle warmer event | ||
if ("type" in event) { | ||
return formatWarmerResponse(event); | ||
} | ||
|
||
const internalEvent = await converter.convertFrom(event); | ||
// This is a workaround | ||
// https://github.com/opennextjs/opennextjs-aws/blob/e9b37fd44eb856eb8ae73168bf455ff85dd8b285/packages/open-next/src/overrides/wrappers/aws-lambda.ts#L49-L53 | ||
const fakeStream: StreamCreator = { | ||
writeHeaders: () => { | ||
return new Writable({ | ||
write: (_chunk, _encoding, callback) => { | ||
callback(); | ||
}, | ||
}); | ||
}, | ||
}; | ||
|
||
const handlerResponse = await handler(internalEvent, { | ||
streamCreator: fakeStream, | ||
}); | ||
|
||
// Check if response is already compressed | ||
// The handlers response headers are lowercase | ||
const alreadyEncoded = handlerResponse.headers["content-encoding"] ?? ""; | ||
|
||
// Return early here if the response is already compressed | ||
if (alreadyEncoded) { | ||
return converter.convertTo(handlerResponse, event); | ||
} | ||
|
||
// We compress the body if the client accepts it | ||
const acceptEncoding = | ||
internalEvent.headers["accept-encoding"] ?? | ||
internalEvent.headers["Accept-Encoding"] ?? | ||
""; | ||
|
||
let contentEncoding: string | null = null; | ||
if (acceptEncoding?.includes("br")) { | ||
contentEncoding = "br"; | ||
} else if (acceptEncoding?.includes("gzip")) { | ||
contentEncoding = "gzip"; | ||
} else if (acceptEncoding?.includes("deflate")) { | ||
contentEncoding = "deflate"; | ||
} | ||
|
||
const response: InternalResult = { | ||
...handlerResponse, | ||
body: compressBody(handlerResponse.body, contentEncoding), | ||
headers: { | ||
...handlerResponse.headers, | ||
...(contentEncoding ? { "content-encoding": contentEncoding } : {}), | ||
}, | ||
isBase64Encoded: !!contentEncoding || handlerResponse.isBase64Encoded, | ||
}; | ||
|
||
return converter.convertTo(response, event); | ||
}; | ||
|
||
export default { | ||
wrapper: handler, | ||
name: "aws-lambda-compressed", | ||
supportStreaming: false, | ||
}; | ||
|
||
function compressBody(body: ReadableStream, encoding: string | null) { | ||
// If no encoding is specified, return original body | ||
if (!encoding) return body; | ||
try { | ||
const readable = Readable.fromWeb(body); | ||
let transform: Transform; | ||
|
||
switch (encoding) { | ||
case "br": | ||
transform = zlib.createBrotliCompress({ | ||
params: { | ||
// This is a compromise between speed and compression ratio. | ||
// The default one will most likely timeout an AWS Lambda with default configuration on large bodies (>6mb). | ||
// Therefore we set it to 6, which is a good compromise. | ||
[zlib.constants.BROTLI_PARAM_QUALITY]: | ||
Number(process.env.BROTLI_QUALITY) ?? 6, | ||
}, | ||
}); | ||
break; | ||
case "gzip": | ||
transform = zlib.createGzip(); | ||
break; | ||
case "deflate": | ||
transform = zlib.createDeflate(); | ||
break; | ||
default: | ||
return body; | ||
} | ||
return Readable.toWeb(readable.pipe(transform)); | ||
} catch (e) { | ||
error("Error compressing body:", e); | ||
// Fall back to no compression on error | ||
return body; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.