From 654b23a8bbdf08d1ee20f8f1afed3a0027b4b826 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 22 Jan 2026 15:15:37 -0500 Subject: [PATCH] Add user-agent on all fetch requests --- packages/signalk-plugin/src/fetch.ts | 14 ++++++++++++++ packages/signalk-plugin/src/metadata.ts | 1 + packages/signalk-plugin/src/reporters/noaa.ts | 1 + packages/signalk-plugin/src/sources/history.ts | 1 + .../signalk-plugin/test/reporters/noaa.test.ts | 5 ++++- 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 packages/signalk-plugin/src/fetch.ts diff --git a/packages/signalk-plugin/src/fetch.ts b/packages/signalk-plugin/src/fetch.ts new file mode 100644 index 0000000..656f8da --- /dev/null +++ b/packages/signalk-plugin/src/fetch.ts @@ -0,0 +1,14 @@ +import pkg from "../package.json" with { type: "json" }; + +/** + * Fetch wrapper that adds defaults like User-Agent header. + */ +export default function (url: string | URL, init: RequestInit = {}) { + return fetch(url, { + ...init, + headers: { + ...(init.headers || {}), + "User-Agent": `${pkg.name}/${pkg.version} (${pkg.homepage})`, + }, + }); +} diff --git a/packages/signalk-plugin/src/metadata.ts b/packages/signalk-plugin/src/metadata.ts index 1b6a742..7e88152 100644 --- a/packages/signalk-plugin/src/metadata.ts +++ b/packages/signalk-plugin/src/metadata.ts @@ -2,6 +2,7 @@ import { ServerAPI } from "@signalk/server-api"; import { readFile, writeFile } from "fs/promises"; import { join } from "path"; import { BATHY_URL } from "./constants.js"; +import fetch from "./fetch.js"; export type Identity = { uuid: string; diff --git a/packages/signalk-plugin/src/reporters/noaa.ts b/packages/signalk-plugin/src/reporters/noaa.ts index 8c8a046..2292611 100644 --- a/packages/signalk-plugin/src/reporters/noaa.ts +++ b/packages/signalk-plugin/src/reporters/noaa.ts @@ -10,6 +10,7 @@ import { } from "../streams/index.js"; import chain from "stream-chain"; import createDebug from "debug"; +import fetch from "../fetch.js"; const debug = createDebug("crowd-depth:noaa"); diff --git a/packages/signalk-plugin/src/sources/history.ts b/packages/signalk-plugin/src/sources/history.ts index 5e02907..88d8be8 100644 --- a/packages/signalk-plugin/src/sources/history.ts +++ b/packages/signalk-plugin/src/sources/history.ts @@ -13,6 +13,7 @@ import { } from "@signalk/server-api/history"; import { Temporal } from "@js-temporal/polyfill"; import { BATHY_EPOCH } from "../constants.js"; +import fetch from "../fetch.js"; export async function createHistorySource( app: ServerAPI, diff --git a/packages/signalk-plugin/test/reporters/noaa.test.ts b/packages/signalk-plugin/test/reporters/noaa.test.ts index 2cd0d69..9d6922d 100644 --- a/packages/signalk-plugin/test/reporters/noaa.test.ts +++ b/packages/signalk-plugin/test/reporters/noaa.test.ts @@ -46,7 +46,10 @@ const data: BathymetryData[] = [ describe("submitGeoJSON", () => { test("success", async () => { - const scope = nock(BATHY_URL).post("/geojson").reply(200, SUCCESS_RESPONSE); + const scope = nock(BATHY_URL) + .post("/geojson") + .matchHeader("user-agent", /^crowd-depth/) + .reply(200, SUCCESS_RESPONSE); const res = await submitGeoJSON( BATHY_URL, config,