Unverified Commit 2943e7a5 authored by Jude Gao's avatar Jude Gao Committed by GitHub

refactor: decouple getOriginalStackFrame from endpoint (#75553)

Refactor in preparation for stack frame request batching. This separates
the stack frame logic from the API layer, which allows me to reuse the
logic for next batching API.
parent 2daa7e1f
...@@ -149,17 +149,6 @@ function isIgnoredSource( ...@@ -149,17 +149,6 @@ function isIgnoredSource(
return false return false
} }
function createStackFrame(searchParams: URLSearchParams) {
const file = searchParams.get('file') as string
return {
file,
methodName: searchParams.get('methodName') as string,
lineNumber: parseInt(searchParams.get('lineNumber') ?? '0', 10) || 0,
column: parseInt(searchParams.get('column') ?? '0', 10) || 0,
arguments: searchParams.getAll('arguments').filter(Boolean),
} satisfies StackFrame
}
function findOriginalSourcePositionAndContentFromCompilation( function findOriginalSourcePositionAndContentFromCompilation(
moduleId: string | undefined, moduleId: string | undefined,
importedModule: string, importedModule: string,
...@@ -344,6 +333,103 @@ async function getSource( ...@@ -344,6 +333,103 @@ async function getSource(
return undefined return undefined
} }
async function getOriginalStackFrame({
isServer,
isEdgeServer,
isAppDirectory,
frame,
clientStats,
serverStats,
edgeServerStats,
rootDirectory,
}: {
isServer: boolean
isEdgeServer: boolean
isAppDirectory: boolean
frame: StackFrame
clientStats: () => webpack.Stats | null
serverStats: () => webpack.Stats | null
edgeServerStats: () => webpack.Stats | null
rootDirectory: string
}) {
const filename = frame.file ?? ''
const source = await getSource(filename, {
getCompilations: () => {
const compilations: webpack.Compilation[] = []
// Try Client Compilation first. In `pages` we leverage
// `isClientError` to check. In `app` it depends on if it's a server
// / client component and when the code throws. E.g. during HTML
// rendering it's the server/edge compilation.
if ((!isEdgeServer && !isServer) || isAppDirectory) {
const compilation = clientStats()?.compilation
if (compilation) {
compilations.push(compilation)
}
}
// Try Server Compilation. In `pages` this could be something
// imported in getServerSideProps/getStaticProps as the code for
// those is tree-shaken. In `app` this finds server components and
// code that was imported from a server component. It also covers
// when client component code throws during HTML rendering.
if (isServer || isAppDirectory) {
const compilation = serverStats()?.compilation
if (compilation) {
compilations.push(compilation)
}
}
// Try Edge Server Compilation. Both cases are the same as Server
// Compilation, main difference is that it covers `runtime: 'edge'`
// pages/app routes.
if (isEdgeServer || isAppDirectory) {
const compilation = edgeServerStats()?.compilation
if (compilation) {
compilations.push(compilation)
}
}
return compilations
},
})
// This stack frame is used for the one that couldn't locate the source or source mapped frame
const defaultStackFrame: IgnorableStackFrame = {
file: frame.file,
lineNumber: frame.lineNumber,
column: frame.column ?? 1,
methodName: frame.methodName,
ignored: shouldIgnorePath(filename),
arguments: [],
}
if (!source) {
// return original stack frame with no source map
return {
originalStackFrame: defaultStackFrame,
originalCodeFrame: null,
}
}
const originalStackFrameResponse = await createOriginalStackFrame({
frame,
source,
rootDirectory,
})
if (!originalStackFrameResponse) {
return {
originalStackFrame: defaultStackFrame,
originalCodeFrame: null,
}
}
return originalStackFrameResponse
}
export function getOverlayMiddleware(options: { export function getOverlayMiddleware(options: {
rootDirectory: string rootDirectory: string
clientStats: () => webpack.Stats | null clientStats: () => webpack.Stats | null
...@@ -363,96 +449,41 @@ export function getOverlayMiddleware(options: { ...@@ -363,96 +449,41 @@ export function getOverlayMiddleware(options: {
const isServer = searchParams.get('isServer') === 'true' const isServer = searchParams.get('isServer') === 'true'
const isEdgeServer = searchParams.get('isEdgeServer') === 'true' const isEdgeServer = searchParams.get('isEdgeServer') === 'true'
const isAppDirectory = searchParams.get('isAppDirectory') === 'true' const isAppDirectory = searchParams.get('isAppDirectory') === 'true'
const frame = createStackFrame(searchParams)
let source: Source | undefined const frame = {
try { file: searchParams.get('file') as string,
source = await getSource(frame.file, { methodName: searchParams.get('methodName') as string,
getCompilations: () => { lineNumber: parseInt(searchParams.get('lineNumber') ?? '0', 10) || 0,
const compilations: webpack.Compilation[] = [] column: parseInt(searchParams.get('column') ?? '0', 10) || 0,
arguments: searchParams.getAll('arguments').filter(Boolean),
// Try Client Compilation first. In `pages` we leverage } satisfies StackFrame
// `isClientError` to check. In `app` it depends on if it's a server
// / client component and when the code throws. E.g. during HTML
// rendering it's the server/edge compilation.
if ((!isEdgeServer && !isServer) || isAppDirectory) {
const compilation = clientStats()?.compilation
if (compilation) {
compilations.push(compilation)
}
}
// Try Server Compilation. In `pages` this could be something
// imported in getServerSideProps/getStaticProps as the code for
// those is tree-shaken. In `app` this finds server components and
// code that was imported from a server component. It also covers
// when client component code throws during HTML rendering.
if (isServer || isAppDirectory) {
const compilation = serverStats()?.compilation
if (compilation) {
compilations.push(compilation)
}
}
// Try Edge Server Compilation. Both cases are the same as Server
// Compilation, main difference is that it covers `runtime: 'edge'`
// pages/app routes.
if (isEdgeServer || isAppDirectory) {
const compilation = edgeServerStats()?.compilation
if (compilation) {
compilations.push(compilation)
}
}
return compilations
},
})
} catch (err) {
console.error('Failed to get source map:', err)
return internalServerError(res)
}
// This stack frame is used for the one that couldn't locate the source or source mapped frame
const defaultStackFrame: IgnorableStackFrame = {
file: frame.file,
lineNumber: frame.lineNumber,
column: frame.column ?? 1,
methodName: frame.methodName,
ignored: shouldIgnorePath(frame.file),
arguments: [],
}
if (!source) {
// return original stack frame with no source map
return json(res, {
originalStackFrame: defaultStackFrame,
originalCodeFrame: null,
})
}
try { try {
const originalStackFrameResponse = await createOriginalStackFrame({ return json(
frame, res,
source, await getOriginalStackFrame({
rootDirectory, isServer,
}) isEdgeServer,
isAppDirectory,
if (!originalStackFrameResponse) { frame,
return json(res, { clientStats,
originalStackFrame: defaultStackFrame, serverStats,
originalCodeFrame: null, edgeServerStats,
rootDirectory,
}) })
} )
return json(res, originalStackFrameResponse)
} catch (err) { } catch (err) {
console.log('Failed to parse source map:', err) console.log('Failed to parse source map:', err)
return internalServerError(res) return internalServerError(res)
} }
} else if (pathname === '/__nextjs_launch-editor') { } else if (pathname === '/__nextjs_launch-editor') {
const frame = createStackFrame(searchParams) const frame = {
file: searchParams.get('file') as string,
methodName: searchParams.get('methodName') as string,
lineNumber: parseInt(searchParams.get('lineNumber') ?? '0', 10) || 0,
column: parseInt(searchParams.get('column') ?? '0', 10) || 0,
arguments: searchParams.getAll('arguments').filter(Boolean),
} satisfies StackFrame
if (!frame.file) return badRequest(res) if (!frame.file) return badRequest(res)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment