Skip to content

Commit b0ef27b

Browse files
committed
add some js doc, mostly copied from #51
1 parent f54ad65 commit b0ef27b

File tree

6 files changed

+116
-79
lines changed

6 files changed

+116
-79
lines changed

src/buffer/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ const DEFAULT_MAX_NAME_LENGTH = 127;
1616
const DEFAULT_BUFFER_SIZE = 65536; // 64 KB
1717
const DEFAULT_MAX_BUFFER_SIZE = 104857600; // 100 MB
1818

19-
/** @classdesc
20-
* The QuestDB client's API provides methods to connect to the database, ingest data, and close the connection.
21-
* If no custom agent is configured, the Sender will use its own agent which overrides some default values
22-
* of <i>undici.Agent</i>. The Sender's own agent uses persistent connections with 1 minute idle timeout, pipelines requests default to 1.
23-
* </p>
19+
/**
20+
* Buffer used by the Sender.
2421
*/
2522
class SenderBuffer {
2623
private bufferSize: number;
@@ -38,7 +35,7 @@ class SenderBuffer {
3835
private readonly log: Logger;
3936

4037
/**
41-
* Creates an instance of Sender.
38+
* Creates an instance of SenderBuffer.
4239
*
4340
* @param {SenderOptions} options - Sender configuration object. <br>
4441
* See SenderOptions documentation for detailed description of configuration options. <br>
@@ -66,7 +63,7 @@ class SenderBuffer {
6663
}
6764

6865
/**
69-
* Extends the size of the sender's buffer. <br>
66+
* Extends the size of the buffer. <br>
7067
* Can be used to increase the size of buffer if overflown.
7168
* The buffer's content is copied into the new buffer.
7269
*

src/transport/http/base.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ We are retrying on the following response codes (copied from the Rust client):
3131
*/
3232
const RETRIABLE_STATUS_CODES = [500, 503, 504, 507, 509, 523, 524, 529, 599];
3333

34-
/** @classdesc
35-
* The QuestDB client's API provides methods to connect to the database, ingest data, and close the connection.
36-
* The supported protocols are HTTP and TCP. HTTP is preferred as it provides feedback in the HTTP response. <br>
34+
/**
35+
* Abstract base class for HTTP-based transport implementations. <br>
36+
* Provides common configuration and functionality for HTTP and HTTPS protocols.
3737
*/
3838
abstract class HttpTransportBase implements SenderTransport {
3939
protected readonly secure: boolean;
@@ -53,6 +53,11 @@ abstract class HttpTransportBase implements SenderTransport {
5353

5454
protected readonly log: Logger;
5555

56+
/**
57+
* Creates a new HttpTransportBase instance.
58+
* @param options - Sender configuration options including connection and authentication details
59+
* @throws Error if required protocol or host options are missing
60+
*/
5661
protected constructor(options: SenderOptions) {
5762
if (!options || !options.protocol) {
5863
throw new Error("The 'protocol' option is mandatory");
@@ -99,16 +104,35 @@ abstract class HttpTransportBase implements SenderTransport {
99104
}
100105
}
101106

107+
/**
108+
* HTTP transport does not require explicit connection establishment.
109+
* @throws Error indicating connect is not required for HTTP transport
110+
*/
102111
connect(): Promise<boolean> {
103112
throw new Error("'connect()' is not required for HTTP transport");
104113
}
105114

106-
async close(): Promise<void> {}
115+
/**
116+
* HTTP transport does not require explicit connection closure.
117+
* @returns Promise that resolves immediately
118+
*/
119+
async close(): Promise<void> {
120+
}
107121

122+
/**
123+
* Gets the default auto-flush row count for HTTP transport.
124+
* @returns Default number of rows that trigger auto-flush
125+
*/
108126
getDefaultAutoFlushRows(): number {
109127
return DEFAULT_HTTP_AUTO_FLUSH_ROWS;
110128
}
111129

130+
/**
131+
* Sends data to the QuestDB server via HTTP.
132+
* Must be implemented by concrete HTTP transport classes.
133+
* @param data - Buffer containing the data to send
134+
* @returns Promise resolving to true if data was sent successfully
135+
*/
112136
abstract send(data: Buffer): Promise<boolean>;
113137
}
114138

src/transport/http/legacy.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ import {
1010
HTTP_NO_CONTENT,
1111
} from "./base";
1212

13-
// default options for HTTP agent
14-
// - persistent connections with 1 minute idle timeout, server side has 5 minutes set by default
15-
// - max open connections is set to 256, same as server side default
13+
/**
14+
* Default configuration for HTTP agents.
15+
* - Persistent connections with 1 minute idle timeout
16+
* - Maximum of 256 open connections (matching server default)
17+
*/
1618
const DEFAULT_HTTP_AGENT_CONFIG = {
1719
maxSockets: 256,
1820
keepAlive: true,
1921
timeout: 60000, // 1 min
2022
};
2123

22-
/** @classdesc
23-
* The QuestDB client's API provides methods to connect to the database, ingest data, and close the connection.
24-
* The supported protocols are HTTP and TCP. HTTP is preferred as it provides feedback in the HTTP response. <br>
25-
* Based on benchmarks HTTP also provides higher throughput, if configured to ingest data in bigger batches.
24+
/**
25+
* HTTP transport implementation using Node.js built-in http/https modules. <br>
26+
* Supports both HTTP and HTTPS protocols with configurable authentication.
2627
*/
2728
class HttpTransport extends HttpTransportBase {
2829
private static DEFAULT_HTTP_AGENT: http.Agent;
@@ -31,10 +32,10 @@ class HttpTransport extends HttpTransportBase {
3132
private readonly agent: http.Agent | https.Agent;
3233

3334
/**
34-
* Creates an instance of Sender.
35+
* Creates a new HttpTransport instance using Node.js HTTP modules.
3536
*
36-
* @param {SenderOptions} options - Sender configuration object. <br>
37-
* See SenderOptions documentation for detailed description of configuration options. <br>
37+
* @param options - Sender configuration object containing connection details
38+
* @throws Error if the protocol is not 'http' or 'https'
3839
*/
3940
constructor(options: SenderOptions) {
4041
super(options);
@@ -59,6 +60,14 @@ class HttpTransport extends HttpTransportBase {
5960
}
6061
}
6162

63+
/**
64+
* Sends data to QuestDB using HTTP POST.
65+
* @param data - Buffer containing data to send
66+
* @param retryBegin - Internal parameter for tracking retry start time
67+
* @param retryInterval - Internal parameter for tracking retry intervals
68+
* @returns Promise resolving to true if data was sent successfully
69+
* @throws Error if request fails after all retries or times out
70+
*/
6271
send(data: Buffer, retryBegin = -1, retryInterval = -1): Promise<boolean> {
6372
const request = this.secure ? https.request : http.request;
6473

src/transport/http/undici.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010
HTTP_NO_CONTENT,
1111
} from "./base";
1212

13+
/**
14+
* Default HTTP options for the Undici agent.
15+
* Configures keep-alive connections with 60-second timeout and single request pipelining.
16+
*/
1317
const DEFAULT_HTTP_OPTIONS: Agent.Options = {
1418
connect: {
1519
keepAlive: true,
@@ -18,11 +22,10 @@ const DEFAULT_HTTP_OPTIONS: Agent.Options = {
1822
keepAliveTimeout: 60000, // 1 minute
1923
};
2024

21-
/** @classdesc
22-
* The QuestDB client's API provides methods to connect to the database, ingest data, and close the connection.
23-
* The supported protocols are HTTP and TCP. HTTP is preferred as it provides feedback in the HTTP response. <br>
24-
* of <i>undici.Agent</i>. The Sender's own agent uses persistent connections with 1 minute idle timeout, pipelines requests default to 1.
25-
* </p>
25+
/**
26+
* HTTP transport implementation using the Undici library. <br>
27+
* Provides high-performance HTTP requests with connection pooling and retry logic. <br>
28+
* Supports both HTTP and HTTPS protocols with configurable authentication.
2629
*/
2730
class UndiciTransport extends HttpTransportBase {
2831
private static DEFAULT_HTTP_AGENT: Agent;
@@ -31,10 +34,10 @@ class UndiciTransport extends HttpTransportBase {
3134
private readonly dispatcher: RetryAgent;
3235

3336
/**
34-
* Creates an instance of Sender.
37+
* Creates a new UndiciTransport instance.
3538
*
36-
* @param {SenderOptions} options - Sender configuration object. <br>
37-
* See SenderOptions documentation for detailed description of configuration options. <br>
39+
* @param options - Sender configuration object containing connection and retry settings
40+
* @throws Error if the protocol is not 'http' or 'https'
3841
*/
3942
constructor(options: SenderOptions) {
4043
super(options);
@@ -89,6 +92,12 @@ class UndiciTransport extends HttpTransportBase {
8992
});
9093
}
9194

95+
/**
96+
* Sends data to QuestDB using HTTP POST.
97+
* @param data - Buffer containing data to send
98+
* @returns Promise resolving to true if data was sent successfully
99+
* @throws Error if request fails after all retries or times out
100+
*/
92101
async send(data: Buffer): Promise<boolean> {
93102
const headers: Record<string, string> = {};
94103
if (this.token) {

src/transport/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,45 @@ import { UndiciTransport } from "./http/undici";
66
import { TcpTransport } from "./tcp";
77
import { HttpTransport } from "./http/legacy";
88

9+
/**
10+
* Interface for QuestDB transport implementations. <br>
11+
* Defines the contract for different transport protocols (HTTP/HTTPS/TCP/TCPS).
12+
*/
913
interface SenderTransport {
14+
/**
15+
* Establishes a connection to the database server. <br>
16+
* Should not be called on HTTP transports.
17+
* @returns Promise resolving to true if connection is successful
18+
*/
1019
connect(): Promise<boolean>;
20+
21+
/**
22+
* Sends buffered data to the database server.
23+
* @param data - Buffer containing the data to send
24+
* @returns Promise resolving to true if data was sent successfully
25+
*/
1126
send(data: Buffer): Promise<boolean>;
27+
28+
/**
29+
* Closes the connection to the database server. <br>
30+
* Should not be called on HTTP transports.
31+
* @returns Promise that resolves when the connection is closed
32+
*/
1233
close(): Promise<void>;
34+
35+
/**
36+
* Gets the default number of rows that trigger auto-flush for this transport.
37+
* @returns Default auto-flush row count
38+
*/
1339
getDefaultAutoFlushRows(): number;
1440
}
1541

42+
/**
43+
* Factory function to create appropriate transport instance based on configuration.
44+
* @param options - Sender configuration options including protocol and connection details
45+
* @returns Transport instance appropriate for the specified protocol
46+
* @throws Error if protocol or host options are missing or invalid
47+
*/
1648
function createTransport(options: SenderOptions): SenderTransport {
1749
if (!options || !options.protocol) {
1850
throw new Error("The 'protocol' option is mandatory");

src/transport/tcp.ts

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,16 @@ import { isBoolean } from "../utils";
1212

1313
const DEFAULT_TCP_AUTO_FLUSH_ROWS = 600;
1414

15-
// an arbitrary public key, not used in authentication
16-
// only used to construct a valid JWK token which is accepted by the crypto API
15+
// Arbitrary public key, used to construct valid JWK tokens.
16+
// These are not used for actual authentication, only required for crypto API compatibility.
1717
const PUBLIC_KEY = {
1818
x: "aultdA0PjhD_cWViqKKyL5chm6H1n-BiZBo_48T-uqc",
1919
y: "__ptaol41JWSpTTL525yVEfzmY8A6Vi_QrW1FjKcHMg",
2020
};
2121

22-
/** @classdesc
23-
* The QuestDB client's API provides methods to connect to the database, ingest data, and close the connection.
24-
* The supported protocols are HTTP and TCP. HTTP is preferred as it provides feedback in the HTTP response. <br>
25-
* Based on benchmarks HTTP also provides higher throughput, if configured to ingest data in bigger batches.
26-
* <p>
27-
* The client supports authentication. <br>
28-
* Authentication details can be passed to the Sender in its configuration options. <br>
29-
* The client supports Basic username/password and Bearer token authentication methods when used with HTTP protocol,
30-
* and JWK token authentication when ingesting data via TCP. <br>
31-
* Please, note that authentication is enabled by default in QuestDB Enterprise only. <br>
32-
* Details on how to configure authentication in the open source version of
33-
* QuestDB: {@link https://questdb.io/docs/reference/api/ilp/authenticate}
34-
* </p>
35-
* <p>
36-
* The client also supports TLS encryption for both, HTTP and TCP transports to provide a secure connection. <br>
37-
* Please, note that the open source version of QuestDB does not support TLS, and requires an external reverse-proxy,
38-
* such as Nginx to enable encryption.
39-
* </p>
40-
* <p>
41-
* The client uses a buffer to store data. It automatically flushes the buffer by sending its content to the server.
42-
* Auto flushing can be disabled via configuration options to gain control over transactions. Initial and maximum
43-
* buffer sizes can also be set.
44-
* </p>
45-
* <p>
46-
* It is recommended that the Sender is created by using one of the static factory methods,
47-
* <i>Sender.fromConfig(configString, extraOptions)</i> or <i>Sender.fromEnv(extraOptions)</i>.
48-
* If the Sender is created via its constructor, at least the SenderOptions configuration object should be
49-
* initialized from a configuration string to make sure that the parameters are validated. <br>
50-
* Detailed description of the Sender's configuration options can be found in
51-
* the <a href="SenderOptions.html">SenderOptions</a> documentation.
52-
* </p>
53-
* <p>
54-
* Extra options can be provided to the Sender in the <i>extraOptions</i> configuration object. <br>
55-
* A custom logging function and a custom HTTP(S) agent can be passed to the Sender in this object. <br>
56-
* The logger implementation provides the option to direct log messages to the same place where the host application's
57-
* log is saved. The default logger writes to the console. <br>
58-
* The custom HTTP(S) agent option becomes handy if there is a need to modify the default options set for the
59-
* HTTP(S) connections. A popular setting would be disabling persistent connections, in this case an agent can be
60-
* passed to the Sender with <i>keepAlive</i> set to <i>false</i>. <br>
61-
* For example: <i>Sender.fromConfig(`http::addr=host:port`, { agent: new undici.Agent({ connect: { keepAlive: false } })})</i> <br>
62-
* If no custom agent is configured, the Sender will use its own agent which overrides some default values
63-
* of <i>undici.Agent</i>. The Sender's own agent uses persistent connections with 1 minute idle timeout, pipelines requests default to 1.
64-
* </p>
22+
/**
23+
* TCP transport implementation. <br>
24+
* Supports both plain TCP or secure TLS-encrypted connections with configurable JWK token authentication.
6525
*/
6626
class TcpTransport implements SenderTransport {
6727
private readonly secure: boolean;
@@ -77,10 +37,10 @@ class TcpTransport implements SenderTransport {
7737
private readonly jwk: Record<string, string>;
7838

7939
/**
80-
* Creates an instance of Sender.
40+
* Creates a new TcpTransport instance.
8141
*
82-
* @param {SenderOptions} options - Sender configuration object. <br>
83-
* See SenderOptions documentation for detailed description of configuration options. <br>
42+
* @param options - Sender configuration object containing connection and authentication details
43+
* @throws Error if required options are missing or protocol is not 'tcp' or 'tcps'
8444
*/
8545
constructor(options: SenderOptions) {
8646
if (!options || !options.protocol) {
@@ -121,8 +81,8 @@ class TcpTransport implements SenderTransport {
12181

12282
/**
12383
* Creates a TCP connection to the database.
124-
*
125-
* @return {Promise<boolean>} Resolves to true if the client is connected.
84+
* @returns Promise resolving to true if the connection is established successfully
85+
* @throws Error if connection fails or authentication is rejected
12686
*/
12787
connect(): Promise<boolean> {
12888
const connOptions: net.NetConnectOpts | tls.ConnectionOptions = {
@@ -193,6 +153,12 @@ class TcpTransport implements SenderTransport {
193153
});
194154
}
195155

156+
/**
157+
* Sends data over the established TCP connection.
158+
* @param data - Buffer containing the data to send
159+
* @returns Promise resolving to true if data was sent successfully
160+
* @throws Error if the data could not be written to the socket
161+
*/
196162
send(data: Buffer): Promise<boolean> {
197163
if (!this.socket || this.socket.destroyed) {
198164
throw new Error("TCP transport is not connected");

0 commit comments

Comments
 (0)