| [62b2964] | 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 | // Write an Int32 byte-length prefix immediately followed by the string's UTF-8
|
|---|
| 58 | // bytes. Postgres' Bind wire format prefixes every parameter with its length,
|
|---|
| 59 | // and doing it in one method computes Buffer.byteLength ONCE — the previous
|
|---|
| 60 | // `addInt32(Buffer.byteLength(s)).addString(s)` pairing scanned the string
|
|---|
| 61 | // three times (byteLength for the prefix, byteLength again inside addString,
|
|---|
| 62 | // then the encode), which is costly for large text parameters.
|
|---|
| 63 | addInt32PrefixedString(string) {
|
|---|
| 64 | const len = Buffer.byteLength(string);
|
|---|
| 65 | this.ensure(4 + len);
|
|---|
| 66 | const buffer = this.buffer;
|
|---|
| 67 | let offset = this.offset;
|
|---|
| 68 | buffer[offset++] = (len >>> 24) & 0xff;
|
|---|
| 69 | buffer[offset++] = (len >>> 16) & 0xff;
|
|---|
| 70 | buffer[offset++] = (len >>> 8) & 0xff;
|
|---|
| 71 | buffer[offset++] = (len >>> 0) & 0xff;
|
|---|
| 72 | buffer.write(string, offset, 'utf-8');
|
|---|
| 73 | this.offset = offset + len;
|
|---|
| 74 | return this;
|
|---|
| 75 | }
|
|---|
| 76 | add(otherBuffer) {
|
|---|
| 77 | this.ensure(otherBuffer.length);
|
|---|
| 78 | otherBuffer.copy(this.buffer, this.offset);
|
|---|
| 79 | this.offset += otherBuffer.length;
|
|---|
| 80 | return this;
|
|---|
| 81 | }
|
|---|
| 82 | join(code) {
|
|---|
| 83 | if (code) {
|
|---|
| 84 | this.buffer[this.headerPosition] = code;
|
|---|
| 85 | //length is everything in this packet minus the code
|
|---|
| 86 | const length = this.offset - (this.headerPosition + 1);
|
|---|
| 87 | this.buffer.writeInt32BE(length, this.headerPosition + 1);
|
|---|
| 88 | }
|
|---|
| 89 | return this.buffer.slice(code ? 0 : 5, this.offset);
|
|---|
| 90 | }
|
|---|
| 91 | flush(code) {
|
|---|
| 92 | const result = this.join(code);
|
|---|
| 93 | this.offset = 5;
|
|---|
| 94 | this.headerPosition = 0;
|
|---|
| 95 | this.buffer = Buffer.allocUnsafe(this.size);
|
|---|
| 96 | return result;
|
|---|
| 97 | }
|
|---|
| 98 | clear() {
|
|---|
| 99 | this.offset = 5;
|
|---|
| 100 | this.headerPosition = 0;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | exports.Writer = Writer;
|
|---|
| 104 | //# sourceMappingURL=buffer-writer.js.map |
|---|