28 lines
2.0 KiB
JavaScript
28 lines
2.0 KiB
JavaScript
// Plan 013 harness: real p2p-routes + temp DB + the frontend files.
|
|
// Run from server/: bun ../plans/harness/p2p-client-server.js
|
|
const { Hono } = await import(process.cwd() + '/node_modules/hono/dist/index.js');
|
|
import { mkdtempSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { createHash } from 'node:crypto';
|
|
const root = mkdtempSync(join(tmpdir(), 'ytp-h13-'));
|
|
process.env.DB_PATH = join(root, 'h.db');
|
|
const dbmod = await import(process.cwd() + '/db.js');
|
|
const p2pDb = await import(process.cwd() + '/p2p-db.js');
|
|
const { registerP2pRoutes } = await import(process.cwd() + '/p2p-routes.js');
|
|
const { sha256Range } = await import(process.cwd() + '/hash.js');
|
|
const FE = process.env.FE_DIR || new URL('../../frontend', import.meta.url).pathname;
|
|
await dbmod.initDb(); await p2pDb.initP2pSchema();
|
|
const body = new Uint8Array(3 * 1024 * 1024 + 123).map((_, i) => (i * 7) % 256);
|
|
const sha = createHash('sha256').update(body).digest('hex');
|
|
const file = join(root, 'good.mp4'); writeFileSync(file, body);
|
|
await p2pDb.upsertContent({ cid: sha, videoId: 'goodAAAAAAA', size: body.length, origin: 'server', now: Date.now() });
|
|
const app = new Hono();
|
|
registerP2pRoutes(app, { cfg: { enabled: true, staleDays: 7 }, p2pDb, fileForCid: async (c) => (c === sha ? { path: file, size: body.length } : null), sha256Range });
|
|
app.get('/api/download/:id', () => new Response(body, { headers: { 'Content-Type': 'video/mp4', 'Content-Length': String(body.length), 'X-Content-SHA256': sha } }));
|
|
app.get('/debug/holders', async (c) => c.json(await p2pDb.listHolders(sha)));
|
|
app.get('/', (c) => c.html('<!doctype html><script src="/opfs.js"></script><script src="/sha256.js"></script><script src="/device-db.js"></script><script src="/p2p-client.js"></script>'));
|
|
app.get('/:f', (c) => new Response(Bun.file(FE + '/' + c.req.param('f')), { headers: { 'Content-Type': 'text/javascript' } }));
|
|
Bun.serve({ port: 8767, fetch: app.fetch });
|
|
console.log('ready');
|