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.1 kB
JavaScript
var Q=Object.defineProperty;var K=Object.getOwnPropertySymbols;var W=Object.prototype.hasOwnProperty,$=Object.prototype.propertyIsEnumerable;var _=s=>{throw TypeError(s)};var T=(s,e,t)=>e in s?Q(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,x=(s,e)=>{for(var t in e||(e={}))W.call(e,t)&&T(s,t,e[t]);if(K)for(var t of K(e))$.call(e,t)&&T(s,t,e[t]);return s};var l=(s,e,t)=>T(s,typeof e!="symbol"?e+"":e,t),F=(s,e,t)=>e.has(s)||_("Cannot "+t);var u=(s,e,t)=>(F(s,e,"read from private field"),t?t.call(s):e.get(s)),w=(s,e,t)=>e.has(s)?_("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(s):e.set(s,t),v=(s,e,t,o)=>(F(s,e,"write to private field"),o?o.call(s,t):e.set(s,t),t),R=(s,e,t)=>(F(s,e,"access private method"),t);var E=(s,e,t)=>new Promise((o,i)=>{var p=a=>{try{n(t.next(a))}catch(b){i(b)}},r=a=>{try{n(t.throw(a))}catch(b){i(b)}},n=a=>a.done?o(a.value):Promise.resolve(a.value).then(p,r);n((t=t.apply(s,e)).next())});import Y from"events";var G=s=>{var t,o,i,p,r,n;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=(i=(o=e.requestContext)==null?void 0:o.http)==null?void 0:i.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=(n=e.headers["x-forwarded-proto"])!=null?n: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 P=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/,z=function(e){return encodeURIComponent(e)};function J(s){return Object.prototype.toString.call(s)==="[object Date]"||s instanceof Date}var L={serialize:(s,e,t)=>{let o=t||{},i=o.encode||z;if(typeof i!="function")throw new TypeError("option encode is invalid");if(!P.test(s))throw new TypeError("argument name is invalid");let p=i(e);if(p&&!P.test(p))throw new TypeError("argument val is invalid");let r=s+"="+p;if(o.maxAge!=null){let n=o.maxAge-0;if(isNaN(n)||!isFinite(n))throw new TypeError("option maxAge is invalid");r+="; Max-Age="+Math.floor(n)}if(o.domain){if(!P.test(o.domain))throw new TypeError("option domain is invalid");r+="; Domain="+o.domain}if(o.path){if(!P.test(o.path))throw new TypeError("option path is invalid");r+="; Path="+o.path}if(o.expires){let n=o.expires;if(!J(n)||isNaN(n.valueOf()))throw new TypeError("option expires is invalid");r+="; Expires="+n.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 X=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("")}},N,H,A,S,m,g,I,V,M,q=class{constructor(e){w(this,g);l(this,"locals");l(this,"callbackWaitsForEmptyEventLoop");w(this,N);w(this,H);w(this,A);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,N,e.context.succeed),v(this,H,e.context.fail),v(this,A,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,N).call(this,e)}done(e,t){u(this,A).call(this,e,t)}fail(e){u(this,H).call(this,e)}cookie(e,t,o){return Array.isArray(this.responseObject.cookies)?this.responseObject.cookies.push(L.serialize(e,t,o)):this.responseObject.cookies=[L.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,I).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,M).call(this,t,o)}else for(var i in t)R(this,g,M).call(this,i,t[i]);return this}get(e){return this.responseObject.headers[e]}json(e){var t,o;R(o=R(t=this.type("application/json"),g,V).call(t,JSON.stringify(e)),g,I).call(o)}send(e){var t;R(t=R(this,g,V).call(this,e),g,I).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 i;let t=isNaN(e[0])?302:e[0],o=typeof e[0]=="string"?e[0]:typeof e[1]=="string"?e[1]:"/";R(i=this.status(t).location(o),g,I).call(i)}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}};N=new WeakMap,H=new WeakMap,A=new WeakMap,S=new WeakMap,m=new WeakMap,g=new WeakSet,I=function(){var t,o,i,p,r;if(Object.keys(this.responseObject.headers).find(n=>n.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),(i=u(this,m).requestContext)!=null&&i.elb&&!u(this,m).multiValueHeaders&&!u(this,m).multiValueQueryStringParameters&&(this.responseObject.cookies.forEach((n,a)=>{R(this,g,M).call(this,X(a),n)}),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[n,a]of Object.entries(this.responseObject.headers))this.responseObject.multiValueHeaders[n]=[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))},V=function(e){return e instanceof Buffer?(this.responseObject.body=e.toString("base64"),this.responseObject.isBase64Encoded=!0):this.responseObject.body=e,this},M=function(e,t){this.responseObject.headers[e]=t};var B=(s,e)=>{var t;if(e){let o=s.findIndex(i=>i.length==4);if(o!=-1){let i=s[o];return s.splice(0,o),i}}else return((t=s[0])==null?void 0:t.length)<4?s[0]:void 0},{IS_LOCAL:Z}=process.env,ee=process.env.NODE_ENV=="production",U=class extends Function{constructor(){super();l(this,"controllers",[]);return new Proxy(this,{apply:(t,o,i)=>t._call(...i)})}_call(...t){return E(this,null,function*(){let o=[...this.controllers],i=G(t[0]),p=t[1],r=t[2],n=null,a=new Y,b=y=>{if(!n){if(typeof y=="object"){n=x({},y);for(let f of Object.keys(n))Array.isArray(n[f])?n[f]=[...y[f]]:n[f]&&typeof n[f]=="object"&&(n[f]=x({},y[f]))}else n=y;a.emit("end")}},c=(y,f,C,O)=>E(this,null,function*(){try{o.shift();let k=B(o,y);k?yield k(y,f,C,O):(Z&&console.log(y),b({statusCode:500,body:"Internal Server Error"}))}catch(k){c(k,f,C,O)}}),d=new q({context:p,resolve:b,req:i,locals:{},callback:r}),h,j=y=>E(this,null,function*(){h=y,o.shift();let f=x({},d.locals),C=x({},d.responseObject);d=new q({context:p,resolve:b,locals:f,req:i,previousResponse:C,callback:r});let O=B(o,h);if(O){try{O.length==4?yield O(h,i,d,j):yield O(i,d,j)}catch(k){yield c(k,i,d,j)}!n&&!o.length&&d.status(404).send(`Cannot ${i.method} ${i.path}`)}else{let k,D=500;ee?(k="Not Found",D=404):h?k=h.stack?h.stack:h:k="Cannot find route controller",d.status(D).send(`<pre>${k}</pre>`)}});return yield new Promise(y=>E(this,null,function*(){a.once("end",y);let f=B(o);if(f)try{yield f(i,d,j)}catch(C){yield c(C,i,d,j),y(void 0)}else y(void 0)})),n!=null?n:{statusCode:204}})}handle(...t){return this.controllers.push(...t),this}use(...t){return this.controllers.push(...t),this}},de=()=>new U;export{de as Router};