oox 0.2.0 → 0.3.0-beta3

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 (46) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +32 -29
  3. package/app.js +142 -0
  4. package/bin/argv.js +70 -95
  5. package/bin/cli.js +43 -58
  6. package/bin/configurer.js +49 -78
  7. package/bin/proxyer.js +61 -95
  8. package/bin/register.js +55 -105
  9. package/bin/starter.js +81 -144
  10. package/index.js +144 -6
  11. package/index.mjs +4 -0
  12. package/modules/http/index.js +180 -0
  13. package/modules/http/utils.js +73 -0
  14. package/modules/index.js +81 -0
  15. package/modules/module.js +16 -0
  16. package/package.json +34 -16
  17. package/types/app.d.ts +37 -0
  18. package/types/bin/argv.d.ts +8 -0
  19. package/types/bin/cli.d.ts +2 -0
  20. package/types/bin/configurer.d.ts +1 -0
  21. package/types/bin/proxyer.d.ts +1 -0
  22. package/types/bin/register.d.ts +1 -0
  23. package/types/bin/starter.d.ts +1 -0
  24. package/types/index.d.ts +70 -0
  25. package/types/modules/http/index.d.ts +47 -0
  26. package/types/modules/http/utils.d.ts +17 -0
  27. package/types/modules/index.d.ts +22 -0
  28. package/types/modules/module.d.ts +13 -0
  29. package/types/utils.d.ts +6 -0
  30. package/utils.js +63 -0
  31. package/.gitattributes +0 -2
  32. package/global.js +0 -118
  33. package/middleware.js +0 -167
  34. package/rpc/config.class.js +0 -48
  35. package/rpc/context.class.js +0 -15
  36. package/rpc/http.class.js +0 -312
  37. package/rpc/rpc.class.js +0 -231
  38. package/rpc/rpc.interface.class.js +0 -119
  39. package/rpc/socketio.class.js +0 -223
  40. package/service/service.class.js +0 -74
  41. package/service/socketio.class.js +0 -145
  42. package/setMap.class.js +0 -67
  43. package/socketio/client.class.js +0 -190
  44. package/socketio/server.class.js +0 -222
  45. package/socketio/socket.class.js +0 -23
  46. package/util.js +0 -224
