{"version":3,"sources":["../../../../src/server/dev/middleware-turbopack.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from 'http'\nimport {\n  DEVTOOLS_CODE_FRAME_MAX_WIDTH,\n  getOriginalCodeFrame,\n  ignoreListAnonymousStackFramesIfSandwiched,\n  type IgnorableStackFrame,\n  type OriginalStackFrameResponse,\n  type OriginalStackFramesRequest,\n  type OriginalStackFramesResponse,\n  type StackFrame,\n} from '../../next-devtools/server/shared'\nimport { middlewareResponse } from '../../next-devtools/server/middleware-response'\nimport path from 'path'\nimport { openFileInEditor } from '../../next-devtools/server/launch-editor'\nimport {\n  SourceMapConsumer,\n  type NullableMappedPosition,\n} from 'next/dist/compiled/source-map08'\nimport type { Project, TurbopackStackFrame } from '../../build/swc/types'\nimport {\n  type ModernSourceMapPayload,\n  devirtualizeReactServerURL,\n  findApplicableSourceMapPayload,\n} from '../lib/source-maps'\nimport { findSourceMap, type SourceMap } from 'node:module'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\nimport { inspect } from 'node:util'\n\nfunction shouldIgnorePath(modulePath: string): boolean {\n  return (\n    modulePath.includes('node_modules') ||\n    // Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo\n    modulePath.includes('next/dist') ||\n    modulePath.startsWith('node:')\n  )\n}\n\nconst currentSourcesByFile: Map<string, Promise<string | null>> = new Map()\n/**\n * @returns 1-based lines and 1-based columns\n */\nasync function batchedTraceSource(\n  project: Project,\n  frame: TurbopackStackFrame\n): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> {\n  const file = frame.file\n    ? // TODO(veil): Why are the frames sent encoded?\n      decodeURIComponent(frame.file)\n    : undefined\n\n  if (!file) return\n\n  // For node internals they cannot traced the actual source code with project.traceSource,\n  // we need an early return to indicate it's ignored to avoid the unknown scheme error from `project.traceSource`.\n  if (file.startsWith('node:')) {\n    return {\n      frame: {\n        file,\n        line1: frame.line ?? null,\n        column1: frame.column ?? null,\n        methodName: frame.methodName ?? '<unknown>',\n        ignored: true,\n        arguments: [],\n      },\n      source: null,\n    }\n  }\n\n  const currentDirectoryFileUrl = pathToFileURL(process.cwd()).href\n\n  const sourceFrame = await project.traceSource(frame, currentDirectoryFileUrl)\n  if (!sourceFrame) {\n    return {\n      frame: {\n        file,\n        line1: frame.line ?? null,\n        column1: frame.column ?? null,\n        methodName: frame.methodName ?? '<unknown>',\n        ignored: shouldIgnorePath(file),\n        arguments: [],\n      },\n      source: null,\n    }\n  }\n\n  let source = null\n  const originalFile = sourceFrame.originalFile\n\n  // Don't look up source for node_modules or internals. These can often be large bundled files.\n  const ignored =\n    // Check the sourcemap's ignoreList (e.g. from 3rd party packages)\n    !!sourceFrame.isIgnored ||\n    shouldIgnorePath(originalFile ?? sourceFrame.file)\n  if (originalFile && !ignored) {\n    let sourcePromise = currentSourcesByFile.get(originalFile)\n    if (!sourcePromise) {\n      sourcePromise = project.getSourceForAsset(originalFile)\n      currentSourcesByFile.set(originalFile, sourcePromise)\n      setTimeout(() => {\n        // Cache file reads for 100ms, as frames will often reference the same\n        // files and can be large.\n        currentSourcesByFile.delete(originalFile!)\n      }, 100)\n    }\n    source = await sourcePromise\n  }\n\n  const ignorableFrame: IgnorableStackFrame = {\n    file: sourceFrame.file,\n    line1: sourceFrame.line ?? null,\n    column1: sourceFrame.column ?? null,\n    methodName:\n      // We ignore the sourcemapped name since it won't be the correct name.\n      // The callsite will point to the column of the variable name instead of the\n      // name of the enclosing function.\n      // TODO(NDX-531): Spy on prepareStackTrace to get the enclosing line number for method name mapping.\n      frame.methodName ?? '<unknown>',\n    ignored,\n    arguments: [],\n  }\n\n  return {\n    frame: ignorableFrame,\n    source,\n  }\n}\n\nfunction parseFile(fileParam: string | null): string | undefined {\n  if (!fileParam) {\n    return undefined\n  }\n\n  const file = devirtualizeReactServerURL(fileParam)\n  // React virtualizes filenames as `'file://' + path`, which is malformed\n  // for paths that need percent-encoding (e.g. a space in the project path)\n  // and then fails both Turbopack's `traceSource` and Node.js' source map\n  // cache lookups. Re-encode through WHATWG URL parsing.\n  // TODO(veil): Revisit if React's virtualization round-trips losslessly.\n  if (file.startsWith('file://') && URL.canParse(file)) {\n    return new URL(file).href\n  }\n  return file\n}\n\nfunction createStackFrames(\n  body: OriginalStackFramesRequest\n): TurbopackStackFrame[] {\n  const { frames, isServer } = body\n\n  return frames\n    .map((frame): TurbopackStackFrame | undefined => {\n      const file = parseFile(frame.file)\n\n      if (!file) {\n        return undefined\n      }\n\n      return {\n        file,\n        methodName: frame.methodName ?? '<unknown>',\n        line: frame.line1 ?? undefined,\n        column: frame.column1 ?? undefined,\n        isServer,\n      }\n    })\n    .filter((f): f is TurbopackStackFrame => f !== undefined)\n}\n\nfunction createStackFrame(\n  searchParams: URLSearchParams\n): TurbopackStackFrame | undefined {\n  const file = parseFile(searchParams.get('file'))\n\n  if (!file) {\n    return undefined\n  }\n\n  return {\n    file,\n    methodName: searchParams.get('methodName') ?? '<unknown>',\n    line: parseInt(searchParams.get('line1') ?? '0', 10) || undefined,\n    column: parseInt(searchParams.get('column1') ?? '0', 10) || undefined,\n    isServer: searchParams.get('isServer') === 'true',\n  }\n}\n\n/**\n * @returns 1-based lines and 1-based columns\n */\nasync function nativeTraceSource(\n  frame: TurbopackStackFrame\n): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> {\n  const sourceURL = frame.file\n  let sourceMapPayload: ModernSourceMapPayload | undefined\n  try {\n    sourceMapPayload = findSourceMap(sourceURL)?.payload\n  } catch (cause) {\n    throw new Error(\n      `${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code.`,\n      { cause }\n    )\n  }\n\n  if (sourceMapPayload !== undefined) {\n    let consumer: SourceMapConsumer\n    try {\n      consumer = await new SourceMapConsumer(sourceMapPayload)\n    } catch (cause) {\n      throw new Error(\n        `${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code.`,\n        { cause }\n      )\n    }\n    let traced: {\n      originalPosition: NullableMappedPosition\n      sourceContent: string | null\n    } | null\n    try {\n      const originalPosition = consumer.originalPositionFor({\n        line: frame.line ?? 1,\n        // 0-based columns out requires 0-based columns in.\n        column: (frame.column ?? 1) - 1,\n      })\n\n      if (originalPosition.source === null) {\n        traced = null\n      } else {\n        const sourceContent: string | null =\n          consumer.sourceContentFor(\n            originalPosition.source,\n            /* returnNullOnMissing */ true\n          ) ?? null\n\n        traced = { originalPosition, sourceContent }\n      }\n    } finally {\n      consumer.destroy()\n    }\n\n    if (traced !== null) {\n      const { originalPosition, sourceContent } = traced\n      const applicableSourceMap = findApplicableSourceMapPayload(\n        (frame.line ?? 1) - 1,\n        (frame.column ?? 1) - 1,\n        sourceMapPayload\n      )\n\n      // TODO(veil): Upstream a method to sourcemap consumer that immediately says if a frame is ignored or not.\n      let ignored = false\n      if (applicableSourceMap === undefined) {\n        console.error(\n          'No applicable source map found in sections for frame',\n          frame\n        )\n      } else {\n        // TODO: O(n^2). Consider moving `ignoreList` into a Set\n        const sourceIndex = applicableSourceMap.sources.indexOf(\n          originalPosition.source!\n        )\n        ignored =\n          applicableSourceMap.ignoreList?.includes(sourceIndex) ??\n          // When sourcemap is not available, fallback to checking `frame.file`.\n          // e.g. In pages router, nextjs server code is not bundled into the page.\n          shouldIgnorePath(frame.file)\n      }\n\n      const originalStackFrame: IgnorableStackFrame = {\n        methodName:\n          // We ignore the sourcemapped name since it won't be the correct name.\n          // The callsite will point to the column of the variable name instead of the\n          // name of the enclosing function.\n          // TODO(NDX-531): Spy on prepareStackTrace to get the enclosing line number for method name mapping.\n          frame.methodName\n            ?.replace('__WEBPACK_DEFAULT_EXPORT__', 'default')\n            ?.replace('__webpack_exports__.', '') || '<unknown>',\n        file: originalPosition.source,\n        line1: originalPosition.line,\n        column1:\n          originalPosition.column === null ? null : originalPosition.column + 1,\n        // TODO: c&p from async createOriginalStackFrame but why not frame.arguments?\n        arguments: [],\n        ignored,\n      }\n\n      return {\n        frame: originalStackFrame,\n        source: sourceContent,\n      }\n    }\n  }\n\n  return undefined\n}\n\n/**\n * Code frame rendering options. The defaults match terminal consumers; only\n * the overlay HTTP path opts in to always-on colors and the wide max width.\n */\ntype CodeFrameOptions = {\n  /** Defaults to `process.stdout.isTTY`. */\n  colors?: boolean\n  /** Defaults to the dev server's terminal width. */\n  maxWidth?: number\n}\n\nasync function createOriginalStackFrame(\n  project: Project,\n  projectPath: string,\n  frame: TurbopackStackFrame,\n  codeFrameOptions?: CodeFrameOptions\n): Promise<OriginalStackFrameResponse | null> {\n  const traced =\n    (await nativeTraceSource(frame)) ??\n    // TODO(veil): When would the bundler know more than native?\n    // If it's faster, try the bundler first and fall back to native later.\n    (await batchedTraceSource(project, frame))\n  if (!traced) {\n    return null\n  }\n\n  let normalizedStackFrameLocation = traced.frame.file\n  if (\n    normalizedStackFrameLocation !== null &&\n    normalizedStackFrameLocation.startsWith('file://')\n  ) {\n    normalizedStackFrameLocation = path.relative(\n      projectPath,\n      fileURLToPath(normalizedStackFrameLocation)\n    )\n  }\n\n  /** undefined = not yet computed */\n  let originalCodeFrame: string | null | undefined\n\n  const tracedFrame = traced.frame\n  return {\n    originalStackFrame: {\n      arguments: tracedFrame.arguments,\n      file: normalizedStackFrameLocation,\n      line1: tracedFrame.line1,\n      column1: tracedFrame.column1,\n      ignored: tracedFrame.ignored,\n      methodName: tracedFrame.methodName,\n    },\n    get originalCodeFrame() {\n      if (originalCodeFrame === undefined) {\n        originalCodeFrame = getOriginalCodeFrame(tracedFrame, traced.source, {\n          colors: codeFrameOptions?.colors,\n          maxWidth: codeFrameOptions?.maxWidth,\n        })\n      }\n      return originalCodeFrame\n    },\n  }\n}\n\nexport function getOverlayMiddleware({\n  project,\n  projectPath,\n  isSrcDir,\n}: {\n  project: Project\n  projectPath: string\n  isSrcDir: boolean\n}) {\n  return async function (\n    req: IncomingMessage,\n    res: ServerResponse,\n    next: () => void\n  ): Promise<void> {\n    const { pathname, searchParams } = new URL(req.url!, 'http://n')\n\n    if (pathname === '/__nextjs_original-stack-frames') {\n      if (req.method !== 'POST') {\n        return middlewareResponse.badRequest(res)\n      }\n\n      const body = await new Promise<string>((resolve, reject) => {\n        let data = ''\n        req.on('data', (chunk) => {\n          data += chunk\n        })\n        req.on('end', () => resolve(data))\n        req.on('error', reject)\n      })\n\n      const request = JSON.parse(body) as OriginalStackFramesRequest\n      const result = await getOriginalStackFrames({\n        project,\n        projectPath,\n        frames: request.frames,\n        isServer: request.isServer,\n        isEdgeServer: request.isEdgeServer,\n        isAppDirectory: request.isAppDirectory,\n        codeFrameOptions: {\n          // Overlay parses ANSI in JS and renders in a scrollable\n          // `<pre>`, so colors are always wanted and terminal width is\n          // irrelevant.\n          colors: true,\n          maxWidth: DEVTOOLS_CODE_FRAME_MAX_WIDTH,\n        },\n      })\n\n      ignoreListAnonymousStackFramesIfSandwiched(result)\n\n      return middlewareResponse.json(res, result)\n    } else if (pathname === '/__nextjs_launch-editor') {\n      const isAppRelativePath = searchParams.get('isAppRelativePath') === '1'\n\n      let openEditorResult\n      if (isAppRelativePath) {\n        const relativeFilePath = searchParams.get('file') || ''\n        const appPath = path.join(\n          isSrcDir ? 'src' : '',\n          'app',\n          relativeFilePath\n        )\n        openEditorResult = await openFileInEditor(appPath, 1, 1, projectPath)\n      } else {\n        const frame = createStackFrame(searchParams)\n        if (!frame) return middlewareResponse.badRequest(res)\n        openEditorResult = await openFileInEditor(\n          frame.file,\n          frame.line ?? 1,\n          frame.column ?? 1,\n          projectPath\n        )\n      }\n\n      if (openEditorResult.error) {\n        return middlewareResponse.internalServerError(\n          res,\n          openEditorResult.error\n        )\n      }\n      if (!openEditorResult.found) {\n        return middlewareResponse.notFound(res)\n      }\n      return middlewareResponse.noContent(res)\n    }\n\n    return next()\n  }\n}\n\nexport function getSourceMapMiddleware(project: Project) {\n  return async function (\n    req: IncomingMessage,\n    res: ServerResponse,\n    next: () => void\n  ): Promise<void> {\n    const { pathname, searchParams } = new URL(req.url!, 'http://n')\n\n    if (pathname !== '/__nextjs_source-map') {\n      return next()\n    }\n\n    let filename = searchParams.get('filename')\n\n    if (!filename) {\n      return middlewareResponse.badRequest(res)\n    }\n\n    let nativeSourceMap: SourceMap | undefined\n    try {\n      nativeSourceMap = findSourceMap(filename)\n    } catch (cause) {\n      return middlewareResponse.internalServerError(\n        res,\n        new Error(\n          `${filename}: Invalid source map. Only conformant source maps can be used to find the original code.`,\n          { cause }\n        )\n      )\n    }\n\n    if (nativeSourceMap !== undefined) {\n      const sourceMapPayload = nativeSourceMap.payload\n      return middlewareResponse.json(res, sourceMapPayload)\n    }\n\n    try {\n      // Turbopack chunk filenames might be URL-encoded.\n      filename = decodeURI(filename)\n    } catch {\n      return middlewareResponse.badRequest(res)\n    }\n\n    if (path.isAbsolute(filename)) {\n      filename = pathToFileURL(filename).href\n    }\n\n    try {\n      const sourceMapString = await project.getSourceMap(filename)\n\n      if (sourceMapString) {\n        return middlewareResponse.jsonString(res, sourceMapString)\n      }\n    } catch (cause) {\n      return middlewareResponse.internalServerError(\n        res,\n        new Error(\n          `Failed to get source map for '${filename}'. This is a bug in Next.js`,\n          {\n            cause,\n          }\n        )\n      )\n    }\n\n    middlewareResponse.noContent(res)\n  }\n}\n\nexport async function getOriginalStackFrames({\n  project,\n  projectPath,\n  frames,\n  isServer,\n  isEdgeServer,\n  isAppDirectory,\n  codeFrameOptions,\n}: {\n  project: Project\n  projectPath: string\n  frames: readonly StackFrame[]\n  isServer: boolean\n  isEdgeServer: boolean\n  isAppDirectory: boolean\n  codeFrameOptions?: CodeFrameOptions\n}): Promise<OriginalStackFramesResponse> {\n  const stackFrames = createStackFrames({\n    frames,\n    isServer,\n    isEdgeServer,\n    isAppDirectory,\n  })\n\n  return Promise.all(\n    stackFrames.map(async (frame) => {\n      try {\n        const stackFrame = await createOriginalStackFrame(\n          project,\n          projectPath,\n          frame,\n          codeFrameOptions\n        )\n        if (stackFrame === null) {\n          return {\n            status: 'rejected',\n            reason: 'Failed to create original stack frame',\n          }\n        }\n        const originalStackFrame = stackFrame.originalStackFrame\n        return {\n          status: 'fulfilled',\n          value: {\n            originalStackFrame,\n            originalCodeFrame:\n              (originalStackFrame?.ignored ?? true)\n                ? null\n                : // TODO: Don't get all codeframes of non-ignored frames eagerly.\n                  stackFrame.originalCodeFrame,\n          },\n        }\n      } catch (error) {\n        return {\n          status: 'rejected',\n          reason: inspect(error, { colors: false }),\n        }\n      }\n    })\n  )\n}\n"],"names":["DEVTOOLS_CODE_FRAME_MAX_WIDTH","getOriginalCodeFrame","ignoreListAnonymousStackFramesIfSandwiched","middlewareResponse","path","openFileInEditor","SourceMapConsumer","devirtualizeReactServerURL","findApplicableSourceMapPayload","findSourceMap","fileURLToPath","pathToFileURL","inspect","shouldIgnorePath","modulePath","includes","startsWith","currentSourcesByFile","Map","batchedTraceSource","project","frame","file","decodeURIComponent","undefined","line1","line","column1","column","methodName","ignored","arguments","source","currentDirectoryFileUrl","process","cwd","href","sourceFrame","traceSource","originalFile","isIgnored","sourcePromise","get","getSourceForAsset","set","setTimeout","delete","ignorableFrame","parseFile","fileParam","URL","canParse","createStackFrames","body","frames","isServer","map","filter","f","createStackFrame","searchParams","parseInt","nativeTraceSource","sourceURL","sourceMapPayload","payload","cause","Error","consumer","traced","originalPosition","originalPositionFor","sourceContent","sourceContentFor","destroy","applicableSourceMap","console","error","sourceIndex","sources","indexOf","ignoreList","originalStackFrame","replace","createOriginalStackFrame","projectPath","codeFrameOptions","normalizedStackFrameLocation","relative","originalCodeFrame","tracedFrame","colors","maxWidth","getOverlayMiddleware","isSrcDir","req","res","next","pathname","url","method","badRequest","Promise","resolve","reject","data","on","chunk","request","JSON","parse","result","getOriginalStackFrames","isEdgeServer","isAppDirectory","json","isAppRelativePath","openEditorResult","relativeFilePath","appPath","join","internalServerError","found","notFound","noContent","getSourceMapMiddleware","filename","nativeSourceMap","decodeURI","isAbsolute","sourceMapString","getSourceMap","jsonString","stackFrames","all","stackFrame","status","reason","value"],"mappings":"AACA,SACEA,6BAA6B,EAC7BC,oBAAoB,EACpBC,0CAA0C,QAMrC,oCAAmC;AAC1C,SAASC,kBAAkB,QAA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