Unverified Commit a3d3016d authored by Max Leiter's avatar Max Leiter Committed by GitHub

next/node-polyfill-web-streams: fix web stream polyfill for Node v16 (#51901)

### What?
Slack thread:
https://vercel.slack.com/archives/C050WU03V3N/p1687889719318819

In Node 16, the ReadableStream is not on the global but instead under
the `stream` package. We should polyfill the global with that
implementation, if available

### Why?
Fixes passing ReadableStream from the Node.js runtime to the vercel/ai
SDK.
parent 73c905dc
// Polyfill Web Streams for the Node.js runtime. // Polyfill Web Streams for the Node.js runtime.
if (!global.ReadableStream) { if (!global.ReadableStream) {
const { ReadableStream } = // In Node v16, ReadableStream is available natively but under the `stream` namespace.
require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') // In Node v18+, it's available under global.
global.ReadableStream = ReadableStream if (require('stream').ReadableStream) {
global.ReadableStream = require('stream').ReadableStream
} else {
const { ReadableStream } =
require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill')
global.ReadableStream = ReadableStream
}
} }
if (!global.TransformStream) { if (!global.TransformStream) {
const { TransformStream } = // Same as ReadableStream above.
require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill') if (require('stream').TransformStream) {
global.TransformStream = TransformStream global.TransformStream = require('stream').TransformStream
} else {
const { TransformStream } =
require('next/dist/compiled/@edge-runtime/ponyfill') as typeof import('next/dist/compiled/@edge-runtime/ponyfill')
global.TransformStream = TransformStream
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment