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

u-wave / core / 12200196837

06 Dec 2024 02:06PM UTC coverage: 84.237% (-0.1%) from 84.36%
12200196837

Pull #682

github

goto-bus-stop
Do not store JWT auth in session
Pull Request #682: Improve session handling

905 of 1083 branches covered (83.56%)

Branch coverage included in aggregate %.

76 of 123 new or added lines in 10 files covered. (61.79%)

6 existing lines in 1 file now uncovered.

9863 of 11700 relevant lines covered (84.3%)

90.4 hits per line

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

5.15
/src/sockets/LostConnection.js
1
import EventEmitter from 'node:events';
1✔
2

1✔
3
class LostConnection extends EventEmitter {
×
4
  #logger;
×
5

×
6
  /**
×
7
   * @param {import('../Uwave.js').default} uw
×
8
   * @param {import('../schema.js').User} user
×
NEW
9
   * @param {string} sessionID
×
10
   */
×
NEW
11
  constructor(uw, user, sessionID, timeout = 30) {
×
12
    super();
×
13
    this.uw = uw;
×
14
    this.user = user;
×
NEW
15
    this.sessionID = sessionID;
×
16
    this.timeout = timeout;
×
NEW
17
    this.#logger = uw.logger.child({
×
NEW
18
      ns: 'uwave:sockets', connectionType: 'LostConnection', userID: this.user.id, sessionID,
×
NEW
19
    });
×
20

×
21
    this.initQueued();
×
22
    this.setTimeout(timeout);
×
23
  }
×
24

×
25
  /**
×
26
   * @private
×
27
   */
×
28
  get key() {
×
NEW
29
    return `http-api:disconnected:${this.sessionID}`;
×
30
  }
×
31

×
32
  /**
×
33
   * @private
×
34
   */
×
35
  get messagesKey() {
×
NEW
36
    return `http-api:disconnected:${this.sessionID}:messages`;
×
37
  }
×
38

×
39
  /**
×
40
   * @private
×
41
   */
×
42
  initQueued() {
×
43
    // We expire the keys after timeout*10, because a server restart near the
×
44
    // end of the timeout might mean that someone fails to reconnect. This way
×
45
    // we can ensure that everyone still gets the full `timeout` duration to
×
46
    // reconnect after a server restart, while also not filling up Redis with
×
47
    // messages to users who left and will never return.
×
48
    this.uw.redis.multi()
×
49
      .set(this.key, 'true', 'EX', this.timeout * 10)
×
50
      .ltrim(this.messagesKey, 0, 0)
×
51
      .expire(this.messagesKey, this.timeout * 10)
×
52
      .exec();
×
53
  }
×
54

×
55
  /**
×
56
   * @param {number} timeout
×
57
   * @private
×
58
   */
×
59
  setTimeout(timeout) {
×
60
    this.removeTimer = setTimeout(() => {
×
61
      this.close();
×
62
      this.uw.redis.del(this.key, this.messagesKey);
×
63
    }, timeout * 1000);
×
64
  }
×
65

×
66
  /**
×
67
   * @param {string} command
×
68
   * @param {import('type-fest').JsonValue} data
×
69
   */
×
70
  send(command, data) {
×
71
    this.#logger.info({ command, data }, 'queue command');
×
72

×
73
    this.uw.redis.rpush(
×
74
      this.messagesKey,
×
75
      JSON.stringify({ command, data }),
×
76
    );
×
77
  }
×
78

×
79
  close() {
×
80
    this.#logger.info('close');
×
81
    this.emit('close');
×
82
  }
×
83

×
84
  removed() {
×
85
    if (this.removeTimer) {
×
86
      clearTimeout(this.removeTimer);
×
87
    }
×
88
  }
×
89

×
90
  toString() {
×
91
    return `Lost { user: ${this.user.id} ${this.user.username} }`;
×
92
  }
×
93
}
×
94

1✔
95
export default LostConnection;
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