@byted-apaas/server-sdk-node 0.0.1-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/LICENSE +7 -0
  2. package/NOTICE +3 -0
  3. package/README.md +1 -0
  4. package/application/application.d.ts +27 -0
  5. package/application/application.js +2 -0
  6. package/application/application.ts +40 -0
  7. package/application/function/function.d.ts +14 -0
  8. package/application/function/function.js +21 -0
  9. package/application/function/function.ts +37 -0
  10. package/application/impl/common.d.ts +2 -0
  11. package/application/impl/common.js +50 -0
  12. package/application/impl/common.ts +50 -0
  13. package/application/impl/impl.d.ts +16 -0
  14. package/application/impl/impl.js +38 -0
  15. package/application/impl/impl.ts +48 -0
  16. package/constants/constants.d.ts +10 -0
  17. package/constants/constants.js +15 -0
  18. package/constants/constants.ts +15 -0
  19. package/context/context.d.ts +74 -0
  20. package/context/context.js +25 -0
  21. package/context/context.ts +130 -0
  22. package/context/db/db.d.ts +73 -0
  23. package/context/db/db.js +4 -0
  24. package/context/db/db.ts +80 -0
  25. package/context/db/impl/IObject.d.ts +251 -0
  26. package/context/db/impl/IObject.js +5 -0
  27. package/context/db/impl/IObject.ts +247 -0
  28. package/context/db/impl/db.d.ts +61 -0
  29. package/context/db/impl/db.js +86 -0
  30. package/context/db/impl/db.ts +95 -0
  31. package/context/db/impl/object.d.ts +92 -0
  32. package/context/db/impl/object.js +524 -0
  33. package/context/db/impl/object.ts +631 -0
  34. package/context/db/impl/oql/ioql.d.ts +6 -0
  35. package/context/db/impl/oql/ioql.js +4 -0
  36. package/context/db/impl/oql/ioql.ts +9 -0
  37. package/context/db/impl/oql/oql.d.ts +8 -0
  38. package/context/db/impl/oql/oql.js +21 -0
  39. package/context/db/impl/oql/oql.ts +25 -0
  40. package/context/db/impl/order.d.ts +9 -0
  41. package/context/db/impl/order.js +15 -0
  42. package/context/db/impl/order.ts +19 -0
  43. package/context/db/impl/propertiesStore.d.ts +8 -0
  44. package/context/db/impl/propertiesStore.js +29 -0
  45. package/context/db/impl/propertiesStore.ts +31 -0
  46. package/context/db/impl/queryBuilder.d.ts +29 -0
  47. package/context/db/impl/queryBuilder.js +78 -0
  48. package/context/db/impl/queryBuilder.ts +106 -0
  49. package/context/db/impl/transaction/index.d.ts +20 -0
  50. package/context/db/impl/transaction/index.js +277 -0
  51. package/context/db/impl/transaction/index.ts +340 -0
  52. package/context/db/impl/transaction/operation.d.ts +27 -0
  53. package/context/db/impl/transaction/operation.js +69 -0
  54. package/context/db/impl/transaction/operation.ts +72 -0
  55. package/context/db/impl/transaction.d.ts +67 -0
  56. package/context/db/impl/transaction.js +4 -0
  57. package/context/db/impl/transaction.ts +79 -0
  58. package/context/globalConfig/globalConfig.d.ts +7 -0
  59. package/context/globalConfig/globalConfig.js +16 -0
  60. package/context/globalConfig/globalConfig.ts +14 -0
  61. package/context/metadata/components/common.d.ts +6 -0
  62. package/context/metadata/components/common.js +17 -0
  63. package/context/metadata/components/common.ts +22 -0
  64. package/context/metadata/components/components.d.ts +12 -0
  65. package/context/metadata/components/components.js +19 -0
  66. package/context/metadata/components/components.ts +22 -0
  67. package/context/metadata/components/desktop/list.d.ts +16 -0
  68. package/context/metadata/components/desktop/list.js +402 -0
  69. package/context/metadata/components/desktop/list.ts +457 -0
  70. package/context/metadata/components/desktop/recordDetail.d.ts +11 -0
  71. package/context/metadata/components/desktop/recordDetail.js +53 -0
  72. package/context/metadata/components/desktop/recordDetail.ts +67 -0
  73. package/context/metadata/components/mobile/list.d.ts +24 -0
  74. package/context/metadata/components/mobile/list.js +145 -0
  75. package/context/metadata/components/mobile/list.ts +179 -0
  76. package/context/metadata/metadata.d.ts +61 -0
  77. package/context/metadata/metadata.js +103 -0
  78. package/context/metadata/metadata.ts +178 -0
  79. package/context/metadata/objects/fields.d.ts +130 -0
  80. package/context/metadata/objects/fields.js +334 -0
  81. package/context/metadata/objects/fields.ts +457 -0
  82. package/context/metadata/objects/fields.util.d.ts +40 -0
  83. package/context/metadata/objects/fields.util.js +319 -0
  84. package/context/metadata/objects/fields.util.ts +360 -0
  85. package/context/metadata/objects/objects.d.ts +11 -0
  86. package/context/metadata/objects/objects.js +20 -0
  87. package/context/metadata/objects/objects.ts +30 -0
  88. package/context/metadata/types/common.d.ts +6 -0
  89. package/context/metadata/types/common.js +9 -0
  90. package/context/metadata/types/common.ts +13 -0
  91. package/context/metadata/types/components.d.ts +90 -0
  92. package/context/metadata/types/components.js +4 -0
  93. package/context/metadata/types/components.ts +108 -0
  94. package/context/metadata/types/objects.d.ts +226 -0
  95. package/context/metadata/types/objects.js +4 -0
  96. package/context/metadata/types/objects.ts +254 -0
  97. package/context/msg/msg.d.ts +38 -0
  98. package/context/msg/msg.js +35 -0
  99. package/context/msg/msg.ts +52 -0
  100. package/context/resources/IResources.d.ts +66 -0
  101. package/context/resources/IResources.js +2 -0
  102. package/context/resources/IResources.ts +73 -0
  103. package/context/resources/impl/resources.d.ts +42 -0
  104. package/context/resources/impl/resources.js +150 -0
  105. package/context/resources/impl/resources.ts +173 -0
  106. package/context/tasks/tasks.d.ts +33 -0
  107. package/context/tasks/tasks.js +139 -0
  108. package/context/tasks/tasks.ts +159 -0
  109. package/data/index.d.ts +27 -0
  110. package/data/index.js +4 -0
  111. package/data/index.ts +30 -0
  112. package/hooks/api.d.ts +61 -0
  113. package/hooks/api.js +327 -0
  114. package/hooks/api.ts +341 -0
  115. package/hooks/hooks.d.ts +1 -0
  116. package/hooks/hooks.js +88 -0
  117. package/hooks/hooks.ts +100 -0
  118. package/kunlun/kunlun.d.ts +55 -0
  119. package/kunlun/kunlun.js +58 -0
  120. package/kunlun/kunlun.ts +72 -0
  121. package/kunlun/operator/IOperator.d.ts +158 -0
  122. package/kunlun/operator/IOperator.js +4 -0
  123. package/kunlun/operator/IOperator.ts +166 -0
  124. package/kunlun/operator/impl/expression.d.ts +47 -0
  125. package/kunlun/operator/impl/expression.js +56 -0
  126. package/kunlun/operator/impl/expression.ts +74 -0
  127. package/kunlun/operator/impl/logic.d.ts +50 -0
  128. package/kunlun/operator/impl/logic.js +509 -0
  129. package/kunlun/operator/impl/logic.ts +598 -0
  130. package/kunlun/operator/impl/logicV2.d.ts +58 -0
  131. package/kunlun/operator/impl/logicV2.js +200 -0
  132. package/kunlun/operator/impl/logicV2.ts +254 -0
  133. package/kunlun/operator/impl/operator.d.ts +179 -0
  134. package/kunlun/operator/impl/operator.js +222 -0
  135. package/kunlun/operator/impl/operator.ts +258 -0
  136. package/kunlun/operator/impl/operatorV2.d.ts +180 -0
  137. package/kunlun/operator/impl/operatorV2.js +216 -0
  138. package/kunlun/operator/impl/operatorV2.ts +249 -0
  139. package/lib/core.d.ts +4 -0
  140. package/lib/core.js +19 -0
  141. package/lib/core.ts +7 -0
  142. package/package.json +50 -0
  143. package/request/common.d.ts +13 -0
  144. package/request/common.js +87 -0
  145. package/request/common.ts +91 -0
  146. package/request/constants.d.ts +10 -0
  147. package/request/constants.js +25 -0
  148. package/request/constants.ts +25 -0
  149. package/request/faasinfra.d.ts +8 -0
  150. package/request/faasinfra.js +358 -0
  151. package/request/faasinfra.ts +393 -0
  152. package/request/innerapi.d.ts +67 -0
  153. package/request/innerapi.js +1412 -0
  154. package/request/innerapi.ts +1648 -0
  155. package/request/interface.d.ts +95 -0
  156. package/request/interface.js +41 -0
  157. package/request/interface.ts +169 -0
  158. package/request/openapi.d.ts +69 -0
  159. package/request/openapi.js +1014 -0
  160. package/request/openapi.ts +1223 -0
  161. package/types/types.d.ts +79 -0
  162. package/types/types.js +4 -0
  163. package/types/types.ts +108 -0
