create-disposabl-app 0.15.0 → 0.17.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 +26 -9
- package/dist/disposabl.js +357 -50
- package/dist/index.js +9026 -160
- package/dist/templates/default/.github/scripts/mobile.sh +3 -2
- package/dist/templates/default/.github/scripts/release.sh +1 -0
- package/dist/templates/default/.github/scripts/verify-android-abis.mjs +22 -0
- package/dist/templates/default/apps/desktop/forge.config.mts +2 -1
- package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
- package/dist/templates/default/apps/mobile/app.config.js +18 -9
- package/dist/templates/default/apps/mobile/package.json +1 -0
- package/dist/templates/default/apps/mobile/plugins/with-e2e-network.js +41 -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 +2 -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/android-architectures.ts +35 -0
- 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/expo-plugin.cjs +23 -1
- 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 +38 -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/android-architectures.test.ts +43 -0
- package/dist/templates/default/packages/tests/android-ota-artifacts.test.ts +5 -1
- package/dist/templates/default/packages/tests/android-ota-selection.test.ts +7 -2
- 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/desktop-packaging.test.ts +12 -0
- package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
- package/dist/templates/default/packages/tests/mobile-android-e2e-network.test.ts +112 -0
- 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
|
@@ -1,9 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
revenueCatDeliveries,
|
|
3
|
+
revenueCatPurchases,
|
|
4
|
+
revenueCatTransfers,
|
|
5
|
+
revenueCatTransferHolds,
|
|
6
|
+
} from "@disposabl/disposabl-kit/server/revenuecat-validator";
|
|
1
7
|
import { desktopUpdateTable } from "./desktopUpdates/validator";
|
|
2
8
|
import { defineSchema, defineTable } from "convex/server";
|
|
3
9
|
import { v } from "convex/values";
|
|
4
10
|
import { profile } from "./users/validator";
|
|
5
11
|
|
|
6
12
|
export default defineSchema({
|
|
13
|
+
telemetryIssuance: defineTable({
|
|
14
|
+
key: v.literal("global"),
|
|
15
|
+
tokens: v.number(),
|
|
16
|
+
updatedAt: v.number(),
|
|
17
|
+
}).index("by_key", ["key"]),
|
|
18
|
+
telemetryIdentities: defineTable({
|
|
19
|
+
hash: v.string(),
|
|
20
|
+
userId: v.optional(v.string()),
|
|
21
|
+
expiresAt: v.number(),
|
|
22
|
+
}).index("by_hash", ["hash"]),
|
|
23
|
+
billingHandoffs: defineTable({
|
|
24
|
+
hash: v.string(),
|
|
25
|
+
userId: v.string(),
|
|
26
|
+
sessionId: v.string(),
|
|
27
|
+
purchase: v.optional(v.union(v.literal("monthly"), v.literal("topup"))),
|
|
28
|
+
expiresAt: v.number(),
|
|
29
|
+
}).index("by_hash", ["hash"]),
|
|
30
|
+
billingAttempts: defineTable({
|
|
31
|
+
userId: v.string(),
|
|
32
|
+
purchase: v.string(),
|
|
33
|
+
requestId: v.string(),
|
|
34
|
+
url: v.optional(v.string()),
|
|
35
|
+
resolvedAt: v.optional(v.number()),
|
|
36
|
+
resolution: v.optional(
|
|
37
|
+
v.object({ outcome: v.string(), evidence: v.string() }),
|
|
38
|
+
),
|
|
39
|
+
})
|
|
40
|
+
.index("by_userId_purchase_resolvedAt_url", [
|
|
41
|
+
"userId",
|
|
42
|
+
"purchase",
|
|
43
|
+
"resolvedAt",
|
|
44
|
+
"url",
|
|
45
|
+
])
|
|
46
|
+
.index("by_userId_requestId", ["userId", "requestId"]),
|
|
47
|
+
revenueCatDeliveries,
|
|
48
|
+
revenueCatTransfers,
|
|
49
|
+
revenueCatTransferHolds,
|
|
50
|
+
revenueCatPurchases,
|
|
7
51
|
desktopUpdates: desktopUpdateTable,
|
|
8
52
|
files: defineTable({
|
|
9
53
|
owner: v.string(),
|
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import { v } from "convex/values";
|
|
2
|
+
import { makeFunctionReference } from "convex/server";
|
|
2
3
|
import { httpAction, internalAction } from "./_generated/server";
|
|
3
4
|
import { getSessionUserId } from "@disposabl/disposabl-kit/server/lib/functions";
|
|
4
5
|
import {
|
|
5
6
|
captureTelemetry,
|
|
6
|
-
|
|
7
|
+
hashTelemetryIdentity,
|
|
8
|
+
telemetryEnabled,
|
|
7
9
|
} from "@disposabl/disposabl-kit/server/lib/telemetry";
|
|
8
10
|
|
|
11
|
+
export {
|
|
12
|
+
claimIdentity,
|
|
13
|
+
expireIdentity,
|
|
14
|
+
} from "@disposabl/disposabl-kit/server/lib/telemetry";
|
|
15
|
+
|
|
16
|
+
const claimIdentityRef = makeFunctionReference<
|
|
17
|
+
"mutation",
|
|
18
|
+
{ hash?: string; newHash: string; userId?: string },
|
|
19
|
+
boolean | null
|
|
20
|
+
>("telemetry:claimIdentity");
|
|
21
|
+
|
|
9
22
|
const pageNames = [
|
|
10
23
|
"home",
|
|
11
24
|
"dashboard",
|
|
@@ -57,6 +70,8 @@ const headers = {
|
|
|
57
70
|
export const options = httpAction(
|
|
58
71
|
async () => new Response(null, { status: 204, headers }),
|
|
59
72
|
);
|
|
73
|
+
const anonymousIdPattern =
|
|
74
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
60
75
|
export const page = httpAction(async (ctx, request) => {
|
|
61
76
|
try {
|
|
62
77
|
const body: unknown = await request.json();
|
|
@@ -67,18 +82,39 @@ export const page = httpAction(async (ctx, request) => {
|
|
|
67
82
|
!("page" in body) ||
|
|
68
83
|
(body.platform !== "web" && body.platform !== "mobile") ||
|
|
69
84
|
typeof body.page !== "string" ||
|
|
70
|
-
!pageNames.includes(body.page as (typeof pageNames)[number])
|
|
85
|
+
!pageNames.includes(body.page as (typeof pageNames)[number]) ||
|
|
86
|
+
("anonymousId" in body &&
|
|
87
|
+
(typeof body.anonymousId !== "string" ||
|
|
88
|
+
!anonymousIdPattern.test(body.anonymousId)))
|
|
71
89
|
)
|
|
72
90
|
return new Response(null, { status: 400, headers });
|
|
91
|
+
if (!telemetryEnabled())
|
|
92
|
+
return new Response(null, { status: 204, headers });
|
|
73
93
|
const userId = await getSessionUserId(ctx);
|
|
74
|
-
|
|
75
|
-
|
|
94
|
+
if (request.headers.has("Authorization") && !userId)
|
|
95
|
+
return new Response(null, { status: 401, headers });
|
|
96
|
+
const existingId =
|
|
97
|
+
"anonymousId" in body ? (body.anonymousId as string) : undefined;
|
|
98
|
+
const issuedId = crypto.randomUUID();
|
|
99
|
+
const accepted = await ctx.runMutation(claimIdentityRef, {
|
|
100
|
+
...(existingId ? { hash: await hashTelemetryIdentity(existingId) } : {}),
|
|
101
|
+
newHash: await hashTelemetryIdentity(issuedId),
|
|
102
|
+
...(userId ? { userId } : {}),
|
|
103
|
+
});
|
|
104
|
+
if (accepted === null) return new Response(null, { status: 429, headers });
|
|
105
|
+
const anonymousId = accepted && existingId ? existingId : issuedId;
|
|
106
|
+
await captureTelemetry(
|
|
76
107
|
body.platform === "web" ? "page_view" : "screen_view",
|
|
77
108
|
{ page: body.page as (typeof pageNames)[number] },
|
|
78
109
|
userId ?? undefined,
|
|
110
|
+
undefined,
|
|
111
|
+
anonymousId,
|
|
79
112
|
);
|
|
113
|
+
return new Response(JSON.stringify({ anonymousId }), {
|
|
114
|
+
status: 200,
|
|
115
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
116
|
+
});
|
|
80
117
|
} catch {
|
|
81
118
|
return new Response(null, { status: 400, headers });
|
|
82
119
|
}
|
|
83
|
-
return new Response(null, { status: 204, headers });
|
|
84
120
|
});
|
|
@@ -30,6 +30,12 @@ async function writeChanged(path: string, text: string) {
|
|
|
30
30
|
|
|
31
31
|
/** Discover opt-in modules, validate the contract, then update both transports together. */
|
|
32
32
|
export async function generate(acceptBreaking = false) {
|
|
33
|
+
// Fresh scaffolds have no route tree; page manifests must reflect current routes.
|
|
34
|
+
const routes = spawnSync(process.execPath, ["run", "generate:routes"], {
|
|
35
|
+
cwd: join(root, "../../apps/web"),
|
|
36
|
+
stdio: "inherit",
|
|
37
|
+
});
|
|
38
|
+
if (routes.status !== 0) throw new Error("Web route generation failed");
|
|
33
39
|
const files = await readdir(convex, { recursive: true });
|
|
34
40
|
const modules: Record<string, Record<string, unknown>> = {};
|
|
35
41
|
for (const file of files.sort()) {
|
|
@@ -52,7 +52,7 @@ export { apiUrl } from "./origin";
|
|
|
52
52
|
const fetchFor = (origin: string): typeof fetch =>
|
|
53
53
|
Object.assign(
|
|
54
54
|
async (
|
|
55
|
-
input:
|
|
55
|
+
input: string | URL | Request,
|
|
56
56
|
init?: Parameters<typeof fetch>[1],
|
|
57
57
|
) => {
|
|
58
58
|
const url = new URL(input instanceof Request ? input.url : input);
|
|
@@ -66,8 +66,10 @@ const fetchFor = (origin: string): typeof fetch =>
|
|
|
66
66
|
signal: init?.signal ?? AbortSignal.timeout(30_000),
|
|
67
67
|
});
|
|
68
68
|
},
|
|
69
|
-
{
|
|
70
|
-
|
|
69
|
+
{
|
|
70
|
+
preconnect: (fetch as typeof fetch & { preconnect?: unknown }).preconnect,
|
|
71
|
+
},
|
|
72
|
+
) as typeof fetch;
|
|
71
73
|
async function load(url: string): Promise<Session> {
|
|
72
74
|
try {
|
|
73
75
|
return JSON.parse(await readFile(sessionPath(url), "utf8"));
|
|
@@ -76,6 +78,13 @@ async function load(url: string): Promise<Session> {
|
|
|
76
78
|
throw error;
|
|
77
79
|
}
|
|
78
80
|
}
|
|
81
|
+
// Local-only binding. Never send this credential fingerprint to analytics.
|
|
82
|
+
export async function credentialRevision() {
|
|
83
|
+
const session = await load("https://www.disposabl.dev");
|
|
84
|
+
return createHash("sha256")
|
|
85
|
+
.update(session.tokens?.access_token ?? "anonymous")
|
|
86
|
+
.digest("hex");
|
|
87
|
+
}
|
|
79
88
|
async function save(url: string, session: Session) {
|
|
80
89
|
await mkdir(directory(), { recursive: true, mode: 0o700 });
|
|
81
90
|
await chmod(directory(), 0o700);
|
|
@@ -280,6 +289,26 @@ async function withCredentials<T>(
|
|
|
280
289
|
await release();
|
|
281
290
|
}
|
|
282
291
|
}
|
|
292
|
+
export function withCredentialSession<T>(
|
|
293
|
+
url: string,
|
|
294
|
+
operation: (session: {
|
|
295
|
+
login: (browser?: boolean) => Promise<void>;
|
|
296
|
+
logout: () => Promise<void>;
|
|
297
|
+
hasCredentials: () => Promise<boolean>;
|
|
298
|
+
accessToken: (refresh?: boolean) => Promise<string>;
|
|
299
|
+
loginAccessToken: () => Promise<string | undefined>;
|
|
300
|
+
}) => Promise<T>,
|
|
301
|
+
): Promise<T> {
|
|
302
|
+
return withCredentials(url, () =>
|
|
303
|
+
operation({
|
|
304
|
+
login: (browser) => unlockedLogin(url, browser),
|
|
305
|
+
logout: () => unlockedLogout(url),
|
|
306
|
+
hasCredentials: () => hasCredentials(url),
|
|
307
|
+
accessToken: (refresh) => unlockedAccessToken(url, refresh),
|
|
308
|
+
loginAccessToken: () => loginAccessToken(url),
|
|
309
|
+
}),
|
|
310
|
+
);
|
|
311
|
+
}
|
|
283
312
|
export const login = (url: string, browser = true) =>
|
|
284
313
|
withCredentials(url, () => unlockedLogin(url, browser));
|
|
285
314
|
export const accessToken = (url: string, refresh = false) =>
|
|
@@ -291,3 +320,7 @@ export const hasCredentials = async (url: string) =>
|
|
|
291
320
|
Boolean(
|
|
292
321
|
process.env.DISPOSABL_API_KEY || (await load(url)).tokens?.access_token,
|
|
293
322
|
);
|
|
323
|
+
|
|
324
|
+
// A freshly completed login has already stored its token; do not refresh it for telemetry.
|
|
325
|
+
export const loginAccessToken = async (url: string) =>
|
|
326
|
+
(await load(url)).tokens?.access_token;
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
import { telemetryBuildDefine } from "./telemetry-build";
|
|
2
|
+
import { name, version } from "./package.json";
|
|
1
3
|
const result = await Bun.build({
|
|
2
4
|
entrypoints: ["index.ts"],
|
|
3
5
|
outdir: "dist",
|
|
4
6
|
target: "node",
|
|
5
7
|
packages: "external",
|
|
8
|
+
define:
|
|
9
|
+
name === "@disposabl/cli"
|
|
10
|
+
? telemetryBuildDefine(version)
|
|
11
|
+
: telemetryBuildDefine("0.0.0-development", {}),
|
|
6
12
|
});
|
|
7
13
|
if (!result.success) throw new AggregateError(result.logs, "CLI build failed");
|
|
8
14
|
|
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
|
+
import { apiError } from "./errors";
|
|
2
3
|
import manifest from "./package.json";
|
|
4
|
+
import {
|
|
5
|
+
identify,
|
|
6
|
+
installedRelease,
|
|
7
|
+
prepareLogin,
|
|
8
|
+
telemetryStatePath,
|
|
9
|
+
resetIdentity,
|
|
10
|
+
telemetryConfig,
|
|
11
|
+
} from "./telemetry";
|
|
3
12
|
import {
|
|
4
13
|
NotAuthenticated,
|
|
5
14
|
accessToken,
|
|
6
15
|
apiUrl,
|
|
7
16
|
hasCredentials,
|
|
8
17
|
login,
|
|
18
|
+
loginAccessToken,
|
|
9
19
|
logout,
|
|
20
|
+
withCredentialSession,
|
|
21
|
+
credentialRevision,
|
|
10
22
|
} from "./auth";
|
|
11
23
|
|
|
12
24
|
export type Schema = {
|
|
@@ -24,16 +36,107 @@ export type Operation = {
|
|
|
24
36
|
requestBody?: { content: { "application/json": { schema: Schema } } };
|
|
25
37
|
};
|
|
26
38
|
|
|
27
|
-
|
|
39
|
+
type ProgramDependencies = {
|
|
40
|
+
version?: string;
|
|
41
|
+
telemetryPath?: string;
|
|
42
|
+
login?: typeof login;
|
|
43
|
+
logout?: typeof logout;
|
|
44
|
+
hasCredentials?: typeof hasCredentials;
|
|
45
|
+
accessToken?: typeof accessToken;
|
|
46
|
+
loginAccessToken?: typeof loginAccessToken;
|
|
47
|
+
credentialSession?: typeof withCredentialSession;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export function createProgram(
|
|
51
|
+
serviceOrigin: string,
|
|
52
|
+
centralAnalytics = false,
|
|
53
|
+
dependencies: ProgramDependencies = {},
|
|
54
|
+
) {
|
|
55
|
+
const version = dependencies.version ?? manifest.version;
|
|
56
|
+
const path = () => dependencies.telemetryPath ?? telemetryStatePath();
|
|
57
|
+
const sessionLogin = dependencies.login ?? login;
|
|
58
|
+
const sessionLogout = dependencies.logout ?? logout;
|
|
59
|
+
const sessionHasCredentials = dependencies.hasCredentials ?? hasCredentials;
|
|
60
|
+
const sessionAccessToken = dependencies.accessToken ?? accessToken;
|
|
61
|
+
const sessionLoginAccessToken =
|
|
62
|
+
dependencies.loginAccessToken ?? loginAccessToken;
|
|
63
|
+
const credentialSession: typeof withCredentialSession = (url, operation) => {
|
|
64
|
+
if (dependencies.credentialSession)
|
|
65
|
+
return dependencies.credentialSession(url, operation);
|
|
66
|
+
if (!(
|
|
67
|
+
dependencies.login ||
|
|
68
|
+
dependencies.logout ||
|
|
69
|
+
dependencies.hasCredentials ||
|
|
70
|
+
dependencies.accessToken ||
|
|
71
|
+
dependencies.loginAccessToken
|
|
72
|
+
))
|
|
73
|
+
return withCredentialSession(url, operation);
|
|
74
|
+
return operation({
|
|
75
|
+
login: (browser) => sessionLogin(url, browser),
|
|
76
|
+
logout: () => sessionLogout(url),
|
|
77
|
+
hasCredentials: () => sessionHasCredentials(url),
|
|
78
|
+
accessToken: (refresh) => sessionAccessToken(url, refresh),
|
|
79
|
+
loginAccessToken: () => sessionLoginAccessToken(url),
|
|
80
|
+
});
|
|
81
|
+
};
|
|
28
82
|
const program = new Command()
|
|
29
83
|
.name(Object.keys(manifest.bin)[0])
|
|
30
84
|
.version(manifest.version);
|
|
85
|
+
if (centralAnalytics)
|
|
86
|
+
program.addHelpText(
|
|
87
|
+
"after",
|
|
88
|
+
"\nDisposabl collects limited CLI usage when configured. Set DO_NOT_TRACK=1 to opt out.\n",
|
|
89
|
+
);
|
|
31
90
|
const origin = () => apiUrl(serviceOrigin);
|
|
32
91
|
const loginCommand = program
|
|
33
92
|
.command("login")
|
|
34
93
|
.option("--no-browser", "Print the login URL without opening a browser")
|
|
35
94
|
.action(async (options) => {
|
|
36
|
-
|
|
95
|
+
const base = origin();
|
|
96
|
+
const central =
|
|
97
|
+
centralAnalytics &&
|
|
98
|
+
base === "https://www.disposabl.dev" &&
|
|
99
|
+
installedRelease(version);
|
|
100
|
+
const enabled = central && telemetryConfig(version);
|
|
101
|
+
const { attempt, token, revision } = await credentialSession(
|
|
102
|
+
base,
|
|
103
|
+
async (session) => {
|
|
104
|
+
let attempt: string | undefined;
|
|
105
|
+
if (central) {
|
|
106
|
+
if (enabled) attempt = await prepareLogin(path());
|
|
107
|
+
else await resetIdentity(path());
|
|
108
|
+
}
|
|
109
|
+
await session.login(options.browser);
|
|
110
|
+
return {
|
|
111
|
+
attempt,
|
|
112
|
+
revision: enabled
|
|
113
|
+
? await credentialRevision().catch(() => undefined)
|
|
114
|
+
: undefined,
|
|
115
|
+
token: enabled
|
|
116
|
+
? await session.loginAccessToken().catch(() => undefined)
|
|
117
|
+
: undefined,
|
|
118
|
+
};
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
if (enabled && token && revision) {
|
|
122
|
+
try {
|
|
123
|
+
const response = await fetch(`${base}/api/users/current`, {
|
|
124
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
125
|
+
redirect: "error",
|
|
126
|
+
signal: AbortSignal.timeout(500),
|
|
127
|
+
});
|
|
128
|
+
if (response.ok)
|
|
129
|
+
await identify(
|
|
130
|
+
version,
|
|
131
|
+
(await response.json())?.id,
|
|
132
|
+
path(),
|
|
133
|
+
attempt,
|
|
134
|
+
revision,
|
|
135
|
+
);
|
|
136
|
+
} catch {
|
|
137
|
+
/* identity lookup is optional */
|
|
138
|
+
}
|
|
139
|
+
}
|
|
37
140
|
console.error("Signed in.");
|
|
38
141
|
});
|
|
39
142
|
loginCommand
|
|
@@ -41,39 +144,108 @@ export function createProgram(serviceOrigin: string) {
|
|
|
41
144
|
.description("Return the server-verified current user")
|
|
42
145
|
.action(async () => {
|
|
43
146
|
const base = origin();
|
|
44
|
-
|
|
147
|
+
const enabled =
|
|
148
|
+
centralAnalytics &&
|
|
149
|
+
base === "https://www.disposabl.dev" &&
|
|
150
|
+
telemetryConfig(version);
|
|
151
|
+
let snapshot:
|
|
152
|
+
| {
|
|
153
|
+
token: string;
|
|
154
|
+
attempt: string | undefined;
|
|
155
|
+
revision: string | undefined;
|
|
156
|
+
}
|
|
157
|
+
| undefined;
|
|
158
|
+
try {
|
|
159
|
+
snapshot = await credentialSession(base, async (session) => {
|
|
160
|
+
if (!(await session.hasCredentials())) {
|
|
161
|
+
if (enabled) await resetIdentity(path());
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const attempt = enabled ? await prepareLogin(path()) : undefined;
|
|
165
|
+
try {
|
|
166
|
+
const token = await session.accessToken();
|
|
167
|
+
return {
|
|
168
|
+
token,
|
|
169
|
+
attempt,
|
|
170
|
+
revision: enabled
|
|
171
|
+
? await credentialRevision().catch(() => undefined)
|
|
172
|
+
: undefined,
|
|
173
|
+
};
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (enabled && attempt) await resetIdentity(path(), attempt);
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (!(error instanceof NotAuthenticated)) throw error;
|
|
181
|
+
console.log(JSON.stringify({ currentUser: null }));
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (!snapshot) {
|
|
45
185
|
console.log(JSON.stringify({ currentUser: null }));
|
|
46
186
|
return;
|
|
47
187
|
}
|
|
48
|
-
const send =
|
|
188
|
+
const send = (token: string) =>
|
|
49
189
|
fetch(`${base}/api/users/current`, {
|
|
50
190
|
headers: {
|
|
51
|
-
Authorization: `Bearer ${
|
|
191
|
+
Authorization: `Bearer ${token}`,
|
|
52
192
|
"X-Disposabl-Source": "cli",
|
|
53
193
|
},
|
|
54
194
|
redirect: "error",
|
|
55
195
|
signal: AbortSignal.timeout(30_000),
|
|
56
196
|
});
|
|
57
|
-
let response
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
197
|
+
let response = await send(snapshot.token);
|
|
198
|
+
if (response.status === 401 && !process.env.DISPOSABL_API_KEY) {
|
|
199
|
+
try {
|
|
200
|
+
const refreshed = await credentialSession(base, async (session) => {
|
|
201
|
+
if ((await session.loginAccessToken()) !== snapshot!.token)
|
|
202
|
+
return undefined;
|
|
203
|
+
const token = await session.accessToken(true);
|
|
204
|
+
return {
|
|
205
|
+
token,
|
|
206
|
+
revision: enabled
|
|
207
|
+
? await credentialRevision().catch(() => undefined)
|
|
208
|
+
: undefined,
|
|
209
|
+
};
|
|
210
|
+
});
|
|
211
|
+
if (refreshed) {
|
|
212
|
+
snapshot.revision = refreshed.revision;
|
|
213
|
+
response = await send(refreshed.token);
|
|
214
|
+
}
|
|
215
|
+
} catch (error) {
|
|
216
|
+
if (!(error instanceof NotAuthenticated)) throw error;
|
|
217
|
+
}
|
|
66
218
|
}
|
|
67
219
|
if (response.status === 401) {
|
|
220
|
+
if (enabled && snapshot.attempt)
|
|
221
|
+
await resetIdentity(path(), snapshot.attempt);
|
|
68
222
|
console.log(JSON.stringify({ currentUser: null }));
|
|
69
223
|
return;
|
|
70
224
|
}
|
|
71
225
|
if (!response.ok)
|
|
72
226
|
throw new Error(`Login status failed (${response.status}).`);
|
|
73
|
-
|
|
227
|
+
const currentUser = await response.json();
|
|
228
|
+
if (enabled && snapshot.revision)
|
|
229
|
+
await identify(
|
|
230
|
+
version,
|
|
231
|
+
currentUser?.id,
|
|
232
|
+
path(),
|
|
233
|
+
snapshot.attempt,
|
|
234
|
+
snapshot.revision,
|
|
235
|
+
);
|
|
236
|
+
console.log(JSON.stringify({ currentUser }));
|
|
74
237
|
});
|
|
75
238
|
program.command("logout").action(async () => {
|
|
76
|
-
|
|
239
|
+
const base = origin();
|
|
240
|
+
await credentialSession(base, async (session) => {
|
|
241
|
+
await session.logout();
|
|
242
|
+
if (
|
|
243
|
+
centralAnalytics &&
|
|
244
|
+
base === "https://www.disposabl.dev" &&
|
|
245
|
+
installedRelease(version)
|
|
246
|
+
)
|
|
247
|
+
await resetIdentity(path());
|
|
248
|
+
});
|
|
77
249
|
console.error("Signed out.");
|
|
78
250
|
});
|
|
79
251
|
|
|
@@ -184,9 +356,7 @@ export async function runApiCli(argv: string[], serviceOrigin: string) {
|
|
|
184
356
|
);
|
|
185
357
|
const response = await requestOperation(base, path, method, input)();
|
|
186
358
|
if (!response.ok)
|
|
187
|
-
throw new Error(
|
|
188
|
-
`API request failed (${response.status}): ${await response.text()}`,
|
|
189
|
-
);
|
|
359
|
+
throw new Error(apiError(response.status, await response.text()));
|
|
190
360
|
console.log(JSON.stringify(await response.json(), null, 2));
|
|
191
361
|
});
|
|
192
362
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Format the credit gate without trusting a redirect or starting checkout. */
|
|
2
|
+
export function apiError(status: number, body: string): string {
|
|
3
|
+
try {
|
|
4
|
+
const data = JSON.parse(body)?.error;
|
|
5
|
+
if (data?.code === "credits_required") {
|
|
6
|
+
const paymentUrl =
|
|
7
|
+
data.paymentUrl === "https://www.disposabl.dev/pricing"
|
|
8
|
+
? `\nAdd credits: ${data.paymentUrl}`
|
|
9
|
+
: "";
|
|
10
|
+
return `At least 10 credits are required for each agent request. Agent compute costs 10 credits per 10-minute window; AI is currently included.${paymentUrl}`;
|
|
11
|
+
}
|
|
12
|
+
if (data?.code === "credits_unavailable")
|
|
13
|
+
return "Credits are temporarily unavailable. Please retry.";
|
|
14
|
+
} catch {
|
|
15
|
+
// Keep the ordinary API error for responses that are not JSON.
|
|
16
|
+
}
|
|
17
|
+
return `API request failed (${status}): ${body}`;
|
|
18
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Public ingestion configuration only. Personal API keys must never be bundled. */
|
|
2
|
+
export function telemetryBuildDefine(
|
|
3
|
+
version: string,
|
|
4
|
+
env: Record<string, string | undefined> = process.env,
|
|
5
|
+
) {
|
|
6
|
+
const token = env.DISPOSABL_CLI_POSTHOG_PROJECT_TOKEN ?? "";
|
|
7
|
+
const host = env.DISPOSABL_CLI_POSTHOG_HOST ?? "";
|
|
8
|
+
const valid =
|
|
9
|
+
/^phc_[a-zA-Z0-9]+$/.test(token) &&
|
|
10
|
+
["https://us.i.posthog.com", "https://eu.i.posthog.com"].includes(host);
|
|
11
|
+
if ((token || host) && !valid)
|
|
12
|
+
throw new Error(
|
|
13
|
+
"Invalid central PostHog public project token or ingestion host.",
|
|
14
|
+
);
|
|
15
|
+
if (
|
|
16
|
+
!valid &&
|
|
17
|
+
(version !== "0.0.0-development" ||
|
|
18
|
+
env.DISPOSABL_CLI_TELEMETRY_REQUIRED === "1")
|
|
19
|
+
)
|
|
20
|
+
throw new Error(
|
|
21
|
+
"Release requires the central public PostHog project token and ingestion host.",
|
|
22
|
+
);
|
|
23
|
+
return {
|
|
24
|
+
"process.env.DISPOSABL_CLI_POSTHOG_PROJECT_TOKEN": JSON.stringify(token),
|
|
25
|
+
"process.env.DISPOSABL_CLI_POSTHOG_HOST": JSON.stringify(host),
|
|
26
|
+
};
|
|
27
|
+
}
|