@h4ad/serverless-adapter 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +73 -41
  2. package/lib/{handlers/base/base.handler.d.ts → core/base-handler.d.ts} +4 -4
  3. package/lib/{handlers/base/base.handler.js → core/base-handler.js} +1 -1
  4. package/lib/core/index.d.ts +1 -0
  5. package/lib/core/index.js +1 -0
  6. package/lib/frameworks/{express.framework.d.ts → express/express.framework.d.ts} +1 -1
  7. package/lib/frameworks/{express.framework.js → express/express.framework.js} +0 -0
  8. package/lib/frameworks/express/index.d.ts +1 -0
  9. package/lib/frameworks/{index.js → express/index.js} +0 -3
  10. package/lib/frameworks/{fastify.framework.d.ts → fastify/fastify.framework.d.ts} +1 -1
  11. package/lib/frameworks/{fastify.framework.js → fastify/fastify.framework.js} +0 -0
  12. package/lib/frameworks/fastify/index.d.ts +1 -0
  13. package/lib/frameworks/fastify/index.js +13 -0
  14. package/lib/frameworks/{hapi.framework.d.ts → hapi/hapi.framework.d.ts} +1 -1
  15. package/lib/frameworks/{hapi.framework.js → hapi/hapi.framework.js} +0 -0
  16. package/lib/frameworks/hapi/index.d.ts +1 -0
  17. package/lib/{handlers/base → frameworks/hapi}/index.js +1 -1
  18. package/lib/frameworks/koa/index.d.ts +1 -0
  19. package/lib/{adapters → frameworks/koa}/index.js +1 -1
  20. package/lib/frameworks/{koa.framework.d.ts → koa/koa.framework.d.ts} +1 -1
  21. package/lib/frameworks/{koa.framework.js → koa/koa.framework.js} +0 -0
  22. package/lib/handlers/{default.handler.d.ts → default/default.handler.d.ts} +5 -5
  23. package/lib/handlers/{default.handler.js → default/default.handler.js} +4 -4
  24. package/lib/handlers/{index.d.ts → default/index.d.ts} +0 -1
  25. package/lib/handlers/{index.js → default/index.js} +0 -1
  26. package/lib/index.d.ts +0 -4
  27. package/lib/index.js +0 -4
  28. package/lib/network/request.d.ts +2 -2
  29. package/lib/network/request.js +2 -5
  30. package/lib/network/response.d.ts +12 -4
  31. package/lib/network/response.js +27 -16
  32. package/lib/resolvers/{aws-context.resolver.d.ts → aws-context/aws-context.resolver.d.ts} +1 -1
  33. package/lib/resolvers/{aws-context.resolver.js → aws-context/aws-context.resolver.js} +0 -0
  34. package/lib/resolvers/aws-context/index.d.ts +1 -0
  35. package/lib/resolvers/{index.js → aws-context/index.js} +0 -2
  36. package/lib/resolvers/{callback.resolver.d.ts → callback/callback.resolver.d.ts} +1 -1
  37. package/lib/resolvers/{callback.resolver.js → callback/callback.resolver.js} +0 -0
  38. package/lib/resolvers/callback/index.d.ts +1 -0
  39. package/lib/resolvers/callback/index.js +13 -0
  40. package/lib/resolvers/promise/index.d.ts +1 -0
  41. package/lib/resolvers/promise/index.js +13 -0
  42. package/lib/resolvers/{promise.resolver.d.ts → promise/promise.resolver.d.ts} +1 -1
  43. package/lib/resolvers/{promise.resolver.js → promise/promise.resolver.js} +0 -0
  44. package/package.json +1 -1
  45. package/lib/adapters/index.d.ts +0 -1
  46. package/lib/frameworks/index.d.ts +0 -4
  47. package/lib/handlers/base/index.d.ts +0 -1
  48. package/lib/resolvers/index.d.ts +0 -3
package/README.md CHANGED
@@ -18,17 +18,21 @@
18
18
  [![Commitizen Friendly][commitizen-img]][commitizen-url]
19
19
  [![Semantic Release][semantic-release-img]][semantic-release-url]
20
20
 
