@geek-fun/serverlessinsight 0.4.0 → 0.5.0

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 (160) hide show
  1. package/.gitattributes +1 -0
  2. package/README.md +108 -8
  3. package/README.zh-CN.md +52 -8
  4. package/dist/package.json +37 -35
  5. package/dist/src/commands/deploy.js +17 -7
  6. package/dist/src/commands/destroy.js +27 -4
  7. package/dist/src/commands/forceUnlock.js +61 -0
  8. package/dist/src/commands/index.js +86 -14
  9. package/dist/src/commands/local.js +10 -1
  10. package/dist/src/commands/plan.js +33 -0
  11. package/dist/src/commands/template.js +3 -1
  12. package/dist/src/commands/validate.js +2 -1
  13. package/dist/src/common/aliyunClient/apigwOperations.js +652 -0
  14. package/dist/src/common/aliyunClient/dnsOperations.js +90 -0
  15. package/dist/src/common/aliyunClient/ecsOperations.js +141 -0
  16. package/dist/src/common/aliyunClient/esOperations.js +219 -0
  17. package/dist/src/common/aliyunClient/fc3Operations.js +270 -0
  18. package/dist/src/common/aliyunClient/index.js +141 -0
  19. package/dist/src/common/aliyunClient/nasOperations.js +233 -0
  20. package/dist/src/common/aliyunClient/ossOperations.js +237 -0
  21. package/dist/src/common/aliyunClient/ramOperations.js +205 -0
  22. package/dist/src/common/aliyunClient/rdsOperations.js +206 -0
  23. package/dist/src/common/aliyunClient/slsOperations.js +218 -0
  24. package/dist/src/common/aliyunClient/tablestoreOperations.js +199 -0
  25. package/dist/src/common/aliyunClient/types.js +2 -0
  26. package/dist/src/common/constants.js +7 -1
  27. package/dist/src/common/context.js +32 -14
  28. package/dist/src/common/credentials.js +39 -0
  29. package/dist/src/common/dependencyGraph/graph.js +280 -0
  30. package/dist/src/common/dependencyGraph/index.js +18 -0
  31. package/dist/src/common/dependencyGraph/types.js +2 -0
  32. package/dist/src/common/fileUtils.js +16 -0
  33. package/dist/src/common/hashUtils.js +121 -0
  34. package/dist/src/common/iacHelper.js +25 -97
  35. package/dist/src/common/imsClient.js +4 -0
  36. package/dist/src/common/index.js +7 -2
  37. package/dist/src/common/lockManager.js +212 -0
  38. package/dist/src/common/logger.js +89 -6
  39. package/dist/src/common/providerEnum.js +2 -3
  40. package/dist/src/common/runtimeMapper.js +160 -0
  41. package/dist/src/common/scfClient.js +84 -0
  42. package/dist/src/common/stateManager.js +107 -0
  43. package/dist/src/common/tencentClient/cosOperations.js +287 -0
  44. package/dist/src/common/tencentClient/esOperations.js +156 -0
  45. package/dist/src/common/tencentClient/index.js +116 -0
  46. package/dist/src/common/tencentClient/scfOperations.js +141 -0
  47. package/dist/src/common/tencentClient/tdsqlcOperations.js +211 -0
  48. package/dist/src/common/tencentClient/types.js +17 -0
  49. package/dist/src/lang/en.js +254 -0
  50. package/dist/src/lang/index.js +28 -8
  51. package/dist/src/lang/zh-CN.js +229 -0
  52. package/dist/src/parser/bucketParser.js +25 -12
  53. package/dist/src/parser/databaseParser.js +14 -10
  54. package/dist/src/parser/functionParser.js +19 -6
  55. package/dist/src/parser/parseUtils.js +74 -0
  56. package/dist/src/parser/tableParser.js +19 -17
  57. package/dist/src/stack/aliyunStack/apigwExecutor.js +84 -0
  58. package/dist/src/stack/aliyunStack/apigwPlanner.js +118 -0
  59. package/dist/src/stack/aliyunStack/apigwResource.js +339 -0
  60. package/dist/src/stack/aliyunStack/apigwTypes.js +125 -0
  61. package/dist/src/stack/aliyunStack/databaseExecutor.js +112 -0
  62. package/dist/src/stack/aliyunStack/databasePlanner.js +128 -0
  63. package/dist/src/stack/aliyunStack/databaseResource.js +228 -0
  64. package/dist/src/stack/aliyunStack/deployer.js +133 -0
  65. package/dist/src/stack/aliyunStack/destroyer.js +114 -0
  66. package/dist/src/stack/aliyunStack/esServerlessTypes.js +141 -0
  67. package/dist/src/stack/aliyunStack/fc3Executor.js +91 -0
  68. package/dist/src/stack/aliyunStack/fc3Planner.js +77 -0
  69. package/dist/src/stack/aliyunStack/fc3Resource.js +511 -0
  70. package/dist/src/stack/aliyunStack/fc3Types.js +76 -0
  71. package/dist/src/stack/aliyunStack/index.js +40 -0
  72. package/dist/src/stack/aliyunStack/ossExecutor.js +91 -0
  73. package/dist/src/stack/aliyunStack/ossPlanner.js +76 -0
  74. package/dist/src/stack/aliyunStack/ossResource.js +196 -0
  75. package/dist/src/stack/aliyunStack/ossTypes.js +50 -0
  76. package/dist/src/stack/aliyunStack/planner.js +37 -0
  77. package/dist/src/stack/aliyunStack/rdsTypes.js +217 -0
  78. package/dist/src/stack/aliyunStack/tablestoreExecutor.js +92 -0
  79. package/dist/src/stack/aliyunStack/tablestorePlanner.js +94 -0
  80. package/dist/src/stack/aliyunStack/tablestoreResource.js +120 -0
  81. package/dist/src/stack/aliyunStack/tablestoreTypes.js +77 -0
  82. package/dist/src/stack/bucketTypes.js +17 -0
  83. package/dist/src/stack/deploy.js +24 -77
  84. package/dist/src/stack/localStack/bucket.js +11 -6
  85. package/dist/src/stack/localStack/event.js +10 -5
  86. package/dist/src/stack/localStack/function.js +13 -7
  87. package/dist/src/stack/localStack/functionRunner.js +1 -1
  88. package/dist/src/stack/localStack/localServer.js +7 -6
  89. package/dist/src/stack/scfStack/cosExecutor.js +91 -0
  90. package/dist/src/stack/scfStack/cosPlanner.js +76 -0
  91. package/dist/src/stack/scfStack/cosResource.js +126 -0
  92. package/dist/src/stack/scfStack/cosTypes.js +46 -0
  93. package/dist/src/stack/scfStack/deployer.js +91 -0
  94. package/dist/src/stack/scfStack/destroyer.js +88 -0
  95. package/dist/src/stack/scfStack/esServerlessExecutor.js +105 -0
  96. package/dist/src/stack/scfStack/esServerlessPlanner.js +86 -0
  97. package/dist/src/stack/scfStack/esServerlessResource.js +94 -0
  98. package/dist/src/stack/scfStack/esServerlessTypes.js +48 -0
  99. package/dist/src/stack/scfStack/index.js +35 -0
  100. package/dist/src/stack/scfStack/planner.js +91 -0
  101. package/dist/src/stack/scfStack/scfExecutor.js +91 -0
  102. package/dist/src/stack/scfStack/scfPlanner.js +78 -0
  103. package/dist/src/stack/scfStack/scfResource.js +216 -0
  104. package/dist/src/stack/scfStack/scfTypes.js +41 -0
  105. package/dist/src/stack/scfStack/tdsqlcExecutor.js +105 -0
  106. package/dist/src/stack/scfStack/tdsqlcPlanner.js +90 -0
  107. package/dist/src/stack/scfStack/tdsqlcResource.js +146 -0
  108. package/dist/src/stack/scfStack/tdsqlcTypes.js +59 -0
  109. package/dist/src/types/domains/lock.js +2 -0
  110. package/dist/src/types/domains/resolvable.js +2 -0
  111. package/dist/src/types/domains/state.js +19 -0
  112. package/dist/src/types/index.js +4 -0
  113. package/dist/src/validator/bucketSchema.js +4 -10
  114. package/dist/src/validator/databaseSchema.js +36 -36
  115. package/dist/src/validator/eventSchema.js +3 -2
  116. package/dist/src/validator/functionSchema.js +51 -46
  117. package/dist/src/validator/iacSchema.js +52 -3
  118. package/dist/src/validator/rootSchema.js +47 -1
  119. package/dist/src/validator/tableschema.js +9 -8
  120. package/dist/src/validator/templateRefSchema.js +23 -0
  121. package/dist/tsconfig.tsbuildinfo +1 -1
  122. package/package.json +37 -35
  123. package/samples/README_TENCENT_COS.md +486 -0
  124. package/samples/README_TENCENT_SCF.md +272 -0
  125. package/samples/aliyun-poc-api.yml +1 -1
  126. package/samples/aliyun-poc-bucket.yml +0 -1
  127. package/samples/aliyun-poc-domain.yml +0 -1
  128. package/samples/aliyun-poc-es.yml +14 -13
  129. package/samples/aliyun-poc-rds.yml +0 -2
  130. package/samples/aliyun-poc-table.yml +1 -3
  131. package/samples/tencent-poc-cos.yml +20 -0
  132. package/samples/tencent-poc-scf.yml +36 -0
  133. package/dist/src/commands/index.d.ts +0 -2
  134. package/dist/src/common/index.d.ts +0 -11
  135. package/dist/src/common/rosAssets.js +0 -178
  136. package/dist/src/common/rosClient.js +0 -198
  137. package/dist/src/index.d.ts +0 -1
  138. package/dist/src/lang/index.d.ts +0 -3
  139. package/dist/src/parser/index.d.ts +0 -3
  140. package/dist/src/stack/index.d.ts +0 -1
  141. package/dist/src/stack/localStack/index.d.ts +0 -5
  142. package/dist/src/stack/rfsStack/index.d.ts +0 -9
  143. package/dist/src/stack/rosStack/bootstrap.js +0 -187
  144. package/dist/src/stack/rosStack/bucket.js +0 -127
  145. package/dist/src/stack/rosStack/database.js +0 -313
  146. package/dist/src/stack/rosStack/event.js +0 -143
  147. package/dist/src/stack/rosStack/function.js +0 -259
  148. package/dist/src/stack/rosStack/index.d.ts +0 -7
  149. package/dist/src/stack/rosStack/index.js +0 -75
  150. package/dist/src/stack/rosStack/stage.js +0 -46
  151. package/dist/src/stack/rosStack/table.js +0 -95
  152. package/dist/src/stack/rosStack/tag.js +0 -11
  153. package/dist/src/stack/rosStack/vars.js +0 -49
  154. package/dist/src/types/index.d.ts +0 -55
  155. package/dist/src/types/localStack/index.d.ts +0 -81
  156. package/dist/src/validator/index.d.ts +0 -1
  157. package/layers/si-bootstrap-sdk/Dockerfile-aliyuncli +0 -12
  158. package/layers/si-bootstrap-sdk/README.md +0 -1
  159. package/layers/si-bootstrap-sdk/package-lock.json +0 -875
  160. package/layers/si-bootstrap-sdk/package.json +0 -33
