The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { ObjectType } from "./Protocol/ObjectCodec.ts"; |
| 2 | /** |
| 3 | * The Worker ↔ Repo DO push protocol (DESIGN §22.10): the pack never |
| 4 | * enters the Durable Object. The Worker receives the body, spills it, |
| 5 | * verifies it through the hasher fan-out, and ships the DO only *rows* — |
| 6 | * staged-object batches (promoted rows are coordinates into the wire |
| 7 | * pack, inline rows carry their zlib bytes) — plus the commit summary. |
| 8 | * |
| 9 | * Batches cross the RPC boundary as one binary buffer: |
| 10 | * `u32 jsonLen | json | zdata…` where the JSON array lists each row's |
| 11 | * metadata and the byte length of its zdata run, in order. |
| 12 | */ |
| 13 | import type { StagedObject } from "./Store/ObjectStore.ts"; |
| 14 | |
| 15 | interface RowMeta { |
| 16 | /** oid */ |
| 17 | readonly o: string; |
| 18 | /** type */ |
| 19 | readonly t: ObjectType; |
| 20 | /** size (inflated) */ |
| 21 | readonly s: number; |
| 22 | /** zdata length in the blob section */ |
| 23 | readonly n: number; |
| 24 | /** zsize (promoted rows: the span in the pack) */ |
| 25 | readonly z?: number | undefined; |
| 26 | /** pack coordinates (promoted rows) */ |
| 27 | readonly p?: { readonly i: string; readonly f: number } | undefined; |
| 28 | } |
| 29 | |
| 30 | /** Encodes staged rows for one `stagePush` call. */ |
| 31 | export const encodeStagedBatch = (objects: ReadonlyArray<StagedObject>): Uint8Array => { |
| 32 | const meta: Array<RowMeta> = []; |
| 33 | let blobBytes = 0; |
| 34 | for (const object of objects) { |
| 35 | meta.push({ |
| 36 | o: object.oid, |
| 37 | t: object.type, |
| 38 | s: object.size, |
| 39 | n: object.zdata.byteLength, |
| 40 | z: object.zsize, |
| 41 | p: object.pack === undefined ? undefined : { i: object.pack.packId, f: object.pack.offset }, |
| 42 | }); |
| 43 | blobBytes += object.zdata.byteLength; |
| 44 | } |
| 45 | const json = new TextEncoder().encode(JSON.stringify(meta)); |
| 46 | const out = new Uint8Array(4 + json.length + blobBytes); |
| 47 | new DataView(out.buffer).setUint32(0, json.length); |
| 48 | out.set(json, 4); |
| 49 | let at = 4 + json.length; |
| 50 | for (const object of objects) { |
| 51 | out.set(object.zdata, at); |
| 52 | at += object.zdata.byteLength; |
| 53 | } |
| 54 | return out; |
| 55 | }; |
| 56 | |
| 57 | /** Decodes a `stagePush` batch back into staged rows (views into `bytes`). */ |
| 58 | export const decodeStagedBatch = (bytes: Uint8Array): ReadonlyArray<StagedObject> => { |
| 59 | const jsonLen = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0); |
| 60 | const meta = JSON.parse( |
| 61 | new TextDecoder().decode(bytes.subarray(4, 4 + jsonLen)), |
| 62 | ) as ReadonlyArray<RowMeta>; |
| 63 | const out: Array<StagedObject> = []; |
| 64 | let at = 4 + jsonLen; |
| 65 | for (const row of meta) { |
| 66 | const zdata = bytes.subarray(at, at + row.n); |
| 67 | at += row.n; |
| 68 | out.push({ |
| 69 | oid: row.o, |
| 70 | type: row.t, |
| 71 | size: row.s, |
| 72 | zdata, |
| 73 | zsize: row.z, |
| 74 | pack: row.p === undefined ? undefined : { packId: row.p.i, offset: row.p.f }, |
| 75 | }); |
| 76 | } |
| 77 | return out; |
| 78 | }; |