@kk-forge/artel-collector 0.3.0 → 0.4.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.
@@ -1,31 +1,41 @@
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';
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
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.
9
+ * What a channel may collect at most, as set in the intake's admin. The collector's mode matches its channel's. Each
10
+ * mode takes the states up to its own: a pseudonymous channel adds the install id, and an identified one the account
11
+ * id.
11
12
  */
12
13
  export declare const Mode: {
13
- readonly Anonymous: 'anonymous';
14
- readonly Identified: 'identified';
14
+ readonly Anonymous: "anonymous";
15
+ readonly Pseudonymous: "pseudonymous";
16
+ readonly Identified: "identified";
15
17
  };
16
18
  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. */
19
+ /** The user's analytics state, which the collector stores and which decides what it sends. Users start anonymous. */
18
20
  export declare const AnalyticsState: {
19
21
  /** 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';
22
+ readonly OptedOut: "opted_out";
21
23
  /** 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';
24
+ readonly Anonymous: "anonymous";
25
+ /**
26
+ * Sends a random install id the collector makes and stores, the browser, the full languages and exact ages. Needs
27
+ * consent. A pseudonymous or identified channel takes it.
28
+ */
29
+ readonly Pseudonymous: "pseudonymous";
30
+ /**
31
+ * Sends what the pseudonymous state sends, with the id from `getId` in place of the install id. Needs consent. Only
32
+ * an identified channel takes it.
33
+ */
34
+ readonly Identified: "identified";
25
35
  };
26
36
  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>;
37
+ /** The states a collector of channel mode `M` takes: those up to its mode. */
38
+ export type StateOf<M extends Mode> = M extends typeof Mode.Identified ? AnalyticsState : M extends typeof Mode.Pseudonymous ? Exclude<AnalyticsState, typeof AnalyticsState.Identified> : Exclude<AnalyticsState, typeof AnalyticsState.Pseudonymous | typeof AnalyticsState.Identified>;
29
39
  /**
30
40
  * Returns the user's id, or undefined while the product does not know it yet. The collector never makes one up. The
31
41
  * collector calls it while it holds its lock, so it must not await a method of the same collector, which would wait on
@@ -76,20 +86,25 @@ export type Delivery = {
76
86
  transport: Transport;
77
87
  endpoint?: never;
78
88
  };
79
- /** The channel mode, with what an identified channel takes besides. */
89
+ /**
90
+ * The browser the product runs in, or a function that returns it, such as `() => detectBrowser(navigator)`. The
91
+ * collector calls the function once, for the first event with an id, the only kind of envelope that carries it.
92
+ */
93
+ type BrowserOption = Browser | (() => Browser | undefined) | undefined;
94
+ /** The channel mode, with what a channel with an id takes besides. */
80
95
  export type ModeOptions = {
81
96
  mode: typeof Mode.Anonymous;
82
97
  getId?: never;
83
98
  browser?: never;
99
+ } | {
100
+ mode: typeof Mode.Pseudonymous;
101
+ getId?: never;
102
+ browser?: BrowserOption;
84
103
  } | {
85
104
  mode: typeof Mode.Identified;
86
105
  /** Gives the id in the identified state. The collector calls it in no other state. */
87
106
  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;
107
+ browser?: BrowserOption;
93
108
  };
94
109
  /** The options only a site's collector takes, since only platform web sends pages. */
95
110
  export interface PageOptions {
@@ -130,29 +145,38 @@ export interface PageFields {
130
145
  }
131
146
  /** Bucket for the number of days with at least one event. */
132
147
  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+';
148
+ readonly None: "0";
149
+ readonly One: "1";
150
+ readonly TwoToFive: "2-5";
151
+ readonly SixToTwenty: "6-20";
152
+ readonly TwentyOneOrMore: "21+";
138
153
  };
139
154
  export type ActiveDaysBucket = (typeof ActiveDaysBucket)[keyof typeof ActiveDaysBucket];
140
155
  /** What an uninstall page gets in its query string, shaped by the state as an envelope is. */
141
156
  export interface UninstallParams extends ProductFields {
142
- /** Days since install: exact up to 31 in the identified state, a bucket otherwise. */
157
+ /** Days since install: exact up to 31 in a state with an id, a bucket otherwise. */
143
158
  age_days: number;
144
159
  /** The bucket of the number of local days with an event. */
145
160
  active_days: ActiveDaysBucket;
146
161
  }
