|
1 | 1 | import type { IncomingMessage, ServerResponse } from 'node:http' |
2 | 2 | import type { Plugin } from 'vite' |
3 | | -import { handleSyncRequest, memorySyncKv } from '../src/lib/sync-rooms' |
| 3 | +import { createRestoreCode, getRestoreSessionId } from '../src/lib/restore-codes' |
| 4 | +import { jsonResponse, handleSyncRequest, memorySyncKv, SyncRoomError } from '../src/lib/sync-rooms' |
4 | 5 |
|
5 | 6 | const kv = memorySyncKv() |
| 7 | +const LOCAL_TEST_SESSION = /^cs_test_[a-zA-Z0-9]+$/ |
6 | 8 |
|
7 | 9 | async function nodeToRequest(req: IncomingMessage): Promise<Request> { |
8 | 10 | const host = req.headers.host ?? '127.0.0.1' |
@@ -34,18 +36,63 @@ async function writeResponse(res: ServerResponse, response: Response): Promise<v |
34 | 36 | res.end(bytes) |
35 | 37 | } |
36 | 38 |
|
37 | | -/** In-memory /api/sync/* so Vite and Playwright can exercise send-to-device. */ |
| 39 | +/** Local-only: accept cs_test_* sessions so Playwright can pair devices without Stripe. */ |
| 40 | +async function handleLocalPurchase(request: Request): Promise<Response> { |
| 41 | + const url = new URL(request.url) |
| 42 | + try { |
| 43 | + if (url.pathname === '/api/restore-codes') { |
| 44 | + if (request.method !== 'POST') return jsonResponse({ error: 'Method not allowed.' }, 405) |
| 45 | + const posted = (await request.json().catch(() => null)) as { session_id?: unknown } | null |
| 46 | + const sessionId = typeof posted?.session_id === 'string' ? posted.session_id : '' |
| 47 | + if (!LOCAL_TEST_SESSION.test(sessionId)) { |
| 48 | + return jsonResponse({ error: 'Local restore codes only accept cs_test_ sessions.' }, 400) |
| 49 | + } |
| 50 | + const code = await createRestoreCode(kv, sessionId, 'local') |
| 51 | + return jsonResponse({ code }, 201) |
| 52 | + } |
| 53 | + |
| 54 | + if (url.pathname === '/api/verify-purchase') { |
| 55 | + if (request.method !== 'GET') return jsonResponse({ unlocked: false, error: 'Method not allowed.' }, 405) |
| 56 | + let sessionId = url.searchParams.get('session_id') ?? '' |
| 57 | + const code = url.searchParams.get('code') ?? '' |
| 58 | + if (code) sessionId = await getRestoreSessionId(kv, code) |
| 59 | + if (!LOCAL_TEST_SESSION.test(sessionId)) { |
| 60 | + return jsonResponse({ unlocked: false, error: 'No such purchase.' }, 404) |
| 61 | + } |
| 62 | + return jsonResponse({ unlocked: true, sessionId }, 200) |
| 63 | + } |
| 64 | + } catch (error) { |
| 65 | + if (error instanceof SyncRoomError) { |
| 66 | + const payload = |
| 67 | + url.pathname === '/api/verify-purchase' |
| 68 | + ? { unlocked: false, error: error.message } |
| 69 | + : { error: error.message } |
| 70 | + return jsonResponse(payload, error.status) |
| 71 | + } |
| 72 | + throw error |
| 73 | + } |
| 74 | + return jsonResponse({ error: 'Not found.' }, 404) |
| 75 | +} |
| 76 | + |
| 77 | +/** In-memory /api/sync/* and Plus restore-code endpoints for Vite and Playwright. */ |
38 | 78 | export function syncApiPlugin(): Plugin { |
39 | 79 | return { |
40 | 80 | name: 'kody-sync-api', |
41 | 81 | configureServer(server) { |
42 | 82 | server.middlewares.use((req, res, next) => { |
43 | | - if (!req.url?.startsWith('/api/sync/')) { |
| 83 | + const path = req.url?.split('?')[0] ?? '' |
| 84 | + const purchase = |
| 85 | + path === '/api/restore-codes' || path === '/api/verify-purchase' |
| 86 | + if (!path.startsWith('/api/sync/') && !purchase) { |
44 | 87 | next() |
45 | 88 | return |
46 | 89 | } |
47 | 90 | void nodeToRequest(req) |
48 | | - .then((request) => handleSyncRequest(request, { SYNC_ROOMS: kv })) |
| 91 | + .then((request) => |
| 92 | + purchase |
| 93 | + ? handleLocalPurchase(request) |
| 94 | + : handleSyncRequest(request, { SYNC_ROOMS: kv }), |
| 95 | + ) |
49 | 96 | .then((response) => writeResponse(res, response)) |
50 | 97 | .catch((error: unknown) => { |
51 | 98 | res.statusCode = 500 |
|
0 commit comments