Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions workers/javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ export default class JavascriptEventWorker extends EventWorker {
bias: SourceMapConsumer.LEAST_UPPER_BOUND,
});

/**
* source-map-js returns nulls when the generated position is not in the map.
* Assigning them would wipe the catcher coordinates and set sourceCode to [].
*
* Fixes bug: https://github.com/codex-team/hawk.workers/issues/600
*/
if (!originalLocation.source) {
return stackFrame;
}

/**
* Source code lines
*/
Expand Down
50 changes: 50 additions & 0 deletions workers/javascript/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,56 @@ describe('JavaScript event worker', () => {
await worker.finish();
});

it('should keep the original frame when the source map has no mapping', async () => {
/**
* Arrange
*/
const worker = new JavascriptEventWorker();

jest.spyOn(worker, 'addTask');
jest.spyOn(JavascriptEventWorker.prototype as any, 'loadSourceMapFile')
.mockResolvedValue(sourceMapFileContent);
await worker.start();

const workerEvent = createEventMock({ withBacktrace: true });
const originalFrame = {
file: 'file:///main.js',
line: 9999,
column: 1,
function: 'handler',
};

/**
* Copy the frame so a mutation inside the worker cannot hide a failed lookup
*/
workerEvent.payload.backtrace = [ { ...originalFrame } ];

const release = createReleaseMock({
projectId: workerEvent.projectId,
release: workerEvent.payload.release,
});

await db.collection('releases').insertOne(release);

/**
* Act
*/
await worker.handle(workerEvent);

/**
* Assert
*/
expect(worker.addTask).toHaveBeenCalledWith(
WorkerNames.GROUPER,
expect.objectContaining({
payload: expect.objectContaining({
backtrace: [ originalFrame ],
}),
})
);
await worker.finish();
});

it('should use cache while processing source maps', async () => {
/**
* Arrange
Expand Down
6 changes: 3 additions & 3 deletions workers/paymaster/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ describe('PaymasterWorker', () => {
/**
* Arrange
*/
const currentDate = new Date();
const currentDate = new Date('2005-12-22');
const paidUntil = new Date(currentDate.getTime());

paidUntil.setMonth(paidUntil.getMonth() + 3);
Expand Down Expand Up @@ -590,7 +590,7 @@ describe('PaymasterWorker', () => {
/**
* Arrange
*/
const currentDate = new Date();
const currentDate = new Date('2005-12-22');
const paidUntil = new Date(currentDate.getTime());

paidUntil.setDate(paidUntil.getDate() + 1);
Expand Down Expand Up @@ -647,7 +647,7 @@ describe('PaymasterWorker', () => {
/**
* Arrange
*/
const currentDate = new Date();
const currentDate = new Date('2005-12-22');
const lastChargeDate = new Date(currentDate.getTime());

lastChargeDate.setMonth(lastChargeDate.getMonth() - 1); // Set last charge date to 1 month ago
Expand Down
13 changes: 11 additions & 2 deletions workers/telegram/src/templates/event.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type { EventsTemplateVariables, TemplateEventData } from 'hawk-worker-sender/types/template-variables';

/**
* Escape characters that break Telegram legacy Markdown parsing
*
* @param text - raw text
*/
export function escapeMarkdown(text: string): string {
return text.replace(/[_*`[]/g, '\\$&');
}

/**
* Return tpl with data substitutions
*
Expand All @@ -17,10 +26,10 @@ export default function render(tplData: EventsTemplateVariables): string {
location = '\n' + '```\n' + location + '\n```'; // markdown code block
}

return ''.concat(event.payload.title,
return ''.concat(escapeMarkdown(event.payload.title),
'\n',
location || '',
'\n',
`[View details](${eventURL}) `, `| *${tplData.project.name}*`, ` | ${eventInfo.newCount} new (${eventInfo.event.totalCount} total)`
`[View details](${eventURL}) `, `| ${escapeMarkdown(tplData.project.name)}`, ` | ${eventInfo.newCount} new (${eventInfo.event.totalCount} total)`
);
}
5 changes: 3 additions & 2 deletions workers/telegram/src/templates/several-events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EventsTemplateVariables } from 'hawk-worker-sender/types/template-variables';
import { escapeMarkdown } from './event';

/**
* Return tpl with data substitutions
Expand All @@ -10,10 +11,10 @@ export default function render(tplData: EventsTemplateVariables): string {
let message = tplData.events.length + ' new events\n\n';

tplData.events.forEach(({ event, newCount }) => {
message += `(${newCount}) ${event.payload.title} \n`;
message += `(${newCount}) ${escapeMarkdown(event.payload.title)} \n`;
});

message += `\n[View all events](${projectUrl}) | *${tplData.project.name}*`;
message += `\n[View all events](${projectUrl}) | ${escapeMarkdown(tplData.project.name)}`;

return message;
}
7 changes: 6 additions & 1 deletion workers/telegram/tests/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventNotification, SeveralEventsNotification } from 'hawk-worker-sender
import { DecodedGroupedEvent, ProjectDBScheme } from '@hawk.so/types';
import TelegramProvider from 'hawk-worker-telegram/src/provider';
import templates from '../src/templates';
import EventTpl from '../src/templates/event';
import EventTpl, { escapeMarkdown } from '../src/templates/event';
import { ObjectId } from 'mongodb';

/**
Expand All @@ -13,6 +13,11 @@ describe('TelegramProvider', () => {
* Check templates rendering
*/
describe('templates', () => {
it('should escape Markdown entities in event titles', () => {
expect(escapeMarkdown('No such file: /home/market_static/*.csv [`x`]'))
.toBe('No such file: /home/market\\_static/\\*.csv \\[\\`x\\`]');
});

/**
* Check that rendering of a single event message works without errors
*/
Expand Down
Loading