162
+ export interface TrackOptions {
163
+ /**
164
+ * Drops this call while a call of the same name with `throttleMs` went less than this many milliseconds before, as
165
+ * for a held key that repeats. The first call of a series goes. A call without `throttleMs` opens no window. The
166
+ * collector keeps the time in memory, so each collector keeps its own.
167
+ */
168
+ throttleMs?: number | undefined;
169
+ }
147
170
  /** Every method resolves and never rejects. A failure goes only to `onError`. */
148
171
  export interface Collector<M extends Mode = Mode> {
149
172
  /**
150
173
  * Queues the event for the current state. Refuses and reports an event the intake would drop for its form or a system
151
174
  * event's params, and `installed`, `updated` and `opted_out`, which only the collector's own methods send. The intake
152
175
  * 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.
176
+ * know, decides the rest. `onError` hears of neither drop, nor of a call that `throttleMs` drops. A `throttleMs` that
177
+ * is no finite number from 0 refuses the event and reports it to `onError`.
154
178
  */
155
- track(name: string, params?: EventParams): Promise<void>;
179
+ track(name: string, params?: EventParams, options?: TrackOptions): Promise<void>;
156
180
  /** Sends `installed` once per install. In the opted_out state it sends nothing and marks nothing. */
157
181
  installed(): Promise<void>;
158
182
  /**
@@ -161,10 +185,11 @@ export interface Collector<M extends Mode = Mode> {
161
185
  */
162
186
  updated(): Promise<void>;
163
187
  /**
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.
188
+ * Stores the user's analytics state and starts a new session. Switching to `opted_out` drops what was not sent,
189
+ * except an earlier `opted_out`, and sends one `opted_out`, at once or at the end of a retry wait that is running.
190
+ * Switching from `pseudonymous` or `identified` forgets the last id, as `forget` does, and drops the queued events
191
+ * that wait for an id. Switching from `pseudonymous` also deletes the install id. A dropped event that no request
192
+ * carried gives back its marks. The same state again does nothing.
168
193
  */
169
194
  setState(state: StateOf<M>): Promise<void>;
170
195
  /**
@@ -173,15 +198,15 @@ export interface Collector<M extends Mode = Mode> {
173
198
  */
174
199
  getState(): Promise<AnalyticsState>;
175
200
  /**
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.
201
+ * Sends what is queued now, or once the retry wait is over. The collector sends within the send delay without it, so a
202
+ * product calls it only to send sooner.
178
203
  */
179
204
  flush(): Promise<void>;
180
205
  /**
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.
206
+ * Drops this id's queued events and asks the intake to delete the id's events, after this collector's request in
207
+ * progress. For the last id, it also drops the events that wait for an id, and the next id counts as another user.
208
+ * The collector stores the request until the intake answers it. The intake also deletes the user's events that arrive
209
+ * within a minute after the forget, such as a request from another context.
185
210
  */
186
211
  forget(id: string): Promise<void>;
187
212
  /**
@@ -1,25 +1,35 @@
1
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.
2
+ * What a channel may collect at most, as set in the intake's admin. The collector's mode matches its channel's. Each
3
+ * mode takes the states up to its own: a pseudonymous channel adds the install id, and an identified one the account
4
+ * id.
4
5
  */
5
6
  export const Mode = {
6
- Anonymous: 'anonymous',
7
- Identified: 'identified',
7
+ Anonymous: "anonymous",
8
+ Pseudonymous: "pseudonymous",
9
+ Identified: "identified",
8
10
  };
9
- /** What the collector sends for this user, a choice the collector stores. Every user starts anonymous. */
11
+ /** The user's analytics state, which the collector stores and which decides what it sends. Users start anonymous. */
10
12
  export const AnalyticsState = {
11
13
  /** Sends nothing. Switching to it sends one `opted_out`, with no id or browser, and drops what was not sent. */
12
- OptedOut: 'opted_out',
14
+ OptedOut: "opted_out",
13
15
  /** 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',
16
+ Anonymous: "anonymous",
17
+ /**
18
+ * Sends a random install id the collector makes and stores, the browser, the full languages and exact ages. Needs
19
+ * consent. A pseudonymous or identified channel takes it.
20
+ */
21
+ Pseudonymous: "pseudonymous",
22
+ /**
23
+ * Sends what the pseudonymous state sends, with the id from `getId` in place of the install id. Needs consent. Only
24
+ * an identified channel takes it.
25
+ */
26
+ Identified: "identified",
17
27
  };
18
28
  /** Bucket for the number of days with at least one event. */
19
29
  export const ActiveDaysBucket = {
20
- None: '0',
21
- One: '1',
22
- TwoToFive: '2-5',
23
- SixToTwenty: '6-20',
24
- TwentyOneOrMore: '21+',
30
+ None: "0",
31
+ One: "1",
32
+ TwoToFive: "2-5",
33
+ SixToTwenty: "6-20",
34
+ TwentyOneOrMore: "21+",
25
35
  };
package/dist/desktop.d.ts CHANGED
@@ -1,11 +1,11 @@
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';
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
4
  /**
5
5
  * The options of `newDesktopCollector`: those of `newCollector` with `storage` optional and a desktop `platform`. An
6
6
  * option left out or `undefined` takes its default.
7
7
  */
8
- export type DesktopCollectorOptions = Omit<CommonOptions, 'storage'> & {
8
+ export type DesktopCollectorOptions = Omit<CommonOptions, "storage"> & {
9
9
  /** The operating system the app runs on, as the intake names it. */
10
10
  platform: typeof Platform.MacOs | typeof Platform.Windows | typeof Platform.Linux;
11
11
  /** The storage, the webview's `localStorage` by default. */
@@ -18,12 +18,9 @@ export type DesktopCollectorOptions = Omit<CommonOptions, 'storage'> & {
18
18
  /**
19
19
  * Returns a collector for a window of a desktop app, such as a Tauri webview. Its windows share the webview's
20
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`.
21
+ * `navigator.language`. The app passes its own `uiLanguage`. It sends `installed` and `updated` itself when they are
22
+ * due.
22
23
  */
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;
24
+ export declare function newDesktopCollector<M extends Mode>(options: DesktopCollectorOptions & {
25
+ mode: M;
26
+ }): Collector<M>;
package/dist/desktop.js CHANGED
@@ -1,10 +1,15 @@
1
- import { newCollector } from './collector.js';
2
- import { newLocalStorageAdapter, newWebLock } from './web.js';
1
+ import { newCollector } from "./collector.js";
2
+ import { newLocalStorageAdapter, newWebLock } from "./web.js";
3
3
  export function newDesktopCollector(options) {
4
- return newCollector({
4
+ const collector = newCollector({
5
5
  ...options,
6
6
  storage: options.storage ?? newLocalStorageAdapter(),
7
7
  lock: options.lock ?? newWebLock(),
8
8
  browserLanguage: options.browserLanguage ?? navigator.language,
9
9
  });
10
+ // A desktop app has no install event, so each start marks the install and the version. Both send once under the
11
+ // lock, so every window may build a collector. The collector runs its calls in this order.
12
+ void collector.installed();
13
+ void collector.updated();
14
+ return collector;
10
15
  }
@@ -1,12 +1,12 @@
1
- import type { Browser } from './browser.js';
2
- import type { Period } from './calendar.js';
1
+ import type { Browser } from "./browser.js";
2
+ import type { Period } from "./calendar.js";
3
3
  /** The platform the product runs on, as the intake names it. */
4
4
  export declare const Platform: {
5
- readonly Extension: 'extension';
6
- readonly MacOs: 'macos';
7
- readonly Windows: 'windows';
8
- readonly Linux: 'linux';
9
- readonly Web: 'web';
5
+ readonly Extension: "extension";
6
+ readonly MacOs: "macos";
7
+ readonly Windows: "windows";
8
+ readonly Linux: "linux";
9
+ readonly Web: "web";
10
10
  };
11
11
  export type Platform = (typeof Platform)[keyof typeof Platform];
12
12
  /**
@@ -14,9 +14,9 @@ export type Platform = (typeof Platform)[keyof typeof Platform];
14
14
  * user turns analytics off.
15
15
  */
16
16
  export declare const LifecycleEvent: {
17
- readonly Installed: 'installed';
18
- readonly Updated: 'updated';
19
- readonly OptedOut: 'opted_out';
17
+ readonly Installed: "installed";
18
+ readonly Updated: "updated";
19
+ readonly OptedOut: "opted_out";
20
20
  };
21
21
  export type LifecycleEvent = (typeof LifecycleEvent)[keyof typeof LifecycleEvent];
22
22
  /** The system event of a page view. Only the first one that a collector records carries the referrer. */
@@ -47,6 +47,8 @@ export interface UsageEvent {
47
47
  page?: string;
48
48
  referrer?: string;
49
49
  }
50
+ /** Where an envelope's `id` comes from: the install id the collector makes, or the account id from `getId`. */
51
+ export type IdKind = "pseudonymous" | "identified";
50
52
  /** What one request to the intake carries. */
51
53
  export interface Envelope {
52
54
  key: string;
@@ -63,6 +65,8 @@ export interface Envelope {
63
65
  browser?: Browser;
64
66
  session: string;
65
67
  id?: string;
68
+ /** Present exactly when `id` is. The intake keeps the id only when its kind is within the channel's mode. */
69
+ id_kind?: IdKind;
66
70
  /** Stays the same on each retry of this envelope. The intake keeps it once. */
67
71
  batch?: string;
68
72
  events: UsageEvent[];
package/dist/envelope.js CHANGED
@@ -1,23 +1,23 @@
1
1
  /** The platform the product runs on, as the intake names it. */
2
2
  export const Platform = {
3
- Extension: 'extension',
4
- MacOs: 'macos',
5
- Windows: 'windows',
6
- Linux: 'linux',
7
- Web: 'web',
3
+ Extension: "extension",
4
+ MacOs: "macos",
5
+ Windows: "windows",
6
+ Linux: "linux",
7
+ Web: "web",
8
8
  };
9
9
  /**
10
10
  * The events that only the collector sends: `installed` and `updated` from its own methods, and `opted_out` when the
11
11
  * user turns analytics off.
12
12
  */
13
13
  export const LifecycleEvent = {
14
- Installed: 'installed',
15
- Updated: 'updated',
16
- OptedOut: 'opted_out',
14
+ Installed: "installed",
15
+ Updated: "updated",
16
+ OptedOut: "opted_out",
17
17
  };
18
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';
19
+ export const PAGE_VIEW = "page_view";
20
+ export const UNINSTALLED = "uninstalled";
21
21
  /**
22
22
  * The intake's `system_events`: the events it takes from every product without the product's allowlist. The collector
23
23
  * sends the lifecycle ones, and the product's own pages and site send the rest.
@@ -25,7 +25,7 @@ export const UNINSTALLED = 'uninstalled';
25
25
  export const SYSTEM_EVENTS = [
26
26
  ...Object.values(LifecycleEvent),
27
27
  UNINSTALLED,
28
- 'welcome_opened',
28
+ "welcome_opened",
29
29
  PAGE_VIEW,
30
- 'install_click',
30
+ "install_click",
31
31
  ];
@@ -1,12 +1,22 @@
1
- import { type EventParams } from './envelope.js';
1
+ import { type TrackOptions } from "./collectorTypes.js";
2
+ import { type EventParams } from "./envelope.js";
2
3
  /**
3
4
  * 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
+ * `updated` and `opted_out`, because only the collector's own methods send them. It never throws: for params whose
6
+ * getter or proxy throws, it returns that it could not read them.
5
7
  */
6
- export declare function eventProblem(name: unknown, params: unknown): string | undefined;
8
+ export declare function checkEvent(name: unknown, params: unknown): string | undefined;
7
9
  /**
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.
10
+ * Returns a copy of the params of an event `track` takes, with the `throttleMs` of its options, or reports why it
11
+ * refuses the event and returns undefined. The copy is taken once, so a later change the host makes to its object
12
+ * reaches neither the check nor the event.
11
13
  */
12
- export declare function copyOfParams(params: EventParams | undefined): EventParams | undefined;
14
+ export declare function acceptedEvent({ name, given, options, reportError, }: {
15
+ name: string;
16
+ given: EventParams | undefined;
17
+ options: TrackOptions | undefined;
18
+ reportError: (error: unknown) => void;
19
+ }): {
20
+ params: EventParams | undefined;
21
+ throttleMs: number | undefined;
22
+ } | undefined;
@@ -1,11 +1,13 @@
1
- import { ActiveDaysBucket } from './collectorTypes.js';
2
- import { LifecycleEvent, SYSTEM_EVENTS } from './envelope.js';
3
- import { quoted } from './hostInput.js';
1
+ import { ActiveDaysBucket } from "./collectorTypes.js";
2
+ import { LifecycleEvent, SYSTEM_EVENTS } from "./envelope.js";
3
+ import { quoted } from "./hostInput.js";
4
+ import { throttleOf } from "./throttle.js";
4
5
  /** The form the intake takes for an event name and a param name. */
5
6
  const NAME_FORMAT = /^[a-z][a-z\d_]{0,39}$/;
6
7
  const MAX_PARAMS = 16;
7
8
  /** The length of a string param the intake takes, in characters. */
8
9
  const MAX_PARAM_STRING_LENGTH = 64;
10
+ const UNREADABLE_PARAMS = "has params that could not be read";
9
11
  const LIFECYCLE_NAMES = new Set(Object.values(LifecycleEvent));
10
12
  const ACTIVE_DAYS = Object.values(ActiveDaysBucket);
11
13
  /**
@@ -14,39 +16,77 @@ const ACTIVE_DAYS = Object.values(ActiveDaysBucket);
14
16
  */
15
17
  const SYSTEM_EVENT_PARAMS = new Map([
16
18
  [
17
- 'uninstalled',
19
+ "uninstalled",
18
20
  {
19
21
  // 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
22
+ age_days: (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0
21
23
  ? 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
+ : "that is not a whole number from 0",
25
+ active_days: (value) => ACTIVE_DAYS.includes(value) ? undefined : `that is not one of ${ACTIVE_DAYS.join(", ")}`,
26
+ },
27
+ ],
28
+ [
29
+ "install_click",
30
+ {
31
+ product: (value) => (typeof value === "string" ? undefined : "that is not a string"),
24
32
  },
25
33
  ],
26
- ['install_click', { product: (value) => (typeof value === 'string' ? undefined : 'that is not a string') }],
27
34
  ]);
28
35
  /**
29
36
  * 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.
37
+ * `updated` and `opted_out`, because only the collector's own methods send them. It never throws: for params whose
38
+ * getter or proxy throws, it returns that it could not read them.
31
39
  */
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';
40
+ export function checkEvent(name, params) {
41
+ if (typeof name !== "string" || !NAME_FORMAT.test(name)) {
42
+ return "has a name the intake does not take";
35
43
  }
36
44
  if (LIFECYCLE_NAMES.has(name)) {
37
- return 'is one only the collector sends';
45
+ return "is one only the collector sends";
46
+ }
47
+ try {
48
+ return paramsProblem(name, params);
49
+ }
50
+ catch {
51
+ return UNREADABLE_PARAMS;
52
+ }
53
+ }
54
+ /**
55
+ * Returns a copy of the params of an event `track` takes, with the `throttleMs` of its options, or reports why it
56
+ * refuses the event and returns undefined. The copy is taken once, so a later change the host makes to its object
57
+ * reaches neither the check nor the event.
58
+ */
59
+ export function acceptedEvent({ name, given, options, reportError, }) {
60
+ const refuse = (problem, cause) => {
61
+ const message = `artel: event ${quoted(name)} ${problem}, so it was not tracked`;
62
+ reportError(cause === undefined ? new Error(message) : new Error(message, { cause }));
63
+ return undefined;
64
+ };
65
+ let params;
66
+ try {
67
+ params = copyOfParams(given);
68
+ }
69
+ catch (error) {
70
+ return refuse(UNREADABLE_PARAMS, error);
71
+ }
72
+ const problem = checkEvent(name, params);
73
+ if (problem !== undefined) {
74
+ return refuse(problem);
38
75
  }
39
- return paramsProblem(name, params);
76
+ const throttle = throttleOf(options);
77
+ return throttle === undefined
78
+ ? refuse("has a throttleMs that is not a finite number from 0")
79
+ : { params, throttleMs: throttle.throttleMs };
40
80
  }
41
81
  /**
42
82
  * Returns a shallow copy of the own enumerable entries of object params, and other params as they are. The collector
43
83
  * checks and queues the copy, so a later change the host makes to its object reaches neither. Throws what a getter or a
44
84
  * proxy of the params throws.
45
85
  */
46
- export function copyOfParams(params) {
86
+ function copyOfParams(params) {
47
87
  // An untyped host can pass null or a list. The function returns such params as they are, so the check refuses them.
48
88
  const value = params;
49
- return typeof value === 'object' && value !== null && !Array.isArray(value)
89
+ return typeof value === "object" && value !== null && !Array.isArray(value)
50
90
  ? Object.fromEntries(Object.entries(value))
51
91
  : params;
52
92
  }
@@ -58,8 +98,8 @@ function paramsProblem(name, params) {
58
98
  if (params === undefined) {
59
99
  return undefined;
60
100
  }
61
- if (typeof params !== 'object' || params === null || Array.isArray(params)) {
62
- return 'has params that are not an object';
101
+ if (typeof params !== "object" || params === null || Array.isArray(params)) {
102
+ return "has params that are not an object";
63
103
  }
64
104
  // The check counts the keys before it reads any value. A content script can send params with many keys, and this
65
105
  // order keeps their cost low.
@@ -81,7 +121,7 @@ function paramsProblem(name, params) {
81
121
  */
82
122
  function paramRule(name, key, value) {
83
123
  if (!NAME_FORMAT.test(key)) {
84
- return 'whose name is not a lowercase letter, then lowercase letters, digits and _, up to 40 characters';
124
+ return "whose name is not a lowercase letter, then lowercase letters, digits and _, up to 40 characters";
85
125
  }
86
126
  if (SYSTEM_EVENTS.includes(name)) {
87
127
  const rule = SYSTEM_EVENT_PARAMS.get(name)?.[key];
@@ -97,14 +137,15 @@ function paramRule(name, key, value) {
97
137
  * that is not finite, because JSON sends it as null.
98
138
  */
99
139
  function valueRule(value) {
100
- if (typeof value === 'string') {
140
+ if (typeof value === "string") {
101
141
  // A string longer than twice the limit in UTF-16 units has more characters than the limit, so the check does not
102
142
  // count its characters.
103
- return value.length > 2 * MAX_PARAM_STRING_LENGTH || Array.from(value).length > MAX_PARAM_STRING_LENGTH
143
+ return value.length > 2 * MAX_PARAM_STRING_LENGTH ||
144
+ Array.from(value).length > MAX_PARAM_STRING_LENGTH
104
145
  ? `past ${MAX_PARAM_STRING_LENGTH} characters`
105
146
  : undefined;
106
147
  }
107
- return typeof value === 'boolean' || Number.isFinite(value)
148
+ return typeof value === "boolean" || Number.isFinite(value)
108
149
  ? undefined
109
- : 'that is not a string, a boolean or a finite number';
150
+ : "that is not a string, a boolean or a finite number";
110
151
  }
package/dist/exports.d.ts CHANGED
@@ -1,8 +1,10 @@
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';
4
- export { Platform, type EventParams } from './envelope.js';
5
- export { newMemoryStorage, type StorageAdapter } from './storage.js';
6
- export { newHttpTransport, type Transport, type TransportResult } from './transport.js';
7
- export type { Lock } from './lock.js';
8
- export { systemClock, type Clock, type TimerHandle } from './clock.js';
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 TrackOptions, type UninstallParams, } from "./collectorTypes.js";
4
+ export { Platform, type EventParams } from "./envelope.js";
5
+ export { checkEvent } from "./eventCheck.js";
6
+ export { checkOptions, isTestKey } from "./hostInput.js";
7
+ export { newMemoryStorage, type StorageAdapter } from "./storage.js";
8
+ export { newHttpTransport, type Transport, type TransportResult } from "./transport.js";
9
+ export type { Lock } from "./lock.js";
10
+ export { systemClock, type Clock, type TimerHandle } from "./clock.js";
package/dist/exports.js CHANGED
@@ -1,7 +1,9 @@
1
- export { detectBrowser, BrowserName } from './browser.js';
2
- export { newCollector } from './collector.js';
3
- export { ActiveDaysBucket, AnalyticsState, Mode, } from './collectorTypes.js';
4
- export { Platform } from './envelope.js';
5
- export { newMemoryStorage } from './storage.js';
6
- export { newHttpTransport } from './transport.js';
7
- export { systemClock } from './clock.js';
1
+ export { detectBrowser, BrowserName } from "./browser.js";
2
+ export { newCollector } from "./collector.js";
3
+ export { ActiveDaysBucket, AnalyticsState, Mode, } from "./collectorTypes.js";
4
+ export { Platform } from "./envelope.js";
5
+ export { checkEvent } from "./eventCheck.js";
6
+ export { checkOptions, isTestKey } from "./hostInput.js";
7
+ export { newMemoryStorage } from "./storage.js";
8
+ export { newHttpTransport } from "./transport.js";
9
+ export { systemClock } from "./clock.js";