@@ -0,0 +1,272 @@
1
+ # Tencent Cloud SCF (Serverless Cloud Function) Support
2
+
3
+ This guide demonstrates how to use ServerlessInsight with Tencent Cloud SCF using the new state-based deployment engine.
4
+
5
+ ## Overview
6
+
7
+ The Tencent SCF support introduces a Terraform-like infrastructure management approach with:
8
+
9
+ - **State Management**: Local state tracking in `.serverlessinsight/state.json`
10
+ - **Plan & Apply Flow**: Preview changes before applying them
11
+ - **Drift Detection**: Detect manual changes made outside ServerlessInsight
12
+ - **Idempotent Operations**: Safe to re-run deployments
13
+
14
+ ## Prerequisites
15
+
16
+ 1. Tencent Cloud account with API credentials
17
+ 2. ServerlessInsight CLI installed
18
+ 3. Function code packaged as a ZIP file
19
+
20
+ ## Configuration
21
+
22
+ Create a `serverless-insight.yml` file:
23
+
24
+ ```yaml
25
+ version: 0.0.1
26
+ provider:
27
+ name: tencent
28
+ region: ap-guangzhou
29
+
30
+ vars:
31
+ handler: index.handler
32
+
33
+ stages:
34
+ default:
35
+ node_env: default
36
+ dev:
37
+ node_env: development
38
+ prod:
39
+ node_env: production
40
+
41
+ service: my-service
42
+
43
+ functions:
44
+ my_function:
45
+ name: my-function-name
46
+ code:
47
+ runtime: Nodejs18.15
48
+ handler: index.handler
49
+ path: path/to/function.zip
50
+ memory: 512
51
+ timeout: 10
52
+ environment:
53
+ NODE_ENV: ${stages.node_env}
54
+ API_KEY: your-api-key
55
+ ```
56
+
57
+ ## Supported Runtimes
58
+
59
+ - Nodejs18.15
60
+ - Nodejs16.13
61
+ - Python3.7
62
+ - Python3.9
63
+ - And other Tencent SCF supported runtimes
64
+
65
+ ## Commands
66
+
67
+ ### Plan
68
+
69
+ Preview changes before deployment:
70
+
71
+ ```bash
72
+ si plan my-stack \
73
+ -f serverless-insight.yml \
74
+ -r ap-guangzhou \
75
+ -k YOUR_SECRET_ID \
76
+ -x YOUR_SECRET_KEY
77
+ ```
78
+
79
+ Output shows:
80
+
81
+ - Resources to be **created** (green `+`)
82
+ - Resources to be **updated** (yellow `~`)
83
+ - Resources to be **deleted** (red `-`)
84
+ - Resources with **no changes** (blue `=`)
85
+
86
+ ### Deploy
87
+
88
+ Apply the infrastructure changes:
89
+
90
+ ```bash
91
+ si deploy my-stack \
92
+ -f serverless-insight.yml \
93
+ -r ap-guangzhou \
94
+ -k YOUR_SECRET_ID \
95
+ -x YOUR_SECRET_KEY
96
+ ```
97
+
98
+ The deploy command will:
99
+
100
+ 1. Generate a plan
101
+ 2. Execute the plan (create/update/delete functions)
102
+ 3. Save the state to `.serverlessinsight/state.json`
103
+
104
+ ### Validate
105
+
106
+ Validate your YAML configuration:
107
+
108
+ ```bash
109
+ si validate my-stack -f serverless-insight.yml
110
+ ```
111
+
112
+ ## State Management
113
+
114
+ State is stored locally in `.serverlessinsight/state.json`:
115
+
116
+ ```json
117
+ {
118
+ "version": "0.1",
119
+ "provider": "tencent",
120
+ "resources": {
121
+ "functions.my_function": {
122
+ "type": "SCF",
123
+ "physicalId": "my-function-name",
124
+ "region": "ap-guangzhou",
125
+ "configHash": "abc123...",
126
+ "lastUpdated": "2025-01-01T12:00:00Z"
127
+ }
128
+ }
129
+ }
130
+ ```
131
+
132
+ **Important**: Add `.serverlessinsight/` to your `.gitignore` if you don't want to commit state files.
133
+
134
+ ## Idempotency
135
+
136
+ Running `si deploy` multiple times with the same configuration is safe:
137
+
138
+ ```bash
139
+ # First run - creates the function
140
+ si deploy my-stack -f serverless-insight.yml
141
+
142
+ # Second run - detects no changes needed
143
+ si deploy my-stack -f serverless-insight.yml
144
+ ```
145
+
146
+ ## Drift Detection
147
+
148
+ If someone modifies the function in Tencent Cloud Console, the plan command will detect the drift:
149
+
150
+ ```bash
151
+ si plan my-stack -f serverless-insight.yml
152
+ ```
153
+
154
+ Output will show:
155
+
156
+ ```
157
+ [UPDATE] Resources to be updated:
158
+ ~ functions.my_function (SCF)
159
+ [DRIFTED] Remote configuration differs from state
160
+ ```
161
+
162
+ ## Environment Variables
163
+
164
+ Use stages to manage different environments:
165
+
166
+ ```yaml
167
+ stages:
168
+ default:
169
+ node_env: default
170
+ api_url: https://api.example.com
171
+ dev:
172
+ node_env: development
173
+ api_url: https://dev.api.example.com
174
+ prod:
175
+ node_env: production
176
+ api_url: https://api.example.com
177
+
178
+ functions:
179
+ my_function:
180
+ environment:
181
+ NODE_ENV: ${stages.node_env}
182
+ API_URL: ${stages.api_url}
183
+ ```
184
+
185
+ Deploy to different stages:
186
+
187
+ ```bash
188
+ # Deploy to dev
189
+ si deploy my-stack -f serverless-insight.yml --stage dev
190
+
191
+ # Deploy to prod
192
+ si deploy my-stack -f serverless-insight.yml --stage prod
193
+ ```
194
+
195
+ ## Function Configuration
196
+
197
+ ### Memory
198
+
199
+ Set function memory (in MB):
200
+
201
+ ```yaml
202
+ functions:
203
+ my_function:
204
+ memory: 512 # 128-3072 MB
205
+ ```
206
+
207
+ ### Timeout
208
+
209
+ Set function timeout (in seconds):
210
+
211
+ ```yaml
212
+ functions:
213
+ my_function:
214
+ timeout: 10 # 1-900 seconds
215
+ ```
216
+
217
+ ### Handler
218
+
219
+ Specify the entry point:
220
+
221
+ ```yaml
222
+ functions:
223
+ my_function:
224
+ code:
225
+ handler: index.handler # <file>.<function>
226
+ ```
227
+
228
+ ## Examples
229
+
230
+ See `samples/tencent-poc-scf.yml` for a complete example.
231
+
232
+ ## Troubleshooting
233
+
234
+ ### Authentication Errors
235
+
236
+ Ensure your Secret ID and Secret Key are correct:
237
+
238
+ ```bash
239
+ export TENCENTCLOUD_SECRET_ID=your_secret_id
240
+ export TENCENTCLOUD_SECRET_KEY=your_secret_key
241
+
242
+ si deploy my-stack -f serverless-insight.yml
243
+ ```
244
+
245
+ ### Function Not Found
246
+
247
+ If you see "ResourceNotFound.FunctionName", the function doesn't exist yet. Run `si deploy` to create it.
248
+
249
+ ### State File Conflicts
250
+
251
+ If you encounter state conflicts, you can:
252
+
253
+ 1. Delete `.serverlessinsight/state.json` and re-deploy (will recreate resources)
254
+ 2. Manually edit the state file (advanced users only)
255
+
256
+ ## Limitations
257
+
258
+ Current limitations:
259
+
260
+ - No support for VPC configuration
261
+ - No support for triggers (API Gateway, COS, etc.)
262
+ - No support for layers
263
+ - No remote state backends
264
+ - No cross-resource dependencies
265
+
266
+ These features will be added in future releases.
267
+
268
+ ## Next Steps
269
+
270
+ 1. Try the example: `samples/tencent-poc-scf.yml`
271
+ 2. Read the [main documentation](../README.md)
272
+ 3. Explore other cloud providers (Aliyun, Huawei)
@@ -46,4 +46,4 @@ events:
46
46
  path: /api/hello
