Skip to content

Commit

Permalink
desc
Browse files Browse the repository at this point in the history
  • Loading branch information
revmischa committed Sep 30, 2022
1 parent 96747bd commit 99bdd73
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 48 deletions.
7 changes: 1 addition & 6 deletions .eslintrc.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ const project = new awscdk.AwsCdkConstructLibrary({
cdkVersion: '2.39.0',
defaultReleaseBranch: 'main',
name: 'cdk-nextjs',
repositoryUrl: 'https://github.com/revmischa/nextjs-cdk.git',
repositoryUrl: 'https://github.com/jetbridge/nextjs-cdk.git',
authorOrganization: 'JetBridge',
packageName: 'cdk-nextjs-standalone',
description: 'Deploy a NextJS app to AWS using CDK. Uses standalone build and output tracing.',
keywords: ['nextjs', 'aws', 'cdk', 'standalone', 'next'],

bundledDeps: [
'chalk',
Expand All @@ -18,10 +22,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
'@types/micromatch',
'esbuild',
] /* Runtime dependencies of this module. */,
// description: undefined, /* The description is just a string that helps people understand the purpose of the package. */
// devDeps: [], /* Build dependencies for this module. */
// packageName: undefined, /* The "name" in package.json. */

eslintOptions: { prettier: true },
eslintOptions: { prettier: true, ignorePatterns: ['src/Functions/**/*'] },
});
project.synth();
223 changes: 202 additions & 21 deletions LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions src/Functions/LambdaNextCompat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import type { IncomingMessage } from 'http';
import type { APIGatewayProxyEventV2, APIGatewayProxyResult } from 'aws-lambda';
import type { NextApiRequestCookies } from 'next/dist/server/api-utils';

const http = require('http');
const Stream = require('stream');

declare type Req = IncomingMessage & {
cookies?: NextApiRequestCookies;
};

// compat layer between aws lambda APIGWv2 payload and nextjs
// PROBABLY WRONG
// based on https://www.npmjs.com/package/@sls-next/next-aws-lambda
export const reqResMapper = (event: APIGatewayProxyEventV2, callback) => {
const base64Support = process.env.BINARY_SUPPORT === 'yes';
const response: Partial<APIGatewayProxyResult & { body: any }> = {
isBase64Encoded: base64Support,
};
let responsePromise;

// build request
const newStream = new Stream.Readable();
const req: Req = Object.assign(newStream, http.IncomingMessage.prototype);
req.url = event.rawPath || '';
req.method = event.requestContext.http.method;
req.headers = event.headers;
req.cookies = Object.fromEntries(event.cookies?.map((c) => c.split('=')) || []);

// build response
const res = new Stream();
Object.defineProperty(res, 'statusCode', {
get() {
return response.statusCode;
},
set(statusCode) {
response.statusCode = statusCode;
},
});
res.headers = {};
res.writeHead = (status, headers) => {
response.statusCode = status;
if (headers) res.headers = Object.assign(res.headers, headers);
};
res.write = (chunk) => {
if (!response.body) {
response.body = Buffer.from('');
}

response.body = Buffer.concat([
Buffer.isBuffer(response.body) ? response.body : Buffer.from(response.body),
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
]);
};
res.setHeader = (name, value) => {
res.headers[name.toLowerCase()] = value;
};
res.removeHeader = (name) => {
delete res.headers[name.toLowerCase()];
};
res.getHeader = (name) => {
return res.headers[name.toLowerCase()];
};
res.getHeaders = () => {
return res.headers;
};
res.hasHeader = (name) => {
return !!res.getHeader(name);
};

const onResEnd = (callback, resolve) => (text) => {
if (text) res.write(text);
if (!res.statusCode) {
res.statusCode = 200;
}

if (response.body) {
response.body = Buffer.from(response.body).toString(base64Support ? 'base64' : undefined);
}
response.headers = res.headers;
res.writeHead(response.statusCode);

if (callback) {
callback(null, response);
} else {
resolve(response);
}
};

if (typeof callback === 'function') {
res.end = onResEnd(callback, null);
} else {
responsePromise = new Promise((resolve) => {
res.end = onResEnd(null, resolve);
});
}

if (event.body) {
req.push(event.body, event.isBase64Encoded ? 'base64' : undefined);
}

req.push(null);

return { req, res, responsePromise };
};
Loading

0 comments on commit 99bdd73

Please sign in to comment.