-
Notifications
You must be signed in to change notification settings - Fork 22
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
feat: dont use ipfs-utils fetch with upload progress by default #1240
Changes from all commits
1f9189f
e6cb47f
6d78127
e845eb9
403356d
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import ipfsUtilsFetch from 'ipfs-utils/src/http/fetch.js' | ||
|
||
/** | ||
* @type {import('./types.js').FetchWithUploadProgress} | ||
*/ | ||
export const fetchWithUploadProgress = (url, init) => { | ||
return ipfsUtilsFetch.fetch(url, init) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,6 @@ import { SpaceDID } from '@web3-storage/capabilities/utils' | |
import retry, { AbortError } from 'p-retry' | ||
import { servicePrincipal, connection } from './service.js' | ||
import { REQUEST_RETRIES } from './constants.js' | ||
import fetchPkg from 'ipfs-utils/src/http/fetch.js' | ||
const { fetch } = fetchPkg | ||
|
||
/** | ||
* | ||
|
@@ -90,10 +88,9 @@ export async function add( | |
const responseAddUpload = result.out.ok | ||
|
||
const fetchWithUploadProgress = | ||
/** @type {(url: string, init?: import('./types.js').FetchOptions) => Promise<Response>} */ ( | ||
fetch | ||
) | ||
options.fetchWithUploadProgress || options.fetch || globalThis.fetch | ||
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. Needs 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. Just call it |
||
|
||
let fetchDidCallUploadProgressCb = false | ||
const res = await retry( | ||
async () => { | ||
try { | ||
|
@@ -103,12 +100,14 @@ export async function add( | |
body: car, | ||
headers: responseAddUpload.headers, | ||
signal: options.signal, | ||
onUploadProgress: options.onUploadProgress | ||
? createUploadProgressHandler( | ||
onUploadProgress: (status) => { | ||
fetchDidCallUploadProgressCb = true | ||
if (options.onUploadProgress) | ||
createUploadProgressHandler( | ||
responseAddUpload.url, | ||
options.onUploadProgress | ||
) | ||
: undefined, | ||
)(status) | ||
}, | ||
// @ts-expect-error - this is needed by recent versions of node - see https://github.com/bluesky-social/atproto/pull/470 for more info | ||
duplex: 'half', | ||
}) | ||
|
@@ -128,6 +127,16 @@ export async function add( | |
} | ||
) | ||
|
||
if (!fetchDidCallUploadProgressCb && options.onUploadProgress) { | ||
// the fetch implementation didn't support onUploadProgress | ||
const carBlob = new Blob([car]) | ||
options.onUploadProgress({ | ||
total: carBlob.size, | ||
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. are 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. yep! the only progress even for this car file, and all we know here is the fetch succeeded so it's 100% progress all at once |
||
loaded: carBlob.size, | ||
lengthComputable: false, | ||
}) | ||
} | ||
|
||
if (!res.ok) { | ||
throw new Error(`upload failed: ${res.status}`) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { | ||
FetchOptions, | ||
FetchOptions as IpfsUtilsFetchOptions, | ||
ProgressStatus as XHRProgressStatus, | ||
} from 'ipfs-utils/src/types.js' | ||
import { Link, UnknownLink, Version } from 'multiformats/link' | ||
|
@@ -45,6 +45,16 @@ import { | |
UsageReportFailure, | ||
} from '@web3-storage/capabilities/types' | ||
|
||
type Override<T, R> = Omit<T, keyof R> & R | ||
|
||
type FetchOptions = Override< | ||
IpfsUtilsFetchOptions, | ||
{ | ||
// `fetch` is a browser API and browsers don't have `Readable` | ||
body: Exclude<IpfsUtilsFetchOptions['body'], import('node:stream').Readable> | ||
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. What does this do? 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. it excludes |
||
} | ||
> | ||
|
||
export type { | ||
FetchOptions, | ||
StoreAdd, | ||
|
@@ -186,7 +196,13 @@ export interface Connectable { | |
connection?: ConnectionView<Service> | ||
} | ||
|
||
export type FetchWithUploadProgress = ( | ||
url: string, | ||
init?: FetchOptions | ||
) => Promise<Response> | ||
|
||
export interface UploadProgressTrackable { | ||
fetchWithUploadProgress?: FetchWithUploadProgress | ||
onUploadProgress?: ProgressFn | ||
} | ||
|
||
|
@@ -210,7 +226,9 @@ export interface RequestOptions | |
extends Retryable, | ||
Abortable, | ||
Connectable, | ||
UploadProgressTrackable {} | ||
UploadProgressTrackable { | ||
fetch?: typeof globalThis.fetch | ||
} | ||
|
||
export interface ListRequestOptions extends RequestOptions, Pageable {} | ||
|
||
|
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.
🙏 Pull out as a separate library that solves the issue or send a PR.