|
| 1 | +import { fileURLToPath } from 'node:url' |
1 | 2 | import { RawSourceMap, SourceMapConsumer } from 'source-map' |
2 | 3 |
|
3 | 4 | export type FileServer = { |
4 | 5 | url: string |
5 | 6 | getFile: (path: string) => Promise<string> |
6 | | - origin: string |
| 7 | + origin?: string |
7 | 8 | } |
8 | 9 |
|
9 | 10 | export class ErrorStackResolver { |
10 | 11 | constructor( |
11 | 12 | readonly fileServers: Array<FileServer>, |
12 | | - ) { |
13 | | - } |
| 13 | + ) {} |
14 | 14 |
|
15 | 15 | async rewriteStack( |
16 | 16 | stack: string, |
17 | 17 | ) { |
18 | | - const re = /(?<pre>\s+at [^\n)]+\()(?<url>\w+:\/\/[^/?]*[^)]*):(?<line>\d+):(?<column>\d+)(?<post>\)$)/gm |
19 | | - let r: RegExpExecArray|null |
20 | | - // eslint-disable-next-line no-cond-assign |
21 | | - while ((r = re.exec(stack)) && r?.groups) { |
22 | | - const url = r.groups.url |
23 | | - const line = Number(r.groups.line) |
24 | | - const column = Number(r.groups.column) |
25 | | - for (const server of this.fileServers) { |
26 | | - if (url.startsWith(server.url) && (server.url.endsWith('/') || url[server.url.length] === '/')) { |
27 | | - const subpath = trimStart(url.substring(server.url.length), '/') |
28 | | - let subPathAndPos = `${subpath}:${line}:${column}` |
| 18 | + for (let end = stack.length, start = end; |
| 19 | + start = stack.lastIndexOf('\n', start - 1), start >= 0; |
| 20 | + end = start |
| 21 | + ) { |
| 22 | + const l = stack.substring(start, end) |
| 23 | + if (l === '\n') { |
| 24 | + continue |
| 25 | + } |
| 26 | + const at = l.match(/^\s+at /) |
| 27 | + if (!at) { |
| 28 | + break |
| 29 | + } |
| 30 | + const m = l.match(/ \((?<file>[^)]+):(?<line>\d+):(?<column>\d+)\)$/) |
| 31 | + if (!m?.groups || m.index === undefined) { |
| 32 | + continue |
| 33 | + } |
29 | 34 |
|
30 | | - // TODO: handle errors when resolving code positions |
| 35 | + const name = l.substring(at[0].length, m.index) |
| 36 | + const {file, line, column} = m.groups |
31 | 37 |
|
32 | | - const content = await server.getFile(subpath) |
33 | | - const mapMatch = String(content).match(/\n\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(?<encodedMap>[0-9a-zA-Z+/]+)\s*$/) |
34 | | - if (mapMatch?.groups) { |
35 | | - const map = JSON.parse(Buffer.from(mapMatch.groups.encodedMap, 'base64').toString('utf8')) as RawSourceMap |
36 | | - const original = await SourceMapConsumer.with(map, null, consumer => { |
37 | | - return consumer.originalPositionFor({ line, column }) |
38 | | - }) |
39 | | - if (original.source) { |
40 | | - subPathAndPos = `${subpath.substring(0, subpath.length - map.file.length)}${original.source}:${String(original.line)}:${String(original.column)}` |
41 | | - } |
42 | | - } |
43 | | - const newStackEntry = r.groups.pre |
44 | | - + server.origin |
45 | | - + (server.origin.endsWith('/') ? '' : '/') |
46 | | - + subPathAndPos |
47 | | - + r.groups.post |
| 38 | + const server = this.getServer(file) |
| 39 | + if (!server) { |
| 40 | + continue |
| 41 | + } |
48 | 42 |
|
49 | | - stack = stack.substring(0, r.index) |
50 | | - + newStackEntry |
51 | | - + stack.substring(r.index + r[0].length) |
| 43 | + const map = await server.getFile(server.path).then( |
| 44 | + source => this.getSourceMap(source, file), |
| 45 | + () => undefined, |
| 46 | + ) |
52 | 47 |
|
53 | | - re.lastIndex += newStackEntry.length - r[0].length |
| 48 | + let resolved = map |
| 49 | + ? await SourceMapConsumer.with(map.raw, map.url, consumer => { |
| 50 | + return consumer.originalPositionFor({line: Number(line), column: Number(column)}) |
| 51 | + }) |
| 52 | + : undefined |
54 | 53 |
|
55 | | - break |
| 54 | + if (!resolved?.source) { |
| 55 | + resolved = { |
| 56 | + source: server.origin |
| 57 | + ? server.origin + (server.origin.endsWith('/') ? '' : '/') + server.path |
| 58 | + : null, |
| 59 | + name: null, |
| 60 | + line: null, |
| 61 | + column: null, |
56 | 62 | } |
57 | 63 | } |
| 64 | + |
| 65 | + if (resolved.source?.startsWith('file://')) { |
| 66 | + resolved.source = fileURLToPath(resolved.source) |
| 67 | + } |
| 68 | + |
| 69 | + stack = stack.substring(0, start) |
| 70 | + + at[0] |
| 71 | + + (resolved.name ?? name) |
| 72 | + + ' (' |
| 73 | + + (resolved.source ?? file) |
| 74 | + + (resolved.line !== null && resolved.column !== null |
| 75 | + ? `:${resolved.line}:${resolved.column}` |
| 76 | + : `:${line}:${column}` |
| 77 | + ) |
| 78 | + + ')' |
| 79 | + + stack.substring(end) |
58 | 80 | } |
59 | 81 | return stack |
60 | 82 | } |
61 | | -} |
62 | 83 |
|
63 | | -function trimStart( |
64 | | - str: string, |
65 | | - chars: string, |
66 | | -) { |
67 | | - for (let i = 0; ; i++) { |
68 | | - if (i >= str.length || !chars.includes(str[i])) { |
69 | | - return str.substring(i) |
| 84 | + protected getServer( |
| 85 | + url: string, |
| 86 | + ) { |
| 87 | + for (const server of this.fileServers) { |
| 88 | + const pre = server.url.endsWith('/') ? server.url : server.url + '/' |
| 89 | + if (url.startsWith(pre)) { |
| 90 | + return { |
| 91 | + origin: server.origin, |
| 92 | + getFile: (p: string) => server.getFile(p), |
| 93 | + path: url.substring(pre.length), |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + protected getSourceMap( |
| 100 | + source: string, |
| 101 | + sourceUrl: string, |
| 102 | + ) { |
| 103 | + const sourceMappingURL = source.match(/\n\/\/# sourceMappingURL=([^\n]+)\s*$/m)?.[1] |
| 104 | + |
| 105 | + if (sourceMappingURL?.startsWith('data:application/json')) { |
| 106 | + const encodedMap = sourceMappingURL.match(/^data:application\/json(?:;charset=[-\w]+)?;base64,(?<encoded>[0-9a-zA-Z+/]+)/)?.groups?.encoded |
| 107 | + if (encodedMap) { |
| 108 | + return { |
| 109 | + raw: JSON.parse(Buffer.from(encodedMap, 'base64').toString()) as RawSourceMap, |
| 110 | + url: sourceUrl, |
| 111 | + } |
| 112 | + } |
70 | 113 | } |
71 | 114 | } |
72 | 115 | } |
0 commit comments