clientlinker 13.1.3 → 14.0.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.
@@ -0,0 +1,16 @@
1
+ import { FlowRuntime } from './flow_runtime';
2
+ import type { ClientRuntime } from './client_runtime';
3
+ export declare class FlowsRuntime {
4
+ runtime: ClientRuntime;
5
+ runned: Array<FlowRuntime | null>;
6
+ lastRunner: FlowRuntime | null;
7
+ constructor(runtime: ClientRuntime);
8
+ getFlowRuntime(name: string): FlowRuntime | undefined;
9
+ run(): Promise<any>;
10
+ nextRunner(): FlowRuntime | undefined;
11
+ toJSON(): {
12
+ runned: Array<{
13
+ name: string;
14
+ }>;
15
+ };
16
+ }
@@ -0,0 +1,63 @@
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.FlowsRuntime = void 0;
7
+ const debug_1 = __importDefault(require("debug"));
8
+ const flow_runtime_1 = require("./flow_runtime");
9
+ const utils_1 = require("../utils");
10
+ const debug = (0, debug_1.default)('clientlinker:flows_runtime');
11
+ class FlowsRuntime {
12
+ constructor(runtime) {
13
+ this.runtime = runtime;
14
+ this.runned = [];
15
+ this.lastRunner = null;
16
+ }
17
+ getFlowRuntime(name) {
18
+ const list = this.runned;
19
+ let index = list.length;
20
+ while (index--) {
21
+ const item = list[index];
22
+ if (item &&
23
+ item.flow &&
24
+ item.flow.name == name) {
25
+ return item;
26
+ }
27
+ }
28
+ }
29
+ run() {
30
+ const runner = this.nextRunner();
31
+ if (!runner) {
32
+ const runtime = this.runtime;
33
+ debug('flow out: %s', runtime.action);
34
+ return Promise.reject((0, utils_1.newNotFoundError)('CLIENT NO FLOWS', runtime));
35
+ }
36
+ return runner.run();
37
+ }
38
+ nextRunner() {
39
+ let flow;
40
+ const client = this.runtime.client;
41
+ const clientFlows = client.options.flows;
42
+ for (let flowName, index = this.runned.length; (flowName = clientFlows[index]); index++) {
43
+ flow = client.linker.flows[flowName];
44
+ if (flow)
45
+ break;
46
+ this.runned.push(null);
47
+ }
48
+ if (!flow)
49
+ return;
50
+ const runner = new flow_runtime_1.FlowRuntime(flow, this);
51
+ this.runned.push(runner);
52
+ this.lastRunner = runner;
53
+ return runner;
54
+ }
55
+ toJSON() {
56
+ return {
57
+ runned: this.runned.map(item => {
58
+ return item.toJSON();
59
+ }),
60
+ };
61
+ }
62
+ }
63
+ exports.FlowsRuntime = FlowsRuntime;
@@ -0,0 +1,11 @@
1
+ import type { ClientRuntime } from './runtime/client_runtime';
2
+ export interface NotFoundError extends Error {
3
+ CLIENTLINKER_TYPE?: string;
4
+ }
5
+ export interface ParsedAction {
6
+ clientName: string;
7
+ method: string;
8
+ }
9
+ export declare function newNotFoundError(type: string, runtime: ClientRuntime): NotFoundError;
10
+ export declare const parseActionCache: Record<string, ParsedAction>;
11
+ export declare function parseAction(action: string): ParsedAction;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseActionCache = void 0;
4
+ exports.newNotFoundError = newNotFoundError;
5
+ exports.parseAction = parseAction;
6
+ function newNotFoundError(type, runtime) {
7
+ const err = new Error('CLIENTLINKER:NotFound,' + runtime.action);
8
+ err.CLIENTLINKER_TYPE = type;
9
+ return err;
10
+ }
11
+ exports.parseActionCache = {};
12
+ function parseAction(action) {
13
+ let cache = exports.parseActionCache[action];
14
+ if (!cache) {
15
+ const arr = action.split(/\.|:/);
16
+ const clientName = arr.shift();
17
+ cache = exports.parseActionCache[action] = {
18
+ clientName: clientName,
19
+ method: arr.join('.')
20
+ };
21
+ }
22
+ return cache;
23
+ }
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "clientlinker",
3
- "version": "13.1.3",
3
+ "version": "14.0.0",
4
4
  "description": "Linker all clients whether rpc, addon, http request, mock data, local file ...",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
