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.

2 lines 12.5 kB
import{d as R,g as l,h as A,i as E,j as v,l as b,t as i,u as f,w as g}from"../../chunk-6BLWJRLG.mjs";import{a as d}from"../../chunk-QWPVIX2T.mjs";var F=class p{constructor(e){this.options=e;this.stripPathFn=g(this.options?.stripBasePath)}static{d(this,"AlbAdapter")}stripPathFn;getAdapterName(){return p.name}canHandle(e){let t=e;return!!(t?.requestContext&&t.requestContext.elb)}getRequest(e){let t=e.httpMethod,r=this.getPathFromEvent(e),o=e.multiValueHeaders?A(e.multiValueHeaders,",",!0):e.headers,n;if(e.body){let[a,u]=l(e.body,e.isBase64Encoded);n=a,o["content-length"]=String(u)}let s="";return o["x-forwarded-for"]&&(s=o["x-forwarded-for"]),{method:t,headers:o,body:n,remoteAddress:s,path:r}}getResponse({event:e,headers:t,body:r,isBase64Encoded:o,statusCode:n}){let s=e.headers?void 0:E(t),a=e.headers?A(t):void 0;return a&&a["transfer-encoding"]==="chunked"&&delete a["transfer-encoding"],s&&s["transfer-encoding"]?.includes("chunked")&&delete s["transfer-encoding"],{statusCode:n,body:r,headers:a,multiValueHeaders:s,isBase64Encoded:o}}onErrorWhileForwarding({error:e,delegatedResolver:t,respondWithErrors:r,event:o,log:n}){let s=r&&e.stack||"",a=this.getResponse({event:o,statusCode:500,body:s,headers:{},isBase64Encoded:!1,log:n});t.succeed(a)}getPathFromEvent(e){let t=this.stripPathFn(e.path),r=e.headers?e.queryStringParameters:e.multiValueQueryStringParameters;return f(t,r||{})}};var x=class p{constructor(e){this.options=e;this.stripPathFn=g(this.options?.stripBasePath)}static{d(this,"ApiGatewayV1Adapter")}stripPathFn;getAdapterName(){return p.name}canHandle(e){let t=e;return!!(t?.requestContext&&t.version!=="2.0"&&t.headers&&t.multiValueHeaders&&(t.queryStringParameters===null&&t.multiValueQueryStringParameters===null||t.queryStringParameters&&t.multiValueQueryStringParameters))}getRequest(e){let t=e.httpMethod,r=this.options?.lowercaseRequestHeaders?b(e.headers):{...e.headers};for(let a of Object.keys(e.multiValueHeaders||{})){let u=e.multiValueHeaders[a];!u||u?.length<=1||(r[a]=u.join(","))}let o=this.getPathFromEvent(e),n;if(e.body){let[a,u]=l(e.body,e.isBase64Encoded);n=a,r["content-length"]=u+""}let s=e.requestContext.identity.sourceIp;return{method:t,headers:r,body:n,remoteAddress:s,path:o}}getResponse({headers:e,body:t,isBase64Encoded:r,statusCode:o,response:n}){let s=E(e),a=i(this.options?.throwOnChunkedTransferEncoding,!0);if(s["transfer-encoding"]?.some(m=>m.includes("chunked"))||n?.chunkedEncoding){if(a)throw new Error("chunked encoding in headers is not supported by API Gateway V1");delete s["transfer-encoding"]}return{statusCode:o,body:t,multiValueHeaders:s,isBase64Encoded:r}}onErrorWhileForwarding({error:e,delegatedResolver:t,respondWithErrors:r,event:o,log:n}){let s=r?e.stack:"",a=this.getResponse({event:o,statusCode:500,body:s||"",headers:{},isBase64Encoded:!1,log:n});t.succeed(a)}getPathFromEvent(e){let t=this.stripPathFn(e.path),r=e.multiValueQueryStringParameters||{};if(e.queryStringParameters)for(let o of Object.keys(e.queryStringParameters)){let n=e.queryStringParameters[o];n!==void 0&&(Array.isArray(r[o])||(r[o]=[]),!r[o].includes(n)&&r[o].push(n))}return f(t,r)}};var C=class p{constructor(e){this.options=e;this.stripPathFn=g(this.options?.stripBasePath)}static{d(this,"ApiGatewayV2Adapter")}stripPathFn;getAdapterName(){return p.name}canHandle(e){let t=e;return!!(t?.requestContext&&t.version==="2.0")}getRequest(e){let t=e.requestContext.http.method,r=this.getPathFromEvent(e),o={...e.headers};e.cookies&&(o.cookie=e.cookies.join("; "));let n;if(e.body){let[a,u]=l(e.body,e.isBase64Encoded);n=a,o["content-length"]=u+""}let s=e.requestContext.http.sourceIp;return{method:t,headers:o,body:n,remoteAddress:s,path:r}}getResponse({headers:e,body:t,isBase64Encoded:r,statusCode:o,response:n}){let{cookies:s,headers:a}=v(e),u=i(this.options?.throwOnChunkedTransferEncoding,!0),c=a["transfer-encoding"];if(c&&c.includes("chunked")||n?.chunkedEncoding){if(u)throw new Error("chunked encoding in headers is not supported by API Gateway V2");delete a["transfer-encoding"]}return{statusCode:o,body:t,headers:a,isBase64Encoded:r,cookies:s}}onErrorWhileForwarding({error:e,delegatedResolver:t,respondWithErrors:r,event:o,log:n}){let s=r?e.stack:"",a=this.getResponse({event:o,statusCode:500,body:s||"",headers:{},isBase64Encoded:!1,log:n});t.succeed(a)}getPathFromEvent(e){let t=this.stripPathFn(e.rawPath),r=e.rawQueryString;return f(t,r||{})}};var h=class{constructor(e){this.options=e}static{d(this,"AwsSimpleAdapter")}getAdapterName(){throw new Error("not implemented.")}canHandle(e){throw new Error("not implemented.")}getRequest(e){let t=this.options.forwardPath,r=this.options.forwardMethod,[o,n]=l(JSON.stringify(e),!1),s={host:this.options.host,"content-type":"application/json","content-length":String(n)};return{method:r,headers:s,body:o,path:t}}getResponse({body:e,headers:t,isBase64Encoded:r,event:o,statusCode:n}){if(this.hasInvalidStatusCode(n))throw new Error(JSON.stringify({body:e,headers:t,isBase64Encoded:r,event:o,statusCode:n}));if(!this.options.batch)return R;if(r)throw new Error("SERVERLESS_ADAPTER: The response could not be base64 encoded when you set batch: true, the response should be a JSON.");return e?JSON.parse(e):R}onErrorWhileForwarding({error:e,delegatedResolver:t}){t.fail(e)}hasInvalidStatusCode(e){return e<200||e>=400}};var B=class p extends h{static{d(this,"DynamoDBAdapter")}constructor(e){super({forwardPath:i(e?.dynamoDBForwardPath,"/dynamo"),forwardMethod:i(e?.dynamoDBForwardMethod,"POST"),batch:e?.batch,host:"dynamodb.amazonaws.com"})}getAdapterName(){return p.name}canHandle(e){let t=e;return Array.isArray(t?.Records)?t.Records[0]?.eventSource==="aws:dynamodb":!1}};var q=class p extends h{static{d(this,"EventBridgeAdapter")}constructor(e){super({forwardPath:i(e?.eventBridgeForwardPath,"/eventbridge"),forwardMethod:i(e?.eventBridgeForwardMethod,"POST"),batch:!1,host:"events.amazonaws.com"})}getAdapterName(){return p.name}canHandle(e){let t=e;return!!(t&&t.version&&t.version==="0"&&t.id&&t["detail-type"]&&t.source&&t.account&&t.time&&t.region&&t.resources&&Array.isArray(t.resources)&&t.detail&&typeof t.detail=="object"&&!Array.isArray(t.detail))}};var P=["Connection","Expect","Keep-Alive","Proxy-Authenticate","Proxy-Authorization","Proxy-Connection","Trailer","Upgrade","X-Accel-Buffering","X-Accel-Charset","X-Accel-Limit-Rate","X-Accel-Redirect",/(X-Amz-Cf-)(.*)/gim,"X-Cache",/(X-Edge-)(.*)/gim,"X-Forwarded-Proto","X-Real-IP"],S=1024*40,w=1024*1024,O=class p{constructor(e){this.options=e;let t=i(this.options?.disallowedHeaders,P);this.cachedDisallowedHeaders=t.map(r=>r instanceof RegExp?r:new RegExp(`(${r})`,"gim"))}static{d(this,"LambdaEdgeAdapter")}cachedDisallowedHeaders;getAdapterName(){return p.name}canHandle(e){let t=e;if(!Array.isArray(t?.Records))return!1;let r=t.Records[0]?.cf?.config?.eventType;return["origin-response","origin-request","viewer-response","viewer-request"].includes(r)}getRequest(e){let t=e.Records[0],r=t.cf.request,o=r.method,n=this.options?.getPathFromEvent?this.options.getPathFromEvent(t):void 0,s=f(r.uri,r.querystring),a=i(n,s),u=r.clientIp,c=this.getFlattenedHeadersFromCloudfrontRequest(r),m;if(r.body){let[k,L]=l(JSON.stringify(r.body),!1);m=k,c["content-length"]=L.toString()}let{host:y}=c;return{method:o,path:a,headers:c,body:m,remoteAddress:u,host:y,hostname:y}}getResponse(e){let t=this.getResponseToLambdaEdge(e),r=new TextEncoder().encode(JSON.stringify(t)).length,n=this.isEventTypeOrigin(e.event.Records[0].cf.config)?i(this.options?.originMaxResponseSizeInBytes,w):i(this.options?.viewerMaxResponseSizeInBytes,S);return r<=n||(this.options?.onResponseSizeExceedLimit?this.options.onResponseSizeExceedLimit(t):e.log.error(`SERVERLESS_ADAPTER:LAMBDA_EDGE_ADAPTER: Max response size exceeded: ${r} of the max of ${n}.`)),t}onErrorWhileForwarding({error:e,delegatedResolver:t}){t.fail(e)}getFlattenedHeadersFromCloudfrontRequest(e){return Object.keys(e.headers).reduce((t,r)=>{let o=e.headers[r];return t[r]=o.map(n=>n.value).join(","),t},{})}getResponseToLambdaEdge({body:e,headers:t}){let r=i(this.options?.shouldUseHeadersFromFramework,!1),o=JSON.parse(e);return o.headers&&(o.headers=Object.keys(o.headers).reduce((n,s)=>(this.shouldStripHeader(s)||(n[s]=o.headers[s]),n),{})),r&&(o.headers=this.getHeadersForCloudfrontResponse(t)),o}getHeadersForCloudfrontResponse(e){return Object.keys(e).reduce((t,r)=>{if(this.shouldStripHeader(r))return t;t[r]||(t[r]=[]);let o=e[r];if(!Array.isArray(o))return t[r].push({key:r,value:o||""}),t;let n=o.map(s=>({key:r,value:s}));return t[r].push(...n),t},{})}shouldStripHeader(e){if(this.options?.shouldStripHeader)return this.options.shouldStripHeader(e);let t=e.toLowerCase();for(let r of this.cachedDisallowedHeaders)if(r.test(t))return!0;return!1}isEventTypeOrigin(e){return e.eventType.includes("origin")}};var H=class p extends h{static{d(this,"S3Adapter")}constructor(e){super({forwardPath:i(e?.s3ForwardPath,"/s3"),forwardMethod:i(e?.s3ForwardMethod,"POST"),batch:!1,host:"s3.amazonaws.com"})}getAdapterName(){return p.name}canHandle(e){let t=e;return Array.isArray(t?.Records)?t.Records[0]?.eventSource==="aws:s3":!1}};var I=class p extends h{static{d(this,"SNSAdapter")}constructor(e){super({forwardPath:i(e?.snsForwardPath,"/sns"),forwardMethod:i(e?.snsForwardMethod,"POST"),batch:!1,host:"sns.amazonaws.com"})}getAdapterName(){return p.name}canHandle(e){let t=e;return Array.isArray(t?.Records)?t.Records[0]?.EventSource==="aws:sns":!1}};var T=class p extends h{static{d(this,"SQSAdapter")}constructor(e){super({forwardPath:i(e?.sqsForwardPath,"/sqs"),forwardMethod:i(e?.sqsForwardMethod,"POST"),batch:e?.batch,host:"sqs.amazonaws.com"})}getAdapterName(){return p.name}canHandle(e){let t=e;return Array.isArray(t?.Records)?t.Records[0]?.eventSource==="aws:sqs":!1}};var D=class p{constructor(e){this.options=e;this.stripPathFn=g(this.options?.stripBasePath);let t=i(this.options?.disallowedHeaders,P);this.cachedDisallowedHeaders=t.map(r=>r instanceof RegExp?r:new RegExp(`(${r})`,"gim"))}static{d(this,"RequestLambdaEdgeAdapter")}stripPathFn;cachedDisallowedHeaders;getAdapterName(){return p.name}canHandle(e){let t=e;if(!Array.isArray(t?.Records))return!1;let r=t.Records[0]?.cf?.config?.eventType;return r==="viewer-request"||r==="origin-request"}getRequest(e){let r=e.Records[0].cf.request,o=r.method,n=this.stripPathFn(f(r.uri,r.querystring)),s=r.clientIp,a=this.getFlattenedHeadersFromCloudfrontRequest(r),u;if(r.body){let[m,y]=l(r.body.data,r.body.encoding==="base64");u=m,a["content-length"]=y.toString()}let{host:c}=a;return{method:o,path:n,headers:a,body:u,remoteAddress:s,host:c,hostname:c}}getResponse({body:e,headers:t,isBase64Encoded:r,statusCode:o,log:n,event:s}){let a=this.getHeadersForCloudfrontResponse(t),u=s.Records[0].cf.config.eventType==="origin-request"?i(this.options?.originMaxResponseSizeInBytes,w):i(this.options?.viewerMaxResponseSizeInBytes,S),c={body:e,status:o.toString(),bodyEncoding:r?"base64":"text",headers:a},m=e.length;return m<=u||(this.options?.onResponseSizeExceedLimit?this.options.onResponseSizeExceedLimit(c):n.error(`SERVERLESS_ADAPTER:LAMBDA_EDGE_ADAPTER: Max response size exceeded: ${m} of the max of ${u}.`)),c}onErrorWhileForwarding({error:e,delegatedResolver:t,respondWithErrors:r,log:o,event:n}){let s=r?e.stack:"",a=this.getResponse({event:n,statusCode:500,body:s||"",headers:{},isBase64Encoded:!1,log:o});t.succeed(a)}getFlattenedHeadersFromCloudfrontRequest(e){return Object.keys(e.headers).reduce((t,r)=>{let o=e.headers[r];return o.length===1?t[r]=o[0].value:t[r]=o.map(n=>n.value).join(","),t},{})}getHeadersForCloudfrontResponse(e){return Object.keys(e).reduce((t,r)=>{if(this.shouldStripHeader(r))return t;let o=r.toLowerCase();t[o]||(t[o]=[]);let n=e[r];if(!Array.isArray(n))return t[o].push({key:r,value:n||""}),t;let s=n.map(a=>({key:r,value:a}));return t[o].push(...s),t},{})}shouldStripHeader(e){if(this.options?.shouldStripHeader)return this.options.shouldStripHeader(e);let t=e.toLowerCase();for(let r of this.cachedDisallowedHeaders)if(r.test(t))return!0;return!1}};export{F as AlbAdapter,x as ApiGatewayV1Adapter,C as ApiGatewayV2Adapter,h as AwsSimpleAdapter,P as DEFAULT_LAMBDA_EDGE_DISALLOWED_HEADERS,w as DEFAULT_ORIGIN_MAX_RESPONSE_SIZE_IN_BYTES,S as DEFAULT_VIEWER_MAX_RESPONSE_SIZE_IN_BYTES,B as DynamoDBAdapter,q as EventBridgeAdapter,O as LambdaEdgeAdapter,D as RequestLambdaEdgeAdapter,H as S3Adapter,I as SNSAdapter,T as SQSAdapter}; //# sourceMappingURL=index.mjs.map