@geek-fun/serverlessinsight 0.6.15 → 0.7.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.
Files changed (71) hide show
  1. package/AGENTS.md +405 -10
  2. package/README.md +59 -1
  3. package/README.zh-CN.md +3 -1
  4. package/dist/package.json +42 -34
  5. package/dist/src/commands/deploy.js +4 -0
  6. package/dist/src/commands/destroy.js +4 -0
  7. package/dist/src/common/aliyunClient/apigwOperations.js +9 -4
  8. package/dist/src/common/aliyunClient/cdnOperations.js +191 -0
  9. package/dist/src/common/aliyunClient/ecsOperations.js +2 -2
  10. package/dist/src/common/aliyunClient/fc3Operations.js +1 -1
  11. package/dist/src/common/aliyunClient/index.js +8 -0
  12. package/dist/src/common/aliyunClient/ossOperations.js +62 -2
  13. package/dist/src/common/aliyunClient/ramOperations.js +1 -1
  14. package/dist/src/common/credentials.js +12 -0
  15. package/dist/src/common/hashUtils.js +4 -0
  16. package/dist/src/common/iacHelper.js +5 -0
  17. package/dist/src/common/lockManager.js +8 -0
  18. package/dist/src/common/planFormatter.js +4 -0
  19. package/dist/src/common/providerEnum.js +1 -0
  20. package/dist/src/common/retryUtils.js +1 -1
  21. package/dist/src/common/runtimeMapper.js +24 -3
  22. package/dist/src/common/stateManager.js +10 -0
  23. package/dist/src/common/volcengineClient/apigwOperations.js +271 -0
  24. package/dist/src/common/volcengineClient/iamOperations.js +349 -0
  25. package/dist/src/common/volcengineClient/index.js +99 -0
  26. package/dist/src/common/volcengineClient/tlsOperations.js +256 -0
  27. package/dist/src/common/volcengineClient/tosOperations.js +440 -0
  28. package/dist/src/common/volcengineClient/types.js +26 -0
  29. package/dist/src/common/volcengineClient/vefaasOperations.js +386 -0
  30. package/dist/src/lang/en.js +135 -0
  31. package/dist/src/lang/zh-CN.js +134 -0
  32. package/dist/src/parser/bucketParser.js +111 -46
  33. package/dist/src/stack/aliyunStack/apigwResource.js +310 -47
  34. package/dist/src/stack/aliyunStack/apigwTypes.js +61 -1
  35. package/dist/src/stack/aliyunStack/ossResource.js +401 -69
  36. package/dist/src/stack/aliyunStack/ossTypes.js +58 -16
  37. package/dist/src/stack/deploy.js +4 -0
  38. package/dist/src/stack/scfStack/cosTypes.js +3 -6
  39. package/dist/src/stack/volcengineStack/apigwExecutor.js +87 -0
  40. package/dist/src/stack/volcengineStack/apigwPlanner.js +110 -0
  41. package/dist/src/stack/volcengineStack/apigwResource.js +302 -0
  42. package/dist/src/stack/volcengineStack/apigwTypes.js +106 -0
  43. package/dist/src/stack/volcengineStack/deployer.js +59 -0
  44. package/dist/src/stack/volcengineStack/destroyer.js +72 -0
  45. package/dist/src/stack/volcengineStack/index.js +44 -0
  46. package/dist/src/stack/volcengineStack/planner.js +27 -0
  47. package/dist/src/stack/volcengineStack/tosExecutor.js +106 -0
  48. package/dist/src/stack/volcengineStack/tosPlanner.js +96 -0
  49. package/dist/src/stack/volcengineStack/tosResource.js +103 -0
  50. package/dist/src/stack/volcengineStack/tosTypes.js +63 -0
  51. package/dist/src/stack/volcengineStack/vefaasExecutor.js +102 -0
  52. package/dist/src/stack/volcengineStack/vefaasPlanner.js +84 -0
  53. package/dist/src/stack/volcengineStack/vefaasResource.js +513 -0
  54. package/dist/src/stack/volcengineStack/vefaasTypes.js +75 -0
  55. package/dist/src/types/domains/state.js +6 -0
  56. package/dist/src/validator/bucketSchema.js +124 -56
  57. package/dist/src/validator/eventSchema.js +20 -0
  58. package/dist/src/validator/functionSchema.js +13 -0
  59. package/dist/src/validator/rootSchema.js +1 -1
  60. package/dist/tsconfig.tsbuildinfo +1 -1
  61. package/package.json +42 -34
  62. package/samples/README_TENCENT_COS.md +3 -15
  63. package/samples/aliyun-poc-bucket-cdn.yml +63 -0
  64. package/samples/aliyun-poc-bucket.yml +0 -2
  65. package/samples/aliyun-poc-domain.yml +0 -2
  66. package/samples/tencent-poc-cos.yml +0 -2
  67. package/samples/volcengine-poc-advanced.yml +59 -0
  68. package/samples/volcengine-poc-api.yml +31 -0
  69. package/samples/volcengine-poc-bucket.yml +16 -0
  70. package/samples/volcengine-poc-function.yml +19 -0
  71. package/samples/volcengine-poc-vpc.yml +34 -0
