oox 0.3.0-beta11 → 0.3.0-beta13

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/index.js CHANGED
@@ -1,168 +1,168 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rpc = exports.setLoadBalancePolicy = exports.removeKeepAliveConnection = exports.addKeepAliveConnection = exports.getKeepAliveConnection = exports.getKeepAliveConnections = exports.keepAliveConnections = exports.RPCKeepAliveConnection = exports.stop = exports.serve = exports.getContext = exports.genContext = exports.genTraceId = exports.setGenTraceIdFunction = exports.config = exports.Config = exports.Context = exports.emit = exports.off = exports.once = exports.on = exports.logger = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
4
- const node_crypto_1 = require("node:crypto");
5
- const app = require("./app");
6
- const utils_1 = require("./utils");
7
- const module_1 = require("./modules/module");
8
- exports.Module = module_1.default;
9
- Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
10
- const modules_1 = require("./modules");
11
- exports.modules = new modules_1.default;
12
- exports.asyncStore = app.asyncStore, exports.setMethods = app.setMethods, exports.getMethods = app.getMethods, exports.kvMethods = app.kvMethods, exports.sourceKVMethods = app.sourceKVMethods, exports.call = app.call, exports.execute = app.execute, exports.logger = app.logger, exports.on = app.on, exports.once = app.once, exports.off = app.off, exports.emit = app.emit;
13
- class Context extends app.Context {
14
- // 请求溯源IP
15
- sourceIP = '';
16
- // 请求者IP
17
- ip = '';
18
- // 请求者名称
19
- caller = 'anonymous';
20
- // 请求者ID (长连接专用)
21
- callerId = '';
22
- // 请求者连接把柄
23
- connection;
24
- toJSON() {
25
- const context = Object.assign({}, this);
26
- delete context.connection;
27
- return context;
28
- }
29
- }
30
- exports.Context = Context;
31
- class Config {
32
- // 服务名称
33
- name = 'local';
34
- // 启动文件
35
- entryFile = {
36
- // 启动文件路径
37
- path: '',
38
- // 服务列表路径
39
- group: '',
40
- };
41
- // 主机地址
42
- host = (0, utils_1.getIPAddress)(4)[0];
43
- // 默认监听端口
44
- port = 0;
45
- // 默认跨域设置
46
- origin = '';
47
- }
48
- exports.Config = Config;
49
- exports.config = new Config();
50
- let genTraceIdFunction = () => (0, node_crypto_1.randomUUID)();
51
- function setGenTraceIdFunction(fn) {
52
- genTraceIdFunction = fn;
53
- }
54
- exports.setGenTraceIdFunction = setGenTraceIdFunction;
55
- /**
56
- * 生成随机不重复id
57
- */
58
- function genTraceId() {
59
- return genTraceIdFunction();
60
- }
61
- exports.genTraceId = genTraceId;
62
- /**
63
- * 获取链路跟踪上下文
64
- */
65
- function genContext(context) {
66
- const newContext = Object.assign(new Context(), context);
67
- if (!newContext.traceId)
68
- newContext.traceId = genTraceId();
69
- if (!newContext.sourceIP)
70
- newContext.sourceIP = newContext.ip;
71
- return newContext;
72
- }
73
- exports.genContext = genContext;
74
- function getContext() {
75
- const context = exports.asyncStore.getStore();
76
- return context || genContext();
77
- }
78
- exports.getContext = getContext;
79
- async function serve() {
80
- await exports.modules.serve();
81
- }
82
- exports.serve = serve;
83
- async function stop() {
84
- await exports.modules.stop();
85
- }
86
- exports.stop = stop;
87
- class RPCKeepAliveConnection {
88
- data;
89
- nativeConnection = null;
90
- adapter = null;
91
- constructor(adapter, nativeConnection, data) {
92
- this.adapter = adapter;
93
- this.nativeConnection = nativeConnection;
94
- if (data)
95
- this.data = data;
96
- }
97
- }
98
- exports.RPCKeepAliveConnection = RPCKeepAliveConnection;
99
- exports.keepAliveConnections = new Map();
100
- function getKeepAliveConnections(name) {
101
- return exports.keepAliveConnections.get(name) || new Map();
102
- }
103
- exports.getKeepAliveConnections = getKeepAliveConnections;
104
- function getKeepAliveConnection(name, id) {
105
- const connections = exports.keepAliveConnections.get(name);
106
- if (!connections)
107
- return null;
108
- return connections.get(id);
109
- }
110
- exports.getKeepAliveConnection = getKeepAliveConnection;
111
- function addKeepAliveConnection(connection) {
112
- const { name, id } = connection.data;
113
- if (exports.keepAliveConnections.has(name)) {
114
- exports.keepAliveConnections.get(name).set(id, connection);
115
- }
116
- else {
117
- exports.keepAliveConnections.set(name, new Map([[id, connection]]));
118
- }
119
- }
120
- exports.addKeepAliveConnection = addKeepAliveConnection;
121
- function removeKeepAliveConnection(name, id) {
122
- if (name instanceof RPCKeepAliveConnection) {
123
- id = name.data.id;
124
- name = name.data.name;
125
- }
126
- if (exports.keepAliveConnections.has(name)) {
127
- const group = exports.keepAliveConnections.get(name);
128
- group.delete(id);
129
- if (!group.size)
130
- exports.keepAliveConnections.delete(name);
131
- }
132
- }
133
- exports.removeKeepAliveConnection = removeKeepAliveConnection;
134
- /**
135
- * random connection select for default load balance policy
136
- * @param name service name
137
- * @returns selected connection
138
- */
139
- let loadBalancePolicy = (name) => {
140
- const connections = exports.keepAliveConnections.get(name);
141
- if (!connections || !connections.size)
142
- return null;
143
- const arrayConnections = Array.from(connections.values());
144
- const index = Math.floor(Math.random() * arrayConnections.length);
145
- return arrayConnections[index];
146
- };
147
- function setLoadBalancePolicy(policy) {
148
- loadBalancePolicy = policy;
149
- }
150
- exports.setLoadBalancePolicy = setLoadBalancePolicy;
151
- async function rpc(arg1, action, params, context) {
152
- if (!context || !context.traceId) {
153
- context = getContext();
154
- }
155
- let connection;
156
- if (arg1 instanceof RPCKeepAliveConnection) {
157
- connection = arg1;
158
- }
159
- else if ('string' === typeof arg1) {
160
- connection = loadBalancePolicy(arg1);
161
- if (!connection)
162
- throw new Error(`Connection<${arg1}> not found`);
163
- }
164
- else
165
- throw new Error(`Unknown rpc arg1<${arg1}>`);
166
- return connection.adapter.rpc(connection.nativeConnection, action, params, context);
167
- }
168
- exports.rpc = rpc;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rpc = exports.setLoadBalancePolicy = exports.removeKeepAliveConnection = exports.addKeepAliveConnection = exports.getKeepAliveConnection = exports.getKeepAliveConnections = exports.keepAliveConnections = exports.RPCKeepAliveConnection = exports.stop = exports.serve = exports.getContext = exports.genContext = exports.genTraceId = exports.setGenTraceIdFunction = exports.config = exports.Config = exports.Context = exports.emit = exports.off = exports.once = exports.on = exports.logger = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const app = require("./app");
6
+ const utils_1 = require("./utils");
7
+ const module_1 = require("./modules/module");
8
+ exports.Module = module_1.default;
9
+ Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
10
+ const modules_1 = require("./modules");
11
+ exports.modules = new modules_1.default;
12
+ exports.asyncStore = app.asyncStore, exports.setMethods = app.setMethods, exports.getMethods = app.getMethods, exports.kvMethods = app.kvMethods, exports.sourceKVMethods = app.sourceKVMethods, exports.call = app.call, exports.execute = app.execute, exports.logger = app.logger, exports.on = app.on, exports.once = app.once, exports.off = app.off, exports.emit = app.emit;
13
+ class Context extends app.Context {
14
+ // 请求溯源IP
15
+ sourceIP = '';
16
+ // 请求者IP
17
+ ip = '';
18
+ // 请求者名称
19
+ caller = 'anonymous';
20
+ // 请求者ID (长连接专用)
21
+ callerId = '';
22
+ // 请求者连接把柄
23
+ connection;
24
+ toJSON() {
25
+ const context = Object.assign({}, this);
26
+ delete context.connection;
27
+ return context;
28
+ }
29
+ }
30
+ exports.Context = Context;
31
+ class Config {
32
+ // 服务名称
33
+ name = 'local';
34
+ // 启动文件
35
+ entryFile = {
36
+ // 启动文件路径
37
+ path: '',
38
+ // 服务列表路径
39
+ group: '',
40
+ };
41
+ // 主机地址
42
+ host = (0, utils_1.getIPAddress)(4)[0];
43
+ // 默认监听端口
44
+ port = 0;
45
+ // 默认跨域设置
46
+ origin = '';
47
+ }
48
+ exports.Config = Config;
49
+ exports.config = new Config();
50
+ let genTraceIdFunction = () => (0, node_crypto_1.randomUUID)();
51
+ function setGenTraceIdFunction(fn) {
52
+ genTraceIdFunction = fn;
53
+ }
54
+ exports.setGenTraceIdFunction = setGenTraceIdFunction;
55
+ /**
56
+ * 生成随机不重复id
57
+ */
58
+ function genTraceId() {
59
+ return genTraceIdFunction();
60
+ }
61
+ exports.genTraceId = genTraceId;
62
+ /**
63
+ * 获取链路跟踪上下文
64
+ */
65
+ function genContext(context) {
66
+ const newContext = Object.assign(new Context(), context);
67
+ if (!newContext.traceId)
68
+ newContext.traceId = genTraceId();
69
+ if (!newContext.sourceIP)
70
+ newContext.sourceIP = newContext.ip;
71
+ return newContext;
72
+ }
73
+ exports.genContext = genContext;
74
+ function getContext() {
75
+ const context = exports.asyncStore.getStore();
76
+ return context || genContext();
77
+ }
78
+ exports.getContext = getContext;
79
+ async function serve() {
80
+ await exports.modules.serve();
81
+ }
82
+ exports.serve = serve;
83
+ async function stop() {
84
+ await exports.modules.stop();
85
+ }
86
+ exports.stop = stop;
87
+ class RPCKeepAliveConnection {
88
+ data;
89
+ nativeConnection = null;
90
+ adapter = null;
91
+ constructor(adapter, nativeConnection, data) {
92
+ this.adapter = adapter;
93
+ this.nativeConnection = nativeConnection;
94
+ if (data)
95
+ this.data = data;
96
+ }
97
+ }
98
+ exports.RPCKeepAliveConnection = RPCKeepAliveConnection;
99
+ exports.keepAliveConnections = new Map();
100
+ function getKeepAliveConnections(name) {
101
+ return exports.keepAliveConnections.get(name) || new Map();
102
+ }
103
+ exports.getKeepAliveConnections = getKeepAliveConnections;
104
+ function getKeepAliveConnection(name, id) {
105
+ const connections = exports.keepAliveConnections.get(name);
106
+ if (!connections)
107
+ return null;
108
+ return connections.get(id);
109
+ }
110
+ exports.getKeepAliveConnection = getKeepAliveConnection;
111
+ function addKeepAliveConnection(connection) {
112
+ const { name, id } = connection.data;
113
+ if (exports.keepAliveConnections.has(name)) {
114
+ exports.keepAliveConnections.get(name).set(id, connection);
115
+ }
116
+ else {
117
+ exports.keepAliveConnections.set(name, new Map([[id, connection]]));
118
+ }
119
+ }
120
+ exports.addKeepAliveConnection = addKeepAliveConnection;
121
+ function removeKeepAliveConnection(name, id) {
122
+ if (name instanceof RPCKeepAliveConnection) {
123
+ id = name.data.id;
124
+ name = name.data.name;
125
+ }
126
+ if (exports.keepAliveConnections.has(name)) {
127
+ const group = exports.keepAliveConnections.get(name);
128
+ group.delete(id);
129
+ if (!group.size)
130
+ exports.keepAliveConnections.delete(name);
131
+ }
132
+ }
133
+ exports.removeKeepAliveConnection = removeKeepAliveConnection;
134
+ /**
135
+ * random connection select for default load balance policy
136
+ * @param name service name
137
+ * @returns selected connection
138
+ */
139
+ let loadBalancePolicy = (name) => {
140
+ const connections = exports.keepAliveConnections.get(name);
141
+ if (!connections || !connections.size)
142
+ return null;
143
+ const arrayConnections = Array.from(connections.values());
144
+ const index = Math.floor(Math.random() * arrayConnections.length);
145
+ return arrayConnections[index];
146
+ };
147
+ function setLoadBalancePolicy(policy) {
148
+ loadBalancePolicy = policy;
149
+ }
150
+ exports.setLoadBalancePolicy = setLoadBalancePolicy;
151
+ async function rpc(arg1, action, params, context) {
152
+ if (!context || !context.traceId) {
153
+ context = getContext();
154
+ }
155
+ let connection;
156
+ if (arg1 instanceof RPCKeepAliveConnection) {
157
+ connection = arg1;
158
+ }
159
+ else if ('string' === typeof arg1) {
160
+ connection = loadBalancePolicy(arg1);
161
+ if (!connection)
162
+ throw new Error(`Connection<${arg1}> not found`);
163
+ }
164
+ else
165
+ throw new Error(`Unknown rpc arg1<${arg1}>`);
166
+ return connection.adapter.rpc(connection.nativeConnection, action, params, context);
167
+ }
168
+ exports.rpc = rpc;
package/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
-
2
- import * as oox from './index.js'
3
-
4
- export * from './index.js'
1
+
2
+ import * as oox from './index.js'
3
+
4
+ export * from './index.js'
5
5
  export default oox
