create-disposabl-app 0.14.18 → 0.16.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 +15 -1
- package/dist/disposabl.js +357 -50
- package/dist/index.js +8973 -154
- package/dist/templates/default/.github/scripts/macos-release.sh +81 -0
- package/dist/templates/default/.github/scripts/platforms.sh +1 -1
- package/dist/templates/default/.github/scripts/release.sh +1 -1
- package/dist/templates/default/.github/workflows/pipeline.yml +14 -3
- package/dist/templates/default/apps/desktop/README.md +13 -2
- package/dist/templates/default/apps/desktop/forge.config.mts +9 -3
- package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
- package/dist/templates/default/apps/mobile/package.json +1 -0
- package/dist/templates/default/apps/web/eslint.config.ts +5 -1
- package/dist/templates/default/apps/web/public/disposabl-logo-light.svg +8 -0
- package/dist/templates/default/apps/web/public/landing/coast.jpg +0 -0
- package/dist/templates/default/apps/web/public/landing/community.jpg +0 -0
- package/dist/templates/default/apps/web/public/landing/stores/app-store.svg +46 -0
- package/dist/templates/default/apps/web/public/landing/stores/google-play.png +0 -0
- package/dist/templates/default/apps/web/public/landing/taste/LICENSE +21 -0
- package/dist/templates/default/apps/web/public/landing/taste/init.js +3 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/lib.js +189 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/moments.js +109 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.css +8 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.js +35 -0
- package/dist/templates/default/apps/web/public/landing/taste/motion/motion.css +321 -0
- package/dist/templates/default/apps/web/public/landing/taste/motion/motion.js +406 -0
- package/dist/templates/default/apps/web/routes.config.ts +14 -0
- package/dist/templates/default/apps/web/src/router/__root.tsx +8 -2
- package/dist/templates/default/apps/web/tsr.config.json +1 -1
- package/dist/templates/default/apps/web/vite.config.ts +10 -2
- package/dist/templates/default/bun.lock +6 -0
- package/dist/templates/default/bunfig.toml +1 -0
- package/dist/templates/default/disposabl.json +1 -1
- package/dist/templates/default/packages/backend/convex/authSetup.ts +2 -5
- package/dist/templates/default/packages/backend/convex/billing.ts +5 -0
- package/dist/templates/default/packages/backend/convex/billingHandoff.ts +6 -0
- package/dist/templates/default/packages/backend/convex/convex.config.ts +8 -0
- package/dist/templates/default/packages/backend/convex/http.ts +6 -0
- package/dist/templates/default/packages/backend/convex/revenuecat.ts +13 -0
- package/dist/templates/default/packages/backend/convex/schema.ts +44 -0
- package/dist/templates/default/packages/backend/convex/telemetry.ts +41 -5
- package/dist/templates/default/packages/backend/generate.ts +6 -0
- package/dist/templates/default/packages/cli/auth.ts +36 -3
- package/dist/templates/default/packages/cli/build.ts +6 -0
- package/dist/templates/default/packages/cli/client.ts +189 -19
- package/dist/templates/default/packages/cli/errors.ts +18 -0
- package/dist/templates/default/packages/cli/telemetry-build.ts +27 -0
- package/dist/templates/default/packages/cli/telemetry.ts +266 -0
- package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +23 -5
- package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
- package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
- package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
- package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
- package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
- package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
- package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
- package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
- package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
- package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
- package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
- package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
- package/dist/templates/default/packages/reconfigure-skill/SKILL.md +36 -0
- package/dist/templates/default/packages/sdk/README.md +21 -0
- package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
- package/dist/templates/default/packages/sdk/src/index.ts +2 -0
- package/dist/templates/default/packages/skill/SKILL.md +2 -0
- package/dist/templates/default/packages/skill/references/agent.md +2 -0
- package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
- package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
- package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
- package/dist/templates/default/packages/tests/billing.test.ts +48 -4
- package/dist/templates/default/packages/tests/ci-platform-selection.test.ts +54 -0
- package/dist/templates/default/packages/tests/desktop-macos-release.test.ts +226 -0
- package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
- package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
- package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
- package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
- package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
- package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
- package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
- package/package.json +1 -1
- package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
|
@@ -11,20 +11,40 @@ import {
|
|
|
11
11
|
import { useAuthToken } from "@convex-dev/auth/react";
|
|
12
12
|
import { sessionId } from "../../lib/auth-session";
|
|
13
13
|
import { paymentsEnabled } from "../../features";
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
requestCheckout,
|
|
16
|
+
handoffContext,
|
|
17
|
+
handoffAllows,
|
|
18
|
+
type CheckoutResult,
|
|
19
|
+
} from "../../payments/checkout-attempt";
|
|
20
|
+
import {
|
|
21
|
+
pricing,
|
|
22
|
+
creditQuantity,
|
|
23
|
+
formatPrice,
|
|
24
|
+
monthlyLabel,
|
|
25
|
+
} from "../../payments/config";
|
|
15
26
|
|
|
16
27
|
export const Route = createFileRoute("/_providers/pricing")({
|
|
17
|
-
component:
|
|
28
|
+
component: () => (
|
|
29
|
+
<main>
|
|
30
|
+
<Pricing />
|
|
31
|
+
</main>
|
|
32
|
+
),
|
|
18
33
|
});
|
|
19
34
|
const snapshot = makeFunctionReference<
|
|
20
35
|
"action",
|
|
21
36
|
Record<string, never>,
|
|
22
|
-
{
|
|
37
|
+
{
|
|
38
|
+
balance: number | null;
|
|
39
|
+
hasMonthlyPlan: boolean | null;
|
|
40
|
+
checkoutNeedsReview: boolean;
|
|
41
|
+
customerId: string;
|
|
42
|
+
}
|
|
23
43
|
>("billing:snapshot");
|
|
24
44
|
const checkout = makeFunctionReference<
|
|
25
45
|
"action",
|
|
26
|
-
{ purchase: "monthly" | "topup" },
|
|
27
|
-
|
|
46
|
+
{ purchase: "monthly" | "topup"; requestId: string },
|
|
47
|
+
CheckoutResult
|
|
28
48
|
>("billing:checkout");
|
|
29
49
|
const portal = makeFunctionReference<
|
|
30
50
|
"action",
|
|
@@ -32,13 +52,13 @@ const portal = makeFunctionReference<
|
|
|
32
52
|
{ url: string }
|
|
33
53
|
>("billing:portal");
|
|
34
54
|
|
|
35
|
-
function Pricing() {
|
|
55
|
+
export function Pricing() {
|
|
36
56
|
if (!paymentsEnabled)
|
|
37
57
|
return (
|
|
38
|
-
<
|
|
58
|
+
<section className="mx-auto max-w-5xl p-8">
|
|
39
59
|
<h1>Payments are unavailable</h1>
|
|
40
60
|
<SubscriptionHelp />
|
|
41
|
-
</
|
|
61
|
+
</section>
|
|
42
62
|
);
|
|
43
63
|
return <SessionPricing />;
|
|
44
64
|
}
|
|
@@ -72,12 +92,22 @@ function SubscriptionHelp() {
|
|
|
72
92
|
);
|
|
73
93
|
}
|
|
74
94
|
function PurchasePage({ session }: { session: string | null }) {
|
|
95
|
+
const [{ handoff, fromIos }] = useState(() =>
|
|
96
|
+
typeof window === "undefined"
|
|
97
|
+
? handoffContext(null, "")
|
|
98
|
+
: handoffContext(
|
|
99
|
+
window.sessionStorage.getItem("billing-from-ios"),
|
|
100
|
+
window.location.search,
|
|
101
|
+
),
|
|
102
|
+
);
|
|
75
103
|
const { isLoading } = useConvexAuth();
|
|
76
104
|
const getSnapshot = useAction(snapshot);
|
|
77
105
|
const start = useAction(checkout);
|
|
78
106
|
const openPortal = useAction(portal);
|
|
79
107
|
const [balance, setBalance] = useState<number | null>(null);
|
|
80
108
|
const [hasMonthlyPlan, setHasMonthlyPlan] = useState<boolean | null>(null);
|
|
109
|
+
const [checkoutNeedsReview, setCheckoutNeedsReview] = useState(false);
|
|
110
|
+
const [customerId, setCustomerId] = useState<string | null>(null);
|
|
81
111
|
const [message, setMessage] = useState("");
|
|
82
112
|
const [refreshError, setRefreshError] = useState("");
|
|
83
113
|
const [busy, setBusy] = useState(false);
|
|
@@ -102,6 +132,8 @@ function PurchasePage({ session }: { session: string | null }) {
|
|
|
102
132
|
return;
|
|
103
133
|
setBalance(result.balance);
|
|
104
134
|
setHasMonthlyPlan(result.hasMonthlyPlan);
|
|
135
|
+
setCheckoutNeedsReview(result.checkoutNeedsReview);
|
|
136
|
+
setCustomerId(result.customerId);
|
|
105
137
|
setRefreshError("");
|
|
106
138
|
} catch {
|
|
107
139
|
if (
|
|
@@ -117,14 +149,30 @@ function PurchasePage({ session }: { session: string | null }) {
|
|
|
117
149
|
if (session) void refresh();
|
|
118
150
|
}, [session, refresh]);
|
|
119
151
|
async function purchase(kind: "monthly" | "topup") {
|
|
120
|
-
if (
|
|
152
|
+
if (
|
|
153
|
+
!session ||
|
|
154
|
+
!customerId ||
|
|
155
|
+
busy ||
|
|
156
|
+
!handoffAllows(fromIos, handoff, customerId, kind)
|
|
157
|
+
)
|
|
158
|
+
return;
|
|
121
159
|
const version = generation.current;
|
|
122
160
|
setBusy(true);
|
|
123
161
|
setMessage("");
|
|
124
162
|
try {
|
|
125
|
-
const result = await
|
|
163
|
+
const result = await requestCheckout(
|
|
164
|
+
localStorage,
|
|
165
|
+
customerId,
|
|
166
|
+
kind,
|
|
167
|
+
start,
|
|
168
|
+
);
|
|
126
169
|
if (version !== generation.current) return;
|
|
127
|
-
if (result.
|
|
170
|
+
if (result.retryAllowed) {
|
|
171
|
+
setMessage(
|
|
172
|
+
"Your previous checkout is closed. Choose Buy again to start a new checkout.",
|
|
173
|
+
);
|
|
174
|
+
await refresh();
|
|
175
|
+
} else if (result.url) window.location.assign(result.url);
|
|
128
176
|
else {
|
|
129
177
|
setMessage(
|
|
130
178
|
"Your purchase is being processed. Refresh the balance to check for confirmation.",
|
|
@@ -133,7 +181,10 @@ function PurchasePage({ session }: { session: string | null }) {
|
|
|
133
181
|
}
|
|
134
182
|
} catch {
|
|
135
183
|
if (version === generation.current)
|
|
136
|
-
setMessage(
|
|
184
|
+
setMessage(
|
|
185
|
+
"Checkout status could not be confirmed. Refresh and contact support before retrying if it needs review.",
|
|
186
|
+
);
|
|
187
|
+
await refresh();
|
|
137
188
|
} finally {
|
|
138
189
|
if (version === generation.current) setBusy(false);
|
|
139
190
|
}
|
|
@@ -152,12 +203,9 @@ function PurchasePage({ session }: { session: string | null }) {
|
|
|
152
203
|
}
|
|
153
204
|
}
|
|
154
205
|
return (
|
|
155
|
-
<
|
|
206
|
+
<section className="mx-auto max-w-5xl space-y-8 p-8">
|
|
156
207
|
<div>
|
|
157
208
|
<h1 className="text-3xl font-semibold">Pricing</h1>
|
|
158
|
-
<p>
|
|
159
|
-
{pricing.creditsPerDollar} credits per {formatPrice(1)}.
|
|
160
|
-
</p>
|
|
161
209
|
</div>
|
|
162
210
|
{isLoading ? (
|
|
163
211
|
<p>Checking your account...</p>
|
|
@@ -181,6 +229,15 @@ function PurchasePage({ session }: { session: string | null }) {
|
|
|
181
229
|
After checkout, credits appear when payment is confirmed. Refresh if
|
|
182
230
|
the balance has not updated yet.
|
|
183
231
|
</p>
|
|
232
|
+
{fromIos && (
|
|
233
|
+
<p>After checkout, return to the app and refresh your credits.</p>
|
|
234
|
+
)}
|
|
235
|
+
{checkoutNeedsReview && (
|
|
236
|
+
<p role="alert">
|
|
237
|
+
A previous checkout needs review. Contact support before retrying
|
|
238
|
+
that purchase.
|
|
239
|
+
</p>
|
|
240
|
+
)}
|
|
184
241
|
<button
|
|
185
242
|
className="underline"
|
|
186
243
|
disabled={busy}
|
|
@@ -191,36 +248,49 @@ function PurchasePage({ session }: { session: string | null }) {
|
|
|
191
248
|
</>
|
|
192
249
|
)}
|
|
193
250
|
<div className="grid gap-5 md:grid-cols-2">
|
|
194
|
-
|
|
195
|
-
<
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
251
|
+
{(!fromIos || handoff?.purchase === "monthly") && (
|
|
252
|
+
<section className="rounded-lg border p-6">
|
|
253
|
+
<h2 className="text-xl font-semibold">{monthlyLabel()}</h2>
|
|
254
|
+
<p>{formatPrice(pricing.monthly.price)} / month</p>
|
|
255
|
+
<p>
|
|
256
|
+
{creditQuantity("monthly")} credits each month. Unused monthly
|
|
257
|
+
credits expire.
|
|
258
|
+
</p>
|
|
259
|
+
<button
|
|
260
|
+
className="mt-4 rounded bg-primary px-4 py-2 text-primary-foreground disabled:opacity-50"
|
|
261
|
+
disabled={
|
|
262
|
+
!session ||
|
|
263
|
+
!customerId ||
|
|
264
|
+
busy ||
|
|
265
|
+
hasMonthlyPlan !== false ||
|
|
266
|
+
!handoffAllows(fromIos, handoff, customerId, "monthly")
|
|
267
|
+
}
|
|
268
|
+
onClick={() => void purchase("monthly")}
|
|
269
|
+
>
|
|
270
|
+
{hasMonthlyPlan
|
|
271
|
+
? "Monthly plan active or scheduled"
|
|
272
|
+
: "Choose monthly"}
|
|
273
|
+
</button>
|
|
274
|
+
</section>
|
|
275
|
+
)}
|
|
276
|
+
{handoffAllows(fromIos, handoff, customerId, "topup") && (
|
|
277
|
+
<section className="rounded-lg border p-6">
|
|
278
|
+
<h2 className="text-xl font-semibold">Top-up</h2>
|
|
279
|
+
<p>{formatPrice(pricing.topup.price)} once</p>
|
|
280
|
+
<p>{creditQuantity("topup")} credits that do not expire.</p>
|
|
281
|
+
<button
|
|
282
|
+
className="mt-4 rounded bg-primary px-4 py-2 text-primary-foreground disabled:opacity-50"
|
|
283
|
+
disabled={!session || !customerId || busy}
|
|
284
|
+
onClick={() => void purchase("topup")}
|
|
285
|
+
>
|
|
286
|
+
Buy top-up
|
|
287
|
+
</button>
|
|
288
|
+
</section>
|
|
289
|
+
)}
|
|
220
290
|
</div>
|
|
221
291
|
<SubscriptionHelp />
|
|
222
292
|
{refreshError && <p role="status">{refreshError}</p>}
|
|
223
293
|
{message && <p role="status">{message}</p>}
|
|
224
|
-
</
|
|
294
|
+
</section>
|
|
225
295
|
);
|
|
226
296
|
}
|
|
@@ -3,6 +3,7 @@ import { appName } from "../../lib/app-name";
|
|
|
3
3
|
import { LegalFooter } from "../components/legal-page";
|
|
4
4
|
import { LogOut } from "../components/ui/log-out";
|
|
5
5
|
import { useState } from "react";
|
|
6
|
+
import { ThemeProvider } from "../lib/theme";
|
|
6
7
|
import {
|
|
7
8
|
createFileRoute,
|
|
8
9
|
Outlet,
|
|
@@ -36,11 +37,18 @@ function Providers() {
|
|
|
36
37
|
});
|
|
37
38
|
|
|
38
39
|
return (
|
|
39
|
-
<
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
<ThemeProvider
|
|
41
|
+
attribute="class"
|
|
42
|
+
defaultTheme="system"
|
|
43
|
+
enableSystem
|
|
44
|
+
disableTransitionOnChange
|
|
45
|
+
>
|
|
46
|
+
<AuthProvider client={client} enabled={authenticationEnabled}>
|
|
47
|
+
<SiteHeader />
|
|
48
|
+
<Outlet />
|
|
49
|
+
<LegalFooter />
|
|
50
|
+
</AuthProvider>
|
|
51
|
+
</ThemeProvider>
|
|
44
52
|
);
|
|
45
53
|
}
|
|
46
54
|
|
|
@@ -15,11 +15,11 @@ const kitRoute = (file: string) =>
|
|
|
15
15
|
);
|
|
16
16
|
|
|
17
17
|
// Both Vite and `tsr generate` use this route tree.
|
|
18
|
-
export const createRoutes = (enabled: boolean) =>
|
|
18
|
+
export const createRoutes = (enabled: boolean, includePricing = true) =>
|
|
19
19
|
rootRoute("__root.tsx", [
|
|
20
20
|
layout("_providers", kitRoute("route.tsx"), [
|
|
21
21
|
physical("."),
|
|
22
|
-
route("/pricing", kitRoute("pricing.tsx")),
|
|
22
|
+
...(includePricing ? [route("/pricing", kitRoute("pricing.tsx"))] : []),
|
|
23
23
|
route("/legal", kitRoute("legal/index.tsx")),
|
|
24
24
|
route("/legal/privacy", kitRoute("legal/privacy.tsx")),
|
|
25
25
|
route("/legal/terms", kitRoute("legal/terms.tsx")),
|
|
@@ -28,6 +28,7 @@ export const createRoutes = (enabled: boolean) =>
|
|
|
28
28
|
...(enabled
|
|
29
29
|
? [
|
|
30
30
|
route("/login", kitRoute("_auth-pages/login.tsx")),
|
|
31
|
+
route("/billing-login", kitRoute("billing-login.tsx")),
|
|
31
32
|
route("/signup", kitRoute("_auth-pages/signUp.tsx")),
|
|
32
33
|
route("/consent", kitRoute("consent.tsx")),
|
|
33
34
|
route(
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { sessionId } from "./auth-session";
|
|
2
|
+
|
|
1
3
|
export function pageCategory(pathname: string) {
|
|
2
4
|
return pathname === "/"
|
|
3
5
|
? "home"
|
|
@@ -13,18 +15,152 @@ export function pageCategory(pathname: string) {
|
|
|
13
15
|
? "account"
|
|
14
16
|
: "other";
|
|
15
17
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
|
|
19
|
+
const uuid =
|
|
20
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
21
|
+
const storageKey = "disposabl.analytics.identity.v1";
|
|
22
|
+
type State = { anonymousId: string | null; session: string | null };
|
|
23
|
+
type Storage = {
|
|
24
|
+
getItem: (key: string) => string | null | Promise<string | null>;
|
|
25
|
+
setItem: (key: string, value: string) => void | Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
type View = {
|
|
28
|
+
origin: string;
|
|
29
|
+
platform: "web" | "mobile";
|
|
30
|
+
page: string;
|
|
31
|
+
token: string | null;
|
|
32
|
+
session: string | null;
|
|
33
|
+
generation: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** One in-flight view and one latest pending view; never queues user content. */
|
|
37
|
+
export function createPageTelemetry(
|
|
38
|
+
storage: Storage,
|
|
39
|
+
transport: typeof fetch = fetch,
|
|
40
|
+
exclusive: (work: () => Promise<void>) => Promise<void> = (work) => work(),
|
|
21
41
|
) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
42
|
+
let state: State | undefined;
|
|
43
|
+
let observed: string | null | undefined;
|
|
44
|
+
let generation = 0;
|
|
45
|
+
let appliedGeneration = 0;
|
|
46
|
+
let pending: View | undefined;
|
|
47
|
+
let running = false;
|
|
48
|
+
|
|
49
|
+
async function load() {
|
|
50
|
+
if (state) return;
|
|
51
|
+
try {
|
|
52
|
+
const value = JSON.parse((await storage.getItem(storageKey)) || "null");
|
|
53
|
+
if (
|
|
54
|
+
value &&
|
|
55
|
+
typeof value === "object" &&
|
|
56
|
+
uuid.test(value.anonymousId) &&
|
|
57
|
+
(value.session === null || typeof value.session === "string")
|
|
58
|
+
) {
|
|
59
|
+
state = { anonymousId: value.anonymousId, session: value.session };
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
/* Storage is optional. */
|
|
63
|
+
}
|
|
64
|
+
state ??= { anonymousId: null, session: null };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function save() {
|
|
68
|
+
try {
|
|
69
|
+
await storage.setItem(storageKey, JSON.stringify(state));
|
|
70
|
+
return true;
|
|
71
|
+
} catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function drain() {
|
|
77
|
+
if (running) return;
|
|
78
|
+
running = true;
|
|
79
|
+
try {
|
|
80
|
+
while (pending) {
|
|
81
|
+
const view = pending;
|
|
82
|
+
pending = undefined;
|
|
83
|
+
await exclusive(async () => {
|
|
84
|
+
// Reload under the shared lock: another tab may have changed identity.
|
|
85
|
+
state = undefined;
|
|
86
|
+
await load();
|
|
87
|
+
if (
|
|
88
|
+
(state!.session !== null && state!.session !== view.session) ||
|
|
89
|
+
appliedGeneration !== view.generation
|
|
90
|
+
) {
|
|
91
|
+
state!.anonymousId = null;
|
|
92
|
+
}
|
|
93
|
+
appliedGeneration = view.generation;
|
|
94
|
+
state!.session = view.session;
|
|
95
|
+
// Persist the rotation before sending; a crash must not reuse an old identity.
|
|
96
|
+
if (!(await save()) || view.generation !== generation) return;
|
|
97
|
+
const controller = new AbortController();
|
|
98
|
+
const timer = setTimeout(() => controller.abort(), 6500);
|
|
99
|
+
try {
|
|
100
|
+
const response = await transport(`${view.origin}/telemetry/page`, {
|
|
101
|
+
method: "POST",
|
|
102
|
+
signal: controller.signal,
|
|
103
|
+
headers: {
|
|
104
|
+
"Content-Type": "text/plain",
|
|
105
|
+
...(view.token
|
|
106
|
+
? { Authorization: `Bearer ${view.token}` }
|
|
107
|
+
: {}),
|
|
108
|
+
},
|
|
109
|
+
body: JSON.stringify({
|
|
110
|
+
platform: view.platform,
|
|
111
|
+
page: view.page,
|
|
112
|
+
...(state!.anonymousId && { anonymousId: state!.anonymousId }),
|
|
113
|
+
}),
|
|
114
|
+
});
|
|
115
|
+
if (response.ok && view.generation === generation) {
|
|
116
|
+
const data: unknown = await response.json();
|
|
117
|
+
if (
|
|
118
|
+
data &&
|
|
119
|
+
typeof data === "object" &&
|
|
120
|
+
"anonymousId" in data &&
|
|
121
|
+
typeof data.anonymousId === "string" &&
|
|
122
|
+
view.generation === generation &&
|
|
123
|
+
uuid.test(data.anonymousId)
|
|
124
|
+
) {
|
|
125
|
+
state!.anonymousId = data.anonymousId;
|
|
126
|
+
await save();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
/* Never interrupt the app or retry indefinitely. */
|
|
131
|
+
} finally {
|
|
132
|
+
clearTimeout(timer);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
} catch {
|
|
137
|
+
// Lock and storage failures must not affect the app.
|
|
138
|
+
} finally {
|
|
139
|
+
running = false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
view(
|
|
145
|
+
origin: string,
|
|
146
|
+
platform: "web" | "mobile",
|
|
147
|
+
pathname: string,
|
|
148
|
+
token: string | null,
|
|
149
|
+
) {
|
|
150
|
+
const session = sessionId(token);
|
|
151
|
+
if (token && !session) return;
|
|
152
|
+
if (observed !== undefined && observed !== null && observed !== session)
|
|
153
|
+
generation++;
|
|
154
|
+
observed = session;
|
|
155
|
+
pending = {
|
|
156
|
+
origin,
|
|
157
|
+
platform,
|
|
158
|
+
page: pageCategory(pathname),
|
|
159
|
+
token,
|
|
160
|
+
session,
|
|
161
|
+
generation,
|
|
162
|
+
};
|
|
163
|
+
void drain();
|
|
27
164
|
},
|
|
28
|
-
|
|
29
|
-
}).catch(() => {});
|
|
165
|
+
};
|
|
30
166
|
}
|