Skip to content

Commit

Permalink
Validate streaming writer chunk type in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 15, 2022
1 parent ec7c911 commit 511ba71
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
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 = (chunk) => {
if (!(chunk instanceof Uint8Array)) {
throw new Error('Writing non Uint8Array chunk in streaming is not allowed')
}
OriginWritableStreamWrite(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

0 comments on commit 511ba71

Please sign in to comment.