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

u-wave / core / 11980840475

22 Nov 2024 10:04PM UTC coverage: 78.492% (-1.7%) from 80.158%
11980840475

Pull #637

github

goto-bus-stop
ci: add node 22
Pull Request #637: Switch to a relational database

757 of 912 branches covered (83.0%)

Branch coverage included in aggregate %.

2001 of 2791 new or added lines in 52 files covered. (71.69%)

9 existing lines in 7 files now uncovered.

8666 of 11093 relevant lines covered (78.12%)

70.72 hits per line

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

5.38
/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
×
NEW
8
   * @param {import('../schema.js').User} user
×
9
   */
×
10
  constructor(uw, user, timeout = 30) {
×
11
    super();
×
12
    this.uw = uw;
×
13
    this.user = user;
×
14
    this.timeout = timeout;
×
15
    this.#logger = uw.logger.child({ ns: 'uwave:sockets', connectionType: 'LostConnection', userId: this.user.id });
×
16

×
17
    this.initQueued();
×
18
    this.setTimeout(timeout);
×
19
  }
×
20

×
21
  /**
×
22
   * @private
×
23
   */
×
24
  get key() {
×
25
    return `http-api:disconnected:${this.user.id}`;
×
26
  }
×
27

×
28
  /**
×
29
   * @private
×
30
   */
×
31
  get messagesKey() {
×
32
    return `http-api:disconnected:${this.user.id}:messages`;
×
33
  }
×
34

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

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

×
62
  /**
×
63
   * @param {string} command
×
64
   * @param {import('type-fest').JsonValue} data
×
65
   */
×
66
  send(command, data) {
×
67
    this.#logger.info({ command, data }, 'queue command');
×
68

×
69
    this.uw.redis.rpush(
×
70
      this.messagesKey,
×
71
      JSON.stringify({ command, data }),
×
72
    );
×
73
  }
×
74

×
75
  close() {
×
76
    this.#logger.info('close');
×
77
    this.emit('close');
×
78
  }
×
79

×
80
  removed() {
×
81
    if (this.removeTimer) {
×
82
      clearTimeout(this.removeTimer);
×
83
    }
×
84
  }
×
85

×
86
  toString() {
×
87
    return `Lost { user: ${this.user.id} ${this.user.username} }`;
×
88
  }
×
89
}
×
90

1✔
91
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