Skip to content

Commit

Permalink
Add user-agent header (pkgxdev#428)
Browse files Browse the repository at this point in the history
* add user-agent header

* tweak product id in UA header

* change user agent to tea.cli
  • Loading branch information
ABevier authored and mxcl committed Mar 14, 2023
1 parent 4640db5 commit a0ec394
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useDownload from "./useDownload.ts"
import useCache from "./useCache.ts"
import useCellar from "./useCellar.ts"
import useExec from "./useExec.ts"
import useFetch from "./useFetch.ts"
import useFlags from "./useFlags.ts"
import useGitHubAPI from "./useGitHubAPI.ts"
import useInventory from "./useInventory.ts"
Expand All @@ -26,6 +27,7 @@ export {
useDownload,
useErrorHandler,
useExec,
useFetch,
useFlags,
useGitHubAPI,
useInventory,
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useDownload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { crypto, toHashString } from "deno/crypto/mod.ts"
import { Logger, teal, gray } from "./useLogger.ts"
import { chuzzle, error, TeaError } from "utils"
import { useFlags, usePrefix } from "hooks"
import { useFlags, usePrefix, useFetch } from "hooks"
import { isString } from "is_what"
import Path from "path"

Expand Down Expand Up @@ -54,7 +54,7 @@ async function internal<T>({ src, headers, logger, dst }: DownloadOptions): Prom
}
}

const rsp = await fetch(src, {headers})
const rsp = await useFetch(src, { headers })

switch (rsp.status) {
case 200: {
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useVersion } from "hooks";

// useFetch wraps the native Deno fetch api and inserts a User-Agent header
export default function useFetch(input: string | URL | Request, init?: RequestInit | undefined): Promise<Response> {
const requestInit = init ?? {} as RequestInit
requestInit.headers = { ...requestInit.headers, "User-Agent": `tea.cli/${useVersion()}` }
return fetch(input, requestInit)
}
3 changes: 2 additions & 1 deletion src/hooks/useGitHubAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GET2, undent, validate_arr, validate_str } from "utils"
import { isArray } from "is_what"
import { useFetch } from "hooks";

//TODO pagination

Expand Down Expand Up @@ -66,7 +67,7 @@ async function *getVersions({ user, repo, type }: GetVersionsOptions): AsyncGene
}
}
}`
const rsp = await fetch('https://api.github.com/graphql', {
const rsp = await useFetch('https://api.github.com/graphql', {
method: 'POST',
body: JSON.stringify({ query }),
headers
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useInventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Package, PackageRequirement } from "types"
import { host, error, TeaError } from "utils"
import SemVer from "semver"
import Path from "../vendor/Path.ts"
import { useFetch } from "hooks";

export interface Inventory {
[project: string]: {
Expand Down Expand Up @@ -29,7 +30,7 @@ const get = async (rq: PackageRequirement | Package) => {
const url = new URL('https://dist.tea.xyz')
url.pathname = Path.root.join(rq.project, platform, arch, 'versions.txt').string

const rsp = await fetch(url)
const rsp = await useFetch(url)

if (!rsp.ok) {
const cause = new Error(`${rsp.status}: ${url}`)
Expand Down
4 changes: 2 additions & 2 deletions src/prefab/install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { usePrefix, useCache, useCellar, useFlags, useDownload, useOffLicense } from "hooks"
import { usePrefix, useCache, useCellar, useFlags, useDownload, useOffLicense, useFetch } from "hooks"
import { host, panic, pkg as pkgutils } from "utils"
import { Logger, red, teal, gray } from "hooks/useLogger.ts"
import { Installation, StowageNativeBottle } from "types"
Expand Down Expand Up @@ -105,7 +105,7 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
}

async function remote_SHA(url: URL) {
const rsp = await fetch(url)
const rsp = await useFetch(url)
if (!rsp.ok) throw rsp
const txt = await rsp.text()
return txt.split(' ')[0]
Expand Down

0 comments on commit a0ec394

Please sign in to comment.