47
47
  backend: ${functions.insight_poc_fn}
48
48
  domain:
49
- domain_name: insight-api.serverlessinsight.com
49
+ domain_name: insight-api.serverlessinsights.com
@@ -4,7 +4,6 @@ provider:
4
4
  name: aliyun
5
5
  region: cn-chengdu
6
6
 
7
-
8
7
  service: insight-bucket-poc
9
8
 
10
9
  tags:
@@ -4,7 +4,6 @@ provider:
4
4
  name: aliyun
5
5
  region: cn-hongkong
6
6
 
7
-
8
7
  service: insight-poc-domain
9
8
 
10
9
  tags:
@@ -2,8 +2,7 @@ version: 0.0.1
2
2
 
3
3
  provider:
4
4
  name: aliyun
5
- region: cn-chengdu
6
-
5
+ region: cn-hangzhou
7
6
 
8
7
  service: insight-db-poc
9
8
 
@@ -11,20 +10,22 @@ tags:
11
10
  owner: geek-fun
12
11
 
13
12
  databases:
14
- # insight_es_db:
15
- # name: insight-poc-es
16
- # type: ELASTICSEARCH_SERVERLESS
17
- # version: '7.10'
18
- # engine_mode: SEARCH
19
- # cu: 1
20
- # storage_size: 20
21
- # security:
22
- # basic_auth:
23
- # password: 'U34I6InQ8elseTgqTWT2t2oFXpoqFg'
24
- insight_poc_db:
13
+ insight_poc_es:
14
+ name: insight-poc-es
15
+ type: ELASTICSEARCH_SERVERLESS
16
+ version: ES_SEARCH_7.10
17
+ cu:
18
+ min: 3
19
+ max: 4
20
+ security:
21
+ basic_auth:
22
+ master_user: admin
23
+ password: 'U34I6InQ8elseTgqTWT2t2oFXpoqFg'
24
+ insight_poc_rds:
25
25
  name: insight-poc-rds