- "test": "cross-env DEBUG=clientlinker* mocha test/test_*",
8
- "test-cov": "istanbul cover _mocha -- test/test_* --reporter dot",
9
- "test-ci": "istanbul cover _mocha --report lcovonly -- test/test_* --reporter dot"
8
+ "build": "tsc -p tsconfig.json",
9
+ "prepare": "npm run build",
10
+ "pretest": "npm run build",
11
+ "test": "cross-env DEBUG=clientlinker* jest"
10
12
  },
11
13
  "keywords": [
12
14
  "client",
@@ -17,12 +19,12 @@
17
19
  ],
18
20
  "repository": {
19
21
  "type": "git",
20
- "url": "git+ssh://git@github.com/igdea/node-clientlinker.git"
22
+ "url": "git+ssh://git@github.com/iGdea/node-clientlinker.git"
21
23
  },
22
24
  "bugs": {
23
- "url": "https://github.com/igdea/node-clientlinker/issues"
25
+ "url": "https://github.com/iGdea/node-clientlinker/issues"
24
26
  },
25
- "homepage": "https://github.com/igdea/node-clientlinker/tree/master/packages/clientlinker",
27
+ "homepage": "https://github.com/iGdea/node-clientlinker/tree/master/packages/clientlinker",
26
28
  "author": "Bacra <bacra.woo@gmail.com>",
27
29
  "license": "MIT",
28
30
  "engines": {
@@ -30,17 +32,16 @@
30
32
  },
31
33
  "dependencies": {
32
34
  "debug": "^4.3.1",
33
- "depd": "^2.0.0",
34
35
  "lodash": "^4.17.21"
35
36
  },
36
37
  "devDependencies": {
38
+ "@types/debug": "^4.1.13",
39
+ "@types/lodash": "^4.17.28",
40
+ "@types/node": "^20.19.43",
37
41
  "async-statistic": "^1.1.0",
38
- "clientlinker-flow-confighandler-test": "^3.1.2",
42
+ "clientlinker-flow-confighandler-test": "^4.0.0",
39
43
  "cross-env": "^7.0.3",
40
- "expect.js": "^0.3.1",
41
- "istanbul": "^0.4.5",
42
- "mkdirp": "^1.0.4",
43
- "mocha": "^8.4.0"
44
+ "mkdirp": "^1.0.4"
44
45
  },
45
- "gitHead": "262dd9e196105768d064d68aa8960d0764edf8b8"
46
+ "gitHead": "f243a5c6c8d90cd764a7536c42d54c48a34403b2"
46
47
  }
