Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Push.ts

61 lines2.1 KB

1import type { RuntimeContext } from "alchemy/RuntimeContext";
2/** Transport-independent input and scoped, opaque push transactions. */
3import * as Effect from "effect/Effect";
4import type * as Fiber from "effect/Fiber";
5import type * as Result from "effect/Result";
6import type { ObjectType } from "./Protocol/ObjectCodec.ts";
7import { StoreError } from "./Protocol/Store.ts";
8import type { CommitPushResult, RefCommand, RepoMetaData } from "./RepoObject.ts";
9import type { StreamingFeeder } from "./Store/StreamingSource.ts";
10​
11/** A proposed ref change. Zero old/new OIDs mean creation/deletion. */
12export type RefUpdate = RefCommand;
13​
14/** Decoded commands and a streaming pack. Use the HTTP decoder or Push.fromStream to construct inputs. */
15export interface PushInput {
16 readonly updates: ReadonlyArray<RefUpdate>;
17 readonly atomic: boolean;
19​
20/** @internal The decoder and engine share this state; applications see PushInput. */
21export interface IncomingState {
22 readonly feeder: StreamingFeeder;
23 readonly receiving: Fiber.Fiber<Result.Result<{ readonly total: number }, StoreError>>;
24 readonly packStart: number;
25 readonly declaredBytes: number | undefined;
26 active: boolean;
27 claimed: boolean;
29​
30/** @internal */
31export const incomingStates = new WeakMap<PushInput, IncomingState>();
32​
33/** A staged push, usable only within the scope that prepared it. */
34export interface PreparedPush {
35 readonly repo: RepoMetaData;
36 readonly updates: ReadonlyArray<RefUpdate>;
37 /** Read an incoming or existing object, bounded to 1 MiB by default. */
38 readonly readObject: (
39 oid: string,
40 maxBytes?: number,
41 ) => Effect.Effect<
42 { readonly type: ObjectType; readonly content: Uint8Array } | undefined,
43 StoreError,
44 RuntimeContext
45 >;
47​
48/** @internal */
49export const preparedStates = new WeakMap<
50 PreparedPush,
51 {
52 readonly owner: object;
53 readonly commit: Effect.Effect<CommitPushResult, StoreError, RuntimeContext>;
54 }
55>();
56​
57/** A transaction/input was already consumed, escaped its scope, or belongs to another engine. */
58export const invalidPush = () =>
59 StoreError.make({
60 reason: "push is no longer active or belongs to another engine",
61 });