bloop-sdk 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jonathan Conway
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @bloop/sdk
2
+
3
+ Lightweight error reporting SDK for [Bloop](https://github.com/jaikoo/eewwror) — self-hosted error tracking.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @bloop/sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { BloopClient } from "@bloop/sdk";
15
+
16
+ const bloop = new BloopClient({
17
+ endpoint: "https://errors.myapp.com",
18
+ projectKey: "bloop_abc123...",
19
+ environment: "production",
20
+ release: "1.2.0",
21
+ });
22
+
23
+ // Capture an Error object
24
+ try {
25
+ riskyOperation();
26
+ } catch (err) {
27
+ bloop.captureError(err, { route: "POST /api/users", httpStatus: 500 });
28
+ }
29
+
30
+ // Capture a structured event
31
+ bloop.capture({
32
+ errorType: "ValidationError",
33
+ message: "Invalid email format",
34
+ route: "POST /api/users",
35
+ httpStatus: 422,
36
+ });
37
+
38
+ // Flush on shutdown
39
+ await bloop.shutdown();
40
+ ```
41
+
42
+ ## Features
43
+
44
+ - **Zero dependencies** — Uses the Web Crypto API (works in Node.js and browsers)
45
+ - **Automatic batching** — Events are buffered and sent in configurable batches
46
+ - **HMAC-SHA256 signing** — All requests are cryptographically signed
47
+ - **Graceful shutdown** — `shutdown()` flushes pending events before exit
48
+
49
+ ## License
50
+
51
+ MIT
@@ -0,0 +1,44 @@
1
+ export interface BloopConfig {
2
+ endpoint: string;
3
+ /** Project API key (used for both auth and project identification). */
4
+ projectKey: string;
5
+ /** @deprecated Use projectKey instead. Legacy HMAC secret for backward compat. */
6
+ secret?: string;
7
+ source?: "ios" | "android" | "api";
8
+ environment: string;
9
+ release: string;
10
+ maxBufferSize?: number;
11
+ flushIntervalMs?: number;
12
+ }
13
+ export interface ErrorEvent {
14
+ errorType: string;
15
+ message: string;
16
+ route?: string;
17
+ stack?: string;
18
+ httpStatus?: number;
19
+ requestId?: string;
20
+ userIdHash?: string;
21
+ metadata?: Record<string, unknown>;
22
+ }
23
+ export declare class BloopClient {
24
+ private config;
25
+ private buffer;
26
+ private timer;
27
+ private signingKey;
28
+ private keyReady;
29
+ constructor(config: BloopConfig);
30
+ /** Import the HMAC key using Web Crypto API. */
31
+ private importKey;
32
+ /** Capture an Error object. */
33
+ captureError(error: Error, context?: Partial<ErrorEvent>): void;
34
+ /** Capture a structured error event. */
35
+ capture(event: ErrorEvent): void;
36
+ /** Flush buffered events to the server. */
37
+ flush(): Promise<void>;
38
+ /** Express/Koa error middleware. */
39
+ errorMiddleware(): (err: Error, req: any, _res: any, next: any) => void;
40
+ /** Stop the flush timer. Call on process shutdown. */
41
+ close(): Promise<void>;
42
+ /** Sign a body string using Web Crypto HMAC-SHA256. */
43
+ private sign;
44
+ }
package/dist/bloop.js ADDED
@@ -0,0 +1,145 @@
1
+ export class BloopClient {
2
+ constructor(config) {
3
+ this.buffer = [];
4
+ this.timer = null;
5
+ this.signingKey = null;
6
+ this.config = {
7
+ source: "api",
8
+ maxBufferSize: 20,
9
+ flushIntervalMs: 5000,
10
+ ...config,
11
+ };
12
+ // Pre-import the signing key (async)
13
+ this.keyReady = this.importKey();
14
+ this.timer = setInterval(() => this.flush(), this.config.flushIntervalMs);
15
+ }
16
+ /** Import the HMAC key using Web Crypto API. */
17
+ async importKey() {
18
+ const secret = this.config.projectKey || this.config.secret || "";
19
+ const encoder = new TextEncoder();
20
+ this.signingKey = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
21
+ }
22
+ /** Capture an Error object. */
23
+ captureError(error, context) {
24
+ this.capture({
25
+ errorType: error.constructor.name,
26
+ message: error.message,
27
+ stack: error.stack,
28
+ ...context,
29
+ });
30
+ }
31
+ /** Capture a structured error event. */
32
+ capture(event) {
33
+ const ingestEvent = {
34
+ timestamp: Date.now(),
35
+ source: this.config.source,
36
+ environment: this.config.environment,
37
+ release: this.config.release,
38
+ error_type: event.errorType,
39
+ message: event.message,
40
+ };
41
+ if (event.route)
42
+ ingestEvent.route_or_procedure = event.route;
43
+ if (event.stack)
44
+ ingestEvent.stack = event.stack.slice(0, 8192);
45
+ if (event.httpStatus)
46
+ ingestEvent.http_status = event.httpStatus;
47
+ if (event.requestId)
48
+ ingestEvent.request_id = event.requestId;
49
+ if (event.userIdHash)
50
+ ingestEvent.user_id_hash = event.userIdHash;
51
+ if (event.metadata)
52
+ ingestEvent.metadata = event.metadata;
53
+ this.buffer.push(ingestEvent);
54
+ if (this.buffer.length >= (this.config.maxBufferSize ?? 20)) {
55
+ this.flush();
56
+ }
57
+ }
58
+ /** Flush buffered events to the server. */
59
+ async flush() {
60
+ if (this.buffer.length === 0)
61
+ return;
62
+ // Ensure key is ready
63
+ await this.keyReady;
64
+ const events = this.buffer.splice(0);
65
+ const body = JSON.stringify({ events });
66
+ const signature = await this.sign(body);
67
+ const headers = {
68
+ "Content-Type": "application/json",
69
+ "X-Signature": signature,
70
+ };
71
+ // Send project key header if using new auth
72
+ if (this.config.projectKey) {
73
+ headers["X-Project-Key"] = this.config.projectKey;
74
+ }
75
+ try {
76
+ const resp = await fetch(`${this.config.endpoint}/v1/ingest/batch`, {
77
+ method: "POST",
78
+ headers,
79
+ body,
80
+ });
81
+ if (!resp.ok) {
82
+ console.warn(`[bloop] flush failed: ${resp.status}`);
83
+ }
84
+ }
85
+ catch (err) {
86
+ // Silently fail - error reporting shouldn't break the app
87
+ if (typeof globalThis !== "undefined" &&
88
+ globalThis.process?.env?.NODE_ENV !== "production") {
89
+ console.warn("[bloop] flush error:", err);
90
+ }
91
+ }
92
+ }
93
+ /** Express/Koa error middleware. */
94
+ errorMiddleware() {
95
+ return (err, req, _res, next) => {
96
+ this.captureError(err, {
97
+ route: req?.path || req?.url,
98
+ httpStatus: err instanceof HttpError ? err.statusCode : 500,
99
+ requestId: req?.headers?.["x-request-id"],
100
+ });
101
+ next(err);
102
+ };
103
+ }
104
+ /** Stop the flush timer. Call on process shutdown. */
105
+ async close() {
106
+ if (this.timer) {
107
+ clearInterval(this.timer);
108
+ this.timer = null;
109
+ }
110
+ await this.flush();
111
+ }
112
+ /** Sign a body string using Web Crypto HMAC-SHA256. */
113
+ async sign(body) {
114
+ if (!this.signingKey) {
115
+ await this.keyReady;
116
+ }
117
+ const encoder = new TextEncoder();
118
+ const signature = await crypto.subtle.sign("HMAC", this.signingKey, encoder.encode(body));
119
+ return Array.from(new Uint8Array(signature))
120
+ .map((b) => b.toString(16).padStart(2, "0"))
121
+ .join("");
122
+ }
123
+ }
124
+ class HttpError extends Error {
125
+ constructor(message, statusCode) {
126
+ super(message);
127
+ this.statusCode = statusCode;
128
+ }
129
+ }
130
+ // --- Usage ---
131
+ // const client = new BloopClient({
132
+ // endpoint: "https://errors.yoursite.com",
133
+ // projectKey: "bloop_abc123...",
134
+ // environment: "production",
135
+ // release: "1.0.0",
136
+ // });
137
+ //
138
+ // // Capture errors
139
+ // client.captureError(new Error("something broke"), { route: "/api/users" });
140
+ //
141
+ // // Express middleware
142
+ // app.use(client.errorMiddleware());
143
+ //
144
+ // // Graceful shutdown
145
+ // process.on("SIGTERM", () => client.close());
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "bloop-sdk",
3
+ "version": "0.2.0",
4
+ "description": "Lightweight error reporting SDK for bloop",
5
+ "main": "dist/bloop.js",
6
+ "types": "dist/bloop.d.ts",
7
+ "files": ["dist"],
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "prepublishOnly": "npm run build"
11
+ },
12
+ "keywords": ["error-tracking", "observability", "bloop"],
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/jaikoo/bloop-js"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^5.0.0"
20
+ }
21
+ }