@geek-fun/serverlessinsight 0.0.5 → 0.1.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 (59) hide show
  1. package/dist/package.json +22 -14
  2. package/dist/src/commands/deploy.js +2 -1
  3. package/dist/src/commands/destroy.js +10 -0
  4. package/dist/src/commands/index.js +28 -13
  5. package/dist/src/commands/template.js +26 -0
  6. package/dist/src/commands/validate.js +4 -5
  7. package/dist/src/common/actionContext.js +13 -7
  8. package/dist/src/common/constants.js +4 -0
  9. package/dist/src/common/iacHelper.js +53 -13
  10. package/dist/src/common/imsClient.js +52 -0
  11. package/dist/src/common/index.d.ts +3 -1
  12. package/dist/src/common/index.js +3 -1
  13. package/dist/src/common/providerEnum.js +11 -0
  14. package/dist/src/common/rosClient.js +92 -9
  15. package/dist/src/parser/databaseParser.js +27 -0
  16. package/dist/src/parser/eventParser.js +15 -0
  17. package/dist/src/parser/functionParser.js +20 -0
  18. package/dist/src/parser/index.d.ts +2 -0
  19. package/dist/src/parser/index.js +36 -0
  20. package/dist/src/parser/tagParser.js +10 -0
  21. package/dist/src/stack/deploy.js +57 -13
  22. package/dist/src/stack/index.d.ts +0 -2
  23. package/dist/src/stack/index.js +0 -16
  24. package/dist/src/stack/rfsStack/function.js +32 -0
  25. package/dist/src/stack/rfsStack/index.d.ts +9 -0
  26. package/dist/src/stack/rfsStack/index.js +39 -0
  27. package/dist/src/stack/rosStack/bootstrap.js +40 -0
  28. package/dist/src/stack/rosStack/database.js +78 -0
  29. package/dist/src/stack/rosStack/event.js +127 -0
  30. package/dist/src/stack/rosStack/function.js +88 -0
  31. package/dist/src/stack/rosStack/index.d.ts +7 -0
  32. package/dist/src/stack/rosStack/index.js +69 -0
  33. package/dist/src/stack/rosStack/rosStack.js +1 -0
  34. package/dist/src/stack/rosStack/stage.js +46 -0
  35. package/dist/src/stack/rosStack/tag.js +11 -0
  36. package/dist/src/stack/rosStack/vars.js +49 -0
  37. package/dist/src/types/assets.js +2 -0
  38. package/dist/src/types/domains/context.js +8 -0
  39. package/dist/src/types/domains/database.js +12 -0
  40. package/dist/src/types/domains/function.js +2 -0
  41. package/dist/src/types/domains/provider.js +2 -0
  42. package/dist/src/types/domains/tag.js +2 -0
  43. package/dist/src/types/domains/vars.js +2 -0
  44. package/dist/src/types/index.d.ts +44 -0
  45. package/dist/src/types/index.js +23 -0
  46. package/dist/src/validator/databaseSchema.js +41 -0
  47. package/dist/src/validator/eventSchema.js +41 -0
  48. package/dist/src/validator/functionSchema.js +45 -0
  49. package/dist/src/validator/iacSchema.js +42 -0
  50. package/dist/src/validator/index.d.ts +1 -0
  51. package/dist/src/validator/index.js +5 -0
  52. package/dist/src/validator/rootSchema.js +52 -0
  53. package/dist/tsconfig.tsbuildinfo +1 -1
  54. package/package.json +22 -14
  55. package/dist/src/common/provider.js +0 -11
  56. package/dist/src/stack/iacSchema.js +0 -145
  57. package/dist/src/stack/iacStack.js +0 -146
  58. package/dist/src/stack/parse.js +0 -46
  59. /package/dist/src/{types.js → types/domains/event.js} +0 -0