package/logger.js CHANGED
@@ -1,40 +1,40 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.trace = exports.error = exports.warn = exports.info = void 0;
4
- const app_1 = require("./app");
5
- function info(...msgs) {
6
- const context = app_1.asyncStore.getStore();
7
- if (context)
8
- app_1.eventHub.emit('log', context, 'info', msgs);
9
- else
10
- console.info('[INFO]', ...msgs);
11
- }
12
- exports.info = info;
13
- function warn(...msgs) {
14
- const context = app_1.asyncStore.getStore();
15
- if (context)
16
- app_1.eventHub.emit('log', context, 'warn', msgs);
17
- else
18
- console.warn('[WARN]', ...msgs);
19
- }
20
- exports.warn = warn;
21
- function error(...msgs) {
22
- const context = app_1.asyncStore.getStore();
23
- if (context)
24
- app_1.eventHub.emit('log', context, 'error', msgs);
25
- else
26
- console.error('[ERROR]', ...msgs);
27
- }
28
- exports.error = error;
29
- function trace(name) {
30
- const context = app_1.asyncStore.getStore();
31
- const trace = { stack: '' };
32
- Error.captureStackTrace(trace);
33
- const stack = trace.stack
34
- .replace(/.*\n.*logger.js.*\n/, name || 'Untitle\n');
35
- if (context)
36
- app_1.eventHub.emit('log', context, 'trace', stack);
37
- else
38
- console.log('[TRACE]', stack);
39
- }
40
- exports.trace = trace;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.trace = exports.error = exports.warn = exports.info = void 0;
4
+ const app_1 = require("./app");
5
+ function info(...msgs) {
6
+ const context = app_1.asyncStore.getStore();
7
+ if (context)
8
+ app_1.eventHub.emit('log', context, 'info', msgs);
9
+ else
10
+ console.info('[INFO]', ...msgs);
11
+ }
12
+ exports.info = info;
13
+ function warn(...msgs) {
14
+ const context = app_1.asyncStore.getStore();
15
+ if (context)
16
+ app_1.eventHub.emit('log', context, 'warn', msgs);
17
+ else
18
+ console.warn('[WARN]', ...msgs);
19
+ }
20
+ exports.warn = warn;
21
+ function error(...msgs) {
22
+ const context = app_1.asyncStore.getStore();
23
+ if (context)
24
+ app_1.eventHub.emit('log', context, 'error', msgs);
25
+ else
26
+ console.error('[ERROR]', ...msgs);
27
+ }
28
+ exports.error = error;
29
+ function trace(name) {
30
+ const context = app_1.asyncStore.getStore();
31
+ const trace = { stack: '' };
32
+ Error.captureStackTrace(trace);
33
+ const stack = trace.stack
34
+ .replace(/.*\n.*logger.js.*\n/, name || 'Untitle\n');
35
+ if (context)
36
+ app_1.eventHub.emit('log', context, 'trace', stack);
37
+ else
38
+ console.log('[TRACE]', stack);
39
+ }
40
+ exports.trace = trace;