@chaja/sdk-node 0.1.0 → 0.2.1

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
@@ -16,4 +16,5 @@ app.use(expressErrorHandler());
16
16
 
17
17
  Uncaught exceptions and unhandled rejections are reported with source context. Events that cannot be delivered can be kept on
18
18
  disk (`offlineDirectory`) and resent on the next start. For Hono, Next.js route handlers and other Fetch-API servers, use
19
- `captureRequestError(error, request)`. All options: [docs/sdk.md](https://github.com/emiliodominguez/chaja/blob/main/docs/sdk.md).
19
+ `captureRequestError(error, request)`. A desktop app's main process passes `sessionId` for its run, and events or errors
20
+ recorded while offline keep their time with `timestamp`. All options: [docs/sdk.md](https://github.com/emiliodominguez/chaja/blob/main/docs/sdk.md).
package/dist/index.d.mts CHANGED
@@ -31,6 +31,12 @@ export interface NodeOptions extends ClientOptions$1 {
31
31
  * HTTP client (tests inject one). Defaults to global fetch.
32
32
  */
33
33
  fetch?: typeof fetch;
34
+ /**
35
+ * Session of this process's run, for apps where one run is one session (a desktop app's main process). Events carry it
36
+ * and errors carry it as `session.id`, so release health counts sessions and crashes. Servers acting for many people
37
+ * leave it unset.
38
+ */
39
+ sessionId?: string;
34
40
  }
35
41
  export interface NodeClient extends Client$1 {
36
42
  close(timeoutMs?: number): Promise<boolean>;
@@ -114,6 +120,14 @@ export interface ServerEvent {
114
120
  * Person properties to set only when the person does not have them yet.
115
121
  */
116
122
  setOnce?: Attributes;
123
+ /**
124
+ * When it happened, in epoch milliseconds, if earlier than now.
125
+ */
126
+ timestamp?: number;
127
+ /**
128
+ * The session it belongs to.
129
+ */
130
+ sessionId?: string;
117
131
  }
118
132
  /**
119
133
  * Record a product analytics event for a person with the client from `init`. Servers act for many people, so every event
package/dist/index.mjs CHANGED
@@ -120,6 +120,7 @@ function init(options) {
120
120
  },
121
121
  now: Date.now,
122
122
  random: Math.random,
123
+ ...options.sessionId === void 0 ? {} : { session: runSession(options.sessionId) },
123
124
  schedule(callback, delayMs) {
124
125
  const handle = setTimeout(callback, delayMs);
125
126
  handle.unref();
@@ -202,6 +203,20 @@ function init(options) {
202
203
  return instance;
203
204
  }
204
205
  /**
206
+ * A session that lasts the whole run: `reset` (sign-out) keeps it, since the run goes on.
207
+ *
208
+ * @param id - Session id.
209
+ * @returns The platform's session hooks.
210
+ */
211
+ function runSession(id) {
212
+ return {
213
+ current() {
214
+ return id;
215
+ },
216
+ reset() {}
217
+ };
218
+ }
219
+ /**
205
220
  * Persist undeliverable envelopes as files.
206
221
  *
207
222
  * @param directory - Offline directory.
@@ -226,11 +241,17 @@ function replay(directory, transport) {
226
241
  (async function resend() {
227
242
  for (const name of readdirSync(directory).filter(isEnvelopeFile).toSorted()) {
228
243
  const path = join(directory, name);
229
- const result = await transport.send(readFileSync(path, "utf8"));
244
+ let body;
245
+ try {
246
+ body = readFileSync(path, "utf8");
247
+ } catch {
248
+ continue;
249
+ }
250
+ const result = await transport.send(body);
230
251
  if (result.status === 0 || result.status === 429 || result.status >= 500) return;
231
252
  rmSync(path, { force: true });
232
253
  }
233
- })();
254
+ })().catch(function ignore() {});
234
255
  }
235
256
  /**
236
257
  * Whether a file name looks like a saved envelope.
@@ -334,7 +355,9 @@ function capture(event) {
334
355
  return current?.capture(event.name, event.properties, {
335
356
  distinctId: event.distinctId,
336
357
  ...event.set ? { set: event.set } : {},
337
- ...event.setOnce ? { setOnce: event.setOnce } : {}
358
+ ...event.setOnce ? { setOnce: event.setOnce } : {},
359
+ ...event.timestamp === void 0 ? {} : { timestamp: event.timestamp },
360
+ ...event.sessionId === void 0 ? {} : { sessionId: event.sessionId }
338
361
  });
339
362
  }
340
363
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaja/sdk-node",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Chajá error tracking for Node.js: process handlers, source context, offline buffer, Express and Fetch helpers.",
5
5
  "keywords": [
6
6
  "chaja",
@@ -38,7 +38,7 @@
38
38
  "tsdown": "0.23.0"
39
39
  },
40
40
  "dependencies": {
41
- "@chaja/sdk-core": "0.1.0"
41
+ "@chaja/sdk-core": "0.2.1"
42
42
  },
43
43
  "engines": {
44
44
  "node": ">=22"