Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

1.7 KB
Raw Blame
import { Readable, Writable } from 'node:stream';
import { finished } from 'node:stream/promises';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { handler, type Service } from '../server.ts';
import type { WorkspaceStore } from '../workspace.ts';

class Capture extends Writable {
  statusCode = 200; headersSent = false;
  headers: Record<string, string> = {}; chunks: Buffer[] = [];
  setHeader(name: string, value: unknown) { this.headers[name] = String(value); }
  writeHead(status: number, headers: Record<string, unknown>) { this.statusCode = status; for (const [key, value] of Object.entries(headers)) this.setHeader(key, value); this.headersSent = true; return this; }
  _write(chunk: Buffer, _: string, done: () => void) { this.chunks.push(Buffer.from(chunk)); done(); }
}
// Runs the real HTTP request handler with streams, without binding a socket.
export function transport(store: WorkspaceStore, context?: Service, handle = handler): (url: string, init?: RequestInit) => Promise<Response> {
  return async (url, init = {}) => {
    const headers = Object.fromEntries(new Headers(init.headers));
    headers.host = 'localhost:4173'; headers.origin = 'http://localhost:4173';
    const input = typeof init.body === 'string' ? Buffer.from(init.body) : init.body instanceof Blob ? Buffer.from(await init.body.arrayBuffer()) : Buffer.alloc(0);
    const req = Object.assign(Readable.from([input]), { url, method: init.method || 'GET', headers });
    const res = new Capture(); const complete = finished(res);
    await handle(req as unknown as IncomingMessage, res as unknown as ServerResponse, store, context);
    await complete;
    return new Response(Buffer.concat(res.chunks), { status: res.statusCode, headers: res.headers });
  };
}