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

u-wave / core / 12197565170

06 Dec 2024 10:51AM UTC coverage: 84.38% (+0.02%) from 84.36%
12197565170

Pull #682

github

goto-bus-stop
Continue to support existing uwsession tokens
Pull Request #682: Improve session handling

911 of 1089 branches covered (83.65%)

Branch coverage included in aggregate %.

81 of 116 new or added lines in 10 files covered. (69.83%)

100 existing lines in 4 files now uncovered.

9882 of 11702 relevant lines covered (84.45%)

92.78 hits per line

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

5.26
/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({ ns: 'uwave:sockets', connectionType: 'LostConnection', userID: this.user.id, sessionID });
×
18

×
19
    this.initQueued();
×
20
    this.setTimeout(timeout);
×
21
  }
×
22

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

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

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

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

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

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

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

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

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

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