-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add source map tracing in error stack traces (#48)
- Loading branch information
1 parent
fea2edd
commit d326b8d
Showing
35 changed files
with
146 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'nxjs-runtime': patch | ||
--- | ||
|
||
Add source map tracing in error stack traces |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js* | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.nro | ||
/node_modules | ||
/romfs/main.js | ||
/romfs/main.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/runtime.js | ||
/runtime.js* | ||
/public |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"author": "Nathan Rajlich <[email protected]>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@jridgewell/trace-mapping": "^0.3.19", | ||
"@types/color-rgba": "^2.1.0", | ||
"@types/to-px": "^1.1.2", | ||
"@ungap/event-target": "^0.2.4", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { dataUriToBuffer } from 'data-uri-to-buffer'; | ||
import { | ||
TraceMap, | ||
originalPositionFor, | ||
type EncodedSourceMap, | ||
} from '@jridgewell/trace-mapping'; | ||
import type { SwitchClass } from './switch'; | ||
|
||
declare const Switch: SwitchClass; | ||
|
||
const SOURCE_MAPPING_URL_PREFIX = '//# sourceMappingURL='; | ||
const sourceMapCache = new Map<string, TraceMap | null>(); | ||
|
||
function filenameToTracer(filename: string) { | ||
let tracer = sourceMapCache.get(filename); | ||
if (typeof tracer !== 'undefined') return tracer; | ||
|
||
// `null` means the source map could not be retrieved for this file | ||
tracer = null; | ||
|
||
const contentsBuffer = Switch.readFileSync(filename); | ||
const contents = new TextDecoder().decode(contentsBuffer).trimEnd(); | ||
const lastNewline = contents.lastIndexOf('\n'); | ||
const lastLine = contents.slice(lastNewline + 1); | ||
if (lastLine.startsWith(SOURCE_MAPPING_URL_PREFIX)) { | ||
const sourceMappingURL = lastLine.slice( | ||
SOURCE_MAPPING_URL_PREFIX.length | ||
); | ||
let sourceMapBuffer: ArrayBuffer; | ||
if (sourceMappingURL.startsWith('data:')) { | ||
sourceMapBuffer = dataUriToBuffer(sourceMappingURL).buffer; | ||
} else { | ||
sourceMapBuffer = Switch.readFileSync( | ||
new URL(sourceMappingURL, filename) | ||
); | ||
} | ||
const sourceMap: EncodedSourceMap = JSON.parse( | ||
new TextDecoder().decode(sourceMapBuffer) | ||
); | ||
tracer = new TraceMap(sourceMap); | ||
} | ||
sourceMapCache.set(filename, tracer); | ||
return tracer; | ||
} | ||
|
||
(Error as any).prepareStackTrace = (_: Error, stack: string) => { | ||
return stack | ||
.split('\n') | ||
.map((line) => { | ||
try { | ||
const m = line.match(/(\s+at )(.*) \((.*)\:(\d+)\)$/); | ||
if (!m) return line; | ||
|
||
const [_, at, name, filename, lineNo] = m; | ||
const tracer = filenameToTracer(filename); | ||
if (!tracer) return line; | ||
|
||
const traced = originalPositionFor(tracer, { | ||
line: +lineNo, | ||
// QuickJS doesn't provide column number. | ||
// Unfortunately that means that minification | ||
// doesn't work well with source maps :( | ||
column: 0, | ||
}); | ||
if (!traced.source || !traced.line) return line; | ||
|
||
const proto = filename === 'romfs:/runtime.js' ? 'nxjs' : 'app'; | ||
return `${at}${traced.name || name} (${proto}:${ | ||
traced.source | ||
}:${traced.line})`; | ||
} catch (_) {} | ||
return line; | ||
}) | ||
.join('\n'); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
APP="${1-hello-world}" | ||
SWITCH_HOST="${2-192.168.86.115:5000}" | ||
curl --netrc-optional \ | ||
--upload-file nxjs.nro "ftp://${SWITCH_HOST}/switch/nxjs.nro" \ | ||
|
||
ARGS=( | ||
--upload-file nxjs.nro "ftp://${SWITCH_HOST}/switch/nxjs.nro" | ||
--upload-file "./apps/${APP}/romfs/main.js" "ftp://${SWITCH_HOST}/switch/nxjs.js" | ||
) | ||
|
||
# Upload source map file, if it exists | ||
if [ -f "./apps/${APP}/romfs/main.js.map" ]; then | ||
ARGS+=(--upload-file "./apps/${APP}/romfs/main.js.map" "ftp://${SWITCH_HOST}/switch/main.js.map") | ||
fi | ||
|
||
curl --netrc-optional "${ARGS[@]}" |