Skip to content

Commit 812d5b8

Browse files
committed
use request timeout and TLS settings from Sender options in '/settings' fetch()
1 parent ccb6cab commit 812d5b8

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

src/options.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { readFileSync } from "node:fs";
12
import { PathOrFileDescriptor } from "fs";
23
import { Agent } from "undici";
34
import http from "http";
45
import https from "https";
56

67
import { Logger } from "./logging";
7-
import { fetchJson } from "./utils";
8+
import { fetchJson, isBoolean, isInteger } from "./utils";
9+
import { DEFAULT_REQUEST_TIMEOUT } from "./transport/http/base";
810

911
const HTTP_PORT = 9000;
1012
const TCP_PORT = 9009;
@@ -235,7 +237,20 @@ class SenderOptions {
235237
const url = `${options.protocol}://${options.host}:${options.port}/settings`;
236238
const settings: {
237239
config: { LINE_PROTO_SUPPORT_VERSION: number[] };
238-
} = await fetchJson(url);
240+
} = await fetchJson(
241+
url,
242+
isInteger(options.request_timeout, 1)
243+
? options.request_timeout
244+
: DEFAULT_REQUEST_TIMEOUT,
245+
new Agent({
246+
connect: {
247+
ca: options.tls_ca ? readFileSync(options.tls_ca) : undefined,
248+
rejectUnauthorized: isBoolean(options.tls_verify)
249+
? options.tls_verify
250+
: true,
251+
},
252+
}),
253+
);
239254
const supportedVersions: string[] = (
240255
settings.config[LINE_PROTO_SUPPORT_VERSION] ?? []
241256
).map((version: unknown) => String(version));

src/transport/http/base.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,9 @@ abstract class HttpTransportBase implements SenderTransport {
135135
abstract send(data: Buffer): Promise<boolean>;
136136
}
137137

138-
export { HttpTransportBase, RETRIABLE_STATUS_CODES, HTTP_NO_CONTENT };
138+
export {
139+
HttpTransportBase,
140+
RETRIABLE_STATUS_CODES,
141+
HTTP_NO_CONTENT,
142+
DEFAULT_REQUEST_TIMEOUT,
143+
};

src/utils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Agent } from "undici";
2+
13
type TimestampUnit = "ns" | "us" | "ms";
24

35
function isBoolean(value: unknown): value is boolean {
@@ -40,13 +42,26 @@ function timestampToNanos(timestamp: bigint, unit: TimestampUnit) {
4042
* Fetches JSON data from a URL.
4143
* @template T - The expected type of the JSON response
4244
* @param url - The URL to fetch from
45+
* @param agent - HTTP agent to be used for the request
46+
* @param timeout - Request timeout, query will be aborted if not finished in time
4347
* @returns Promise resolving to the parsed JSON data
4448
* @throws Error if the request fails or returns a non-OK status
4549
*/
46-
async function fetchJson<T>(url: string): Promise<T> {
50+
async function fetchJson<T>(
51+
url: string,
52+
timeout: number,
53+
agent: Agent,
54+
): Promise<T> {
55+
const controller = new AbortController();
56+
const { signal } = controller;
57+
setTimeout(() => controller.abort(), timeout);
58+
4759
let response: globalThis.Response;
4860
try {
49-
response = await fetch(url);
61+
response = await fetch(url, {
62+
dispatcher: agent,
63+
signal,
64+
});
5065
} catch (error) {
5166
throw new Error(`Failed to load ${url} [error=${error}]`);
5267
}

test/options.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ describe("Configuration string parser suite", function () {
281281
);
282282
expect(options.protocol_version).toBe("2");
283283
options = await SenderOptions.fromConfig(
284-
`https::addr=localhost:${MOCK_HTTPS_PORT}`,
284+
`https::addr=localhost:${MOCK_HTTPS_PORT};tls_verify=unsafe_off`,
285285
);
286286
expect(options.protocol_version).toBe("2");
287287

@@ -302,7 +302,7 @@ describe("Configuration string parser suite", function () {
302302
);
303303
expect(options.protocol_version).toBe("1");
304304
options = await SenderOptions.fromConfig(
305-
`https::addr=localhost:${MOCK_HTTPS_PORT}`,
305+
`https::addr=localhost:${MOCK_HTTPS_PORT};tls_verify=unsafe_off`,
306306
);
307307
expect(options.protocol_version).toBe("1");
308308

@@ -323,7 +323,7 @@ describe("Configuration string parser suite", function () {
323323
);
324324
expect(options.protocol_version).toBe("1");
325325
options = await SenderOptions.fromConfig(
326-
`https::addr=localhost:${MOCK_HTTPS_PORT}`,
326+
`https::addr=localhost:${MOCK_HTTPS_PORT};tls_verify=unsafe_off`,
327327
);
328328
expect(options.protocol_version).toBe("1");
329329

@@ -342,15 +342,15 @@ describe("Configuration string parser suite", function () {
342342
await expect(
343343
async () =>
344344
await SenderOptions.fromConfig(
345-
`http::addr=localhost:${MOCK_HTTP_PORT}`,
345+
`http::addr=localhost:${MOCK_HTTP_PORT};tls_verify=unsafe_off`,
346346
),
347347
).rejects.toThrow(
348348
"Unsupported protocol versions received from server: 3,5",
349349
);
350350
await expect(
351351
async () =>
352352
await SenderOptions.fromConfig(
353-
`https::addr=localhost:${MOCK_HTTPS_PORT}`,
353+
`https::addr=localhost:${MOCK_HTTPS_PORT};tls_verify=unsafe_off`,
354354
),
355355
).rejects.toThrow(
356356
"Unsupported protocol versions received from server: 3,5",
@@ -407,7 +407,7 @@ describe("Configuration string parser suite", function () {
407407
);
408408
expect(options.protocol_version).toBe("2");
409409
options = await SenderOptions.fromConfig(
410-
`https::addr=localhost:${MOCK_HTTPS_PORT};protocol_version=auto`,
410+
`https::addr=localhost:${MOCK_HTTPS_PORT};tls_verify=unsafe_off;protocol_version=auto`,
411411
);
412412
expect(options.protocol_version).toBe("2");
413413
});

0 commit comments

Comments
 (0)