@opra/socketio 1.20.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 +21 -0
- package/README.md +3 -0
- package/cjs/index.js +6 -0
- package/cjs/package.json +3 -0
- package/cjs/socketio-adapter.js +146 -0
- package/cjs/socketio-context.js +40 -0
- package/esm/index.js +3 -0
- package/esm/package.json +3 -0
- package/esm/socketio-adapter.js +141 -0
- package/esm/socketio-context.js +36 -0
- package/package.json +64 -0
- package/types/index.d.cts +2 -0
- package/types/index.d.ts +2 -0
- package/types/socketio-adapter.d.ts +83 -0
- package/types/socketio-context.d.ts +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Panates
|
|
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
package/cjs/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
// export * from './constants.js';
|
|
5
|
+
tslib_1.__exportStar(require("./socketio-adapter.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./socketio-context.js"), exports);
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SocketioAdapter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@opra/common");
|
|
6
|
+
const core_1 = require("@opra/core");
|
|
7
|
+
const socketio = tslib_1.__importStar(require("socket.io"));
|
|
8
|
+
const socketio_context_js_1 = require("./socketio-context.js");
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @class SocketioAdapter
|
|
12
|
+
*/
|
|
13
|
+
class SocketioAdapter extends core_1.PlatformAdapter {
|
|
14
|
+
constructor(document, options) {
|
|
15
|
+
super(options);
|
|
16
|
+
this._controllerInstances = new Map();
|
|
17
|
+
this._eventsRegByName = new Map();
|
|
18
|
+
this._eventsRegByPattern = [];
|
|
19
|
+
this.transform = 'ws';
|
|
20
|
+
this.platform = SocketioAdapter.PlatformName;
|
|
21
|
+
this._document = document;
|
|
22
|
+
if (!(this.document.api instanceof common_1.WSApi)) {
|
|
23
|
+
throw new TypeError(`The document doesn't expose a WS Api`);
|
|
24
|
+
}
|
|
25
|
+
this.interceptors = [...(options?.interceptors || [])];
|
|
26
|
+
this._scope = options?.scope;
|
|
27
|
+
this.server = new socketio.Server();
|
|
28
|
+
this.server.on('error', error => {
|
|
29
|
+
this.emit('error', error, undefined, this);
|
|
30
|
+
});
|
|
31
|
+
this.server.on('connection', (socket) => {
|
|
32
|
+
this._initSocket(socket);
|
|
33
|
+
this.emit('connection', socket, this);
|
|
34
|
+
});
|
|
35
|
+
for (const contDef of this.api.controllers.values()) {
|
|
36
|
+
for (const oprDef of contDef.operations.values()) {
|
|
37
|
+
const fn = contDef.instance[oprDef.name];
|
|
38
|
+
if (typeof fn !== 'function')
|
|
39
|
+
continue;
|
|
40
|
+
const reg = {
|
|
41
|
+
event: oprDef.event,
|
|
42
|
+
contDef,
|
|
43
|
+
oprDef,
|
|
44
|
+
handler: fn,
|
|
45
|
+
decoders: [],
|
|
46
|
+
};
|
|
47
|
+
if (typeof reg.event === 'string')
|
|
48
|
+
this._eventsRegByName.set(reg.event, reg);
|
|
49
|
+
else
|
|
50
|
+
this._eventsRegByPattern.push(reg);
|
|
51
|
+
/** Generate decoders */
|
|
52
|
+
if (oprDef.arguments?.length) {
|
|
53
|
+
for (const dt of oprDef.arguments) {
|
|
54
|
+
reg.decoders.push(dt.generateCodec('decode', {
|
|
55
|
+
scope: this.scope,
|
|
56
|
+
ignoreReadonlyFields: true,
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** Generate response encoder */
|
|
61
|
+
if (oprDef.response) {
|
|
62
|
+
reg.encoder = oprDef.response.generateCodec('encode', {
|
|
63
|
+
scope: this.scope,
|
|
64
|
+
ignoreWriteonlyFields: true,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
get api() {
|
|
71
|
+
return this.document.getWsApi();
|
|
72
|
+
}
|
|
73
|
+
get scope() {
|
|
74
|
+
return this._scope;
|
|
75
|
+
}
|
|
76
|
+
close() {
|
|
77
|
+
return this.server.close();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Attaches socket.io to a server or port.
|
|
81
|
+
*
|
|
82
|
+
* @param srv - server or port
|
|
83
|
+
* @param opts - options passed to engine.io
|
|
84
|
+
* @return self
|
|
85
|
+
*/
|
|
86
|
+
listen(srv, opts) {
|
|
87
|
+
if (this.server.httpServer?.listening)
|
|
88
|
+
throw new Error('Server is already listening');
|
|
89
|
+
if (opts?.path)
|
|
90
|
+
this.server.path(opts?.path);
|
|
91
|
+
this.server.listen(srv, opts);
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
_initSocket(socket) {
|
|
95
|
+
socket.on('close', () => {
|
|
96
|
+
this.emit('close', socket, this);
|
|
97
|
+
});
|
|
98
|
+
socket.on('error', error => {
|
|
99
|
+
this.emit('error', error, socket, this);
|
|
100
|
+
});
|
|
101
|
+
socket.onAny((event, ...args) => {
|
|
102
|
+
const callback = args.length > 0 ? args[args.length - 1] : null;
|
|
103
|
+
const reg = this._eventsRegByName.get(event) ||
|
|
104
|
+
this._eventsRegByPattern.find(r => r.event.test(event));
|
|
105
|
+
if (!reg) {
|
|
106
|
+
if (callback)
|
|
107
|
+
callback(new Error(`Unknown event "${event}"`));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const parameters = callback ? args.slice(0, -1) : args;
|
|
111
|
+
Promise.resolve().then(async () => {
|
|
112
|
+
const ctx = new socketio_context_js_1.SocketioContext({
|
|
113
|
+
__adapter: this,
|
|
114
|
+
__contDef: reg.contDef,
|
|
115
|
+
__oprDef: reg.oprDef,
|
|
116
|
+
__controller: reg.contDef.instance,
|
|
117
|
+
__handler: reg.handler,
|
|
118
|
+
socket,
|
|
119
|
+
event,
|
|
120
|
+
parameters,
|
|
121
|
+
});
|
|
122
|
+
try {
|
|
123
|
+
let x = await reg.handler.apply(reg.contDef.instance, [
|
|
124
|
+
ctx,
|
|
125
|
+
...parameters,
|
|
126
|
+
]);
|
|
127
|
+
if (reg.encoder)
|
|
128
|
+
x = reg.encoder(x);
|
|
129
|
+
if (x != null && typeof x !== 'string')
|
|
130
|
+
x = JSON.stringify(x);
|
|
131
|
+
callback(x);
|
|
132
|
+
}
|
|
133
|
+
catch (err) {
|
|
134
|
+
const error = new common_1.OpraException(err);
|
|
135
|
+
callback(error);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
getControllerInstance(controllerPath) {
|
|
141
|
+
const controller = this.api.findController(controllerPath);
|
|
142
|
+
return controller && this._controllerInstances.get(controller);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.SocketioAdapter = SocketioAdapter;
|
|
146
|
+
SocketioAdapter.PlatformName = 'socketio';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SocketioContext = void 0;
|
|
4
|
+
const core_1 = require("@opra/core");
|
|
5
|
+
/**
|
|
6
|
+
* Provides the context for handling messages.
|
|
7
|
+
* It extends the ExecutionContext and implements the AsyncEventEmitter.
|
|
8
|
+
*/
|
|
9
|
+
class SocketioContext extends core_1.ExecutionContext {
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
* @param init the context options
|
|
13
|
+
*/
|
|
14
|
+
constructor(init) {
|
|
15
|
+
super({
|
|
16
|
+
...init,
|
|
17
|
+
__docNode: init.__oprDef?.node ||
|
|
18
|
+
init.__contDef?.node ||
|
|
19
|
+
init.__adapter.document.node,
|
|
20
|
+
transport: 'ws',
|
|
21
|
+
platform: 'socketio',
|
|
22
|
+
});
|
|
23
|
+
this.parameters = [];
|
|
24
|
+
this.socket = init.socket;
|
|
25
|
+
this.event = init.event;
|
|
26
|
+
this.parameters = init.parameters || [];
|
|
27
|
+
if (init.__contDef)
|
|
28
|
+
this.__contDef = init.__contDef;
|
|
29
|
+
if (init.__controller)
|
|
30
|
+
this.__controller = init.__controller;
|
|
31
|
+
if (init.__oprDef)
|
|
32
|
+
this.__oprDef = init.__oprDef;
|
|
33
|
+
if (init.__handler)
|
|
34
|
+
this.__handler = init.__handler;
|
|
35
|
+
}
|
|
36
|
+
get server() {
|
|
37
|
+
return this.__adapter.server;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.SocketioContext = SocketioContext;
|
package/esm/index.js
ADDED
package/esm/package.json
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { OpraException, WSApi, } from '@opra/common';
|
|
2
|
+
import { PlatformAdapter } from '@opra/core';
|
|
3
|
+
import * as socketio from 'socket.io';
|
|
4
|
+
import { SocketioContext } from './socketio-context.js';
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @class SocketioAdapter
|
|
8
|
+
*/
|
|
9
|
+
export class SocketioAdapter extends PlatformAdapter {
|
|
10
|
+
constructor(document, options) {
|
|
11
|
+
super(options);
|
|
12
|
+
this._controllerInstances = new Map();
|
|
13
|
+
this._eventsRegByName = new Map();
|
|
14
|
+
this._eventsRegByPattern = [];
|
|
15
|
+
this.transform = 'ws';
|
|
16
|
+
this.platform = SocketioAdapter.PlatformName;
|
|
17
|
+
this._document = document;
|
|
18
|
+
if (!(this.document.api instanceof WSApi)) {
|
|
19
|
+
throw new TypeError(`The document doesn't expose a WS Api`);
|
|
20
|
+
}
|
|
21
|
+
this.interceptors = [...(options?.interceptors || [])];
|
|
22
|
+
this._scope = options?.scope;
|
|
23
|
+
this.server = new socketio.Server();
|
|
24
|
+
this.server.on('error', error => {
|
|
25
|
+
this.emit('error', error, undefined, this);
|
|
26
|
+
});
|
|
27
|
+
this.server.on('connection', (socket) => {
|
|
28
|
+
this._initSocket(socket);
|
|
29
|
+
this.emit('connection', socket, this);
|
|
30
|
+
});
|
|
31
|
+
for (const contDef of this.api.controllers.values()) {
|
|
32
|
+
for (const oprDef of contDef.operations.values()) {
|
|
33
|
+
const fn = contDef.instance[oprDef.name];
|
|
34
|
+
if (typeof fn !== 'function')
|
|
35
|
+
continue;
|
|
36
|
+
const reg = {
|
|
37
|
+
event: oprDef.event,
|
|
38
|
+
contDef,
|
|
39
|
+
oprDef,
|
|
40
|
+
handler: fn,
|
|
41
|
+
decoders: [],
|
|
42
|
+
};
|
|
43
|
+
if (typeof reg.event === 'string')
|
|
44
|
+
this._eventsRegByName.set(reg.event, reg);
|
|
45
|
+
else
|
|
46
|
+
this._eventsRegByPattern.push(reg);
|
|
47
|
+
/** Generate decoders */
|
|
48
|
+
if (oprDef.arguments?.length) {
|
|
49
|
+
for (const dt of oprDef.arguments) {
|
|
50
|
+
reg.decoders.push(dt.generateCodec('decode', {
|
|
51
|
+
scope: this.scope,
|
|
52
|
+
ignoreReadonlyFields: true,
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Generate response encoder */
|
|
57
|
+
if (oprDef.response) {
|
|
58
|
+
reg.encoder = oprDef.response.generateCodec('encode', {
|
|
59
|
+
scope: this.scope,
|
|
60
|
+
ignoreWriteonlyFields: true,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
get api() {
|
|
67
|
+
return this.document.getWsApi();
|
|
68
|
+
}
|
|
69
|
+
get scope() {
|
|
70
|
+
return this._scope;
|
|
71
|
+
}
|
|
72
|
+
close() {
|
|
73
|
+
return this.server.close();
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Attaches socket.io to a server or port.
|
|
77
|
+
*
|
|
78
|
+
* @param srv - server or port
|
|
79
|
+
* @param opts - options passed to engine.io
|
|
80
|
+
* @return self
|
|
81
|
+
*/
|
|
82
|
+
listen(srv, opts) {
|
|
83
|
+
if (this.server.httpServer?.listening)
|
|
84
|
+
throw new Error('Server is already listening');
|
|
85
|
+
if (opts?.path)
|
|
86
|
+
this.server.path(opts?.path);
|
|
87
|
+
this.server.listen(srv, opts);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
_initSocket(socket) {
|
|
91
|
+
socket.on('close', () => {
|
|
92
|
+
this.emit('close', socket, this);
|
|
93
|
+
});
|
|
94
|
+
socket.on('error', error => {
|
|
95
|
+
this.emit('error', error, socket, this);
|
|
96
|
+
});
|
|
97
|
+
socket.onAny((event, ...args) => {
|
|
98
|
+
const callback = args.length > 0 ? args[args.length - 1] : null;
|
|
99
|
+
const reg = this._eventsRegByName.get(event) ||
|
|
100
|
+
this._eventsRegByPattern.find(r => r.event.test(event));
|
|
101
|
+
if (!reg) {
|
|
102
|
+
if (callback)
|
|
103
|
+
callback(new Error(`Unknown event "${event}"`));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const parameters = callback ? args.slice(0, -1) : args;
|
|
107
|
+
Promise.resolve().then(async () => {
|
|
108
|
+
const ctx = new SocketioContext({
|
|
109
|
+
__adapter: this,
|
|
110
|
+
__contDef: reg.contDef,
|
|
111
|
+
__oprDef: reg.oprDef,
|
|
112
|
+
__controller: reg.contDef.instance,
|
|
113
|
+
__handler: reg.handler,
|
|
114
|
+
socket,
|
|
115
|
+
event,
|
|
116
|
+
parameters,
|
|
117
|
+
});
|
|
118
|
+
try {
|
|
119
|
+
let x = await reg.handler.apply(reg.contDef.instance, [
|
|
120
|
+
ctx,
|
|
121
|
+
...parameters,
|
|
122
|
+
]);
|
|
123
|
+
if (reg.encoder)
|
|
124
|
+
x = reg.encoder(x);
|
|
125
|
+
if (x != null && typeof x !== 'string')
|
|
126
|
+
x = JSON.stringify(x);
|
|
127
|
+
callback(x);
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
const error = new OpraException(err);
|
|
131
|
+
callback(error);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
getControllerInstance(controllerPath) {
|
|
137
|
+
const controller = this.api.findController(controllerPath);
|
|
138
|
+
return controller && this._controllerInstances.get(controller);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
SocketioAdapter.PlatformName = 'socketio';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ExecutionContext } from '@opra/core';
|
|
2
|
+
/**
|
|
3
|
+
* Provides the context for handling messages.
|
|
4
|
+
* It extends the ExecutionContext and implements the AsyncEventEmitter.
|
|
5
|
+
*/
|
|
6
|
+
export class SocketioContext extends ExecutionContext {
|
|
7
|
+
/**
|
|
8
|
+
* Constructor
|
|
9
|
+
* @param init the context options
|
|
10
|
+
*/
|
|
11
|
+
constructor(init) {
|
|
12
|
+
super({
|
|
13
|
+
...init,
|
|
14
|
+
__docNode: init.__oprDef?.node ||
|
|
15
|
+
init.__contDef?.node ||
|
|
16
|
+
init.__adapter.document.node,
|
|
17
|
+
transport: 'ws',
|
|
18
|
+
platform: 'socketio',
|
|
19
|
+
});
|
|
20
|
+
this.parameters = [];
|
|
21
|
+
this.socket = init.socket;
|
|
22
|
+
this.event = init.event;
|
|
23
|
+
this.parameters = init.parameters || [];
|
|
24
|
+
if (init.__contDef)
|
|
25
|
+
this.__contDef = init.__contDef;
|
|
26
|
+
if (init.__controller)
|
|
27
|
+
this.__controller = init.__controller;
|
|
28
|
+
if (init.__oprDef)
|
|
29
|
+
this.__oprDef = init.__oprDef;
|
|
30
|
+
if (init.__handler)
|
|
31
|
+
this.__handler = init.__handler;
|
|
32
|
+
}
|
|
33
|
+
get server() {
|
|
34
|
+
return this.__adapter.server;
|
|
35
|
+
}
|
|
36
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@opra/socketio",
|
|
3
|
+
"version": "1.20.0",
|
|
4
|
+
"description": "Opra Socket.IO adapter",
|
|
5
|
+
"author": "Panates",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@jsopen/objects": "^2.0.2",
|
|
9
|
+
"@browsery/type-is": "^2.0.1",
|
|
10
|
+
"content-type": "^1.0.5",
|
|
11
|
+
"iconv-lite": "^0.7.0",
|
|
12
|
+
"node-events-async": "^1.2.0",
|
|
13
|
+
"tslib": "^2.8.1",
|
|
14
|
+
"valgen": "^5.18.2"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@opra/common": "^1.20.0",
|
|
18
|
+
"@opra/core": "^1.20.0",
|
|
19
|
+
"socket.io": ">=4.0.0"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./types/index.d.ts",
|
|
26
|
+
"default": "./esm/index.js"
|
|
27
|
+
},
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./types/index.d.cts",
|
|
30
|
+
"default": "./cjs/index.js"
|
|
31
|
+
},
|
|
32
|
+
"default": "./esm/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"main": "./cjs/index.js",
|
|
37
|
+
"module": "./esm/index.js",
|
|
38
|
+
"types": "./types/index.d.ts",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/panates/opra.git",
|
|
42
|
+
"directory": "packages/socketio"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=16.0",
|
|
46
|
+
"npm": ">=7.0.0"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"cjs/",
|
|
50
|
+
"esm/",
|
|
51
|
+
"types/",
|
|
52
|
+
"LICENSE",
|
|
53
|
+
"README.md"
|
|
54
|
+
],
|
|
55
|
+
"keywords": [
|
|
56
|
+
"opra",
|
|
57
|
+
"socketio",
|
|
58
|
+
"websocket",
|
|
59
|
+
"ws"
|
|
60
|
+
],
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ApiDocument, OpraSchema, WSApi, WSController, WSOperation } from '@opra/common';
|
|
2
|
+
import { PlatformAdapter } from '@opra/core';
|
|
3
|
+
import type * as http from 'http';
|
|
4
|
+
import type * as http2 from 'http2';
|
|
5
|
+
import type * as https from 'https';
|
|
6
|
+
import * as socketio from 'socket.io';
|
|
7
|
+
import { SocketioContext } from './socketio-context.js';
|
|
8
|
+
type TServerInstance = http.Server | https.Server | http2.Http2SecureServer | http2.Http2Server;
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @class SocketioAdapter
|
|
12
|
+
*/
|
|
13
|
+
export declare class SocketioAdapter extends PlatformAdapter<SocketioAdapter.Events> {
|
|
14
|
+
static readonly PlatformName = "socketio";
|
|
15
|
+
protected _controllerInstances: Map<WSController, any>;
|
|
16
|
+
protected _eventsRegByName: Map<string, MessageHandlerReg>;
|
|
17
|
+
protected _eventsRegByPattern: MessageHandlerReg[];
|
|
18
|
+
protected _scope?: string;
|
|
19
|
+
readonly transform: OpraSchema.Transport;
|
|
20
|
+
readonly platform = "socketio";
|
|
21
|
+
readonly interceptors: (SocketioAdapter.InterceptorFunction | SocketioAdapter.IWSInterceptor)[];
|
|
22
|
+
readonly server: socketio.Server;
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @constructor
|
|
26
|
+
*/
|
|
27
|
+
constructor(document: ApiDocument, options?: SocketioAdapter.Options);
|
|
28
|
+
get api(): WSApi;
|
|
29
|
+
get scope(): string | undefined;
|
|
30
|
+
close(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Attaches socket.io to a server or port.
|
|
33
|
+
*
|
|
34
|
+
* @param srv - server or port
|
|
35
|
+
* @param opts - options passed to engine.io
|
|
36
|
+
* @return self
|
|
37
|
+
*/
|
|
38
|
+
listen(srv: TServerInstance | number, opts?: Partial<socketio.ServerOptions>): this;
|
|
39
|
+
protected _initSocket(socket: socketio.Socket): void;
|
|
40
|
+
getControllerInstance<T>(controllerPath: string): T | undefined;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @namespace SocketioAdapter
|
|
44
|
+
*/
|
|
45
|
+
export declare namespace SocketioAdapter {
|
|
46
|
+
type NextCallback = () => Promise<any>;
|
|
47
|
+
interface Options extends PlatformAdapter.Options {
|
|
48
|
+
interceptors?: (InterceptorFunction | IWSInterceptor)[];
|
|
49
|
+
scope?: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @type InterceptorFunction
|
|
53
|
+
*/
|
|
54
|
+
type InterceptorFunction = IWSInterceptor['intercept'];
|
|
55
|
+
/**
|
|
56
|
+
* @interface IWSInterceptor
|
|
57
|
+
*/
|
|
58
|
+
type IWSInterceptor = {
|
|
59
|
+
intercept(context: SocketioContext, next: NextCallback): Promise<any>;
|
|
60
|
+
};
|
|
61
|
+
interface Events {
|
|
62
|
+
error: [
|
|
63
|
+
error: Error,
|
|
64
|
+
socket: socketio.Socket | undefined,
|
|
65
|
+
_this: SocketioAdapter
|
|
66
|
+
];
|
|
67
|
+
connection: [socket: socketio.Socket, _this: SocketioAdapter];
|
|
68
|
+
close: [socket: socketio.Socket, _this: SocketioAdapter];
|
|
69
|
+
execute: [context: SocketioContext];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @interface
|
|
74
|
+
*/
|
|
75
|
+
interface MessageHandlerReg {
|
|
76
|
+
event: string | RegExp;
|
|
77
|
+
handler: Function;
|
|
78
|
+
contDef: WSController;
|
|
79
|
+
oprDef: WSOperation;
|
|
80
|
+
decoders: ((data: any) => any)[];
|
|
81
|
+
encoder?: (data: any) => any;
|
|
82
|
+
}
|
|
83
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { WSController, WSOperation } from '@opra/common';
|
|
2
|
+
import { ExecutionContext } from '@opra/core';
|
|
3
|
+
import type { AsyncEventEmitter } from 'node-events-async';
|
|
4
|
+
import * as socketio from 'socket.io';
|
|
5
|
+
import type { SocketioAdapter } from './socketio-adapter.js';
|
|
6
|
+
/**
|
|
7
|
+
* Provides the context for handling messages.
|
|
8
|
+
* It extends the ExecutionContext and implements the AsyncEventEmitter.
|
|
9
|
+
*/
|
|
10
|
+
export declare class SocketioContext extends ExecutionContext implements AsyncEventEmitter {
|
|
11
|
+
readonly __contDef: WSController;
|
|
12
|
+
readonly __oprDef: WSOperation;
|
|
13
|
+
readonly __controller: any;
|
|
14
|
+
readonly __handler?: Function;
|
|
15
|
+
readonly __adapter: SocketioAdapter;
|
|
16
|
+
readonly socket: socketio.Socket;
|
|
17
|
+
readonly event: string;
|
|
18
|
+
readonly parameters: any[];
|
|
19
|
+
/**
|
|
20
|
+
* Constructor
|
|
21
|
+
* @param init the context options
|
|
22
|
+
*/
|
|
23
|
+
constructor(init: SocketioContext.Initiator);
|
|
24
|
+
get server(): socketio.Server;
|
|
25
|
+
}
|
|
26
|
+
export declare namespace SocketioContext {
|
|
27
|
+
interface Initiator extends Omit<ExecutionContext.Initiator, 'document' | 'transport' | 'platform' | '__docNode'> {
|
|
28
|
+
__adapter: SocketioAdapter;
|
|
29
|
+
__contDef?: WSController;
|
|
30
|
+
__oprDef?: WSOperation;
|
|
31
|
+
__controller?: any;
|
|
32
|
+
__handler?: Function;
|
|
33
|
+
socket: socketio.Socket;
|
|
34
|
+
event: string;
|
|
35
|
+
parameters?: any[];
|
|
36
|
+
}
|
|
37
|
+
}
|