Skip to content

Commit 9e5d66a

Browse files
committed
refactor(javascript): MessageProcessor abstraction added and implemented in catcher event processing pipeline
- BreadcrumbStore abstraction and browser implementation added - MessageProcessor interface and MessageHint type - BrowserMessageProcessor, BreadcrumbsMessageProcessor, ConsoleCatcherMessageProcessor, DebugMessageProcessor - replaced inline addon logic with sequential MessageProcessor pipeline - rearranged core files and directories
1 parent 8339ef4 commit 9e5d66a

47 files changed

Lines changed: 704 additions & 361 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/core/src/errors.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/core/src/users/hawk-user-manager.ts renamed to packages/core/src/features/hawk-user-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AffectedUser } from '@hawk.so/types';
2-
import type { HawkStorage } from '../storages/hawk-storage';
2+
import type { HawkStorage } from '../utils/hawk-storage';
33
import { id } from '../utils/id';
44
import type { RandomGenerator } from '../utils/random';
55

packages/core/src/modules/stack-parser.ts renamed to packages/core/src/features/stack-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { StackFrame } from 'error-stack-parser';
22
import ErrorStackParser from 'error-stack-parser';
33
import type { BacktraceFrame, SourceCodeLine } from '@hawk.so/types';
4-
import fetchTimer from './fetch-timer';
4+
import fetchTimer from '../utils/fetch-timer';
55

66
/**
77
* This module prepares parsed backtrace

packages/core/src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
export type { HawkStorage } from './storages/hawk-storage';
1+
export type { HawkStorage } from './utils/hawk-storage';
22
export type { RandomGenerator } from './utils/random';
3-
export { HawkUserManager } from './users/hawk-user-manager';
4-
export type { Logger, LogType } from './logger/logger';
5-
export { isLoggerSet, setLogger, resetLogger, log } from './logger/logger';
3+
export { HawkUserManager } from './features/hawk-user-manager';
4+
export type { Logger, LogType } from './utils/logger';
5+
export { isLoggerSet, setLogger, resetLogger, log } from './utils/logger';
66
export { validateUser, validateContext, isValidEventPayload, isValidBreadcrumb } from './utils/validation';
77
export { isPlainObject, isArray, isClassPrototype, isClassInstance, isString } from './utils/type-guards';
8-
export { Sanitizer } from './modules/sanitizer';
9-
export type { Transport } from './transports/transport';
10-
export type { SanitizerTypeHandler } from './modules/sanitizer';
11-
export { StackParser } from './modules/stack-parser';
8+
export { Sanitizer } from './utils/sanitizer';
9+
export type { Transport } from './utils/transport';
10+
export type { SanitizerTypeHandler } from './utils/sanitizer';
11+
export { StackParser } from './features/stack-parser';
1212
export { buildElementSelector } from './utils/selector';
13-
export { EventRejectedError } from './errors';
1413
export { isErrorProcessed, markErrorAsProcessed } from './utils/event';
14+
export type { BreadcrumbStore, BreadcrumbsAPI, BreadcrumbHint, BreadcrumbInput } from './types/breadcrumb-store';
15+
export type { MessageProcessor, ProcessingPayload } from './types/message-processor';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Breadcrumb } from '@hawk.so/types';
2+
3+
/**
4+
* Hint passed to beforeBreadcrumb callback.
5+
*/
6+
export interface BreadcrumbHint {
7+
[key: string]: unknown;
8+
}
9+
10+
/**
11+
* Breadcrumb input type - breadcrumb data with optional timestamp.
12+
*/
13+
export type BreadcrumbInput = Omit<Breadcrumb, 'timestamp'> & { timestamp?: number };
14+
15+
/**
16+
* Contract for breadcrumb storage. Also serves as public breadcrumbs API.
17+
*/
18+
export interface BreadcrumbStore {
19+
add(breadcrumb: BreadcrumbInput, hint?: BreadcrumbHint): void;
20+
get(): Breadcrumb[];
21+
clear(): void;
22+
}
23+
24+
/**
25+
* @deprecated Use {@link BreadcrumbStore} instead.
26+
*/
27+
export type BreadcrumbsAPI = BreadcrumbStore;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { CatcherMessagePayload, CatcherMessageType } from '@hawk.so/types';
2+
3+
/**
4+
* Extracted addons type from catcher message payload.
5+
*
6+
* @typeParam T - catcher message type
7+
*/
8+
type ExtractAddons<T extends CatcherMessageType> =
9+
CatcherMessagePayload<T> extends { addons?: infer A } ? A : never;
10+
11+
/**
12+
* Payload type used during message processing pipeline.
13+
*
14+
* Same as {@link CatcherMessagePayload} but with `addons` always defined and partially filled —
15+
* processors may contribute individual addon fields independently of each other.
16+
*
17+
* @typeParam T - catcher message type this payload belongs to
18+
*/
19+
export type ProcessingPayload<T extends CatcherMessageType> =
20+
Omit<CatcherMessagePayload<T>, 'addons'> & {
21+
addons: Partial<ExtractAddons<T>>;
22+
};
23+
24+
/**
25+
* Single step in message processing pipeline before message is sent.
26+
*
27+
* @typeParam T - catcher message type this processor handles
28+
*/
29+
export interface MessageProcessor<T extends CatcherMessageType = CatcherMessageType> {
30+
/**
31+
* Handles input message. May mutate, replace or drop it.
32+
*
33+
* Dropped message won't be sent.
34+
*
35+
* @param payload - processed event message payload with partially-built addons
36+
* @param error - original error
37+
* @returns modified payload, or `null` to drop message
38+
*/
39+
apply(
40+
payload: ProcessingPayload<T>,
41+
error?: Error | string,
42+
): ProcessingPayload<T> | null
43+
}

packages/core/src/utils/event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { log } from '../logger/logger';
1+
import { log } from './logger';
22

33
/**
44
* Symbol to mark error as processed by Hawk
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { log } from '../logger/logger';
1+
import { log } from '../utils/logger';
22

33
/**
44
* Sends AJAX request and wait for some time.
File renamed without changes.

0 commit comments

Comments
 (0)