-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Piping res.body
to TransformStream throws an error
#6635
Comments
I found that when I polyfill import { installGlobals } from '@remix-run/node'
import { TransformStream } from '@remix-run/web-stream'
installGlobals()
global.TransformStream = TransformStream But when I patch the source code directly, it works: diff --git a/node_modules/ai/dist/index.js b/node_modules/ai/dist/index.js
index 46dcb84..b608338 100644
--- a/node_modules/ai/dist/index.js
+++ b/node_modules/ai/dist/index.js
@@ -70,6 +70,8 @@ __export(streams_exports, {
});
module.exports = __toCommonJS(streams_exports);
+const { Response } = require("@remix-run/node");
+const { TransformStream, ReadableStream } = require("@remix-run/web-stream");
// streams/ai-stream.ts
var import_eventsource_parser = require("eventsource-parser");
function createEventStreamTransformer(customParser) { Note I also have to import Response and ReadableStream explicitly from Remix packages, otherwise, it doesn't work while deployed in the Vercel environment. |
res.body
to TransformStream throws an error
Any updates? Still facing this when using Remix with Vercel AI SDK 3.1 |
I have encountered the same problem. As mentioned, this is an issue with the polyfill. const { fetch } = require("@remix-run/web-fetch");
(async () => {
const res = await fetch("https://remix.run");
res.body.pipeThrough(new TextDecoderStream()).getReader()
// this throws:
// Uncaught TypeError: First parameter has member 'readable' that is not a ReadableStream.
})();
If you are using Node 20 or later as a runtime, you can avoid this issue by using the native fetch. There are several issues that I suspect might be caused by this problem:
According to these comments, the fetch polyfill will likely be discontinued in the next major version of Remix. |
@pokutuna I tried your solution, |
@wong2
Although it is unstable, you can avoid this issue by enabling single fetch. Since single fetch is being migrated to turbo-stream, it should resolve issues related to web-fetch and web-streams-polyfill. |
@amaany3 Thanks! I also found after enable single fetch it works! |
Sorry for the confusion. I was talking about transforming streams. Thank you for the follow-up. @amaany3 |
What version of Remix are you using?
Latest
Are all your remix dependencies & dev-dependencies using the same version?
Steps to Reproduce
res.body
intoTransformStream
Expected Behavior
Fetching and piping through the stream to TransformStream works without issues.
Actual Behavior
You will receive an error from Web polyfill used by Remix that "first parameter" (in this case, TransformStream) has a readable property that is not a ReadableStream.
This is because TransformStream is not polyfilled along ReadableStream/WritableStream, as a result, native Node implementation is loaded.
When there are both polyfill and native streams circling around, this can lead to issues, e.g: MattiasBuelens/web-streams-polyfill#93 (comment)
The text was updated successfully, but these errors were encountered: