/ node_modules / pg-protocol / dist / buffer-writer.js
buffer-writer.js
 1  "use strict";
 2  //binary data writer tuned for encoding binary specific to the postgres binary protocol
 3  Object.defineProperty(exports, "__esModule", { value: true });
 4  exports.Writer = void 0;
 5  class Writer {
 6      constructor(size = 256) {
 7          this.size = size;
 8          this.offset = 5;
 9          this.headerPosition = 0;
10          this.buffer = Buffer.allocUnsafe(size);
11      }
12      ensure(size) {
13          const remaining = this.buffer.length - this.offset;
14          if (remaining < size) {
15              const oldBuffer = this.buffer;
16              // exponential growth factor of around ~ 1.5
17              // https://stackoverflow.com/questions/2269063/buffer-growth-strategy
18              const newSize = oldBuffer.length + (oldBuffer.length >> 1) + size;
19              this.buffer = Buffer.allocUnsafe(newSize);
20              oldBuffer.copy(this.buffer);
21          }
22      }
23      addInt32(num) {
24          this.ensure(4);
25          this.buffer[this.offset++] = (num >>> 24) & 0xff;
26          this.buffer[this.offset++] = (num >>> 16) & 0xff;
27          this.buffer[this.offset++] = (num >>> 8) & 0xff;
28          this.buffer[this.offset++] = (num >>> 0) & 0xff;
29          return this;
30      }
31      addInt16(num) {
32          this.ensure(2);
33          this.buffer[this.offset++] = (num >>> 8) & 0xff;
34          this.buffer[this.offset++] = (num >>> 0) & 0xff;
35          return this;
36      }
37      addCString(string) {
38          if (!string) {
39              this.ensure(1);
40          }
41          else {
42              const len = Buffer.byteLength(string);
43              this.ensure(len + 1); // +1 for null terminator
44              this.buffer.write(string, this.offset, 'utf-8');
45              this.offset += len;
46          }
47          this.buffer[this.offset++] = 0; // null terminator
48          return this;
49      }
50      addString(string = '') {
51          const len = Buffer.byteLength(string);
52          this.ensure(len);
53          this.buffer.write(string, this.offset);
54          this.offset += len;
55          return this;
56      }
57      add(otherBuffer) {
58          this.ensure(otherBuffer.length);
59          otherBuffer.copy(this.buffer, this.offset);
60          this.offset += otherBuffer.length;
61          return this;
62      }
63      join(code) {
64          if (code) {
65              this.buffer[this.headerPosition] = code;
66              //length is everything in this packet minus the code
67              const length = this.offset - (this.headerPosition + 1);
68              this.buffer.writeInt32BE(length, this.headerPosition + 1);
69          }
70          return this.buffer.slice(code ? 0 : 5, this.offset);
71      }
72      flush(code) {
73          const result = this.join(code);
74          this.offset = 5;
75          this.headerPosition = 0;
76          this.buffer = Buffer.allocUnsafe(this.size);
77          return result;
78      }
79  }
80  exports.Writer = Writer;
81  //# sourceMappingURL=buffer-writer.js.map