Unverified Commit b6017fac authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Handle edge runtime in places that import node:stream (#92354)

## What?

The codepath would cause checks to fail during deployment because it
tries to import `node:stream` even when not used. This adds a specific
path for edge runtime to avoid that.

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent 15d9b4d7
...@@ -1145,5 +1145,8 @@ ...@@ -1145,5 +1145,8 @@
"1144": "Prefetch inlining is enabled but no hints were found for route \"%s\". This is a bug in the Next.js build pipeline — prefetch-hints.json should contain an entry for every route that produces segment data.", "1144": "Prefetch inlining is enabled but no hints were found for route \"%s\". This is a bug in the Next.js build pipeline — prefetch-hints.json should contain an entry for every route that produces segment data.",
"1145": "Internal Next.js Error: Prefetch inlining is enabled but no hints were found for route \"%s\". prefetch-hints.json should contain an entry for every route that produces segment data. This is a bug in Next.js.", "1145": "Internal Next.js Error: Prefetch inlining is enabled but no hints were found for route \"%s\". prefetch-hints.json should contain an entry for every route that produces segment data. This is a bug in Next.js.",
"1146": "Internal Next.js Error: Prefetch inlining is enabled but no hint tree was provided during incremental static revalidation. The prefetch-hints.json manifest should contain an entry for this route. This is a bug in Next.js.", "1146": "Internal Next.js Error: Prefetch inlining is enabled but no hint tree was provided during incremental static revalidation. The prefetch-hints.json manifest should contain an entry for this route. This is a bug in Next.js.",
"1147": "Turbopack database compaction is not supported on this platform" "1147": "Turbopack database compaction is not supported on this platform",
"1148": "webToReadable cannot be used in the edge runtime",
"1149": "Node.js Readable cannot be teed in the edge runtime",
"1150": "Node.js Readable cannot be converted to a web stream in the edge runtime"
} }
...@@ -30,21 +30,29 @@ export class ReactServerResult { ...@@ -30,21 +30,29 @@ export class ReactServerResult {
return tee[1] return tee[1]
} }
let Readable: typeof import('node:stream').Readable if (process.env.NEXT_RUNTIME === 'edge') {
if (process.env.TURBOPACK) { throw new InvariantError(
Readable = (require('node:stream') as typeof import('node:stream')) 'Node.js Readable cannot be teed in the edge runtime'
.Readable )
} else { } else {
Readable = ( let Readable: typeof import('node:stream').Readable
__non_webpack_require__('node:stream') as typeof import('node:stream') if (process.env.TURBOPACK) {
).Readable Readable = (require('node:stream') as typeof import('node:stream'))
.Readable
} else {
Readable = (
__non_webpack_require__('node:stream') as typeof import('node:stream')
).Readable
}
const webStream = Readable.toWeb(
this._stream
) as ReadableStream<Uint8Array>
const tee = webStream.tee()
this._stream = Readable.fromWeb(
tee[0] as import('stream/web').ReadableStream
)
return Readable.fromWeb(tee[1] as import('stream/web').ReadableStream)
} }
const webStream = Readable.toWeb(this._stream) as ReadableStream<Uint8Array>
const tee = webStream.tee()
this._stream = Readable.fromWeb(
tee[0] as import('stream/web').ReadableStream
)
return Readable.fromWeb(tee[1] as import('stream/web').ReadableStream)
} }
consume(): AnyStream { consume(): AnyStream {
......
...@@ -248,16 +248,24 @@ export default class RenderResult< ...@@ -248,16 +248,24 @@ export default class RenderResult<
} }
if (isNodeReadable(this.response)) { if (isNodeReadable(this.response)) {
let Readable: typeof import('node:stream').Readable if (process.env.NEXT_RUNTIME === 'edge') {
if (process.env.TURBOPACK) { throw new InvariantError(
Readable = (require('node:stream') as typeof import('node:stream')) 'Node.js Readable cannot be converted to a web stream in the edge runtime'
.Readable )
} else { } else {
Readable = ( let Readable: typeof import('node:stream').Readable
__non_webpack_require__('node:stream') as typeof import('node:stream') if (process.env.TURBOPACK) {
).Readable Readable = (require('node:stream') as typeof import('node:stream'))
.Readable
} else {
Readable = (
__non_webpack_require__(
'node:stream'
) as typeof import('node:stream')
).Readable
}
return Readable.toWeb(this.response) as ReadableStream<Uint8Array>
} }
return Readable.toWeb(this.response) as ReadableStream<Uint8Array>
} }
return this.response return this.response
...@@ -283,16 +291,24 @@ export default class RenderResult< ...@@ -283,16 +291,24 @@ export default class RenderResult<
} else if (Buffer.isBuffer(this.response)) { } else if (Buffer.isBuffer(this.response)) {
return [streamFromBuffer(this.response)] return [streamFromBuffer(this.response)]
} else if (isNodeReadable(this.response)) { } else if (isNodeReadable(this.response)) {
let Readable: typeof import('node:stream').Readable if (process.env.NEXT_RUNTIME === 'edge') {
if (process.env.TURBOPACK) { throw new InvariantError(
Readable = (require('node:stream') as typeof import('node:stream')) 'Node.js Readable cannot be converted to a web stream in the edge runtime'
.Readable )
} else { } else {
Readable = ( let Readable: typeof import('node:stream').Readable
__non_webpack_require__('node:stream') as typeof import('node:stream') if (process.env.TURBOPACK) {
).Readable Readable = (require('node:stream') as typeof import('node:stream'))
.Readable
} else {
Readable = (
__non_webpack_require__(
'node:stream'
) as typeof import('node:stream')
).Readable
}
return [Readable.toWeb(this.response) as ReadableStream<Uint8Array>]
} }
return [Readable.toWeb(this.response) as ReadableStream<Uint8Array>]
} else { } else {
return [this.response] return [this.response]
} }
......
...@@ -135,20 +135,26 @@ export async function webstreamToUint8Array( ...@@ -135,20 +135,26 @@ export async function webstreamToUint8Array(
function webToReadable( function webToReadable(
stream: ReadableStream<Uint8Array> | import('node:stream').Readable stream: ReadableStream<Uint8Array> | import('node:stream').Readable
): import('node:stream').Readable { ): import('node:stream').Readable {
let Readable: typeof import('node:stream').Readable if (process.env.NEXT_RUNTIME === 'edge') {
if (process.env.TURBOPACK) { throw new Error('webToReadable cannot be used in the edge runtime')
Readable = (require('node:stream') as typeof import('node:stream')).Readable
} else if (process.env.__NEXT_BUNDLER === 'Webpack') {
Readable = (
__non_webpack_require__('node:stream') as typeof import('node:stream')
).Readable
} else { } else {
Readable = (require('node:stream') as typeof import('node:stream')).Readable let Readable: typeof import('node:stream').Readable
} if (process.env.TURBOPACK) {
if (stream instanceof Readable) { Readable = (require('node:stream') as typeof import('node:stream'))
return stream .Readable
} else if (process.env.__NEXT_BUNDLER === 'Webpack') {
Readable = (
__non_webpack_require__('node:stream') as typeof import('node:stream')
).Readable
} else {
Readable = (require('node:stream') as typeof import('node:stream'))
.Readable
}
if (stream instanceof Readable) {
return stream
}
return Readable.fromWeb(stream as import('stream/web').ReadableStream)
} }
return Readable.fromWeb(stream as import('stream/web').ReadableStream)
} }
export async function nodestreamToUint8Array( export async function nodestreamToUint8Array(
...@@ -162,20 +168,29 @@ export async function nodestreamToUint8Array( ...@@ -162,20 +168,29 @@ export async function nodestreamToUint8Array(
} }
export async function streamToUint8Array(stream: AnyStream) { export async function streamToUint8Array(stream: AnyStream) {
let Readable: typeof import('node:stream').Readable if (process.env.NEXT_RUNTIME === 'edge') {
if (process.env.TURBOPACK) { // Edge runtime always uses web streams
Readable = (require('node:stream') as typeof import('node:stream')).Readable return webstreamToUint8Array(stream as ReadableStream<Uint8Array>)
} else if (process.env.__NEXT_BUNDLER === 'Webpack') {
Readable = (
__non_webpack_require__('node:stream') as typeof import('node:stream')
).Readable
} else { } else {
Readable = (require('node:stream') as typeof import('node:stream')).Readable let Readable: typeof import('node:stream').Readable
} if (process.env.TURBOPACK) {
if (stream instanceof Readable) { Readable = (require('node:stream') as typeof import('node:stream'))
return nodestreamToUint8Array(stream) .Readable
} else if (process.env.__NEXT_BUNDLER === 'Webpack') {
Readable = (
__non_webpack_require__('node:stream') as typeof import('node:stream')
).Readable
} else {
Readable = (require('node:stream') as typeof import('node:stream'))
.Readable
}
if (stream instanceof Readable) {
return nodestreamToUint8Array(stream)
}
return webstreamToUint8Array(stream)
} }
return webstreamToUint8Array(stream)
} }
export async function streamToBuffer( export async function streamToBuffer(
......
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