@hazeljs/serverless 0.2.0-alpha.1
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.
- package/LICENSE +192 -0
- package/README.md +493 -0
- package/dist/adapters.test.d.ts +2 -0
- package/dist/adapters.test.d.ts.map +1 -0
- package/dist/adapters.test.js +432 -0
- package/dist/cloud-function.adapter.d.ts +109 -0
- package/dist/cloud-function.adapter.d.ts.map +1 -0
- package/dist/cloud-function.adapter.js +271 -0
- package/dist/cold-start.optimizer.d.ts +70 -0
- package/dist/cold-start.optimizer.d.ts.map +1 -0
- package/dist/cold-start.optimizer.js +202 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/integration.test.d.ts +6 -0
- package/dist/integration.test.d.ts.map +1 -0
- package/dist/integration.test.js +191 -0
- package/dist/lambda.adapter.d.ts +102 -0
- package/dist/lambda.adapter.d.ts.map +1 -0
- package/dist/lambda.adapter.js +258 -0
- package/dist/serverless.decorator.d.ts +166 -0
- package/dist/serverless.decorator.d.ts.map +1 -0
- package/dist/serverless.decorator.js +56 -0
- package/dist/serverless.test.d.ts +2 -0
- package/dist/serverless.test.d.ts.map +1 -0
- package/dist/serverless.test.js +246 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Copyright 2024 HazelJS Team
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
22
|
+
|
|
23
|
+
1. Definitions.
|
|
24
|
+
|
|
25
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
26
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
27
|
+
|
|
28
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
29
|
+
the copyright owner that is granting the License.
|
|
30
|
+
|
|
31
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
32
|
+
other entities that control, are controlled by, or are under common
|
|
33
|
+
control with that entity. For the purposes of this definition,
|
|
34
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
35
|
+
direction or management of such entity, whether by contract or
|
|
36
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
37
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
38
|
+
|
|
39
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
40
|
+
exercising permissions granted by this License.
|
|
41
|
+
|
|
42
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
43
|
+
including but not limited to software source code, documentation
|
|
44
|
+
source, and configuration files.
|
|
45
|
+
|
|
46
|
+
"Object" form shall mean any form resulting from mechanical
|
|
47
|
+
transformation or translation of a Source form, including but
|
|
48
|
+
not limited to compiled object code, generated documentation,
|
|
49
|
+
and conversions to other media types.
|
|
50
|
+
|
|
51
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
52
|
+
Object form, made available under the License, as indicated by a
|
|
53
|
+
copyright notice that is included in or attached to the work
|
|
54
|
+
(an example is provided in the Appendix below).
|
|
55
|
+
|
|
56
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
57
|
+
form, that is based on (or derived from) the Work and for which the
|
|
58
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
59
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
60
|
+
of this License, Derivative Works shall not include works that remain
|
|
61
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
62
|
+
the Work and Derivative Works thereof.
|
|
63
|
+
|
|
64
|
+
"Contribution" shall mean any work of authorship, including
|
|
65
|
+
the original version of the Work and any modifications or additions
|
|
66
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
67
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
68
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
69
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
70
|
+
means any form of electronic, verbal, or written communication sent
|
|
71
|
+
to the Licensor or its representatives, including but not limited to
|
|
72
|
+
communication on electronic mailing lists, source code control systems,
|
|
73
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
74
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
75
|
+
excluding communication that is conspicuously marked or otherwise
|
|
76
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
77
|
+
|
|
78
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
79
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
80
|
+
subsequently incorporated within the Work.
|
|
81
|
+
|
|
82
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
86
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
87
|
+
Work and such Derivative Works in Source or Object form.
|
|
88
|
+
|
|
89
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
90
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
91
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
92
|
+
(except as stated in this section) patent license to make, have made,
|
|
93
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
94
|
+
where such license applies only to those patent claims licensable
|
|
95
|
+
by such Contributor that are necessarily infringed by their
|
|
96
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
97
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
98
|
+
institute patent litigation against any entity (including a
|
|
99
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
100
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
101
|
+
or contributory patent infringement, then any patent licenses
|
|
102
|
+
granted to You under this License for that Work shall terminate
|
|
103
|
+
as of the date such litigation is filed.
|
|
104
|
+
|
|
105
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
106
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
107
|
+
modifications, and in Source or Object form, provided that You
|
|
108
|
+
meet the following conditions:
|
|
109
|
+
|
|
110
|
+
(a) You must give any other recipients of the Work or
|
|
111
|
+
Derivative Works a copy of this License; and
|
|
112
|
+
|
|
113
|
+
(b) You must cause any modified files to carry prominent notices
|
|
114
|
+
stating that You changed the files; and
|
|
115
|
+
|
|
116
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
117
|
+
that You distribute, all copyright, patent, trademark, and
|
|
118
|
+
attribution notices from the Source form of the Work,
|
|
119
|
+
excluding those notices that do not pertain to any part of
|
|
120
|
+
the Derivative Works; and
|
|
121
|
+
|
|
122
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
123
|
+
distribution, then any Derivative Works that You distribute must
|
|
124
|
+
include a readable copy of the attribution notices contained
|
|
125
|
+
within such NOTICE file, excluding those notices that do not
|
|
126
|
+
pertain to any part of the Derivative Works, in at least one
|
|
127
|
+
of the following places: within a NOTICE text file distributed
|
|
128
|
+
as part of the Derivative Works; within the Source form or
|
|
129
|
+
documentation, if provided along with the Derivative Works; or,
|
|
130
|
+
within a display generated by the Derivative Works, if and
|
|
131
|
+
wherever such third-party notices normally appear. The contents
|
|
132
|
+
of the NOTICE file are for informational purposes only and
|
|
133
|
+
do not modify the License. You may add Your own attribution
|
|
134
|
+
notices within Derivative Works that You distribute, alongside
|
|
135
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
136
|
+
that such additional attribution notices cannot be construed
|
|
137
|
+
as modifying the License.
|
|
138
|
+
|
|
139
|
+
You may add Your own copyright statement to Your modifications and
|
|
140
|
+
may provide additional or different license terms and conditions
|
|
141
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
142
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
143
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
144
|
+
the conditions stated in this License.
|
|
145
|
+
|
|
146
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
147
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
148
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
149
|
+
this License, without any additional terms or conditions.
|
|
150
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
151
|
+
the terms of any separate license agreement you may have executed
|
|
152
|
+
with Licensor regarding such Contributions.
|
|
153
|
+
|
|
154
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
155
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
156
|
+
except as required for reasonable and customary use in describing the
|
|
157
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
158
|
+
|
|
159
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
160
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
161
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
162
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
163
|
+
implied, including, without limitation, any warranties or conditions
|
|
164
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
165
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
166
|
+
appropriateness of using or redistributing the Work and assume any
|
|
167
|
+
risks associated with Your exercise of permissions under this License.
|
|
168
|
+
|
|
169
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
170
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
171
|
+
unless required by applicable law (such as deliberate and grossly
|
|
172
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
173
|
+
liable to You for damages, including any direct, indirect, special,
|
|
174
|
+
incidental, or consequential damages of any character arising as a
|
|
175
|
+
result of this License or out of the use or inability to use the
|
|
176
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
177
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
178
|
+
other commercial damages or losses), even if such Contributor
|
|
179
|
+
has been advised of the possibility of such damages.
|
|
180
|
+
|
|
181
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
182
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
183
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
184
|
+
or other liability obligations and/or rights consistent with this
|
|
185
|
+
License. However, in accepting such obligations, You may act only
|
|
186
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
187
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
188
|
+
defend, and hold each Contributor harmless for any liability
|
|
189
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
190
|
+
of your accepting any such warranty or additional liability.
|
|
191
|
+
|
|
192
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,493 @@
|
|
|
1
|
+
# @hazeljs/serverless
|
|
2
|
+
|
|
3
|
+
**Deploy to Lambda or Cloud Functions. Zero code changes.**
|
|
4
|
+
|
|
5
|
+
Same HazelJS app, same controllers, same routing. Wrap it and ship. Cold start optimization, request mapping, env handling — built in.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@hazeljs/serverless)
|
|
8
|
+
[](https://www.npmjs.com/package/@hazeljs/serverless)
|
|
9
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- ☁️ **AWS Lambda** - Deploy to AWS Lambda
|
|
14
|
+
- 🌐 **Google Cloud Functions** - Deploy to GCP
|
|
15
|
+
- 🔄 **Zero Config** - No code changes needed
|
|
16
|
+
- 🎯 **Cold Start Optimization** - Minimize cold start times
|
|
17
|
+
- 📊 **Request/Response Mapping** - Automatic event transformation
|
|
18
|
+
- 🔐 **Environment Variables** - Seamless config management
|
|
19
|
+
- 🎨 **Decorator Support** - `@Serverless` decorator
|
|
20
|
+
- 📦 **Bundle Optimization** - Tree-shaking and minification
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @hazeljs/serverless
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## AWS Lambda
|
|
29
|
+
|
|
30
|
+
### Quick Start
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
// lambda.ts
|
|
34
|
+
import { createLambdaHandler } from '@hazeljs/serverless';
|
|
35
|
+
import { AppModule } from './app.module';
|
|
36
|
+
|
|
37
|
+
export const handler = createLambdaHandler(AppModule);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### With Options
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { createLambdaHandler } from '@hazeljs/serverless';
|
|
44
|
+
import { AppModule } from './app.module';
|
|
45
|
+
|
|
46
|
+
export const handler = createLambdaHandler(AppModule, {
|
|
47
|
+
// Enable binary response
|
|
48
|
+
binaryMimeTypes: ['image/*', 'application/pdf'],
|
|
49
|
+
|
|
50
|
+
// Custom initialization
|
|
51
|
+
onInit: async (app) => {
|
|
52
|
+
console.log('Lambda initialized');
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
// Custom error handling
|
|
56
|
+
onError: (error) => {
|
|
57
|
+
console.error('Lambda error:', error);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Deployment
|
|
63
|
+
|
|
64
|
+
#### Using AWS SAM
|
|
65
|
+
|
|
66
|
+
```yaml
|
|
67
|
+
# template.yaml
|
|
68
|
+
AWSTemplateFormatVersion: '2010-09-09'
|
|
69
|
+
Transform: AWS::Serverless-2016-10-31
|
|
70
|
+
|
|
71
|
+
Resources:
|
|
72
|
+
HazelFunction:
|
|
73
|
+
Type: AWS::Serverless::Function
|
|
74
|
+
Properties:
|
|
75
|
+
CodeUri: dist/
|
|
76
|
+
Handler: lambda.handler
|
|
77
|
+
Runtime: nodejs20.x
|
|
78
|
+
MemorySize: 512
|
|
79
|
+
Timeout: 30
|
|
80
|
+
Environment:
|
|
81
|
+
Variables:
|
|
82
|
+
NODE_ENV: production
|
|
83
|
+
DATABASE_URL: !Ref DatabaseUrl
|
|
84
|
+
Events:
|
|
85
|
+
ApiEvent:
|
|
86
|
+
Type: Api
|
|
87
|
+
Properties:
|
|
88
|
+
Path: /{proxy+}
|
|
89
|
+
Method: ANY
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Deploy:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
npm run build
|
|
96
|
+
sam build
|
|
97
|
+
sam deploy --guided
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### Using Serverless Framework
|
|
101
|
+
|
|
102
|
+
```yaml
|
|
103
|
+
# serverless.yml
|
|
104
|
+
service: hazeljs-app
|
|
105
|
+
|
|
106
|
+
provider:
|
|
107
|
+
name: aws
|
|
108
|
+
runtime: nodejs20.x
|
|
109
|
+
stage: ${opt:stage, 'dev'}
|
|
110
|
+
region: us-east-1
|
|
111
|
+
environment:
|
|
112
|
+
NODE_ENV: ${self:provider.stage}
|
|
113
|
+
DATABASE_URL: ${env:DATABASE_URL}
|
|
114
|
+
|
|
115
|
+
functions:
|
|
116
|
+
api:
|
|
117
|
+
handler: dist/lambda.handler
|
|
118
|
+
events:
|
|
119
|
+
- http:
|
|
120
|
+
path: /{proxy+}
|
|
121
|
+
method: ANY
|
|
122
|
+
cors: true
|
|
123
|
+
|
|
124
|
+
package:
|
|
125
|
+
individually: true
|
|
126
|
+
patterns:
|
|
127
|
+
- '!node_modules/**'
|
|
128
|
+
- 'node_modules/@hazeljs/**'
|
|
129
|
+
- 'dist/**'
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Deploy:
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm run build
|
|
136
|
+
serverless deploy
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Google Cloud Functions
|
|
140
|
+
|
|
141
|
+
### Quick Start
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
// index.ts
|
|
145
|
+
import { createCloudFunctionHandler } from '@hazeljs/serverless';
|
|
146
|
+
import { AppModule } from './app.module';
|
|
147
|
+
|
|
148
|
+
export const hazelApp = createCloudFunctionHandler(AppModule);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### With Options
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { createCloudFunctionHandler } from '@hazeljs/serverless';
|
|
155
|
+
import { AppModule } from './app.module';
|
|
156
|
+
|
|
157
|
+
export const hazelApp = createCloudFunctionHandler(AppModule, {
|
|
158
|
+
// Custom initialization
|
|
159
|
+
onInit: async (app) => {
|
|
160
|
+
console.log('Cloud Function initialized');
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
// Custom error handling
|
|
164
|
+
onError: (error) => {
|
|
165
|
+
console.error('Cloud Function error:', error);
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**Event handler (Pub/Sub, Storage):** `createCloudFunctionEventHandler(Module)` returns a stub that initializes the app and logs the event but does not route to user code. Implement your own event handling and call it from that handler, or use a separate entrypoint for event-triggered functions.
|
|
171
|
+
|
|
172
|
+
### Deployment
|
|
173
|
+
|
|
174
|
+
#### Using gcloud CLI
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
# Build
|
|
178
|
+
npm run build
|
|
179
|
+
|
|
180
|
+
# Deploy
|
|
181
|
+
gcloud functions deploy hazeljs-app \
|
|
182
|
+
--runtime nodejs20 \
|
|
183
|
+
--trigger-http \
|
|
184
|
+
--allow-unauthenticated \
|
|
185
|
+
--entry-point hazelApp \
|
|
186
|
+
--source dist \
|
|
187
|
+
--set-env-vars NODE_ENV=production,DATABASE_URL=your-db-url
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
#### Using Cloud Functions YAML
|
|
191
|
+
|
|
192
|
+
```yaml
|
|
193
|
+
# function.yaml
|
|
194
|
+
runtime: nodejs20
|
|
195
|
+
entryPoint: hazelApp
|
|
196
|
+
environmentVariables:
|
|
197
|
+
NODE_ENV: production
|
|
198
|
+
DATABASE_URL: ${DATABASE_URL}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Deploy:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
gcloud functions deploy hazeljs-app --config function.yaml
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Decorator-Based Optimization
|
|
208
|
+
|
|
209
|
+
Mark **controllers** (classes) for serverless optimization with `@Serverless`:
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { Controller, Get } from '@hazeljs/core';
|
|
213
|
+
import { Serverless } from '@hazeljs/serverless';
|
|
214
|
+
|
|
215
|
+
@Controller('/api')
|
|
216
|
+
@Serverless({ memory: 512, timeout: 30, coldStartOptimization: true })
|
|
217
|
+
export class ApiController {
|
|
218
|
+
@Get('/hello')
|
|
219
|
+
hello() {
|
|
220
|
+
return { message: 'Hello from serverless!' };
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
> **Note:** The decorator is class-level only. Method-level `@Serverless({ optimize, cache })` and per-route `binaryResponse` are planned for a future release.
|
|
226
|
+
|
|
227
|
+
## Cold Start Optimization
|
|
228
|
+
|
|
229
|
+
**ColdStartOptimizer** preloads the DI container and built-in modules (http, https, crypto, buffer); behavior is best-effort and may vary by Node version. **KeepAliveHelper** runs an interval but does not perform an actual HTTP request—use provisioned concurrency, a cron job, or implement `fetch`/`http.get` in the interval to warm the function.
|
|
230
|
+
|
|
231
|
+
### Minimize Bundle Size
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
// Use dynamic imports for heavy dependencies
|
|
235
|
+
@Get('/heavy')
|
|
236
|
+
async heavyOperation() {
|
|
237
|
+
const { processData } = await import('./heavy-processor');
|
|
238
|
+
return processData();
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Connection Pooling
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
// Reuse database connections across invocations
|
|
246
|
+
let cachedDb: any = null;
|
|
247
|
+
|
|
248
|
+
async function connectToDatabase() {
|
|
249
|
+
if (cachedDb) {
|
|
250
|
+
return cachedDb;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
cachedDb = await createDatabaseConnection();
|
|
254
|
+
return cachedDb;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
@Injectable()
|
|
258
|
+
export class DatabaseService {
|
|
259
|
+
async query(sql: string) {
|
|
260
|
+
const db = await connectToDatabase();
|
|
261
|
+
return db.query(sql);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Provisioned Concurrency (AWS Lambda)
|
|
267
|
+
|
|
268
|
+
```yaml
|
|
269
|
+
# template.yaml
|
|
270
|
+
Resources:
|
|
271
|
+
HazelFunction:
|
|
272
|
+
Type: AWS::Serverless::Function
|
|
273
|
+
Properties:
|
|
274
|
+
# ... other properties
|
|
275
|
+
ProvisionedConcurrencyConfig:
|
|
276
|
+
ProvisionedConcurrentExecutions: 5
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## Environment-Specific Configuration
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
// config/serverless.config.ts
|
|
283
|
+
export const serverlessConfig = {
|
|
284
|
+
development: {
|
|
285
|
+
timeout: 30,
|
|
286
|
+
memorySize: 512,
|
|
287
|
+
},
|
|
288
|
+
production: {
|
|
289
|
+
timeout: 60,
|
|
290
|
+
memorySize: 1024,
|
|
291
|
+
provisionedConcurrency: 5,
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
// lambda.ts
|
|
296
|
+
const config = serverlessConfig[process.env.NODE_ENV || 'development'];
|
|
297
|
+
|
|
298
|
+
export const handler = createLambdaHandler(AppModule, {
|
|
299
|
+
timeout: config.timeout,
|
|
300
|
+
});
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Request/Response Handling
|
|
304
|
+
|
|
305
|
+
### AWS Lambda Event Types
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
|
309
|
+
|
|
310
|
+
// Access raw Lambda event
|
|
311
|
+
@Get('/lambda-info')
|
|
312
|
+
getLambdaInfo(@Req() req: any) {
|
|
313
|
+
const event: APIGatewayProxyEvent = req.apiGateway.event;
|
|
314
|
+
|
|
315
|
+
return {
|
|
316
|
+
requestId: event.requestContext.requestId,
|
|
317
|
+
sourceIp: event.requestContext.identity.sourceIp,
|
|
318
|
+
userAgent: event.headers['user-agent'],
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
### Binary Responses
|
|
324
|
+
|
|
325
|
+
You can pass `binaryMimeTypes` in `createLambdaHandler` options for future use. Automatic base64 encoding of binary response bodies for Lambda is planned.
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
export const handler = createLambdaHandler(AppModule, {
|
|
329
|
+
binaryMimeTypes: ['image/png', 'application/pdf'],
|
|
330
|
+
});
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
## Logging
|
|
334
|
+
|
|
335
|
+
### CloudWatch Logs (AWS)
|
|
336
|
+
|
|
337
|
+
```typescript
|
|
338
|
+
import { Logger } from '@hazeljs/core';
|
|
339
|
+
|
|
340
|
+
@Injectable()
|
|
341
|
+
export class MyService {
|
|
342
|
+
private logger = new Logger(MyService.name);
|
|
343
|
+
|
|
344
|
+
async doSomething() {
|
|
345
|
+
this.logger.log('Processing request');
|
|
346
|
+
this.logger.error('An error occurred');
|
|
347
|
+
this.logger.warn('Warning message');
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### Cloud Logging (GCP)
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
import { Logger } from '@hazeljs/core';
|
|
356
|
+
|
|
357
|
+
@Injectable()
|
|
358
|
+
export class MyService {
|
|
359
|
+
private logger = new Logger(MyService.name);
|
|
360
|
+
|
|
361
|
+
async doSomething() {
|
|
362
|
+
// Logs automatically sent to Cloud Logging
|
|
363
|
+
this.logger.log('Processing request', {
|
|
364
|
+
userId: '123',
|
|
365
|
+
action: 'create',
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## Best Practices
|
|
372
|
+
|
|
373
|
+
1. **Minimize Dependencies** - Only include necessary packages
|
|
374
|
+
2. **Reuse Connections** - Cache database and API connections
|
|
375
|
+
3. **Use Environment Variables** - Store configuration externally
|
|
376
|
+
4. **Optimize Bundle Size** - Use tree-shaking and minification
|
|
377
|
+
5. **Handle Cold Starts** - Implement warming strategies
|
|
378
|
+
6. **Monitor Performance** - Track execution time and memory usage
|
|
379
|
+
7. **Set Appropriate Timeouts** - Balance cost and functionality
|
|
380
|
+
8. **Use Provisioned Concurrency** - For latency-sensitive endpoints
|
|
381
|
+
|
|
382
|
+
## Monitoring
|
|
383
|
+
|
|
384
|
+
### AWS Lambda
|
|
385
|
+
|
|
386
|
+
```typescript
|
|
387
|
+
// Add X-Ray tracing
|
|
388
|
+
import AWSXRay from 'aws-xray-sdk-core';
|
|
389
|
+
|
|
390
|
+
const AWS = AWSXRay.captureAWS(require('aws-sdk'));
|
|
391
|
+
|
|
392
|
+
// Custom metrics
|
|
393
|
+
import { CloudWatch } from 'aws-sdk';
|
|
394
|
+
|
|
395
|
+
const cloudwatch = new CloudWatch();
|
|
396
|
+
|
|
397
|
+
async function recordMetric(name: string, value: number) {
|
|
398
|
+
await cloudwatch.putMetricData({
|
|
399
|
+
Namespace: 'HazelJS',
|
|
400
|
+
MetricData: [{
|
|
401
|
+
MetricName: name,
|
|
402
|
+
Value: value,
|
|
403
|
+
Unit: 'Count',
|
|
404
|
+
}],
|
|
405
|
+
}).promise();
|
|
406
|
+
}
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### Google Cloud Functions
|
|
410
|
+
|
|
411
|
+
```typescript
|
|
412
|
+
// Add Cloud Trace
|
|
413
|
+
import { TraceAgent } from '@google-cloud/trace-agent';
|
|
414
|
+
|
|
415
|
+
TraceAgent.start();
|
|
416
|
+
|
|
417
|
+
// Custom metrics
|
|
418
|
+
import { Monitoring } from '@google-cloud/monitoring';
|
|
419
|
+
|
|
420
|
+
const monitoring = new Monitoring.MetricServiceClient();
|
|
421
|
+
|
|
422
|
+
async function recordMetric(name: string, value: number) {
|
|
423
|
+
const request = {
|
|
424
|
+
name: monitoring.projectPath(projectId),
|
|
425
|
+
timeSeries: [{
|
|
426
|
+
metric: { type: `custom.googleapis.com/${name}` },
|
|
427
|
+
points: [{
|
|
428
|
+
interval: { endTime: { seconds: Date.now() / 1000 } },
|
|
429
|
+
value: { doubleValue: value },
|
|
430
|
+
}],
|
|
431
|
+
}],
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
await monitoring.createTimeSeries(request);
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
## Cost Optimization
|
|
439
|
+
|
|
440
|
+
### Request Batching
|
|
441
|
+
|
|
442
|
+
```typescript
|
|
443
|
+
// Batch multiple operations
|
|
444
|
+
@Post('/batch')
|
|
445
|
+
async batchProcess(@Body() items: any[]) {
|
|
446
|
+
const results = await Promise.all(
|
|
447
|
+
items.map(item => this.processItem(item))
|
|
448
|
+
);
|
|
449
|
+
return results;
|
|
450
|
+
}
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
### Caching
|
|
454
|
+
|
|
455
|
+
```typescript
|
|
456
|
+
import { Cache } from '@hazeljs/cache';
|
|
457
|
+
|
|
458
|
+
@Injectable()
|
|
459
|
+
export class DataService {
|
|
460
|
+
@Cache({ key: 'expensive-data', ttl: 3600 })
|
|
461
|
+
async getExpensiveData() {
|
|
462
|
+
// This will be cached for 1 hour
|
|
463
|
+
return await this.fetchFromDatabase();
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
## Examples
|
|
469
|
+
|
|
470
|
+
See the [examples](../../example/src/serverless) directory for complete working examples.
|
|
471
|
+
|
|
472
|
+
## Testing
|
|
473
|
+
|
|
474
|
+
```bash
|
|
475
|
+
npm test
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
## Contributing
|
|
479
|
+
|
|
480
|
+
Contributions are welcome! Please read our [Contributing Guide](../../CONTRIBUTING.md) for details.
|
|
481
|
+
|
|
482
|
+
## License
|
|
483
|
+
|
|
484
|
+
Apache 2.0 © [HazelJS](https://hazeljs.com)
|
|
485
|
+
|
|
486
|
+
## Links
|
|
487
|
+
|
|
488
|
+
- [Documentation](https://hazeljs.com/docs/packages/serverless)
|
|
489
|
+
- [AWS Lambda Docs](https://docs.aws.amazon.com/lambda/)
|
|
490
|
+
- [Google Cloud Functions Docs](https://cloud.google.com/functions/docs)
|
|
491
|
+
- [GitHub](https://github.com/hazel-js/hazeljs)
|
|
492
|
+
- [Issues](https://github.com/hazel-js/hazeljs/issues)
|
|
493
|
+
- [Discord](https://discord.com/channels/1448263814238965833/1448263814859456575)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapters.test.d.ts","sourceRoot":"","sources":["../src/adapters.test.ts"],"names":[],"mappings":""}
|