Skip to content
Merged
Changes from all commits
Commits
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
26 changes: 25 additions & 1 deletion packages/provider-hypihub/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFileSync } from "node:fs";
import { requestDeadline } from "@hypit/runtime-kit";
import type { AsyncEndpoint, EndpointCredential, EndpointFulfillment, EndpointInvocationContext, EndpointPollContext, EndpointPricingReader, EndpointStartContext, EndpointOutcome, ImmediateEndpointHandler } from "@hypit/endpoint-kit";
import { defineEndpointPackage, wakeAfter } from "@hypit/endpoint-kit";
Expand All @@ -21,6 +22,29 @@ import type { RuntimeDoctorDiagnostic } from "@hypit/runtime-kit";
import { createHypiHubAuth, hypiHubCredentialNeedsRefresh } from "./oauth.js";
import type { HypiHubAuth } from "./oauth.js";

function distributionVersion(): string {
try {
const manifest = JSON.parse(readFileSync(new URL("../../../package.json", import.meta.url), "utf8")) as {
readonly name?: unknown; readonly version?: unknown;
};
if (manifest.name !== "@hypit/hypit" || typeof manifest.version !== "string" || manifest.version.length === 0) {
return "unknown";
}
return manifest.version;
} catch {
return "unknown";
}
}

const userAgent = `hypit/${distributionVersion()}`;

const identifiedFetch = (fetcher: typeof globalThis.fetch): typeof globalThis.fetch =>
async (input, init) => {
const headers: Record<string, string> = { "user-agent": userAgent };
new Headers(init?.headers).forEach((value, name) => { headers[name] = value; });
return await fetcher(input, { ...init, headers });
};

export const hypiHubProviderModuleRef = { name: "@hypit/provider-hypihub", version: "1" } as const;

export type CreateHypiHubProviderOptions = {
Expand Down Expand Up @@ -118,7 +142,7 @@ class HypiHubClient {
this.timeout = options.timeout;
this.oauthTimeout = options.oauthTimeout;
this.downloadAttempts = options.downloadAttempts;
this.fetcher = options.fetcher;
this.fetcher = identifiedFetch(options.fetcher);
this.uploader = new HypiHubUploader({
baseUrl: this.baseUrl,
requestTimeoutMs: this.timeout,
Expand Down
Loading