oox 0.3.5 → 0.3.7

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 (42) hide show
  1. package/README.md +1 -1
  2. package/app.js +1 -7
  3. package/bin/cli.js +0 -5
  4. package/bin/proxy-require.js +1 -23
  5. package/bin/starter.js +9 -6
  6. package/config.js +30 -1
  7. package/context.js +58 -0
  8. package/index.js +6 -146
  9. package/keepalive-connection.js +61 -1
  10. package/logger.js +2 -1
  11. package/modules/http/index.js +4 -1
  12. package/modules/index.js +0 -3
  13. package/modules/module.js +1 -1
  14. package/package.json +6 -5
  15. package/proxy.js +30 -0
  16. package/registry.js +1 -1
  17. package/samples/index.js +1 -1
  18. package/samples/keepalive-connection-sample.js +46 -33
  19. package/types/app.d.ts +1 -7
  20. package/types/bin/starter.d.ts +2 -1
  21. package/types/config.d.ts +13 -1
  22. package/types/context.d.ts +30 -0
  23. package/types/index.d.ts +6 -52
  24. package/types/keepalive-connection.d.ts +12 -1
  25. package/types/modules/http/index.d.ts +2 -1
  26. package/types/modules/index.d.ts +1 -3
  27. package/types/modules/module.d.ts +2 -1
  28. package/types/proxy.d.ts +1 -0
  29. package/types/registry.d.ts +1 -1
  30. package/types/samples/index.d.ts +1 -1
  31. package/types/samples/keepalive-connection-sample.d.ts +23 -22
  32. package/utils.js +1 -1
  33. package/modules/socketio/adapter.js +0 -49
  34. package/modules/socketio/index.js +0 -23
  35. package/modules/socketio/server.js +0 -200
  36. package/modules/socketio/socket.js +0 -1
  37. package/modules/socketio/utils.js +0 -41
  38. package/types/modules/socketio/adapter.d.ts +0 -14
  39. package/types/modules/socketio/index.d.ts +0 -4
  40. package/types/modules/socketio/server.d.ts +0 -44
  41. package/types/modules/socketio/socket.d.ts +0 -8
  42. package/types/modules/socketio/utils.d.ts +0 -10
