@kk-forge/artel-collector 0.4.1 → 0.5.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/README.md +1 -1
- package/dist/clock.d.ts +4 -4
- package/dist/clock.js +4 -2
- package/dist/exports.d.ts +1 -1
- package/dist/outbox.js +15 -13
- package/dist/transport.js +2 -2
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -131,7 +131,7 @@ Each constructor throws a `TypeError` that names the option when the options wou
|
|
|
131
131
|
- `lock`: a function that takes an async operation, runs it while no other context that shares `storage` runs one, and resolves or rejects as the operation does. It must run the operation exactly once and hold the lock until the operation's promise settles, as `navigator.locks.request` does. The collector never asks for it while it holds it. The collector runs each method, and each queue change of a send, under it. Without it, two contexts recording at once may both mark the first event of a day, overwrite each other's queued events or send one envelope twice. The rule that no event goes under another user's id then holds only within one context, and so does the opt-out: an event another context records during `setState('opted_out')` may still leave. Each collector runs one of its own operations at a time with or without a lock.
|
|
132
132
|
- `sessionIdleMinutes`: minutes without events that end a session, 10 by default. A value that is not a finite number from 0 takes the default, and so does one of `sendDelaySeconds`.
|
|
133
133
|
- `sendDelaySeconds`: the longest an event waits in the queue before a send starts, 5 by default.
|
|
134
|
-
- `clock`: time and timers, the host's own by default. Tests pass a fake.
|
|
134
|
+
- `clock`: time and timers, the host's own by default. Tests pass a fake. A `Clock` has `now()`, which returns milliseconds since the epoch, and `setTimeout(callback, delayMs)`, which returns the function that cancels that timer. A cancel after the timer ran does nothing. The port names no timer handle, whose type is a number in the DOM typings and an object in the Node ones. A clock written for 0.4 drops `clearTimeout` and returns the cancel from `setTimeout` instead, and `TimerHandle` is no longer exported.
|
|
135
135
|
- `onError`: receives each failure the collector does not retry. An `onError` that throws is ignored. A report quotes at most 64 characters of host or stored text, and never a page. It receives:
|
|
136
136
|
- an event `track` refuses;
|
|
137
137
|
- an event or envelope dropped by a limit, by its age or by the intake's refusal;
|
package/dist/clock.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export type TimerHandle = ReturnType<typeof setTimeout>;
|
|
2
1
|
/**
|
|
3
2
|
* Gives the time and the timers. The collector and the transport read both through this port, so that a test can move
|
|
4
|
-
* them by hand.
|
|
3
|
+
* them by hand. A timer hands back its own cancel, so the port names no timer handle, whose type differs between
|
|
4
|
+
* hosts.
|
|
5
5
|
*/
|
|
6
6
|
export interface Clock {
|
|
7
7
|
/** Returns milliseconds since the epoch. */
|
|
8
8
|
now(): number;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
/** Runs `callback` after `delayMs` and returns the function that cancels it. A cancel after the run does nothing. */
|
|
10
|
+
setTimeout(callback: () => void, delayMs: number): () => void;
|
|
11
11
|
}
|
|
12
12
|
export declare const systemClock: Clock;
|
package/dist/clock.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export const systemClock = {
|
|
2
2
|
now: () => Date.now(),
|
|
3
|
-
setTimeout: (callback, delayMs) =>
|
|
4
|
-
|
|
3
|
+
setTimeout: (callback, delayMs) => {
|
|
4
|
+
const handle = setTimeout(callback, delayMs);
|
|
5
|
+
return () => clearTimeout(handle);
|
|
6
|
+
},
|
|
5
7
|
};
|
package/dist/exports.d.ts
CHANGED
|
@@ -7,4 +7,4 @@ export { checkOptions, isTestKey } from "./hostInput.js";
|
|
|
7
7
|
export { newMemoryStorage, type StorageAdapter } from "./storage.js";
|
|
8
8
|
export { newHttpTransport, type Transport, type TransportResult } from "./transport.js";
|
|
9
9
|
export type { Lock } from "./lock.js";
|
|
10
|
-
export { systemClock, type Clock
|
|
10
|
+
export { systemClock, type Clock } from "./clock.js";
|
package/dist/outbox.js
CHANGED
|
@@ -25,9 +25,10 @@ export function newOutbox({ key, storage, lock, transport, forgetTransport, iden
|
|
|
25
25
|
const queue = newQueue({ storage, reportError });
|
|
26
26
|
/** Names this outbox's leases, apart from those of other contexts that share the storage. */
|
|
27
27
|
const holder = newRandomId();
|
|
28
|
-
let
|
|
28
|
+
let cancelSendTimer;
|
|
29
29
|
let sendDue = Number.POSITIVE_INFINITY;
|
|
30
|
-
|
|
30
|
+
/** Cancels the timer of the pending retry. Set only while a retry is pending. */
|
|
31
|
+
let cancelRetryTimer;
|
|
31
32
|
let retryDelay = RETRY_DELAY_MS;
|
|
32
33
|
/** Chains the sends, so that one runs at a time and queued envelopes leave in the order they were made. */
|
|
33
34
|
let sending = Promise.resolve();
|
|
@@ -40,23 +41,24 @@ export function newOutbox({ key, storage, lock, transport, forgetTransport, iden
|
|
|
40
41
|
return { holder, now: clock.now(), until: clock.now() + LEASE_MS };
|
|
41
42
|
}
|
|
42
43
|
function cancelSend() {
|
|
43
|
-
|
|
44
|
+
cancelSendTimer?.();
|
|
45
|
+
cancelSendTimer = undefined;
|
|
44
46
|
sendDue = Number.POSITIVE_INFINITY;
|
|
45
47
|
}
|
|
46
48
|
function cancelRetry() {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
cancelRetryTimer?.();
|
|
50
|
+
cancelRetryTimer = undefined;
|
|
49
51
|
}
|
|
50
52
|
/** Starts a send within `delayMs`, unless a send is due sooner or a retry is pending. */
|
|
51
53
|
function sendWithin(delayMs) {
|
|
52
54
|
// The pending retry carries the new events too. An earlier send would reach an intake that asked the outbox to
|
|
53
55
|
// wait.
|
|
54
|
-
if (
|
|
56
|
+
if (cancelRetryTimer !== undefined || sendDue <= clock.now() + delayMs) {
|
|
55
57
|
return;
|
|
56
58
|
}
|
|
57
|
-
|
|
59
|
+
cancelSendTimer?.();
|
|
58
60
|
sendDue = clock.now() + delayMs;
|
|
59
|
-
|
|
61
|
+
cancelSendTimer = clock.setTimeout(() => void sendUnlessRetryPending(), delayMs);
|
|
60
62
|
}
|
|
61
63
|
/** Sends at once, cancelling a pending retry, as `flush` and a retry timer do. */
|
|
62
64
|
function send() {
|
|
@@ -72,7 +74,7 @@ export function newOutbox({ key, storage, lock, transport, forgetTransport, iden
|
|
|
72
74
|
function sendUnlessRetryPending() {
|
|
73
75
|
cancelSend();
|
|
74
76
|
sending = sending
|
|
75
|
-
.then(async () => (
|
|
77
|
+
.then(async () => (cancelRetryTimer === undefined ? sendLeased() : undefined))
|
|
76
78
|
.catch(reportError);
|
|
77
79
|
return sending;
|
|
78
80
|
}
|
|
@@ -152,7 +154,7 @@ export function newOutbox({ key, storage, lock, transport, forgetTransport, iden
|
|
|
152
154
|
cancelSend();
|
|
153
155
|
cancelRetry();
|
|
154
156
|
const waitMs = Math.min(Math.max(retryDelay, askedWaitMs ?? 0), MAX_RETRY_DELAY_MS);
|
|
155
|
-
|
|
157
|
+
cancelRetryTimer = clock.setTimeout(() => void send(), waitMs);
|
|
156
158
|
retryDelay = Math.min(retryDelay * 2, MAX_RETRY_DELAY_MS);
|
|
157
159
|
await lock(async () => keepRetryWait(waitMs));
|
|
158
160
|
}
|
|
@@ -284,9 +286,9 @@ function httpStatus(status, reportError) {
|
|
|
284
286
|
* answers null or undefined, as only a product's own transport outside TypeScript does.
|
|
285
287
|
*/
|
|
286
288
|
async function answerOf(transport, body, clock) {
|
|
287
|
-
let
|
|
289
|
+
let cancelLate;
|
|
288
290
|
const late = new Promise((resolve) => {
|
|
289
|
-
|
|
291
|
+
cancelLate = clock.setTimeout(() => resolve({ status: 0 }), TRANSPORT_LIMIT_MS);
|
|
290
292
|
});
|
|
291
293
|
try {
|
|
292
294
|
const answer = await Promise.race([transport(body), late]);
|
|
@@ -299,7 +301,7 @@ async function answerOf(transport, body, clock) {
|
|
|
299
301
|
return { status: 0 };
|
|
300
302
|
}
|
|
301
303
|
finally {
|
|
302
|
-
|
|
304
|
+
cancelLate?.();
|
|
303
305
|
}
|
|
304
306
|
}
|
|
305
307
|
/**
|
package/dist/transport.js
CHANGED
|
@@ -10,7 +10,7 @@ export function newHttpTransport(endpoint, {
|
|
|
10
10
|
fetch = (input, init) => globalThis.fetch(input, init), clock = systemClock, } = {}) {
|
|
11
11
|
return async (body) => {
|
|
12
12
|
const timeout = new AbortController();
|
|
13
|
-
const
|
|
13
|
+
const cancelTimeout = clock.setTimeout(() => timeout.abort(), TIMEOUT_MS);
|
|
14
14
|
try {
|
|
15
15
|
const response = await fetch(endpoint, {
|
|
16
16
|
method: "POST",
|
|
@@ -32,7 +32,7 @@ fetch = (input, init) => globalThis.fetch(input, init), clock = systemClock, } =
|
|
|
32
32
|
return { status: 0 };
|
|
33
33
|
}
|
|
34
34
|
finally {
|
|
35
|
-
|
|
35
|
+
cancelTimeout();
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kk-forge/artel-collector",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Sends a product's analytics events to the intake service",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "KK Forge",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"build": "node --eval \"process.getBuiltinModule('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json",
|
|
46
46
|
"lint": "npm run lint:tsc && npm run lint:oxlint && npm run lint:format",
|
|
47
47
|
"lint:fix": "npm run lint:oxlint:fix && npm run lint:format:fix",
|
|
48
|
-
"lint:tsc": "tsc",
|
|
48
|
+
"lint:tsc": "tsc && tsc -p tsconfig.dom.json && tsc -p tsconfig.node.json",
|
|
49
49
|
"lint:oxlint": "oxlint src tests",
|
|
50
50
|
"lint:oxlint:fix": "oxlint --fix src tests",
|
|
51
51
|
"lint:format": "oxfmt --check .",
|
|
@@ -55,7 +55,8 @@
|
|
|
55
55
|
"test:ci": "vitest run --coverage"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@kk-forge/oxlint-config": "^
|
|
58
|
+
"@kk-forge/oxlint-config": "^4.0.0",
|
|
59
|
+
"@types/node": "^26.6.6",
|
|
59
60
|
"@vitest/coverage-v8": "^5.0.5",
|
|
60
61
|
"oxfmt": "^0.73.0",
|
|
61
62
|
"oxlint": "^1.90.0",
|