@systemfsoftware/agent-storage-cloudflare 0.0.0-bootstrap.0 → 0.1.0-beta.192
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/DoMemoryStore-AaGtt8CZ.d.mts +29 -0
- package/dist/DoMemoryStore-DVun329p.mjs +67 -0
- package/dist/DoMemoryStore-DVun329p.mjs.map +1 -0
- package/dist/DoMemoryStore.d.mts +2 -0
- package/dist/DoMemoryStore.mjs +1 -0
- package/dist/DoMessageDeliveryStore-BvB6OPI1.mjs +34 -0
- package/dist/DoMessageDeliveryStore-BvB6OPI1.mjs.map +1 -0
- package/dist/DoMessageDeliveryStore-Cb-DNN5s.d.mts +16 -0
- package/dist/DoMessageDeliveryStore.d.mts +2 -0
- package/dist/DoMessageDeliveryStore.mjs +1 -0
- package/dist/DoScheduleStore-BiTgDvX1.d.mts +36 -0
- package/dist/DoScheduleStore-C7GkSReF.mjs +167 -0
- package/dist/DoScheduleStore-C7GkSReF.mjs.map +1 -0
- package/dist/DoScheduleStore.d.mts +2 -0
- package/dist/DoScheduleStore.mjs +1 -0
- package/dist/DoStorageConfig-BqezsbD6.mjs +43 -0
- package/dist/DoStorageConfig-BqezsbD6.mjs.map +1 -0
- package/dist/DoStorageConfig-CuB6ZOtX.d.mts +36 -0
- package/dist/DoStorageConfig.d.mts +2 -0
- package/dist/DoStorageConfig.mjs +1 -0
- package/dist/DoStorageError-C-F5Z_FZ.d.mts +82 -0
- package/dist/DoStorageError-DSQzuCb0.mjs +138 -0
- package/dist/DoStorageError-DSQzuCb0.mjs.map +1 -0
- package/dist/DoStorageError.d.mts +2 -0
- package/dist/DoStorageError.mjs +1 -0
- package/dist/DoStorageFailpoint-C7zLUULu.d.mts +19 -0
- package/dist/DoStorageFailpoint-WO3B_DQl.mjs +20 -0
- package/dist/DoStorageFailpoint-WO3B_DQl.mjs.map +1 -0
- package/dist/DoStorageFailpoint.d.mts +2 -0
- package/dist/DoStorageFailpoint.mjs +1 -0
- package/dist/DoStorageFailpointTesting.d.mts +21 -0
- package/dist/DoStorageFailpointTesting.mjs +18 -0
- package/dist/DoStorageFailpointTesting.mjs.map +1 -0
- package/dist/DoStorageVersion-BrolfBDS.mjs +7 -0
- package/dist/DoStorageVersion-BrolfBDS.mjs.map +1 -0
- package/dist/DoStorageVersion-D7CIxFc_.d.mts +8 -0
- package/dist/DoStorageVersion.d.mts +3 -0
- package/dist/DoStorageVersion.mjs +2 -0
- package/dist/DoSubmissionLedger-Oq5OstxL.mjs +1878 -0
- package/dist/DoSubmissionLedger-Oq5OstxL.mjs.map +1 -0
- package/dist/DoSubmissionLedger-tLjwidUn.d.mts +25 -0
- package/dist/DoSubmissionLedger.d.mts +2 -0
- package/dist/DoSubmissionLedger.mjs +1 -0
- package/dist/DoSubscriptionStore-BMJiFxt3.mjs +185 -0
- package/dist/DoSubscriptionStore-BMJiFxt3.mjs.map +1 -0
- package/dist/DoSubscriptionStore-zx6nfbbh.d.mts +25 -0
- package/dist/DoSubscriptionStore.d.mts +2 -0
- package/dist/DoSubscriptionStore.mjs +1 -0
- package/dist/DoThreadStore-2zTSfui-.d.mts +81 -0
- package/dist/DoThreadStore-CeYHcRR9.mjs +411 -0
- package/dist/DoThreadStore-CeYHcRR9.mjs.map +1 -0
- package/dist/DoThreadStore.d.mts +2 -0
- package/dist/DoThreadStore.mjs +5 -0
- package/dist/MemoryProtocol-CtCFYe4s.mjs +221 -0
- package/dist/MemoryProtocol-CtCFYe4s.mjs.map +1 -0
- package/dist/MemoryProtocol-DXV77CcJ.d.mts +38160 -0
- package/dist/MemoryProtocol.d.mts +2 -0
- package/dist/MemoryProtocol.mjs +1 -0
- package/dist/PortProtocol-4xvMyCgP.mjs +322 -0
- package/dist/PortProtocol-4xvMyCgP.mjs.map +1 -0
- package/dist/PortProtocol-DXuguAgD.d.mts +966 -0
- package/dist/PortProtocol.d.mts +2 -0
- package/dist/PortProtocol.mjs +1 -0
- package/dist/PortRouting-B9302ouX.mjs +683 -0
- package/dist/PortRouting-B9302ouX.mjs.map +1 -0
- package/dist/PortRouting-DsoI6soW.d.mts +105 -0
- package/dist/PortRouting.d.mts +2 -0
- package/dist/PortRouting.mjs +1 -0
- package/dist/do-journal-nPNEAjV5.mjs +769 -0
- package/dist/do-journal-nPNEAjV5.mjs.map +1 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.mjs +14 -0
- package/dist/migrations-BRTvj8Ue.mjs +233 -0
- package/dist/migrations-BRTvj8Ue.mjs.map +1 -0
- package/dist/owned-state-DrOD9s0c.mjs +157 -0
- package/dist/owned-state-DrOD9s0c.mjs.map +1 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/package.json +104 -5
- package/src/DoMemoryStore.ts +71 -0
- package/src/DoMessageDeliveryStore.ts +46 -0
- package/src/DoScheduleStore.ts +276 -0
- package/src/DoStorageConfig.ts +45 -0
- package/src/DoStorageError.ts +146 -0
- package/src/DoStorageFailpoint.ts +25 -0
- package/src/DoStorageFailpointTesting.ts +42 -0
- package/src/DoStorageVersion.ts +2 -0
- package/src/DoSubmissionLedger.ts +3980 -0
- package/src/DoSubscriptionStore.ts +331 -0
- package/src/DoThreadStore.ts +602 -0
- package/src/MemoryProtocol.ts +357 -0
- package/src/PortProtocol.ts +626 -0
- package/src/PortRouting.ts +1888 -0
- package/src/index.ts +13 -0
- package/src/internal/canonical-append.ts +15 -0
- package/src/internal/do-journal.ts +1449 -0
- package/src/internal/migrations.ts +395 -0
- package/src/internal/owned-state.ts +300 -0
- package/src/internal/prompt-checkpoint.ts +170 -0
- package/src/internal/storage-span.ts +54 -0
- package/README.md +0 -3
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import * as SqliteClient from "@effect/sql-sqlite-do/SqliteClient";
|
|
2
|
+
import { makeStorageOwner } from "@systemfsoftware/agent/run-storage";
|
|
3
|
+
import * as Deferred from "effect/Deferred";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Exit from "effect/Exit";
|
|
6
|
+
import * as Predicate from "effect/Predicate";
|
|
7
|
+
import * as Schema from "effect/Schema";
|
|
8
|
+
import * as Scope from "effect/Scope";
|
|
9
|
+
import * as Semaphore from "effect/Semaphore";
|
|
10
|
+
import * as SqlClient from "effect/sql/SqlClient";
|
|
11
|
+
import { a as DoStorageError, i as DoStorageCorruptionError } from "./DoStorageError-DSQzuCb0.mjs";
|
|
12
|
+
// src/internal/owned-state.ts
|
|
13
|
+
/** One disposable view per physical Object database, including separately acquired adapters. */
|
|
14
|
+
var OwnedState = class {
|
|
15
|
+
owner;
|
|
16
|
+
constructor(owner) {
|
|
17
|
+
this.owner = owner;
|
|
18
|
+
}
|
|
19
|
+
invalidators = /* @__PURE__ */ new Set();
|
|
20
|
+
invalidate = Effect.sync(() => {
|
|
21
|
+
for (const invalidate of this.invalidators) invalidate();
|
|
22
|
+
});
|
|
23
|
+
/** Cached readers must wait for the complete transaction, including rollback/commit. */
|
|
24
|
+
read = (effect) => this.owner.withMutation(effect);
|
|
25
|
+
transaction = (effect) => this.read(Effect.flatMap(SqlClient.SqlClient, (sql) => sql.withTransaction(effect).pipe(Effect.onExit((exit) => Exit.isFailure(exit) ? this.invalidate : Effect.void))));
|
|
26
|
+
};
|
|
27
|
+
const states = /* @__PURE__ */ new WeakMap();
|
|
28
|
+
/** Explicit invalidation for maintenance that writes private tables outside the adapters. */
|
|
29
|
+
const invalidateOwnedState = (storage) => Effect.suspend(() => {
|
|
30
|
+
const state = states.get(storage);
|
|
31
|
+
return state === void 0 ? Effect.void : Deferred.await(state.ready).pipe(Effect.flatMap((state) => state.read(state.invalidate)));
|
|
32
|
+
});
|
|
33
|
+
const DoClient = Schema.declare((value) => Predicate.hasProperty(value, SqliteClient.TypeId));
|
|
34
|
+
const ownedState = (sql) => Schema.decodeUnknownEffect(DoClient)(sql).pipe(Effect.mapError(() => DoStorageError.make({
|
|
35
|
+
operation: "open owned state",
|
|
36
|
+
message: "Owned state requires a Durable Object SQLite client."
|
|
37
|
+
})), Effect.flatMap((client) => Effect.uninterruptible(Effect.gen(function* () {
|
|
38
|
+
const key = client.config.storage ?? client.config.db ?? client;
|
|
39
|
+
for (;;) {
|
|
40
|
+
let retained = states.get(key);
|
|
41
|
+
if (retained?.closing) {
|
|
42
|
+
yield* Deferred.await(retained.disposed);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (retained === void 0) {
|
|
46
|
+
retained = {
|
|
47
|
+
ready: Deferred.makeUnsafe(),
|
|
48
|
+
disposed: Deferred.makeUnsafe(),
|
|
49
|
+
scope: Scope.makeUnsafe(),
|
|
50
|
+
references: 1,
|
|
51
|
+
closing: false
|
|
52
|
+
};
|
|
53
|
+
states.set(key, retained);
|
|
54
|
+
const owner = yield* makeStorageOwner(Semaphore.makeUnsafe(1).withPermits(1)).pipe(Scope.provide(retained.scope));
|
|
55
|
+
yield* Deferred.succeed(retained.ready, new OwnedState(owner));
|
|
56
|
+
} else retained.references++;
|
|
57
|
+
const current = retained;
|
|
58
|
+
yield* Effect.addFinalizer(() => Effect.uninterruptible(Effect.gen(function* () {
|
|
59
|
+
current.references--;
|
|
60
|
+
if (current.references !== 0) return;
|
|
61
|
+
current.closing = true;
|
|
62
|
+
yield* Scope.close(current.scope, Exit.void).pipe(Effect.ensuring(Effect.gen(function* () {
|
|
63
|
+
if (states.get(key) === current) states.delete(key);
|
|
64
|
+
yield* Deferred.succeed(current.disposed, void 0);
|
|
65
|
+
})));
|
|
66
|
+
})));
|
|
67
|
+
return yield* Deferred.await(current.ready);
|
|
68
|
+
}
|
|
69
|
+
}))));
|
|
70
|
+
/**
|
|
71
|
+
* Demand-loaded row views. Writes apply SQLite's RETURNING rows to affected retained views in
|
|
72
|
+
* the same transaction; rollback discards them. Bounded views avoid loading a host's entire
|
|
73
|
+
* retained submission history just to fence one live submission.
|
|
74
|
+
*/
|
|
75
|
+
const ownedRows = (schema, table, key, identityColumn, uniqueFields = [], projection = (sql) => sql.literal("*")) => {
|
|
76
|
+
const caches = /* @__PURE__ */ new WeakMap();
|
|
77
|
+
const decodeRows = Schema.decodeUnknownEffect(Schema.Array(schema));
|
|
78
|
+
return (state, sql) => {
|
|
79
|
+
let cache = caches.get(state);
|
|
80
|
+
if (cache === void 0) {
|
|
81
|
+
cache = /* @__PURE__ */ new Map();
|
|
82
|
+
caches.set(state, cache);
|
|
83
|
+
const current = cache;
|
|
84
|
+
state.invalidators.add(() => current.clear());
|
|
85
|
+
}
|
|
86
|
+
const current = cache;
|
|
87
|
+
const decode = (rows) => decodeRows(rows).pipe(Effect.mapErrorEager(() => DoStorageCorruptionError.make({
|
|
88
|
+
table,
|
|
89
|
+
rowKey: "owned-state",
|
|
90
|
+
message: "Stored rows do not satisfy the storage schema"
|
|
91
|
+
})));
|
|
92
|
+
const retain = (id, matches, rows, maxRows) => {
|
|
93
|
+
const bytes = new TextEncoder().encode(JSON.stringify(rows)).byteLength;
|
|
94
|
+
current.delete(id);
|
|
95
|
+
if (rows.length > maxRows || bytes > 4194304) return;
|
|
96
|
+
current.set(id, {
|
|
97
|
+
id,
|
|
98
|
+
matches,
|
|
99
|
+
maxRows,
|
|
100
|
+
rows,
|
|
101
|
+
bytes
|
|
102
|
+
});
|
|
103
|
+
let total = 0;
|
|
104
|
+
for (const view of current.values()) total += view.bytes;
|
|
105
|
+
while (current.size > 128 || total > 4194304) {
|
|
106
|
+
const oldest = current.entries().next().value;
|
|
107
|
+
if (oldest === void 0) break;
|
|
108
|
+
total -= oldest[1].bytes;
|
|
109
|
+
current.delete(oldest[0]);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const matching = (id, matches, query, maxRows = 1e400) => state.read(Effect.suspend(() => {
|
|
113
|
+
const view = current.get(id);
|
|
114
|
+
if (view !== void 0) return Effect.succeed(view.rows);
|
|
115
|
+
return Effect.flatMap(query, (rows) => Effect.mapEager(decode(rows), (rows) => {
|
|
116
|
+
retain(id, matches, rows, maxRows);
|
|
117
|
+
return rows;
|
|
118
|
+
}));
|
|
119
|
+
}));
|
|
120
|
+
const by = (field, value) => matching(JSON.stringify([field, value]), (row) => row[field] === value, Effect.suspend(() => sql`SELECT ${projection(sql)} FROM ${sql(table)} WHERE ${sql(field)} = ${value}`));
|
|
121
|
+
const seed = (field, value, rows) => retain(JSON.stringify([field, value]), (row) => row[field] === value, rows, 1e400);
|
|
122
|
+
const byFields = (fields) => matching(JSON.stringify(fields), (row) => fields.every(([field, value]) => row[field] === value), Effect.suspend(() => sql`SELECT ${projection(sql)} FROM ${sql(table)} WHERE ${sql.and(fields.map(([field, value]) => sql`${sql(field)} = ${value}`))}`));
|
|
123
|
+
const apply = (remove) => (effect) => Effect.gen(function* () {
|
|
124
|
+
const rows = yield* decode(yield* effect);
|
|
125
|
+
if (rows.length === 0) return rows;
|
|
126
|
+
const changed = new Set(rows.map(key));
|
|
127
|
+
for (const { id, matches, maxRows, rows: prior } of Array.from(current.values())) {
|
|
128
|
+
const matchingRows = remove ? [] : rows.filter(matches);
|
|
129
|
+
if (matchingRows.length === 0 && !prior.some((row) => changed.has(key(row)))) continue;
|
|
130
|
+
const next = prior.filter((row) => !changed.has(key(row)));
|
|
131
|
+
next.push(...matchingRows);
|
|
132
|
+
retain(id, matches, next, maxRows);
|
|
133
|
+
}
|
|
134
|
+
if (identityColumn !== void 0) for (const row of rows) {
|
|
135
|
+
const value = row[identityColumn];
|
|
136
|
+
if (typeof value === "string") retain(JSON.stringify([identityColumn, value]), (row) => row[identityColumn] === value, remove ? [] : [row], 1e400);
|
|
137
|
+
}
|
|
138
|
+
for (const row of rows) for (const fields of uniqueFields) {
|
|
139
|
+
const values = fields.map((field) => [field, row[field]]);
|
|
140
|
+
retain(JSON.stringify(values), (candidate) => values.every(([field, value]) => candidate[field] === value), remove ? [] : [row], 1);
|
|
141
|
+
}
|
|
142
|
+
return rows;
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
by,
|
|
146
|
+
byFields,
|
|
147
|
+
matching,
|
|
148
|
+
seed,
|
|
149
|
+
seedMatching: retain,
|
|
150
|
+
write: apply(false),
|
|
151
|
+
remove: apply(true)
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
export { ownedRows as n, ownedState as r, invalidateOwnedState as t };
|
|
156
|
+
|
|
157
|
+
//# sourceMappingURL=owned-state-DrOD9s0c.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"owned-state-DrOD9s0c.mjs","names":[],"sources":["../src/internal/owned-state.ts"],"sourcesContent":["import * as SqliteClient from '@effect/sql-sqlite-do/SqliteClient'\nimport { makeStorageOwner, type StorageOwner } from '@systemfsoftware/agent/run-storage'\nimport * as Deferred from 'effect/Deferred'\nimport * as Effect from 'effect/Effect'\nimport * as Exit from 'effect/Exit'\nimport * as Predicate from 'effect/Predicate'\nimport * as Schema from 'effect/Schema'\nimport * as Scope from 'effect/Scope'\nimport * as Semaphore from 'effect/Semaphore'\nimport * as SqlClient from 'effect/sql/SqlClient'\nimport type * as SqlError from 'effect/sql/SqlError'\n\nimport { DoStorageCorruptionError, DoStorageError } from '../DoStorageError'\n\n/** One disposable view per physical Object database, including separately acquired adapters. */\nexport class OwnedState {\n constructor(readonly owner: StorageOwner['Service']) {}\n readonly invalidators = new Set<() => void>()\n\n readonly invalidate = Effect.sync(() => {\n for (const invalidate of this.invalidators) invalidate()\n })\n\n /** Cached readers must wait for the complete transaction, including rollback/commit. */\n readonly read = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> => this.owner.withMutation(effect)\n\n readonly transaction = <A, E, R>(\n effect: Effect.Effect<A, E, R>,\n ): Effect.Effect<A, E | SqlError.SqlError, R | SqlClient.SqlClient> =>\n this.read(\n Effect.flatMap(SqlClient.SqlClient, (sql) =>\n sql\n .withTransaction(effect)\n .pipe(Effect.onExit((exit) => (Exit.isFailure(exit) ? this.invalidate : Effect.void)))),\n )\n}\n\ninterface RetainedState {\n readonly ready: Deferred.Deferred<OwnedState>\n readonly disposed: Deferred.Deferred<void>\n readonly scope: Scope.Closeable\n references: number\n closing: boolean\n}\n\nconst states = new WeakMap<object, RetainedState>()\n\n/** Explicit invalidation for maintenance that writes private tables outside the adapters. */\nexport const invalidateOwnedState = (storage: DurableObjectStorage): Effect.Effect<void> =>\n Effect.suspend(() => {\n const state = states.get(storage)\n\n return state === undefined\n ? Effect.void\n : Deferred.await(state.ready).pipe(Effect.flatMap((state) => state.read(state.invalidate)))\n })\n\nconst DoClient = Schema.declare<SqliteClient.SqliteClient>(\n (value): value is SqliteClient.SqliteClient => Predicate.hasProperty(value, SqliteClient.TypeId),\n)\n\nexport const ownedState = (sql: SqlClient.SqlClient) =>\n Schema.decodeUnknownEffect(DoClient)(sql).pipe(\n Effect.mapError(() =>\n DoStorageError.make({\n operation: 'open owned state',\n message: 'Owned state requires a Durable Object SQLite client.',\n })\n ),\n Effect.flatMap((client) =>\n Effect.uninterruptible(\n Effect.gen(function*() {\n // Instrumentation may return fresh wrappers; the physical Object identity owns the root.\n const key = client.config.storage ?? client.config.db ?? client\n\n for (;;) {\n let retained = states.get(key)\n\n if (retained?.closing) {\n yield* Deferred.await(retained.disposed)\n continue\n }\n if (retained === undefined) {\n retained = {\n ready: Deferred.makeUnsafe(),\n disposed: Deferred.makeUnsafe(),\n scope: Scope.makeUnsafe(),\n references: 1,\n closing: false,\n }\n states.set(key, retained)\n\n const owner = yield* makeStorageOwner(Semaphore.makeUnsafe(1).withPermits(1)).pipe(\n Scope.provide(retained.scope),\n )\n\n yield* Deferred.succeed(retained.ready, new OwnedState(owner))\n } else retained.references++\n const current = retained\n\n yield* Effect.addFinalizer(() =>\n Effect.uninterruptible(\n Effect.gen(function*() {\n current.references--\n if (current.references !== 0) return\n current.closing = true\n yield* Scope.close(current.scope, Exit.void).pipe(\n Effect.ensuring(\n Effect.gen(function*() {\n if (states.get(key) === current) states.delete(key)\n yield* Deferred.succeed(current.disposed, undefined)\n }),\n ),\n )\n }),\n )\n )\n\n return yield* Deferred.await(current.ready)\n }\n }),\n )\n ),\n )\n\n/**\n * Demand-loaded row views. Writes apply SQLite's RETURNING rows to affected retained views in\n * the same transaction; rollback discards them. Bounded views avoid loading a host's entire\n * retained submission history just to fence one live submission.\n */\nexport const ownedRows = <A, I>(\n schema: Schema.Codec<A, I>,\n table: string,\n key: (row: A) => string,\n identityColumn?: keyof A & string,\n uniqueFields: ReadonlyArray<ReadonlyArray<keyof A & string>> = [],\n projection: (sql: SqlClient.SqlClient) => ReturnType<SqlClient.SqlClient['literal']> = (sql) => sql.literal('*'),\n) => {\n type View = {\n readonly id: string\n readonly matches: (row: A) => boolean\n readonly maxRows: number\n readonly rows: ReadonlyArray<A>\n readonly bytes: number\n }\n const caches = new WeakMap<OwnedState, Map<string, View>>()\n const decodeRows = Schema.decodeUnknownEffect(Schema.Array(schema))\n\n return (state: OwnedState, sql: SqlClient.SqlClient) => {\n let cache = caches.get(state)\n\n if (cache === undefined) {\n cache = new Map()\n caches.set(state, cache)\n const current = cache\n\n state.invalidators.add(() => current.clear())\n }\n const current = cache\n\n const decode = (rows: unknown) =>\n decodeRows(rows).pipe(\n Effect.mapErrorEager(() =>\n DoStorageCorruptionError.make({\n table,\n rowKey: 'owned-state',\n message: 'Stored rows do not satisfy the storage schema',\n })\n ),\n )\n\n const retain = (\n id: string,\n matches: View['matches'],\n rows: ReadonlyArray<A>,\n maxRows: number,\n ) => {\n const bytes = new TextEncoder().encode(JSON.stringify(rows)).byteLength\n\n current.delete(id)\n if (rows.length > maxRows || bytes > 4 * 1024 * 1024) return\n current.set(id, { id, matches, maxRows, rows, bytes })\n let total = 0\n\n for (const view of current.values()) total += view.bytes\n while (current.size > 128 || total > 4 * 1024 * 1024) {\n const oldest = current.entries().next().value\n\n if (oldest === undefined) break\n total -= oldest[1].bytes\n current.delete(oldest[0])\n }\n }\n\n const matching = (\n id: string,\n matches: View['matches'],\n query: Effect.Effect<ReadonlyArray<unknown>, SqlError.SqlError>,\n maxRows = Infinity,\n ) =>\n state.read(\n Effect.suspend(() => {\n const view = current.get(id)\n\n if (view !== undefined) return Effect.succeed(view.rows)\n\n return Effect.flatMap(query, (rows) =>\n Effect.mapEager(decode(rows), (rows) => {\n retain(id, matches, rows, maxRows)\n\n return rows\n }))\n }),\n )\n\n const by = (field: keyof A & string, value: string) =>\n matching(\n JSON.stringify([field, value]),\n (row) => row[field] === value,\n Effect.suspend(\n () => sql`SELECT ${projection(sql)} FROM ${sql(table)} WHERE ${sql(field)} = ${value}`,\n ),\n )\n\n // Only a source mutation proving the complete result may seed a view. This must run\n // under the owner transaction; rollback invalidates it with every other retained view.\n const seed = (field: keyof A & string, value: string, rows: ReadonlyArray<A>) =>\n retain(JSON.stringify([field, value]), (row) => row[field] === value, rows, Infinity)\n\n const byFields = (fields: ReadonlyArray<readonly [keyof A & string, string]>) =>\n matching(\n JSON.stringify(fields),\n (row) => fields.every(([field, value]) => row[field] === value),\n Effect.suspend(\n () =>\n sql`SELECT ${projection(sql)} FROM ${sql(table)} WHERE ${\n sql.and(fields.map(([field, value]) => sql`${sql(field)} = ${value}`))\n }`,\n ),\n )\n\n const apply = (remove: boolean) => <E, R>(effect: Effect.Effect<ReadonlyArray<unknown>, E, R>) =>\n Effect.gen(function*() {\n const rows = yield* decode(yield* effect)\n\n if (rows.length === 0) return rows\n const changed = new Set(rows.map(key))\n\n for (const { id, matches, maxRows, rows: prior } of Array.from(current.values())) {\n const matchingRows = remove ? [] : rows.filter(matches)\n\n if (matchingRows.length === 0 && !prior.some((row) => changed.has(key(row)))) continue\n const next = prior.filter((row) => !changed.has(key(row)))\n\n next.push(...matchingRows)\n retain(id, matches, next, maxRows)\n }\n // A RETURNING row completely defines its unique-key view, even on a new Thread.\n if (identityColumn !== undefined) {\n for (const row of rows) {\n const value = row[identityColumn]\n\n if (typeof value === 'string') {\n retain(\n JSON.stringify([identityColumn, value]),\n (row) => row[identityColumn] === value,\n remove ? [] : [row],\n Infinity,\n )\n }\n }\n }\n\n for (const row of rows) {\n for (const fields of uniqueFields) {\n const values = fields.map((field) => [field, row[field]] as const)\n\n retain(\n JSON.stringify(values),\n (candidate) => values.every(([field, value]) => candidate[field] === value),\n remove ? [] : [row],\n 1,\n )\n }\n }\n\n return rows\n })\n\n return {\n by,\n byFields,\n matching,\n seed,\n seedMatching: retain,\n write: apply(false),\n remove: apply(true),\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAeA,IAAa,aAAb,MAAwB;CACD;CAArB,YAAqB,OAAgC;EAAhC,KAAA,QAAA;CAAiC;CAC7C,+BAAe,IAAI,IAAgB;CAEnC,aAAa,OAAO,WAAW;EACtC,KAAK,MAAM,cAAc,KAAK,cAAc,WAAW;CACzD,CAAC;;CAGQ,QAAiB,WAA2D,KAAK,MAAM,aAAa,MAAM;CAE1G,eACP,WAEA,KAAK,KACH,OAAO,QAAQ,UAAU,YAAY,QACnC,IACG,gBAAgB,MAAM,CAAC,CACvB,KAAK,OAAO,QAAQ,SAAU,KAAK,UAAU,IAAI,IAAI,KAAK,aAAa,OAAO,IAAK,CAAC,CAAC,CAC5F;AACJ;AAUA,MAAM,yBAAS,IAAI,QAA+B;;AAGlD,MAAa,wBAAwB,YACnC,OAAO,cAAc;CACnB,MAAM,QAAQ,OAAO,IAAI,OAAO;CAEhC,OAAO,UAAU,KAAA,IACb,OAAO,OACP,SAAS,MAAM,MAAM,KAAK,CAAC,CAAC,KAAK,OAAO,SAAS,UAAU,MAAM,KAAK,MAAM,UAAU,CAAC,CAAC;AAC9F,CAAC;AAEH,MAAM,WAAW,OAAO,SACrB,UAA8C,UAAU,YAAY,OAAO,aAAa,MAAM,CACjG;AAEA,MAAa,cAAc,QACzB,OAAO,oBAAoB,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,KACxC,OAAO,eACL,eAAe,KAAK;CAClB,WAAW;CACX,SAAS;AACX,CAAC,CACH,GACA,OAAO,SAAS,WACd,OAAO,gBACL,OAAO,IAAI,aAAY;CAErB,MAAM,MAAM,OAAO,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K,MAAM,OAAO,MAAM;IACtB,MAAM,QAAQ,IAAI;IAElB,IAAI,OAAO,UAAU,UACnB,OACE,KAAK,UAAU,CAAC,gBAAgB,KAAK,CAAC,IACrC,QAAQ,IAAI,oBAAoB,OACjC,SAAS,CAAC,IAAI,CAAC,GAAG,GAClB,KACF;GAEJ;GAGF,KAAK,MAAM,OAAO,MAChB,KAAK,MAAM,UAAU,cAAc;IACjC,MAAM,SAAS,OAAO,KAAK,UAAU,CAAC,OAAO,IAAI,MAAM,CAAU;IAEjE,OACE,KAAK,UAAU,MAAM,IACpB,cAAc,OAAO,OAAO,CAAC,OAAO,WAAW,UAAU,WAAW,KAAK,GAC1E,SAAS,CAAC,IAAI,CAAC,GAAG,GAClB,CACF;GACF;GAGF,OAAO;EACT,CAAC;EAEH,OAAO;GACL;GACA;GACA;GACA;GACA,cAAc;GACd,OAAO,MAAM,KAAK;GAClB,QAAQ,MAAM,IAAI;EACpB;CACF;AACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, flags) => {
|
|
4
|
+
let target = Object.setPrototypeOf({}, null);
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!(flags & 1)) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return flags & 2 ? target : Object.preventExtensions(target);
|
|
11
|
+
};
|
|
12
|
+
export { __exportAll as t };
|
package/package.json
CHANGED
|
@@ -1,10 +1,109 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@systemfsoftware/agent-storage-cloudflare",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0-beta.192",
|
|
4
|
+
"description": "Durable Object SQLite storage adapters and the routed port protocol for Effect Agent on Cloudflare.",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
7
|
-
"url": "git+https://github.com/systemfsoftware/agent.git"
|
|
8
|
+
"url": "git+https://github.com/systemfsoftware/agent.git",
|
|
9
|
+
"directory": "packages/storage-cloudflare"
|
|
8
10
|
},
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"src"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": [],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.mts",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./do-memory-store": {
|
|
23
|
+
"types": "./dist/DoMemoryStore.d.mts",
|
|
24
|
+
"default": "./dist/DoMemoryStore.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./do-schedule-store": {
|
|
27
|
+
"types": "./dist/DoScheduleStore.d.mts",
|
|
28
|
+
"default": "./dist/DoScheduleStore.mjs"
|
|
29
|
+
},
|
|
30
|
+
"./do-storage-config": {
|
|
31
|
+
"types": "./dist/DoStorageConfig.d.mts",
|
|
32
|
+
"default": "./dist/DoStorageConfig.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./do-storage-error": {
|
|
35
|
+
"types": "./dist/DoStorageError.d.mts",
|
|
36
|
+
"default": "./dist/DoStorageError.mjs"
|
|
37
|
+
},
|
|
38
|
+
"./do-storage-failpoint": {
|
|
39
|
+
"types": "./dist/DoStorageFailpoint.d.mts",
|
|
40
|
+
"default": "./dist/DoStorageFailpoint.mjs"
|
|
41
|
+
},
|
|
42
|
+
"./do-storage-version": {
|
|
43
|
+
"types": "./dist/DoStorageVersion.d.mts",
|
|
44
|
+
"default": "./dist/DoStorageVersion.mjs"
|
|
45
|
+
},
|
|
46
|
+
"./do-submission-ledger": {
|
|
47
|
+
"types": "./dist/DoSubmissionLedger.d.mts",
|
|
48
|
+
"default": "./dist/DoSubmissionLedger.mjs"
|
|
49
|
+
},
|
|
50
|
+
"./do-subscription-store": {
|
|
51
|
+
"types": "./dist/DoSubscriptionStore.d.mts",
|
|
52
|
+
"default": "./dist/DoSubscriptionStore.mjs"
|
|
53
|
+
},
|
|
54
|
+
"./do-thread-store": {
|
|
55
|
+
"types": "./dist/DoThreadStore.d.mts",
|
|
56
|
+
"default": "./dist/DoThreadStore.mjs"
|
|
57
|
+
},
|
|
58
|
+
"./memory-protocol": {
|
|
59
|
+
"types": "./dist/MemoryProtocol.d.mts",
|
|
60
|
+
"default": "./dist/MemoryProtocol.mjs"
|
|
61
|
+
},
|
|
62
|
+
"./port-protocol": {
|
|
63
|
+
"types": "./dist/PortProtocol.d.mts",
|
|
64
|
+
"default": "./dist/PortProtocol.mjs"
|
|
65
|
+
},
|
|
66
|
+
"./port-routing": {
|
|
67
|
+
"types": "./dist/PortRouting.d.mts",
|
|
68
|
+
"default": "./dist/PortRouting.mjs"
|
|
69
|
+
},
|
|
70
|
+
"./testing/do-storage-failpoint-testing": {
|
|
71
|
+
"types": "./dist/DoStorageFailpointTesting.d.mts",
|
|
72
|
+
"default": "./dist/DoStorageFailpointTesting.mjs"
|
|
73
|
+
},
|
|
74
|
+
"./do-message-delivery-store": {
|
|
75
|
+
"types": "./dist/DoMessageDeliveryStore.d.mts",
|
|
76
|
+
"default": "./dist/DoMessageDeliveryStore.mjs"
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"publishConfig": {
|
|
80
|
+
"access": "public",
|
|
81
|
+
"provenance": true
|
|
82
|
+
},
|
|
83
|
+
"dependencies": {
|
|
84
|
+
"@effect/platform-browser": "4.1.2",
|
|
85
|
+
"@effect/sql-sqlite-do": "4.1.2",
|
|
86
|
+
"@systemfsoftware/agent": "0.1.0-beta.192",
|
|
87
|
+
"@systemfsoftware/agent-storage-sql": "0.1.0-beta.192"
|
|
88
|
+
},
|
|
89
|
+
"devDependencies": {
|
|
90
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
91
|
+
"@cloudflare/vitest-plugin": "1.4.0",
|
|
92
|
+
"@cloudflare/workers-types": "5.20261026.1",
|
|
93
|
+
"@effect/vitest": "4.1.2",
|
|
94
|
+
"@systemfsoftware/agent-testing": "0.1.0-beta.192",
|
|
95
|
+
"effect": "4.1.2",
|
|
96
|
+
"tsdown": "^0.23.0",
|
|
97
|
+
"typescript": "7.0.2",
|
|
98
|
+
"vitest": "5.0.3"
|
|
99
|
+
},
|
|
100
|
+
"peerDependencies": {
|
|
101
|
+
"effect": "^4.1.0"
|
|
102
|
+
},
|
|
103
|
+
"scripts": {
|
|
104
|
+
"build": "tsdown",
|
|
105
|
+
"typecheck": "tsc -b",
|
|
106
|
+
"test": "vitest run --passWithNoTests",
|
|
107
|
+
"check:pack": "pnpm pack --pack-destination .pack && attw .pack/*.tgz --profile esm-only; s=$?; rm -rf .pack; exit $s"
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as SqliteClient from '@effect/sql-sqlite-do/SqliteClient'
|
|
2
|
+
import { MemoryMutationFailpoint, MemoryStorageError } from '@systemfsoftware/agent/memory-store'
|
|
3
|
+
import {
|
|
4
|
+
memoryStoreLayerWithFailpoints,
|
|
5
|
+
SqlMemoryLimits,
|
|
6
|
+
SqlStorageOwner,
|
|
7
|
+
} from '@systemfsoftware/agent/sql-memory-store'
|
|
8
|
+
import * as Effect from 'effect/Effect'
|
|
9
|
+
import * as Layer from 'effect/Layer'
|
|
10
|
+
import * as Schema from 'effect/Schema'
|
|
11
|
+
import * as SqlClientService from 'effect/sql/SqlClient'
|
|
12
|
+
|
|
13
|
+
import { ownedState } from './internal/owned-state'
|
|
14
|
+
|
|
15
|
+
export class DoMemoryStorageLimits extends Schema.Class<DoMemoryStorageLimits>(
|
|
16
|
+
'@effect-agent/storage-cloudflare/DoMemoryStorageLimits',
|
|
17
|
+
)({
|
|
18
|
+
maxRowBytes: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 1_900_000 })),
|
|
19
|
+
maxStorageBytes: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 536_870_912 })),
|
|
20
|
+
maxDocuments: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 100_000 })),
|
|
21
|
+
maxReceipts: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 1_000_000 })),
|
|
22
|
+
reservedWithdrawalBytes: Schema.optional(Schema.Natural),
|
|
23
|
+
reservedWithdrawalReceipts: Schema.optional(Schema.Natural),
|
|
24
|
+
}) {}
|
|
25
|
+
|
|
26
|
+
export const defaultDoMemoryStorageLimits = DoMemoryStorageLimits.make({
|
|
27
|
+
maxRowBytes: 1_900_000,
|
|
28
|
+
maxStorageBytes: 536_870_912,
|
|
29
|
+
maxDocuments: 10_000,
|
|
30
|
+
maxReceipts: 100_000,
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Local memory only, without Thread tables. Pass the full storage handle: sql-only
|
|
35
|
+
* handles cannot provide atomic receipts and revisions. Services over the same storage
|
|
36
|
+
* share a transaction gate and bounded, write-through document and receipt views.
|
|
37
|
+
* Byte limits conservatively count encoded rows, not SQLite page/index overhead.
|
|
38
|
+
* Optional withdrawal reserves default to zero and stay within hard byte/receipt limits.
|
|
39
|
+
* Ordinary Put cannot consume them. Deploy exclusively upgraded writers before relying
|
|
40
|
+
* on reserves; the SQL accounting migration preserves older writers and their receipts.
|
|
41
|
+
*/
|
|
42
|
+
export const doMemoryStoreLayerWithFailpoints = (
|
|
43
|
+
storage: NonNullable<SqliteClient.SqliteClientConfig['storage']>,
|
|
44
|
+
limits: DoMemoryStorageLimits = defaultDoMemoryStorageLimits,
|
|
45
|
+
) =>
|
|
46
|
+
Layer.unwrap(
|
|
47
|
+
Effect.gen(function*() {
|
|
48
|
+
const validated = yield* Schema.decodeEffect(DoMemoryStorageLimits)(limits).pipe(
|
|
49
|
+
Effect.mapError(() => MemoryStorageError.make({ operation: 'memory storage limits', reason: 'invalid-input' })),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
const sql = yield* SqlClientService.SqlClient
|
|
53
|
+
|
|
54
|
+
const owner = yield* ownedState(sql).pipe(
|
|
55
|
+
Effect.mapError(() => MemoryStorageError.make({ operation: 'open memory owner', reason: 'unavailable' })),
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
return memoryStoreLayerWithFailpoints.pipe(
|
|
59
|
+
Layer.provide(Layer.succeed(SqlMemoryLimits, validated)),
|
|
60
|
+
Layer.provide(Layer.succeed(SqlStorageOwner, owner)),
|
|
61
|
+
)
|
|
62
|
+
}),
|
|
63
|
+
).pipe(Layer.provide(SqliteClient.layer({ storage })))
|
|
64
|
+
|
|
65
|
+
export const doMemoryStoreLayer = (
|
|
66
|
+
storage: NonNullable<SqliteClient.SqliteClientConfig['storage']>,
|
|
67
|
+
limits: DoMemoryStorageLimits = defaultDoMemoryStorageLimits,
|
|
68
|
+
) =>
|
|
69
|
+
doMemoryStoreLayerWithFailpoints(storage, limits).pipe(
|
|
70
|
+
Layer.provide(MemoryMutationFailpoint.layer),
|
|
71
|
+
)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as BrowserCrypto from '@effect/platform-browser/BrowserCrypto'
|
|
2
|
+
import { SqlLifecycleSource } from '@systemfsoftware/agent-storage-sql/sql-lifecycle-publication'
|
|
3
|
+
import {
|
|
4
|
+
makeSqlMessageDeliveryStore,
|
|
5
|
+
registerCanonicalMessageWriter,
|
|
6
|
+
} from '@systemfsoftware/agent-storage-sql/sql-message-delivery-store'
|
|
7
|
+
import { MessageDeliveryStore, type MessageDeliveryStoreLimits } from '@systemfsoftware/agent/message-delivery'
|
|
8
|
+
import { SqlStorageOwner } from '@systemfsoftware/agent/sql-storage-owner'
|
|
9
|
+
import * as Effect from 'effect/Effect'
|
|
10
|
+
import * as Layer from 'effect/Layer'
|
|
11
|
+
import * as SqlClient from 'effect/sql/SqlClient'
|
|
12
|
+
|
|
13
|
+
import { DoStorageConfig } from './DoStorageConfig'
|
|
14
|
+
import { DoStorageFailpoint } from './DoStorageFailpoint'
|
|
15
|
+
import { initializeDoJournal } from './internal/do-journal'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Message obligations paired with the Thread Object's physical storage owner. Canonical
|
|
19
|
+
* publication uses this configured writer inside the source transaction. The platform must
|
|
20
|
+
* retain delivery maintenance with that transaction and while nextDeadline is present.
|
|
21
|
+
*/
|
|
22
|
+
export const doMessageDeliveryStoreLayer = (limits?: MessageDeliveryStoreLimits) =>
|
|
23
|
+
Layer.effect(
|
|
24
|
+
MessageDeliveryStore,
|
|
25
|
+
Effect.gen(function*() {
|
|
26
|
+
const sql = yield* SqlClient.SqlClient
|
|
27
|
+
const config = yield* DoStorageConfig
|
|
28
|
+
const failpoint = yield* DoStorageFailpoint
|
|
29
|
+
|
|
30
|
+
const journal = yield* initializeDoJournal(sql, failpoint.hit, config.maxStoredValueBytes)
|
|
31
|
+
|
|
32
|
+
const store = yield* makeSqlMessageDeliveryStore(journal.state.owner, limits, {
|
|
33
|
+
maxStoredValueBytes: config.maxStoredValueBytes,
|
|
34
|
+
}).pipe(
|
|
35
|
+
Effect.provideService(SqlStorageOwner, journal.owner),
|
|
36
|
+
Effect.provideService(SqlLifecycleSource, {
|
|
37
|
+
beforeRetain: (threadId) => journal.flushCanonical(threadId),
|
|
38
|
+
beforePending: journal.flushPublications(),
|
|
39
|
+
}),
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
yield* registerCanonicalMessageWriter(journal.state.owner, store)
|
|
43
|
+
|
|
44
|
+
return store
|
|
45
|
+
}),
|
|
46
|
+
).pipe(Layer.provide(BrowserCrypto.layer))
|