@@ -0,0 +1,70 @@
1
+ /// <reference types="node" />
2
+ import * as app from './app';
3
+ import Module, { ModuleConfig } from './modules/module';
4
+ import Modules from './modules';
5
+ export { ReturnsBody } from './app';
6
+ export { Module, ModuleConfig };
7
+ export declare const modules: Modules;
8
+ export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, on: typeof app.on;
9
+ export declare class Context extends app.Context {
10
+ sourceIP: string;
11
+ ip: string;
12
+ caller: string;
13
+ callerId: string;
14
+ connection?: RPCKeepAliveConnection;
15
+ toJSON?(): string;
16
+ }
17
+ export declare class Config {
18
+ [x: string]: any;
19
+ name: string;
20
+ host: string;
21
+ }
22
+ export declare const config: Config;
23
+ export declare function getConfig(): void;
24
+ export declare function setGenTraceIdFunction(fn: () => string): void;
25
+ /**
26
+ * 生成随机不重复id
27
+ */
28
+ export declare function genTraceId(): string;
29
+ /**
30
+ * 获取链路跟踪上下文
31
+ */
32
+ export declare function genContext(context?: Context): Context;
33
+ export declare function getContext(): Context;
34
+ export declare function serve(): Promise<void>;
35
+ export declare function stop(): Promise<void>;
36
+ export interface RPCConnectionAdapter {
37
+ rpc(connection: any, action: string, params: any[], context: Context): Promise<any>;
38
+ }
39
+ export interface RPCKeepAliveConnectionData {
40
+ /**
41
+ * 是否连接成功
42
+ */
43
+ connected: boolean;
44
+ /**
45
+ * 连接主机地址
46
+ */
47
+ host: string;
48
+ /**
49
+ * 连接服务名称
50
+ */
51
+ name: string;
52
+ /**
53
+ * 目标服务网络唯一ID
54
+ */
55
+ id: string;
56
+ }
57
+ export declare class RPCKeepAliveConnection {
58
+ data: RPCKeepAliveConnectionData;
59
+ nativeConnection: any;
60
+ adapter: RPCConnectionAdapter;
61
+ constructor(adapter: RPCConnectionAdapter, nativeConnection: any, data?: RPCKeepAliveConnectionData);
62
+ }
63
+ export declare const keepAliveConnections: Map<string, Map<string, RPCKeepAliveConnection>>;
64
+ export declare function getKeepAliveConnections(name: string): Map<string, RPCKeepAliveConnection>;
65
+ export declare function getKeepAliveConnection(name: string, id: string): RPCKeepAliveConnection | null;
66
+ export declare function addKeepAliveConnection(connection: RPCKeepAliveConnection): void;
67
+ export declare function removeKeepAliveConnection(connection: RPCKeepAliveConnection): void;
68
+ export declare function removeKeepAliveConnection(name: string, id: string): void;
69
+ export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
70
+ export declare function rpc(connection: RPCKeepAliveConnection, action: string, params: any[], context?: Context): Promise<any>;
@@ -0,0 +1,47 @@
1
+ /// <reference types="node" />
2
+ import * as http from 'node:http';
3
+ import * as oox from '../../index';
4
+ import Module, { ModuleConfig } from '../module';
5
+ export declare class HTTPConfig extends ModuleConfig {
6
+ port: number;
7
+ path: string;
8
+ origin: string;
9
+ }
10
+ export default class HTTPModule extends Module {
11
+ name: string;
12
+ config: HTTPConfig;
13
+ server: http.Server;
14
+ setConfig(config: HTTPConfig): void;
15
+ getConfig(): HTTPConfig;
16
+ /**
17
+ * start http service
18
+ */
19
+ serve(): Promise<void>;
20
+ /**
21
+ * stop http service
22
+ */
23
+ stop(): Promise<void>;
24
+ /**
25
+ * browser cross origin
26
+ */
27
+ cors(request: http.IncomingMessage, response: http.ServerResponse): boolean;
28
+ /**
29
+ * HTTP-RPC服务器请求监听方法
30
+ */
31
+ call(request: http.IncomingMessage, response: http.ServerResponse): Promise<void>;
32
+ /**
33
+ * HTTP Response Catch
34
+ */
35
+ respond(request: http.IncomingMessage, response: http.ServerResponse, format: {
36
+ body?: any;
37
+ success: boolean;
38
+ error?: {
39
+ message: any;
40
+ stack: any;
41
+ };
42
+ }): void;
43
+ /**
44
+ * HTTP RPC
45
+ */
46
+ rpc(url: string | URL, action: string, params: Array<any>, context?: oox.Context): Promise<any>;
47
+ }
@@ -0,0 +1,17 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ import * as http from 'node:http';
5
+ import * as stream from 'node:stream';
6
+ /**
7
+ * Stream => Buffer
8
+ */
9
+ export declare function stream2buffer(stream: stream.Readable, totalLength?: number): Promise<Buffer>;
10
+ /**
11
+ * Request => JSONObject
12
+ */
13
+ export declare function parseHTTPBody(request: http.IncomingMessage): Promise<any>;
14
+ /**
15
+ * http request
16
+ */
17
+ export declare function httpRequest(url: URL | string, options: http.RequestOptions, body: string): Promise<any>;
@@ -0,0 +1,22 @@
1
+ import Module from './module';
2
+ import HTTP from './http';
3
+ export default class Modules extends Module {
4
+ #private;
5
+ /**
6
+ * the module unique name
7
+ */
8
+ name: string;
9
+ /**
10
+ * all builtin modules
11
+ */
12
+ builtins: {
13
+ http: HTTP;
14
+ };
15
+ constructor();
16
+ add(module: Module): this;
17
+ get(name: string): Module;
18
+ remove(name: string): Promise<void>;
19
+ setConfig(config: any): void;
20
+ serve(): Promise<void>;
21
+ stop(): Promise<void>;
22
+ }
@@ -0,0 +1,13 @@
1
+ export declare class ModuleConfig {
2
+ [x: string]: any;
3
+ disabled: boolean;
4
+ }
5
+ export default class Module {
6
+ [x: string]: any;
7
+ config: ModuleConfig;
8
+ name: string;
9
+ setConfig(config: any): void;
10
+ getConfig(): ModuleConfig;
11
+ serve(): Promise<void>;
12
+ stop(): Promise<void>;
13
+ }
@@ -0,0 +1,6 @@
1
+ export declare function getIPAddress(version?: number): string[];
2
+ export declare function getAllCallablePropertyNames(obj: any): string[];
3
+ export declare function genKVMethods(methods: any, kvMethods?: Map<string, Function>, sourceKVMethods?: Map<string, Function>, nameStack?: string[]): {
4
+ kvMethods: Map<string, Function>;
5
+ sourceKVMethods: Map<string, Function>;
6
+ };
package/utils.js ADDED
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.genKVMethods = exports.getAllCallablePropertyNames = exports.getIPAddress = void 0;
4
+ const os_1 = require("os");
5
+ function getIPAddress(version = 4) {
6
+ const interfaces = (0, os_1.networkInterfaces)();
7
+ const ip = [];
8
+ for (const name of Object.keys(interfaces))
9
+ for (const intf of interfaces[name])
10
+ if (intf.mac !== '00:00:00:00:00:00')
11
+ if ((version !== 4 && version !== 6) || 'IPv' + version === intf.family)
12
+ ip.push(intf.address);
13
+ if (!ip.length) {
14
+ if (version !== 6)
15
+ ip.push('127.0.0.1');
16
+ if (version !== 4)
17
+ ip.push('::1');
18
+ }
19
+ return ip;
20
+ }
21
+ exports.getIPAddress = getIPAddress;
22
+ function getAllCallablePropertyNames(obj) {
23
+ if (!obj)
24
+ return [];
25
+ let props = [], tmpProps = [], index = 0, size = 0, tmpProp = '';
26
+ const bans = ["constructor", "__defineGetter__", "__defineSetter__",
27
+ "hasOwnProperty", "__lookupGetter__", "__lookupSetter__",
28
+ "isPrototypeOf", "propertyIsEnumerable", "toString",
29
+ "valueOf", "__proto__", "toLocaleString"];
30
+ do {
31
+ tmpProps = Object.getOwnPropertyNames(obj);
32
+ index = -1, size = tmpProps.length;
33
+ while (++index < size) {
34
+ tmpProp = tmpProps[index];
35
+ if (!props.includes(tmpProp) && !bans.includes(tmpProp)) {
36
+ props.push(tmpProp);
37
+ }
38
+ }
39
+ } while (obj = Object.getPrototypeOf(obj));
40
+ return props;
41
+ }
42
+ exports.getAllCallablePropertyNames = getAllCallablePropertyNames;
43
+ function genKVMethods(methods, kvMethods = new Map(), sourceKVMethods = new Map(), nameStack = []) {
44
+ let keys = getAllCallablePropertyNames(methods);
45
+ for (const key of keys) {
46
+ /**
47
+ * @type {Function}
48
+ */
49
+ let val = methods[key];
50
+ if ('function' === typeof val) {
51
+ const action = nameStack.concat(key).join('.');
52
+ // 原函数绑定
53
+ sourceKVMethods.set(action, val);
54
+ // 壳函数绑定
55
+ kvMethods.set(action, val.bind(methods));
56
+ }
57
+ else {
58
+ genKVMethods(val, kvMethods, sourceKVMethods, nameStack.concat(key));
59
+ }
60
+ }
61
+ return { kvMethods, sourceKVMethods };
62
+ }
63
+ exports.genKVMethods = genKVMethods;
package/.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
package/global.js DELETED
@@ -1,118 +0,0 @@
1
-
2
- if ( Error.stackTraceLimit < 20 ) Error.stackTraceLimit = 20
3
-
4
- const { AsyncLocalStorage } = require ( 'async_hooks' )
5
-
6
- const Context = require ( './rpc/context.class' )
7
-
8
- const RPC = require ( './rpc/rpc.interface.class' )
9
-
10
- const Socket = require ( './socketio/socket.class' )
11
-
12
- const SetMap = require ( './setMap.class' )
13
-
14
- const Middleware = require ( './middleware' )
15
-
16
-
17
-
18
- const Global = {
19
-
20
-
21
-
22
- asyncStore: new AsyncLocalStorage ( ),
23
-
24
-
25
-
26
- md: Middleware.handler,
27
-
28
-
29
-
30
- /**
31
- * @type {[RPC]}
32
- */
33
- instances: [ ],
34
-
35
-
36
-
37
- /**
38
- * @type {Map<String,Socket>}
39
- */
40
- sockets: new Map ( ),
41
-
42
-
43
-
44
- /**
45
- * @type {SocketIOServer[]}
46
- */
47
- socketIOServers: [ ],
48
-
49
-
50
-
51
- /**
52
- * @type {Map<String,Set<Socket>>}
53
- */
54
- socketIORegistry: new SetMap,
55
-
56
-
57
-
58
- /**
59
- * 生成随机不重复id
60
- * @returns {String}
61
- */
62
- genTraceId ( ) {
63
-
64
- const uid = [
65
- Math.floor ( Date.now ( ) / 1000 ).toString ( 16 ),
66
- Math.floor ( Math.random ( ) * 0xffffffff ).toString ( 16 ).padStart ( 8, '0' )
67
- ]
68
-
69
- return uid.join ( '' )
70
- },
71
-
72
-
73
-
74
- /**
75
- * 获取链路跟踪上下文
76
- * @param {Context} param0
77
- * @returns {Context}
78
- */
79
- genContext ( { caller, callerId, traceId, ip, sourceIP } = { } ) {
80
-
81
- const context = new Context ( )
82
-
83
- if ( caller ) {
84
-
85
- context.caller = caller
86
- } else {
87
-
88
- const primaryService = this.instances [ 0 ]
89
-
90
- if ( primaryService ) context.caller = primaryService.name
91
- }
92
-
93
- context.traceId = traceId || this.genTraceId ( )
94
- context.ip = ip
95
- context.sourceIP = sourceIP || ip
96
- context.callerId = callerId
97
-
98
- return context
99
- },
100
-
101
-
102
-
103
- /**
104
- *
105
- * @returns {Context}
106
- */
107
- getContext ( ) {
108
-
109
- const context = this.asyncStore.getStore ( )
110
-
111
- return context || this.genContext ( )
112
- },
113
- }
114
-
115
- /**
116
- * @type {Global}
117
- */
118
- global.oox = module.exports = global.oox || Global
package/middleware.js DELETED
@@ -1,167 +0,0 @@
1
-
2
-
3
-
4
- /**
5
- * Map<中间件名字,中间件函数>
6
- * @type {Map<String,Function>}
7
- */
8
- exports.middlewares = new Map ( )
9
-
10
-
11
-
12
- /**
13
- * WeakMap<殼函數,原函數>
14
- * @type {Map<String,Function>}
15
- */
16
- exports.wrappedActions = new Map ( )
17
-
18
-
19
-
20
- /**
21
- * Map<中间件名字,Set<接口函数>>
22
- * @type {Map<String,Set<Function>>}
23
- */
24
- exports.middlewareActions = new Map ( )
25
-
26
-
27
-
28
- /**
29
- * WeakMap<接口函数,[中间件名字]>
30
- * @type {WeakMap<Function,[String]>}
31
- */
32
- exports.actionMiddlewares = new WeakMap ( )
33
-
34
-
35
-
36
- /**
37
- * @type {Map<String,Proxy>}
38
- */
39
- exports.middlewareProxys = new Map ( )
40
-
41
-
42
-
43
- /**
44
- *
45
- * @param {String} key
46
- * @param {Function} middleware
47
- * @returns {(Function)=>{}}
48
- */
49
- exports.define = ( key, middleware ) => {
50
-
51
- if ( middleware ) {
52
-
53
- exports.middlewares.set ( key, middleware )
54
-
55
- exports.middlewareActions.set ( key, new Set ( ) )
56
-
57
- const proxy = new Proxy ( middleware, {
58
-
59
- has ( target, action ) {
60
-
61
- const sourceMethod = exports.wrappedActions.get ( action )
62
-
63
- return exports.middlewareActions.get ( key ).has ( sourceMethod )
64
- }
65
- } )
66
-
67
- // save proxy
68
- exports.middlewareProxys.set ( key, proxy )
69
- } else {
70
-
71
- return arg => exports.define ( key, arg )
72
- }
73
- }
74
-
75
-
76
-
77
- /**
78
- *
79
- * @param {String} key
80
- * @param {Function} action
81
- * @param {'start'|'end'} place
82
- */
83
- exports.use = ( key, action, place='end' ) => {
84
-
85
- if ( !exports.middlewareActions.has ( key ) ) exports.middlewareActions.set ( key, new Set ( ) )
86
-
87
- if ( !exports.actionMiddlewares.has ( action ) ) exports.actionMiddlewares.set ( action, [ ] )
88
-
89
- // 双向绑定
90
-
91
- exports.middlewareActions.get ( key ).add ( action )
92
-
93
- if ( place === 'end' ) {
94
-
95
- exports.actionMiddlewares.get ( action ).push ( key )
96
- } else {
97
-
98
- exports.actionMiddlewares.get ( action ).unshift ( key )
99
- }
100
- }
101
-
102
-
103
-
104
- exports.delete = ( key ) => {
105
-
106
- exports.middlewares.delete ( key )
107
-
108
- if ( exports.middlewareActions.has ( key ) ) {
109
-
110
- const actions = exports.middlewareActions.get ( key ).values ( )
111
-
112
- // remove action bindings
113
- for ( const action of actions ) {
114
-
115
- const middlewareNames = exports.actionMiddlewares.get ( action ).filter ( value => value !== key )
116
-
117
- exports.actionMiddlewares.set ( action, middlewareNames )
118
- }
119
- }
120
-
121
- exports.middlewareActions.get ( key ).clear ( )
122
-
123
- exports.middlewareActions.delete ( key )
124
- }
125
-
126
-
127
-
128
- exports.handler = new Proxy ( o=>o, {
129
-
130
-
131
-
132
- get ( target, key ) {
133
-
134
- return exports.middlewareProxys.get ( key )
135
- },
136
-
137
-
138
-
139
- set ( target, key, action ) {
140
-
141
- if ( exports.middlewares.has ( key ) ) {
142
-
143
- exports.use ( key, action, 'start' )
144
- } else {
145
-
146
- exports.define ( key, action )
147
- }
148
-
149
- return true
150
- },
151
-
152
-
153
-
154
- deleteProperty ( target, key ) {
155
-
156
- exports.delete ( key )
157
-
158
- return true
159
- },
160
-
161
-
162
-
163
- apply ( target, thisArg, args ) {
164
-
165
- return exports.define.apply ( exports, args )
166
- }
167
- } )
@@ -1,48 +0,0 @@
1
-
2
- module.exports = class Config {
3
-
4
-
5
-
6
- /**
7
- * 服务名称
8
- */
9
- name = ''
10
-
11
-
12
-
13
- /**
14
- * 主机地址列表
15
- */
16
- host = ''
17
-
18
-
19
-
20
- /**
21
- * 跨域处理
22
- */
23
- origin = null
24
-
25
-
26
-
27
- /**
28
- * 网关地址
29
- */
30
- gateway = {
31
-
32
-
33
-
34
- /**
35
- * http 网关地址
36
- * @type {{port:Number,path:String}}
37
- */
38
- http: null,
39
-
40
-
41
-
42
- /**
43
- * socketio 网关地址
44
- * @type {{port:Number,path:String}}
45
- */
46
- socketio: null
47
- }
48
- }
@@ -1,15 +0,0 @@
1
-
2
-
3
- module.exports = class Context {
4
-
5
- // 请求溯源IP
6
- sourceIP = ''
7
- // 请求者IP
8
- ip = ''
9
- // 请求溯源ID
10
- traceId = ''
11
- // 请求者名称
12
- caller = ''
13
- // 请求者ID (长连接专用)
14
- callerId = ''
15
- }