@geek-fun/serverlessinsight 0.9.6 → 0.9.8
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/AGENTS.md +1 -0
- package/CHANGELOG.md +68 -0
- package/README.md +52 -11
- package/README.zh-CN.md +44 -10
- package/dist/package.json +2 -1
- package/dist/src/commands/deploy.js +3 -1
- package/dist/src/commands/destroy.js +3 -1
- package/dist/src/commands/forceUnlock.js +2 -2
- package/dist/src/commands/index.js +28 -8
- package/dist/src/commands/local.js +3 -2
- package/dist/src/commands/login.js +120 -37
- package/dist/src/commands/logout.js +3 -5
- package/dist/src/commands/migrate.js +17 -10
- package/dist/src/commands/plan.js +5 -1
- package/dist/src/commands/show.js +3 -1
- package/dist/src/commands/whoami.js +24 -6
- package/dist/src/common/aliyunClient/cdnOperations.js +64 -29
- package/dist/src/common/aliyunClient/dnsOperations.js +10 -0
- package/dist/src/common/aliyunClient/fc3Operations.js +36 -7
- package/dist/src/common/apiClient.js +1 -0
- package/dist/src/common/bindingReconcile.js +182 -0
- package/dist/src/common/cdnCapabilities.js +80 -0
- package/dist/src/common/certUtils.js +44 -5
- package/dist/src/common/consoleLines.js +126 -0
- package/dist/src/common/constants.js +4 -2
- package/dist/src/common/credentialStore.js +136 -39
- package/dist/src/common/domainUtils.js +23 -4
- package/dist/src/common/functionDefinition.js +4 -1
- package/dist/src/common/functionDomainCapabilities.js +28 -0
- package/dist/src/common/functionDomainReconcile.js +197 -0
- package/dist/src/common/lineContext.js +164 -0
- package/dist/src/common/planCapabilities.js +14 -27
- package/dist/src/common/planCompare.js +8 -7
- package/dist/src/common/planFormatter.js +71 -37
- package/dist/src/common/stateBackend/cosStateBackend.js +0 -46
- package/dist/src/common/stateBackend/index.js +24 -16
- package/dist/src/common/stateBackend/ossStateBackend.js +0 -25
- package/dist/src/common/stateBackend/remoteStateBackend.js +5 -22
- package/dist/src/common/stateBackend/saasStateBackend.js +46 -53
- package/dist/src/common/stateBackend/tosStateBackend.js +100 -0
- package/dist/src/common/stateBackend/types.js +0 -9
- package/dist/src/common/tencentClient/cdnOperations.js +165 -0
- package/dist/src/common/tencentClient/cosOperations.js +18 -2
- package/dist/src/common/tencentClient/dnspodOperations.js +12 -0
- package/dist/src/common/tencentClient/index.js +16 -0
- package/dist/src/common/tencentClient/scfOperations.js +23 -1
- package/dist/src/common/tencentClient/sslOperations.js +147 -77
- package/dist/src/common/volcengineClient/cdnOperations.js +221 -0
- package/dist/src/common/volcengineClient/dnsOperations.js +132 -0
- package/dist/src/common/volcengineClient/index.js +28 -0
- package/dist/src/common/volcengineClient/tosOperations.js +131 -1
- package/dist/src/lang/en.js +89 -12
- package/dist/src/lang/zh-CN.js +90 -11
- package/dist/src/parser/bucketParser.js +22 -79
- package/dist/src/parser/functionParser.js +13 -9
- package/dist/src/stack/aliyunStack/apigwResource.js +2 -1
- package/dist/src/stack/aliyunStack/fc3Planner.js +32 -12
- package/dist/src/stack/aliyunStack/fc3Resource.js +169 -121
- package/dist/src/stack/aliyunStack/ossPlanner.js +12 -4
- package/dist/src/stack/aliyunStack/ossResource.js +484 -501
- package/dist/src/stack/aliyunStack/ossTypes.js +21 -67
- package/dist/src/stack/localStack/aliyunFc.js +59 -9
- package/dist/src/stack/localStack/bucket.js +75 -28
- package/dist/src/stack/localStack/event.js +33 -24
- package/dist/src/stack/localStack/httpAdapter.js +1 -13
- package/dist/src/stack/localStack/index.js +2 -2
- package/dist/src/stack/localStack/localServer.js +85 -7
- package/dist/src/stack/scfStack/cosPlanner.js +11 -3
- package/dist/src/stack/scfStack/cosResource.js +339 -161
- package/dist/src/stack/scfStack/cosTypes.js +25 -34
- package/dist/src/stack/scfStack/scfPlanner.js +26 -8
- package/dist/src/stack/scfStack/scfResource.js +191 -124
- package/dist/src/stack/volcengineStack/tosPlanner.js +13 -1
- package/dist/src/stack/volcengineStack/tosResource.js +398 -7
- package/dist/src/stack/volcengineStack/tosTypes.js +27 -0
- package/dist/src/stack/volcengineStack/vefaasTypes.js +1 -1
- package/dist/src/types/domains/state.js +6 -0
- package/dist/src/validator/bucketSchema.js +35 -113
- package/dist/src/validator/cdnSchema.js +30 -0
- package/dist/src/validator/eventSchema.js +2 -20
- package/dist/src/validator/functionSchema.js +55 -8
- package/dist/src/validator/rootSchema.js +3 -3
- package/dist/src/validator/semanticValidation.js +504 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/release-notes.md +82 -6
- package/samples/aliyun-poc-api-cdn.yml +1 -1
- package/samples/aliyun-poc-bucket-cdn.yml +28 -26
- package/samples/aliyun-poc-domain.yml +2 -1
- package/samples/volcengine-poc-tos-state.yml +36 -0
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.cloudOssToDefinition = exports.extractOssBucketDefinition = exports.bucketToOssBucketConfig = exports.OSS_BUCKET_POLICY_INSTANCE_TYPE = void 0;
|
|
4
4
|
const bucketPolicy_1 = require("../../common/bucketPolicy");
|
|
5
5
|
const types_1 = require("../../types");
|
|
6
|
+
const bindingReconcile_1 = require("../../common/bindingReconcile");
|
|
6
7
|
const bucketTypes_1 = require("../bucketTypes");
|
|
7
8
|
exports.OSS_BUCKET_POLICY_INSTANCE_TYPE = 'ALIYUN_OSS_BUCKET_POLICY';
|
|
8
9
|
const aclMap = {
|
|
@@ -10,11 +11,6 @@ const aclMap = {
|
|
|
10
11
|
[types_1.BucketAccessEnum.PUBLIC_READ]: bucketTypes_1.BucketACL.PUBLIC_READ,
|
|
11
12
|
[types_1.BucketAccessEnum.PUBLIC_READ_WRITE]: bucketTypes_1.BucketACL.PUBLIC_READ_WRITE,
|
|
12
13
|
};
|
|
13
|
-
const getDomain = (bucket) => {
|
|
14
|
-
if (bucket.domain?.domain_name)
|
|
15
|
-
return bucket.domain.domain_name;
|
|
16
|
-
return bucket.website?.domain;
|
|
17
|
-
};
|
|
18
14
|
/* istanbul ignore next */
|
|
19
15
|
const bucketToOssBucketConfig = (bucket) => {
|
|
20
16
|
const config = {
|
|
@@ -35,39 +31,17 @@ const bucketToOssBucketConfig = (bucket) => {
|
|
|
35
31
|
if (bucket.storage?.class) {
|
|
36
32
|
config.storageClass = bucket.storage.class;
|
|
37
33
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
config.domain = domain;
|
|
34
|
+
if (bucket.accelerate != null) {
|
|
35
|
+
config.accelerateEnabled = bucket.accelerate;
|
|
41
36
|
}
|
|
42
|
-
if (bucket.
|
|
43
|
-
config.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (typeof bucket.domain.cdn === 'boolean') {
|
|
51
|
-
config.cdnEnabled = bucket.domain.cdn;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
config.cdnEnabled = bucket.domain.cdn.enabled;
|
|
55
|
-
config.cdnType = bucket.domain.cdn.cdn_type;
|
|
56
|
-
config.cdnScope = bucket.domain.cdn.scope;
|
|
57
|
-
config.cdnCacheTtl = bucket.domain.cdn.cache_ttl;
|
|
58
|
-
config.cdnIgnoreQueryString = bucket.domain.cdn.ignore_query_string;
|
|
59
|
-
config.cdnOriginProtocol = bucket.domain.cdn.origin_protocol;
|
|
60
|
-
config.cdnCompression = bucket.domain.cdn.compression;
|
|
61
|
-
config.cdnForceRedirectHttps = bucket.domain.cdn.force_redirect_https;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
else if (bucket.website?.domain) {
|
|
66
|
-
config.wwwBindApex = bucket.website.www_bind_apex;
|
|
67
|
-
config.domainCertificateId = bucket.website.domain_certificate_id;
|
|
68
|
-
config.domainCertificateBody = bucket.website.domain_certificate_body;
|
|
69
|
-
config.domainCertificatePrivateKey = bucket.website.domain_certificate_private_key;
|
|
70
|
-
config.domainProtocol = bucket.website.domain_protocol;
|
|
37
|
+
if (bucket.domains?.length) {
|
|
38
|
+
config.domains = bucket.domains.map((binding) => ({
|
|
39
|
+
domainName: binding.domain_name,
|
|
40
|
+
certificateId: binding.certificate_id,
|
|
41
|
+
certificateBody: binding.certificate_body,
|
|
42
|
+
certificatePrivateKey: binding.certificate_private_key,
|
|
43
|
+
...(binding.cdn != null ? { cdn: binding.cdn } : {}),
|
|
44
|
+
}));
|
|
71
45
|
}
|
|
72
46
|
if (bucket.versioning?.status) {
|
|
73
47
|
config.versioningStatus = bucket.versioning.status;
|
|
@@ -94,38 +68,18 @@ const extractOssBucketDefinition = (config, websiteCodeHash) => {
|
|
|
94
68
|
: {},
|
|
95
69
|
websiteCodeHash: websiteCodeHash ?? null,
|
|
96
70
|
storageClass: config.storageClass ?? null,
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
71
|
+
// Bindings are canonicalized: sorted by normalized hostname so list
|
|
72
|
+
// reordering in YAML never produces a plan change (issue #279).
|
|
73
|
+
domains: (config.domains ?? [])
|
|
74
|
+
.slice()
|
|
75
|
+
.sort((a, b) => a.domainName.localeCompare(b.domainName))
|
|
76
|
+
.map(bindingReconcile_1.bindingToDefinitionEntry),
|
|
77
|
+
accelerate: config.accelerateEnabled ?? false,
|
|
103
78
|
versioningStatus: config.versioningStatus ?? null,
|
|
104
79
|
sseAlgorithm: config.sseAlgorithm ?? null,
|
|
105
80
|
sseKmsMasterKeyId: config.sseKmsMasterKeyId ?? null,
|
|
106
81
|
policy: (0, bucketPolicy_1.serializeBucketPolicy)(config.iam, 'aliyun'),
|
|
107
82
|
};
|
|
108
|
-
// Only include CDN/accelerate fields when they're actually enabled
|
|
109
|
-
if (config.cdnEnabled !== undefined) {
|
|
110
|
-
def.cdnEnabled = config.cdnEnabled;
|
|
111
|
-
if (config.cdnEnabled)
|
|
112
|
-
def.cdnType = config.cdnType ?? 'web';
|
|
113
|
-
if (config.cdnEnabled)
|
|
114
|
-
def.cdnScope = config.cdnScope ?? 'global';
|
|
115
|
-
if (config.cdnCacheTtl != null)
|
|
116
|
-
def.cdnCacheTtl = config.cdnCacheTtl;
|
|
117
|
-
if (config.cdnIgnoreQueryString != null)
|
|
118
|
-
def.cdnIgnoreQueryString = config.cdnIgnoreQueryString;
|
|
119
|
-
if (config.cdnOriginProtocol)
|
|
120
|
-
def.cdnOriginProtocol = config.cdnOriginProtocol;
|
|
121
|
-
if (config.cdnCompression != null)
|
|
122
|
-
def.cdnCompression = config.cdnCompression;
|
|
123
|
-
if (config.cdnForceRedirectHttps != null)
|
|
124
|
-
def.cdnForceRedirectHttps = config.cdnForceRedirectHttps;
|
|
125
|
-
}
|
|
126
|
-
if (config.accelerateEnabled !== undefined) {
|
|
127
|
-
def.accelerateEnabled = config.accelerateEnabled;
|
|
128
|
-
}
|
|
129
83
|
return def;
|
|
130
84
|
};
|
|
131
85
|
exports.extractOssBucketDefinition = extractOssBucketDefinition;
|
|
@@ -151,8 +105,8 @@ const cloudOssToDefinition = (info) => ({
|
|
|
151
105
|
? undefined
|
|
152
106
|
: (info.encryptionConfig?.kmsMasterKeyId ?? null),
|
|
153
107
|
policy: info.policy,
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
108
|
+
// accelerate is intentionally not mapped from the cloud: transfer
|
|
109
|
+
// acceleration cannot be disabled through update (destroy+recreate is
|
|
110
|
+
// required), so a live-vs-desired diff here would be unresolvable drift.
|
|
157
111
|
});
|
|
158
112
|
exports.cloudOssToDefinition = cloudOssToDefinition;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.logApiGatewayRequest = exports.transformFCResponse = exports.addFCHeaders = exports.createAliyunContext = exports.createAliyunContextSerializable = exports.transformToAliyunEvent = void 0;
|
|
3
|
+
exports.logApiGatewayRequest = exports.transformFCResponse = exports.addFCHeaders = exports.createAliyunContext = exports.createAliyunContextSerializable = exports.aliyunHttpAdapter = exports.transformToAliyunEvent = exports.buildLocalGatewayHost = void 0;
|
|
4
4
|
const common_1 = require("../../common");
|
|
5
|
+
const localStack_1 = require("../../types/localStack");
|
|
5
6
|
const createFCLogger = (requestId) => {
|
|
6
7
|
const formatLog = (level, message) => {
|
|
7
8
|
const timestamp = new Date().toISOString();
|
|
@@ -15,22 +16,71 @@ const createFCLogger = (requestId) => {
|
|
|
15
16
|
log: (message) => formatLog('INFO', message),
|
|
16
17
|
};
|
|
17
18
|
};
|
|
18
|
-
const
|
|
19
|
+
const buildLocalGatewayHost = (iac, stage) => `${iac.app}-${stage}.si-local.gateway`;
|
|
20
|
+
exports.buildLocalGatewayHost = buildLocalGatewayHost;
|
|
21
|
+
const GATEWAY_REBUILT_HEADERS = new Set([
|
|
22
|
+
'connection',
|
|
23
|
+
'keep-alive',
|
|
24
|
+
'proxy-authenticate',
|
|
25
|
+
'proxy-authorization',
|
|
26
|
+
'trailer',
|
|
27
|
+
'te',
|
|
28
|
+
'transfer-encoding',
|
|
29
|
+
'upgrade',
|
|
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
|
+
const buildGatewayRequestHeaders = (req, iac, stage, requestId) => {
|
|
36
|
+
const forwarded = Object.fromEntries(Object.entries(req.headers)
|
|
37
|
+
.filter(([key]) => !GATEWAY_REBUILT_HEADERS.has(key) &&
|
|
38
|
+
!key.startsWith('x-ca-') &&
|
|
39
|
+
key !== 'host' &&
|
|
40
|
+
key !== 'x-forwarded-for')
|
|
41
|
+
.map(([key, value]) => [key, Array.isArray(value) ? value.join(', ') : (value ?? '')]));
|
|
42
|
+
const clientXff = req.headers['x-forwarded-for'];
|
|
43
|
+
const xff = [
|
|
44
|
+
...(Array.isArray(clientXff) ? clientXff : clientXff ? [clientXff] : []),
|
|
45
|
+
remoteAddress(req),
|
|
46
|
+
].join(', ');
|
|
47
|
+
return {
|
|
48
|
+
...forwarded,
|
|
49
|
+
host: (0, exports.buildLocalGatewayHost)(iac, stage),
|
|
50
|
+
'x-forwarded-for': xff,
|
|
51
|
+
'x-forwarded-proto': 'http',
|
|
52
|
+
via: '1.1 si-local-api-gateway',
|
|
53
|
+
'x-ca-request-id': requestId,
|
|
54
|
+
'x-ca-stage': stage,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
const transformToAliyunEvent = async (req, parsed, iac, requestId) => {
|
|
19
58
|
const rawBody = await (0, common_1.readRequestBody)(req);
|
|
20
|
-
const
|
|
59
|
+
const stage = parsed.stage ?? 'default';
|
|
21
60
|
const event = {
|
|
22
|
-
path: url,
|
|
61
|
+
path: parsed.url,
|
|
23
62
|
httpMethod: req.method || 'GET',
|
|
24
|
-
headers: req
|
|
25
|
-
queryParameters: query,
|
|
26
|
-
pathParameters,
|
|
63
|
+
headers: buildGatewayRequestHeaders(req, iac, stage, requestId),
|
|
64
|
+
queryParameters: parsed.query,
|
|
65
|
+
pathParameters: parsed.pathParameters ?? {},
|
|
27
66
|
body: rawBody || undefined,
|
|
28
67
|
isBase64Encoded: false,
|
|
68
|
+
requestContext: { stage, requestId },
|
|
29
69
|
};
|
|
30
|
-
|
|
31
|
-
return { event: eventBuffer, headers: req.headers };
|
|
70
|
+
return Buffer.from(JSON.stringify(event));
|
|
32
71
|
};
|
|
33
72
|
exports.transformToAliyunEvent = transformToAliyunEvent;
|
|
73
|
+
exports.aliyunHttpAdapter = {
|
|
74
|
+
validate: () => undefined,
|
|
75
|
+
buildInvocation: async (req, parsed, iac, fn, requestId) => ({
|
|
76
|
+
event: await (0, exports.transformToAliyunEvent)(req, parsed, iac, requestId),
|
|
77
|
+
context: (0, exports.createAliyunContextSerializable)(iac, fn.name, fn.code.handler, fn.memory ?? 128, fn.timeout ?? 3, requestId),
|
|
78
|
+
env: { ...fn.environment },
|
|
79
|
+
}),
|
|
80
|
+
transformResponse: (result, kind) => result || kind === localStack_1.RouteKind.SI_EVENTS
|
|
81
|
+
? (0, exports.transformFCResponse)(result)
|
|
82
|
+
: { statusCode: 200, body: result },
|
|
83
|
+
};
|
|
34
84
|
const createAliyunContextSerializable = (iac, functionName, handler, memory, timeout, requestId) => {
|
|
35
85
|
return {
|
|
36
86
|
requestId,
|
|
@@ -90,6 +90,76 @@ const getAllFiles = (dirPath, bucketPath, fileList = []) => {
|
|
|
90
90
|
return fileList;
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
|
+
const serveFileResponse = (filePath) => {
|
|
94
|
+
try {
|
|
95
|
+
const fileContent = node_fs_1.default.readFileSync(filePath);
|
|
96
|
+
const mimeType = getMimeType(filePath);
|
|
97
|
+
// For text files, return as string; for binary files, return as base64
|
|
98
|
+
const isTextFile = TEXT_MIME_TYPES.has(mimeType);
|
|
99
|
+
return {
|
|
100
|
+
statusCode: 200,
|
|
101
|
+
headers: {
|
|
102
|
+
'Content-Type': mimeType,
|
|
103
|
+
'Content-Length': String(fileContent.length),
|
|
104
|
+
},
|
|
105
|
+
body: isTextFile ? fileContent.toString('utf-8') : fileContent.toString('base64'),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
common_1.logger.error(lang_1.lang.__('ERROR_READING_FILE', { error: String(error) }));
|
|
110
|
+
return {
|
|
111
|
+
statusCode: 500,
|
|
112
|
+
body: {
|
|
113
|
+
error: 'Failed to read file',
|
|
114
|
+
message: error instanceof Error ? error.message : String(error),
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const isPathInsideBucket = (filePath, bucketBasePath) => node_path_1.default.normalize(filePath).startsWith(node_path_1.default.normalize(bucketBasePath));
|
|
120
|
+
const serveWebsiteRequest = (url, bucketBasePath, bucketKey, indexDocument) => {
|
|
121
|
+
const requestedPath = url.startsWith('/') ? url.slice(1) : url;
|
|
122
|
+
const filePath = node_path_1.default.join(bucketBasePath, url);
|
|
123
|
+
if (!isPathInsideBucket(filePath, bucketBasePath)) {
|
|
124
|
+
return {
|
|
125
|
+
statusCode: 403,
|
|
126
|
+
body: { error: 'Access denied: Path traversal attempt detected' },
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const serveIndexDocument = (indexPath) => {
|
|
130
|
+
if (node_fs_1.default.existsSync(indexPath) && node_fs_1.default.statSync(indexPath).isFile()) {
|
|
131
|
+
return serveFileResponse(indexPath);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
statusCode: 404,
|
|
135
|
+
body: {
|
|
136
|
+
error: lang_1.lang.__('LOCAL_BUCKET_INDEX_NOT_FOUND'),
|
|
137
|
+
index: indexDocument,
|
|
138
|
+
bucketKey,
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
if (node_fs_1.default.existsSync(filePath)) {
|
|
143
|
+
if (node_fs_1.default.statSync(filePath).isFile()) {
|
|
144
|
+
return serveFileResponse(filePath);
|
|
145
|
+
}
|
|
146
|
+
const directoryIndex = node_path_1.default.join(filePath, indexDocument);
|
|
147
|
+
if (node_fs_1.default.existsSync(directoryIndex) && node_fs_1.default.statSync(directoryIndex).isFile()) {
|
|
148
|
+
return serveFileResponse(directoryIndex);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (node_path_1.default.extname(requestedPath) !== '') {
|
|
152
|
+
return {
|
|
153
|
+
statusCode: 404,
|
|
154
|
+
body: {
|
|
155
|
+
error: lang_1.lang.__('LOCAL_BUCKET_FILE_NOT_FOUND'),
|
|
156
|
+
path: requestedPath,
|
|
157
|
+
bucketKey,
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return serveIndexDocument(node_path_1.default.join(bucketBasePath, indexDocument));
|
|
162
|
+
};
|
|
93
163
|
const bucketsHandler = async (req, parsed, iac) => {
|
|
94
164
|
common_1.logger.info(lang_1.lang.__('BUCKET_REQUEST_RECEIVED', {
|
|
95
165
|
method: req.method || '',
|
|
@@ -125,6 +195,9 @@ const bucketsHandler = async (req, parsed, iac) => {
|
|
|
125
195
|
},
|
|
126
196
|
};
|
|
127
197
|
}
|
|
198
|
+
if (bucketDef.website?.code) {
|
|
199
|
+
return serveWebsiteRequest(parsed.url, bucketBasePath, bucketDef.key, bucketDef.website.index ?? 'index.html');
|
|
200
|
+
}
|
|
128
201
|
// Root path lists all files in bucket
|
|
129
202
|
if (parsed.url === '/') {
|
|
130
203
|
try {
|
|
@@ -155,9 +228,7 @@ const bucketsHandler = async (req, parsed, iac) => {
|
|
|
155
228
|
const requestedPath = parsed.url.startsWith('/') ? parsed.url.slice(1) : parsed.url;
|
|
156
229
|
const filePath = node_path_1.default.join(bucketBasePath, requestedPath);
|
|
157
230
|
// Security check: ensure the requested file is within the bucket directory
|
|
158
|
-
|
|
159
|
-
const normalizedBucketPath = node_path_1.default.normalize(bucketBasePath);
|
|
160
|
-
if (!normalizedFilePath.startsWith(normalizedBucketPath)) {
|
|
231
|
+
if (!isPathInsideBucket(filePath, bucketBasePath)) {
|
|
161
232
|
return {
|
|
162
233
|
statusCode: 403,
|
|
163
234
|
body: { error: 'Access denied: Path traversal attempt detected' },
|
|
@@ -202,30 +273,6 @@ const bucketsHandler = async (req, parsed, iac) => {
|
|
|
202
273
|
};
|
|
203
274
|
}
|
|
204
275
|
}
|
|
205
|
-
|
|
206
|
-
try {
|
|
207
|
-
const fileContent = node_fs_1.default.readFileSync(filePath);
|
|
208
|
-
const mimeType = getMimeType(filePath);
|
|
209
|
-
// For text files, return as string; for binary files, return as base64
|
|
210
|
-
const isTextFile = TEXT_MIME_TYPES.has(mimeType);
|
|
211
|
-
return {
|
|
212
|
-
statusCode: 200,
|
|
213
|
-
headers: {
|
|
214
|
-
'Content-Type': mimeType,
|
|
215
|
-
'Content-Length': String(fileContent.length),
|
|
216
|
-
},
|
|
217
|
-
body: isTextFile ? fileContent.toString('utf-8') : fileContent.toString('base64'),
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
catch (error) {
|
|
221
|
-
common_1.logger.error(lang_1.lang.__('ERROR_READING_FILE', { error: String(error) }));
|
|
222
|
-
return {
|
|
223
|
-
statusCode: 500,
|
|
224
|
-
body: {
|
|
225
|
-
error: 'Failed to read file',
|
|
226
|
-
message: error instanceof Error ? error.message : String(error),
|
|
227
|
-
},
|
|
228
|
-
};
|
|
229
|
-
}
|
|
276
|
+
return serveFileResponse(filePath);
|
|
230
277
|
};
|
|
231
278
|
exports.bucketsHandler = bucketsHandler;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.eventsHandler = void 0;
|
|
3
|
+
exports.eventsHandler = exports.matchTriggerWithParams = void 0;
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
5
|
const lodash_1 = require("lodash");
|
|
6
6
|
const localStack_1 = require("../../types/localStack");
|
|
@@ -9,41 +9,40 @@ const function_1 = require("./function");
|
|
|
9
9
|
const aliyunFc_1 = require("./aliyunFc");
|
|
10
10
|
const requestId_1 = require("./requestId");
|
|
11
11
|
const lang_1 = require("../../lang");
|
|
12
|
-
|
|
13
|
-
* Route matching shared by every provider; the `exact` mode mirrors how the
|
|
14
|
-
* provider actually deploys the route:
|
|
15
|
-
* - Aliyun: gateway routes support `[param]` segments and a trailing `*`
|
|
16
|
-
* prefix wildcard, so paths are compared segment-wise after trimming
|
|
17
|
-
* slashes.
|
|
18
|
-
* - Volcengine: si creates API Gateway routes with `MatchType: 'Exact'`
|
|
19
|
-
* (see `createRoute` in volcengineClient/apigwOperations.ts), so the path
|
|
20
|
-
* is compared byte-for-byte and `[param]` / `*` carry no wildcard meaning.
|
|
21
|
-
*/
|
|
22
|
-
const matchTrigger = (req, trigger, options = { exact: false }) => {
|
|
12
|
+
const matchTriggerWithParams = (req, trigger, options = { exact: false }) => {
|
|
23
13
|
if (trigger.method !== 'ANY' && req.method !== trigger.method) {
|
|
24
|
-
return false;
|
|
14
|
+
return { matched: false, params: {} };
|
|
25
15
|
}
|
|
26
16
|
if (options.exact) {
|
|
27
|
-
return trigger.path === req.path;
|
|
17
|
+
return { matched: trigger.path === req.path, params: {} };
|
|
28
18
|
}
|
|
29
19
|
const normalize = (s) => s.replace(/^\/+|\/+$/g, '');
|
|
30
20
|
const [pathSegments, triggerSegments] = [
|
|
31
21
|
normalize(req.path).split('/'),
|
|
32
22
|
normalize(trigger.path).split('/'),
|
|
33
23
|
];
|
|
24
|
+
if (triggerSegments.length === 1 && triggerSegments[0] === '') {
|
|
25
|
+
return { matched: true, params: {} };
|
|
26
|
+
}
|
|
34
27
|
const hasWildcard = triggerSegments[triggerSegments.length - 1] === '*';
|
|
35
28
|
const prefixSegments = hasWildcard ? triggerSegments.slice(0, -1) : triggerSegments;
|
|
36
29
|
const minRequiredSegments = prefixSegments.length;
|
|
37
30
|
if (pathSegments.length < minRequiredSegments)
|
|
38
|
-
return false;
|
|
39
|
-
|
|
31
|
+
return { matched: false, params: {} };
|
|
32
|
+
const params = {};
|
|
33
|
+
const matched = prefixSegments.every((triggerSegment, index) => {
|
|
40
34
|
const pathSegment = pathSegments[index];
|
|
41
35
|
if (triggerSegment.startsWith('[') && triggerSegment.endsWith(']')) {
|
|
42
|
-
|
|
36
|
+
if (pathSegment === '')
|
|
37
|
+
return false;
|
|
38
|
+
params[triggerSegment.slice(1, -1)] = pathSegment;
|
|
39
|
+
return true;
|
|
43
40
|
}
|
|
44
41
|
return triggerSegment === pathSegment;
|
|
45
42
|
});
|
|
43
|
+
return matched ? { matched: true, params } : { matched: false, params: {} };
|
|
46
44
|
};
|
|
45
|
+
exports.matchTriggerWithParams = matchTriggerWithParams;
|
|
47
46
|
const servEvent = async (req, parsed, iac) => {
|
|
48
47
|
const startTime = new Date();
|
|
49
48
|
const requestId = (0, requestId_1.generateProviderRequestId)(iac.provider.name);
|
|
@@ -70,9 +69,14 @@ const servEvent = async (req, parsed, iac) => {
|
|
|
70
69
|
method: req.method || '',
|
|
71
70
|
url: req.url || '',
|
|
72
71
|
}));
|
|
73
|
-
const matchedTrigger = event.triggers
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
const matchedTrigger = event.triggers
|
|
73
|
+
.map((trigger) => ({
|
|
74
|
+
trigger,
|
|
75
|
+
match: (0, exports.matchTriggerWithParams)({ method: parsed.method, path: parsed.url }, trigger, {
|
|
76
|
+
exact: iac.provider.name === common_1.ProviderEnum.VOLCENGINE,
|
|
77
|
+
}),
|
|
78
|
+
}))
|
|
79
|
+
.find((entry) => entry.match.matched);
|
|
76
80
|
if (!matchedTrigger) {
|
|
77
81
|
const endTime = new Date();
|
|
78
82
|
if (isAliyun) {
|
|
@@ -80,8 +84,8 @@ const servEvent = async (req, parsed, iac) => {
|
|
|
80
84
|
}
|
|
81
85
|
return { statusCode: 404, body: { error: 'No matching trigger found' } };
|
|
82
86
|
}
|
|
83
|
-
if (matchedTrigger.backend) {
|
|
84
|
-
const backendDef = (0, common_1.getIacDefinition)(iac, matchedTrigger.backend);
|
|
87
|
+
if (matchedTrigger.trigger.backend) {
|
|
88
|
+
const backendDef = (0, common_1.getIacDefinition)(iac, matchedTrigger.trigger.backend);
|
|
85
89
|
if (!backendDef || !(0, common_1.isFunctionDomain)(backendDef)) {
|
|
86
90
|
const endTime = new Date();
|
|
87
91
|
if (isAliyun) {
|
|
@@ -89,10 +93,15 @@ const servEvent = async (req, parsed, iac) => {
|
|
|
89
93
|
}
|
|
90
94
|
return {
|
|
91
95
|
statusCode: 500,
|
|
92
|
-
body: { error: 'Backend definition missing', backend: matchedTrigger.backend },
|
|
96
|
+
body: { error: 'Backend definition missing', backend: matchedTrigger.trigger.backend },
|
|
93
97
|
};
|
|
94
98
|
}
|
|
95
|
-
const result = await (0, function_1.functionsHandler)(req, {
|
|
99
|
+
const result = await (0, function_1.functionsHandler)(req, {
|
|
100
|
+
...parsed,
|
|
101
|
+
kind: localStack_1.RouteKind.SI_EVENTS,
|
|
102
|
+
identifier: backendDef.key,
|
|
103
|
+
pathParameters: matchedTrigger.match.params,
|
|
104
|
+
}, iac, requestId);
|
|
96
105
|
const endTime = new Date();
|
|
97
106
|
if (isAliyun) {
|
|
98
107
|
(0, aliyunFc_1.logApiGatewayRequest)(requestId, parsed.url, result.statusCode, startTime, endTime, sourceIp);
|
|
@@ -3,23 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getLocalHttpAdapter = void 0;
|
|
4
4
|
const common_1 = require("../../common");
|
|
5
5
|
const lang_1 = require("../../lang");
|
|
6
|
-
const localStack_1 = require("../../types/localStack");
|
|
7
6
|
const aliyunFc_1 = require("./aliyunFc");
|
|
8
7
|
const legacyLambda_1 = require("./legacyLambda");
|
|
9
8
|
const tencent_1 = require("./tencent");
|
|
10
9
|
const volcengine_1 = require("./volcengine");
|
|
11
10
|
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
11
|
/**
|
|
24
12
|
* Generic execution for providers without a native local HTTP simulation.
|
|
25
13
|
* The request body is treated as an already-formed event object; this is a
|
|
@@ -38,7 +26,7 @@ const genericHttpAdapter = {
|
|
|
38
26
|
transformResponse: (result) => ({ statusCode: 200, body: result }),
|
|
39
27
|
};
|
|
40
28
|
const httpAdapters = {
|
|
41
|
-
[common_1.ProviderEnum.ALIYUN]: aliyunHttpAdapter,
|
|
29
|
+
[common_1.ProviderEnum.ALIYUN]: aliyunFc_1.aliyunHttpAdapter,
|
|
42
30
|
[common_1.ProviderEnum.VOLCENGINE]: volcengine_1.volcengineHttpAdapter,
|
|
43
31
|
[common_1.ProviderEnum.TENCENT]: tencent_1.scfHttpAdapter,
|
|
44
32
|
[common_1.ProviderEnum.AWS]: genericHttpAdapter,
|
|
@@ -27,8 +27,8 @@ const handlers = [
|
|
|
27
27
|
{ kind: localStack_1.RouteKind.SI_EVENTS, handler: event_1.eventsHandler },
|
|
28
28
|
{ kind: localStack_1.RouteKind.SI_BUCKETS, handler: bucket_1.bucketsHandler },
|
|
29
29
|
];
|
|
30
|
-
const startLocalStack = async (iac) => {
|
|
31
|
-
await (0, localServer_1.servLocal)(handlers, iac);
|
|
30
|
+
const startLocalStack = async (iac, options = {}) => {
|
|
31
|
+
await (0, localServer_1.servLocal)(handlers, iac, options);
|
|
32
32
|
// functions.*.triggers.timer fire locally too (issue #258)
|
|
33
33
|
(0, timer_1.startLocalTimers)(iac);
|
|
34
34
|
};
|
|
@@ -5,8 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.stopLocal = exports.servLocal = void 0;
|
|
7
7
|
const localStack_1 = require("../../types/localStack");
|
|
8
|
+
const types_1 = require("../../types");
|
|
8
9
|
const common_1 = require("../../common");
|
|
9
10
|
const localRoute_1 = require("./localRoute");
|
|
11
|
+
const event_1 = require("./event");
|
|
10
12
|
const node_http_1 = __importDefault(require("node:http"));
|
|
11
13
|
const lang_1 = require("../../lang");
|
|
12
14
|
let localServer;
|
|
@@ -22,7 +24,7 @@ const respondRaw = (res, status, body, headers = {}) => {
|
|
|
22
24
|
res.writeHead(status, headers);
|
|
23
25
|
res.end(body);
|
|
24
26
|
};
|
|
25
|
-
const parseRequest = (req, preservePath) => {
|
|
27
|
+
const parseRequest = (req, preservePath, stage) => {
|
|
26
28
|
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
27
29
|
const [routeSegment, identifierSegment, ...rest] = cleanPathSegments(url.pathname);
|
|
28
30
|
if (!routeSegment) {
|
|
@@ -45,26 +47,102 @@ const parseRequest = (req, preservePath) => {
|
|
|
45
47
|
method: req.method ?? 'GET',
|
|
46
48
|
query: Object.fromEntries(url.searchParams.entries()),
|
|
47
49
|
rawUrl: url.pathname,
|
|
50
|
+
stage,
|
|
48
51
|
};
|
|
49
52
|
};
|
|
50
|
-
const
|
|
53
|
+
const resolveGatewayRequest = (req, iac, stage) => {
|
|
54
|
+
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
55
|
+
const request = { method: req.method ?? 'GET', path: url.pathname };
|
|
56
|
+
const matchedEventKeys = (iac.events ?? [])
|
|
57
|
+
.filter((event) => event.type === types_1.EventTypes.API_GATEWAY)
|
|
58
|
+
.filter((event) => event.triggers.some((trigger) => (0, event_1.matchTriggerWithParams)(request, trigger, { exact: false }).matched))
|
|
59
|
+
.map((event) => event.key);
|
|
60
|
+
if (matchedEventKeys.length === 1) {
|
|
61
|
+
const cleaned = cleanPathSegments(url.pathname);
|
|
62
|
+
return {
|
|
63
|
+
kind: 'matched',
|
|
64
|
+
parsed: {
|
|
65
|
+
kind: localStack_1.RouteKind.SI_EVENTS,
|
|
66
|
+
identifier: matchedEventKeys[0],
|
|
67
|
+
url: cleaned.length > 0 ? `/${cleaned.join('/')}` : '/',
|
|
68
|
+
method: request.method,
|
|
69
|
+
query: Object.fromEntries(url.searchParams.entries()),
|
|
70
|
+
rawUrl: url.pathname,
|
|
71
|
+
stage,
|
|
72
|
+
pathParameters: {},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return matchedEventKeys.length > 1
|
|
77
|
+
? { kind: 'ambiguous', eventKeys: matchedEventKeys }
|
|
78
|
+
: { kind: 'none' };
|
|
79
|
+
};
|
|
80
|
+
const findAmbiguousGatewayTriggers = (iac) => Object.entries((iac.events ?? [])
|
|
81
|
+
.filter((event) => event.type === types_1.EventTypes.API_GATEWAY)
|
|
82
|
+
.flatMap((event) => event.triggers.map((trigger) => ({
|
|
83
|
+
eventKey: event.key,
|
|
84
|
+
method: String(trigger.method).toUpperCase(),
|
|
85
|
+
path: String(trigger.path),
|
|
86
|
+
})))
|
|
87
|
+
.reduce((groups, trigger) => {
|
|
88
|
+
const existing = groups[trigger.path] ?? [];
|
|
89
|
+
return {
|
|
90
|
+
...groups,
|
|
91
|
+
[trigger.path]: [...existing, { eventKey: trigger.eventKey, method: trigger.method }],
|
|
92
|
+
};
|
|
93
|
+
}, {}))
|
|
94
|
+
.map(([trigPath, declarations]) => {
|
|
95
|
+
const eventKeys = [...new Set(declarations.map((d) => d.eventKey))];
|
|
96
|
+
if (eventKeys.length < 2)
|
|
97
|
+
return undefined;
|
|
98
|
+
const methods = new Set(declarations.map((d) => d.method));
|
|
99
|
+
const compatible = methods.has('ANY') ||
|
|
100
|
+
declarations.every((d, index, all) => all.every((other) => other.method === d.method || other.method === 'ANY'));
|
|
101
|
+
return compatible ? `'${trigPath}' → ${eventKeys.join(', ')}` : undefined;
|
|
102
|
+
})
|
|
103
|
+
.filter((detail) => detail !== undefined);
|
|
104
|
+
const servLocal = async (handlers, iac, options = {}) => {
|
|
51
105
|
if (localServer) {
|
|
52
106
|
common_1.logger.info(lang_1.lang.__('LOCAL_SERVER_ALREADY_RUNNING', { port: String(common_1.SI_LOCALSTACK_SERVER_PORT) }));
|
|
53
107
|
return;
|
|
54
108
|
}
|
|
109
|
+
const directResolutionEnabled = iac.provider.name === common_1.ProviderEnum.ALIYUN && options.strictPrefix !== true;
|
|
110
|
+
if (directResolutionEnabled) {
|
|
111
|
+
common_1.logger.info(lang_1.lang.__('LOCAL_DIRECT_RESOLUTION_ENABLED'));
|
|
112
|
+
const ambiguous = findAmbiguousGatewayTriggers(iac);
|
|
113
|
+
if (ambiguous.length > 0) {
|
|
114
|
+
common_1.logger.warn(lang_1.lang.__('LOCAL_GATEWAY_AMBIGUOUS_TRIGGERS', { details: ambiguous.join('; ') }));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
55
117
|
localServer = node_http_1.default.createServer(async (req, res) => {
|
|
56
118
|
try {
|
|
57
|
-
const parsed = parseRequest(req, (0, localRoute_1.preservesLocalPath)(iac.provider.name));
|
|
58
|
-
|
|
119
|
+
const parsed = parseRequest(req, (0, localRoute_1.preservesLocalPath)(iac.provider.name), options.stage);
|
|
120
|
+
let dispatched = parsed;
|
|
121
|
+
if (!dispatched && directResolutionEnabled) {
|
|
122
|
+
const direct = resolveGatewayRequest(req, iac, options.stage);
|
|
123
|
+
if (direct.kind === 'matched') {
|
|
124
|
+
dispatched = direct.parsed;
|
|
125
|
+
}
|
|
126
|
+
else if (direct.kind === 'ambiguous') {
|
|
127
|
+
respondJson(res, 404, {
|
|
128
|
+
error: 'Route not found',
|
|
129
|
+
hint: lang_1.lang.__('LOCAL_GATEWAY_AMBIGUOUS_TRIGGERS', {
|
|
130
|
+
details: `'${new URL(req.url ?? '/', 'http://localhost').pathname}' → ${direct.eventKeys.join(', ')}`,
|
|
131
|
+
}),
|
|
132
|
+
});
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (!dispatched) {
|
|
59
137
|
respondJson(res, 404, { error: 'Route not found' });
|
|
60
138
|
return;
|
|
61
139
|
}
|
|
62
|
-
const route = handlers.find((h) => h.kind ===
|
|
140
|
+
const route = handlers.find((h) => h.kind === dispatched.kind);
|
|
63
141
|
if (!route) {
|
|
64
|
-
respondJson(res, 404, { error: `Handler for ${
|
|
142
|
+
respondJson(res, 404, { error: `Handler for ${dispatched.kind} not registered` });
|
|
65
143
|
return;
|
|
66
144
|
}
|
|
67
|
-
const outcome = await route.handler(req,
|
|
145
|
+
const outcome = await route.handler(req, dispatched, iac);
|
|
68
146
|
if (!outcome) {
|
|
69
147
|
respondJson(res, 204, {});
|
|
70
148
|
return;
|
|
@@ -49,9 +49,17 @@ const generateBucketPlan = async (context, state, buckets) => {
|
|
|
49
49
|
cloudToDefinition: cosTypes_1.cloudCosToDefinition,
|
|
50
50
|
removedDependents: {
|
|
51
51
|
...(0, bucketPolicy_1.removedBucketPolicy)(cosTypes_1.COS_BUCKET_POLICY_INSTANCE_TYPE, currentState, desiredDefinition),
|
|
52
|
-
...(!config.
|
|
53
|
-
(currentState?.instances ?? []).some((i) => i.type === types_1.ResourceTypeEnum.COS_DNS_CNAME
|
|
54
|
-
|
|
52
|
+
...(!(config.domains?.length ?? 0) &&
|
|
53
|
+
(currentState?.instances ?? []).some((i) => i.type === types_1.ResourceTypeEnum.COS_DNS_CNAME ||
|
|
54
|
+
i.type === types_1.ResourceTypeEnum.TENCENT_CDN_DOMAIN ||
|
|
55
|
+
i.type === types_1.ResourceTypeEnum.TENCENT_CDN_DNS_CNAME)
|
|
56
|
+
? {
|
|
57
|
+
domains: currentState?.definition.domains ?? null,
|
|
58
|
+
// Legacy single-domain state records the binding under `domain`.
|
|
59
|
+
...(currentState?.definition.domain != null
|
|
60
|
+
? { domain: currentState.definition.domain }
|
|
61
|
+
: {}),
|
|
62
|
+
}
|
|
55
63
|
: {}),
|
|
56
64
|
},
|
|
57
65
|
refresh: context.refresh,
|