Repository: vercel/next.js
File: packages/next/src/server/dev/middleware-turbopack.ts (TypeScript)

Code before:
  /** undefined = not yet computed */
  let originalCodeFrame: string | null | undefined

  const tracedFrame = traced.frame
  return Object.defineProperty(
    {
      originalStackFrame: {
        arguments: tracedFrame.arguments,
        file: normalizedStackFrameLocation,
        line1: tracedFrame.line1,
        column1: tracedFrame.column1,
        ignored: tracedFrame.ignored,
        methodName: tracedFrame.methodName,
      },
      originalCodeFrame: null,
    },
    'originalCodeFrame',
    {
      get: () => {
        if (originalCodeFrame === undefined) {
          originalCodeFrame = getOriginalCodeFrame(tracedFrame, traced.source)
        }
        return originalCodeFrame
      },
    }
  )
}

export function getOverlayMiddleware({
  project,
  projectPath,
  isSrcDir,
}: {
  project: Project
  projectPath: string
  isSrcDir: boolean
}) {
  return async function (
    req: IncomingMessage,
    res: ServerResponse,
    next: () => void
  ): Promise<void> {
    const { pathname, searchParams } = new URL(req.url!, 'http://n')

    if (pathname === '/__nextjs_original-stack-frames') {
      if (req.method !== 'POST') {
        return middlewareResponse.badRequest(res)
      }

      const body = await new Promise<string>((resolve, reject) => {
        let data = ''

Review comment (suggestion):
a getter method should be slightly slightly faster (1 fewer shape transition) a little cuter too

```suggestion
  const tracedFrame = traced.frame
  return {
      originalStackFrame: {
        arguments: tracedFrame.arguments,
        file: normalizedStackFrameLocation,
        line1: tracedFrame.line1,
        column1: tracedFrame.column1,
        ignored: tracedFrame.ignored,
        methodName: tracedFrame.methodName,
      },
      get originalCodeFrame() {
        if (originalCodeFrame === undefined) {
          originalCodeFrame = getOriginalCodeFrame(tracedFrame, traced.source)
        }
        return originalCodeFrame
      },
    }
```

Code after:
  /** undefined = not yet computed */
  let originalCodeFrame: string | null | undefined

  const tracedFrame = traced.frame
  return {
    originalStackFrame: {
      arguments: tracedFrame.arguments,
      file: normalizedStackFrameLocation,
      line1: tracedFrame.line1,
      column1: tracedFrame.column1,
      ignored: tracedFrame.ignored,
      methodName: tracedFrame.methodName,
    },
    get originalCodeFrame() {
      if (originalCodeFrame === undefined) {
        originalCodeFrame = getOriginalCodeFrame(tracedFrame, traced.source)
      }
      return originalCodeFrame
    },
  }
}

export function getOverlayMiddleware({
  project,
  projectPath,
  isSrcDir,
}: {
  project: Project
  projectPath: string
  isSrcDir: boolean
}) {
  return async function (
    req: IncomingMessage,
    res: ServerResponse,
    next: () => void
  ): Promise<void> {
    const { pathname, searchParams } = new URL(req.url!, 'http://n')

    if (pathname === '/__nextjs_original-stack-frames') {
      if (req.method !== 'POST') {
        return middlewareResponse.badRequest(res)
      }

      const body = await new Promise<string>((resolve, reject) => {
        let data = ''
        req.on('data', (chunk) => {
          data += chunk
        })
        req.on('end', () => resolve(data))
        req.on('error', reject)
      })