@@ -1,200 +0,0 @@
1
- import * as http from 'node:http';
2
- import * as https from 'node:https';
3
- import { Server } from 'socket.io';
4
- import * as oox from '../../index.js';
5
- import { Module } from '../../index.js';
6
- import SocketIOAdapter from './adapter.js';
7
- import { OOXEvent } from './utils.js';
8
- export class SocketIOConfig {
9
- enabled = true;
10
- // listen port
11
- port = 0;
12
- // service path
13
- path = '/socket.io';
14
- // browser cross origin
15
- origin = '';
16
- // https options
17
- ssl = {
18
- // enable https
19
- enabled: false,
20
- // ssl certificate path
21
- cert: '',
22
- // ssl private key path
23
- key: '',
24
- // ssl ca path
25
- ca: ''
26
- };
27
- }
28
- export default class SocketIOServer extends Module {
29
- name = 'socketio';
30
- config = new SocketIOConfig;
31
- /**
32
- * means this.server created by myself<SocketIOServer>
33
- */
34
- #isSelfServer = false;
35
- server = null;
36
- socketServer = null;
37
- adapter = new SocketIOAdapter();
38
- constructor() {
39
- super();
40
- oox.keepAliveConnectionAdapters.set(this.name, this.adapter);
41
- }
42
- getURL() {
43
- const { host } = oox.config;
44
- const { port, path } = this.config;
45
- const protocol = this.config.ssl.enabled ? 'wss:' : 'ws:';
46
- return new URL(`${protocol}//${host}:${port}${path}`);
47
- }
48
- setConfig(config) {
49
- Object.assign(this.config, config);
50
- if (!config.hasOwnProperty('port')) {
51
- this.config.port = oox.config.port;
52
- }
53
- if (!config.hasOwnProperty('origin')) {
54
- this.config.origin = oox.config.origin;
55
- }
56
- }
57
- getConfig() {
58
- return this.config;
59
- }
60
- async serve() {
61
- await this.stop();
62
- const { port, ssl } = this.config;
63
- const isSelfServer = this.#isSelfServer = this.server ? true : false;
64
- let server = null;
65
- if (isSelfServer) {
66
- if (!this.server)
67
- throw new Error('HTTP Server is not created');
68
- server = this.server;
69
- }
70
- else {
71
- if (ssl.enabled) {
72
- const fs = await import('node:fs');
73
- const options = {
74
- key: ssl.key ? fs.readFileSync(ssl.key) : undefined,
75
- cert: ssl.cert ? fs.readFileSync(ssl.cert) : undefined,
76
- ca: ssl.ca ? fs.readFileSync(ssl.ca) : undefined
77
- };
78
- if (!options.key || !options.cert) {
79
- throw new Error('HTTPS enabled but missing key or cert');
80
- }
81
- server = https.createServer(options, (_request, response) => response.end('No HTTP Gateway'));
82
- }
83
- else {
84
- server = http.createServer((_request, response) => response.end('No HTTP Gateway'));
85
- }
86
- }
87
- this.server = server;
88
- if (!server.listening)
89
- server.listen(port);
90
- const address = server.address();
91
- if (!address || 'object' !== typeof address)
92
- throw new Error('Cannot read socket.io server port');
93
- this.config.port = address.port;
94
- this.createSocketIOServer();
95
- }
96
- async stop() {
97
- const { server, socketServer } = this;
98
- if (socketServer)
99
- await new Promise((resolve, reject) => socketServer.close(error => error ? reject(error) : resolve()));
100
- if (this.#isSelfServer)
101
- await new Promise((resolve, reject) => server?.close(error => error ? reject(error) : resolve()));
102
- }
103
- genSocketIOServerOptions() {
104
- const options = {
105
- /**
106
- * name of the path to capture
107
- * @default "/socket.io"
108
- */
109
- path: this.config.path,
110
- /**
111
- * how many ms before a client without namespace is closed
112
- * @default 45000
113
- */
114
- connectTimeout: 5000,
115
- /**
116
- * how many ms without a pong packet to consider the connection closed
117
- * @default 5000
118
- */
119
- pingTimeout: 2000,
120
- /**
121
- * how many ms before sending a new ping packet
122
- * @default 25000
123
- */
124
- pingInterval: 10000,
125
- /**
126
- * how many bytes or characters a message can be, before closing the session (to avoid DoS).
127
- * @default 1e5 (100 KB)
128
- */
129
- maxHttpBufferSize: 1e5
130
- };
131
- const { origin } = this.config;
132
- if (origin)
133
- options.cors = { origin };
134
- return options;
135
- }
136
- createSocketIOServer() {
137
- const { server } = this;
138
- if (!server)
139
- throw new Error('HTTP Server is not created');
140
- const socketServer = this.socketServer = new Server(server, this.genSocketIOServerOptions());
141
- socketServer.on('connection', async (socket) => {
142
- try {
143
- this.serverOnSocketConnection(socket);
144
- }
145
- catch (error) {
146
- console.error(error);
147
- socket.send(error.message).disconnect(true);
148
- }
149
- });
150
- }
151
- /**
152
- * 服务端Socket连接事件
153
- */
154
- serverOnSocketConnection(socket) {
155
- const headers = socket.handshake.headers;
156
- const callerId = String(headers['x-caller-id'] || '') || socket.id;
157
- // client ip or caller service ip
158
- const ip = String(headers['x-real-ip'] || socket.handshake.address);
159
- // service name
160
- const caller = String(headers['x-caller'] || 'anonymous');
161
- const token = String(headers['x-token'] || '');
162
- // check token
163
- const { allow, reason } = oox.checkAllow(ip, caller, token);
164
- if (!allow) {
165
- socket.send(reason).disconnect(true);
166
- return;
167
- }
168
- const connection = new oox.KeepAliveConnection(this.adapter, socket, {
169
- ip,
170
- name: caller,
171
- id: callerId,
172
- token,
173
- });
174
- oox.addKeepAliveConnection(connection);
175
- this.bindServerConnectionEvents(connection);
176
- this.adapter.bindCall(connection);
177
- socket.emit(OOXEvent.READY, {
178
- id: oox.config.id,
179
- name: oox.config.name
180
- });
181
- connection.enabled = true;
182
- }
183
- /**
184
- * 绑定服务器连接事件
185
- * @param connection
186
- */
187
- bindServerConnectionEvents(connection) {
188
- const socket = connection.nativeConnection;
189
- socket.on(OOXEvent.REGISTRY_SYNC_CONNECTIONS, async (fn) => {
190
- if ('function' !== typeof fn)
191
- return;
192
- oox.registry.onSyncConnections(connection, {}, fn);
193
- });
194
- socket.on('disconnecting', (reason) => {
195
- oox.removeKeepAliveConnection(connection);
196
- socket.removeAllListeners();
197
- connection.emit('disconnect');
198
- });
199
- }
200
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,41 +0,0 @@
1
- export var OOXEvent;
2
- (function (OOXEvent) {
3
- OOXEvent["READY"] = "oox:ready";
4
- OOXEvent["ENABLED"] = "oox:enabled";
5
- OOXEvent["DISABLED"] = "oox:disabled";
6
- OOXEvent["REGISTRY_SYNC_CONNECTIONS"] = "oox:registry:sync_connections";
7
- OOXEvent["REGISTRY_SUBSCRIBE"] = "oox:registry:subscribe";
8
- OOXEvent["REGISTRY_NOTIFY"] = "oox:registry:notify";
9
- })(OOXEvent || (OOXEvent = {}));
10
- export function isWebSocketURL(url) {
11
- if ('string' === typeof url) {
12
- return /^wss?:\/\/.+$/.test(url);
13
- }
14
- else {
15
- return url.protocol === 'ws:' || url.protocol === 'wss:';
16
- }
17
- }
18
- export function genWebSocketURL(url) {
19
- // :6000
20
- if (url.startsWith(':'))
21
- url = 'ws://localhost' + url;
22
- // 127.0.0.1:6000
23
- if (!/^\w+?:\/.+$/.test(url))
24
- url = 'ws://' + url;
25
- const urlObject = new URL(url);
26
- // :8000 => :8000/socket.io
27
- const notSetPath = !urlObject.pathname || (urlObject.pathname === '/'
28
- && !url.endsWith('/')
29
- && !urlObject.search
30
- && !urlObject.hash);
31
- if (notSetPath) {
32
- urlObject.pathname = '/socket.io';
33
- }
34
- if (urlObject.protocol !== 'wss:') {
35
- if (urlObject.port === '443')
36
- urlObject.protocol = 'wss:';
37
- else
38
- urlObject.protocol = 'ws:';
39
- }
40
- return urlObject;
41
- }
@@ -1,14 +0,0 @@
1
- import * as oox from '../../index.js';
2
- import { Socket } from './socket.js';
3
- import { OOXEvent } from './utils.js';
4
- import { SampleKeepAliveConnectionAdapter } from '../../samples/index.js';
5
- export default class SocketIOAdapter extends SampleKeepAliveConnectionAdapter<Socket> {
6
- name: string;
7
- OOXEvent: typeof OOXEvent;
8
- nativeEvent: {
9
- CONNECT: string;
10
- DISCONNECT: string;
11
- ERROR: string;
12
- };
13
- newConnection(identify: string | URL | oox.KeepAliveConnectionData): oox.KeepAliveConnection<Socket>;
14
- }
@@ -1,4 +0,0 @@
1
- import SocketINServer from './server.js';
2
- export default class SocketIOModule extends SocketINServer {
3
- serve(): Promise<void>;
4
- }
@@ -1,44 +0,0 @@
1
- import * as http from 'node:http';
2
- import * as https from 'node:https';
3
- import { Server, ServerOptions } from 'socket.io';
4
- import * as oox from '../../index.js';
5
- import { Module, ModuleConfig } from '../../index.js';
6
- import { Socket } from './socket.js';
7
- import SocketIOAdapter from './adapter.js';
8
- export declare class SocketIOConfig implements ModuleConfig {
9
- enabled: boolean;
10
- port: number;
11
- path: string;
12
- origin: string | string[];
13
- ssl: {
14
- enabled: boolean;
15
- cert: string;
16
- key: string;
17
- ca: string;
18
- };
19
- }
20
- export default class SocketIOServer extends Module {
21
- #private;
22
- name: string;
23
- config: SocketIOConfig;
24
- server: http.Server | https.Server | null;
25
- socketServer: Server | null;
26
- adapter: SocketIOAdapter;
27
- constructor();
28
- getURL(): URL;
29
- setConfig(config: SocketIOConfig): void;
30
- getConfig(): SocketIOConfig;
31
- serve(): Promise<void>;
32
- stop(): Promise<void>;
33
- genSocketIOServerOptions(): Partial<ServerOptions>;
34
- createSocketIOServer(): void;
35
- /**
36
- * 服务端Socket连接事件
37
- */
38
- serverOnSocketConnection(socket: Socket<'server'>): void;
39
- /**
40
- * 绑定服务器连接事件
41
- * @param connection
42
- */
43
- bindServerConnectionEvents(connection: oox.KeepAliveConnection<Socket<'server'>>): void;
44
- }
@@ -1,8 +0,0 @@
1
- import { Socket as ServerSocket } from 'socket.io';
2
- import { Socket as ClientSocket } from 'socket.io-client';
3
- export type SocketMap = {
4
- [key: string]: ServerSocket | ClientSocket;
5
- server: ServerSocket;
6
- client: ClientSocket;
7
- };
8
- export type Socket<T extends keyof SocketMap = keyof SocketMap> = SocketMap[T];
@@ -1,10 +0,0 @@
1
- export declare enum OOXEvent {
2
- READY = "oox:ready",
3
- ENABLED = "oox:enabled",
4
- DISABLED = "oox:disabled",
5
- REGISTRY_SYNC_CONNECTIONS = "oox:registry:sync_connections",
6
- REGISTRY_SUBSCRIBE = "oox:registry:subscribe",
7
- REGISTRY_NOTIFY = "oox:registry:notify"
8
- }
9
- export declare function isWebSocketURL(url: string | URL): boolean;
10
- export declare function genWebSocketURL(url: string): URL;