Skip to content

Commit 084fce2

Browse files
authored
fix(): fix source-map saving (#449)
* fix(): avoid mutations on savedFiles * revert js worker changes * fix(): js worker tests * imp(): catch errors on get function context
1 parent 9515e2f commit 084fce2

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

workers/javascript/src/index.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,20 @@ export default class JavascriptEventWorker extends EventWorker {
228228
* Fixes bug: https://github.com/codex-team/hawk.workers/issues/121
229229
*/
230230
if (originalLocation.source) {
231-
/**
232-
* Get 5 lines above and 5 below
233-
*/
234-
lines = this.readSourceLines(consumer, originalLocation);
231+
try {
232+
/**
233+
* Get 5 lines above and 5 below
234+
*/
235+
lines = this.readSourceLines(consumer, originalLocation);
235236

236-
const _originalContent = consumer.sourceContentFor(originalLocation.source);
237+
const originalContent = consumer.sourceContentFor(originalLocation.source);
237238

238-
// functionContext = this.getFunctionContext(originalContent, originalLocation.line) ?? originalLocation.name;
239+
functionContext = this.getFunctionContext(originalContent, originalLocation.line) ?? originalLocation.name;
240+
} catch(e) {
241+
HawkCatcher.send(e);
242+
this.logger.error('Can\'t get function context');
243+
this.logger.error(e);
244+
}
239245
}
240246

241247
return Object.assign(stackFrame, {
@@ -254,7 +260,7 @@ export default class JavascriptEventWorker extends EventWorker {
254260
* @param line - number of the line from the stack trace
255261
* @returns {string | null} - string of the function context or null if it could not be parsed
256262
*/
257-
private _getFunctionContext(sourceCode: string, line: number): string | null {
263+
private getFunctionContext(sourceCode: string, line: number): string | null {
258264
let functionName: string | null = null;
259265
let className: string | null = null;
260266
let isAsync = false;
@@ -264,6 +270,7 @@ export default class JavascriptEventWorker extends EventWorker {
264270
const ast = parse(sourceCode, {
265271
sourceType: 'module',
266272
plugins: [
273+
'jsx',
267274
'typescript',
268275
'classProperties',
269276
'decorators',
@@ -284,7 +291,7 @@ export default class JavascriptEventWorker extends EventWorker {
284291
ClassDeclaration(path) {
285292
if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
286293
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);
287-
294+
288295
className = path.node.id.name || null;
289296
}
290297
},
@@ -297,7 +304,7 @@ export default class JavascriptEventWorker extends EventWorker {
297304
ClassMethod(path) {
298305
if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
299306
console.log(`class declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);
300-
307+
301308
// Handle different key types
302309
if (path.node.key.type === 'Identifier') {
303310
functionName = path.node.key.name;
@@ -313,7 +320,7 @@ export default class JavascriptEventWorker extends EventWorker {
313320
FunctionDeclaration(path) {
314321
if (path.node.loc && path.node.loc.start.line <= line && path.node.loc.end.line >= line) {
315322
console.log(`function declaration: loc: ${path.node.loc}, line: ${line}, node.start.line: ${path.node.loc.start.line}, node.end.line: ${path.node.loc.end.line}`);
316-
323+
317324
functionName = path.node.id.name || null;
318325
isAsync = path.node.async;
319326
}

workers/release/src/index.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export default class ReleaseWorker extends Worker {
162162
/**
163163
* Iterate all maps of the new release and save only new
164164
*/
165-
let savedFiles = await Promise.all(files.map(async (map: SourceMapDataExtended) => {
165+
const savedFiles = await Promise.all(files.map(async (map: SourceMapDataExtended) => {
166166
/**
167167
* Skip already saved maps
168168
*/
@@ -181,30 +181,29 @@ export default class ReleaseWorker extends Worker {
181181
/**
182182
* Save id of saved file instead
183183
*/
184-
map._id = fileInfo._id;
185-
186-
return map;
184+
return {
185+
...map,
186+
_id: fileInfo._id,
187+
};
187188
} catch (error) {
188189
this.logger.error(`Map ${map.mapFileName} was not saved: ${error}`);
189190
}
190191
}));
191192

192193
/**
193-
* Delete file content after it is saved to the GridFS
194+
* Filter undefined files and then prepare files that would be saved to releases table
195+
* we do not need their content since it would be stored in gridFS
194196
*/
195-
savedFiles.forEach(file => {
196-
delete file.content;
197+
const savedFilesWithoutContent: Omit<SourceMapDataExtended, 'content'>[] = savedFiles.filter(file => {
198+
return file !== undefined;
199+
}).map(({ content, ...rest }) => {
200+
return rest;
197201
});
198202

199-
/**
200-
* Filter unsaved maps
201-
*/
202-
savedFiles = savedFiles.filter((file) => file !== undefined);
203-
204203
/**
205204
* Nothing to save: maps was previously saved
206205
*/
207-
if (savedFiles.length === 0) {
206+
if (savedFilesWithoutContent.length === 0) {
208207
return;
209208
}
210209

@@ -218,7 +217,7 @@ export default class ReleaseWorker extends Worker {
218217
await this.releasesCollection.insertOne({
219218
projectId: projectId,
220219
release: payload.release,
221-
files: savedFiles as SourceMapDataExtended[],
220+
files: savedFilesWithoutContent,
222221
} as ReleaseDBScheme, { session });
223222
}
224223

@@ -228,7 +227,7 @@ export default class ReleaseWorker extends Worker {
228227
}, {
229228
$push: {
230229
files: {
231-
$each: savedFiles as SourceMapDataExtended[],
230+
$each: savedFilesWithoutContent,
232231
},
233232
},
234233
}, { session });

0 commit comments

Comments
 (0)