create-disposabl-app 0.8.9 → 0.8.11
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 +8 -1
- package/dist/disposabl.js +42 -42
- package/dist/index.js +1 -1
- package/dist/templates/default/bun.lock +1 -7
- package/dist/templates/default/packages/backend/README.md +4 -8
- package/dist/templates/default/packages/backend/convex/_generated/api.d.ts +0 -67
- package/dist/templates/default/packages/backend/convex/_generated/server.d.ts +0 -9
- package/dist/templates/default/packages/backend/convex/convex.config.ts +0 -11
- package/dist/templates/default/packages/backend/convex/http.ts +1 -9
- package/dist/templates/default/packages/backend/convex/schema.ts +1 -69
- package/dist/templates/default/packages/backend/generated/openapi.json +1 -1262
- package/dist/templates/default/packages/backend/generated/pages.json +0 -4
- package/dist/templates/default/packages/backend/generated/platforms.ts +2 -20
- package/dist/templates/default/packages/backend/package.json +1 -7
- package/dist/templates/default/packages/cli/README.md +5 -1
- package/dist/templates/default/packages/cli/commands.ts +15 -8
- package/dist/templates/default/packages/cli/delete.ts +40 -31
- package/dist/templates/default/packages/skill/SKILL.md +13 -5
- package/dist/templates/default/packages/skill/references/delivery.md +1 -1
- package/package.json +1 -1
- package/dist/templates/default/packages/backend/convex/apps/actions/cleanup.ts +0 -296
- package/dist/templates/default/packages/backend/convex/apps/actions/create.ts +0 -30
- package/dist/templates/default/packages/backend/convex/apps/actions/delete.ts +0 -15
- package/dist/templates/default/packages/backend/convex/apps/actions/expo.ts +0 -174
- package/dist/templates/default/packages/backend/convex/apps/actions/setup.ts +0 -48
- package/dist/templates/default/packages/backend/convex/apps/internal.ts +0 -211
- package/dist/templates/default/packages/backend/convex/apps/operations/internal.ts +0 -213
- package/dist/templates/default/packages/backend/convex/apps/validator.ts +0 -23
- package/dist/templates/default/packages/backend/convex/connections/actions/credentials.ts +0 -56
- package/dist/templates/default/packages/backend/convex/connections/actions/expo.ts +0 -79
- package/dist/templates/default/packages/backend/convex/connections/actions/github.ts +0 -119
- package/dist/templates/default/packages/backend/convex/connections/actions/oauth.ts +0 -128
- package/dist/templates/default/packages/backend/convex/connections/index.ts +0 -80
- package/dist/templates/default/packages/backend/convex/connections/internal.ts +0 -147
- package/dist/templates/default/packages/backend/convex/connections/validator.ts +0 -35
- package/dist/templates/default/packages/backend/convex/downloads/action.ts +0 -136
- package/dist/templates/default/packages/backend/convex/downloads/http.ts +0 -32
- package/dist/templates/default/packages/backend/convex/downloads/internal.ts +0 -58
- package/dist/templates/default/packages/backend/convex/github/actions/http.ts +0 -69
- package/dist/templates/default/packages/backend/convex/github/actions/internal.ts +0 -196
- package/dist/templates/default/packages/backend/convex/github/actions/oidc.ts +0 -75
- package/dist/templates/default/packages/backend/convex/github/actions/provision.ts +0 -76
- package/dist/templates/default/packages/backend/convex/github/actions/secrets.ts +0 -180
- package/dist/templates/default/packages/backend/convex/github/actions/signing.ts +0 -41
- package/dist/templates/default/packages/backend/convex/github/actions/tokens.ts +0 -145
- package/dist/templates/default/packages/backend/convex/github/identityCache.ts +0 -54
- package/dist/templates/default/packages/backend/convex/github/internal.ts +0 -33
- package/dist/templates/default/packages/backend/convex/previews/internal.ts +0 -257
- package/dist/templates/default/packages/backend/convex/previews/token/actions/create.ts +0 -83
- package/dist/templates/default/packages/backend/convex/previews/token/actions/management.ts +0 -182
- package/dist/templates/default/packages/backend/convex/previews/token/internal.ts +0 -32
- package/dist/templates/default/packages/backend/convex/releases/internal.ts +0 -28
- package/dist/templates/default/packages/backend/convex/validator.ts +0 -87
- package/dist/templates/default/packages/sdk/src/generated/client/client.gen.ts +0 -300
- package/dist/templates/default/packages/sdk/src/generated/client/index.ts +0 -27
- package/dist/templates/default/packages/sdk/src/generated/client/types.gen.ts +0 -238
- package/dist/templates/default/packages/sdk/src/generated/client/utils.gen.ts +0 -337
- package/dist/templates/default/packages/sdk/src/generated/client.gen.ts +0 -28
- package/dist/templates/default/packages/sdk/src/generated/core/auth.gen.ts +0 -49
- package/dist/templates/default/packages/sdk/src/generated/core/bodySerializer.gen.ts +0 -96
- package/dist/templates/default/packages/sdk/src/generated/core/params.gen.ts +0 -188
- package/dist/templates/default/packages/sdk/src/generated/core/pathSerializer.gen.ts +0 -186
- package/dist/templates/default/packages/sdk/src/generated/core/queryKeySerializer.gen.ts +0 -134
- package/dist/templates/default/packages/sdk/src/generated/core/serverSentEvents.gen.ts +0 -265
- package/dist/templates/default/packages/sdk/src/generated/core/types.gen.ts +0 -126
- package/dist/templates/default/packages/sdk/src/generated/core/utils.gen.ts +0 -146
- package/dist/templates/default/packages/sdk/src/generated/index.ts +0 -63
- package/dist/templates/default/packages/sdk/src/generated/sdk.gen.ts +0 -234
- package/dist/templates/default/packages/sdk/src/generated/types.gen.ts +0 -660
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
"use node";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
identityFields,
|
|
5
|
-
internalAction,
|
|
6
|
-
} from "@disposabl/disposabl-kit/server/lib/functions";
|
|
7
|
-
import { v } from "convex/values";
|
|
8
|
-
import { internal } from "../../_generated/api";
|
|
9
|
-
import { credential, unseal } from "../../connections/actions/credentials";
|
|
10
|
-
import { ensureExpo } from "../../apps/actions/expo";
|
|
11
|
-
import { validateExpo } from "../../connections/actions/expo";
|
|
12
|
-
import {
|
|
13
|
-
management,
|
|
14
|
-
ensureProduction,
|
|
15
|
-
} from "../../previews/token/actions/management";
|
|
16
|
-
import { githubRequest, githubToken } from "./tokens";
|
|
17
|
-
import { generateAndroidSigning } from "./signing";
|
|
18
|
-
|
|
19
|
-
export const configure = internalAction({
|
|
20
|
-
args: { ...identityFields, appId: v.string(), projectId: v.number() },
|
|
21
|
-
returns: v.null(),
|
|
22
|
-
handler: async (ctx, args) => {
|
|
23
|
-
const details = await ctx.runQuery(internal.apps.internal.get, {
|
|
24
|
-
userId: args.userId,
|
|
25
|
-
appId: args.appId,
|
|
26
|
-
});
|
|
27
|
-
const app = await ctx.runQuery(internal.apps.internal.githubApp, {
|
|
28
|
-
appId: args.appId,
|
|
29
|
-
});
|
|
30
|
-
if (!app)
|
|
31
|
-
throw new Error(
|
|
32
|
-
"Reconnect the GitHub account that owns this app before continuing setup.",
|
|
33
|
-
);
|
|
34
|
-
const repository = `${app.githubOwner}/${app.githubRepo}`;
|
|
35
|
-
const token = await githubToken(
|
|
36
|
-
ctx,
|
|
37
|
-
app.installationId,
|
|
38
|
-
["secrets:write"],
|
|
39
|
-
repository,
|
|
40
|
-
);
|
|
41
|
-
const path = `/repos/${repository}/actions/secrets`;
|
|
42
|
-
const names = new Set<string>();
|
|
43
|
-
for (let page = 1; ; page++) {
|
|
44
|
-
const existing = await githubRequest<{ secrets: { name: string }[] }>(
|
|
45
|
-
token,
|
|
46
|
-
`${path}?per_page=100&page=${page}`,
|
|
47
|
-
);
|
|
48
|
-
for (const secret of existing.secrets) names.add(secret.name);
|
|
49
|
-
if (existing.secrets.length < 100) break;
|
|
50
|
-
}
|
|
51
|
-
const legacySigning = [
|
|
52
|
-
"ANDROID_KEYSTORE_BASE64",
|
|
53
|
-
"ANDROID_KEYSTORE_PASSWORD",
|
|
54
|
-
"ANDROID_KEY_ALIAS",
|
|
55
|
-
"ANDROID_KEY_PASSWORD",
|
|
56
|
-
];
|
|
57
|
-
const legacyCount = legacySigning.filter((name) => names.has(name)).length;
|
|
58
|
-
if (
|
|
59
|
-
details.platforms.includes("android") &&
|
|
60
|
-
!names.has("DISPOSABL_ANDROID_SIGNING") &&
|
|
61
|
-
legacyCount > 0 &&
|
|
62
|
-
legacyCount < legacySigning.length
|
|
63
|
-
)
|
|
64
|
-
throw new Error(
|
|
65
|
-
"Android signing secrets are incomplete. Restore all four legacy ANDROID_* signing secrets before retrying setup. If this app has never shipped, remove the partial set to generate a new key.",
|
|
66
|
-
);
|
|
67
|
-
const publicKey = await githubRequest<{ key_id: string; key: string }>(
|
|
68
|
-
token,
|
|
69
|
-
`${path}/public-key`,
|
|
70
|
-
);
|
|
71
|
-
const { default: sodium } = await import("libsodium-wrappers");
|
|
72
|
-
await sodium.ready;
|
|
73
|
-
const save = async (name: string, value: string) => {
|
|
74
|
-
const encrypted = sodium.crypto_box_seal(
|
|
75
|
-
value,
|
|
76
|
-
sodium.from_base64(publicKey.key, sodium.base64_variants.ORIGINAL),
|
|
77
|
-
);
|
|
78
|
-
await githubRequest(
|
|
79
|
-
token,
|
|
80
|
-
`${path}/${name}`,
|
|
81
|
-
{
|
|
82
|
-
key_id: publicKey.key_id,
|
|
83
|
-
encrypted_value: sodium.to_base64(
|
|
84
|
-
encrypted,
|
|
85
|
-
sodium.base64_variants.ORIGINAL,
|
|
86
|
-
),
|
|
87
|
-
},
|
|
88
|
-
"PUT",
|
|
89
|
-
);
|
|
90
|
-
};
|
|
91
|
-
if (
|
|
92
|
-
details.platforms.includes("android") &&
|
|
93
|
-
!names.has("DISPOSABL_ANDROID_SIGNING") &&
|
|
94
|
-
legacyCount === 0
|
|
95
|
-
) {
|
|
96
|
-
await save(
|
|
97
|
-
"DISPOSABL_ANDROID_SIGNING",
|
|
98
|
-
JSON.stringify(await generateAndroidSigning()),
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
const { token: managementToken } = await credential(
|
|
102
|
-
ctx,
|
|
103
|
-
args.userId,
|
|
104
|
-
"convex",
|
|
105
|
-
);
|
|
106
|
-
if (!names.has("CONVEX_DEPLOY_KEY")) {
|
|
107
|
-
const deployment = await ensureProduction(
|
|
108
|
-
managementToken,
|
|
109
|
-
args.projectId,
|
|
110
|
-
);
|
|
111
|
-
const { deployKey } = await management<{ deployKey: string }>(
|
|
112
|
-
managementToken,
|
|
113
|
-
`/deployments/${deployment}/create_deploy_key`,
|
|
114
|
-
{ name: "github-actions-production" },
|
|
115
|
-
);
|
|
116
|
-
if (!deployKey.startsWith(`prod:${deployment}|`))
|
|
117
|
-
throw new Error("Expected a production deploy key.");
|
|
118
|
-
await save("CONVEX_DEPLOY_KEY", deployKey);
|
|
119
|
-
}
|
|
120
|
-
if (!names.has("CONVEX_DEPLOY_KEY_PREVIEW")) {
|
|
121
|
-
const { previewDeployKey } = await management<{
|
|
122
|
-
previewDeployKey: string;
|
|
123
|
-
}>(
|
|
124
|
-
managementToken,
|
|
125
|
-
`/projects/${args.projectId}/create_preview_deploy_key`,
|
|
126
|
-
{ name: "github-actions-preview" },
|
|
127
|
-
);
|
|
128
|
-
if (!previewDeployKey.startsWith("preview:"))
|
|
129
|
-
throw new Error("Expected a preview deploy key.");
|
|
130
|
-
await save("CONVEX_DEPLOY_KEY_PREVIEW", previewDeployKey);
|
|
131
|
-
}
|
|
132
|
-
const expo = await ctx.runQuery(internal.connections.internal.get, {
|
|
133
|
-
userId: args.userId,
|
|
134
|
-
provider: "expo",
|
|
135
|
-
});
|
|
136
|
-
if (
|
|
137
|
-
expo &&
|
|
138
|
-
details.platforms.some(
|
|
139
|
-
(platform) => platform === "android" || platform === "ios",
|
|
140
|
-
)
|
|
141
|
-
) {
|
|
142
|
-
const token = unseal(expo.token, `${args.userId}:expo`);
|
|
143
|
-
const accounts = await validateExpo(token);
|
|
144
|
-
if (!accounts.some((account) => account.id === expo.accountId))
|
|
145
|
-
throw new Error(
|
|
146
|
-
"Reconnect your Expo account before setting up mobile builds.",
|
|
147
|
-
);
|
|
148
|
-
const linked = await ctx.runQuery(
|
|
149
|
-
internal.apps.operations.internal.linked,
|
|
150
|
-
{
|
|
151
|
-
userId: args.userId,
|
|
152
|
-
appId: args.appId,
|
|
153
|
-
},
|
|
154
|
-
);
|
|
155
|
-
await ctx.runMutation(internal.apps.operations.internal.saveExpo, {
|
|
156
|
-
userId: args.userId,
|
|
157
|
-
appId: args.appId,
|
|
158
|
-
accountId: expo.accountId,
|
|
159
|
-
});
|
|
160
|
-
const project = await ensureExpo(
|
|
161
|
-
token,
|
|
162
|
-
expo.accountId,
|
|
163
|
-
expo.name,
|
|
164
|
-
args.appId,
|
|
165
|
-
linked.expoProjectId,
|
|
166
|
-
);
|
|
167
|
-
await ctx.runMutation(internal.apps.operations.internal.saveExpo, {
|
|
168
|
-
userId: args.userId,
|
|
169
|
-
appId: args.appId,
|
|
170
|
-
projectId: project.id,
|
|
171
|
-
accountId: expo.accountId,
|
|
172
|
-
});
|
|
173
|
-
await save("EXPO_PROJECT_ID", project.id);
|
|
174
|
-
await save("EXPO_SLUG", project.slug);
|
|
175
|
-
await save("EXPO_TOKEN", token);
|
|
176
|
-
await save("EXPO_OWNER", expo.name);
|
|
177
|
-
}
|
|
178
|
-
return null;
|
|
179
|
-
},
|
|
180
|
-
});
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use node";
|
|
2
|
-
|
|
3
|
-
import { generateKeyPair, randomBytes } from "node:crypto";
|
|
4
|
-
import { promisify } from "node:util";
|
|
5
|
-
import forge from "node-forge";
|
|
6
|
-
|
|
7
|
-
export async function generateAndroidSigning() {
|
|
8
|
-
const { privateKey, publicKey } = await promisify(generateKeyPair)("rsa", {
|
|
9
|
-
modulusLength: 2048,
|
|
10
|
-
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
11
|
-
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
12
|
-
});
|
|
13
|
-
const key = forge.pki.privateKeyFromPem(privateKey);
|
|
14
|
-
const certificate = forge.pki.createCertificate();
|
|
15
|
-
certificate.publicKey = forge.pki.publicKeyFromPem(publicKey);
|
|
16
|
-
certificate.serialNumber = `01${randomBytes(16).toString("hex")}`;
|
|
17
|
-
certificate.validity.notBefore = new Date();
|
|
18
|
-
certificate.validity.notAfter = new Date();
|
|
19
|
-
certificate.validity.notAfter.setUTCFullYear(
|
|
20
|
-
certificate.validity.notAfter.getUTCFullYear() + 30,
|
|
21
|
-
);
|
|
22
|
-
const subject = [{ name: "commonName", value: "Android Release" }];
|
|
23
|
-
certificate.setSubject(subject);
|
|
24
|
-
certificate.setIssuer(subject);
|
|
25
|
-
certificate.sign(key, forge.md.sha256.create());
|
|
26
|
-
const storePassword = randomBytes(32).toString("base64url");
|
|
27
|
-
const keyAlias = "android-release";
|
|
28
|
-
const p12 = forge.pkcs12.toPkcs12Asn1(key, [certificate], storePassword, {
|
|
29
|
-
algorithm: "3des",
|
|
30
|
-
friendlyName: keyAlias,
|
|
31
|
-
});
|
|
32
|
-
return {
|
|
33
|
-
keystoreBase64: Buffer.from(
|
|
34
|
-
forge.asn1.toDer(p12).getBytes(),
|
|
35
|
-
"binary",
|
|
36
|
-
).toString("base64"),
|
|
37
|
-
storePassword,
|
|
38
|
-
keyAlias,
|
|
39
|
-
keyPassword: storePassword,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
"use node";
|
|
2
|
-
|
|
3
|
-
import { getToken } from "@vercel/connect";
|
|
4
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
5
|
-
import { ConvexError } from "convex/values";
|
|
6
|
-
import { env, type ActionCtx } from "../../_generated/server";
|
|
7
|
-
import { internal } from "../../_generated/api";
|
|
8
|
-
import { seal, unseal } from "../../connections/actions/credentials";
|
|
9
|
-
|
|
10
|
-
export async function githubToken(
|
|
11
|
-
ctx: ActionCtx,
|
|
12
|
-
installationId: string,
|
|
13
|
-
permissions: string[],
|
|
14
|
-
repository?: string,
|
|
15
|
-
) {
|
|
16
|
-
const token = await vercelIdentity(ctx);
|
|
17
|
-
return getToken(
|
|
18
|
-
env.GITHUB_CONNECTOR!,
|
|
19
|
-
{
|
|
20
|
-
subject: { type: "app" },
|
|
21
|
-
installationId,
|
|
22
|
-
authorizationDetails: [
|
|
23
|
-
{
|
|
24
|
-
type: "github_app_installation",
|
|
25
|
-
permissions,
|
|
26
|
-
...(repository ? { repositories: [repository] } : {}),
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
},
|
|
30
|
-
{ vercelToken: token },
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
export async function vercelIdentity(ctx: ActionCtx): Promise<string> {
|
|
34
|
-
if (
|
|
35
|
-
!env.VERCEL_TOKEN ||
|
|
36
|
-
!env.VERCEL_PROJECT_ID ||
|
|
37
|
-
!env.VERCEL_TEAM_ID ||
|
|
38
|
-
!env.GITHUB_CONNECTOR
|
|
39
|
-
)
|
|
40
|
-
throw new Error("GitHub App connection is not configured.");
|
|
41
|
-
const key = createHash("sha256")
|
|
42
|
-
.update(
|
|
43
|
-
JSON.stringify([
|
|
44
|
-
env.VERCEL_PROJECT_ID,
|
|
45
|
-
env.VERCEL_TEAM_ID,
|
|
46
|
-
env.VERCEL_TOKEN,
|
|
47
|
-
env.CONNECTIONS_ENCRYPTION_KEY,
|
|
48
|
-
]),
|
|
49
|
-
)
|
|
50
|
-
.digest("hex");
|
|
51
|
-
const lease = randomUUID();
|
|
52
|
-
const cached = await ctx.runMutation(internal.github.identityCache.acquire, {
|
|
53
|
-
key,
|
|
54
|
-
lease,
|
|
55
|
-
});
|
|
56
|
-
if (cached.token) return unseal(cached.token, `vercel-identity:${key}`);
|
|
57
|
-
if (!cached.acquired)
|
|
58
|
-
throw new ConvexError({
|
|
59
|
-
code: "UPSTREAM_RETRY",
|
|
60
|
-
retryAfter: cached.retryAfter,
|
|
61
|
-
});
|
|
62
|
-
let refreshAfter = Date.now() + 5_000;
|
|
63
|
-
try {
|
|
64
|
-
const response = await fetch(
|
|
65
|
-
`https://api.vercel.com/v1/projects/${encodeURIComponent(env.VERCEL_PROJECT_ID)}/token?teamId=${encodeURIComponent(env.VERCEL_TEAM_ID)}`,
|
|
66
|
-
{
|
|
67
|
-
method: "POST",
|
|
68
|
-
redirect: "error",
|
|
69
|
-
headers: { Authorization: `Bearer ${env.VERCEL_TOKEN}` },
|
|
70
|
-
signal: AbortSignal.timeout(30_000),
|
|
71
|
-
},
|
|
72
|
-
);
|
|
73
|
-
if (!response.ok) {
|
|
74
|
-
if (response.status === 429 || response.status >= 500) {
|
|
75
|
-
const retry = Number(response.headers.get("retry-after"));
|
|
76
|
-
const reset = Number(response.headers.get("x-ratelimit-reset"));
|
|
77
|
-
const delay = Math.max(
|
|
78
|
-
5,
|
|
79
|
-
Number.isFinite(retry) ? retry : 0,
|
|
80
|
-
Number.isFinite(reset) ? reset - Date.now() / 1000 : 0,
|
|
81
|
-
);
|
|
82
|
-
refreshAfter = Date.now() + Math.min(delay, 3600) * 1000;
|
|
83
|
-
throw new ConvexError({
|
|
84
|
-
code: "UPSTREAM_RETRY",
|
|
85
|
-
retryAfter: Math.ceil((refreshAfter - Date.now()) / 1000),
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
throw new Error(`GitHub connection failed (${response.status}).`);
|
|
89
|
-
}
|
|
90
|
-
const { token } = (await response.json()) as { token: string };
|
|
91
|
-
const payload = token.split(".").at(1);
|
|
92
|
-
const expiresAt = payload
|
|
93
|
-
? Number(JSON.parse(Buffer.from(payload, "base64url").toString()).exp) *
|
|
94
|
-
1000
|
|
95
|
-
: 0;
|
|
96
|
-
if (!Number.isFinite(expiresAt) || expiresAt <= Date.now() + 60_000)
|
|
97
|
-
throw new Error("Vercel returned an expired identity.");
|
|
98
|
-
await ctx.runMutation(internal.github.identityCache.finish, {
|
|
99
|
-
key,
|
|
100
|
-
lease,
|
|
101
|
-
token: seal(token, `vercel-identity:${key}`),
|
|
102
|
-
expiresAt,
|
|
103
|
-
refreshAfter: 0,
|
|
104
|
-
});
|
|
105
|
-
return token;
|
|
106
|
-
} catch (error) {
|
|
107
|
-
await ctx.runMutation(internal.github.identityCache.finish, {
|
|
108
|
-
key,
|
|
109
|
-
lease,
|
|
110
|
-
expiresAt: 0,
|
|
111
|
-
refreshAfter,
|
|
112
|
-
});
|
|
113
|
-
throw error;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
export class GitHubError extends Error {
|
|
117
|
-
constructor(readonly status: number) {
|
|
118
|
-
super(`GitHub request failed (${status}).`);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
export async function githubRequest<T>(
|
|
122
|
-
token: string,
|
|
123
|
-
path: string,
|
|
124
|
-
body?: unknown,
|
|
125
|
-
method = body === undefined ? "GET" : "POST",
|
|
126
|
-
): Promise<T> {
|
|
127
|
-
const response = await fetch(`https://api.github.com${path}`, {
|
|
128
|
-
method,
|
|
129
|
-
redirect: "error",
|
|
130
|
-
signal: AbortSignal.timeout(30_000),
|
|
131
|
-
headers: {
|
|
132
|
-
Authorization: `Bearer ${token}`,
|
|
133
|
-
Accept: "application/vnd.github+json",
|
|
134
|
-
"Content-Type": "application/json",
|
|
135
|
-
"X-GitHub-Api-Version": "2022-11-28",
|
|
136
|
-
},
|
|
137
|
-
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
138
|
-
});
|
|
139
|
-
if (!response.ok) throw new GitHubError(response.status);
|
|
140
|
-
return (
|
|
141
|
-
response.status === 204 || response.headers.get("content-length") === "0"
|
|
142
|
-
? undefined
|
|
143
|
-
: await response.json()
|
|
144
|
-
) as T;
|
|
145
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { internalMutation } from "../_generated/server";
|
|
2
|
-
import { v } from "convex/values";
|
|
3
|
-
|
|
4
|
-
// Internal only: encrypted service identity, shared across action workers.
|
|
5
|
-
export const acquire = internalMutation({
|
|
6
|
-
args: { key: v.string(), lease: v.string() },
|
|
7
|
-
returns: v.object({
|
|
8
|
-
token: v.optional(v.string()),
|
|
9
|
-
acquired: v.boolean(),
|
|
10
|
-
retryAfter: v.number(),
|
|
11
|
-
}),
|
|
12
|
-
handler: async (ctx, { key, lease }) => {
|
|
13
|
-
const row = await ctx.db
|
|
14
|
-
.query("serviceIdentities")
|
|
15
|
-
.withIndex("by_key", (q) => q.eq("key", key))
|
|
16
|
-
.unique();
|
|
17
|
-
const now = Date.now();
|
|
18
|
-
if (row?.token && row.expiresAt > now + 60_000)
|
|
19
|
-
return { token: row.token, acquired: false, retryAfter: 0 };
|
|
20
|
-
if (row && row.refreshAfter > now)
|
|
21
|
-
return {
|
|
22
|
-
acquired: false,
|
|
23
|
-
retryAfter: Math.ceil((row.refreshAfter - now) / 1000),
|
|
24
|
-
};
|
|
25
|
-
const value = { lease, refreshAfter: now + 35_000 };
|
|
26
|
-
if (row) await ctx.db.patch("serviceIdentities", row._id, value);
|
|
27
|
-
else
|
|
28
|
-
await ctx.db.insert("serviceIdentities", { key, expiresAt: 0, ...value });
|
|
29
|
-
return { acquired: true, retryAfter: 0 };
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
export const finish = internalMutation({
|
|
34
|
-
args: {
|
|
35
|
-
key: v.string(),
|
|
36
|
-
lease: v.string(),
|
|
37
|
-
token: v.optional(v.string()),
|
|
38
|
-
expiresAt: v.number(),
|
|
39
|
-
refreshAfter: v.number(),
|
|
40
|
-
},
|
|
41
|
-
returns: v.null(),
|
|
42
|
-
handler: async (ctx, { key, lease, ...value }) => {
|
|
43
|
-
const row = await ctx.db
|
|
44
|
-
.query("serviceIdentities")
|
|
45
|
-
.withIndex("by_key", (q) => q.eq("key", key))
|
|
46
|
-
.unique();
|
|
47
|
-
if (row?.lease === lease)
|
|
48
|
-
await ctx.db.patch("serviceIdentities", row._id, {
|
|
49
|
-
...value,
|
|
50
|
-
lease: undefined,
|
|
51
|
-
});
|
|
52
|
-
return null;
|
|
53
|
-
},
|
|
54
|
-
});
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
identityFields,
|
|
3
|
-
internalMutation,
|
|
4
|
-
} from "@disposabl/disposabl-kit/server/lib/functions";
|
|
5
|
-
import { ConvexError, v } from "convex/values";
|
|
6
|
-
|
|
7
|
-
/** A trusted installation callback supplies this verified binding, never a client API. */
|
|
8
|
-
export const connect = internalMutation({
|
|
9
|
-
args: {
|
|
10
|
-
...identityFields,
|
|
11
|
-
owner: v.string(),
|
|
12
|
-
installationId: v.string(),
|
|
13
|
-
},
|
|
14
|
-
returns: v.null(),
|
|
15
|
-
handler: async (ctx, { userId, owner, installationId }) => {
|
|
16
|
-
if (!/^[a-zA-Z0-9-]+$/.test(owner) || !/^\d+$/.test(installationId))
|
|
17
|
-
throw new ConvexError("Invalid GitHub installation.");
|
|
18
|
-
owner = owner.toLowerCase();
|
|
19
|
-
const existing = await ctx.db
|
|
20
|
-
.query("githubConnections")
|
|
21
|
-
.withIndex("by_userId_owner", (q) =>
|
|
22
|
-
q.eq("userId", userId).eq("owner", owner),
|
|
23
|
-
)
|
|
24
|
-
.unique();
|
|
25
|
-
if (existing) await ctx.db.delete("githubConnections", existing._id);
|
|
26
|
-
await ctx.db.insert("githubConnections", {
|
|
27
|
-
userId,
|
|
28
|
-
owner,
|
|
29
|
-
installationId,
|
|
30
|
-
});
|
|
31
|
-
return null;
|
|
32
|
-
},
|
|
33
|
-
});
|