@geek-fun/serverlessinsight 0.9.1 → 0.9.3
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/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +1 -1
- package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +2 -2
- package/.serverlessinsight/state-insight-poc-app-insight-poc-tencent.json.backup +4 -8
- package/.serverlessinsight/state-insight-poc-app-log-insight-poc-tencent-log.json.backup +3 -8
- package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +4 -4
- package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +1 -1
- package/.serverlessinsight/state-insight-poc-timer-app-insight-poc-timer.json.backup +132 -0
- package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +2 -2
- package/.serverlessinsight/state-insight-volc-nested-app-insight-volc-nested.json.backup +3 -3
- package/.serverlessinsight/state-nested-drift-app-nested-drift-service.json.backup +20 -5
- package/.serverlessinsight/state-role-reconcile-test-app-role-reconcile-test-service.json.backup +3 -3
- package/AGENTS.md +10 -0
- package/CHANGELOG.md +51 -0
- package/README.md +103 -13
- package/README.zh-CN.md +73 -13
- package/codecov +0 -0
- package/codecov.SHA256SUM +1 -0
- package/codecov.SHA256SUM.sig +16 -0
- package/dist/package.json +1 -1
- package/dist/src/commands/index.js +92 -24
- package/dist/src/commands/local.js +16 -3
- package/dist/src/commands/plan.js +31 -4
- package/dist/src/commands/schema.js +24 -0
- package/dist/src/commands/show.js +64 -10
- package/dist/src/commands/validate.js +34 -2
- package/dist/src/common/aliyunClient/apigwOperations.js +17 -1
- package/dist/src/common/aliyunClient/fc3Operations.js +39 -2
- package/dist/src/common/aliyunClient/ossOperations.js +214 -221
- package/dist/src/common/bucketPolicy.js +63 -0
- package/dist/src/common/constants.js +7 -1
- package/dist/src/common/domainUtils.js +4 -1
- package/dist/src/common/functionDefinition.js +25 -0
- package/dist/src/common/index.js +4 -0
- package/dist/src/common/jsonOutput.js +82 -0
- package/dist/src/common/logger.js +6 -3
- package/dist/src/common/nameBuilder.js +3 -1
- package/dist/src/common/noColor.js +9 -0
- package/dist/src/common/planCapabilities.js +119 -0
- package/dist/src/common/planCompare.js +119 -66
- package/dist/src/common/planFormatter.js +34 -20
- package/dist/src/common/planRedaction.js +26 -0
- package/dist/src/common/refreshPlanner.js +66 -21
- package/dist/src/common/siCron.js +415 -0
- package/dist/src/common/stateBackend/saasStateBackend.js +5 -4
- package/dist/src/common/tencentClient/cosOperations.js +326 -322
- package/dist/src/common/tencentClient/scfOperations.js +11 -2
- package/dist/src/common/tencentClient/tdsqlcOperations.js +6 -11
- package/dist/src/common/triggerMapper.js +16 -1
- package/dist/src/common/volcengineClient/apigwOperations.js +82 -0
- package/dist/src/common/volcengineClient/iamOperations.js +27 -3
- package/dist/src/common/volcengineClient/tosOperations.js +62 -56
- package/dist/src/common/volcengineClient/vefaasOperations.js +45 -0
- package/dist/src/lang/en.js +70 -3
- package/dist/src/lang/index.js +36 -1
- package/dist/src/lang/zh-CN.js +70 -3
- package/dist/src/parser/eventParser.js +1 -0
- package/dist/src/parser/functionParser.js +48 -2
- package/dist/src/stack/aliyunStack/apigwExecutor.js +1 -1
- package/dist/src/stack/aliyunStack/apigwPlanner.js +103 -23
- package/dist/src/stack/aliyunStack/apigwResource.js +4 -3
- package/dist/src/stack/aliyunStack/apigwTypes.js +3 -7
- package/dist/src/stack/aliyunStack/deployer.js +2 -0
- package/dist/src/stack/aliyunStack/esServerlessTypes.js +8 -4
- package/dist/src/stack/aliyunStack/fc3Executor.js +7 -3
- package/dist/src/stack/aliyunStack/fc3Planner.js +148 -153
- package/dist/src/stack/aliyunStack/fc3Resource.js +206 -53
- package/dist/src/stack/aliyunStack/fc3Types.js +30 -16
- package/dist/src/stack/aliyunStack/ossPlanner.js +15 -1
- package/dist/src/stack/aliyunStack/ossResource.js +113 -102
- package/dist/src/stack/aliyunStack/ossTypes.js +20 -20
- package/dist/src/stack/localStack/aliyunFc.js +1 -6
- package/dist/src/stack/localStack/event.js +31 -71
- package/dist/src/stack/localStack/function.js +25 -77
- package/dist/src/stack/localStack/functionRunner.js +10 -2
- package/dist/src/stack/localStack/httpAdapter.js +54 -0
- package/dist/src/stack/localStack/httpError.js +5 -0
- package/dist/src/stack/localStack/index.js +8 -1
- package/dist/src/stack/localStack/legacyLambda.js +26 -0
- package/dist/src/stack/localStack/localContext.js +29 -0
- package/dist/src/stack/localStack/localRoute.js +12 -0
- package/dist/src/stack/localStack/localRuntimes.js +22 -0
- package/dist/src/stack/localStack/localServer.js +8 -3
- package/dist/src/stack/localStack/requestBody.js +24 -0
- package/dist/src/stack/localStack/requestId.js +18 -0
- package/dist/src/stack/localStack/tencent.js +147 -0
- package/dist/src/stack/localStack/timer.js +183 -0
- package/dist/src/stack/localStack/utils.js +19 -1
- package/dist/src/stack/localStack/volcengine.js +68 -0
- package/dist/src/stack/scfStack/cosPlanner.js +9 -14
- package/dist/src/stack/scfStack/cosResource.js +42 -50
- package/dist/src/stack/scfStack/cosTypes.js +19 -14
- package/dist/src/stack/scfStack/deployer.js +2 -0
- package/dist/src/stack/scfStack/scfExecutor.js +7 -3
- package/dist/src/stack/scfStack/scfHttpTrigger.js +106 -0
- package/dist/src/stack/scfStack/scfPlanner.js +114 -29
- package/dist/src/stack/scfStack/scfResource.js +329 -311
- package/dist/src/stack/scfStack/scfTypes.js +4 -3
- package/dist/src/stack/scfStack/tdsqlcPlanner.js +10 -13
- package/dist/src/stack/volcengineStack/apigwDomains.js +64 -0
- package/dist/src/stack/volcengineStack/apigwPlanner.js +71 -15
- package/dist/src/stack/volcengineStack/apigwResource.js +60 -24
- package/dist/src/stack/volcengineStack/apigwTypes.js +5 -1
- package/dist/src/stack/volcengineStack/deployer.js +6 -0
- package/dist/src/stack/volcengineStack/tosPlanner.js +8 -13
- package/dist/src/stack/volcengineStack/tosResource.js +22 -35
- package/dist/src/stack/volcengineStack/tosTypes.js +5 -8
- package/dist/src/stack/volcengineStack/vefaasPlanner.js +186 -44
- package/dist/src/stack/volcengineStack/vefaasResource.js +232 -14
- package/dist/src/stack/volcengineStack/vefaasTypes.js +6 -3
- package/dist/src/validator/functionSchema.js +25 -2
- package/dist/src/validator/iacSchema.js +2 -1
- package/dist/src/validator/index.js +4 -1
- package/dist/src/validator/rootSchema.js +49 -18
- package/dist/src/validator/schemaExport.js +60 -0
- package/dist/src/validator/semanticValidation.js +79 -0
- package/dist/src/validator/templateRefSchema.js +9 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/release-notes.md +79 -0
- package/samples/aliyun-poc-timer.yml +39 -0
- package/samples/tencent-local/index.js +13 -0
- package/samples/tencent-local/serverlessinsight.yml +23 -0
- package/samples/volcengine-local/index.js +13 -0
- package/samples/volcengine-local/serverlessinsight.yml +28 -0
|
@@ -4,14 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.functionsHandler = void 0;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_util_1 = require("node:util");
|
|
7
9
|
const common_1 = require("../../common");
|
|
8
10
|
const functionRunner_1 = require("./functionRunner");
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const aliyunFc_1 = require("./aliyunFc");
|
|
11
|
+
const requestId_1 = require("./requestId");
|
|
12
|
+
const httpAdapter_1 = require("./httpAdapter");
|
|
12
13
|
const utils_1 = require("./utils");
|
|
13
14
|
const lang_1 = require("../../lang");
|
|
14
|
-
const functionsHandler = async (req, parsed, iac) => {
|
|
15
|
+
const functionsHandler = async (req, parsed, iac, requestId = (0, requestId_1.generateProviderRequestId)(iac.provider.name)) => {
|
|
15
16
|
common_1.logger.info(lang_1.lang.__('FUNCTION_REQUEST_RECEIVED', {
|
|
16
17
|
method: req.method || '',
|
|
17
18
|
identifier: parsed.identifier ?? '/',
|
|
@@ -20,29 +21,21 @@ const functionsHandler = async (req, parsed, iac) => {
|
|
|
20
21
|
if (!fcDef) {
|
|
21
22
|
return {
|
|
22
23
|
statusCode: 404,
|
|
23
|
-
body: { error: '
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (!fcDef.code) {
|
|
27
|
-
return {
|
|
28
|
-
statusCode: 400,
|
|
29
|
-
body: { error: 'Function code configuration not found', functionKey: fcDef.key },
|
|
24
|
+
body: { error: lang_1.lang.__('LOCAL_FUNCTION_NOT_FOUND'), functionKey: parsed.identifier },
|
|
30
25
|
};
|
|
31
26
|
}
|
|
32
27
|
let tempDir = null;
|
|
33
28
|
try {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
codeDir = codePath;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
codeDir = node_path_1.default.dirname(codePath);
|
|
29
|
+
const adapter = (0, httpAdapter_1.getLocalHttpAdapter)(iac.provider.name);
|
|
30
|
+
adapter.validate(fcDef);
|
|
31
|
+
if (!fcDef.code) {
|
|
32
|
+
return {
|
|
33
|
+
statusCode: 400,
|
|
34
|
+
body: { error: lang_1.lang.__('LOCAL_FUNCTION_CODE_MISSING'), functionKey: fcDef.key },
|
|
35
|
+
};
|
|
45
36
|
}
|
|
37
|
+
const { codeDir, tempDir: resolvedTempDir } = await (0, utils_1.resolveCodeDir)(fcDef.code.path);
|
|
38
|
+
tempDir = resolvedTempDir;
|
|
46
39
|
const funOptions = {
|
|
47
40
|
codeDir,
|
|
48
41
|
functionKey: fcDef.key,
|
|
@@ -50,69 +43,24 @@ const functionsHandler = async (req, parsed, iac) => {
|
|
|
50
43
|
servicePath: '',
|
|
51
44
|
timeout: (fcDef.timeout ?? 3) * 1000,
|
|
52
45
|
};
|
|
53
|
-
|
|
54
|
-
const isAliyun = iac.provider.name === common_1.ProviderEnum.ALIYUN;
|
|
55
|
-
let event;
|
|
56
|
-
let fcContext;
|
|
57
|
-
let env;
|
|
58
|
-
if (isAliyun) {
|
|
59
|
-
// Aliyun FC format: event is a Buffer containing JSON
|
|
60
|
-
const requestId = (0, aliyunFc_1.generateRequestId)();
|
|
61
|
-
const { event: aliyunEvent } = await (0, aliyunFc_1.transformToAliyunEvent)(req, parsed.url, parsed.query);
|
|
62
|
-
event = aliyunEvent;
|
|
63
|
-
// Use serializable context for worker thread (logger will be added inside worker)
|
|
64
|
-
fcContext = (0, aliyunFc_1.createAliyunContextSerializable)(iac, fcDef.name, fcDef.code.handler, fcDef.memory ?? 128, fcDef.timeout ?? 3, requestId);
|
|
65
|
-
env = {
|
|
66
|
-
...fcDef.environment,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
// AWS Lambda format (default)
|
|
71
|
-
const rawBody = await (0, common_1.readRequestBody)(req);
|
|
72
|
-
event = rawBody ? JSON.parse(rawBody) : {};
|
|
73
|
-
env = {
|
|
74
|
-
...fcDef.environment,
|
|
75
|
-
AWS_REGION: iac.provider.region || 'us-east-1',
|
|
76
|
-
FUNCTION_NAME: fcDef.name,
|
|
77
|
-
FUNCTION_MEMORY_SIZE: String(fcDef.memory),
|
|
78
|
-
FUNCTION_TIMEOUT: String(fcDef.timeout),
|
|
79
|
-
};
|
|
80
|
-
fcContext = {
|
|
81
|
-
functionName: fcDef.name,
|
|
82
|
-
functionVersion: '$LATEST',
|
|
83
|
-
memoryLimitInMB: fcDef.memory,
|
|
84
|
-
logGroupName: `/aws/lambda/${fcDef.name}`,
|
|
85
|
-
logStreamName: `${new Date().toISOString().split('T')[0]}/[$LATEST]${Math.random().toString(36).substring(7)}`,
|
|
86
|
-
invokedFunctionArn: `arn:aws:lambda:${iac.provider.region}:000000000000:function:${fcDef.name}`,
|
|
87
|
-
awsRequestId: Math.random().toString(36).substring(2, 15),
|
|
88
|
-
};
|
|
89
|
-
}
|
|
46
|
+
const { event, context, env } = await adapter.buildInvocation(req, parsed, iac, fcDef, requestId);
|
|
90
47
|
common_1.logger.debug(lang_1.lang.__('INVOKING_WORKER_WITH_EVENT_TYPE', {
|
|
91
|
-
eventType:
|
|
48
|
+
eventType: Buffer.isBuffer(event) ? 'Buffer' : 'Object',
|
|
92
49
|
}));
|
|
93
50
|
common_1.logger.debug(lang_1.lang.__('WORKER_CODE_DIR', { codeDir, handler: funOptions.handler }));
|
|
94
|
-
const result = await (0, functionRunner_1.invokeFunction)(funOptions, env, event,
|
|
95
|
-
common_1.logger.info(lang_1.lang.__('FUNCTION_EXECUTION_RESULT', { result:
|
|
96
|
-
|
|
97
|
-
if (isAliyun && result) {
|
|
98
|
-
const transformed = (0, aliyunFc_1.transformFCResponse)(result);
|
|
99
|
-
return {
|
|
100
|
-
statusCode: transformed.statusCode,
|
|
101
|
-
headers: transformed.headers,
|
|
102
|
-
body: transformed.body,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
return {
|
|
106
|
-
statusCode: 200,
|
|
107
|
-
body: result,
|
|
108
|
-
};
|
|
51
|
+
const result = await (0, functionRunner_1.invokeFunction)(funOptions, env, event, context);
|
|
52
|
+
common_1.logger.info(lang_1.lang.__('FUNCTION_EXECUTION_RESULT', { result: (0, node_util_1.inspect)(result) }));
|
|
53
|
+
return adapter.transformResponse(result, parsed.kind);
|
|
109
54
|
}
|
|
110
55
|
catch (error) {
|
|
111
56
|
common_1.logger.error(lang_1.lang.__('FUNCTION_EXECUTION_ERROR', { error: String(error) }));
|
|
57
|
+
const statusCode = error instanceof Error && 'statusCode' in error && typeof error.statusCode === 'number'
|
|
58
|
+
? error.statusCode
|
|
59
|
+
: 500;
|
|
112
60
|
return {
|
|
113
|
-
statusCode
|
|
61
|
+
statusCode,
|
|
114
62
|
body: {
|
|
115
|
-
error: '
|
|
63
|
+
error: lang_1.lang.__('LOCAL_FUNCTION_EXECUTION_FAILED'),
|
|
116
64
|
message: error instanceof Error ? error.message : String(error),
|
|
117
65
|
},
|
|
118
66
|
};
|
|
@@ -37,6 +37,7 @@ exports.invokeFunction = exports.runFunction = void 0;
|
|
|
37
37
|
const node_fs_1 = require("node:fs");
|
|
38
38
|
const node_path_1 = __importStar(require("node:path"));
|
|
39
39
|
const node_url_1 = require("node:url");
|
|
40
|
+
const node_util_1 = require("node:util");
|
|
40
41
|
const lang_1 = require("../../lang");
|
|
41
42
|
const node_worker_threads_1 = require("node:worker_threads");
|
|
42
43
|
// ============================================================================
|
|
@@ -197,7 +198,7 @@ const createMessageHandler = (port, resolve, reject) => {
|
|
|
197
198
|
return;
|
|
198
199
|
resolved = true;
|
|
199
200
|
port.close();
|
|
200
|
-
return value
|
|
201
|
+
return node_util_1.types.isNativeError(value) ? reject(value) : resolve(value);
|
|
201
202
|
};
|
|
202
203
|
const handleError = (err) => {
|
|
203
204
|
if (resolved)
|
|
@@ -264,10 +265,17 @@ const runFunction = (funOptions, env) => {
|
|
|
264
265
|
exports.runFunction = runFunction;
|
|
265
266
|
const invokeFunction = async (funOptions, env, event, context) => {
|
|
266
267
|
const runner = (0, exports.runFunction)(funOptions, env);
|
|
268
|
+
let timeoutId;
|
|
269
|
+
const deadline = new Promise((_resolve, reject) => {
|
|
270
|
+
timeoutId = setTimeout(() => {
|
|
271
|
+
reject(new Error(lang_1.lang.__('LOCAL_EXECUTION_TIMEOUT', { timeoutMs: String(funOptions.timeout) })));
|
|
272
|
+
}, funOptions.timeout);
|
|
273
|
+
});
|
|
267
274
|
try {
|
|
268
|
-
return await runner.execute(event, context);
|
|
275
|
+
return await Promise.race([runner.execute(event, context), deadline]);
|
|
269
276
|
}
|
|
270
277
|
finally {
|
|
278
|
+
clearTimeout(timeoutId);
|
|
271
279
|
await runner.terminate();
|
|
272
280
|
}
|
|
273
281
|
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLocalHttpAdapter = void 0;
|
|
4
|
+
const common_1 = require("../../common");
|
|
5
|
+
const lang_1 = require("../../lang");
|
|
6
|
+
const localStack_1 = require("../../types/localStack");
|
|
7
|
+
const aliyunFc_1 = require("./aliyunFc");
|
|
8
|
+
const legacyLambda_1 = require("./legacyLambda");
|
|
9
|
+
const tencent_1 = require("./tencent");
|
|
10
|
+
const volcengine_1 = require("./volcengine");
|
|
11
|
+
const httpError_1 = require("./httpError");
|
|
12
|
+
const aliyunHttpAdapter = {
|
|
13
|
+
validate: () => undefined,
|
|
14
|
+
buildInvocation: async (req, parsed, iac, fn, requestId) => ({
|
|
15
|
+
event: (await (0, aliyunFc_1.transformToAliyunEvent)(req, parsed.url, parsed.query)).event,
|
|
16
|
+
context: (0, aliyunFc_1.createAliyunContextSerializable)(iac, fn.name, fn.code.handler, fn.memory ?? 128, fn.timeout ?? 3, requestId),
|
|
17
|
+
env: { ...fn.environment },
|
|
18
|
+
}),
|
|
19
|
+
transformResponse: (result, kind) => result || kind === localStack_1.RouteKind.SI_EVENTS
|
|
20
|
+
? (0, aliyunFc_1.transformFCResponse)(result)
|
|
21
|
+
: { statusCode: 200, body: result },
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Generic execution for providers without a native local HTTP simulation.
|
|
25
|
+
* The request body is treated as an already-formed event object; this is a
|
|
26
|
+
* low-level invocation harness, not a provider-native simulation.
|
|
27
|
+
*/
|
|
28
|
+
const genericHttpAdapter = {
|
|
29
|
+
validate: () => undefined,
|
|
30
|
+
buildInvocation: async (req, _parsed, iac, fn, requestId) => {
|
|
31
|
+
const rawBody = await (0, common_1.readRequestBody)(req);
|
|
32
|
+
return {
|
|
33
|
+
event: rawBody ? JSON.parse(rawBody) : {},
|
|
34
|
+
env: { ...fn.environment, ...(0, legacyLambda_1.createLegacyEnv)(iac, fn) },
|
|
35
|
+
context: (0, legacyLambda_1.createLegacyContext)(iac, fn, requestId),
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
transformResponse: (result) => ({ statusCode: 200, body: result }),
|
|
39
|
+
};
|
|
40
|
+
const httpAdapters = {
|
|
41
|
+
[common_1.ProviderEnum.ALIYUN]: aliyunHttpAdapter,
|
|
42
|
+
[common_1.ProviderEnum.VOLCENGINE]: volcengine_1.volcengineHttpAdapter,
|
|
43
|
+
[common_1.ProviderEnum.TENCENT]: tencent_1.scfHttpAdapter,
|
|
44
|
+
[common_1.ProviderEnum.AWS]: genericHttpAdapter,
|
|
45
|
+
[common_1.ProviderEnum.HUAWEI]: genericHttpAdapter,
|
|
46
|
+
};
|
|
47
|
+
const getLocalHttpAdapter = (provider) => {
|
|
48
|
+
const adapter = Object.hasOwn(httpAdapters, provider) ? httpAdapters[provider] : undefined;
|
|
49
|
+
if (!adapter) {
|
|
50
|
+
throw (0, httpError_1.createLocalHttpError)(501, lang_1.lang.__('LOCAL_HTTP_PROVIDER_UNSUPPORTED', { provider }));
|
|
51
|
+
}
|
|
52
|
+
return adapter;
|
|
53
|
+
};
|
|
54
|
+
exports.getLocalHttpAdapter = getLocalHttpAdapter;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLocalHttpError = void 0;
|
|
4
|
+
const createLocalHttpError = (statusCode, message) => Object.assign(new Error(message), { statusCode });
|
|
5
|
+
exports.createLocalHttpError = createLocalHttpError;
|
|
@@ -17,10 +17,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
exports.startLocalStack = exports.stopLocal = void 0;
|
|
18
18
|
const localStack_1 = require("../../types/localStack");
|
|
19
19
|
const localServer_1 = require("./localServer");
|
|
20
|
-
Object.defineProperty(exports, "stopLocal", { enumerable: true, get: function () { return localServer_1.stopLocal; } });
|
|
21
20
|
const event_1 = require("./event");
|
|
22
21
|
const function_1 = require("./function");
|
|
23
22
|
const bucket_1 = require("./bucket");
|
|
23
|
+
const timer_1 = require("./timer");
|
|
24
24
|
__exportStar(require("./event"), exports);
|
|
25
25
|
const handlers = [
|
|
26
26
|
{ kind: localStack_1.RouteKind.SI_FUNCTIONS, handler: function_1.functionsHandler },
|
|
@@ -29,5 +29,12 @@ const handlers = [
|
|
|
29
29
|
];
|
|
30
30
|
const startLocalStack = async (iac) => {
|
|
31
31
|
await (0, localServer_1.servLocal)(handlers, iac);
|
|
32
|
+
// functions.*.triggers.timer fire locally too (issue #258)
|
|
33
|
+
(0, timer_1.startLocalTimers)(iac);
|
|
32
34
|
};
|
|
33
35
|
exports.startLocalStack = startLocalStack;
|
|
36
|
+
const stopLocal = async () => {
|
|
37
|
+
(0, timer_1.stopLocalTimers)();
|
|
38
|
+
await (0, localServer_1.stopLocal)();
|
|
39
|
+
};
|
|
40
|
+
exports.stopLocal = stopLocal;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLegacyEnv = exports.createLegacyContext = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* The generic execution context shared by providers without a native local HTTP
|
|
6
|
+
* simulation (AWS, Huawei). It is an AWS Lambda-shaped context kept only for
|
|
7
|
+
* backward compatibility: it is not a provider-native contract for either
|
|
8
|
+
* provider, and local provider-native parity is not claimed for them.
|
|
9
|
+
*/
|
|
10
|
+
const createLegacyContext = (iac, fn, requestId) => ({
|
|
11
|
+
functionName: fn.name,
|
|
12
|
+
functionVersion: '$LATEST',
|
|
13
|
+
memoryLimitInMB: fn.memory,
|
|
14
|
+
logGroupName: `/aws/lambda/${fn.name}`,
|
|
15
|
+
logStreamName: `${new Date().toISOString().split('T')[0]}/[$LATEST]${requestId}`,
|
|
16
|
+
invokedFunctionArn: `arn:aws:lambda:${iac.provider.region}:000000000000:function:${fn.name}`,
|
|
17
|
+
awsRequestId: requestId,
|
|
18
|
+
});
|
|
19
|
+
exports.createLegacyContext = createLegacyContext;
|
|
20
|
+
const createLegacyEnv = (iac, fn) => ({
|
|
21
|
+
AWS_REGION: iac.provider.region || 'us-east-1',
|
|
22
|
+
FUNCTION_NAME: fn.name,
|
|
23
|
+
FUNCTION_MEMORY_SIZE: String(fn.memory),
|
|
24
|
+
FUNCTION_TIMEOUT: String(fn.timeout),
|
|
25
|
+
});
|
|
26
|
+
exports.createLegacyEnv = createLegacyEnv;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildLocalContext = void 0;
|
|
4
|
+
const common_1 = require("../../common");
|
|
5
|
+
const aliyunFc_1 = require("./aliyunFc");
|
|
6
|
+
const legacyLambda_1 = require("./legacyLambda");
|
|
7
|
+
const tencent_1 = require("./tencent");
|
|
8
|
+
const volcengine_1 = require("./volcengine");
|
|
9
|
+
/**
|
|
10
|
+
* The single entry point for local invocation contexts.
|
|
11
|
+
*
|
|
12
|
+
* HTTP adapters and the local timer simulation both go through it, so a
|
|
13
|
+
* provider is never handed another provider's context shape. Every provider
|
|
14
|
+
* owns its builder; AWS and Huawei share the legacy generic context and make
|
|
15
|
+
* no provider-native claim.
|
|
16
|
+
*/
|
|
17
|
+
const buildLocalContext = (provider, iac, fn, requestId) => {
|
|
18
|
+
switch (provider) {
|
|
19
|
+
case common_1.ProviderEnum.ALIYUN:
|
|
20
|
+
return (0, aliyunFc_1.createAliyunContextSerializable)(iac, fn.name, fn.code?.handler ?? '', fn.memory ?? 128, fn.timeout ?? 3, requestId);
|
|
21
|
+
case common_1.ProviderEnum.TENCENT:
|
|
22
|
+
return (0, tencent_1.createScfContextSerializable)(iac, fn, requestId);
|
|
23
|
+
case common_1.ProviderEnum.VOLCENGINE:
|
|
24
|
+
return (0, volcengine_1.createVolcengineContext)(requestId);
|
|
25
|
+
default:
|
|
26
|
+
return (0, legacyLambda_1.createLegacyContext)(iac, fn, requestId);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
exports.buildLocalContext = buildLocalContext;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.preservesLocalPath = void 0;
|
|
4
|
+
const common_1 = require("../../common");
|
|
5
|
+
/**
|
|
6
|
+
* Providers whose local HTTP routes keep the request path verbatim, including
|
|
7
|
+
* repeated and trailing slashes. Their simulated HTTP triggers match the path
|
|
8
|
+
* exactly rather than collapsing or wildcarding path segments.
|
|
9
|
+
*/
|
|
10
|
+
const PRESERVES_PATH = [common_1.ProviderEnum.TENCENT, common_1.ProviderEnum.VOLCENGINE];
|
|
11
|
+
const preservesLocalPath = (provider) => PRESERVES_PATH.includes(provider);
|
|
12
|
+
exports.preservesLocalPath = preservesLocalPath;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isLocalNodeRuntime = exports.localNodeRuntimes = void 0;
|
|
4
|
+
const common_1 = require("../../common");
|
|
5
|
+
/**
|
|
6
|
+
* Runtimes the shared local worker can actually execute, keyed by provider.
|
|
7
|
+
*
|
|
8
|
+
* Entries use the runtime identifiers an IAC declares: the standard runtimes of
|
|
9
|
+
* src/common/runtimeMapper.ts for Tencent, the veFaaS runtime ids for Volcengine.
|
|
10
|
+
* This is a local execution capability list, not the deploy-time support list —
|
|
11
|
+
* the local worker runs the installed Node.js, so other languages, custom
|
|
12
|
+
* runtimes and container images cannot be simulated. Keep it in step with the
|
|
13
|
+
* provider's Node.js runtime set when that changes.
|
|
14
|
+
*/
|
|
15
|
+
const LOCAL_NODE_RUNTIMES = {
|
|
16
|
+
[common_1.ProviderEnum.TENCENT]: ['nodejs10', 'nodejs12', 'nodejs14', 'nodejs16', 'nodejs18'],
|
|
17
|
+
[common_1.ProviderEnum.VOLCENGINE]: ['node14/v1', 'node20/v1'],
|
|
18
|
+
};
|
|
19
|
+
const localNodeRuntimes = (provider) => LOCAL_NODE_RUNTIMES[provider] ?? [];
|
|
20
|
+
exports.localNodeRuntimes = localNodeRuntimes;
|
|
21
|
+
const isLocalNodeRuntime = (provider, runtime) => (0, exports.localNodeRuntimes)(provider).includes(runtime);
|
|
22
|
+
exports.isLocalNodeRuntime = isLocalNodeRuntime;
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.stopLocal = exports.servLocal = void 0;
|
|
7
7
|
const localStack_1 = require("../../types/localStack");
|
|
8
8
|
const common_1 = require("../../common");
|
|
9
|
+
const localRoute_1 = require("./localRoute");
|
|
9
10
|
const node_http_1 = __importDefault(require("node:http"));
|
|
10
11
|
const lang_1 = require("../../lang");
|
|
11
12
|
let localServer;
|
|
@@ -21,7 +22,7 @@ const respondRaw = (res, status, body, headers = {}) => {
|
|
|
21
22
|
res.writeHead(status, headers);
|
|
22
23
|
res.end(body);
|
|
23
24
|
};
|
|
24
|
-
const parseRequest = (req) => {
|
|
25
|
+
const parseRequest = (req, preservePath) => {
|
|
25
26
|
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
26
27
|
const [routeSegment, identifierSegment, ...rest] = cleanPathSegments(url.pathname);
|
|
27
28
|
if (!routeSegment) {
|
|
@@ -32,7 +33,11 @@ const parseRequest = (req) => {
|
|
|
32
33
|
if (!kind) {
|
|
33
34
|
return undefined;
|
|
34
35
|
}
|
|
35
|
-
const subPath =
|
|
36
|
+
const subPath = preservePath
|
|
37
|
+
? (url.pathname.match(/^\/[^/]+\/[^/]+(\/.*)?$/)?.[1] ?? '/')
|
|
38
|
+
: rest.length > 0
|
|
39
|
+
? `/${rest.join('/')}`
|
|
40
|
+
: '/';
|
|
36
41
|
return {
|
|
37
42
|
kind,
|
|
38
43
|
identifier: identifierSegment,
|
|
@@ -49,7 +54,7 @@ const servLocal = async (handlers, iac) => {
|
|
|
49
54
|
}
|
|
50
55
|
localServer = node_http_1.default.createServer(async (req, res) => {
|
|
51
56
|
try {
|
|
52
|
-
const parsed = parseRequest(req);
|
|
57
|
+
const parsed = parseRequest(req, (0, localRoute_1.preservesLocalPath)(iac.provider.name));
|
|
53
58
|
if (!parsed) {
|
|
54
59
|
respondJson(res, 404, { error: 'Route not found' });
|
|
55
60
|
return;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readUtf8Body = void 0;
|
|
4
|
+
const consumers_1 = require("node:stream/consumers");
|
|
5
|
+
const node_util_1 = require("node:util");
|
|
6
|
+
const lang_1 = require("../../lang");
|
|
7
|
+
const httpError_1 = require("./httpError");
|
|
8
|
+
/**
|
|
9
|
+
* Read a request body as UTF-8 text, refusing bytes that are not valid UTF-8.
|
|
10
|
+
*
|
|
11
|
+
* The providers simulated here hand the handler a string body, so a binary
|
|
12
|
+
* request has no representable event. `messageKey` selects the provider's
|
|
13
|
+
* "binary requests are not simulated" message.
|
|
14
|
+
*/
|
|
15
|
+
const readUtf8Body = async (req, messageKey) => {
|
|
16
|
+
const body = await (0, consumers_1.buffer)(req);
|
|
17
|
+
try {
|
|
18
|
+
return new node_util_1.TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(body);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw (0, httpError_1.createLocalHttpError)(415, lang_1.lang.__(messageKey));
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
exports.readUtf8Body = readUtf8Body;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateProviderRequestId = exports.generateRequestId = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const common_1 = require("../../common");
|
|
6
|
+
/**
|
|
7
|
+
* Local request ids are UUIDs. SCF (Tencent) and veFaaS (Volcengine) request ids
|
|
8
|
+
* keep their hyphens, matching the shape the providers document; Aliyun FC
|
|
9
|
+
* strips them.
|
|
10
|
+
*/
|
|
11
|
+
const generateRequestId = (preserveHyphens = false) => {
|
|
12
|
+
const requestId = (0, node_crypto_1.randomUUID)();
|
|
13
|
+
return preserveHyphens ? requestId : requestId.replace(/-/g, '');
|
|
14
|
+
};
|
|
15
|
+
exports.generateRequestId = generateRequestId;
|
|
16
|
+
const keepsHyphens = (provider) => provider === common_1.ProviderEnum.TENCENT || provider === common_1.ProviderEnum.VOLCENGINE;
|
|
17
|
+
const generateProviderRequestId = (provider) => (0, exports.generateRequestId)(keepsHyphens(provider));
|
|
18
|
+
exports.generateProviderRequestId = generateProviderRequestId;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.scfHttpAdapter = exports.transformScfResponse = exports.transformToScfEvent = exports.createScfContextSerializable = void 0;
|
|
4
|
+
const node_http_1 = require("node:http");
|
|
5
|
+
const common_1 = require("../../common");
|
|
6
|
+
const lang_1 = require("../../lang");
|
|
7
|
+
const httpError_1 = require("./httpError");
|
|
8
|
+
const localRuntimes_1 = require("./localRuntimes");
|
|
9
|
+
const requestBody_1 = require("./requestBody");
|
|
10
|
+
/**
|
|
11
|
+
* Local SCF (Tencent) HTTP simulation of the contract si deploys:
|
|
12
|
+
* `functions.*.triggers.http` creates a **function URL** (an SCF HTTP trigger
|
|
13
|
+
* with a NetConfig TriggerDesc), which for an event function delivers an
|
|
14
|
+
* API-Gateway-compatible event and expects a response envelope.
|
|
15
|
+
*
|
|
16
|
+
* A Web Function — the other HTTP-triggered function type, where the URL passes
|
|
17
|
+
* the raw HTTP request straight to an in-container server with no event
|
|
18
|
+
* conversion — is a different contract and is not simulated.
|
|
19
|
+
*
|
|
20
|
+
* Account identifiers are synthetic and stable across invocations: no cloud
|
|
21
|
+
* caller, CAM identity or app id exists offline.
|
|
22
|
+
*/
|
|
23
|
+
const SYNTHETIC_NAMESPACE = 'default';
|
|
24
|
+
const SYNTHETIC_APPID = '1250000000';
|
|
25
|
+
const SYNTHETIC_UIN = '100000000000';
|
|
26
|
+
const DEFAULT_REGION = 'ap-guangzhou';
|
|
27
|
+
const JSON_HEADERS = { 'content-type': 'application/json' };
|
|
28
|
+
const invalidResponse = () => {
|
|
29
|
+
throw (0, httpError_1.createLocalHttpError)(502, lang_1.lang.__('LOCAL_SCF_INVALID_RESPONSE'));
|
|
30
|
+
};
|
|
31
|
+
const remoteAddress = (req) => {
|
|
32
|
+
const address = req.socket?.remoteAddress || '127.0.0.1';
|
|
33
|
+
return address === '::1' || address === '::ffff:127.0.0.1' ? '127.0.0.1' : address;
|
|
34
|
+
};
|
|
35
|
+
/** Node keeps `set-cookie` as an array; the event carries one joined string. */
|
|
36
|
+
const requestHeaders = (req) => Object.fromEntries(Object.entries(req.headers).map(([key, value]) => [
|
|
37
|
+
key,
|
|
38
|
+
Array.isArray(value) ? value.join(', ') : (value ?? ''),
|
|
39
|
+
]));
|
|
40
|
+
const createScfContextSerializable = (iac, fn, requestId) => ({
|
|
41
|
+
memory_limit_in_mb: fn.memory ?? 128,
|
|
42
|
+
time_limit_in_ms: (fn.timeout ?? 3) * 1000,
|
|
43
|
+
request_id: requestId,
|
|
44
|
+
environment: JSON.stringify({ SCF_NAMESPACE: SYNTHETIC_NAMESPACE }),
|
|
45
|
+
environ: `SCF_NAMESPACE=${SYNTHETIC_NAMESPACE};SCF_NAMESPACE=${SYNTHETIC_NAMESPACE}`,
|
|
46
|
+
function_version: '$LATEST',
|
|
47
|
+
function_name: fn.name,
|
|
48
|
+
namespace: SYNTHETIC_NAMESPACE,
|
|
49
|
+
tencentcloud_region: iac.provider.region || DEFAULT_REGION,
|
|
50
|
+
tencentcloud_appid: SYNTHETIC_APPID,
|
|
51
|
+
tencentcloud_uin: SYNTHETIC_UIN,
|
|
52
|
+
});
|
|
53
|
+
exports.createScfContextSerializable = createScfContextSerializable;
|
|
54
|
+
/**
|
|
55
|
+
* The documented function-URL event: API-Gateway-compatible, minus the
|
|
56
|
+
* `requestContext`, `pathParameters`, `queryStringParameters`, `headerParameters`
|
|
57
|
+
* and `isBase64Encoded` fields a gateway integration request would carry.
|
|
58
|
+
*/
|
|
59
|
+
const transformToScfEvent = async (req, parsed) => ({
|
|
60
|
+
body: await (0, requestBody_1.readUtf8Body)(req, 'LOCAL_SCF_BINARY_REQUEST_UNSUPPORTED'),
|
|
61
|
+
headers: { ...requestHeaders(req), 'x-scf-remote-addr': remoteAddress(req) },
|
|
62
|
+
httpMethod: parsed.method,
|
|
63
|
+
path: parsed.url,
|
|
64
|
+
queryString: { ...parsed.query },
|
|
65
|
+
});
|
|
66
|
+
exports.transformToScfEvent = transformToScfEvent;
|
|
67
|
+
const toHeaderRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
68
|
+
? value
|
|
69
|
+
: undefined;
|
|
70
|
+
const normalizeHeaders = (headers) => Object.fromEntries(Object.entries(headers).map(([key, value]) => {
|
|
71
|
+
if (typeof value !== 'string')
|
|
72
|
+
return invalidResponse();
|
|
73
|
+
try {
|
|
74
|
+
(0, node_http_1.validateHeaderName)(key);
|
|
75
|
+
(0, node_http_1.validateHeaderValue)(key, value);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return invalidResponse();
|
|
79
|
+
}
|
|
80
|
+
return [key.toLowerCase(), value];
|
|
81
|
+
}));
|
|
82
|
+
const serializeJson = (value) => {
|
|
83
|
+
try {
|
|
84
|
+
const serialized = JSON.stringify(value);
|
|
85
|
+
return typeof serialized === 'string' ? serialized : invalidResponse();
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return invalidResponse();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Translate a handler result with the function-URL response contract: an
|
|
93
|
+
* explicit `{ statusCode, headers, body }` envelope is used as-is, and anything
|
|
94
|
+
* else is serialized with SCF's format inference (HTTP 200 and a JSON content
|
|
95
|
+
* type). No `isBase64Encoded` binary envelope is invented.
|
|
96
|
+
*/
|
|
97
|
+
const transformScfResponse = (result) => {
|
|
98
|
+
if (result == null) {
|
|
99
|
+
return { statusCode: 200, headers: JSON_HEADERS, body: '' };
|
|
100
|
+
}
|
|
101
|
+
if (typeof result === 'string') {
|
|
102
|
+
return { statusCode: 200, headers: JSON_HEADERS, body: result };
|
|
103
|
+
}
|
|
104
|
+
if (typeof result !== 'object') {
|
|
105
|
+
// bare numbers, booleans and other scalars have no inferred SCF response form
|
|
106
|
+
return invalidResponse();
|
|
107
|
+
}
|
|
108
|
+
const envelope = result;
|
|
109
|
+
if ('isBase64Encoded' in envelope) {
|
|
110
|
+
throw (0, httpError_1.createLocalHttpError)(501, lang_1.lang.__('LOCAL_SCF_BINARY_RESPONSE_UNSUPPORTED'));
|
|
111
|
+
}
|
|
112
|
+
if (!('statusCode' in envelope)) {
|
|
113
|
+
return { statusCode: 200, headers: JSON_HEADERS, body: serializeJson(envelope) };
|
|
114
|
+
}
|
|
115
|
+
const { statusCode = 200, headers = {}, body = '' } = envelope;
|
|
116
|
+
if (typeof statusCode !== 'number' || !Number.isInteger(statusCode))
|
|
117
|
+
return invalidResponse();
|
|
118
|
+
if (statusCode < 200 || statusCode > 599)
|
|
119
|
+
return invalidResponse();
|
|
120
|
+
if (typeof body !== 'string')
|
|
121
|
+
return invalidResponse();
|
|
122
|
+
const responseHeaders = toHeaderRecord(headers);
|
|
123
|
+
if (!responseHeaders)
|
|
124
|
+
return invalidResponse();
|
|
125
|
+
return {
|
|
126
|
+
statusCode,
|
|
127
|
+
headers: { ...JSON_HEADERS, ...normalizeHeaders(responseHeaders) },
|
|
128
|
+
body,
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
exports.transformScfResponse = transformScfResponse;
|
|
132
|
+
exports.scfHttpAdapter = {
|
|
133
|
+
validate: (fn) => {
|
|
134
|
+
if (fn.container || !fn.code || !(0, localRuntimes_1.isLocalNodeRuntime)(common_1.ProviderEnum.TENCENT, fn.code.runtime)) {
|
|
135
|
+
throw (0, httpError_1.createLocalHttpError)(501, lang_1.lang.__('LOCAL_SCF_MODE_UNSUPPORTED', {
|
|
136
|
+
functionName: fn.name,
|
|
137
|
+
runtimes: (0, localRuntimes_1.localNodeRuntimes)(common_1.ProviderEnum.TENCENT).join(', '),
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
buildInvocation: async (req, parsed, iac, fn, requestId) => ({
|
|
142
|
+
event: await (0, exports.transformToScfEvent)(req, parsed),
|
|
143
|
+
context: (0, exports.createScfContextSerializable)(iac, fn, requestId),
|
|
144
|
+
env: { ...fn.environment },
|
|
145
|
+
}),
|
|
146
|
+
transformResponse: (result) => (0, exports.transformScfResponse)(result),
|
|
147
|
+
};
|