@hazeljs/serverless 0.2.0-beta.54 → 0.2.0-beta.55
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/README.md +1 -1
- 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 +2 -2
package/README.md
CHANGED
|
@@ -498,4 +498,4 @@ Apache 2.0 © [HazelJS](https://hazeljs.com)
|
|
|
498
498
|
- [Google Cloud Functions Docs](https://cloud.google.com/functions/docs)
|
|
499
499
|
- [GitHub](https://github.com/hazel-js/hazeljs)
|
|
500
500
|
- [Issues](https://github.com/hazel-js/hazeljs/issues)
|
|
501
|
-
- [Discord](https://discord.
|
|
501
|
+
- [Discord](https://discord.com/channels/1448263814238965833/1448263814859456575)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverless.test.d.ts","sourceRoot":"","sources":["../src/serverless.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const serverless_decorator_1 = require("./serverless.decorator");
|
|
13
|
+
const cold_start_optimizer_1 = require("./cold-start.optimizer");
|
|
14
|
+
const lambda_adapter_1 = require("./lambda.adapter");
|
|
15
|
+
const cloud_function_adapter_1 = require("./cloud-function.adapter");
|
|
16
|
+
const core_1 = require("@hazeljs/core");
|
|
17
|
+
describe('Serverless Decorator', () => {
|
|
18
|
+
it('should mark class as serverless', () => {
|
|
19
|
+
let TestController = class TestController {
|
|
20
|
+
};
|
|
21
|
+
TestController = __decorate([
|
|
22
|
+
(0, serverless_decorator_1.Serverless)()
|
|
23
|
+
], TestController);
|
|
24
|
+
expect((0, serverless_decorator_1.isServerless)(TestController)).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
it('should store serverless metadata', () => {
|
|
27
|
+
const options = {
|
|
28
|
+
memory: 1024,
|
|
29
|
+
timeout: 60,
|
|
30
|
+
coldStartOptimization: true,
|
|
31
|
+
};
|
|
32
|
+
let TestController = class TestController {
|
|
33
|
+
};
|
|
34
|
+
TestController = __decorate([
|
|
35
|
+
(0, serverless_decorator_1.Serverless)(options)
|
|
36
|
+
], TestController);
|
|
37
|
+
const metadata = (0, serverless_decorator_1.getServerlessMetadata)(TestController);
|
|
38
|
+
expect(metadata).toBeDefined();
|
|
39
|
+
expect(metadata?.memory).toBe(1024);
|
|
40
|
+
expect(metadata?.timeout).toBe(60);
|
|
41
|
+
expect(metadata?.coldStartOptimization).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
it('should use default options', () => {
|
|
44
|
+
let TestController = class TestController {
|
|
45
|
+
};
|
|
46
|
+
TestController = __decorate([
|
|
47
|
+
(0, serverless_decorator_1.Serverless)()
|
|
48
|
+
], TestController);
|
|
49
|
+
const metadata = (0, serverless_decorator_1.getServerlessMetadata)(TestController);
|
|
50
|
+
expect(metadata?.memory).toBe(512);
|
|
51
|
+
expect(metadata?.timeout).toBe(30);
|
|
52
|
+
expect(metadata?.coldStartOptimization).toBe(true);
|
|
53
|
+
expect(metadata?.autoSplit).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
it('should return undefined for non-serverless class', () => {
|
|
56
|
+
class TestController {
|
|
57
|
+
}
|
|
58
|
+
expect((0, serverless_decorator_1.isServerless)(TestController)).toBe(false);
|
|
59
|
+
expect((0, serverless_decorator_1.getServerlessMetadata)(TestController)).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe('ColdStartOptimizer', () => {
|
|
63
|
+
let optimizer;
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
optimizer = cold_start_optimizer_1.ColdStartOptimizer.getInstance();
|
|
66
|
+
optimizer.reset();
|
|
67
|
+
});
|
|
68
|
+
describe('warmUp', () => {
|
|
69
|
+
it('should warm up the application', async () => {
|
|
70
|
+
expect(optimizer.isWarm()).toBe(false);
|
|
71
|
+
await optimizer.warmUp();
|
|
72
|
+
expect(optimizer.isWarm()).toBe(true);
|
|
73
|
+
expect(optimizer.getWarmupTimestamp()).toBeDefined();
|
|
74
|
+
});
|
|
75
|
+
it('should not warm up twice', async () => {
|
|
76
|
+
await optimizer.warmUp();
|
|
77
|
+
const firstTimestamp = optimizer.getWarmupTimestamp();
|
|
78
|
+
await optimizer.warmUp();
|
|
79
|
+
const secondTimestamp = optimizer.getWarmupTimestamp();
|
|
80
|
+
expect(firstTimestamp).toBe(secondTimestamp);
|
|
81
|
+
});
|
|
82
|
+
it('should preload critical modules', async () => {
|
|
83
|
+
await optimizer.warmUp();
|
|
84
|
+
const preloaded = optimizer.getPreloadedModules();
|
|
85
|
+
expect(preloaded.length).toBeGreaterThan(0);
|
|
86
|
+
expect(preloaded).toContain('http');
|
|
87
|
+
expect(preloaded).toContain('crypto');
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
describe('isWarm', () => {
|
|
91
|
+
it('should return false initially', () => {
|
|
92
|
+
expect(optimizer.isWarm()).toBe(false);
|
|
93
|
+
});
|
|
94
|
+
it('should return true after warmup', async () => {
|
|
95
|
+
await optimizer.warmUp();
|
|
96
|
+
expect(optimizer.isWarm()).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe('getWarmupDuration', () => {
|
|
100
|
+
it('should return undefined before warmup', () => {
|
|
101
|
+
expect(optimizer.getWarmupDuration()).toBeUndefined();
|
|
102
|
+
});
|
|
103
|
+
it('should return duration after warmup', async () => {
|
|
104
|
+
await optimizer.warmUp();
|
|
105
|
+
const duration = optimizer.getWarmupDuration();
|
|
106
|
+
expect(duration).toBeDefined();
|
|
107
|
+
expect(duration).toBeGreaterThanOrEqual(0);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
describe('reset', () => {
|
|
111
|
+
it('should reset warmup state', async () => {
|
|
112
|
+
await optimizer.warmUp();
|
|
113
|
+
expect(optimizer.isWarm()).toBe(true);
|
|
114
|
+
optimizer.reset();
|
|
115
|
+
expect(optimizer.isWarm()).toBe(false);
|
|
116
|
+
expect(optimizer.getWarmupTimestamp()).toBeUndefined();
|
|
117
|
+
expect(optimizer.getPreloadedModules()).toHaveLength(0);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
describe('OptimizeColdStart Decorator', () => {
|
|
122
|
+
let optimizer;
|
|
123
|
+
beforeEach(() => {
|
|
124
|
+
optimizer = cold_start_optimizer_1.ColdStartOptimizer.getInstance();
|
|
125
|
+
optimizer.reset();
|
|
126
|
+
});
|
|
127
|
+
it('should warm up before method execution', async () => {
|
|
128
|
+
class TestClass {
|
|
129
|
+
async testMethod() {
|
|
130
|
+
return 'result';
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
__decorate([
|
|
134
|
+
(0, cold_start_optimizer_1.OptimizeColdStart)(),
|
|
135
|
+
__metadata("design:type", Function),
|
|
136
|
+
__metadata("design:paramtypes", []),
|
|
137
|
+
__metadata("design:returntype", Promise)
|
|
138
|
+
], TestClass.prototype, "testMethod", null);
|
|
139
|
+
const instance = new TestClass();
|
|
140
|
+
expect(optimizer.isWarm()).toBe(false);
|
|
141
|
+
const result = await instance.testMethod();
|
|
142
|
+
expect(result).toBe('result');
|
|
143
|
+
expect(optimizer.isWarm()).toBe(true);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
describe('KeepAliveHelper', () => {
|
|
147
|
+
let helper;
|
|
148
|
+
beforeEach(() => {
|
|
149
|
+
helper = new cold_start_optimizer_1.KeepAliveHelper();
|
|
150
|
+
});
|
|
151
|
+
afterEach(() => {
|
|
152
|
+
helper.stop();
|
|
153
|
+
});
|
|
154
|
+
it('should start keep-alive', () => {
|
|
155
|
+
expect(() => {
|
|
156
|
+
helper.start('http://example.com', 1000);
|
|
157
|
+
}).not.toThrow();
|
|
158
|
+
});
|
|
159
|
+
it('should stop keep-alive', () => {
|
|
160
|
+
helper.start('http://example.com', 1000);
|
|
161
|
+
expect(() => {
|
|
162
|
+
helper.stop();
|
|
163
|
+
}).not.toThrow();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
describe('LambdaAdapter', () => {
|
|
167
|
+
let TestModule = class TestModule {
|
|
168
|
+
};
|
|
169
|
+
TestModule = __decorate([
|
|
170
|
+
(0, core_1.HazelModule)({
|
|
171
|
+
controllers: [],
|
|
172
|
+
})
|
|
173
|
+
], TestModule);
|
|
174
|
+
let adapter;
|
|
175
|
+
beforeEach(() => {
|
|
176
|
+
adapter = new lambda_adapter_1.LambdaAdapter(TestModule);
|
|
177
|
+
});
|
|
178
|
+
it('should create Lambda adapter', () => {
|
|
179
|
+
expect(adapter).toBeDefined();
|
|
180
|
+
});
|
|
181
|
+
it('should create handler function', () => {
|
|
182
|
+
const handler = adapter.createHandler();
|
|
183
|
+
expect(handler).toBeDefined();
|
|
184
|
+
expect(typeof handler).toBe('function');
|
|
185
|
+
});
|
|
186
|
+
it('should be cold on first check', () => {
|
|
187
|
+
expect(adapter.isCold()).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
describe('createLambdaHandler', () => {
|
|
191
|
+
let TestModule = class TestModule {
|
|
192
|
+
};
|
|
193
|
+
TestModule = __decorate([
|
|
194
|
+
(0, core_1.HazelModule)({
|
|
195
|
+
controllers: [],
|
|
196
|
+
})
|
|
197
|
+
], TestModule);
|
|
198
|
+
it('should create Lambda handler', () => {
|
|
199
|
+
const handler = (0, lambda_adapter_1.createLambdaHandler)(TestModule);
|
|
200
|
+
expect(handler).toBeDefined();
|
|
201
|
+
expect(typeof handler).toBe('function');
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
describe('CloudFunctionAdapter', () => {
|
|
205
|
+
let TestModule = class TestModule {
|
|
206
|
+
};
|
|
207
|
+
TestModule = __decorate([
|
|
208
|
+
(0, core_1.HazelModule)({
|
|
209
|
+
controllers: [],
|
|
210
|
+
})
|
|
211
|
+
], TestModule);
|
|
212
|
+
let adapter;
|
|
213
|
+
beforeEach(() => {
|
|
214
|
+
adapter = new cloud_function_adapter_1.CloudFunctionAdapter(TestModule);
|
|
215
|
+
});
|
|
216
|
+
it('should create Cloud Function adapter', () => {
|
|
217
|
+
expect(adapter).toBeDefined();
|
|
218
|
+
});
|
|
219
|
+
it('should create HTTP handler', () => {
|
|
220
|
+
const handler = adapter.createHttpHandler();
|
|
221
|
+
expect(handler).toBeDefined();
|
|
222
|
+
expect(typeof handler).toBe('function');
|
|
223
|
+
});
|
|
224
|
+
it('should create event handler', () => {
|
|
225
|
+
const handler = adapter.createEventHandler();
|
|
226
|
+
expect(handler).toBeDefined();
|
|
227
|
+
expect(typeof handler).toBe('function');
|
|
228
|
+
});
|
|
229
|
+
it('should be cold on first check', () => {
|
|
230
|
+
expect(adapter.isCold()).toBe(true);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
describe('createCloudFunctionHandler', () => {
|
|
234
|
+
let TestModule = class TestModule {
|
|
235
|
+
};
|
|
236
|
+
TestModule = __decorate([
|
|
237
|
+
(0, core_1.HazelModule)({
|
|
238
|
+
controllers: [],
|
|
239
|
+
})
|
|
240
|
+
], TestModule);
|
|
241
|
+
it('should create Cloud Function HTTP handler', () => {
|
|
242
|
+
const handler = (0, cloud_function_adapter_1.createCloudFunctionHandler)(TestModule);
|
|
243
|
+
expect(handler).toBeDefined();
|
|
244
|
+
expect(typeof handler).toBe('function');
|
|
245
|
+
});
|
|
246
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hazeljs/serverless",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.55",
|
|
4
4
|
"description": "Serverless adapters (AWS Lambda, Google Cloud Functions) for HazelJS framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"@hazeljs/core": ">=0.2.0-beta.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "f2e54f346eea552595a44607999454a9e388cb9e"
|
|
52
52
|
}
|