Skip to content

chore(nodejs): extract transport and buffer from sender, add back support for core http #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

glasstiger
Copy link
Collaborator

@glasstiger glasstiger commented Jul 16, 2025

Extracting the transport layer from Sender, and introducing the SenderTransport interface with different implementations:

  • TCP transport: uses a socket with ILP over TCP
  • HTTP transport: uses node's core http module with ILP over HTTP
  • HTTP Undici transport: uses the undici module with ILP over HTTP

Also extracting the buffer from Sender into a class SenderBuffer.
Now the Sender is mostly just wiring between a transport and a buffer, and responsible only for auto flushing.
In fact, you could create your own transport and buffer, and use them without a Sender if you are calling flush() manually.
You could also create your own pools of buffers and transports, the transports could be even different types, and just flush your buffers when needed using one of the transports from the pool.

Also refactored and improved tests.
Introduced more types, such as Logger, ExtraOptions, TimestampUnit and MockConfig.
Fixed any and unknown type warnings.

@glasstiger glasstiger changed the title chore(nodejs): extract transport from sender, add back support for core http chore(nodejs): extract transport and buffer from sender, add back support for core http Jul 17, 2025
private readonly log: Logger;

/**
* Creates an instance of Sender.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Creates an instance of Sender.
* Creates an instance of SenderBuffer.

timeoutMillis: number,
): http.RequestOptions | https.RequestOptions {
return {
//protocol: this.secure ? "https:" : "http:",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A leftover? Shouldn't we specify the protocol?

/** @classdesc
* The QuestDB client's API provides methods to connect to the database, ingest data, and close the connection.
* The supported protocols are HTTP and TCP. HTTP is preferred as it provides feedback in the HTTP response. <br>
* of <i>undici.Agent</i>. The Sender's own agent uses persistent connections with 1 minute idle timeout, pipelines requests default to 1.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: of undici.Agent. doesn't seem to be a complete sentence. Also, all of the transport and buffer jsdocs seem to be the same (The QuestDB client's API provides methods to connect...), so they don't seem to be very helpful.

private readonly dispatcher: RetryAgent;

/**
* Creates an instance of Sender.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Creates an instance of Sender.
* Creates an instance of UndiciTransport.

if (body.byteLength > 0) {
this.log(
"warn",
`Unexpected message from server: ${Buffer.from(body).toString()}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the response may be large, so it's better to truncate it when logging.

import { TcpTransport } from "./tcp";
import { HttpTransport } from "./http/legacy";

interface SenderTransport {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: shouldn't we have some jsdoc for the common interfaces like this one?

private readonly jwk: Record<string, string>;

/**
* Creates an instance of Sender.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Creates an instance of Sender.
* Creates an instance of TcpTransport.


/**
* Closes the TCP connection to the database. <br>
* Data sitting in the Sender's buffer will be lost unless flush() is called before close().
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might be a good idea to log a warning when some data is left in the buffer when close() is called.


/** @private */ socket;
private buffer: SenderBuffer;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could be readonly?

`Column value must be of type ${valueType}, received ${typeof value}`,
);
this.log("debug", `Pending row count: ${this.pendingRowCount}`);
await this.tryFlush();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Java Sender auto_flush is only supported for HTTP transport, but this change enables it for TCP transport. Is that on purpose?

log?: Logger;
agent?: Agent | http.Agent | https.Agent;

legacy_http?: boolean;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This option doesn't seem to be documented. Also, I wouldn't call the default HTTP client "legacy", so something like stdlib_http or default_http may be a better name.

@puzpuzpuz puzpuzpuz added the enhancement New feature or request label Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants