UNPKG

serverless-aws-lambda

Version:

AWS Application Load Balancer and API Gateway - Lambda dev tool for Serverless. Allows Express synthax in handlers. Supports packaging, local invoking and offline ALB, APG, S3, SNS, SQS, DynamoDB Stream server mocking.

2 lines (1 loc) 8.87 kB
"use strict";var E=Object.create;var g=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var N=Object.getPrototypeOf,H=Object.prototype.hasOwnProperty;var P=(o,e)=>{for(var t in e)g(o,t,{get:e[t],enumerable:!0})},v=(o,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of q(e))!H.call(o,r)&&r!==t&&g(o,r,{get:()=>e[r],enumerable:!(s=I(e,r))||s.enumerable});return o};var A=(o,e,t)=>(t=o!=null?E(N(o)):{},v(e||!o||!o.__esModule?g(t,"default",{value:o,enumerable:!0}):t,o)),M=o=>v(g({},"__esModule",{value:!0}),o);var D={};P(D,{Router:()=>U});module.exports=M(D);var j=A(require("events"));var S=o=>{let e={...o};try{if(delete e.cookies,e.method=o.httpMethod,e.query={},o.multiValueQueryStringParameters)for(let[r,i]of Object.entries(o.multiValueQueryStringParameters)){let n=i.map(decodeURIComponent);n.length==1?e.query[r]=n[0]:e.query[r]=n}else if(o.queryStringParameters)for(let[r,i]of Object.entries(o.queryStringParameters))e.query[r]=decodeURIComponent(i);let t={};if(o.multiValueHeaders)for(let[r,i]of Object.entries(o.multiValueHeaders)){let n=i.map(decodeURIComponent);n.length==1?t[r]=n[0]:t[r]=n}else if(o.headers)for(let[r,i]of Object.entries(o.headers))t[r]=decodeURIComponent(i);e.headers=t,e.get=r=>e.headers[r.toLowerCase()],e.path=e.path??e.rawPath,e.requestContext&&(e.method||(e.method=e.requestContext?.http?.method),e.path||(e.path=e.requestContext?.http?.path));let s=e.path?decodeURIComponent(e.path):void 0;if(e.params=s?.split("/").filter(r=>r),e.protocol=e.headers["x-forwarded-proto"]??void 0,e.secure=e.protocol?e.protocol=="https":void 0,!e.isBase64Encoded&&e.headers["content-type"]=="application/json"){let r=JSON.parse(o.body);e.body=r}}catch(t){console.log(t)}return e};var b=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/,T=function(e){return encodeURIComponent(e)};function F(o){return Object.prototype.toString.call(o)==="[object Date]"||o instanceof Date}var R={serialize:(o,e,t)=>{let s=t||{},r=s.encode||T;if(typeof r!="function")throw new TypeError("option encode is invalid");if(!b.test(o))throw new TypeError("argument name is invalid");let i=r(e);if(i&&!b.test(i))throw new TypeError("argument val is invalid");let n=o+"="+i;if(s.maxAge!=null){let l=s.maxAge-0;if(isNaN(l)||!isFinite(l))throw new TypeError("option maxAge is invalid");n+="; Max-Age="+Math.floor(l)}if(s.domain){if(!b.test(s.domain))throw new TypeError("option domain is invalid");n+="; Domain="+s.domain}if(s.path){if(!b.test(s.path))throw new TypeError("option path is invalid");n+="; Path="+s.path}if(s.expires){let l=s.expires;if(!F(l)||isNaN(l.valueOf()))throw new TypeError("option expires is invalid");n+="; Expires="+l.toUTCString()}if(s.httpOnly&&(n+="; HttpOnly"),s.secure&&(n+="; Secure"),s.priority)switch(typeof s.priority=="string"?s.priority.toLowerCase():s.priority){case"low":n+="; Priority=Low";break;case"medium":n+="; Priority=Medium";break;case"high":n+="; Priority=High";break;default:throw new TypeError("option priority is invalid")}if(s.sameSite)switch(typeof s.sameSite=="string"?s.sameSite.toLowerCase():s.sameSite){case!0:n+="; SameSite=Strict";break;case"lax":n+="; SameSite=Lax";break;case"strict":n+="; SameSite=Strict";break;case"none":n+="; SameSite=None";break;default:throw new TypeError("option sameSite is invalid")}return n}};var L=o=>{if(o==0||o>8)return"Set-Cookie";{let e=["s","e","t","c","o","o","k","i","e"];return e[o]=e[o].toUpperCase(),[...e.slice(0,3),"-",...e.slice(3)].join("")}},m=class{locals;callbackWaitsForEmptyEventLoop;#n;#r;#i;functionVersion;functionName;memoryLimitInMB;logGroupName;logStreamName;clientContext;identity;invokedFunctionArn;awsRequestId;getRemainingTimeInMillis;callback;responseObject={cookies:[],isBase64Encoded:!1,statusCode:200,headers:{},body:""};#s;#e;constructor(e){this.locals=e.locals,this.callbackWaitsForEmptyEventLoop=e.context.callbackWaitsForEmptyEventLoop,this.#n=e.context.succeed,this.#r=e.context.fail,this.#i=e.context.done,this.functionVersion=e.context.functionVersion,this.functionName=e.context.functionName,this.memoryLimitInMB=e.context.memoryLimitInMB,this.logGroupName=e.context.logGroupName,this.logStreamName=e.context.logStreamName,this.clientContext=e.context.clientContext,this.identity=e.context.identity,this.invokedFunctionArn=e.context.invokedFunctionArn,this.awsRequestId=e.context.awsRequestId,this.awsRequestId=e.context.awsRequestId,this.getRemainingTimeInMillis=e.context.getRemainingTimeInMillis,this.callback=e.callback,this.#s=e.resolve,e.previousResponse&&(this.responseObject=e.previousResponse),e.req&&(this.#e=e.req)}succeed(e){this.#n(e)}done(e,t){this.#i(e,t)}fail(e){this.#r(e)}#t(){if(Object.keys(this.responseObject.headers).find(t=>t.toLowerCase()=="content-type")||(this.#e.requestContext?.elb?this.type("text/html; charset=utf-8"):this.type("application/json")),this.responseObject.cookies?.length?(this.#e.version=="1.0"&&(this.responseObject.multiValueHeaders={},this.responseObject.multiValueHeaders["Set-Cookie"]=[...this.responseObject.cookies],delete this.responseObject.cookies),this.#e.requestContext?.elb&&!this.#e.multiValueHeaders&&!this.#e.multiValueQueryStringParameters&&(this.responseObject.cookies.forEach((t,s)=>{this.#o(L(s),t)}),delete this.responseObject.cookies)):delete this.responseObject.cookies,this.#e.requestContext?.elb&&(this.#e.multiValueHeaders||this.#e.multiValueQueryStringParameters)){this.responseObject.multiValueHeaders={};for(let[t,s]of Object.entries(this.responseObject.headers))this.responseObject.multiValueHeaders[t]=[s];this.responseObject.cookies?.length&&(this.responseObject.multiValueHeaders["Set-Cookie"]=[...this.responseObject.cookies],delete this.responseObject.cookies),delete this.responseObject.headers}this.#s({...this.responseObject})}#a(e){return e instanceof Buffer?(this.responseObject.body=e.toString("base64"),this.responseObject.isBase64Encoded=!0):this.responseObject.body=e,this}cookie(e,t,s){return Array.isArray(this.responseObject.cookies)?this.responseObject.cookies.push(R.serialize(e,t,s)):this.responseObject.cookies=[R.serialize(e,t,s)],this}clearCookie(e,t){let s={expires:new Date(1),path:"/",...t};return this.cookie(e,"",s)}status(e){return this.responseObject.statusCode=e,this}sendStatus(e){this.status(e).#t()}type(e){return this.responseObject.headers["Content-Type"]=e,this}#o(e,t){this.responseObject.headers[e]=t}set(...e){let t=e[0];if(e.length===2){let i=e[1];var s=Array.isArray(i)?i.map(String):String(i);this.#o(t,s)}else for(var r in t)this.#o(r,t[r]);return this}setHeader=this.set;get(e){return this.responseObject.headers[e]}getHeader=this.get;json(e){this.type("application/json").#a(JSON.stringify(e)).#t()}send(e){this.#a(e).#t()}end(e){let t;Array.isArray(e)?t=[...e]:e&&typeof e=="object"?t={...e}:t=e,this.#s(t)}redirect(...e){let t=isNaN(e[0])?302:e[0],s=typeof e[0]=="string"?e[0]:typeof e[1]=="string"?e[1]:"/";this.status(t).location(s).#t()}location(e){let t=e;return e==="back"&&(t=this.#e.get("Referrer")||this.#e.get("Referer")||"/"),this.set("Location",encodeURI(t))}links(e){let t=this.get("Link")||"";return t&&(t+=", "),this.set("Link",t+Object.keys(e).map(function(s){return"<"+e[s]+'>; rel="'+s+'"'}).join(", "))}get cookies(){return this.responseObject.cookies}get headers(){return this.responseObject.headers}};var O=(o,e)=>{if(e){let t=o.findIndex(s=>s.length==4);if(t!=-1){let s=o[t];return o.splice(0,t),s}}else return o[0]?.length<4?o[0]:void 0},{IS_LOCAL:V}=process.env,B=process.env.NODE_ENV=="production",w=class extends Function{controllers=[];constructor(){return super(),new Proxy(this,{apply:(e,t,s)=>e._call(...s)})}async _call(...e){let t=[...this.controllers],s=S(e[0]),r=e[1],i=e[2],n=null,l=new j.default,k=a=>{if(!n){if(typeof a=="object"){n={...a};for(let c of Object.keys(n))Array.isArray(n[c])?n[c]=[...a[c]]:n[c]&&typeof n[c]=="object"&&(n[c]={...a[c]})}else n=a;l.emit("end")}},x=async(a,c,y,h)=>{try{t.shift();let p=O(t,a);p?await p(a,c,y,h):(V&&console.log(a),k({statusCode:500,body:"Internal Server Error"}))}catch(p){x(p,c,y,h)}},d=new m({context:r,resolve:k,req:s,locals:{},callback:i}),u,f=async a=>{u=a,t.shift();let c={...d.locals},y={...d.responseObject};d=new m({context:r,resolve:k,locals:c,req:s,previousResponse:y,callback:i});let h=O(t,u);if(h){try{h.length==4?await h(u,s,d,f):await h(s,d,f)}catch(p){await x(p,s,d,f)}!n&&!t.length&&d.status(404).send(`Cannot ${s.method} ${s.path}`)}else{let p,C=500;B?(p="Not Found",C=404):u?p=u.stack?u.stack:u:p="Cannot find route controller",d.status(C).send(`<pre>${p}</pre>`)}};return await new Promise(async a=>{l.once("end",a);let c=O(t);if(c)try{await c(s,d,f)}catch(y){await x(y,s,d,f),a(void 0)}else a(void 0)}),n??{statusCode:204}}handle(...e){return this.controllers.push(...e),this}use(...e){return this.controllers.push(...e),this}},U=()=>new w;0&&(module.exports={Router});