@codelitdev/observability 0.1.0-alpha.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 CodeLit
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,19 @@
1
+ # @codelitdev/observability
2
+
3
+ Explicit `createObservability` factory for structured stdout logging and
4
+ optional exception/event capture. The package does not read environment
5
+ variables or construct SDK clients at import. Products pass configuration
6
+ and own event catalogs. Tenant identity is an opaque `subjectId`.
7
+
8
+ Pass `posthog: { apiKey }` for best-effort HTTP capture or provide a typed
9
+ client in tests. `logs.otlp` adds an OTLP/HTTP log fan-out while structured
10
+ stdout remains enabled; both remote pipelines are failure-isolated and
11
+ timeout-bounded during shutdown.
12
+
13
+ Next.js (and other browser) apps use `createBrowserObservability` from
14
+ `@codelitdev/observability/browser`. Pass `apiKey` from request-time env
15
+ (`POSTHOG_API_KEY`), not `NEXT_PUBLIC_*`, so OSS images can enable telemetry
16
+ at container start. Missing `apiKey` is a no-op and does not load
17
+ `posthog-js`. After init, call `identify({ id })` with a stable auth user id
18
+ and `reset()` on logout. Session recording uses PostHog JS defaults; enable
19
+ Session replay on the project to record.
@@ -0,0 +1,25 @@
1
+ export type BrowserPerson = {
2
+ id?: string;
3
+ email?: string;
4
+ name?: string | null;
5
+ };
6
+ export type CreateBrowserObservabilityOptions = {
7
+ serviceName: string;
8
+ environment: string;
9
+ apiKey?: string | null;
10
+ host?: string;
11
+ };
12
+ export type BrowserObservability = {
13
+ enabled: boolean;
14
+ init(): Promise<void>;
15
+ identify(user: BrowserPerson | null | undefined): void;
16
+ reset(): void;
17
+ };
18
+ /**
19
+ * Optional browser PostHog (identify + session recording). Does not read
20
+ * environment variables. Products pass `apiKey` from request-time env so
21
+ * Next.js OSS images are not baked with `NEXT_PUBLIC_*`. Missing key is a
22
+ * no-op and never loads `posthog-js`.
23
+ */
24
+ export declare function createBrowserObservability(options: CreateBrowserObservabilityOptions): BrowserObservability;
25
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAeA,MAAM,MAAM,aAAa,GAAG;IAC1B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,iCAAiC,GAAG;IAC9C,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,CAAC;IACvD,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAQF;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,iCAAiC,GACzC,oBAAoB,CAoDtB"}
@@ -0,0 +1,63 @@
1
+ const DEFAULT_HOST = "https://us.i.posthog.com";
2
+ let client = null;
3
+ let initPromise = null;
4
+ let activeKey = null;
5
+ /**
6
+ * Optional browser PostHog (identify + session recording). Does not read
7
+ * environment variables. Products pass `apiKey` from request-time env so
8
+ * Next.js OSS images are not baked with `NEXT_PUBLIC_*`. Missing key is a
9
+ * no-op and never loads `posthog-js`.
10
+ */
11
+ export function createBrowserObservability(options) {
12
+ const apiKey = options.apiKey?.trim() || null;
13
+ const enabled = Boolean(apiKey);
14
+ return {
15
+ enabled,
16
+ init() {
17
+ if (!apiKey) {
18
+ return Promise.resolve();
19
+ }
20
+ if (client && activeKey === apiKey) {
21
+ return Promise.resolve();
22
+ }
23
+ if (!initPromise) {
24
+ initPromise = import("posthog-js")
25
+ .then((mod) => {
26
+ const posthog = (mod.default ?? mod);
27
+ if (client && activeKey === apiKey) {
28
+ return;
29
+ }
30
+ posthog.init(apiKey, {
31
+ api_host: options.host?.trim() || DEFAULT_HOST,
32
+ defaults: "2026-05-30",
33
+ loaded(instance) {
34
+ instance.register({
35
+ service: options.serviceName,
36
+ environment: options.environment,
37
+ });
38
+ },
39
+ });
40
+ client = posthog;
41
+ activeKey = apiKey;
42
+ })
43
+ .catch(() => {
44
+ initPromise = null;
45
+ });
46
+ }
47
+ return initPromise ?? Promise.resolve();
48
+ },
49
+ identify(user) {
50
+ if (!client || !user?.id) {
51
+ return;
52
+ }
53
+ client.identify(user.id, {
54
+ ...(user.email ? { email: user.email } : {}),
55
+ ...(user.name ? { name: user.name } : {}),
56
+ });
57
+ },
58
+ reset() {
59
+ client?.reset();
60
+ },
61
+ };
62
+ }
63
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAmCA,MAAM,YAAY,GAAG,0BAA0B,CAAC;AAEhD,IAAI,MAAM,GAAqB,IAAI,CAAC;AACpC,IAAI,WAAW,GAAyB,IAAI,CAAC;AAC7C,IAAI,SAAS,GAAkB,IAAI,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CACxC,OAA0C;IAE1C,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,IAAI,CAAC;IAC9C,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEhC,OAAO;QACL,OAAO;QACP,IAAI;YACF,IAAI,CAAC,MAAM,EAAE,CAAC;gBACZ,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;YAC3B,CAAC;YACD,IAAI,MAAM,IAAI,SAAS,KAAK,MAAM,EAAE,CAAC;gBACnC,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;YAC3B,CAAC;YACD,IAAI,CAAC,WAAW,EAAE,CAAC;gBACjB,WAAW,GAAG,MAAM,CAAC,YAAY,CAAC;qBAC/B,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;oBACZ,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,OAAO,IAAI,GAAG,CAAyB,CAAC;oBAC7D,IAAI,MAAM,IAAI,SAAS,KAAK,MAAM,EAAE,CAAC;wBACnC,OAAO;oBACT,CAAC;oBACD,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE;wBACnB,QAAQ,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,YAAY;wBAC9C,QAAQ,EAAE,YAAY;wBACtB,MAAM,CAAC,QAAQ;4BACb,QAAQ,CAAC,QAAQ,CAAC;gCAChB,OAAO,EAAE,OAAO,CAAC,WAAW;gCAC5B,WAAW,EAAE,OAAO,CAAC,WAAW;6BACjC,CAAC,CAAC;wBACL,CAAC;qBACF,CAAC,CAAC;oBACH,MAAM,GAAG,OAAO,CAAC;oBACjB,SAAS,GAAG,MAAM,CAAC;gBACrB,CAAC,CAAC;qBACD,KAAK,CAAC,GAAG,EAAE;oBACV,WAAW,GAAG,IAAI,CAAC;gBACrB,CAAC,CAAC,CAAC;YACP,CAAC;YACD,OAAO,WAAW,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;QAC1C,CAAC;QACD,QAAQ,CAAC,IAAI;YACX,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE,EAAE,EAAE,CAAC;gBACzB,OAAO;YACT,CAAC;YACD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE;gBACvB,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC5C,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAC1C,CAAC,CAAC;QACL,CAAC;QACD,KAAK;YACH,MAAM,EAAE,KAAK,EAAE,CAAC;QAClB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,62 @@
1
+ import type { NextFunction, Request, Response } from "express";
2
+ import { type Logger } from "pino";
3
+ import { type Clock } from "./limits.js";
4
+ export type ObservabilityClock = Clock;
5
+ export type CaptureClient = {
6
+ captureException(error: Error, distinctId: string, properties: Record<string, unknown>): void;
7
+ capture(input: {
8
+ event: string;
9
+ distinctId: string;
10
+ properties: Record<string, unknown>;
11
+ }): void;
12
+ shutdown(): Promise<void>;
13
+ };
14
+ export type ContextPolicy = {
15
+ propertyAllowlist: ReadonlySet<string>;
16
+ };
17
+ export type CreateObservabilityOptions = {
18
+ serviceName: string;
19
+ environment: string;
20
+ posthog?: {
21
+ apiKey: string;
22
+ host?: string;
23
+ client?: CaptureClient;
24
+ };
25
+ logs?: {
26
+ level?: string;
27
+ otlp?: {
28
+ endpoint: string;
29
+ headers?: Record<string, string>;
30
+ };
31
+ };
32
+ contextPolicy: ContextPolicy;
33
+ clock?: Clock;
34
+ perSourceCap?: number;
35
+ dedupeTtlMs?: number;
36
+ dedupeMaxKeys?: number;
37
+ };
38
+ export type Observability = {
39
+ logger: Logger;
40
+ captureEvent(input: {
41
+ event: string;
42
+ source: string;
43
+ subjectId?: unknown;
44
+ properties?: Record<string, unknown>;
45
+ }): void;
46
+ captureException(input: {
47
+ error: unknown;
48
+ source: string;
49
+ subjectId?: unknown;
50
+ context?: Record<string, unknown>;
51
+ severity?: "error" | "warning" | "critical";
52
+ }): void;
53
+ expressErrorHandler(error: unknown, req: Request, res: Response, next: NextFunction): void;
54
+ shutdown(timeoutMs?: number): Promise<void>;
55
+ enabled: boolean;
56
+ };
57
+ /**
58
+ * Explicit factory. Does not read environment variables or create SDK
59
+ * clients at module import. Tenant identity is an opaque `subjectId`.
60
+ */
61
+ export declare function createObservability(options: CreateObservabilityOptions): Observability;
62
+ //# sourceMappingURL=create.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAa,EAAE,KAAK,MAAM,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,EAAE,KAAK,KAAK,EAAiC,MAAM,aAAa,CAAC;AAGxE,MAAM,MAAM,kBAAkB,GAAG,KAAK,CAAC;AAEvC,MAAM,MAAM,aAAa,GAAG;IAC1B,gBAAgB,CACd,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAA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C,CAAC;KACtC,GAAG,IAAI,CAAC;IACT,gBAAgB,CAAC,KAAK,EAAE;QACtB,KAAK,EAAE,OAAO,CAAC;QACf,MAAM,EAAE,MAAM,CAAC;QACf,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAClC,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,GAAG,UAAU,CAAC;KAC7C,GAAG,IAAI,CAAC;IACT,mBAAmB,CACjB,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,IAAI,CAAC;IACR,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAiCF;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,0BAA0B,GAClC,aAAa,CAwIf"}
package/dist/create.js ADDED
@@ -0,0 +1,256 @@
1
+ import pino from "pino";
2
+ import { createDedupe, createSourceCap } from "./limits.js";
3
+ import { opaqueSubjectId, pickAllowlisted, redactText } from "./redaction.js";
4
+ function createOtlpLogStream(input) {
5
+ const pending = new Set();
6
+ const stream = {
7
+ write(message) {
8
+ const controller = new AbortController();
9
+ const timer = setTimeout(() => controller.abort(), 2_000);
10
+ const request = fetch(input.endpoint, {
11
+ method: "POST",
12
+ headers: { "content-type": "application/json", ...input.headers },
13
+ body: JSON.stringify({
14
+ resourceLogs: [
15
+ {
16
+ scopeLogs: [
17
+ {
18
+ logRecords: [{ body: { stringValue: message.trim() } }],
19
+ },
20
+ ],
21
+ },
22
+ ],
23
+ }),
24
+ signal: controller.signal,
25
+ })
26
+ .then(() => undefined)
27
+ .catch(() => undefined)
28
+ .finally(() => {
29
+ clearTimeout(timer);
30
+ pending.delete(request);
31
+ });
32
+ pending.add(request);
33
+ },
34
+ };
35
+ return {
36
+ stream,
37
+ async shutdown() {
38
+ await Promise.allSettled([...pending]);
39
+ },
40
+ };
41
+ }
42
+ function createHttpCaptureClient(input) {
43
+ const endpoint = `${(input.host ?? "https://app.posthog.com").replace(/\/$/, "")}/capture/`;
44
+ const pending = new Set();
45
+ const send = (payload) => {
46
+ const controller = new AbortController();
47
+ const timer = setTimeout(() => controller.abort(), 2_000);
48
+ const request = fetch(endpoint, {
49
+ method: "POST",
50
+ headers: {
51
+ "content-type": "application/json",
52
+ authorization: `Bearer ${input.apiKey}`,
53
+ },
54
+ body: JSON.stringify({ api_key: input.apiKey, ...payload }),
55
+ signal: controller.signal,
56
+ })
57
+ .then(() => undefined)
58
+ .catch(() => undefined)
59
+ .finally(() => {
60
+ clearTimeout(timer);
61
+ pending.delete(request);
62
+ });
63
+ pending.add(request);
64
+ };
65
+ return {
66
+ captureException(error, distinctId, properties) {
67
+ send({
68
+ event: "$exception",
69
+ distinct_id: distinctId,
70
+ properties: {
71
+ ...properties,
72
+ $exception_message: error.message,
73
+ $exception_type: error.name,
74
+ $exception_stack_trace_raw: redactText(error.stack ?? "", 2_000),
75
+ },
76
+ });
77
+ },
78
+ capture(input) {
79
+ send({
80
+ event: input.event,
81
+ distinct_id: input.distinctId,
82
+ properties: input.properties,
83
+ });
84
+ },
85
+ async shutdown() {
86
+ await Promise.allSettled([...pending]);
87
+ },
88
+ };
89
+ }
90
+ const systemClock = { now: () => new Date() };
91
+ function errorParts(error) {
92
+ if (error instanceof Error) {
93
+ const message = redactText(error.message, 500);
94
+ const stack = redactText(error.stack ?? "", 2_000);
95
+ const normalized = new Error(message);
96
+ normalized.name = error.name || "Error";
97
+ normalized.stack = stack;
98
+ const stackTop = redactText((error.stack ?? "").split("\n")[1]?.trim() ?? "", 300);
99
+ return {
100
+ name: error.name || "Error",
101
+ message,
102
+ stackTop,
103
+ normalized,
104
+ };
105
+ }
106
+ const message = redactText(typeof error === "string" ? error : "Unknown error thrown", 500);
107
+ const normalized = new Error(message);
108
+ normalized.stack = redactText(normalized.stack ?? "", 2_000);
109
+ return {
110
+ name: "Error",
111
+ message,
112
+ stackTop: "",
113
+ normalized,
114
+ };
115
+ }
116
+ /**
117
+ * Explicit factory. Does not read environment variables or create SDK
118
+ * clients at module import. Tenant identity is an opaque `subjectId`.
119
+ */
120
+ export function createObservability(options) {
121
+ const clock = options.clock ?? systemClock;
122
+ const otlp = options.logs?.otlp ? createOtlpLogStream(options.logs.otlp) : null;
123
+ const logger = pino({
124
+ name: options.serviceName,
125
+ level: options.logs?.level ?? "info",
126
+ redact: {
127
+ paths: [
128
+ "authorization",
129
+ "cookie",
130
+ "headers.authorization",
131
+ "headers.cookie",
132
+ 'headers["x-api-key"]',
133
+ "password",
134
+ "secret",
135
+ "token",
136
+ "body",
137
+ "email",
138
+ "card",
139
+ ],
140
+ remove: true,
141
+ },
142
+ base: {
143
+ service: options.serviceName,
144
+ environment: options.environment,
145
+ },
146
+ }, otlp
147
+ ? pino.multistream([{ stream: process.stdout }, { stream: otlp.stream }])
148
+ : undefined);
149
+ const client = options.posthog
150
+ ? (options.posthog.client ?? createHttpCaptureClient(options.posthog))
151
+ : null;
152
+ const enabled = Boolean(client || otlp);
153
+ const dedupe = createDedupe({
154
+ ttlMs: options.dedupeTtlMs ?? 60_000,
155
+ maxKeys: options.dedupeMaxKeys ?? 10_000,
156
+ clock,
157
+ });
158
+ const cap = createSourceCap({
159
+ perSource: options.perSourceCap ?? 100,
160
+ clock,
161
+ });
162
+ const seenExpress = new WeakSet();
163
+ const captureException = (input) => {
164
+ if (!client)
165
+ return;
166
+ try {
167
+ const parts = errorParts(input.error);
168
+ const subject = opaqueSubjectId(input.subjectId);
169
+ const fingerprint = `${input.source}|${parts.name}|${parts.stackTop}`;
170
+ if (dedupe.shouldSkip(fingerprint))
171
+ return;
172
+ if (cap.isLimited(input.source))
173
+ return;
174
+ client.captureException(parts.normalized, subject, {
175
+ service: options.serviceName,
176
+ environment: options.environment,
177
+ source: input.source,
178
+ subject_id: subject,
179
+ severity: input.severity ?? "error",
180
+ error_name: parts.name,
181
+ error_message: parts.message,
182
+ error_stack_top: parts.stackTop,
183
+ ...pickAllowlisted(input.context ?? {}, options.contextPolicy.propertyAllowlist),
184
+ });
185
+ }
186
+ catch {
187
+ // Telemetry must never fail the caller.
188
+ }
189
+ };
190
+ return {
191
+ logger,
192
+ enabled,
193
+ captureEvent(input) {
194
+ if (!client)
195
+ return;
196
+ try {
197
+ const subject = opaqueSubjectId(input.subjectId);
198
+ const fingerprint = `${input.source}|event|${input.event}|${subject}`;
199
+ if (dedupe.shouldSkip(fingerprint) || cap.isLimited(input.source)) {
200
+ return;
201
+ }
202
+ client.capture({
203
+ event: input.event,
204
+ distinctId: subject,
205
+ properties: {
206
+ service: options.serviceName,
207
+ environment: options.environment,
208
+ source: input.source,
209
+ subject_id: subject,
210
+ ...pickAllowlisted(input.properties ?? {}, options.contextPolicy.propertyAllowlist),
211
+ },
212
+ });
213
+ }
214
+ catch {
215
+ // Telemetry must never fail the caller.
216
+ }
217
+ },
218
+ captureException,
219
+ expressErrorHandler(error, _req, _res, next) {
220
+ if (error && typeof error === "object") {
221
+ if (seenExpress.has(error)) {
222
+ next(error);
223
+ return;
224
+ }
225
+ seenExpress.add(error);
226
+ }
227
+ captureException({ error, source: "express" });
228
+ next(error);
229
+ },
230
+ async shutdown(timeoutMs = 2000) {
231
+ if (!client && !otlp)
232
+ return;
233
+ let timer;
234
+ const timeout = new Promise((resolve) => {
235
+ timer = setTimeout(resolve, timeoutMs);
236
+ });
237
+ try {
238
+ await Promise.race([
239
+ Promise.all([
240
+ client?.shutdown() ?? Promise.resolve(),
241
+ otlp?.shutdown() ?? Promise.resolve(),
242
+ ]).then(() => undefined),
243
+ timeout,
244
+ ]);
245
+ }
246
+ catch {
247
+ // best-effort
248
+ }
249
+ finally {
250
+ if (timer)
251
+ clearTimeout(timer);
252
+ }
253
+ },
254
+ };
255
+ }
256
+ //# sourceMappingURL=create.js.map
@@ -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AQ,EAAE,IAAI,OAAO,CAAC,OAAO,EAAE;qBACtC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC;oBACxB,OAAO;iBACR,CAAC,CAAC;YACL,CAAC;YAAC,MAAM,CAAC;gBACP,cAAc;YAChB,CAAC;oBAAS,CAAC;gBACT,IAAI,KAAK;oBAAE,YAAY,CAAC,KAAK,CAAC,CAAC;YACjC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { type CaptureClient, type ContextPolicy, type CreateObservabilityOptions, createObservability, type Observability, type ObservabilityClock, } from "./create.js";
2
+ export { opaqueSubjectId, redactText } from "./redaction.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,0BAA0B,EAC/B,mBAAmB,EACnB,KAAK,aAAa,EAClB,KAAK,kBAAkB,GACxB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createObservability, } from "./create.js";
2
+ export { opaqueSubjectId, redactText } from "./redaction.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,mBAAmB,GAGpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,18 @@
1
+ export type Clock = {
2
+ now(): Date;
3
+ };
4
+ export declare function createDedupe(options: {
5
+ ttlMs: number;
6
+ maxKeys: number;
7
+ clock: Clock;
8
+ }): {
9
+ shouldSkip(fingerprint: string): boolean;
10
+ size(): number;
11
+ };
12
+ export declare function createSourceCap(options: {
13
+ perSource: number;
14
+ clock: Clock;
15
+ }): {
16
+ isLimited(source: string): boolean;
17
+ };
18
+ //# sourceMappingURL=limits.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"limits.d.ts","sourceRoot":"","sources":["../src/limits.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,KAAK,GAAG;IAAE,GAAG,IAAI,IAAI,CAAA;CAAE,CAAC;AAEpC,wBAAgB,YAAY,CAAC,OAAO,EAAE;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,KAAK,CAAC;CACd;4BAG2B,MAAM,GAAG,OAAO;YAQhC,MAAM;EAIjB;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE;sBAGtD,MAAM,GAAG,OAAO;EAYrC"}
package/dist/limits.js ADDED
@@ -0,0 +1,36 @@
1
+ export function createDedupe(options) {
2
+ const fingerprints = new Map();
3
+ return {
4
+ shouldSkip(fingerprint) {
5
+ const now = options.clock.now().getTime();
6
+ const expiry = fingerprints.get(fingerprint);
7
+ if (expiry && expiry > now)
8
+ return true;
9
+ if (fingerprints.size >= options.maxKeys)
10
+ fingerprints.clear();
11
+ fingerprints.set(fingerprint, now + options.ttlMs);
12
+ return false;
13
+ },
14
+ size() {
15
+ return fingerprints.size;
16
+ },
17
+ };
18
+ }
19
+ export function createSourceCap(options) {
20
+ const windows = new Map();
21
+ return {
22
+ isLimited(source) {
23
+ const minute = Math.floor(options.clock.now().getTime() / 60_000);
24
+ const current = windows.get(source);
25
+ if (!current || current.minute !== minute) {
26
+ windows.set(source, { minute, count: 1 });
27
+ return false;
28
+ }
29
+ if (current.count >= options.perSource)
30
+ return true;
31
+ current.count += 1;
32
+ return false;
33
+ },
34
+ };
35
+ }
36
+ //# sourceMappingURL=limits.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C;YACf,CAAC;YACD,IAAI,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,SAAS;gBAAE,OAAO,IAAI,CAAC;YACpD,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC;YACnB,OAAO,KAAK,CAAC;QACf,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,5 @@
1
+ export declare function redactText(input: string, limit?: number): string;
2
+ export declare function isSensitiveKey(key: string): boolean;
3
+ export declare function pickAllowlisted(input: Record<string, unknown>, allowlist: ReadonlySet<string>): Record<string, string | number | boolean>;
4
+ export declare function opaqueSubjectId(value: unknown): string;
5
+ //# sourceMappingURL=redaction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redaction.d.ts","sourceRoot":"","sources":["../src/redaction.ts"],"names":[],"mappings":"AAQA,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,SAAM,GAAG,MAAM,CAQ7D;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEnD;AAED,wBAAgB,eAAe,CAC7B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,SAAS,EAAE,WAAW,CAAC,MAAM,CAAC,GAC7B,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAa3C;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAItD"}
@@ -0,0 +1,40 @@
1
+ const EMAIL = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi;
2
+ const TOKEN = /\b[A-Za-z0-9_-]{24,}\b/g;
3
+ const SECRET_KEYS = /^(authorization|cookie|password|secret|token|api[_-]?key|pepper|digest|card|cvv|pan)$/i;
4
+ const SENSITIVE_ASSIGNMENT = /\b(authorization|cookie|password|secret|token|api[_-]?key|pepper|digest|card|cvv|pan)\b\s*([=:])\s*(?:"[^"]*"|'[^']*'|[^\s,;&})\]]+)/gi;
5
+ const BEARER_TOKEN = /\bBearer\s+[A-Za-z0-9._~+/-]+=*/gi;
6
+ export function redactText(input, limit = 500) {
7
+ const masked = input
8
+ .replace(SENSITIVE_ASSIGNMENT, "$1$2[redacted]")
9
+ .replace(BEARER_TOKEN, "Bearer [redacted]")
10
+ .replace(EMAIL, "[redacted-email]")
11
+ .replace(TOKEN, "[redacted-token]");
12
+ if (masked.length <= limit)
13
+ return masked;
14
+ return `${masked.slice(0, limit)}...`;
15
+ }
16
+ export function isSensitiveKey(key) {
17
+ return SECRET_KEYS.test(key);
18
+ }
19
+ export function pickAllowlisted(input, allowlist) {
20
+ const out = {};
21
+ for (const [key, value] of Object.entries(input)) {
22
+ if (!allowlist.has(key) || isSensitiveKey(key))
23
+ continue;
24
+ if (typeof value === "number" || typeof value === "boolean") {
25
+ out[key] = value;
26
+ continue;
27
+ }
28
+ if (typeof value === "string") {
29
+ out[key] = redactText(value, 500);
30
+ }
31
+ }
32
+ return out;
33
+ }
34
+ export function opaqueSubjectId(value) {
35
+ if (value === null || value === undefined)
36
+ return "system";
37
+ const normalized = String(value).trim();
38
+ return normalized || "system";
39
+ }
40
+ //# sourceMappingURL=redaction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redaction.js","sourceRoot":"","sources":["../src/redaction.ts"],"names":[],"mappings":"AAAA,MAAM,KAAK,GAAG,6CAA6C,CAAC;AAC5D,MAAM,KAAK,GAAG,yBAAyB,CAAC;AACxC,MAAM,WAAW,GACf,wFAAwF,CAAC;AAC3F,MAAM,oBAAoB,GACxB,wIAAwI,CAAC;AAC3I,MAAM,YAAY,GAAG,mCAAmC,CAAC;AAEzD,MAAM,UAAU,UAAU,CAAC,KAAa,EAAE,KAAK,GAAG,GAAG;IACnD,MAAM,MAAM,GAAG,KAAK;SACjB,OAAO,CAAC,oBAAoB,EAAE,gBAAgB,CAAC;SAC/C,OAAO,CAAC,YAAY,EAAE,mBAAmB,CAAC;SAC1C,OAAO,CAAC,K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
1
+ {
2
+ "name": "@codelitdev/observability",
3
+ "version": "0.1.0-alpha.0",
4
+ "description": "Provider-neutral logging and optional telemetry for CodeLit products",
5
+ "author": "CodeLit",
6
+ "type": "module",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/codelitdev/platform.git",
11
+ "directory": "packages/observability"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "./browser": {
20
+ "types": "./dist/browser.d.ts",
21
+ "import": "./dist/browser.js",
22
+ "default": "./dist/browser.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "scripts": {
29
+ "build": "tsc -p tsconfig.json",
30
+ "test": "bun test --timeout 60000 src",
31
+ "test:packed": "bun scripts/packed-consumer.mjs",
32
+ "typecheck": "tsc -p tsconfig.test.json --noEmit",
33
+ "lint": "biome lint .",
34
+ "prepublishOnly": "bun run build"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public",
38
+ "tag": "latest"
39
+ },
40
+ "dependencies": {
41
+ "pino": "^9.6.0"
42
+ },
43
+ "peerDependencies": {
44
+ "express": "^4.21.2",
45
+ "posthog-js": "^1.200.0",
46
+ "posthog-node": "^4.0.0"
47
+ },
48
+ "peerDependenciesMeta": {
49
+ "express": {
50
+ "optional": true
51
+ },
52
+ "posthog-js": {
53
+ "optional": true
54
+ },
55
+ "posthog-node": {
56
+ "optional": true
57
+ }
58
+ },
59
+ "devDependencies": {
60
+ "@types/bun": "^1.4.1",
61
+ "@types/express": "^4.17.21",
62
+ "@types/node": "^22.14.1",
63
+ "express": "^4.21.2",
64
+ "posthog-js": "^1.425.1",
65
+ "typescript": "^5.9.3"
66
+ }
67
+ }