|
| 1 | +import { Readable, type Transform, Writable } from "node:stream"; |
| 2 | +import type { ReadableStream } from "node:stream/web"; |
| 3 | +import zlib from "node:zlib"; |
| 4 | + |
| 5 | +import type { AwsLambdaEvent, AwsLambdaReturn } from "types/aws-lambda"; |
| 6 | +import type { InternalResult, StreamCreator } from "types/open-next"; |
| 7 | +import type { WrapperHandler } from "types/overrides"; |
| 8 | +import { error } from "../../adapters/logger"; |
| 9 | +import { formatWarmerResponse } from "./aws-lambda"; |
| 10 | + |
| 11 | +const handler: WrapperHandler = |
| 12 | + async (handler, converter) => |
| 13 | + async (event: AwsLambdaEvent): Promise<AwsLambdaReturn> => { |
| 14 | + // Handle warmer event |
| 15 | + if ("type" in event) { |
| 16 | + return formatWarmerResponse(event); |
| 17 | + } |
| 18 | + |
| 19 | + const internalEvent = await converter.convertFrom(event); |
| 20 | + // This is a workaround |
| 21 | + // https://github.com/opennextjs/opennextjs-aws/blob/e9b37fd44eb856eb8ae73168bf455ff85dd8b285/packages/open-next/src/overrides/wrappers/aws-lambda.ts#L49-L53 |
| 22 | + const fakeStream: StreamCreator = { |
| 23 | + writeHeaders: () => { |
| 24 | + return new Writable({ |
| 25 | + write: (_chunk, _encoding, callback) => { |
| 26 | + callback(); |
| 27 | + }, |
| 28 | + }); |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + const handlerResponse = await handler(internalEvent, { |
| 33 | + streamCreator: fakeStream, |
| 34 | + }); |
| 35 | + |
| 36 | + // Check if response is already compressed |
| 37 | + // The handlers response headers are lowercase |
| 38 | + const alreadyEncoded = handlerResponse.headers["content-encoding"] ?? ""; |
| 39 | + |
| 40 | + // Return early here if the response is already compressed |
| 41 | + if (alreadyEncoded) { |
| 42 | + return converter.convertTo(handlerResponse, event); |
| 43 | + } |
| 44 | + |
| 45 | + // We compress the body if the client accepts it |
| 46 | + const acceptEncoding = |
| 47 | + internalEvent.headers["accept-encoding"] ?? |
| 48 | + internalEvent.headers["Accept-Encoding"] ?? |
| 49 | + ""; |
| 50 | + |
| 51 | + let contentEncoding: string | null = null; |
| 52 | + if (acceptEncoding?.includes("br")) { |
| 53 | + contentEncoding = "br"; |
| 54 | + } else if (acceptEncoding?.includes("gzip")) { |
| 55 | + contentEncoding = "gzip"; |
| 56 | + } else if (acceptEncoding?.includes("deflate")) { |
| 57 | + contentEncoding = "deflate"; |
| 58 | + } |
| 59 | + |
| 60 | + const response: InternalResult = { |
| 61 | + ...handlerResponse, |
| 62 | + body: compressBody(handlerResponse.body, contentEncoding), |
| 63 | + headers: { |
| 64 | + ...handlerResponse.headers, |
| 65 | + ...(contentEncoding ? { "content-encoding": contentEncoding } : {}), |
| 66 | + }, |
| 67 | + isBase64Encoded: !!contentEncoding || handlerResponse.isBase64Encoded, |
| 68 | + }; |
| 69 | + |
| 70 | + return converter.convertTo(response, event); |
| 71 | + }; |
| 72 | + |
| 73 | +export default { |
| 74 | + wrapper: handler, |
| 75 | + name: "aws-lambda-compressed", |
| 76 | + supportStreaming: false, |
| 77 | +}; |
| 78 | + |
| 79 | +function compressBody(body: ReadableStream, encoding: string | null) { |
| 80 | + // If no encoding is specified, return original body |
| 81 | + if (!encoding) return body; |
| 82 | + try { |
| 83 | + const readable = Readable.fromWeb(body); |
| 84 | + let transform: Transform; |
| 85 | + |
| 86 | + switch (encoding) { |
| 87 | + case "br": |
| 88 | + transform = zlib.createBrotliCompress({ |
| 89 | + params: { |
| 90 | + // This is a compromise between speed and compression ratio. |
| 91 | + // The default one will most likely timeout an AWS Lambda with default configuration on large bodies (>6mb). |
| 92 | + // Therefore we set it to 6, which is a good compromise. |
| 93 | + [zlib.constants.BROTLI_PARAM_QUALITY]: |
| 94 | + Number(process.env.BROTLI_QUALITY) ?? 6, |
| 95 | + }, |
| 96 | + }); |
| 97 | + break; |
| 98 | + case "gzip": |
| 99 | + transform = zlib.createGzip(); |
| 100 | + break; |
| 101 | + case "deflate": |
| 102 | + transform = zlib.createDeflate(); |
| 103 | + break; |
| 104 | + default: |
| 105 | + return body; |
| 106 | + } |
| 107 | + return Readable.toWeb(readable.pipe(transform)); |
| 108 | + } catch (e) { |
| 109 | + error("Error compressing body:", e); |
| 110 | + // Fall back to no compression on error |
| 111 | + return body; |
| 112 | + } |
| 113 | +} |
0 commit comments