Skip to content

Commit 1d43359

Browse files
authored
feat(nodejs): protocol version option, binary protocol for doubles (#49)
1 parent 3f97d77 commit 1d43359

File tree

14 files changed

+1954
-937
lines changed

14 files changed

+1954
-937
lines changed

src/buffer/base.ts

Lines changed: 431 additions & 0 deletions
Large diffs are not rendered by default.

src/buffer/bufferv1.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @ts-check
2+
import { SenderOptions } from "../options";
3+
import { SenderBuffer } from "./index";
4+
import { SenderBufferBase } from "./base";
5+
6+
/**
7+
* Buffer implementation for protocol version 1.
8+
* Sends floating point numbers in their text form.
9+
*/
10+
class SenderBufferV1 extends SenderBufferBase {
11+
constructor(options: SenderOptions) {
12+
super(options);
13+
}
14+
15+
/**
16+
* Writes a 64-bit floating point value into the buffer using v1 serialization (text format). <br>
17+
* Use it to insert into DOUBLE or FLOAT database columns.
18+
*
19+
* @param {string} name - Column name.
20+
* @param {number} value - Column value, accepts only number values.
21+
* @return {Sender} Returns with a reference to this sender.
22+
*/
23+
floatColumn(name: string, value: number): SenderBuffer {
24+
this.writeColumn(
25+
name,
26+
value,
27+
() => {
28+
const valueStr = value.toString();
29+
this.checkCapacity([valueStr]);
30+
this.write(valueStr);
31+
},
32+
"number",
33+
);
34+
return this;
35+
}
36+
}
37+
38+
export { SenderBufferV1 };

src/buffer/bufferv2.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @ts-check
2+
import { SenderOptions } from "../options";
3+
import { SenderBuffer } from "./index";
4+
import { SenderBufferBase } from "./base";
5+
6+
const ENTITY_TYPE_DOUBLE: number = 16;
7+
const EQUALS_SIGN: number = "=".charCodeAt(0);
8+
9+
/**
10+
* Buffer implementation for protocol version 2.
11+
* Sends floating point numbers in binary form.
12+
*/
13+
class SenderBufferV2 extends SenderBufferBase {
14+
constructor(options: SenderOptions) {
15+
super(options);
16+
}
17+
18+
/**
19+
* Writes a 64-bit floating point value into the buffer using v2 serialization (binary format). <br>
20+
* Use it to insert into DOUBLE or FLOAT database columns.
21+
*
22+
* @param {string} name - Column name.
23+
* @param {number} value - Column value, accepts only number values.
24+
* @return {Sender} Returns with a reference to this sender.
25+
*/
26+
floatColumn(name: string, value: number): SenderBuffer {
27+
this.writeColumn(
28+
name,
29+
value,
30+
() => {
31+
this.checkCapacity([], 10);
32+
this.writeByte(EQUALS_SIGN);
33+
this.writeByte(ENTITY_TYPE_DOUBLE);
34+
this.writeDouble(value);
35+
},
36+
"number",
37+
);
38+
return this;
39+
}
40+
}
41+
42+
export { SenderBufferV2 };

0 commit comments

Comments
 (0)