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

panates / postgresql-client / 50d5a8f1-a1ba-4c39-947d-fb4f2c7685e1

16 Apr 2024 07:12AM UTC coverage: 85.802% (+0.3%) from 85.476%
50d5a8f1-a1ba-4c39-947d-fb4f2c7685e1

push

circleci

erayhanoglu
Added prettier formatting

696 of 980 branches covered (71.02%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

52 existing lines in 10 files now uncovered.

2513 of 2760 relevant lines covered (91.05%)

799.06 hits per line

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

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

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

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

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

26
  constructor(cfg?: SmartBufferConfig) {
27
    // @ts-ignore
28
    super(Buffer.allocUnsafe((cfg?.pageSize ? parseInt(cfg.pageSize, 10) : 0) || SmartBuffer.DEFAULT_PAGE_SIZE));
87✔
29
    this._houseKeepInterval = cfg?.houseKeepInterval || 5000;
87✔
30
    this.pageSize = this.buffer.length;
87✔
31
    this.maxSize = cfg?.maxLength || SmartBuffer.DEFAULT_MAX_SIZE;
87✔
32
    this._length = 0;
87✔
33
  }
34

35
  get capacity(): number {
36
    return this.buffer.length;
30,717✔
37
  }
38

39
  get length(): number {
40
    return this._length;
44,889✔
41
  }
42

43
  start(): this {
44
    this.offset = 0;
4,470✔
45
    this._length = 0;
4,470✔
46
    if (this._houseKeepTimer) {
4,470✔
47
      clearTimeout(this._houseKeepTimer);
4,389✔
48
      this._houseKeepTimer = undefined;
4,389✔
49
    }
50
    return this;
4,470✔
51
  }
52

53
  flush(): Buffer {
54
    if (this._houseKeepTimer) clearTimeout(this._houseKeepTimer);
4,473!
55

56
    const length = this.length;
4,473✔
57
    this._length = 0;
4,473✔
58
    const out = this.buffer.slice(0, length);
4,473✔
59

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

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

69
    return out;
4,473✔
70
  }
71

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

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

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

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

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

123
  writeInt8(n: number): this {
124
    this.growSize(1);
4,419✔
125
    this.buffer.writeInt8(n, this.offset);
4,419✔
126
    this.offset++;
4,419✔
127
    return this;
4,419✔
128
  }
129

130
  writeUInt8(n: number): this {
131
    this.growSize(1);
8,358✔
132
    this.buffer.writeUInt8(n, this.offset);
8,358✔
133
    this.offset++;
8,358✔
134
    return this;
8,358✔
135
  }
136

137
  writeUInt16BE(n: number): this {
138
    this.growSize(2);
3,033✔
139
    this.buffer.writeUInt16BE(n, this.offset);
3,033✔
140
    this.offset += 2;
3,033✔
141
    return this;
3,033✔
142
  }
143

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

151
  writeInt16BE(n: number): this {
152
    this.growSize(2);
759✔
153
    this.buffer.writeInt16BE(n, this.offset);
759✔
154
    this.offset += 2;
759✔
155
    return this;
759✔
156
  }
157

158
  writeInt32BE(n: number): this {
159
    this.growSize(4);
6,156✔
160
    this.buffer.writeInt32BE(n, this.offset);
6,156✔
161
    this.offset += 4;
6,156✔
162
    return this;
6,156✔
163
  }
164

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

174
  writeFloatBE(n: number): this {
175
    this.growSize(4);
30✔
176
    this.buffer.writeFloatBE(n, this.offset);
30✔
177
    this.offset += 4;
30✔
178
    return this;
30✔
179
  }
180

181
  writeDoubleBE(n: number): this {
182
    this.growSize(8);
294✔
183
    this.buffer.writeDoubleBE(n, this.offset);
294✔
184
    this.offset += 8;
294✔
185
    return this;
294✔
186
  }
187

188
  writeBuffer(buffer: Buffer): this {
189
    this.growSize(buffer.length);
33✔
190
    buffer.copy(this.buffer, this.offset, 0, buffer.length);
33✔
191
    this.offset += buffer.length;
33✔
192
    return this;
33✔
193
  }
194

195
  private _houseKeep(): void {
196
    const needSize = this._stMaxPages * this.pageSize;
4,476✔
197
    if (this.buffer.length > needSize) this.buffer = Buffer.allocUnsafe(needSize);
4,476✔
198
    this._stMaxPages = this.length ? Math.ceil(this.length / this.pageSize) : 1;
4,476!
199
  }
200
}
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