@iobroker/socket-classes 1.6.2 → 2.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iobroker/socket-classes",
3
- "version": "1.6.2",
3
+ "version": "2.0.2",
4
4
  "description": "ioBroker server-side web sockets",
5
5
  "author": {
6
6
  "name": "bluefox",
@@ -21,41 +21,47 @@
21
21
  "url": "https://github.com/ioBroker/ioBroker.socket-classes"
22
22
  },
23
23
  "dependencies": {
24
- "@iobroker/adapter-core": "^3.2.2",
25
- "axios": "^1.7.8",
24
+ "@iobroker/adapter-core": "^3.2.3",
25
+ "axios": "^1.7.9",
26
26
  "cookie-parser": "^1.4.7",
27
27
  "passport": "^0.7.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@alcalzone/release-script": "^3.8.0",
31
31
  "@alcalzone/release-script-plugin-license": "^3.7.0",
32
- "@foxriver76/eslint-config": "^1.0.5",
32
+ "@iobroker/eslint-config": "^1.0.0",
33
+ "@iobroker/js-controller-common-db": "^7.0.6",
34
+ "@iobroker/ws-server": "^4.2.3",
35
+ "@iobroker/types": "^7.0.6",
36
+ "@types/axios": "^0.14.4",
37
+ "@types/cookie-parser": "^1.4.8",
38
+ "@types/node": "^22.13.1",
39
+ "@types/passport": "^1.0.17",
33
40
  "chai": "^4.5.0",
34
- "gulp": "^4.0.2",
35
- "iobroker.js-controller": "^6.0.11",
36
- "mocha": "^10.8.2"
41
+ "iobroker.js-controller": "^7.0.6",
42
+ "mocha": "^11.1.0",
43
+ "typescript": "^5.7.3"
37
44
  },
38
45
  "bugs": {
39
46
  "url": "https://github.com/ioBroker/ioBroker.web/issues"
40
47
  },
41
- "main": "index.js",
48
+ "main": "dist/index.js",
42
49
  "files": [
43
50
  "LICENSE",
44
- "lib/",
45
- "index.js",
46
- "lib/socketCommon.js",
47
- "lib/socketAdmin.js"
51
+ "dist/"
48
52
  ],
49
53
  "scripts": {
50
- "lint": "eslint .",
51
- "test": "node node_modules/mocha/bin/mocha --exit",
54
+ "lint": "eslint -c eslint.config.mjs",
55
+ "test": "mocha --exit",
56
+ "build": "npm run build-backend && npm run docs",
57
+ "build-backend": "tsc -p tsconfig.build.json && node tasks --prebuild",
52
58
  "release": "release-script",
53
59
  "release-patch": "release-script patch --no-update-lockfile --yes",
54
60
  "release-minor": "release-script minor --no-update-lockfile --yes",
55
61
  "release-major": "release-script major --no-update-lockfile --yes",
56
- "update-packages": "ncu --upgrade",
57
- "npm": "npm install --ignore-script",
58
- "docs": "gulp"
62
+ "update-packages": "npx -y npm-check-updates --upgrade",
63
+ "docs": "node tasks",
64
+ "npm": "npm i -f"
59
65
  },
60
66
  "license": "MIT",
