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

project-slippi / slippi-js / 16128942796

07 Jul 2025 10:09PM UTC coverage: 83.615% (-0.006%) from 83.621%
16128942796

Pull #145

github

web-flow
Merge 58965f7f8 into 7653c831c
Pull Request #145: fix breaking bugs

668 of 844 branches covered (79.15%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 2 files covered. (25.0%)

1853 of 2171 relevant lines covered (85.35%)

249086.85 hits per line

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

25.81
/src/console/communication.ts
1
import { decode, encode } from "@shelacek/ubjson";
24✔
2

3
export enum CommunicationType {
24✔
4
  HANDSHAKE = 1,
24✔
5
  REPLAY = 2,
24✔
6
  KEEP_ALIVE = 3,
24✔
7
}
8

9
export type CommunicationMessage = {
10
  type: CommunicationType;
11
  payload: {
12
    cursor: Uint8Array;
13
    clientToken: Uint8Array;
14
    pos: Uint8Array;
15
    nextPos: Uint8Array;
16
    data: Uint8Array;
17
    nick: string | null;
18
    forcePos: boolean;
19
    nintendontVersion: string | null;
20
  };
21
};
22

23
// This class is responsible for handling the communication protocol between the Wii and the
24
// desktop app
25
export class ConsoleCommunication {
24✔
26
  private receiveBuf = Buffer.from([]);
×
27
  private messages = new Array<CommunicationMessage>();
×
28

29
  public receive(data: Buffer): void {
30
    this.receiveBuf = Buffer.concat([this.receiveBuf, data]);
×
31

32
    while (this.receiveBuf.length >= 4) {
×
33
      // First get the size of the message we are expecting
34
      const msgSize = this.receiveBuf.readUInt32BE(0);
×
35

36
      if (this.receiveBuf.length < msgSize + 4) {
×
37
        // If we haven't received all the data yet, let's wait for more
38
        return;
×
39
      }
40

41
      // Here we have received all the data, so let's decode it
NEW
42
      const ubjsonArrayBuffer = this.receiveBuf.buffer.slice(
×
43
        this.receiveBuf.byteOffset + 4,
44
        this.receiveBuf.byteOffset + msgSize + 4,
45
      );
NEW
46
      this.messages.push(decode(ubjsonArrayBuffer));
×
47

48
      // Remove the processed data from receiveBuf
NEW
49
      this.receiveBuf = this.receiveBuf.subarray(msgSize + 4);
×
50
    }
51
  }
52

53
  public getReceiveBuffer(): Buffer {
54
    return this.receiveBuf;
×
55
  }
56

57
  public getMessages(): Array<CommunicationMessage> {
58
    const toReturn = this.messages;
×
59
    this.messages = [];
×
60

61
    return toReturn;
×
62
  }
63

64
  public genHandshakeOut(cursor: Uint8Array, clientToken: number, isRealtime = false): Buffer {
×
65
    const clientTokenBuf = Buffer.from([0, 0, 0, 0]);
×
66
    clientTokenBuf.writeUInt32BE(clientToken, 0);
×
67

68
    const message = {
×
69
      type: CommunicationType.HANDSHAKE,
70
      payload: {
71
        cursor: cursor,
72
        clientToken: Uint8Array.from(clientTokenBuf), // TODO: Use real instance token
73
        isRealtime: isRealtime,
74
      },
75
    };
76

77
    const buf = encode(message, {
×
78
      optimizeArrays: true,
79
    });
80

81
    const msg = Buffer.concat([Buffer.from([0, 0, 0, 0]), Buffer.from(buf)]);
×
82

83
    msg.writeUInt32BE(buf.byteLength, 0);
×
84

85
    return msg;
×
86
  }
87
}
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

© 2025 Coveralls, Inc