@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 62 lines 2.1 KB
Raw
1 import * as k2 from "@distilled.cloud/cloudflare/k2";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import { toProduceRecords } from "./K2Codec.ts";
5 import { type K2Auth, makeK2HttpAuth } from "./K2Http.ts";
6 import type { EncodedRecord, RecordSchema } from "./K2Types.ts";
7 import type { Stream } from "./Stream.ts";
8 import { WriteStream } from "./WriteStream.ts";
9 import { makeWriteStreamClient } from "./WriteStreamClient.ts";
10
11 /**
12 * HTTP-backed implementation of the {@link WriteStream} service. Mints a
13 * scoped `K2 Produce` {@link AccountApiToken}, binds it into the host, and
14 * appends records through the stream's HTTP input
15 * (`POST https://<id>.k2.cloudflarestorage.com/produce`).
16 *
17 * The stream must have its HTTP input enabled (`http: true`). Works on any
18 * host — Workers, containers, Lambda.
19 * ### Providing the Layer
20 * **Example:** Produce over HTTP
21 * ```typescript
22 * const Orders = Cloudflare.K2.Stream("Orders", { http: true });
23 *
24 * Effect.gen(function* () {
25 * const orders = yield* Cloudflare.K2.WriteStream(Orders);
26 * // ...
27 * }).pipe(Effect.provide(Cloudflare.K2.WriteStreamHttp));
28 * ```
29 *
30 * @layer
31 * @provides Cloudflare.K2.WriteStream
32 * @product K2
33 * @category Storage & Databases
34 */
35 export const WriteStreamHttp = Layer.effect(
36 WriteStream,
37 Effect.gen(function* () {
38 const auth = yield* makeK2HttpAuth;
39 return Effect.fn(function* (stream: Stream<any>) {
40 const streamAuth = yield* auth(stream, "K2 Produce");
41 return makeWriteStreamHttpClient(streamAuth, yield* stream.streamId, stream.RecordSchema);
42 });
43 }),
44 );
45
46 /**
47 * Build the producer client over the K2 `produce` HTTP API. Shared by
48 * {@link WriteStreamHttp} and `WriteStreamLocal`.
49 */
50 export const makeWriteStreamHttpClient = (
51 auth: K2Auth,
52 streamId: Effect.Effect<string>,
53 schema: RecordSchema<any> | undefined,
54 ) =>
55 makeWriteStreamClient(schema, (records: ReadonlyArray<EncodedRecord>) =>
56 streamId.pipe(
57 Effect.flatMap((id) =>
58 auth.authorize(k2.produce({ streamId: id, records: toProduceRecords(records) })),
59 ),
60 Effect.asVoid,
61 ),
62 );