@oox/client 1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 lipingruan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
File without changes
package/app.js ADDED
@@ -0,0 +1,130 @@
1
+ import { EventEmitter } from 'events';
2
+ import { genKVMethods } from './utils.js';
3
+ import * as tracer from './tracer.js';
4
+ export const eventHub = new EventEmitter();
5
+ /**
6
+ * sourceMethods => methods
7
+ */
8
+ let sourceMethods = {};
9
+ /**
10
+ * the kvMethods is all actions refs [has bind this]
11
+ */
12
+ export const kvMethods = new Map();
13
+ export const sourceKVMethods = new Map();
14
+ export function setMethods(methods) {
15
+ sourceMethods = methods;
16
+ kvMethods.clear();
17
+ sourceKVMethods.clear();
18
+ genKVMethods(methods, kvMethods, sourceKVMethods);
19
+ }
20
+ export function getMethods() {
21
+ return sourceMethods;
22
+ }
23
+ export function on(event, listener) {
24
+ eventHub.on(event, listener);
25
+ }
26
+ export function once(event, listener) {
27
+ eventHub.once(event, listener);
28
+ }
29
+ export function off(event, listener) {
30
+ eventHub.off(event, listener);
31
+ }
32
+ export function emit(event, ...args) {
33
+ return eventHub.emit(event, ...args);
34
+ }
35
+ /**
36
+ * Call an Function on RPC server
37
+ * @param action
38
+ * @param params
39
+ * @param context
40
+ * @returns
41
+ */
42
+ export async function call(action, params = [], context) {
43
+ if (!Array.isArray(params))
44
+ params = [params];
45
+ const { traceId } = context;
46
+ emit('call:start', Date.now(), action, params, context);
47
+ const returns = {
48
+ traceId,
49
+ success: true
50
+ };
51
+ const OOX_TRACER = {};
52
+ OOX_TRACER[traceId] = async (action, [...params], context) => {
53
+ return await execute(action, [...params], context);
54
+ };
55
+ tracer.enterWith(context);
56
+ try {
57
+ const result = await OOX_TRACER[traceId](action, [...params], context);
58
+ returns.body = result;
59
+ emit('call:success', Date.now(), action, params, context, result);
60
+ }
61
+ catch (error) {
62
+ returns.success = false;
63
+ returns.error = {
64
+ message: error.message,
65
+ stack: error.stack
66
+ };
67
+ emit('call:fail', Date.now(), action, params, context, error);
68
+ }
69
+ finally {
70
+ tracer.exitWith(context);
71
+ return returns;
72
+ }
73
+ }
74
+ export async function execute(action, params, context) {
75
+ const __proxy = '__proxy', _proxy = '_proxy';
76
+ if (__proxy === action || _proxy === action || action.endsWith(_proxy))
77
+ throw new Error('Invalid Action[' + action + ']');
78
+ const methods = kvMethods;
79
+ // 目标函数
80
+ const target = methods.get(action);
81
+ // 目标代理函数
82
+ const targetProxy = methods.get(action + _proxy);
83
+ // 即不存在目标也不存在目标代理时, 报错函数不存在
84
+ if (!target && !targetProxy)
85
+ throw new Error('Unknown Action [' + action + ']');
86
+ // ============================ PROXY BEGIN ============================
87
+ // 最顶层代理
88
+ const topProxy = methods.get(__proxy);
89
+ if (topProxy) {
90
+ const proxyReturns = await topProxy(action, params, context);
91
+ if (proxyReturns !== undefined)
92
+ return proxyReturns;
93
+ }
94
+ // 'x.y.z' => [ 'x', 'y', 'z' ]
95
+ const nameStack = action.split('.'), size = nameStack.length - 1;
96
+ let index = -1, proxyPrefix = '';
97
+ // 根代理遍历
98
+ while (++index < size) {
99
+ // x.
100
+ // x.y.
101
+ proxyPrefix += nameStack[index] + '.';
102
+ // x.__proxy
103
+ // x.y.__proxy
104
+ const rootProxy = methods.get(proxyPrefix + __proxy);
105
+ // x.__proxy ( 'y.z', ... )
106
+ // x.y.__proxy ( 'z', ... )
107
+ if (rootProxy) {
108
+ const proxyReturns = await rootProxy(nameStack.slice(index).join('.'), params, context);
109
+ if (proxyReturns !== undefined)
110
+ return proxyReturns;
111
+ }
112
+ }
113
+ // 同级代理
114
+ const layerProxy = methods.get(proxyPrefix + _proxy);
115
+ if (layerProxy) {
116
+ const proxyReturns = await layerProxy(nameStack[index], params, context);
117
+ if (proxyReturns !== undefined)
118
+ return proxyReturns;
119
+ }
120
+ if (targetProxy) {
121
+ const proxyReturns = await targetProxy(params, context);
122
+ if (proxyReturns !== undefined)
123
+ return proxyReturns;
124
+ }
125
+ // ============================= PROXY END =============================
126
+ // make sure target action execute after all proxies
127
+ if (target) {
128
+ return await target(...params);
129
+ }
130
+ }