• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

u-wave / core / 13782383620

11 Mar 2025 07:13AM UTC coverage: 85.077% (-0.02%) from 85.093%
13782383620

push

github

web-flow
deps: bump secure-json-parse from 3.0.2 to 4.0.0 (#690)

Bumps [secure-json-parse](https://github.com/fastify/secure-json-parse) from 3.0.2 to 4.0.0.
- [Release notes](https://github.com/fastify/secure-json-parse/releases)
- [Commits](https://github.com/fastify/secure-json-parse/compare/v3.0.2...v4.0.0)

---
updated-dependencies:
- dependency-name: secure-json-parse
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

929 of 1106 branches covered (84.0%)

Branch coverage included in aggregate %.

9983 of 11720 relevant lines covered (85.18%)

90.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.1
/src/sockets/GuestConnection.js
1
import EventEmitter from 'node:events';
1✔
2
import Ultron from 'ultron';
1✔
3
import WebSocket from 'ws';
1✔
4

1✔
5
const PING_TIMEOUT = 5_000;
1✔
6
const DEAD_TIMEOUT = 30_000;
1✔
7

1✔
8
class GuestConnection extends EventEmitter {
25✔
9
  #events;
25✔
10

25✔
11
  #logger;
25✔
12

25✔
13
  #lastMessage = Date.now();
25✔
14

25✔
15
  /**
25✔
16
   * @param {import('../Uwave.js').default} uw
25✔
17
   * @param {import('ws').WebSocket} socket
25✔
18
   * @param {{ authRegistry: import('../AuthRegistry.js').default }} options
25✔
19
   */
25✔
20
  constructor(uw, socket, options) {
25✔
21
    super();
25✔
22
    this.uw = uw;
25✔
23
    this.socket = socket;
25✔
24
    this.options = options;
25✔
25
    this.#logger = uw.logger.child({ ns: 'uwave:sockets', connectionType: 'GuestConnection', userId: null });
25✔
26

25✔
27
    this.#events = new Ultron(socket);
25✔
28

25✔
29
    this.#events.on('close', () => {
25✔
30
      this.emit('close');
×
31
    });
25✔
32

25✔
33
    this.#events.on('message', /** @param {string|Buffer} token */ (token) => {
25✔
34
      this.attemptAuth(token.toString()).then(() => {
25✔
35
        this.send('authenticated');
25✔
36
      }).catch((error) => {
25✔
37
        this.send('error', error.message);
×
38
      });
25✔
39
    });
25✔
40

25✔
41
    this.#events.on('pong', () => {
25✔
42
      this.#lastMessage = Date.now();
×
43
    });
25✔
44
  }
25✔
45

25✔
46
  /**
25✔
47
   * @param {string} token
25✔
48
   * @private
25✔
49
   */
25✔
50
  async attemptAuth(token) {
25✔
51
    const { bans, users } = this.uw;
25✔
52
    const { authRegistry } = this.options;
25✔
53

25✔
54
    const { userID, sessionID } = await authRegistry.getTokenUser(token);
25✔
55
    if (!sessionID || typeof sessionID !== 'string') {
25!
56
      throw new Error('Invalid token');
×
57
    }
×
58
    const userModel = await users.getUser(userID);
25✔
59
    if (!userModel) {
25!
60
      throw new Error('Invalid session');
×
61
    }
×
62

25✔
63
    // Users who are banned can still join as guests, but cannot log in. So we
25✔
64
    // ignore their socket login attempts, and just keep their connections
25✔
65
    // around as guest connections.
25✔
66
    if (await bans.isBanned(userModel)) {
25!
67
      throw new Error('You have been banned');
×
68
    }
×
69

25✔
70
    this.emit('authenticate', userModel, sessionID);
25✔
71
  }
25✔
72

25✔
73
  /**
25✔
74
   * @param {string} sessionID
25✔
75
   */
25✔
76
  isReconnect(sessionID) {
25✔
77
    return this.uw.redis.exists(`http-api:disconnected:${sessionID}`);
25✔
78
  }
25✔
79

25✔
80
  /**
25✔
81
   * @param {string} command
25✔
82
   * @param {import('type-fest').JsonValue} [data]
25✔
83
   */
25✔
84
  send(command, data) {
25✔
85
    this.socket.send(JSON.stringify({ command, data }));
25✔
86
  }
25✔
87

25✔
88
  #timeSinceLastMessage() {
25✔
89
    return Date.now() - this.#lastMessage;
×
90
  }
×
91

25✔
92
  ping() {
25✔
93
    if (this.socket.readyState !== WebSocket.OPEN) {
×
94
      return;
×
95
    }
×
96
    if (this.#timeSinceLastMessage() > DEAD_TIMEOUT) {
×
97
      this.socket.terminate();
×
98
      return;
×
99
    }
×
100
    if (this.#timeSinceLastMessage() > PING_TIMEOUT) {
×
101
      this.socket.ping();
×
102
    }
×
103
  }
×
104

25✔
105
  close() {
25✔
106
    this.#logger.info('close');
×
107
    this.socket.close();
×
108
  }
×
109

25✔
110
  removed() {
25✔
111
    this.#events.remove();
25✔
112
  }
25✔
113

25✔
114
  toString() {
25✔
115
    return 'Guest';
×
116
  }
×
117
}
25✔
118

1✔
119
export default GuestConnection;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc