@connextar/house 0.22.3 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +570 -60
- package/dist/admin/index.d.ts +2 -2
- package/dist/admin/index.d.ts.map +1 -1
- package/dist/admin/index.js +2 -2
- package/dist/admin/index.js.map +1 -1
- package/dist/admin/proof.d.ts +49 -9
- package/dist/admin/proof.d.ts.map +1 -1
- package/dist/admin/proof.js +83 -4
- package/dist/admin/proof.js.map +1 -1
- package/dist/admin/second-factor.d.ts +3 -1
- package/dist/admin/second-factor.d.ts.map +1 -1
- package/dist/admin/second-factor.js +3 -2
- package/dist/admin/second-factor.js.map +1 -1
- package/dist/admin/view-as.d.ts +37 -5
- package/dist/admin/view-as.d.ts.map +1 -1
- package/dist/admin/view-as.js +67 -6
- package/dist/admin/view-as.js.map +1 -1
- package/dist/analytics/browser.d.ts +108 -0
- package/dist/analytics/browser.d.ts.map +1 -0
- package/dist/analytics/browser.js +319 -0
- package/dist/analytics/browser.js.map +1 -0
- package/dist/analytics/client-index.d.ts +6 -0
- package/dist/analytics/client-index.d.ts.map +1 -0
- package/dist/analytics/client-index.js +6 -0
- package/dist/analytics/client-index.js.map +1 -0
- package/dist/analytics/config.d.ts +50 -0
- package/dist/analytics/config.d.ts.map +1 -0
- package/dist/analytics/config.js +39 -0
- package/dist/analytics/config.js.map +1 -0
- package/dist/analytics/index.d.ts +10 -0
- package/dist/analytics/index.d.ts.map +1 -0
- package/dist/analytics/index.js +10 -0
- package/dist/analytics/index.js.map +1 -0
- package/dist/analytics/ingest.d.ts +30 -0
- package/dist/analytics/ingest.d.ts.map +1 -0
- package/dist/analytics/ingest.js +64 -0
- package/dist/analytics/ingest.js.map +1 -0
- package/dist/analytics/react-index.d.ts +6 -0
- package/dist/analytics/react-index.d.ts.map +1 -0
- package/dist/analytics/react-index.js +6 -0
- package/dist/analytics/react-index.js.map +1 -0
- package/dist/analytics/react.d.ts +75 -0
- package/dist/analytics/react.d.ts.map +1 -0
- package/dist/analytics/react.js +159 -0
- package/dist/analytics/react.js.map +1 -0
- package/dist/analytics/redact.d.ts +35 -0
- package/dist/analytics/redact.d.ts.map +1 -0
- package/dist/analytics/redact.js +109 -0
- package/dist/analytics/redact.js.map +1 -0
- package/dist/analytics/server-index.d.ts +6 -0
- package/dist/analytics/server-index.d.ts.map +1 -0
- package/dist/analytics/server-index.js +6 -0
- package/dist/analytics/server-index.js.map +1 -0
- package/dist/analytics/server.d.ts +89 -0
- package/dist/analytics/server.d.ts.map +1 -0
- package/dist/analytics/server.js +211 -0
- package/dist/analytics/server.js.map +1 -0
- package/dist/jobs/idle.d.ts +49 -0
- package/dist/jobs/idle.d.ts.map +1 -0
- package/dist/jobs/idle.js +61 -0
- package/dist/jobs/idle.js.map +1 -0
- package/dist/jobs/index.d.ts +2 -1
- package/dist/jobs/index.d.ts.map +1 -1
- package/dist/jobs/index.js +2 -1
- package/dist/jobs/index.js.map +1 -1
- package/dist/jobs/next-index.d.ts +5 -0
- package/dist/jobs/next-index.d.ts.map +1 -0
- package/dist/jobs/next-index.js +5 -0
- package/dist/jobs/next-index.js.map +1 -0
- package/dist/jobs/next.d.ts +25 -0
- package/dist/jobs/next.d.ts.map +1 -0
- package/dist/jobs/next.js +51 -0
- package/dist/jobs/next.js.map +1 -0
- package/dist/jobs/prisma-index.d.ts +6 -0
- package/dist/jobs/prisma-index.d.ts.map +1 -0
- package/dist/jobs/prisma-index.js +6 -0
- package/dist/jobs/prisma-index.js.map +1 -0
- package/dist/jobs/prisma.d.ts +77 -0
- package/dist/jobs/prisma.d.ts.map +1 -0
- package/dist/jobs/prisma.js +92 -0
- package/dist/jobs/prisma.js.map +1 -0
- package/dist/jobs/scheduler.d.ts +7 -0
- package/dist/jobs/scheduler.d.ts.map +1 -1
- package/dist/jobs/scheduler.js +10 -0
- package/dist/jobs/scheduler.js.map +1 -1
- package/dist/rate-limit/better-auth.d.ts +50 -0
- package/dist/rate-limit/better-auth.d.ts.map +1 -0
- package/dist/rate-limit/better-auth.js +64 -0
- package/dist/rate-limit/better-auth.js.map +1 -0
- package/dist/rate-limit/index.d.ts +13 -0
- package/dist/rate-limit/index.d.ts.map +1 -0
- package/dist/rate-limit/index.js +13 -0
- package/dist/rate-limit/index.js.map +1 -0
- package/dist/rate-limit/keys.d.ts +45 -0
- package/dist/rate-limit/keys.d.ts.map +1 -0
- package/dist/rate-limit/keys.js +95 -0
- package/dist/rate-limit/keys.js.map +1 -0
- package/dist/rate-limit/limiter.d.ts +114 -0
- package/dist/rate-limit/limiter.d.ts.map +1 -0
- package/dist/rate-limit/limiter.js +166 -0
- package/dist/rate-limit/limiter.js.map +1 -0
- package/dist/rate-limit/prisma-index.d.ts +6 -0
- package/dist/rate-limit/prisma-index.d.ts.map +1 -0
- package/dist/rate-limit/prisma-index.js +6 -0
- package/dist/rate-limit/prisma-index.js.map +1 -0
- package/dist/rate-limit/prisma.d.ts +116 -0
- package/dist/rate-limit/prisma.d.ts.map +1 -0
- package/dist/rate-limit/prisma.js +79 -0
- package/dist/rate-limit/prisma.js.map +1 -0
- package/dist/rate-limit/store.d.ts +27 -0
- package/dist/rate-limit/store.d.ts.map +1 -0
- package/dist/rate-limit/store.js +28 -0
- package/dist/rate-limit/store.js.map +1 -0
- package/package.json +36 -1
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The same-origin `/ingest` relay to PostHog — one route handler, edge-safe, importing nothing.
|
|
3
|
+
*
|
|
4
|
+
* Next's rewrites forwarded everything: the visitor's cookies for this site, the page they came from, their address.
|
|
5
|
+
* This forwards four request headers and two response headers, and only the endpoints posthog-js needs to send what
|
|
6
|
+
* the gate let through. It refuses `/flags` and `/decide`: posthog-js's feature-flag request carries the first page's
|
|
7
|
+
* full address — query and all — in its body, past `before_send`. PharmaLine found it (security round 5, S1); Quickie
|
|
8
|
+
* copied the fix; AltEd still forwarded it. And it refuses `/i/v1/logs`, which the app answers itself (`logsSink`).
|
|
9
|
+
*/
|
|
10
|
+
const FORWARD_REQUEST = ["content-type", "content-encoding", "user-agent", "accept"];
|
|
11
|
+
const FORWARD_RESPONSE = ["content-type", "cache-control"];
|
|
12
|
+
const REFUSED = /^\/(?:flags|decide|i\/v1\/logs)(?:\/|$)/;
|
|
13
|
+
const ALLOWED = /^\/(?:e|i\/v0\/e|s|batch|static|array)(?:\/|$)/;
|
|
14
|
+
export function ingestProxy(options = {}) {
|
|
15
|
+
const apiHost = (options.apiHost ?? "https://eu.i.posthog.com").replace(/\/+$/, "");
|
|
16
|
+
const assetHost = (options.assetHost ?? "https://eu-assets.i.posthog.com").replace(/\/+$/, "");
|
|
17
|
+
const mount = (options.mount ?? "/ingest").replace(/\/+$/, "");
|
|
18
|
+
const key = options.projectKey;
|
|
19
|
+
function upstreamFor(url) {
|
|
20
|
+
if (!url.pathname.startsWith(`${mount}/`))
|
|
21
|
+
return null;
|
|
22
|
+
// The raw pathname, so a trailing slash (`/ingest/e/`) survives — PostHog's endpoints end in one.
|
|
23
|
+
const rest = url.pathname.slice(mount.length);
|
|
24
|
+
if (rest.includes("/../") || rest.includes("/./") || REFUSED.test(rest) || !ALLOWED.test(rest))
|
|
25
|
+
return null;
|
|
26
|
+
if (key && rest.startsWith("/array/") && !rest.startsWith(`/array/${key}/`))
|
|
27
|
+
return null;
|
|
28
|
+
const host = rest.startsWith("/static/") || rest.startsWith("/array/") ? assetHost : apiHost;
|
|
29
|
+
return `${host}${rest}${url.search}`;
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
upstreamFor,
|
|
33
|
+
async handle(request) {
|
|
34
|
+
const target = upstreamFor(new URL(request.url));
|
|
35
|
+
if (!target)
|
|
36
|
+
return new Response(null, { status: 404 });
|
|
37
|
+
const headers = new Headers();
|
|
38
|
+
for (const name of FORWARD_REQUEST) {
|
|
39
|
+
const value = request.headers.get(name);
|
|
40
|
+
if (value)
|
|
41
|
+
headers.set(name, value);
|
|
42
|
+
}
|
|
43
|
+
const hasBody = request.method !== "GET" && request.method !== "HEAD";
|
|
44
|
+
const upstream = await (options.fetch ?? fetch)(target, {
|
|
45
|
+
method: request.method,
|
|
46
|
+
headers,
|
|
47
|
+
body: hasBody ? await request.arrayBuffer() : undefined,
|
|
48
|
+
redirect: "manual",
|
|
49
|
+
});
|
|
50
|
+
const out = new Headers();
|
|
51
|
+
for (const name of FORWARD_RESPONSE) {
|
|
52
|
+
const value = upstream.headers.get(name);
|
|
53
|
+
if (value)
|
|
54
|
+
out.set(name, value);
|
|
55
|
+
}
|
|
56
|
+
return new Response(upstream.body, { status: upstream.status, headers: out });
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** `app/ingest/i/v1/logs/route.ts`: posthog-js's console-log sink, answered here and never forwarded. */
|
|
61
|
+
export function logsSink() {
|
|
62
|
+
return new Response(null, { status: 204 });
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=ingest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ingest.js","sourceRoot":"","sources":["../../src/analytics/ingest.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAcH,MAAM,eAAe,GAAG,CAAC,cAAc,EAAE,kBAAkB,EAAE,YAAY,EAAE,QAAQ,CAAU,CAAC;AAC9F,MAAM,gBAAgB,GAAG,CAAC,cAAc,EAAE,eAAe,CAAU,CAAC;AACpE,MAAM,OAAO,GAAG,yCAAyC,CAAC;AAC1D,MAAM,OAAO,GAAG,gDAAgD,CAAC;AASjE,MAAM,UAAU,WAAW,CAAC,UAA8B,EAAE;IAC1D,MAAM,OAAO,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,0BAA0B,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACpF,MAAM,SAAS,GAAG,CAAC,OAAO,CAAC,SAAS,IAAI,iCAAiC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC/F,MAA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AAC7C,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/analytics/react` — the cookie card and what it drives, the pre-paint script, the markers, and the
|
|
3
|
+
* consent hook. Client components.
|
|
4
|
+
*/
|
|
5
|
+
export { ConsentKnownScript, CookieSettingsButton, NotMeasured, RecordingAllowed, SITE_ANALYTICS_CLASS_NAMES, SITE_ANALYTICS_LABELS, SiteAnalytics, useAnalyticsConsent, type CookieCardProps, type SiteAnalyticsProps, } from "./react.js";
|
|
6
|
+
//# sourceMappingURL=react-index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-index.d.ts","sourceRoot":"","sources":["../../src/analytics/react-index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,WAAW,EACX,gBAAgB,EAChB,0BAA0B,EAC1B,qBAAqB,EACrB,aAAa,EACb,mBAAmB,EACnB,KAAK,eAAe,EACpB,KAAK,kBAAkB,GACxB,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/analytics/react` — the cookie card and what it drives, the pre-paint script, the markers, and the
|
|
3
|
+
* consent hook. Client components.
|
|
4
|
+
*/
|
|
5
|
+
export { ConsentKnownScript, CookieSettingsButton, NotMeasured, RecordingAllowed, SITE_ANALYTICS_CLASS_NAMES, SITE_ANALYTICS_LABELS, SiteAnalytics, useAnalyticsConsent, } from "./react.js";
|
|
6
|
+
//# sourceMappingURL=react-index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-index.js","sourceRoot":"","sources":["../../src/analytics/react-index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,WAAW,EACX,gBAAgB,EAChB,0BAA0B,EAC1B,qBAAqB,EACrB,aAAa,EACb,mBAAmB,GAGpB,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { AnalyticsConsent, BrowserAnalytics, PostHogLike } from "./browser.js";
|
|
3
|
+
export interface CookieCardProps {
|
|
4
|
+
answer(consent: AnalyticsConsent): void;
|
|
5
|
+
reopened: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare const SITE_ANALYTICS_LABELS: {
|
|
8
|
+
title: string;
|
|
9
|
+
body: string;
|
|
10
|
+
accept: string;
|
|
11
|
+
reject: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const SITE_ANALYTICS_CLASS_NAMES: {
|
|
14
|
+
root: string;
|
|
15
|
+
title: string;
|
|
16
|
+
body: string;
|
|
17
|
+
actions: string;
|
|
18
|
+
reject: string;
|
|
19
|
+
accept: string;
|
|
20
|
+
};
|
|
21
|
+
export interface SiteAnalyticsProps {
|
|
22
|
+
browser: BrowserAnalytics;
|
|
23
|
+
/** The GTM container. Empty: no GTM. */
|
|
24
|
+
containerId?: string;
|
|
25
|
+
/** The PostHog project key; whether PostHog is on. Empty: off. */
|
|
26
|
+
posthogKey?: string;
|
|
27
|
+
/** posthog-js's instance, which `instrumentation-client.ts` initialised. */
|
|
28
|
+
posthog?: PostHogLike | null;
|
|
29
|
+
/**
|
|
30
|
+
* The event a click on a same-origin link to a private page is: `begin_sign_up` for `/sign-up`. Null for none.
|
|
31
|
+
* The click leaves the public pages with a full load, so GTM is not carried into the app.
|
|
32
|
+
*/
|
|
33
|
+
leavingEvent?: (pathname: string) => string | null;
|
|
34
|
+
/** The app's own card. Default: the house's, on the theme tokens. */
|
|
35
|
+
renderCard?: (props: CookieCardProps) => React.ReactNode;
|
|
36
|
+
labels?: Partial<typeof SITE_ANALYTICS_LABELS>;
|
|
37
|
+
classNames?: Partial<typeof SITE_ANALYTICS_CLASS_NAMES>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* In the root layout, once. It asks — on a page where measuring could happen, and only there — and loads GTM after a
|
|
41
|
+
* yes on a tagged page, starts and stops replay as the page changes, and makes leaving the public pages a full load.
|
|
42
|
+
*/
|
|
43
|
+
export declare function SiteAnalytics({ browser, containerId, posthogKey, posthog, leavingEvent, renderCard, labels, classNames, }: SiteAnalyticsProps): React.JSX.Element | null;
|
|
44
|
+
/** In `<head>`: marks the document before paint once an answer is known, so the card never flashes. */
|
|
45
|
+
export declare function ConsentKnownScript({ browser, containerId, posthogKey, nonce, }: {
|
|
46
|
+
browser: BrowserAnalytics;
|
|
47
|
+
containerId?: string;
|
|
48
|
+
posthogKey?: string;
|
|
49
|
+
nonce?: string;
|
|
50
|
+
}): React.JSX.Element | null;
|
|
51
|
+
/** The footer's "Cookie settings": asks again. */
|
|
52
|
+
export declare function CookieSettingsButton({ browser, className, children, }: {
|
|
53
|
+
browser: BrowserAnalytics;
|
|
54
|
+
className?: string;
|
|
55
|
+
children?: React.ReactNode;
|
|
56
|
+
}): React.JSX.Element;
|
|
57
|
+
/**
|
|
58
|
+
* On a signed-in page the app lets be measured (`recordablePath`), for an ordinary member: render it for them, and
|
|
59
|
+
* never for an administrator or during a view. Without it the page is not measured.
|
|
60
|
+
*/
|
|
61
|
+
export declare function RecordingAllowed({ browser }: {
|
|
62
|
+
browser: BrowserAnalytics;
|
|
63
|
+
}): React.JSX.Element;
|
|
64
|
+
/**
|
|
65
|
+
* On a page that must never be measured, however it was reached and whoever accepted: an administrator reading the
|
|
66
|
+
* internal changelog at a public address, say. `ViewingAsBanner` already carries its own marker.
|
|
67
|
+
*/
|
|
68
|
+
export declare function NotMeasured(): React.JSX.Element;
|
|
69
|
+
/** For the settings page's own consent card: the answer, whether the browser said no for everybody, and a setter. */
|
|
70
|
+
export declare function useAnalyticsConsent(browser: BrowserAnalytics): {
|
|
71
|
+
consent: AnalyticsConsent | null;
|
|
72
|
+
optedOut: boolean;
|
|
73
|
+
setConsent: (choice: AnalyticsConsent) => void;
|
|
74
|
+
};
|
|
75
|
+
//# sourceM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B,UAAe,EACf,KAAK,GACN,EAAE;IACD,OAAO,EAAE,gBAAgB,CAAC;IAC1B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,4BAGA;AAED,kDAAkD;AAClD,wBAAgB,oBAAoB,CAAC,EACnC,OAAO,EACP,SAAS,EACT,QAA4B,GAC7B,EAAE;IACD,OAAO,EAAE,gBAAgB,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B,qBAMA;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,OAAO,EAAE,EAAE;IAAE,OAAO,EAAE,gBAAgB,CAAA;CAAE,qBAS1E;AAED;;;GAGG;AACH,wBAAgB,WAAW,sBAE1B;AAED,qHAAqH;AACrH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,gBAAgB;;;yBAIhD,gBAAgB;EAW5B"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { usePathname } from "next/navigation.js";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import { cx } from "../internal/cx.js";
|
|
6
|
+
import { NOT_MEASURED_ATTRIBUTE, RECORDABLE_ATTRIBUTE } from "./config.js";
|
|
7
|
+
/**
|
|
8
|
+
* The components: the cookie card and everything it drives, the pre-paint script, the "Cookie settings" button, the
|
|
9
|
+
* markers a page renders to say whether it may be measured, and a hook for the settings page's own consent card.
|
|
10
|
+
*
|
|
11
|
+
* Each takes the app's `BrowserAnalytics` — built once in a plain module and imported by the app's own client
|
|
12
|
+
* wrapper, since a definition holding functions cannot cross from a server component to a client one.
|
|
13
|
+
*/
|
|
14
|
+
const subscribeToNothing = () => () => { };
|
|
15
|
+
/** The page this document was first loaded on was tagged — read once, before any client navigation. */
|
|
16
|
+
const documentBeganTagged = (browser) => typeof window !== "undefined" && browser.analytics.isTaggedPath(window.location.pathname);
|
|
17
|
+
export const SITE_ANALYTICS_LABELS = {
|
|
18
|
+
title: "Cookies",
|
|
19
|
+
body: "We'd like to measure how people use these pages, to make them better. Nothing is measured inside your account, and nothing is sold.",
|
|
20
|
+
accept: "Accept",
|
|
21
|
+
reject: "Reject",
|
|
22
|
+
};
|
|
23
|
+
export const SITE_ANALYTICS_CLASS_NAMES = {
|
|
24
|
+
root: "fixed inset-x-4 bottom-4 z-50 mx-auto max-w-xl rounded-lg border border-border bg-card p-4 text-sm text-foreground shadow-lg",
|
|
25
|
+
title: "font-semibold",
|
|
26
|
+
body: "mt-1 text-muted-foreground",
|
|
27
|
+
actions: "mt-3 flex flex-wrap justify-end gap-2",
|
|
28
|
+
reject: "rounded-md border border-border px-3 py-1.5 font-medium hover:bg-muted",
|
|
29
|
+
accept: "rounded-md bg-primary px-3 py-1.5 font-medium text-primary-foreground hover:opacity-90",
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* In the root layout, once. It asks — on a page where measuring could happen, and only there — and loads GTM after a
|
|
33
|
+
* yes on a tagged page, starts and stops replay as the page changes, and makes leaving the public pages a full load.
|
|
34
|
+
*/
|
|
35
|
+
export function SiteAnalytics({ browser, containerId = "", posthogKey = "", posthog, leavingEvent, renderCard, labels, classNames, }) {
|
|
36
|
+
const { analytics } = browser;
|
|
37
|
+
const pathname = usePathname() ?? "/";
|
|
38
|
+
const tagged = analytics.isTaggedPath(pathname);
|
|
39
|
+
const recordable = React.useSyncExternalStore(browser.subscribeToRecordable, browser.pageIsRecordable, () => false);
|
|
40
|
+
const askHere = tagged || (Boolean(posthogKey) && analytics.recordablePath(pathname) && recordable);
|
|
41
|
+
const consent = React.useSyncExternalStore(browser.subscribeToConsent, browser.readConsent, () => null);
|
|
42
|
+
const optedOut = React.useSyncExternalStore(subscribeToNothing, browser.browserOptsOut, () => false);
|
|
43
|
+
const [reopened, setReopened] = React.useState(false);
|
|
44
|
+
const asking = reopened || (consent === null && !optedOut);
|
|
45
|
+
const beganTagged = React.useRef(documentBeganTagged(browser));
|
|
46
|
+
React.useEffect(() => {
|
|
47
|
+
const ask = () => setReopened(true);
|
|
48
|
+
window.addEventListener(analytics.cookieSettingsEvent, ask);
|
|
49
|
+
return () => window.removeEventListener(analytics.cookieSettingsEvent, ask);
|
|
50
|
+
}, [analytics.cookieSettingsEvent]);
|
|
51
|
+
React.useEffect(() => {
|
|
52
|
+
if (!containerId || consent !== "granted" || !tagged || optedOut)
|
|
53
|
+
return;
|
|
54
|
+
// A document that began on a private page reloads rather than loading GTM into it.
|
|
55
|
+
if (beganTagged.current)
|
|
56
|
+
browser.loadGtm(containerId);
|
|
57
|
+
else
|
|
58
|
+
window.location.reload();
|
|
59
|
+
}, [browser, containerId, consent, tagged, optedOut]);
|
|
60
|
+
React.useEffect(() => {
|
|
61
|
+
browser.syncRecording(posthog);
|
|
62
|
+
}, [browser, posthog, consent, pathname, recordable]);
|
|
63
|
+
React.useEffect(() => {
|
|
64
|
+
// Arriving on a private page with GTM loaded: drop it with a full load.
|
|
65
|
+
if (!analytics.isTaggedPath(pathname) && browser.isGtmLoaded())
|
|
66
|
+
window.location.reload();
|
|
67
|
+
}, [analytics, browser, pathname]);
|
|
68
|
+
React.useEffect(() => {
|
|
69
|
+
function onClick(event) {
|
|
70
|
+
if (event.defaultPrevented || event.button !== 0)
|
|
71
|
+
return;
|
|
72
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
|
73
|
+
return;
|
|
74
|
+
if (!browser.isGtmLoaded() || !(event.target instanceof Element))
|
|
75
|
+
return;
|
|
76
|
+
const anchor = event.target.closest("a[href]");
|
|
77
|
+
if (!(anchor instanceof HTMLAnchorElement) || anchor.hasAttribute("download"))
|
|
78
|
+
return;
|
|
79
|
+
if (anchor.target && anchor.target !== "_self")
|
|
80
|
+
return;
|
|
81
|
+
const url = new URL(anchor.href, window.location.href);
|
|
82
|
+
if (url.origin !== window.location.origin || analytics.isTaggedPath(url.pathname))
|
|
83
|
+
return;
|
|
84
|
+
const name = leavingEvent?.(url.pathname) ?? null;
|
|
85
|
+
if (name) {
|
|
86
|
+
browser.track(name, {
|
|
87
|
+
cta_text: anchor.textContent?.trim() || undefined,
|
|
88
|
+
cta_location: browser.ctaLocation(anchor),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
event.preventDefault();
|
|
92
|
+
window.location.assign(url.href);
|
|
93
|
+
}
|
|
94
|
+
document.addEventListener("click", onClick, true);
|
|
95
|
+
return () => document.removeEventListener("click", onClick, true);
|
|
96
|
+
}, [analytics, browser, leavingEvent]);
|
|
97
|
+
if ((!containerId && !posthogKey) || !asking || !askHere)
|
|
98
|
+
return null;
|
|
99
|
+
function answer(choice) {
|
|
100
|
+
setReopened(false);
|
|
101
|
+
const wasLoaded = browser.isGtmLoaded();
|
|
102
|
+
browser.writeConsent(choice);
|
|
103
|
+
if (choice === "denied" && wasLoaded) {
|
|
104
|
+
browser.withdrawConsent();
|
|
105
|
+
window.location.reload();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (renderCard)
|
|
109
|
+
return _jsx(_Fragment, { children: renderCard({ answer, reopened }) });
|
|
110
|
+
const words = { ...SITE_ANALYTICS_LABELS, ...labels };
|
|
111
|
+
const c = { ...SITE_ANALYTICS_CLASS_NAMES, ...classNames };
|
|
112
|
+
return (_jsxs("section", { "aria-label": words.title, "data-cookie-card": "", "data-reopened": reopened ? "" : undefined, className: c.root, children: [_jsx("p", { className: c.title, children: words.title }), _jsx("p", { className: c.body, children: words.body }), _jsxs("div", { className: c.actions, children: [_jsx("button", { type: "button", className: c.reject, onClick: () => answer("denied"), children: words.reject }), _jsx("button", { type: "button", className: c.accept, onClick: () => answer("granted"), children: words.accept })] })] }));
|
|
113
|
+
}
|
|
114
|
+
/** In `<head>`: marks the document before paint once an answer is known, so the card never flashes. */
|
|
115
|
+
export function ConsentKnownScript({ browser, containerId = "", posthogKey = "", nonce, }) {
|
|
116
|
+
if (!containerId && !posthogKey)
|
|
117
|
+
return null;
|
|
118
|
+
return _jsx("script", { nonce: nonce, dangerouslySetInnerHTML: { __html: browser.consentKnownScript } });
|
|
119
|
+
}
|
|
120
|
+
/** The footer's "Cookie settings": asks again. */
|
|
121
|
+
export function CookieSettingsButton({ browser, className, children = "Cookie settings", }) {
|
|
122
|
+
return (_jsx("button", { type: "button", className: cx(className), onClick: () => browser.reopenConsent(), children: children }));
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* On a signed-in page the app lets be measured (`recordablePath`), for an ordinary member: render it for them, and
|
|
126
|
+
* never for an administrator or during a view. Without it the page is not measured.
|
|
127
|
+
*/
|
|
128
|
+
export function RecordingAllowed({ browser }) {
|
|
129
|
+
React.useEffect(() => {
|
|
130
|
+
browser.recordableChanged();
|
|
131
|
+
return () => {
|
|
132
|
+
// After unmount the marker is gone; tell the listeners on the next tick, when the DOM says so.
|
|
133
|
+
queueMicrotask(() => browser.recordableChanged());
|
|
134
|
+
};
|
|
135
|
+
}, [browser]);
|
|
136
|
+
return _jsx("span", { hidden: true, [RECORDABLE_ATTRIBUTE]: "" });
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* On a page that must never be measured, however it was reached and whoever accepted: an administrator reading the
|
|
140
|
+
* internal changelog at a public address, say. `ViewingAsBanner` already carries its own marker.
|
|
141
|
+
*/
|
|
142
|
+
export function NotMeasured() {
|
|
143
|
+
return _jsx("span", { hidden: true, [NOT_MEASURED_ATTRIBUTE]: "" });
|
|
144
|
+
}
|
|
145
|
+
/** For the settings page's own consent card: the answer, whether the browser said no for everybody, and a setter. */
|
|
146
|
+
export function useAnalyticsConsent(browser) {
|
|
147
|
+
const consent = React.useSyncExternalStore(browser.subscribeToConsent, browser.readConsent, () => null);
|
|
148
|
+
const optedOut = React.useSyncExternalStore(subscribeToNothing, browser.browserOptsOut, () => false);
|
|
149
|
+
const setConsent = React.useCallback((choice) => {
|
|
150
|
+
const wasLoaded = browser.isGtmLoaded();
|
|
151
|
+
browser.writeConsent(choice);
|
|
152
|
+
if (choice === "denied" && wasLoaded) {
|
|
153
|
+
browser.withdrawConsent();
|
|
154
|
+
window.location.reload();
|
|
155
|
+
}
|
|
156
|
+
}, [browser]);
|
|
157
|
+
return { consent: optedOut ? "denied" : consent, optedOut, setConsent };
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","sourceRoot":"","sources":["../../src/analytics/react.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAC;AAEvC,OAAO,EAAE,sBAAsB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAE3E;;;;;;GAMG;AAEH,MAAM,kBAAkB,GAAG,GAAG,EAAE,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;AAE1C,uGAAuG;AACvG,MAAM,mBAAmB,GAAG,CAAC,OAAyB,EAAE,EAAE,CACxD,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,CAAC,SAAS,CAAC,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAO5F,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,KAAK,EAAE,SAAS;IAChB,IAAI,EAAE,qIAAqI;IAC3I,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;CACjB,CAAC;AAEF,MAAM,CAAC,MAAM,0BAA0B,GAAG;IACxC,IAAI,EAAE,8HAA8H;IACpI,KAAK,EAAE,eAAe;IACtB,IAAI,EAAE,4BAA4B;IAClC,OAAO,EAAE,uCAAuC;IAChD,M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
* Redaction: what analytics and error reports may say about an address, a message or a stack.
|
|
3
|
+
*
|
|
4
|
+
* The union of the three copies' rules, each the strictest: Quickie's token paths (encoded forms too) and generic
|
|
5
|
+
* record ids, PharmaLine's named id segments and its full redaction of paths, and the email, long-number and UK
|
|
6
|
+
* postcode rules both of them had and AltEd lacked. Every query value is replaced. The app supplies its own words:
|
|
7
|
+
* which path prefixes carry a token, which query parameters are sensitive, which collections take an id.
|
|
8
|
+
*/
|
|
9
|
+
export interface RedactionRules {
|
|
10
|
+
/** Path prefixes whose next segment is a secret: `/i/<token>`, `/api/calendar/<token>`. Longest first is not needed. */
|
|
11
|
+
tokenPathPrefixes?: readonly string[];
|
|
12
|
+
/** Query parameters to redact even where a value is only half a URL — `?token=`, `&email=`. */
|
|
13
|
+
sensitiveQueryParams?: readonly string[];
|
|
14
|
+
/** Collections whose next segment is a record id that is not a cuid or UUID: `/patients/<id>`. */
|
|
15
|
+
idSegments?: readonly string[];
|
|
16
|
+
/** Segments after an id collection that are pages, not ids: `new`, `import`. */
|
|
17
|
+
idSegmentExceptions?: readonly string[];
|
|
18
|
+
/** Anything else the app must never send: `[/\bNHS\s?\d{10}\b/g, "[nhs-number]"]`. */
|
|
19
|
+
patterns?: readonly (readonly [RegExp, string])[];
|
|
20
|
+
}
|
|
21
|
+
export interface Redactor {
|
|
22
|
+
/** Every rule, over any text: a message, a stack line, a property value. */
|
|
23
|
+
redact(text: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* An address as analytics may keep it: this site's — the redacted path, no query, no fragment; anybody else's —
|
|
26
|
+
* the origin alone; a bare path — redacted.
|
|
27
|
+
*/
|
|
28
|
+
cleanUrl(value: string, ownOrigin: string): string;
|
|
29
|
+
/** `redact` over every string, and every key, in plain objects and arrays. Dates, classes and the rest stay. */
|
|
30
|
+
redactDeep<T>(value: T): T;
|
|
31
|
+
}
|
|
32
|
+
export declare const TOKEN_PLACEHOLDER = ":token";
|
|
33
|
+
export declare const REDACTED = ":redacted";
|
|
34
|
+
export declare function createRedactor(rules?: RedactionRules): Redactor;
|
|
35
|
+
//# sourceMappingURL=redact.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redact.d.ts","sourceRoot":"","sources":["../../src/analytics/redact.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,cAAc;IAC7B,wHAAwH;IACxH,iBAAiB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,+FAA+F;IAC/F,oBAAoB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC,kGAAkG;IAClG,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC/B,gFAAgF;IAChF,mBAAmB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACxC,sFAAsF;IACtF,QAAQ,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC;CACnD;AAED,MAAM,WAAW,QAAQ;IACvB,4EAA4E;IAC5E,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAC7B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IACnD,gHAAgH;IAChH,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;CAC5B;AAED,eAAO,MAAM,iBAAiB,WAAW,CAAC;AAC1C,eAAO,MAAM,QAAQ,cAAc,CAAC;AAoBpC,wBAAgB,cAAc,CAAC,KAAK,GAAE,cAAmB,GAAG,QAAQ,CAqFnE"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redaction: what analytics and error reports may say about an address, a message or a stack.
|
|
3
|
+
*
|
|
4
|
+
* The union of the three copies' rules, each the strictest: Quickie's token paths (encoded forms too) and generic
|
|
5
|
+
* record ids, PharmaLine's named id segments and its full redaction of paths, and the email, long-number and UK
|
|
6
|
+
* postcode rules both of them had and AltEd lacked. Every query value is replaced. The app supplies its own words:
|
|
7
|
+
* which path prefixes carry a token, which query parameters are sensitive, which collections take an id.
|
|
8
|
+
*/
|
|
9
|
+
export const TOKEN_PLACEHOLDER = ":token";
|
|
10
|
+
export const REDACTED = ":redacted";
|
|
11
|
+
const escape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
12
|
+
/** Prisma's `cuid()` (25 characters, starting with `c`) and UUIDs, as a whole path segment. */
|
|
13
|
+
const RECORD_ID = /(\/)(?:c[a-z0-9]{24}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?=[/?#\s"'\\]|$)/gi;
|
|
14
|
+
const QUERY_VALUE = /([?&][\w.%-]+=)[^&#\s"'\\]+/g;
|
|
15
|
+
const EMAIL = /[\w.+-]+@[\w-]+(?:\.[\w-]+)+/g;
|
|
16
|
+
const LONG_NUMBER = /\+?\d(?:[\s-]?\d){9,}/g;
|
|
17
|
+
const POSTCODE = /\b[A-Z]{1,2}\d[A-Z\d]?\s*\d[A-Z]{2}\b/gi;
|
|
18
|
+
const PLACEHOLDER_ORIGIN = "https://placeholder.invalid";
|
|
19
|
+
const MAX_DEPTH = 32;
|
|
20
|
+
const isPlainObject = (value) => {
|
|
21
|
+
if (value === null || typeof value !== "object")
|
|
22
|
+
return false;
|
|
23
|
+
const prototype = Object.getPrototypeOf(value);
|
|
24
|
+
return prototype === Object.prototype || prototype === null;
|
|
25
|
+
};
|
|
26
|
+
export function createRedactor(rules = {}) {
|
|
27
|
+
const prefixes = [...(rules.tokenPathPrefixes ?? [])].sort((a, b) => b.length - a.length);
|
|
28
|
+
const params = rules.sensitiveQueryParams ?? [];
|
|
29
|
+
const segments = rules.idSegments ?? [];
|
|
30
|
+
const exceptions = rules.idSegmentExceptions ?? ["new", "import"];
|
|
31
|
+
for (const prefix of prefixes) {
|
|
32
|
+
if (!prefix.startsWith("/") || prefix.endsWith("/")) {
|
|
33
|
+
throw new Error(`[analytics] a token path prefix starts with "/" and does not end with one: "${prefix}".`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const PATH_START = String.raw `(^|[\s"'\x60=(<>,;[{]|[a-z][a-z0-9+.-]*:\/\/[^/\s"'\x60<>?#]*)`;
|
|
37
|
+
// A route pattern's placeholder (`[token]`, `:token`) is not a token, and stays.
|
|
38
|
+
const SEGMENT = String.raw `(?![[:])[^/?#\s"'\x60<>)\]},;&]+`;
|
|
39
|
+
const tokenPath = prefixes.length
|
|
40
|
+
? new RegExp(`${PATH_START}(${prefixes.map(escape).join("|")})/${SEGMENT}`, "gi")
|
|
41
|
+
: null;
|
|
42
|
+
const ENCODED_PATH_START = String.raw `(^|=|%3D|[\s"'\x60]|%3A%2F%2F[^%/?#&\s"'\x60]*)`;
|
|
43
|
+
const ENCODED_SEGMENT = String.raw `[^%/?#&\s"'\x60<>]+`;
|
|
44
|
+
const encodedTokenPath = prefixes.length
|
|
45
|
+
? new RegExp(`${ENCODED_PATH_START}(${prefixes.map((p) => escape(p).replace(/\//g, "%2F")).join("|")})%2F${ENCODED_SEGMENT}`, "gi")
|
|
46
|
+
: null;
|
|
47
|
+
const names = params.map(escape).join("|");
|
|
48
|
+
const sensitiveParam = params.length ? new RegExp(String.raw `([?&;#])(${names})=[^&#\s"'\x60<>]*`, "gi") : null;
|
|
49
|
+
const encodedSensitiveParam = params.length
|
|
50
|
+
? new RegExp(String.raw `(%3F|%26)(${names})%3D(?:(?!%26)[^&#\s"'\x60<>])*`, "gi")
|
|
51
|
+
: null;
|
|
52
|
+
const idSegment = segments.length
|
|
53
|
+
? new RegExp(String.raw `(\/(?:${segments.map(escape).join("|")})\/)(?!(?:${exceptions.map(escape).join("|")})(?:[/?#\s"'\\]|$))[^/?#\s"'\\]+`, "gi")
|
|
54
|
+
: null;
|
|
55
|
+
function redact(text) {
|
|
56
|
+
let out = text;
|
|
57
|
+
if (tokenPath)
|
|
58
|
+
out = out.replace(tokenPath, `$1$2/${TOKEN_PLACEHOLDER}`);
|
|
59
|
+
if (encodedTokenPath)
|
|
60
|
+
out = out.replace(encodedTokenPath, `$1$2%2F${TOKEN_PLACEHOLDER}`);
|
|
61
|
+
if (sensitiveParam)
|
|
62
|
+
out = out.replace(sensitiveParam, `$1$2=${REDACTED}`);
|
|
63
|
+
if (encodedSensitiveParam)
|
|
64
|
+
out = out.replace(encodedSensitiveParam, `$1$2%3D${REDACTED}`);
|
|
65
|
+
out = out.replace(RECORD_ID, "$1[id]");
|
|
66
|
+
if (idSegment)
|
|
67
|
+
out = out.replace(idSegment, "$1[id]");
|
|
68
|
+
out = out
|
|
69
|
+
.replace(QUERY_VALUE, "$1[redacted]")
|
|
70
|
+
.replace(EMAIL, "[email]")
|
|
71
|
+
.replace(LONG_NUMBER, "[number]")
|
|
72
|
+
.replace(POSTCODE, "[postcode]");
|
|
73
|
+
for (const [pattern, replacement] of rules.patterns ?? []) {
|
|
74
|
+
out = out.replace(new RegExp(pattern.source, pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`), replacement);
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
function cleanUrl(value, ownOrigin) {
|
|
79
|
+
let url;
|
|
80
|
+
try {
|
|
81
|
+
url = new URL(value, PLACEHOLDER_ORIGIN);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return redact(value);
|
|
85
|
+
}
|
|
86
|
+
if (url.origin === PLACEHOLDER_ORIGIN)
|
|
87
|
+
return redact(url.pathname);
|
|
88
|
+
if (url.origin !== ownOrigin)
|
|
89
|
+
return url.origin;
|
|
90
|
+
return url.origin + redact(url.pathname);
|
|
91
|
+
}
|
|
92
|
+
function redactDeep(value, depth = 0) {
|
|
93
|
+
if (typeof value === "string")
|
|
94
|
+
return redact(value);
|
|
95
|
+
if (depth > MAX_DEPTH)
|
|
96
|
+
return value;
|
|
97
|
+
if (Array.isArray(value))
|
|
98
|
+
return value.map((item) => redactDeep(item, depth + 1));
|
|
99
|
+
if (isPlainObject(value)) {
|
|
100
|
+
const copy = {};
|
|
101
|
+
for (const [key, item] of Object.entries(value))
|
|
102
|
+
copy[redact(key)] = redactDeep(item, depth + 1);
|
|
103
|
+
return copy;
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
return { redact, cleanUrl, redactDeep: (value) => redactDeep(value) };
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=redact.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redact.js","sourceRoot":"","sources":["../../src/analytics/redact.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA2BH,MAAM,CAAC,MAAM,iBAAiB,GAAG,QAAQ,CAAC;AAC1C,MAAM,CAAC,MAAM,QAAQ,GAAG,WAAW,CAAC;AAEpC,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AAE7E,+FAA+F;AAC/F,MAAM,SAAS,GACb,uGAAuG,CAAC;AAC1G,MAAM,WAAW,GAAG,8BAA8B,CAAC;AACnD,MAAM,KAAK,GAAG,+BAA+B,CAAC;AAC9C,MAAM,WAAW,GAAG,wBAAwB,CAAC;AAC7C,MAAM,QAAQ,GAAG,yCAAyC,CAAC;AAC3D,MAAM,kBAAkB,GAAG,6BAA6B,CAAC;AACzD,MAAM,SAAS,GAAG,EAAE,CAAC;AAErB,MAAM,aAAa,GAAG,CAAC,KAAc,EAAoC,EAAE;IACzE,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC9D,MAAM,SAAS,GAAY,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IACxD,OAAO,SAAS,KAAK,MAAM,CAAC,SAAS,IAAI,SAAS,KAAK,IAAI,CAAC;AAC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
1
|
+
/**
|
|
2
|
+
* `@connextar/house/analytics/server` — error reports (`onRequestError` and handled 500s), product events, and GA4's
|
|
3
|
+
* server-side `sign_up`. Imports no browser package, no PostHog and no Next: each is handed in.
|
|
4
|
+
*/
|
|
5
|
+
export { createServerAnalytics, measurementProtocol, readGaCookies, registeredErrorReporter, signUpMethod, type GaCookies, type MeasurementProtocolOptions, type PostHogServerLike, type ServerAnalytics, type ServerAnalyticsOptions, type SignUpContext, } from "./server.js";
|
|
6
|
+
//# sourceMappingURL=server-index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-index.d.ts","sourceRoot":"","sources":["../../src/analytics/server-index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EACb,uBAAuB,EACvB,YAAY,EACZ,KAAK,SAAS,EACd,KAAK,0BAA0B,EAC/B,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,sBAAsB,EAC3B,KAAK,aAAa,GACnB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/analytics/server` — error reports (`onRequestError` and handled 500s), product events, and GA4's
|
|
3
|
+
* server-side `sign_up`. Imports no browser package, no PostHog and no Next: each is handed in.
|
|
4
|
+
*/
|
|
5
|
+
export { createServerAnalytics, measurementProtocol, readGaCookies, registeredErrorReporter, signUpMethod, } from "./server.js";
|
|
6
|
+
//# sourceMappingURL=server-index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-index.js","sourceRoot":"","sources":["../../src/analytics/server-index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,aAAa,EACb,uBAAuB,EACvB,YAAY,GAOb,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Analytics } from "./config.js";
|
|
2
|
+
/**
|
|
3
|
+
* The server half: error reports, product events and GA4's server-side `sign_up`.
|
|
4
|
+
*
|
|
5
|
+
* Imports no browser package, no PostHog and no Next: the app passes its `posthog-node` client (`posthog`) and Next's
|
|
6
|
+
* `after` (`after`), so this runs wherever the app's server code does and the versions stay the app's.
|
|
7
|
+
*
|
|
8
|
+
* - **Errors** — `onRequestError` and the handled 500s — are sent anonymous (no distinct id, no person profile),
|
|
9
|
+
* with the message and every stack frame redacted, and a Prisma error reduced to its code: a Prisma message quotes
|
|
10
|
+
* the query's values. Quickie's frames are taken only after the original `name: message` head, so a message line that
|
|
11
|
+
* looks like ` at 12 High Street` cannot pass as a frame. AltEd reduced nothing and redacted only tokens.
|
|
12
|
+
* - **Events** keyed by a person are refused while viewing as somebody (senco's rule — Quickie's had none) and for a
|
|
13
|
+
* request that sends `Sec-GPC: 1`, and their properties are redacted.
|
|
14
|
+
* - **`sign_up` by Measurement Protocol** only on a production host, never with `Sec-GPC: 1` (AltEd lacked it), and
|
|
15
|
+
* only with both Google cookies — a stray `_ga` from another site is not a session (AltEd accepted it). Clearing
|
|
16
|
+
* them on a no is what withdraws it.
|
|
17
|
+
*/
|
|
18
|
+
/** The parts of posthog-node's client this calls. */
|
|
19
|
+
export interface PostHogServerLike {
|
|
20
|
+
captureExceptionImmediate(error: unknown, distinctId?: string, properties?: Record<string, unknown>): Promise<unknown>;
|
|
21
|
+
capture(event: {
|
|
22
|
+
distinctId: string;
|
|
23
|
+
event: string;
|
|
24
|
+
properties?: Record<string, unknown>;
|
|
25
|
+
}): void;
|
|
26
|
+
flush(): Promise<unknown>;
|
|
27
|
+
}
|
|
28
|
+
export interface ServerAnalyticsOptions {
|
|
29
|
+
analytics: Analytics;
|
|
30
|
+
/** The app's posthog-node client, or null when analytics is off. Called each time, so the env can turn it off. */
|
|
31
|
+
posthog: () => PostHogServerLike | null | undefined;
|
|
32
|
+
/** Whether this request is a "view as". Fail-closed: one that throws counts as yes. */
|
|
33
|
+
isViewingAs?: () => boolean | Promise<boolean>;
|
|
34
|
+
/** The request's headers, for `Sec-GPC`: Next's `headers`. Outside a request, return null. */
|
|
35
|
+
requestHeaders?: () => Pick<Headers, "get"> | null | undefined | Promise<Pick<Headers, "get"> | null | undefined>;
|
|
36
|
+
/** Next's `after`, to send once the response has gone. Default: sent at once, not awaited. */
|
|
37
|
+
after?: (task: () => Promise<void>) => void;
|
|
38
|
+
logger?: {
|
|
39
|
+
error(message: string, ...detail: unknown[]): void;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export type ServerAnalytics = ReturnType<typeof createServerAnalytics>;
|
|
43
|
+
export declare function createServerAnalytics(options: ServerAnalyticsOptions): {
|
|
44
|
+
redactedError: (error: unknown) => Error;
|
|
45
|
+
reportServerError: (error: unknown, properties?: Record<string, unknown>) => Promise<void>;
|
|
46
|
+
reportServerErrorAfterResponse: (error: unknown, properties?: Record<string, unknown>) => void;
|
|
47
|
+
onRequestError: (error: unknown, request: {
|
|
48
|
+
method?: string;
|
|
49
|
+
}, context: {
|
|
50
|
+
routePath?: string;
|
|
51
|
+
routeType?: string;
|
|
52
|
+
}) => Promise<void>;
|
|
53
|
+
captureServerEvent: (distinctId: string, event: string, properties?: Record<string, unknown>) => Promise<void>;
|
|
54
|
+
captureServerEventAfterResponse: (distinctId: string, event: string, properties?: Record<string, unknown>) => void;
|
|
55
|
+
/** For `register()`: puts the after-response reporter where the app's `handleError` finds it. */
|
|
56
|
+
registerErrorReporter(target?: object): void;
|
|
57
|
+
};
|
|
58
|
+
/** The reporter `registerErrorReporter` put on `globalThis`, for `handleError` — a no-op until it is registered. */
|
|
59
|
+
export declare function registeredErrorReporter(analytics: Analytics, target?: object): (error: unknown, properties?: Record<string, unknown>) => void;
|
|
60
|
+
export interface GaCookies {
|
|
61
|
+
clientId: string;
|
|
62
|
+
sessionId: string;
|
|
63
|
+
}
|
|
64
|
+
/** `_ga`'s client id and the `_ga_<id>` session — both, or null. */
|
|
65
|
+
export declare function readGaCookies(cookieHeader: string, measurementId: string): GaCookies | null;
|
|
66
|
+
/** How the account was made, from the Better Auth route that made it. */
|
|
67
|
+
export declare function signUpMethod(path: string | undefined, providerId?: string): string;
|
|
68
|
+
/** Better Auth's endpoint context, as `databaseHooks.user.create.after` receives it. */
|
|
69
|
+
export interface SignUpContext {
|
|
70
|
+
path?: string;
|
|
71
|
+
params?: Record<string, string | undefined>;
|
|
72
|
+
headers?: Headers | Pick<Headers, "get">;
|
|
73
|
+
request?: Request;
|
|
74
|
+
}
|
|
75
|
+
export interface MeasurementProtocolOptions {
|
|
76
|
+
measurementId: string | undefined;
|
|
77
|
+
apiSecret: string | undefined;
|
|
78
|
+
/** The hosts a sign-up counts on: production's. A preview or a laptop never sends one. */
|
|
79
|
+
productionHosts: readonly string[];
|
|
80
|
+
fetch?: typeof fetch;
|
|
81
|
+
logger?: {
|
|
82
|
+
error(message: string, ...detail: unknown[]): void;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** `databaseHooks.user.create.after: async (_user, context) => recordSignUp(context)`. Never throws. */
|
|
86
|
+
export declare function measurementProtocol(options: MeasurementProtocolOptions): {
|
|
87
|
+
recordSignUp(context: SignUpContext | null | undefined): Promise<void>;
|
|
88
|
+
};
|
|
89
|
+
//# sourceMappingURL=server.d.ts.map
|