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

panates / postgrejs / 19930003674

04 Dec 2025 01:06PM UTC coverage: 91.013%. First build
19930003674

Pull #55

github

erayhanoglu
2.22.9
Pull Request #55: Dev

907 of 1140 branches covered (79.56%)

Branch coverage included in aggregate %.

31 of 32 new or added lines in 3 files covered. (96.88%)

5615 of 6026 relevant lines covered (93.18%)

208.78 hits per line

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

94.47
/src/protocol/smart-buffer.ts
1
import * as os from 'os';
1✔
2
import { writeBigUInt64BE } from '../util/bigint-methods.js';
1✔
3
import { BufferReader } from './buffer-reader.js';
1✔
4

1✔
5
export interface SmartBufferConfig {
1✔
6
  pageSize?: number;
1✔
7
  maxLength?: number;
1✔
8
  houseKeepInterval?: number;
1✔
9
}
1✔
10

1✔
11
export class SmartBuffer extends BufferReader {
1✔
12
  static DEFAULT_PAGE_SIZE = 4096;
1✔
13
  static DEFAULT_MAX_SIZE = Math.min(
1✔
14
    Math.floor(os.totalmem() / 2),
1✔
15
    1024 * 1024 * 1024 * 2, // 2 GB
1✔
16
  );
1✔
17

1✔
18
  private readonly _houseKeepInterval: number;
1✔
19
  private _houseKeepTimer?: NodeJS.Timeout;
1✔
20
  private _lastHouseKeep = 0;
1✔
21
  private _stMaxPages = 1;
1✔
22
  private _length = 0;
1✔
23
  readonly pageSize: number;
1✔
24
  readonly maxSize: number;
1✔
25

1✔
26
  constructor(cfg?: SmartBufferConfig) {
1✔
27
    super(Buffer.allocUnsafe(cfg?.pageSize || SmartBuffer.DEFAULT_PAGE_SIZE));
64✔
28
    this._houseKeepInterval = cfg?.houseKeepInterval || 5000;
64✔
29
    this.pageSize = this.buffer.length;
64✔
30
    this.maxSize = cfg?.maxLength || SmartBuffer.DEFAULT_MAX_SIZE;
64✔
31
    this._length = 0;
64✔
32
  }
64✔
33

1✔
34
  get capacity(): number {
1✔
35
    return this.buffer.length;
8,938✔
36
  }
8,938✔
37

1✔
38
  get length(): number {
1✔
39
    return this._length;
25,396✔
40
  }
25,396✔
41

1✔
42
  start(): this {
1✔
43
    this.offset = 0;
1,622✔
44
    this._length = 0;
1,622✔
45
    if (this._houseKeepTimer) {
1,622✔
46
      clearTimeout(this._houseKeepTimer);
1,560✔
47
      this._houseKeepTimer = undefined;
1,560✔
48
    }
1,560✔
49
    return this;
1,622✔
50
  }
1,622✔
51

1✔
52
  flush(): Buffer {
1✔
53
    if (this._houseKeepTimer) clearTimeout(this._houseKeepTimer);
1,623!
54

1,623✔
55
    const length = this.length;
1,623✔
56
    this._length = 0;
1,623✔
57
    const out = this.buffer.slice(0, length);
1,623✔
58

1,623✔
59
    const pages = length ? Math.ceil(length / this.pageSize) : 1;
1,623!
60
    this._stMaxPages = Math.max(this._stMaxPages, pages);
1,623✔
61
    if (this._lastHouseKeep < Date.now() + this._houseKeepInterval)
1,623✔
62
      this._houseKeep();
1,623✔
63

1,623✔
64
    this._houseKeepTimer = setTimeout(() => {
1,623✔
65
      this._houseKeepTimer = undefined;
1✔
66
      this._houseKeep();
1✔
67
    }, this._houseKeepInterval).unref();
1,623✔
68

1,623✔
69
    return out;
1,623✔
70
  }
1,623✔
71

1✔
72
  growSize(len: number): this {
1✔
73
    const endOffset = this.offset + len;
8,938✔
74
    if (this.capacity < endOffset) {
8,938✔
75
      if (endOffset > this.maxSize) throw new Error('Buffer limit exceeded.');
2!
76
      const newSize = Math.ceil(endOffset / this.pageSize) * this.pageSize;
2✔
77
      const newBuffer = Buffer.allocUnsafe(newSize);
2✔
78
      this.buffer.copy(newBuffer);
2✔
79
      this.buffer = newBuffer;
2✔
80
    }
2✔
81
    this._length = Math.max(this.length, endOffset);
8,938✔
82
    return this;
8,938✔
83
  }
8,938✔
84

1✔
85
  fill(value = 0, len = 1): this {
1✔
NEW
86
    this.ensureSize(len);
×
87
    this.buffer.fill(value, this.offset, this.offset + len);
×
88
    this.offset += len;
×
89
    return this;
×
90
  }
×
91

1✔
92
  writeCString(str: string, encoding?: BufferEncoding): this {
1✔
93
    const len = str ? Buffer.byteLength(str, encoding) : 0;
2,452✔
94
    this.ensureSize(len + 1);
2,452✔
95
    if (str) {
2,452✔
96
      this.buffer.write(str, this.offset, encoding);
2,391✔
97
      this.offset += len;
2,391✔
98
    }
2,391✔
99
    this.writeUInt8(0);
2,452✔
100
    return this;
2,452✔
101
  }
2,452✔
102

1✔
103
  writeLString(str?: string, encoding?: BufferEncoding): this {
1✔
104
    const len = str ? Buffer.byteLength(str, encoding) : 0;
10!
105
    this.ensureSize(len + 4);
10✔
106
    this.writeInt32BE(str == null ? -1 : len);
10!
107
    if (str) {
10✔
108
      if (encoding)
10✔
109
        this.offset += this.buffer.write(str, this.offset, encoding);
10✔
110
      else this.offset += this.buffer.write(str, this.offset);
1✔
111
    }
10✔
112
    return this;
10✔
113
  }
10✔
114

1✔
115
  writeString(str: string, encoding?: BufferEncoding): this {
1✔
116
    if (str) {
57✔
117
      const len = Buffer.byteLength(str, encoding);
57✔
118
      this.ensureSize(len);
57✔
119
      this.offset += this.buffer.write(str, this.offset, encoding);
57✔
120
    }
57✔
121
    return this;
57✔
122
  }
57✔
123

1✔
124
  writeInt8(n: number): this {
1✔
125
    this.ensureSize(1);
1,508✔
126
    this.buffer.writeInt8(n, this.offset);
1,508✔
127
    this.offset++;
1,508✔
128
    return this;
1,508✔
129
  }
1,508✔
130

1✔
131
  writeUInt8(n: number): this {
1✔
132
    this.ensureSize(1);
3,199✔
133
    this.buffer.writeUInt8(n, this.offset);
3,199✔
134
    this.offset++;
3,199✔
135
    return this;
3,199✔
136
  }
3,199✔
137

1✔
138
  writeUInt16BE(n: number): this {
1✔
139
    this.ensureSize(2);
1,145✔
140
    this.buffer.writeUInt16BE(n, this.offset);
1,145✔
141
    this.offset += 2;
1,145✔
142
    return this;
1,145✔
143
  }
1,145✔
144

1✔
145
  writeUInt32BE(n: number): this {
1✔
146
    this.ensureSize(4);
454✔
147
    this.buffer.writeUInt32BE(n, this.offset);
454✔
148
    this.offset += 4;
454✔
149
    return this;
454✔
150
  }
454✔
151

1✔
152
  writeInt16BE(n: number): this {
1✔
153
    this.ensureSize(2);
323✔
154
    this.buffer.writeInt16BE(n, this.offset);
323✔
155
    this.offset += 2;
323✔
156
    return this;
323✔
157
  }
323✔
158

1✔
159
  writeInt32BE(n: number): this {
1✔
160
    this.ensureSize(4);
2,123✔
161
    this.buffer.writeInt32BE(n, this.offset);
2,123✔
162
    this.offset += 4;
2,123✔
163
    return this;
2,123✔
164
  }
2,123✔
165

1✔
166
  writeBigInt64BE(n: bigint | number): this {
1✔
167
    n = typeof n === 'bigint' ? n : BigInt(n);
10!
168
    this.ensureSize(8);
10✔
169
    if (typeof this.buffer.writeBigInt64BE === 'function')
10✔
170
      this.buffer.writeBigInt64BE(n, this.offset);
10!
171
    else writeBigUInt64BE(this.buffer, n, this.offset);
×
172
    this.offset += 8;
10✔
173
    return this;
10✔
174
  }
10✔
175

1✔
176
  writeFloatBE(n: number): this {
1✔
177
    this.ensureSize(4);
10✔
178
    this.buffer.writeFloatBE(n, this.offset);
10✔
179
    this.offset += 4;
10✔
180
    return this;
10✔
181
  }
10✔
182

1✔
183
  writeDoubleBE(n: number): this {
1✔
184
    this.ensureSize(8);
98✔
185
    this.buffer.writeDoubleBE(n, this.offset);
98✔
186
    this.offset += 8;
98✔
187
    return this;
98✔
188
  }
98✔
189

1✔
190
  writeBuffer(buffer: Buffer): this {
1✔
191
    this.ensureSize(buffer.length);
11✔
192
    buffer.copy(this.buffer, this.offset, 0, buffer.length);
11✔
193
    this.offset += buffer.length;
11✔
194
    return this;
11✔
195
  }
11✔
196

1✔
197
  private ensureSize(len: number): this {
1✔
198
    const n = this.offset + len - this.length;
11,400✔
199
    if (n > 0) this.growSize(n);
11,400✔
200
    return this;
11,400✔
201
  }
11,400✔
202

1✔
203
  private _houseKeep(): void {
1✔
204
    const needSize = this._stMaxPages * this.pageSize;
1,624✔
205
    if (this.buffer.length > needSize)
1,624✔
206
      this.buffer = Buffer.allocUnsafe(needSize);
1,624✔
207
    this._stMaxPages = this.length ? Math.ceil(this.length / this.pageSize) : 1;
1,624!
208
  }
1,624✔
209
}
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