61
67
  "maintainers": [
package/index.js DELETED
@@ -1,19 +0,0 @@
1
- const SocketAdmin = require('./lib/socketAdmin');
2
- const SocketCommon = require('./lib/socketCommon');
3
- const SocketCommands = require('./lib/socketCommands');
4
- const SocketCommandsAdmin = require('./lib/socketCommandsAdmin');
5
- const passportSocket = require('./lib/passportSocket');
6
-
7
- module.exports = {
8
- SocketAdmin,
9
- SocketCommon,
10
- SocketCommands,
11
- SocketCommandsAdmin,
12
- passportSocket,
13
- get passport() {
14
- return require('passport');
15
- },
16
- get cookieParser() {
17
- return require('cookie-parser');
18
- }
19
- };
@@ -1,105 +0,0 @@
1
- // Originally taken from here: https://github.com/jfromaniello/passport.socketio/blob/master/lib/index.js
2
- // Copyright Licensed under the MIT-License. 2012-2013 José F. Romaniello.
3
-
4
- function parseCookie(auth, cookieHeader) {
5
- const cookieParser = auth.cookieParser(auth.secret);
6
- const req = {
7
- headers: {
8
- cookie: cookieHeader
9
- }
10
- };
11
-
12
- let result;
13
-
14
- cookieParser(req, {}, err => {
15
- if (err) {
16
- throw err;
17
- }
18
- result = req.signedCookies || req.cookies;
19
- });
20
-
21
- return result;
22
- }
23
-
24
- function getQuery(url) {
25
- const query = url.split('?')[1] || '';
26
- const parts = query.split('&');
27
- const result = {};
28
- for (let p = 0; p < parts.length; p++) {
29
- const parts1 = parts[p].split('=');
30
- result[parts1[0]] = parts1[1];
31
- }
32
- return result;
33
- }
34
-
35
- function authorize(options) {
36
- const defaults = {
37
- key: 'connect.sid',
38
- secret: null,
39
- store: null,
40
- userProperty: 'user'
41
- };
42
-
43
- const auth = Object.assign({}, defaults, options);
44
-
45
- if (!auth.passport) {
46
- throw new Error("passport is required to use require('passport'), please install passport");
47
- }
48
-
49
- if (!auth.cookieParser) {
50
- throw new Error(
51
- "cookieParser is required use require('cookie-parser'), connect.cookieParser or express.cookieParser"
52
- );
53
- }
54
-
55
- return function (data, accept) {
56
- data.query = getQuery(data.url);
57
-
58
- if (options.checkUser && data.query.user && data.query.pass) {
59
- return options.checkUser(data.query.user, data.query.pass, (error, result) => {
60
- if (error) {
61
- return auth.fail(data, 'Cannot check user', false, accept);
62
- } else if (!result) {
63
- return auth.fail(data, 'User not found', false, accept);
64
- } else {
65
- data[auth.userProperty] = result;
66
- data[auth.userProperty].logged_in = true;
67
- auth.success(data, accept);
68
- }
69
- });
70
- }
71
-
72
- data.cookie = parseCookie(auth, data.headers.cookie || '');
73
- data.sessionID = data.cookie[auth.key] || '';
74
- data[auth.userProperty] = {
75
- logged_in: false
76
- };
77
-
78
- auth.store.get(data.sessionID, (err, session) => {
79
- if (err) {
80
- return auth.fail(data, 'Error in session store:\n' + err.message, true, accept);
81
- } else if (!session) {
82
- return auth.fail(data, 'No session found', false, accept);
83
- } else if (!session[auth.passport._key]) {
84
- return auth.fail(data, 'Passport was not initialized', true, accept);
85
- }
86
-
87
- const userKey = session[auth.passport._key].user;
88
-
89
- if (typeof userKey === 'undefined') {
90
- return auth.fail(
91
- data,
92
- 'User not authorized through passport. (User Property not found)',
93
- false,
94
- accept
95
- );
96
- }
97
-
98
- data[auth.userProperty] = userKey;
99
- data[auth.userProperty].logged_in = true;
100
- auth.success(data, accept);
101
- });
102
- };
103
- }
104
-
105
- exports.authorize = authorize;
@@ -1,203 +0,0 @@
1
- /**
2
- * Class Socket
3
- *
4
- * Copyright 2014-2022 bluefox <dogafox@gmail.com>,
5
- * MIT License
6
- *
7
- */
8
- const SocketCommon = require('./socketCommon');
9
- const SocketCommandsAdmin = require('./socketCommandsAdmin');
10
- let passport; // require('passport') - only if auth is activated
11
- let cookieParser; // require('cookie-parser') - only if auth is activated
12
- let passportSocketIo; // require('./passportSocket') - only if auth is activated
13
-
14
- class SocketAdmin extends SocketCommon {
15
- constructor(settings, adapter, objects) {
16
- super(settings, adapter);
17
-
18
- // user another set of commands for admin
19
- this.commands = new SocketCommandsAdmin(adapter, socket => this.__updateSession(socket), objects);
20
- }
21
-
22
- __getIsNoDisconnect() {
23
- // ws does not require disconnect
24
- return true;
25
- }
26
-
27
- _onAuthorizeSuccess = (data, accept) => {
28
- this.adapter.log.debug(`successful connection to socket.io from ${data.connection.remoteAddress}`);
29
- accept();
30
- };
31
-
32
- _onAuthorizeFail = (data, message, error, accept) => {
33
- setTimeout(() => data.socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE), 100);
34
-
35
- error &&
36
- this.adapter.log.info(`failed connection to socket.io from ${data.connection.remoteAddress}: ${message}`);
37
-
38
- if (error) {
39
- accept(new Error(message));
40
- } else {
41
- accept(new Error(`failed connection to socket.io: ${message}`)); //null, false);
42
- }
43
- // this error will be sent to the user as a special error-package
44
- // see: http://socket.io/docs/client-api/#socket > error-object
45
- };
46
-
47
- __initAuthentication(authOptions) {
48
- passportSocketIo = passportSocketIo || require('./passportSocket');
49
- passport = passport || require('passport');
50
- cookieParser = cookieParser || require('cookie-parser');
51
-
52
- this.store = authOptions.store;
53
-
54
- this.server.use(
55
- passportSocketIo.authorize({
56
- passport,
57
- cookieParser,
58
- key: authOptions.userKey, // the name of the cookie where express/connect stores its session_id
59
- secret: authOptions.secret, // the session_secret to parse the cookie
60
- store: authOptions.store, // we NEED to use a sessionstore. no memorystore please
61
- success: this._onAuthorizeSuccess, // *optional* callback on success - read more below
62
- fail: this._onAuthorizeFail // *optional* callback on fail/error - read more below
63
- })
64
- );
65
- }
66
-
67
- // Extract username from socket
68
- __getUserFromSocket(socket, callback) {
69
- if (socket.conn.request.sessionID) {
70
- socket._secure = true;
71
- socket._sessionID = socket.conn.request.sessionID;
72
- // Get user for session
73
- return this.adapter.getSession(socket.conn.request.sessionID, obj => {
74
- if (!obj || !obj.passport) {
75
- if (socket._acl) {
76
- socket._acl.user = '';
77
- }
78
- socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE);
79
- callback('Cannot detect user');
80
- } else {
81
- callback(null, obj.passport.user ? `system.user.${obj.passport.user}` : '');
82
- }
83
- });
84
- }
85
- callback('Cannot detect user');
86
- }
87
-
88
- __getClientAddress(socket) {
89
- let address;
90
- if (socket.connection) {
91
- address = socket.connection && socket.connection.remoteAddress;
92
- } else {
93
- address = socket.ws._socket.remoteAddress;
94
- }
95
-
96
- if (!address && socket.handshake) {
97
- address = socket.handshake.address;
98
- }
99
- if (!address && socket.conn.request && socket.conn.request.connection) {
100
- address = socket.conn.request.connection.remoteAddress;
101
- }
102
- return address;
103
- }
104
-
105
- // update session ID, but not ofter than 60 seconds
106
- __updateSession(socket) {
107
- if (socket._sessionID) {
108
- const time = Date.now();
109
- if (socket._lastActivity && time - socket._lastActivity > this.settings.ttl * 1000) {
110
- this.adapter.log.warn('REAUTHENTICATE!');
111
- socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE);
112
- return false;
113
- }
114
- socket._lastActivity = time;
115
- socket._sessionTimer =
116
- socket._sessionTimer ||
117
- setTimeout(() => {
118
- socket._sessionTimer = null;
119
- this.adapter.getSession(socket._sessionID, obj => {
120
- if (obj) {
121
- this.adapter.setSession(socket._sessionID, this.settings.ttl, obj);
122
- } else {
123
- this.adapter.log.warn('REAUTHENTICATE!');
124
- socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE);
125
- }
126
- });
127
- }, 60_000);
128
- }
129
-
130
- return true;
131
- }
132
-
133
- __getSessionID(socket) {
134
- return this.settings.auth && socket.conn.request.sessionID;
135
- }
136
-
137
- start(server, socketClass, authOptions, socketOptions) {
138
- super.start(server, socketClass, authOptions, socketOptions);
139
-
140
- this.commands.start(thresholdEnabled => this.onThresholdChanged(thresholdEnabled));
141
- }
142
-
143
- onThresholdChanged(enabled) {
144
- this.server && this.server.sockets && this.server.sockets.emit('eventsThreshold', enabled);
145
- }
146
-
147
- stateChange(id, state) {
148
- this.commands.stateChange(id, state);
149
-
150
- const sockets = this.getSocketsList();
151
- if (sockets) {
152
- sockets.forEach(socket => this.commands.publish(socket, 'stateChange', id, state));
153
- }
154
- }
155
-
156
- fileChange(id, fileName, size) {
157
- const sockets = this.getSocketsList();
158
- if (sockets) {
159
- sockets.forEach(socket => this.commands.publishFile(socket, id, fileName, size));
160
- }
161
- }
162
-
163
- repoUpdated() {
164
- const sockets = this.getSocketsList();
165
- if (sockets) {
166
- sockets.forEach(socket => this.__updateSession(socket));
167
-
168
- this.server.sockets.emit('repoUpdated');
169
- }
170
- }
171
-
172
- objectChange(id, obj) {
173
- const sockets = this.getSocketsList();
174
- if (sockets) {
175
- sockets.forEach(socket => this.commands.publish(socket, 'objectChange', id, obj));
176
- }
177
- }
178
-
179
- subscribe(type, pattern) {
180
- this.commands.subscribe(null, type, pattern);
181
- }
182
-
183
- subscribeFile(id, pattern) {
184
- this.commands.subscribe(null, 'fileChange', id, pattern);
185
- }
186
-
187
- sendCommand(obj) {
188
- if (this.commands.sendCommand(obj)) {
189
- const sockets = this.getSocketsList();
190
- if (sockets) {
191
- sockets.forEach(socket => this.__updateSession(socket));
192
-
193
- this.server.sockets.emit(obj.command, obj.message.id, obj.message.data);
194
- }
195
- }
196
- }
197
-
198
- updateRatings(uuid, isAutoUpdate) {
199
- return this.commands.updateRatings(uuid, isAutoUpdate);
200
- }
201
- }
202
-
203
- module.exports = SocketAdmin;