@kk-forge/artel-collector 0.1.1 → 0.3.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 +226 -84
- package/dist/browser.d.ts +41 -0
- package/dist/browser.js +99 -0
- package/dist/collector.d.ts +12 -81
- package/dist/collector.js +252 -123
- package/dist/collectorTypes.d.ts +193 -0
- package/dist/collectorTypes.js +25 -0
- package/dist/desktop.d.ts +29 -0
- package/dist/desktop.js +10 -0
- package/dist/envelope.d.ts +29 -7
- package/dist/envelope.js +19 -3
- package/dist/eventCheck.d.ts +12 -0
- package/dist/eventCheck.js +110 -0
- package/dist/exports.d.ts +4 -2
- package/dist/exports.js +3 -1
- package/dist/extension.d.ts +44 -7
- package/dist/extension.js +98 -117
- package/dist/fields.d.ts +34 -0
- package/dist/fields.js +103 -0
- package/dist/hostInput.d.ts +39 -0
- package/dist/hostInput.js +182 -0
- package/dist/installState.d.ts +30 -4
- package/dist/installState.js +110 -25
- package/dist/lock.d.ts +10 -1
- package/dist/lock.js +12 -1
- package/dist/outbox.d.ts +31 -12
- package/dist/outbox.js +161 -52
- package/dist/page.d.ts +30 -0
- package/dist/page.js +69 -0
- package/dist/pagePath.d.ts +12 -0
- package/dist/pagePath.js +163 -0
- package/dist/queue.d.ts +37 -14
- package/dist/queue.js +98 -35
- package/dist/stateFields.d.ts +36 -0
- package/dist/stateFields.js +36 -0
- package/dist/storage.d.ts +9 -1
- package/dist/storage.js +11 -0
- package/dist/transport.d.ts +11 -5
- package/dist/transport.js +5 -3
- package/dist/web.d.ts +37 -9
- package/dist/web.js +94 -18
- package/package.json +5 -1
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { Browser } from './browser.js';
|
|
2
|
+
import type { Clock } from './clock.js';
|
|
3
|
+
import type { EventParams, Platform } from './envelope.js';
|
|
4
|
+
import type { Lock } from './lock.js';
|
|
5
|
+
import type { ProductFields } from './stateFields.js';
|
|
6
|
+
import type { StorageAdapter } from './storage.js';
|
|
7
|
+
import type { Transport } from './transport.js';
|
|
8
|
+
/**
|
|
9
|
+
* What a channel may collect at most, as set in the intake's admin. The collector's mode matches its channel's. An
|
|
10
|
+
* identified channel lets a user who agrees be identified. An anonymous one never does.
|
|
11
|
+
*/
|
|
12
|
+
export declare const Mode: {
|
|
13
|
+
readonly Anonymous: 'anonymous';
|
|
14
|
+
readonly Identified: 'identified';
|
|
15
|
+
};
|
|
16
|
+
export type Mode = (typeof Mode)[keyof typeof Mode];
|
|
17
|
+
/** What the collector sends for this user, a choice the collector stores. Every user starts anonymous. */
|
|
18
|
+
export declare const AnalyticsState: {
|
|
19
|
+
/** Sends nothing. Switching to it sends one `opted_out`, with no id or browser, and drops what was not sent. */
|
|
20
|
+
readonly OptedOut: 'opted_out';
|
|
21
|
+
/** Sends events with no id or browser, the primary languages only and coarse ages. Needs no consent. */
|
|
22
|
+
readonly Anonymous: 'anonymous';
|
|
23
|
+
/** Sends the id from `getId`, the browser, the full languages and exact ages. Only an identified channel takes it. */
|
|
24
|
+
readonly Identified: 'identified';
|
|
25
|
+
};
|
|
26
|
+
export type AnalyticsState = (typeof AnalyticsState)[keyof typeof AnalyticsState];
|
|
27
|
+
/** The states a collector of channel mode `M` takes: an anonymous channel never identifies a user. */
|
|
28
|
+
export type StateOf<M extends Mode> = M extends typeof Mode.Identified ? AnalyticsState : Exclude<AnalyticsState, typeof AnalyticsState.Identified>;
|
|
29
|
+
/**
|
|
30
|
+
* Returns the user's id, or undefined while the product does not know it yet. The collector never makes one up. The
|
|
31
|
+
* collector calls it while it holds its lock, so it must not await a method of the same collector, which would wait on
|
|
32
|
+
* it forever. The same holds for a `browser` function.
|
|
33
|
+
*/
|
|
34
|
+
export type GetId = () => string | undefined | Promise<string | undefined>;
|
|
35
|
+
/** The options every collector takes, whatever its platform and mode. */
|
|
36
|
+
export interface CommonOptions {
|
|
37
|
+
/**
|
|
38
|
+
* The live channel key in the build for the store, the test key in any other build. A key that breaks the intake's
|
|
39
|
+
* form, or whose channel kind does not take the platform, makes the constructor throw.
|
|
40
|
+
*/
|
|
41
|
+
key: string;
|
|
42
|
+
/**
|
|
43
|
+
* Product version, such as `1.2.3` or `1.2.3-beta.1`. The intake refuses an envelope without one, so a version that
|
|
44
|
+
* breaks the intake's format makes the constructor throw.
|
|
45
|
+
*/
|
|
46
|
+
version: string;
|
|
47
|
+
/** The user's language, such as `pt-BR`: what the browser asks sites for. */
|
|
48
|
+
browserLanguage?: string | undefined;
|
|
49
|
+
/** The language the user picked in the product, when the product offers that choice. Left out otherwise. */
|
|
50
|
+
uiLanguage?: string | undefined;
|
|
51
|
+
storage: StorageAdapter;
|
|
52
|
+
/** Minutes without events that end a session, 10 by default. */
|
|
53
|
+
sessionIdleMinutes?: number | undefined;
|
|
54
|
+
/** The longest an event waits in the queue before a send starts, 5 seconds by default. */
|
|
55
|
+
sendDelaySeconds?: number | undefined;
|
|
56
|
+
/** Time and timers, the host's own by default. */
|
|
57
|
+
clock?: Clock | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Runs an operation while no other context that shares `storage` runs one. Without it, two contexts recording at once
|
|
60
|
+
* may both mark the first event of a day, or both send one envelope, and an event another context records during an
|
|
61
|
+
* opt-out may still leave.
|
|
62
|
+
*/
|
|
63
|
+
lock?: Lock | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Hears every failure the collector does not retry, such as a refused event, a dropped envelope or a storage error.
|
|
66
|
+
* A request the collector sends again later, after status 0, 429 or 5xx, reaches it only once its envelope is
|
|
67
|
+
* dropped.
|
|
68
|
+
*/
|
|
69
|
+
onError?: ((error: Error) => void) | undefined;
|
|
70
|
+
}
|
|
71
|
+
/** Where envelopes go: the intake's address, or the product's own transport instead. */
|
|
72
|
+
export type Delivery = {
|
|
73
|
+
endpoint: string;
|
|
74
|
+
transport?: never;
|
|
75
|
+
} | {
|
|
76
|
+
transport: Transport;
|
|
77
|
+
endpoint?: never;
|
|
78
|
+
};
|
|
79
|
+
/** The channel mode, with what an identified channel takes besides. */
|
|
80
|
+
export type ModeOptions = {
|
|
81
|
+
mode: typeof Mode.Anonymous;
|
|
82
|
+
getId?: never;
|
|
83
|
+
browser?: never;
|
|
84
|
+
} | {
|
|
85
|
+
mode: typeof Mode.Identified;
|
|
86
|
+
/** Gives the id in the identified state. The collector calls it in no other state. */
|
|
87
|
+
getId: GetId;
|
|
88
|
+
/**
|
|
89
|
+
* The browser the product runs in, or a function that returns it, such as `() => detectBrowser(navigator)`. The
|
|
90
|
+
* collector calls the function once, for the first event with an id, the only kind of envelope that carries it.
|
|
91
|
+
*/
|
|
92
|
+
browser?: Browser | (() => Browser | undefined) | undefined;
|
|
93
|
+
};
|
|
94
|
+
/** The options only a site's collector takes, since only platform web sends pages. */
|
|
95
|
+
export interface PageOptions {
|
|
96
|
+
/**
|
|
97
|
+
* Returns the page of an event with this name. The collector calls it when a method is called, so a navigation right
|
|
98
|
+
* after the call does not move the event. The core reads no page itself, and shapes whatever this function returns
|
|
99
|
+
* as `PageFields` says.
|
|
100
|
+
*/
|
|
101
|
+
getPage?: ((name: string) => PageFields | undefined) | undefined;
|
|
102
|
+
}
|
|
103
|
+
/** The platforms whose products run in a browser. Only their envelopes may name it. */
|
|
104
|
+
type BrowserPlatform = typeof Platform.Web | typeof Platform.Extension;
|
|
105
|
+
/** The options of `newCollector`. Only a site takes `getPage`, and a desktop product names no browser. */
|
|
106
|
+
export type CollectorOptions = CommonOptions & Delivery & ModeOptions & (({
|
|
107
|
+
platform: typeof Platform.Web;
|
|
108
|
+
} & PageOptions) | {
|
|
109
|
+
platform: typeof Platform.Extension;
|
|
110
|
+
getPage?: never;
|
|
111
|
+
} | {
|
|
112
|
+
platform: Exclude<Platform, BrowserPlatform>;
|
|
113
|
+
browser?: never;
|
|
114
|
+
getPage?: never;
|
|
115
|
+
});
|
|
116
|
+
/** What a site channel's event says about its page. The intake keeps only the path and the referring host. */
|
|
117
|
+
export interface PageFields {
|
|
118
|
+
/**
|
|
119
|
+
* The page's path, or its full URL. The collector sends the path without its query string, fragment and path
|
|
120
|
+
* parameters, with each segment that holds an `@`, an IPv4 or an IPv6 address as `:address`, and a segment that
|
|
121
|
+
* starts a URL and the rest of the path after it as one `:address`, cut to 256 characters.
|
|
122
|
+
*/
|
|
123
|
+
page?: string;
|
|
124
|
+
/**
|
|
125
|
+
* The referrer URL. The collector sends its host only, with the first `page_view` it records, and none for a host
|
|
126
|
+
* that is an IP address or holds an `@`, or a host past 128 characters. It leaves the referrer out when the page is a
|
|
127
|
+
* full URL of the same host.
|
|
128
|
+
*/
|
|
129
|
+
referrer?: string;
|
|
130
|
+
}
|
|
131
|
+
/** Bucket for the number of days with at least one event. */
|
|
132
|
+
export declare const ActiveDaysBucket: {
|
|
133
|
+
readonly None: '0';
|
|
134
|
+
readonly One: '1';
|
|
135
|
+
readonly TwoToFive: '2-5';
|
|
136
|
+
readonly SixToTwenty: '6-20';
|
|
137
|
+
readonly TwentyOneOrMore: '21+';
|
|
138
|
+
};
|
|
139
|
+
export type ActiveDaysBucket = (typeof ActiveDaysBucket)[keyof typeof ActiveDaysBucket];
|
|
140
|
+
/** What an uninstall page gets in its query string, shaped by the state as an envelope is. */
|
|
141
|
+
export interface UninstallParams extends ProductFields {
|
|
142
|
+
/** Days since install: exact up to 31 in the identified state, a bucket otherwise. */
|
|
143
|
+
age_days: number;
|
|
144
|
+
/** The bucket of the number of local days with an event. */
|
|
145
|
+
active_days: ActiveDaysBucket;
|
|
146
|
+
}
|
|
147
|
+
/** Every method resolves and never rejects. A failure goes only to `onError`. */
|
|
148
|
+
export interface Collector<M extends Mode = Mode> {
|
|
149
|
+
/**
|
|
150
|
+
* Queues the event for the current state. Refuses and reports an event the intake would drop for its form or a system
|
|
151
|
+
* event's params, and `installed`, `updated` and `opted_out`, which only the collector's own methods send. The intake
|
|
152
|
+
* drops an event whose string param looks like an address, and the product's allowlist, which the collector does not
|
|
153
|
+
* know, decides the rest. `onError` hears of neither drop.
|
|
154
|
+
*/
|
|
155
|
+
track(name: string, params?: EventParams): Promise<void>;
|
|
156
|
+
/** Sends `installed` once per install. In the opted_out state it sends nothing and marks nothing. */
|
|
157
|
+
installed(): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Sends `updated` when the version differs from the one `installed()` or `updated()` stored, or when none is stored.
|
|
160
|
+
* Call it after `installed()`. In the opted_out state it sends nothing and marks nothing.
|
|
161
|
+
*/
|
|
162
|
+
updated(): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Stores the user's choice and starts a new session. Switching to `opted_out` drops what was not sent, except an
|
|
165
|
+
* earlier `opted_out`, and sends one `opted_out`, at once or at the end of a retry wait that is running. Switching
|
|
166
|
+
* from `identified` drops the queued events that name the user. A dropped event that no request carried gives back
|
|
167
|
+
* its marks. The same state again does nothing.
|
|
168
|
+
*/
|
|
169
|
+
setState(state: StateOf<M>): Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* Resolves to the stored state, `anonymous` until the user chose. A storage failure resolves to `anonymous` too,
|
|
172
|
+
* whatever the user chose, and goes to `onError`.
|
|
173
|
+
*/
|
|
174
|
+
getState(): Promise<AnalyticsState>;
|
|
175
|
+
/**
|
|
176
|
+
* Sends what is queued now, or once the retry wait is over. A page calls it when it is hidden, since the user may not
|
|
177
|
+
* come back.
|
|
178
|
+
*/
|
|
179
|
+
flush(): Promise<void>;
|
|
180
|
+
/**
|
|
181
|
+
* Drops this id's queued events and resolves once this collector's request in progress ends. For the last id, it also
|
|
182
|
+
* drops the events that wait for an id, and the next id counts as another user. The host calls it once the user left
|
|
183
|
+
* the identified state or `getId` stopped giving the id, then asks the intake to forget the user. The intake also
|
|
184
|
+
* deletes the user's events that arrive within a minute after the forget, such as a request from another context.
|
|
185
|
+
*/
|
|
186
|
+
forget(id: string): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Resolves to the uninstall page's parameters. Resolves to undefined in the opted_out state, and when the storage
|
|
189
|
+
* fails.
|
|
190
|
+
*/
|
|
191
|
+
uninstallParams(): Promise<UninstallParams | undefined>;
|
|
192
|
+
}
|
|
193
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a channel may collect at most, as set in the intake's admin. The collector's mode matches its channel's. An
|
|
3
|
+
* identified channel lets a user who agrees be identified. An anonymous one never does.
|
|
4
|
+
*/
|
|
5
|
+
export const Mode = {
|
|
6
|
+
Anonymous: 'anonymous',
|
|
7
|
+
Identified: 'identified',
|
|
8
|
+
};
|
|
9
|
+
/** What the collector sends for this user, a choice the collector stores. Every user starts anonymous. */
|
|
10
|
+
export const AnalyticsState = {
|
|
11
|
+
/** Sends nothing. Switching to it sends one `opted_out`, with no id or browser, and drops what was not sent. */
|
|
12
|
+
OptedOut: 'opted_out',
|
|
13
|
+
/** Sends events with no id or browser, the primary languages only and coarse ages. Needs no consent. */
|
|
14
|
+
Anonymous: 'anonymous',
|
|
15
|
+
/** Sends the id from `getId`, the browser, the full languages and exact ages. Only an identified channel takes it. */
|
|
16
|
+
Identified: 'identified',
|
|
17
|
+
};
|
|
18
|
+
/** Bucket for the number of days with at least one event. */
|
|
19
|
+
export const ActiveDaysBucket = {
|
|
20
|
+
None: '0',
|
|
21
|
+
One: '1',
|
|
22
|
+
TwoToFive: '2-5',
|
|
23
|
+
SixToTwenty: '6-20',
|
|
24
|
+
TwentyOneOrMore: '21+',
|
|
25
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Collector, CommonOptions, Delivery, Mode, ModeOptions } from './collectorTypes.js';
|
|
2
|
+
import type { Platform } from './envelope.js';
|
|
3
|
+
import type { StorageAdapter } from './storage.js';
|
|
4
|
+
/**
|
|
5
|
+
* The options of `newDesktopCollector`: those of `newCollector` with `storage` optional and a desktop `platform`. An
|
|
6
|
+
* option left out or `undefined` takes its default.
|
|
7
|
+
*/
|
|
8
|
+
export type DesktopCollectorOptions = Omit<CommonOptions, 'storage'> & {
|
|
9
|
+
/** The operating system the app runs on, as the intake names it. */
|
|
10
|
+
platform: typeof Platform.MacOs | typeof Platform.Windows | typeof Platform.Linux;
|
|
11
|
+
/** The storage, the webview's `localStorage` by default. */
|
|
12
|
+
storage?: StorageAdapter | undefined;
|
|
13
|
+
/** A desktop product names no browser. */
|
|
14
|
+
browser?: never;
|
|
15
|
+
/** A desktop product sends no page. */
|
|
16
|
+
getPage?: never;
|
|
17
|
+
} & Delivery & ModeOptions;
|
|
18
|
+
/**
|
|
19
|
+
* Returns a collector for a window of a desktop app, such as a Tauri webview. Its windows share the webview's
|
|
20
|
+
* `localStorage` and Web Locks, so they keep one queue and one state. It sends the user's language from
|
|
21
|
+
* `navigator.language`. The app passes its own `uiLanguage`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function newDesktopCollector(options: DesktopCollectorOptions & {
|
|
24
|
+
mode: typeof Mode.Identified;
|
|
25
|
+
}): Collector<'identified'>;
|
|
26
|
+
export declare function newDesktopCollector(options: DesktopCollectorOptions & {
|
|
27
|
+
mode: typeof Mode.Anonymous;
|
|
28
|
+
}): Collector<'anonymous'>;
|
|
29
|
+
export declare function newDesktopCollector(options: DesktopCollectorOptions): Collector;
|
package/dist/desktop.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { newCollector } from './collector.js';
|
|
2
|
+
import { newLocalStorageAdapter, newWebLock } from './web.js';
|
|
3
|
+
export function newDesktopCollector(options) {
|
|
4
|
+
return newCollector({
|
|
5
|
+
...options,
|
|
6
|
+
storage: options.storage ?? newLocalStorageAdapter(),
|
|
7
|
+
lock: options.lock ?? newWebLock(),
|
|
8
|
+
browserLanguage: options.browserLanguage ?? navigator.language,
|
|
9
|
+
});
|
|
10
|
+
}
|
package/dist/envelope.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Browser } from './browser.js';
|
|
1
2
|
import type { Period } from './calendar.js';
|
|
2
3
|
/** The platform the product runs on, as the intake names it. */
|
|
3
4
|
export declare const Platform: {
|
|
@@ -8,15 +9,28 @@ export declare const Platform: {
|
|
|
8
9
|
readonly Web: 'web';
|
|
9
10
|
};
|
|
10
11
|
export type Platform = (typeof Platform)[keyof typeof Platform];
|
|
11
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* The events that only the collector sends: `installed` and `updated` from its own methods, and `opted_out` when the
|
|
14
|
+
* user turns analytics off.
|
|
15
|
+
*/
|
|
12
16
|
export declare const LifecycleEvent: {
|
|
13
17
|
readonly Installed: 'installed';
|
|
14
18
|
readonly Updated: 'updated';
|
|
15
|
-
readonly
|
|
16
|
-
readonly UsageCountsOff: 'usage_counts_off';
|
|
19
|
+
readonly OptedOut: 'opted_out';
|
|
17
20
|
};
|
|
18
21
|
export type LifecycleEvent = (typeof LifecycleEvent)[keyof typeof LifecycleEvent];
|
|
19
|
-
/**
|
|
22
|
+
/** The system event of a page view. Only the first one that a collector records carries the referrer. */
|
|
23
|
+
export declare const PAGE_VIEW = "page_view";
|
|
24
|
+
export declare const UNINSTALLED = "uninstalled";
|
|
25
|
+
/**
|
|
26
|
+
* The intake's `system_events`: the events it takes from every product without the product's allowlist. The collector
|
|
27
|
+
* sends the lifecycle ones, and the product's own pages and site send the rest.
|
|
28
|
+
*/
|
|
29
|
+
export declare const SYSTEM_EVENTS: readonly string[];
|
|
30
|
+
/**
|
|
31
|
+
* Event parameters. The intake drops the whole event when a parameter is off the product's allowlist, when an enum
|
|
32
|
+
* value is off its list, or when a string looks like an address, a domain or an IP.
|
|
33
|
+
*/
|
|
20
34
|
export type EventParams = Record<string, string | number | boolean>;
|
|
21
35
|
/** One event as the intake receives it, inside an envelope. */
|
|
22
36
|
export interface UsageEvent {
|
|
@@ -25,9 +39,9 @@ export interface UsageEvent {
|
|
|
25
39
|
day: string;
|
|
26
40
|
/** The periods, such as the day, in which this event is the user's first one. */
|
|
27
41
|
first?: Period[];
|
|
28
|
-
/** Days since install
|
|
42
|
+
/** Days since install, as a bucket without an `id`. Only the first event of a day carries it. */
|
|
29
43
|
age_days?: number;
|
|
30
|
-
/** Months since install
|
|
44
|
+
/** Months since install, as a bucket without an `id`. Only the first event of a month carries it. */
|
|
31
45
|
age_months?: number;
|
|
32
46
|
params?: EventParams;
|
|
33
47
|
page?: string;
|
|
@@ -38,7 +52,15 @@ export interface Envelope {
|
|
|
38
52
|
key: string;
|
|
39
53
|
platform: Platform;
|
|
40
54
|
version: string;
|
|
41
|
-
|
|
55
|
+
/** The user's language, such as `pt-BR`; only `pt` without an `id`. */
|
|
56
|
+
browser_language?: string;
|
|
57
|
+
/**
|
|
58
|
+
* The language the product shows its interface in. An envelope without an `id` carries only the primary language,
|
|
59
|
+
* such as `pt`.
|
|
60
|
+
*/
|
|
61
|
+
ui_language?: string;
|
|
62
|
+
/** Present only on a web or extension envelope that carries an `id`. */
|
|
63
|
+
browser?: Browser;
|
|
42
64
|
session: string;
|
|
43
65
|
id?: string;
|
|
44
66
|
/** Stays the same on each retry of this envelope. The intake keeps it once. */
|
package/dist/envelope.js
CHANGED
|
@@ -6,10 +6,26 @@ export const Platform = {
|
|
|
6
6
|
Linux: 'linux',
|
|
7
7
|
Web: 'web',
|
|
8
8
|
};
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* The events that only the collector sends: `installed` and `updated` from its own methods, and `opted_out` when the
|
|
11
|
+
* user turns analytics off.
|
|
12
|
+
*/
|
|
10
13
|
export const LifecycleEvent = {
|
|
11
14
|
Installed: 'installed',
|
|
12
15
|
Updated: 'updated',
|
|
13
|
-
|
|
14
|
-
UsageCountsOff: 'usage_counts_off',
|
|
16
|
+
OptedOut: 'opted_out',
|
|
15
17
|
};
|
|
18
|
+
/** The system event of a page view. Only the first one that a collector records carries the referrer. */
|
|
19
|
+
export const PAGE_VIEW = 'page_view';
|
|
20
|
+
export const UNINSTALLED = 'uninstalled';
|
|
21
|
+
/**
|
|
22
|
+
* The intake's `system_events`: the events it takes from every product without the product's allowlist. The collector
|
|
23
|
+
* sends the lifecycle ones, and the product's own pages and site send the rest.
|
|
24
|
+
*/
|
|
25
|
+
export const SYSTEM_EVENTS = [
|
|
26
|
+
...Object.values(LifecycleEvent),
|
|
27
|
+
UNINSTALLED,
|
|
28
|
+
'welcome_opened',
|
|
29
|
+
PAGE_VIEW,
|
|
30
|
+
'install_click',
|
|
31
|
+
];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type EventParams } from './envelope.js';
|
|
2
|
+
/**
|
|
3
|
+
* Tells why the intake would drop the event, or returns undefined when its form passes. The check refuses `installed`,
|
|
4
|
+
* `updated` and `opted_out`, because only the collector's own methods send them.
|
|
5
|
+
*/
|
|
6
|
+
export declare function eventProblem(name: unknown, params: unknown): string | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Returns a shallow copy of the own enumerable entries of object params, and other params as they are. The collector
|
|
9
|
+
* checks and queues the copy, so a later change the host makes to its object reaches neither. Throws what a getter or a
|
|
10
|
+
* proxy of the params throws.
|
|
11
|
+
*/
|
|
12
|
+
export declare function copyOfParams(params: EventParams | undefined): EventParams | undefined;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { ActiveDaysBucket } from './collectorTypes.js';
|
|
2
|
+
import { LifecycleEvent, SYSTEM_EVENTS } from './envelope.js';
|
|
3
|
+
import { quoted } from './hostInput.js';
|
|
4
|
+
/** The form the intake takes for an event name and a param name. */
|
|
5
|
+
const NAME_FORMAT = /^[a-z][a-z\d_]{0,39}$/;
|
|
6
|
+
const MAX_PARAMS = 16;
|
|
7
|
+
/** The length of a string param the intake takes, in characters. */
|
|
8
|
+
const MAX_PARAM_STRING_LENGTH = 64;
|
|
9
|
+
const LIFECYCLE_NAMES = new Set(Object.values(LifecycleEvent));
|
|
10
|
+
const ACTIVE_DAYS = Object.values(ActiveDaysBucket);
|
|
11
|
+
/**
|
|
12
|
+
* The params each system event takes, from the intake's `SystemEventParams`. A system event that this map leaves out
|
|
13
|
+
* takes no params.
|
|
14
|
+
*/
|
|
15
|
+
const SYSTEM_EVENT_PARAMS = new Map([
|
|
16
|
+
[
|
|
17
|
+
'uninstalled',
|
|
18
|
+
{
|
|
19
|
+
// The intake reads the value as whole days from 0. It drops the event when the value has another form.
|
|
20
|
+
age_days: (value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0
|
|
21
|
+
? undefined
|
|
22
|
+
: 'that is not a whole number from 0',
|
|
23
|
+
active_days: (value) => ACTIVE_DAYS.includes(value) ? undefined : `that is not one of ${ACTIVE_DAYS.join(', ')}`,
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
['install_click', { product: (value) => (typeof value === 'string' ? undefined : 'that is not a string') }],
|
|
27
|
+
]);
|
|
28
|
+
/**
|
|
29
|
+
* Tells why the intake would drop the event, or returns undefined when its form passes. The check refuses `installed`,
|
|
30
|
+
* `updated` and `opted_out`, because only the collector's own methods send them.
|
|
31
|
+
*/
|
|
32
|
+
export function eventProblem(name, params) {
|
|
33
|
+
if (typeof name !== 'string' || !NAME_FORMAT.test(name)) {
|
|
34
|
+
return 'has a name the intake does not take';
|
|
35
|
+
}
|
|
36
|
+
if (LIFECYCLE_NAMES.has(name)) {
|
|
37
|
+
return 'is one only the collector sends';
|
|
38
|
+
}
|
|
39
|
+
return paramsProblem(name, params);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns a shallow copy of the own enumerable entries of object params, and other params as they are. The collector
|
|
43
|
+
* checks and queues the copy, so a later change the host makes to its object reaches neither. Throws what a getter or a
|
|
44
|
+
* proxy of the params throws.
|
|
45
|
+
*/
|
|
46
|
+
export function copyOfParams(params) {
|
|
47
|
+
// An untyped host can pass null or a list. The function returns such params as they are, so the check refuses them.
|
|
48
|
+
const value = params;
|
|
49
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
50
|
+
? Object.fromEntries(Object.entries(value))
|
|
51
|
+
: params;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Tells why the intake would drop this event for its params, or returns undefined when it takes them. For an event that
|
|
55
|
+
* is not a system event, the intake's allowlist decides the rest. The collector does not know that list.
|
|
56
|
+
*/
|
|
57
|
+
function paramsProblem(name, params) {
|
|
58
|
+
if (params === undefined) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (typeof params !== 'object' || params === null || Array.isArray(params)) {
|
|
62
|
+
return 'has params that are not an object';
|
|
63
|
+
}
|
|
64
|
+
// The check counts the keys before it reads any value. A content script can send params with many keys, and this
|
|
65
|
+
// order keeps their cost low.
|
|
66
|
+
const keys = Object.keys(params);
|
|
67
|
+
if (keys.length > MAX_PARAMS) {
|
|
68
|
+
return `has more than ${MAX_PARAMS} params`;
|
|
69
|
+
}
|
|
70
|
+
for (const key of keys) {
|
|
71
|
+
const rule = paramRule(name, key, Reflect.get(params, key));
|
|
72
|
+
if (rule !== undefined) {
|
|
73
|
+
return `has a param ${quoted(key)} ${rule}`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Tells which rule of the intake one param breaks, in the order the intake checks them, or returns undefined when it
|
|
80
|
+
* keeps them.
|
|
81
|
+
*/
|
|
82
|
+
function paramRule(name, key, value) {
|
|
83
|
+
if (!NAME_FORMAT.test(key)) {
|
|
84
|
+
return 'whose name is not a lowercase letter, then lowercase letters, digits and _, up to 40 characters';
|
|
85
|
+
}
|
|
86
|
+
if (SYSTEM_EVENTS.includes(name)) {
|
|
87
|
+
const rule = SYSTEM_EVENT_PARAMS.get(name)?.[key];
|
|
88
|
+
if (rule === undefined) {
|
|
89
|
+
return `that ${name} does not take`;
|
|
90
|
+
}
|
|
91
|
+
return rule(value) ?? valueRule(value);
|
|
92
|
+
}
|
|
93
|
+
return valueRule(value);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Tells which rule of every param the value breaks, or returns undefined when it keeps them. The check refuses a number
|
|
97
|
+
* that is not finite, because JSON sends it as null.
|
|
98
|
+
*/
|
|
99
|
+
function valueRule(value) {
|
|
100
|
+
if (typeof value === 'string') {
|
|
101
|
+
// A string longer than twice the limit in UTF-16 units has more characters than the limit, so the check does not
|
|
102
|
+
// count its characters.
|
|
103
|
+
return value.length > 2 * MAX_PARAM_STRING_LENGTH || Array.from(value).length > MAX_PARAM_STRING_LENGTH
|
|
104
|
+
? `past ${MAX_PARAM_STRING_LENGTH} characters`
|
|
105
|
+
: undefined;
|
|
106
|
+
}
|
|
107
|
+
return typeof value === 'boolean' || Number.isFinite(value)
|
|
108
|
+
? undefined
|
|
109
|
+
: 'that is not a string, a boolean or a finite number';
|
|
110
|
+
}
|
package/dist/exports.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { detectBrowser, BrowserName, type Browser, type NavigatorFields } from './browser.js';
|
|
2
|
+
export { newCollector } from './collector.js';
|
|
3
|
+
export { ActiveDaysBucket, AnalyticsState, Mode, type Collector, type CollectorOptions, type CommonOptions, type Delivery, type GetId, type ModeOptions, type PageFields, type PageOptions, type StateOf, type UninstallParams, } from './collectorTypes.js';
|
|
2
4
|
export { Platform, type EventParams } from './envelope.js';
|
|
3
5
|
export { newMemoryStorage, type StorageAdapter } from './storage.js';
|
|
4
|
-
export { newHttpTransport, type Transport } from './transport.js';
|
|
6
|
+
export { newHttpTransport, type Transport, type TransportResult } from './transport.js';
|
|
5
7
|
export type { Lock } from './lock.js';
|
|
6
8
|
export { systemClock, type Clock, type TimerHandle } from './clock.js';
|
package/dist/exports.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { detectBrowser, BrowserName } from './browser.js';
|
|
2
|
+
export { newCollector } from './collector.js';
|
|
3
|
+
export { ActiveDaysBucket, AnalyticsState, Mode, } from './collectorTypes.js';
|
|
2
4
|
export { Platform } from './envelope.js';
|
|
3
5
|
export { newMemoryStorage } from './storage.js';
|
|
4
6
|
export { newHttpTransport } from './transport.js';
|
package/dist/extension.d.ts
CHANGED
|
@@ -1,7 +1,31 @@
|
|
|
1
1
|
import { type Clock } from './clock.js';
|
|
2
|
-
import type
|
|
2
|
+
import { Mode, type Collector, type CommonOptions, type Delivery, type ModeOptions } from './collectorTypes.js';
|
|
3
3
|
import { type EventParams } from './envelope.js';
|
|
4
4
|
import type { StorageAdapter } from './storage.js';
|
|
5
|
+
/**
|
|
6
|
+
* The options of `newExtensionCollector`: those of `newCollector` without `platform`, with `storage` and `version`
|
|
7
|
+
* optional. An option left out or `undefined` takes its default.
|
|
8
|
+
*/
|
|
9
|
+
export type ExtensionCollectorOptions = Omit<CommonOptions, 'storage' | 'version'> & {
|
|
10
|
+
/** The storage, `chrome.storage.local` by default. */
|
|
11
|
+
storage?: StorageAdapter | undefined;
|
|
12
|
+
/** The product version, the manifest's by default. */
|
|
13
|
+
version?: string | undefined;
|
|
14
|
+
/** An extension sends no page. */
|
|
15
|
+
getPage?: never;
|
|
16
|
+
} & Delivery & ModeOptions;
|
|
17
|
+
/**
|
|
18
|
+
* Returns a collector for an extension's worker or page. It needs the `storage` permission. In the worker it first
|
|
19
|
+
* closes the storage to content scripts, which could otherwise change the user's choice and the queue. The product
|
|
20
|
+
* builds it at the worker's top level, so the call runs at each worker start.
|
|
21
|
+
*/
|
|
22
|
+
export declare function newExtensionCollector(options: ExtensionCollectorOptions & {
|
|
23
|
+
mode: typeof Mode.Identified;
|
|
24
|
+
}): Collector<'identified'>;
|
|
25
|
+
export declare function newExtensionCollector(options: ExtensionCollectorOptions & {
|
|
26
|
+
mode: typeof Mode.Anonymous;
|
|
27
|
+
}): Collector<'anonymous'>;
|
|
28
|
+
export declare function newExtensionCollector(options: ExtensionCollectorOptions): Collector;
|
|
5
29
|
/**
|
|
6
30
|
* Returns a storage adapter over `chrome.storage.local`, which the service worker and the extension pages share. It
|
|
7
31
|
* needs the `storage` permission.
|
|
@@ -12,16 +36,29 @@ export declare function newChromeStorageAdapter(): StorageAdapter;
|
|
|
12
36
|
* script's own requests, which carry the page's origin.
|
|
13
37
|
*/
|
|
14
38
|
export declare function forwardTrack(name: string, params?: EventParams): Promise<void>;
|
|
39
|
+
/** The part of the collector a content script uses: it tracks events, and the worker keeps the user's choice. */
|
|
40
|
+
export type ContentScriptCollector = Pick<Collector, 'track' | 'flush'>;
|
|
41
|
+
/**
|
|
42
|
+
* Returns the collector of a content script. Its `track` hands each event to the service worker with `forwardTrack`,
|
|
43
|
+
* and the worker's collector checks, queues and sends it under the user's choice, so `flush` does nothing here. The
|
|
44
|
+
* user's choice lives in the worker and the extension pages, so a content script can neither read nor change it. It
|
|
45
|
+
* reads no storage, needs no key, version or endpoint, and never throws.
|
|
46
|
+
*/
|
|
47
|
+
export declare function newContentScriptCollector(): ContentScriptCollector;
|
|
15
48
|
/**
|
|
16
|
-
* Tracks the events content scripts forward with `forwardTrack`, at most 60
|
|
17
|
-
*
|
|
18
|
-
* the function that stops it.
|
|
49
|
+
* Tracks the events content scripts forward with `forwardTrack`, at most 60 a minute. It takes them only from this
|
|
50
|
+
* extension, and only the product's own events: never a system event of the intake, such as `installed` or
|
|
51
|
+
* `uninstalled`, which a content script has no reason to send. Returns the function that stops it.
|
|
19
52
|
*/
|
|
20
53
|
export declare function acceptForwarded(collector: Collector, { clock }?: {
|
|
21
54
|
clock?: Clock;
|
|
22
55
|
}): () => void;
|
|
23
56
|
/**
|
|
24
|
-
* Sets the uninstall page with `uninstallParams()`
|
|
25
|
-
*
|
|
57
|
+
* Sets the uninstall page to `pageUrl` with the fields of `uninstallParams()` added to its query, or to `pageUrl` alone
|
|
58
|
+
* when `uninstallParams()` resolves to undefined. Chrome keeps the address as it was set, so the service worker calls
|
|
59
|
+
* this on each start and after each change of state. It resolves and never rejects. A failure of `uninstallParams()`
|
|
60
|
+
* goes to the collector's `onError`, and any other failure to its own `onError`.
|
|
26
61
|
*/
|
|
27
|
-
export declare function refreshUninstallUrl(collector: Collector, pageUrl: string
|
|
62
|
+
export declare function refreshUninstallUrl(collector: Collector, pageUrl: string, { onError }?: {
|
|
63
|
+
onError?: (error: Error) => void;
|
|
64
|
+
}): Promise<void>;
|