@kk-forge/artel-collector 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -24
- package/dist/browser.d.ts +8 -8
- package/dist/browser.js +23 -21
- package/dist/calendar.d.ts +5 -5
- package/dist/calendar.js +7 -7
- package/dist/clock.d.ts +4 -4
- package/dist/clock.js +4 -2
- package/dist/collector.d.ts +7 -2
- package/dist/collector.js +85 -49
- package/dist/collectorTypes.d.ts +30 -21
- package/dist/collectorTypes.js +12 -12
- package/dist/desktop.d.ts +4 -4
- package/dist/desktop.js +2 -2
- package/dist/envelope.d.ts +11 -11
- package/dist/envelope.js +12 -12
- package/dist/eventCheck.d.ts +14 -5
- package/dist/eventCheck.js +50 -29
- package/dist/exports.d.ts +10 -10
- package/dist/exports.js +9 -9
- package/dist/extension.d.ts +15 -11
- package/dist/extension.js +120 -61
- package/dist/fields.d.ts +3 -3
- package/dist/fields.js +7 -7
- package/dist/hostInput.d.ts +5 -5
- package/dist/hostInput.js +26 -20
- package/dist/installState.d.ts +4 -4
- package/dist/installState.js +28 -27
- package/dist/outbox.d.ts +11 -9
- package/dist/outbox.js +45 -27
- package/dist/page.d.ts +1 -1
- package/dist/page.js +8 -6
- package/dist/pagePath.js +14 -14
- package/dist/queue.d.ts +6 -4
- package/dist/queue.js +26 -13
- package/dist/stateFields.d.ts +4 -4
- package/dist/stateFields.js +6 -4
- package/dist/testing.d.ts +8 -5
- package/dist/testing.js +27 -17
- package/dist/throttle.d.ts +30 -0
- package/dist/throttle.js +41 -0
- package/dist/transport.d.ts +10 -1
- package/dist/transport.js +35 -8
- package/dist/web.d.ts +4 -4
- package/dist/web.js +30 -17
- package/package.json +6 -4
package/dist/extension.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { detectBrowser } from
|
|
2
|
-
import { systemClock } from
|
|
3
|
-
import {
|
|
4
|
-
import { AnalyticsState, Mode, } from
|
|
5
|
-
import { Platform, SYSTEM_EVENTS } from
|
|
6
|
-
import { checkEvent } from
|
|
7
|
-
import { reportTo } from
|
|
8
|
-
import { parseQueue, QUEUE_KEY } from
|
|
9
|
-
import {
|
|
10
|
-
|
|
1
|
+
import { detectBrowser } from "./browser.js";
|
|
2
|
+
import { systemClock } from "./clock.js";
|
|
3
|
+
import { newCollectorNotingDays, sendDelayMsOf } from "./collector.js";
|
|
4
|
+
import { AnalyticsState, Mode, } from "./collectorTypes.js";
|
|
5
|
+
import { Platform, SYSTEM_EVENTS } from "./envelope.js";
|
|
6
|
+
import { checkEvent } from "./eventCheck.js";
|
|
7
|
+
import { reportTo } from "./hostInput.js";
|
|
8
|
+
import { parseQueue, QUEUE_KEY } from "./queue.js";
|
|
9
|
+
import { newThrottle, throttleOf } from "./throttle.js";
|
|
10
|
+
import { newWebLock } from "./web.js";
|
|
11
|
+
const FORWARD_TYPE = "artel.track";
|
|
11
12
|
/**
|
|
12
13
|
* The event names the worker refuses from a content script: every system event, which the intake counts from any page
|
|
13
14
|
* without the product's allowlist.
|
|
@@ -32,10 +33,15 @@ export function newExtensionCollector(options) {
|
|
|
32
33
|
lock: options.lock ?? newWebLock(),
|
|
33
34
|
browserLanguage: options.browserLanguage ?? navigator.language,
|
|
34
35
|
};
|
|
36
|
+
// `onNewDay` refreshes the uninstall page, since a day's first event changes its `active_days`. The collector queues
|
|
37
|
+
// no event before `refreshUninstallPage` is set below.
|
|
38
|
+
let refreshUninstallPage;
|
|
39
|
+
const onNewDay = () => void refreshUninstallPage?.();
|
|
35
40
|
const built = options.mode === Mode.Anonymous
|
|
36
|
-
?
|
|
37
|
-
:
|
|
38
|
-
const collector = withUninstallPage(built, options.uninstallPage, options.onError);
|
|
41
|
+
? newCollectorNotingDays({ ...options, ...defaults }, onNewDay)
|
|
42
|
+
: newCollectorNotingDays({ ...options, ...defaults, browser: options.browser ?? (() => detectBrowser(navigator)) }, onNewDay);
|
|
43
|
+
const { collector, refresh } = withUninstallPage(built, options.uninstallPage, options.onError);
|
|
44
|
+
refreshUninstallPage = refresh;
|
|
39
45
|
if (isWorker) {
|
|
40
46
|
listenAsWorker(collector, {
|
|
41
47
|
queueKey: `artel.${options.key}.${QUEUE_KEY}`,
|
|
@@ -58,7 +64,7 @@ function listenAsWorker(collector, { queueKey, sendDelayMs, clock, onError, }) {
|
|
|
58
64
|
let isSendScheduled = false;
|
|
59
65
|
storage.onChanged.addListener((changes, area) => {
|
|
60
66
|
const change = changes[queueKey];
|
|
61
|
-
if (area !==
|
|
67
|
+
if (area !== "local" || change === undefined || isSendScheduled || !addsEvents(change)) {
|
|
62
68
|
return;
|
|
63
69
|
}
|
|
64
70
|
isSendScheduled = true;
|
|
@@ -68,10 +74,10 @@ function listenAsWorker(collector, { queueKey, sendDelayMs, clock, onError, }) {
|
|
|
68
74
|
}, sendDelayMs);
|
|
69
75
|
});
|
|
70
76
|
runtime.onInstalled.addListener(({ reason }) => {
|
|
71
|
-
if (reason ===
|
|
77
|
+
if (reason === "install") {
|
|
72
78
|
void collector.installed();
|
|
73
79
|
}
|
|
74
|
-
else if (reason ===
|
|
80
|
+
else if (reason === "update") {
|
|
75
81
|
void collector.updated();
|
|
76
82
|
}
|
|
77
83
|
});
|
|
@@ -79,12 +85,13 @@ function listenAsWorker(collector, { queueKey, sendDelayMs, clock, onError, }) {
|
|
|
79
85
|
acceptForwarded(collector, clock, onError);
|
|
80
86
|
}
|
|
81
87
|
/**
|
|
82
|
-
* Returns the collector, which sets the uninstall page now and after each `setState` when `pageUrl` is given
|
|
83
|
-
* keeps the address as it was set, so the query must follow the user's state
|
|
88
|
+
* Returns the collector, which sets the uninstall page now and after each `setState` when `pageUrl` is given, and
|
|
89
|
+
* `refresh`, which sets it again. Chrome keeps the address as it was set, so the query must follow the user's state
|
|
90
|
+
* and the active days.
|
|
84
91
|
*/
|
|
85
92
|
function withUninstallPage(collector, pageUrl, onError) {
|
|
86
93
|
if (pageUrl === undefined) {
|
|
87
|
-
return collector;
|
|
94
|
+
return { collector, refresh: async () => undefined };
|
|
88
95
|
}
|
|
89
96
|
// Every write of this context goes in turn on one chain. A write that started before the latest `setState` is
|
|
90
97
|
// dropped, so a slow refresh never puts back the fields of a state the user has left.
|
|
@@ -94,40 +101,44 @@ function withUninstallPage(collector, pageUrl, onError) {
|
|
|
94
101
|
const startedAt = changes;
|
|
95
102
|
const earlier = writes;
|
|
96
103
|
writes = (async () => {
|
|
97
|
-
|
|
104
|
+
// A failed write must not stop the next one, nor the `setState` that waits for it.
|
|
105
|
+
await earlier.catch(() => undefined);
|
|
98
106
|
const fields = await params();
|
|
99
107
|
if (startedAt === changes) {
|
|
100
108
|
await setUninstallUrl(pageUrl, fields, onError);
|
|
101
109
|
}
|
|
102
110
|
})();
|
|
103
|
-
return writes;
|
|
111
|
+
return writes.catch((error) => reportTo(onError, error));
|
|
104
112
|
};
|
|
105
113
|
const refresh = () => write(() => collector.uninstallParams());
|
|
106
114
|
void refresh();
|
|
107
115
|
return {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
collector: {
|
|
117
|
+
...collector,
|
|
118
|
+
setState: async (state) => {
|
|
119
|
+
changes++;
|
|
120
|
+
if (state === AnalyticsState.OptedOut || state === AnalyticsState.Anonymous) {
|
|
121
|
+
// A change of state can take 30 seconds or more, as for the opted_out or forget request it sends, and the
|
|
122
|
+
// page must not carry the earlier state's fields meanwhile. The page alone carries fewer than any state's.
|
|
123
|
+
await write(async () => undefined);
|
|
124
|
+
}
|
|
125
|
+
await collector.setState(state);
|
|
126
|
+
await refresh();
|
|
127
|
+
},
|
|
118
128
|
},
|
|
129
|
+
refresh,
|
|
119
130
|
};
|
|
120
131
|
}
|
|
121
132
|
/** Tells whether a change of the stored queue adds an event to it, as a new envelope or one more event of an envelope. */
|
|
122
133
|
function addsEvents({ oldValue, newValue }) {
|
|
123
|
-
const queueOf = (value) => parseQueue(typeof value ===
|
|
134
|
+
const queueOf = (value) => parseQueue(typeof value === "string" ? value : undefined) ?? [];
|
|
124
135
|
const before = new Map(queueOf(oldValue).map(({ qid, envelope }) => [qid, envelope.events.length]));
|
|
125
136
|
return queueOf(newValue).some(({ qid, envelope }) => envelope.events.length > (before.get(qid) ?? 0));
|
|
126
137
|
}
|
|
127
138
|
/** Tells whether the caller runs in the extension's service worker, the one context that may set the access level. */
|
|
128
139
|
function isServiceWorker() {
|
|
129
|
-
const scope = Reflect.get(globalThis,
|
|
130
|
-
return typeof scope ===
|
|
140
|
+
const scope = Reflect.get(globalThis, "ServiceWorkerGlobalScope");
|
|
141
|
+
return typeof scope === "function" && globalThis instanceof scope;
|
|
131
142
|
}
|
|
132
143
|
/**
|
|
133
144
|
* Lets only the extension's own pages and worker read and write `chrome.storage.local`, and resolves once Chrome has
|
|
@@ -136,10 +147,10 @@ function isServiceWorker() {
|
|
|
136
147
|
async function closeToContentScripts(onError) {
|
|
137
148
|
try {
|
|
138
149
|
const local = chromeApi().storage.local;
|
|
139
|
-
if (typeof local.setAccessLevel !==
|
|
140
|
-
throw new TypeError(
|
|
150
|
+
if (typeof local.setAccessLevel !== "function") {
|
|
151
|
+
throw new TypeError("artel: chrome.storage.local.setAccessLevel is missing, so content scripts can write it");
|
|
141
152
|
}
|
|
142
|
-
await local.setAccessLevel({ accessLevel:
|
|
153
|
+
await local.setAccessLevel({ accessLevel: "TRUSTED_CONTEXTS" });
|
|
143
154
|
}
|
|
144
155
|
catch (error) {
|
|
145
156
|
reportTo(onError, error);
|
|
@@ -166,7 +177,7 @@ export function newChromeStorageAdapter() {
|
|
|
166
177
|
return {
|
|
167
178
|
get: async (key) => {
|
|
168
179
|
const value = (await chromeApi().storage.local.get(key))[key];
|
|
169
|
-
return typeof value ===
|
|
180
|
+
return typeof value === "string" ? value : undefined;
|
|
170
181
|
},
|
|
171
182
|
set: async (key, value) => {
|
|
172
183
|
await chromeApi().storage.local.set({ [key]: value });
|
|
@@ -179,20 +190,43 @@ export function newChromeStorageAdapter() {
|
|
|
179
190
|
*/
|
|
180
191
|
export async function forwardTrack(name, params) {
|
|
181
192
|
try {
|
|
182
|
-
await chromeApi().runtime.sendMessage({
|
|
193
|
+
await chromeApi().runtime.sendMessage({
|
|
194
|
+
type: FORWARD_TYPE,
|
|
195
|
+
name,
|
|
196
|
+
...(params !== undefined && { params }),
|
|
197
|
+
});
|
|
183
198
|
}
|
|
184
199
|
catch {
|
|
185
200
|
// The send fails while the service worker updates or is gone. The content script drops the event and does not fail.
|
|
186
201
|
}
|
|
187
202
|
}
|
|
188
203
|
/**
|
|
189
|
-
* Returns the collector of a content script. Its `track`
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
204
|
+
* Returns the collector of a content script. Its `track` applies a call's `throttleMs` in the script's memory, by
|
|
205
|
+
* `clock`, then hands the event to the worker with `forwardTrack`. The worker checks, queues and sends it under the
|
|
206
|
+
* user's analytics state, which a content script can neither read nor change. It reads no storage, needs no key,
|
|
207
|
+
* version or endpoint, never throws, and drops a call with a bad `throttleMs` without a report, having no `onError`.
|
|
193
208
|
*/
|
|
194
|
-
export function newContentScriptCollector() {
|
|
195
|
-
|
|
209
|
+
export function newContentScriptCollector({ clock = systemClock, } = {}) {
|
|
210
|
+
const throttle = newThrottle();
|
|
211
|
+
return {
|
|
212
|
+
track: async (name, params, options) => {
|
|
213
|
+
const now = clock.now();
|
|
214
|
+
const asked = throttleOf(options);
|
|
215
|
+
if (asked === undefined) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const call = { name, throttleMs: asked.throttleMs, now };
|
|
219
|
+
if (throttle.isDropped(call)) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
// The window opens for an event that passes the worker's check. Only the worker knows the user's state, so an
|
|
223
|
+
// event the worker drops in the opted_out state opens a window too.
|
|
224
|
+
if (isForwardable(name, params)) {
|
|
225
|
+
throttle.went(call);
|
|
226
|
+
}
|
|
227
|
+
await forwardTrack(name, params);
|
|
228
|
+
},
|
|
229
|
+
};
|
|
196
230
|
}
|
|
197
231
|
/**
|
|
198
232
|
* Tracks the events content scripts forward with `forwardTrack`, at most 60 a minute. It takes them only from this
|
|
@@ -226,42 +260,67 @@ function acceptForwarded(collector, clock, onError) {
|
|
|
226
260
|
}
|
|
227
261
|
/**
|
|
228
262
|
* Sets the uninstall page to `pageUrl` with `params` added to its query, or to `pageUrl` alone without them. It
|
|
229
|
-
* resolves and never rejects: a failure goes to `onError`.
|
|
263
|
+
* resolves and never rejects: a failure, such as a `pageUrl` that is not a URL, goes to `onError`.
|
|
230
264
|
*/
|
|
231
265
|
async function setUninstallUrl(pageUrl, params, onError) {
|
|
232
266
|
try {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
await chromeApi().runtime.setUninstallURL(url.toString());
|
|
267
|
+
// oxlint-disable-next-line no-new -- The constructor throws for a page that is not a URL. The page goes as written.
|
|
268
|
+
new URL(pageUrl);
|
|
269
|
+
await chromeApi().runtime.setUninstallURL(withQueryAdded(pageUrl, params));
|
|
238
270
|
}
|
|
239
271
|
catch (error) {
|
|
240
272
|
reportTo(onError, error);
|
|
241
273
|
}
|
|
242
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Returns `pageUrl` with `params` added at the end of its query, before any fragment. The page's own query stays as
|
|
277
|
+
* written, since a page may read it in a form that `URLSearchParams` would write another way.
|
|
278
|
+
*/
|
|
279
|
+
function withQueryAdded(pageUrl, params) {
|
|
280
|
+
const query = new URLSearchParams(Object.entries(params ?? {}).map(([name, value]) => [name, String(value)])).toString();
|
|
281
|
+
if (query === "") {
|
|
282
|
+
return pageUrl;
|
|
283
|
+
}
|
|
284
|
+
const hashAt = pageUrl.indexOf("#");
|
|
285
|
+
const beforeFragment = hashAt === -1 ? pageUrl : pageUrl.slice(0, hashAt);
|
|
286
|
+
const fragment = hashAt === -1 ? "" : pageUrl.slice(hashAt);
|
|
287
|
+
return `${beforeFragment}${joinerAfter(beforeFragment)}${query}${fragment}`;
|
|
288
|
+
}
|
|
289
|
+
/** Returns what goes between `address` and an added query: `?`, `&`, or nothing after a `?` or `&` it ends in. */
|
|
290
|
+
function joinerAfter(address) {
|
|
291
|
+
if (!address.includes("?")) {
|
|
292
|
+
return "?";
|
|
293
|
+
}
|
|
294
|
+
return address.endsWith("?") || address.endsWith("&") ? "" : "&";
|
|
295
|
+
}
|
|
243
296
|
/** Returns the global `chrome` API, or throws outside an extension context. */
|
|
244
297
|
function chromeApi() {
|
|
245
|
-
const chrome = Reflect.get(globalThis,
|
|
298
|
+
const chrome = Reflect.get(globalThis, "chrome");
|
|
246
299
|
if (!isChromeApi(chrome)) {
|
|
247
|
-
throw new Error(
|
|
300
|
+
throw new Error("artel: the chrome extension API is not available here");
|
|
248
301
|
}
|
|
249
302
|
return chrome;
|
|
250
303
|
}
|
|
251
304
|
/** Checks only that the extension runtime is there. A missing `storage` permission surfaces when storage is used. */
|
|
252
305
|
function isChromeApi(value) {
|
|
253
|
-
return typeof value ===
|
|
306
|
+
return (typeof value === "object" && value !== null && typeof Reflect.get(value, "runtime") === "object");
|
|
254
307
|
}
|
|
255
308
|
/**
|
|
256
|
-
* Checks a forwarded message against what the
|
|
257
|
-
*
|
|
309
|
+
* Checks a forwarded message against what the worker takes, as `isForwardable` does. Content scripts run among the
|
|
310
|
+
* page's scripts, so the worker cannot trust what they send.
|
|
258
311
|
*/
|
|
259
312
|
function isForwarded(message) {
|
|
260
|
-
if (typeof message !==
|
|
313
|
+
if (typeof message !== "object" ||
|
|
314
|
+
message === null ||
|
|
315
|
+
Reflect.get(message, "type") !== FORWARD_TYPE) {
|
|
261
316
|
return false;
|
|
262
317
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
318
|
+
return isForwardable(Reflect.get(message, "name"), Reflect.get(message, "params"));
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Tells whether the worker takes a forwarded event: one `track` takes, with a name outside `REFUSED_NAMES`, the
|
|
322
|
+
* intake's system events.
|
|
323
|
+
*/
|
|
324
|
+
function isForwardable(name, params) {
|
|
325
|
+
return (typeof name === "string" && !REFUSED_NAMES.has(name) && checkEvent(name, params) === undefined);
|
|
267
326
|
}
|
package/dist/fields.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ActiveDaysBucket, type PageFields } from
|
|
2
|
-
import type { EventParams, UsageEvent } from
|
|
3
|
-
import type { EventStamp } from
|
|
1
|
+
import { ActiveDaysBucket, type PageFields } from "./collectorTypes.js";
|
|
2
|
+
import type { EventParams, UsageEvent } from "./envelope.js";
|
|
3
|
+
import type { EventStamp } from "./installState.js";
|
|
4
4
|
/** Checks a product version, such as `1.2.3` or `1.2.3-beta.1`, against the format and length the intake takes. */
|
|
5
5
|
export declare function isVersion(value: unknown): value is string;
|
|
6
6
|
/** Checks a language tag, such as `en`, `pt-BR` or `zh-Hant-TW`, against the format and length the intake takes. */
|
package/dist/fields.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Period, daysBetween, formatDay, monthsBetween } from
|
|
2
|
-
import { ActiveDaysBucket } from
|
|
1
|
+
import { Period, daysBetween, formatDay, monthsBetween } from "./calendar.js";
|
|
2
|
+
import { ActiveDaysBucket } from "./collectorTypes.js";
|
|
3
3
|
// The intake takes `version` and the language fields only in these formats. The intake refuses an envelope with a field
|
|
4
4
|
// that breaks them, and all its events are lost. So the collector drops a bad language, and its constructor throws for
|
|
5
5
|
// a bad version.
|
|
@@ -9,18 +9,18 @@ const LANGUAGE_FORMAT = /^[a-z]{2,3}(-[A-Za-z0-9]{2,8}){0,2}$/;
|
|
|
9
9
|
const MAX_LANGUAGE_LENGTH = 16;
|
|
10
10
|
/** Checks a product version, such as `1.2.3` or `1.2.3-beta.1`, against the format and length the intake takes. */
|
|
11
11
|
export function isVersion(value) {
|
|
12
|
-
return typeof value ===
|
|
12
|
+
return (typeof value === "string" && value.length <= MAX_VERSION_LENGTH && VERSION_FORMAT.test(value));
|
|
13
13
|
}
|
|
14
14
|
/** Checks a language tag, such as `en`, `pt-BR` or `zh-Hant-TW`, against the format and length the intake takes. */
|
|
15
15
|
export function isLanguage(value) {
|
|
16
|
-
return typeof value ===
|
|
16
|
+
return (typeof value === "string" && value.length <= MAX_LANGUAGE_LENGTH && LANGUAGE_FORMAT.test(value));
|
|
17
17
|
}
|
|
18
18
|
/** Returns the tag with `-` for each `_`, as `en_US` becomes `en-US`. The intake reads the tag this way too. */
|
|
19
19
|
export function languageTag(value) {
|
|
20
|
-
return typeof value ===
|
|
20
|
+
return typeof value === "string" ? value.replaceAll("_", "-") : value;
|
|
21
21
|
}
|
|
22
22
|
export function primaryLanguage(tag) {
|
|
23
|
-
return tag.split(
|
|
23
|
+
return tag.split("-")[0] ?? tag;
|
|
24
24
|
}
|
|
25
25
|
/** The highest exact age in days. The intake reads 31 as 31 days or more. */
|
|
26
26
|
const MAX_AGE_DAYS = 31;
|
|
@@ -45,7 +45,7 @@ export function ageInDays(days, { isExact }) {
|
|
|
45
45
|
*/
|
|
46
46
|
export function uninstalledParams(params, { isExact }) {
|
|
47
47
|
const age = params?.age_days;
|
|
48
|
-
return typeof age ===
|
|
48
|
+
return typeof age === "number" ? { ...params, age_days: ageInDays(age, { isExact }) } : params;
|
|
49
49
|
}
|
|
50
50
|
/** Returns the age capped at 25 months when `isExact`, and its bucket otherwise. */
|
|
51
51
|
function ageInMonths(months, { isExact }) {
|
package/dist/hostInput.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type Browser } from
|
|
2
|
-
import type { CollectorOptions, Delivery, GetId } from
|
|
3
|
-
import type { Build } from
|
|
4
|
-
import type { StorageAdapter } from
|
|
1
|
+
import { type Browser } from "./browser.js";
|
|
2
|
+
import type { CollectorOptions, Delivery, GetId } from "./collectorTypes.js";
|
|
3
|
+
import type { Build } from "./stateFields.js";
|
|
4
|
+
import type { StorageAdapter } from "./storage.js";
|
|
5
5
|
type ReportError = (error: unknown) => void;
|
|
6
6
|
/**
|
|
7
7
|
* Returns `value` as JSON, cut to its first 64 characters with `…` after a cut, or its type where JSON cannot write it,
|
|
@@ -24,7 +24,7 @@ export declare function checkedBuild(options: CollectorOptions): Build;
|
|
|
24
24
|
* Throws the `TypeError` a constructor throws for a key, a version, a platform, an endpoint or a transport the intake
|
|
25
25
|
* would refuse every envelope of, and returns otherwise. It reads no host API, so a product's build or test can call it.
|
|
26
26
|
*/
|
|
27
|
-
export declare function checkOptions(options: Pick<CollectorOptions,
|
|
27
|
+
export declare function checkOptions(options: Pick<CollectorOptions, "key" | "version" | "platform"> & Delivery): void;
|
|
28
28
|
/** Tells whether `key` is a channel's test key, `<kind>-test-<10 characters>`. */
|
|
29
29
|
export declare function isTestKey(key: string): boolean;
|
|
30
30
|
/**
|
package/dist/hostInput.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { copyBrowser } from
|
|
2
|
-
import { Platform } from
|
|
3
|
-
import { isVersion } from
|
|
1
|
+
import { copyBrowser } from "./browser.js";
|
|
2
|
+
import { Platform } from "./envelope.js";
|
|
3
|
+
import { isVersion } from "./fields.js";
|
|
4
4
|
/** The most characters an id may have for the intake to take it. */
|
|
5
5
|
const MAX_ID_LENGTH = 64;
|
|
6
6
|
/** The hosts an http endpoint may name: this machine, as `URL` writes its hostname. */
|
|
7
|
-
const DEVELOPMENT_HOSTS = new Set([
|
|
7
|
+
const DEVELOPMENT_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
8
8
|
/** The most characters of host or stored text a report quotes. A planted value can be megabytes long. */
|
|
9
9
|
const MAX_QUOTED_LENGTH = 64;
|
|
10
10
|
/**
|
|
@@ -12,9 +12,9 @@ const MAX_QUOTED_LENGTH = 64;
|
|
|
12
12
|
* key, then 10 characters of `[0-9a-z]`. The intake refuses an envelope of a platform its channel's kind does not take.
|
|
13
13
|
*/
|
|
14
14
|
const CHANNEL_KINDS = [
|
|
15
|
-
{ kind:
|
|
16
|
-
{ kind:
|
|
17
|
-
{ kind:
|
|
15
|
+
{ kind: "extension", platforms: [Platform.Extension] },
|
|
16
|
+
{ kind: "desktop", platforms: [Platform.MacOs, Platform.Windows, Platform.Linux] },
|
|
17
|
+
{ kind: "site", platforms: [Platform.Web] },
|
|
18
18
|
].map(({ kind, platforms }) => ({
|
|
19
19
|
kind,
|
|
20
20
|
keyFormat: new RegExp(String.raw `^${kind}-(?:test-)?[\da-z]{10}$`),
|
|
@@ -34,7 +34,9 @@ export function quoted(value) {
|
|
|
34
34
|
return typeof value;
|
|
35
35
|
}
|
|
36
36
|
const characters = Array.from(text.slice(0, 2 * MAX_QUOTED_LENGTH));
|
|
37
|
-
return characters.length > MAX_QUOTED_LENGTH
|
|
37
|
+
return characters.length > MAX_QUOTED_LENGTH
|
|
38
|
+
? `${characters.slice(0, MAX_QUOTED_LENGTH).join("")}…`
|
|
39
|
+
: text;
|
|
38
40
|
}
|
|
39
41
|
/** Hands `error` to the host's `onError` as an Error. A handler that throws reaches no caller. */
|
|
40
42
|
export function reportTo(onError, error) {
|
|
@@ -62,7 +64,9 @@ export function accepted(name, value, check, reportError) {
|
|
|
62
64
|
export function checkedBuild(options) {
|
|
63
65
|
checkOptions(options);
|
|
64
66
|
const { platform, version } = options;
|
|
65
|
-
if (options.browser !== undefined &&
|
|
67
|
+
if (options.browser !== undefined &&
|
|
68
|
+
platform !== Platform.Web &&
|
|
69
|
+
platform !== Platform.Extension) {
|
|
66
70
|
throw new TypeError(`artel: platform ${platform} names no browser`);
|
|
67
71
|
}
|
|
68
72
|
return { platform, version };
|
|
@@ -94,30 +98,32 @@ export function isTestKey(key) {
|
|
|
94
98
|
*/
|
|
95
99
|
function checkDelivery({ endpoint, transport }) {
|
|
96
100
|
if (transport !== undefined) {
|
|
97
|
-
if (typeof transport !==
|
|
101
|
+
if (typeof transport !== "function") {
|
|
98
102
|
throw new TypeError(`artel: transport is a ${typeof transport}, not a function`);
|
|
99
103
|
}
|
|
100
104
|
return;
|
|
101
105
|
}
|
|
102
106
|
let url;
|
|
103
107
|
try {
|
|
104
|
-
url = typeof endpoint ===
|
|
108
|
+
url = typeof endpoint === "string" ? new URL(endpoint) : undefined;
|
|
105
109
|
}
|
|
106
110
|
catch {
|
|
107
111
|
// The throw below covers an endpoint that is no URL.
|
|
108
112
|
}
|
|
109
|
-
const isHttps = url?.protocol ===
|
|
110
|
-
if (!isHttps || url?.username !==
|
|
111
|
-
throw new TypeError(
|
|
113
|
+
const isHttps = url?.protocol === "https:" || (url?.protocol === "http:" && isDevelopmentHost(url.hostname));
|
|
114
|
+
if (!isHttps || url?.username !== "" || url.password !== "") {
|
|
115
|
+
throw new TypeError("artel: endpoint is not an https URL without a user or password, or http to a development host");
|
|
112
116
|
}
|
|
113
117
|
}
|
|
114
118
|
/** Tells whether a URL's host is one only this machine or a development setup serves. */
|
|
115
119
|
function isDevelopmentHost(hostname) {
|
|
116
|
-
return DEVELOPMENT_HOSTS.has(hostname) || hostname.endsWith(
|
|
120
|
+
return (DEVELOPMENT_HOSTS.has(hostname) || hostname.endsWith(".localhost") || hostname.endsWith(".test"));
|
|
117
121
|
}
|
|
118
122
|
/** Throws unless `key` has the intake's form, and its channel kind takes `platform`. */
|
|
119
123
|
function checkKey(key, platform) {
|
|
120
|
-
const channel = typeof key ===
|
|
124
|
+
const channel = typeof key === "string"
|
|
125
|
+
? CHANNEL_KINDS.find(({ keyFormat }) => keyFormat.test(key))
|
|
126
|
+
: undefined;
|
|
121
127
|
if (channel === undefined) {
|
|
122
128
|
throw new TypeError(`artel: key ${quoted(key)} is not <kind>-<10 characters> or <kind>-test-<10 characters>`);
|
|
123
129
|
}
|
|
@@ -131,7 +137,7 @@ function checkKey(key, platform) {
|
|
|
131
137
|
* leaves out a value that is not a browser, which only a host outside TypeScript passes.
|
|
132
138
|
*/
|
|
133
139
|
export function newBrowserReader(browser, reportError) {
|
|
134
|
-
if (typeof browser !==
|
|
140
|
+
if (typeof browser !== "function") {
|
|
135
141
|
const copy = acceptedBrowser(browser, reportError);
|
|
136
142
|
return () => copy;
|
|
137
143
|
}
|
|
@@ -163,7 +169,7 @@ function acceptedBrowser(browser, reportError) {
|
|
|
163
169
|
}
|
|
164
170
|
/** Tells whether `id` is an id the intake takes: a string of 1 to 64 characters. */
|
|
165
171
|
export function isUsableId(id) {
|
|
166
|
-
return typeof id ===
|
|
172
|
+
return typeof id === "string" && id !== "" && Array.from(id).length <= MAX_ID_LENGTH;
|
|
167
173
|
}
|
|
168
174
|
/**
|
|
169
175
|
* Returns a `getId` that gives a non-empty string of at most 64 characters, or undefined. A host outside TypeScript may
|
|
@@ -172,7 +178,7 @@ export function isUsableId(id) {
|
|
|
172
178
|
export function usableId(getId, reportError) {
|
|
173
179
|
return async () => {
|
|
174
180
|
const id = await getId();
|
|
175
|
-
if (typeof id !==
|
|
181
|
+
if (typeof id !== "string" || id === "") {
|
|
176
182
|
return undefined;
|
|
177
183
|
}
|
|
178
184
|
// The intake refuses the whole envelope of an id past 64 characters. The event waits as for an unknown id instead.
|
|
@@ -191,7 +197,7 @@ export function newStringStorage(storage) {
|
|
|
191
197
|
return {
|
|
192
198
|
get: async (key) => {
|
|
193
199
|
const value = await storage.get(key);
|
|
194
|
-
return typeof value ===
|
|
200
|
+
return typeof value === "string" ? value : undefined;
|
|
195
201
|
},
|
|
196
202
|
set: (key, value) => storage.set(key, value),
|
|
197
203
|
};
|
package/dist/installState.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Period, type CalendarDate } from
|
|
2
|
-
import { type Envelope } from
|
|
3
|
-
import type { StorageAdapter } from
|
|
1
|
+
import { Period, type CalendarDate } from "./calendar.js";
|
|
2
|
+
import { type Envelope } from "./envelope.js";
|
|
3
|
+
import type { StorageAdapter } from "./storage.js";
|
|
4
4
|
/** What the collector knows about one event when it records it. */
|
|
5
5
|
export interface EventStamp {
|
|
6
6
|
today: CalendarDate;
|
|
@@ -35,7 +35,7 @@ interface InstallState {
|
|
|
35
35
|
* again: first-of-period marks, the `installed` mark, and the `updated` version while it is still stored. A
|
|
36
36
|
* first-of-day event also removes its day from the active days.
|
|
37
37
|
*/
|
|
38
|
-
giveBack(envelopes: readonly Pick<Envelope,
|
|
38
|
+
giveBack(envelopes: readonly Pick<Envelope, "version" | "events">[]): Promise<void>;
|
|
39
39
|
/** Ends the session, so the next event starts a new one. */
|
|
40
40
|
endSession(): Promise<void>;
|
|
41
41
|
/** Marks the install and stores `version`. Resolves to true, or to false when the install was marked before. */
|