UNPKG

ts-lambda-local-dev

Version:
113 lines 5.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalLambda = exports.DefaultPathParamsPattern = exports.DefaultPort = void 0; const url = __importStar(require("url")); const express_1 = __importDefault(require("express")); const utils_1 = require("./utils"); exports.DefaultPort = 8000; // binary upload content-type headers const defaultBinaryContentTypeHeaders = [ 'application/octet-stream', 'image/png', 'image/jpeg', 'image/gif', 'application/pdf', 'application/zip', ]; exports.DefaultPathParamsPattern = '/'; class LocalLambda { constructor(config, app, defaultPath) { var _a, _b, _c, _d, _e; this.handler = config.handler; this.port = (_a = config.port) !== null && _a !== void 0 ? _a : exports.DefaultPort; this.context = config.context || {}; this.enableCORS = (_b = config.enableCORS) !== null && _b !== void 0 ? _b : true; this.binaryContentTypesOverride = new Set((_c = config.binaryContentTypesOverride) !== null && _c !== void 0 ? _c : defaultBinaryContentTypeHeaders); this.pathParamsPattern = (_d = config.pathParamsPattern) !== null && _d !== void 0 ? _d : exports.DefaultPathParamsPattern; this.app = app || (0, express_1.default)(); this.defaultPath = defaultPath !== null && defaultPath !== void 0 ? defaultPath : exports.DefaultPathParamsPattern; this.requestContext = (_e = config.requestContext) !== null && _e !== void 0 ? _e : {}; } createRoute() { const router = express_1.default.Router(); this.app.use(this.defaultPath, router); router.all(`${this.pathParamsPattern}`, async (request, response) => { // create a copy of requestContext to avoid accidental mutation const copyOfRequestContext = (0, utils_1.cloneDeep)(this.requestContext); const data = []; const parsedUrl = url.parse(request.url, true); request.on('data', chunk => { data.push(chunk); }); request.on('end', async () => { if (this.enableCORS && request.method === 'OPTIONS') { this.setCORSHeaders(response); response.writeHead(200); response.end(); return; // for complex requests(POST etc)' CORS header } const contentType = request.headers['content-type']; const isBinaryUpload = this.binaryContentTypesOverride.has(contentType); const body = Buffer.concat(data); const req = { path: parsedUrl.pathname, httpMethod: request.method, headers: request.headers, /* if duplicate queryParameters are present then API Gateway will flatten them into a comma-separated list eg: ?a=1&a=2&a=3 will be parsed as { a: [1,2,3] } by url.parse and flattenArraysInJSON will convert it to { a: '1,2,3' } which is the same behavior as API Gateway */ queryStringParameters: (0, utils_1.flattenArraysInJSON)(parsedUrl.query), body: isBinaryUpload ? body.toString('base64') : body.toString('utf8'), isBase64Encoded: isBinaryUpload ? true : false, pathParameters: request.params, requestContext: copyOfRequestContext, }; const rs = await this.handler(req, this.context); // for simple requests' CORS header this.enableCORS && this.setCORSHeaders(response); response.statusCode = rs.statusCode; response.writeHead(rs.statusCode, rs.headers); rs.body && (rs.body = Buffer.from(rs.body, rs.isBase64Encoded ? 'base64' : 'utf8')); response.end(rs.body); }); }); } run() { this.createRoute(); this.app.listen(this.port, () => console.info(`🚀 Server ready at http://localhost:${this.port} at '${new Date().toLocaleString()}'`)); } setCORSHeaders(res) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Request-Method', '*'); res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, DELETE'); res.setHeader('Access-Control-Allow-Headers', '*'); } } exports.LocalLambda = LocalLambda; //# sourceMappingURL=local.lambda.js.map