@oox/socketio 1.0.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 lipingruan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
File without changes
package/adapter.js ADDED
@@ -0,0 +1,48 @@
1
+ import * as SocketIOClient from 'socket.io-client';
2
+ import * as oox from 'oox';
3
+ import { OOXEvent, genWebSocketURL } from './utils.js';
4
+ import { randomUUID } from 'node:crypto';
5
+ import { SampleKeepAliveConnectionAdapter } from 'oox/samples';
6
+ export default class SocketIOAdapter extends SampleKeepAliveConnectionAdapter {
7
+ name = 'socketio';
8
+ OOXEvent = OOXEvent;
9
+ newConnection(identify) {
10
+ const { id, name } = oox.config;
11
+ const headers = {
12
+ 'x-caller': name,
13
+ 'x-caller-id': id,
14
+ };
15
+ let mURL;
16
+ const connectionData = {
17
+ name: 'anonymous',
18
+ id: randomUUID(),
19
+ adapter: this.name,
20
+ ip: '',
21
+ token: ''
22
+ };
23
+ if ('string' === typeof identify) {
24
+ mURL = genWebSocketURL(identify);
25
+ }
26
+ else if (identify instanceof URL) {
27
+ mURL = identify;
28
+ }
29
+ else if (identify.url) {
30
+ // KeepAliveConnectionData
31
+ Object.assign(connectionData, identify);
32
+ mURL = new URL(identify.url);
33
+ if (identify.token) {
34
+ headers['x-token'] = identify.token;
35
+ }
36
+ }
37
+ else {
38
+ throw new Error('identify must be string, URL, or KeepAliveConnectionData');
39
+ }
40
+ const socket = SocketIOClient.io(mURL.origin, {
41
+ extraHeaders: headers,
42
+ path: mURL.pathname,
43
+ autoConnect: false,
44
+ });
45
+ const connection = new oox.KeepAliveConnection(this, socket, connectionData);
46
+ return connection;
47
+ }
48
+ }
package/index.js ADDED
@@ -0,0 +1,23 @@
1
+ import * as PATH from 'node:path';
2
+ import * as oox from 'oox';
3
+ import SocketINServer from './server.js';
4
+ export default class SocketIOModule extends SocketINServer {
5
+ async serve() {
6
+ await this.stop();
7
+ const _http = oox.modules.builtins.http;
8
+ const httpConfig = _http.getConfig(), config = this.getConfig();
9
+ let isShareServer = false;
10
+ // 都没设置端口
11
+ isShareServer = !httpConfig.port && !config.port;
12
+ // 都设置相同端口
13
+ isShareServer ||= httpConfig.port === config.port;
14
+ // http 模块未被禁用
15
+ isShareServer &&= httpConfig.enabled;
16
+ if (isShareServer) {
17
+ config.path = PATH.posix.join(httpConfig.path, config.path);
18
+ this.server = _http.server;
19
+ this.config.ssl = _http.config.ssl;
20
+ }
21
+ await super.serve();
22
+ }
23
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@oox/socketio",
3
+ "version": "1.0.0",
4
+ "description": "OOX SocketIO Module",
5
+ "publishConfig": {
6
+ "access": "public",
7
+ "registry": "https://registry.npmjs.org/"
8
+ },
9
+ "main": "index.js",
10
+ "types": "./types/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./index.js",
14
+ "require": "./index.js",
15
+ "types": "./types/index.d.ts"
16
+ }
17
+ },
18
+ "keywords": [
19
+ "oox",
20
+ "socketio",
21
+ "module",
22
+ "websocket"
23
+ ],
24
+ "author": "lipingruan",
25
+ "email": "hello@ruanmail.com",
26
+ "homepage": "https://gitee.com/lipingruan/oox/tree/main/packages/oox-socketio",
27
+ "license": "MIT",
28
+ "type": "module",
29
+ "dependencies": {
30
+ "socket.io": "^4.8.3",
31
+ "socket.io-client": "^4.8.3"
32
+ }
33
+ }
package/server.js ADDED
@@ -0,0 +1,207 @@
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 'oox';
5
+ import { Module } from 'oox';
6
+ import SocketIOAdapter from './adapter.js';
7
+ import { OOXEvent } from './utils.js';
8
+ import { randomUUID } from 'node:crypto';
9
+ export class SocketIOConfig {
10
+ enabled = true;
11
+ // listen port
12
+ port = 0;
13
+ // service path
14
+ path = '/socket.io';
15
+ // browser cross origin
16
+ origin = '';
17
+ // https options
18
+ ssl = {
19
+ // enable https
20
+ enabled: false,
21
+ // ssl certificate path
22
+ cert: '',
23
+ // ssl private key path
24
+ key: '',
25
+ // ssl ca path
26
+ ca: ''
27
+ };
28
+ }
29
+ export default class SocketIOServer extends Module {
30
+ name = 'socketio';
31
+ config = new SocketIOConfig;
32
+ /**
33
+ * means this.server created by myself<SocketIOServer>
34
+ */
35
+ #isSelfServer = false;
36
+ server = null;
37
+ socketServer = null;
38
+ adapter = new SocketIOAdapter();
39
+ constructor() {
40
+ super();
41
+ oox.keepAliveConnectionAdapters.set(this.name, this.adapter);
42
+ }
43
+ getURL() {
44
+ const { host } = oox.config;
45
+ const { port, path } = this.config;
46
+ const protocol = this.config.ssl.enabled ? 'wss:' : 'ws:';
47
+ return new URL(`${protocol}//${host}:${port}${path}`);
48
+ }
49
+ setConfig(config) {
50
+ Object.assign(this.config, config);
51
+ if (!config.hasOwnProperty('port')) {
52
+ this.config.port = oox.config.port;
53
+ }
54
+ if (!config.hasOwnProperty('origin')) {
55
+ this.config.origin = oox.config.origin;
56
+ }
57
+ }
58
+ getConfig() {
59
+ return this.config;
60
+ }
61
+ // 对外提供服务的URL
62
+ get serviceURL() {
63
+ return this.getURL();
64
+ }
65
+ async serve() {
66
+ await this.stop();
67
+ const { port, ssl } = this.config;
68
+ const isSelfServer = this.#isSelfServer = this.server ? true : false;
69
+ let server = null;
70
+ if (isSelfServer) {
71
+ if (!this.server)
72
+ throw new Error('HTTP Server is not created');
73
+ server = this.server;
74
+ }
75
+ else {
76
+ if (ssl.enabled) {
77
+ const fs = await import('node:fs');
78
+ const options = {
79
+ key: ssl.key ? fs.readFileSync(ssl.key) : undefined,
80
+ cert: ssl.cert ? fs.readFileSync(ssl.cert) : undefined,
81
+ ca: ssl.ca ? fs.readFileSync(ssl.ca) : undefined
82
+ };
83
+ if (!options.key || !options.cert) {
84
+ throw new Error('HTTPS enabled but missing key or cert');
85
+ }
86
+ server = https.createServer(options, (_request, response) => response.end('No HTTP Gateway'));
87
+ }
88
+ else {
89
+ server = http.createServer((_request, response) => response.end('No HTTP Gateway'));
90
+ }
91
+ }
92
+ this.server = server;
93
+ if (!server.listening)
94
+ server.listen(port);
95
+ const address = server.address();
96
+ if (!address || 'object' !== typeof address)
97
+ throw new Error('Cannot read socket.io server port');
98
+ this.config.port = address.port;
99
+ this.createSocketIOServer();
100
+ }
101
+ async stop() {
102
+ const { server, socketServer } = this;
103
+ if (socketServer)
104
+ await new Promise((resolve, reject) => socketServer.close(error => error ? reject(error) : resolve()));
105
+ if (this.#isSelfServer)
106
+ await new Promise((resolve, reject) => server?.close(error => error ? reject(error) : resolve()));
107
+ }
108
+ genSocketIOServerOptions() {
109
+ const options = {
110
+ /**
111
+ * name of the path to capture
112
+ * @default "/socket.io"
113
+ */
114
+ path: this.config.path,
115
+ /**
116
+ * how many ms before a client without namespace is closed
117
+ * @default 45000
118
+ */
119
+ connectTimeout: 5000,
120
+ /**
121
+ * how many ms without a pong packet to consider the connection closed
122
+ * @default 5000
123
+ */
124
+ pingTimeout: 2000,
125
+ /**
126
+ * how many ms before sending a new ping packet
127
+ * @default 25000
128
+ */
129
+ pingInterval: 10000,
130
+ /**
131
+ * how many bytes or characters a message can be, before closing the session (to avoid DoS).
132
+ * @default 1e5 (100 KB)
133
+ */
134
+ maxHttpBufferSize: 1e5
135
+ };
136
+ const { origin } = this.config;
137
+ if (origin)
138
+ options.cors = { origin };
139
+ return options;
140
+ }
141
+ createSocketIOServer() {
142
+ const { server } = this;
143
+ if (!server)
144
+ throw new Error('HTTP Server is not created');
145
+ const socketServer = this.socketServer = new Server(server, {
146
+ ...this.genSocketIOServerOptions(),
147
+ });
148
+ socketServer.on('connection', async (socket) => {
149
+ try {
150
+ this.serverOnSocketConnection(socket);
151
+ }
152
+ catch (error) {
153
+ console.error(error);
154
+ socket.send(error.message).disconnect(true);
155
+ }
156
+ });
157
+ }
158
+ /**
159
+ * 服务端Socket连接事件
160
+ */
161
+ serverOnSocketConnection(socket) {
162
+ const headers = socket.handshake.headers;
163
+ const callerId = String(headers['x-caller-id'] || randomUUID());
164
+ // client ip or caller service ip
165
+ const ip = String(headers['x-real-ip'] || socket.handshake.address);
166
+ // service name
167
+ const caller = String(headers['x-caller'] || 'anonymous');
168
+ const token = String(headers['x-token'] || '');
169
+ // check token
170
+ const { allow, reason } = oox.checkAllow(ip, caller, token);
171
+ if (!allow) {
172
+ socket.send(reason).disconnect(true);
173
+ return;
174
+ }
175
+ const connection = new oox.KeepAliveConnection(this.adapter, socket, {
176
+ ip,
177
+ name: caller,
178
+ id: callerId,
179
+ token,
180
+ });
181
+ oox.addKeepAliveConnection(connection);
182
+ this.bindServerConnectionEvents(connection);
183
+ socket.emit(OOXEvent.READY, {
184
+ id: oox.config.id,
185
+ name: oox.config.name
186
+ });
187
+ connection.enabled = true;
188
+ this.adapter.bindCall(connection);
189
+ }
190
+ /**
191
+ * 绑定服务器连接事件
192
+ * @param connection
193
+ */
194
+ bindServerConnectionEvents(connection) {
195
+ const socket = connection.nativeConnection;
196
+ socket.on(OOXEvent.REGISTRY_SYNC_CONNECTIONS, async (fn) => {
197
+ if ('function' !== typeof fn)
198
+ return;
199
+ oox.registry.onSyncConnections(connection, {}, fn);
200
+ });
201
+ socket.on('disconnecting', (reason) => {
202
+ socket.removeAllListeners();
203
+ oox.removeKeepAliveConnection(connection);
204
+ connection.emit('disconnect');
205
+ });
206
+ }
207
+ }
package/socket.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ import * as oox from 'oox';
2
+ import { Socket } from './socket.js';
3
+ import { SampleKeepAliveConnectionAdapter } from 'oox/samples';
4
+ export default class SocketIOAdapter extends SampleKeepAliveConnectionAdapter<Socket> {
5
+ name: string;
6
+ OOXEvent: {
7
+ CONNECT: string;
8
+ DISCONNECT: string;
9
+ ERROR: string;
10
+ CALL: string;
11
+ READY: string;
12
+ ENABLED: string;
13
+ DISABLED: string;
14
+ REGISTRY_SYNC_CONNECTIONS: string;
15
+ REGISTRY_SUBSCRIBE: string;
16
+ REGISTRY_NOTIFY: string;
17
+ };
18
+ newConnection(identify: string | URL | oox.KeepAliveConnectionData): oox.KeepAliveConnection<Socket>;
19
+ }
@@ -0,0 +1,4 @@
1
+ import SocketINServer from './server.js';
2
+ export default class SocketIOModule extends SocketINServer {
3
+ serve(): Promise<void>;
4
+ }
@@ -0,0 +1,45 @@
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 'oox';
5
+ import { Module, ModuleConfig } from 'oox';
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
+ get serviceURL(): URL;
32
+ serve(): Promise<void>;
33
+ stop(): Promise<void>;
34
+ genSocketIOServerOptions(): Partial<ServerOptions>;
35
+ createSocketIOServer(): void;
36
+ /**
37
+ * 服务端Socket连接事件
38
+ */
39
+ serverOnSocketConnection(socket: Socket<'server'>): void;
40
+ /**
41
+ * 绑定服务器连接事件
42
+ * @param connection
43
+ */
44
+ bindServerConnectionEvents(connection: oox.KeepAliveConnection<Socket<'server'>>): void;
45
+ }
@@ -0,0 +1,8 @@
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];
@@ -0,0 +1,14 @@
1
+ export declare const OOXEvent: {
2
+ CONNECT: string;
3
+ DISCONNECT: string;
4
+ ERROR: string;
5
+ CALL: string;
6
+ READY: string;
7
+ ENABLED: string;
8
+ DISABLED: string;
9
+ REGISTRY_SYNC_CONNECTIONS: string;
10
+ REGISTRY_SUBSCRIBE: string;
11
+ REGISTRY_NOTIFY: string;
12
+ };
13
+ export declare function isWebSocketURL(url: string | URL): boolean;
14
+ export declare function genWebSocketURL(url: string): URL;
package/utils.js ADDED
@@ -0,0 +1,44 @@
1
+ export const OOXEvent = {
2
+ CONNECT: 'connect',
3
+ DISCONNECT: 'disconnect',
4
+ ERROR: 'error',
5
+ CALL: 'call',
6
+ READY: 'oox:ready',
7
+ ENABLED: 'oox:enabled',
8
+ DISABLED: 'oox:disabled',
9
+ REGISTRY_SYNC_CONNECTIONS: 'oox:registry:sync_connections',
10
+ REGISTRY_SUBSCRIBE: 'oox:registry:subscribe',
11
+ REGISTRY_NOTIFY: 'oox:registry:notify',
12
+ };
13
+ export function isWebSocketURL(url) {
14
+ if ('string' === typeof url) {
15
+ return /^wss?:\/\/.+$/.test(url);
16
+ }
17
+ else {
18
+ return url.protocol === 'ws:' || url.protocol === 'wss:';
19
+ }
20
+ }
21
+ export function genWebSocketURL(url) {
22
+ // :6000
23
+ if (url.startsWith(':'))
24
+ url = 'ws://localhost' + url;
25
+ // 127.0.0.1:6000
26
+ if (!/^\w+?:\/.+$/.test(url))
27
+ url = 'ws://' + url;
28
+ const urlObject = new URL(url);
29
+ // :8000 => :8000/socket.io
30
+ const notSetPath = !urlObject.pathname || (urlObject.pathname === '/'
31
+ && !url.endsWith('/')
32
+ && !urlObject.search
33
+ && !urlObject.hash);
34
+ if (notSetPath) {
35
+ urlObject.pathname = '/socket.io';
36
+ }
37
+ if (urlObject.protocol !== 'wss:') {
38
+ if (urlObject.port === '443')
39
+ urlObject.protocol = 'wss:';
40
+ else
41
+ urlObject.protocol = 'ws:';
42
+ }
43
+ return urlObject;
44
+ }