Skip to content

Commit

Permalink
feat(middleware): adds telemetry for allowDynamic configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed Sep 1, 2022
1 parent 0aec52b commit 093cdfd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
37 changes: 28 additions & 9 deletions packages/next/build/webpack/plugins/middleware-plugin.ts
Expand Up @@ -23,6 +23,8 @@ import {
getPageStaticInfo,
MiddlewareConfig,
} from '../../analysis/get-page-static-info'
import { Telemetry } from '../../../telemetry/storage'
import { traceGlobals } from '../../../trace/shared'

export interface EdgeFunctionDefinition {
env: string[]
Expand Down Expand Up @@ -605,13 +607,16 @@ async function findEntryEdgeFunctionConfig(
)
)
if (typeof pageFilePath === 'string') {
return (
await getPageStaticInfo({
nextConfig: {},
pageFilePath,
isDev: false,
})
).middleware
return {
file: pageFilePath,
config: (
await getPageStaticInfo({
nextConfig: {},
pageFilePath,
isDev: false,
})
).middleware,
}
}
}
}
Expand All @@ -627,6 +632,7 @@ function getExtractMetadata(params: {
return async () => {
metadataByEntry.clear()
const resolver = compilation.resolverFactory.get('normal')
const telemetry: Telemetry = traceGlobals.get('telemetry')

for (const [entryName, entry] of compilation.entries) {
if (entry.options.runtime !== EDGE_RUNTIME_WEBPACK) {
Expand Down Expand Up @@ -684,10 +690,23 @@ function getExtractMetadata(params: {
continue
}

if (edgeFunctionConfig?.config?.allowDynamicGlobs) {
telemetry.record({
eventName: 'NEXT_EDGE_ALLOW_DYNAMIC_USED',
payload: {
...edgeFunctionConfig,
file: edgeFunctionConfig.file.replace(rootDir ?? '', ''),
fileWithDynamicCode: module.userRequest.replace(
rootDir ?? '',
''
),
},
})
}
if (
!isDynamicCodeEvaluationAllowed(
module.userRequest,
edgeFunctionConfig,
edgeFunctionConfig?.config,
rootDir
)
) {
Expand All @@ -699,7 +718,7 @@ function getExtractMetadata(params: {
', '
)}`
: ''
}`,
}\nLearn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`,
entryModule: module,
compilation,
})
Expand Down
Expand Up @@ -35,6 +35,7 @@ const appOption = {
}
const routeUrl = '/api/route'
const middlewareUrl = '/'
const TELEMETRY_EVENT_NAME = 'NEXT_EDGE_ALLOW_DYNAMIC_USED'

describe('Edge runtime configurable guards', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -101,13 +102,15 @@ describe('Edge runtime configurable guards', () => {
const output = await nextBuild(context.appDir, undefined, {
stdout: true,
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})
expect(output.stderr).toContain(`Build failed`)
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`
)
expect(output.stderr).toContain(`Used by default`)
expect(output.stderr).toContain(TELEMETRY_EVENT_NAME)
})
})

Expand Down Expand Up @@ -299,8 +302,10 @@ describe('Edge runtime configurable guards', () => {
const output = await nextBuild(context.appDir, undefined, {
stdout: true,
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})
expect(output.stderr).not.toContain(`Build failed`)
expect(output.stderr).toContain(TELEMETRY_EVENT_NAME)
context.app = await nextStart(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(200)
Expand Down Expand Up @@ -373,11 +378,13 @@ describe('Edge runtime configurable guards', () => {
const output = await nextBuild(context.appDir, undefined, {
stdout: true,
stderr: true,
env: { NEXT_TELEMETRY_DEBUG: 1 },
})
expect(output.stderr).toContain(`Build failed`)
expect(output.stderr).not.toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime`
expect(output.stderr).toContain(
`Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime`
)
expect(output.stderr).toContain(TELEMETRY_EVENT_NAME)
})
})
})

0 comments on commit 093cdfd

Please sign in to comment.