Unverified Commit 2e00eb67 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Validate streaming writer chunk type in testing (#36200)

* Validate streaming writer chunk type in testing

* call

* update text
parent d895a50a
// Use CJS format for represent next/dist/compiled/node-polyfill-web-streams.js
const {
WritableStreamDefaultWriter,
ReadableStream,
TransformStream,
} = require('next/dist/compiled/web-streams-polyfill')
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
const mod = require('module')
const hookPropertyMap = new Map([
[
/node-polyfill-web-streams/,
require.resolve('../__mocks__/node-polyfill-web-streams.js'),
],
])
function matchModule(request) {
for (const [key, value] of hookPropertyMap) {
if (key.test(request)) {
return value
}
}
return null
}
const resolveFilename = mod._resolveFilename
mod._resolveFilename = function (request, parent, isMain, options) {
const hookResolved = matchModule(request)
if (hookResolved) request = hookResolved
return resolveFilename.call(mod, request, parent, isMain, options)
}
...@@ -31,7 +31,13 @@ export function initNextServerScript( ...@@ -31,7 +31,13 @@ export function initNextServerScript(
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const instance = spawn( const instance = spawn(
'node', 'node',
[...((opts && opts.nodeArgs) || []), '--no-deprecation', scriptPath], [
...((opts && opts.nodeArgs) || []),
'-r',
require.resolve('./mocks-require-hook'),
'--no-deprecation',
scriptPath,
],
{ {
env, env,
cwd: opts && opts.cwd, cwd: opts && opts.cwd,
...@@ -147,7 +153,14 @@ export function runNextCommand(argv, options = {}) { ...@@ -147,7 +153,14 @@ export function runNextCommand(argv, options = {}) {
console.log(`Running command "next ${argv.join(' ')}"`) console.log(`Running command "next ${argv.join(' ')}"`)
const instance = spawn( const instance = spawn(
'node', 'node',
[...(options.nodeArgs || []), '--no-deprecation', nextBin, ...argv], [
...(options.nodeArgs || []),
'-r',
require.resolve('./mocks-require-hook'),
'--no-deprecation',
nextBin,
...argv,
],
{ {
...options.spawnOptions, ...options.spawnOptions,
cwd, cwd,
...@@ -237,7 +250,14 @@ export function runNextCommandDev(argv, stdOut, opts = {}) { ...@@ -237,7 +250,14 @@ export function runNextCommandDev(argv, stdOut, opts = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const instance = spawn( const instance = spawn(
'node', 'node',
[...nodeArgs, '--no-deprecation', nextBin, ...argv], [
...nodeArgs,
'-r',
require.resolve('./mocks-require-hook'),
'--no-deprecation',
nextBin,
...argv,
],
{ {
cwd, cwd,
env, env,
......
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