Skip to content

Commit

Permalink
Merge pull request #5 from BugSplat-Git/pr-2-updates
Browse files Browse the repository at this point in the history
Updates for PR #2
  • Loading branch information
bobbyg603 authored Apr 15, 2021
2 parents 6da3c48 + c6bb7db commit becd733
Show file tree
Hide file tree
Showing 7 changed files with 1,378 additions and 5,173 deletions.
8 changes: 8 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
dist
coverage
.eslintrc.js
babel.config.js
jest.config.js
__tests__
.idea
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {
'semi': ['error', 'always'],
'object-curly-spacing': ['error', 'always']
}
};
4,330 changes: 0 additions & 4,330 deletions package-lock.json

This file was deleted.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"version": "1.0.0",
"scripts": {
"test": "jest",
"lint": "DEBUG=eslint:cli-engine yarn eslint . --ext .js,.jsx,.ts,.tsx --fix",
"runScript": "ts-node",
"t": "ts-node scripts/t.ts",
"combineTest": "ts-node scripts/combineTest.ts"
},
"dependencies": {
"combine-source-map": "^0.8.0",
"fs-extra": "^9.1.0",
"stacktrace-parser": "^0.1.10",
"ts-node": "^9.1.1",
Expand All @@ -19,10 +19,14 @@
"@babel/preset-env": "^7.13.10",
"@babel/preset-typescript": "^7.13.0",
"@types/jest": "^26.0.21",
"@types/lodash": "^4.14.168",
"@types/node": "^14.14.31",
"@types/source-map": "^0.5.7",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"babel-jest": "^26.6.3",
"eslint": "^7.24.0",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"ts-jest": "^26.5.4"
},
"license": "UNLICENSED"
Expand Down
2 changes: 1 addition & 1 deletion scripts/t.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {StackConverter} from "../src/stackconverter";
import { StackConverter } from "../src/stackconverter";
import * as fs from "fs";


Expand Down
60 changes: 30 additions & 30 deletions src/stackconverter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as stackTraceParser from 'stacktrace-parser';
import {SourceMapConsumer} from 'source-map';
import * as fs from "fs/promises";
import {StackFrame} from "stacktrace-parser";
import {get, set} from 'lodash';
import { get, set } from 'lodash';
import * as path from "path";
import { SourceMapConsumer } from 'source-map';
import * as stackTraceParser from 'stacktrace-parser';
import { StackFrame } from "stacktrace-parser";

