@kk-forge/artel-collector 0.2.0 → 0.4.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 +136 -121
- package/dist/collector.d.ts +5 -7
- package/dist/collector.js +112 -78
- package/dist/collectorTypes.d.ts +40 -24
- package/dist/collectorTypes.js +14 -4
- package/dist/desktop.d.ts +26 -0
- package/dist/desktop.js +15 -0
- package/dist/envelope.d.ts +4 -0
- package/dist/eventCheck.d.ts +6 -5
- package/dist/eventCheck.js +22 -2
- package/dist/exports.d.ts +2 -0
- package/dist/exports.js +2 -0
- package/dist/extension.d.ts +20 -25
- package/dist/extension.js +119 -31
- package/dist/hostInput.d.ts +10 -1
- package/dist/hostInput.js +20 -4
- package/dist/installState.d.ts +7 -3
- package/dist/installState.js +17 -1
- package/dist/outbox.d.ts +17 -8
- package/dist/outbox.js +62 -20
- package/dist/queue.d.ts +21 -7
- package/dist/queue.js +50 -16
- package/dist/stateFields.d.ts +6 -5
- package/dist/stateFields.js +23 -8
- package/dist/testing.d.ts +28 -0
- package/dist/testing.js +75 -0
- package/dist/transport.d.ts +5 -4
- package/dist/transport.js +1 -1
- package/dist/web.d.ts +5 -16
- package/dist/web.js +3 -17
- package/package.json +12 -4
package/dist/queue.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { type Envelope, type UsageEvent } from './envelope.js';
|
|
1
|
+
import { type Envelope, type IdKind, type UsageEvent } from './envelope.js';
|
|
2
2
|
import type { StorageAdapter } from './storage.js';
|
|
3
|
+
export declare const QUEUE_KEY = "queue";
|
|
3
4
|
export interface QueuedEnvelope {
|
|
4
5
|
qid: string;
|
|
5
6
|
/**
|
|
@@ -11,15 +12,16 @@ export interface QueuedEnvelope {
|
|
|
11
12
|
leasedBy?: string;
|
|
12
13
|
/** The time the lease ends, in epoch milliseconds. */
|
|
13
14
|
leasedUntil?: number;
|
|
14
|
-
/** Marks an
|
|
15
|
+
/** Marks an envelope of a state with an id the product does not know yet. It waits for the id and goes with it. */
|
|
15
16
|
awaitsId?: true;
|
|
16
17
|
/** Marks the envelope of an `opted_out`, which the intake takes only alone. No other event joins it. */
|
|
17
18
|
isOptOut?: true;
|
|
18
19
|
envelope: Envelope;
|
|
19
20
|
}
|
|
20
|
-
/** The user that an
|
|
21
|
+
/** The user that an envelope awaiting an id names, known once the product knows the id. */
|
|
21
22
|
export interface Identity {
|
|
22
23
|
id: string;
|
|
24
|
+
id_kind: IdKind;
|
|
23
25
|
browser?: Envelope['browser'];
|
|
24
26
|
}
|
|
25
27
|
/** Holds a sealed envelope and the body each send attempt carries. */
|
|
@@ -36,9 +38,10 @@ export interface LeaseTerms {
|
|
|
36
38
|
until: number;
|
|
37
39
|
}
|
|
38
40
|
/**
|
|
39
|
-
* Keeps envelopes in storage until the intake takes them, so a host that stops, such as an idle
|
|
40
|
-
* worker, loses none. A method that drops envelopes resolves to those whose marks may go back: the
|
|
41
|
-
* a body for, since the intake may keep a request that failed for the sender. `removeId` resolves
|
|
41
|
+
* Keeps envelopes and forget requests in storage until the intake takes them, so a host that stops, such as an idle
|
|
42
|
+
* extension service worker, loses none. A method that drops envelopes resolves to those whose marks may go back: the
|
|
43
|
+
* ones no seal stored a body for, since the intake may keep a request that failed for the sender. `removeId` resolves
|
|
44
|
+
* to all of them.
|
|
42
45
|
*/
|
|
43
46
|
interface Queue {
|
|
44
47
|
/**
|
|
@@ -75,11 +78,22 @@ interface Queue {
|
|
|
75
78
|
/** Drops the envelopes that name a user or wait to, as when the user stops being identified. */
|
|
76
79
|
removeIdentified(): Promise<QueuedEnvelope[]>;
|
|
77
80
|
removeAllButOptOuts(): Promise<QueuedEnvelope[]>;
|
|
81
|
+
/** Stores the id of a forget request, unless it is stored already. */
|
|
82
|
+
addForget(id: string): Promise<void>;
|
|
83
|
+
/** Resolves to the ids of the stored forget requests, oldest first. */
|
|
84
|
+
forgets(): Promise<string[]>;
|
|
85
|
+
removeForget(id: string): Promise<void>;
|
|
78
86
|
}
|
|
79
87
|
/** Returns a queue in `storage`. The caller holds a lock around each call. */
|
|
80
88
|
export declare function newQueue({ storage, reportError, }: {
|
|
81
89
|
storage: StorageAdapter;
|
|
82
90
|
reportError: (error: unknown) => void;
|
|
83
91
|
}): Queue;
|
|
84
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Returns the envelopes of a stored queue, oldest first, or undefined for a value that is no JSON list. It skips an item
|
|
94
|
+
* without a queue id and an envelope with a list of events.
|
|
95
|
+
*/
|
|
96
|
+
export declare function parseQueue(stored: string | undefined): QueuedEnvelope[] | undefined;
|
|
97
|
+
/** Returns `byteCount` random bytes, 8 by default, as lowercase hex. */
|
|
98
|
+
export declare function newRandomId(byteCount?: number): string;
|
|
85
99
|
export {};
|
package/dist/queue.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { LifecycleEvent } from './envelope.js';
|
|
2
|
-
const QUEUE_KEY = 'queue';
|
|
2
|
+
export const QUEUE_KEY = 'queue';
|
|
3
|
+
/** Names the ids of the stored forget requests, a JSON list. */
|
|
4
|
+
const FORGETS_KEY = 'forgets';
|
|
3
5
|
const MAX_ENVELOPE_EVENTS = 100;
|
|
4
6
|
/**
|
|
5
7
|
* The largest request body, under 64 KiB with some margin. A keepalive request and the intake both refuse a larger
|
|
@@ -20,21 +22,13 @@ export function newQueue({ storage, reportError, }) {
|
|
|
20
22
|
* throw.
|
|
21
23
|
*/
|
|
22
24
|
async function read() {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
queued = JSON.parse(stored);
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
// The check below reports its own error and drops the parser's error. The parser's message quotes the stored
|
|
30
|
-
// text, which may hold an id or a page.
|
|
31
|
-
}
|
|
32
|
-
if (!Array.isArray(queued)) {
|
|
25
|
+
const queued = parseQueue(await storage.get(QUEUE_KEY));
|
|
26
|
+
if (queued === undefined) {
|
|
33
27
|
reportError(new Error('artel: the stored queue is not a JSON list, so it was dropped'));
|
|
34
28
|
await write([]);
|
|
35
29
|
return [];
|
|
36
30
|
}
|
|
37
|
-
return queued
|
|
31
|
+
return queued;
|
|
38
32
|
}
|
|
39
33
|
async function write(items) {
|
|
40
34
|
await storage.set(QUEUE_KEY, JSON.stringify(items));
|
|
@@ -59,6 +53,20 @@ export function newQueue({ storage, reportError, }) {
|
|
|
59
53
|
const gone = new Set([...droppedOptOuts, ...dropped]);
|
|
60
54
|
return { kept: items.filter((item) => !gone.has(item)), dropped: unsent(dropped) };
|
|
61
55
|
}
|
|
56
|
+
/** Returns the stored forget requests. A value that is no JSON list reads as none, and only its strings count. */
|
|
57
|
+
async function readForgets() {
|
|
58
|
+
let bodies;
|
|
59
|
+
try {
|
|
60
|
+
bodies = JSON.parse((await storage.get(FORGETS_KEY)) ?? '[]');
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
return Array.isArray(bodies) ? bodies.filter((body) => typeof body === 'string') : [];
|
|
66
|
+
}
|
|
67
|
+
async function writeForgets(ids) {
|
|
68
|
+
await storage.set(FORGETS_KEY, JSON.stringify(ids));
|
|
69
|
+
}
|
|
62
70
|
async function update(change) {
|
|
63
71
|
await write(change(await read()));
|
|
64
72
|
}
|
|
@@ -119,14 +127,15 @@ export function newQueue({ storage, reportError, }) {
|
|
|
119
127
|
return undefined;
|
|
120
128
|
}
|
|
121
129
|
if (item.awaitsId === true && identity !== undefined) {
|
|
122
|
-
// The browser, the session and the id go after the other fields and the events. The body stored
|
|
123
|
-
// keeps that order on every attempt.
|
|
130
|
+
// The browser, the session and the id with its kind go after the other fields and the events. The body stored
|
|
131
|
+
// at this seal keeps that order on every attempt.
|
|
124
132
|
const { session, ...fields } = item.envelope;
|
|
125
133
|
item.envelope = {
|
|
126
134
|
...fields,
|
|
127
135
|
...(identity.browser !== undefined && { browser: identity.browser }),
|
|
128
136
|
session,
|
|
129
137
|
id: identity.id,
|
|
138
|
+
id_kind: identity.id_kind,
|
|
130
139
|
};
|
|
131
140
|
delete item.awaitsId;
|
|
132
141
|
}
|
|
@@ -149,8 +158,31 @@ export function newQueue({ storage, reportError, }) {
|
|
|
149
158
|
removeId: (id) => removeWhere((item) => item.envelope.id === id),
|
|
150
159
|
removeIdentified: async () => unsent(await removeWhere((item) => item.envelope.id !== undefined || item.awaitsId === true)),
|
|
151
160
|
removeAllButOptOuts: async () => unsent(await removeWhere((item) => item.isOptOut !== true)),
|
|
161
|
+
addForget: async (id) => {
|
|
162
|
+
const ids = await readForgets();
|
|
163
|
+
if (!ids.includes(id)) {
|
|
164
|
+
await writeForgets([...ids, id]);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
forgets: readForgets,
|
|
168
|
+
removeForget: async (id) => writeForgets((await readForgets()).filter((stored) => stored !== id)),
|
|
152
169
|
};
|
|
153
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Returns the envelopes of a stored queue, oldest first, or undefined for a value that is no JSON list. It skips an item
|
|
173
|
+
* without a queue id and an envelope with a list of events.
|
|
174
|
+
*/
|
|
175
|
+
export function parseQueue(stored) {
|
|
176
|
+
let queued;
|
|
177
|
+
try {
|
|
178
|
+
queued = JSON.parse(stored ?? '[]');
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
// The caller reports a queue that is no list. The parser's message quotes the stored text, which may hold an id or
|
|
182
|
+
// a page, so it goes nowhere.
|
|
183
|
+
}
|
|
184
|
+
return Array.isArray(queued) ? queued.filter(isQueuedEnvelope) : undefined;
|
|
185
|
+
}
|
|
154
186
|
/** Returns the envelopes no seal stored a body for, which no request carried to the intake. */
|
|
155
187
|
function unsent(items) {
|
|
156
188
|
return items.filter((item) => item.body === undefined);
|
|
@@ -174,8 +206,10 @@ function fits(envelope, event) {
|
|
|
174
206
|
const withEvent = { ...envelope, events: [...envelope.events, event] };
|
|
175
207
|
return new TextEncoder().encode(JSON.stringify(withEvent)).length <= MAX_ENVELOPE_BYTES;
|
|
176
208
|
}
|
|
177
|
-
|
|
178
|
-
|
|
209
|
+
/** Returns `byteCount` random bytes, 8 by default, as lowercase hex. */
|
|
210
|
+
export function newRandomId(byteCount = 8) {
|
|
211
|
+
const bytes = crypto.getRandomValues(new Uint8Array(byteCount));
|
|
212
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
179
213
|
}
|
|
180
214
|
function isQueuedEnvelope(value) {
|
|
181
215
|
if (typeof value !== 'object' || value === null || typeof Reflect.get(value, 'qid') !== 'string') {
|
package/dist/stateFields.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AnalyticsState, Mode } from './collectorTypes.js';
|
|
2
|
-
import { Platform, type Envelope } from './envelope.js';
|
|
2
|
+
import { Platform, type Envelope, type IdKind } from './envelope.js';
|
|
3
3
|
export interface Build {
|
|
4
4
|
platform: Platform;
|
|
5
5
|
version: string;
|
|
@@ -14,17 +14,18 @@ export interface StateFields {
|
|
|
14
14
|
product: ProductFields;
|
|
15
15
|
/** Whether the install ages go exact, rather than as the lowest value of their bucket. */
|
|
16
16
|
isAgeExact: boolean;
|
|
17
|
-
/**
|
|
18
|
-
|
|
17
|
+
/** The kind of id an envelope carries, with the browser, or undefined for a state that names no user. */
|
|
18
|
+
idKind: IdKind | undefined;
|
|
19
19
|
/** Whether an event may carry its page and referrer. The intake keeps them only from a site channel. */
|
|
20
20
|
mayCarryPage: boolean;
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
export declare function isAnalyticsState(value: unknown): value is AnalyticsState;
|
|
23
|
+
/** Tells whether a channel of `mode` takes `state`: a state up to its mode. A mode that does not exist is anonymous. */
|
|
23
24
|
export declare function channelTakes(mode: Mode, state: unknown): state is AnalyticsState;
|
|
24
25
|
/** Returns whether events of this build may carry a page, which the intake keeps only from a site channel. */
|
|
25
26
|
export declare function carriesPage(build: Pick<Build, 'platform'>): boolean;
|
|
26
27
|
/**
|
|
27
|
-
* Returns what the collector sends in `state` for this build and these languages. Only
|
|
28
|
+
* Returns what the collector sends in `state` for this build and these languages. Only a state with an id carries the
|
|
28
29
|
* full languages and the exact ages: with the platform and version they can single out a user, so they go only with the
|
|
29
30
|
* consent the id needs.
|
|
30
31
|
*/
|
package/dist/stateFields.js
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
1
|
import { AnalyticsState, Mode } from './collectorTypes.js';
|
|
2
2
|
import { Platform } from './envelope.js';
|
|
3
3
|
import { primaryLanguage } from './fields.js';
|
|
4
|
-
/**
|
|
4
|
+
/** Ranks the modes and the states by what they reveal of a user. A mode takes the states of a rank up to its own. */
|
|
5
|
+
const MODE_RANKS = new Map([
|
|
6
|
+
[Mode.Anonymous, 0],
|
|
7
|
+
[Mode.Pseudonymous, 1],
|
|
8
|
+
[Mode.Identified, 2],
|
|
9
|
+
]);
|
|
10
|
+
const STATE_RANKS = new Map([
|
|
11
|
+
[AnalyticsState.OptedOut, 0],
|
|
12
|
+
[AnalyticsState.Anonymous, 0],
|
|
13
|
+
[AnalyticsState.Pseudonymous, 1],
|
|
14
|
+
[AnalyticsState.Identified, 2],
|
|
15
|
+
]);
|
|
16
|
+
export function isAnalyticsState(value) {
|
|
17
|
+
return STATE_RANKS.has(value);
|
|
18
|
+
}
|
|
19
|
+
/** Tells whether a channel of `mode` takes `state`: a state up to its mode. A mode that does not exist is anonymous. */
|
|
5
20
|
export function channelTakes(mode, state) {
|
|
6
|
-
return (
|
|
7
|
-
(state !== AnalyticsState.Identified || mode === Mode.Identified));
|
|
21
|
+
return (STATE_RANKS.get(state) ?? Number.POSITIVE_INFINITY) <= (MODE_RANKS.get(mode) ?? 0);
|
|
8
22
|
}
|
|
9
23
|
/** Returns whether events of this build may carry a page, which the intake keeps only from a site channel. */
|
|
10
24
|
export function carriesPage(build) {
|
|
11
25
|
return build.platform === Platform.Web;
|
|
12
26
|
}
|
|
13
27
|
/**
|
|
14
|
-
* Returns what the collector sends in `state` for this build and these languages. Only
|
|
28
|
+
* Returns what the collector sends in `state` for this build and these languages. Only a state with an id carries the
|
|
15
29
|
* full languages and the exact ages: with the platform and version they can single out a user, so they go only with the
|
|
16
30
|
* consent the id needs.
|
|
17
31
|
*/
|
|
18
32
|
export function fieldsForState({ state, build, browserLanguage, uiLanguage, }) {
|
|
19
|
-
const
|
|
20
|
-
const
|
|
33
|
+
const idKind = state === AnalyticsState.Pseudonymous || state === AnalyticsState.Identified ? state : undefined;
|
|
34
|
+
const isNamed = idKind !== undefined;
|
|
35
|
+
const shape = (tag) => tag === undefined || isNamed ? tag : primaryLanguage(tag);
|
|
21
36
|
const shapedBrowserLanguage = shape(browserLanguage);
|
|
22
37
|
const shapedUiLanguage = shape(uiLanguage);
|
|
23
38
|
const recordsEvents = state !== AnalyticsState.OptedOut;
|
|
@@ -29,8 +44,8 @@ export function fieldsForState({ state, build, browserLanguage, uiLanguage, }) {
|
|
|
29
44
|
...(shapedBrowserLanguage !== undefined && { browser_language: shapedBrowserLanguage }),
|
|
30
45
|
...(shapedUiLanguage !== undefined && { ui_language: shapedUiLanguage }),
|
|
31
46
|
},
|
|
32
|
-
isAgeExact:
|
|
33
|
-
|
|
47
|
+
isAgeExact: isNamed,
|
|
48
|
+
idKind,
|
|
34
49
|
mayCarryPage: recordsEvents && carriesPage(build),
|
|
35
50
|
};
|
|
36
51
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Mode, type Collector } from './collectorTypes.js';
|
|
2
|
+
import type { EventParams } from './envelope.js';
|
|
3
|
+
import { type StorageAdapter } from './storage.js';
|
|
4
|
+
/** One event as a product tracked it. */
|
|
5
|
+
export interface RecordedEvent {
|
|
6
|
+
name: string;
|
|
7
|
+
params?: EventParams;
|
|
8
|
+
}
|
|
9
|
+
/** A collector that keeps in memory what it would send, for a product's own tests. */
|
|
10
|
+
export type RecordingCollector<M extends Mode = Mode> = Collector<M> & {
|
|
11
|
+
/** The events it took, oldest first. */
|
|
12
|
+
readonly events: RecordedEvent[];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Returns a collector of channel mode `mode`, `anonymous` by default, that sends nothing and records each event the
|
|
16
|
+
* real collector would send. Its user starts in the anonymous state. It refuses an event `track` refuses, a state above
|
|
17
|
+
* the mode and an id `forget` refuses, with the same report to `onError`. It records `opted_out` at the opt-out, and
|
|
18
|
+
* nothing in the opted_out state. It keeps one version: `installed` records once, and `updated` only while neither of
|
|
19
|
+
* them recorded. `uninstallParams` resolves to undefined.
|
|
20
|
+
*/
|
|
21
|
+
export declare function newRecordingCollector<M extends Mode = typeof Mode.Anonymous>(mode?: M, { onError }?: {
|
|
22
|
+
onError?: (error: Error) => void;
|
|
23
|
+
}): RecordingCollector<M>;
|
|
24
|
+
/**
|
|
25
|
+
* Resolves to the events a collector of channel `key` holds queued in `storage`, oldest first, so a product's test reads
|
|
26
|
+
* them without the queue's stored form. It writes nothing, and reads a stored queue that is no JSON list as empty.
|
|
27
|
+
*/
|
|
28
|
+
export declare function queuedEvents(storage: StorageAdapter, key: string): Promise<RecordedEvent[]>;
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { AnalyticsState, Mode } from './collectorTypes.js';
|
|
2
|
+
import { acceptedEvent } from './eventCheck.js';
|
|
3
|
+
import { isUsableId, quoted, reportTo } from './hostInput.js';
|
|
4
|
+
import { parseQueue, QUEUE_KEY } from './queue.js';
|
|
5
|
+
import { channelTakes } from './stateFields.js';
|
|
6
|
+
import { newChannelStorage } from './storage.js';
|
|
7
|
+
/**
|
|
8
|
+
* Returns a collector of channel mode `mode`, `anonymous` by default, that sends nothing and records each event the
|
|
9
|
+
* real collector would send. Its user starts in the anonymous state. It refuses an event `track` refuses, a state above
|
|
10
|
+
* the mode and an id `forget` refuses, with the same report to `onError`. It records `opted_out` at the opt-out, and
|
|
11
|
+
* nothing in the opted_out state. It keeps one version: `installed` records once, and `updated` only while neither of
|
|
12
|
+
* them recorded. `uninstallParams` resolves to undefined.
|
|
13
|
+
*/
|
|
14
|
+
export function newRecordingCollector(mode, { onError } = {}) {
|
|
15
|
+
const channelMode = mode ?? Mode.Anonymous;
|
|
16
|
+
const reportError = (error) => reportTo(onError, error);
|
|
17
|
+
const events = [];
|
|
18
|
+
let state = AnalyticsState.Anonymous;
|
|
19
|
+
let isInstalled = false;
|
|
20
|
+
let isVersionMarked = false;
|
|
21
|
+
const record = (name, params) => {
|
|
22
|
+
if (state !== AnalyticsState.OptedOut) {
|
|
23
|
+
events.push({ name, ...(params !== undefined && { params }) });
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
events,
|
|
28
|
+
track: async (name, given) => {
|
|
29
|
+
const event = acceptedEvent(name, given, reportError);
|
|
30
|
+
if (event !== undefined) {
|
|
31
|
+
record(name, event.params);
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
installed: async () => {
|
|
35
|
+
if (!isInstalled && state !== AnalyticsState.OptedOut) {
|
|
36
|
+
isInstalled = true;
|
|
37
|
+
isVersionMarked = true;
|
|
38
|
+
record('installed');
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
updated: async () => {
|
|
42
|
+
if (!isVersionMarked && state !== AnalyticsState.OptedOut) {
|
|
43
|
+
isVersionMarked = true;
|
|
44
|
+
record('updated');
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
setState: async (next) => {
|
|
48
|
+
if (channelTakes(channelMode, next)) {
|
|
49
|
+
if (next === AnalyticsState.OptedOut) {
|
|
50
|
+
record(AnalyticsState.OptedOut);
|
|
51
|
+
}
|
|
52
|
+
state = next;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
reportError(new TypeError(`artel: a channel of mode ${quoted(channelMode)} takes no state ${quoted(next)}`));
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
getState: async () => state,
|
|
59
|
+
flush: async () => undefined,
|
|
60
|
+
forget: async (id) => {
|
|
61
|
+
if (!isUsableId(id)) {
|
|
62
|
+
reportError(new TypeError('artel: forget takes an id of 1 to 64 characters'));
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
uninstallParams: async () => undefined,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Resolves to the events a collector of channel `key` holds queued in `storage`, oldest first, so a product's test reads
|
|
70
|
+
* them without the queue's stored form. It writes nothing, and reads a stored queue that is no JSON list as empty.
|
|
71
|
+
*/
|
|
72
|
+
export async function queuedEvents(storage, key) {
|
|
73
|
+
const queued = parseQueue(await newChannelStorage(storage, key).get(QUEUE_KEY)) ?? [];
|
|
74
|
+
return queued.flatMap(({ envelope }) => envelope.events.map(({ name, params }) => ({ name, ...(params !== undefined && { params }) })));
|
|
75
|
+
}
|
package/dist/transport.d.ts
CHANGED
|
@@ -5,13 +5,14 @@ export interface TransportResult {
|
|
|
5
5
|
retryAfter?: string;
|
|
6
6
|
}
|
|
7
7
|
/**
|
|
8
|
-
* Sends one
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* Sends one request body and resolves to the intake's answer or its HTTP status. The body is an envelope, for the
|
|
9
|
+
* intake's `/v1/public/relay`, or a forget request `{ key, id }` without `events`, for its `/v1/public/forget`. It must
|
|
10
|
+
* resolve within 30 seconds. After 35 seconds the outbox counts it as status 0 and sends the body again. The intake
|
|
11
|
+
* keeps a forgotten user's events from a request that arrives over a minute after the forget.
|
|
11
12
|
*/
|
|
12
13
|
export type Transport = (body: string) => Promise<number | TransportResult>;
|
|
13
14
|
/**
|
|
14
|
-
* Returns a transport that posts each
|
|
15
|
+
* Returns a transport that posts each body to `endpoint` with `fetch`, the host's own by default. The transport
|
|
15
16
|
* resolves to the status and the raw `Retry-After` header, or to status 0 when a request fails or takes over 30
|
|
16
17
|
* seconds.
|
|
17
18
|
*/
|
package/dist/transport.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { systemClock } from './clock.js';
|
|
2
2
|
const TIMEOUT_MS = 30_000;
|
|
3
3
|
/**
|
|
4
|
-
* Returns a transport that posts each
|
|
4
|
+
* Returns a transport that posts each body to `endpoint` with `fetch`, the host's own by default. The transport
|
|
5
5
|
* resolves to the status and the raw `Retry-After` header, or to status 0 when a request fails or takes over 30
|
|
6
6
|
* seconds.
|
|
7
7
|
*/
|
package/dist/web.d.ts
CHANGED
|
@@ -10,17 +10,12 @@ export type WebCollectorOptions = Omit<CommonOptions, 'storage'> & {
|
|
|
10
10
|
} & PageOptions & Delivery & ModeOptions;
|
|
11
11
|
/**
|
|
12
12
|
* Returns a collector for a website or a web app. It runs over `localStorage` and Web Locks, and sends the user's
|
|
13
|
-
* language from `navigator.language`, the page from `pageFields` and, in
|
|
14
|
-
* `detectBrowser`. The site passes its own `uiLanguage`. A
|
|
15
|
-
* this module's helpers.
|
|
13
|
+
* language from `navigator.language`, the page from `pageFields` and, in a state with an id, the browser from
|
|
14
|
+
* `detectBrowser`. The site passes its own `uiLanguage`. A desktop window uses `newDesktopCollector`.
|
|
16
15
|
*/
|
|
17
|
-
export declare function newWebCollector(options: WebCollectorOptions & {
|
|
18
|
-
mode:
|
|
19
|
-
}): Collector<
|
|
20
|
-
export declare function newWebCollector(options: WebCollectorOptions & {
|
|
21
|
-
mode: typeof Mode.Anonymous;
|
|
22
|
-
}): Collector<'anonymous'>;
|
|
23
|
-
export declare function newWebCollector(options: WebCollectorOptions): Collector;
|
|
16
|
+
export declare function newWebCollector<M extends Mode>(options: WebCollectorOptions & {
|
|
17
|
+
mode: M;
|
|
18
|
+
}): Collector<M>;
|
|
24
19
|
/**
|
|
25
20
|
* Returns a storage adapter over `localStorage`, which every tab of the origin shares. A Web Worker has no
|
|
26
21
|
* `localStorage`, so a collector there passes its own storage.
|
|
@@ -42,9 +37,3 @@ export declare function pageFields(): PageFields;
|
|
|
42
37
|
* function that stops it. A query change alone sends none. Once every tracker stops, `history` is as they found it.
|
|
43
38
|
*/
|
|
44
39
|
export declare function trackPageViews(collector: Collector): () => void;
|
|
45
|
-
/**
|
|
46
|
-
* Flushes the queue when the page is hidden, since the user may not return. A request to `endpoint` that started
|
|
47
|
-
* before the tab closes still finishes. A send not yet started waits for the next visit. Returns the function that
|
|
48
|
-
* removes the listener.
|
|
49
|
-
*/
|
|
50
|
-
export declare function flushWhenHidden(collector: Collector): () => void;
|
package/dist/web.js
CHANGED
|
@@ -10,9 +10,9 @@ export function newWebCollector(options) {
|
|
|
10
10
|
browserLanguage: options.browserLanguage ?? navigator.language,
|
|
11
11
|
getPage: options.getPage ?? pageFieldsWherePageIs,
|
|
12
12
|
};
|
|
13
|
-
return options.mode === Mode.
|
|
14
|
-
? newCollector({ ...options, ...defaults
|
|
15
|
-
: newCollector({ ...options, ...defaults });
|
|
13
|
+
return options.mode === Mode.Anonymous
|
|
14
|
+
? newCollector({ ...options, ...defaults })
|
|
15
|
+
: newCollector({ ...options, ...defaults, browser: options.browser ?? (() => detectBrowser(navigator)) });
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Returns a storage adapter over `localStorage`, which every tab of the origin shares. A Web Worker has no
|
|
@@ -110,17 +110,3 @@ function navigationApi() {
|
|
|
110
110
|
const navigation = Reflect.get(globalThis, 'navigation');
|
|
111
111
|
return navigation instanceof EventTarget ? navigation : undefined;
|
|
112
112
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Flushes the queue when the page is hidden, since the user may not return. A request to `endpoint` that started
|
|
115
|
-
* before the tab closes still finishes. A send not yet started waits for the next visit. Returns the function that
|
|
116
|
-
* removes the listener.
|
|
117
|
-
*/
|
|
118
|
-
export function flushWhenHidden(collector) {
|
|
119
|
-
const onChange = () => {
|
|
120
|
-
if (document.visibilityState === 'hidden') {
|
|
121
|
-
void collector.flush();
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
document.addEventListener('visibilitychange', onChange);
|
|
125
|
-
return () => document.removeEventListener('visibilitychange', onChange);
|
|
126
|
-
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kk-forge/artel-collector",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Sends a product's
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Sends a product's analytics events to the intake service",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "KK Forge",
|
|
7
7
|
"repository": {
|
|
@@ -27,6 +27,14 @@
|
|
|
27
27
|
"./extension": {
|
|
28
28
|
"types": "./dist/extension.d.ts",
|
|
29
29
|
"default": "./dist/extension.js"
|
|
30
|
+
},
|
|
31
|
+
"./desktop": {
|
|
32
|
+
"types": "./dist/desktop.d.ts",
|
|
33
|
+
"default": "./dist/desktop.js"
|
|
34
|
+
},
|
|
35
|
+
"./testing": {
|
|
36
|
+
"types": "./dist/testing.d.ts",
|
|
37
|
+
"default": "./dist/testing.js"
|
|
30
38
|
}
|
|
31
39
|
},
|
|
32
40
|
"publishConfig": {
|
|
@@ -37,8 +45,8 @@
|
|
|
37
45
|
"build": "node --eval \"process.getBuiltinModule('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json",
|
|
38
46
|
"lint": "npm run lint:tsc && npm run lint:oxlint && npm run lint:format",
|
|
39
47
|
"lint:fix": "npm run lint:oxlint:fix && npm run lint:format:fix",
|
|
40
|
-
"lint:tsc": "tsc
|
|
41
|
-
"lint:oxlint": "oxlint --type-aware
|
|
48
|
+
"lint:tsc": "tsc",
|
|
49
|
+
"lint:oxlint": "oxlint --type-aware src tests",
|
|
42
50
|
"lint:oxlint:fix": "oxlint --type-aware --fix src tests",
|
|
43
51
|
"lint:format": "oxfmt --check .",
|
|
44
52
|
"lint:format:fix": "oxfmt .",
|