UNPKG

6.4 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "tslib", "fs", "path", "source-map", "../common/ErrorFormatter", "./util"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 var tslib_1 = require("tslib");
13 var fs_1 = require("fs");
14 var path_1 = require("path");
15 var source_map_1 = require("source-map");
16 var ErrorFormatter_1 = tslib_1.__importDefault(require("../common/ErrorFormatter"));
17 var util_1 = require("./util");
18 var NodeErrorFormatter = (function (_super) {
19 tslib_1.__extends(NodeErrorFormatter, _super);
20 function NodeErrorFormatter(executor) {
21 var _this = _super.call(this, executor) || this;
22 _this.fileSourceMaps = {};
23 _this.fileSources = {};
24 return _this;
25 }
26 NodeErrorFormatter.prototype._getSource = function (tracepath) {
27 if (tracepath === '<anonymous>') {
28 return 'anonymous';
29 }
30 var sourcepath = this._getSourceHelper(tracepath);
31 while (sourcepath !== tracepath) {
32 tracepath = sourcepath;
33 sourcepath = this._getSourceHelper(tracepath);
34 }
35 return tracepath;
36 };
37 NodeErrorFormatter.prototype._getSourceHelper = function (tracepath) {
38 var match;
39 var source;
40 var line;
41 var col;
42 var map;
43 var originalPos;
44 var result;
45 if (!(match = /^(.*?):(\d+)(:\d+)?$/.exec(tracepath))) {
46 return tracepath;
47 }
48 tracepath = match[1];
49 line = Number(match[2]);
50 col = match[3] ? Number(match[3].substring(1)) : null;
51 if (tracepath.indexOf(this.executor.config.serverUrl) === 0) {
52 tracepath = tracepath.slice(this.executor.config.serverUrl.length);
53 tracepath = tracepath.replace(/^__intern\//, this.executor.config.internPath);
54 }
55 tracepath = path_1.resolve(tracepath);
56 var instrumentedStore = this.executor.instrumentedMapStore;
57 if (tracepath in instrumentedStore.data) {
58 map = new source_map_1.SourceMapConsumer(instrumentedStore.data[tracepath].data);
59 originalPos = this.getOriginalPosition(map, line, col);
60 line = originalPos.line;
61 col = originalPos.column;
62 if (originalPos.source) {
63 source = originalPos.source;
64 }
65 }
66 source = source || tracepath;
67 var sourceMapStore = this.executor.sourceMapStore;
68 if (source in sourceMapStore.data) {
69 map = new source_map_1.SourceMapConsumer(sourceMapStore.data[source].data);
70 }
71 else {
72 map = this.getSourceMap(source);
73 }
74 source = path_1.relative('.', source);
75 if (map) {
76 originalPos = this.getOriginalPosition(map, line, col);
77 line = originalPos.line;
78 col = originalPos.column;
79 if (originalPos.source) {
80 if (originalPos.source.indexOf('/') === -1 ||
81 /\.\.?\//.test(originalPos.source)) {
82 source = path_1.join(path_1.dirname(source), originalPos.source);
83 }
84 else {
85 source = originalPos.source;
86 }
87 }
88 }
89 source = path_1.relative('.', source);
90 result = source + ':' + line;
91 if (col !== null) {
92 result += ':' + col;
93 }
94 return result;
95 };
96 NodeErrorFormatter.prototype.getOriginalPosition = function (map, line, column) {
97 var originalPosition = map.originalPositionFor({
98 line: line,
99 column: column,
100 });
101 if (originalPosition.line != null) {
102 return originalPosition;
103 }
104 var entries = [];
105 map.eachMapping(function (entry) {
106 if (entry.generatedLine === line) {
107 entries.push(entry);
108 }
109 }, null, source_map_1.SourceMapConsumer.GENERATED_ORDER);
110 if (entries.length === 0) {
111 return { line: line, column: column };
112 }
113 var position = entries[0];
114 if (column != null) {
115 var entry = void 0;
116 for (var i = 1; i < entries.length; i++) {
117 entry = entries[i];
118 if (column > position.generatedColumn &&
119 column >= entry.generatedColumn) {
120 position = entry;
121 }
122 }
123 }
124 return {
125 line: position.originalLine,
126 column: position.originalColumn,
127 source: position.source,
128 };
129 };
130 NodeErrorFormatter.prototype.getSourceMap = function (filepath) {
131 if (filepath in this.fileSourceMaps) {
132 return this.fileSourceMaps[filepath];
133 }
134 try {
135 var data = void 0;
136 if (filepath in this.fileSources) {
137 data = this.fileSources[filepath];
138 }
139 else {
140 data = fs_1.readFileSync(filepath).toString('utf-8');
141 this.fileSources[filepath] = data;
142 }
143 var rawMap = util_1.readSourceMap(filepath, data);
144 if (rawMap) {
145 this.fileSourceMaps[filepath] = new source_map_1.SourceMapConsumer(rawMap);
146 return this.fileSourceMaps[filepath];
147 }
148 }
149 catch (error) {
150 }
151 };
152 return NodeErrorFormatter;
153 }(ErrorFormatter_1.default));
154 exports.default = NodeErrorFormatter;
155});
156//# sourceMappingURL=ErrorFormatter.js.map
\No newline at end of file