@pionne/node 0.1.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 +21 -0
- package/README.md +74 -0
- package/dist/index.cjs +223 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +62 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.mjs +187 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +58 -0
- package/src/index.ts +286 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) AGKG Creations
|
|
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,74 @@
|
|
|
1
|
+
# @pionne/node
|
|
2
|
+
|
|
3
|
+
Error monitoring SDK for Node.js — by [Pionne](https://pionne.agkgcreations.fr).
|
|
4
|
+
|
|
5
|
+
Auto-captures `uncaughtException` and `unhandledRejection`, ships rich runtime context (Node version, OS, hostname, pid, memory). Zero external dependencies — uses Node 18+ global `fetch`.
|
|
6
|
+
|
|
7
|
+
Works with **plain Node, Express, Fastify, NestJS, Koa, Hono, Bun, Deno (with compat shim)**.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @pionne/node
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// At the top of your entrypoint (server.ts / index.ts)
|
|
19
|
+
import { Pionne } from '@pionne/node';
|
|
20
|
+
|
|
21
|
+
Pionne.init({
|
|
22
|
+
token: 'pio_live_xxx',
|
|
23
|
+
release: process.env.GIT_SHA, // optional
|
|
24
|
+
environment: process.env.NODE_ENV ?? 'production',
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
That's it. Crashes and unhandled rejections are reported automatically.
|
|
29
|
+
|
|
30
|
+
### Express / NestJS / Connect
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import express from 'express';
|
|
34
|
+
import { Pionne, expressErrorHandler } from '@pionne/node';
|
|
35
|
+
|
|
36
|
+
Pionne.init({ token: 'pio_live_xxx' });
|
|
37
|
+
|
|
38
|
+
const app = express();
|
|
39
|
+
app.get('/boom', () => { throw new Error('boom'); });
|
|
40
|
+
|
|
41
|
+
// Mount AFTER your routes:
|
|
42
|
+
app.use(expressErrorHandler);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Manual capture
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
try {
|
|
49
|
+
await processOrder(orderId);
|
|
50
|
+
} catch (err) {
|
|
51
|
+
Pionne.captureException(err, {
|
|
52
|
+
tags: { feature: 'checkout' },
|
|
53
|
+
user_id_anon: req.user?.id,
|
|
54
|
+
});
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### User identity, tags, opt-out
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
Pionne.setUser('u_42');
|
|
63
|
+
Pionne.setTags({ tier: 'pro' });
|
|
64
|
+
Pionne.setEnabled(false);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Options
|
|
68
|
+
|
|
69
|
+
Same shape as `@pionne/web` and `@pionne/react-native`. See the type
|
|
70
|
+
definitions for the full list.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Pionne: () => Pionne,
|
|
34
|
+
expressErrorHandler: () => expressErrorHandler
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var os = __toESM(require("os"));
|
|
38
|
+
var process = __toESM(require("process"));
|
|
39
|
+
var DEFAULT_ENDPOINT = "https://pionne.agkgcreations.fr/api/ingest";
|
|
40
|
+
var DEFAULT_MAX_STACK = 50;
|
|
41
|
+
var SDK_NAME = "pionne.node";
|
|
42
|
+
var SDK_VERSION = "0.1.0";
|
|
43
|
+
var config = null;
|
|
44
|
+
var staticContext = {};
|
|
45
|
+
var onUncaught = null;
|
|
46
|
+
var onRejection = null;
|
|
47
|
+
function gatherStaticContext() {
|
|
48
|
+
let timezone;
|
|
49
|
+
try {
|
|
50
|
+
timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
51
|
+
} catch {
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
os_name: os.type(),
|
|
55
|
+
os_version: os.release(),
|
|
56
|
+
timezone,
|
|
57
|
+
contexts: {
|
|
58
|
+
sdk: { name: SDK_NAME, version: SDK_VERSION },
|
|
59
|
+
runtime: {
|
|
60
|
+
name: "node",
|
|
61
|
+
version: process.versions.node,
|
|
62
|
+
v8: process.versions.v8
|
|
63
|
+
},
|
|
64
|
+
os: {
|
|
65
|
+
name: os.type(),
|
|
66
|
+
version: os.release(),
|
|
67
|
+
platform: process.platform,
|
|
68
|
+
arch: process.arch,
|
|
69
|
+
cpu_count: os.cpus().length,
|
|
70
|
+
total_memory: os.totalmem(),
|
|
71
|
+
free_memory: os.freemem()
|
|
72
|
+
},
|
|
73
|
+
app: {
|
|
74
|
+
hostname: os.hostname(),
|
|
75
|
+
pid: process.pid,
|
|
76
|
+
cwd: process.cwd()
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function parseStack(error, max) {
|
|
82
|
+
if (!error.stack) return [];
|
|
83
|
+
return error.stack.split("\n").slice(0, max).map((s) => s.trim()).filter(Boolean);
|
|
84
|
+
}
|
|
85
|
+
function buildEvent(err, level, mechanism, handled, extra) {
|
|
86
|
+
if (!config || !config.enabled) return null;
|
|
87
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
88
|
+
const event = {
|
|
89
|
+
...staticContext,
|
|
90
|
+
exception_type: e.name || "Error",
|
|
91
|
+
message: e.message || null,
|
|
92
|
+
stack: parseStack(e, config.maxStackFrames),
|
|
93
|
+
level,
|
|
94
|
+
release: config.release,
|
|
95
|
+
environment: config.environment,
|
|
96
|
+
user_id_anon: config.userIdAnon,
|
|
97
|
+
tags: config.tags,
|
|
98
|
+
mechanism: { type: mechanism, handled },
|
|
99
|
+
...extra
|
|
100
|
+
};
|
|
101
|
+
if (config.beforeSend) {
|
|
102
|
+
const result = config.beforeSend(event);
|
|
103
|
+
if (!result) return null;
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
return event;
|
|
107
|
+
}
|
|
108
|
+
async function send(event) {
|
|
109
|
+
if (!config) return;
|
|
110
|
+
try {
|
|
111
|
+
if (typeof fetch !== "function") return;
|
|
112
|
+
await fetch(config.endpoint, {
|
|
113
|
+
method: "POST",
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
"X-Pionne-Token": config.token
|
|
117
|
+
},
|
|
118
|
+
body: JSON.stringify(event)
|
|
119
|
+
});
|
|
120
|
+
} catch {
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function installUncaughtHandler() {
|
|
124
|
+
onUncaught = (err) => {
|
|
125
|
+
const event = buildEvent(err, "fatal", "uncaughtException", false);
|
|
126
|
+
if (event) {
|
|
127
|
+
void send(event);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
process.on("uncaughtException", onUncaught);
|
|
131
|
+
}
|
|
132
|
+
function installRejectionHandler() {
|
|
133
|
+
onRejection = (reason) => {
|
|
134
|
+
const err = reason instanceof Error ? reason : new Error(String(reason));
|
|
135
|
+
const event = buildEvent(err, "error", "unhandledRejection", false);
|
|
136
|
+
if (event) void send(event);
|
|
137
|
+
};
|
|
138
|
+
process.on("unhandledRejection", onRejection);
|
|
139
|
+
}
|
|
140
|
+
var Pionne = {
|
|
141
|
+
init(options) {
|
|
142
|
+
if (!options?.token || !options.token.startsWith("pio_live_")) {
|
|
143
|
+
if (process.env.NODE_ENV !== "production") {
|
|
144
|
+
console.warn(
|
|
145
|
+
"[Pionne] Missing or invalid token (must start with pio_live_)."
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const autoContext = options.autoContext ?? true;
|
|
151
|
+
staticContext = autoContext ? gatherStaticContext() : {};
|
|
152
|
+
config = {
|
|
153
|
+
token: options.token,
|
|
154
|
+
endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
|
|
155
|
+
release: options.release,
|
|
156
|
+
environment: options.environment ?? process.env.NODE_ENV ?? "production",
|
|
157
|
+
enabled: options.enabled ?? true,
|
|
158
|
+
captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,
|
|
159
|
+
captureUnhandledRejections: options.captureUnhandledRejections ?? true,
|
|
160
|
+
autoContext,
|
|
161
|
+
beforeSend: options.beforeSend,
|
|
162
|
+
userIdAnon: options.userIdAnon,
|
|
163
|
+
tags: options.tags,
|
|
164
|
+
maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK
|
|
165
|
+
};
|
|
166
|
+
if (config.captureUncaughtExceptions) installUncaughtHandler();
|
|
167
|
+
if (config.captureUnhandledRejections) installRejectionHandler();
|
|
168
|
+
},
|
|
169
|
+
captureException(err, extra) {
|
|
170
|
+
const event = buildEvent(
|
|
171
|
+
err,
|
|
172
|
+
extra?.level ?? "error",
|
|
173
|
+
"manual",
|
|
174
|
+
true,
|
|
175
|
+
extra
|
|
176
|
+
);
|
|
177
|
+
if (event) void send(event);
|
|
178
|
+
},
|
|
179
|
+
captureMessage(message, extra) {
|
|
180
|
+
const event = buildEvent(
|
|
181
|
+
new Error(message),
|
|
182
|
+
extra?.level ?? "info",
|
|
183
|
+
"manual",
|
|
184
|
+
true,
|
|
185
|
+
{ exception_type: "Message", ...extra }
|
|
186
|
+
);
|
|
187
|
+
if (event) void send(event);
|
|
188
|
+
},
|
|
189
|
+
setUser(userIdAnon) {
|
|
190
|
+
if (!config) return;
|
|
191
|
+
config.userIdAnon = userIdAnon ?? void 0;
|
|
192
|
+
},
|
|
193
|
+
setTags(tags) {
|
|
194
|
+
if (!config) return;
|
|
195
|
+
config.tags = tags ?? void 0;
|
|
196
|
+
},
|
|
197
|
+
setEnabled(enabled) {
|
|
198
|
+
if (!config) return;
|
|
199
|
+
config.enabled = enabled;
|
|
200
|
+
},
|
|
201
|
+
/**
|
|
202
|
+
* Detach all auto handlers. Useful in tests / CLI scripts that need a
|
|
203
|
+
* clean shutdown. Re-init by calling `init()` again.
|
|
204
|
+
*/
|
|
205
|
+
uninstall() {
|
|
206
|
+
if (onUncaught) process.removeListener("uncaughtException", onUncaught);
|
|
207
|
+
if (onRejection) process.removeListener("unhandledRejection", onRejection);
|
|
208
|
+
onUncaught = null;
|
|
209
|
+
onRejection = null;
|
|
210
|
+
config = null;
|
|
211
|
+
staticContext = {};
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
function expressErrorHandler(err, _req, _res, next) {
|
|
215
|
+
Pionne.captureException(err);
|
|
216
|
+
next(err);
|
|
217
|
+
}
|
|
218
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
219
|
+
0 && (module.exports = {
|
|
220
|
+
Pionne,
|
|
221
|
+
expressErrorHandler
|
|
222
|
+
});
|
|
223
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import * as os from 'node:os';\nimport * as process from 'node:process';\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\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'>\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 }\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) void send(event);\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\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\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,SAAoB;AACpB,cAAyB;AAiDzB,IAAM,mBAAmB;AACzB,IAAM,oBAAoB;AAC1B,IAAM,WAAW;AACjB,IAAM,cAAc;AAWpB,IAAI,SAAgC;AACpC,IAAI,gBAAsC,CAAC;AAC3C,IAAI,aAA4C;AAChD,IAAI,cAAkD;AAEtD,SAAS,sBAA4C;AACnD,MAAI;AACJ,MAAI;AACF,eAAW,KAAK,eAAe,EAAE,gBAAgB,EAAE;AAAA,EACrD,QAAQ;AAAA,EAER;AACA,SAAO;AAAA,IACL,SAAY,QAAK;AAAA,IACjB,YAAe,WAAQ;AAAA,IACvB;AAAA,IACA,UAAU;AAAA,MACR,KAAK,EAAE,MAAM,UAAU,SAAS,YAAY;AAAA,MAC5C,SAAS;AAAA,QACP,MAAM;AAAA,QACN,SAAiB,iBAAS;AAAA,QAC1B,IAAY,iBAAS;AAAA,MACvB;AA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+
type MechanismType = 'uncaughtException' | 'unhandledRejection' | 'manual';
|
|
3
|
+
interface Mechanism {
|
|
4
|
+
type: MechanismType;
|
|
5
|
+
handled: boolean;
|
|
6
|
+
}
|
|
7
|
+
interface PionneEvent {
|
|
8
|
+
exception_type: string;
|
|
9
|
+
message?: string | null;
|
|
10
|
+
stack?: string[];
|
|
11
|
+
level?: Level;
|
|
12
|
+
release?: string;
|
|
13
|
+
environment?: string;
|
|
14
|
+
app_version?: string;
|
|
15
|
+
os_name?: string;
|
|
16
|
+
os_version?: string;
|
|
17
|
+
user_id_anon?: string;
|
|
18
|
+
locale?: string;
|
|
19
|
+
timezone?: string;
|
|
20
|
+
contexts?: Record<string, Record<string, unknown> | undefined>;
|
|
21
|
+
mechanism?: Mechanism;
|
|
22
|
+
tags?: Record<string, string>;
|
|
23
|
+
}
|
|
24
|
+
interface PionneOptions {
|
|
25
|
+
/** Project token (starts with `pio_live_`). Required. */
|
|
26
|
+
token: string;
|
|
27
|
+
endpoint?: string;
|
|
28
|
+
release?: string;
|
|
29
|
+
environment?: string;
|
|
30
|
+
enabled?: boolean;
|
|
31
|
+
captureUncaughtExceptions?: boolean;
|
|
32
|
+
captureUnhandledRejections?: boolean;
|
|
33
|
+
autoContext?: boolean;
|
|
34
|
+
beforeSend?: (event: PionneEvent) => PionneEvent | null;
|
|
35
|
+
userIdAnon?: string;
|
|
36
|
+
tags?: Record<string, string>;
|
|
37
|
+
maxStackFrames?: number;
|
|
38
|
+
}
|
|
39
|
+
declare const Pionne: {
|
|
40
|
+
init(options: PionneOptions): void;
|
|
41
|
+
captureException(err: unknown, extra?: Partial<PionneEvent>): void;
|
|
42
|
+
captureMessage(message: string, extra?: Partial<PionneEvent>): void;
|
|
43
|
+
setUser(userIdAnon: string | null): void;
|
|
44
|
+
setTags(tags: Record<string, string> | null): void;
|
|
45
|
+
setEnabled(enabled: boolean): void;
|
|
46
|
+
/**
|
|
47
|
+
* Detach all auto handlers. Useful in tests / CLI scripts that need a
|
|
48
|
+
* clean shutdown. Re-init by calling `init()` again.
|
|
49
|
+
*/
|
|
50
|
+
uninstall(): void;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Express / Connect / NestJS error middleware. Reports the error then passes
|
|
54
|
+
* it down the chain. Mount it AFTER your routes:
|
|
55
|
+
*
|
|
56
|
+
* import { Pionne, expressErrorHandler } from '@pionne/node';
|
|
57
|
+
* app.use(expressErrorHandler);
|
|
58
|
+
* // your fallback error handler here
|
|
59
|
+
*/
|
|
60
|
+
declare function expressErrorHandler(err: unknown, _req: unknown, _res: unknown, next: (err?: unknown) => void): void;
|
|
61
|
+
|
|
62
|
+
export { type Level, type Mechanism, type MechanismType, Pionne, type PionneEvent, type PionneOptions, expressErrorHandler };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
type Level = 'fatal' | 'error' | 'warning' | 'info';
|
|
2
|
+
type MechanismType = 'uncaughtException' | 'unhandledRejection' | 'manual';
|
|
3
|
+
interface Mechanism {
|
|
4
|
+
type: MechanismType;
|
|
5
|
+
handled: boolean;
|
|
6
|
+
}
|
|
7
|
+
interface PionneEvent {
|
|
8
|
+
exception_type: string;
|
|
9
|
+
message?: string | null;
|
|
10
|
+
stack?: string[];
|
|
11
|
+
level?: Level;
|
|
12
|
+
release?: string;
|
|
13
|
+
environment?: string;
|
|
14
|
+
app_version?: string;
|
|
15
|
+
os_name?: string;
|
|
16
|
+
os_version?: string;
|
|
17
|
+
user_id_anon?: string;
|
|
18
|
+
locale?: string;
|
|
19
|
+
timezone?: string;
|
|
20
|
+
contexts?: Record<string, Record<string, unknown> | undefined>;
|
|
21
|
+
mechanism?: Mechanism;
|
|
22
|
+
tags?: Record<string, string>;
|
|
23
|
+
}
|
|
24
|
+
interface PionneOptions {
|
|
25
|
+
/** Project token (starts with `pio_live_`). Required. */
|
|
26
|
+
token: string;
|
|
27
|
+
endpoint?: string;
|
|
28
|
+
release?: string;
|
|
29
|
+
environment?: string;
|
|
30
|
+
enabled?: boolean;
|
|
31
|
+
captureUncaughtExceptions?: boolean;
|
|
32
|
+
captureUnhandledRejections?: boolean;
|
|
33
|
+
autoContext?: boolean;
|
|
34
|
+
beforeSend?: (event: PionneEvent) => PionneEvent | null;
|
|
35
|
+
userIdAnon?: string;
|
|
36
|
+
tags?: Record<string, string>;
|
|
37
|
+
maxStackFrames?: number;
|
|
38
|
+
}
|
|
39
|
+
declare const Pionne: {
|
|
40
|
+
init(options: PionneOptions): void;
|
|
41
|
+
captureException(err: unknown, extra?: Partial<PionneEvent>): void;
|
|
42
|
+
captureMessage(message: string, extra?: Partial<PionneEvent>): void;
|
|
43
|
+
setUser(userIdAnon: string | null): void;
|
|
44
|
+
setTags(tags: Record<string, string> | null): void;
|
|
45
|
+
setEnabled(enabled: boolean): void;
|
|
46
|
+
/**
|
|
47
|
+
* Detach all auto handlers. Useful in tests / CLI scripts that need a
|
|
48
|
+
* clean shutdown. Re-init by calling `init()` again.
|
|
49
|
+
*/
|
|
50
|
+
uninstall(): void;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Express / Connect / NestJS error middleware. Reports the error then passes
|
|
54
|
+
* it down the chain. Mount it AFTER your routes:
|
|
55
|
+
*
|
|
56
|
+
* import { Pionne, expressErrorHandler } from '@pionne/node';
|
|
57
|
+
* app.use(expressErrorHandler);
|
|
58
|
+
* // your fallback error handler here
|
|
59
|
+
*/
|
|
60
|
+
declare function expressErrorHandler(err: unknown, _req: unknown, _res: unknown, next: (err?: unknown) => void): void;
|
|
61
|
+
|
|
62
|
+
export { type Level, type Mechanism, type MechanismType, Pionne, type PionneEvent, type PionneOptions, expressErrorHandler };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import * as os from "os";
|
|
3
|
+
import * as process from "process";
|
|
4
|
+
var DEFAULT_ENDPOINT = "https://pionne.agkgcreations.fr/api/ingest";
|
|
5
|
+
var DEFAULT_MAX_STACK = 50;
|
|
6
|
+
var SDK_NAME = "pionne.node";
|
|
7
|
+
var SDK_VERSION = "0.1.0";
|
|
8
|
+
var config = null;
|
|
9
|
+
var staticContext = {};
|
|
10
|
+
var onUncaught = null;
|
|
11
|
+
var onRejection = null;
|
|
12
|
+
function gatherStaticContext() {
|
|
13
|
+
let timezone;
|
|
14
|
+
try {
|
|
15
|
+
timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
16
|
+
} catch {
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
os_name: os.type(),
|
|
20
|
+
os_version: os.release(),
|
|
21
|
+
timezone,
|
|
22
|
+
contexts: {
|
|
23
|
+
sdk: { name: SDK_NAME, version: SDK_VERSION },
|
|
24
|
+
runtime: {
|
|
25
|
+
name: "node",
|
|
26
|
+
version: process.versions.node,
|
|
27
|
+
v8: process.versions.v8
|
|
28
|
+
},
|
|
29
|
+
os: {
|
|
30
|
+
name: os.type(),
|
|
31
|
+
version: os.release(),
|
|
32
|
+
platform: process.platform,
|
|
33
|
+
arch: process.arch,
|
|
34
|
+
cpu_count: os.cpus().length,
|
|
35
|
+
total_memory: os.totalmem(),
|
|
36
|
+
free_memory: os.freemem()
|
|
37
|
+
},
|
|
38
|
+
app: {
|
|
39
|
+
hostname: os.hostname(),
|
|
40
|
+
pid: process.pid,
|
|
41
|
+
cwd: process.cwd()
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function parseStack(error, max) {
|
|
47
|
+
if (!error.stack) return [];
|
|
48
|
+
return error.stack.split("\n").slice(0, max).map((s) => s.trim()).filter(Boolean);
|
|
49
|
+
}
|
|
50
|
+
function buildEvent(err, level, mechanism, handled, extra) {
|
|
51
|
+
if (!config || !config.enabled) return null;
|
|
52
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
53
|
+
const event = {
|
|
54
|
+
...staticContext,
|
|
55
|
+
exception_type: e.name || "Error",
|
|
56
|
+
message: e.message || null,
|
|
57
|
+
stack: parseStack(e, config.maxStackFrames),
|
|
58
|
+
level,
|
|
59
|
+
release: config.release,
|
|
60
|
+
environment: config.environment,
|
|
61
|
+
user_id_anon: config.userIdAnon,
|
|
62
|
+
tags: config.tags,
|
|
63
|
+
mechanism: { type: mechanism, handled },
|
|
64
|
+
...extra
|
|
65
|
+
};
|
|
66
|
+
if (config.beforeSend) {
|
|
67
|
+
const result = config.beforeSend(event);
|
|
68
|
+
if (!result) return null;
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
return event;
|
|
72
|
+
}
|
|
73
|
+
async function send(event) {
|
|
74
|
+
if (!config) return;
|
|
75
|
+
try {
|
|
76
|
+
if (typeof fetch !== "function") return;
|
|
77
|
+
await fetch(config.endpoint, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers: {
|
|
80
|
+
"Content-Type": "application/json",
|
|
81
|
+
"X-Pionne-Token": config.token
|
|
82
|
+
},
|
|
83
|
+
body: JSON.stringify(event)
|
|
84
|
+
});
|
|
85
|
+
} catch {
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function installUncaughtHandler() {
|
|
89
|
+
onUncaught = (err) => {
|
|
90
|
+
const event = buildEvent(err, "fatal", "uncaughtException", false);
|
|
91
|
+
if (event) {
|
|
92
|
+
void send(event);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
process.on("uncaughtException", onUncaught);
|
|
96
|
+
}
|
|
97
|
+
function installRejectionHandler() {
|
|
98
|
+
onRejection = (reason) => {
|
|
99
|
+
const err = reason instanceof Error ? reason : new Error(String(reason));
|
|
100
|
+
const event = buildEvent(err, "error", "unhandledRejection", false);
|
|
101
|
+
if (event) void send(event);
|
|
102
|
+
};
|
|
103
|
+
process.on("unhandledRejection", onRejection);
|
|
104
|
+
}
|
|
105
|
+
var Pionne = {
|
|
106
|
+
init(options) {
|
|
107
|
+
if (!options?.token || !options.token.startsWith("pio_live_")) {
|
|
108
|
+
if (process.env.NODE_ENV !== "production") {
|
|
109
|
+
console.warn(
|
|
110
|
+
"[Pionne] Missing or invalid token (must start with pio_live_)."
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const autoContext = options.autoContext ?? true;
|
|
116
|
+
staticContext = autoContext ? gatherStaticContext() : {};
|
|
117
|
+
config = {
|
|
118
|
+
token: options.token,
|
|
119
|
+
endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
|
|
120
|
+
release: options.release,
|
|
121
|
+
environment: options.environment ?? process.env.NODE_ENV ?? "production",
|
|
122
|
+
enabled: options.enabled ?? true,
|
|
123
|
+
captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,
|
|
124
|
+
captureUnhandledRejections: options.captureUnhandledRejections ?? true,
|
|
125
|
+
autoContext,
|
|
126
|
+
beforeSend: options.beforeSend,
|
|
127
|
+
userIdAnon: options.userIdAnon,
|
|
128
|
+
tags: options.tags,
|
|
129
|
+
maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK
|
|
130
|
+
};
|
|
131
|
+
if (config.captureUncaughtExceptions) installUncaughtHandler();
|
|
132
|
+
if (config.captureUnhandledRejections) installRejectionHandler();
|
|
133
|
+
},
|
|
134
|
+
captureException(err, extra) {
|
|
135
|
+
const event = buildEvent(
|
|
136
|
+
err,
|
|
137
|
+
extra?.level ?? "error",
|
|
138
|
+
"manual",
|
|
139
|
+
true,
|
|
140
|
+
extra
|
|
141
|
+
);
|
|
142
|
+
if (event) void send(event);
|
|
143
|
+
},
|
|
144
|
+
captureMessage(message, extra) {
|
|
145
|
+
const event = buildEvent(
|
|
146
|
+
new Error(message),
|
|
147
|
+
extra?.level ?? "info",
|
|
148
|
+
"manual",
|
|
149
|
+
true,
|
|
150
|
+
{ exception_type: "Message", ...extra }
|
|
151
|
+
);
|
|
152
|
+
if (event) void send(event);
|
|
153
|
+
},
|
|
154
|
+
setUser(userIdAnon) {
|
|
155
|
+
if (!config) return;
|
|
156
|
+
config.userIdAnon = userIdAnon ?? void 0;
|
|
157
|
+
},
|
|
158
|
+
setTags(tags) {
|
|
159
|
+
if (!config) return;
|
|
160
|
+
config.tags = tags ?? void 0;
|
|
161
|
+
},
|
|
162
|
+
setEnabled(enabled) {
|
|
163
|
+
if (!config) return;
|
|
164
|
+
config.enabled = enabled;
|
|
165
|
+
},
|
|
166
|
+
/**
|
|
167
|
+
* Detach all auto handlers. Useful in tests / CLI scripts that need a
|
|
168
|
+
* clean shutdown. Re-init by calling `init()` again.
|
|
169
|
+
*/
|
|
170
|
+
uninstall() {
|
|
171
|
+
if (onUncaught) process.removeListener("uncaughtException", onUncaught);
|
|
172
|
+
if (onRejection) process.removeListener("unhandledRejection", onRejection);
|
|
173
|
+
onUncaught = null;
|
|
174
|
+
onRejection = null;
|
|
175
|
+
config = null;
|
|
176
|
+
staticContext = {};
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
function expressErrorHandler(err, _req, _res, next) {
|
|
180
|
+
Pionne.captureException(err);
|
|
181
|
+
next(err);
|
|
182
|
+
}
|
|
183
|
+
export {
|
|
184
|
+
Pionne,
|
|
185
|
+
expressErrorHandler
|
|
186
|
+
};
|
|
187
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import * as os from 'node:os';\nimport * as process from 'node:process';\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\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'>\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 }\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) void send(event);\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\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\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"],"mappings":";AAAA,YAAY,QAAQ;AACpB,YAAY,aAAa;AAiDzB,IAAM,mBAAmB;AACzB,IAAM,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xB;AAAA,EAEA,WAAW,SAAwB;AACjC,QAAI,CAAC,OAAQ;AACb,WAAO,UAAU;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAkB;AAChB,QAAI,WAAY,CAAQ,uBAAe,qBAAqB,UAAU;AACtE,QAAI,YAAa,CAAQ,uBAAe,sBAAsB,WAAW;AACzE,iBAAa;AACb,kBAAc;AACd,aAAS;AACT,oBAAgB,CAAC;AAAA,EACnB;AACF;AAUO,SAAS,oBACd,KACA,MACA,MACA,MACM;AACN,SAAO,iBAAiB,GAAG;AAC3B,OAAK,GAAG;AACV;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pionne/node",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Error monitoring SDK for Node.js — Pionne. Auto-captures uncaught exceptions and unhandled rejections, ships rich runtime context (Node version, OS, hostname, pid).",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "AGKG Creations",
|
|
7
|
+
"homepage": "https://pionne.agkgcreations.fr",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/agkgcreations/pionne.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"pionne",
|
|
14
|
+
"node",
|
|
15
|
+
"nodejs",
|
|
16
|
+
"express",
|
|
17
|
+
"fastify",
|
|
18
|
+
"nestjs",
|
|
19
|
+
"error-monitoring",
|
|
20
|
+
"crash-reporting",
|
|
21
|
+
"sentry-alternative",
|
|
22
|
+
"observability"
|
|
23
|
+
],
|
|
24
|
+
"main": "dist/index.cjs",
|
|
25
|
+
"module": "dist/index.mjs",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"source": "src/index.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.mjs",
|
|
32
|
+
"require": "./dist/index.cjs"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"src",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"prepublishOnly": "npm run typecheck && npm run build"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^20.0.0",
|
|
49
|
+
"tsup": "^8.3.0",
|
|
50
|
+
"typescript": "^5.5.0"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=18"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import * as os from 'node:os';
|
|
2
|
+
import * as process from 'node:process';
|
|
3
|
+
|
|
4
|
+
export type Level = 'fatal' | 'error' | 'warning' | 'info';
|
|
5
|
+
export type MechanismType =
|
|
6
|
+
| 'uncaughtException'
|
|
7
|
+
| 'unhandledRejection'
|
|
8
|
+
| 'manual';
|
|
9
|
+
|
|
10
|
+
export interface Mechanism {
|
|
11
|
+
type: MechanismType;
|
|
12
|
+
handled: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface PionneEvent {
|
|
16
|
+
exception_type: string;
|
|
17
|
+
message?: string | null;
|
|
18
|
+
stack?: string[];
|
|
19
|
+
level?: Level;
|
|
20
|
+
|
|
21
|
+
release?: string;
|
|
22
|
+
environment?: string;
|
|
23
|
+
app_version?: string;
|
|
24
|
+
os_name?: string;
|
|
25
|
+
os_version?: string;
|
|
26
|
+
user_id_anon?: string;
|
|
27
|
+
locale?: string;
|
|
28
|
+
timezone?: string;
|
|
29
|
+
|
|
30
|
+
contexts?: Record<string, Record<string, unknown> | undefined>;
|
|
31
|
+
mechanism?: Mechanism;
|
|
32
|
+
tags?: Record<string, string>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PionneOptions {
|
|
36
|
+
/** Project token (starts with `pio_live_`). Required. */
|
|
37
|
+
token: string;
|
|
38
|
+
endpoint?: string;
|
|
39
|
+
release?: string;
|
|
40
|
+
environment?: string;
|
|
41
|
+
enabled?: boolean;
|
|
42
|
+
captureUncaughtExceptions?: boolean;
|
|
43
|
+
captureUnhandledRejections?: boolean;
|
|
44
|
+
autoContext?: boolean;
|
|
45
|
+
beforeSend?: (event: PionneEvent) => PionneEvent | null;
|
|
46
|
+
userIdAnon?: string;
|
|
47
|
+
tags?: Record<string, string>;
|
|
48
|
+
maxStackFrames?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const DEFAULT_ENDPOINT = 'https://pionne.agkgcreations.fr/api/ingest';
|
|
52
|
+
const DEFAULT_MAX_STACK = 50;
|
|
53
|
+
const SDK_NAME = 'pionne.node';
|
|
54
|
+
const SDK_VERSION = '0.1.0';
|
|
55
|
+
|
|
56
|
+
type ResolvedConfig = Required<
|
|
57
|
+
Omit<PionneOptions, 'beforeSend' | 'userIdAnon' | 'tags' | 'release'>
|
|
58
|
+
> & {
|
|
59
|
+
beforeSend?: PionneOptions['beforeSend'];
|
|
60
|
+
userIdAnon?: string;
|
|
61
|
+
tags?: Record<string, string>;
|
|
62
|
+
release?: string;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
let config: ResolvedConfig | null = null;
|
|
66
|
+
let staticContext: Partial<PionneEvent> = {};
|
|
67
|
+
let onUncaught: ((err: Error) => void) | null = null;
|
|
68
|
+
let onRejection: ((reason: unknown) => void) | null = null;
|
|
69
|
+
|
|
70
|
+
function gatherStaticContext(): Partial<PionneEvent> {
|
|
71
|
+
let timezone: string | undefined;
|
|
72
|
+
try {
|
|
73
|
+
timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
74
|
+
} catch {
|
|
75
|
+
// ignore
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
os_name: os.type(),
|
|
79
|
+
os_version: os.release(),
|
|
80
|
+
timezone,
|
|
81
|
+
contexts: {
|
|
82
|
+
sdk: { name: SDK_NAME, version: SDK_VERSION },
|
|
83
|
+
runtime: {
|
|
84
|
+
name: 'node',
|
|
85
|
+
version: process.versions.node,
|
|
86
|
+
v8: process.versions.v8,
|
|
87
|
+
},
|
|
88
|
+
os: {
|
|
89
|
+
name: os.type(),
|
|
90
|
+
version: os.release(),
|
|
91
|
+
platform: process.platform,
|
|
92
|
+
arch: process.arch,
|
|
93
|
+
cpu_count: os.cpus().length,
|
|
94
|
+
total_memory: os.totalmem(),
|
|
95
|
+
free_memory: os.freemem(),
|
|
96
|
+
},
|
|
97
|
+
app: {
|
|
98
|
+
hostname: os.hostname(),
|
|
99
|
+
pid: process.pid,
|
|
100
|
+
cwd: process.cwd(),
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function parseStack(error: Error, max: number): string[] {
|
|
107
|
+
if (!error.stack) return [];
|
|
108
|
+
return error.stack
|
|
109
|
+
.split('\n')
|
|
110
|
+
.slice(0, max)
|
|
111
|
+
.map((s) => s.trim())
|
|
112
|
+
.filter(Boolean);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function buildEvent(
|
|
116
|
+
err: unknown,
|
|
117
|
+
level: Level,
|
|
118
|
+
mechanism: MechanismType,
|
|
119
|
+
handled: boolean,
|
|
120
|
+
extra?: Partial<PionneEvent>,
|
|
121
|
+
): PionneEvent | null {
|
|
122
|
+
if (!config || !config.enabled) return null;
|
|
123
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
124
|
+
const event: PionneEvent = {
|
|
125
|
+
...staticContext,
|
|
126
|
+
exception_type: e.name || 'Error',
|
|
127
|
+
message: e.message || null,
|
|
128
|
+
stack: parseStack(e, config.maxStackFrames),
|
|
129
|
+
level,
|
|
130
|
+
release: config.release,
|
|
131
|
+
environment: config.environment,
|
|
132
|
+
user_id_anon: config.userIdAnon,
|
|
133
|
+
tags: config.tags,
|
|
134
|
+
mechanism: { type: mechanism, handled },
|
|
135
|
+
...extra,
|
|
136
|
+
};
|
|
137
|
+
if (config.beforeSend) {
|
|
138
|
+
const result = config.beforeSend(event);
|
|
139
|
+
if (!result) return null;
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
return event;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function send(event: PionneEvent): Promise<void> {
|
|
146
|
+
if (!config) return;
|
|
147
|
+
try {
|
|
148
|
+
// Node >=18 has global `fetch`. We rely on it instead of pulling in
|
|
149
|
+
// node-fetch / undici as a dependency.
|
|
150
|
+
if (typeof fetch !== 'function') return;
|
|
151
|
+
await fetch(config.endpoint, {
|
|
152
|
+
method: 'POST',
|
|
153
|
+
headers: {
|
|
154
|
+
'Content-Type': 'application/json',
|
|
155
|
+
'X-Pionne-Token': config.token,
|
|
156
|
+
},
|
|
157
|
+
body: JSON.stringify(event),
|
|
158
|
+
});
|
|
159
|
+
} catch {
|
|
160
|
+
// Best-effort: a monitoring SDK must never crash the host process.
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function installUncaughtHandler(): void {
|
|
165
|
+
onUncaught = (err: Error) => {
|
|
166
|
+
const event = buildEvent(err, 'fatal', 'uncaughtException', false);
|
|
167
|
+
if (event) {
|
|
168
|
+
// Fire-and-forget: process is going to die anyway. Best we can do is
|
|
169
|
+
// try to flush before exit, but Node will tear down imminently.
|
|
170
|
+
void send(event);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
process.on('uncaughtException', onUncaught);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function installRejectionHandler(): void {
|
|
177
|
+
onRejection = (reason: unknown) => {
|
|
178
|
+
const err = reason instanceof Error ? reason : new Error(String(reason));
|
|
179
|
+
const event = buildEvent(err, 'error', 'unhandledRejection', false);
|
|
180
|
+
if (event) void send(event);
|
|
181
|
+
};
|
|
182
|
+
process.on('unhandledRejection', onRejection);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export const Pionne = {
|
|
186
|
+
init(options: PionneOptions): void {
|
|
187
|
+
if (!options?.token || !options.token.startsWith('pio_live_')) {
|
|
188
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
189
|
+
console.warn(
|
|
190
|
+
'[Pionne] Missing or invalid token (must start with pio_live_).',
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const autoContext = options.autoContext ?? true;
|
|
197
|
+
staticContext = autoContext ? gatherStaticContext() : {};
|
|
198
|
+
|
|
199
|
+
config = {
|
|
200
|
+
token: options.token,
|
|
201
|
+
endpoint: options.endpoint ?? DEFAULT_ENDPOINT,
|
|
202
|
+
release: options.release,
|
|
203
|
+
environment:
|
|
204
|
+
options.environment ?? process.env.NODE_ENV ?? 'production',
|
|
205
|
+
enabled: options.enabled ?? true,
|
|
206
|
+
captureUncaughtExceptions: options.captureUncaughtExceptions ?? true,
|
|
207
|
+
captureUnhandledRejections: options.captureUnhandledRejections ?? true,
|
|
208
|
+
autoContext,
|
|
209
|
+
beforeSend: options.beforeSend,
|
|
210
|
+
userIdAnon: options.userIdAnon,
|
|
211
|
+
tags: options.tags,
|
|
212
|
+
maxStackFrames: options.maxStackFrames ?? DEFAULT_MAX_STACK,
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
if (config.captureUncaughtExceptions) installUncaughtHandler();
|
|
216
|
+
if (config.captureUnhandledRejections) installRejectionHandler();
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
captureException(err: unknown, extra?: Partial<PionneEvent>): void {
|
|
220
|
+
const event = buildEvent(
|
|
221
|
+
err,
|
|
222
|
+
extra?.level ?? 'error',
|
|
223
|
+
'manual',
|
|
224
|
+
true,
|
|
225
|
+
extra,
|
|
226
|
+
);
|
|
227
|
+
if (event) void send(event);
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
captureMessage(message: string, extra?: Partial<PionneEvent>): void {
|
|
231
|
+
const event = buildEvent(
|
|
232
|
+
new Error(message),
|
|
233
|
+
extra?.level ?? 'info',
|
|
234
|
+
'manual',
|
|
235
|
+
true,
|
|
236
|
+
{ exception_type: 'Message', ...extra },
|
|
237
|
+
);
|
|
238
|
+
if (event) void send(event);
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
setUser(userIdAnon: string | null): void {
|
|
242
|
+
if (!config) return;
|
|
243
|
+
config.userIdAnon = userIdAnon ?? undefined;
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
setTags(tags: Record<string, string> | null): void {
|
|
247
|
+
if (!config) return;
|
|
248
|
+
config.tags = tags ?? undefined;
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
setEnabled(enabled: boolean): void {
|
|
252
|
+
if (!config) return;
|
|
253
|
+
config.enabled = enabled;
|
|
254
|
+
},
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Detach all auto handlers. Useful in tests / CLI scripts that need a
|
|
258
|
+
* clean shutdown. Re-init by calling `init()` again.
|
|
259
|
+
*/
|
|
260
|
+
uninstall(): void {
|
|
261
|
+
if (onUncaught) process.removeListener('uncaughtException', onUncaught);
|
|
262
|
+
if (onRejection) process.removeListener('unhandledRejection', onRejection);
|
|
263
|
+
onUncaught = null;
|
|
264
|
+
onRejection = null;
|
|
265
|
+
config = null;
|
|
266
|
+
staticContext = {};
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Express / Connect / NestJS error middleware. Reports the error then passes
|
|
272
|
+
* it down the chain. Mount it AFTER your routes:
|
|
273
|
+
*
|
|
274
|
+
* import { Pionne, expressErrorHandler } from '@pionne/node';
|
|
275
|
+
* app.use(expressErrorHandler);
|
|
276
|
+
* // your fallback error handler here
|
|
277
|
+
*/
|
|
278
|
+
export function expressErrorHandler(
|
|
279
|
+
err: unknown,
|
|
280
|
+
_req: unknown,
|
|
281
|
+
_res: unknown,
|
|
282
|
+
next: (err?: unknown) => void,
|
|
283
|
+
): void {
|
|
284
|
+
Pionne.captureException(err);
|
|
285
|
+
next(err);
|
|
286
|
+
}
|