/**
* A class for converting stacktraces, mangled by transpiling, back to match the originating files.
Expand Down Expand Up @@ -38,49 +38,49 @@ export class StackConverter {
try {
const stat = await fs.lstat(this.fileName);
if (!stat) {
return {error: `file "${this.fileName}" does not exist`};
return { error: `file "${this.fileName}" does not exist` };
}
this.fromDirectory = stat.isDirectory()
this.fromDirectory = stat.isDirectory();
if (this.fromDirectory) {
return await this.initFromDirectory();
}
return await this.initFromFile();
} catch (err) {
return {error: err.message}
return { error: err.message };
}
}

private static async sourceMapFromFile(file: string): Promise<{sourceMap?: SourceMapConsumer, error?: string}> {
if (!file) {
return {error: 'could not initialize StackConverter, fileName not set'};
return { error: 'could not initialize StackConverter, fileName not set' };
}
// if the file doesn't exist, return error string
const stat = await fs.lstat(file);
if (!stat) {
return {error: `file "${file}" does not exist`}
return { error: `file "${file}" does not exist` };
}

const fileData = await fs.readFile(file, {
encoding: "utf8",
flag: "r",
});
const parsedSourceMap = JSON.parse(fileData);
const sourceMap = await new SourceMapConsumer(parsedSourceMap)
return {sourceMap};
const sourceMap = await new SourceMapConsumer(parsedSourceMap);
return { sourceMap };
}

private async initFromFile(): Promise<{ error?: string }> {
const { sourceMap, error } = await StackConverter.sourceMapFromFile(this.fileName);
if (error) {
return {error};
return { error };
}
this.sourceMapConsumer = sourceMap;

if (!this.sourceMapConsumer) {
return {error: `cannot init, error loading source map file ${this.fileName}`};
return { error: `cannot init, error loading source map file ${this.fileName}` };
}
this.initialized = true;
return {}
return {};
}

private async mapFilesInDir(): Promise<boolean> {
Expand All @@ -95,7 +95,7 @@ export class StackConverter {
private async initFromDirectory(): Promise<{ error?: string }> {
// confirm that there are sourceMap files in directory
if (!await this.mapFilesInDir()) {
return {error: `there are no map files in directory ${this.fileName}`};
return { error: `there are no map files in directory ${this.fileName}` };
}
this.initialized = true;
return {};
Expand All @@ -110,15 +110,15 @@ export class StackConverter {
public async convert(stackString: string): Promise<{error?: string, stack?: string}>
{
if (!this.initialized) {
return {error: 'have not initialized, call init() first'};
return { error: 'have not initialized, call init() first' };
}
const stackFrames = stackTraceParser.parse(stackString);
if (!stackString) {
// an empty stack is converted to an empty stack.
return {stack: ''};
return { stack: '' };
}
if (stackFrames.length == 0) {
return {error: 'no stacks found in input'};
return { error: 'no stacks found in input' };
}

if (this.fromDirectory) {
Expand All @@ -127,23 +127,23 @@ export class StackConverter {

const INDENT = ' ';
const buff: string[] = [];
stackFrames.forEach(({methodName, lineNumber, column}) => {
stackFrames.forEach(({ methodName, lineNumber, column }) => {
const funcName = methodName || '';
try {
if (!lineNumber || (lineNumber < 1)) {
buff.push(`${INDENT}at ${funcName}`);
} else {
const origPos = this.sourceMapConsumer.originalPositionFor({line: lineNumber, column})
const origPos = this.sourceMapConsumer.originalPositionFor({ line: lineNumber, column });
if (origPos && origPos.line != null) {
const origName = origPos.name || '<unknown>';
buff.push(`${INDENT}at ${origName} (${origPos.source}:${origPos.line}:${origPos.column})`)
buff.push(`${INDENT}at ${origName} (${origPos.source}:${origPos.line}:${origPos.column})`);
}
}
} catch (err) {
buff.push(`${INDENT}**could not convert frame**, err = ${err}`);
}
});
return {stack: buff.join('\n')};
return { stack: buff.join('\n') };
}

private static frameLine(methodName: string, file: string, line: number, column: number, comment?: string): string {
Expand All @@ -157,7 +157,7 @@ export class StackConverter {
const sourceMapErrors: { [filename: string]: boolean } = {};

for (const frame of stackFrames) {
const {file, methodName, lineNumber, column} = frame;
const { file, methodName, lineNumber, column } = frame;
const mapFile = path.join(this.fileName, path.basename(file) + '.map');
if (mapFile in sourceMapErrors) {
// skip loading maps for files that failed
Expand All @@ -167,31 +167,31 @@ export class StackConverter {
const funcName = methodName || '';
try {
if (!lineNumber || (lineNumber < 1)) {
buff.push(`${StackConverter.INDENT}at ${funcName}`)
buff.push(`${StackConverter.INDENT}at ${funcName}`);
} else {
let sm: SourceMapConsumer = get(sourceMaps, mapFile);
if (!sm) {
const { sourceMap, error } = await StackConverter.sourceMapFromFile(mapFile);
if (!sourceMap || error) {
// could not load so don't convert frame
set(sourceMapErrors, file, true);
buff.push(StackConverter.frameLine(methodName, file, lineNumber, column))
buff.push(StackConverter.frameLine(methodName, file, lineNumber, column));
continue;
}
sourceMaps[mapFile] = sourceMap;
sm = sourceMap;
}
const origPos = sm.originalPositionFor({line: lineNumber, column});
const origPos = sm.originalPositionFor({ line: lineNumber, column });
if (!origPos || !origPos.line) {
buff.push(StackConverter.frameLine(methodName, file, lineNumber, column, 'could not convert'))
buff.push(StackConverter.frameLine(methodName, file, lineNumber, column, 'could not convert'));
} else {
buff.push(StackConverter.frameLine(origPos.name, origPos.source, origPos.line, origPos.column))
buff.push(StackConverter.frameLine(origPos.name, origPos.source, origPos.line, origPos.column));
}
}
} catch (err) {
buff.push(StackConverter.frameLine(methodName, file, lineNumber, column, `could not convert, err = ${err}`));
}
}
return {stack: buff.join('\n')};
return { stack: buff.join('\n') };
}
}
}
Loading

0 comments on commit becd733

Please sign in to comment.