File size: 4,738 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env node

/**
 * Script to translate a minified stack trace to a real one.
 *
 * Usage:
 * stacktrace-translate
 *
 * Paste stacktrace array from error log into stdin.
 */

const fs = require( 'fs' );
const readline = require( 'readline' );
const glob = require( 'glob' );
const _ = require( 'lodash' );
const SourceMap = require( 'source-map' );
const StackFrame = require( 'stackframe' );
const StackTraceGPS = require( 'stacktrace-gps' );

const BUNDLE_URL_ROOT = 'https://wordpress.com/calypso/';
const SOURCE_PATTERN = './public/*.js';
const SOURCEMAP_PATTERN = './public/*.js.map';

let verbose = false;

function loadSources() {
	const filePrefixChars = SOURCE_PATTERN.indexOf( '*' );
	const fileNames = glob.sync( SOURCE_PATTERN );
	const files = {};

	fileNames.forEach( ( fileName ) => {
		const relativeFileName = fileName.substring( filePrefixChars );
		const url = BUNDLE_URL_ROOT + relativeFileName;
		const text = fs.readFileSync( fileName );
		const textWithMapping = text + '\n//# sourceMappingURL=' + url + '.map';
		files[ url ] = textWithMapping;
	} );

	return files;
}

function loadSourceMaps() {
	const filePrefixChars = SOURCEMAP_PATTERN.indexOf( '*' );
	const fileNames = glob.sync( SOURCEMAP_PATTERN );
	const files = {};

	fileNames.forEach( ( fileName ) => {
		const url = BUNDLE_URL_ROOT + fileName.substring( filePrefixChars );
		const text = fs.readFileSync( fileName );
		const data = JSON.parse( text );
		files[ url ] = new SourceMap.SourceMapConsumer( data );
	} );

	return files;
}

function readStackTraceText( text, sourceCache, sourceMapConsumerCache ) {
	const originalStack = JSON.parse( text );

	const gps = new StackTraceGPS( { offline: true, sourceCache, sourceMapConsumerCache } );

	originalStack.forEach( ( originalStackFrame, index ) => {
		const stackFrame = new StackFrame( {
			fileName: originalStackFrame.url,
			lineNumber: originalStackFrame.line,
			columnNumber: originalStackFrame.column,
		} );

		gps.getMappedLocation( stackFrame ).then( ( mappedStackFrame ) => {
			printStackFrame( originalStackFrame, mappedStackFrame, index );
		} );
	} );
}

function printStackFrame( minified, mapped, index ) {
	console.log( '------------------------------------------------------------' );
	console.log( ' Stack Frame [ ' + index + ' ]' );
	console.log( '------------------------------------------------------------' );
	console.log( ' minified source: \t' + minified.url );
	console.log( '          function: \t' + minified.func );
	console.log( '          args: \t' + JSON.stringify( minified.args ) );
	console.log( '          location: \t' + minified.line + ':' + minified.column );
	console.log( '' );
	console.log( ' translated source: \t' + mapped.fileName );
	console.log( '            function: \t' + mapped.functionName );
	console.log( '            location: \t' + mapped.lineNumber + ':' + mapped.columnNumber );
	console.log( '' );
}

function loadBuild() {
	if ( verbose ) {
		console.log( 'Loading sources and maps...' );
	}

	const sources = loadSources();
	const sourceMaps = loadSourceMaps();

	if ( verbose ) {
		console.log(
			Object.keys( sources ).length +
				' sources and ' +
				Object.keys( sourceMaps ).length +
				' maps loaded.'
		);
	}

	return {
		sources,
		sourceMaps,
	};
}

function readFromFile( fileName ) {
	const { sources, sourceMaps } = loadBuild();

	if ( verbose ) {
		console.log( 'Reading from file: ' + fileName );
	}

	const text = String( fs.readFileSync( fileName ) );

	readStackTraceText( text, sources, sourceMaps );
}

function readFromStdin() {
	const { sources, sourceMaps } = loadBuild();

	const rl = readline.createInterface( {
		input: process.stdin,
		output: process.stdout,
		terminal: true,
		prompt: '',
	} );

	if ( verbose ) {
		console.log( 'Paste stacktrace array from error log: ' );
	}
	rl.on( 'line', ( line ) => {
		readStackTraceText( line, sources, sourceMaps );
		rl.close();
	} );
}

function showUsage() {
	console.log( 'Usage: stacktrace-translate.js [options] <json file>' );
	console.log( '' );
	console.log( '<json file>: Stack trace file (if not provided stdin will be used.)' );
	console.log( '' );
	console.log( 'Options:' );
	console.log( ' -?, --help\t\t\tShow usage' );
	console.log( ' -v, --verbose\t\t\tVerbose output' );
}

function run( args ) {
	const usage = _.includes( args, '-?' ) || _.includes( args, '--help' );

	if ( usage ) {
		showUsage( args[ 0 ], args[ 1 ] );
		process.exit( 0 );
	}

	verbose = _.includes( args, '-v' ) || _.includes( args, '--verbose' );
	const fileName = _.find( args, ( el, index ) => {
		return 2 <= index && ! el.startsWith( '-' );
	} );

	if ( fileName ) {
		readFromFile( fileName );
	} else {
		readFromStdin();
	}
}

run( process.argv );