Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

setup.ts

76 lines2.9 KB

1import { execFile } from 'node:child_process'
2import { mkdtemp, rm } from 'node:fs/promises'
3import { tmpdir } from 'node:os'
4import { join } from 'node:path'
5import { fileURLToPath, pathToFileURL } from 'node:url'
6import { promisify } from 'node:util'
7import { startLocalBackend } from '../local-backend'
8import { withTestCleanup } from '../../tests/helpers'
9import { prepareAuthApp } from './prepare'
10import { authEnvironment } from './configure'
11import type { TestProject } from 'vitest/node'
12​
13const run = promisify(execFile)
14const upstream = `https://github.com/get-convex/convex-auth-with-role-based-permissions.git`
15const revision = `45f08008e1257758560948db4280cc4e7d64e84a`
16​
17/** The provider issues genuine JWTs, no client is given deployment admin auth. */
18export default async function setup(project: TestProject) {
19 const checkout = await mkdtemp(join(tmpdir(), `convex-auth-upstream-`))
20 let stop: (() => Promise<void>) | undefined
21 const runtime = fileURLToPath(new URL(`./runtime/`, import.meta.url))
22 try {
23 await run(`git`, [`init`, checkout])
24 await run(`git`, [`-C`, checkout, `remote`, `add`, `origin`, upstream])
25 await run(`git`, [`-C`, checkout, `fetch`, `--depth=1`, `origin`, revision])
26 await run(`git`, [`-C`, checkout, `checkout`, `--detach`, `FETCH_HEAD`])
27 const { stdout } = await run(`git`, [`-C`, checkout, `rev-parse`, `HEAD`])
28 if (stdout.trim() !== revision)
29 throw new Error(`Wrong upstream auth revision`)
30 await prepareAuthApp(checkout)
31 const backend = await startLocalBackend(pathToFileURL(`${runtime}/`))
32 stop = backend.stop
33 if (!backend.url.startsWith(`http://127.0.0.1:`))
34 throw new Error(`Authentication setup requires an isolated local backend`)
35 const {
36 CONVEX_DEPLOY_KEY: _key,
37 CONVEX_SELF_HOSTED_URL: _url,
38 CONVEX_SELF_HOSTED_ADMIN_KEY: _admin,
39 CONVEX_DEPLOYMENT: _deployment,
40 ...environment
41 } = process.env
42 const cli = fileURLToPath(
43 new URL(`./node_modules/convex/bin/main.js`, import.meta.url),
44 )
45 for (const [name, value] of Object.entries(await authEnvironment())) {
46 try {
47 await run(process.execPath, [cli, `env`, `set`, name, `--`, value], {
48 cwd: runtime,
49 env: environment,
50 })
51 } catch {
52 // execFile errors contain argv, which contains the ephemeral private key.
53 throw new Error(
54 `Failed to configure local authentication variable ${name}`,
55 )
56 }
57 }
58 project.provide(`authAppUrl`, backend.url)
59 } catch (cause) {
60 await withTestCleanup(
61 () => {
62 throw cause
63 },
64 () => stop?.(),
65 () => rm(checkout, { recursive: true, force: true }),
66 () => rm(runtime, { recursive: true, force: true }),
67 )
68 }
69 return async () =>
70 withTestCleanup(
71 () => {},
72 () => stop?.(),
73 () => rm(checkout, { recursive: true, force: true }),
74 () => rm(runtime, { recursive: true, force: true }),
75 )