rsnext/test/__mocks__/node-polyfill-web-streams.js
Javi Velasco 14463ddd10
Update Edge Runtime (#38862)
This PR updates the Edge Runtime to use a new version that loads dependencies differently. This addresses https://github.com/vercel/next.js/pull/38766 so `instanceof` works as expected.

It involved a few code changes, mostly regarding to types. The most important change is that the `Runner` function in the sandbox doesn't take a `ReadableStream` as `body` anymore since this implies creating the instance on "node land" and makes the runtime `fetch` function not to be able to compare with `ReadableStream` using `instanceof`.  Instead we introduce a "clonable body" abstraction that allows to create the `ReadableStream` from `Readable` by using the edge runtime primitive which would hold the correct prototype.

Also, this PR changes the way we pre-compile the Edge Runtime to adapt it to the new version.
2022-07-21 18:29:19 +00:00

19 lines
698 B
JavaScript

const {
ReadableStream,
TransformStream,
WritableStreamDefaultWriter,
} = require('next/dist/compiled/@edge-runtime/primitives/streams')
const OriginWritableStreamWrite = WritableStreamDefaultWriter.prototype.write
// Override writable stream write method to validate chunk type.
// Currently CF workers only allow to write the encoded chunk in Uint8Array format.
WritableStreamDefaultWriter.prototype.write = function (chunk) {
if (!(chunk instanceof Uint8Array)) {
throw new Error('Writing non-Uint8Array chunks in a stream is not allowed.')
}
return OriginWritableStreamWrite.call(this, chunk)
}
global.ReadableStream = ReadableStream
global.TransformStream = TransformStream