@byted-apaas/server-sdk-node 1.0.4 → 1.0.5-beta.6

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.
@@ -8,3 +8,6 @@ export declare const httpHeaderKeys: {
8
8
  export declare const httpStatusCodes: {
9
9
  redirect: number;
10
10
  };
11
+ export declare const triggerType: {
12
+ defaultTrigger: string;
13
+ };
@@ -2,7 +2,7 @@
2
2
  // Copyright 2022 ByteDance Ltd. and/or its affiliates
3
3
  // SPDX-License-Identifier: MIT
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.httpStatusCodes = exports.httpHeaderKeys = exports.notifyModel = void 0;
5
+ exports.triggerType = exports.httpStatusCodes = exports.httpHeaderKeys = exports.notifyModel = void 0;
6
6
  exports.notifyModel = {
7
7
  key: "ee.kunlun.cloudfunction.faas_buildin",
8
8
  };
@@ -13,3 +13,6 @@ exports.httpHeaderKeys = {
13
13
  exports.httpStatusCodes = {
14
14
  redirect: 302,
15
15
  };
16
+ exports.triggerType = {
17
+ defaultTrigger: "default-trigger"
18
+ };
@@ -4,7 +4,7 @@ import { Message } from "./msg/msg";
4
4
  import { IDBGetter } from "./db/db";
5
5
  import { Context as CommonContext } from '@byted-apaas/server-common-node';
6
6
  import { IMetaData } from "./metadata/metadata";
7
- export declare class Context<T, mt, cf, gv> extends CommonContext<T> implements IContext<T, mt, cf, gv> {
7
+ export declare class Context<T, mt, cf, gv> extends CommonContext implements IContext<T, mt, cf, gv> {
8
8
  /**
9
9
  * 操作数据库中的记录数据
10
10
  */
@@ -23,7 +23,7 @@ class Tasks {
23
23
  async createAsyncTask(apiName, params) {
24
24
  if (apiName && params) {
25
25
  // low_code 1.0
26
- if (utils.isOnlineDev()) { // TODO 区分 4.0 vs 5.0
26
+ if (utils.isOnlineDev()) {
27
27
  // 1.debug模式
28
28
  if (utils.isDebug()) {
29
29
  return await this.f(apiName).invoke(params);
package/hooks/api.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as common from "@byted-apaas/server-common-node";
2
- import { IGfCtx } from "@byted-apaas/server-common-node/context/IContext";
2
+ import { IFuCtx } from "@byted-apaas/server-common-node/context/IContext";
3
3
  import { Tasks } from "../context/tasks/tasks";
4
4
  import { DB } from "../context/db/impl/db";
5
5
  import { _Resources } from "../context/resources/impl/resources";
@@ -33,7 +33,7 @@ export declare function invokeMicroservice(): (apiName: string) => {
33
33
  */
34
34
  invokeAsync: (path: string, method?: string, params?: any) => Promise<any>;
35
35
  };
36
- export declare const db: DB<IGfCtx, import("../data").metadataMap>;
36
+ export declare const db: DB<IFuCtx, import("../data").metadataMap>;
37
37
  export declare const resources: _Resources;
38
38
  export declare const msg: Message;
39
39
  export declare const tasks: Tasks<unknown>;
package/hooks/api.js CHANGED
@@ -11,14 +11,12 @@ const resources_1 = require("../context/resources/impl/resources");
11
11
  const msg_1 = require("../context/msg/msg");
12
12
  const Request = require("../request/interface");
13
13
  const constants = require("../constants/constants");
14
- const utils_1 = require("@byted-apaas/server-common-node/utils/utils");
15
14
  const metadata_1 = require("../context/metadata/metadata");
16
15
  const nodeCls = require("node-cls");
17
16
  const nodeClsCtx = common.nodeClsCtx;
18
17
  const exceptions = common.exceptions;
19
18
  const utils = common.utils;
20
19
  const checkUtils = common.checkUtils;
21
- const functionSources = common.constants.functionSources;
22
20
  // 同步调用云函数
23
21
  function getFunctionSync(context, logger) {
24
22
  return (funcName) => {
@@ -79,7 +77,6 @@ function getFunctionSync(context, logger) {
79
77
  ctx.loopMasks = loopMasks;
80
78
  return await ctx.run(async () => {
81
79
  const dynCtx = nodeClsCtx.getDynCtx();
82
- dynCtx.source = functionSources.globalFunction;
83
80
  return await dynCtx.run(async () => {
84
81
  return await common.hooks.invoke(func, params, context, logger);
85
82
  });
@@ -89,29 +86,12 @@ function getFunctionSync(context, logger) {
89
86
  };
90
87
  }
91
88
  return {
89
+ // 多语言函数开发, 走远端
92
90
  invoke: async (params) => {
93
- let func = (0, utils_1.getRequireFunc)()(funcName);
94
- if (!func) {
95
- throw new exceptions.InvalidParamError(`The function (${funcName}) does not exist.`);
96
- }
97
- let loopMasks = [funcName];
98
- let reqCtx = nodeCls.get("loopCtx");
99
- if (reqCtx && reqCtx.loopMasks && Array.isArray(reqCtx.loopMasks)) {
100
- if (reqCtx.loopMasks.includes(funcName)) {
101
- throw new exceptions.InvalidParamError(`Task execution forms a loop.`);
102
- }
103
- loopMasks.push(...reqCtx.loopMasks);
104
- }
105
- // 修改 source 全局函数
106
- let ctx = nodeCls.create("loopCtx");
107
- ctx.loopMasks = loopMasks;
108
- return await ctx.run(async () => {
109
- const dynCtx = nodeClsCtx.getDynCtx();
110
- dynCtx.source = functionSources.globalFunction;
111
- return await dynCtx.run(async () => {
112
- return await common.hooks.invoke(func, params, context, logger);
113
- });
114
- });
91
+ return await Request.GetInstance().invokeFuncSync({
92
+ APIName: funcName,
93
+ isInvokeByAPIName: true
94
+ }, params);
115
95
  }
116
96
  };
117
97
  };
package/hooks/hooks.js CHANGED
@@ -6,7 +6,6 @@ const msg_1 = require("../context/msg/msg");
6
6
  const server_common_node_1 = require("@byted-apaas/server-common-node");
7
7
  const api = require("./api");
8
8
  const operator_1 = require("../kunlun/operator/impl/operator");
9
- const constants_1 = require("@byted-apaas/server-common-node/constants/constants");
10
9
  const db_1 = require("../context/db/impl/db");
11
10
  const common_1 = require("../request/common");
12
11
  /**
@@ -38,31 +37,12 @@ function pre(params, context, logger, requireFunc) {
38
37
  * @param requireFunc
39
38
  */
40
39
  function mountContext(context, logger, requireFunc) {
41
- const source = server_common_node_1.utils.getSource();
42
- // 1. 根据不同调用场景差异化初始化内容
43
- // - db 是否包含 currentObject,transaction 是否包含 currentObject
44
- // - global function 场景需要初始化 setRespHeader 和 RedirectLocation 方法,以及 method 属性
45
- switch (source) {
46
- case constants_1.functionSources.workflow:
47
- context.db = new db_1.DBWithCurrentObject(context.objectApiName);
48
- break;
49
- case constants_1.functionSources.globalFunction:
50
- context.db = new db_1.DBWithCurrentObject(null);
51
- // set response headers
52
- context.setResponseHeader = api.setRespHeader();
53
- context.setRedirectLocation = api.setRedirectLocation(context);
54
- // 暴露context.method属性
55
- context.method = server_common_node_1.utils.getHttpMethod();
56
- break;
57
- case constants_1.functionSources.recordAutomation:
58
- context.db = new db_1.DBWithCurrentObject(context.objectApiName);
59
- break;
60
- case constants_1.functionSources.scheduleAutomation:
61
- context.db = new db_1.DBWithCurrentObject(null);
62
- break;
63
- default:
64
- throw new server_common_node_1.exceptions.InvalidParamError(`This npm package does not contain this source ${source}`);
65
- }
40
+ // 1. 全量挂载
41
+ context.db = new db_1.DBWithCurrentObject(context.objectApiName);
42
+ // set response headers
43
+ context.setResponseHeader = api.setRespHeader();
44
+ context.setRedirectLocation = api.setRedirectLocation(context);
45
+ context.method = server_common_node_1.utils.getHttpMethod();
66
46
  // 2. 初始化 core 包中需要初始化的内容,因此强转为 IContext 类型
67
47
  // - function
68
48
  // - tasks
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byted-apaas/server-sdk-node",
3
- "version": "1.0.4",
3
+ "version": "1.0.5-beta.6",
4
4
  "description": "aPaaS Server SDK",
5
5
  "author": "zhouwexin <zhouwexin@bytedance.com>",
6
6
  "homepage": "",
@@ -12,7 +12,7 @@
12
12
  "pre-build": "rm -rf build && tsc"
13
13
  },
14
14
  "dependencies": {
15
- "@byted-apaas/server-common-node": "^1.0.4",
15
+ "@byted-apaas/server-common-node": "^1.0.7-beta.6",
16
16
  "@jorgeferrero/stream-to-buffer": "^2.0.6",
17
17
  "dayjs": "^1.9.6",
18
18
  "form-data": "^3.0.0",
@@ -37,6 +37,5 @@
37
37
  "ts-node/register"
38
38
  ],
39
39
  "timeout": 20000
40
- },
41
- "gitHead": "e2274551684797a45a4fa5e2168f1c54a7559c7e"
40
+ }
42
41
  }
@@ -1345,7 +1345,7 @@ async function invokeFuncSync(idOrName, params) {
1345
1345
  }
1346
1346
  param.Params = JSON.stringify(params);
1347
1347
  param.Context = JSON.stringify((0, common_1.getTriggerCtx)());
1348
- param.TriggerType = utils.getTriggerType();
1348
+ param.TriggerType = constants_1.triggerType.defaultTrigger;
1349
1349
  let resp;
1350
1350
  try {
1351
1351
  resp = await rpc.getInnerAPICli().InvokeFuncSync(ctx, param);
@@ -1380,7 +1380,7 @@ async function createAsyncTaskV1(idOrName, params) {
1380
1380
  }
1381
1381
  param.Params = JSON.stringify(params);
1382
1382
  param.Context = JSON.stringify((0, common_1.getTriggerCtx)());
1383
- param.TriggerType = utils.getTriggerType();
1383
+ param.TriggerType = constants_1.triggerType.defaultTrigger;
1384
1384
  let resp;
1385
1385
  try {
1386
1386
  resp = await rpc.getInnerAPICli().CreateAsyncTaskOld(ctx, param);
@@ -1399,7 +1399,7 @@ async function createAsyncTaskV2(APIName, params) {
1399
1399
  param.APIAlias = APIName;
1400
1400
  param.Params = JSON.stringify(params);
1401
1401
  param.Context = JSON.stringify((0, common_1.getTriggerCtx)());
1402
- param.TriggerType = utils.getTriggerType();
1402
+ param.TriggerType = constants_1.triggerType.defaultTrigger;
1403
1403
  param.Extra = (0, common_1.getDebugExtraInfo)();
1404
1404
  let resp;
1405
1405
  try {
@@ -852,7 +852,7 @@ async function invokeFuncSync(idOrName, params) {
852
852
  options.json = {
853
853
  "params": JSON.stringify(params),
854
854
  "context": JSON.stringify(getTriggerCtx()),
855
- "triggerType": utils.getTriggerType(),
855
+ "triggerType": constants_1.triggerType.defaultTrigger,
856
856
  };
857
857
  // 相等时,表示是新版本函数的场景
858
858
  if (idOrName.isInvokeByAPIName) {
@@ -877,7 +877,7 @@ async function createAsyncTaskV1(idOrName, params) {
877
877
  options.json = {
878
878
  "params": JSON.stringify(params),
879
879
  "context": JSON.stringify(getTriggerCtx()),
880
- "triggerType": utils.getTriggerType(),
880
+ "triggerType": constants_1.triggerType.defaultTrigger,
881
881
  };
882
882
  // 相等时,表示是新版本函数的场景
883
883
  if (idOrName.isInvokeByAPIName) {
@@ -914,7 +914,7 @@ async function createAsyncTaskV2(APIName, params) {
914
914
  "apiAlias": APIName,
915
915
  "params": JSON.stringify(params),
916
916
  "context": JSON.stringify(getTriggerCtx()),
917
- "triggerType": utils.getTriggerType(),
917
+ "triggerType": constants_1.triggerType.defaultTrigger,
918
918
  "extra": (0, common_1.getDebugExtraInfo)()
919
919
  };
920
920
  let res = await openapi.doRequest(null, urlPath, options);