@opra/socketio 1.21.0 → 1.22.1

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.21.0",
3
+ "version": "1.22.1",
4
4
  "description": "Opra Socket.IO adapter",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -8,50 +8,34 @@
8
8
  "@jsopen/objects": "^2.0.2",
9
9
  "@browsery/type-is": "^2.0.1",
10
10
  "content-type": "^1.0.5",
11
- "iconv-lite": "^0.7.0",
12
- "node-events-async": "^1.2.0",
11
+ "iconv-lite": "^0.7.1",
12
+ "node-events-async": "^1.5.0",
13
13
  "tslib": "^2.8.1",
14
14
  "valgen": "^5.18.2"
15
15
  },
16
16
  "peerDependencies": {
17
- "@opra/common": "^1.21.0",
18
- "@opra/core": "^1.21.0",
17
+ "@opra/common": "^1.22.1",
18
+ "@opra/core": "^1.22.1",
19
19
  "socket.io": ">=4.0.0"
20
20
  },
21
21
  "type": "module",
22
+ "module": "./index.js",
23
+ "types": "./index.d.ts",
22
24
  "exports": {
23
25
  ".": {
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"
26
+ "types": "./index.d.ts",
27
+ "default": "./index.js"
33
28
  },
34
29
  "./package.json": "./package.json"
35
30
  },
