UNPKG

@h4ad/serverless-adapter

Version:

Run REST APIs and other web applications using your existing Node.js application framework (NestJS, Express, Koa, Hapi, Fastify and many others), on top of AWS, Azure, Digital Ocean and many other clouds.

13 lines 8.12 kB
import{a as o}from"./chunk-QWPVIX2T.mjs";import{IncomingMessage as j}from"node:http";var G=["gzip","deflate","br"],J=["image/png","image/jpeg","image/jpg","image/avif","image/bmp","image/x-png","image/gif","image/webp","video/mp4","application/pdf"],X={};var S={context:null,event:null};function ee(){return S}o(ee,"getCurrentInvoke");function re({event:r,context:e}){S.event=r,S.context=e}o(re,"setCurrentInvoke");function oe(r,e){let t=e?"base64":"utf8",n=Buffer.from(r,t),s=Buffer.byteLength(n,t);return[n,s]}o(oe,"getEventBodyAsBuffer");function ae(r,e=",",t=!1){return Object.keys(r).reduce((n,s)=>{let i=t?s.toLowerCase():s,a=r[s];return Array.isArray(a)?n[i]=a.join(e):n[i]=(a??"")+"",n},{})}o(ae,"getFlattenedHeadersMap");function fe(r){return Object.keys(r).reduce((e,t)=>{let n=r[t];return e[t.toLowerCase()]=Array.isArray(n)?n.map(String):[String(n)],e},{})}o(fe,"getMultiValueHeadersMap");function ue(r){return Object.keys(r).reduce((e,t)=>{let n=r[t],s=t.toLowerCase();return Array.isArray(n)?s!=="set-cookie"?e.headers[t]=n.join(","):e.cookies.push(...n):s==="set-cookie"&&n!==void 0?e.cookies.push(n??""):e.headers[t]=String(n??""),e},{cookies:[],headers:{}})}o(ue,"getFlattenedHeadersMapAndCookies");function b(r){if(!r)return{};let e={},t=r.trim().split(` `);for(let n=0;n<t.length;n++){let s=t[n],i=s.indexOf(":"),a=s.slice(0,i).trim().toLowerCase(),u=s.slice(i+1).trim();typeof e[a]>"u"?e[a]=u:Array.isArray(e[a])?e[a].push(u):e[a]=[e[a],u]}return e}o(b,"parseHeaders");function de(r){let e={};for(let[t,n]of Object.entries(r))e[t.toLowerCase()]=n;return e}o(de,"keysToLowercase");function O(r,e){let t=r["content-encoding"];return t?(Array.isArray(t)||(t=t.split(",")),t.some(n=>e.includes(n.trim()))):!1}o(O,"isContentEncodingBinary");function I(r){let e=r["content-type"],t=Array.isArray(e)?e[0]||"":e||"";if(!e)return"";let n=t.indexOf(";");return n===-1?t:t.slice(0,n)}o(I,"getContentType");function P(r,e){let t=I(r);return t?e.includes(t.trim()):!1}o(P,"isContentTypeBinary");function ce(r,e){return"isBinary"in e?e.isBinary===!1?!1:e.isBinary(r):O(r,e.contentEncodings)||P(r,e.contentTypes)}o(ce,"isBinary");var f=o(()=>{},"NO_OP");var w=Symbol("InternalLogger"),_={debug:[["debug","debug"],["verbose","debug"],["info","info"],["error","error"],["warn","warn"]],verbose:[["verbose","debug"],["info","info"],["error","error"],["warn","warn"]],info:[["info","info"],["error","error"],["warn","warn"]],warn:[["warn","warn"],["error","error"]],error:[["error","error"]],none:[]},D=o(r=>typeof r=="function"?r():r,"lazyPrint"),V=o(r=>(e,...t)=>console[r](e,...t.map(D)),"print");function ve({level:r}={level:"error"}){let e=_[r];if(!e)throw new Error("Invalid log level");let t={[w]:!0,error:f,debug:f,info:f,verbose:f,warn:f};for(let[n,s]of e)t[n]=V(s);return t}o(ve,"createDefaultLogger");function Se(r){return!!r[w]}o(Se,"isInternalLogger");function Re(r,e){return r===void 0?e:r}o(Re,"getDefaultIfUndefined");function Ae(r,e){if(String(e||"").length===0)return r;if(typeof e=="string")return`${r}?${e}`;let t=F(e);return t?`${r}?${t}`:r}o(Ae,"getPathWithQueryStringParams");function F(r){let e=new URLSearchParams,t=Object.entries(r||{});if(t.length===0)return"";for(let[n,s]of t){if(!Array.isArray(s)){e.append(n,s||"");continue}for(let i of s)e.append(n,i)}return e.toString()}o(F,"getQueryParamsStringFromRecord");var N=o(r=>r,"NOOPBasePath");function Ce(r){if(!r)return N;let e=r.length;return t=>t.startsWith(r)?t.slice(t.indexOf(r)+e,t.length)||"/":t}o(Ce,"buildStripBasePath");import"node:stream";function W(r){return!!("readableEnded"in r&&r.readableEnded||"writableEnded"in r&&r.writableEnded)}o(W,"isStreamEnded");function Ie(r){return W(r)?Promise.resolve(r):new Promise((e,t)=>{let n=!1;function s(i){n||(n=!0,r.removeListener("error",s),r.removeListener("end",s),r.removeListener("finish",s),i?t(i):e(r))}o(s,"complete"),r.once("error",s),r.once("end",s),r.once("finish",s)})}o(Ie,"waitForStreamComplete");var U=443,y=class extends j{static{o(this,"ServerlessRequest")}constructor({method:e,url:t,headers:n,body:s,remoteAddress:i}){super({encrypted:!0,readable:!0,remoteAddress:i,address:o(()=>({port:U}),"address"),on:f,removeListener:f,removeEventListener:f,end:f,destroy:f}),this.statusCode=200,this.statusMessage="OK",this.complete=!0,this.httpVersion="1.1",this.httpVersionMajor=1,this.httpVersionMinor=1,this.method=e,this.headers=n,this.body=s,this.url=t,this.ip=i,this._read=()=>{this.push(s),this.push(null)}}ip;body};import{ServerResponse as M}from"node:http";function m(r){if(Buffer.isBuffer(r))return r.toString("utf8");if(typeof r=="string")return r;if(r instanceof Uint8Array)return new TextDecoder().decode(r);throw new Error(`response.write() of unexpected type: ${typeof r}`)}o(m,"getString");var A=`\r \r `,$=`0\r \r `,g=Symbol("Response body"),c=Symbol("Response headers");function T(r,e){if(Buffer.isBuffer(e)||typeof e=="string"||e instanceof Uint8Array)r[g].push(Buffer.from(e));else throw new Error(`response.write() of unexpected type: ${typeof e}`)}o(T,"addData");var h=class r extends M{static{o(this,"ServerlessResponse")}constructor({method:e}){super({method:e}),this[g]=[],this[c]={},this.useChunkedEncodingByDefault=!1,this.chunkedEncoding=!1,this._header="";let t=1,n={_writableState:{},writable:!0,on:f,removeListener:f,destroy:f,cork:f,uncork:f,write:o((s,i,a)=>{if(typeof i=="function"&&(a=i,i=null),this._header===""||this._wroteHeader)this.chunkedEncoding?t>0?t--:s!==$&&(T(this,s),t=3):T(this,s);else{let u=m(s),d=u.indexOf(A);if(d!==-1){let l=u.slice(d+A.length);l&&!this.chunkedEncoding&&T(this,l),this._wroteHeader=!0}}typeof a=="function"&&a()},"write")};this.assignSocket(n)}_header;_headers;_wroteHeader;[g];[c];get headers(){return this[c]}static from(e){let t=new r({method:e.method});return t.statusCode=e.statusCode||0,t[c]=e.headers,t[g]=e.body?[Buffer.from(e.body)]:[],t.end(),t}static body(e){return Buffer.concat(e[g])}static headers(e){let t=e.getHeaders();return Object.assign(t,e[c])}setHeader(e,t){this._wroteHeader?this[c][e]=t:super.setHeader(e,t)}writeHead(e,t,n){let s=typeof t=="string"?n:t,i=Array.isArray(s)?s:[s||{}];for(let a of i)for(let u in a)if(this.setHeader(u,a[u]),!this._wroteHeader)break;return this.callNativeWriteHead(e,t,n)}callNativeWriteHead(e,t,n){return super.writeHead(e,t,n)}};import{ServerResponse as K}from"node:http";var Y=Buffer.from(`\r `),C=`0\r \r `,q=`\r \r `,z=`\r `,k=class extends K{static{o(this,"ServerlessStreamResponse")}constructor({method:e,onReceiveHeaders:t,log:n}){super({method:e}),this.useChunkedEncodingByDefault=!0,this.chunkedEncoding=!0;let s=null,i=!1,a=!1,u={_writableState:{},writable:!0,on:f,removeListener:f,destroy:f,cork:f,uncork:f,write:o((d,l,x)=>{if(typeof l=="function"&&(x=l,l=null),n.debug("SERVERLESS_ADAPTER:RESPONSE_STREAM:DATA",()=>({data:Buffer.isBuffer(d)?d.toString("utf8"):d,encoding:l})),!s){let p=m(d),v=p.indexOf(z),L=+p.slice(0,v).split(" ")[1],E=p.indexOf(q),H=p.slice(v+2,E),R=b(H);return n.debug("SERVERLESS_ADAPTER:RESPONSE_STREAM:FRAMEWORK_HEADERS",()=>({headers:R})),s=t(L,R),p.substring(E+4)===C&&s.end(),!0}if(d===C)return s.end(x),!0;if(Buffer.isBuffer(d)&&Y.equals(d)){if(!i)return i=!0,!0;if(i&&a)return i=!1,a=!1,!0}return i&&(a=!0,s.write(d,x)),!0},"write")};this.assignSocket(u)}};var B=class{static{o(this,"BaseHandler")}getAdapterByEventAndContext(e,t,n,s){let i=n.filter(a=>a.canHandle(e,t,s));if(i.length===0)throw new Error("SERVERLESS_ADAPTER: Couldn't find adapter to handle this event.");if(i.length>1)throw new Error(`SERVERLESS_ADAPTER: Two or more adapters was resolved by the event, the adapters are: ${n.map(a=>a.getAdapterName()).join(", ")}.`);return i[0]}getServerlessRequestResponseFromAdapterRequest(e){let t=new y({method:e.method,headers:e.headers,body:e.body,remoteAddress:e.remoteAddress,url:e.path}),n=new h({method:e.method});return[t,n]}};export{B as a,G as b,J as c,X as d,ee as e,re as f,oe as g,ae as h,fe as i,ue as j,b as k,de as l,O as m,I as n,P as o,ce as p,f as q,ve as r,Se as s,Re as t,Ae as u,F as v,Ce as w,W as x,Ie as y,y as z,m as A,h as B,k as C}; //# sourceMappingURL=chunk-6BLWJRLG.mjs.map