@haakco/custd-sdk 0.0.0-stage → 3.71.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 +958 -2
- 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 +122 -0
- package/dist/admin-client-setup.js +408 -0
- package/dist/admin-data-labels.d.ts +124 -0
- package/dist/admin-data-labels.js +51 -0
- 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 +123 -0
- package/dist/admin-offboarding.js +129 -0
- package/dist/admin-predictions.d.ts +266 -0
- package/dist/admin-predictions.js +93 -0
- package/dist/admin-privacy-erasures.d.ts +74 -0
- package/dist/admin-privacy-erasures.js +73 -0
- package/dist/admin-retention.d.ts +99 -0
- package/dist/admin-retention.js +60 -0
- 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 +79 -0
- package/dist/admin-subject-exports.js +44 -0
- 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 +35 -0
- package/dist/admin-tenant-storage.js +24 -0
- package/dist/admin-timeplans.d.ts +512 -0
- package/dist/admin-timeplans.js +200 -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 +167 -0
- package/dist/analytics-events.js +197 -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 +85 -0
- package/dist/backend-lifecycle.js +212 -0
- package/dist/browser-manifest.json +7 -0
- package/dist/browser-script-BWnwDjAC.js +711 -0
- package/dist/browser-script.d.ts +1 -0
- package/dist/browser-script.js +5 -0
- package/dist/browser-sign-in.d.ts +153 -0
- package/dist/browser-sign-in.js +410 -0
- package/dist/browser-tracker.d.ts +142 -0
- package/dist/browser-tracker.js +775 -0
- package/dist/browser-web-vitals.d.ts +22 -0
- package/dist/browser-web-vitals.js +116 -0
- package/dist/browser.d.ts +10 -0
- package/dist/browser.js +10 -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 +2324 -0
- package/dist/index.js +2226 -0
- 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/mobile-adapter.d.ts +25 -0
- package/dist/mobile-adapter.js +57 -0
- package/dist/mobile-context.d.ts +32 -0
- package/dist/mobile-context.js +156 -0
- package/dist/mobile-queue.d.ts +22 -0
- package/dist/mobile-queue.js +73 -0
- package/dist/oauth.d.ts +1 -0
- package/dist/oauth.js +8 -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/reporting-state.d.ts +46 -0
- package/dist/reporting-state.js +76 -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/runtime-readiness.d.ts +30 -0
- package/dist/runtime-readiness.js +150 -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 +10 -0
- package/dist/version.js +10 -0
- package/package.json +57 -4
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Uncertainty model items: a named set of quantities, each an input estimate or a formula over
|
|
2
|
+
// other quantities, saved as immutable numbered versions (Admin API /api/v1/app/item/model) and
|
|
3
|
+
// run as seeded Monte Carlo (Analytics API /api/v1/app/models). The JSON names match the routes.
|
|
4
|
+
export const APP_MODEL_QUANTITY_KINDS = {
|
|
5
|
+
input: "input",
|
|
6
|
+
formula: "formula",
|
|
7
|
+
};
|
|
8
|
+
export const APP_MODEL_INPUT_SHAPES = {
|
|
9
|
+
/** One known value. */
|
|
10
|
+
point: "point",
|
|
11
|
+
/** A low and high read as a 90% interval. */
|
|
12
|
+
range: "range",
|
|
13
|
+
/** k successes of n trials, read as a beta distribution. */
|
|
14
|
+
proportion: "proportion",
|
|
15
|
+
/** Pasted data used as an empirical distribution. */
|
|
16
|
+
samples: "samples",
|
|
17
|
+
/** A live input read from its source when the model runs. */
|
|
18
|
+
bound: "bound",
|
|
19
|
+
};
|
|
20
|
+
export const APP_MODEL_RANGE_DISTRIBUTIONS = {
|
|
21
|
+
normal: "normal",
|
|
22
|
+
lognormal: "lognormal",
|
|
23
|
+
uniform: "uniform",
|
|
24
|
+
};
|
|
25
|
+
export const APP_MODEL_SOURCE_KINDS = {
|
|
26
|
+
/** The latest completed counter forecast of a measurement project. */
|
|
27
|
+
counterForecast: "counter_forecast",
|
|
28
|
+
};
|
|
29
|
+
export const APP_MODEL_FORECAST_FIELDS = {
|
|
30
|
+
completionDays: "completion_days",
|
|
31
|
+
deadlineProbability: "deadline_probability",
|
|
32
|
+
ratePerDay: "rate_per_day",
|
|
33
|
+
};
|
|
34
|
+
/** Problem codes of the model routes, beside the app item codes. */
|
|
35
|
+
export const APP_MODEL_ERROR_CODES = {
|
|
36
|
+
versionLimit: "app_item_model_version_limit",
|
|
37
|
+
runMissing: "app_model_run_missing",
|
|
38
|
+
runInvalid: "uncertainty_model_run_invalid",
|
|
39
|
+
/** The quantities reference each other in a loop; `fieldErrors` names each one in it. */
|
|
40
|
+
formulaCycle: "formula_cycle",
|
|
41
|
+
/** Too many calculations from this caller; wait and try again. */
|
|
42
|
+
calculateLimited: "app_model_calculate_limited",
|
|
43
|
+
};
|
|
44
|
+
export const APP_MODEL_RUN_TRIGGERS = {
|
|
45
|
+
request: "request",
|
|
46
|
+
sourceChanged: "source_changed",
|
|
47
|
+
};
|
|
48
|
+
export const APP_MODEL_BOUND_STATES = {
|
|
49
|
+
resolved: "resolved",
|
|
50
|
+
sourceMissing: "source_missing",
|
|
51
|
+
valueMissing: "value_missing",
|
|
52
|
+
};
|
|
53
|
+
// ---- Summary CSV ----
|
|
54
|
+
const CSV_COLUMNS = ["quantity", "unit", "mean", "std_dev", "p5", "p25", "p50", "p75", "p95", "error"];
|
|
55
|
+
/**
|
|
56
|
+
* One row per quantity of a run: mean, spread and percentiles, or the reason it failed. Units come
|
|
57
|
+
* from the model's quantities when given. Text cells are quoted and cells a spreadsheet would read
|
|
58
|
+
* as a formula are prefixed with `'`.
|
|
59
|
+
*/
|
|
60
|
+
export function modelRunSummaryCsv(run, quantities = []) {
|
|
61
|
+
const units = new Map(quantities.map((quantity) => [quantity.name, quantity.unit ?? ""]));
|
|
62
|
+
const rows = (run.quantities ?? []).map((result) => {
|
|
63
|
+
const summary = result.summary;
|
|
64
|
+
const numbers = summary
|
|
65
|
+
? [
|
|
66
|
+
summary.mean,
|
|
67
|
+
summary.stdDev,
|
|
68
|
+
summary.percentiles.p5,
|
|
69
|
+
summary.percentiles.p25,
|
|
70
|
+
summary.percentiles.p50,
|
|
71
|
+
summary.percentiles.p75,
|
|
72
|
+
summary.percentiles.p95,
|
|
73
|
+
].map(String)
|
|
74
|
+
: ["", "", "", "", "", "", ""];
|
|
75
|
+
const error = result.error ? `${result.error.code}: ${result.error.detail}` : "";
|
|
76
|
+
return [csvText(result.name), csvText(units.get(result.name) ?? ""), ...numbers, csvText(error)].join(",");
|
|
77
|
+
});
|
|
78
|
+
return `${[CSV_COLUMNS.join(","), ...rows].join("\r\n")}\r\n`;
|
|
79
|
+
}
|
|
80
|
+
function csvText(value) {
|
|
81
|
+
const safe = /^[=+\-@\t\r]/.test(value) ? `'${value}` : value;
|
|
82
|
+
return /[",\r\n]/.test(safe) ? `"${safe.replace(/"/g, '""')}"` : safe;
|
|
83
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** The part of the browser WebSocket this channel uses. */
|
|
2
|
+
export type AppWebSocket = {
|
|
3
|
+
readonly readyState: number;
|
|
4
|
+
onopen: ((event: unknown) => void) | null;
|
|
5
|
+
onmessage: ((event: {
|
|
6
|
+
data: unknown;
|
|
7
|
+
}) => void) | null;
|
|
8
|
+
onclose: ((event: {
|
|
9
|
+
code: number;
|
|
10
|
+
}) => void) | null;
|
|
11
|
+
onerror: ((event: unknown) => void) | null;
|
|
12
|
+
send(data: string): void;
|
|
13
|
+
close(code?: number, reason?: string): void;
|
|
14
|
+
};
|
|
15
|
+
/** Builds a socket for a URL; `globalThis.WebSocket` is the default. */
|
|
16
|
+
export type AppWebSocketFactory = (url: string) => AppWebSocket;
|
|
17
|
+
/** What one watched read sends when it subscribes. Tokens are read at every (re)subscribe. */
|
|
18
|
+
export type AppPushTarget = {
|
|
19
|
+
linkToken?: string;
|
|
20
|
+
item?: string;
|
|
21
|
+
userToken: () => string | undefined;
|
|
22
|
+
};
|
|
23
|
+
/** What the channel tells a watched read. */
|
|
24
|
+
export type AppPushEvents = {
|
|
25
|
+
/** The item changed, or the subscription (re)started: poll now. */
|
|
26
|
+
changed: () => void;
|
|
27
|
+
/** Push is carrying this read (true) or not (false); while false the read polls on its timer. */
|
|
28
|
+
pushed: (on: boolean) => void;
|
|
29
|
+
};
|
|
30
|
+
export declare const APP_REALTIME_PATH = "/api/v1/realtime";
|
|
31
|
+
export declare class AppPushChannel {
|
|
32
|
+
private readonly url;
|
|
33
|
+
private readonly siteKey;
|
|
34
|
+
private readonly createSocket;
|
|
35
|
+
private readonly random;
|
|
36
|
+
private readonly entries;
|
|
37
|
+
private socket;
|
|
38
|
+
private ready;
|
|
39
|
+
private refused;
|
|
40
|
+
private retries;
|
|
41
|
+
private retryTimer;
|
|
42
|
+
private nextName;
|
|
43
|
+
constructor(url: string, siteKey: string, createSocket: AppWebSocketFactory, random?: () => number);
|
|
44
|
+
/** Starts pushing changes for one watched read; the returned function stops it. */
|
|
45
|
+
watch(target: AppPushTarget, events: AppPushEvents): () => void;
|
|
46
|
+
/**
|
|
47
|
+
* Resubscribes every read whose signed-in user's token changed since it subscribed, so a
|
|
48
|
+
* sign-in, refresh or sign-out does not leave the server checking a token the page dropped.
|
|
49
|
+
*/
|
|
50
|
+
refreshCredentials(): void;
|
|
51
|
+
private unwatch;
|
|
52
|
+
private connect;
|
|
53
|
+
private disconnect;
|
|
54
|
+
private receive;
|
|
55
|
+
private subscribe;
|
|
56
|
+
private closed;
|
|
57
|
+
private scheduleReconnect;
|
|
58
|
+
private clearRetry;
|
|
59
|
+
private sendFrame;
|
|
60
|
+
}
|
|
61
|
+
/** The realtime URL for an API origin: the same host, over ws(s). */
|
|
62
|
+
export declare function appRealtimeUrl(apiBaseUrl: string): string;
|
|
63
|
+
/** The global WebSocket as a factory, or undefined where there is none. */
|
|
64
|
+
export declare function globalWebSocketFactory(): AppWebSocketFactory | undefined;
|
package/dist/app-push.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Pushed "item changed" for watched app reads. One WebSocket per client carries every watched
|
|
2
|
+
// item: it says hello with the Site key, subscribes each watched read with its link or user
|
|
3
|
+
// token, and on `subscribed` or `item.changed` asks that read's poller to poll now, so the read
|
|
4
|
+
// itself still goes through the conditional GET. While the socket is down or refused, the poller's
|
|
5
|
+
// own timer runs as before; the socket reconnects with backoff and resubscribes everything.
|
|
6
|
+
export const APP_REALTIME_PATH = "/api/v1/realtime";
|
|
7
|
+
const OPEN = 1;
|
|
8
|
+
const MIN_RETRY_MS = 1000;
|
|
9
|
+
const MAX_RETRY_MS = 30000;
|
|
10
|
+
/** The server refused the hello or the frame: retrying cannot help until the page reloads. */
|
|
11
|
+
const CLOSE_REFUSED = 1008;
|
|
12
|
+
export class AppPushChannel {
|
|
13
|
+
constructor(url, siteKey, createSocket, random = Math.random) {
|
|
14
|
+
this.url = url;
|
|
15
|
+
this.siteKey = siteKey;
|
|
16
|
+
this.createSocket = createSocket;
|
|
17
|
+
this.random = random;
|
|
18
|
+
this.entries = new Map();
|
|
19
|
+
this.ready = false;
|
|
20
|
+
this.refused = false;
|
|
21
|
+
this.retries = 0;
|
|
22
|
+
this.nextName = 0;
|
|
23
|
+
}
|
|
24
|
+
/** Starts pushing changes for one watched read; the returned function stops it. */
|
|
25
|
+
watch(target, events) {
|
|
26
|
+
this.nextName += 1;
|
|
27
|
+
const entry = { name: `w${this.nextName}`, target, events };
|
|
28
|
+
this.entries.set(entry.name, entry);
|
|
29
|
+
if (this.ready)
|
|
30
|
+
this.subscribe(entry);
|
|
31
|
+
else
|
|
32
|
+
this.connect();
|
|
33
|
+
return () => this.unwatch(entry);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Resubscribes every read whose signed-in user's token changed since it subscribed, so a
|
|
37
|
+
* sign-in, refresh or sign-out does not leave the server checking a token the page dropped.
|
|
38
|
+
*/
|
|
39
|
+
refreshCredentials() {
|
|
40
|
+
if (!this.ready)
|
|
41
|
+
return;
|
|
42
|
+
for (const entry of this.entries.values()) {
|
|
43
|
+
if (entry.target.userToken() === entry.sentUserToken)
|
|
44
|
+
continue;
|
|
45
|
+
this.sendFrame({ type: "unsubscribe", subscription: entry.name });
|
|
46
|
+
this.subscribe(entry);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
unwatch(entry) {
|
|
50
|
+
if (!this.entries.delete(entry.name))
|
|
51
|
+
return;
|
|
52
|
+
if (this.ready)
|
|
53
|
+
this.sendFrame({ type: "unsubscribe", subscription: entry.name });
|
|
54
|
+
if (this.entries.size === 0)
|
|
55
|
+
this.disconnect();
|
|
56
|
+
}
|
|
57
|
+
connect() {
|
|
58
|
+
if (this.socket || this.retryTimer || this.refused || this.entries.size === 0)
|
|
59
|
+
return;
|
|
60
|
+
let socket;
|
|
61
|
+
try {
|
|
62
|
+
socket = this.createSocket(this.url);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
this.refused = true;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.socket = socket;
|
|
69
|
+
socket.onopen = () => this.sendFrame({ type: "hello", mode: "app", siteKey: this.siteKey });
|
|
70
|
+
socket.onmessage = (event) => this.receive(event.data);
|
|
71
|
+
socket.onclose = (event) => this.closed(socket, event.code);
|
|
72
|
+
socket.onerror = () => undefined;
|
|
73
|
+
}
|
|
74
|
+
disconnect() {
|
|
75
|
+
this.clearRetry();
|
|
76
|
+
const socket = this.socket;
|
|
77
|
+
this.socket = undefined;
|
|
78
|
+
this.ready = false;
|
|
79
|
+
this.retries = 0;
|
|
80
|
+
socket?.close(1000, "no watched reads");
|
|
81
|
+
}
|
|
82
|
+
receive(data) {
|
|
83
|
+
const frame = parseFrame(data);
|
|
84
|
+
if (!frame)
|
|
85
|
+
return;
|
|
86
|
+
if (frame.type === "ready") {
|
|
87
|
+
this.ready = true;
|
|
88
|
+
this.retries = 0;
|
|
89
|
+
for (const entry of this.entries.values())
|
|
90
|
+
this.subscribe(entry);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const entry = typeof frame.subscription === "string" ? this.entries.get(frame.subscription) : undefined;
|
|
94
|
+
if (!entry)
|
|
95
|
+
return;
|
|
96
|
+
if (frame.type === "subscribed") {
|
|
97
|
+
entry.events.pushed(true);
|
|
98
|
+
entry.events.changed();
|
|
99
|
+
}
|
|
100
|
+
else if (frame.type === "item.changed") {
|
|
101
|
+
entry.events.changed();
|
|
102
|
+
}
|
|
103
|
+
else if (frame.type === "refused") {
|
|
104
|
+
// A user token that rotated since it subscribed is tried once more with the new one;
|
|
105
|
+
// otherwise the read's own poll reports why, and it keeps polling on its timer.
|
|
106
|
+
if (entry.target.userToken() !== entry.sentUserToken)
|
|
107
|
+
this.subscribe(entry);
|
|
108
|
+
else
|
|
109
|
+
entry.events.pushed(false);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
subscribe(entry) {
|
|
113
|
+
entry.sentUserToken = entry.target.userToken();
|
|
114
|
+
this.sendFrame({
|
|
115
|
+
type: "subscribe",
|
|
116
|
+
subscription: entry.name,
|
|
117
|
+
linkToken: entry.target.linkToken,
|
|
118
|
+
userToken: entry.sentUserToken,
|
|
119
|
+
item: entry.target.item,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
closed(socket, code) {
|
|
123
|
+
if (this.socket !== socket)
|
|
124
|
+
return;
|
|
125
|
+
this.socket = undefined;
|
|
126
|
+
this.ready = false;
|
|
127
|
+
for (const entry of this.entries.values())
|
|
128
|
+
entry.events.pushed(false);
|
|
129
|
+
if (code === CLOSE_REFUSED) {
|
|
130
|
+
this.refused = true;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
this.scheduleReconnect();
|
|
134
|
+
}
|
|
135
|
+
scheduleReconnect() {
|
|
136
|
+
if (this.entries.size === 0 || this.retryTimer)
|
|
137
|
+
return;
|
|
138
|
+
// Full jitter, between 0 and the capped exponential delay, so clients dropped together do
|
|
139
|
+
// not return together.
|
|
140
|
+
const delay = Math.floor(this.random() * Math.min(MIN_RETRY_MS * 2 ** this.retries, MAX_RETRY_MS));
|
|
141
|
+
this.retries += 1;
|
|
142
|
+
this.retryTimer = setTimeout(() => {
|
|
143
|
+
this.retryTimer = undefined;
|
|
144
|
+
this.connect();
|
|
145
|
+
}, delay);
|
|
146
|
+
}
|
|
147
|
+
clearRetry() {
|
|
148
|
+
if (this.retryTimer !== undefined)
|
|
149
|
+
clearTimeout(this.retryTimer);
|
|
150
|
+
this.retryTimer = undefined;
|
|
151
|
+
}
|
|
152
|
+
sendFrame(frame) {
|
|
153
|
+
if (this.socket?.readyState === OPEN)
|
|
154
|
+
this.socket.send(JSON.stringify(frame));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/** The realtime URL for an API origin: the same host, over ws(s). */
|
|
158
|
+
export function appRealtimeUrl(apiBaseUrl) {
|
|
159
|
+
const url = new URL(`${apiBaseUrl.replace(/\/+$/, "")}${APP_REALTIME_PATH}`);
|
|
160
|
+
url.protocol = url.protocol === "http:" ? "ws:" : "wss:";
|
|
161
|
+
return url.toString();
|
|
162
|
+
}
|
|
163
|
+
/** The global WebSocket as a factory, or undefined where there is none. */
|
|
164
|
+
export function globalWebSocketFactory() {
|
|
165
|
+
const WebSocketClass = globalThis.WebSocket;
|
|
166
|
+
return WebSocketClass ? (url) => new WebSocketClass(url) : undefined;
|
|
167
|
+
}
|
|
168
|
+
function parseFrame(data) {
|
|
169
|
+
if (typeof data !== "string")
|
|
170
|
+
return undefined;
|
|
171
|
+
try {
|
|
172
|
+
const frame = JSON.parse(data);
|
|
173
|
+
return typeof frame === "object" && frame !== null ? frame : undefined;
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type OffboardingAcknowledgeResponse, type OffboardingDownloadResponse, type OffboardingDownloadStream, type OffboardingExecuteResponse, type OffboardingExportResponse, type OffboardingPreviewResponse, type OffboardingReceiptResponse } from "./admin-offboarding.js";
|
|
2
|
+
import type { ClientSetupOAuthPurposeProfile, RequestOptions, RuntimeReadinessOptions, RuntimeReadinessResult } from "./index.js";
|
|
3
|
+
export type BackendLifecycleRequester = <T>(method: string, path: string, body?: unknown, options?: RequestOptions) => Promise<T>;
|
|
4
|
+
export type BackendLifecycleDownloader = (path: string, options?: RequestOptions) => Promise<OffboardingDownloadStream>;
|
|
5
|
+
export type OneTimeCredentialSecret = {
|
|
6
|
+
tenantSlug: string;
|
|
7
|
+
clientId: string;
|
|
8
|
+
purposeProfile: ClientSetupOAuthPurposeProfile;
|
|
9
|
+
clientSecret: string;
|
|
10
|
+
};
|
|
11
|
+
export type PersistOneTimeCredentialSecret = (credential: OneTimeCredentialSecret) => void | Promise<void>;
|
|
12
|
+
export type RotateCredentialOptions = RequestOptions & {
|
|
13
|
+
tenantSlug: string;
|
|
14
|
+
clientId: string;
|
|
15
|
+
purposeProfile: ClientSetupOAuthPurposeProfile;
|
|
16
|
+
persistSecret: PersistOneTimeCredentialSecret;
|
|
17
|
+
};
|
|
18
|
+
export type RotateCredentialResult = {
|
|
19
|
+
tenantSlug: string;
|
|
20
|
+
clientId: string;
|
|
21
|
+
purposeProfile: ClientSetupOAuthPurposeProfile;
|
|
22
|
+
secretPersisted: true;
|
|
23
|
+
};
|
|
24
|
+
export type ZeroStateReconciliationResult = {
|
|
25
|
+
zero: boolean;
|
|
26
|
+
remaining?: readonly string[];
|
|
27
|
+
};
|
|
28
|
+
export type VerifyZeroState = (input: {
|
|
29
|
+
tenantSlug: string;
|
|
30
|
+
requestUuid: string;
|
|
31
|
+
receipt: OffboardingReceiptResponse;
|
|
32
|
+
}) => ZeroStateReconciliationResult | Promise<ZeroStateReconciliationResult>;
|
|
33
|
+
export type ExportDeliveryVerificationResult = {
|
|
34
|
+
verified: boolean;
|
|
35
|
+
evidence?: string;
|
|
36
|
+
};
|
|
37
|
+
export type ReceiveAndVerifyOffboardingExport = (input: {
|
|
38
|
+
tenantSlug: string;
|
|
39
|
+
requestUuid: string;
|
|
40
|
+
download: OffboardingDownloadResponse;
|
|
41
|
+
export?: OffboardingExportResponse;
|
|
42
|
+
}) => ExportDeliveryVerificationResult | Promise<ExportDeliveryVerificationResult>;
|
|
43
|
+
export type PersistOffboardingExport = (input: {
|
|
44
|
+
tenantSlug: string;
|
|
45
|
+
requestUuid: string;
|
|
46
|
+
bytes: Uint8Array;
|
|
47
|
+
checksumSha256: string;
|
|
48
|
+
}) => string | undefined | Promise<string | undefined>;
|
|
49
|
+
export type VerifiedExportReceiverOptions = {
|
|
50
|
+
persist: PersistOffboardingExport;
|
|
51
|
+
};
|
|
52
|
+
export declare function createVerifiedOffboardingExportReceiver(options: VerifiedExportReceiverOptions): ReceiveAndVerifyOffboardingExport;
|
|
53
|
+
export type ZeroStateReconciliationOptions = {
|
|
54
|
+
tenantSlug: string;
|
|
55
|
+
requestUuid: string;
|
|
56
|
+
receipt: OffboardingReceiptResponse;
|
|
57
|
+
verifyZeroState: VerifyZeroState;
|
|
58
|
+
};
|
|
59
|
+
export type CompleteOffboardingOptions = RequestOptions & {
|
|
60
|
+
tenantSlug: string;
|
|
61
|
+
requestUuid: string;
|
|
62
|
+
receiveAndVerifyExport: ReceiveAndVerifyOffboardingExport;
|
|
63
|
+
verifyZeroState: VerifyZeroState;
|
|
64
|
+
};
|
|
65
|
+
export type CompleteOffboardingResult = {
|
|
66
|
+
tenantSlug: string;
|
|
67
|
+
requestUuid: string;
|
|
68
|
+
preview?: OffboardingPreviewResponse;
|
|
69
|
+
export?: OffboardingExportResponse;
|
|
70
|
+
download: OffboardingDownloadResponse;
|
|
71
|
+
exportDelivery: ExportDeliveryVerificationResult;
|
|
72
|
+
acknowledgement: OffboardingAcknowledgeResponse;
|
|
73
|
+
execution: OffboardingExecuteResponse;
|
|
74
|
+
receipt: OffboardingReceiptResponse;
|
|
75
|
+
zeroState: ZeroStateReconciliationResult;
|
|
76
|
+
};
|
|
77
|
+
export declare class BackendLifecycleClient {
|
|
78
|
+
private readonly request;
|
|
79
|
+
private readonly offboarding;
|
|
80
|
+
constructor(request: BackendLifecycleRequester, offboardingDownload: BackendLifecycleDownloader);
|
|
81
|
+
rotateCredential(options: RotateCredentialOptions): Promise<RotateCredentialResult>;
|
|
82
|
+
verifyReadiness(options: RuntimeReadinessOptions): Promise<RuntimeReadinessResult>;
|
|
83
|
+
reconcileZeroState(options: ZeroStateReconciliationOptions): Promise<ZeroStateReconciliationResult>;
|
|
84
|
+
completeOffboarding(options: CompleteOffboardingOptions): Promise<CompleteOffboardingResult>;
|
|
85
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { OffboardingClient, } from "./admin-offboarding.js";
|
|
2
|
+
import { checkRuntimeReadiness } from "./runtime-readiness.js";
|
|
3
|
+
function hex(bytes) {
|
|
4
|
+
return Array.from(bytes, (value) => value.toString(16).padStart(2, "0")).join("");
|
|
5
|
+
}
|
|
6
|
+
export function createVerifiedOffboardingExportReceiver(options) {
|
|
7
|
+
if (typeof options.persist !== "function") {
|
|
8
|
+
throw new Error("Custd lifecycle requires an export persistence callback");
|
|
9
|
+
}
|
|
10
|
+
return async ({ tenantSlug, requestUuid, download, export: exported }) => {
|
|
11
|
+
if (download.bytes.byteLength !== download.byteSize || (exported && download.byteSize !== exported.byteSize)) {
|
|
12
|
+
throw new Error("Custd lifecycle export byte size did not match server metadata");
|
|
13
|
+
}
|
|
14
|
+
const digest = hex(new Uint8Array(await globalThis.crypto.subtle.digest("SHA-256", Uint8Array.from(download.bytes).buffer)));
|
|
15
|
+
if (digest !== download.checksumSha256 || (exported && digest !== exported.checksumSha256)) {
|
|
16
|
+
throw new Error("Custd lifecycle export checksum verification failed");
|
|
17
|
+
}
|
|
18
|
+
const evidence = await options.persist({
|
|
19
|
+
tenantSlug,
|
|
20
|
+
requestUuid,
|
|
21
|
+
bytes: download.bytes,
|
|
22
|
+
checksumSha256: digest,
|
|
23
|
+
});
|
|
24
|
+
return { verified: true, ...(evidence ? { evidence } : {}) };
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const purposeProfiles = [
|
|
28
|
+
"ingest",
|
|
29
|
+
"schema",
|
|
30
|
+
"reporting",
|
|
31
|
+
"lifecycle",
|
|
32
|
+
"broker",
|
|
33
|
+
];
|
|
34
|
+
function nonEmpty(value) {
|
|
35
|
+
return typeof value === "string" && value.trim() !== "";
|
|
36
|
+
}
|
|
37
|
+
function requireText(name, value) {
|
|
38
|
+
if (!nonEmpty(value))
|
|
39
|
+
throw new Error(`Custd lifecycle requires ${name}`);
|
|
40
|
+
}
|
|
41
|
+
function requirePurpose(value) {
|
|
42
|
+
if (!purposeProfiles.includes(value)) {
|
|
43
|
+
throw new Error(`Custd lifecycle does not support credential purpose "${String(value)}"`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function requireRequestUuid(value, expected, step) {
|
|
47
|
+
if (!value || typeof value !== "object" || value.requestUuid !== expected) {
|
|
48
|
+
throw new Error(`Custd lifecycle ${step} returned a different offboarding request`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function requireReceipt(receipt) {
|
|
52
|
+
if (receipt.finalState !== "complete") {
|
|
53
|
+
throw new Error(`Custd lifecycle receipt is not complete (state ${receipt.finalState || "unknown"})`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function requireNonNegativeInteger(name, value) {
|
|
57
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
|
|
58
|
+
throw new Error(`Custd lifecycle requires valid ${name}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function requireMatchingExportMetadata(exported, download) {
|
|
62
|
+
const matches = [exported.checksumSha256 === download.checksumSha256, exported.byteSize === download.byteSize].every(Boolean);
|
|
63
|
+
if (!matches) {
|
|
64
|
+
throw new Error("Custd lifecycle export metadata did not match the download");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function requireDownload(download) {
|
|
68
|
+
if (!(download.bytes instanceof Uint8Array)) {
|
|
69
|
+
throw new Error("Custd lifecycle requires offboarding download bytes");
|
|
70
|
+
}
|
|
71
|
+
requireText("offboarding download checksum", download.checksumSha256);
|
|
72
|
+
requireNonNegativeInteger("offboarding download byte size", download.byteSize);
|
|
73
|
+
if (download.bytes.byteLength !== download.byteSize) {
|
|
74
|
+
throw new Error("Custd lifecycle offboarding download byte size did not match its body");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function validateRotation(options) {
|
|
78
|
+
requireText("tenant slug", options.tenantSlug);
|
|
79
|
+
requireText("client ID", options.clientId);
|
|
80
|
+
requirePurpose(options.purposeProfile);
|
|
81
|
+
if (typeof options.persistSecret !== "function") {
|
|
82
|
+
throw new Error("Custd lifecycle requires a one-time secret persistence callback");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function validateOffboarding(options) {
|
|
86
|
+
requireText("tenant slug", options.tenantSlug);
|
|
87
|
+
requireText("offboarding request UUID", options.requestUuid);
|
|
88
|
+
if (typeof options.receiveAndVerifyExport !== "function") {
|
|
89
|
+
throw new Error("Custd lifecycle requires an export download, persistence, and verification callback");
|
|
90
|
+
}
|
|
91
|
+
if (typeof options.verifyZeroState !== "function") {
|
|
92
|
+
throw new Error("Custd lifecycle requires a zero-state reconciliation callback");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export class BackendLifecycleClient {
|
|
96
|
+
constructor(request, offboardingDownload) {
|
|
97
|
+
this.request = request;
|
|
98
|
+
this.offboarding = new OffboardingClient(request, offboardingDownload);
|
|
99
|
+
}
|
|
100
|
+
async rotateCredential(options) {
|
|
101
|
+
validateRotation(options);
|
|
102
|
+
const existing = await this.request("GET", `/oauth-clients/${encodeURIComponent(options.clientId)}`, undefined, options);
|
|
103
|
+
if (existing.clientId !== options.clientId || existing.companySlug !== options.tenantSlug) {
|
|
104
|
+
throw new Error("Custd lifecycle credential does not belong to the requested tenant");
|
|
105
|
+
}
|
|
106
|
+
const rotated = await this.request("POST", `/oauth-clients/${encodeURIComponent(options.clientId)}/rotate-secret`, undefined, options);
|
|
107
|
+
requireText("rotated client secret", rotated?.clientSecret);
|
|
108
|
+
try {
|
|
109
|
+
await options.persistSecret({
|
|
110
|
+
tenantSlug: options.tenantSlug,
|
|
111
|
+
clientId: options.clientId,
|
|
112
|
+
purposeProfile: options.purposeProfile,
|
|
113
|
+
clientSecret: rotated.clientSecret,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
throw new Error("Custd lifecycle credential rotation completed but secret persistence failed; reconcile before retrying");
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
tenantSlug: options.tenantSlug,
|
|
121
|
+
clientId: options.clientId,
|
|
122
|
+
purposeProfile: options.purposeProfile,
|
|
123
|
+
secretPersisted: true,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
verifyReadiness(options) {
|
|
127
|
+
return checkRuntimeReadiness(options);
|
|
128
|
+
}
|
|
129
|
+
async reconcileZeroState(options) {
|
|
130
|
+
requireText("tenant slug", options.tenantSlug);
|
|
131
|
+
requireText("offboarding request UUID", options.requestUuid);
|
|
132
|
+
if (typeof options.verifyZeroState !== "function") {
|
|
133
|
+
throw new Error("Custd lifecycle requires a zero-state reconciliation callback");
|
|
134
|
+
}
|
|
135
|
+
let result;
|
|
136
|
+
try {
|
|
137
|
+
result = await options.verifyZeroState({
|
|
138
|
+
tenantSlug: options.tenantSlug,
|
|
139
|
+
requestUuid: options.requestUuid,
|
|
140
|
+
receipt: options.receipt,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
throw new Error("Custd lifecycle zero-state reconciliation failed");
|
|
145
|
+
}
|
|
146
|
+
if (result?.zero !== true) {
|
|
147
|
+
throw new Error("Custd lifecycle zero-state reconciliation did not confirm an empty tenant");
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
async completeOffboarding(options) {
|
|
152
|
+
validateOffboarding(options);
|
|
153
|
+
const current = await this.offboarding.getRequest(options.requestUuid, options);
|
|
154
|
+
requireRequestUuid(current, options.requestUuid, "state");
|
|
155
|
+
let preview;
|
|
156
|
+
let exported;
|
|
157
|
+
switch (current.state) {
|
|
158
|
+
case "preview":
|
|
159
|
+
preview = await this.offboarding.preview(options.requestUuid, options);
|
|
160
|
+
requireRequestUuid(preview, options.requestUuid, "preview");
|
|
161
|
+
if (!preview.complete || preview.partial) {
|
|
162
|
+
throw new Error("Custd lifecycle offboarding preview is incomplete");
|
|
163
|
+
}
|
|
164
|
+
exported = await this.offboarding.export(options.requestUuid, options);
|
|
165
|
+
requireRequestUuid(exported, options.requestUuid, "export");
|
|
166
|
+
break;
|
|
167
|
+
case "exported":
|
|
168
|
+
break;
|
|
169
|
+
default:
|
|
170
|
+
throw new Error(`Custd lifecycle cannot complete offboarding from state ${current.state || "unknown"}`);
|
|
171
|
+
}
|
|
172
|
+
const download = await this.offboarding.download(options.requestUuid, options);
|
|
173
|
+
requireDownload(download);
|
|
174
|
+
if (exported) {
|
|
175
|
+
requireMatchingExportMetadata(exported, download);
|
|
176
|
+
}
|
|
177
|
+
const exportDelivery = await options.receiveAndVerifyExport({
|
|
178
|
+
tenantSlug: options.tenantSlug,
|
|
179
|
+
requestUuid: options.requestUuid,
|
|
180
|
+
download,
|
|
181
|
+
...(exported ? { export: exported } : {}),
|
|
182
|
+
});
|
|
183
|
+
if (exportDelivery?.verified !== true) {
|
|
184
|
+
throw new Error("Custd lifecycle export delivery was not verified");
|
|
185
|
+
}
|
|
186
|
+
const acknowledgement = await this.offboarding.acknowledge(options.requestUuid, options);
|
|
187
|
+
requireRequestUuid(acknowledgement, options.requestUuid, "acknowledgement");
|
|
188
|
+
const confirmation = await this.offboarding.confirmRequest(options.requestUuid, options);
|
|
189
|
+
requireRequestUuid(confirmation, options.requestUuid, "confirmation");
|
|
190
|
+
const execution = await this.offboarding.execute(options.requestUuid, options);
|
|
191
|
+
const receipt = await this.offboarding.receipt(options.requestUuid, options);
|
|
192
|
+
requireReceipt(receipt);
|
|
193
|
+
const zeroState = await this.reconcileZeroState({
|
|
194
|
+
tenantSlug: options.tenantSlug,
|
|
195
|
+
requestUuid: options.requestUuid,
|
|
196
|
+
receipt,
|
|
197
|
+
verifyZeroState: options.verifyZeroState,
|
|
198
|
+
});
|
|
199
|
+
return {
|
|
200
|
+
tenantSlug: options.tenantSlug,
|
|
201
|
+
requestUuid: options.requestUuid,
|
|
202
|
+
...(preview ? { preview } : {}),
|
|
203
|
+
...(exported ? { export: exported } : {}),
|
|
204
|
+
download,
|
|
205
|
+
exportDelivery,
|
|
206
|
+
acknowledgement,
|
|
207
|
+
execution,
|
|
208
|
+
receipt,
|
|
209
|
+
zeroState,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|