Skip to content

Commit fa0e6ac

Browse files
Merge pull request #4 from John-Lin/fix/probe-default-port
fix(probe): use protocol-aware default port for endpoint probing
2 parents f0af602 + 73ae4d0 commit fa0e6ac

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

src/probe.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@ export type ProbeResult = { ok: boolean; ms: number; error?: string }
77
* Opens a TCP connection to the host and port parsed from `endpoint` to verify
88
* reachability before the OTel SDK initialises. Resolves within 5 seconds.
99
*/
10-
export function probeEndpoint(endpoint: string): Promise<ProbeResult> {
11-
let host: string
12-
let port: number
10+
export function parseEndpoint(endpoint: string): { host: string; port: number } | null {
1311
try {
1412
const url = new URL(endpoint)
15-
host = url.hostname
16-
port = parseInt(url.port || "4317", 10)
13+
const defaultPort = url.protocol === "http:" ? 80 : url.protocol === "https:" ? 443 : 4317
14+
return { host: url.hostname, port: url.port ? parseInt(url.port, 10) : defaultPort }
1715
} catch {
16+
return null
17+
}
18+
}
19+
20+
export function probeEndpoint(endpoint: string): Promise<ProbeResult> {
21+
const parsed = parseEndpoint(endpoint)
22+
if (!parsed) {
1823
return Promise.resolve({ ok: false, ms: 0, error: `invalid endpoint URL: ${endpoint}` })
1924
}
25+
const { host, port } = parsed
2026
return new Promise((resolve) => {
2127
const start = Date.now()
2228
const socket = net.createConnection({ host, port }, () => {

tests/probe.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
import { describe, test, expect } from "bun:test"
2-
import { probeEndpoint } from "../src/probe.ts"
2+
import { probeEndpoint, parseEndpoint } from "../src/probe.ts"
3+
4+
describe("parseEndpoint", () => {
5+
test("uses port 80 for http:// URLs without explicit port", () => {
6+
expect(parseEndpoint("http://api.honeycomb.io")).toEqual({ host: "api.honeycomb.io", port: 80 })
7+
})
8+
9+
test("uses port 443 for https:// URLs without explicit port", () => {
10+
expect(parseEndpoint("https://api.honeycomb.io")).toEqual({ host: "api.honeycomb.io", port: 443 })
11+
})
12+
13+
test("uses explicit port when provided", () => {
14+
expect(parseEndpoint("http://localhost:4317")).toEqual({ host: "localhost", port: 4317 })
15+
})
16+
17+
test("defaults to 4317 for unknown protocols without explicit port", () => {
18+
expect(parseEndpoint("grpc://api.honeycomb.io")).toEqual({ host: "api.honeycomb.io", port: 4317 })
19+
})
20+
21+
test("returns null for invalid URLs", () => {
22+
expect(parseEndpoint("not a url")).toBeNull()
23+
})
24+
})
325

426
describe("probeEndpoint", () => {
527
test("returns error for malformed URL (no scheme)", async () => {

0 commit comments

Comments
 (0)