oox 0.3.0-beta13 → 0.3.0-beta14
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 +21 -21
- package/README.md +32 -32
- package/app.js +29 -41
- package/bin/argv.js +4 -11
- package/bin/cli.js +19 -19
- package/bin/configurer.js +5 -9
- package/bin/loader.mjs +379 -459
- package/bin/proxyer.js +8 -10
- package/bin/register.js +6 -10
- package/bin/starter.js +42 -34
- package/index.js +44 -59
- package/index.mjs +4 -4
- package/logger.js +13 -20
- package/modules/http/index.js +35 -24
- package/modules/http/utils.js +11 -16
- package/modules/index.js +6 -9
- package/modules/module.js +2 -7
- package/modules/socketio/client.js +8 -11
- package/modules/socketio/index.js +17 -21
- package/modules/socketio/server.js +11 -16
- package/modules/socketio/socket.js +1 -4
- package/package.json +7 -7
- package/types/app.d.ts +2 -4
- package/types/bin/cli.d.ts +2 -2
- package/types/bin/configurer.d.ts +1 -1
- package/types/bin/proxyer.d.ts +1 -1
- package/types/bin/starter.d.ts +5 -2
- package/types/index.d.ts +7 -6
- package/types/modules/http/index.d.ts +6 -3
- package/types/modules/http/utils.d.ts +0 -3
- package/types/modules/index.d.ts +3 -3
- package/types/modules/socketio/client.d.ts +2 -2
- package/types/modules/socketio/index.d.ts +4 -4
- package/types/modules/socketio/server.d.ts +2 -3
- package/types/modules/socketio/socket.d.ts +2 -2
- package/utils.js +5 -11
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const oox = require("../../index");
|
|
7
|
-
class SocketIOCore extends server_1.default {
|
|
1
|
+
import * as SocketIOClient from 'socket.io-client';
|
|
2
|
+
import { sockets } from './socket.js';
|
|
3
|
+
import SocketIOServer from './server.js';
|
|
4
|
+
import * as oox from '../../index.js';
|
|
5
|
+
export default class SocketIOCore extends SocketIOServer {
|
|
8
6
|
/**
|
|
9
7
|
* connect to <SocketIO RPC> service
|
|
10
8
|
*/
|
|
11
9
|
async connect(url) {
|
|
12
|
-
let socket =
|
|
10
|
+
let socket = sockets.get(url);
|
|
13
11
|
// 已经连接的直接返回
|
|
14
12
|
if (socket) {
|
|
15
13
|
try {
|
|
@@ -35,7 +33,7 @@ class SocketIOCore extends server_1.default {
|
|
|
35
33
|
path: mURL.pathname
|
|
36
34
|
});
|
|
37
35
|
socket.data = { name: 'anonymous', connected: false, id: url, host: mURL.host };
|
|
38
|
-
|
|
36
|
+
sockets.set(url, socket);
|
|
39
37
|
try {
|
|
40
38
|
await this.clientWaitConnection(socket);
|
|
41
39
|
}
|
|
@@ -62,7 +60,7 @@ class SocketIOCore extends server_1.default {
|
|
|
62
60
|
clientOnSocketDisconnect(socket, reason) {
|
|
63
61
|
socket.data.connected = false;
|
|
64
62
|
socket.disconnect();
|
|
65
|
-
|
|
63
|
+
sockets.delete(socket.data.id);
|
|
66
64
|
this.clientOnDisconnect(socket, reason);
|
|
67
65
|
}
|
|
68
66
|
/**
|
|
@@ -98,4 +96,3 @@ class SocketIOCore extends server_1.default {
|
|
|
98
96
|
this.clientOnSocketConnection(socket);
|
|
99
97
|
}
|
|
100
98
|
}
|
|
101
|
-
exports.default = SocketIOCore;
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Object.defineProperty(exports, "sockets", { enumerable: true, get: function () { return socket_1.sockets; } });
|
|
10
|
-
class SocketIOModule extends client_1.default {
|
|
11
|
-
sockets = socket_1.sockets;
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
import SocketIOClient from './client.js';
|
|
3
|
+
import * as oox from '../../index.js';
|
|
4
|
+
import { RPCKeepAliveConnection, removeKeepAliveConnection, addKeepAliveConnection, getKeepAliveConnections } from '../../index.js';
|
|
5
|
+
import { sockets } from './socket.js';
|
|
6
|
+
export { sockets };
|
|
7
|
+
export default class SocketIOModule extends SocketIOClient {
|
|
8
|
+
sockets = sockets;
|
|
12
9
|
async serve() {
|
|
13
10
|
await this.stop();
|
|
14
11
|
const _http = oox.modules.builtins.http;
|
|
@@ -27,7 +24,7 @@ class SocketIOModule extends client_1.default {
|
|
|
27
24
|
await super.serve();
|
|
28
25
|
}
|
|
29
26
|
onSyncConnection(socket) {
|
|
30
|
-
const mSockets = Array.from(
|
|
27
|
+
const mSockets = Array.from(sockets.values())
|
|
31
28
|
.filter(s => s !== socket &&
|
|
32
29
|
s.data.name !== socket.data.name &&
|
|
33
30
|
s.data.id.startsWith('ws://'));
|
|
@@ -35,11 +32,11 @@ class SocketIOModule extends client_1.default {
|
|
|
35
32
|
}
|
|
36
33
|
serverOnDisconnect(socket, reason) {
|
|
37
34
|
super.serverOnDisconnect(socket, reason);
|
|
38
|
-
|
|
35
|
+
removeKeepAliveConnection(socket.data.name, socket.data.id);
|
|
39
36
|
}
|
|
40
37
|
clientOnDisconnect(socket, reason) {
|
|
41
38
|
super.clientOnDisconnect(socket, reason);
|
|
42
|
-
|
|
39
|
+
removeKeepAliveConnection(socket.data.name, socket.data.id);
|
|
43
40
|
}
|
|
44
41
|
/**
|
|
45
42
|
*
|
|
@@ -48,7 +45,7 @@ class SocketIOModule extends client_1.default {
|
|
|
48
45
|
*/
|
|
49
46
|
clientOnSyncConnection(socket, connectionDatas) {
|
|
50
47
|
for (const data of connectionDatas)
|
|
51
|
-
if (!
|
|
48
|
+
if (!sockets.has(data.id))
|
|
52
49
|
this.connect(data.id).catch((error) => console.error(error));
|
|
53
50
|
}
|
|
54
51
|
onFetchActions(socket, search) {
|
|
@@ -59,13 +56,13 @@ class SocketIOModule extends client_1.default {
|
|
|
59
56
|
return data;
|
|
60
57
|
}
|
|
61
58
|
fetchActions(id, search = '') {
|
|
62
|
-
let socket =
|
|
59
|
+
let socket = sockets.get(id);
|
|
63
60
|
if (!socket) {
|
|
64
|
-
const connections =
|
|
61
|
+
const connections = getKeepAliveConnections(id);
|
|
65
62
|
if (!connections || !connections.size)
|
|
66
63
|
throw new Error(`Unknown service identify<${id}>`);
|
|
67
64
|
id = connections.keys().next().value;
|
|
68
|
-
socket =
|
|
65
|
+
socket = sockets.get(id);
|
|
69
66
|
}
|
|
70
67
|
if (!socket)
|
|
71
68
|
throw new Error(`Unknown service identify<${id}>`);
|
|
@@ -73,8 +70,8 @@ class SocketIOModule extends client_1.default {
|
|
|
73
70
|
}
|
|
74
71
|
onConnection(socket) {
|
|
75
72
|
const { id, name, host } = socket.data;
|
|
76
|
-
const connection = new
|
|
77
|
-
|
|
73
|
+
const connection = new RPCKeepAliveConnection(this, id, socket.data);
|
|
74
|
+
addKeepAliveConnection(connection);
|
|
78
75
|
const connectionContext = {
|
|
79
76
|
sourceIP: '',
|
|
80
77
|
ip: host,
|
|
@@ -165,4 +162,3 @@ class SocketIOModule extends client_1.default {
|
|
|
165
162
|
return body;
|
|
166
163
|
}
|
|
167
164
|
}
|
|
168
|
-
exports.default = SocketIOModule;
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const index_1 = require("../../index");
|
|
8
|
-
const socket_1 = require("./socket");
|
|
9
|
-
class SocketIOConfig extends index_1.ModuleConfig {
|
|
1
|
+
import * as http from 'node:http';
|
|
2
|
+
import { Server } from 'socket.io';
|
|
3
|
+
import * as oox from '../../index.js';
|
|
4
|
+
import { Module, ModuleConfig } from '../../index.js';
|
|
5
|
+
import { sockets } from './socket.js';
|
|
6
|
+
export class SocketIOConfig extends ModuleConfig {
|
|
10
7
|
// listen port
|
|
11
8
|
port = 0;
|
|
12
9
|
// service path
|
|
@@ -14,8 +11,7 @@ class SocketIOConfig extends index_1.ModuleConfig {
|
|
|
14
11
|
// browser cross origin
|
|
15
12
|
origin = '';
|
|
16
13
|
}
|
|
17
|
-
|
|
18
|
-
class SocketIOServer extends index_1.Module {
|
|
14
|
+
export default class SocketIOServer extends Module {
|
|
19
15
|
name = 'socketio';
|
|
20
16
|
config = new SocketIOConfig;
|
|
21
17
|
/**
|
|
@@ -90,7 +86,7 @@ class SocketIOServer extends index_1.Module {
|
|
|
90
86
|
return options;
|
|
91
87
|
}
|
|
92
88
|
createSocketIOServer() {
|
|
93
|
-
const socketServer = this.socketServer = new
|
|
89
|
+
const socketServer = this.socketServer = new Server(this.server, this.genSocketIOServerOptions());
|
|
94
90
|
socketServer.on('connection', async (socket) => {
|
|
95
91
|
try {
|
|
96
92
|
this.serverOnSocketConnection(socket);
|
|
@@ -107,7 +103,7 @@ class SocketIOServer extends index_1.Module {
|
|
|
107
103
|
const headers = socket.handshake.headers;
|
|
108
104
|
const callerId = String(headers['x-caller-id'] || '') || socket.id;
|
|
109
105
|
// 已经存在相同的连接
|
|
110
|
-
if (
|
|
106
|
+
if (sockets.has(callerId))
|
|
111
107
|
throw new Error('Connection Exists');
|
|
112
108
|
// client ip or caller service ip
|
|
113
109
|
const ip = String(headers['x-real-ip'] || headers['x-ip'] || socket.handshake.address);
|
|
@@ -115,7 +111,7 @@ class SocketIOServer extends index_1.Module {
|
|
|
115
111
|
const caller = String(headers['x-caller'] || 'anonymous');
|
|
116
112
|
socket.data = { connected: true, host: ip, name: caller, id: callerId };
|
|
117
113
|
// 保存 callerId 与 socket 对应关系
|
|
118
|
-
|
|
114
|
+
sockets.set(callerId, socket);
|
|
119
115
|
socket.on('disconnect', reason => this.serverOnSocketDisconnect(socket, reason));
|
|
120
116
|
socket.emit('oox_connected', { name: oox.config.name });
|
|
121
117
|
this.serverOnConnection(socket);
|
|
@@ -128,9 +124,8 @@ class SocketIOServer extends index_1.Module {
|
|
|
128
124
|
*/
|
|
129
125
|
serverOnSocketDisconnect(socket, reason) {
|
|
130
126
|
socket.data.connected = false;
|
|
131
|
-
|
|
127
|
+
sockets.delete(socket.data.id);
|
|
132
128
|
this.serverOnDisconnect(socket, reason);
|
|
133
129
|
}
|
|
134
130
|
serverOnDisconnect(socket, reason) { }
|
|
135
131
|
}
|
|
136
|
-
exports.default = SocketIOServer;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oox",
|
|
3
|
-
"version": "0.3.0-
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0-beta14",
|
|
4
|
+
"description": "OOX Service Engine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"http",
|
|
7
7
|
"websocket",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"distributed",
|
|
13
13
|
"tracing"
|
|
14
14
|
],
|
|
15
|
-
"type": "
|
|
15
|
+
"type": "module",
|
|
16
16
|
"main": "index.js",
|
|
17
17
|
"types": "types/index.d.ts",
|
|
18
18
|
"exports": {
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
"homepage": "https://github.com/lipingruan/oox",
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"chalk": "^
|
|
34
|
-
"socket.io": "^4.
|
|
35
|
-
"socket.io-client": "^4.
|
|
33
|
+
"chalk": "^5.6.2",
|
|
34
|
+
"socket.io": "^4.8.3",
|
|
35
|
+
"socket.io-client": "^4.8.3"
|
|
36
36
|
},
|
|
37
37
|
"engines": {
|
|
38
|
-
"node": ">=
|
|
38
|
+
"node": ">=18.0.0"
|
|
39
39
|
}
|
|
40
40
|
}
|
package/types/app.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
1
|
import { EventEmitter } from 'node:events';
|
|
4
2
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
-
export * as logger from './logger';
|
|
3
|
+
export * as logger from './logger.js';
|
|
6
4
|
export interface ReturnsBody {
|
|
7
5
|
traceId: string;
|
|
8
6
|
success: boolean;
|
|
@@ -17,7 +15,7 @@ export declare class Context {
|
|
|
17
15
|
traceId?: string;
|
|
18
16
|
}
|
|
19
17
|
export declare const asyncStore: AsyncLocalStorage<Context>;
|
|
20
|
-
export declare const eventHub: EventEmitter
|
|
18
|
+
export declare const eventHub: EventEmitter<[never]>;
|
|
21
19
|
/**
|
|
22
20
|
* the kvMethods is all actions refs [has bind this]
|
|
23
21
|
*/
|
package/types/bin/cli.d.ts
CHANGED
package/types/bin/proxyer.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function proxyGroup(groupDirectory: string, excludes?:
|
|
1
|
+
export declare function proxyGroup(groupDirectory: string, excludes?: string[]): void;
|
package/types/bin/starter.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import * as oox from '../index.js';
|
|
2
|
+
export declare function registLoader(): Promise<void>;
|
|
3
|
+
export declare function configure(env?: {
|
|
2
4
|
[x: string]: any;
|
|
3
|
-
}, entryFilename?: string): Promise<
|
|
5
|
+
}, entryFilename?: string): Promise<oox.Config>;
|
|
6
|
+
export declare function startup(): Promise<void>;
|
package/types/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
export { ReturnsBody } from './app';
|
|
1
|
+
import * as app from './app.js';
|
|
2
|
+
import Module, { ModuleConfig } from './modules/module.js';
|
|
3
|
+
import Modules from './modules/index.js';
|
|
4
|
+
export { ReturnsBody } from './app.js';
|
|
6
5
|
export { Module, ModuleConfig };
|
|
7
6
|
export declare const modules: Modules;
|
|
8
7
|
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, logger: typeof app.logger, on: typeof app.on, once: typeof app.once, off: typeof app.off, emit: typeof app.emit;
|
|
@@ -17,7 +16,9 @@ export declare class Context extends app.Context {
|
|
|
17
16
|
export declare class Config {
|
|
18
17
|
[x: string]: any;
|
|
19
18
|
name: string;
|
|
20
|
-
|
|
19
|
+
group: string;
|
|
20
|
+
ignore: string[];
|
|
21
|
+
entryInfo: {
|
|
21
22
|
path: string;
|
|
22
23
|
group: string;
|
|
23
24
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import * as http from 'node:http';
|
|
3
|
-
import * as oox from '../../index';
|
|
4
|
-
import Module, { ModuleConfig } from '../module';
|
|
2
|
+
import * as oox from '../../index.js';
|
|
3
|
+
import Module, { ModuleConfig } from '../module.js';
|
|
5
4
|
export declare class HTTPConfig extends ModuleConfig {
|
|
6
5
|
port: number;
|
|
7
6
|
path: string;
|
|
@@ -25,6 +24,10 @@ export default class HTTPModule extends Module {
|
|
|
25
24
|
* browser cross origin
|
|
26
25
|
*/
|
|
27
26
|
cors(request: http.IncomingMessage, response: http.ServerResponse): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* 从请求里获取调用的接口和参数
|
|
29
|
+
*/
|
|
30
|
+
getCallArgsFromRequest(request: http.IncomingMessage): Promise<void>;
|
|
28
31
|
/**
|
|
29
32
|
* HTTP-RPC服务器请求监听方法
|
|
30
33
|
*/
|
package/types/modules/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Module from './module';
|
|
2
|
-
import HTTP from './http';
|
|
3
|
-
import SocketIO from './socketio';
|
|
1
|
+
import Module from './module.js';
|
|
2
|
+
import HTTP from './http/index.js';
|
|
3
|
+
import SocketIO from './socketio/index.js';
|
|
4
4
|
export default class Modules extends Module {
|
|
5
5
|
#private;
|
|
6
6
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ClientSocket as Socket } from './socket';
|
|
2
|
-
import SocketIOServer from './server';
|
|
1
|
+
import { ClientSocket as Socket } from './socket.js';
|
|
2
|
+
import SocketIOServer from './server.js';
|
|
3
3
|
export default class SocketIOCore extends SocketIOServer {
|
|
4
4
|
/**
|
|
5
5
|
* connect to <SocketIO RPC> service
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import SocketIOClient from './client';
|
|
2
|
-
import * as oox from '../../index';
|
|
3
|
-
import { RPCKeepAliveConnectionData, RPCConnectionAdapter } from '../../index';
|
|
4
|
-
import { Socket, sockets, ServerSocket, ClientSocket } from './socket';
|
|
1
|
+
import SocketIOClient from './client.js';
|
|
2
|
+
import * as oox from '../../index.js';
|
|
3
|
+
import { RPCKeepAliveConnectionData, RPCConnectionAdapter } from '../../index.js';
|
|
4
|
+
import { Socket, sockets, ServerSocket, ClientSocket } from './socket.js';
|
|
5
5
|
export { Socket, sockets };
|
|
6
6
|
export default class SocketIOModule extends SocketIOClient implements RPCConnectionAdapter {
|
|
7
7
|
sockets: Map<string, Socket>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import * as http from 'node:http';
|
|
3
2
|
import { Server, ServerOptions } from 'socket.io';
|
|
4
|
-
import { Module, ModuleConfig } from '../../index';
|
|
5
|
-
import { ServerSocket as Socket } from './socket';
|
|
3
|
+
import { Module, ModuleConfig } from '../../index.js';
|
|
4
|
+
import { ServerSocket as Socket } from './socket.js';
|
|
6
5
|
export declare class SocketIOConfig extends ModuleConfig {
|
|
7
6
|
port: number;
|
|
8
7
|
path: string;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Socket as _ServerSocket } from 'socket.io';
|
|
2
2
|
import { Socket as _ClientSocket } from 'socket.io-client';
|
|
3
|
-
import { RPCKeepAliveConnectionData } from '../../index';
|
|
3
|
+
import { RPCKeepAliveConnectionData } from '../../index.js';
|
|
4
4
|
export interface ServerSocket extends _ServerSocket {
|
|
5
5
|
data: RPCKeepAliveConnectionData;
|
|
6
6
|
}
|
|
7
7
|
export interface ClientSocket extends _ClientSocket {
|
|
8
8
|
data: RPCKeepAliveConnectionData;
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export type Socket = ServerSocket | ClientSocket;
|
|
11
11
|
export declare const sockets: Map<string, Socket>;
|
package/utils.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const os_1 = require("os");
|
|
5
|
-
function getIPAddress(version = 4) {
|
|
6
|
-
const interfaces = (0, os_1.networkInterfaces)();
|
|
1
|
+
import { networkInterfaces } from 'os';
|
|
2
|
+
export function getIPAddress(version = 4) {
|
|
3
|
+
const interfaces = networkInterfaces();
|
|
7
4
|
const ip = [];
|
|
8
5
|
for (const name of Object.keys(interfaces))
|
|
9
6
|
for (const intf of interfaces[name])
|
|
@@ -18,8 +15,7 @@ function getIPAddress(version = 4) {
|
|
|
18
15
|
}
|
|
19
16
|
return ip;
|
|
20
17
|
}
|
|
21
|
-
|
|
22
|
-
function getAllCallablePropertyNames(obj) {
|
|
18
|
+
export function getAllCallablePropertyNames(obj) {
|
|
23
19
|
if (!obj)
|
|
24
20
|
return [];
|
|
25
21
|
let props = [], tmpProps = [], index = 0, size = 0, tmpProp = '';
|
|
@@ -39,8 +35,7 @@ function getAllCallablePropertyNames(obj) {
|
|
|
39
35
|
} while (obj = Object.getPrototypeOf(obj));
|
|
40
36
|
return props;
|
|
41
37
|
}
|
|
42
|
-
|
|
43
|
-
function genKVMethods(methods, kvMethods = new Map(), sourceKVMethods = new Map(), nameStack = []) {
|
|
38
|
+
export function genKVMethods(methods, kvMethods = new Map(), sourceKVMethods = new Map(), nameStack = []) {
|
|
44
39
|
let keys = getAllCallablePropertyNames(methods);
|
|
45
40
|
for (const key of keys) {
|
|
46
41
|
/**
|
|
@@ -60,4 +55,3 @@ function genKVMethods(methods, kvMethods = new Map(), sourceKVMethods = new Map(
|
|
|
60
55
|
}
|
|
61
56
|
return { kvMethods, sourceKVMethods };
|
|
62
57
|
}
|
|
63
|
-
exports.genKVMethods = genKVMethods;
|