25 lines
1.6 KiB
JavaScript
25 lines
1.6 KiB
JavaScript
// Plan 017 harness: p2p-routes + p2p-hub + the transfer scripts (port 8770).
|
|
// Run from server/: bun ../plans/harness/transfer-server.js
|
|
const { Hono } = await import(process.cwd() + '/node_modules/hono/dist/index.js');
|
|
import { mkdtempSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
const root = mkdtempSync(join(tmpdir(), 'ytp-h14-'));
|
|
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 { createP2pHub } = await import(process.cwd() + '/p2p-hub.js');
|
|
const FE = process.env.FE_DIR || new URL('../../frontend', import.meta.url).pathname;
|
|
await dbmod.initDb(); await p2pDb.initP2pSchema();
|
|
const app = new Hono();
|
|
registerP2pRoutes(app, { cfg: { enabled: true, staleDays: 7 }, p2pDb, fileForCid: async () => null, sha256Range: async () => '' });
|
|
const hub = createP2pHub({ getDevice: p2pDb.getDevice });
|
|
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><script src="/p2p-transfer.js"></script>'));
|
|
app.get('/:f', (c) => new Response(Bun.file(FE + '/' + c.req.param('f')), { headers: { 'Content-Type': 'text/javascript' } }));
|
|
Bun.serve({ port: 8770, fetch(req, server) {
|
|
if (new URL(req.url).pathname === '/ws/p2p') return hub.upgrade(req, server);
|
|
return app.fetch(req, server);
|
|
}, websocket: hub.websocket });
|
|
console.log('ready');
|