@pionne/node 0.3.0 → 0.3.2

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/README.md CHANGED
@@ -77,10 +77,56 @@ Pionne.setTags({ tier: 'pro' });
77
77
  Pionne.setEnabled(false);
78
78
  ```
79
79
 
80
+ ### Geography (opt-in)
81
+
82
+ Approximate server location (city, region, country) attached to every event,
83
+ just like Sentry. Off by default for privacy — flip `sendGeography` to enable:
84
+
85
+ ```ts
86
+ Pionne.init({
87
+ token: 'pio_live_xxx',
88
+ sendGeography: true,
89
+ });
90
+ ```
91
+
92
+ Resolved once at startup via a free IP→geo lookup (`https://ipapi.co/json/`
93
+ by default), with a 4 s timeout. If the lookup fails the SDK silently keeps
94
+ shipping events without geo. Override the endpoint via `geographyEndpoint`
95
+ if you have your own.
96
+
80
97
  ## Options
81
98
 
82
- Same shape as `@pionne/web` and `@pionne/react-native`. See the type
83
- definitions for the full list.
99
+ Same shape as `@pionne/web` and `@pionne/react-native`. Highlights :
100
+
101
+ | Option | Type | Default |
102
+ | --------------------- | -------------------------- | ------------------------ |
103
+ | `token` | `string` (required) | — |
104
+ | `endpoint` | `string` | Pionne production |
105
+ | `release` | `string` | unset |
106
+ | `environment` | `string` | `NODE_ENV` ou `production` |
107
+ | `enabled` | `boolean` | `true` |
108
+ | `captureUncaughtErrors` | `boolean` | `true` |
109
+ | `captureUnhandledRejections` | `boolean` | `true` |
110
+ | `tags` | `Record<string, string>` | unset |
111
+ | `userIdAnon` | `string` | unset |
112
+ | `maxStackFrames` | `number` | `50` |
113
+ | `beforeSend` | `(event) => event \| null` | unset (drop if `null`) |
114
+ | `sendGeography` | `boolean` | `false` |
115
+ | `geographyEndpoint` | `string` | `https://ipapi.co/json/` |
116
+ | `releaseHealth` | `boolean` | `true` |
117
+ | `maxEventsPerSecond` | `number` | `10` |
118
+
119
+ ### Notes
120
+
121
+ - **`maxEventsPerSecond`** — token-bucket process-wide. Au-delà, les events sont droppés silencieusement. Protège contre une boucle d'erreur dans un worker. `0` désactive (déconseillé sur des serveurs longue durée).
122
+ - **`releaseHealth`** — ouvre une session au `init()`, utile pour les services Node longue durée que tu veux mesurer comme un mobile (crash-free uptime).
123
+ - **`sendGeography`** — opt-in : résout city/region/country côté IP via [ipapi.co](https://ipapi.co/json/) par défaut. Sur un serveur, l'IP source est l'IP publique du serveur lui-même (pas celle d'un user). Customize via `geographyEndpoint` pour pointer sur ta propre lookup ou un proxy interne.
124
+
125
+ Voir les types TypeScript pour la liste complète.
126
+
127
+ ## Rate limit serveur
128
+
129
+ L'API Pionne cap **600 req/min/token** (= 10/sec) sur tous les endpoints publics (`/ingest`, `/sessions`, `/feedback`). Au-delà → `HTTP 429` avec un header `Retry-After`. Le SDK fait silencieusement échouer (try/catch interne). Empêche un token leaké (ou un worker qui boucle) de drainer ton infra ou ton quota mensuel. Voir [doc rate limits](https://pionne.agkgcreations.fr/security/rate-limits).
84
130
 
85
131
  ## License
86
132
 
package/dist/index.cjs CHANGED
@@ -37,6 +37,54 @@ module.exports = __toCommonJS(index_exports);
37
37
  var os = __toESM(require("os"));
38
38
  var process2 = __toESM(require("process"));
39
39
 
40
+ // src/security.ts
41
+ var TOKEN_PREFIX = "pio_live_";
42
+ var MIN_TOKEN_LENGTH = TOKEN_PREFIX.length + 16;
43
+ function validateEndpoint(endpoint, isDev) {
44
+ try {
45
+ const u = new URL(endpoint);
46
+ if (u.protocol === "https:") return true;
47
+ if (u.protocol !== "http:") return false;
48
+ if (!isDev) return false;
49
+ return /^(localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\]|.*\.local)$/.test(u.hostname);
50
+ } catch {
51
+ return false;
52
+ }
53
+ }
54
+ function validateToken(token) {
55
+ if (typeof token !== "string") return false;
56
+ if (!token.startsWith(TOKEN_PREFIX)) return false;
57
+ if (token.length < MIN_TOKEN_LENGTH) return false;
58
+ const lower = token.toLowerCase();
59
+ for (const bad of ["xxx", "yyy", "todo", "fixme", "replace", "changeme"]) {
60
+ if (lower.includes(bad)) return false;
61
+ }
62
+ return true;
63
+ }
64
+ var RateLimiter = class {
65
+ constructor(capacity, refillPerSecond) {
66
+ this.capacity = capacity;
67
+ this.refillPerSecond = refillPerSecond;
68
+ this.tokens = capacity;
69
+ this.lastRefill = Date.now();
70
+ }
71
+ allow() {
72
+ if (this.refillPerSecond <= 0) return true;
73
+ const now = Date.now();
74
+ const elapsedMs = now - this.lastRefill;
75
+ if (elapsedMs > 0) {
76
+ const refill = elapsedMs / 1e3 * this.refillPerSecond;
77
+ this.tokens = Math.min(this.capacity, this.tokens + refill);
78
+ this.lastRefill = now;
79
+ }
80
+ if (this.tokens >= 1) {
81
+ this.tokens -= 1;
82
+ return true;
83
+ }
84
+ return false;
85
+ }
86
+ };
87
+
40
88
  // src/sessions.ts
41
89
  var crypto = __toESM(require("crypto"));
42
90
  var current = null;
