|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +import { startWorker } from "jazz-tools/worker" |
| 4 | +import { ServerAccount } from "../src/shared/schema/server" |
| 5 | + |
| 6 | +type CliArgs = { |
| 7 | + syncServer: string |
| 8 | + accountId: string |
| 9 | + accountSecret: string |
| 10 | + timeoutMs: number |
| 11 | +} |
| 12 | + |
| 13 | +async function main() { |
| 14 | + if (process.argv.includes("--help") || process.argv.includes("-h")) { |
| 15 | + printUsage() |
| 16 | + process.exit(0) |
| 17 | + } |
| 18 | + |
| 19 | + let args = parseArgs(process.argv.slice(2)) |
| 20 | + if (!args) { |
| 21 | + printUsage() |
| 22 | + process.exit(1) |
| 23 | + } |
| 24 | + |
| 25 | + console.log("Checking Jazz worker credentials...") |
| 26 | + console.log(`- sync server: ${args.syncServer}`) |
| 27 | + console.log(`- expected account: ${args.accountId}`) |
| 28 | + |
| 29 | + let timeout = new Promise<never>((_, reject) => { |
| 30 | + setTimeout( |
| 31 | + () => reject(new Error(`Timed out after ${args.timeoutMs}ms`)), |
| 32 | + args.timeoutMs, |
| 33 | + ) |
| 34 | + }) |
| 35 | + |
| 36 | + let workerPromise = startWorker({ |
| 37 | + AccountSchema: ServerAccount, |
| 38 | + syncServer: args.syncServer, |
| 39 | + accountID: args.accountId, |
| 40 | + accountSecret: args.accountSecret, |
| 41 | + skipInboxLoad: true, |
| 42 | + asActiveAccount: false, |
| 43 | + }) |
| 44 | + |
| 45 | + let workerResult = await Promise.race([workerPromise, timeout]) |
| 46 | + let actualAccountId = workerResult.worker.$jazz.id |
| 47 | + |
| 48 | + console.log(`- loaded account: ${actualAccountId}`) |
| 49 | + |
| 50 | + if (actualAccountId !== args.accountId) { |
| 51 | + console.error("\nFAIL: secret authenticated a different account") |
| 52 | + console.error(`expected: ${args.accountId}`) |
| 53 | + console.error(`actual: ${actualAccountId}`) |
| 54 | + await workerResult.shutdownWorker?.() |
| 55 | + process.exit(2) |
| 56 | + } |
| 57 | + |
| 58 | + await workerResult.shutdownWorker?.() |
| 59 | + console.log("\nOK: worker account + secret match") |
| 60 | +} |
| 61 | + |
| 62 | +function parseArgs(argv: string[]): CliArgs | null { |
| 63 | + let syncServer = |
| 64 | + readFlag(argv, "--sync-server") || process.env.PUBLIC_JAZZ_SYNC_SERVER |
| 65 | + let accountId = |
| 66 | + readFlag(argv, "--account-id") || process.env.PUBLIC_JAZZ_WORKER_ACCOUNT |
| 67 | + let accountSecret = |
| 68 | + readFlag(argv, "--account-secret") || process.env.JAZZ_WORKER_SECRET |
| 69 | + let timeoutRaw = readFlag(argv, "--timeout-ms") |
| 70 | + let timeoutMs = timeoutRaw ? Number(timeoutRaw) : 30_000 |
| 71 | + |
| 72 | + if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) { |
| 73 | + console.error("Invalid --timeout-ms value") |
| 74 | + return null |
| 75 | + } |
| 76 | + |
| 77 | + if (!syncServer || !accountId || !accountSecret) { |
| 78 | + console.error("Missing required inputs") |
| 79 | + return null |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + syncServer, |
| 84 | + accountId, |
| 85 | + accountSecret, |
| 86 | + timeoutMs, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function readFlag(argv: string[], key: string): string | undefined { |
| 91 | + let index = argv.indexOf(key) |
| 92 | + if (index === -1) return undefined |
| 93 | + let value = argv[index + 1] |
| 94 | + if (!value || value.startsWith("--")) return undefined |
| 95 | + return value |
| 96 | +} |
| 97 | + |
| 98 | +function printUsage() { |
| 99 | + console.log(` |
| 100 | +Usage: |
| 101 | + tsx scripts/check-worker-credentials.ts \\ |
| 102 | + --sync-server <ws://...> \\ |
| 103 | + --account-id <co_...> \\ |
| 104 | + --account-secret <sealerSecret_.../signerSecret_...> |
| 105 | +
|
| 106 | +Or use env vars: |
| 107 | + PUBLIC_JAZZ_SYNC_SERVER |
| 108 | + PUBLIC_JAZZ_WORKER_ACCOUNT |
| 109 | + JAZZ_WORKER_SECRET |
| 110 | +
|
| 111 | +Optional: |
| 112 | + --timeout-ms <number> (default: 30000) |
| 113 | +`) |
| 114 | +} |
| 115 | + |
| 116 | +main().catch(error => { |
| 117 | + let message = error instanceof Error ? error.message : String(error) |
| 118 | + console.error("\nFAIL:", message) |
| 119 | + process.exit(1) |
| 120 | +}) |
0 commit comments