package/.istanbul.yml DELETED
@@ -1,7 +0,0 @@
1
- instrumentation:
2
- root: .
3
- extensions:
4
- - .js
5
- default-excludes: true
6
- excludes:
7
- - lib/deps/*
package/index.js DELETED
@@ -1,4 +0,0 @@
1
- exports = module.exports = require('./lib/main');
2
- exports.util = {
3
- parseAction: require('./lib/utils').parseAction
4
- };
package/lib/client.js DELETED
@@ -1,51 +0,0 @@
1
- const debug = require('debug')('clientlinker:client');
2
-
3
- class Client {
4
- constructor(name, linker, options) {
5
- this.name = name;
6
- this.linker = linker;
7
- this.options = options || {};
8
- // flow 会放一些cache在这里
9
- this.cache = {};
10
- }
11
-
12
- run(runtime) {
13
- return runtime.run();
14
- }
15
-
16
- async methods() {
17
- const clientFlows = this.options.flows;
18
- if (!clientFlows) return [];
19
-
20
- const promises = clientFlows.map(async flowName => {
21
- const flow = this.linker.flow(flowName);
22
- if (!flow) return;
23
-
24
- const list = await flow.methods(this);
25
-
26
- if (!list) {
27
- debug('no method inifo: %s', flowName);
28
- return;
29
- }
30
-
31
- return {
32
- flow: flow,
33
- methods: list
34
- };
35
- });
36
-
37
- const methodList = await Promise.all(promises);
38
- const map = {};
39
- methodList.forEach(item => {
40
- if (!item) return;
41
- item.methods.forEach(method => {
42
- const methodInfo = map[method] || (map[method] = []);
43
- methodInfo.push(item.flow);
44
- });
45
- });
46
-
47
- return map;
48
- }
49
- }
50
-
51
- exports.Client = Client;
package/lib/flow.js DELETED
@@ -1,19 +0,0 @@
1
- const debug = require('debug')('clientlinker:flow');
2
-
3
- class Flow {
4
- constructor(name) {
5
- this.name = name;
6
- }
7
-
8
- run(runtime, callback) {
9
- debug('skip by no handler: %s', this.name);
10
- return callback.next();
11
- }
12
-
13
- async methods() {
14
- debug('skip by no handler: %s', this.name);
15
- return [];
16
- }
17
- }
18
-
19
- exports.Flow = Flow;
package/lib/linker.js DELETED
@@ -1,196 +0,0 @@
1
- const _ = require('lodash');
2
- const debug = require('debug')('clientlinker:linker');
3
- const { Client } = require('./client');
4
- const { Flow } = require('./flow');
5
- const { ClientRuntime } = require('./runtime/client_runtime');
6
- const utils = require('./utils');
7
-
8
- class Linker {
9
- constructor(options) {
10
- this._clients = {};
11
- this.flows = {};
12
- // 包含初始化client的一些配置
13
- this.options = options || {};
14
- // 用来暴露最近一次执行runIn时,返回的runtime对象
15
- this.lastRuntime = null;
16
-
17
- // flow 可能会放一些缓存变量在这里
18
- this.cache = {};
19
- }
20
-
21
- client(clientName, options) {
22
- if (typeof clientName != 'string') {
23
- throw new Error(
24
- `CLIENTLINKER:ClientNameMustBeString,${clientName}`
25
- );
26
- }
27
- if (arguments.length == 1) {
28
- return this._clients[clientName];
29
- }
30
- if (typeof options != 'object') {
31
- throw new Error(
32
- `CLIENTLINKER:ClientOptionsMustBeObject,${clientName}`
33
- );
34
- }
35
-
36
- options = {
37
- ...this.options.defaults,
38
- ...options
39
- };
40
-
41
- if (!options.flows) options.flows = [];
42
- else options.flows = options.flows.slice();
43
-
44
- const client = new Client(clientName, this, options);
45
- this._clients[clientName] = client;
46
- debug('add client:%s', clientName);
47
-
48
- return client;
49
- }
50
-
51
- /**
52
- * 注册/获取flower
53
- *
54
- * @param {string} flowName
55
- * @param {function/Flow} handler
56
- * @returns {Flow}
57
- */
58
- flow(flowName, handler) {
59
- if (typeof flowName != 'string') {
60
- throw new Error(
61
- `'CLIENTLINKER:FlowNameMustBeString,${flowName}`
62
- );
63
- } else if (arguments.length == 1) {
64
- return this.flows[flowName];
65
- }
66
-
67
- let flow;
68
- if (handler instanceof Flow) {
69
- flow = handler;
70
- } else if (typeof handler !== 'function') {
71
- throw new Error(
72
- `CLIENTLINKER:FlowHandlerMustBeFunction,${flowName}`
73
- );
74
- } else {
75
- flow = new Flow(flowName);
76
- handler(flow, this);
77
- }
78
-
79
- if (typeof flow.run != 'function') {
80
- throw new Error(
81
- `CLIENTLINKER:Flow.runMustBeFunction,${flowName}`
82
- );
83
- }
84
-
85
- if (flow.methods && typeof flow.methods != 'function') {
86
- throw new Error(
87
- `CLIENTLINKER:Flow.methodsMustBeFunction,${flowName}`
88
- );
89
- }
90
-
91
- this.flows[flowName] = flow;
92
-
93
- return flow;
94
- }
95
-
96
- // 标准输入参数
97
- run(action, query, body, options) {
98
- return this.runIn(action, query, body, options);
99
- }
100
-
101
- // 相比run,多了env
102
- runIn(action, query, body, options, env) {
103
- const runtime = this._runtime(action, query, body, options, env);
104
- if (!runtime.client) {
105
- return Promise.reject(utils.newNotFoundError('NO CLIENT', runtime));
106
- }
107
-
108
- // 通过这种手段,同步情况下,暴露runtime
109
- // runtime保存着运行时的所有数据,方便进行调试
110
- this.lastRuntime = runtime;
111
-
112
- return runtime.run_();
113
- }
114
-
115
- runtime() {
116
- const runtime = this._runtime.apply(this, arguments);
117
-
118
- // 直接使用Promise包裹,比async性能好
119
- return runtime.client
120
- ? Promise.resolve(runtime)
121
- : Promise.reject(utils.newNotFoundError('NO CLIENT', runtime))
122
- }
123
-
124
- _runtime(action, query, body, options, env) {
125
- const data = this._parseAction(action);
126
-
127
- const runtime = new ClientRuntime(this, action, query, body, options);
128
- if (env) runtime.env = env;
129
- runtime.method = data.method;
130
- runtime.client = data.client;
131
-
132
- return runtime;
133
- }
134
-
135
- // 解析action
136
- async parseAction(action) {
137
- return this._parseAction(action);
138
- }
139
-
140
- _parseAction(action) {
141
- const info = utils.parseAction(action);
142
-
143
- return {
144
- client: this._clients[info.clientName],
145
- method: info.method
146
- };
147
- }
148
-
149
- // 罗列methods
150
- async methods() {
151
- const clients = this._clients;
152
-
153
- const promises = _.map(clients, async (client) => {
154
- const methodList = await client.methods();
155
- return {
156
- client: client,
157
- methods: methodList
158
- };
159
- });
160
-
161
- const list = await Promise.all(promises);
162
-
163
- // 整理
164
- const map = {};
165
- list.forEach(item => {
166
- map[item.client.name] = item;
167
- });
168
-
169
- return map;
170
- }
171
-
172
- async clients() {
173
- return this._clients;
174
- }
175
-
176
- async clearCache(clientName) {
177
- if (!clientName) this.cache = {};
178
-
179
- const clients = this._clients;
180
-
181
- if (clientName) {
182
- const client = clients[clientName];
183
- if (client) client.cache = {};
184
- } else {
185
- _.each(clients, item => {
186
- item.cache = {};
187
- });
188
- }
189
- }
190
- }
191
-
192
- exports.Linker = Linker;
193
-
194
-
195
- Linker.prototype.JSON = utils.JSON;
196
- Linker.prototype.version = require('../package.json').version;
package/lib/main.js DELETED
@@ -1,49 +0,0 @@
1
- const _ = require('lodash');
2
- const { Linker } = require('./linker');
3
- const debug = require('debug')('clientlinker');
4
- const deprecate = require('depd')('clientlinker');
5
-
6
- /**
7
- * [options]
8
- * flows
9
- * httpproxy
10
- * clients
11
- * defaults
12
- */
13
- function clientlinker(options) {
14
- options || (options = {});
15
- const defaults =
16
- options.defaults ||
17
- options.clientDefaultOptions ||
18
- (options.defaults = {});
19
- !defaults.flows &&
20
- options.flows &&
21
- (defaults.flows = options.flows.slice());
22
-
23
- const linker = new Linker(options);
24
- // client options
25
- if (options.clients) {
26
- _.each(options.clients, (handler, name) => {
27
- linker.client(name, handler);
28
- });
29
- }
30
-
31
- if (options.customFlows) {
32
- deprecate('`options.customFlows` will not be supported.');
33
-
34
- _.each(options.customFlows, (handler, name) => {
35
- linker.flow(name, flow => {
36
- flow.run = handler;
37
- if (handler.methods) flow.methods = handler.methods;
38
- });
39
- });
40
- }
41
-
42
- linker.clients().then(clients => {
43
- debug('init clients:%s', Object.keys(clients));
44
- });
45
-
46
- return linker;
47
- }
48
-
49
- exports = module.exports = clientlinker;
@@ -1,122 +0,0 @@
1
- const _ = require('lodash');
2
- const { FlowsRuntime } = require('./flows_runtime');
3
-
4
- class ClientRuntime {
5
- constructor(linker, action, query, body, options) {
6
- this.linker = linker;
7
- this.action = action;
8
- this.client = null;
9
- this.method = null;
10
-
11
- // 如果使用httpproxy,必须是可以被序列化的
12
- this.query = query;
13
- // 如果使用httpproxy,必须是可以被序列化的
14
- this.body = body;
15
- // 如果使用httpproxy,必须是可以被序列化的
16
- this.options = options;
17
- // 保存框架运行过程中的数据
18
- // 如果使用httpproxy,必须是可以被序列化的
19
- this.env = {};
20
-
21
- // 执行过程中额外生成的一些数据
22
- this._debugData = null;
23
-
24
- // 运行中的状态
25
- this.retry = [];
26
- this.lastTry = null;
27
- }
28
-
29
- run() {
30
- if (this.lastTry) this.retry = [];
31
- return this.run_();
32
- }
33
-
34
- run_(err) {
35
- const onetry = new FlowsRuntime(this);
36
- this.lastTry = onetry;
37
-
38
- this.retry.push(onetry);
39
-
40
- // 减少client linker的逻辑,将retry放到linker
41
- // 否则要绑定removeAllListner
42
- // this.linker.emit('retry', { runtime: this, error: err });
43
-
44
- let promise = onetry.run();
45
- const retry = this.options && this.options.retry || this.client.options.retry;
46
- if (this.retry.length < retry) {
47
- promise = promise.catch(() => this.run_(err));
48
- }
49
-
50
- return promise;
51
- }
52
-
53
- debug(key, val) {
54
- const data = this._debugData || (this._debugData = {});
55
- switch (arguments.length) {
56
- case 0:
57
- return data;
58
-
59
- case 1: {
60
- let arr = data[key];
61
- return arr && arr[arr.length - 1].value;
62
- }
63
-
64
- case 2:
65
- default: {
66
- let arr = data[key] || (data[key] = []);
67
- arr.push(new DebugData(val, this));
68
- }
69
- }
70
- }
71
-
72
- getLastRunner() {
73
- return this.lastTry && this.lastTry.lastRunner;
74
- }
75
-
76
- getFirstRunner() {
77
- const firstTry = this.retry[0];
78
- return firstTry && firstTry.runned[0];
79
- }
80
-
81
- toJSON() {
82
- const debugData = {};
83
- _.each(this._debugData, (vals, key) => {
84
- debugData[key] = vals.map(item => {
85
- return item.toJSON();
86
- });
87
- });
88
-
89
- return {
90
- action: this.action,
91
- query: this.query,
92
- body: this.body,
93
- options: this.options,
94
-
95
- client: this.client && this.client.name,
96
- env: this.env,
97
- debugData: debugData,
98
-
99
- retry: this.retry.map(item => {
100
- return item.toJSON();
101
- }),
102
- };
103
- }
104
- }
105
-
106
- exports.ClientRuntime = ClientRuntime;
107
-
108
- class DebugData {
109
- constructor(value, runtime) {
110
- this.value = value;
111
- this.retry = runtime.retry.length;
112
- this.runner = runtime.getLastRunner();
113
- }
114
-
115
- toJSON() {
116
- return {
117
- value: this.value,
118
- retry: this.retry,
119
- flow: this.runner.flow.name
120
- };
121
- }
122
- }
@@ -1,42 +0,0 @@
1
- const debug = require('debug')('clientlinker:flow_runtime');
2
- const utils = require('../utils');
3
-
4
- class FlowRuntime {
5
- constructor(flow, onetry) {
6
- this.flow = flow;
7
- this.onetry = onetry;
8
- this.runtime = onetry.runtime;
9
-
10
- this._nextRunnerPromise = null;
11
- }
12
-
13
- async run() {
14
- return this.flow.run(this.runtime, this);
15
- }
16
-
17
- next() {
18
- if (!this._nextRunnerPromise) {
19
- const nextRunner = this.onetry.nextRunner();
20
- if (nextRunner) {
21
- debug('nextRunner:%s', nextRunner.flow.name);
22
- this._nextRunnerPromise = nextRunner.run();
23
- } else {
24
- const runtime = this.runtime;
25
- debug('flow out: %s', runtime.action);
26
- return Promise.reject(utils.newNotFoundError('CLIENT FLOW OUT', runtime));
27
- }
28
- } else {
29
- debug('next handler run twice');
30
- }
31
-
32
- return this._nextRunnerPromise;
33
- }
34
-
35
- toJSON() {
36
- return {
37
- name: this.flow.name,
38
- };
39
- }
40
- }
41
-
42
- exports.FlowRuntime = FlowRuntime;