@@ -1,145 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.validateYaml = void 0;
7
- const ajv_1 = __importDefault(require("ajv"));
8
- const common_1 = require("../common");
9
- const ajv = new ajv_1.default({ allowUnionTypes: true, strict: false, allErrors: true });
10
- const schema = {
11
- type: 'object',
12
- properties: {
13
- version: { type: 'string', enum: ['0.0.0', '0.0.1'] },
14
- provider: { type: 'string', enum: ['aliyun', 'huawei'] },
15
- service: { type: 'string' },
16
- vars: {
17
- type: 'object',
18
- additionalProperties: {
19
- type: ['string', 'number', 'boolean'],
20
- },
21
- },
22
- stages: {
23
- type: 'object',
24
- patternProperties: {
25
- '.*': {
26
- type: 'object',
27
- additionalProperties: {
28
- type: ['string', 'number', 'boolean'],
29
- },
30
- },
31
- },
32
- },
33
- tags: {
34
- type: 'object',
35
- additionalProperties: {
36
- type: ['string', 'number', 'boolean'],
37
- },
38
- },
39
- functions: {
40
- type: 'object',
41
- patternProperties: {
42
- '.*': {
43
- type: 'object',
44
- properties: {
45
- name: { type: 'string' },
46
- runtime: {
47
- type: 'string',
48
- enum: [
49
- 'nodejs20',
50
- 'nodejs18',
51
- 'nodejs16',
52
- 'nodejs14',
53
- 'nodejs12',
54
- 'nodejs10',
55
- 'nodejs8',
56
- 'python3.10',
57
- 'python3.9',
58
- 'python3',
59
- 'PHP 7.2',
60
- 'Java 11',
61
- '.NET Core 3.1',
62
- 'Go 1.x',
63
- ],
64
- },
65
- handler: { type: 'string' },
66
- code: { type: 'string' },
67
- memory: { type: 'number' },
68
- timeout: { type: 'number' },
69
- environment: {
70
- type: 'object',
71
- additionalProperties: {
72
- type: ['string', 'number', 'boolean'],
73
- },
74
- },
75
- },
76
- additionalProperties: false,
77
- },
78
- },
79
- },
80
- events: {
81
- type: 'object',
82
- patternProperties: {
83
- '.*': {
84
- type: 'object',
85
- properties: {
86
- name: { type: 'string' },
87
- type: { type: 'string', enum: ['API_GATEWAY'] },
88
- triggers: {
89
- type: 'array',
90
- items: {
91
- method: { type: 'string', enum: ['GET', 'POST', 'PUT', 'DELETE', 'ANY'] },
92
- path: { type: 'string' },
93
- backend: { type: 'string' },
94
- required: ['method', 'path', 'backend'],
95
- },
96
- },
97
- custom_domain: {
98
- type: 'object',
99
- properties: {
100
- domain_name: { type: 'string' },
101
- certificate_name: { type: 'string' },
102
- certificate_body: { type: 'string' },
103
- certificate_private_key: { type: 'string' },
104
- },
105
- required: [
106
- 'domain_name',
107
- 'certificate_name',
108
- 'certificate_body',
109
- 'certificate_private_key',
110
- ],
111
- },
112
- },
113
- required: ['name', 'type', 'triggers'],
114
- },
115
- },
116
- },
117
- },
118
- required: ['version', 'provider', 'service'],
119
- additionalProperties: false,
120
- };
121
- class IacSchemaErrors extends Error {
122
- constructor(errors) {
123
- super(`Invalid yaml`);
124
- this.schemaErrors = errors.map((error) => ({
125
- instancePath: error.instancePath,
126
- schemaPath: error.schemaPath,
127
- message: error.message,
128
- allowedValues: error.params?.allowedValues,
129
- type: error.keyword,
130
- }));
131
- }
132
- get errors() {
133
- return this.schemaErrors;
134
- }
135
- }
136
- const validateYaml = (iacJson) => {
137
- const validate = ajv.compile(schema);
138
- const valid = validate(iacJson);
139
- if (!valid) {
140
- common_1.logger.debug(`Invalid yaml: ${JSON.stringify(validate.errors)}`);
141
- throw new IacSchemaErrors(validate.errors);
142
- }
143
- return true;
144
- };
145
- exports.validateYaml = validateYaml;
@@ -1,146 +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 (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.IacStack = void 0;
27
- const ros = __importStar(require("@alicloud/ros-cdk-core"));
28
- const ros_cdk_core_1 = require("@alicloud/ros-cdk-core");
29
- const types_1 = require("../types");
30
- const fc = __importStar(require("@alicloud/ros-cdk-fc3"));
31
- const ram = __importStar(require("@alicloud/ros-cdk-ram"));
32
- const agw = __importStar(require("@alicloud/ros-cdk-apigateway"));
33
- const common_1 = require("../common");
34
- class IacStack extends ros.Stack {
35
- constructor(scope, iac, context) {
36
- super(scope, iac.service, {
37
- stackName: context.stackName,
38
- tags: iac.tags.reduce((acc, tag) => {
39
- acc[tag.key] = (0, common_1.replaceVars)(tag.value, context.stage);
40
- return acc;
41
- }, {}),
42
- });
43
- // Define Parameters
44
- Object.entries(iac.vars).map(([key, value]) => new ros.RosParameter(this, key, {
45
- type: ros_cdk_core_1.RosParameterType.STRING,
46
- defaultValue: value,
47
- }));
48
- // Define Mappings
49
- new ros.RosMapping(this, 'stages', { mapping: (0, common_1.replaceVars)(iac.stages, context.stage) });
50
- new ros.RosInfo(this, ros.RosInfo.description, (0, common_1.replaceVars)(`${iac.service} stack`, context.stage));
51
- iac.functions.forEach((fnc) => {
52
- new fc.RosFunction(this, fnc.key, {
53
- functionName: (0, common_1.replaceVars)(fnc.name, context.stage),
54
- handler: (0, common_1.replaceVars)(fnc.handler, context.stage),
55
- runtime: (0, common_1.replaceVars)(fnc.runtime, context.stage),
56
- memorySize: (0, common_1.replaceVars)(fnc.memory, context.stage),
57
- timeout: (0, common_1.replaceVars)(fnc.timeout, context.stage),
58
- environmentVariables: (0, common_1.replaceVars)(fnc.environment, context.stage),
59
- code: {
60
- zipFile: (0, common_1.resolveCode)(fnc.code),
61
- },
62
- }, true);
63
- });
64
- const apiGateway = iac.events?.filter((event) => event.type === types_1.EventTypes.API_GATEWAY);
65
- if (apiGateway?.length) {
66
- const gatewayAccessRole = new ram.RosRole(this, (0, common_1.replaceVars)(`${iac.service}_role`, context.stage), {
67
- roleName: (0, common_1.replaceVars)(`${iac.service}-gateway-access-role`, context.stage),
68
- description: (0, common_1.replaceVars)(`${iac.service} role`, context.stage),
69
- assumeRolePolicyDocument: {
70
- version: '1',
71
- statement: [
72
- {
73
- action: 'sts:AssumeRole',
74
- effect: 'Allow',
75
- principal: {
76
- service: ['apigateway.aliyuncs.com'],
77
- },
78
- },
79
- ],
80
- },
81
- policies: [
82
- {
83
- policyName: (0, common_1.replaceVars)(`${iac.service}-policy`, context.stage),
84
- policyDocument: {
85
- version: '1',
86
- statement: [
87
- {
88
- action: ['fc:InvokeFunction'],
89
- effect: 'Allow',
90
- // @TODO implement at least permission granting
91
- resource: ['*'],
92
- },
93
- ],
94
- },
95
- },
96
- ],
97
- }, true);
98
- const apiGatewayGroup = new agw.RosGroup(this, (0, common_1.replaceVars)(`${iac.service}_apigroup`, context.stage), {
99
- groupName: (0, common_1.replaceVars)(`${iac.service}_apigroup`, context.stage),
100
- tags: (0, common_1.replaceVars)(iac.tags, context.stage),
101
- }, true);
102
- // new agw.RosCustomDomain(
103
- // this,
104
- // 'customDomain',
105
- // {
106
- // domainName: 'example.com',
107
- // certificateName: 'example.com',
108
- // certificateBody: 'example.com',
109
- // certificatePrivateKey: 'example.com',
110
- // groupId: apiGatewayGroup.attrGroupId,
111
- // },
112
- // true,
113
- // );
114
- apiGateway.forEach((event) => {
115
- event.triggers.forEach((trigger) => {
116
- const key = `${trigger.method}_${trigger.path}`.toLowerCase().replace(/\//g, '_');
117
- const api = new agw.RosApi(this, (0, common_1.replaceVars)(`${event.key}_api_${key}`, context.stage), {
118
- apiName: (0, common_1.replaceVars)(`${event.name}_api_${key}`, context.stage),
119
- groupId: apiGatewayGroup.attrGroupId,
120
- visibility: 'PRIVATE',
121
- requestConfig: {
122
- requestProtocol: 'HTTP',
123
- requestHttpMethod: (0, common_1.replaceVars)(trigger.method, context.stage),
124
- requestPath: (0, common_1.replaceVars)(trigger.path, context.stage),
125
- requestMode: 'PASSTHROUGH',
126
- },
127
- serviceConfig: {
128
- serviceProtocol: 'FunctionCompute',
129
- functionComputeConfig: {
130
- fcRegionId: context.region,
131
- functionName: trigger.backend,
132
- roleArn: gatewayAccessRole.attrArn,
133
- fcVersion: '3.0',
134
- },
135
- },
136
- resultSample: 'ServerlessInsight resultSample',
137
- resultType: 'JSON',
138
- tags: (0, common_1.replaceVars)(iac.tags, context.stage),
139
- }, true);
140
- api.addDependsOn(apiGatewayGroup);
141
- });
142
- });
143
- }
144
- }
145
- }
146
- exports.IacStack = IacStack;
@@ -1,46 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseYaml = void 0;
4
- const yaml_1 = require("yaml");
5
- const node_fs_1 = require("node:fs");
6
- const iacSchema_1 = require("./iacSchema");
7
- const mapToArr = (obj) => {
8
- if (!obj) {
9
- return [];
10
- }
11
- return Object.entries(obj)?.map(([key, value]) => typeof value === 'string' ? { [key]: value } : { key, ...value });
12
- };
13
- const mapToKvArr = (obj) => {
14
- if (!obj) {
15
- return [];
16
- }
17
- return Object.entries(obj)?.map(([key, value]) => ({ key, value }));
18
- };
19
- const validateExistence = (path) => {
20
- if (!(0, node_fs_1.existsSync)(path)) {
21
- throw new Error(`File does not exist at path: ${path}`);
22
- }
23
- };
24
- const transformYaml = (iacJson) => {
25
- return {
26
- service: iacJson.service,
27
- version: iacJson.version,
28
- provider: iacJson.provider,
29
- vars: iacJson.vars,
30
- stages: iacJson.stages,
31
- functions: mapToArr(iacJson.functions),
32
- events: mapToArr(iacJson.events),
33
- tags: [
34
- { key: 'iac-provider', value: 'ServerlessInsight' },
35
- ...mapToKvArr(iacJson.tags),
36
- ],
37
- };
38
- };
39
- const parseYaml = (yamlPath) => {
40
- validateExistence(yamlPath);
41
- const yamlContent = (0, node_fs_1.readFileSync)(yamlPath, 'utf8');
42
- const iacJson = (0, yaml_1.parse)(yamlContent);
43
- (0, iacSchema_1.validateYaml)(iacJson);
44
- return transformYaml(iacJson);
45
- };
46
- exports.parseYaml = parseYaml;
File without changes