|
| 1 | +# Hawk JavaScript Core |
| 2 | + |
| 3 | +Environment-agnostic base for Hawk JavaScript/TypeScript SDKs. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```shell |
| 8 | +npm install @hawk.so/core --save |
| 9 | +``` |
| 10 | + |
| 11 | +```shell |
| 12 | +yarn add @hawk.so/core |
| 13 | +``` |
| 14 | + |
| 15 | +## What is inside |
| 16 | + |
| 17 | +| Export | Purpose | |
| 18 | +|----------------------------------------------------------------------------------|-------------------------------------------------------------------------------------| |
| 19 | +| `BaseCatcher` | Abstract catcher. Handles send pipeline, context, user, breadcrumbs. | |
| 20 | +| `BeforeSendHook` | User hook applied to every outgoing event. Applied after `MessageProcessor`'s. | |
| 21 | +| `BreadcrumbStore` | Breadcrumbs storage contract. Also serves as public breadcrumbs API. | |
| 22 | +| `HawkUserManager` | Resolves affected user. Persists auto-generated anonymous ID via `HawkStorage`. | |
| 23 | +| `HawkStorage` | Key–value persistence interface (e.g. `localStorage`, file, memory). | |
| 24 | +| `log` / `setLogger` | Binding point for environment-specific logger implementation. | |
| 25 | +| `MessageProcessor` | Pipeline step applied to every outgoing event. May enrich, replace or drop payload. | |
| 26 | +| `RandomGenerator` | Source of random bytes for ID generation. | |
| 27 | +| `Transport` | Interface for message delivery to Collector. | |
| 28 | +| `Sanitizer` | Trims long strings, flattens big/deep objects, formats class instances. | |
| 29 | +| `StackParser` | Parses `Error.stack` into structured backtrace frames with source code context. | |
| 30 | +| `validateUser` / `validateContext` / `isValidEventPayload` / `isValidBreadcrumb` | Runtime validators used across SDKs. | |
| 31 | + |
| 32 | +## Building your own catcher |
| 33 | + |
| 34 | +Extend `BaseCatcher` and supply environment-specific pieces via constructor. |
| 35 | + |
| 36 | +```ts |
| 37 | +import { |
| 38 | + BaseCatcher, |
| 39 | + HawkUserManager, |
| 40 | + setLogger, |
| 41 | + type Transport, |
| 42 | + type HawkStorage, |
| 43 | + type RandomGenerator, |
| 44 | +} from '@hawk.so/core'; |
| 45 | + |
| 46 | +// 1. Provide a transport that delivers assembled messages to Collector |
| 47 | +class MyTransport implements Transport<'errors/javascript'> { |
| 48 | + public async send(message): Promise<void> { |
| 49 | + // e.g. WebSocket, fetch, IPC — whatever fits the runtime |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// 2. Provide persistence for the anonymous user ID |
| 54 | +class MyStorage implements HawkStorage { |
| 55 | + public getItem(key) { |
| 56 | + // ... |
| 57 | + } |
| 58 | + |
| 59 | + public setItem(key, value) { |
| 60 | + // ... |
| 61 | + } |
| 62 | + |
| 63 | + public removeItem(key) { |
| 64 | + // ... |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// 3. Provide randomness (crypto.getRandomValues, node:crypto, …) |
| 69 | +class MyRandom implements RandomGenerator { |
| 70 | + public getRandomNumbers(length) { /* … */ |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// 4. Optionally, register logger |
| 75 | +setLogger((msg, type, args) => console[type ?? 'log'](msg, args)); |
| 76 | + |
| 77 | +// 5. Extend BaseCatcher |
| 78 | +export class MyCatcher extends BaseCatcher<'errors/javascript'> { |
| 79 | + public constructor(token: string) { |
| 80 | + const userManager = new HawkUserManager(new MyStorage(), new MyRandom()); |
| 81 | + |
| 82 | + super( |
| 83 | + token, |
| 84 | + new MyTransport(), |
| 85 | + userManager, |
| 86 | + /* release */ undefined, |
| 87 | + /* context */ undefined, |
| 88 | + /* beforeSend */ undefined, |
| 89 | + /* breadcrumbStore */ undefined |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + protected getCatcherType() { |
| 94 | + return 'errors/javascript' as const; |
| 95 | + } |
| 96 | + |
| 97 | + protected getCatcherVersion() { |
| 98 | + return '1.0.0'; |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +This project is licensed under the **GNU Affero General Public License v3.0 (AGPL-3.0)**. |
| 106 | +See the [LICENSE](../../LICENSE) file for the full text. |
0 commit comments