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

nestjs / nest / 0bf7da57-9a2d-480b-be74-74e127c0ebfe

06 Aug 2025 01:10PM UTC coverage: 88.902% (-0.01%) from 88.913%
0bf7da57-9a2d-480b-be74-74e127c0ebfe

Pull #15508

circleci

kim-sung-jee
fix(microservices): report correct buffer length in exception
Pull Request #15508: fix(microservices): report correct buffer length in exception

2732 of 3451 branches covered (79.17%)

0 of 2 new or added lines in 1 file covered. (0.0%)

7282 of 8191 relevant lines covered (88.9%)

16.69 hits per line

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

93.18
/packages/microservices/helpers/json-socket.ts
1
import { Buffer } from 'buffer';
1✔
2
import { StringDecoder } from 'string_decoder';
1✔
3
import { CorruptedPacketLengthException } from '../errors/corrupted-packet-length.exception';
1✔
4
import { MaxPacketLengthExceededException } from '../errors/max-packet-length-exceeded.exception';
1✔
5
import { TcpSocket } from './tcp-socket';
1✔
6

7
const MAX_BUFFER_SIZE = (512 * 1024 * 1024) / 4; // 512 MBs in characters with 4 bytes per character (32-bit)
1✔
8

9
export class JsonSocket extends TcpSocket {
1✔
10
  private contentLength: number | null = null;
17✔
11
  private buffer = '';
17✔
12

13
  private readonly stringDecoder = new StringDecoder();
17✔
14
  private readonly delimiter = '#';
17✔
15

16
  protected handleSend(message: any, callback?: (err?: any) => void) {
17
    this.socket.write(this.formatMessageData(message), 'utf-8', callback);
104✔
18
  }
19

20
  protected handleData(dataRaw: Buffer | string) {
21
    const data = Buffer.isBuffer(dataRaw)
129✔
22
      ? this.stringDecoder.write(dataRaw)
129✔
23
      : dataRaw;
24
    this.buffer += data;
129✔
25

26
    if (this.buffer.length > MAX_BUFFER_SIZE) {
129!
NEW
27
      const bufferLength = this.buffer.length;
×
28
      this.buffer = '';
×
NEW
29
      throw new MaxPacketLengthExceededException(bufferLength);
×
30
    }
31

32
    if (this.contentLength === null) {
129✔
33
      const i = this.buffer.indexOf(this.delimiter);
125✔
34
      /**
35
       * Check if the buffer has the delimiter (#),
36
       * if not, the end of the buffer string might be in the middle of a content length string
37
       */
38
      if (i !== -1) {
125!
39
        const rawContentLength = this.buffer.substring(0, i);
125✔
40
        this.contentLength = parseInt(rawContentLength, 10);
125✔
41

42
        if (isNaN(this.contentLength)) {
125✔
43
          this.contentLength = null;
3✔
44
          this.buffer = '';
3✔
45
          throw new CorruptedPacketLengthException(rawContentLength);
3✔
46
        }
47
        this.buffer = this.buffer.substring(i + 1);
122✔
48
      }
49
    }
50

51
    if (this.contentLength !== null) {
126!
52
      const length = this.buffer.length;
126✔
53
      if (length === this.contentLength) {
126✔
54
        this.handleMessage(this.buffer);
19✔
55
      } else if (length > this.contentLength) {
107✔
56
        const message = this.buffer.substring(0, this.contentLength);
103✔
57
        const rest = this.buffer.substring(this.contentLength);
103✔
58
        this.handleMessage(message);
103✔
59
        this.handleData(rest);
103✔
60
      }
61
    }
62
  }
63

64
  private handleMessage(message: any) {
65
    this.contentLength = null;
122✔
66
    this.buffer = '';
122✔
67
    this.emitMessage(message);
122✔
68
  }
69

70
  private formatMessageData(message: any) {
71
    const messageData = JSON.stringify(message);
104✔
72
    const length = messageData.length;
104✔
73
    const data = length + this.delimiter + messageData;
104✔
74
    return data;
104✔
75
  }
76
}
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