@@ -108,10 +156,13 @@ function flipFromEvent(level, mechanismType) {
108
156
 
109
157
  // src/index.ts
110
158
  var DEFAULT_ENDPOINT = "https://pionne.agkgcreations.fr/api/ingest";
159
+ var DEFAULT_GEO_ENDPOINT = "https://ipapi.co/json/";
111
160
  var DEFAULT_MAX_STACK = 50;
112
161
  var SDK_NAME = "pionne.node";
113
162
  var SDK_VERSION = "0.1.0";
114
163
  var config = null;
164
+ var rateLimiter = null;
165
+ var droppedByRateLimit = 0;
115
166
  var staticContext = {};
116
167
  var onUncaught = null;
117
168
  var onRejection = null;
@@ -177,6 +228,13 @@ function buildEvent(err, level, mechanism, handled, extra) {
177
228
  return event;
178
229
  }
179
230
  async function send(event) {
231
+ if (rateLimiter && !rateLimiter.allow()) {
232
+ droppedByRateLimit++;
233
+ if (process2.env.NODE_ENV !== "production" && droppedByRateLimit % 50 === 1) {
234
+ console.warn(`[Pionne] rate-limit reached (${droppedByRateLimit} events dropped). Bump maxEventsPerSecond if intentional.`);
235
+ }
236
+ return;
237
+ }
180
238
  if (!config) return;
181
239
  try {
182
240
  if (typeof fetch !== "function") return;
@@ -191,6 +249,31 @@ async function send(event) {
191
249
  } catch {
192
250
  }
193
251
  }
252
+ function fetchGeography(endpoint) {
253
+ if (typeof fetch !== "function") return;
254
+ const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
255
+ const timeout = controller ? setTimeout(() => controller.abort(), 4e3) : null;
256
+ fetch(endpoint, {
257
+ method: "GET",
258
+ headers: { Accept: "application/json" },
259
+ signal: controller?.signal
260
+ }).then((res) => res.ok ? res.json() : null).then((data) => {
261
+ if (!data || typeof data !== "object") return;
262
+ const d = data;
263
+ const geo = {};
264
+ if (typeof d.city === "string") geo.city = d.city;
265
+ if (typeof d.region === "string") geo.region = d.region;
266
+ if (typeof d.country_name === "string") geo.country = d.country_name;
267
+ else if (typeof d.country === "string") geo.country = d.country;
268
+ if (typeof d.country_code === "string") geo.country_code = d.country_code;
269
+ if (Object.keys(geo).length === 0) return;
270
+ const ctx = staticContext.contexts ?? {};
271
+ staticContext.contexts = { ...ctx, geo };
272
+ }).catch(() => {
273
+ }).finally(() => {
274
+ if (timeout) clearTimeout(timeout);
275
+ });
276
+ }
194
277
  function installUncaughtHandler() {
195
278
  onUncaught = (err) => {
196
279
  const event = buildEvent(err, "fatal", "uncaughtException", false);
@@ -214,42 +297,56 @@ function installRejectionHandler() {
214
297
  }
215
298
  var Pionne = {
216
299
  init(options) {
217
- if (!options?.token || !options.token.startsWith("pio_live_")) {
218
- if (process2.env.NODE_ENV !== "production") {
219
- console.warn(
220
- "[Pionne] Missing or invalid token (must start with pio_live_)."
221
- );
300
+ try {
301
+ const isDev = process2.env.NODE_ENV !== "production";
302
+ if (!options?.token || !validateToken(options.token)) {
303
+ if (isDev) {
304
+ console.warn("[Pionne] Missing or invalid token (expected pio_live_<\u226516 chars>, no placeholders).");
305
+ }
306
+ return;
222
307
  }
223
- return;
224
- }
225
- const autoContext = options.autoContext ?? true;
226
- staticContext = autoContext ? gatherStaticContext() : {};
227
- config = {
228
- token: options.token,
229
- endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
230
- release: options.release,
231
- environment: options.environment ?? process2.env.NODE_ENV ?? "production",
232
- enabled: options.enabled ?? true,
233
- captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,
234
- captureUnhandledRejections: options.captureUnhandledRejections ?? true,
235
- autoContext,
236
- beforeSend: options.beforeSend,
237
- userIdAnon: options.userIdAnon,
238
- tags: options.tags,
239
- maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK
240
- };
241
- if (config.captureUncaughtExceptions) installUncaughtHandler();
242
- if (config.captureUnhandledRejections) installRejectionHandler();
243
- if (options.releaseHealth !== false) {
244
- startSession({
245
- endpoint: config.endpoint,
246
- token: config.token,
247
- release: config.release,
248
- environment: config.environment,
249
- appVersion: staticContext.app_version,
250
- osName: staticContext.os_name,
251
- userIdAnon: config.userIdAnon
252
- });
308
+ const endpoint = options.endpoint ?? DEFAULT_ENDPOINT;
309
+ if (!validateEndpoint(endpoint, isDev)) {
310
+ console.warn("[Pionne] Refusing non-HTTPS endpoint in production:", endpoint);
311
+ return;
312
+ }
313
+ const rps = options.maxEventsPerSecond ?? 10;
314
+ rateLimiter = rps > 0 ? new RateLimiter(rps, rps) : null;
315
+ const autoContext = options.autoContext ?? true;
316
+ staticContext = autoContext ? gatherStaticContext() : {};
317
+ config = {
318
+ token: options.token,
319
+ endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
320
+ release: options.release,
321
+ environment: options.environment ?? process2.env.NODE_ENV ?? "production",
322
+ enabled: options.enabled ?? true,
323
+ captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,
324
+ captureUnhandledRejections: options.captureUnhandledRejections ?? true,
325
+ autoContext,
326
+ beforeSend: options.beforeSend,
327
+ userIdAnon: options.userIdAnon,
328
+ tags: options.tags,
329
+ maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK,
330
+ sendGeography: options.sendGeography ?? false,
331
+ geographyEndpoint: options.geographyEndpoint ?? DEFAULT_GEO_ENDPOINT
332
+ };
333
+ if (config.captureUncaughtExceptions) installUncaughtHandler();
334
+ if (config.captureUnhandledRejections) installRejectionHandler();
335
+ if (config.sendGeography) fetchGeography(config.geographyEndpoint);
336
+ if (options.releaseHealth !== false) {
337
+ startSession({
338
+ endpoint: config.endpoint,
339
+ token: config.token,
340
+ release: config.release,
341
+ environment: config.environment,
342
+ appVersion: staticContext.app_version,
343
+ osName: staticContext.os_name,
344
+ userIdAnon: config.userIdAnon
345
+ });
346
+ }
347
+ } catch (e) {
348
+ console.warn("[Pionne] init failed silently \u2014 monitoring disabled.", e);
349
+ config = null;
253
350
  }
254
351
  },
255
352
  captureException(err, extra) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/sessions.ts"],"sourcesContent":["import * as os from 'node:os';\nimport * as process from 'node:process';\n\nimport {\n endSession as _endSession,\n flipFromEvent,\n getCurrentSessionId,\n startSession as _startSession,\n} from './sessions';\n\nexport type Level = 'fatal' | 'error' | 'warning' | 'info';\nexport type MechanismType =\n | 'uncaughtException'\n | 'unhandledRejection'\n | 'manual';\n\nexport interface Mechanism {\n type: MechanismType;\n handled: boolean;\n}\n\nexport interface PionneEvent {\n exception_type: string;\n message?: string | null;\n stack?: string[];\n level?: Level;\n\n release?: string;\n environment?: string;\n app_version?: string;\n os_name?: string;\n os_version?: string;\n user_id_anon?: string;\n locale?: string;\n timezone?: string;\n\n contexts?: Record<string, Record<string, unknown> | undefined>;\n mechanism?: Mechanism;\n tags?: Record<string, string>;\n}\n\nexport interface PionneOptions {\n /** Project token (starts with `pio_live_`). Required. */\n token: string;\n endpoint?: string;\n release?: string;\n environment?: string;\n enabled?: boolean;\n captureUncaughtExceptions?: boolean;\n captureUnhandledRejections?: boolean;\n autoContext?: boolean;\n beforeSend?: (event: PionneEvent) => PionneEvent | null;\n userIdAnon?: string;\n tags?: Record<string, string>;\n maxStackFrames?: number;\n /**\n * Release Health — opens a session at init() with status='ok', flips to\n * 'crashed'/'errored' if a fatal/error fires through the global handlers.\n * The dashboard derives crash-free user rate per release. Default: true.\n */\n releaseHealth?: boolean;\n}\n\nconst DEFAULT_ENDPOINT = 'https://pionne.agkgcreations.fr/api/ingest';\nconst DEFAULT_MAX_STACK = 50;\nconst SDK_NAME = 'pionne.node';\nconst SDK_VERSION = '0.1.0';\n\ntype ResolvedConfig = Required<\n Omit<PionneOptions, 'beforeSend' | 'userIdAnon' | 'tags' | 'release' | 'releaseHealth'>\n> & {\n beforeSend?: PionneOptions['beforeSend'];\n userIdAnon?: string;\n tags?: Record<string, string>;\n release?: string;\n};\n\nlet config: ResolvedConfig | null = null;\nlet staticContext: Partial<PionneEvent> = {};\nlet onUncaught: ((err: Error) => void) | null = null;\nlet onRejection: ((reason: unknown) => void) | null = null;\n\nfunction gatherStaticContext(): Partial<PionneEvent> {\n let timezone: string | undefined;\n try {\n timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;\n } catch {\n // ignore\n }\n return {\n os_name: os.type(),\n os_version: os.release(),\n timezone,\n contexts: {\n sdk: { name: SDK_NAME, version: SDK_VERSION },\n runtime: {\n name: 'node',\n version: process.versions.node,\n v8: process.versions.v8,\n },\n os: {\n name: os.type(),\n version: os.release(),\n platform: process.platform,\n arch: process.arch,\n cpu_count: os.cpus().length,\n total_memory: os.totalmem(),\n free_memory: os.freemem(),\n },\n app: {\n hostname: os.hostname(),\n pid: process.pid,\n cwd: process.cwd(),\n },\n },\n };\n}\n\nfunction parseStack(error: Error, max: number): string[] {\n if (!error.stack) return [];\n return error.stack\n .split('\\n')\n .slice(0, max)\n .map((s) => s.trim())\n .filter(Boolean);\n}\n\nfunction buildEvent(\n err: unknown,\n level: Level,\n mechanism: MechanismType,\n handled: boolean,\n extra?: Partial<PionneEvent>,\n): PionneEvent | null {\n if (!config || !config.enabled) return null;\n const e = err instanceof Error ? err : new Error(String(err));\n const event: PionneEvent = {\n ...staticContext,\n exception_type: e.name || 'Error',\n message: e.message || null,\n stack: parseStack(e, config.maxStackFrames),\n level,\n release: config.release,\n environment: config.environment,\n user_id_anon: config.userIdAnon,\n tags: config.tags,\n mechanism: { type: mechanism, handled },\n ...extra,\n };\n if (config.beforeSend) {\n const result = config.beforeSend(event);\n if (!result) return null;\n return result;\n }\n return event;\n}\n\nasync function send(event: PionneEvent): Promise<void> {\n if (!config) return;\n try {\n // Node >=18 has global `fetch`. We rely on it instead of pulling in\n // node-fetch / undici as a dependency.\n if (typeof fetch !== 'function') return;\n await fetch(config.endpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Pionne-Token': config.token,\n },\n body: JSON.stringify(event),\n });\n } catch {\n // Best-effort: a monitoring SDK must never crash the host process.\n }\n}\n\nfunction installUncaughtHandler(): void {\n onUncaught = (err: Error) => {\n const event = buildEvent(err, 'fatal', 'uncaughtException', false);\n if (event) {\n // Fire-and-forget: process is going to die anyway. Best we can do is\n // try to flush before exit, but Node will tear down imminently.\n void send(event);\n flipFromEvent(event.level, event.mechanism?.type ?? 'uncaughtException');\n }\n };\n process.on('uncaughtException', onUncaught);\n}\n\nfunction installRejectionHandler(): void {\n onRejection = (reason: unknown) => {\n const err = reason instanceof Error ? reason : new Error(String(reason));\n const event = buildEvent(err, 'error', 'unhandledRejection', false);\n if (event) {\n void send(event);\n flipFromEvent(event.level, event.mechanism?.type ?? 'unhandledRejection');\n }\n };\n process.on('unhandledRejection', onRejection);\n}\n\nexport const Pionne = {\n init(options: PionneOptions): void {\n if (!options?.token || !options.token.startsWith('pio_live_')) {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n '[Pionne] Missing or invalid token (must start with pio_live_).',\n );\n }\n return;\n }\n\n const autoContext = options.autoContext ?? true;\n staticContext = autoContext ? gatherStaticContext() : {};\n\n config = {\n token: options.token,\n endpoint: options.endpoint ?? DEFAULT_ENDPOINT,\n release: options.release,\n environment:\n options.environment ?? process.env.NODE_ENV ?? 'production',\n enabled: options.enabled ?? true,\n captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,\n captureUnhandledRejections: options.captureUnhandledRejections ?? true,\n autoContext,\n beforeSend: options.beforeSend,\n userIdAnon: options.userIdAnon,\n tags: options.tags,\n maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK,\n };\n\n if (config.captureUncaughtExceptions) installUncaughtHandler();\n if (config.captureUnhandledRejections) installRejectionHandler();\n\n // Release Health — open a session unless the host opted out.\n if (options.releaseHealth !== false) {\n _startSession({\n endpoint: config.endpoint,\n token: config.token,\n release: config.release,\n environment: config.environment,\n appVersion: staticContext.app_version,\n osName: staticContext.os_name,\n userIdAnon: config.userIdAnon,\n });\n }\n },\n\n captureException(err: unknown, extra?: Partial<PionneEvent>): void {\n const event = buildEvent(\n err,\n extra?.level ?? 'error',\n 'manual',\n true,\n extra,\n );\n if (event) void send(event);\n },\n\n captureMessage(message: string, extra?: Partial<PionneEvent>): void {\n const event = buildEvent(\n new Error(message),\n extra?.level ?? 'info',\n 'manual',\n true,\n { exception_type: 'Message', ...extra },\n );\n if (event) void send(event);\n },\n\n setUser(userIdAnon: string | null): void {\n if (!config) return;\n config.userIdAnon = userIdAnon ?? undefined;\n },\n\n setTags(tags: Record<string, string> | null): void {\n if (!config) return;\n config.tags = tags ?? undefined;\n },\n\n setEnabled(enabled: boolean): void {\n if (!config) return;\n config.enabled = enabled;\n },\n\n /**\n * Detach all auto handlers. Useful in tests / CLI scripts that need a\n * clean shutdown. Re-init by calling `init()` again.\n */\n uninstall(): void {\n if (onUncaught) process.removeListener('uncaughtException', onUncaught);\n if (onRejection) process.removeListener('unhandledRejection', onRejection);\n onUncaught = null;\n onRejection = null;\n config = null;\n staticContext = {};\n },\n\n // ─── Release Health ───────────────────────────────────────────────────\n\n /** Manually end the current session (status='exited'). */\n endSession(): void {\n _endSession();\n },\n\n /** UUID of the current open session (for diagnostics). */\n getSessionId(): string | null {\n return getCurrentSessionId();\n },\n};\n\n/**\n * Express / Connect / NestJS error middleware. Reports the error then passes\n * it down the chain. Mount it AFTER your routes:\n *\n * import { Pionne, expressErrorHandler } from '@pionne/node';\n * app.use(expressErrorHandler);\n * // your fallback error handler here\n */\nexport function expressErrorHandler(\n err: unknown,\n _req: unknown,\n _res: unknown,\n next: (err?: unknown) => void,\n): void {\n Pionne.captureException(err);\n next(err);\n}\n","// Release Health for Node.js. Same protocol as the browser/RN SDKs.\n// We auto-flip on uncaughtException/unhandledRejection through the host\n// SDK's existing handlers, and best-effort-emit 'exited' on process.exit.\n\nimport * as crypto from 'node:crypto';\n\nexport type SessionStatus = 'ok' | 'crashed' | 'errored' | 'abnormal' | 'exited';\n\nexport interface SessionContext {\n endpoint: string;\n token: string;\n release?: string;\n environment?: string;\n appVersion?: string;\n osName?: string;\n userIdAnon?: string;\n}\n\ninterface SessionState {\n id: string;\n startedAt: number;\n status: SessionStatus;\n ctx: SessionContext;\n}\n\nlet current: SessionState | null = null;\n\nfunction sessionsUrl(ingestEndpoint: string): string {\n if (ingestEndpoint.endsWith('/ingest')) {\n return ingestEndpoint.slice(0, -'/ingest'.length) + '/sessions';\n }\n return ingestEndpoint.replace(/\\/+$/, '') + '/sessions';\n}\n\nfunction postSession(state: SessionState, status: SessionStatus, durationMs?: number): void {\n const url = sessionsUrl(state.ctx.endpoint);\n const body = {\n session_id: state.id,\n status,\n release: state.ctx.release,\n environment: state.ctx.environment,\n app_version: state.ctx.appVersion,\n os_name: state.ctx.osName,\n user_id_anon: state.ctx.userIdAnon,\n duration_ms: durationMs,\n };\n for (const k of Object.keys(body) as (keyof typeof body)[]) {\n if (body[k] === undefined) delete body[k];\n }\n // Node 18+ has global fetch.\n fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Pionne-Token': state.ctx.token,\n },\n body: JSON.stringify(body),\n }).catch(() => undefined);\n}\n\nexport function startSession(ctx: SessionContext): string {\n current = {\n id: crypto.randomUUID(),\n startedAt: Date.now(),\n status: 'ok',\n ctx,\n };\n postSession(current, 'ok');\n\n // Best-effort 'exited' flip on graceful shutdown. We listen on 'exit'\n // (sync only — no async I/O guaranteed) and 'beforeExit' (where async\n // works). The actual session POST is fire-and-forget anyway.\n const onShutdown = () => {\n if (!current || current.status !== 'ok') return;\n flipSession('exited');\n };\n process.once('beforeExit', onShutdown);\n\n return current.id;\n}\n\nexport function flipSession(status: SessionStatus): void {\n if (!current) return;\n const rank: Record<SessionStatus, number> =\n { ok: 0, exited: 1, errored: 2, abnormal: 3, crashed: 4 };\n if (rank[status] <= rank[current.status]) return;\n current.status = status;\n postSession(current, status, Date.now() - current.startedAt);\n}\n\nexport function endSession(status: SessionStatus = 'exited'): void {\n if (!current) return;\n flipSession(status);\n current = null;\n}\n\nexport function getCurrentSessionId(): string | null {\n return current?.id ?? null;\n}\n\nexport function flipFromEvent(\n level: 'fatal' | 'error' | 'warning' | 'info' | undefined,\n mechanismType: string,\n): void {\n if (mechanismType === 'manual') return;\n if (level === 'fatal') flipSession('crashed');\n else if (level === 'error') flipSession('errored');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,SAAoB;AACpB,IAAAA,WAAyB;;;ACGzB,aAAwB;AAqBxB,IAAI,UAA+B;AAEnC,SAAS,YAAY,gBAAgC;AACnD,MAAI,eAAe,SAAS,SAAS,GAAG;AACtC,WAAO,eAAe,MAAM,GAAG,CAAC,UAAU,MAAM,IAAI;AAAA,EACtD;AACA,SAAO,eAAe,QAAQ,QAAQ,EAAE,IAAI;AAC9C;AAEA,SAAS,YAAY,OAAqB,QAAuB,YAA2B;AAC1F,QAAM,MAAM,YAAY,MAAM,IAAI,QAAQ;AAC1C,QAAM,OAAO;AAAA,IACX,YAAY,MAAM;AAAA,IAClB;AAAA,IACA,SAAS,MAAM,IAAI;AAAA,IACnB,aAAa,MAAM,IAAI;AAAA,IACvB,aAAa,MAAM,IAAI;AAAA,IACvB,SAAS,MAAM,IAAI;AAAA,IACnB,cAAc,MAAM,IAAI;AAAA,IACxB,aAAa;AAAA,EACf;AACA,aAAW,KAAK,OAAO,KAA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ACjB,OAAO,SAAS;AAAA,MAChB;AAAA,MACA;AAAA,MACA,EAAE,gBAAgB,WAAW,GAAG,MAAM;AAAA,IACxC;AACA,QAAI,MAAO,MAAK,KAAK,KAAK;AAAA,EAC5B;AAAA,EAEA,QAAQ,YAAiC;AACvC,QAAI,CAAC,OAAQ;AACb,WAAO,aAAa,cAAc;AAAA,EACpC;AAAA,EAEA,QAAQ,MAA2C;AACjD,QAAI,CAAC,OAAQ;AACb,WAAO,OAAO,QAAQ;AAAA,EACxB;AAAA,EAEA,WAAW,SAAwB;AACjC,QAAI,CAAC,OAAQ;AACb,WAAO,UAAU;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAkB;AAChB,QAAI,WAAY,CAAQ,wBAAe,qBAAqB,UAAU;AACtE,QAAI,YAAa,CAAQ,wBAAe,sBAAsB,WAAW;AACzE,iBAAa;AACb,kBAAc;AACd,aAAS;AACT,oBAAgB,CAAC;AAAA,EACnB;AAAA;AAAA;AAAA,EAKA,aAAmB;AACjB,eAAY;AAAA,EACd;AAAA;AAAA,EAGA,eAA8B;AAC5B,WAAO,oBAAoB;AAAA,EAC7B;AACF;AAUO,SAAS,oBACd,KACA,MACA,MACA,MACM;AACN,SAAO,iBAAiB,GAAG;AAC3B,OAAK,GAAG;AACV;","names":["process"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/security.ts","../src/sessions.ts"],"sourcesContent":["import * as os from 'node:os';\nimport * as process from 'node:process';\n\nimport { RateLimiter, validateEndpoint, validateToken } from './security';\nimport {\n endSession as _endSession,\n flipFromEvent,\n getCurrentSessionId,\n startSession as _startSession,\n} from './sessions';\n\nexport type Level = 'fatal' | 'error' | 'warning' | 'info';\nexport type MechanismType =\n | 'uncaughtException'\n | 'unhandledRejection'\n | 'manual';\n\nexport interface Mechanism {\n type: MechanismType;\n handled: boolean;\n}\n\nexport interface PionneEvent {\n exception_type: string;\n message?: string | null;\n stack?: string[];\n level?: Level;\n\n release?: string;\n environment?: string;\n app_version?: string;\n os_name?: string;\n os_version?: string;\n user_id_anon?: string;\n locale?: string;\n timezone?: string;\n\n contexts?: Record<string, Record<string, unknown> | undefined>;\n mechanism?: Mechanism;\n tags?: Record<string, string>;\n}\n\nexport interface PionneOptions {\n /** Project token (starts with `pio_live_`). Required. */\n token: string;\n endpoint?: string;\n release?: string;\n environment?: string;\n enabled?: boolean;\n captureUncaughtExceptions?: boolean;\n captureUnhandledRejections?: boolean;\n autoContext?: boolean;\n beforeSend?: (event: PionneEvent) => PionneEvent | null;\n userIdAnon?: string;\n tags?: Record<string, string>;\n maxStackFrames?: number;\n /**\n * Release Health — opens a session at init() with status='ok', flips to\n * 'crashed'/'errored' if a fatal/error fires through the global handlers.\n * The dashboard derives crash-free user rate per release. Default: true.\n */\n releaseHealth?: boolean;\n /** Token-bucket rate limit (events/sec). Default 10, set 0 to disable. */\n maxEventsPerSecond?: number;\n /**\n * Opt-in: resolve approximate server geography (city, region, country) once\n * at startup via a free IP→location lookup, and attach it to every event\n * under `contexts.geo`. Off by default for privacy.\n */\n sendGeography?: boolean;\n /**\n * Override the IP→geography endpoint. Must return JSON with at least\n * `city`, `region`, `country` (or `country_name`), and `country_code`\n * fields. Default: `https://ipapi.co/json/`.\n */\n geographyEndpoint?: string;\n}\n\nconst DEFAULT_ENDPOINT = 'https://pionne.agkgcreations.fr/api/ingest';\nconst DEFAULT_GEO_ENDPOINT = 'https://ipapi.co/json/';\nconst DEFAULT_MAX_STACK = 50;\nconst SDK_NAME = 'pionne.node';\nconst SDK_VERSION = '0.1.0';\n\ntype ResolvedConfig = Required<\n Omit<PionneOptions, 'beforeSend' | 'userIdAnon' | 'tags' | 'release' | 'releaseHealth' | 'maxEventsPerSecond'>\n> & {\n beforeSend?: PionneOptions['beforeSend'];\n userIdAnon?: string;\n tags?: Record<string, string>;\n release?: string;\n};\n\nlet config: ResolvedConfig | null = null;\nlet rateLimiter: RateLimiter | null = null;\nlet droppedByRateLimit = 0;\nlet staticContext: Partial<PionneEvent> = {};\nlet onUncaught: ((err: Error) => void) | null = null;\nlet onRejection: ((reason: unknown) => void) | null = null;\n\nfunction gatherStaticContext(): Partial<PionneEvent> {\n let timezone: string | undefined;\n try {\n timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;\n } catch {\n // ignore\n }\n return {\n os_name: os.type(),\n os_version: os.release(),\n timezone,\n contexts: {\n sdk: { name: SDK_NAME, version: SDK_VERSION },\n runtime: {\n name: 'node',\n version: process.versions.node,\n v8: process.versions.v8,\n },\n os: {\n name: os.type(),\n version: os.release(),\n platform: process.platform,\n arch: process.arch,\n cpu_count: os.cpus().length,\n total_memory: os.totalmem(),\n free_memory: os.freemem(),\n },\n app: {\n hostname: os.hostname(),\n pid: process.pid,\n cwd: process.cwd(),\n },\n },\n };\n}\n\nfunction parseStack(error: Error, max: number): string[] {\n if (!error.stack) return [];\n return error.stack\n .split('\\n')\n .slice(0, max)\n .map((s) => s.trim())\n .filter(Boolean);\n}\n\nfunction buildEvent(\n err: unknown,\n level: Level,\n mechanism: MechanismType,\n handled: boolean,\n extra?: Partial<PionneEvent>,\n): PionneEvent | null {\n if (!config || !config.enabled) return null;\n const e = err instanceof Error ? err : new Error(String(err));\n const event: PionneEvent = {\n ...staticContext,\n exception_type: e.name || 'Error',\n message: e.message || null,\n stack: parseStack(e, config.maxStackFrames),\n level,\n release: config.release,\n environment: config.environment,\n user_id_anon: config.userIdAnon,\n tags: config.tags,\n mechanism: { type: mechanism, handled },\n ...extra,\n };\n if (config.beforeSend) {\n const result = config.beforeSend(event);\n if (!result) return null;\n return result;\n }\n return event;\n}\n\nasync function send(event: PionneEvent): Promise<void> {\n if (rateLimiter && !rateLimiter.allow()) {\n droppedByRateLimit++;\n if (process.env.NODE_ENV !== 'production' && droppedByRateLimit % 50 === 1) {\n console.warn(`[Pionne] rate-limit reached (${droppedByRateLimit} events dropped). Bump maxEventsPerSecond if intentional.`);\n }\n return;\n }\n\n if (!config) return;\n try {\n // Node >=18 has global `fetch`. We rely on it instead of pulling in\n // node-fetch / undici as a dependency.\n if (typeof fetch !== 'function') return;\n await fetch(config.endpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Pionne-Token': config.token,\n },\n body: JSON.stringify(event),\n });\n } catch {\n // Best-effort: a monitoring SDK must never crash the host process.\n }\n}\n\n/**\n * Fire-and-forget IP→geo lookup. Mutates `staticContext.contexts.geo` once it\n * resolves so subsequent events carry the location. Failures are silent — a\n * monitoring SDK must never crash or stall the host process.\n */\nfunction fetchGeography(endpoint: string): void {\n if (typeof fetch !== 'function') return;\n const controller =\n typeof AbortController !== 'undefined' ? new AbortController() : null;\n const timeout = controller\n ? setTimeout(() => controller.abort(), 4000)\n : null;\n fetch(endpoint, {\n method: 'GET',\n headers: { Accept: 'application/json' },\n signal: controller?.signal,\n })\n .then((res) => (res.ok ? res.json() : null))\n .then((data: unknown) => {\n if (!data || typeof data !== 'object') return;\n const d = data as Record<string, unknown>;\n const geo: Record<string, string> = {};\n if (typeof d.city === 'string') geo.city = d.city;\n if (typeof d.region === 'string') geo.region = d.region;\n if (typeof d.country_name === 'string') geo.country = d.country_name;\n else if (typeof d.country === 'string') geo.country = d.country;\n if (typeof d.country_code === 'string') geo.country_code = d.country_code;\n if (Object.keys(geo).length === 0) return;\n const ctx = staticContext.contexts ?? {};\n staticContext.contexts = { ...ctx, geo };\n })\n .catch(() => {\n // Best-effort: silently ignore lookup failures.\n })\n .finally(() => {\n if (timeout) clearTimeout(timeout);\n });\n}\n\nfunction installUncaughtHandler(): void {\n onUncaught = (err: Error) => {\n const event = buildEvent(err, 'fatal', 'uncaughtException', false);\n if (event) {\n // Fire-and-forget: process is going to die anyway. Best we can do is\n // try to flush before exit, but Node will tear down imminently.\n void send(event);\n flipFromEvent(event.level, event.mechanism?.type ?? 'uncaughtException');\n }\n };\n process.on('uncaughtException', onUncaught);\n}\n\nfunction installRejectionHandler(): void {\n onRejection = (reason: unknown) => {\n const err = reason instanceof Error ? reason : new Error(String(reason));\n const event = buildEvent(err, 'error', 'unhandledRejection', false);\n if (event) {\n void send(event);\n flipFromEvent(event.level, event.mechanism?.type ?? 'unhandledRejection');\n }\n };\n process.on('unhandledRejection', onRejection);\n}\n\nexport const Pionne = {\n init(options: PionneOptions): void {\n try {\n const isDev = process.env.NODE_ENV !== 'production';\n if (!options?.token || !validateToken(options.token)) {\n if (isDev) {\n console.warn('[Pionne] Missing or invalid token (expected pio_live_<≥16 chars>, no placeholders).');\n }\n return;\n }\n const endpoint = options.endpoint ?? DEFAULT_ENDPOINT;\n if (!validateEndpoint(endpoint, isDev)) {\n console.warn('[Pionne] Refusing non-HTTPS endpoint in production:', endpoint);\n return;\n }\n const rps = options.maxEventsPerSecond ?? 10;\n rateLimiter = rps > 0 ? new RateLimiter(rps, rps) : null;\n\n const autoContext = options.autoContext ?? true;\n staticContext = autoContext ? gatherStaticContext() : {};\n\n config = {\n token: options.token,\n endpoint: options.endpoint ?? DEFAULT_ENDPOINT,\n release: options.release,\n environment:\n options.environment ?? process.env.NODE_ENV ?? 'production',\n enabled: options.enabled ?? true,\n captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,\n captureUnhandledRejections: options.captureUnhandledRejections ?? true,\n autoContext,\n beforeSend: options.beforeSend,\n userIdAnon: options.userIdAnon,\n tags: options.tags,\n maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK,\n sendGeography: options.sendGeography ?? false,\n geographyEndpoint: options.geographyEndpoint ?? DEFAULT_GEO_ENDPOINT,\n };\n\n if (config.captureUncaughtExceptions) installUncaughtHandler();\n if (config.captureUnhandledRejections) installRejectionHandler();\n if (config.sendGeography) fetchGeography(config.geographyEndpoint);\n\n // Release Health — open a session unless the host opted out.\n if (options.releaseHealth !== false) {\n _startSession({\n endpoint: config.endpoint,\n token: config.token,\n release: config.release,\n environment: config.environment,\n appVersion: staticContext.app_version,\n osName: staticContext.os_name,\n userIdAnon: config.userIdAnon,\n });\n }\n } catch (e) {\n console.warn('[Pionne] init failed silently — monitoring disabled.', e);\n config = null;\n }\n },\n\n captureException(err: unknown, extra?: Partial<PionneEvent>): void {\n const event = buildEvent(\n err,\n extra?.level ?? 'error',\n 'manual',\n true,\n extra,\n );\n if (event) void send(event);\n },\n\n captureMessage(message: string, extra?: Partial<PionneEvent>): void {\n const event = buildEvent(\n new Error(message),\n extra?.level ?? 'info',\n 'manual',\n true,\n { exception_type: 'Message', ...extra },\n );\n if (event) void send(event);\n },\n\n setUser(userIdAnon: string | null): void {\n if (!config) return;\n config.userIdAnon = userIdAnon ?? undefined;\n },\n\n setTags(tags: Record<string, string> | null): void {\n if (!config) return;\n config.tags = tags ?? undefined;\n },\n\n setEnabled(enabled: boolean): void {\n if (!config) return;\n config.enabled = enabled;\n },\n\n /**\n * Detach all auto handlers. Useful in tests / CLI scripts that need a\n * clean shutdown. Re-init by calling `init()` again.\n */\n uninstall(): void {\n if (onUncaught) process.removeListener('uncaughtException', onUncaught);\n if (onRejection) process.removeListener('unhandledRejection', onRejection);\n onUncaught = null;\n onRejection = null;\n config = null;\n staticContext = {};\n },\n\n // ─── Release Health ───────────────────────────────────────────────────\n\n /** Manually end the current session (status='exited'). */\n endSession(): void {\n _endSession();\n },\n\n /** UUID of the current open session (for diagnostics). */\n getSessionId(): string | null {\n return getCurrentSessionId();\n },\n};\n\n/**\n * Express / Connect / NestJS error middleware. Reports the error then passes\n * it down the chain. Mount it AFTER your routes:\n *\n * import { Pionne, expressErrorHandler } from '@pionne/node';\n * app.use(expressErrorHandler);\n * // your fallback error handler here\n */\nexport function expressErrorHandler(\n err: unknown,\n _req: unknown,\n _res: unknown,\n next: (err?: unknown) => void,\n): void {\n Pionne.captureException(err);\n next(err);\n}\n","// Mirror of @pionne/react-native and @pionne/web security guards.\n// Node-specific tweak: the \"localhost\" rule for non-HTTPS endpoints\n// also accepts hosts ending in `.local` (mDNS) — useful for staging\n// boxes behind Tailscale/ZeroTier.\n\nconst TOKEN_PREFIX = 'pio_live_';\nconst MIN_TOKEN_LENGTH = TOKEN_PREFIX.length + 16;\n\nexport function validateEndpoint(endpoint: string, isDev: boolean): boolean {\n try {\n const u = new URL(endpoint);\n if (u.protocol === 'https:') return true;\n if (u.protocol !== 'http:') return false;\n if (!isDev) return false;\n return /^(localhost|127\\.0\\.0\\.1|0\\.0\\.0\\.0|\\[::1\\]|.*\\.local)$/.test(u.hostname);\n } catch {\n return false;\n }\n}\n\nexport function validateToken(token: string): boolean {\n if (typeof token !== 'string') return false;\n if (!token.startsWith(TOKEN_PREFIX)) return false;\n if (token.length < MIN_TOKEN_LENGTH) return false;\n const lower = token.toLowerCase();\n for (const bad of ['xxx', 'yyy', 'todo', 'fixme', 'replace', 'changeme']) {\n if (lower.includes(bad)) return false;\n }\n return true;\n}\n\nexport class RateLimiter {\n private tokens: number;\n private lastRefill: number;\n constructor(\n private capacity: number,\n private refillPerSecond: number,\n ) {\n this.tokens = capacity;\n this.lastRefill = Date.now();\n }\n allow(): boolean {\n if (this.refillPerSecond <= 0) return true;\n const now = Date.now();\n const elapsedMs = now - this.lastRefill;\n if (elapsedMs > 0) {\n const refill = (elapsedMs / 1000) * this.refillPerSecond;\n this.tokens = Math.min(this.capacity, this.tokens + refill);\n this.lastRefill = now;\n }\n if (this.tokens >= 1) {\n this.tokens -= 1;\n return true;\n }\n return false;\n }\n}\n","// Release Health for Node.js. Same protocol as the browser/RN SDKs.\n// We auto-flip on uncaughtException/unhandledRejection through the host\n// SDK's existing handlers, and best-effort-emit 'exited' on process.exit.\n\nimport * as crypto from 'node:crypto';\n\nexport type SessionStatus = 'ok' | 'crashed' | 'errored' | 'abnormal' | 'exited';\n\nexport interface SessionContext {\n endpoint: string;\n token: string;\n release?: string;\n environment?: string;\n appVersion?: string;\n osName?: string;\n userIdAnon?: string;\n}\n\ninterface SessionState {\n id: string;\n startedAt: number;\n status: SessionStatus;\n ctx: SessionContext;\n}\n\nlet current: SessionState | null = null;\n\nfunction sessionsUrl(ingestEndpoint: string): string {\n if (ingestEndpoint.endsWith('/ingest')) {\n return ingestEndpoint.slice(0, -'/ingest'.length) + '/sessions';\n }\n return ingestEndpoint.replace(/\\/+$/, '') + '/sessions';\n}\n\nfunction postSession(state: SessionState, status: SessionStatus, durationMs?: number): void {\n const url = sessionsUrl(state.ctx.endpoint);\n const body = {\n session_id: state.id,\n status,\n release: state.ctx.release,\n environment: state.ctx.environment,\n app_version: state.ctx.appVersion,\n os_name: state.ctx.osName,\n user_id_anon: state.ctx.userIdAnon,\n duration_ms: durationMs,\n };\n for (const k of Object.keys(body) as (keyof typeof body)[]) {\n if (body[k] === undefined) delete body[k];\n }\n // Node 18+ has global fetch.\n fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Pionne-Token': state.ctx.token,\n },\n body: JSON.stringify(body),\n }).catch(() => undefined);\n}\n\nexport function startSession(ctx: SessionContext): string {\n current = {\n id: crypto.randomUUID(),\n startedAt: Date.now(),\n status: 'ok',\n ctx,\n };\n postSession(current, 'ok');\n\n // Best-effort 'exited' flip on graceful shutdown. We listen on 'exit'\n // (sync only — no async I/O guaranteed) and 'beforeExit' (where async\n // works). The actual session POST is fire-and-forget anyway.\n const onShutdown = () => {\n if (!current || current.status !== 'ok') return;\n flipSession('exited');\n };\n process.once('beforeExit', onShutdown);\n\n return current.id;\n}\n\nexport function flipSession(status: SessionStatus): void {\n if (!current) return;\n const rank: Record<SessionStatus, number> =\n { ok: 0, exited: 1, errored: 2, abnormal: 3, crashed: 4 };\n if (rank[status] <= rank[current.status]) return;\n current.status = status;\n postSession(current, status, Date.now() - current.startedAt);\n}\n\nexport function endSession(status: SessionStatus = 'exited'): void {\n if (!current) return;\n flipSession(status);\n current = null;\n}\n\nexport function getCurrentSessionId(): string | null {\n return current?.id ?? null;\n}\n\nexport function flipFromEvent(\n level: 'fatal' | 'error' | 'warning' | 'info' | undefined,\n mechanismType: string,\n): void {\n if (mechanismType === 'manual') return;\n if (level === 'fatal') flipSession('crashed');\n else if (level === 'error') flipSession('errored');\n}\n"],"m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EACpC;AAAA,EAEA,QAAQ,MAA2C;AACjD,QAAI,CAAC,OAAQ;AACb,WAAO,OAAO,QAAQ;AAAA,EACxB;AAAA,EAEA,WAAW,SAAwB;AACjC,QAAI,CAAC,OAAQ;AACb,WAAO,UAAU;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAkB;AAChB,QAAI,WAAY,CAAQ,wBAAe,qBAAqB,UAAU;AACtE,QAAI,YAAa,CAAQ,wBAAe,sBAAsB,WAAW;AACzE,iBAAa;AACb,kBAAc;AACd,aAAS;AACT,oBAAgB,CAAC;AAAA,EACnB;AAAA;AAAA;AAAA,EAKA,aAAmB;AACjB,eAAY;AAAA,EACd;AAAA;AAAA,EAGA,eAA8B;AAC5B,WAAO,oBAAoB;AAAA,EAC7B;AACF;AAUO,SAAS,oBACd,KACA,MACA,MACA,MACM;AACN,SAAO,iBAAiB,GAAG;AAC3B,OAAK,GAAG;AACV;","names":["process"]}
package/dist/index.d.mts CHANGED
@@ -41,6 +41,20 @@ interface PionneOptions {
41
41
  * The dashboard derives crash-free user rate per release. Default: true.
42
42
  */
43
43
  releaseHealth?: boolean;
44
+ /** Token-bucket rate limit (events/sec). Default 10, set 0 to disable. */
45
+ maxEventsPerSecond?: number;
46
+ /**
47
+ * Opt-in: resolve approximate server geography (city, region, country) once
48
+ * at startup via a free IP→location lookup, and attach it to every event
49
+ * under `contexts.geo`. Off by default for privacy.
50
+ */
51
+ sendGeography?: boolean;
52
+ /**
53
+ * Override the IP→geography endpoint. Must return JSON with at least
54
+ * `city`, `region`, `country` (or `country_name`), and `country_code`
55
+ * fields. Default: `https://ipapi.co/json/`.
56
+ */
57
+ geographyEndpoint?: string;
44
58
  }
45
59
  declare const Pionne: {
46
60
  init(options: PionneOptions): void;
package/dist/index.d.ts CHANGED
@@ -41,6 +41,20 @@ interface PionneOptions {
41
41
  * The dashboard derives crash-free user rate per release. Default: true.
42
42
  */
43
43
  releaseHealth?: boolean;
44
+ /** Token-bucket rate limit (events/sec). Default 10, set 0 to disable. */
45
+ maxEventsPerSecond?: number;
46
+ /**
47
+ * Opt-in: resolve approximate server geography (city, region, country) once
48
+ * at startup via a free IP→location lookup, and attach it to every event
49
+ * under `contexts.geo`. Off by default for privacy.
50
+ */
51
+ sendGeography?: boolean;
52
+ /**
53
+ * Override the IP→geography endpoint. Must return JSON with at least
54
+ * `city`, `region`, `country` (or `country_name`), and `country_code`
55
+ * fields. Default: `https://ipapi.co/json/`.
56
+ */
57
+ geographyEndpoint?: string;
44
58
  }
45
59
  declare const Pionne: {
46
60
  init(options: PionneOptions): void;
package/dist/index.mjs CHANGED
@@ -2,6 +2,54 @@
2
2
  import * as os from "os";
3
3
  import * as process2 from "process";
4
4
 
5
+ // src/security.ts
6
+ var TOKEN_PREFIX = "pio_live_";
7
+ var MIN_TOKEN_LENGTH = TOKEN_PREFIX.length + 16;
8
+ function validateEndpoint(endpoint, isDev) {
9
+ try {
10
+ const u = new URL(endpoint);
11
+ if (u.protocol === "https:") return true;
12
+ if (u.protocol !== "http:") return false;
13
+ if (!isDev) return false;
14
+ return /^(localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\]|.*\.local)$/.test(u.hostname);
15
+ } catch {
16
+ return false;
17
+ }
18
+ }
19
+ function validateToken(token) {
20
+ if (typeof token !== "string") return false;
21
+ if (!token.startsWith(TOKEN_PREFIX)) return false;
22
+ if (token.length < MIN_TOKEN_LENGTH) return false;
23
+ const lower = token.toLowerCase();
24
+ for (const bad of ["xxx", "yyy", "todo", "fixme", "replace", "changeme"]) {
25
+ if (lower.includes(bad)) return false;
26
+ }
27
+ return true;
28
+ }
29
+ var RateLimiter = class {
30
+ constructor(capacity, refillPerSecond) {
31
+ this.capacity = capacity;
32
+ this.refillPerSecond = refillPerSecond;
33
+ this.tokens = capacity;
34
+ this.lastRefill = Date.now();
35
+ }
36
+ allow() {
37
+ if (this.refillPerSecond <= 0) return true;
38
+ const now = Date.now();
39
+ const elapsedMs = now - this.lastRefill;
40
+ if (elapsedMs > 0) {
41
+ const refill = elapsedMs / 1e3 * this.refillPerSecond;
42
+ this.tokens = Math.min(this.capacity, this.tokens + refill);
43
+ this.lastRefill = now;
44
+ }
45
+ if (this.tokens >= 1) {
46
+ this.tokens -= 1;
47
+ return true;
48
+ }
49
+ return false;
50
+ }
51
+ };
52
+
5
53
  // src/sessions.ts
6
54
  import * as crypto from "crypto";
7
55
  var current = null;
@@ -73,10 +121,13 @@ function flipFromEvent(level, mechanismType) {
73
121
 
74
122
  // src/index.ts
75
123
  var DEFAULT_ENDPOINT = "https://pionne.agkgcreations.fr/api/ingest";
124
+ var DEFAULT_GEO_ENDPOINT = "https://ipapi.co/json/";
76
125
  var DEFAULT_MAX_STACK = 50;
77
126
  var SDK_NAME = "pionne.node";
78
127
  var SDK_VERSION = "0.1.0";
79
128
  var config = null;
129
+ var rateLimiter = null;
130
+ var droppedByRateLimit = 0;
80
131
  var staticContext = {};
81
132
  var onUncaught = null;
82
133
  var onRejection = null;
@@ -142,6 +193,13 @@ function buildEvent(err, level, mechanism, handled, extra) {
142
193
  return event;
143
194
  }
144
195
  async function send(event) {
196
+ if (rateLimiter && !rateLimiter.allow()) {
197
+ droppedByRateLimit++;
198
+ if (process2.env.NODE_ENV !== "production" && droppedByRateLimit % 50 === 1) {
199
+ console.warn(`[Pionne] rate-limit reached (${droppedByRateLimit} events dropped). Bump maxEventsPerSecond if intentional.`);
200
+ }
201
+ return;
202
+ }
145
203
  if (!config) return;
146
204
  try {
147
205
  if (typeof fetch !== "function") return;
@@ -156,6 +214,31 @@ async function send(event) {
156
214
  } catch {
157
215
  }
158
216
  }
217
+ function fetchGeography(endpoint) {
218
+ if (typeof fetch !== "function") return;
219
+ const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
220
+ const timeout = controller ? setTimeout(() => controller.abort(), 4e3) : null;
221
+ fetch(endpoint, {
222
+ method: "GET",
223
+ headers: { Accept: "application/json" },
224
+ signal: controller?.signal
225
+ }).then((res) => res.ok ? res.json() : null).then((data) => {
226
+ if (!data || typeof data !== "object") return;
227
+ const d = data;
228
+ const geo = {};
229
+ if (typeof d.city === "string") geo.city = d.city;
230
+ if (typeof d.region === "string") geo.region = d.region;
231
+ if (typeof d.country_name === "string") geo.country = d.country_name;
232
+ else if (typeof d.country === "string") geo.country = d.country;
233
+ if (typeof d.country_code === "string") geo.country_code = d.country_code;
234
+ if (Object.keys(geo).length === 0) return;
235
+ const ctx = staticContext.contexts ?? {};
236
+ staticContext.contexts = { ...ctx, geo };
237
+ }).catch(() => {
238
+ }).finally(() => {
239
+ if (timeout) clearTimeout(timeout);
240
+ });
241
+ }
159
242
  function installUncaughtHandler() {
160
243
  onUncaught = (err) => {
161
244
  const event = buildEvent(err, "fatal", "uncaughtException", false);
@@ -179,42 +262,56 @@ function installRejectionHandler() {
179
262
  }
180
263
  var Pionne = {
181
264
  init(options) {
182
- if (!options?.token || !options.token.startsWith("pio_live_")) {
183
- if (process2.env.NODE_ENV !== "production") {
184
- console.warn(
185
- "[Pionne] Missing or invalid token (must start with pio_live_)."
186
- );
265
+ try {
266
+ const isDev = process2.env.NODE_ENV !== "production";
267
+ if (!options?.token || !validateToken(options.token)) {
268
+ if (isDev) {
269
+ console.warn("[Pionne] Missing or invalid token (expected pio_live_<\u226516 chars>, no placeholders).");
270
+ }
271
+ return;
187
272
  }
188
- return;
189
- }
190
- const autoContext = options.autoContext ?? true;
191
- staticContext = autoContext ? gatherStaticContext() : {};
192
- config = {
193
- token: options.token,
194
- endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
195
- release: options.release,
196
- environment: options.environment ?? process2.env.NODE_ENV ?? "production",
197
- enabled: options.enabled ?? true,
198
- captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,
199
- captureUnhandledRejections: options.captureUnhandledRejections ?? true,
200
- autoContext,
201
- beforeSend: options.beforeSend,
202
- userIdAnon: options.userIdAnon,
203
- tags: options.tags,
204
- maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK
205
- };
206
- if (config.captureUncaughtExceptions) installUncaughtHandler();
207
- if (config.captureUnhandledRejections) installRejectionHandler();
208
- if (options.releaseHealth !== false) {
209
- startSession({
210
- endpoint: config.endpoint,
211
- token: config.token,
212
- release: config.release,
213
- environment: config.environment,
214
- appVersion: staticContext.app_version,
215
- osName: staticContext.os_name,
216
- userIdAnon: config.userIdAnon
217
- });
273
+ const endpoint = options.endpoint ?? DEFAULT_ENDPOINT;
274
+ if (!validateEndpoint(endpoint, isDev)) {
275
+ console.warn("[Pionne] Refusing non-HTTPS endpoint in production:", endpoint);
276
+ return;
277
+ }
278
+ const rps = options.maxEventsPerSecond ?? 10;
279
+ rateLimiter = rps > 0 ? new RateLimiter(rps, rps) : null;
280
+ const autoContext = options.autoContext ?? true;
281
+ staticContext = autoContext ? gatherStaticContext() : {};
282
+ config = {
283
+ token: options.token,
284
+ endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
285
+ release: options.release,
286
+ environment: options.environment ?? process2.env.NODE_ENV ?? "production",
287
+ enabled: options.enabled ?? true,
288
+ captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,
289
+ captureUnhandledRejections: options.captureUnhandledRejections ?? true,
290
+ autoContext,
291
+ beforeSend: options.beforeSend,
292
+ userIdAnon: options.userIdAnon,
293
+ tags: options.tags,
294
+ maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK,
295
+ sendGeography: options.sendGeography ?? false,
296
+ geographyEndpoint: options.geographyEndpoint ?? DEFAULT_GEO_ENDPOINT
297
+ };
298
+ if (config.captureUncaughtExceptions) installUncaughtHandler();
299
+ if (config.captureUnhandledRejections) installRejectionHandler();
300
+ if (config.sendGeography) fetchGeography(config.geographyEndpoint);
301
+ if (options.releaseHealth !== false) {
302
+ startSession({
303
+ endpoint: config.endpoint,
304
+ token: config.token,
305
+ release: config.release,
306
+ environment: config.environment,
307
+ appVersion: staticContext.app_version,
308
+ osName: staticContext.os_name,
309
+ userIdAnon: config.userIdAnon
310
+ });
311
+ }
312
+ } catch (e) {
313
+ console.warn("[Pionne] init failed silently \u2014 monitoring disabled.", e);
314
+ config = null;
218
315
  }
219
316
  },
220
317
  captureException(err, extra) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/sessions.ts"],"sourcesContent":["import * as os from 'node:os';\nimport * as process from 'node:process';\n\nimport {\n endSession as _endSession,\n flipFromEvent,\n getCurrentSessionId,\n startSession as _startSession,\n} from './sessions';\n\nexport type Level = 'fatal' | 'error' | 'warning' | 'info';\nexport type MechanismType =\n | 'uncaughtException'\n | 'unhandledRejection'\n | 'manual';\n\nexport interface Mechanism {\n type: MechanismType;\n handled: boolean;\n}\n\nexport interface PionneEvent {\n exception_type: string;\n message?: string | null;\n stack?: string[];\n level?: Level;\n\n release?: string;\n environment?: string;\n app_version?: string;\n os_name?: string;\n os_version?: string;\n user_id_anon?: string;\n locale?: string;\n timezone?: string;\n\n contexts?: Record<string, Record<string, unknown> | undefined>;\n mechanism?: Mechanism;\n tags?: Record<string, string>;\n}\n\nexport interface PionneOptions {\n /** Project token (starts with `pio_live_`). Required. */\n token: string;\n endpoint?: string;\n release?: string;\n environment?: string;\n enabled?: boolean;\n captureUncaughtExceptions?: boolean;\n captureUnhandledRejections?: boolean;\n autoContext?: boolean;\n beforeSend?: (event: PionneEvent) => PionneEvent | null;\n userIdAnon?: string;\n tags?: Record<string, string>;\n maxStackFrames?: number;\n /**\n * Release Health — opens a session at init() with status='ok', flips to\n * 'crashed'/'errored' if a fatal/error fires through the global handlers.\n * The dashboard derives crash-free user rate per release. Default: true.\n */\n releaseHealth?: boolean;\n}\n\nconst DEFAULT_ENDPOINT = 'https://pionne.agkgcreations.fr/api/ingest';\nconst DEFAULT_MAX_STACK = 50;\nconst SDK_NAME = 'pionne.node';\nconst SDK_VERSION = '0.1.0';\n\ntype ResolvedConfig = Required<\n Omit<PionneOptions, 'beforeSend' | 'userIdAnon' | 'tags' | 'release' | 'releaseHealth'>\n> & {\n beforeSend?: PionneOptions['beforeSend'];\n userIdAnon?: string;\n tags?: Record<string, string>;\n release?: string;\n};\n\nlet config: ResolvedConfig | null = null;\nlet staticContext: Partial<PionneEvent> = {};\nlet onUncaught: ((err: Error) => void) | null = null;\nlet onRejection: ((reason: unknown) => void) | null = null;\n\nfunction gatherStaticContext(): Partial<PionneEvent> {\n let timezone: string | undefined;\n try {\n timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;\n } catch {\n // ignore\n }\n return {\n os_name: os.type(),\n os_version: os.release(),\n timezone,\n contexts: {\n sdk: { name: SDK_NAME, version: SDK_VERSION },\n runtime: {\n name: 'node',\n version: process.versions.node,\n v8: process.versions.v8,\n },\n os: {\n name: os.type(),\n version: os.release(),\n platform: process.platform,\n arch: process.arch,\n cpu_count: os.cpus().length,\n total_memory: os.totalmem(),\n free_memory: os.freemem(),\n },\n app: {\n hostname: os.hostname(),\n pid: process.pid,\n cwd: process.cwd(),\n },\n },\n };\n}\n\nfunction parseStack(error: Error, max: number): string[] {\n if (!error.stack) return [];\n return error.stack\n .split('\\n')\n .slice(0, max)\n .map((s) => s.trim())\n .filter(Boolean);\n}\n\nfunction buildEvent(\n err: unknown,\n level: Level,\n mechanism: MechanismType,\n handled: boolean,\n extra?: Partial<PionneEvent>,\n): PionneEvent | null {\n if (!config || !config.enabled) return null;\n const e = err instanceof Error ? err : new Error(String(err));\n const event: PionneEvent = {\n ...staticContext,\n exception_type: e.name || 'Error',\n message: e.message || null,\n stack: parseStack(e, config.maxStackFrames),\n level,\n release: config.release,\n environment: config.environment,\n user_id_anon: config.userIdAnon,\n tags: config.tags,\n mechanism: { type: mechanism, handled },\n ...extra,\n };\n if (config.beforeSend) {\n const result = config.beforeSend(event);\n if (!result) return null;\n return result;\n }\n return event;\n}\n\nasync function send(event: PionneEvent): Promise<void> {\n if (!config) return;\n try {\n // Node >=18 has global `fetch`. We rely on it instead of pulling in\n // node-fetch / undici as a dependency.\n if (typeof fetch !== 'function') return;\n await fetch(config.endpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Pionne-Token': config.token,\n },\n body: JSON.stringify(event),\n });\n } catch {\n // Best-effort: a monitoring SDK must never crash the host process.\n }\n}\n\nfunction installUncaughtHandler(): void {\n onUncaught = (err: Error) => {\n const event = buildEvent(err, 'fatal', 'uncaughtException', false);\n if (event) {\n // Fire-and-forget: process is going to die anyway. Best we can do is\n // try to flush before exit, but Node will tear down imminently.\n void send(event);\n flipFromEvent(event.level, event.mechanism?.type ?? 'uncaughtException');\n }\n };\n process.on('uncaughtException', onUncaught);\n}\n\nfunction installRejectionHandler(): void {\n onRejection = (reason: unknown) => {\n const err = reason instanceof Error ? reason : new Error(String(reason));\n const event = buildEvent(err, 'error', 'unhandledRejection', false);\n if (event) {\n void send(event);\n flipFromEvent(event.level, event.mechanism?.type ?? 'unhandledRejection');\n }\n };\n process.on('unhandledRejection', onRejection);\n}\n\nexport const Pionne = {\n init(options: PionneOptions): void {\n if (!options?.token || !options.token.startsWith('pio_live_')) {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n '[Pionne] Missing or invalid token (must start with pio_live_).',\n );\n }\n return;\n }\n\n const autoContext = options.autoContext ?? true;\n staticContext = autoContext ? gatherStaticContext() : {};\n\n config = {\n token: options.token,\n endpoint: options.endpoint ?? DEFAULT_ENDPOINT,\n release: options.release,\n environment:\n options.environment ?? process.env.NODE_ENV ?? 'production',\n enabled: options.enabled ?? true,\n captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,\n captureUnhandledRejections: options.captureUnhandledRejections ?? true,\n autoContext,\n beforeSend: options.beforeSend,\n userIdAnon: options.userIdAnon,\n tags: options.tags,\n maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK,\n };\n\n if (config.captureUncaughtExceptions) installUncaughtHandler();\n if (config.captureUnhandledRejections) installRejectionHandler();\n\n // Release Health — open a session unless the host opted out.\n if (options.releaseHealth !== false) {\n _startSession({\n endpoint: config.endpoint,\n token: config.token,\n release: config.release,\n environment: config.environment,\n appVersion: staticContext.app_version,\n osName: staticContext.os_name,\n userIdAnon: config.userIdAnon,\n });\n }\n },\n\n captureException(err: unknown, extra?: Partial<PionneEvent>): void {\n const event = buildEvent(\n err,\n extra?.level ?? 'error',\n 'manual',\n true,\n extra,\n );\n if (event) void send(event);\n },\n\n captureMessage(message: string, extra?: Partial<PionneEvent>): void {\n const event = buildEvent(\n new Error(message),\n extra?.level ?? 'info',\n 'manual',\n true,\n { exception_type: 'Message', ...extra },\n );\n if (event) void send(event);\n },\n\n setUser(userIdAnon: string | null): void {\n if (!config) return;\n config.userIdAnon = userIdAnon ?? undefined;\n },\n\n setTags(tags: Record<string, string> | null): void {\n if (!config) return;\n config.tags = tags ?? undefined;\n },\n\n setEnabled(enabled: boolean): void {\n if (!config) return;\n config.enabled = enabled;\n },\n\n /**\n * Detach all auto handlers. Useful in tests / CLI scripts that need a\n * clean shutdown. Re-init by calling `init()` again.\n */\n uninstall(): void {\n if (onUncaught) process.removeListener('uncaughtException', onUncaught);\n if (onRejection) process.removeListener('unhandledRejection', onRejection);\n onUncaught = null;\n onRejection = null;\n config = null;\n staticContext = {};\n },\n\n // ─── Release Health ───────────────────────────────────────────────────\n\n /** Manually end the current session (status='exited'). */\n endSession(): void {\n _endSession();\n },\n\n /** UUID of the current open session (for diagnostics). */\n getSessionId(): string | null {\n return getCurrentSessionId();\n },\n};\n\n/**\n * Express / Connect / NestJS error middleware. Reports the error then passes\n * it down the chain. Mount it AFTER your routes:\n *\n * import { Pionne, expressErrorHandler } from '@pionne/node';\n * app.use(expressErrorHandler);\n * // your fallback error handler here\n */\nexport function expressErrorHandler(\n err: unknown,\n _req: unknown,\n _res: unknown,\n next: (err?: unknown) => void,\n): void {\n Pionne.captureException(err);\n next(err);\n}\n","// Release Health for Node.js. Same protocol as the browser/RN SDKs.\n// We auto-flip on uncaughtException/unhandledRejection through the host\n// SDK's existing handlers, and best-effort-emit 'exited' on process.exit.\n\nimport * as crypto from 'node:crypto';\n\nexport type SessionStatus = 'ok' | 'crashed' | 'errored' | 'abnormal' | 'exited';\n\nexport interface SessionContext {\n endpoint: string;\n token: string;\n release?: string;\n environment?: string;\n appVersion?: string;\n osName?: string;\n userIdAnon?: string;\n}\n\ninterface SessionState {\n id: string;\n startedAt: number;\n status: SessionStatus;\n ctx: SessionContext;\n}\n\nlet current: SessionState | null = null;\n\nfunction sessionsUrl(ingestEndpoint: string): string {\n if (ingestEndpoint.endsWith('/ingest')) {\n return ingestEndpoint.slice(0, -'/ingest'.length) + '/sessions';\n }\n return ingestEndpoint.replace(/\\/+$/, '') + '/sessions';\n}\n\nfunction postSession(state: SessionState, status: SessionStatus, durationMs?: number): void {\n const url = sessionsUrl(state.ctx.endpoint);\n const body = {\n session_id: state.id,\n status,\n release: state.ctx.release,\n environment: state.ctx.environment,\n app_version: state.ctx.appVersion,\n os_name: state.ctx.osName,\n user_id_anon: state.ctx.userIdAnon,\n duration_ms: durationMs,\n };\n for (const k of Object.keys(body) as (keyof typeof body)[]) {\n if (body[k] === undefined) delete body[k];\n }\n // Node 18+ has global fetch.\n fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Pionne-Token': state.ctx.token,\n },\n body: JSON.stringify(body),\n }).catch(() => undefined);\n}\n\nexport function startSession(ctx: SessionContext): string {\n current = {\n id: crypto.randomUUID(),\n startedAt: Date.now(),\n status: 'ok',\n ctx,\n };\n postSession(current, 'ok');\n\n // Best-effort 'exited' flip on graceful shutdown. We listen on 'exit'\n // (sync only — no async I/O guaranteed) and 'beforeExit' (where async\n // works). The actual session POST is fire-and-forget anyway.\n const onShutdown = () => {\n if (!current || current.status !== 'ok') return;\n flipSession('exited');\n };\n process.once('beforeExit', onShutdown);\n\n return current.id;\n}\n\nexport function flipSession(status: SessionStatus): void {\n if (!current) return;\n const rank: Record<SessionStatus, number> =\n { ok: 0, exited: 1, errored: 2, abnormal: 3, crashed: 4 };\n if (rank[status] <= rank[current.status]) return;\n current.status = status;\n postSession(current, status, Date.now() - current.startedAt);\n}\n\nexport function endSession(status: SessionStatus = 'exited'): void {\n if (!current) return;\n flipSession(status);\n current = null;\n}\n\nexport function getCurrentSessionId(): string | null {\n return current?.id ?? null;\n}\n\nexport function flipFromEvent(\n level: 'fatal' | 'error' | 'warning' | 'info' | undefined,\n mechanismType: string,\n): void {\n if (mechanismType === 'manual') return;\n if (level === 'fatal') flipSession('crashed');\n else if (level === 'error') flipSession('errored');\n}\n"],"mappings":";AAAA,YAAY,QAAQ;AACpB,YAAYA,cAAa;;;ACGzB,YAAY,YAAY;AAqBxB,IAAI,UAA+B;AAEnC,SAAS,YAAY,gBAAgC;AACnD,MAAI,eAAe,SAAS,SAAS,GAAG;AACtC,WAAO,eAAe,MAAM,GAAG,CAAC,UAAU,MAAM,IAAI;AAAA,EACtD;AACA,SAAO,eAAe,QAAQ,QAAQ,EAAE,IAAI;AAC9C;AAEA,SAAS,YAAY,OAAqB,QAAuB,YAA2B;AAC1F,QAAM,MAAM,YAAY,MAAM,IAAI,QAAQ;AAC1C,QAAM,OAAO;AAAA,IACX,YAAY,MAAM;AAAA,IAClB;AAAA,IACA,SAAS,MAAM,IAAI;AAAA,IACnB,aAAa,MAAM,IAAI;AAAA,IACvB,aAAa,MAAM,IAAI;AAAA,IACvB,SAAS,MAAM,IAAI;AAAA,IACnB,cAAc,MAAM,IAAI;AAAA,IACxB,aAAa;AAAA,EACf;AACA,aAAW,KAAK,OAAO,KAAK,IAAI,GAA4B;AAC1D,QAAI,KAAK,CAAC,MAAM,OAAW,QAAO,KAAK,CAAC;AAAA,EAC1C;AAEA,QAAM,KAAK;AAAA,IACT,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,kBAAkB,MAAM,IAAI;AAAA,IAC9B;AAAA,IACA,MAAM,KAAK,UAAU,IAAI;AAAA,EAC3B,CAAC,EAAE,MAAM,MAAM,MAAS;AAC1B;AAEO,SAAS,aAAa,KAA6B;AACxD,YAAU;AAAA,IACR,IAAW,kBA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jC,QAAI,CAAC,OAAQ;AACb,WAAO,UAAU;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAkB;AAChB,QAAI,WAAY,CAAQ,wBAAe,qBAAqB,UAAU;AACtE,QAAI,YAAa,CAAQ,wBAAe,sBAAsB,WAAW;AACzE,iBAAa;AACb,kBAAc;AACd,aAAS;AACT,oBAAgB,CAAC;AAAA,EACnB;AAAA;AAAA;AAAA,EAKA,aAAmB;AACjB,eAAY;AAAA,EACd;AAAA;AAAA,EAGA,eAA8B;AAC5B,WAAO,oBAAoB;AAAA,EAC7B;AACF;AAUO,SAAS,oBACd,KACA,MACA,MACA,MACM;AACN,SAAO,iBAAiB,GAAG;AAC3B,OAAK,GAAG;AACV;","names":["process"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/security.ts","../src/sessions.ts"],"sourcesContent":["import * as os from 'node:os';\nimport * as process from 'node:process';\n\nimport { RateLimiter, validateEndpoint, validateToken } from './security';\nimport {\n endSession as _endSession,\n flipFromEvent,\n getCurrentSessionId,\n startSession as _startSession,\n} from './sessions';\n\nexport type Level = 'fatal' | 'error' | 'warning' | 'info';\nexport type MechanismType =\n | 'uncaughtException'\n | 'unhandledRejection'\n | 'manual';\n\nexport interface Mechanism {\n type: MechanismType;\n handled: boolean;\n}\n\nexport interface PionneEvent {\n exception_type: string;\n message?: string | null;\n stack?: string[];\n level?: Level;\n\n release?: string;\n environment?: string;\n app_version?: string;\n os_name?: string;\n os_version?: string;\n user_id_anon?: string;\n locale?: string;\n timezone?: string;\n\n contexts?: Record<string, Record<string, unknown> | undefined>;\n mechanism?: Mechanism;\n tags?: Record<string, string>;\n}\n\nexport interface PionneOptions {\n /** Project token (starts with `pio_live_`). Required. */\n token: string;\n endpoint?: string;\n release?: string;\n environment?: string;\n enabled?: boolean;\n captureUncaughtExceptions?: boolean;\n captureUnhandledRejections?: boolean;\n autoContext?: boolean;\n beforeSend?: (event: PionneEvent) => PionneEvent | null;\n userIdAnon?: string;\n tags?: Record<string, string>;\n maxStackFrames?: number;\n /**\n * Release Health — opens a session at init() with status='ok', flips to\n * 'crashed'/'errored' if a fatal/error fires through the global handlers.\n * The dashboard derives crash-free user rate per release. Default: true.\n */\n releaseHealth?: boolean;\n /** Token-bucket rate limit (events/sec). Default 10, set 0 to disable. */\n maxEventsPerSecond?: number;\n /**\n * Opt-in: resolve approximate server geography (city, region, country) once\n * at startup via a free IP→location lookup, and attach it to every event\n * under `contexts.geo`. Off by default for privacy.\n */\n sendGeography?: boolean;\n /**\n * Override the IP→geography endpoint. Must return JSON with at least\n * `city`, `region`, `country` (or `country_name`), and `country_code`\n * fields. Default: `https://ipapi.co/json/`.\n */\n geographyEndpoint?: string;\n}\n\nconst DEFAULT_ENDPOINT = 'https://pionne.agkgcreations.fr/api/ingest';\nconst DEFAULT_GEO_ENDPOINT = 'https://ipapi.co/json/';\nconst DEFAULT_MAX_STACK = 50;\nconst SDK_NAME = 'pionne.node';\nconst SDK_VERSION = '0.1.0';\n\ntype ResolvedConfig = Required<\n Omit<PionneOptions, 'beforeSend' | 'userIdAnon' | 'tags' | 'release' | 'releaseHealth' | 'maxEventsPerSecond'>\n> & {\n beforeSend?: PionneOptions['beforeSend'];\n userIdAnon?: string;\n tags?: Record<string, string>;\n release?: string;\n};\n\nlet config: ResolvedConfig | null = null;\nlet rateLimiter: RateLimiter | null = null;\nlet droppedByRateLimit = 0;\nlet staticContext: Partial<PionneEvent> = {};\nlet onUncaught: ((err: Error) => void) | null = null;\nlet onRejection: ((reason: unknown) => void) | null = null;\n\nfunction gatherStaticContext(): Partial<PionneEvent> {\n let timezone: string | undefined;\n try {\n timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;\n } catch {\n // ignore\n }\n return {\n os_name: os.type(),\n os_version: os.release(),\n timezone,\n contexts: {\n sdk: { name: SDK_NAME, version: SDK_VERSION },\n runtime: {\n name: 'node',\n version: process.versions.node,\n v8: process.versions.v8,\n },\n os: {\n name: os.type(),\n version: os.release(),\n platform: process.platform,\n arch: process.arch,\n cpu_count: os.cpus().length,\n total_memory: os.totalmem(),\n free_memory: os.freemem(),\n },\n app: {\n hostname: os.hostname(),\n pid: process.pid,\n cwd: process.cwd(),\n },\n },\n };\n}\n\nfunction parseStack(error: Error, max: number): string[] {\n if (!error.stack) return [];\n return error.stack\n .split('\\n')\n .slice(0, max)\n .map((s) => s.trim())\n .filter(Boolean);\n}\n\nfunction buildEvent(\n err: unknown,\n level: Level,\n mechanism: MechanismType,\n handled: boolean,\n extra?: Partial<PionneEvent>,\n): PionneEvent | null {\n if (!config || !config.enabled) return null;\n const e = err instanceof Error ? err : new Error(String(err));\n const event: PionneEvent = {\n ...staticContext,\n exception_type: e.name || 'Error',\n message: e.message || null,\n stack: parseStack(e, config.maxStackFrames),\n level,\n release: config.release,\n environment: config.environment,\n user_id_anon: config.userIdAnon,\n tags: config.tags,\n mechanism: { type: mechanism, handled },\n ...extra,\n };\n if (config.beforeSend) {\n const result = config.beforeSend(event);\n if (!result) return null;\n return result;\n }\n return event;\n}\n\nasync function send(event: PionneEvent): Promise<void> {\n if (rateLimiter && !rateLimiter.allow()) {\n droppedByRateLimit++;\n if (process.env.NODE_ENV !== 'production' && droppedByRateLimit % 50 === 1) {\n console.warn(`[Pionne] rate-limit reached (${droppedByRateLimit} events dropped). Bump maxEventsPerSecond if intentional.`);\n }\n return;\n }\n\n if (!config) return;\n try {\n // Node >=18 has global `fetch`. We rely on it instead of pulling in\n // node-fetch / undici as a dependency.\n if (typeof fetch !== 'function') return;\n await fetch(config.endpoint, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Pionne-Token': config.token,\n },\n body: JSON.stringify(event),\n });\n } catch {\n // Best-effort: a monitoring SDK must never crash the host process.\n }\n}\n\n/**\n * Fire-and-forget IP→geo lookup. Mutates `staticContext.contexts.geo` once it\n * resolves so subsequent events carry the location. Failures are silent — a\n * monitoring SDK must never crash or stall the host process.\n */\nfunction fetchGeography(endpoint: string): void {\n if (typeof fetch !== 'function') return;\n const controller =\n typeof AbortController !== 'undefined' ? new AbortController() : null;\n const timeout = controller\n ? setTimeout(() => controller.abort(), 4000)\n : null;\n fetch(endpoint, {\n method: 'GET',\n headers: { Accept: 'application/json' },\n signal: controller?.signal,\n })\n .then((res) => (res.ok ? res.json() : null))\n .then((data: unknown) => {\n if (!data || typeof data !== 'object') return;\n const d = data as Record<string, unknown>;\n const geo: Record<string, string> = {};\n if (typeof d.city === 'string') geo.city = d.city;\n if (typeof d.region === 'string') geo.region = d.region;\n if (typeof d.country_name === 'string') geo.country = d.country_name;\n else if (typeof d.country === 'string') geo.country = d.country;\n if (typeof d.country_code === 'string') geo.country_code = d.country_code;\n if (Object.keys(geo).length === 0) return;\n const ctx = staticContext.contexts ?? {};\n staticContext.contexts = { ...ctx, geo };\n })\n .catch(() => {\n // Best-effort: silently ignore lookup failures.\n })\n .finally(() => {\n if (timeout) clearTimeout(timeout);\n });\n}\n\nfunction installUncaughtHandler(): void {\n onUncaught = (err: Error) => {\n const event = buildEvent(err, 'fatal', 'uncaughtException', false);\n if (event) {\n // Fire-and-forget: process is going to die anyway. Best we can do is\n // try to flush before exit, but Node will tear down imminently.\n void send(event);\n flipFromEvent(event.level, event.mechanism?.type ?? 'uncaughtException');\n }\n };\n process.on('uncaughtException', onUncaught);\n}\n\nfunction installRejectionHandler(): void {\n onRejection = (reason: unknown) => {\n const err = reason instanceof Error ? reason : new Error(String(reason));\n const event = buildEvent(err, 'error', 'unhandledRejection', false);\n if (event) {\n void send(event);\n flipFromEvent(event.level, event.mechanism?.type ?? 'unhandledRejection');\n }\n };\n process.on('unhandledRejection', onRejection);\n}\n\nexport const Pionne = {\n init(options: PionneOptions): void {\n try {\n const isDev = process.env.NODE_ENV !== 'production';\n if (!options?.token || !validateToken(options.token)) {\n if (isDev) {\n console.warn('[Pionne] Missing or invalid token (expected pio_live_<≥16 chars>, no placeholders).');\n }\n return;\n }\n const endpoint = options.endpoint ?? DEFAULT_ENDPOINT;\n if (!validateEndpoint(endpoint, isDev)) {\n console.warn('[Pionne] Refusing non-HTTPS endpoint in production:', endpoint);\n return;\n }\n const rps = options.maxEventsPerSecond ?? 10;\n rateLimiter = rps > 0 ? new RateLimiter(rps, rps) : null;\n\n const autoContext = options.autoContext ?? true;\n staticContext = autoContext ? gatherStaticContext() : {};\n\n config = {\n token: options.token,\n endpoint: options.endpoint ?? DEFAULT_ENDPOINT,\n release: options.release,\n environment:\n options.environment ?? process.env.NODE_ENV ?? 'production',\n enabled: options.enabled ?? true,\n captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,\n captureUnhandledRejections: options.captureUnhandledRejections ?? true,\n autoContext,\n beforeSend: options.beforeSend,\n userIdAnon: options.userIdAnon,\n tags: options.tags,\n maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK,\n sendGeography: options.sendGeography ?? false,\n geographyEndpoint: options.geographyEndpoint ?? DEFAULT_GEO_ENDPOINT,\n };\n\n if (config.captureUncaughtExceptions) installUncaughtHandler();\n if (config.captureUnhandledRejections) installRejectionHandler();\n if (config.sendGeography) fetchGeography(config.geographyEndpoint);\n\n // Release Health — open a session unless the host opted out.\n if (options.releaseHealth !== false) {\n _startSession({\n endpoint: config.endpoint,\n token: config.token,\n release: config.release,\n environment: config.environment,\n appVersion: staticContext.app_version,\n osName: staticContext.os_name,\n userIdAnon: config.userIdAnon,\n });\n }\n } catch (e) {\n console.warn('[Pionne] init failed silently — monitoring disabled.', e);\n config = null;\n }\n },\n\n captureException(err: unknown, extra?: Partial<PionneEvent>): void {\n const event = buildEvent(\n err,\n extra?.level ?? 'error',\n 'manual',\n true,\n extra,\n );\n if (event) void send(event);\n },\n\n captureMessage(message: string, extra?: Partial<PionneEvent>): void {\n const event = buildEvent(\n new Error(message),\n extra?.level ?? 'info',\n 'manual',\n true,\n { exception_type: 'Message', ...extra },\n );\n if (event) void send(event);\n },\n\n setUser(userIdAnon: string | null): void {\n if (!config) return;\n config.userIdAnon = userIdAnon ?? undefined;\n },\n\n setTags(tags: Record<string, string> | null): void {\n if (!config) return;\n config.tags = tags ?? undefined;\n },\n\n setEnabled(enabled: boolean): void {\n if (!config) return;\n config.enabled = enabled;\n },\n\n /**\n * Detach all auto handlers. Useful in tests / CLI scripts that need a\n * clean shutdown. Re-init by calling `init()` again.\n */\n uninstall(): void {\n if (onUncaught) process.removeListener('uncaughtException', onUncaught);\n if (onRejection) process.removeListener('unhandledRejection', onRejection);\n onUncaught = null;\n onRejection = null;\n config = null;\n staticContext = {};\n },\n\n // ─── Release Health ───────────────────────────────────────────────────\n\n /** Manually end the current session (status='exited'). */\n endSession(): void {\n _endSession();\n },\n\n /** UUID of the current open session (for diagnostics). */\n getSessionId(): string | null {\n return getCurrentSessionId();\n },\n};\n\n/**\n * Express / Connect / NestJS error middleware. Reports the error then passes\n * it down the chain. Mount it AFTER your routes:\n *\n * import { Pionne, expressErrorHandler } from '@pionne/node';\n * app.use(expressErrorHandler);\n * // your fallback error handler here\n */\nexport function expressErrorHandler(\n err: unknown,\n _req: unknown,\n _res: unknown,\n next: (err?: unknown) => void,\n): void {\n Pionne.captureException(err);\n next(err);\n}\n","// Mirror of @pionne/react-native and @pionne/web security guards.\n// Node-specific tweak: the \"localhost\" rule for non-HTTPS endpoints\n// also accepts hosts ending in `.local` (mDNS) — useful for staging\n// boxes behind Tailscale/ZeroTier.\n\nconst TOKEN_PREFIX = 'pio_live_';\nconst MIN_TOKEN_LENGTH = TOKEN_PREFIX.length + 16;\n\nexport function validateEndpoint(endpoint: string, isDev: boolean): boolean {\n try {\n const u = new URL(endpoint);\n if (u.protocol === 'https:') return true;\n if (u.protocol !== 'http:') return false;\n if (!isDev) return false;\n return /^(localhost|127\\.0\\.0\\.1|0\\.0\\.0\\.0|\\[::1\\]|.*\\.local)$/.test(u.hostname);\n } catch {\n return false;\n }\n}\n\nexport function validateToken(token: string): boolean {\n if (typeof token !== 'string') return false;\n if (!token.startsWith(TOKEN_PREFIX)) return false;\n if (token.length < MIN_TOKEN_LENGTH) return false;\n const lower = token.toLowerCase();\n for (const bad of ['xxx', 'yyy', 'todo', 'fixme', 'replace', 'changeme']) {\n if (lower.includes(bad)) return false;\n }\n return true;\n}\n\nexport class RateLimiter {\n private tokens: number;\n private lastRefill: number;\n constructor(\n private capacity: number,\n private refillPerSecond: number,\n ) {\n this.tokens = capacity;\n this.lastRefill = Date.now();\n }\n allow(): boolean {\n if (this.refillPerSecond <= 0) return true;\n const now = Date.now();\n const elapsedMs = now - this.lastRefill;\n if (elapsedMs > 0) {\n const refill = (elapsedMs / 1000) * this.refillPerSecond;\n this.tokens = Math.min(this.capacity, this.tokens + refill);\n this.lastRefill = now;\n }\n if (this.tokens >= 1) {\n this.tokens -= 1;\n return true;\n }\n return false;\n }\n}\n","// Release Health for Node.js. Same protocol as the browser/RN SDKs.\n// We auto-flip on uncaughtException/unhandledRejection through the host\n// SDK's existing handlers, and best-effort-emit 'exited' on process.exit.\n\nimport * as crypto from 'node:crypto';\n\nexport type SessionStatus = 'ok' | 'crashed' | 'errored' | 'abnormal' | 'exited';\n\nexport interface SessionContext {\n endpoint: string;\n token: string;\n release?: string;\n environment?: string;\n appVersion?: string;\n osName?: string;\n userIdAnon?: string;\n}\n\ninterface SessionState {\n id: string;\n startedAt: number;\n status: SessionStatus;\n ctx: SessionContext;\n}\n\nlet current: SessionState | null = null;\n\nfunction sessionsUrl(ingestEndpoint: string): string {\n if (ingestEndpoint.endsWith('/ingest')) {\n return ingestEndpoint.slice(0, -'/ingest'.length) + '/sessions';\n }\n return ingestEndpoint.replace(/\\/+$/, '') + '/sessions';\n}\n\nfunction postSession(state: SessionState, status: SessionStatus, durationMs?: number): void {\n const url = sessionsUrl(state.ctx.endpoint);\n const body = {\n session_id: state.id,\n status,\n release: state.ctx.release,\n environment: state.ctx.environment,\n app_version: state.ctx.appVersion,\n os_name: state.ctx.osName,\n user_id_anon: state.ctx.userIdAnon,\n duration_ms: durationMs,\n };\n for (const k of Object.keys(body) as (keyof typeof body)[]) {\n if (body[k] === undefined) delete body[k];\n }\n // Node 18+ has global fetch.\n fetch(url, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Pionne-Token': state.ctx.token,\n },\n body: JSON.stringify(body),\n }).catch(() => undefined);\n}\n\nexport function startSession(ctx: SessionContext): string {\n current = {\n id: crypto.randomUUID(),\n startedAt: Date.now(),\n status: 'ok',\n ctx,\n };\n postSession(current, 'ok');\n\n // Best-effort 'exited' flip on graceful shutdown. We listen on 'exit'\n // (sync only — no async I/O guaranteed) and 'beforeExit' (where async\n // works). The actual session POST is fire-and-forget anyway.\n const onShutdown = () => {\n if (!current || current.status !== 'ok') return;\n flipSession('exited');\n };\n process.once('beforeExit', onShutdown);\n\n return current.id;\n}\n\nexport function flipSession(status: SessionStatus): void {\n if (!current) return;\n const rank: Record<SessionStatus, number> =\n { ok: 0, exited: 1, errored: 2, abnormal: 3, crashed: 4 };\n if (rank[status] <= rank[current.status]) return;\n current.status = status;\n postSession(current, status, Date.now() - current.startedAt);\n}\n\nexport function endSession(status: SessionStatus = 'exited'): void {\n if (!current) return;\n flipSession(status);\n current = null;\n}\n\nexport function getCurrentSessionId(): string | null {\n return current?.id ?? null;\n}\n\nexport function flipFromEvent(\n level: 'fatal' | 'error' | 'warning' | 'info' | undefined,\n mechanismType: string,\n): void {\n if (mechanismType === 'manual') return;\n if (level === 'fatal') flipSession('crashed');\n else if (level === 'error') flipSession('errored');\n}\n"],"mappings":";AAAA,YAAY,QAAQ;AACpB,YAAYA,cAAa;;;ACIzB,IAAM,eAAe;AACrB,IAAM,mBAAmB,aAAa,SAAS;AAExC,SAAS,iBAAiB,UAAkB,OAAyB;AAC1E,MAAI;AACF,UAAM,IAAI,IAAI,IAAI,QAAQ;AAC1B,QAAI,EAAE,aAAa,SAAU,QAAO;AACpC,QAAI,EAAE,aAAa,QAAS,QAAO;AACnC,QAAI,CAAC,MAAO,QAAO;AACnB,WAAO,0DAA0D,KAAK,EAAE,QAAQ;AAAA,EAClF,QAAQ;A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B;AACjB,eAAY;AAAA,EACd;AAAA;AAAA,EAGA,eAA8B;AAC5B,WAAO,oBAAoB;AAAA,EAC7B;AACF;AAUO,SAAS,oBACd,KACA,MACA,MACA,MACM;AACN,SAAO,iBAAiB,GAAG;AAC3B,OAAK,GAAG;AACV;","names":["process"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pionne/node",
3
- "version": "0.3.0",
4
- "description": "Error monitoring SDK for Node.js Pionne. Auto-captures uncaught exceptions and unhandled rejections, ships runtime context (Node, OS, hostname, pid), and tracks Release Health (crash-free user rate per release).",
3
+ "version": "0.3.2",
4
+ "description": "Error monitoring SDK for Node.js \u2014 Pionne. Auto-captures uncaught exceptions and unhandled rejections, ships runtime context (Node, OS, hostname, pid), and tracks Release Health (crash-free user rate per release).",
5
5
  "license": "MIT",
6
6
  "author": "AGKG Creations",
7
7
  "homepage": "https://pionne.agkgcreations.fr",
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as os from 'node:os';
2
2
  import * as process from 'node:process';
3
3
 
4
+ import { RateLimiter, validateEndpoint, validateToken } from './security';
4
5
  import {
5
6
  endSession as _endSession,
6
7
  flipFromEvent,
@@ -59,15 +60,30 @@ export interface PionneOptions {
59
60
  * The dashboard derives crash-free user rate per release. Default: true.
60
61
  */
61
62
  releaseHealth?: boolean;
63
+ /** Token-bucket rate limit (events/sec). Default 10, set 0 to disable. */
64
+ maxEventsPerSecond?: number;
65
+ /**
66
+ * Opt-in: resolve approximate server geography (city, region, country) once
67
+ * at startup via a free IP→location lookup, and attach it to every event
68
+ * under `contexts.geo`. Off by default for privacy.
69
+ */
70
+ sendGeography?: boolean;
71
+ /**
72
+ * Override the IP→geography endpoint. Must return JSON with at least
73
+ * `city`, `region`, `country` (or `country_name`), and `country_code`
74
+ * fields. Default: `https://ipapi.co/json/`.
75
+ */
76
+ geographyEndpoint?: string;
62
77
  }
63
78
 
64
79
  const DEFAULT_ENDPOINT = 'https://pionne.agkgcreations.fr/api/ingest';
80
+ const DEFAULT_GEO_ENDPOINT = 'https://ipapi.co/json/';
65
81
  const DEFAULT_MAX_STACK = 50;
66
82
  const SDK_NAME = 'pionne.node';
67
83
  const SDK_VERSION = '0.1.0';
68
84
 
69
85
  type ResolvedConfig = Required<
70
- Omit<PionneOptions, 'beforeSend' | 'userIdAnon' | 'tags' | 'release' | 'releaseHealth'>
86
+ Omit<PionneOptions, 'beforeSend' | 'userIdAnon' | 'tags' | 'release' | 'releaseHealth' | 'maxEventsPerSecond'>
71
87
  > & {
72
88
  beforeSend?: PionneOptions['beforeSend'];
73
89
  userIdAnon?: string;
@@ -76,6 +92,8 @@ type ResolvedConfig = Required<
76
92
  };
77
93
 
78
94
  let config: ResolvedConfig | null = null;
95
+ let rateLimiter: RateLimiter | null = null;
96
+ let droppedByRateLimit = 0;
79
97
  let staticContext: Partial<PionneEvent> = {};
80
98
  let onUncaught: ((err: Error) => void) | null = null;
81
99
  let onRejection: ((reason: unknown) => void) | null = null;
@@ -156,6 +174,14 @@ function buildEvent(
156
174
  }
157
175
 
158
176
  async function send(event: PionneEvent): Promise<void> {
177
+ if (rateLimiter && !rateLimiter.allow()) {
178
+ droppedByRateLimit++;
179
+ if (process.env.NODE_ENV !== 'production' && droppedByRateLimit % 50 === 1) {
180
+ console.warn(`[Pionne] rate-limit reached (${droppedByRateLimit} events dropped). Bump maxEventsPerSecond if intentional.`);
181
+ }
182
+ return;
183
+ }
184
+
159
185
  if (!config) return;
160
186
  try {
161
187
  // Node >=18 has global `fetch`. We rely on it instead of pulling in
@@ -174,6 +200,45 @@ async function send(event: PionneEvent): Promise<void> {
174
200
  }
175
201
  }
176
202
 
203
+ /**
204
+ * Fire-and-forget IP→geo lookup. Mutates `staticContext.contexts.geo` once it
205
+ * resolves so subsequent events carry the location. Failures are silent — a
206
+ * monitoring SDK must never crash or stall the host process.
207
+ */
208
+ function fetchGeography(endpoint: string): void {
209
+ if (typeof fetch !== 'function') return;
210
+ const controller =
211
+ typeof AbortController !== 'undefined' ? new AbortController() : null;
212
+ const timeout = controller
213
+ ? setTimeout(() => controller.abort(), 4000)
214
+ : null;
215
+ fetch(endpoint, {
216
+ method: 'GET',
217
+ headers: { Accept: 'application/json' },
218
+ signal: controller?.signal,
219
+ })
220
+ .then((res) => (res.ok ? res.json() : null))
221
+ .then((data: unknown) => {
222
+ if (!data || typeof data !== 'object') return;
223
+ const d = data as Record<string, unknown>;
224
+ const geo: Record<string, string> = {};
225
+ if (typeof d.city === 'string') geo.city = d.city;
226
+ if (typeof d.region === 'string') geo.region = d.region;
227
+ if (typeof d.country_name === 'string') geo.country = d.country_name;
228
+ else if (typeof d.country === 'string') geo.country = d.country;
229
+ if (typeof d.country_code === 'string') geo.country_code = d.country_code;
230
+ if (Object.keys(geo).length === 0) return;
231
+ const ctx = staticContext.contexts ?? {};
232
+ staticContext.contexts = { ...ctx, geo };
233
+ })
234
+ .catch(() => {
235
+ // Best-effort: silently ignore lookup failures.
236
+ })
237
+ .finally(() => {
238
+ if (timeout) clearTimeout(timeout);
239
+ });
240
+ }
241
+
177
242
  function installUncaughtHandler(): void {
178
243
  onUncaught = (err: Error) => {
179
244
  const event = buildEvent(err, 'fatal', 'uncaughtException', false);
@@ -201,14 +266,21 @@ function installRejectionHandler(): void {
201
266
 
202
267
  export const Pionne = {
203
268
  init(options: PionneOptions): void {
204
- if (!options?.token || !options.token.startsWith('pio_live_')) {
205
- if (process.env.NODE_ENV !== 'production') {
206
- console.warn(
207
- '[Pionne] Missing or invalid token (must start with pio_live_).',
208
- );
269
+ try {
270
+ const isDev = process.env.NODE_ENV !== 'production';
271
+ if (!options?.token || !validateToken(options.token)) {
272
+ if (isDev) {
273
+ console.warn('[Pionne] Missing or invalid token (expected pio_live_<≥16 chars>, no placeholders).');
274
+ }
275
+ return;
209
276
  }
210
- return;
211
- }
277
+ const endpoint = options.endpoint ?? DEFAULT_ENDPOINT;
278
+ if (!validateEndpoint(endpoint, isDev)) {
279
+ console.warn('[Pionne] Refusing non-HTTPS endpoint in production:', endpoint);
280
+ return;
281
+ }
282
+ const rps = options.maxEventsPerSecond ?? 10;
283
+ rateLimiter = rps > 0 ? new RateLimiter(rps, rps) : null;
212
284
 
213
285
  const autoContext = options.autoContext ?? true;
214
286
  staticContext = autoContext ? gatherStaticContext() : {};
@@ -227,10 +299,13 @@ export const Pionne = {
227
299
  userIdAnon: options.userIdAnon,
228
300
  tags: options.tags,
229
301
  maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK,
302
+ sendGeography: options.sendGeography ?? false,
303
+ geographyEndpoint: options.geographyEndpoint ?? DEFAULT_GEO_ENDPOINT,
230
304
  };
231
305
 
232
306
  if (config.captureUncaughtExceptions) installUncaughtHandler();
233
307
  if (config.captureUnhandledRejections) installRejectionHandler();
308
+ if (config.sendGeography) fetchGeography(config.geographyEndpoint);
234
309
 
235
310
  // Release Health — open a session unless the host opted out.
236
311
  if (options.releaseHealth !== false) {
@@ -244,6 +319,10 @@ export const Pionne = {
244
319
  userIdAnon: config.userIdAnon,
245
320
  });
246
321
  }
322
+ } catch (e) {
323
+ console.warn('[Pionne] init failed silently — monitoring disabled.', e);
324
+ config = null;
325
+ }
247
326
  },
248
327
 
249
328
  captureException(err: unknown, extra?: Partial<PionneEvent>): void {
@@ -0,0 +1,57 @@
1
+ // Mirror of @pionne/react-native and @pionne/web security guards.
2
+ // Node-specific tweak: the "localhost" rule for non-HTTPS endpoints
3
+ // also accepts hosts ending in `.local` (mDNS) — useful for staging
4
+ // boxes behind Tailscale/ZeroTier.
5
+
6
+ const TOKEN_PREFIX = 'pio_live_';
7
+ const MIN_TOKEN_LENGTH = TOKEN_PREFIX.length + 16;
8
+
9
+ export function validateEndpoint(endpoint: string, isDev: boolean): boolean {
10
+ try {
11
+ const u = new URL(endpoint);
12
+ if (u.protocol === 'https:') return true;
13
+ if (u.protocol !== 'http:') return false;
14
+ if (!isDev) return false;
15
+ return /^(localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\]|.*\.local)$/.test(u.hostname);
16
+ } catch {
17
+ return false;
18
+ }
19
+ }
20
+
21
+ export function validateToken(token: string): boolean {
22
+ if (typeof token !== 'string') return false;
23
+ if (!token.startsWith(TOKEN_PREFIX)) return false;
24
+ if (token.length < MIN_TOKEN_LENGTH) return false;
25
+ const lower = token.toLowerCase();
26
+ for (const bad of ['xxx', 'yyy', 'todo', 'fixme', 'replace', 'changeme']) {
27
+ if (lower.includes(bad)) return false;
28
+ }
29
+ return true;
30
+ }
31
+
32
+ export class RateLimiter {
33
+ private tokens: number;
34
+ private lastRefill: number;
35
+ constructor(
36
+ private capacity: number,
37
+ private refillPerSecond: number,
38
+ ) {
39
+ this.tokens = capacity;
40
+ this.lastRefill = Date.now();
41
+ }
42
+ allow(): boolean {
43
+ if (this.refillPerSecond <= 0) return true;
44
+ const now = Date.now();
45
+ const elapsedMs = now - this.lastRefill;
46
+ if (elapsedMs > 0) {
47
+ const refill = (elapsedMs / 1000) * this.refillPerSecond;
48
+ this.tokens = Math.min(this.capacity, this.tokens + refill);
49
+ this.lastRefill = now;
50
+ }
51
+ if (this.tokens >= 1) {
52
+ this.tokens -= 1;
53
+ return true;
54
+ }
55
+ return false;
56
+ }
57
+ }