Skip to content

Commit b4ab90a

Browse files
committed
refactor(core): Extract shared modules and utilities
- fetchTimer function moved to @hawk.so/core - StackParser moved to @hawk.so/core - event utility functions moved to @hawk.so/core - selector utility functions moved to @hawk.so/core - EventRejectedError moved to @hawk.so/core
1 parent 2a179c0 commit b4ab90a

File tree

9 files changed

+25
-20
lines changed

9 files changed

+25
-20
lines changed

packages/core/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ export { validateUser, validateContext, isValidEventPayload, isValidBreadcrumb }
77
export { isPlainObject, isArray, isClassPrototype, isClassInstance, isString } from './utils/type-guards';
88
export { Sanitizer } from './modules/sanitizer';
99
export type { Transport } from './transports/transport';
10+
export type { SanitizerTypeHandler } from './modules/sanitizer';
11+
export { StackParser } from './modules/stack-parser';
12+
export { buildElementSelector } from './utils/selector';
13+
export { EventRejectedError } from './errors';
14+
export { isErrorProcessed, markErrorAsProcessed } from './utils/event';

packages/javascript/src/modules/fetchTimer.ts renamed to packages/core/src/modules/fetch-timer.ts

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

33
/**
44
* Sends AJAX request and wait for some time.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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 './fetchTimer';
4+
import fetchTimer from './fetch-timer';
55

66
/**
77
* This module prepares parsed backtrace
88
*/
9-
export default class StackParser {
9+
export class StackParser {
1010
/**
1111
* Prevents loading one file several times
1212
* name -> content
@@ -48,7 +48,7 @@ export default class StackParser {
4848
try {
4949
if (!frame.fileName) {
5050
return null;
51-
};
51+
}
5252

5353
if (!this.isValidUrl(frame.fileName)) {
5454
return null;
@@ -118,9 +118,9 @@ export default class StackParser {
118118
/**
119119
* Downloads source file
120120
*
121-
* @param {string} fileName - name of file to download
121+
* @param fileName - name of file to download
122122
*/
123-
private async loadSourceFile(fileName): Promise<string | null> {
123+
private async loadSourceFile(fileName: string): Promise<string | null> {
124124
if (this.sourceFilesCache[fileName] !== undefined) {
125125
return this.sourceFilesCache[fileName];
126126
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { log } from '@hawk.so/core';
1+
import { log } from '../logger/logger';
22

33
/**
44
* Symbol to mark error as processed by Hawk
55
*/
66
const errorSentShadowProperty = Symbol('__hawk_processed__');
77

88
/**
9-
* Check if the error has alrady been sent to Hawk.
9+
* Check if the error has already been sent to Hawk.
1010
*
1111
* Motivation:
1212
* Some integrations may catch errors on their own side and then normally re-throw them down.
@@ -20,7 +20,7 @@ export function isErrorProcessed(error: unknown): boolean {
2020
return false;
2121
}
2222

23-
return error[errorSentShadowProperty] === true;
23+
return (error as Record<symbol, unknown>)[errorSentShadowProperty] === true;
2424
}
2525

2626
/**
@@ -35,7 +35,7 @@ export function markErrorAsProcessed(error: unknown): void {
3535
}
3636

3737
Object.defineProperty(error, errorSentShadowProperty, {
38-
enumerable: false, // Prevent from beight collected by Hawk
38+
enumerable: false, // Prevent from being collected by Hawk
3939
value: true,
4040
writable: true,
4141
configurable: true,

packages/javascript/src/addons/breadcrumbs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* @file Breadcrumbs module - captures chronological trail of events before an error
33
*/
44
import type { Breadcrumb, BreadcrumbLevel, BreadcrumbType, Json, JsonNode } from '@hawk.so/types';
5-
import { isValidBreadcrumb, log, Sanitizer } from '@hawk.so/core';
6-
import { buildElementSelector } from '../utils/selector';
5+
import { buildElementSelector, isValidBreadcrumb, log, Sanitizer } from '@hawk.so/core';
76

87
/**
98
* Default maximum number of breadcrumbs to store

packages/javascript/src/catcher.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import './modules/element-sanitizer';
22
import Socket from './modules/socket';
3-
import StackParser from './modules/stackParser';
43
import type { BreadcrumbsAPI, CatcherMessage, HawkInitialSettings, HawkJavaScriptEvent, Transport } from './types';
54
import { VueIntegration } from './integrations/vue';
65
import type {
@@ -12,24 +11,26 @@ import type {
1211
Json,
1312
VueIntegrationAddons
1413
} from '@hawk.so/types';
15-
import type { JavaScriptCatcherIntegrations } from './types/integrations';
16-
import { EventRejectedError } from './errors';
17-
import { isErrorProcessed, markErrorAsProcessed } from './utils/event';
18-
import { BrowserRandomGenerator } from './utils/random';
14+
import type { JavaScriptCatcherIntegrations } from '@/types';
1915
import { ConsoleCatcher } from './addons/consoleCatcher';
2016
import { BreadcrumbManager } from './addons/breadcrumbs';
2117
import {
18+
EventRejectedError,
2219
HawkUserManager,
20+
isErrorProcessed,
2321
isLoggerSet,
2422
isValidEventPayload,
2523
log,
24+
markErrorAsProcessed,
2625
Sanitizer,
2726
setLogger,
27+
StackParser,
2828
validateContext,
2929
validateUser
3030
} from '@hawk.so/core';
3131
import { HawkLocalStorage } from './storages/hawk-local-storage';
3232
import { createBrowserLogger } from './logger/logger';
33+
import { BrowserRandomGenerator } from './utils/random';
3334

3435
/**
3536
* Allow to use global VERSION, that will be overwritten by Webpack
@@ -694,6 +695,6 @@ export default class Catcher {
694695
* @param integrationAddons - extra addons
695696
*/
696697
private appendIntegrationAddons(errorFormatted: CatcherMessage<typeof Catcher.type>, integrationAddons: JavaScriptCatcherIntegrations): void {
697-
Object.assign(errorFormatted.payload.addons!, integrationAddons);
698+
Object.assign(errorFormatted.payload.addons, integrationAddons);
698699
}
699700
}

packages/javascript/src/types/hawk-initial-settings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { EventContext, AffectedUser } from '@hawk.so/types';
1+
import type { AffectedUser, EventContext } from '@hawk.so/types';
22
import type { HawkJavaScriptEvent } from './event';
3-
import type { Transport } from './transport';
3+
import type { Transport } from '@hawk.so/core';
44
import type { BreadcrumbsOptions } from '../addons/breadcrumbs';
55

66
/**

0 commit comments

Comments
 (0)