@opra/socketio 1.22.9 → 1.23.2
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 +4 -4
- package/socketio-adapter.d.ts +2 -0
- package/socketio-adapter.js +26 -12
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/socketio",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.23.2",
|
|
4
4
|
"description": "Opra Socket.IO adapter",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@jsopen/objects": "^2.
|
|
8
|
+
"@jsopen/objects": "^2.1.1",
|
|
9
9
|
"@browsery/type-is": "^2.0.1",
|
|
10
10
|
"content-type": "^1.0.5",
|
|
11
11
|
"iconv-lite": "^0.7.2",
|
|
@@ -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.2",
|
|
18
|
+
"@opra/core": "^1.23.2",
|
|
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,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { updateErrorMessage } from '@jsopen/objects';
|
|
2
|
+
import { OperationResult, OpraException, WSApi, } from '@opra/common';
|
|
2
3
|
import { PlatformAdapter } from '@opra/core';
|
|
3
4
|
import * as socketio from 'socket.io';
|
|
4
5
|
import { vg } from 'valgen';
|
|
@@ -9,6 +10,7 @@ import { SocketioContext } from './socketio-context.js';
|
|
|
9
10
|
*/
|
|
10
11
|
export class SocketioAdapter extends PlatformAdapter {
|
|
11
12
|
static PlatformName = 'socketio';
|
|
13
|
+
_operationResultEncoder;
|
|
12
14
|
_controllerInstances = new Map();
|
|
13
15
|
_eventsRegByName = new Map();
|
|
14
16
|
_eventsRegByPattern = [];
|
|
@@ -33,6 +35,11 @@ export class SocketioAdapter extends PlatformAdapter {
|
|
|
33
35
|
this._initSocket(socket);
|
|
34
36
|
this.emit('connection', socket, this);
|
|
35
37
|
});
|
|
38
|
+
const operationResultType = document.node.getDataType(OperationResult);
|
|
39
|
+
this._operationResultEncoder = operationResultType.generateCodec('encode', {
|
|
40
|
+
scope: this.scope,
|
|
41
|
+
ignoreWriteonlyFields: true,
|
|
42
|
+
});
|
|
36
43
|
}
|
|
37
44
|
get api() {
|
|
38
45
|
return this.document.getWsApi();
|
|
@@ -114,14 +121,13 @@ export class SocketioAdapter extends PlatformAdapter {
|
|
|
114
121
|
const callback = args.length > 0 && typeof args[args.length - 1] === 'function'
|
|
115
122
|
? args[args.length - 1]
|
|
116
123
|
: 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
124
|
Promise.resolve().then(async () => {
|
|
124
125
|
try {
|
|
126
|
+
const reg = this._eventsRegByName.get(event) ||
|
|
127
|
+
this._eventsRegByPattern.find(r => r.event.test(event));
|
|
128
|
+
if (!reg) {
|
|
129
|
+
throw new OpraException(`Unknown event "${event}"`);
|
|
130
|
+
}
|
|
125
131
|
const inputParameters = callback ? args.slice(0, -1) : args;
|
|
126
132
|
const ctx = new SocketioContext({
|
|
127
133
|
__adapter: this,
|
|
@@ -145,22 +151,30 @@ export class SocketioAdapter extends PlatformAdapter {
|
|
|
145
151
|
callArgs.push(v);
|
|
146
152
|
}
|
|
147
153
|
catch (err) {
|
|
148
|
-
err
|
|
154
|
+
updateErrorMessage(err, `Failed to decode parameter ${i} of event "${event}": ${err.message}`);
|
|
149
155
|
throw err;
|
|
150
156
|
}
|
|
151
157
|
i++;
|
|
152
158
|
}
|
|
153
|
-
|
|
159
|
+
const resp = await reg.handler.apply(reg.contDef.instance, callArgs);
|
|
154
160
|
if (callback) {
|
|
161
|
+
let out;
|
|
155
162
|
if (reg.encoder)
|
|
156
|
-
|
|
157
|
-
|
|
163
|
+
out = reg.encoder(resp);
|
|
164
|
+
if (!(resp instanceof OperationResult))
|
|
165
|
+
out = this._operationResultEncoder({
|
|
166
|
+
payload: out,
|
|
167
|
+
});
|
|
168
|
+
callback(out);
|
|
158
169
|
}
|
|
159
170
|
}
|
|
160
171
|
catch (err) {
|
|
161
172
|
if (callback) {
|
|
162
173
|
const error = err instanceof OpraException ? err : new OpraException(err);
|
|
163
|
-
|
|
174
|
+
const out = this._operationResultEncoder({
|
|
175
|
+
errors: [error],
|
|
176
|
+
});
|
|
177
|
+
callback(out);
|
|
164
178
|
}
|
|
165
179
|
}
|
|
166
180
|
});
|