@opra/socketio 1.22.8 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/socketio",
3
- "version": "1.22.8",
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.22.8",
18
- "@opra/core": "^1.22.8",
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",
@@ -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[];
@@ -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();
@@ -111,15 +117,16 @@ export class SocketioAdapter extends PlatformAdapter {
111
117
  this.emit('error', error, socket, this);
112
118
  });
113
119
  socket.onAny((event, ...args) => {
114
- const callback = args.length > 0 ? args[args.length - 1] : null;
115
- const reg = this._eventsRegByName.get(event) ||
116
- this._eventsRegByPattern.find(r => r.event.test(event));
117
- if (!reg) {
118
- callback?.(new OpraException(`Unknown event "${event}"`));
119
- return;
120
- }
120
+ const callback = args.length > 0 && typeof args[args.length - 1] === 'function'
121
+ ? args[args.length - 1]
122
+ : null;
121
123
  Promise.resolve().then(async () => {
122
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
+ }
123
130
  const inputParameters = callback ? args.slice(0, -1) : args;
124
131
  const ctx = new SocketioContext({
125
132
  __adapter: this,
@@ -148,17 +155,25 @@ export class SocketioAdapter extends PlatformAdapter {
148
155
  }
149
156
  i++;
150
157
  }
151
- let x = await reg.handler.apply(reg.contDef.instance, callArgs);
158
+ const resp = await reg.handler.apply(reg.contDef.instance, callArgs);
152
159
  if (callback) {
160
+ let out;
153
161
  if (reg.encoder)
154
- x = reg.encoder(x);
155
- callback(x);
162
+ out = reg.encoder(resp);
163
+ if (!(resp instanceof OperationResult))
164
+ out = this._operationResultEncoder({
165
+ payload: out,
166
+ });
167
+ callback(out);
156
168
  }
157
169
  }
158
170
  catch (err) {
159
171
  if (callback) {
160
172
  const error = err instanceof OpraException ? err : new OpraException(err);
161
- callback({ errors: [error] });
173
+ const out = this._operationResultEncoder({
174
+ errors: [error],
175
+ });
176
+ callback(out);
162
177
  }
163
178
  }
164
179
  });