@@ -0,0 +1,91 @@
1
+ import {headers} from "@byted-apaas/server-common-node/constants/constants";
2
+ import * as common from "@byted-apaas/server-common-node";
3
+ import {GetInstance} from "./interface";
4
+ import {AppCtx} from "../application/application";
5
+ import {AppCredential} from "@byted-apaas/server-common-node/credential/credential";
6
+ import {InvalidParamError} from "@byted-apaas/server-common-node/utils/exceptions";
7
+ import {runCtxForOpenSDK} from "../application/impl/common";
8
+
9
+ const utils = common.utils;
10
+ const checkUtils = common.checkUtils;
11
+ const exceptions = common.exceptions;
12
+ const nodeCls = require("node-cls");
13
+
14
+
15
+ function checkLoopCtx(reqCtx: any): any {
16
+ return reqCtx && reqCtx.loopMasks && Array.isArray(reqCtx.loopMasks)
17
+ }
18
+
19
+ export function getDebugExtraInfo() {
20
+ let extra: Record<string, string> = {
21
+ "FunctionFrameVersion": "FunctionFrameV2",
22
+ };
23
+ if (utils.isDebug()) {
24
+ extra.DebugInfo = utils.getDebugCode();
25
+ }
26
+
27
+ return extra
28
+ }
29
+
30
+ export function getTriggerCtx(): any {
31
+ let triggerCtx: Record<string, any> = {}, taskID = utils.getTriggerTaskID();
32
+ if (taskID) {
33
+ triggerCtx[headers.triggerTaskId] = taskID;
34
+ }
35
+
36
+ let temp = utils.getDistributedHandleMask();
37
+ if (temp) {
38
+ triggerCtx[headers.distributedHandleMask] = temp;
39
+ }
40
+
41
+ let reqCtx = nodeCls.get(headers.loopCtxKey);
42
+ if (checkLoopCtx(reqCtx)) {
43
+ triggerCtx[headers.loopMasks] = reqCtx.loopMasks
44
+ }
45
+ return triggerCtx;
46
+ }
47
+
48
+ export function transMapToFlowVariable(params: Record<string, any>): { api_name: string, value: any }[] {
49
+ let variables: { api_name: string, value: any }[] = [];
50
+ if (params) {
51
+ if (checkUtils.isNotObject(params)) {
52
+ throw new exceptions.InvalidParamError(`params should be object, and not array`);
53
+ }
54
+ for (const [k, v] of Object.entries(params)) {
55
+ variables.push({api_name: k, value: v});
56
+ }
57
+ }
58
+ return variables;
59
+ }
60
+
61
+ export function transFlowVariableToMap(variables: { api_name: string, value: any }[]): Record<string, any> {
62
+ let data: Record<string, any> = {};
63
+ if (variables && Array.isArray(variables)) {
64
+ for (const o of variables) {
65
+ data[o.api_name] = o.value
66
+ }
67
+ }
68
+ return data;
69
+ }
70
+
71
+ export async function getTenantInfo(appCtx: AppCtx): Promise<any> {
72
+ if (!appCtx) {
73
+ appCtx = {
74
+ mode: "faasSDK",
75
+ credential: new AppCredential(utils.getClientId(), utils.getClientSecret(), true)
76
+ }
77
+ }
78
+ if (!appCtx.credential) {
79
+ throw new InvalidParamError(`credential is empty`)
80
+ }
81
+ return await runCtxForOpenSDK(appCtx, async () => {
82
+ return await GetInstance().getTenantInfo(appCtx);
83
+ });
84
+ }
85
+
86
+ export async function getNamespaceForOpenAndFaaSSDK(): Promise<string> {
87
+ if (utils.isOpenSDKMode()) { // TODO check need assign?
88
+ return (await utils.getCredential().getTenantInfo()).namespace;
89
+ }
90
+ return utils.getNamespaceFromCtx();
91
+ }
@@ -0,0 +1,10 @@
1
+ import { GetHttpConfig, PostHttpConfig } from "@byted-apaas/server-common-node/http/config";
2
+ export declare const metricsMethodKeys: Record<string, string>;
3
+ export declare const openapiHttpPath: {
4
+ getBatchAttachmentToken: PostHttpConfig;
5
+ oql: PostHttpConfig;
6
+ getExecutionUserTaskInfo: GetHttpConfig;
7
+ executeFlow: PostHttpConfig;
8
+ revokeExecution: PostHttpConfig;
9
+ getExecutionInfo: GetHttpConfig;
10
+ };
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.openapiHttpPath = exports.metricsMethodKeys = void 0;
4
+ const config_1 = require("@byted-apaas/server-common-node/http/config");
5
+ const constants_1 = require("@byted-apaas/server-common-node/http/constants");
6
+ exports.metricsMethodKeys = {
7
+ // openapi
8
+ // file upload & download
9
+ getBatchAttachmentToken: "openapi_getBatchAttachmentToken",
10
+ oql: "openapi_oql",
11
+ // flow
12
+ getExecutionUserTaskInfo: "openapi_getExecutionUserTaskInfo",
13
+ getExecutionInfo: "openapi_getExecutionInfo",
14
+ revokeExecution: "openapi_revokeExecution",
15
+ executeFlow: "openapi_executeFlow",
16
+ };
17
+ exports.openapiHttpPath = {
18
+ getBatchAttachmentToken: new config_1.PostHttpConfig(`/attachment/v1/getBatchAttachmentToken`, exports.metricsMethodKeys.getBatchAttachmentToken),
19
+ oql: new config_1.PostHttpConfig(`/data/v5/namespaces/${constants_1.replaceKeys.namespace}/records/query`, exports.metricsMethodKeys.oql),
20
+ // flow
21
+ getExecutionUserTaskInfo: new config_1.GetHttpConfig(`/api/flow/v1/namespaces/${constants_1.replaceKeys.namespace}/executions/${constants_1.replaceKeys.id}/userTaskInfo`, exports.metricsMethodKeys.getExecutionUserTaskInfo),
22
+ executeFlow: new config_1.PostHttpConfig(`/api/flow/v1/namespaces/${constants_1.replaceKeys.namespace}/flows/${constants_1.replaceKeys.apiName}/execute`, exports.metricsMethodKeys.executeFlow),
23
+ revokeExecution: new config_1.PostHttpConfig(`/api/flow/v1/namespaces/${constants_1.replaceKeys.namespace}/executions/${constants_1.replaceKeys.id}/revoke`, exports.metricsMethodKeys.revokeExecution),
24
+ getExecutionInfo: new config_1.GetHttpConfig(`/api/flow/v1/namespaces/${constants_1.replaceKeys.namespace}/executions/${constants_1.replaceKeys.id}/detail`, exports.metricsMethodKeys.getExecutionInfo),
25
+ };
@@ -0,0 +1,25 @@
1
+ import {GetHttpConfig, PostHttpConfig} from "@byted-apaas/server-common-node/http/config";
2
+ import {replaceKeys} from "@byted-apaas/server-common-node/http/constants";
3
+
4
+ export const metricsMethodKeys: Record<string, string> = {
5
+ // openapi
6
+ // file upload & download
7
+ getBatchAttachmentToken: "openapi_getBatchAttachmentToken",
8
+ oql: "openapi_oql",
9
+ // flow
10
+ getExecutionUserTaskInfo: "openapi_getExecutionUserTaskInfo",
11
+ getExecutionInfo: "openapi_getExecutionInfo",
12
+ revokeExecution: "openapi_revokeExecution",
13
+ executeFlow: "openapi_executeFlow",
14
+ }
15
+
16
+ export const openapiHttpPath = {
17
+ getBatchAttachmentToken: new PostHttpConfig(`/attachment/v1/getBatchAttachmentToken`, metricsMethodKeys.getBatchAttachmentToken),
18
+ oql: new PostHttpConfig(`/data/v5/namespaces/${replaceKeys.namespace}/records/query`, metricsMethodKeys.oql),
19
+
20
+ // flow
21
+ getExecutionUserTaskInfo: new GetHttpConfig(`/api/flow/v1/namespaces/${replaceKeys.namespace}/executions/${replaceKeys.id}/userTaskInfo`, metricsMethodKeys.getExecutionUserTaskInfo),
22
+ executeFlow: new PostHttpConfig(`/api/flow/v1/namespaces/${replaceKeys.namespace}/flows/${replaceKeys.apiName}/execute`, metricsMethodKeys.executeFlow),
23
+ revokeExecution: new PostHttpConfig(`/api/flow/v1/namespaces/${replaceKeys.namespace}/executions/${replaceKeys.id}/revoke`, metricsMethodKeys.revokeExecution),
24
+ getExecutionInfo: new GetHttpConfig(`/api/flow/v1/namespaces/${replaceKeys.namespace}/executions/${replaceKeys.id}/detail`, metricsMethodKeys.getExecutionInfo),
25
+ }
@@ -0,0 +1,8 @@
1
+ import { IFaaSInfraRequest } from "./interface";
2
+ export declare class FaaSInfraRequest implements IFaaSInfraRequest {
3
+ constructor();
4
+ createDistributeTaskV1(dataset: any, handlerFunc: string, progressCallbackFunc: string, completedCallbackFunc: string, options: any): Promise<any>;
5
+ createDistributeTaskV2(dataset: any, handlerFunc: string, progressCallbackFunc: string, completedCallbackFunc: string, options: any): Promise<any>;
6
+ invokeMicroserviceSync(apiName: string, path: string, method: string, body: any): Promise<any>;
7
+ invokeMicroserviceAsync(apiName: string, path: string, method: string, body: any): Promise<any>;
8
+ }
@@ -0,0 +1,358 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FaaSInfraRequest = void 0;
4
+ const common = require("@byted-apaas/server-common-node");
5
+ const headers = common.constants.headers;
6
+ const commonHttp = common.http;
7
+ const faasinfra = common.http.faasinfra;
8
+ const faasinfraHttpPath = common.http.faasinfraHttpPath;
9
+ const replaceKeys = common.http.replaceKeys;
10
+ const exceptions = common.exceptions;
11
+ const utils = common.utils;
12
+ const nodeCls = require("node-cls");
13
+ const distributedHandleMask = headers.distributedHandleMask;
14
+ const loopMasks = headers.loopMasks;
15
+ const loopMasksKey = headers.loopMasksKey;
16
+ const loopCtxKey = headers.loopCtxKey;
17
+ const url_1 = require("url");
18
+ const exceptions_1 = require("@byted-apaas/server-common-node/utils/exceptions");
19
+ class FaaSInfraRequest {
20
+ constructor() {
21
+ }
22
+ async createDistributeTaskV1(dataset, handlerFunc, progressCallbackFunc, completedCallbackFunc, options) {
23
+ // 1.获取 options
24
+ let config = commonHttp.getOptions(null, faasinfraHttpPath.createDistributedTask);
25
+ // 2.构造url
26
+ let urlPath = config._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
27
+ // 3.请求
28
+ config.json = {
29
+ "domainName": utils.getTenantNameFromCtx(),
30
+ "namespace": utils.getNamespaceFromCtx(),
31
+ "userId": utils.getUserIDFromCtx(),
32
+ "dataset": dataset,
33
+ "handlerFunc": handlerFunc,
34
+ "progressCallbackFunc": progressCallbackFunc,
35
+ "completedCallbackFunc": completedCallbackFunc,
36
+ "options": options
37
+ };
38
+ let task_id = utils.getTriggerTaskID();
39
+ if (task_id) {
40
+ config.json[headers.triggerTaskId] = task_id;
41
+ }
42
+ let loopCtx = nodeCls.get(loopCtxKey);
43
+ if (checkLoopCtx(loopCtx)) {
44
+ config.json[loopMasksKey] = loopCtx.loopMasks;
45
+ if (!utils.isMicroservice() && (handlerFunc !== "" && config.json[loopMasksKey].includes(utils.handleFuncName({}, handlerFunc))) ||
46
+ (progressCallbackFunc != "" && config.json[loopMasksKey].includes(utils.handleFuncName({}, progressCallbackFunc))) ||
47
+ (completedCallbackFunc != "" && config.json[loopMasksKey].includes(utils.handleFuncName({}, completedCallbackFunc)))) {
48
+ throw new exceptions.InvalidParamError(`Task execution forms a loop.`);
49
+ }
50
+ }
51
+ return await faasinfra.doRequest(null, urlPath, config);
52
+ }
53
+ async createDistributeTaskV2(dataset, handlerFunc, progressCallbackFunc, completedCallbackFunc, options) {
54
+ // 检查函数是否存在
55
+ utils.requireFunction(handlerFunc);
56
+ // 1.获取 options
57
+ let config = commonHttp.getOptions(null, faasinfraHttpPath.createDistributedTaskV2);
58
+ // 2.构造url
59
+ let urlPath = config._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
60
+ // 3.请求
61
+ config.json = {
62
+ "domainName": utils.getTenantNameFromCtx(),
63
+ "namespace": utils.getNamespaceFromCtx(),
64
+ "userId": utils.getUserIDFromCtx(),
65
+ "dataset": dataset,
66
+ "handlerFunc": handlerFunc,
67
+ "progressCallbackFunc": progressCallbackFunc,
68
+ "completedCallbackFunc": completedCallbackFunc,
69
+ "options": options,
70
+ "extra": getDebugExtraInfo()
71
+ };
72
+ let task_id = utils.getTriggerTaskID();
73
+ if (task_id) {
74
+ config.json[headers.triggerTaskId] = task_id;
75
+ }
76
+ let loopCtx = nodeCls.get(loopCtxKey);
77
+ if (checkLoopCtx(loopCtx)) {
78
+ config.json[loopMasksKey] = loopCtx.loopMasks;
79
+ if ((handlerFunc !== "" && config.json[loopMasksKey].includes(handlerFunc)) ||
80
+ (progressCallbackFunc != "" && config.json[loopMasksKey].includes(progressCallbackFunc)) ||
81
+ (completedCallbackFunc != "" && config.json[loopMasksKey].includes(completedCallbackFunc))) {
82
+ throw new exceptions.InvalidParamError(`Task execution forms a loop.`);
83
+ }
84
+ }
85
+ return await faasinfra.doRequest(null, urlPath, config);
86
+ }
87
+ async invokeMicroserviceSync(apiName, path, method, body) {
88
+ let reqPath = faasinfraHttpPath.invokeMicroserviceSync;
89
+ let options = commonHttp.getOptions(null, reqPath);
90
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
91
+ options.json = {
92
+ "microserviceId": apiName,
93
+ "path": path,
94
+ "method": method,
95
+ "query": parseQueryParams(path),
96
+ };
97
+ // 处理 body 参数
98
+ if (method.toUpperCase() !== "GET") {
99
+ options.json.body = JSON.stringify(body);
100
+ }
101
+ return await faasinfra.doRequest(null, urlPath, options);
102
+ }
103
+ async invokeMicroserviceAsync(apiName, path, method, body) {
104
+ let reqPath = faasinfraHttpPath.invokeMicroserviceAsync;
105
+ let options = commonHttp.getOptions(null, reqPath);
106
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
107
+ options.json = {
108
+ "microserviceId": apiName,
109
+ "path": path,
110
+ "method": method,
111
+ "query": parseQueryParams(path),
112
+ };
113
+ // 处理 body 参数
114
+ if (method.toUpperCase() !== "GET") {
115
+ options.json.body = JSON.stringify(body);
116
+ }
117
+ return await faasinfra.doRequest(null, urlPath, options);
118
+ }
119
+ }
120
+ exports.FaaSInfraRequest = FaaSInfraRequest;
121
+ function getDebugExtraInfo() {
122
+ let extra = {
123
+ "FunctionFrameVersion": "FunctionFrameV2",
124
+ };
125
+ if (utils.isDebug()) {
126
+ extra.DebugInfo = utils.getDebugCode();
127
+ }
128
+ return extra;
129
+ }
130
+ function checkLoopCtx(reqCtx) {
131
+ return reqCtx && reqCtx.loopMasks && Array.isArray(reqCtx.loopMasks);
132
+ }
133
+ function parseQueryParams(path) {
134
+ let params = {};
135
+ let url = new url_1.URL(path, "https://aa.bb.cc");
136
+ for (let [k, v] of url.searchParams.entries()) {
137
+ if (typeof (k) !== "string") {
138
+ throw new exceptions_1.InternalError(`The type of k should be string, but ${typeof (k)}`);
139
+ }
140
+ if (typeof (v) !== "string") {
141
+ throw new exceptions_1.InternalError(`The type of v should be string, but ${typeof (v)}`);
142
+ }
143
+ params[k] = v.split(",");
144
+ }
145
+ if (Object.keys(params).length === 0) {
146
+ return undefined;
147
+ }
148
+ return params;
149
+ }
150
+ /*
151
+ export async function invokeFuncSync(apiName: string, params: any, oldFuncName: string, isSync: boolean) {
152
+ let result = await invokeFunc(apiName, params, oldFuncName, isSync);
153
+ if (result && result.code === "0") {
154
+ return result.data;
155
+ }
156
+ return result;
157
+ }
158
+
159
+ async function invokeFunc(apiName: string, params: any, oldFuncName: string, isSync = false) {
160
+ let reqPath = faasinfraHttpPath.createAsyncTask;
161
+ if (isSync) {
162
+ reqPath = faasinfraHttpPath.invokeFuncSync;
163
+ }
164
+
165
+ let options = commonHttp.getOptions(null, reqPath);
166
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
167
+
168
+ let triggerCtx: Record<string, any> = {};
169
+ let task_id = utils.getTriggerTaskID();
170
+ if (task_id) {
171
+ triggerCtx[headers.triggerTaskId] = task_id;
172
+ }
173
+
174
+ let temp = utils.getDistributedHandleMask();
175
+ if (temp) {
176
+ triggerCtx[distributedHandleMask] = temp;
177
+ }
178
+
179
+ let reqCtx = nodeCls.get(loopCtxKey);
180
+ if (checkLoopCtx(reqCtx)) {
181
+ triggerCtx[loopMasks] = reqCtx.loopMasks
182
+ }
183
+
184
+ // 相等时,表示是新版本函数的场景
185
+ if (apiName === oldFuncName) {
186
+ options.json = {
187
+ "apiAlias": apiName,
188
+ "params": JSON.stringify(params),
189
+ "context": JSON.stringify(triggerCtx),
190
+ "triggerType": utils.getTriggerType(),
191
+ }
192
+ } else {
193
+ options.json = {
194
+ "function_name": apiName,
195
+ "params": JSON.stringify(params),
196
+ "context": JSON.stringify(triggerCtx),
197
+ "triggerType": utils.getTriggerType(),
198
+ }
199
+ }
200
+
201
+ if (utils.isMicroservice()) {
202
+ options.json.apiAlias = oldFuncName;
203
+ }
204
+
205
+ return await faasinfra.doRequest(null, urlPath, options);
206
+ }
207
+
208
+ export async function asyncTask(apiName: string, params: any) {
209
+ // 检查函数是否存在
210
+ utils.requireFunction(apiName);
211
+
212
+ let reqPath = faasinfraHttpPath.createAsyncTaskV2;
213
+ let options = commonHttp.getOptions(null, reqPath);
214
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
215
+
216
+ let triggerCtx: Record<string, any> = {};
217
+ let task_id = utils.getTriggerTaskID();
218
+ if (task_id) {
219
+ triggerCtx[headers.triggerTaskId] = task_id;
220
+ }
221
+
222
+ let temp = utils.getDistributedHandleMask();
223
+ if (temp) {
224
+ triggerCtx[distributedHandleMask] = temp;
225
+ }
226
+
227
+ let reqCtx = nodeCls.get(loopCtxKey);
228
+ if (checkLoopCtx(reqCtx)) {
229
+ triggerCtx[loopMasks] = reqCtx.loopMasks
230
+ }
231
+
232
+ options.json = {
233
+ "api_name": apiName,
234
+ "params": JSON.stringify(params),
235
+ "context": JSON.stringify(triggerCtx),
236
+ "triggerType": utils.getTriggerType(),
237
+ "extra": getDebugExtraInfo()
238
+ }
239
+
240
+ return await faasinfra.doRequest(null, urlPath, options);
241
+ }
242
+
243
+ export async function createDistributedTaskV1(dataset: any, handlerFunc: string, progressCallbackFunc: string, completedCallbackFunc: string, options: _Options):
244
+ Promise<number> {
245
+ // 1.获取 options
246
+ let config = commonHttp.getOptions(null, faasinfraHttpPath.createDistributedTask);
247
+
248
+ // 2.构造url
249
+ let urlPath = config._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
250
+
251
+ // 3.请求
252
+ config.json = {
253
+ "domainName": utils.getTenantNameFromCtx(),
254
+ "namespace": utils.getNamespaceFromCtx(),
255
+ "userId": utils.getUserIDFromCtx(),
256
+
257
+ "dataset": dataset,
258
+ "handlerFunc": handlerFunc,
259
+ "progressCallbackFunc": progressCallbackFunc,
260
+ "completedCallbackFunc": completedCallbackFunc,
261
+ "options": options
262
+ }
263
+
264
+ let task_id = utils.getTriggerTaskID();
265
+ if (task_id) {
266
+ config.json[headers.triggerTaskId] = task_id;
267
+ }
268
+
269
+ let loopCtx = nodeCls.get(loopCtxKey);
270
+ if (checkLoopCtx(loopCtx)) {
271
+ config.json[loopMasksKey] = loopCtx.loopMasks;
272
+ if (!utils.isMicroservice() && (handlerFunc !== "" && config.json[loopMasksKey].includes(utils.handleFuncName({}, handlerFunc))) ||
273
+ (progressCallbackFunc != "" && config.json[loopMasksKey].includes(utils.handleFuncName({}, progressCallbackFunc))) ||
274
+ (completedCallbackFunc != "" && config.json[loopMasksKey].includes(utils.handleFuncName({}, completedCallbackFunc)))) {
275
+ throw new exceptions.InvalidParamError(`Task execution forms a loop.`);
276
+ }
277
+ }
278
+
279
+ return await faasinfra.doRequest(null, urlPath, config);
280
+ }
281
+
282
+ export async function createDistributedTask(dataset: any, handlerFunc: string, progressCallbackFunc: string, completedCallbackFunc: string, options: _Options): Promise<number> {
283
+ if (utils.isOnlineDev()) {
284
+ return await createDistributedTaskV1(dataset, handlerFunc, progressCallbackFunc, completedCallbackFunc, options);
285
+ }
286
+ // 检查函数是否存在
287
+ utils.requireFunction(handlerFunc);
288
+
289
+ // 1.获取 options
290
+ let config = commonHttp.getOptions(null, faasinfraHttpPath.createDistributedTaskV2);
291
+
292
+ // 2.构造url
293
+ let urlPath = config._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
294
+
295
+ // 3.请求
296
+ config.json = {
297
+ "domainName": utils.getTenantNameFromCtx(),
298
+ "namespace": utils.getNamespaceFromCtx(),
299
+ "userId": utils.getUserIDFromCtx(),
300
+
301
+ "dataset": dataset,
302
+ "handlerFunc": handlerFunc,
303
+ "progressCallbackFunc": progressCallbackFunc,
304
+ "completedCallbackFunc": completedCallbackFunc,
305
+ "options": options,
306
+ "extra": getDebugExtraInfo()
307
+ }
308
+
309
+ let task_id = utils.getTriggerTaskID();
310
+ if (task_id) {
311
+ config.json[headers.triggerTaskId] = task_id;
312
+ }
313
+
314
+ let loopCtx = nodeCls.get(loopCtxKey);
315
+ if (checkLoopCtx(loopCtx)) {
316
+ config.json[loopMasksKey] = loopCtx.loopMasks;
317
+ if ((handlerFunc !== "" && config.json[loopMasksKey].includes(handlerFunc)) ||
318
+ (progressCallbackFunc != "" && config.json[loopMasksKey].includes(progressCallbackFunc)) ||
319
+ (completedCallbackFunc != "" && config.json[loopMasksKey].includes(completedCallbackFunc))) {
320
+ throw new exceptions.InvalidParamError(`Task execution forms a loop.`);
321
+ }
322
+ }
323
+
324
+ return await faasinfra.doRequest(null, urlPath, config);
325
+ }
326
+
327
+ export async function invokeMicroserviceSync(apiName: string, path: string, method: string, body: any) {
328
+ return await invokeMicroservice(apiName, path, method, body, true);
329
+ }
330
+
331
+ export async function invokeMicroserviceAsync(apiName: string, path: string, method: string, body: any) {
332
+ return await invokeMicroservice(apiName, path, method, body, false);
333
+ }
334
+
335
+ async function invokeMicroservice(apiName: string, path: string, method: string, body: any, isSync = false) {
336
+ let reqPath = faasinfraHttpPath.invokeMicroserviceAsync;
337
+ if (isSync) {
338
+ reqPath = faasinfraHttpPath.invokeMicroserviceSync;
339
+ }
340
+
341
+ let options = commonHttp.getOptions(null, reqPath);
342
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, utils.getNamespaceFromCtx());
343
+
344
+ options.json = {
345
+ "microserviceId": apiName,
346
+ "path": path,
347
+ "method": method,
348
+ "query": parseQueryParams(path),
349
+ }
350
+
351
+ // 处理 body 参数
352
+ if (method.toUpperCase() !== "GET") {
353
+ options.json.body = JSON.stringify(body);
354
+ }
355
+
356
+ return await faasinfra.doRequest(null, urlPath, options);
357
+ }
358
+ */