first commit
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled

This commit is contained in:
Arian Tron
2026-03-10 19:37:31 +03:30
commit 61f56f997c
27684 changed files with 2784175 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import { NextResponse } from 'next/server'
// populated with tests
export default () => {
return NextResponse.next()
}
export const config = {
matcher: '/',
}

View File

@@ -0,0 +1 @@
// populated by tests

View File

@@ -0,0 +1,8 @@
{
"name": "lib",
"private": true,
"type": "module",
"exports": {
".": "./index.js"
}
}

View File

@@ -0,0 +1 @@
.pnpm/test/node_modules/lib

View File

@@ -0,0 +1,8 @@
// populated by tests
export default () => {
return Response.json({ ok: true })
}
export const config = {
runtime: 'edge',
}

View File

@@ -0,0 +1,3 @@
export default function Page() {
return <div>ok</div>
}

View File

@@ -0,0 +1,524 @@
/* eslint-env jest */
import { join } from 'path'
import {
fetchViaHTTP,
File,
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
retry,
waitFor,
} from 'next-test-utils'
import { remove } from 'fs-extra'
jest.setTimeout(1000 * 60 * 2)
const context: Record<string, any> = {
appDir: join(__dirname, '../'),
logs: { output: '', stdout: '', stderr: '' },
api: new File(join(__dirname, '../pages/api/route.js')),
middleware: new File(join(__dirname, '../middleware.js')),
lib: new File(
join(
__dirname,
// Simulated .pnpm node_modules path:
'../node_modules/.pnpm/test/node_modules/lib/index.js'
)
),
}
const appOption = {
onStdout(msg) {
context.logs.output += msg
context.logs.stdout += msg
},
onStderr(msg) {
context.logs.output += msg
context.logs.stderr += msg
},
}
const routeUrl = '/api/route'
const middlewareUrl = '/'
const TELEMETRY_EVENT_NAME = 'NEXT_EDGE_ALLOW_DYNAMIC_USED'
describe('Edge runtime configurable guards', () => {
beforeEach(async () => {
await remove(join(__dirname, '../.next'))
context.appPort = await findPort()
context.logs = { output: '', stdout: '', stderr: '' }
})
afterEach(async () => {
if (context.app) {
await killApp(context.app)
}
context.api.restore()
context.middleware.restore()
context.lib.restore()
})
describe('Multiple functions with different configurations', () => {
beforeEach(async () => {
context.middleware.write(`
import { NextResponse } from 'next/server'
export default () => {
eval('100')
return NextResponse.next()
}
export const config = {
unstable_allowDynamic: '/middleware.js'
}
`)
context.api.write(`
export default async function handler(request) {
eval('100')
return Response.json({ result: true })
}
export const config = {
runtime: 'edge',
unstable_allowDynamic: '**/node_modules/lib/**'
}
`)
await waitFor(500)
})
it('warns in dev for allowed code', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, middlewareUrl)
await waitFor(500)
expect(res.status).toBe(200)
await retry(async () => {
expect(context.logs.output).toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
)
})
})
it('warns in dev for unallowed code', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, routeUrl)
expect(res.status).toBe(200)
await retry(async () => {
expect(context.logs.output).toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
)
})
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('fails to build because of unallowed code', async () => {
const output = await nextBuild(context.appDir, undefined, {
stdout: true,
stderr: true,
env: process.env.IS_TURBOPACK_TEST
? {}
: { NEXT_TELEMETRY_DEBUG: '1' },
})
expect(output.code).toBe(1)
if (!process.env.IS_TURBOPACK_TEST) {
expect(output.stderr).toContain(`./pages/api/route.js`)
}
expect(output.stderr).toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime`
)
if (!process.env.IS_TURBOPACK_TEST) {
expect(output.stderr).toContain(`Used by default`)
expect(output.stderr).toContain(TELEMETRY_EVENT_NAME)
}
})
}
)
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init() {
context.api.write(`
export default async function handler(request) {
eval('100')
return Response.json({ result: true })
}
export const config = {
runtime: 'edge',
unstable_allowDynamic: '**'
}
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init() {
context.middleware.write(`
import { NextResponse } from 'next/server'
export default () => {
eval('100')
return NextResponse.next()
}
export const config = {
unstable_allowDynamic: '**'
}
`)
},
},
{
title: 'Edge API using lib',
url: routeUrl,
init() {
context.api.write(`
import { hasDynamic } from 'lib'
export default async function handler(request) {
await hasDynamic()
return Response.json({ result: true })
}
export const config = {
runtime: 'edge',
unstable_allowDynamic: '**/node_modules/lib/**'
}
`)
context.lib.write(`
export async function hasDynamic() {
eval('100')
}
`)
},
},
{
title: 'Middleware using lib',
url: middlewareUrl,
init() {
context.middleware.write(`
import { NextResponse } from 'next/server'
import { hasDynamic } from 'lib'
// populated with tests
export default async function () {
await hasDynamic()
return NextResponse.next()
}
export const config = {
unstable_allowDynamic: '**/node_modules/lib/**'
}
`)
context.lib.write(`
export async function hasDynamic() {
eval('100')
}
`)
},
// TODO: Re-enable when Turbopack applies the middleware dynamic code
// evaluation transforms also to code in node_modules.
skip: Boolean(process.env.IS_TURBOPACK_TEST),
},
])('$title with allowed, used dynamic code', ({ init, url, skip }) => {
beforeEach(() => init())
;(skip ? it.skip : it)('still warns in dev at runtime', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
await waitFor(500)
// eslint-disable-next-line jest/no-standalone-expect
expect(res.status).toBe(200)
// eslint-disable-next-line jest/no-standalone-expect
expect(context.logs.output).toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
)
})
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init() {
context.api.write(`
export default async function handler(request) {
if ((() => false)()) {
eval('100')
}
return Response.json({ result: true })
}
export const config = {
runtime: 'edge',
unstable_allowDynamic: '**'
}
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init() {
context.middleware.write(`
import { NextResponse } from 'next/server'
// populated with tests
export default () => {
if ((() => false)()) {
eval('100')
}
return NextResponse.next()
}
export const config = {
unstable_allowDynamic: '**'
}
`)
},
},
{
title: 'Edge API using lib',
url: routeUrl,
init() {
context.api.write(`
import { hasUnusedDynamic } from 'lib'
export default async function handler(request) {
await hasUnusedDynamic()
return Response.json({ result: true })
}
export const config = {
runtime: 'edge',
unstable_allowDynamic: '**/node_modules/lib/**'
}
`)
context.lib.write(`
export async function hasUnusedDynamic() {
if ((() => false)()) {
eval('100')
}
}
`)
},
},
{
title: 'Middleware using lib',
url: middlewareUrl,
init() {
context.middleware.write(`
import { NextResponse } from 'next/server'
import { hasUnusedDynamic } from 'lib'
// populated with tests
export default async function () {
await hasUnusedDynamic()
return NextResponse.next()
}
export const config = {
unstable_allowDynamic: '**/node_modules/lib/**'
}
`)
context.lib.write(`
export async function hasUnusedDynamic() {
if ((() => false)()) {
eval('100')
}
}
`)
},
},
])('$title with allowed, unused dynamic code', ({ init, url }) => {
beforeEach(() => init())
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
// This checks the unstable_allowDynamic configuration which is not supported in Turbopack.
;(process.env.IS_TURBOPACK_TEST ? it.skip : it)(
'build and does not warn at runtime',
async () => {
const output = await nextBuild(context.appDir, undefined, {
stdout: true,
stderr: true,
env: process.env.IS_TURBOPACK_TEST
? {}
: { NEXT_TELEMETRY_DEBUG: '1' },
})
// eslint-disable-next-line jest/no-standalone-expect
expect(output.stderr).not.toContain(`Build failed`)
if (!process.env.IS_TURBOPACK_TEST) {
// eslint-disable-next-line jest/no-standalone-expect
expect(output.stderr).toContain(TELEMETRY_EVENT_NAME)
}
context.appPort = await findPort()
context.app = await nextStart(
context.appDir,
context.appPort,
appOption
)
const res = await fetchViaHTTP(context.appPort, url)
// eslint-disable-next-line jest/no-standalone-expect
expect(res.status).toBe(200)
// eslint-disable-next-line jest/no-standalone-expect
expect(context.logs.output).not.toContain(`warn`)
// eslint-disable-next-line jest/no-standalone-expect
expect(context.logs.output).not.toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
)
}
)
}
)
})
describe.each([
{
title: 'Edge API using lib',
url: routeUrl,
init() {
context.api.write(`
import { hasDynamic } from 'lib'
export default async function handler(request) {
await hasDynamic()
return Response.json({ result: true })
}
export const config = {
runtime: 'edge',
unstable_allowDynamic: '/pages/**'
}
`)
context.lib.write(`
export async function hasDynamic() {
eval('100')
}
`)
},
// TODO: Re-enable when Turbopack applies the edge runtime transforms also
// to code in node_modules.
skip: Boolean(process.env.IS_TURBOPACK_TEST),
},
{
title: 'Middleware using lib',
url: middlewareUrl,
init() {
context.middleware.write(`
import { NextResponse } from 'next/server'
import { hasDynamic } from 'lib'
export default async function () {
await hasDynamic()
return NextResponse.next()
}
export const config = {
unstable_allowDynamic: '/pages/**'
}
`)
context.lib.write(`
export async function hasDynamic() {
eval('100')
}
`)
},
// TODO: Re-enable when Turbopack applies the middleware dynamic code
// evaluation transforms also to code in node_modules.
skip: Boolean(process.env.IS_TURBOPACK_TEST),
},
])('$title with unallowed, used dynamic code', ({ init, url, skip }) => {
beforeEach(() => init())
;(skip ? it.skip : it)('warns in dev at runtime', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
await waitFor(500)
// eslint-disable-next-line jest/no-standalone-expect
expect(res.status).toBe(200)
// eslint-disable-next-line jest/no-standalone-expect
expect(context.logs.output).toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
)
})
;(skip || process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('fails to build because of dynamic code evaluation', async () => {
const output = await nextBuild(context.appDir, undefined, {
stdout: true,
stderr: true,
env: process.env.IS_TURBOPACK_TEST
? {}
: { NEXT_TELEMETRY_DEBUG: '1' },
})
expect(output.stderr).toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime`
)
if (!process.env.IS_TURBOPACK_TEST) {
expect(output.stderr).toContain(TELEMETRY_EVENT_NAME)
}
})
}
)
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init() {
context.api.write(`
export default async function handler(request) {
return Response.json({ result: (() => {}) instanceof Function })
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init() {
context.middleware.write(`
import { NextResponse } from 'next/server'
import { returnTrue } from 'lib'
export default async function () {
(() => {}) instanceof Function
return NextResponse.next()
}
`)
},
},
])('$title with use of Function as a type', ({ init, url }) => {
beforeEach(() => init())
it('does not warn in dev at runtime', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
await waitFor(500)
expect(res.status).toBe(200)
expect(context.logs.output).not.toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
)
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
// This checks the unstable_allowDynamic configuration which is not supported in Turbopack.
;(process.env.IS_TURBOPACK_TEST ? it.skip : it)(
'build and does not warn at runtime',
async () => {
const output = await nextBuild(context.appDir, undefined, {
stdout: true,
stderr: true,
})
// eslint-disable-next-line jest/no-standalone-expect
expect(output.stderr).not.toContain(`Build failed`)
context.appPort = await findPort()
context.app = await nextStart(
context.appDir,
context.appPort,
appOption
)
const res = await fetchViaHTTP(context.appPort, url)
// eslint-disable-next-line jest/no-standalone-expect
expect(res.status).toBe(200)
// eslint-disable-next-line jest/no-standalone-expect
expect(context.logs.output).not.toContain(`warn`)
// eslint-disable-next-line jest/no-standalone-expect
expect(context.logs.output).not.toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
)
}
)
}
)
})
})