-
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 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 |
---|---|---|
|
@@ -85,32 +85,33 @@ function createMultipartStream(entries: readonly MultipartEntry[]): MultipartFor | |
}; | ||
} | ||
|
||
async function detectStreamSupport(): Promise<boolean> { | ||
async function detectStreamSupport() { | ||
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', { | ||
let duplexAccessed = false; | ||
const hasContentType = new Request('https://', { | ||
body: new ReadableStream(), | ||
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', | ||
}); | ||
get duplex() { | ||
duplexAccessed = true; | ||
return 'half'; | ||
}, | ||
}).headers.has('Content-Type'); | ||
|
||
// 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 { | ||
const res = await fetch( | ||
new Request('data:text/plain;charset=utf-8,42', { | ||
method: 'POST', | ||
body: new ReadableStream(), | ||
// @ts-ignore | ||
duplex: 'half', | ||
}), | ||
); | ||
const body = await res.text(); | ||
|
||
return duplexAccessed && !hasContentType && body === '\x00'; | ||
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. The intent with the original check was to verify that the testStream was consumed by checking its text representation. A buffer with Since the 'response' of a Data URI request is the data URI payload itself, would this be a string with '42' in this case and not match the 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'll take this PR for a spin and see if we can adjust it. |
||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
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.