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