Skip to content

Commit 9a56cdf

Browse files
committed
Fix some bugs in the bits library.
1 parent 2d9b674 commit 9a56cdf

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

postgres-js/bits.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
var chr, proto;
1111

12-
exports.Encoder = function () {
12+
exports.Encoder = function (header) {
13+
this.header = header || "";
1314
this.data = "";
1415
};
1516

@@ -30,13 +31,9 @@
3031
return chr(a) + chr(b) + chr(c) + chr(d);
3132
}
3233

33-
// Add a postgres header to the binary string
34-
proto.add_header = function (code) {
35-
if (code === undefined) {
36-
code = "";
37-
}
38-
this.data = code + encode_int32(this.data.length + 4) + this.data;
39-
return this;
34+
// Add a postgres header to the binary string and return it.
35+
proto.toString = function () {
36+
return this.header + encode_int32(this.data.length + 4) + this.data;
4037
};
4138

4239
// Encode number as 32 bit 2s compliment
@@ -73,6 +70,16 @@
7370
this.data += fields.join("\0") + "\0\0";
7471
return this;
7572
};
73+
74+
proto.push_hash = function (hash) {
75+
for (key in hash) {
76+
if (hash.hasOwnProperty(key)) {
77+
this.data += key + "\0" + hash[key] + "\0";
78+
}
79+
}
80+
this.data += "\0";
81+
return this;
82+
};
7683

7784
}());
7885

@@ -89,6 +96,12 @@
8996

9097
proto = exports.Decoder.prototype;
9198

99+
proto.shift_code = function () {
100+
var code = this.data[0];
101+
this.data = this.data.substr(1);
102+
return code;
103+
}
104+
92105
// Convert 4 characters to signed 32 bit integer
93106
proto.shift_int32 = function () {
94107
var unsigned = this.data.charCodeAt(0) * 0x1000000 + this.data.charCodeAt(1) * 0x10000 + this.data.charCodeAt(2) * 0x100 + this.data.charCodeAt(3);
@@ -113,7 +126,7 @@
113126
// Grab a null terminated string
114127
proto.shift_cstring = function () {
115128
var pos, string;
116-
pos = this.indexOf("\0");
129+
pos = this.data.indexOf("\0");
117130
string = this.data.substr(0, pos);
118131
this.data = this.data.substr(pos + 1);
119132
return string;

0 commit comments

Comments
 (0)