File size: 2,779 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
export type ComponentStackFrame = {
  canOpenInEditor: boolean
  component: string
  file: string | null
  line1: number | null
  column1: number | null
}

enum LocationType {
  FILE = 'file',
  WEBPACK_INTERNAL = 'webpack-internal',
  HTTP = 'http',
  PROTOCOL_RELATIVE = 'protocol-relative',
  UNKNOWN = 'unknown',
}

/**
 * Get the type of frame line based on the location
 */
function getLocationType(location: string): LocationType {
  if (location.startsWith('file://')) {
    return LocationType.FILE
  }
  if (location.includes('webpack-internal://')) {
    return LocationType.WEBPACK_INTERNAL
  }
  if (location.startsWith('http://') || location.startsWith('https://')) {
    return LocationType.HTTP
  }
  if (location.startsWith('//')) {
    return LocationType.PROTOCOL_RELATIVE
  }
  return LocationType.UNKNOWN
}

function parseStackFrameLocation(
  location: string
): Omit<ComponentStackFrame, 'component'> {
  const locationType = getLocationType(location)

  const modulePath = location?.replace(
    /^(webpack-internal:\/\/\/|file:\/\/)(\(.*\)\/)?/,
    ''
  )
  const [, file, line1, column1] = modulePath?.match(/^(.+):(\d+):(\d+)/) ?? []

  switch (locationType) {
    case LocationType.FILE:
    case LocationType.WEBPACK_INTERNAL:
      return {
        canOpenInEditor: true,
        file,
        line1: line1 ? Number(line1) : null,
        column1: column1 ? Number(column1) : null,
      }
    // When the location is a URL we only show the file
    // TODO: Resolve http(s) URLs through sourcemaps
    case LocationType.HTTP:
    case LocationType.PROTOCOL_RELATIVE:
    case LocationType.UNKNOWN:
    default: {
      return {
        canOpenInEditor: false,
        file: null,
        line1: null,
        column1: null,
      }
    }
  }
}

export function parseComponentStack(
  componentStack: string
): ComponentStackFrame[] {
  const componentStackFrames: ComponentStackFrame[] = []
  for (const line of componentStack.trim().split('\n')) {
    // TODO: support safari stack trace
    // Get component and file from the component stack line
    const match = /at ([^ ]+)( \((.*)\))?/.exec(line)
    if (match?.[1]) {
      const component = match[1]
      const location = match[3]

      if (!location) {
        componentStackFrames.push({
          canOpenInEditor: false,
          component,
          file: null,
          line1: null,
          column1: null,
        })
        continue
      }

      // Stop parsing the component stack if we reach a Next.js component
      if (location?.includes('next/dist')) {
        break
      }

      const frameLocation = parseStackFrameLocation(location)
      componentStackFrames.push({
        component,
        ...frameLocation,
      })
    }
  }

  return componentStackFrames
}