Skip to content

feat(nodejs): array support #50

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

Merged
merged 35 commits into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
bca401e
chore(nodejs): extract transport from sender, add back support for co…
glasstiger Jul 16, 2025
7efb5ec
make linter happy
glasstiger Jul 16, 2025
0ddf668
more type fixes
glasstiger Jul 16, 2025
eb0d7ee
extract buffer from sender
glasstiger Jul 17, 2025
7f4d454
TimestampUnit type
glasstiger Jul 17, 2025
f54ad65
code formatting
glasstiger Jul 23, 2025
7a70a6d
feat(nodejs): protocol version option, binary protocol for doubles
glasstiger Jul 24, 2025
87c26a4
more tests
glasstiger Jul 24, 2025
c7b2525
feat(nodejs): array support
glasstiger Jul 26, 2025
46664d0
test unsupported array types
glasstiger Jul 29, 2025
2cdf4e5
more tests, better error message
glasstiger Jul 30, 2025
ab6f968
array validation
glasstiger Jul 31, 2025
591f740
Merge remote-tracking branch 'origin/main' into binary_protocol_arrays
glasstiger Jul 31, 2025
83455aa
formatting
glasstiger Jul 31, 2025
50adf5c
fix protocol_version doc
glasstiger Jul 31, 2025
e5a7dee
fix merge fallout
glasstiger Jul 31, 2025
dc29488
fix merge fallout
glasstiger Jul 31, 2025
3021396
fix merge fallout
glasstiger Jul 31, 2025
0ad1eca
js doc for buffer
glasstiger Jul 31, 2025
4880075
code formatting
glasstiger Jul 31, 2025
d873996
more js doc
glasstiger Jul 31, 2025
165d827
Merge branch 'binary_protocol_arrays' into arrays_support
glasstiger Jul 31, 2025
505a9bb
add back scheduled run of build
glasstiger Jul 31, 2025
95d997f
remove redundant buffer overflow checks
glasstiger Aug 5, 2025
4f01717
js doc fix
glasstiger Aug 5, 2025
ccb6cab
js doc fix
glasstiger Aug 6, 2025
812d5b8
use request timeout and TLS settings from Sender options in '/setting…
glasstiger Aug 6, 2025
7c79739
use bracket notation for LINE_PROTO_SUPPORT_VERSION
glasstiger Aug 6, 2025
7ce8469
Merge branch 'binary_protocol_arrays' into arrays_support
glasstiger Aug 6, 2025
adf94c3
move writeArray() into buffer v2 from base
glasstiger Aug 6, 2025
d705546
Update src/utils.ts
glasstiger Aug 6, 2025
8ad622f
fix test
glasstiger Aug 6, 2025
c4feb7b
empty array support
glasstiger Aug 6, 2025
935fed3
Merge remote-tracking branch 'origin/main' into arrays_support
glasstiger Aug 6, 2025
3acd71f
handle zeros in arrays properly
glasstiger Aug 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches:
- main
pull_request:
schedule:
- cron: '15 2,10,18 * * *'