26
26
  type: RDS_PGSQL_SERVERLESS
27
27
  version: PGSQL_16
28
28
  security:
29
29
  basic_auth:
30
+ master_user: admin
30
31
  password: 'U34I6InQ8elseTgqTWT2t2oFXpoqFg'
@@ -4,7 +4,6 @@ provider:
4
4
  name: aliyun
5
5
  region: cn-chengdu
6
6
 
7
-
8
7
  service: insight-rds-poc
9
8
 
10
9
  tags:
@@ -21,4 +20,3 @@ databases:
21
20
  password: 'U34I6InQ8elseTgqTWT2t2oFXpoqFg'
22
21
  network:
23
22
  type: 'PRIVATE'
24
-
@@ -4,7 +4,6 @@ provider:
4
4
  name: aliyun
5
5
  region: cn-hangzhou
6
6
 
7
-
8
7
  service: insight-table-poc
9
8
 
10
9
  tags:
@@ -18,7 +17,7 @@ tables:
18
17
  type: TABLE_STORE_H
19
18
  network:
20
19
  type: 'PRIVATE'
21
- vpc_id: "vpc-12345678"
20
+ vpc_id: 'vpc-12345678'
22
21
  ingress_rules:
23
22
  - TCP:0.0.0.0/0:80
24
23
  - TCP:0.0.0.0/0:443