@@ -0,0 +1,256 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createTlsOperations = void 0;
4
+ const logger_1 = require("../logger");
5
+ const lang_1 = require("../../lang");
6
+ const WAIT_INTERVAL_MS = 5000;
7
+ const MAX_WAIT_ATTEMPTS = 30;
8
+ const delay = (ms) => new Promise((resolve) => {
9
+ setTimeout(resolve, ms);
10
+ });
11
+ const createTlsOperations = (tlsClient) => {
12
+ return {
13
+ createProject: async (config) => {
14
+ const response = await tlsClient.fetchOpenAPI({
15
+ Action: 'CreateProject',
16
+ Version: '2024-01-01',
17
+ method: 'POST',
18
+ headers: { 'content-type': 'application/json' },
19
+ data: {
20
+ ProjectName: config.projectName,
21
+ Description: config.description,
22
+ Region: config.region,
23
+ },
24
+ });
25
+ const data = (response.Result || {});
26
+ logger_1.logger.info(lang_1.lang.__('TLS_PROJECT_CREATED', { projectName: config.projectName }));
27
+ return {
28
+ projectId: data.ProjectId,
29
+ projectName: config.projectName,
30
+ description: config.description,
31
+ region: config.region,
32
+ createTime: data.CreateTime,
33
+ status: data.Status,
34
+ };
35
+ },
36
+ getProject: async (projectName) => {
37
+ try {
38
+ const response = await tlsClient.fetchOpenAPI({
39
+ Action: 'GetProject',
40
+ Version: '2024-01-01',
41
+ method: 'POST',
42
+ headers: { 'content-type': 'application/json' },
43
+ data: { ProjectName: projectName },
44
+ });
45
+ const data = (response.Result || {});
46
+ return {
47
+ projectId: data.ProjectId,
48
+ projectName: data.ProjectName,
49
+ description: data.Description,
50
+ region: data.Region,
51
+ createTime: data.CreateTime,
52
+ status: data.Status,
53
+ };
54
+ }
55
+ catch (error) {
56
+ if (error && typeof error === 'object' && 'code' in error) {
57
+ if (error.code === 'ProjectNotFound' || error.code === 'ResourceNotFound') {
58
+ return null;
59
+ }
60
+ }
61
+ throw error;
62
+ }
63
+ },
64
+ deleteProject: async (projectName) => {
65
+ try {
66
+ await tlsClient.fetchOpenAPI({
67
+ Action: 'DeleteProject',
68
+ Version: '2024-01-01',
69
+ method: 'POST',
70
+ headers: { 'content-type': 'application/json' },
71
+ data: { ProjectName: projectName },
72
+ });
73
+ logger_1.logger.info(lang_1.lang.__('TLS_PROJECT_DELETED', { projectName }));
74
+ }
75
+ catch (error) {
76
+ if (error && typeof error === 'object' && 'code' in error) {
77
+ if (error.code === 'ProjectNotFound' || error.code === 'ResourceNotFound') {
78
+ logger_1.logger.warn(lang_1.lang.__('TLS_PROJECT_NOT_FOUND', { projectName }));
79
+ return;
80
+ }
81
+ }
82
+ throw error;
83
+ }
84
+ },
85
+ createTopic: async (config) => {
86
+ const response = await tlsClient.fetchOpenAPI({
87
+ Action: 'CreateTopic',
88
+ Version: '2024-01-01',
89
+ method: 'POST',
90
+ headers: { 'content-type': 'application/json' },
91
+ data: {
92
+ ProjectName: config.projectName,
93
+ TopicName: config.topicName,
94
+ Description: config.description,
95
+ TTL: config.ttl ?? 30,
96
+ },
97
+ });
98
+ const data = (response.Result || {});
99
+ logger_1.logger.info(lang_1.lang.__('TLS_TOPIC_CREATED', { topicName: config.topicName }));
100
+ return {
101
+ topicId: data.TopicId,
102
+ topicName: config.topicName,
103
+ projectName: config.projectName,
104
+ description: config.description,
105
+ ttl: config.ttl,
106
+ createTime: data.CreateTime,
107
+ status: data.Status,
108
+ };
109
+ },
110
+ getTopic: async (projectName, topicName) => {
111
+ try {
112
+ const response = await tlsClient.fetchOpenAPI({
113
+ Action: 'GetTopic',
114
+ Version: '2024-01-01',
115
+ method: 'POST',
116
+ headers: { 'content-type': 'application/json' },
117
+ data: {
118
+ ProjectName: projectName,
119
+ TopicName: topicName,
120
+ },
121
+ });
122
+ const data = (response.Result || {});
123
+ return {
124
+ topicId: data.TopicId,
125
+ topicName: data.TopicName,
126
+ projectName: data.ProjectName,
127
+ description: data.Description,
128
+ ttl: data.TTL,
129
+ createTime: data.CreateTime,
130
+ status: data.Status,
131
+ };
132
+ }
133
+ catch (error) {
134
+ if (error && typeof error === 'object' && 'code' in error) {
135
+ if (error.code === 'TopicNotFound' || error.code === 'ResourceNotFound') {
136
+ return null;
137
+ }
138
+ }
139
+ throw error;
140
+ }
141
+ },
142
+ deleteTopic: async (projectName, topicName) => {
143
+ try {
144
+ await tlsClient.fetchOpenAPI({
145
+ Action: 'DeleteTopic',
146
+ Version: '2024-01-01',
147
+ method: 'POST',
148
+ headers: { 'content-type': 'application/json' },
149
+ data: {
150
+ ProjectName: projectName,
151
+ TopicName: topicName,
152
+ },
153
+ });
154
+ logger_1.logger.info(lang_1.lang.__('TLS_TOPIC_DELETED', { topicName }));
155
+ }
156
+ catch (error) {
157
+ if (error && typeof error === 'object' && 'code' in error) {
158
+ if (error.code === 'TopicNotFound' || error.code === 'ResourceNotFound') {
159
+ logger_1.logger.warn(lang_1.lang.__('TLS_TOPIC_NOT_FOUND', { topicName }));
160
+ return;
161
+ }
162
+ }
163
+ throw error;
164
+ }
165
+ },
166
+ createIndex: async (config) => {
167
+ await tlsClient.fetchOpenAPI({
168
+ Action: 'CreateIndex',
169
+ Version: '2024-01-01',
170
+ method: 'POST',
171
+ headers: { 'content-type': 'application/json' },
172
+ data: {
173
+ ProjectName: config.projectName,
174
+ TopicName: config.topicName,
175
+ FullTextIndex: config.fullTextIndex
176
+ ? {
177
+ Delimiter: config.fullTextIndex.delimiter ?? ' ,.?;!\n\t',
178
+ CaseSensitive: config.fullTextIndex.caseSensitive ?? false,
179
+ }
180
+ : undefined,
181
+ KeyValueIndex: config.keyValueIndex,
182
+ },
183
+ });
184
+ logger_1.logger.info(lang_1.lang.__('TLS_INDEX_CREATED', { topicName: config.topicName }));
185
+ },
186
+ deleteIndex: async (projectName, topicName) => {
187
+ try {
188
+ await tlsClient.fetchOpenAPI({
189
+ Action: 'DeleteIndex',
190
+ Version: '2024-01-01',
191
+ method: 'POST',
192
+ headers: { 'content-type': 'application/json' },
193
+ data: {
194
+ ProjectName: projectName,
195
+ TopicName: topicName,
196
+ },
197
+ });
198
+ logger_1.logger.info(lang_1.lang.__('TLS_INDEX_DELETED', { topicName }));
199
+ }
200
+ catch (error) {
201
+ if (error && typeof error === 'object' && 'code' in error) {
202
+ if (error.code === 'IndexNotFound' || error.code === 'ResourceNotFound') {
203
+ return;
204
+ }
205
+ }
206
+ throw error;
207
+ }
208
+ },
209
+ waitForProject: async (projectName) => {
210
+ for (let attempt = 0; attempt < MAX_WAIT_ATTEMPTS; attempt++) {
211
+ const project = await tlsClient.fetchOpenAPI({
212
+ Action: 'GetProject',
213
+ Version: '2024-01-01',
214
+ method: 'POST',
215
+ headers: { 'content-type': 'application/json' },
216
+ data: { ProjectName: projectName },
217
+ });
218
+ const data = (project.Result || {});
219
+ const status = data.Status;
220
+ if (status === 'Active') {
221
+ return;
222
+ }
223
+ if (status === 'Failed' || status === 'Error') {
224
+ throw new Error(lang_1.lang.__('TLS_PROJECT_FAILED', { projectName }));
225
+ }
226
+ await delay(WAIT_INTERVAL_MS);
227
+ }
228
+ throw new Error(lang_1.lang.__('TLS_PROJECT_TIMEOUT', { projectName }));
229
+ },
230
+ waitForTopic: async (projectName, topicName) => {
231
+ for (let attempt = 0; attempt < MAX_WAIT_ATTEMPTS; attempt++) {
232
+ const topic = await tlsClient.fetchOpenAPI({
233
+ Action: 'GetTopic',
234
+ Version: '2024-01-01',
235
+ method: 'POST',
236
+ headers: { 'content-type': 'application/json' },
237
+ data: {
238
+ ProjectName: projectName,
239
+ TopicName: topicName,
240
+ },
241
+ });
242
+ const data = (topic.Result || {});
243
+ const status = data.Status;
244
+ if (status === 'Active') {
245
+ return;
246
+ }
247
+ if (status === 'Failed' || status === 'Error') {
248
+ throw new Error(lang_1.lang.__('TLS_TOPIC_FAILED', { topicName }));
249
+ }
250
+ await delay(WAIT_INTERVAL_MS);
251
+ }
252
+ throw new Error(lang_1.lang.__('TLS_TOPIC_TIMEOUT', { topicName }));
253
+ },
254
+ };
255
+ };
256
+ exports.createTlsOperations = createTlsOperations;
@@ -0,0 +1,440 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.createTosOperations = void 0;
37
+ const fs = __importStar(require("node:fs"));
38
+ const path = __importStar(require("node:path"));
39
+ const logger_1 = require("../logger");
40
+ const lang_1 = require("../../lang");
41
+ const TOS_ACL_VALUES = ['private', 'public-read', 'public-read-write'];
42
+ const getContentType = (filePath) => {
43
+ const ext = path.extname(filePath).toLowerCase();
44
+ const contentTypes = {
45
+ '.html': 'text/html',
46
+ '.css': 'text/css',
47
+ '.js': 'application/javascript',
48
+ '.json': 'application/json',
49
+ '.png': 'image/png',
50
+ '.jpg': 'image/jpeg',
51
+ '.jpeg': 'image/jpeg',
52
+ '.gif': 'image/gif',
53
+ '.svg': 'image/svg+xml',
54
+ '.ico': 'image/x-icon',
55
+ '.woff': 'font/woff',
56
+ '.woff2': 'font/woff2',
57
+ '.ttf': 'font/ttf',
58
+ '.eot': 'application/vnd.ms-fontobject',
59
+ '.mp4': 'video/mp4',
60
+ '.webm': 'video/webm',
61
+ '.mp3': 'audio/mpeg',
62
+ '.pdf': 'application/pdf',
63
+ '.zip': 'application/zip',
64
+ '.xml': 'application/xml',
65
+ '.txt': 'text/plain',
66
+ };
67
+ return contentTypes[ext] || 'application/octet-stream';
68
+ };
69
+ const createTosOperations = (client, _region) => {
70
+ return {
71
+ createBucket: async (config) => {
72
+ if (!client) {
73
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
74
+ }
75
+ const params = {
76
+ Bucket: config.bucketName,
77
+ };
78
+ if (config.acl && TOS_ACL_VALUES.includes(config.acl)) {
79
+ params.ACL = config.acl;
80
+ }
81
+ if (config.storageClass) {
82
+ params.StorageClass = config.storageClass;
83
+ }
84
+ await client.fetchOpenAPI({
85
+ Action: 'CreateBucket',
86
+ Version: '2018-08-01',
87
+ method: 'PUT',
88
+ headers: {
89
+ 'content-type': 'application/xml',
90
+ },
91
+ query: params,
92
+ });
93
+ logger_1.logger.info(lang_1.lang.__('TOS_BUCKET_CREATED', { bucketName: config.bucketName }));
94
+ if (config.websiteConfig) {
95
+ await client.fetchOpenAPI({
96
+ Action: 'PutBucketWebsite',
97
+ Version: '2018-08-01',
98
+ method: 'PUT',
99
+ headers: {
100
+ 'content-type': 'application/xml',
101
+ },
102
+ query: {
103
+ Bucket: config.bucketName,
104
+ },
105
+ data: {
106
+ IndexDocument: {
107
+ Suffix: config.websiteConfig.indexDocument,
108
+ },
109
+ ...(config.websiteConfig.errorDocument && {
110
+ ErrorDocument: {
111
+ Key: config.websiteConfig.errorDocument,
112
+ },
113
+ }),
114
+ },
115
+ });
116
+ logger_1.logger.info(lang_1.lang.__('TOS_BUCKET_WEBSITE_CONFIGURED', { bucketName: config.bucketName }));
117
+ }
118
+ const bucketInfo = await (async () => {
119
+ const response = await client.fetchOpenAPI({
120
+ Action: 'GetBucketInfo',
121
+ Version: '2018-08-01',
122
+ method: 'GET',
123
+ query: {
124
+ Bucket: config.bucketName,
125
+ },
126
+ });
127
+ const result = (response.Result || {});
128
+ const bucketData = (result.BucketInfo || {});
129
+ return {
130
+ name: config.bucketName,
131
+ location: bucketData.Location,
132
+ creationDate: bucketData.CreationDate,
133
+ storageClass: bucketData.StorageClass,
134
+ extranetEndpoint: bucketData.ExtranetEndpoint,
135
+ intranetEndpoint: bucketData.IntranetEndpoint,
136
+ acl: bucketData.ACL,
137
+ };
138
+ })();
139
+ return bucketInfo;
140
+ },
141
+ getBucket: async (bucketName) => {
142
+ if (!client) {
143
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
144
+ }
145
+ try {
146
+ const response = await client.fetchOpenAPI({
147
+ Action: 'GetBucketInfo',
148
+ Version: '2018-08-01',
149
+ method: 'GET',
150
+ query: {
151
+ Bucket: bucketName,
152
+ },
153
+ });
154
+ const result = (response.Result || {});
155
+ const bucketData = (result.BucketInfo || {});
156
+ let websiteConfig;
157
+ try {
158
+ const websiteResponse = await client.fetchOpenAPI({
159
+ Action: 'GetBucketWebsite',
160
+ Version: '2018-08-01',
161
+ method: 'GET',
162
+ query: {
163
+ Bucket: bucketName,
164
+ },
165
+ });
166
+ const websiteResult = (websiteResponse.Result || {});
167
+ if (websiteResult.IndexDocument) {
168
+ const indexDoc = websiteResult.IndexDocument;
169
+ websiteConfig = {
170
+ indexDocument: indexDoc.Suffix,
171
+ };
172
+ if (websiteResult.ErrorDocument) {
173
+ const errorDoc = websiteResult.ErrorDocument;
174
+ websiteConfig.errorDocument = errorDoc.Key;
175
+ }
176
+ }
177
+ }
178
+ catch {
179
+ // Website config may not exist
180
+ }
181
+ return {
182
+ name: bucketName,
183
+ location: bucketData.Location,
184
+ creationDate: bucketData.CreationDate,
185
+ storageClass: bucketData.StorageClass,
186
+ extranetEndpoint: bucketData.ExtranetEndpoint,
187
+ intranetEndpoint: bucketData.IntranetEndpoint,
188
+ acl: bucketData.ACL,
189
+ websiteConfig,
190
+ };
191
+ }
192
+ catch (error) {
193
+ if (error &&
194
+ typeof error === 'object' &&
195
+ 'code' in error &&
196
+ (error.code === 'NoSuchBucket' || error.code === 'ResourceNotFound')) {
197
+ return null;
198
+ }
199
+ throw error;
200
+ }
201
+ },
202
+ updateBucketAcl: async (bucketName, acl) => {
203
+ if (!client) {
204
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
205
+ }
206
+ await client.fetchOpenAPI({
207
+ Action: 'PutBucketAcl',
208
+ Version: '2018-08-01',
209
+ method: 'PUT',
210
+ headers: {
211
+ 'content-type': 'application/xml',
212
+ },
213
+ query: {
214
+ Bucket: bucketName,
215
+ ACL: acl,
216
+ },
217
+ });
218
+ logger_1.logger.info(lang_1.lang.__('TOS_BUCKET_ACL_UPDATED', { bucketName, acl }));
219
+ },
220
+ updateBucketWebsite: async (bucketName, websiteConfig) => {
221
+ if (!client) {
222
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
223
+ }
224
+ const data = {
225
+ IndexDocument: {
226
+ Suffix: websiteConfig.indexDocument,
227
+ },
228
+ };
229
+ if (websiteConfig.errorDocument) {
230
+ data.ErrorDocument = {
231
+ Key: websiteConfig.errorDocument,
232
+ };
233
+ }
234
+ await client.fetchOpenAPI({
235
+ Action: 'PutBucketWebsite',
236
+ Version: '2018-08-01',
237
+ method: 'PUT',
238
+ headers: {
239
+ 'content-type': 'application/xml',
240
+ },
241
+ query: {
242
+ Bucket: bucketName,
243
+ },
244
+ data,
245
+ });
246
+ logger_1.logger.info(lang_1.lang.__('TOS_BUCKET_WEBSITE_UPDATED', { bucketName }));
247
+ },
248
+ deleteBucket: async (bucketName) => {
249
+ if (!client) {
250
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
251
+ }
252
+ try {
253
+ let continuationToken;
254
+ do {
255
+ const listResponse = await client.fetchOpenAPI({
256
+ Action: 'ListObjectsType2',
257
+ Version: '2018-08-01',
258
+ method: 'GET',
259
+ query: {
260
+ Bucket: bucketName,
261
+ 'list-type': '2',
262
+ ...(continuationToken && { 'continuation-token': continuationToken }),
263
+ },
264
+ });
265
+ const listResult = (listResponse.Result || {});
266
+ const contents = (listResult.Contents || []);
267
+ if (contents.length > 0) {
268
+ const objects = contents.map((obj) => ({ Key: obj.Key }));
269
+ await client.fetchOpenAPI({
270
+ Action: 'DeleteMultipleObjects',
271
+ Version: '2018-08-01',
272
+ method: 'POST',
273
+ headers: {
274
+ 'content-type': 'application/xml',
275
+ },
276
+ query: {
277
+ Bucket: bucketName,
278
+ },
279
+ data: {
280
+ Delete: {
281
+ Object: objects,
282
+ },
283
+ },
284
+ });
285
+ }
286
+ continuationToken = listResult.NextContinuationToken;
287
+ } while (continuationToken);
288
+ await client.fetchOpenAPI({
289
+ Action: 'DeleteBucket',
290
+ Version: '2018-08-01',
291
+ method: 'DELETE',
292
+ query: {
293
+ Bucket: bucketName,
294
+ },
295
+ });
296
+ logger_1.logger.info(lang_1.lang.__('TOS_BUCKET_DELETED', { bucketName }));
297
+ }
298
+ catch (error) {
299
+ if (error &&
300
+ typeof error === 'object' &&
301
+ 'code' in error &&
302
+ (error.code === 'NoSuchBucket' || error.code === 'ResourceNotFound')) {
303
+ logger_1.logger.warn(lang_1.lang.__('RESOURCE_NOT_FOUND_PROVIDER', { resourceType: 'Bucket', name: bucketName }));
304
+ return;
305
+ }
306
+ throw error;
307
+ }
308
+ },
309
+ putObject: async (bucket, key, body) => {
310
+ if (!client) {
311
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
312
+ }
313
+ await client.fetchOpenAPI({
314
+ Action: 'PutObject',
315
+ Version: '2018-08-01',
316
+ method: 'PUT',
317
+ headers: {
318
+ 'content-type': getContentType(key),
319
+ },
320
+ query: {
321
+ Bucket: bucket,
322
+ Key: key,
323
+ },
324
+ data: body,
325
+ });
326
+ },
327
+ listObjects: async (bucket, prefix) => {
328
+ if (!client) {
329
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
330
+ }
331
+ const objects = [];
332
+ let continuationToken;
333
+ do {
334
+ const response = await client.fetchOpenAPI({
335
+ Action: 'ListObjectsType2',
336
+ Version: '2018-08-01',
337
+ method: 'GET',
338
+ query: {
339
+ Bucket: bucket,
340
+ 'list-type': '2',
341
+ ...(prefix && { Prefix: prefix }),
342
+ ...(continuationToken && { 'continuation-token': continuationToken }),
343
+ },
344
+ });
345
+ const result = (response.Result || {});
346
+ const contents = (result.Contents || []);
347
+ for (const obj of contents) {
348
+ objects.push({
349
+ key: obj.Key,
350
+ size: obj.Size,
351
+ lastModified: obj.LastModified,
352
+ etag: obj.ETag,
353
+ });
354
+ }
355
+ continuationToken = result.NextContinuationToken;
356
+ } while (continuationToken);
357
+ return objects;
358
+ },
359
+ deleteObjects: async (bucket, keys) => {
360
+ if (!client) {
361
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
362
+ }
363
+ if (keys.length === 0)
364
+ return;
365
+ const objects = keys.map((key) => ({ Key: key }));
366
+ await client.fetchOpenAPI({
367
+ Action: 'DeleteMultipleObjects',
368
+ Version: '2018-08-01',
369
+ method: 'POST',
370
+ headers: {
371
+ 'content-type': 'application/xml',
372
+ },
373
+ query: {
374
+ Bucket: bucket,
375
+ },
376
+ data: {
377
+ Delete: {
378
+ Object: objects,
379
+ },
380
+ },
381
+ });
382
+ },
383
+ uploadFiles: async (bucketName, sourcePath) => {
384
+ if (!client) {
385
+ throw new Error(lang_1.lang.__('VOLCENGINE_TOS_CLIENT_NOT_INITIALIZED'));
386
+ }
387
+ const uploadDirectory = async (dirPath, prefix = '') => {
388
+ const entries = fs.readdirSync(dirPath, { withFileTypes: true });
389
+ for (const entry of entries) {
390
+ const fullPath = path.join(dirPath, entry.name);
391
+ const objectKey = prefix ? `${prefix}/${entry.name}` : entry.name;
392
+ if (entry.isDirectory()) {
393
+ await uploadDirectory(fullPath, objectKey);
394
+ }
395
+ else if (entry.isFile()) {
396
+ const fileContent = fs.readFileSync(fullPath);
397
+ const contentType = getContentType(fullPath);
398
+ await client.fetchOpenAPI({
399
+ Action: 'PutObject',
400
+ Version: '2018-08-01',
401
+ method: 'PUT',
402
+ headers: {
403
+ 'content-type': contentType,
404
+ },
405
+ query: {
406
+ Bucket: bucketName,
407
+ Key: objectKey,
408
+ },
409
+ data: fileContent,
410
+ });
411
+ logger_1.logger.debug(lang_1.lang.__('TOS_OBJECT_UPLOADED', { bucket: bucketName, key: objectKey }));
412
+ }
413
+ }
414
+ };
415
+ const stats = fs.statSync(sourcePath);
416
+ if (stats.isDirectory()) {
417
+ await uploadDirectory(sourcePath);
418
+ }
419
+ else {
420
+ const fileName = path.basename(sourcePath);
421
+ const fileContent = fs.readFileSync(sourcePath);
422
+ await client.fetchOpenAPI({
423
+ Action: 'PutObject',
424
+ Version: '2018-08-01',
425
+ method: 'PUT',
426
+ headers: {
427
+ 'content-type': getContentType(sourcePath),
428
+ },
429
+ query: {
430
+ Bucket: bucketName,
431
+ Key: fileName,
432
+ },
433
+ data: fileContent,
434
+ });
435
+ }
436
+ logger_1.logger.info(lang_1.lang.__('TOS_FILES_UPLOADED', { bucket: bucketName, path: sourcePath }));
437
+ },
438
+ };
439
+ };
440
+ exports.createTosOperations = createTosOperations;