@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.
- package/README.md +127 -110
- 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/collector.d.ts +11 -8
- package/dist/collector.js +176 -106
- package/dist/collectorTypes.d.ts +67 -42
- package/dist/collectorTypes.js +24 -14
- package/dist/desktop.d.ts +9 -12
- package/dist/desktop.js +8 -3
- package/dist/envelope.d.ts +14 -10
- package/dist/envelope.js +12 -12
- package/dist/eventCheck.d.ts +17 -7
- package/dist/eventCheck.js +64 -23
- package/dist/exports.d.ts +10 -8
- package/dist/exports.js +9 -7
- package/dist/extension.d.ts +27 -37
- package/dist/extension.js +200 -51
- package/dist/fields.d.ts +3 -3
- package/dist/fields.js +7 -7
- package/dist/hostInput.d.ts +13 -4
- package/dist/hostInput.js +44 -22
- package/dist/installState.d.ts +11 -7
- package/dist/installState.js +42 -25
- package/dist/outbox.d.ts +28 -17
- package/dist/outbox.js +89 -31
- package/dist/page.d.ts +1 -1
- package/dist/page.js +8 -6
- package/dist/pagePath.js +14 -14
- package/dist/queue.d.ts +26 -10
- package/dist/queue.js +70 -23
- package/dist/stateFields.d.ts +9 -8
- package/dist/stateFields.js +28 -11
- package/dist/testing.d.ts +31 -0
- package/dist/testing.js +85 -0
- package/dist/throttle.d.ts +30 -0
- package/dist/throttle.js +41 -0
- package/dist/transport.d.ts +15 -5
- package/dist/transport.js +34 -7
- package/dist/web.d.ts +8 -18
- package/dist/web.js +32 -33
- package/package.json +10 -5
package/dist/transport.d.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import { type Clock } from
|
|
1
|
+
import { type Clock } from "./clock.js";
|
|
2
|
+
import type { CollectorOptions } from "./collectorTypes.js";
|
|
2
3
|
/** What the intake answered: its HTTP status, 0 when the request did not reach it, and its `Retry-After` header. */
|
|
3
4
|
export interface TransportResult {
|
|
4
5
|
status: number;
|
|
5
6
|
retryAfter?: string;
|
|
6
7
|
}
|
|
7
8
|
/**
|
|
8
|
-
* Sends one
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Sends one request body and resolves to the intake's answer or its HTTP status. The body is an envelope, for the
|
|
10
|
+
* intake's `/v1/public/relay`, or a forget request `{ key, id }` without `events`, for its `/v1/public/forget`. It must
|
|
11
|
+
* resolve within 30 seconds. After 35 seconds the outbox counts it as status 0 and sends the body again. The intake
|
|
12
|
+
* keeps a forgotten user's events from a request that arrives over a minute after the forget.
|
|
11
13
|
*/
|
|
12
14
|
export type Transport = (body: string) => Promise<number | TransportResult>;
|
|
13
15
|
/**
|
|
14
|
-
* Returns a transport that posts each
|
|
16
|
+
* Returns a transport that posts each body to `endpoint` with `fetch`, the host's own by default. The transport
|
|
15
17
|
* resolves to the status and the raw `Retry-After` header, or to status 0 when a request fails or takes over 30
|
|
16
18
|
* seconds.
|
|
17
19
|
*/
|
|
@@ -19,3 +21,11 @@ export declare function newHttpTransport(endpoint: string, { fetch, clock, }?: {
|
|
|
19
21
|
fetch?: typeof globalThis.fetch;
|
|
20
22
|
clock?: Clock;
|
|
21
23
|
}): Transport;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the transport of the envelopes and that of the forget requests. A product's own transport takes both kinds
|
|
26
|
+
* of request.
|
|
27
|
+
*/
|
|
28
|
+
export declare function transportsOf(options: CollectorOptions): {
|
|
29
|
+
transport: Transport;
|
|
30
|
+
forgetTransport: Transport;
|
|
31
|
+
};
|
package/dist/transport.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { systemClock } from
|
|
1
|
+
import { systemClock } from "./clock.js";
|
|
2
2
|
const TIMEOUT_MS = 30_000;
|
|
3
3
|
/**
|
|
4
|
-
* Returns a transport that posts each
|
|
4
|
+
* Returns a transport that posts each body to `endpoint` with `fetch`, the host's own by default. The transport
|
|
5
5
|
* resolves to the status and the raw `Retry-After` header, or to status 0 when a request fails or takes over 30
|
|
6
6
|
* seconds.
|
|
7
7
|
*/
|
|
@@ -13,19 +13,19 @@ fetch = (input, init) => globalThis.fetch(input, init), clock = systemClock, } =
|
|
|
13
13
|
const timer = clock.setTimeout(() => timeout.abort(), TIMEOUT_MS);
|
|
14
14
|
try {
|
|
15
15
|
const response = await fetch(endpoint, {
|
|
16
|
-
method:
|
|
16
|
+
method: "POST",
|
|
17
17
|
// This type skips a CORS preflight. The intake reads the body as JSON.
|
|
18
|
-
headers: {
|
|
18
|
+
headers: { "content-type": "text/plain;charset=UTF-8" },
|
|
19
19
|
body,
|
|
20
20
|
// An extension with host access, or a page on the intake's own origin, would otherwise send the user's cookies
|
|
21
21
|
// for that host. The intake reads none.
|
|
22
|
-
credentials:
|
|
22
|
+
credentials: "omit",
|
|
23
23
|
// A page's own policy could otherwise send its full address, which `getPage` exists to keep out.
|
|
24
|
-
referrerPolicy:
|
|
24
|
+
referrerPolicy: "no-referrer",
|
|
25
25
|
keepalive: true,
|
|
26
26
|
signal: timeout.signal,
|
|
27
27
|
});
|
|
28
|
-
const retryAfter = response.headers.get(
|
|
28
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
29
29
|
return { status: response.status, ...(retryAfter !== null && { retryAfter }) };
|
|
30
30
|
}
|
|
31
31
|
catch {
|
|
@@ -36,3 +36,30 @@ fetch = (input, init) => globalThis.fetch(input, init), clock = systemClock, } =
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Returns the transport of the envelopes and that of the forget requests. A product's own transport takes both kinds
|
|
41
|
+
* of request.
|
|
42
|
+
*/
|
|
43
|
+
export function transportsOf(options) {
|
|
44
|
+
if (options.transport !== undefined) {
|
|
45
|
+
return { transport: options.transport, forgetTransport: options.transport };
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
transport: newHttpTransport(options.endpoint),
|
|
49
|
+
forgetTransport: newHttpTransport(forgetUrlOf(options.endpoint)),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Returns the intake's forget address: `endpoint` with `forget` in place of the last segment of its path, which is
|
|
54
|
+
* `relay`. A trailing slash goes with that segment. The query stays, as on the envelopes' address.
|
|
55
|
+
*/
|
|
56
|
+
function forgetUrlOf(endpoint) {
|
|
57
|
+
const url = new URL(endpoint);
|
|
58
|
+
const path = url.pathname;
|
|
59
|
+
let end = path.length;
|
|
60
|
+
while (end > 0 && path[end - 1] === "/") {
|
|
61
|
+
end--;
|
|
62
|
+
}
|
|
63
|
+
url.pathname = `${path.slice(0, path.lastIndexOf("/", end - 1) + 1)}forget`;
|
|
64
|
+
return url.href;
|
|
65
|
+
}
|
package/dist/web.d.ts
CHANGED
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
import { Mode, type Collector, type CommonOptions, type Delivery, type ModeOptions, type PageFields, type PageOptions } from
|
|
2
|
-
import type { Lock } from
|
|
3
|
-
import type { StorageAdapter } from
|
|
1
|
+
import { Mode, type Collector, type CommonOptions, type Delivery, type ModeOptions, type PageFields, type PageOptions } from "./collectorTypes.js";
|
|
2
|
+
import type { Lock } from "./lock.js";
|
|
3
|
+
import type { StorageAdapter } from "./storage.js";
|
|
4
4
|
/**
|
|
5
5
|
* The options of `newWebCollector`: those of `newCollector` without `platform`, with `storage` optional. An option left
|
|
6
6
|
* out or `undefined` takes its default.
|
|
7
7
|
*/
|
|
8
|
-
export type WebCollectorOptions = Omit<CommonOptions,
|
|
8
|
+
export type WebCollectorOptions = Omit<CommonOptions, "storage"> & {
|
|
9
9
|
storage?: StorageAdapter | undefined;
|
|
10
10
|
} & PageOptions & Delivery & ModeOptions;
|
|
11
11
|
/**
|
|
12
12
|
* Returns a collector for a website or a web app. It runs over `localStorage` and Web Locks, and sends the user's
|
|
13
|
-
* language from `navigator.language`, the page from `pageFields` and, in
|
|
13
|
+
* language from `navigator.language`, the page from `pageFields` and, in a state with an id, the browser from
|
|
14
14
|
* `detectBrowser`. The site passes its own `uiLanguage`. A desktop window uses `newDesktopCollector`.
|
|
15
15
|
*/
|
|
16
|
-
export declare function newWebCollector(options: WebCollectorOptions & {
|
|
17
|
-
mode:
|
|
18
|
-
}): Collector<
|
|
19
|
-
export declare function newWebCollector(options: WebCollectorOptions & {
|
|
20
|
-
mode: typeof Mode.Anonymous;
|
|
21
|
-
}): Collector<'anonymous'>;
|
|
22
|
-
export declare function newWebCollector(options: WebCollectorOptions): Collector;
|
|
16
|
+
export declare function newWebCollector<M extends Mode>(options: WebCollectorOptions & {
|
|
17
|
+
mode: M;
|
|
18
|
+
}): Collector<M>;
|
|
23
19
|
/**
|
|
24
20
|
* Returns a storage adapter over `localStorage`, which every tab of the origin shares. A Web Worker has no
|
|
25
21
|
* `localStorage`, so a collector there passes its own storage.
|
|
@@ -41,9 +37,3 @@ export declare function pageFields(): PageFields;
|
|
|
41
37
|
* function that stops it. A query change alone sends none. Once every tracker stops, `history` is as they found it.
|
|
42
38
|
*/
|
|
43
39
|
export declare function trackPageViews(collector: Collector): () => void;
|
|
44
|
-
/**
|
|
45
|
-
* Flushes the queue when the page is hidden, since the user may not return. A request to `endpoint` that started
|
|
46
|
-
* before the tab closes still finishes. A send not yet started waits for the next visit. Returns the function that
|
|
47
|
-
* removes the listener.
|
|
48
|
-
*/
|
|
49
|
-
export declare function flushWhenHidden(collector: Collector): () => void;
|
package/dist/web.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { detectBrowser } from
|
|
2
|
-
import { newCollector } from
|
|
3
|
-
import { Mode, } from
|
|
4
|
-
import { PAGE_VIEW, Platform } from
|
|
1
|
+
import { detectBrowser } from "./browser.js";
|
|
2
|
+
import { newCollector } from "./collector.js";
|
|
3
|
+
import { Mode, } from "./collectorTypes.js";
|
|
4
|
+
import { PAGE_VIEW, Platform } from "./envelope.js";
|
|
5
5
|
export function newWebCollector(options) {
|
|
6
6
|
const defaults = {
|
|
7
7
|
platform: Platform.Web,
|
|
@@ -10,9 +10,13 @@ export function newWebCollector(options) {
|
|
|
10
10
|
browserLanguage: options.browserLanguage ?? navigator.language,
|
|
11
11
|
getPage: options.getPage ?? pageFieldsWherePageIs,
|
|
12
12
|
};
|
|
13
|
-
return options.mode === Mode.
|
|
14
|
-
? newCollector({ ...options, ...defaults
|
|
15
|
-
: newCollector({
|
|
13
|
+
return options.mode === Mode.Anonymous
|
|
14
|
+
? newCollector({ ...options, ...defaults })
|
|
15
|
+
: newCollector({
|
|
16
|
+
...options,
|
|
17
|
+
...defaults,
|
|
18
|
+
browser: options.browser ?? (() => detectBrowser(navigator)),
|
|
19
|
+
});
|
|
16
20
|
}
|
|
17
21
|
/**
|
|
18
22
|
* Returns a storage adapter over `localStorage`, which every tab of the origin shares. A Web Worker has no
|
|
@@ -33,8 +37,8 @@ export function newLocalStorageAdapter() {
|
|
|
33
37
|
export function newWebLock() {
|
|
34
38
|
return (operation) => {
|
|
35
39
|
// A test DOM such as happy-dom sets `navigator.locks` to null, so the check asks for the method itself.
|
|
36
|
-
const locks = typeof navigator ===
|
|
37
|
-
return typeof locks?.request ===
|
|
40
|
+
const locks = typeof navigator === "undefined" ? undefined : navigator.locks;
|
|
41
|
+
return typeof locks?.request === "function" ? locks.request("artel", operation) : operation();
|
|
38
42
|
};
|
|
39
43
|
}
|
|
40
44
|
/**
|
|
@@ -43,11 +47,16 @@ export function newWebLock() {
|
|
|
43
47
|
* `getPage` that returns the route pattern instead.
|
|
44
48
|
*/
|
|
45
49
|
export function pageFields() {
|
|
46
|
-
return {
|
|
50
|
+
return {
|
|
51
|
+
page: `${location.protocol}//${location.host}${location.pathname}`,
|
|
52
|
+
referrer: document.referrer,
|
|
53
|
+
};
|
|
47
54
|
}
|
|
48
55
|
/** Returns `pageFields`, or undefined in a Web Worker, which has no page. */
|
|
49
56
|
function pageFieldsWherePageIs() {
|
|
50
|
-
return typeof document ===
|
|
57
|
+
return typeof document === "undefined" || typeof location === "undefined"
|
|
58
|
+
? undefined
|
|
59
|
+
: pageFields();
|
|
51
60
|
}
|
|
52
61
|
/**
|
|
53
62
|
* Sends `page_view` for the current page and after each navigation that changes `location.pathname`, and returns the
|
|
@@ -63,17 +72,17 @@ export function trackPageViews(collector) {
|
|
|
63
72
|
lastPath = location.pathname;
|
|
64
73
|
void collector.track(PAGE_VIEW);
|
|
65
74
|
};
|
|
66
|
-
const unwrapPushState = callAfter(
|
|
67
|
-
const unwrapReplaceState = callAfter(
|
|
68
|
-
window.addEventListener(
|
|
75
|
+
const unwrapPushState = callAfter("pushState", onNavigation);
|
|
76
|
+
const unwrapReplaceState = callAfter("replaceState", onNavigation);
|
|
77
|
+
window.addEventListener("popstate", onNavigation);
|
|
69
78
|
const navigation = navigationApi();
|
|
70
|
-
navigation?.addEventListener(
|
|
79
|
+
navigation?.addEventListener("currententrychange", onNavigation);
|
|
71
80
|
onNavigation();
|
|
72
81
|
return () => {
|
|
73
82
|
unwrapPushState();
|
|
74
83
|
unwrapReplaceState();
|
|
75
|
-
window.removeEventListener(
|
|
76
|
-
navigation?.removeEventListener(
|
|
84
|
+
window.removeEventListener("popstate", onNavigation);
|
|
85
|
+
navigation?.removeEventListener("currententrychange", onNavigation);
|
|
77
86
|
};
|
|
78
87
|
}
|
|
79
88
|
/** The wrappers `callAfter` installed, by the wrapper function. */
|
|
@@ -84,7 +93,11 @@ const wrappings = new WeakMap();
|
|
|
84
93
|
* active one or under a product's own wrapper stays and passes calls on.
|
|
85
94
|
*/
|
|
86
95
|
function callAfter(method, listener) {
|
|
87
|
-
const wrapping = {
|
|
96
|
+
const wrapping = {
|
|
97
|
+
found: history[method],
|
|
98
|
+
wasOwn: Object.hasOwn(history, method),
|
|
99
|
+
isActive: true,
|
|
100
|
+
};
|
|
88
101
|
const wrapper = function (...args) {
|
|
89
102
|
wrapping.found.apply(this, args);
|
|
90
103
|
if (wrapping.isActive) {
|
|
@@ -107,20 +120,6 @@ function callAfter(method, listener) {
|
|
|
107
120
|
}
|
|
108
121
|
/** Returns the Navigation API, or undefined where the browser lacks it. */
|
|
109
122
|
function navigationApi() {
|
|
110
|
-
const navigation = Reflect.get(globalThis,
|
|
123
|
+
const navigation = Reflect.get(globalThis, "navigation");
|
|
111
124
|
return navigation instanceof EventTarget ? navigation : undefined;
|
|
112
125
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Flushes the queue when the page is hidden, since the user may not return. A request to `endpoint` that started
|
|
115
|
-
* before the tab closes still finishes. A send not yet started waits for the next visit. Returns the function that
|
|
116
|
-
* removes the listener.
|
|
117
|
-
*/
|
|
118
|
-
export function flushWhenHidden(collector) {
|
|
119
|
-
const onChange = () => {
|
|
120
|
-
if (document.visibilityState === 'hidden') {
|
|
121
|
-
void collector.flush();
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
document.addEventListener('visibilitychange', onChange);
|
|
125
|
-
return () => document.removeEventListener('visibilitychange', onChange);
|
|
126
|
-
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kk-forge/artel-collector",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Sends a product's
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Sends a product's analytics events to the intake service",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "KK Forge",
|
|
7
7
|
"repository": {
|
|
@@ -31,6 +31,10 @@
|
|
|
31
31
|
"./desktop": {
|
|
32
32
|
"types": "./dist/desktop.d.ts",
|
|
33
33
|
"default": "./dist/desktop.js"
|
|
34
|
+
},
|
|
35
|
+
"./testing": {
|
|
36
|
+
"types": "./dist/testing.d.ts",
|
|
37
|
+
"default": "./dist/testing.js"
|
|
34
38
|
}
|
|
35
39
|
},
|
|
36
40
|
"publishConfig": {
|
|
@@ -41,9 +45,9 @@
|
|
|
41
45
|
"build": "node --eval \"process.getBuiltinModule('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json",
|
|
42
46
|
"lint": "npm run lint:tsc && npm run lint:oxlint && npm run lint:format",
|
|
43
47
|
"lint:fix": "npm run lint:oxlint:fix && npm run lint:format:fix",
|
|
44
|
-
"lint:tsc": "tsc
|
|
45
|
-
"lint:oxlint": "oxlint
|
|
46
|
-
"lint:oxlint:fix": "oxlint --
|
|
48
|
+
"lint:tsc": "tsc",
|
|
49
|
+
"lint:oxlint": "oxlint src tests",
|
|
50
|
+
"lint:oxlint:fix": "oxlint --fix src tests",
|
|
47
51
|
"lint:format": "oxfmt --check .",
|
|
48
52
|
"lint:format:fix": "oxfmt .",
|
|
49
53
|
"test": "npm run test:unit",
|
|
@@ -51,6 +55,7 @@
|
|
|
51
55
|
"test:ci": "vitest run --coverage"
|
|
52
56
|
},
|
|
53
57
|
"devDependencies": {
|
|
58
|
+
"@kk-forge/oxlint-config": "^1.3.0",
|
|
54
59
|
"@vitest/coverage-v8": "^5.0.5",
|
|
55
60
|
"oxfmt": "^0.73.0",
|
|
56
61
|
"oxlint": "^1.90.0",
|