MCPcopy
hub / github.com/vercel/next.js / nativeTraceSource

Function nativeTraceSource

packages/next/src/server/dev/middleware-turbopack.ts:180–283  ·  view source on GitHub ↗

* @returns 1-based lines and 1-based columns

(
  frame: TurbopackStackFrame
)

Source from the content-addressed store, hash-verified

178 * @returns 1-based lines and 1-based columns
179 */
180async function nativeTraceSource(
181 frame: TurbopackStackFrame
182): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> {
183 const sourceURL = frame.file
184 let sourceMapPayload: ModernSourceMapPayload | undefined
185 try {
186 sourceMapPayload = findSourceMap(sourceURL)?.payload
187 } catch (cause) {
188 throw new Error(
189 `${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code.`,
190 { cause }
191 )
192 }
193
194 if (sourceMapPayload !== undefined) {
195 let consumer: SourceMapConsumer
196 try {
197 consumer = await new SourceMapConsumer(sourceMapPayload)
198 } catch (cause) {
199 throw new Error(
200 `${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code.`,
201 { cause }
202 )
203 }
204 let traced: {
205 originalPosition: NullableMappedPosition
206 sourceContent: string | null
207 } | null
208 try {
209 const originalPosition = consumer.originalPositionFor({
210 line: frame.line ?? 1,
211 // 0-based columns out requires 0-based columns in.
212 column: (frame.column ?? 1) - 1,
213 })
214
215 if (originalPosition.source === null) {
216 traced = null
217 } else {
218 const sourceContent: string | null =
219 consumer.sourceContentFor(
220 originalPosition.source,
221 /* returnNullOnMissing */ true
222 ) ?? null
223
224 traced = { originalPosition, sourceContent }
225 }
226 } finally {
227 consumer.destroy()
228 }
229
230 if (traced !== null) {
231 const { originalPosition, sourceContent } = traced
232 const applicableSourceMap = findApplicableSourceMapPayload(
233 (frame.line ?? 1) - 1,
234 (frame.column ?? 1) - 1,
235 sourceMapPayload
236 )
237

Callers 1

createOriginalStackFrameFunction · 0.85

Calls 9

includesMethod · 0.80
shouldIgnorePathFunction · 0.70
errorMethod · 0.65
replaceMethod · 0.65
originalPositionForMethod · 0.45
sourceContentForMethod · 0.45
destroyMethod · 0.45
indexOfMethod · 0.45

Tested by

no test coverage detected