-
Notifications
You must be signed in to change notification settings - Fork 922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch from node-fetch
to undici
#402
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,18 @@ | ||
/** | ||
* Disclaimer: modules in _shims aren't intended to be imported by SDK users. | ||
*/ | ||
import * as nf from 'node-fetch'; | ||
import * as fd from 'formdata-node'; | ||
import { type File, type FilePropertyBag } from 'formdata-node'; | ||
import KeepAliveAgent from 'agentkeepalive'; | ||
import { AbortController as AbortControllerPolyfill } from 'abort-controller'; | ||
import { ReadStream as FsReadStream } from 'node:fs'; | ||
import { type Agent } from 'node:http'; | ||
import uf from 'undici'; | ||
import type { File, Agent, FormData } from 'undici'; | ||
import type { FilePropertyBag } from 'formdata-node'; | ||
import { FormDataEncoder } from 'form-data-encoder'; | ||
import { ReadStream as FsReadStream } from 'node:fs'; | ||
import { Readable } from 'node:stream'; | ||
import { ReadableStream } from 'node:stream/web'; | ||
import { Blob } from 'node:buffer'; | ||
import { type RequestOptions } from '../core'; | ||
import { MultipartBody } from './MultipartBody'; | ||
import { type Shims } from './registry'; | ||
|
||
// @ts-ignore (this package does not have proper export maps for this export) | ||
import { ReadableStream } from 'web-streams-polyfill/dist/ponyfill.es2018.js'; | ||
|
||
type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>; | ||
|
||
let fileFromPathWarned = false; | ||
|
@@ -40,11 +36,11 @@ async function fileFromPath(path: string, ...args: any[]): Promise<File> { | |
return await _fileFromPath(path, ...args); | ||
} | ||
|
||
const defaultHttpAgent: Agent = new KeepAliveAgent({ keepAlive: true, timeout: 5 * 60 * 1000 }); | ||
const defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({ keepAlive: true, timeout: 5 * 60 * 1000 }); | ||
const defaultHttpAgent = new uf.Agent({ keepAliveTimeout: 5 * 60 * 1000 }); | ||
const defaultHttpsAgent = new uf.Agent({ keepAliveTimeout: 5 * 60 * 1000 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary to get reasonable (note we also have weird timeout shenanigans elsewhere to bump the agent's timeout so that a long timeout for a given request isn't cut short by the agent's timeout; not sure whether that'd still be necessary or would need adjustment.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd have to go look at the default agent timeout to be sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: undici will honor the keep-alive hint from the server. |
||
|
||
async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>( | ||
form: fd.FormData, | ||
form: FormData, | ||
opts: RequestOptions<T>, | ||
): Promise<RequestOptions<T>> { | ||
const encoder = new FormDataEncoder(form); | ||
|
@@ -67,13 +63,13 @@ export function getRuntime(): Shims { | |
} | ||
return { | ||
kind: 'node', | ||
fetch: nf.default, | ||
Request: nf.Request, | ||
Response: nf.Response, | ||
Headers: nf.Headers, | ||
FormData: fd.FormData, | ||
Blob: fd.Blob, | ||
File: fd.File, | ||
fetch: uf.fetch, | ||
Request: uf.Request, | ||
Response: uf.Response, | ||
Headers: uf.Headers, | ||
FormData: uf.FormData, | ||
Blob: Blob, | ||
File: uf.File, | ||
ReadableStream, | ||
getMultipartRequestOptions, | ||
getDefaultAgent: (url: string): Agent => (url.startsWith('https') ? defaultHttpsAgent : defaultHttpAgent), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | Uint8Arra | |
export type Uploadable = FileLike | ResponseLike | FsReadStream; | ||
|
||
/** | ||
* Intended to match web.Blob, node.Blob, node-fetch.Blob, etc. | ||
* Intended to match web.Blob, node.Blob, undici.Blob, etc. | ||
*/ | ||
export interface BlobLike { | ||
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */ | ||
|
@@ -125,7 +125,7 @@ export async function toFile( | |
} | ||
} | ||
|
||
return new File(bits, name, options); | ||
return new File(bits as (string | Blob | NodeJS.ArrayBufferView)[], name, options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't for the life of me figure out how to get this type to work. Help appreciated! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. heh, I believe it, can try to take a look soon… likely thursday… There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Ethan-Arrowood I think |
||
} | ||
|
||
async function getBytes(value: ToFileInput): Promise<Array<BlobPart>> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment probably needs updating