@opra/socketio 1.22.9 → 1.23.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/package.json +3 -3
- package/socketio-adapter.d.ts +2 -0
- package/socketio-adapter.js +24 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/socketio",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.23.0",
|
|
4
4
|
"description": "Opra Socket.IO adapter",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"valgen": "^5.19.4"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@opra/common": "^1.
|
|
18
|
-
"@opra/core": "^1.
|
|
17
|
+
"@opra/common": "^1.23.0",
|
|
18
|
+
"@opra/core": "^1.23.0",
|
|
19
19
|
"socket.io": ">=4.0.0"
|
|
20
20
|
},
|
|
21
21
|
"type": "module",
|
package/socketio-adapter.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type * as http from 'http';
|
|
|
4
4
|
import type * as http2 from 'http2';
|
|
5
5
|
import type * as https from 'https';
|
|
6
6
|
import * as socketio from 'socket.io';
|
|
7
|
+
import { type Validator } from 'valgen';
|
|
7
8
|
import { SocketioContext } from './socketio-context.js';
|
|
8
9
|
type TServerInstance = http.Server | https.Server | http2.Http2SecureServer | http2.Http2Server;
|
|
9
10
|
/**
|
|
@@ -12,6 +13,7 @@ type TServerInstance = http.Server | https.Server | http2.Http2SecureServer | ht
|
|
|
12
13
|
*/
|
|
13
14
|
export declare class SocketioAdapter extends PlatformAdapter<SocketioAdapter.Events> {
|
|
14
15
|
static readonly PlatformName = "socketio";
|
|
16
|
+
protected _operationResultEncoder: Validator;
|
|
15
17
|
protected _controllerInstances: Map<WSController, any>;
|
|
16
18
|
protected _eventsRegByName: Map<string, MessageHandlerReg>;
|
|
17
19
|
protected _eventsRegByPattern: MessageHandlerReg[];
|
package/socketio-adapter.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OpraException, WSApi, } from '@opra/common';
|
|
1
|
+
import { OperationResult, OpraException, WSApi, } from '@opra/common';
|
|
2
2
|
import { PlatformAdapter } from '@opra/core';
|
|
3
3
|
import * as socketio from 'socket.io';
|
|
4
4
|
import { vg } from 'valgen';
|
|
@@ -9,6 +9,7 @@ import { SocketioContext } from './socketio-context.js';
|
|
|
9
9
|
*/
|
|
10
10
|
export class SocketioAdapter extends PlatformAdapter {
|
|
11
11
|
static PlatformName = 'socketio';
|
|
12
|
+
_operationResultEncoder;
|
|
12
13
|
_controllerInstances = new Map();
|
|
13
14
|
_eventsRegByName = new Map();
|
|
14
15
|
_eventsRegByPattern = [];
|
|
@@ -33,6 +34,11 @@ export class SocketioAdapter extends PlatformAdapter {
|
|
|
33
34
|
this._initSocket(socket);
|
|
34
35
|
this.emit('connection', socket, this);
|
|
35
36
|
});
|
|
37
|
+
const operationResultType = document.node.getDataType(OperationResult);
|
|
38
|
+
this._operationResultEncoder = operationResultType.generateCodec('encode', {
|
|
39
|
+
scope: this.scope,
|
|
40
|
+
ignoreWriteonlyFields: true,
|
|
41
|
+
});
|
|
36
42
|
}
|
|
37
43
|
get api() {
|
|
38
44
|
return this.document.getWsApi();
|
|
@@ -114,14 +120,13 @@ export class SocketioAdapter extends PlatformAdapter {
|
|
|
114
120
|
const callback = args.length > 0 && typeof args[args.length - 1] === 'function'
|
|
115
121
|
? args[args.length - 1]
|
|
116
122
|
: null;
|
|
117
|
-
const reg = this._eventsRegByName.get(event) ||
|
|
118
|
-
this._eventsRegByPattern.find(r => r.event.test(event));
|
|
119
|
-
if (!reg) {
|
|
120
|
-
callback?.(new OpraException(`Unknown event "${event}"`));
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
123
|
Promise.resolve().then(async () => {
|
|
124
124
|
try {
|
|
125
|
+
const reg = this._eventsRegByName.get(event) ||
|
|
126
|
+
this._eventsRegByPattern.find(r => r.event.test(event));
|
|
127
|
+
if (!reg) {
|
|
128
|
+
throw new OpraException(`Unknown event "${event}"`);
|
|
129
|
+
}
|
|
125
130
|
const inputParameters = callback ? args.slice(0, -1) : args;
|
|
126
131
|
const ctx = new SocketioContext({
|
|
127
132
|
__adapter: this,
|
|
@@ -150,17 +155,25 @@ export class SocketioAdapter extends PlatformAdapter {
|
|
|
150
155
|
}
|
|
151
156
|
i++;
|
|
152
157
|
}
|
|
153
|
-
|
|
158
|
+
const resp = await reg.handler.apply(reg.contDef.instance, callArgs);
|
|
154
159
|
if (callback) {
|
|
160
|
+
let out;
|
|
155
161
|
if (reg.encoder)
|
|
156
|
-
|
|
157
|
-
|
|
162
|
+
out = reg.encoder(resp);
|
|
163
|
+
if (!(resp instanceof OperationResult))
|
|
164
|
+
out = this._operationResultEncoder({
|
|
165
|
+
payload: out,
|
|
166
|
+
});
|
|
167
|
+
callback(out);
|
|
158
168
|
}
|
|
159
169
|
}
|
|
160
170
|
catch (err) {
|
|
161
171
|
if (callback) {
|
|
162
172
|
const error = err instanceof OpraException ? err : new OpraException(err);
|
|
163
|
-
|
|
173
|
+
const out = this._operationResultEncoder({
|
|
174
|
+
errors: [error],
|
|
175
|
+
});
|
|
176
|
+
callback(out);
|
|
164
177
|
}
|
|
165
178
|
}
|
|
166
179
|
});
|