-
Notifications
You must be signed in to change notification settings - Fork 2
Fix readable stream as body check on safari #12
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -85,34 +85,20 @@ function createMultipartStream(entries: readonly MultipartEntry[]): MultipartFor | |
}; | ||
} | ||
|
||
async function detectStreamSupport(): Promise<boolean> { | ||
try { | ||
const testStream = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(new Uint8Array([0])); // Minimal stream chunk | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
const request = new Request('data:text/plain;charset=utf-8,42', { | ||
method: 'POST', | ||
body: testStream, | ||
// Required for streaming request body in some browsers, | ||
// or it will fail and assume it's not supported | ||
// @ts-ignore | ||
duplex: 'half', | ||
}); | ||
|
||
// If fails to handle fetch(Request), it's likely not a native implementation of | ||
// Fetch API, so it's not supported. | ||
await fetch(request.clone()); | ||
|
||
const body = await request.text(); | ||
// if different from '\x00', it's likely not supported | ||
return body === '\x00'; | ||
} catch { | ||
return false; | ||
} | ||
function detectStreamSupport() { | ||
let duplexAccessed = false; | ||
|
||
const hasContentType = new Request('data:text/plain;charset=utf-8,42', { | ||
|
||
body: new ReadableStream(), | ||
method: 'POST', | ||
// @ts-ignore | ||
get duplex() { | ||
duplexAccessed = true; | ||
return 'half'; | ||
}, | ||
}).headers.has('Content-Type'); | ||
|
||
return duplexAccessed && !hasContentType; | ||
} | ||
|
||
function createFormData(entries: readonly MultipartEntry[]): FormData { | ||
|
@@ -163,7 +149,7 @@ export async function createMultipartRequestInit( | |
method: 'POST' | 'PUT', | ||
entries: readonly MultipartEntry[], | ||
): Promise<RequestInit> { | ||
if (await detectStreamSupport()) { | ||
if (detectStreamSupport()) { | ||
const { stream, boundary } = createMultipartStream(entries); | ||
|
||
return { | ||
|
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.
Hey @bugii , thank you for contributing with this great alternative.
There's 2 usages we are trying to assess here, one is browsers and one is Node.js with patched or non-native Fetch API implementations.
This line specifically designed to fail with patched
fetch
that do not supportfetch(Request)
and instead just assumesfetch(uri, options)
. We have seen this in few circumstances one of which was the ElizaOS monorepo.Uh oh!
There was an error while loading. Please reload this page.
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.
Hey @cesarenaldi, thank you for the review! Was late at night and I overlooked that code somehow :D
I added the fetch call again.