@haakco/custd-sdk 2.4.0 → 3.71.2
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/LICENSE +21 -0
- package/README.md +586 -40
- package/dist/admin-accounting-evidence.d.ts +48 -0
- package/dist/admin-accounting-evidence.js +24 -0
- package/dist/admin-app-releases.d.ts +116 -0
- package/dist/admin-app-releases.js +8 -0
- package/dist/admin-apps.d.ts +120 -0
- package/dist/admin-apps.js +47 -0
- package/dist/admin-auth-project-domain.d.ts +125 -0
- package/dist/admin-auth-project-domain.js +37 -0
- package/dist/admin-auth-projects.d.ts +989 -0
- package/dist/admin-auth-projects.js +527 -0
- package/dist/admin-campaign-email.d.ts +120 -0
- package/dist/admin-campaign-email.js +64 -0
- package/dist/admin-client-setup.d.ts +22 -0
- package/dist/admin-client-setup.js +11 -1
- package/dist/admin-data-scopes.d.ts +93 -0
- package/dist/admin-data-scopes.js +154 -0
- package/dist/admin-email-relay.d.ts +66 -0
- package/dist/admin-email-relay.js +25 -0
- package/dist/admin-environment-policy.d.ts +32 -0
- package/dist/admin-environment-policy.js +26 -0
- package/dist/admin-error-issues.d.ts +91 -0
- package/dist/admin-error-issues.js +52 -0
- package/dist/admin-event-types.d.ts +57 -0
- package/dist/admin-event-types.js +28 -0
- package/dist/admin-impersonation.d.ts +48 -0
- package/dist/admin-impersonation.js +27 -0
- package/dist/admin-ingest-claims.d.ts +35 -0
- package/dist/admin-ingest-claims.js +26 -0
- package/dist/admin-managed-audit.d.ts +13 -0
- package/dist/admin-managed-audit.js +13 -0
- package/dist/admin-measurement-projects.d.ts +166 -0
- package/dist/admin-measurement-projects.js +120 -0
- package/dist/admin-notification-destinations.d.ts +31 -0
- package/dist/admin-notification-destinations.js +19 -0
- package/dist/admin-notification-webhooks.d.ts +64 -0
- package/dist/admin-notification-webhooks.js +42 -0
- package/dist/admin-oauth-client-admissions.d.ts +65 -0
- package/dist/admin-oauth-client-admissions.js +46 -0
- package/dist/admin-offboarding.d.ts +20 -6
- package/dist/admin-offboarding.js +27 -6
- package/dist/admin-predictions.d.ts +127 -8
- package/dist/admin-predictions.js +22 -12
- package/dist/admin-privacy-erasures.d.ts +24 -12
- package/dist/admin-privacy-erasures.js +3 -2
- package/dist/admin-retention.d.ts +72 -24
- package/dist/admin-retention.js +38 -13
- package/dist/admin-rls.d.ts +94 -0
- package/dist/admin-rls.js +33 -0
- package/dist/admin-sdk-usage.d.ts +41 -0
- package/dist/admin-sdk-usage.js +23 -0
- package/dist/admin-site-ai.d.ts +72 -0
- package/dist/admin-site-ai.js +1 -0
- package/dist/admin-site-analytics.d.ts +52 -0
- package/dist/admin-site-analytics.js +14 -0
- package/dist/admin-site-audit-pages.d.ts +76 -0
- package/dist/admin-site-audit-pages.js +27 -0
- package/dist/admin-site-campaign-links.d.ts +40 -0
- package/dist/admin-site-campaign-links.js +9 -0
- package/dist/admin-site-domains.d.ts +733 -0
- package/dist/admin-site-domains.js +768 -0
- package/dist/admin-site-export.d.ts +76 -0
- package/dist/admin-site-export.js +15 -0
- package/dist/admin-site-insights-tasks.d.ts +44 -0
- package/dist/admin-site-insights-tasks.js +28 -0
- package/dist/admin-site-insights.d.ts +527 -0
- package/dist/admin-site-insights.js +61 -0
- package/dist/admin-site-links.d.ts +87 -0
- package/dist/admin-site-links.js +16 -0
- package/dist/admin-site-monitors.d.ts +110 -0
- package/dist/admin-site-monitors.js +25 -0
- package/dist/admin-site-next-actions.d.ts +93 -0
- package/dist/admin-site-next-actions.js +36 -0
- package/dist/admin-site-page-queries.d.ts +63 -0
- package/dist/admin-site-page-queries.js +20 -0
- package/dist/admin-site-rankings.d.ts +98 -0
- package/dist/admin-site-rankings.js +46 -0
- package/dist/admin-site-regional-trends.d.ts +30 -0
- package/dist/admin-site-regional-trends.js +1 -0
- package/dist/admin-site-saved-reports.d.ts +45 -0
- package/dist/admin-site-saved-reports.js +13 -0
- package/dist/admin-site-search-pool.d.ts +77 -0
- package/dist/admin-site-search-pool.js +41 -0
- package/dist/admin-site-search-value.d.ts +97 -0
- package/dist/admin-site-search-value.js +29 -0
- package/dist/admin-site-status-page.d.ts +57 -0
- package/dist/admin-site-status-page.js +10 -0
- package/dist/admin-site-traffic.d.ts +145 -0
- package/dist/admin-site-traffic.js +32 -0
- package/dist/admin-site-web-data.d.ts +258 -0
- package/dist/admin-site-web-data.js +60 -0
- package/dist/admin-subject-exports.d.ts +58 -27
- package/dist/admin-subject-exports.js +32 -16
- package/dist/admin-support-access.d.ts +32 -0
- package/dist/admin-support-access.js +21 -0
- package/dist/admin-system.d.ts +283 -0
- package/dist/admin-system.js +65 -0
- package/dist/admin-tenant-storage.d.ts +22 -14
- package/dist/admin-tenant-storage.js +13 -14
- package/dist/admin-timeplans.d.ts +276 -2
- package/dist/admin-timeplans.js +54 -0
- package/dist/admin-uncertainty-models.d.ts +42 -0
- package/dist/admin-uncertainty-models.js +53 -0
- package/dist/admin-usage.d.ts +124 -0
- package/dist/admin-usage.js +169 -0
- package/dist/admin-users.d.ts +124 -0
- package/dist/admin-users.js +82 -0
- package/dist/admin-workflow-timings.d.ts +367 -0
- package/dist/admin-workflow-timings.js +117 -0
- package/dist/analytics-events.d.ts +82 -0
- package/dist/analytics-events.js +157 -0
- package/dist/analytics-reads.d.ts +81 -0
- package/dist/analytics-reads.js +5 -0
- package/dist/app-account.d.ts +84 -0
- package/dist/app-account.js +177 -0
- package/dist/app-alerts.d.ts +120 -0
- package/dist/app-alerts.js +44 -0
- package/dist/app-items.d.ts +786 -0
- package/dist/app-items.js +524 -0
- package/dist/app-live.d.ts +84 -0
- package/dist/app-live.js +228 -0
- package/dist/app-meetings.d.ts +95 -0
- package/dist/app-meetings.js +26 -0
- package/dist/app-models.d.ts +271 -0
- package/dist/app-models.js +83 -0
- package/dist/app-push.d.ts +64 -0
- package/dist/app-push.js +178 -0
- package/dist/backend-lifecycle.d.ts +2 -2
- package/dist/browser-manifest.json +5 -5
- package/dist/browser-script-BWnwDjAC.js +711 -0
- package/dist/browser-sign-in.d.ts +153 -0
- package/dist/browser-sign-in.js +410 -0
- package/dist/browser-tracker.d.ts +95 -0
- package/dist/browser-tracker.js +362 -40
- package/dist/browser-web-vitals.d.ts +22 -0
- package/dist/browser-web-vitals.js +116 -0
- package/dist/browser.d.ts +9 -0
- package/dist/browser.js +9 -0
- package/dist/download-stream.d.ts +16 -0
- package/dist/download-stream.js +55 -0
- package/dist/error-capture.d.ts +62 -0
- package/dist/error-capture.js +168 -0
- package/dist/hosted-app.d.ts +115 -0
- package/dist/hosted-app.js +275 -0
- package/dist/index.d.ts +1168 -154
- package/dist/index.js +682 -162
- package/dist/ingest-operations.d.ts +483 -0
- package/dist/ingest-operations.js +154 -0
- package/dist/measurement.d.ts +164 -0
- package/dist/measurement.js +104 -0
- package/dist/parsers.d.ts +149 -0
- package/dist/parsers.js +115 -0
- package/dist/public-reads.d.ts +79 -0
- package/dist/public-reads.js +57 -0
- package/dist/reporting-catalogue.d.ts +264 -0
- package/dist/reporting-catalogue.js +5 -0
- package/dist/response-lists.d.ts +16 -0
- package/dist/response-lists.generated.d.ts +7 -0
- package/dist/response-lists.generated.js +1927 -0
- package/dist/response-lists.js +70 -0
- package/dist/response-validation.d.ts +30 -0
- package/dist/response-validation.js +94 -0
- package/dist/schema-registry.d.ts +45 -0
- package/dist/schema-registry.js +9 -0
- package/dist/secure-url.d.ts +4 -0
- package/dist/secure-url.js +23 -0
- package/dist/site-ai-owner.d.ts +34 -0
- package/dist/site-ai-owner.js +99 -0
- package/dist/site-monitor-reports.d.ts +15 -0
- package/dist/site-monitor-reports.js +87 -0
- package/dist/testing/fake-custd.d.ts +150 -0
- package/dist/testing/fake-custd.js +842 -0
- package/dist/testing/fake-results.d.ts +30 -0
- package/dist/testing/fake-results.js +293 -0
- package/dist/testing.d.ts +2 -0
- package/dist/testing.js +3 -0
- package/dist/version.d.ts +2 -2
- package/dist/version.js +1 -1
- package/package.json +25 -14
- package/dist/browser-script-BvgjdZSO.js +0 -429
- package/dist/fixtures.d.ts +0 -10
- package/dist/fixtures.js +0 -57
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** Layout shifts closer together than this belong to one session window. */
|
|
2
|
+
const clsSessionGapMs = 1000;
|
|
3
|
+
/** A layout-shift session window never spans longer than this. */
|
|
4
|
+
const clsSessionMaxMs = 5000;
|
|
5
|
+
/** Interactions shorter than this cannot be the page's INP. */
|
|
6
|
+
const inpDurationThresholdMs = 40;
|
|
7
|
+
/** INP ignores one outlier for every this many interactions. */
|
|
8
|
+
const inpInteractionsPerOutlier = 50;
|
|
9
|
+
/** Bounds the per-document interaction memory. */
|
|
10
|
+
const maxTrackedInteractions = 1000;
|
|
11
|
+
export function startWebVitals() {
|
|
12
|
+
const supported = supportedEntryTypes();
|
|
13
|
+
const hiddenAtStart = typeof document !== "undefined" && document.visibilityState === "hidden";
|
|
14
|
+
const state = { lcp: undefined, fcp: undefined };
|
|
15
|
+
const cls = { value: 0, session: 0, first: 0, last: 0 };
|
|
16
|
+
const interactions = new Map();
|
|
17
|
+
const observers = [];
|
|
18
|
+
const observe = (type, onEntries, extra = {}) => {
|
|
19
|
+
if (!supported.includes(type)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const observer = new PerformanceObserver((list) => onEntries(list.getEntries()));
|
|
23
|
+
observer.observe({ type, buffered: true, ...extra });
|
|
24
|
+
observers.push(observer);
|
|
25
|
+
};
|
|
26
|
+
observe("largest-contentful-paint", (entries) => {
|
|
27
|
+
const last = entries[entries.length - 1];
|
|
28
|
+
if (last)
|
|
29
|
+
state.lcp = last.startTime;
|
|
30
|
+
});
|
|
31
|
+
observe("paint", (entries) => {
|
|
32
|
+
const fcp = entries.find((entry) => entry.name === "first-contentful-paint");
|
|
33
|
+
if (fcp)
|
|
34
|
+
state.fcp = fcp.startTime;
|
|
35
|
+
});
|
|
36
|
+
observe("layout-shift", (entries) => addLayoutShifts(cls, entries));
|
|
37
|
+
observe("event", (entries) => addInteractions(interactions, entries), {
|
|
38
|
+
durationThreshold: inpDurationThresholdMs,
|
|
39
|
+
});
|
|
40
|
+
observe("first-input", (entries) => addInteractions(interactions, entries));
|
|
41
|
+
return {
|
|
42
|
+
snapshot: () => {
|
|
43
|
+
const snapshot = {};
|
|
44
|
+
setMilliseconds(snapshot, "lcp", hiddenAtStart ? undefined : state.lcp);
|
|
45
|
+
setMilliseconds(snapshot, "fcp", hiddenAtStart ? undefined : state.fcp);
|
|
46
|
+
setMilliseconds(snapshot, "inp", interactionToNextPaint(interactions));
|
|
47
|
+
setMilliseconds(snapshot, "ttfb", timeToFirstByte());
|
|
48
|
+
if (supported.includes("layout-shift") && Number.isFinite(cls.value) && cls.value >= 0) {
|
|
49
|
+
snapshot.cls = Math.round(cls.value * 10000) / 10000;
|
|
50
|
+
}
|
|
51
|
+
return snapshot;
|
|
52
|
+
},
|
|
53
|
+
stop: () => {
|
|
54
|
+
for (const observer of observers.splice(0, observers.length)) {
|
|
55
|
+
observer.disconnect();
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function supportedEntryTypes() {
|
|
61
|
+
if (typeof PerformanceObserver === "undefined") {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
return PerformanceObserver.supportedEntryTypes ?? [];
|
|
65
|
+
}
|
|
66
|
+
/** CLS is the largest session window of shifts the visitor did not cause. */
|
|
67
|
+
function addLayoutShifts(cls, entries) {
|
|
68
|
+
for (const entry of entries) {
|
|
69
|
+
if (entry.hadRecentInput) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const continuesSession = cls.session > 0 && entry.startTime - cls.last < clsSessionGapMs && entry.startTime - cls.first < clsSessionMaxMs;
|
|
73
|
+
if (continuesSession) {
|
|
74
|
+
cls.session += entry.value;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
cls.session = entry.value;
|
|
78
|
+
cls.first = entry.startTime;
|
|
79
|
+
}
|
|
80
|
+
cls.last = entry.startTime;
|
|
81
|
+
cls.value = Math.max(cls.value, cls.session);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function addInteractions(interactions, entries) {
|
|
85
|
+
for (const entry of entries) {
|
|
86
|
+
const id = entry.interactionId ?? 0;
|
|
87
|
+
if (id === 0 || (!interactions.has(id) && interactions.size >= maxTrackedInteractions)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
interactions.set(id, Math.max(interactions.get(id) ?? 0, entry.duration));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** The slowest interaction, skipping one outlier per 50 interactions. */
|
|
94
|
+
function interactionToNextPaint(interactions) {
|
|
95
|
+
if (interactions.size === 0) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
const durations = [...interactions.values()].sort((a, b) => b - a);
|
|
99
|
+
const index = Math.min(Math.floor(interactions.size / inpInteractionsPerOutlier), durations.length - 1);
|
|
100
|
+
return durations[index];
|
|
101
|
+
}
|
|
102
|
+
function timeToFirstByte() {
|
|
103
|
+
if (typeof performance === "undefined" || typeof performance.getEntriesByType !== "function") {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
const navigation = performance.getEntriesByType("navigation")[0];
|
|
107
|
+
if (!navigation) {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
return navigation.responseStart - Math.max(navigation.activationStart ?? 0, 0);
|
|
111
|
+
}
|
|
112
|
+
function setMilliseconds(snapshot, key, value) {
|
|
113
|
+
if (value !== undefined && Number.isFinite(value) && value >= 0) {
|
|
114
|
+
snapshot[key] = Math.round(value);
|
|
115
|
+
}
|
|
116
|
+
}
|
package/dist/browser.d.ts
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
+
export * from "./app-account.js";
|
|
2
|
+
export * from "./app-alerts.js";
|
|
3
|
+
export * from "./app-items.js";
|
|
4
|
+
export { APP_LIVE_DEFAULT_INTERVAL_MS, APP_LIVE_STATUSES, type AppLiveState, type AppLiveStatus, type AppLiveWatch, type AppLiveWatchOptions, } from "./app-live.js";
|
|
5
|
+
export * from "./app-meetings.js";
|
|
6
|
+
export * from "./app-models.js";
|
|
7
|
+
export { APP_REALTIME_PATH, type AppWebSocket, type AppWebSocketFactory } from "./app-push.js";
|
|
1
8
|
export * from "./browser-tracker.js";
|
|
9
|
+
export { type CaptureExceptionRequest, ErrorCaptureClient, type ErrorCaptureConfig, type ErrorCaptureResult, installBrowserErrorCapture, } from "./error-capture.js";
|
|
10
|
+
export { APP_EVENT_OUTCOMES, type AppEventMap, type AppEventOutcome, type AppEventProperties, HOSTED_APP_CAPABILITIES, HOSTED_APP_DISCOVERY_PATH, HOSTED_APP_ERROR_CODES, HOSTED_APP_STATUSES, type HostedApp, type HostedAppCapability, type HostedAppDiscovery, type HostedAppDiscoverySignIn, HostedAppError, type HostedAppErrorCapture, type HostedAppErrorCaptureStart, type HostedAppErrorCode, type HostedAppOptions, type HostedAppStart, startHostedApp, } from "./hosted-app.js";
|
package/dist/browser.js
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
+
export * from "./app-account.js";
|
|
2
|
+
export * from "./app-alerts.js";
|
|
3
|
+
export * from "./app-items.js";
|
|
4
|
+
export { APP_LIVE_DEFAULT_INTERVAL_MS, APP_LIVE_STATUSES, } from "./app-live.js";
|
|
5
|
+
export * from "./app-meetings.js";
|
|
6
|
+
export * from "./app-models.js";
|
|
7
|
+
export { APP_REALTIME_PATH } from "./app-push.js";
|
|
1
8
|
export * from "./browser-tracker.js";
|
|
9
|
+
export { ErrorCaptureClient, installBrowserErrorCapture, } from "./error-capture.js";
|
|
10
|
+
export { APP_EVENT_OUTCOMES, HOSTED_APP_CAPABILITIES, HOSTED_APP_DISCOVERY_PATH, HOSTED_APP_ERROR_CODES, HOSTED_APP_STATUSES, HostedAppError, startHostedApp, } from "./hosted-app.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** A download read as it arrives, with the response metadata the server sent. */
|
|
2
|
+
export type CustdDownloadStream = {
|
|
3
|
+
/** The body. Reading it to the end checks any length the SDK knows; cancel it to stop early. */
|
|
4
|
+
body: ReadableStream<Uint8Array>;
|
|
5
|
+
/** The Content-Type header; empty when the server sent none. */
|
|
6
|
+
contentType: string;
|
|
7
|
+
/** The server's X-Request-Id, to quote when reporting a problem; empty when the server sent none. */
|
|
8
|
+
requestId: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Wraps a successful response as a stream. With `byteSize`, the body errors as soon as it runs past
|
|
12
|
+
* that length or ends short of it, so a truncated download never reads as complete.
|
|
13
|
+
*/
|
|
14
|
+
export declare function responseDownloadStream(response: Response, operation: string, byteSize?: number): CustdDownloadStream;
|
|
15
|
+
/** Reads a whole stream into memory, refusing one longer than `maxBytes`. */
|
|
16
|
+
export declare function readDownloadStream(body: ReadableStream<Uint8Array>, maxBytes: number, operation: string): Promise<Uint8Array>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Streaming downloads: the body is handed to the caller as it arrives, with the response
|
|
2
|
+
// metadata the server sent, so a large export never has to fit in memory.
|
|
3
|
+
/**
|
|
4
|
+
* Wraps a successful response as a stream. With `byteSize`, the body errors as soon as it runs past
|
|
5
|
+
* that length or ends short of it, so a truncated download never reads as complete.
|
|
6
|
+
*/
|
|
7
|
+
export function responseDownloadStream(response, operation, byteSize) {
|
|
8
|
+
const body = response.body ?? new ReadableStream({ start: (controller) => controller.close() });
|
|
9
|
+
return {
|
|
10
|
+
body: byteSize === undefined ? body : lengthCheckedBody(body, byteSize, operation),
|
|
11
|
+
contentType: response.headers.get("content-type") ?? "",
|
|
12
|
+
requestId: response.headers.get("x-request-id") ?? "",
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function lengthCheckedBody(body, byteSize, operation) {
|
|
16
|
+
let received = 0;
|
|
17
|
+
const mismatch = () => new Error(`custd: ${operation} content length mismatch`);
|
|
18
|
+
return body.pipeThrough(new TransformStream({
|
|
19
|
+
transform(chunk, controller) {
|
|
20
|
+
received += chunk.byteLength;
|
|
21
|
+
if (received > byteSize)
|
|
22
|
+
controller.error(mismatch());
|
|
23
|
+
else
|
|
24
|
+
controller.enqueue(chunk);
|
|
25
|
+
},
|
|
26
|
+
flush(controller) {
|
|
27
|
+
if (received !== byteSize)
|
|
28
|
+
controller.error(mismatch());
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
/** Reads a whole stream into memory, refusing one longer than `maxBytes`. */
|
|
33
|
+
export async function readDownloadStream(body, maxBytes, operation) {
|
|
34
|
+
const reader = body.getReader();
|
|
35
|
+
const chunks = [];
|
|
36
|
+
let total = 0;
|
|
37
|
+
while (true) {
|
|
38
|
+
const { done, value } = await reader.read();
|
|
39
|
+
if (done)
|
|
40
|
+
break;
|
|
41
|
+
total += value.byteLength;
|
|
42
|
+
if (total > maxBytes) {
|
|
43
|
+
await reader.cancel();
|
|
44
|
+
throw new Error(`custd: ${operation} exceeds ${maxBytes / (1024 * 1024)} MiB`);
|
|
45
|
+
}
|
|
46
|
+
chunks.push(value);
|
|
47
|
+
}
|
|
48
|
+
const result = new Uint8Array(total);
|
|
49
|
+
let offset = 0;
|
|
50
|
+
for (const chunk of chunks) {
|
|
51
|
+
result.set(chunk, offset);
|
|
52
|
+
offset += chunk.byteLength;
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** A registered browser Site and a server-only Site must use different write keys. */
|
|
2
|
+
export interface ErrorCaptureConfig {
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
writeKey: string;
|
|
5
|
+
environment: string;
|
|
6
|
+
release?: string;
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
}
|
|
9
|
+
export interface CaptureExceptionRequest {
|
|
10
|
+
error: unknown;
|
|
11
|
+
mode?: "handled" | "unhandled";
|
|
12
|
+
/** Application-owned ULID; no user, account or request content. */
|
|
13
|
+
correlationId?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface ErrorCaptureResult {
|
|
16
|
+
outcome: "accepted" | "unavailable" | "rejected" | "limited";
|
|
17
|
+
status?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface ErrorFrame {
|
|
20
|
+
filename: string;
|
|
21
|
+
lineno: number;
|
|
22
|
+
colno?: number;
|
|
23
|
+
}
|
|
24
|
+
/** Sanitized app-to-server relay contract. Credentials and raw error content never enter it. */
|
|
25
|
+
export interface ErrorCaptureEvent {
|
|
26
|
+
eventId?: string;
|
|
27
|
+
platform: "javascript" | "go";
|
|
28
|
+
exceptionType: string;
|
|
29
|
+
frames: ErrorFrame[];
|
|
30
|
+
mode: "handled" | "unhandled";
|
|
31
|
+
environment: string;
|
|
32
|
+
release?: string;
|
|
33
|
+
correlationId?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface ErrorCaptureEventConfig {
|
|
36
|
+
environment: string;
|
|
37
|
+
release?: string;
|
|
38
|
+
eventId?: string;
|
|
39
|
+
}
|
|
40
|
+
/** Pure formatter for authenticated application relays, including Expo without browser globals. */
|
|
41
|
+
export declare function createErrorCaptureEvent(request: CaptureExceptionRequest, config: ErrorCaptureEventConfig): ErrorCaptureEvent;
|
|
42
|
+
/** Native Custd collector. No global scope, persisted queue, retries or third-party collector. */
|
|
43
|
+
export declare class ErrorCaptureClient {
|
|
44
|
+
private readonly config;
|
|
45
|
+
private readonly origin;
|
|
46
|
+
private readonly send;
|
|
47
|
+
private captures;
|
|
48
|
+
private concurrent;
|
|
49
|
+
constructor(config: ErrorCaptureConfig);
|
|
50
|
+
/** Always resolves: optional diagnostics never changes the operation that failed. */
|
|
51
|
+
captureException(request: CaptureExceptionRequest): Promise<ErrorCaptureResult>;
|
|
52
|
+
}
|
|
53
|
+
/** Install once at the application boundary; close it when that application unmounts. */
|
|
54
|
+
export declare function installBrowserErrorCapture(client: Pick<ErrorCaptureClient, "captureException">): () => void;
|
|
55
|
+
export type NativeErrorHandler = (error: unknown, fatal?: boolean) => void;
|
|
56
|
+
/** React Native ErrorUtils-shaped seam; no native dependencies or global patching. */
|
|
57
|
+
export interface NativeErrorSource {
|
|
58
|
+
getGlobalHandler(): NativeErrorHandler;
|
|
59
|
+
setGlobalHandler(handler: NativeErrorHandler): void;
|
|
60
|
+
}
|
|
61
|
+
/** Capture delivery remains bounded by the caller; native error handling always runs. */
|
|
62
|
+
export declare function installNativeErrorCapture(source: NativeErrorSource, capture: (request: CaptureExceptionRequest) => unknown): () => void;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { SDK_VERSION } from "./version.js";
|
|
2
|
+
/** Pure formatter for authenticated application relays, including Expo without browser globals. */
|
|
3
|
+
export function createErrorCaptureEvent(request, config) {
|
|
4
|
+
if (!/^[a-zA-Z0-9._-]{1,64}$/u.test(config.environment) ||
|
|
5
|
+
(config.release !== undefined && !/^[a-zA-Z0-9@._+-]{1,200}$/u.test(config.release)) ||
|
|
6
|
+
(config.eventId !== undefined && !/^[a-f0-9]{32}$/u.test(config.eventId))) {
|
|
7
|
+
throw new Error("custd: invalid error event configuration");
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
...(config.eventId === undefined ? {} : { eventId: config.eventId }),
|
|
11
|
+
platform: "javascript",
|
|
12
|
+
exceptionType: safeExceptionType(request.error),
|
|
13
|
+
frames: exceptionFrames(request.error),
|
|
14
|
+
mode: request.mode === "unhandled" ? "unhandled" : "handled",
|
|
15
|
+
environment: config.environment,
|
|
16
|
+
...(config.release === undefined ? {} : { release: config.release }),
|
|
17
|
+
...(request.correlationId && ulid.test(request.correlationId) ? { correlationId: request.correlationId } : {}),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const maxCaptures = 20;
|
|
21
|
+
const maxConcurrent = 4;
|
|
22
|
+
const maxFrames = 24;
|
|
23
|
+
const maxEnvelopeBytes = 16384;
|
|
24
|
+
const ulid = /^[0-7][0-9A-HJKMNP-TV-Z]{25}$/u;
|
|
25
|
+
/** Native Custd collector. No global scope, persisted queue, retries or third-party collector. */
|
|
26
|
+
export class ErrorCaptureClient {
|
|
27
|
+
constructor(config) {
|
|
28
|
+
this.config = config;
|
|
29
|
+
this.captures = 0;
|
|
30
|
+
this.concurrent = 0;
|
|
31
|
+
const url = new URL(config.baseUrl);
|
|
32
|
+
const local = ["localhost", "127.0.0.1", "[::1]", "host.docker.internal"].includes(url.hostname);
|
|
33
|
+
if ((url.protocol !== "https:" && !(url.protocol === "http:" && local)) ||
|
|
34
|
+
url.username ||
|
|
35
|
+
url.password ||
|
|
36
|
+
url.search ||
|
|
37
|
+
url.hash ||
|
|
38
|
+
url.pathname !== "/" ||
|
|
39
|
+
!/^site_pk_[A-Za-z0-9_-]{32,128}$/u.test(config.writeKey) ||
|
|
40
|
+
!/^[a-zA-Z0-9._-]{1,64}$/u.test(config.environment) ||
|
|
41
|
+
(config.release !== undefined && !/^[a-zA-Z0-9@._+-]{1,200}$/u.test(config.release))) {
|
|
42
|
+
throw new Error("custd: invalid error capture configuration");
|
|
43
|
+
}
|
|
44
|
+
this.origin = url.origin;
|
|
45
|
+
this.send = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
46
|
+
}
|
|
47
|
+
/** Always resolves: optional diagnostics never changes the operation that failed. */
|
|
48
|
+
async captureException(request) {
|
|
49
|
+
if (this.captures >= maxCaptures || this.concurrent >= maxConcurrent)
|
|
50
|
+
return { outcome: "limited" };
|
|
51
|
+
this.captures += 1;
|
|
52
|
+
this.concurrent += 1;
|
|
53
|
+
try {
|
|
54
|
+
const id = crypto.randomUUID().replace(/-/gu, "");
|
|
55
|
+
const capture = createErrorCaptureEvent(request, { ...this.config, eventId: id });
|
|
56
|
+
const header = {
|
|
57
|
+
event_id: id,
|
|
58
|
+
dsn: `${this.origin.replace("://", `://${this.config.writeKey}@`)}/api/v1/errors/1`,
|
|
59
|
+
};
|
|
60
|
+
const event = {
|
|
61
|
+
event_id: id,
|
|
62
|
+
timestamp: new Date().toISOString(),
|
|
63
|
+
platform: "javascript",
|
|
64
|
+
level: "error",
|
|
65
|
+
environment: capture.environment,
|
|
66
|
+
release: capture.release,
|
|
67
|
+
sdk: { name: "custd.javascript", version: SDK_VERSION },
|
|
68
|
+
tags: capture.correlationId ? { correlation_id: capture.correlationId } : undefined,
|
|
69
|
+
exception: {
|
|
70
|
+
values: [
|
|
71
|
+
{
|
|
72
|
+
type: capture.exceptionType,
|
|
73
|
+
mechanism: { handled: capture.mode !== "unhandled" },
|
|
74
|
+
stacktrace: { frames: capture.frames },
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
const body = [JSON.stringify(header), '{"type":"event"}', JSON.stringify(event)].join("\n");
|
|
80
|
+
if (new TextEncoder().encode(body).length > maxEnvelopeBytes)
|
|
81
|
+
return { outcome: "limited" };
|
|
82
|
+
const response = await this.send(`${this.origin}/api/v1/errors/api/1/envelope/`, {
|
|
83
|
+
method: "POST",
|
|
84
|
+
body,
|
|
85
|
+
headers: { "Content-Type": "text/plain" },
|
|
86
|
+
credentials: "omit",
|
|
87
|
+
redirect: "error",
|
|
88
|
+
referrerPolicy: "no-referrer",
|
|
89
|
+
signal: AbortSignal.timeout(2000),
|
|
90
|
+
});
|
|
91
|
+
return { outcome: response.ok ? "accepted" : "rejected", status: response.status };
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return { outcome: "unavailable" };
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
this.concurrent -= 1;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function safeExceptionType(error) {
|
|
102
|
+
return error instanceof Error && /^[A-Za-z_$][A-Za-z0-9_$.]{0,127}$/u.test(error.name) ? error.name : "Error";
|
|
103
|
+
}
|
|
104
|
+
/** Drop the message line and retain only bounded code locations, never full URLs or local paths. */
|
|
105
|
+
function exceptionFrames(error) {
|
|
106
|
+
if (!(error instanceof Error) || typeof error.stack !== "string")
|
|
107
|
+
return [];
|
|
108
|
+
return error.stack
|
|
109
|
+
.slice(0, 32768)
|
|
110
|
+
.split("\n")
|
|
111
|
+
.slice(1, maxFrames + 1)
|
|
112
|
+
.flatMap((line) => {
|
|
113
|
+
const location = line.match(/(?:\(|@|\s)((?:https?:\/\/|file:\/\/|\/)?[^\s]+):(\d+):(\d+)\)?$/u);
|
|
114
|
+
if (!location)
|
|
115
|
+
return [];
|
|
116
|
+
const path = location[1].replace(/[?#].*$/u, "");
|
|
117
|
+
let filename;
|
|
118
|
+
try {
|
|
119
|
+
filename = decodeURIComponent(path.split(/[\\/]/u).pop() ?? "");
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
if (!filename || !/^[A-Za-z0-9_.+[\]-]{1,128}\.(?:[cm]?js|jsx|tsx?|bundle)$/u.test(filename))
|
|
125
|
+
return [];
|
|
126
|
+
return [
|
|
127
|
+
{
|
|
128
|
+
filename,
|
|
129
|
+
lineno: Math.min(Number(location[2]), 1000000),
|
|
130
|
+
colno: Math.min(Number(location[3]), 2147483647),
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
})
|
|
134
|
+
.reverse();
|
|
135
|
+
}
|
|
136
|
+
/** Install once at the application boundary; close it when that application unmounts. */
|
|
137
|
+
export function installBrowserErrorCapture(client) {
|
|
138
|
+
const onError = (event) => {
|
|
139
|
+
void client.captureException({ error: event.error, mode: "unhandled" });
|
|
140
|
+
};
|
|
141
|
+
const onRejection = (event) => {
|
|
142
|
+
void client.captureException({ error: event.reason, mode: "unhandled" });
|
|
143
|
+
};
|
|
144
|
+
window.addEventListener("error", onError);
|
|
145
|
+
window.addEventListener("unhandledrejection", onRejection);
|
|
146
|
+
return () => {
|
|
147
|
+
window.removeEventListener("error", onError);
|
|
148
|
+
window.removeEventListener("unhandledrejection", onRejection);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** Capture delivery remains bounded by the caller; native error handling always runs. */
|
|
152
|
+
export function installNativeErrorCapture(source, capture) {
|
|
153
|
+
const original = source.getGlobalHandler();
|
|
154
|
+
const handler = (error, fatal) => {
|
|
155
|
+
try {
|
|
156
|
+
void Promise.resolve(capture({ error, mode: "unhandled" })).catch(() => undefined);
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
// Optional capture cannot prevent the native handler from reporting the failure.
|
|
160
|
+
}
|
|
161
|
+
original(error, fatal);
|
|
162
|
+
};
|
|
163
|
+
source.setGlobalHandler(handler);
|
|
164
|
+
return () => {
|
|
165
|
+
if (source.getGlobalHandler() === handler)
|
|
166
|
+
source.setGlobalHandler(original);
|
|
167
|
+
};
|
|
168
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { AppItemsClient } from "./app-items.js";
|
|
2
|
+
import type { AppWebSocketFactory } from "./app-push.js";
|
|
3
|
+
import { BrowserSignIn, type CustdDiscoveryDocument, type CustdDiscoverySignIn, type SignInStorage } from "./browser-sign-in.js";
|
|
4
|
+
import { type BrowserIdentityMode, type BrowserTracker, type BrowserTrackerConfig } from "./browser-tracker.js";
|
|
5
|
+
import { ErrorCaptureClient, type ErrorCaptureConfig } from "./error-capture.js";
|
|
6
|
+
export declare const HOSTED_APP_DISCOVERY_PATH = "/.well-known/custd.json";
|
|
7
|
+
export declare const HOSTED_APP_CAPABILITIES: {
|
|
8
|
+
readonly tracking: "tracking";
|
|
9
|
+
readonly errorCapture: "error_capture";
|
|
10
|
+
readonly appData: "app_data";
|
|
11
|
+
readonly signIn: "sign_in";
|
|
12
|
+
};
|
|
13
|
+
export type HostedAppCapability = (typeof HOSTED_APP_CAPABILITIES)[keyof typeof HOSTED_APP_CAPABILITIES];
|
|
14
|
+
/** The hosted environment's sign-in block: its own directory's OAuth public client. */
|
|
15
|
+
export type HostedAppDiscoverySignIn = CustdDiscoverySignIn & {
|
|
16
|
+
audience: string;
|
|
17
|
+
redirectUri: string;
|
|
18
|
+
};
|
|
19
|
+
/** `/.well-known/custd.json` as Custd serves it for a hosted environment. */
|
|
20
|
+
export type HostedAppDiscovery = CustdDiscoveryDocument & {
|
|
21
|
+
apiBaseUrl: string;
|
|
22
|
+
host: string;
|
|
23
|
+
siteUuid: string;
|
|
24
|
+
/** The Site's public browser write key (`site_pk_...`). */
|
|
25
|
+
browserKey: string;
|
|
26
|
+
environment: string;
|
|
27
|
+
/** Capabilities this SDK knows; unknown values from a newer backend are dropped. */
|
|
28
|
+
capabilities: HostedAppCapability[];
|
|
29
|
+
signIn?: HostedAppDiscoverySignIn;
|
|
30
|
+
/** The Site's public config; when both are present the tracker starts without fetching it. */
|
|
31
|
+
identityMode?: BrowserIdentityMode;
|
|
32
|
+
allowedOrigins?: string[];
|
|
33
|
+
pageViewSchemaVersion?: string;
|
|
34
|
+
};
|
|
35
|
+
export declare const HOSTED_APP_ERROR_CODES: {
|
|
36
|
+
readonly discoveryUnavailable: "discovery_unavailable";
|
|
37
|
+
readonly invalidDiscovery: "invalid_discovery";
|
|
38
|
+
readonly siteConfigUnavailable: "site_config_unavailable";
|
|
39
|
+
readonly capabilityFailed: "capability_failed";
|
|
40
|
+
};
|
|
41
|
+
export type HostedAppErrorCode = (typeof HOSTED_APP_ERROR_CODES)[keyof typeof HOSTED_APP_ERROR_CODES];
|
|
42
|
+
export declare class HostedAppError extends Error {
|
|
43
|
+
readonly code: HostedAppErrorCode;
|
|
44
|
+
readonly capability?: HostedAppCapability | undefined;
|
|
45
|
+
readonly status?: number | undefined;
|
|
46
|
+
/** @param capability The capability that failed to start; absent when discovery itself failed. */
|
|
47
|
+
constructor(code: HostedAppErrorCode, message: string, capability?: HostedAppCapability | undefined, status?: number | undefined);
|
|
48
|
+
}
|
|
49
|
+
/** Typed event properties: flat scalar values, as registered event schemas declare them. */
|
|
50
|
+
export type AppEventProperties = Record<string, string | number | boolean | null>;
|
|
51
|
+
/** Maps each registered app event type slug to its properties. Declare it with `type`, not `interface`. */
|
|
52
|
+
export type AppEventMap = Record<string, AppEventProperties>;
|
|
53
|
+
export declare const APP_EVENT_OUTCOMES: {
|
|
54
|
+
/** Handed to the tracker, which still applies consent and Do Not Track. */
|
|
55
|
+
readonly tracked: "tracked";
|
|
56
|
+
/** Tracking is not enabled or did not start; nothing was sent. */
|
|
57
|
+
readonly trackingOff: "tracking_off";
|
|
58
|
+
/** The collector or network refused the event. */
|
|
59
|
+
readonly failed: "failed";
|
|
60
|
+
};
|
|
61
|
+
export type AppEventOutcome = (typeof APP_EVENT_OUTCOMES)[keyof typeof APP_EVENT_OUTCOMES];
|
|
62
|
+
/** What a hosted app's error capture offers: report one error. Always resolves. */
|
|
63
|
+
export type HostedAppErrorCapture = Pick<ErrorCaptureClient, "captureException">;
|
|
64
|
+
/** Started error capture and how to remove the page handlers it installed. */
|
|
65
|
+
export type HostedAppErrorCaptureStart = {
|
|
66
|
+
client: HostedAppErrorCapture;
|
|
67
|
+
uninstall: () => void;
|
|
68
|
+
};
|
|
69
|
+
export type HostedApp<TEvents extends AppEventMap = AppEventMap> = {
|
|
70
|
+
discovery: HostedAppDiscovery;
|
|
71
|
+
tracker?: BrowserTracker;
|
|
72
|
+
errorCapture?: HostedAppErrorCapture;
|
|
73
|
+
/** Present when sign-in is enabled. Call `restore()` on page load (the React provider does). */
|
|
74
|
+
signIn?: BrowserSignIn;
|
|
75
|
+
/** Present when app data is enabled: create, read and change the app's items. */
|
|
76
|
+
appItems?: AppItemsClient;
|
|
77
|
+
/** Capabilities the discovery file enabled that could not start. The app keeps working. */
|
|
78
|
+
problems: HostedAppError[];
|
|
79
|
+
/** Sends a typed app event through the tracker. Never rejects. */
|
|
80
|
+
trackAppEvent: <TName extends keyof TEvents & string>(name: TName, properties: TEvents[TName]) => Promise<AppEventOutcome>;
|
|
81
|
+
/** Stops tracking and error capture. Safe to call more than once. */
|
|
82
|
+
stop: () => void;
|
|
83
|
+
};
|
|
84
|
+
export declare const HOSTED_APP_STATUSES: {
|
|
85
|
+
readonly ready: "ready";
|
|
86
|
+
readonly error: "error";
|
|
87
|
+
};
|
|
88
|
+
export type HostedAppStart<TEvents extends AppEventMap = AppEventMap> = {
|
|
89
|
+
status: typeof HOSTED_APP_STATUSES.ready;
|
|
90
|
+
app: HostedApp<TEvents>;
|
|
91
|
+
} | {
|
|
92
|
+
status: typeof HOSTED_APP_STATUSES.error;
|
|
93
|
+
error: HostedAppError;
|
|
94
|
+
};
|
|
95
|
+
export type HostedAppOptions = {
|
|
96
|
+
/** Defaults to `/.well-known/custd.json` on the page's own origin. */
|
|
97
|
+
discoveryUrl?: string;
|
|
98
|
+
/** Used for discovery, site config, error capture and sign-in; the tracker uses the global fetch. */
|
|
99
|
+
fetch?: typeof fetch;
|
|
100
|
+
/** Opens the realtime socket for watched app reads; see `AppItemsClientConfig.webSocket`. */
|
|
101
|
+
webSocket?: AppWebSocketFactory | null;
|
|
102
|
+
/** Release reported with captured errors, such as the build's commit. */
|
|
103
|
+
release?: string;
|
|
104
|
+
/** Overrides the consent default the Site's identity mode sets. */
|
|
105
|
+
consent?: BrowserTrackerConfig["consent"];
|
|
106
|
+
pageContext?: BrowserTrackerConfig["pageContext"];
|
|
107
|
+
signInScopes?: string[];
|
|
108
|
+
signInStorage?: SignInStorage;
|
|
109
|
+
/** Builds the tracker; defaults to the browser tracker. `@haakco/custd-sdk/testing` records instead. */
|
|
110
|
+
createTracker?: (config: BrowserTrackerConfig) => BrowserTracker;
|
|
111
|
+
/** Starts error capture; defaults to the page's error handlers. `@haakco/custd-sdk/testing` records instead. */
|
|
112
|
+
startErrorCapture?: (config: ErrorCaptureConfig) => HostedAppErrorCaptureStart;
|
|
113
|
+
};
|
|
114
|
+
/** Reads the hosted app's discovery file and starts what it enables. Never rejects. */
|
|
115
|
+
export declare function startHostedApp<TEvents extends AppEventMap = AppEventMap>(options?: HostedAppOptions): Promise<HostedAppStart<TEvents>>;
|