oox 0.3.0-beta1 → 0.3.0-beta2

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,149 +1,144 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rpc = 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.getConfig = exports.config = exports.Config = exports.Context = exports.on = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
4
- const app = require("./app");
5
- const utils_1 = require("./utils");
6
- const module_1 = require("./modules/module");
7
- exports.Module = module_1.default;
8
- Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
9
- const modules_1 = require("./modules");
10
- exports.modules = new modules_1.default;
11
- 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.on = app.on;
12
- class Context extends app.Context {
13
- // 请求溯源IP
14
- sourceIP = '';
15
- // 请求者IP
16
- ip = '';
17
- // 请求者名称
18
- caller = 'anonymous';
19
- // 请求者ID (长连接专用)
20
- callerId = '';
21
- // 请求者连接把柄
22
- connection;
23
- toJSON() {
24
- const context = Object.assign({}, this);
25
- delete context.connection;
26
- return JSON.stringify(context);
27
- }
28
- }
29
- exports.Context = Context;
30
- class Config {
31
- // 服务名称
32
- name = 'local';
33
- // 主机地址
34
- host = (0, utils_1.getIPAddress)(4)[0];
35
- }
36
- exports.Config = Config;
37
- exports.config = new Config();
38
- function getConfig() {
39
- }
40
- exports.getConfig = getConfig;
41
- let genTraceIdFunction = () => {
42
- const uid = [
43
- Math.floor(Date.now() / 1000).toString(16),
44
- Math.floor(Math.random() * 0xffffffff).toString(16).padStart(8, '0')
45
- ];
46
- return uid.join('');
47
- };
48
- function setGenTraceIdFunction(fn) {
49
- genTraceIdFunction = fn;
50
- }
51
- exports.setGenTraceIdFunction = setGenTraceIdFunction;
52
- /**
53
- * 生成随机不重复id
54
- */
55
- function genTraceId() {
56
- return genTraceIdFunction();
57
- }
58
- exports.genTraceId = genTraceId;
59
- /**
60
- * 获取链路跟踪上下文
61
- */
62
- function genContext(context) {
63
- const newContext = Object.assign(new Context(), context);
64
- if (!newContext.traceId)
65
- newContext.traceId = genTraceId();
66
- if (!newContext.sourceIP)
67
- newContext.sourceIP = newContext.ip;
68
- return newContext;
69
- }
70
- exports.genContext = genContext;
71
- function getContext() {
72
- const context = exports.asyncStore.getStore();
73
- return context || genContext();
74
- }
75
- exports.getContext = getContext;
76
- async function serve() {
77
- await exports.modules.serve();
78
- }
79
- exports.serve = serve;
80
- async function stop() {
81
- await exports.modules.stop();
82
- }
83
- exports.stop = stop;
84
- class RPCKeepAliveConnection {
85
- data;
86
- nativeConnection = null;
87
- adapter = null;
88
- constructor(adapter, nativeConnection, data) {
89
- this.adapter = adapter;
90
- this.nativeConnection = nativeConnection;
91
- if (data)
92
- this.data = data;
93
- }
94
- }
95
- exports.RPCKeepAliveConnection = RPCKeepAliveConnection;
96
- exports.keepAliveConnections = new Map();
97
- function getKeepAliveConnections(name) {
98
- return exports.keepAliveConnections.get(name) || new Map();
99
- }
100
- exports.getKeepAliveConnections = getKeepAliveConnections;
101
- function getKeepAliveConnection(name, id) {
102
- const connections = exports.keepAliveConnections.get(name);
103
- if (!connections)
104
- return null;
105
- return connections.get(id);
106
- }
107
- exports.getKeepAliveConnection = getKeepAliveConnection;
108
- function addKeepAliveConnection(connection) {
109
- const { name, id } = connection.data;
110
- if (exports.keepAliveConnections.has(name)) {
111
- exports.keepAliveConnections.get(name).set(id, connection);
112
- }
113
- else {
114
- exports.keepAliveConnections.set(name, new Map([[id, connection]]));
115
- }
116
- }
117
- exports.addKeepAliveConnection = addKeepAliveConnection;
118
- function removeKeepAliveConnection(name, id) {
119
- if (name instanceof RPCKeepAliveConnection) {
120
- id = name.data.id;
121
- name = name.data.name;
122
- }
123
- if (exports.keepAliveConnections.has(name)) {
124
- const group = exports.keepAliveConnections.get(name);
125
- group.delete(id);
126
- if (!group.size)
127
- exports.keepAliveConnections.delete(name);
128
- }
129
- }
130
- exports.removeKeepAliveConnection = removeKeepAliveConnection;
131
- async function rpc(arg1, action, params, context) {
132
- if (!context || !context.traceId) {
133
- context = getContext();
134
- }
135
- let connection;
136
- if (arg1 instanceof RPCKeepAliveConnection) {
137
- connection = arg1;
138
- }
139
- else if ('string' === typeof arg1) {
140
- const connections = exports.keepAliveConnections.get(arg1);
141
- if (!connections || !connections.size)
142
- throw new Error(`Connection<${arg1}> not found`);
143
- connection = connections.values().next().value;
144
- }
145
- else
146
- throw new Error(`Unknown rpc arg1<${arg1}>`);
147
- return connection.adapter.rpc(connection.nativeConnection, action, params, context);
148
- }
149
- exports.rpc = rpc;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rpc = 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.getConfig = exports.config = exports.Config = exports.Context = exports.on = 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.on = app.on;
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 JSON.stringify(context);
28
+ }
29
+ }
30
+ exports.Context = Context;
31
+ class Config {
32
+ // 服务名称
33
+ name = 'local';
34
+ // 主机地址
35
+ host = (0, utils_1.getIPAddress)(4)[0];
36
+ }
37
+ exports.Config = Config;
38
+ exports.config = new Config();
39
+ function getConfig() {
40
+ }
41
+ exports.getConfig = getConfig;
42
+ let genTraceIdFunction = () => (0, node_crypto_1.randomUUID)();
43
+ function setGenTraceIdFunction(fn) {
44
+ genTraceIdFunction = fn;
45
+ }
46
+ exports.setGenTraceIdFunction = setGenTraceIdFunction;
47
+ /**
48
+ * 生成随机不重复id
49
+ */
50
+ function genTraceId() {
51
+ return genTraceIdFunction();
52
+ }
53
+ exports.genTraceId = genTraceId;
54
+ /**
55
+ * 获取链路跟踪上下文
56
+ */
57
+ function genContext(context) {
58
+ const newContext = Object.assign(new Context(), context);
59
+ if (!newContext.traceId)
60
+ newContext.traceId = genTraceId();
61
+ if (!newContext.sourceIP)
62
+ newContext.sourceIP = newContext.ip;
63
+ return newContext;
64
+ }
65
+ exports.genContext = genContext;
66
+ function getContext() {
67
+ const context = exports.asyncStore.getStore();
68
+ return context || genContext();
69
+ }
70
+ exports.getContext = getContext;
71
+ async function serve() {
72
+ await exports.modules.serve();
73
+ }
74
+ exports.serve = serve;
75
+ async function stop() {
76
+ await exports.modules.stop();
77
+ }
78
+ exports.stop = stop;
79
+ class RPCKeepAliveConnection {
80
+ data;
81
+ nativeConnection = null;
82
+ adapter = null;
83
+ constructor(adapter, nativeConnection, data) {
84
+ this.adapter = adapter;
85
+ this.nativeConnection = nativeConnection;
86
+ if (data)
87
+ this.data = data;
88
+ }
89
+ }
90
+ exports.RPCKeepAliveConnection = RPCKeepAliveConnection;
91
+ exports.keepAliveConnections = new Map();
92
+ function getKeepAliveConnections(name) {
93
+ return exports.keepAliveConnections.get(name) || new Map();
94
+ }
95
+ exports.getKeepAliveConnections = getKeepAliveConnections;
96
+ function getKeepAliveConnection(name, id) {
97
+ const connections = exports.keepAliveConnections.get(name);
98
+ if (!connections)
99
+ return null;
100
+ return connections.get(id);
101
+ }
102
+ exports.getKeepAliveConnection = getKeepAliveConnection;
103
+ function addKeepAliveConnection(connection) {
104
+ const { name, id } = connection.data;
105
+ if (exports.keepAliveConnections.has(name)) {
106
+ exports.keepAliveConnections.get(name).set(id, connection);
107
+ }
108
+ else {
109
+ exports.keepAliveConnections.set(name, new Map([[id, connection]]));
110
+ }
111
+ }
112
+ exports.addKeepAliveConnection = addKeepAliveConnection;
113
+ function removeKeepAliveConnection(name, id) {
114
+ if (name instanceof RPCKeepAliveConnection) {
115
+ id = name.data.id;
116
+ name = name.data.name;
117
+ }
118
+ if (exports.keepAliveConnections.has(name)) {
119
+ const group = exports.keepAliveConnections.get(name);
120
+ group.delete(id);
121
+ if (!group.size)
122
+ exports.keepAliveConnections.delete(name);
123
+ }
124
+ }
125
+ exports.removeKeepAliveConnection = removeKeepAliveConnection;
126
+ async function rpc(arg1, action, params, context) {
127
+ if (!context || !context.traceId) {
128
+ context = getContext();
129
+ }
130
+ let connection;
131
+ if (arg1 instanceof RPCKeepAliveConnection) {
132
+ connection = arg1;
133
+ }
134
+ else if ('string' === typeof arg1) {
135
+ const connections = exports.keepAliveConnections.get(arg1);
136
+ if (!connections || !connections.size)
137
+ throw new Error(`Connection<${arg1}> not found`);
138
+ connection = connections.values().next().value;
139
+ }
140
+ else
141
+ throw new Error(`Unknown rpc arg1<${arg1}>`);
142
+ return connection.adapter.rpc(connection.nativeConnection, action, params, context);
143
+ }
144
+ exports.rpc = rpc;
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
-
2
- import * as oox from './index.js'
3
-
1
+
2
+ import * as oox from './index.js'
3
+
4
4
  export default oox