jobs:
test:
Expand Down
17 changes: 15 additions & 2 deletions src/buffer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const DEFAULT_MAX_NAME_LENGTH = 127;
abstract class SenderBufferBase implements SenderBuffer {
private bufferSize: number;
private readonly maxBufferSize: number;
private buffer: Buffer<ArrayBuffer>;
private position: number;
protected buffer: Buffer<ArrayBuffer>;
protected position: number;
private endOfLastRow: number;

private hasTable: boolean;
Expand Down Expand Up @@ -232,6 +232,15 @@ abstract class SenderBufferBase implements SenderBuffer {
*/
abstract floatColumn(name: string, value: number): SenderBuffer;

/**
* Writes an array column with its values into the buffer.
*
* @param {string} name - Column name.
* @param {unknown[]} value - Column value, accepts only arrays.
* @return {Sender} Returns with a reference to this sender.
*/
abstract arrayColumn(name: string, value: unknown[]): SenderBuffer;

/**
* Writes a 64-bit signed integer into the buffer. <br>
* Use it to insert into LONG, INT, SHORT and BYTE columns.
Expand Down Expand Up @@ -386,6 +395,10 @@ abstract class SenderBufferBase implements SenderBuffer {
this.position = this.buffer.writeInt8(data, this.position);
}

protected writeInt(data: number) {
this.position = this.buffer.writeInt32LE(data, this.position);
}

protected writeDouble(data: number) {
this.position = this.buffer.writeDoubleLE(data, this.position);
}
Expand Down
4 changes: 4 additions & 0 deletions src/buffer/bufferv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class SenderBufferV1 extends SenderBufferBase {
);
return this;
}

arrayColumn(): SenderBuffer {
throw new Error("Arrays are not supported in protocol v1");
}
}

export { SenderBufferV1 };
91 changes: 91 additions & 0 deletions src/buffer/bufferv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
import { SenderOptions } from "../options";
import { SenderBuffer } from "./index";
import { SenderBufferBase } from "./base";
import { ArrayPrimitive, getDimensions, validateArray } from "../utils";

const COLUMN_TYPE_DOUBLE: number = 10;
const COLUMN_TYPE_NULL: number = 33;

const ENTITY_TYPE_ARRAY: number = 14;
const ENTITY_TYPE_DOUBLE: number = 16;

const EQUALS_SIGN: number = "=".charCodeAt(0);

/**
Expand Down Expand Up @@ -37,6 +43,91 @@ class SenderBufferV2 extends SenderBufferBase {
);
return this;
}

arrayColumn(name: string, value: unknown[]): SenderBuffer {
const dimensions = getDimensions(value);
const type = validateArray(value, dimensions);
// only number arrays and NULL supported for now
if (type !== "number" && type !== null) {
throw new Error(`Unsupported array type [type=${type}]`);
}

this.writeColumn(name, value, () => {
this.checkCapacity([], 3);
this.writeByte(EQUALS_SIGN);
this.writeByte(ENTITY_TYPE_ARRAY);

if (!value) {
this.writeByte(COLUMN_TYPE_NULL);
} else {
this.writeByte(COLUMN_TYPE_DOUBLE);
this.writeArray(value, dimensions, type);
}
});
return this;
}

private writeArray(
arr: unknown[],
dimensions: number[],
type: ArrayPrimitive,
) {
this.checkCapacity([], 1 + dimensions.length * 4);
this.writeByte(dimensions.length);
for (let i = 0; i < dimensions.length; i++) {
this.writeInt(dimensions[i]);
}

this.checkCapacity([], SenderBufferV2.arraySize(dimensions, type));
this.writeArrayValues(arr, dimensions);
}

private writeArrayValues(arr: unknown[], dimensions: number[]) {
if (Array.isArray(arr[0])) {
for (let i = 0; i < arr.length; i++) {
this.writeArrayValues(arr[i] as unknown[], dimensions);
}
} else {
const type = arr[0] !== undefined ? typeof arr[0] : null;
switch (type) {
case "number":
for (let i = 0; i < arr.length; i++) {
this.position = this.buffer.writeDoubleLE(
arr[i] as number,
this.position,
);
}
break;
case null:
// empty array
break;
default:
throw new Error(`Unsupported array type [type=${type}]`);
}
}
}

private static arraySize(dimensions: number[], type: ArrayPrimitive): number {
let numOfElements = 1;
for (let i = 0; i < dimensions.length; i++) {
numOfElements *= dimensions[i];
}

switch (type) {
case "number":
return numOfElements * 8;
case "boolean":
return numOfElements;
case "string":
// in case of string[] capacity check is done separately for each array element
return 0;
case null:
// empty array
return 0;
default:
throw new Error(`Unsupported array type [type=${type}]`);
}
}
}

export { SenderBufferV2 };
2 changes: 2 additions & 0 deletions src/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ interface SenderBuffer {
*/
floatColumn(name: string, value: number): SenderBuffer;

arrayColumn(name: string, value: unknown[]): SenderBuffer;

/**
* Writes an integer column with its value into the buffer.
*
Expand Down
5 changes: 5 additions & 0 deletions src/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ class Sender {
return this;
}

arrayColumn(name: string, value: unknown[]): Sender {
this.buffer.arrayColumn(name, value);
return this;
}

/**
* Writes an integer column with its value into the buffer of the sender.
*
Expand Down
71 changes: 71 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Agent } from "undici";

type ArrayPrimitive = "number" | "boolean" | "string" | null;

type TimestampUnit = "ns" | "us" | "ms";

function isBoolean(value: unknown): value is boolean {
Expand Down Expand Up @@ -38,6 +40,72 @@ function timestampToNanos(timestamp: bigint, unit: TimestampUnit) {
}
}

function getDimensions(data: unknown) {
const dimensions: number[] = [];
while (Array.isArray(data)) {
dimensions.push(data.length);
data = data[0];
}
return dimensions;
}

function validateArray(data: unknown[], dimensions: number[]): ArrayPrimitive {
if (data === null || data === undefined) {
return null;
}
if (!Array.isArray(data)) {
throw new Error(
`The value must be an array [value=${JSON.stringify(data)}, type=${typeof data}]`,
);
}

let expectedType: ArrayPrimitive = null;

function checkArray(
array: unknown[],
depth: number = 0,
path: string = "",
): void {
const expectedLength = dimensions[depth];
if (array.length !== expectedLength) {
throw new Error(
`Lengths of sub-arrays do not match [expected=${expectedLength}, actual=${array.length}, dimensions=[${dimensions}], path=${path}]`,
);
}

if (depth < dimensions.length - 1) {
// intermediate level, expecting arrays
for (let i = 0; i < array.length; i++) {
if (!Array.isArray(array[i])) {
throw new Error(
`Mixed types found [expected=array, current=${typeof array[i]}, path=${path}[${i}]]`,
);
}
checkArray(array[i] as unknown[], depth + 1, `${path}[${i}]`);
}
} else {
// leaf level, expecting primitives
if (expectedType === null && array[0] !== undefined) {
expectedType = typeof array[0] as ArrayPrimitive;
}

for (let i = 0; i < array.length; i++) {
const currentType = typeof array[i] as ArrayPrimitive;
if (currentType !== expectedType) {
throw new Error(
expectedType !== null
? `Mixed types found [expected=${expectedType}, current=${currentType}, path=${path}[${i}]]`
: `Unsupported array type [type=${currentType}]`,
);
}
}
}
}

checkArray(data);
return expectedType;
}

/**
* Fetches JSON data from a URL.
* @template T - The expected type of the JSON response
Expand Down Expand Up @@ -83,4 +151,7 @@ export {
timestampToNanos,
TimestampUnit,
fetchJson,
getDimensions,
validateArray,
ArrayPrimitive,
};
Loading