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,30 +333,27 @@ async function getSource( ...@@ -344,30 +333,27 @@ async function getSource(
return undefined return undefined
} }
export function getOverlayMiddleware(options: { async function getOriginalStackFrame({
rootDirectory: string isServer,
isEdgeServer,
isAppDirectory,
frame,
clientStats,
serverStats,
edgeServerStats,
rootDirectory,
}: {
isServer: boolean
isEdgeServer: boolean
isAppDirectory: boolean
frame: StackFrame
clientStats: () => webpack.Stats | null clientStats: () => webpack.Stats | null
serverStats: () => webpack.Stats | null serverStats: () => webpack.Stats | null
edgeServerStats: () => webpack.Stats | null edgeServerStats: () => webpack.Stats | null
rootDirectory: string
}) { }) {
const { rootDirectory, clientStats, serverStats, edgeServerStats } = options const filename = frame.file ?? ''
const source = await getSource(filename, {
return async function (
req: IncomingMessage,
res: ServerResponse,
next: () => void
): Promise<void> {
const { pathname, searchParams } = new URL(`http://n${req.url}`)
if (pathname === '/__nextjs_original-stack-frame') {
const isServer = searchParams.get('isServer') === 'true'
const isEdgeServer = searchParams.get('isEdgeServer') === 'true'
const isAppDirectory = searchParams.get('isAppDirectory') === 'true'
const frame = createStackFrame(searchParams)
let source: Source | undefined
try {
source = await getSource(frame.file, {
getCompilations: () => { getCompilations: () => {
const compilations: webpack.Compilation[] = [] const compilations: webpack.Compilation[] = []
...@@ -410,10 +396,6 @@ export function getOverlayMiddleware(options: { ...@@ -410,10 +396,6 @@ export function getOverlayMiddleware(options: {
return compilations 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 // This stack frame is used for the one that couldn't locate the source or source mapped frame
const defaultStackFrame: IgnorableStackFrame = { const defaultStackFrame: IgnorableStackFrame = {
...@@ -421,18 +403,17 @@ export function getOverlayMiddleware(options: { ...@@ -421,18 +403,17 @@ export function getOverlayMiddleware(options: {
lineNumber: frame.lineNumber, lineNumber: frame.lineNumber,
column: frame.column ?? 1, column: frame.column ?? 1,
methodName: frame.methodName, methodName: frame.methodName,
ignored: shouldIgnorePath(frame.file), ignored: shouldIgnorePath(filename),
arguments: [], arguments: [],
} }
if (!source) { if (!source) {
// return original stack frame with no source map // return original stack frame with no source map
return json(res, { return {
originalStackFrame: defaultStackFrame, originalStackFrame: defaultStackFrame,
originalCodeFrame: null, originalCodeFrame: null,
}) }
} }
try {
const originalStackFrameResponse = await createOriginalStackFrame({ const originalStackFrameResponse = await createOriginalStackFrame({
frame, frame,
source, source,
...@@ -440,19 +421,69 @@ export function getOverlayMiddleware(options: { ...@@ -440,19 +421,69 @@ export function getOverlayMiddleware(options: {
}) })
if (!originalStackFrameResponse) { if (!originalStackFrameResponse) {
return json(res, { return {
originalStackFrame: defaultStackFrame, originalStackFrame: defaultStackFrame,
originalCodeFrame: null, originalCodeFrame: null,
})
} }
}
return originalStackFrameResponse
}
export function getOverlayMiddleware(options: {
rootDirectory: string
clientStats: () => webpack.Stats | null
serverStats: () => webpack.Stats | null
edgeServerStats: () => webpack.Stats | null
}) {
const { rootDirectory, clientStats, serverStats, edgeServerStats } = options
return async function (
req: IncomingMessage,
res: ServerResponse,
next: () => void
): Promise<void> {
const { pathname, searchParams } = new URL(`http://n${req.url}`)
return json(res, originalStackFrameResponse) if (pathname === '/__nextjs_original-stack-frame') {
const isServer = searchParams.get('isServer') === 'true'
const isEdgeServer = searchParams.get('isEdgeServer') === 'true'
const isAppDirectory = searchParams.get('isAppDirectory') === 'true'
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
try {
return json(
res,
await getOriginalStackFrame({
isServer,
isEdgeServer,
isAppDirectory,
frame,
clientStats,
serverStats,
edgeServerStats,
rootDirectory,
})
)
} 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