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.
86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
import { createNext } from 'e2e-utils'
|
|
import { NextInstance } from 'test/lib/next-modes/base'
|
|
import { sandbox } from '../acceptance/helpers'
|
|
|
|
const middlewarePath = 'middleware.js'
|
|
const middlewareWarning = `A middleware can not alter response's body`
|
|
|
|
describe('middlewares', () => {
|
|
let next: NextInstance
|
|
let cleanup
|
|
|
|
beforeAll(async () => {
|
|
next = await createNext({
|
|
files: {},
|
|
skipStart: true,
|
|
})
|
|
})
|
|
|
|
afterAll(() => next.destroy())
|
|
|
|
afterEach(() => cleanup?.())
|
|
|
|
it.each([
|
|
{
|
|
title: 'returning response with literal string',
|
|
code: `export default function middleware() {
|
|
return new Response('this is not allowed');
|
|
}`,
|
|
},
|
|
{
|
|
title: 'returning response with literal number',
|
|
code: `export default function middleware() {
|
|
return new Response(10);
|
|
}`,
|
|
},
|
|
{
|
|
title: 'returning response with JSON.stringify',
|
|
code: `export default function middleware() {
|
|
return new Response(JSON.stringify({ foo: 'this is not allowed' }));
|
|
}`,
|
|
},
|
|
{
|
|
title: 'populating response with a value',
|
|
code: `export default function middleware(request) {
|
|
const body = JSON.stringify({ foo: 'this should not be allowed, but hard to detect with AST' })
|
|
return new Response(body);
|
|
}`,
|
|
},
|
|
{
|
|
title: 'populating response with a function call',
|
|
code: `function buildBody() {
|
|
return 'this should not be allowed, but hard to detect with AST'
|
|
}
|
|
export default function middleware(request) {
|
|
return new Response(buildBody());
|
|
}`,
|
|
},
|
|
{
|
|
title: 'populating response with an async function call',
|
|
code: `export default async function middleware(request) {
|
|
return new Response(await fetch('https://example.vercel.sh'));
|
|
}`,
|
|
},
|
|
])('warns when $title', async ({ code }) => {
|
|
;({ cleanup } = await sandbox(next, new Map([[middlewarePath, code]])))
|
|
expect(next.cliOutput).toMatch(middlewareWarning)
|
|
})
|
|
|
|
it.each([
|
|
{
|
|
title: 'returning null reponse body',
|
|
code: `export default function middleware() {
|
|
return new Response(null);
|
|
}`,
|
|
},
|
|
{
|
|
title: 'returning undefined response body',
|
|
code: `export default function middleware() {
|
|
return new Response(undefined);
|
|
}`,
|
|
},
|
|
])('does not warn when $title', async ({ code }) => {
|
|
;({ cleanup } = await sandbox(next, new Map([[middlewarePath, code]])))
|
|
expect(next.cliOutput).not.toMatch(middlewareWarning)
|
|
})
|
|
})
|