oox 0.3.0-beta9 → 0.3.2

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