-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: drop node-fetch dependency (#4957)
- Loading branch information
Showing
19 changed files
with
104 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import type { Headers, RequestInit, Response } from 'node-fetch'; | ||
import nodeFetch from 'node-fetch'; | ||
|
||
import { FetchUrlError } from './FetchError.js'; | ||
import { FetchUrlError, isError } from './FetchError.js'; | ||
|
||
export async function fetchHead(request: string | URL): Promise<Headers> { | ||
const r = await fetch(request, { method: 'HEAD' }); | ||
return r.headers; | ||
const url = toURL(request); | ||
try { | ||
const r = await fetch(url, { method: 'HEAD' }); | ||
return r.headers; | ||
} catch (e) { | ||
console.warn('fetchHead Error %o', e); | ||
if (isError(e)) { | ||
throw FetchUrlError.fromError(url, e); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
export async function fetchURL(url: URL): Promise<Buffer> { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw FetchUrlError.create(url, response.status); | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw FetchUrlError.create(url, response.status); | ||
} | ||
return Buffer.from(await response.arrayBuffer()); | ||
} catch (e) { | ||
// console.warn('fetchURL Error %o', e); | ||
if (e instanceof FetchUrlError) throw e; | ||
if (isError(e)) { | ||
throw FetchUrlError.fromError(url, e); | ||
} | ||
throw e; | ||
} | ||
return Buffer.from(await response.arrayBuffer()); | ||
} | ||
|
||
export function fetch(url: string | URL, init?: RequestInit): Promise<Response> { | ||
/// This is a n issue with how TypeScript handles packages without `type` being set. | ||
// @ts-ignore | ||
return nodeFetch(url, init); | ||
function toURL(url: string | URL): URL { | ||
return typeof url === 'string' ? new URL(url) : url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.