| "use strict"; |
| Object.defineProperty(exports, "__esModule", { |
| value: true |
| }); |
| 0 && (module.exports = { |
| getOriginalStackFrames: null, |
| getOverlayMiddleware: null, |
| getSourceMapMiddleware: null |
| }); |
| function _export(target, all) { |
| for(var name in all)Object.defineProperty(target, name, { |
| enumerable: true, |
| get: all[name] |
| }); |
| } |
| _export(exports, { |
| getOriginalStackFrames: function() { |
| return getOriginalStackFrames; |
| }, |
| getOverlayMiddleware: function() { |
| return getOverlayMiddleware; |
| }, |
| getSourceMapMiddleware: function() { |
| return getSourceMapMiddleware; |
| } |
| }); |
| const _shared = require("../../next-devtools/server/shared"); |
| const _middlewareresponse = require("../../next-devtools/server/middleware-response"); |
| const _path = _interop_require_default(require("path")); |
| const _launcheditor = require("../../next-devtools/server/launch-editor"); |
| const _sourcemap08 = require("next/dist/compiled/source-map08"); |
| const _sourcemaps = require("../lib/source-maps"); |
| const _nodemodule = require("node:module"); |
| const _nodeurl = require("node:url"); |
| const _nodeutil = require("node:util"); |
| function _interop_require_default(obj) { |
| return obj && obj.__esModule ? obj : { |
| default: obj |
| }; |
| } |
| function shouldIgnorePath(modulePath) { |
| return modulePath.includes('node_modules') || |
| modulePath.includes('next/dist') || modulePath.startsWith('node:'); |
| } |
| const currentSourcesByFile = new Map(); |
| |
| |
| async function batchedTraceSource(project, frame) { |
| const file = frame.file ? decodeURIComponent(frame.file) : undefined; |
| if (!file) return; |
| |
| |
| if (file.startsWith('node:')) { |
| return { |
| frame: { |
| file, |
| line1: frame.line ?? null, |
| column1: frame.column ?? null, |
| methodName: frame.methodName ?? '<unknown>', |
| ignored: true, |
| arguments: [] |
| }, |
| source: null |
| }; |
| } |
| const currentDirectoryFileUrl = (0, _nodeurl.pathToFileURL)(process.cwd()).href; |
| const sourceFrame = await project.traceSource(frame, currentDirectoryFileUrl); |
| if (!sourceFrame) { |
| return { |
| frame: { |
| file, |
| line1: frame.line ?? null, |
| column1: frame.column ?? null, |
| methodName: frame.methodName ?? '<unknown>', |
| ignored: shouldIgnorePath(file), |
| arguments: [] |
| }, |
| source: null |
| }; |
| } |
| let source = null; |
| const originalFile = sourceFrame.originalFile; |
| |
| const ignored = shouldIgnorePath(originalFile ?? sourceFrame.file) || |
| !!sourceFrame.isInternal; |
| if (originalFile && !ignored) { |
| let sourcePromise = currentSourcesByFile.get(originalFile); |
| if (!sourcePromise) { |
| sourcePromise = project.getSourceForAsset(originalFile); |
| currentSourcesByFile.set(originalFile, sourcePromise); |
| setTimeout(()=>{ |
| |
| |
| currentSourcesByFile.delete(originalFile); |
| }, 100); |
| } |
| source = await sourcePromise; |
| } |
| |
| const ignorableFrame = { |
| file: sourceFrame.file, |
| line1: sourceFrame.line ?? null, |
| column1: sourceFrame.column ?? null, |
| methodName: |
| |
| |
| |
| frame.methodName ?? '<unknown>', |
| ignored, |
| arguments: [] |
| }; |
| return { |
| frame: ignorableFrame, |
| source |
| }; |
| } |
| function parseFile(fileParam) { |
| if (!fileParam) { |
| return undefined; |
| } |
| return (0, _sourcemaps.devirtualizeReactServerURL)(fileParam); |
| } |
| function createStackFrames(body) { |
| const { frames, isServer } = body; |
| return frames.map((frame)=>{ |
| const file = parseFile(frame.file); |
| if (!file) { |
| return undefined; |
| } |
| return { |
| file, |
| methodName: frame.methodName ?? '<unknown>', |
| line: frame.line1 ?? undefined, |
| column: frame.column1 ?? undefined, |
| isServer |
| }; |
| }).filter((f)=>f !== undefined); |
| } |
| function createStackFrame(searchParams) { |
| const file = parseFile(searchParams.get('file')); |
| if (!file) { |
| return undefined; |
| } |
| return { |
| file, |
| methodName: searchParams.get('methodName') ?? '<unknown>', |
| line: parseInt(searchParams.get('line1') ?? '0', 10) || undefined, |
| column: parseInt(searchParams.get('column1') ?? '0', 10) || undefined, |
| isServer: searchParams.get('isServer') === 'true' |
| }; |
| } |
| |
| |
| async function nativeTraceSource(frame) { |
| const sourceURL = frame.file; |
| let sourceMapPayload; |
| try { |
| var _findSourceMap; |
| sourceMapPayload = (_findSourceMap = (0, _nodemodule.findSourceMap)(sourceURL)) == null ? void 0 : _findSourceMap.payload; |
| } catch (cause) { |
| throw Object.defineProperty(new Error(`${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code.`, { |
| cause |
| }), "__NEXT_ERROR_CODE", { |
| value: "E635", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| if (sourceMapPayload !== undefined) { |
| let consumer; |
| try { |
| consumer = await new _sourcemap08.SourceMapConsumer(sourceMapPayload); |
| } catch (cause) { |
| throw Object.defineProperty(new Error(`${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code.`, { |
| cause |
| }), "__NEXT_ERROR_CODE", { |
| value: "E635", |
| enumerable: false, |
| configurable: true |
| }); |
| } |
| let traced; |
| try { |
| const originalPosition = consumer.originalPositionFor({ |
| line: frame.line ?? 1, |
| |
| column: (frame.column ?? 1) - 1 |
| }); |
| if (originalPosition.source === null) { |
| traced = null; |
| } else { |
| const sourceContent = consumer.sourceContentFor(originalPosition.source, true) ?? null; |
| traced = { |
| originalPosition, |
| sourceContent |
| }; |
| } |
| } finally{ |
| consumer.destroy(); |
| } |
| if (traced !== null) { |
| var |
| |
| |
| |
| _frame_methodName_replace, _frame_methodName; |
| const { originalPosition, sourceContent } = traced; |
| const applicableSourceMap = (0, _sourcemaps.findApplicableSourceMapPayload)((frame.line ?? 1) - 1, (frame.column ?? 1) - 1, sourceMapPayload); |
| |
| let ignored = false; |
| if (applicableSourceMap === undefined) { |
| console.error('No applicable source map found in sections for frame', frame); |
| } else { |
| var _applicableSourceMap_ignoreList; |
| |
| const sourceIndex = applicableSourceMap.sources.indexOf(originalPosition.source); |
| ignored = ((_applicableSourceMap_ignoreList = applicableSourceMap.ignoreList) == null ? void 0 : _applicableSourceMap_ignoreList.includes(sourceIndex)) ?? |
| |
| shouldIgnorePath(frame.file); |
| } |
| const originalStackFrame = { |
| methodName: ((_frame_methodName = frame.methodName) == null ? void 0 : (_frame_methodName_replace = _frame_methodName.replace('__WEBPACK_DEFAULT_EXPORT__', 'default')) == null ? void 0 : _frame_methodName_replace.replace('__webpack_exports__.', '')) || '<unknown>', |
| file: originalPosition.source, |
| line1: originalPosition.line, |
| column1: originalPosition.column === null ? null : originalPosition.column + 1, |
| |
| arguments: [], |
| ignored |
| }; |
| return { |
| frame: originalStackFrame, |
| source: sourceContent |
| }; |
| } |
| } |
| return undefined; |
| } |
| async function createOriginalStackFrame(project, projectPath, frame) { |
| const traced = await nativeTraceSource(frame) ?? |
| |
| await batchedTraceSource(project, frame); |
| if (!traced) { |
| return null; |
| } |
| let normalizedStackFrameLocation = traced.frame.file; |
| if (normalizedStackFrameLocation !== null && normalizedStackFrameLocation.startsWith('file://')) { |
| normalizedStackFrameLocation = _path.default.relative(projectPath, (0, _nodeurl.fileURLToPath)(normalizedStackFrameLocation)); |
| } |
| return { |
| originalStackFrame: { |
| arguments: traced.frame.arguments, |
| file: normalizedStackFrameLocation, |
| line1: traced.frame.line1, |
| column1: traced.frame.column1, |
| ignored: traced.frame.ignored, |
| methodName: traced.frame.methodName |
| }, |
| originalCodeFrame: (0, _shared.getOriginalCodeFrame)(traced.frame, traced.source) |
| }; |
| } |
| function getOverlayMiddleware({ project, projectPath, isSrcDir }) { |
| return async function(req, res, next) { |
| const { pathname, searchParams } = new URL(req.url, 'http://n'); |
| if (pathname === '/__nextjs_original-stack-frames') { |
| if (req.method !== 'POST') { |
| return _middlewareresponse.middlewareResponse.badRequest(res); |
| } |
| const body = await new Promise((resolve, reject)=>{ |
| let data = ''; |
| req.on('data', (chunk)=>{ |
| data += chunk; |
| }); |
| req.on('end', ()=>resolve(data)); |
| req.on('error', reject); |
| }); |
| const request = JSON.parse(body); |
| const result = await getOriginalStackFrames({ |
| project, |
| projectPath, |
| frames: request.frames, |
| isServer: request.isServer, |
| isEdgeServer: request.isEdgeServer, |
| isAppDirectory: request.isAppDirectory |
| }); |
| (0, _shared.ignoreListAnonymousStackFramesIfSandwiched)(result); |
| return _middlewareresponse.middlewareResponse.json(res, result); |
| } else if (pathname === '/__nextjs_launch-editor') { |
| const isAppRelativePath = searchParams.get('isAppRelativePath') === '1'; |
| let openEditorResult; |
| if (isAppRelativePath) { |
| const relativeFilePath = searchParams.get('file') || ''; |
| const appPath = _path.default.join('app', isSrcDir ? 'src' : '', relativeFilePath); |
| openEditorResult = await (0, _launcheditor.openFileInEditor)(appPath, 1, 1, projectPath); |
| } else { |
| const frame = createStackFrame(searchParams); |
| if (!frame) return _middlewareresponse.middlewareResponse.badRequest(res); |
| openEditorResult = await (0, _launcheditor.openFileInEditor)(frame.file, frame.line ?? 1, frame.column ?? 1, projectPath); |
| } |
| if (openEditorResult.error) { |
| return _middlewareresponse.middlewareResponse.internalServerError(res, openEditorResult.error); |
| } |
| if (!openEditorResult.found) { |
| return _middlewareresponse.middlewareResponse.notFound(res); |
| } |
| return _middlewareresponse.middlewareResponse.noContent(res); |
| } |
| return next(); |
| }; |
| } |
| function getSourceMapMiddleware(project) { |
| return async function(req, res, next) { |
| const { pathname, searchParams } = new URL(req.url, 'http://n'); |
| if (pathname !== '/__nextjs_source-map') { |
| return next(); |
| } |
| let filename = searchParams.get('filename'); |
| if (!filename) { |
| return _middlewareresponse.middlewareResponse.badRequest(res); |
| } |
| let nativeSourceMap; |
| try { |
| nativeSourceMap = (0, _nodemodule.findSourceMap)(filename); |
| } catch (cause) { |
| return _middlewareresponse.middlewareResponse.internalServerError(res, Object.defineProperty(new Error(`${filename}: Invalid source map. Only conformant source maps can be used to find the original code.`, { |
| cause |
| }), "__NEXT_ERROR_CODE", { |
| value: "E635", |
| enumerable: false, |
| configurable: true |
| })); |
| } |
| if (nativeSourceMap !== undefined) { |
| const sourceMapPayload = nativeSourceMap.payload; |
| return _middlewareresponse.middlewareResponse.json(res, sourceMapPayload); |
| } |
| try { |
| |
| filename = decodeURI(filename); |
| } catch { |
| return _middlewareresponse.middlewareResponse.badRequest(res); |
| } |
| if (_path.default.isAbsolute(filename)) { |
| filename = (0, _nodeurl.pathToFileURL)(filename).href; |
| } |
| try { |
| const sourceMapString = await project.getSourceMap(filename); |
| if (sourceMapString) { |
| return _middlewareresponse.middlewareResponse.jsonString(res, sourceMapString); |
| } |
| } catch (cause) { |
| return _middlewareresponse.middlewareResponse.internalServerError(res, Object.defineProperty(new Error(`Failed to get source map for '${filename}'. This is a bug in Next.js`, { |
| cause |
| }), "__NEXT_ERROR_CODE", { |
| value: "E719", |
| enumerable: false, |
| configurable: true |
| })); |
| } |
| _middlewareresponse.middlewareResponse.noContent(res); |
| }; |
| } |
| async function getOriginalStackFrames({ project, projectPath, frames, isServer, isEdgeServer, isAppDirectory }) { |
| const stackFrames = createStackFrames({ |
| frames, |
| isServer, |
| isEdgeServer, |
| isAppDirectory |
| }); |
| return Promise.all(stackFrames.map(async (frame)=>{ |
| try { |
| const stackFrame = await createOriginalStackFrame(project, projectPath, frame); |
| if (stackFrame === null) { |
| return { |
| status: 'rejected', |
| reason: 'Failed to create original stack frame' |
| }; |
| } |
| return { |
| status: 'fulfilled', |
| value: stackFrame |
| }; |
| } catch (error) { |
| return { |
| status: 'rejected', |
| reason: (0, _nodeutil.inspect)(error, { |
| colors: false |
| }) |
| }; |
| } |
| })); |
| } |
|
|
| |