36
- "main": "./cjs/index.js",
37
- "module": "./esm/index.js",
38
- "types": "./types/index.d.ts",
31
+ "engines": {
32
+ "node": ">=20.0"
33
+ },
39
34
  "repository": {
40
35
  "type": "git",
41
36
  "url": "git+https://github.com/panates/opra.git",
42
37
  "directory": "packages/socketio"
43
38
  },
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
39
  "keywords": [
56
40
  "opra",
57
41
  "socketio",
@@ -1,19 +1,24 @@
1
1
  import { OpraException, WSApi, } from '@opra/common';
2
2
  import { PlatformAdapter } from '@opra/core';
3
3
  import * as socketio from 'socket.io';
4
+ import { vg } from 'valgen';
4
5
  import { SocketioContext } from './socketio-context.js';
5
6
  /**
6
7
  *
7
8
  * @class SocketioAdapter
8
9
  */
9
10
  export class SocketioAdapter extends PlatformAdapter {
11
+ static PlatformName = 'socketio';
12
+ _controllerInstances = new Map();
13
+ _eventsRegByName = new Map();
14
+ _eventsRegByPattern = [];
15
+ _scope;
16
+ transform = 'ws';
17
+ platform = SocketioAdapter.PlatformName;
18
+ interceptors;
19
+ server;
10
20
  constructor(document, options) {
11
21
  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
22
  this._document = document;
18
23
  if (!(this.document.api instanceof WSApi)) {
19
24
  throw new TypeError(`The document doesn't expose a WS Api`);
@@ -73,10 +78,15 @@ export class SocketioAdapter extends PlatformAdapter {
73
78
  /** Generate decoders */
74
79
  if (oprDef.arguments?.length) {
75
80
  for (const arg of oprDef.arguments) {
76
- reg.decoders.push(arg.type.generateCodec('decode', {
81
+ let fn2 = arg.type.generateCodec('decode', {
77
82
  scope: this.scope,
78
83
  ignoreReadonlyFields: true,
79
- }));
84
+ });
85
+ if (arg.required)
86
+ fn2 = vg.required(fn2);
87
+ else
88
+ fn2 = vg.optional(fn2);
89
+ reg.decoders.push(fn2);
80
90
  }
81
91
  }
82
92
  /** Generate response encoder */
@@ -156,4 +166,3 @@ export class SocketioAdapter extends PlatformAdapter {
156
166
  return controller && this._controllerInstances.get(controller);
157
167
  }
158
168
  }
159
- SocketioAdapter.PlatformName = 'socketio';
@@ -4,6 +4,9 @@ import { ExecutionContext } from '@opra/core';
4
4
  * It extends the ExecutionContext and implements the AsyncEventEmitter.
5
5
  */
6
6
  export class SocketioContext extends ExecutionContext {
7
+ socket;
8
+ event;
9
+ parameters = [];
7
10
  /**
8
11
  * Constructor
9
12
  * @param init the context options
@@ -17,7 +20,6 @@ export class SocketioContext extends ExecutionContext {
17
20
  transport: 'ws',
18
21
  platform: 'socketio',
19
22
  });
20
- this.parameters = [];
21
23
  this.socket = init.socket;
22
24
  this.event = init.event;
23
25
  this.parameters = init.parameters || [];
package/cjs/index.js DELETED
@@ -1,6 +0,0 @@
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 DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
@@ -1,164 +0,0 @@
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
- }
36
- get api() {
37
- return this.document.getWsApi();
38
- }
39
- get scope() {
40
- return this._scope;
41
- }
42
- async close() {
43
- return this.server.close().finally(() => {
44
- this._controllerInstances.clear();
45
- this._eventsRegByName.clear();
46
- this._eventsRegByPattern = [];
47
- });
48
- }
49
- /**
50
- * Attaches socket.io to a server or port.
51
- *
52
- * @param srv - server or port
53
- * @param opts - options passed to engine.io
54
- * @return self
55
- */
56
- listen(srv, opts) {
57
- if (this.server.httpServer?.listening)
58
- throw new Error('Server is already listening');
59
- if (opts?.path)
60
- this.server.path(opts?.path);
61
- for (const contDef of this.api.controllers.values()) {
62
- for (const oprDef of contDef.operations.values()) {
63
- const fn = contDef.instance[oprDef.name];
64
- if (typeof fn !== 'function')
65
- continue;
66
- const reg = {
67
- event: oprDef.event,
68
- contDef,
69
- oprDef,
70
- handler: fn,
71
- decoders: [],
72
- };
73
- if (typeof reg.event === 'string')
74
- this._eventsRegByName.set(reg.event, reg);
75
- else
76
- this._eventsRegByPattern.push(reg);
77
- /** Generate decoders */
78
- if (oprDef.arguments?.length) {
79
- for (const arg of oprDef.arguments) {
80
- reg.decoders.push(arg.type.generateCodec('decode', {
81
- scope: this.scope,
82
- ignoreReadonlyFields: true,
83
- }));
84
- }
85
- }
86
- /** Generate response encoder */
87
- if (oprDef.response) {
88
- reg.encoder = oprDef.response.generateCodec('encode', {
89
- scope: this.scope,
90
- ignoreWriteonlyFields: true,
91
- });
92
- }
93
- }
94
- }
95
- this.server.listen(srv, opts);
96
- return this;
97
- }
98
- _initSocket(socket) {
99
- socket.on('close', () => {
100
- this.emit('close', socket, this);
101
- });
102
- socket.on('error', error => {
103
- this.emit('error', error, socket, this);
104
- });
105
- socket.onAny((event, ...args) => {
106
- const callback = args.length > 0 ? args[args.length - 1] : null;
107
- const reg = this._eventsRegByName.get(event) ||
108
- this._eventsRegByPattern.find(r => r.event.test(event));
109
- if (!reg) {
110
- if (callback)
111
- callback(new common_1.OpraException(`Unknown event "${event}"`));
112
- return;
113
- }
114
- Promise.resolve().then(async () => {
115
- try {
116
- const inputParameters = callback ? args.slice(0, -1) : args;
117
- const ctx = new socketio_context_js_1.SocketioContext({
118
- __adapter: this,
119
- __contDef: reg.contDef,
120
- __oprDef: reg.oprDef,
121
- __controller: reg.contDef.instance,
122
- __handler: reg.handler,
123
- socket,
124
- event,
125
- });
126
- const callArgs = [ctx];
127
- let i = 0;
128
- for (const prm of inputParameters) {
129
- try {
130
- const v = reg.decoders[i](prm);
131
- const arg = reg.oprDef.arguments[i];
132
- ctx.parameters.push(v);
133
- if (arg.parameterIndex != null)
134
- callArgs[arg.parameterIndex] = v;
135
- else
136
- callArgs.push(v);
137
- }
138
- catch (err) {
139
- err.message = `Failed to decode parameter ${i} of event "${event}": ${err.message}`;
140
- throw err;
141
- }
142
- i++;
143
- }
144
- let x = await reg.handler.apply(reg.contDef.instance, callArgs);
145
- if (reg.encoder)
146
- x = reg.encoder(x);
147
- if (x != null && typeof x !== 'string')
148
- x = JSON.stringify(x);
149
- callback(x);
150
- }
151
- catch (err) {
152
- const error = err instanceof common_1.OpraException ? err : new common_1.OpraException(err);
153
- callback({ error });
154
- }
155
- });
156
- });
157
- }
158
- getControllerInstance(controllerPath) {
159
- const controller = this.api.findController(controllerPath);
160
- return controller && this._controllerInstances.get(controller);
161
- }
162
- }
163
- exports.SocketioAdapter = SocketioAdapter;
164
- SocketioAdapter.PlatformName = 'socketio';
@@ -1,40 +0,0 @@
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/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "module"
3
- }
package/types/index.d.cts DELETED
@@ -1,2 +0,0 @@
1
- export * from './socketio-adapter.js';
2
- export * from './socketio-context.js';
File without changes
File without changes