Skip to content
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

Validate streaming writer chunk type in testing #36200

Merged
merged 3 commits into from Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/__mocks__/node-polyfill-web-streams.js
@@ -0,0 +1,21 @@
// 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 chunk in streaming is not allowed')
huozhi marked this conversation as resolved.
Show resolved Hide resolved
}
return OriginWritableStreamWrite.call(this, chunk)
}

global.ReadableStream = ReadableStream
global.TransformStream = TransformStream
24 changes: 24 additions & 0 deletions test/lib/mocks-require-hook.js
@@ -0,0 +1,24 @@
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) // hookPropertyMap.get(request)
if (hookResolved) request = hookResolved
return resolveFilename.call(mod, request, parent, isMain, options)
}
26 changes: 23 additions & 3 deletions test/lib/next-test-utils.js
Expand Up @@ -31,7 +31,13 @@ export function initNextServerScript(
return new Promise((resolve, reject) => {
const instance = spawn(
'node',
[...((opts && opts.nodeArgs) || []), '--no-deprecation', scriptPath],
[
...((opts && opts.nodeArgs) || []),
'-r',
require.resolve('./mocks-require-hook'),
'--no-deprecation',
scriptPath,
],
{
env,
cwd: opts && opts.cwd,
Expand Down Expand Up @@ -147,7 +153,14 @@ export function runNextCommand(argv, options = {}) {
console.log(`Running command "next ${argv.join(' ')}"`)
const instance = spawn(
'node',
[...(options.nodeArgs || []), '--no-deprecation', nextBin, ...argv],
[
...(options.nodeArgs || []),
'-r',
require.resolve('./mocks-require-hook'),
'--no-deprecation',
nextBin,
...argv,
],
{
...options.spawnOptions,
cwd,
Expand Down Expand Up @@ -237,7 +250,14 @@ export function runNextCommandDev(argv, stdOut, opts = {}) {
return new Promise((resolve, reject) => {
const instance = spawn(
'node',
[...nodeArgs, '--no-deprecation', nextBin, ...argv],
[
...nodeArgs,
'-r',
require.resolve('./mocks-require-hook'),
'--no-deprecation',
nextBin,
...argv,
],
{
cwd,
env,
Expand Down