21
- Run REST APIs and other web applications using your existing Node.js application framework (Express, Koa, Hapi and Fastify)
22
- , on top of AWS Lambda, Amazon API Gateway and many other event sources.
21
+ Run REST APIs and other web applications using your existing Node.js application framework (Express, Koa, Hapi and
22
+ Fastify), on top of AWS Lambda, Amazon API Gateway and many other event sources.
23
23
 
24
- This library was a refactored version of [@vendia/serverless-express](https://github.com/vendia/serverless-express), I created a new way to interact and extend event sources by creating contracts to abstract the integrations between each library layer.
24
+ This library was a refactored version of [@vendia/serverless-express](https://github.com/vendia/serverless-express), I
25
+ created a new way to interact and extend event sources by creating contracts to abstract the integrations between each
26
+ library layer.
25
27
 
26
28
  Why you would use this libray instead of [@vendia/serverless-express](https://github.com/vendia/serverless-express)?
27
29
 
28
30
  - Better APIs to extend library functionality.
29
- - You don't need me to release a new version to integrate with the new event source, you can create an adapter and just call the `addAdapter` method when building your handler.
31
+ - You don't need me to release a new version to integrate with the new event source, you can create an adapter and
32
+ just call the `addAdapter` method when building your handler.
30
33
  - All code can be extended, if you want to modify the current behavior you can.
31
- - This is important because if you find a bug, you can quickly resolve it by extending the class, _and then you can submit a PR to fix the bug_.
34
+ - This is important because if you find a bug, you can quickly resolve it by extending the class, _and then you can
35
+ submit a PR to fix the bug_.
32
36
  - All code was written in Typescript.
33
37
  - We have >99% coverage.
34
38
 
@@ -51,6 +55,11 @@ yarn add @h4ad/serverless-adapter
51
55
  You can quickly use this library as follows:
52
56
 
53
57
  ```ts
58
+ import { ServerlessAdapter } from '@h4ad/serverless-adapter';
59
+ import { ApiGatewayV2Adapter } from '@h4ad/serverless-adapter/lib/adapters/aws';
60
+ import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
61
+ import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
62
+ import { PromiseResolver } from '@h4ad/serverless-adapter/lib/resolvers/promise';
54
63
  import app from './app';
55
64
 
56
65
  export const handler = ServerlessAdapter.new(app)
@@ -67,21 +76,26 @@ export { handler };
67
76
 
68
77
  Too fast? Ok, I can explain.
69
78
 
70
- First, you need to create an instance of the builder with `ServerlessAdapter.new(app)`.
71
- The variable `app` is the instance of your framework (Express, Fastify, Koa, etc).
79
+ First, you need to create an instance of the builder with `ServerlessAdapter.new(app)`. The variable `app` is the
80
+ instance of your framework (Express, Fastify, Koa, etc).
72
81
 
73
- So you need to specify for this library which `framework` you will use to handle the `request` with `setFramework`, you can only use one.
74
- If you use express in our backend, use [ExpressFramework](./src/frameworks/express.framework.ts), see more in [support](#support).
82
+ So you need to specify for this library which `framework` you will use to handle the `request` with `setFramework`, you
83
+ can only use one. If you use express in our backend, use [ExpressFramework](src/frameworks/express/express.framework.ts)
84
+ , see more in [support](#support).
75
85
 
76
- Then you specify which `handler` you will use to interact with the serverless cloud with `setHandler`, currently we only have one, the [DefaultHandler](./src/handlers/default.handler.ts).
77
- In the next releases, maybe we will add support for Azure, Huawei and others, for now, you can use this default.
86
+ Then you specify which `handler` you will use to interact with the serverless cloud with `setHandler`, currently we only
87
+ have one, the [DefaultHandler](src/handlers/default/default.handler.ts). In the next releases, maybe we will add support
88
+ for Azure, Huawei and others, for now, you can use this default.
78
89
 
79
- Then you specify with `setResolver` which `resolver` you will use to wait for the library to forward the request to your `framework`, and then get the `response` back to your cloud.
80
- I recommend you use [PromiseResolver](./src/resolvers/promise.resolver.ts), it's the most cloud-agnostic `resolver`.
90
+ Then you specify with `setResolver` which `resolver` you will use to wait for the library to forward the request to
91
+ your `framework`, and then get the `response` back to your cloud. I recommend you
92
+ use [PromiseResolver](src/resolvers/promise/promise.resolver.ts), it's the most cloud-agnostic `resolver`.
81
93
 
82
- By now, you've already added the basic abstractions of this library, now, you can add the `adapters` that will add support for receiving and processing different sources of `addAdapter` events.
83
- In the example I added [AlbAdapter](./src/adapters/aws/alb.adapter.ts), [SQSAdapter](./src/adapters/aws/sqs.adapter.ts) and [SNSAdapter](./src/adapters/aws/sns.adapter.ts).
84
- With these adapters you can connect your lambda to three different event sources and you can add more if you wish.
94
+ By now, you've already added the basic abstractions of this library, now, you can add the `adapters` that will add
95
+ support for receiving and processing different sources of `addAdapter` events. In the example I
96
+ added [AlbAdapter](./src/adapters/aws/alb.adapter.ts), [SQSAdapter](./src/adapters/aws/sqs.adapter.ts)
97
+ and [SNSAdapter](./src/adapters/aws/sns.adapter.ts). With these adapters you can connect your lambda to three different
98
+ event sources and you can add more if you wish.
85
99
 
86
100
  Finally, we call `build` which will assemble your handler that you can expose directly to your cloud.
87
101
 
@@ -102,10 +116,10 @@ By design we have these contracts that define the layers of the library: Framewo
102
116
 
103
117
  Currently, we support these frameworks:
104
118
 
105
- - [Express](https://expressjs.com/) by using ([ExpressFramework](./src/frameworks/express.framework.ts))
106
- - [Fastify](https://www.fastify.io/) by using ([FastifyFramework](./src/frameworks/fastify.framework.ts))
107
- - [Hapi](https://hapi.dev/) by using ([HapiFramework](./src/frameworks/hapi.framework.ts))
108
- - [Koa](https://koajs.com/) by using ([KoaFramework](./src/frameworks/koa.framework.ts))
119
+ - [Express](https://expressjs.com/) by using ([ExpressFramework](src/frameworks/express/express.framework.ts))
120
+ - [Fastify](https://www.fastify.io/) by using ([FastifyFramework](src/frameworks/fastify/fastify.framework.ts))
121
+ - [Hapi](https://hapi.dev/) by using ([HapiFramework](src/frameworks/hapi/hapi.framework.ts))
122
+ - [Koa](https://koajs.com/) by using ([KoaFramework](src/frameworks/koa/koa.framework.ts))
109
123
 
110
124
  We support these event sources:
111
125
 
@@ -127,53 +141,71 @@ We support these event sources:
127
141
  - [AWS SQS](https://docs.aws.amazon.com/pt_br/lambda/latest/dg/with-sqs.html) by
128
142
  using ([SQSAdapter](./src/adapters/aws/sqs.adapter.ts))
129
143
  - Azure
130
- - The support is cooming soon.
144
+ - The support is coming soon.
131
145
  - Huawei
132
- - The support is cooming soon.
146
+ - The support is coming soon.
133
147
 
134
148
  We support these resolvers:
135
149
 
136
- - Promise by using ([PromiseResolver](./src/resolvers/promise.resolver.ts))
137
- - Callback by using ([CallbackResolver](./src/resolvers/callback.resolver.ts))
138
- - AWS Context by using ([AwsContextResolver](./src/resolvers/aws-context.resolver.ts))
150
+ - Promise by using ([PromiseResolver](src/resolvers/promise/promise.resolver.ts))
151
+ - Callback by using ([CallbackResolver](src/resolvers/callback/callback.resolver.ts))
152
+ - AWS Context by using ([AwsContextResolver](src/resolvers/aws-context/aws-context.resolver.ts))
139
153
 
140
154
  We support these handlers:
141
155
 
142
- - Default by using ([DefaultHandler](./src/handlers/default.handler.ts))
156
+ - Default by using ([DefaultHandler](src/handlers/default/default.handler.ts))
143
157
 
144
158
  # Architecture
145
159
 
146
- The main purpose of this library is to allow the developer to add support for any cloud and as many event sources as he wants, without having to create an issue to request the feature or copy the library code because the library doesn't expose good APIs for you to extend its functionality
160
+ The main purpose of this library is to allow the developer to add support for any cloud and as many event sources as he
161
+ wants, without having to create an issue to request the feature or copy the library code because the library doesn't
162
+ expose good APIs for you to extend its functionality
147
163
 
148
- So I refactored [@vendia/serverless-express](https://github.com/vendia/serverless-express) with 4 layers of abstraction: Framework, Handler, Resolver and Adapter.
164
+ So I refactored [@vendia/serverless-express](https://github.com/vendia/serverless-express) with 4 layers of abstraction:
165
+ Framework, Handler, Resolver and Adapter.
149
166
 
150
- The [FrameworkContract](./src/contracts/framework.contract.ts) is responsible for forwarding to [IncomingMessage](https://nodejs.org/api/http.html#class-httpincomingmessage) and [ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse) for your application instance.
151
- With this abstraction you can implement any framework you want, they just need to accept both parameters and call [end](https://nodejs.org/api/http.html#class-httpserverresponse) in [ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse), so the library knows when to continue and return the response.
167
+ The [FrameworkContract](./src/contracts/framework.contract.ts) is responsible for forwarding
168
+ to [IncomingMessage](https://nodejs.org/api/http.html#class-httpincomingmessage)
169
+ and [ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse) for your application instance. With this
170
+ abstraction you can implement any framework you want, they just need to accept both parameters and
171
+ call [end](https://nodejs.org/api/http.html#class-httpserverresponse)
172
+ in [ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse), so the library knows when to continue
173
+ and return the response.
152
174
 
153
- The [HandlerContract](./src/contracts/handler.contract.ts) is responsible to get the input from the serverless and then manage to call each layer of abstraction to return a response.
154
- With this abstraction, you can implement different ways to receive input from your serverless environment.
155
- They usually have the same structure, but if you need to deal with a very different cloud, you can use this abstraction to add support for that cloud.
175
+ The [HandlerContract](./src/contracts/handler.contract.ts) is responsible to get the input from the serverless and then
176
+ manage to call each layer of abstraction to return a response. With this abstraction, you can implement different ways
177
+ to receive input from your serverless environment. They usually have the same structure, but if you need to deal with a
178
+ very different cloud, you can use this abstraction to add support for that cloud.
156
179
 
157
180
  > Handler is a good choice for implementing (~~monsters~~) ways to receive input.
158
181
  > For example, we can create an http server as its handler to test our serverless code without having to launch the framework. Because? I don't know, but you can.
159
182
 
160
- The [ResolverContract](./src/contracts/resolver.contract.ts) is responsible for waiting for the framework to handle the request and then returning the response to the cloud.
161
- Using AWS for example, you have three ways to wait for the response: returning a promise, calling the callback, and ~~using in-context methods~~, each option has its own benefits, but generally the promise option will be the better because any good cloud provider will support promises.
183
+ The [ResolverContract](./src/contracts/resolver.contract.ts) is responsible for waiting for the framework to handle the
184
+ request and then returning the response to the cloud. Using AWS for example, you have three ways to wait for the
185
+ response: returning a promise, calling the callback, and ~~using in-context methods~~, each option has its own benefits,
186
+ but generally the promise option will be the better because any good cloud provider will support promises.
162
187
 
163
- Finally, the masterpiece of this library, the [AdapterContract](./src/contracts/adapter.contract.ts) is responsible for handling the received event, transforming the request in a way that your application can understand and then transforming the response in a way your cloud can understand.
188
+ Finally, the masterpiece of this library, the [AdapterContract](./src/contracts/adapter.contract.ts) is responsible for
189
+ handling the received event, transforming the request in a way that your application can understand and then
190
+ transforming the response in a way your cloud can understand.
164
191
 
165
- Well, with these four contracts, you'll be able to add support to any cloud that exists (no more excuses not to use cloud X with NodeJS).
192
+ Well, with these four contracts, you'll be able to add support to any cloud that exists (no more excuses not to use
193
+ cloud X with NodeJS).
166
194
 
167
195
  ## Why you create this library?
168
196
 
169
- The real reason I created this library was because I wanted to add API Gateway and SQS support at the same time to save some money.
170
- But, [@vendia/serverless-express](https://github.com/vendia/serverless-express) was not supported, so I [created a PR](https://github.com/vendia/serverless-express/pull/483), but until I finished this library, that PR was never accepted.
197
+ The real reason I created this library was because I wanted to add API Gateway and SQS support at the same time to save
198
+ some money. But, [@vendia/serverless-express](https://github.com/vendia/serverless-express) was not supported, so
199
+ I [created a PR](https://github.com/vendia/serverless-express/pull/483), but until I finished this library, that PR was
200
+ never accepted.
171
201
 
172
- So I build my own library based on that library with better APIs so I never have to wait for the maintainer to accept my PR just to extend the library's functionality :)
202
+ So I build my own library based on that library with better APIs so I never have to wait for the maintainer to accept my
203
+ PR just to extend the library's functionality :)
173
204
 
174
205
  # Credits
175
206
 
176
- Honestly, I just refactored all the code that the @vendia team and many other contributors wrote, thanks so much to them for existing and giving us a brilliant library that is the core of my current company.
207
+ Honestly, I just refactored all the code that the @vendia team and many other contributors wrote, thanks so much to them
208
+ for existing and giving us a brilliant library that is the core of my current company.
177
209
 
178
210
  [build-img]:https://github.com/H4ad/serverless-adapter/actions/workflows/release.yml/badge.svg
179
211
 
@@ -1,7 +1,7 @@
1
- import { BinarySettings } from '../../@types';
2
- import { AdapterContract, AdapterRequest, FrameworkContract, HandlerContract, ResolverContract, ServerlessHandler } from '../../contracts';
3
- import { ILogger } from '../../core';
4
- import { ServerlessRequest, ServerlessResponse } from '../../network';
1
+ import { BinarySettings } from '../@types';
2
+ import { AdapterContract, AdapterRequest, FrameworkContract, HandlerContract, ResolverContract, ServerlessHandler } from '../contracts';
3
+ import { ServerlessRequest, ServerlessResponse } from '../network';
4
+ import { ILogger } from './index';
5
5
  /**
6
6
  * The abstract class that represents the base class for a handler
7
7
  */
@@ -2,7 +2,7 @@
2
2
  //#region Imports
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.BaseHandler = void 0;
5
- const network_1 = require("../../network");
5
+ const network_1 = require("../network");
6
6
  //#endregion
7
7
  /**
8
8
  * The abstract class that represents the base class for a handler
@@ -1,3 +1,4 @@
1
+ export * from './base-handler';
1
2
  export * from './constants';
2
3
  export * from './current-invoke';
3
4
  export * from './event-body';
package/lib/core/index.js CHANGED
@@ -10,6 +10,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./base-handler"), exports);
13
14
  __exportStar(require("./constants"), exports);
14
15
  __exportStar(require("./current-invoke"), exports);
15
16
  __exportStar(require("./event-body"), exports);
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage, ServerResponse } from 'http';
3
3
  import type { Express } from 'express';
4
- import { FrameworkContract } from '../contracts';
4
+ import { FrameworkContract } from '../../contracts';
5
5
  /**
6
6
  * The framework that forwards requests to express handler
7
7
  */
@@ -0,0 +1 @@
1
+ export * from './express.framework';
@@ -11,6 +11,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./express.framework"), exports);
14
- __exportStar(require("./fastify.framework"), exports);
15
- __exportStar(require("./hapi.framework"), exports);
16
- __exportStar(require("./koa.framework"), exports);
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage, ServerResponse } from 'http';
3
3
  import type { FastifyInstance } from 'fastify';
4
- import { FrameworkContract } from '../contracts';
4
+ import { FrameworkContract } from '../../contracts';
5
5
  /**
6
6
  * The framework that forwards requests to fastify handler
7
7
  */
@@ -0,0 +1 @@
1
+ export * from './fastify.framework';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./fastify.framework"), exports);
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage, ServerResponse } from 'http';
3
3
  import type { Server } from 'hapi';
4
- import { FrameworkContract } from '../contracts';
4
+ import { FrameworkContract } from '../../contracts';
5
5
  /**
6
6
  * The framework that forwards requests to hapi handler
7
7
  */
@@ -0,0 +1 @@
1
+ export * from './hapi.framework';
@@ -10,4 +10,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./base.handler"), exports);
13
+ __exportStar(require("./hapi.framework"), exports);
@@ -0,0 +1 @@
1
+ export * from './koa.framework';
@@ -10,4 +10,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./aws"), exports);
13
+ __exportStar(require("./koa.framework"), exports);
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { IncomingMessage, ServerResponse } from 'http';
3
3
  import type Application from 'koa';
4
- import { FrameworkContract } from '../contracts';
4
+ import { FrameworkContract } from '../../contracts';
5
5
  /**
6
6
  * The framework that forwards requests to koa handler
7
7
  */
@@ -1,8 +1,8 @@
1
- import { BinarySettings, SingleValueHeaders } from '../@types';
2
- import { AdapterContract, AdapterRequest, FrameworkContract, ResolverContract, ServerlessHandler } from '../contracts';
3
- import { ILogger } from '../core';
4
- import { ServerlessResponse } from '../network';
5
- import { BaseHandler } from './base';
1
+ import { BinarySettings, SingleValueHeaders } from '../../@types';
2
+ import { AdapterContract, AdapterRequest, FrameworkContract, ResolverContract, ServerlessHandler } from '../../contracts';
3
+ import { ILogger } from '../../core';
4
+ import { BaseHandler } from '../../core/base-handler';
5
+ import { ServerlessResponse } from '../../network';
6
6
  /**
7
7
  * The class that implements a default serverless handler consisting of a function with event, context and callback parameters respectively
8
8
  */
@@ -15,14 +15,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.DefaultHandler = void 0;
17
17
  const util_1 = __importDefault(require("util"));
18
- const core_1 = require("../core");
19
- const network_1 = require("../network");
20
- const base_1 = require("./base");
18
+ const core_1 = require("../../core");
19
+ const base_handler_1 = require("../../core/base-handler");
20
+ const network_1 = require("../../network");
21
21
  //#endregion
22
22
  /**
23
23
  * The class that implements a default serverless handler consisting of a function with event, context and callback parameters respectively
24
24
  */
25
- class DefaultHandler extends base_1.BaseHandler {
25
+ class DefaultHandler extends base_handler_1.BaseHandler {
26
26
  //#region Public Methods
27
27
  /**
28
28
  * @inheritDoc
@@ -1,2 +1 @@
1
- export * from './base';
2
1
  export * from './default.handler';
@@ -10,5 +10,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./base"), exports);
14
13
  __exportStar(require("./default.handler"), exports);
package/lib/index.d.ts CHANGED
@@ -1,9 +1,5 @@
1
1
  export * from './@types';
2
- export * from './adapters';
3
2
  export * from './contracts';
4
3
  export * from './core';
5
- export * from './frameworks';
6
- export * from './handlers';
7
4
  export * from './network';
8
- export * from './resolvers';
9
5
  export * from './serverless-adapter';
package/lib/index.js CHANGED
@@ -11,11 +11,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./@types"), exports);
14
- __exportStar(require("./adapters"), exports);
15
14
  __exportStar(require("./contracts"), exports);
16
15
  __exportStar(require("./core"), exports);
17
- __exportStar(require("./frameworks"), exports);
18
- __exportStar(require("./handlers"), exports);
19
16
  __exportStar(require("./network"), exports);
20
- __exportStar(require("./resolvers"), exports);
21
17
  __exportStar(require("./serverless-adapter"), exports);
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import http from 'http';
2
+ import { IncomingMessage } from 'http';
3
3
  import { SingleValueHeaders } from '../@types';
4
4
  export interface ServerlessRequestProps {
5
5
  method: string;
@@ -8,7 +8,7 @@ export interface ServerlessRequestProps {
8
8
  body?: Buffer;
9
9
  remoteAddress?: string;
10
10
  }
11
- export declare class ServerlessRequest extends http.IncomingMessage {
11
+ export declare class ServerlessRequest extends IncomingMessage {
12
12
  constructor({ method, url, headers, body, remoteAddress, }: ServerlessRequestProps);
13
13
  ip?: string;
14
14
  body?: Buffer;
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.ServerlessRequest = void 0;
7
4
  // ATTRIBUTION: https://github.com/dougmoscrop/serverless-http
8
- const http_1 = __importDefault(require("http"));
5
+ const http_1 = require("http");
9
6
  const core_1 = require("../core");
10
7
  const HTTPS_PORT = 443;
11
- class ServerlessRequest extends http_1.default.IncomingMessage {
8
+ class ServerlessRequest extends http_1.IncomingMessage {
12
9
  constructor({ method, url, headers, body, remoteAddress, }) {
13
10
  super({
14
11
  encrypted: true,
@@ -1,11 +1,11 @@
1
1
  /// <reference types="node" />
2
- import http, { IncomingMessage, OutgoingHttpHeaders } from 'http';
2
+ import { IncomingMessage, ServerResponse } from 'http';
3
3
  declare const BODY: unique symbol;
4
4
  declare const HEADERS: unique symbol;
5
5
  export interface ServerlessResponseProps {
6
6
  method?: string;
7
7
  }
8
- export declare class ServerlessResponse extends http.ServerResponse {
8
+ export declare class ServerlessResponse extends ServerResponse {
9
9
  constructor({ method }: ServerlessResponseProps);
10
10
  _header: string;
11
11
  _headers?: Record<any, any>;
@@ -15,8 +15,16 @@ export declare class ServerlessResponse extends http.ServerResponse {
15
15
  get headers(): Record<any, any>;
16
16
  static from(res: IncomingMessage): ServerlessResponse;
17
17
  static body(res: ServerlessResponse): Buffer;
18
- static headers(res: ServerlessResponse): Record<any, any>;
18
+ static headers(res: ServerlessResponse): import("http").OutgoingHttpHeaders & Record<any, any>;
19
19
  setHeader(key: string, value: number | string | readonly string[]): any;
20
- writeHead(statusCode: number, reason?: string | OutgoingHttpHeaders, obj?: OutgoingHttpHeaders): any;
20
+ writeHead(statusCode: number, statusMessage?: string | any | any[], obj?: any | any[]): any;
21
+ /**
22
+ * I use ignore here because in nodejs 12.x, statusMessage can be string | OutgoingHttpHeaders
23
+ * But in nodejs >=14.x, statusMessage can also be OutgoingHttpHeaders[]
24
+ * I take care of these cases above, but here I can't handle it well, so I give up
25
+ * nodejs 12.x ref: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v12/http.d.ts#L229
26
+ * nodejs 14.x ref: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v14/http.d.ts#L263
27
+ */
28
+ protected callNativeWriteHead(statusCode: number, statusMessage?: string | any | any[], obj?: any | any[]): this;
21
29
  }
22
30
  export {};
@@ -1,11 +1,8 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.ServerlessResponse = void 0;
7
4
  // ATTRIBUTION: https://github.com/dougmoscrop/serverless-http
8
- const http_1 = __importDefault(require("http"));
5
+ const http_1 = require("http");
9
6
  const core_1 = require("../core");
10
7
  const headerEnd = '\r\n\r\n';
11
8
  const BODY = Symbol('Response body');
@@ -24,7 +21,7 @@ function addData(stream, data) {
24
21
  else
25
22
  throw new Error(`response.write() of unexpected type: ${typeof data}`);
26
23
  }
27
- class ServerlessResponse extends http_1.default.ServerResponse {
24
+ class ServerlessResponse extends http_1.ServerResponse {
28
25
  constructor({ method }) {
29
26
  super({ method });
30
27
  this[BODY] = [];
@@ -78,7 +75,7 @@ class ServerlessResponse extends http_1.default.ServerResponse {
78
75
  return Buffer.concat(res[BODY]);
79
76
  }
80
77
  static headers(res) {
81
- const headers = typeof res.getHeaders === 'function' ? res.getHeaders() : res._headers;
78
+ const headers = res.getHeaders();
82
79
  return Object.assign(headers, res[HEADERS]);
83
80
  }
84
81
  setHeader(key, value) {
@@ -87,18 +84,32 @@ class ServerlessResponse extends http_1.default.ServerResponse {
87
84
  else
88
85
  super.setHeader(key, value);
89
86
  }
90
- writeHead(statusCode, reason, obj) {
91
- const headers = typeof reason === 'string' ? obj : reason;
92
- for (const name in headers) {
93
- this.setHeader(name, headers[name]);
94
- if (!this._wroteHeader) {
95
- // we only need to initiate super.headers once
96
- // writeHead will add the other headers itself
97
- break;
87
+ writeHead(statusCode, statusMessage, obj) {
88
+ const headersObjOrArray = typeof statusMessage === 'string' ? obj : statusMessage;
89
+ const arrayHeaders = Array.isArray(headersObjOrArray)
90
+ ? headersObjOrArray
91
+ : [headersObjOrArray || {}];
92
+ for (const headers of arrayHeaders) {
93
+ for (const name in headers) {
94
+ this.setHeader(name, headers[name]);
95
+ if (!this._wroteHeader) {
96
+ // we only need to initiate super.headers once
97
+ // writeHead will add the other headers itself
98
+ break;
99
+ }
98
100
  }
99
101
  }
100
- // @ts-ignore
101
- super.writeHead(statusCode, reason, obj);
102
+ return this.callNativeWriteHead(statusCode, statusMessage, obj);
103
+ }
104
+ /**
105
+ * I use ignore here because in nodejs 12.x, statusMessage can be string | OutgoingHttpHeaders
106
+ * But in nodejs >=14.x, statusMessage can also be OutgoingHttpHeaders[]
107
+ * I take care of these cases above, but here I can't handle it well, so I give up
108
+ * nodejs 12.x ref: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v12/http.d.ts#L229
109
+ * nodejs 14.x ref: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v14/http.d.ts#L263
110
+ */
111
+ callNativeWriteHead(statusCode, statusMessage, obj) {
112
+ return super.writeHead(statusCode, statusMessage, obj);
102
113
  }
103
114
  }
104
115
  exports.ServerlessResponse = ServerlessResponse;
@@ -1,5 +1,5 @@
1
1
  import type { Context } from 'aws-lambda';
2
- import { Resolver, ResolverContract, ResolverProps } from '../contracts';
2
+ import { Resolver, ResolverContract, ResolverProps } from '../../contracts';
3
3
  /**
4
4
  * The class that implements the resolver by using the AWS Context object.
5
5
  *
@@ -0,0 +1 @@
1
+ export * from './aws-context.resolver';
@@ -11,5 +11,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./aws-context.resolver"), exports);
14
- __exportStar(require("./callback.resolver"), exports);
15
- __exportStar(require("./promise.resolver"), exports);
@@ -1,4 +1,4 @@
1
- import { Resolver, ResolverContract, ResolverProps } from '../contracts';
1
+ import { Resolver, ResolverContract, ResolverProps } from '../../contracts';
2
2
  /**
3
3
  * The default signature of the callback sent by serverless
4
4
  */
@@ -0,0 +1 @@
1
+ export * from './callback.resolver';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./callback.resolver"), exports);
@@ -0,0 +1 @@
1
+ export * from './promise.resolver';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./promise.resolver"), exports);
@@ -1,4 +1,4 @@
1
- import { Resolver, ResolverContract, ResolverProps } from '../contracts';
1
+ import { Resolver, ResolverContract, ResolverProps } from '../../contracts';
2
2
  /**
3
3
  * The class that implements the resolver using the promise object sent by this library
4
4
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@h4ad/serverless-adapter",
3
- "version": "1.0.2",
3
+ "version": "2.0.0",
4
4
  "description": "This library enables you to utilize any Cloud or Event Source to respond to web and API requests using your existing Node.js application framework.",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -1 +0,0 @@
1
- export * from './aws';
@@ -1,4 +0,0 @@
1
- export * from './express.framework';
2
- export * from './fastify.framework';
3
- export * from './hapi.framework';
4
- export * from './koa.framework';
@@ -1 +0,0 @@
1
- export * from './base.handler';
@@ -1,3 +0,0 @@
1
- export * from './aws-context.resolver';
2
- export * from './callback.resolver';
3
- export * from './promise.resolver';