@@ -45,4 +44,3 @@ tables:
45
44
  type: DOUBLE
46
45
  - name: active
47
46
  type: BOOLEAN
48
-
@@ -0,0 +1,20 @@
1
+ version: 0.0.1
2
+ provider:
3
+ name: tencent
4
+ region: ap-guangzhou
5
+
6
+ service: insight-poc-cos
7
+
8
+ tags:
9
+ owner: geek-fun
10
+
11
+ buckets:
12
+ insight_poc_cos_bucket:
13
+ name: insight-poc-cos-bucket-1234567890 # Must be globally unique
14
+ security:
15
+ acl: PUBLIC_READ # PRIVATE, PUBLIC_READ, or PUBLIC_READ_WRITE
16
+ website:
17
+ code: dist
18
+ index: index.html
19
+ error_page: 404.html
20
+ error_code: 404
@@ -0,0 +1,36 @@
1
+ version: 0.0.1
2
+ provider:
3
+ name: tencent
4
+ region: ap-guangzhou
5
+
6
+ vars:
7
+ testv: testVarValue
8
+ handler: index.handler
9
+
10
+ stages:
11
+ default:
12
+ node_env: default
13
+ dev:
14
+ node_env: development
15
+ memory: 512
16
+ prod:
17
+ node_env: production
18
+ memory: 1024
19
+
20
+ service: insight-poc-scf
21
+
22
+ tags:
23
+ owner: geek-fun
24
+
25
+ functions:
26
+ insight_poc_fn:
27
+ name: insight-poc-api-fn
28
+ code:
29
+ runtime: nodejs18
30
+ handler: index.handler
31
+ path: artifacts/scf_code.zip
32
+ memory: ${stages.memory}
33
+ timeout: 10
34
+ environment:
35
+ NODE_ENV: ${stages.node_env}
36
+ TEST_VAR: ${vars.testv}
@@ -1,2 +0,0 @@
1
- #! /usr/bin/env node
2
- export {};
@@ -1,11 +0,0 @@
1
- export * from './providerEnum';
2
- export * from './logger';
3
- export * from './getVersion';
4
- export * from './rosClient';
5
- export * from './context';
6
- export * from './iacHelper';
7
- export * from './constants';
8
- export * from './imsClient';
9
- export * from './base64';
10
- export * from './rosAssets';
11
- export * from './requestHelper';
@@ -1,178 +0,0 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.cleanupAssets = exports.publishAssets = exports.constructAssets = exports.getAssets = void 0;
40
- const node_fs_1 = __importDefault(require("node:fs"));
41
- const ossDeployment = __importStar(require("@alicloud/ros-cdk-ossdeployment"));
42
- const node_path_1 = __importDefault(require("node:path"));
43
- const jszip_1 = __importDefault(require("jszip"));
44
- const logger_1 = require("./logger");
45
- const lodash_1 = require("lodash");
46
- const ali_oss_1 = __importDefault(require("ali-oss"));
47
- const context_1 = require("./context");
48
- const buildAssets = (rootPath, relativePath) => {
49
- const location = node_path_1.default.resolve(rootPath, relativePath);
50
- if (!node_fs_1.default.existsSync(location)) {
51
- throw new Error(`Location: ${location} is not exists!`);
52
- }
53
- if (node_fs_1.default.lstatSync(location).isFile()) {
54
- return [
55
- ossDeployment.Source.asset(location, {}, relativePath.substring(0, relativePath.lastIndexOf('/') + 1)),
56
- ];
57
- }
58
- return node_fs_1.default
59
- .readdirSync(location)
60
- .map((file) => buildAssets(rootPath, `${relativePath}/${file}`.replace(/^\//, '')))
61
- .flat();
62
- };
63
- const getAssets = (location) => {
64
- return buildAssets(location, '');
65
- };
66
- exports.getAssets = getAssets;
67
- const assembleFiles = (folder, zip) => {
68
- const files = node_fs_1.default.readdirSync(folder);
69
- files.forEach((file) => {
70
- const filePath = node_path_1.default.join(folder, file);
71
- if (node_fs_1.default.lstatSync(filePath).isFile()) {
72
- const content = node_fs_1.default.readFileSync(filePath);
73
- zip.file(file, content);
74
- }
75
- else {
76
- const subZip = zip.folder(file);
77
- if (subZip) {
78
- assembleFiles(filePath, subZip);
79
- }
80
- }
81
- });
82
- };
83
- const zipAssets = async (assetsPath) => {
84
- const zip = new jszip_1.default();
85
- assembleFiles(assetsPath, zip);
86
- const zipPath = `${assetsPath.replace(/\/$/, '').trim()}.zip`;
87
- await zip
88
- .generateAsync({ type: 'nodebuffer' })
89
- .then((content) => {
90
- node_fs_1.default.writeFileSync(zipPath, content);
91
- logger_1.logger.info(`Folder compressed to: ${zipPath}`);
92
- })
93
- .catch((e) => {
94
- logger_1.logger.error(`Failed to compress folder: ${e}`);
95
- throw e;
96
- });
97
- return zipPath;
98
- };
99
- const constructAssets = async ({ files, rootPath, }) => {
100
- const { region } = (0, context_1.getContext)();
101
- const assets = await Promise.all(Object.entries(files)
102
- .filter(([, fileItem]) => !fileItem.source.path.endsWith('.template.json'))
103
- .map(async ([, fileItem]) => {
104
- let sourcePath = `${rootPath}/${fileItem.source.path}`;
105
- if (fileItem.source.packaging === 'zip') {
106
- sourcePath = await zipAssets(`${rootPath}/${fileItem.source.path}`);
107
- }
108
- return {
109
- bucketName: (0, lodash_1.get)(fileItem, 'destinations.current_account-current_region.bucketName', '').replace('${ALIYUN::Region}', region),
110
- source: sourcePath,
111
- objectKey: (0, lodash_1.get)(fileItem, 'destinations.current_account-current_region.objectKey'),
112
- };
113
- }));
114
- return !(0, lodash_1.isEmpty)(assets) ? assets : undefined;
115
- };
116
- exports.constructAssets = constructAssets;
117
- const ensureBucketExits = async (bucketName, ossClient) => await ossClient.getBucketInfo(bucketName).catch((err) => {
118
- if (err.code === 'NoSuchBucket') {
119
- logger_1.logger.info(`Bucket: ${bucketName} not exists, creating...`);
120
- return ossClient.putBucket(bucketName, {
121
- storageClass: 'Standard',
122
- acl: 'private',
123
- dataRedundancyType: 'LRS',
124
- });
125
- }
126
- else {
127
- throw err;
128
- }
129
- });
130
- const publishAssets = async (assets) => {
131
- if (!assets?.length) {
132
- logger_1.logger.info('No assets to publish, skipped!');
133
- return;
134
- }
135
- const context = (0, context_1.getContext)();
136
- const bucketName = assets[0].bucketName;
137
- const client = new ali_oss_1.default({
138
- region: `oss-${context.region}`,
139
- accessKeyId: context.accessKeyId,
140
- accessKeySecret: context.accessKeySecret,
141
- bucket: bucketName,
142
- timeout: 600000, // 10 minutes
143
- });
144
- await ensureBucketExits(bucketName, client);
145
- const headers = {
146
- 'x-oss-storage-class': 'Standard',
147
- 'x-oss-object-acl': 'default',
148
- 'x-oss-forbid-overwrite': 'false',
149
- };
150
- await Promise.all(assets.map(async ({ source, objectKey }) => {
151
- await client.put(objectKey, node_path_1.default.normalize(source), { headers });
152
- logger_1.logger.debug(`Upload file: ${source} to bucket: ${bucketName} successfully!`);
153
- }));
154
- return bucketName;
155
- };
156
- exports.publishAssets = publishAssets;
157
- const cleanupAssets = async (assets) => {
158
- if (!assets?.length) {
159
- logger_1.logger.info('No assets to cleanup, skipped!');
160
- return;
161
- }
162
- const context = (0, context_1.getContext)();
163
- const bucketName = assets[0].bucketName;
164
- const client = new ali_oss_1.default({
165
- region: `oss-${context.region}`,
166
- accessKeyId: context.accessKeyId,
167
- accessKeySecret: context.accessKeySecret,
168
- bucket: bucketName,
169
- });
170
- await Promise.all(assets.map(async ({ objectKey }) => {
171
- await client.delete(objectKey);
172
- logger_1.logger.debug(`Cleanup file: ${objectKey} from bucket: ${bucketName} successfully!`);
173
- }));
174
- // delete the bucket
175
- await client.deleteBucket(bucketName);
176
- logger_1.logger.debug(`Cleanup bucket: ${bucketName} successfully!`);
177
- };
178
- exports.cleanupAssets = cleanupAssets;