clientlinker 13.1.4 → 14.0.1
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.
- package/README.md +38 -22
- package/dist/index.d.ts +8 -0
- package/dist/index.js +7 -0
- package/dist/lib/client.d.ts +18 -0
- package/dist/lib/client.js +51 -0
- package/dist/lib/flow.d.ts +11 -0
- package/dist/lib/flow.js +22 -0
- package/dist/lib/linker.d.ts +47 -0
- package/dist/lib/linker.js +157 -0
- package/dist/lib/main.d.ts +9 -0
- package/dist/lib/main.js +35 -0
- package/dist/lib/runtime/client_runtime.d.ts +37 -0
- package/dist/lib/runtime/client_runtime.js +98 -0
- package/dist/lib/runtime/flow_runtime.d.ts +15 -0
- package/dist/lib/runtime/flow_runtime.js +44 -0
- package/dist/lib/runtime/flows_runtime.d.ts +16 -0
- package/dist/lib/runtime/flows_runtime.js +65 -0
- package/dist/lib/utils.d.ts +11 -0
- package/dist/lib/utils.js +23 -0
- package/package.json +13 -9
- package/.istanbul.yml +0 -7
- package/index.js +0 -4
- package/lib/client.js +0 -51
- package/lib/flow.js +0 -19
- package/lib/linker.js +0 -196
- package/lib/main.js +0 -49
- package/lib/runtime/client_runtime.js +0 -122
- package/lib/runtime/flow_runtime.js +0 -42
- package/lib/runtime/flows_runtime.js +0 -83
- package/lib/utils.js +0 -23
package/README.md
CHANGED
|
@@ -54,14 +54,14 @@ npm install clientlinker --save
|
|
|
54
54
|
```javascript
|
|
55
55
|
// `clientlinker.conf.js` file content
|
|
56
56
|
|
|
57
|
-
var clientlinker = require('clientlinker');
|
|
57
|
+
var { clientlinker } = require('clientlinker');
|
|
58
58
|
var linker = clientlinker(options);
|
|
59
59
|
|
|
60
60
|
// Register flows
|
|
61
|
-
linker.flow('confighandler', require('clientlinker-flow-confighandler'));
|
|
62
|
-
linker.flow('logger', require('clientlinker-flow-logger'));
|
|
63
|
-
linker.flow('httpproxy', require('clientlinker-flow-httpproxy'));
|
|
64
|
-
linker.flow('pkghandler', require('clientlinker-flow-pkghandler'));
|
|
61
|
+
linker.flow('confighandler', require('clientlinker-flow-confighandler').confighandlerFlow);
|
|
62
|
+
linker.flow('logger', require('clientlinker-flow-logger').loggerFlow);
|
|
63
|
+
linker.flow('httpproxy', require('clientlinker-flow-httpproxy').httpproxyFlow);
|
|
64
|
+
linker.flow('pkghandler', require('clientlinker-flow-pkghandler').pkghandlerFlow);
|
|
65
65
|
|
|
66
66
|
// Add clients outsid of config step
|
|
67
67
|
linker.client(name, clientOptions);
|
|
@@ -82,15 +82,30 @@ linker.run('mail.read', userid, {mailid: 'xxxx'}, options)
|
|
|
82
82
|
|
|
83
83
|
## Upgrade
|
|
84
84
|
|
|
85
|
-
###
|
|
85
|
+
### 14.x
|
|
86
|
+
|
|
87
|
+
* `clientlinker` no longer exports a callable factory; the entry is now an object with named exports, and `clientlinker.util` is removed.
|
|
88
|
+
* Flow packages no longer export the installer function as the module itself; the old default export is replaced by named `*Flow` exports.
|
|
89
|
+
* `clientlinker-flow-httpproxy` no longer exports `.route`, `.route.express` or `.route.koa`; route helpers are named exports now.
|
|
90
|
+
* `clientlinker-flow-confighandler-test` no longer exports `.run` or `.flows.*`; `run` is now `runClientHandler` and the flows are named exports. Its entry requires `@jest/globals`.
|
|
91
|
+
* All packages now build to `dist/`; old deep paths such as `flow/*`, `lib/*` and `route/*` no longer exist, and deep modules are named exports.
|
|
92
|
+
* `src` is not published; packages include compiled JavaScript and `.d.ts` files.
|
|
93
|
+
* `options.customFlows` is removed; custom flows are only supported through `linker.flow`.
|
|
94
|
+
* `options.clientDefaultOptions` is removed; `options.defaults` is the only default-options key.
|
|
95
|
+
|
|
96
|
+
### 13.x
|
|
86
97
|
|
|
87
|
-
* remove `andToError`
|
|
88
|
-
* remove ext errinfo of `expandError`
|
|
89
98
|
* remove `callback` param of `clientlinker.run` and `clientlinker.runIn`
|
|
99
|
+
* `clientlinker.runIn` signature changed to `runIn(action, query, body, options, env)`; the old `runIn([action, query, body, callback, options], source, env)` is removed
|
|
90
100
|
* remove `retry` event
|
|
91
101
|
* remove `runtime.tmp`
|
|
92
102
|
|
|
93
|
-
###
|
|
103
|
+
### 12.x
|
|
104
|
+
|
|
105
|
+
* remove `anyToError` option and `linker.anyToError`
|
|
106
|
+
* remove ext errinfo of `expandError`
|
|
107
|
+
|
|
108
|
+
### 11.x
|
|
94
109
|
|
|
95
110
|
Remove deps handlers
|
|
96
111
|
|
|
@@ -117,28 +132,29 @@ Remove callback handlers of `flow.run(runtime, callback)`
|
|
|
117
132
|
|
|
118
133
|
Remove `retry` event of `runtime`. Add `retry` event of `linker`.
|
|
119
134
|
|
|
135
|
+
### 10.x
|
|
120
136
|
|
|
121
|
-
|
|
137
|
+
* remove timing: `runtime.timing`, `runtime.navigationStart`, `runner.startTime`, `runner.endTime`, and `timing` in `toJSON()`
|
|
122
138
|
|
|
123
|
-
|
|
124
|
-
`httpproxy` `confighandler` `pkghandler` `debugger` `localfile` `logger`
|
|
125
|
-
* Move `proxyRoute` of `linker` to `clientlinker-flow-httpproxy` pkg.
|
|
126
|
-
* `anyToError(err, runner)` instead of `anyToError(err, runtime)`. You can get `runtime` with `runner.runtime`.
|
|
127
|
-
* `err.fromClient` instead of `err.CLIENTLINKER_CLIENT`. `err.fromClientMethod` install of `err.CLIENTLINKER_ACTION`.
|
|
128
|
-
* Remove `linker.run().runtime` & `linker.run().runtimePromise`. You can get `runtime` width `linker.lastRuntime` after `linker.run`.
|
|
139
|
+
### 7.x
|
|
129
140
|
|
|
141
|
+
* Remove all sys flow: `httpproxy` `confighandler` `pkghandler` `debugger` `localfile` `logger` are no longer built in.
|
|
142
|
+
* Move `proxyRoute` of `linker` to `clientlinker-flow-httpproxy` pkg.
|
|
143
|
+
* `anyToError` second argument changed from `runtime` to `runner`.
|
|
144
|
+
* `err.fromClient`/`err.fromClientMethod` replace `err.CLIENTLINKER_CLIENT`/`err.CLIENTLINKER_ACTION`.
|
|
145
|
+
* Remove `linker.run().runtime` and `linker.run().runtimePromise`.
|
|
130
146
|
|
|
131
|
-
### 5.x.x => 6.0.0
|
|
132
147
|
|
|
133
|
-
|
|
134
|
-
* Running `callback.next` like `callback.next(true)`. Use `callback.nextAndResolve()` instead of previous caller.
|
|
148
|
+
### 6.x
|
|
135
149
|
|
|
136
|
-
|
|
150
|
+
* A flow handler returning a Promise binds the callback automatically.
|
|
151
|
+
* `callback.next` now needs `callback.next(true)`; `callback.nextAndResolve()` is removed.
|
|
152
|
+
* `callback.next()` without `true` in a custom flow causes the rpc to time out.
|
|
137
153
|
|
|
138
154
|
|
|
139
|
-
###
|
|
155
|
+
### 5.x
|
|
140
156
|
|
|
141
|
-
*
|
|
157
|
+
* The `httpproxy` protocol changed; server and client must be upgraded together.
|
|
142
158
|
|
|
143
159
|
|
|
144
160
|
[npm-image]: https://img.shields.io/npm/v/clientlinker.svg
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { clientlinker } from './lib/main';
|
|
2
|
+
export { parseAction } from './lib/utils';
|
|
3
|
+
export type { Linker, LinkerOptions } from './lib/linker';
|
|
4
|
+
export type { Client, ClientOptions } from './lib/client';
|
|
5
|
+
export type { Flow, FlowRunHandler, FlowMethodsHandler } from './lib/flow';
|
|
6
|
+
export type { ClientRuntime } from './lib/runtime/client_runtime';
|
|
7
|
+
export type { FlowsRuntime } from './lib/runtime/flows_runtime';
|
|
8
|
+
export type { FlowRuntime } from './lib/runtime/flow_runtime';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseAction = exports.clientlinker = void 0;
|
|
4
|
+
var main_1 = require("./lib/main");
|
|
5
|
+
Object.defineProperty(exports, "clientlinker", { enumerable: true, get: function () { return main_1.clientlinker; } });
|
|
6
|
+
var utils_1 = require("./lib/utils");
|
|
7
|
+
Object.defineProperty(exports, "parseAction", { enumerable: true, get: function () { return utils_1.parseAction; } });
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Linker } from './linker';
|
|
2
|
+
import type { Flow } from './flow';
|
|
3
|
+
export interface ClientOptions {
|
|
4
|
+
flows?: string[];
|
|
5
|
+
retry?: number;
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
}
|
|
8
|
+
export declare class Client {
|
|
9
|
+
name: string;
|
|
10
|
+
linker: Linker;
|
|
11
|
+
options: ClientOptions;
|
|
12
|
+
cache: Record<string, any>;
|
|
13
|
+
constructor(name: string, linker: Linker, options?: ClientOptions);
|
|
14
|
+
run(runtime: {
|
|
15
|
+
run(): Promise<unknown>;
|
|
16
|
+
}): Promise<unknown>;
|
|
17
|
+
methods(): Promise<Record<string, Flow[]> | []>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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.Client = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const debug = (0, debug_1.default)('clientlinker:client');
|
|
9
|
+
class Client {
|
|
10
|
+
constructor(name, linker, options) {
|
|
11
|
+
this.name = name;
|
|
12
|
+
this.linker = linker;
|
|
13
|
+
this.options = options || {};
|
|
14
|
+
// flow 会放一些cache在这里
|
|
15
|
+
this.cache = {};
|
|
16
|
+
}
|
|
17
|
+
run(runtime) {
|
|
18
|
+
return runtime.run();
|
|
19
|
+
}
|
|
20
|
+
async methods() {
|
|
21
|
+
const clientFlows = this.options.flows;
|
|
22
|
+
if (!clientFlows)
|
|
23
|
+
return [];
|
|
24
|
+
const promises = clientFlows.map(async (flowName) => {
|
|
25
|
+
const flow = this.linker.flow(flowName);
|
|
26
|
+
if (!flow)
|
|
27
|
+
return;
|
|
28
|
+
const list = await flow.methods(this);
|
|
29
|
+
if (!list) {
|
|
30
|
+
debug('no method inifo: %s', flowName);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
flow: flow,
|
|
35
|
+
methods: list
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
const methodList = await Promise.all(promises);
|
|
39
|
+
const map = {};
|
|
40
|
+
methodList.forEach(item => {
|
|
41
|
+
if (!item)
|
|
42
|
+
return;
|
|
43
|
+
item.methods.forEach(method => {
|
|
44
|
+
const methodInfo = map[method] || (map[method] = []);
|
|
45
|
+
methodInfo.push(item.flow);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
return map;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.Client = Client;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Client } from './client';
|
|
2
|
+
import type { ClientRuntime } from './runtime/client_runtime';
|
|
3
|
+
import type { FlowRuntime } from './runtime/flow_runtime';
|
|
4
|
+
export type FlowRunHandler = (runtime: ClientRuntime, callback: FlowRuntime) => any;
|
|
5
|
+
export type FlowMethodsHandler = (client: Client) => string[] | undefined | void | Promise<string[] | undefined | void>;
|
|
6
|
+
export declare class Flow {
|
|
7
|
+
name: string;
|
|
8
|
+
constructor(name: string);
|
|
9
|
+
run(runtime: ClientRuntime, callback: FlowRuntime): ReturnType<FlowRunHandler>;
|
|
10
|
+
methods(client: Client): ReturnType<FlowMethodsHandler>;
|
|
11
|
+
}
|
package/dist/lib/flow.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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.Flow = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const debug = (0, debug_1.default)('clientlinker:flow');
|
|
9
|
+
class Flow {
|
|
10
|
+
constructor(name) {
|
|
11
|
+
this.name = name;
|
|
12
|
+
}
|
|
13
|
+
run(runtime, callback) {
|
|
14
|
+
debug('skip by no handler: %s', this.name);
|
|
15
|
+
return callback.next();
|
|
16
|
+
}
|
|
17
|
+
methods(client) {
|
|
18
|
+
debug('skip by no handler: %s', this.name);
|
|
19
|
+
return Promise.resolve([]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Flow = Flow;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Client, ClientOptions } from './client';
|
|
2
|
+
import { Flow } from './flow';
|
|
3
|
+
import { ClientRuntime } from './runtime/client_runtime';
|
|
4
|
+
export interface LinkerOptions {
|
|
5
|
+
flows?: string[];
|
|
6
|
+
defaults?: ClientOptions;
|
|
7
|
+
clients?: Record<string, ClientOptions>;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
export type FlowHandler = (flow: Flow, linker: Linker) => void;
|
|
11
|
+
export interface ClientMethods {
|
|
12
|
+
client: Client;
|
|
13
|
+
methods: Record<string, Flow[]> | [];
|
|
14
|
+
}
|
|
15
|
+
export declare class Linker {
|
|
16
|
+
_clients: Record<string, Client>;
|
|
17
|
+
flows: Record<string, Flow>;
|
|
18
|
+
options: LinkerOptions;
|
|
19
|
+
lastRuntime: ClientRuntime | null;
|
|
20
|
+
cache: Record<string, any>;
|
|
21
|
+
constructor(options?: LinkerOptions);
|
|
22
|
+
client(clientName: string): Client | undefined;
|
|
23
|
+
client(clientName: string, options: ClientOptions): Client;
|
|
24
|
+
/**
|
|
25
|
+
* 注册/获取flower
|
|
26
|
+
*/
|
|
27
|
+
flow(flowName: string): Flow | undefined;
|
|
28
|
+
flow(flowName: string, handler: FlowHandler | Flow): Flow;
|
|
29
|
+
run(action: string, query?: unknown, body?: unknown, options?: ClientRuntime['options']): Promise<any>;
|
|
30
|
+
runIn(action: string, query?: unknown, body?: unknown, options?: ClientRuntime['options'], env?: ClientRuntime['env']): Promise<any>;
|
|
31
|
+
runtime(...args: Parameters<Linker['_runtime']>): Promise<ClientRuntime>;
|
|
32
|
+
_runtime(action: string, query?: unknown, body?: unknown, options?: ClientRuntime['options'], env?: ClientRuntime['env']): ClientRuntime;
|
|
33
|
+
parseAction(action: string): Promise<{
|
|
34
|
+
client: Client | undefined;
|
|
35
|
+
method: string;
|
|
36
|
+
}>;
|
|
37
|
+
_parseAction(action: string): {
|
|
38
|
+
client: Client | undefined;
|
|
39
|
+
method: string;
|
|
40
|
+
};
|
|
41
|
+
methods(): Promise<Record<string, ClientMethods>>;
|
|
42
|
+
clients(): Promise<Record<string, Client>>;
|
|
43
|
+
clearCache(clientName?: string): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
export interface Linker {
|
|
46
|
+
version: string;
|
|
47
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
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.Linker = void 0;
|
|
7
|
+
const lodash_1 = require("lodash");
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const client_1 = require("./client");
|
|
10
|
+
const flow_1 = require("./flow");
|
|
11
|
+
const client_runtime_1 = require("./runtime/client_runtime");
|
|
12
|
+
const utils_1 = require("./utils");
|
|
13
|
+
const debug = (0, debug_1.default)('clientlinker:linker');
|
|
14
|
+
const pkg = require('../../package.json');
|
|
15
|
+
class Linker {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this._clients = {};
|
|
18
|
+
this.flows = {};
|
|
19
|
+
// 包含初始化client的一些配置
|
|
20
|
+
this.options = options || {};
|
|
21
|
+
// 用来暴露最近一次执行runIn时,返回的runtime对象
|
|
22
|
+
this.lastRuntime = null;
|
|
23
|
+
// flow 可能会放一些缓存变量在这里
|
|
24
|
+
this.cache = {};
|
|
25
|
+
}
|
|
26
|
+
client(clientName, options) {
|
|
27
|
+
if (typeof clientName != 'string') {
|
|
28
|
+
throw new Error(`CLIENTLINKER:ClientNameMustBeString,${clientName}`);
|
|
29
|
+
}
|
|
30
|
+
if (arguments.length == 1) {
|
|
31
|
+
return this._clients[clientName];
|
|
32
|
+
}
|
|
33
|
+
if (typeof options != 'object') {
|
|
34
|
+
throw new Error(`CLIENTLINKER:ClientOptionsMustBeObject,${clientName}`);
|
|
35
|
+
}
|
|
36
|
+
options = {
|
|
37
|
+
...this.options.defaults,
|
|
38
|
+
...options
|
|
39
|
+
};
|
|
40
|
+
if (!options.flows)
|
|
41
|
+
options.flows = [];
|
|
42
|
+
else
|
|
43
|
+
options.flows = options.flows.slice();
|
|
44
|
+
const client = new client_1.Client(clientName, this, options);
|
|
45
|
+
this._clients[clientName] = client;
|
|
46
|
+
debug('add client:%s', clientName);
|
|
47
|
+
return client;
|
|
48
|
+
}
|
|
49
|
+
flow(flowName, handler) {
|
|
50
|
+
if (typeof flowName != 'string') {
|
|
51
|
+
throw new Error(`'CLIENTLINKER:FlowNameMustBeString,${flowName}`);
|
|
52
|
+
}
|
|
53
|
+
else if (arguments.length == 1) {
|
|
54
|
+
return this.flows[flowName];
|
|
55
|
+
}
|
|
56
|
+
let flow;
|
|
57
|
+
if (handler instanceof flow_1.Flow) {
|
|
58
|
+
flow = handler;
|
|
59
|
+
}
|
|
60
|
+
else if (typeof handler !== 'function') {
|
|
61
|
+
throw new Error(`CLIENTLINKER:FlowHandlerMustBeFunction,${flowName}`);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
flow = new flow_1.Flow(flowName);
|
|
65
|
+
handler(flow, this);
|
|
66
|
+
}
|
|
67
|
+
if (typeof flow.run != 'function') {
|
|
68
|
+
throw new Error(`CLIENTLINKER:Flow.runMustBeFunction,${flowName}`);
|
|
69
|
+
}
|
|
70
|
+
if (flow.methods && typeof flow.methods != 'function') {
|
|
71
|
+
throw new Error(`CLIENTLINKER:Flow.methodsMustBeFunction,${flowName}`);
|
|
72
|
+
}
|
|
73
|
+
this.flows[flowName] = flow;
|
|
74
|
+
return flow;
|
|
75
|
+
}
|
|
76
|
+
// 标准输入参数
|
|
77
|
+
run(action, query, body, options) {
|
|
78
|
+
return this.runIn(action, query, body, options);
|
|
79
|
+
}
|
|
80
|
+
// 相比run,多了env
|
|
81
|
+
runIn(action, query, body, options, env) {
|
|
82
|
+
const runtime = this._runtime(action, query, body, options, env);
|
|
83
|
+
if (!runtime.client) {
|
|
84
|
+
return Promise.reject((0, utils_1.newNotFoundError)('NO CLIENT', runtime));
|
|
85
|
+
}
|
|
86
|
+
// 通过这种手段,同步情况下,暴露runtime
|
|
87
|
+
// runtime保存着运行时的所有数据,方便进行调试
|
|
88
|
+
this.lastRuntime = runtime;
|
|
89
|
+
return runtime.run_();
|
|
90
|
+
}
|
|
91
|
+
runtime(...args) {
|
|
92
|
+
const runtime = this._runtime(...args);
|
|
93
|
+
// 直接使用Promise包裹,比async性能好
|
|
94
|
+
return runtime.client
|
|
95
|
+
? Promise.resolve(runtime)
|
|
96
|
+
: Promise.reject((0, utils_1.newNotFoundError)('NO CLIENT', runtime));
|
|
97
|
+
}
|
|
98
|
+
_runtime(action, query, body, options, env) {
|
|
99
|
+
const data = this._parseAction(action);
|
|
100
|
+
const runtime = new client_runtime_1.ClientRuntime(this, action, query, body, options);
|
|
101
|
+
if (env)
|
|
102
|
+
runtime.env = env;
|
|
103
|
+
runtime.method = data.method;
|
|
104
|
+
// 找不到 client 时,由调用方(runIn/runtime)负责 reject
|
|
105
|
+
runtime.client = data.client;
|
|
106
|
+
return runtime;
|
|
107
|
+
}
|
|
108
|
+
// 解析action
|
|
109
|
+
async parseAction(action) {
|
|
110
|
+
return this._parseAction(action);
|
|
111
|
+
}
|
|
112
|
+
_parseAction(action) {
|
|
113
|
+
const info = (0, utils_1.parseAction)(action);
|
|
114
|
+
return {
|
|
115
|
+
client: this._clients[info.clientName],
|
|
116
|
+
method: info.method
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
// 罗列methods
|
|
120
|
+
async methods() {
|
|
121
|
+
const clients = this._clients;
|
|
122
|
+
const promises = (0, lodash_1.map)(clients, async (client) => {
|
|
123
|
+
const methodList = await client.methods();
|
|
124
|
+
return {
|
|
125
|
+
client: client,
|
|
126
|
+
methods: methodList
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
const list = await Promise.all(promises);
|
|
130
|
+
// 整理
|
|
131
|
+
const result = {};
|
|
132
|
+
list.forEach(item => {
|
|
133
|
+
result[item.client.name] = item;
|
|
134
|
+
});
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
async clients() {
|
|
138
|
+
return this._clients;
|
|
139
|
+
}
|
|
140
|
+
async clearCache(clientName) {
|
|
141
|
+
if (!clientName)
|
|
142
|
+
this.cache = {};
|
|
143
|
+
const clients = this._clients;
|
|
144
|
+
if (clientName) {
|
|
145
|
+
const client = clients[clientName];
|
|
146
|
+
if (client)
|
|
147
|
+
client.cache = {};
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
(0, lodash_1.each)(clients, item => {
|
|
151
|
+
item.cache = {};
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.Linker = Linker;
|
|
157
|
+
Linker.prototype.version = pkg.version;
|
package/dist/lib/main.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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.clientlinker = clientlinker;
|
|
7
|
+
const lodash_1 = require("lodash");
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const linker_1 = require("./linker");
|
|
10
|
+
const debug = (0, debug_1.default)('clientlinker');
|
|
11
|
+
/**
|
|
12
|
+
* [options]
|
|
13
|
+
* flows
|
|
14
|
+
* httpproxy
|
|
15
|
+
* clients
|
|
16
|
+
* defaults
|
|
17
|
+
*/
|
|
18
|
+
function clientlinker(options) {
|
|
19
|
+
options || (options = {});
|
|
20
|
+
const defaults = options.defaults || (options.defaults = {});
|
|
21
|
+
!defaults.flows &&
|
|
22
|
+
options.flows &&
|
|
23
|
+
(defaults.flows = options.flows.slice());
|
|
24
|
+
const linker = new linker_1.Linker(options);
|
|
25
|
+
// client options
|
|
26
|
+
if (options.clients) {
|
|
27
|
+
(0, lodash_1.each)(options.clients, (handler, name) => {
|
|
28
|
+
linker.client(name, handler);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
linker.clients().then(clients => {
|
|
32
|
+
debug('init clients:%s', Object.keys(clients));
|
|
33
|
+
});
|
|
34
|
+
return linker;
|
|
35
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { FlowsRuntime } from './flows_runtime';
|
|
2
|
+
import type { Linker } from '../linker';
|
|
3
|
+
import type { Client } from '../client';
|
|
4
|
+
import type { FlowRuntime } from './flow_runtime';
|
|
5
|
+
export declare class ClientRuntime {
|
|
6
|
+
linker: Linker;
|
|
7
|
+
action: string;
|
|
8
|
+
client: Client;
|
|
9
|
+
method: string;
|
|
10
|
+
query: any;
|
|
11
|
+
body: any;
|
|
12
|
+
options: Record<string, any> | undefined;
|
|
13
|
+
env: Record<string, any>;
|
|
14
|
+
_debugData: Record<string, DebugData[]> | null;
|
|
15
|
+
retry: FlowsRuntime[];
|
|
16
|
+
lastTry: FlowsRuntime | null;
|
|
17
|
+
constructor(linker: Linker, action: string, query?: any, body?: any, options?: Record<string, any>);
|
|
18
|
+
run(): Promise<any>;
|
|
19
|
+
run_(err?: unknown): Promise<any>;
|
|
20
|
+
debug(): Record<string, DebugData[]>;
|
|
21
|
+
debug(key: string): unknown;
|
|
22
|
+
debug(key: string, val: unknown): void;
|
|
23
|
+
getLastRunner(): FlowRuntime | null;
|
|
24
|
+
getFirstRunner(): FlowRuntime | null | undefined;
|
|
25
|
+
toJSON(): Record<string, any>;
|
|
26
|
+
}
|
|
27
|
+
export declare class DebugData {
|
|
28
|
+
value: unknown;
|
|
29
|
+
retry: number;
|
|
30
|
+
runner: FlowRuntime | null;
|
|
31
|
+
constructor(value: unknown, runtime: ClientRuntime);
|
|
32
|
+
toJSON(): {
|
|
33
|
+
value: unknown;
|
|
34
|
+
retry: number;
|
|
35
|
+
flow: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DebugData = exports.ClientRuntime = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const flows_runtime_1 = require("./flows_runtime");
|
|
6
|
+
class ClientRuntime {
|
|
7
|
+
constructor(linker, action, query, body, options) {
|
|
8
|
+
this.linker = linker;
|
|
9
|
+
this.action = action;
|
|
10
|
+
this.client = null;
|
|
11
|
+
this.method = null;
|
|
12
|
+
this.query = query;
|
|
13
|
+
this.body = body;
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.env = {};
|
|
16
|
+
this._debugData = null;
|
|
17
|
+
this.retry = [];
|
|
18
|
+
this.lastTry = null;
|
|
19
|
+
}
|
|
20
|
+
run() {
|
|
21
|
+
if (this.lastTry)
|
|
22
|
+
this.retry = [];
|
|
23
|
+
return this.run_();
|
|
24
|
+
}
|
|
25
|
+
run_(err) {
|
|
26
|
+
const onetry = new flows_runtime_1.FlowsRuntime(this);
|
|
27
|
+
this.lastTry = onetry;
|
|
28
|
+
this.retry.push(onetry);
|
|
29
|
+
// 减少client linker的逻辑,将retry放到linker
|
|
30
|
+
// 否则要绑定removeAllListner
|
|
31
|
+
// this.linker.emit('retry', { runtime: this, error: err });
|
|
32
|
+
let promise = onetry.run();
|
|
33
|
+
const retry = this.options && this.options.retry || this.client.options.retry;
|
|
34
|
+
if (this.retry.length < retry) {
|
|
35
|
+
promise = promise.catch(() => this.run_(err));
|
|
36
|
+
}
|
|
37
|
+
return promise;
|
|
38
|
+
}
|
|
39
|
+
debug(key, val) {
|
|
40
|
+
const data = this._debugData || (this._debugData = {});
|
|
41
|
+
switch (arguments.length) {
|
|
42
|
+
case 0:
|
|
43
|
+
return data;
|
|
44
|
+
case 1: {
|
|
45
|
+
const arr = data[key];
|
|
46
|
+
return arr && arr[arr.length - 1].value;
|
|
47
|
+
}
|
|
48
|
+
case 2:
|
|
49
|
+
default: {
|
|
50
|
+
const arr = data[key] || (data[key] = []);
|
|
51
|
+
arr.push(new DebugData(val, this));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
getLastRunner() {
|
|
56
|
+
return this.lastTry && this.lastTry.lastRunner;
|
|
57
|
+
}
|
|
58
|
+
getFirstRunner() {
|
|
59
|
+
const firstTry = this.retry[0];
|
|
60
|
+
return firstTry && firstTry.runned[0];
|
|
61
|
+
}
|
|
62
|
+
toJSON() {
|
|
63
|
+
const debugData = {};
|
|
64
|
+
(0, lodash_1.each)(this._debugData, (vals, key) => {
|
|
65
|
+
debugData[key] = vals.map(item => {
|
|
66
|
+
return item.toJSON();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
action: this.action,
|
|
71
|
+
query: this.query,
|
|
72
|
+
body: this.body,
|
|
73
|
+
options: this.options,
|
|
74
|
+
client: this.client && this.client.name,
|
|
75
|
+
env: this.env,
|
|
76
|
+
debugData: debugData,
|
|
77
|
+
retry: this.retry.map(item => {
|
|
78
|
+
return item.toJSON();
|
|
79
|
+
}),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.ClientRuntime = ClientRuntime;
|
|
84
|
+
class DebugData {
|
|
85
|
+
constructor(value, runtime) {
|
|
86
|
+
this.value = value;
|
|
87
|
+
this.retry = runtime.retry.length;
|
|
88
|
+
this.runner = runtime.getLastRunner();
|
|
89
|
+
}
|
|
90
|
+
toJSON() {
|
|
91
|
+
return {
|
|
92
|
+
value: this.value,
|
|
93
|
+
retry: this.retry,
|
|
94
|
+
flow: this.runner.flow.name
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.DebugData = DebugData;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Flow } from '../flow';
|
|
2
|
+
import type { FlowsRuntime } from './flows_runtime';
|
|
3
|
+
import type { ClientRuntime } from './client_runtime';
|
|
4
|
+
export declare class FlowRuntime {
|
|
5
|
+
flow: Flow;
|
|
6
|
+
onetry: FlowsRuntime;
|
|
7
|
+
runtime: ClientRuntime;
|
|
8
|
+
_nextRunnerPromise: Promise<any> | null;
|
|
9
|
+
constructor(flow: Flow, onetry: FlowsRuntime);
|
|
10
|
+
run(): Promise<any>;
|
|
11
|
+
next(): Promise<any>;
|
|
12
|
+
toJSON(): {
|
|
13
|
+
name: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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.FlowRuntime = void 0;
|
|
7
|
+
const debug_1 = __importDefault(require("debug"));
|
|
8
|
+
const utils_1 = require("../utils");
|
|
9
|
+
const debug = (0, debug_1.default)('clientlinker:flow_runtime');
|
|
10
|
+
class FlowRuntime {
|
|
11
|
+
constructor(flow, onetry) {
|
|
12
|
+
this.flow = flow;
|
|
13
|
+
this.onetry = onetry;
|
|
14
|
+
this.runtime = onetry.runtime;
|
|
15
|
+
this._nextRunnerPromise = null;
|
|
16
|
+
}
|
|
17
|
+
async run() {
|
|
18
|
+
return this.flow.run(this.runtime, this);
|
|
19
|
+
}
|
|
20
|
+
next() {
|
|
21
|
+
if (!this._nextRunnerPromise) {
|
|
22
|
+
const nextRunner = this.onetry.nextRunner();
|
|
23
|
+
if (nextRunner) {
|
|
24
|
+
debug('nextRunner:%s', nextRunner.flow.name);
|
|
25
|
+
this._nextRunnerPromise = nextRunner.run();
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const runtime = this.runtime;
|
|
29
|
+
debug('flow out: %s', runtime.action);
|
|
30
|
+
return Promise.reject((0, utils_1.newNotFoundError)('CLIENT FLOW OUT', runtime));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
debug('next handler run twice');
|
|
35
|
+
}
|
|
36
|
+
return this._nextRunnerPromise;
|
|
37
|
+
}
|
|
38
|
+
toJSON() {
|
|
39
|
+
return {
|
|
40
|
+
name: this.flow.name,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.FlowRuntime = FlowRuntime;
|