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.

7 lines 5.02 kB
"use strict";var l=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var s=(n,e)=>l(n,"name",{value:e,configurable:!0});var H=(n,e)=>{for(var t in e)l(n,t,{get:e[t],enumerable:!0})},B=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of k(e))!C.call(n,r)&&r!==t&&l(n,r,{get:()=>e[r],enumerable:!(o=w(e,r))||o.enumerable});return n};var L=n=>B(l({},"__esModule",{value:!0}),n);var D={};H(D,{HttpTriggerV4Adapter:()=>x});module.exports=L(D);var A=require("url");var P=require("http");var v=require("http");function g(n){if(Buffer.isBuffer(n))return n.toString("utf8");if(typeof n=="string")return n;if(n instanceof Uint8Array)return new TextDecoder().decode(n);throw new Error(`response.write() of unexpected type: ${typeof n}`)}s(g,"getString");var S=`\r \r `,I=`0\r \r `,c=Symbol("Response body"),f=Symbol("Response headers");function m(n,e){if(Buffer.isBuffer(e)||typeof e=="string"||e instanceof Uint8Array)n[c].push(Buffer.from(e));else throw new Error(`response.write() of unexpected type: ${typeof e}`)}s(m,"addData");var y=class n extends v.ServerResponse{static{s(this,"ServerlessResponse")}constructor({method:e}){super({method:e}),this[c]=[],this[f]={},this.useChunkedEncodingByDefault=!1,this.chunkedEncoding=!1,this._header="";let t=1,o={_writableState:{},writable:!0,on:u,removeListener:u,destroy:u,cork:u,uncork:u,write:s((r,i,a)=>{if(typeof i=="function"&&(a=i,i=null),this._header===""||this._wroteHeader)this.chunkedEncoding?t>0?t--:r!==I&&(m(this,r),t=3):m(this,r);else{let d=g(r),p=d.indexOf(S);if(p!==-1){let R=d.slice(p+S.length);R&&!this.chunkedEncoding&&m(this,R),this._wroteHeader=!0}}typeof a=="function"&&a()},"write")};this.assignSocket(o)}_header;_headers;_wroteHeader;[c];[f];get headers(){return this[f]}static from(e){let t=new n({method:e.method});return t.statusCode=e.statusCode||0,t[f]=e.headers,t[c]=e.body?[Buffer.from(e.body)]:[],t.end(),t}static body(e){return Buffer.concat(e[c])}static headers(e){let t=e.getHeaders();return Object.assign(t,e[f])}setHeader(e,t){this._wroteHeader?this[f][e]=t:super.setHeader(e,t)}writeHead(e,t,o){let r=typeof t=="string"?o:t,i=Array.isArray(r)?r:[r||{}];for(let a of i)for(let d in a)if(this.setHeader(d,a[d]),!this._wroteHeader)break;return this.callNativeWriteHead(e,t,o)}callNativeWriteHead(e,t,o){return super.writeHead(e,t,o)}};var O=require("http");var J=Buffer.from(`\r `);function b(n,e){let t=e?"base64":"utf8",o=Buffer.from(n,t),r=Buffer.byteLength(o,t);return[o,r]}s(b,"getEventBodyAsBuffer");function h(n,e=",",t=!1){return Object.keys(n).reduce((o,r)=>{let i=t?r.toLowerCase():r,a=n[r];return Array.isArray(a)?o[i]=a.join(e):o[i]=(a??"")+"",o},{})}s(h,"getFlattenedHeadersMap");var u=s(()=>{},"NO_OP");var ve=Symbol("InternalLogger");function E(n,e){return n===void 0?e:n}s(E,"getDefaultIfUndefined");function T(n,e){if(String(e||"").length===0)return n;if(typeof e=="string")return`${n}?${e}`;let t=_(e);return t?`${n}?${t}`:n}s(T,"getPathWithQueryStringParams");function _(n){let e=new URLSearchParams,t=Object.entries(n||{});if(t.length===0)return"";for(let[o,r]of t){if(!Array.isArray(r)){e.append(o,r||"");continue}for(let i of r)e.append(o,i)}return e.toString()}s(_,"getQueryParamsStringFromRecord");var Ce=require("stream");var x=class n{constructor(e){this.options=e}static{s(this,"HttpTriggerV4Adapter")}getAdapterName(){return n.name}canHandle(e,t){let o=e,r=t;return!!(o&&o.method&&o.headers&&o.url&&o.query&&r&&r.traceContext&&r.bindingDefinitions&&r.log&&r.log.info&&r.bindingData)}getRequest(e){let t=this.getPathFromEvent(e),o=e.method,r=h(e.headers,",",!0),i;if(e.body){let[d,p]=b(e.rawBody,!1);i=d,r["content-length"]=String(p)}let a=r["x-forwarded-for"];return{method:o,path:t,headers:r,remoteAddress:a,body:i}}getResponse({body:e,statusCode:t,headers:o}){let r=h(o,",",!0),i=this.getAzureCookiesFromHeaders(o);return r["set-cookie"]&&delete r["set-cookie"],{body:e,statusCode:t,headers:r,enableContentNegotiation:!1,cookies:i}}onErrorWhileForwarding({error:e,respondWithErrors:t,event:o,delegatedResolver:r,log:i}){let a=t?e.stack:"",d=this.getResponse({event:o,statusCode:500,body:a||"",headers:{},isBase64Encoded:!1,log:i});r.succeed(d)}getPathFromEvent(e){let t=E(this.options?.stripBasePath,""),r=new A.URL(e.url).pathname,i=new RegExp(`^${t}`),a=r.replace(i,""),d=e.query;return T(a,d)}getAzureCookiesFromHeaders(e){let t=e["set-cookie"];return(Array.isArray(t)?t:t?[t]:[]).map(r=>this.parseCookie(r))}parseCookie(e){return e.split(";").reduce((t,o,r)=>{let[i,a]=o.split("="),d=i.toLowerCase().trim(),p=a&&a.trim();if(r===0)return t.name=i,t.value=p,t;switch(d){case"domain":t.domain=p;break;case"path":t.path=p;break;case"expires":t.expires=new Date(p);break;case"secure":t.secure=!0;break;case"httponly":t.httpOnly=!0;break;case"samesite":t.sameSite=p;break;case"max-age":t.maxAge=Number(p);break}return t},{name:"",value:""})}};0&&(module.exports={HttpTriggerV4Adapter}); //# sourceMappingURL=index.cjs.map