Skip to content

Commit f54ad65

Browse files
committed
code formatting
1 parent 7f4d454 commit f54ad65

20 files changed

+548
-334
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"build": "bunchee",
88
"eslint": "eslint src/**",
99
"typecheck": "tsc --noEmit",
10+
"format": "prettier --write '{src,test}/**/*.{ts,js,json}'",
1011
"docs": "pnpm run build && jsdoc ./dist/cjs/index.js README.md -d docs",
1112
"preview:docs": "serve docs"
1213
},

src/buffer/index.ts

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { Buffer } from "node:buffer";
44
import { log, Logger } from "../logging";
55
import { validateColumnName, validateTableName } from "../validation";
66
import { SenderOptions } from "../options";
7-
import { isInteger, timestampToMicros, timestampToNanos, TimestampUnit } from "../utils";
7+
import {
8+
isInteger,
9+
timestampToMicros,
10+
timestampToNanos,
11+
TimestampUnit,
12+
} from "../utils";
813

914
const DEFAULT_MAX_NAME_LENGTH = 127;
1015

@@ -42,15 +47,17 @@ class SenderBuffer {
4247
this.log = options && typeof options.log === "function" ? options.log : log;
4348
SenderOptions.resolveDeprecated(options, this.log);
4449

45-
this.maxNameLength = options && isInteger(options.max_name_len, 1)
46-
? options.max_name_len
47-
: DEFAULT_MAX_NAME_LENGTH;
50+
this.maxNameLength =
51+
options && isInteger(options.max_name_len, 1)
52+
? options.max_name_len
53+
: DEFAULT_MAX_NAME_LENGTH;
4854

49-
this.maxBufferSize = options && isInteger(options.max_buf_size, 1)
50-
? options.max_buf_size
51-
: DEFAULT_MAX_BUFFER_SIZE;
55+
this.maxBufferSize =
56+
options && isInteger(options.max_buf_size, 1)
57+
? options.max_buf_size
58+
: DEFAULT_MAX_BUFFER_SIZE;
5259
this.resize(
53-
options && isInteger(options.init_buf_size, 1)
60+
options && isInteger(options.init_buf_size, 1)
5461
? options.init_buf_size
5562
: DEFAULT_BUFFER_SIZE,
5663
);
@@ -67,7 +74,9 @@ class SenderBuffer {
6774
*/
6875
private resize(bufferSize: number) {
6976
if (bufferSize > this.maxBufferSize) {
70-
throw new Error(`Max buffer size is ${this.maxBufferSize} bytes, requested buffer size: ${bufferSize}`);
77+
throw new Error(
78+
`Max buffer size is ${this.maxBufferSize} bytes, requested buffer size: ${bufferSize}`,
79+
);
7180
}
7281
this.bufferSize = bufferSize;
7382
// Allocating an extra byte because Buffer.write() does not fail if the length of the data to be written is
@@ -158,7 +167,9 @@ class SenderBuffer {
158167
throw new Error(`Symbol name must be a string, received ${typeof name}`);
159168
}
160169
if (!this.hasTable || this.hasColumns) {
161-
throw new Error("Symbol can be added only after table name is set and before any column added");
170+
throw new Error(
171+
"Symbol can be added only after table name is set and before any column added",
172+
);
162173
}
163174
const valueStr = value.toString();
164175
this.checkCapacity([name, valueStr], 2 + name.length + valueStr.length);
@@ -262,7 +273,11 @@ class SenderBuffer {
262273
* @param {string} [unit=us] - Timestamp unit. Supported values: 'ns' - nanoseconds, 'us' - microseconds, 'ms' - milliseconds. Defaults to 'us'.
263274
* @return {Sender} Returns with a reference to this sender.
264275
*/
265-
timestampColumn(name: string, value: number | bigint, unit: TimestampUnit = "us"): SenderBuffer {
276+
timestampColumn(
277+
name: string,
278+
value: number | bigint,
279+
unit: TimestampUnit = "us",
280+
): SenderBuffer {
266281
if (typeof value !== "bigint" && !Number.isInteger(value)) {
267282
throw new Error(`Value must be an integer or BigInt, received ${value}`);
268283
}
@@ -284,10 +299,14 @@ class SenderBuffer {
284299
*/
285300
at(timestamp: number | bigint, unit: TimestampUnit = "us") {
286301
if (!this.hasSymbols && !this.hasColumns) {
287-
throw new Error("The row must have a symbol or column set before it is closed");
302+
throw new Error(
303+
"The row must have a symbol or column set before it is closed",
304+
);
288305
}
289306
if (typeof timestamp !== "bigint" && !Number.isInteger(timestamp)) {
290-
throw new Error(`Designated timestamp must be an integer or BigInt, received ${timestamp}`);
307+
throw new Error(
308+
`Designated timestamp must be an integer or BigInt, received ${timestamp}`,
309+
);
291310
}
292311
const timestampNanos = timestampToNanos(BigInt(timestamp), unit);
293312
const timestampStr = timestampNanos.toString();
@@ -304,7 +323,9 @@ class SenderBuffer {
304323
*/
305324
atNow() {
306325
if (!this.hasSymbols && !this.hasColumns) {
307-
throw new Error("The row must have a symbol or column set before it is closed");
326+
throw new Error(
327+
"The row must have a symbol or column set before it is closed",
328+
);
308329
}
309330
this.checkCapacity([], 1);
310331
this.write("\n");
@@ -334,17 +355,17 @@ class SenderBuffer {
334355
}
335356

336357
private writeColumn(
337-
name: string,
338-
value: unknown,
339-
writeValue: () => void,
340-
valueType?: string
358+
name: string,
359+
value: unknown,
360+
writeValue: () => void,
361+
valueType?: string,
341362
) {
342363
if (typeof name !== "string") {
343364
throw new Error(`Column name must be a string, received ${typeof name}`);
344365
}
345366
if (valueType && typeof value !== valueType) {
346367
throw new Error(
347-
`Column value must be of type ${valueType}, received ${typeof value}`,
368+
`Column value must be of type ${valueType}, received ${typeof value}`,
348369
);
349370
}
350371
if (!this.hasTable) {
@@ -364,7 +385,7 @@ class SenderBuffer {
364385
if (this.position > this.bufferSize) {
365386
// should never happen, if checkCapacity() is correctly used
366387
throw new Error(
367-
`Buffer overflow [position=${this.position}, bufferSize=${this.bufferSize}]`,
388+
`Buffer overflow [position=${this.position}, bufferSize=${this.bufferSize}]`,
368389
);
369390
}
370391
}

src/logging.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const LOG_LEVELS = {
88
const DEFAULT_CRITICALITY = LOG_LEVELS.info.criticality;
99

1010
type Logger = (
11-
level: "error" | "warn" | "info" | "debug",
12-
message: string | Error,
11+
level: "error" | "warn" | "info" | "debug",
12+
message: string | Error,
1313
) => void;
1414

1515
/**
@@ -20,7 +20,10 @@ type Logger = (
2020
* @param {'error'|'warn'|'info'|'debug'} level - The log level of the message.
2121
* @param {string | Error} message - The log message.
2222
*/
23-
function log(level: "error" | "warn" | "info" | "debug", message: string | Error) {
23+
function log(
24+
level: "error" | "warn" | "info" | "debug",
25+
message: string | Error,
26+
) {
2427
const logLevel = LOG_LEVELS[level];
2528
if (!logLevel) {
2629
throw new Error(`Invalid log level: '${level}'`);

src/options.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const UNSAFE_OFF = "unsafe_off";
2020
type ExtraOptions = {
2121
log?: Logger;
2222
agent?: Agent | http.Agent | https.Agent;
23-
}
23+
};
2424

2525
type DeprecatedOptions = {
2626
/** @deprecated */
@@ -190,34 +190,46 @@ class SenderOptions {
190190
this.log = extraOptions.log;
191191

192192
if (
193-
extraOptions.agent
194-
&& !(extraOptions.agent instanceof Agent)
195-
&& !(extraOptions.agent instanceof http.Agent)
196-
// @ts-expect-error - Not clear what the problem is, the two lines above have no issues
197-
&& !(extraOptions.agent instanceof https.Agent)
193+
extraOptions.agent &&
194+
!(extraOptions.agent instanceof Agent) &&
195+
!(extraOptions.agent instanceof http.Agent) &&
196+
// @ts-expect-error - Not clear what the problem is, the two lines above have no issues
197+
!(extraOptions.agent instanceof https.Agent)
198198
) {
199199
throw new Error("Invalid HTTP agent");
200200
}
201201
this.agent = extraOptions.agent;
202202
}
203203
}
204204

205-
static resolveDeprecated(options: SenderOptions & DeprecatedOptions, log: Logger) {
205+
static resolveDeprecated(
206+
options: SenderOptions & DeprecatedOptions,
207+
log: Logger,
208+
) {
206209
if (!options) {
207210
return;
208211
}
209212

210213
// deal with deprecated options
211214
if (options.copy_buffer !== undefined) {
212-
log("warn", `Option 'copy_buffer' is not supported anymore, please, remove it`);
215+
log(
216+
"warn",
217+
`Option 'copy_buffer' is not supported anymore, please, remove it`,
218+
);
213219
options.copy_buffer = undefined;
214220
}
215221
if (options.copyBuffer !== undefined) {
216-
log("warn", `Option 'copyBuffer' is not supported anymore, please, remove it`);
222+
log(
223+
"warn",
224+
`Option 'copyBuffer' is not supported anymore, please, remove it`,
225+
);
217226
options.copyBuffer = undefined;
218227
}
219228
if (options.bufferSize !== undefined) {
220-
log("warn", `Option 'bufferSize' is not supported anymore, please, replace it with 'init_buf_size'`);
229+
log(
230+
"warn",
231+
`Option 'bufferSize' is not supported anymore, please, replace it with 'init_buf_size'`,
232+
);
221233
options.init_buf_size = options.bufferSize;
222234
options.bufferSize = undefined;
223235
}
@@ -235,7 +247,10 @@ class SenderOptions {
235247
*
236248
* @return {SenderOptions} A Sender configuration object initialized from the provided configuration string.
237249
*/
238-
static fromConfig(configurationString: string, extraOptions?: ExtraOptions): SenderOptions {
250+
static fromConfig(
251+
configurationString: string,
252+
extraOptions?: ExtraOptions,
253+
): SenderOptions {
239254
return new SenderOptions(configurationString, extraOptions);
240255
}
241256

@@ -255,7 +270,10 @@ class SenderOptions {
255270
}
256271
}
257272

258-
function parseConfigurationString(options: SenderOptions, configString: string) {
273+
function parseConfigurationString(
274+
options: SenderOptions,
275+
configString: string,
276+
) {
259277
if (!configString) {
260278
throw new Error("Configuration string is missing or empty");
261279
}
@@ -278,7 +296,10 @@ function parseSettings(
278296
) {
279297
let index = configString.indexOf(";", position);
280298
while (index > -1) {
281-
if (index + 1 < configString.length && configString.charAt(index + 1) === ";") {
299+
if (
300+
index + 1 < configString.length &&
301+
configString.charAt(index + 1) === ";"
302+
) {
282303
index = configString.indexOf(";", index + 2);
283304
continue;
284305
}
@@ -436,7 +457,7 @@ function parseTlsOptions(options: SenderOptions) {
436457
if (options.tls_roots || options.tls_roots_password) {
437458
throw new Error(
438459
"'tls_roots' and 'tls_roots_password' options are not supported, please, " +
439-
"use the 'tls_ca' option or the NODE_EXTRA_CA_CERTS environment variable instead",
460+
"use the 'tls_ca' option or the NODE_EXTRA_CA_CERTS environment variable instead",
440461
);
441462
}
442463
}

src/sender.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ class Sender {
9999
*
100100
* @return {Sender} A Sender object initialized from the provided configuration string.
101101
*/
102-
static fromConfig(configurationString: string, extraOptions?: ExtraOptions): Sender {
102+
static fromConfig(
103+
configurationString: string,
104+
extraOptions?: ExtraOptions,
105+
): Sender {
103106
return new Sender(
104107
SenderOptions.fromConfig(configurationString, extraOptions),
105108
);
@@ -155,7 +158,10 @@ class Sender {
155158
return false; // Nothing to send
156159
}
157160

158-
this.log("debug", `Flushing, number of flushed rows: ${this.pendingRowCount}`);
161+
this.log(
162+
"debug",
163+
`Flushing, number of flushed rows: ${this.pendingRowCount}`,
164+
);
159165
this.resetAutoFlush();
160166

161167
await this.transport.send(dataToSend);
@@ -248,7 +254,11 @@ class Sender {
248254
* @param {string} [unit=us] - Timestamp unit. Supported values: 'ns' - nanoseconds, 'us' - microseconds, 'ms' - milliseconds. Defaults to 'us'.
249255
* @return {Sender} Returns with a reference to this sender.
250256
*/
251-
timestampColumn(name: string, value: number | bigint, unit: TimestampUnit = "us"): Sender {
257+
timestampColumn(
258+
name: string,
259+
value: number | bigint,
260+
unit: TimestampUnit = "us",
261+
): Sender {
252262
this.buffer.timestampColumn(name, value, unit);
253263
return this;
254264
}
@@ -285,12 +295,11 @@ class Sender {
285295

286296
private async tryFlush() {
287297
if (
288-
this.autoFlush
289-
&& this.pendingRowCount > 0
290-
&& (
291-
(this.autoFlushRows > 0 && this.pendingRowCount >= this.autoFlushRows)
292-
|| (this.autoFlushInterval > 0 && Date.now() - this.lastFlushTime >= this.autoFlushInterval)
293-
)
298+
this.autoFlush &&
299+
this.pendingRowCount > 0 &&
300+
((this.autoFlushRows > 0 && this.pendingRowCount >= this.autoFlushRows) ||
301+
(this.autoFlushInterval > 0 &&
302+
Date.now() - this.lastFlushTime >= this.autoFlushInterval))
294303
) {
295304
await this.flush();
296305
}

src/transport/http/base.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ abstract class HttpTransportBase implements SenderTransport {
7676
this.port = options.port;
7777

7878
this.requestMinThroughput = isInteger(options.request_min_throughput, 0)
79-
? options.request_min_throughput
80-
: DEFAULT_REQUEST_MIN_THROUGHPUT;
79+
? options.request_min_throughput
80+
: DEFAULT_REQUEST_MIN_THROUGHPUT;
8181
this.requestTimeout = isInteger(options.request_timeout, 1)
82-
? options.request_timeout
83-
: DEFAULT_REQUEST_TIMEOUT;
82+
? options.request_timeout
83+
: DEFAULT_REQUEST_TIMEOUT;
8484
this.retryTimeout = isInteger(options.retry_timeout, 0)
85-
? options.retry_timeout
86-
: DEFAULT_RETRY_TIMEOUT;
85+
? options.retry_timeout
86+
: DEFAULT_RETRY_TIMEOUT;
8787

8888
switch (options.protocol) {
8989
case HTTP:
@@ -93,16 +93,17 @@ abstract class HttpTransportBase implements SenderTransport {
9393
this.secure = true;
9494
break;
9595
default:
96-
throw new Error("The 'protocol' has to be 'http' or 'https' for the HTTP transport");
96+
throw new Error(
97+
"The 'protocol' has to be 'http' or 'https' for the HTTP transport",
98+
);
9799
}
98100
}
99101

100102
connect(): Promise<boolean> {
101103
throw new Error("'connect()' is not required for HTTP transport");
102104
}
103105

104-
async close(): Promise<void> {
105-
}
106+
async close(): Promise<void> {}
106107

107108
getDefaultAutoFlushRows(): number {
108109
return DEFAULT_HTTP_AUTO_FLUSH_ROWS;

0 commit comments

Comments
 (0)