@livestore/sync-cf 0.4.0-dev.8 → 0.4.0
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/README.md +7 -8
- package/dist/.tsbuildinfo +1 -1
- package/dist/cf-worker/do/durable-object.d.ts +1 -1
- package/dist/cf-worker/do/durable-object.d.ts.map +1 -1
- package/dist/cf-worker/do/durable-object.js +15 -14
- package/dist/cf-worker/do/durable-object.js.map +1 -1
- package/dist/cf-worker/do/layer.d.ts +6 -6
- package/dist/cf-worker/do/layer.d.ts.map +1 -1
- package/dist/cf-worker/do/layer.js +32 -9
- package/dist/cf-worker/do/layer.js.map +1 -1
- package/dist/cf-worker/do/pull.d.ts +8 -3
- package/dist/cf-worker/do/pull.d.ts.map +1 -1
- package/dist/cf-worker/do/pull.js +22 -10
- package/dist/cf-worker/do/pull.js.map +1 -1
- package/dist/cf-worker/do/push.d.ts +5 -4
- package/dist/cf-worker/do/push.d.ts.map +1 -1
- package/dist/cf-worker/do/push.js +80 -41
- package/dist/cf-worker/do/push.js.map +1 -1
- package/dist/cf-worker/do/sqlite.d.ts +10 -1
- package/dist/cf-worker/do/sqlite.d.ts.map +1 -1
- package/dist/cf-worker/do/sqlite.js +13 -4
- package/dist/cf-worker/do/sqlite.js.map +1 -1
- package/dist/cf-worker/do/sync-storage.d.ts +14 -9
- package/dist/cf-worker/do/sync-storage.d.ts.map +1 -1
- package/dist/cf-worker/do/sync-storage.js +92 -18
- package/dist/cf-worker/do/sync-storage.js.map +1 -1
- package/dist/cf-worker/do/transport/do-rpc-server.d.ts +2 -1
- package/dist/cf-worker/do/transport/do-rpc-server.d.ts.map +1 -1
- package/dist/cf-worker/do/transport/do-rpc-server.js +13 -7
- package/dist/cf-worker/do/transport/do-rpc-server.js.map +1 -1
- package/dist/cf-worker/do/transport/http-rpc-server.d.ts +3 -1
- package/dist/cf-worker/do/transport/http-rpc-server.d.ts.map +1 -1
- package/dist/cf-worker/do/transport/http-rpc-server.js +24 -15
- package/dist/cf-worker/do/transport/http-rpc-server.js.map +1 -1
- package/dist/cf-worker/do/transport/ws-rpc-server.d.ts +2 -1
- package/dist/cf-worker/do/transport/ws-rpc-server.d.ts.map +1 -1
- package/dist/cf-worker/do/transport/ws-rpc-server.js +30 -8
- package/dist/cf-worker/do/transport/ws-rpc-server.js.map +1 -1
- package/dist/cf-worker/shared.d.ts +123 -30
- package/dist/cf-worker/shared.d.ts.map +1 -1
- package/dist/cf-worker/shared.js +50 -6
- package/dist/cf-worker/shared.js.map +1 -1
- package/dist/cf-worker/worker.d.ts +64 -71
- package/dist/cf-worker/worker.d.ts.map +1 -1
- package/dist/cf-worker/worker.js +70 -48
- package/dist/cf-worker/worker.js.map +1 -1
- package/dist/client/transport/do-rpc-client.d.ts.map +1 -1
- package/dist/client/transport/do-rpc-client.js +27 -10
- package/dist/client/transport/do-rpc-client.js.map +1 -1
- package/dist/client/transport/http-rpc-client.d.ts.map +1 -1
- package/dist/client/transport/http-rpc-client.js +29 -9
- package/dist/client/transport/http-rpc-client.js.map +1 -1
- package/dist/client/transport/ws-rpc-client.d.ts +2 -1
- package/dist/client/transport/ws-rpc-client.d.ts.map +1 -1
- package/dist/client/transport/ws-rpc-client.js +31 -17
- package/dist/client/transport/ws-rpc-client.js.map +1 -1
- package/dist/common/constants.d.ts +7 -0
- package/dist/common/constants.d.ts.map +1 -0
- package/dist/common/constants.js +17 -0
- package/dist/common/constants.js.map +1 -0
- package/dist/common/do-rpc-schema.d.ts +6 -6
- package/dist/common/do-rpc-schema.d.ts.map +1 -1
- package/dist/common/do-rpc-schema.js +4 -4
- package/dist/common/do-rpc-schema.js.map +1 -1
- package/dist/common/http-rpc-schema.d.ts +4 -4
- package/dist/common/http-rpc-schema.d.ts.map +1 -1
- package/dist/common/http-rpc-schema.js +4 -4
- package/dist/common/http-rpc-schema.js.map +1 -1
- package/dist/common/mod.d.ts +4 -1
- package/dist/common/mod.d.ts.map +1 -1
- package/dist/common/mod.js +4 -1
- package/dist/common/mod.js.map +1 -1
- package/dist/common/sync-message-types.d.ts +2 -2
- package/dist/common/sync-message-types.js +3 -3
- package/dist/common/sync-message-types.js.map +1 -1
- package/dist/common/ws-rpc-schema.d.ts +3 -3
- package/dist/common/ws-rpc-schema.d.ts.map +1 -1
- package/dist/common/ws-rpc-schema.js +3 -3
- package/dist/common/ws-rpc-schema.js.map +1 -1
- package/package.json +72 -14
- package/src/cf-worker/do/durable-object.ts +23 -18
- package/src/cf-worker/do/layer.ts +35 -13
- package/src/cf-worker/do/pull.ts +43 -14
- package/src/cf-worker/do/push.ts +107 -46
- package/src/cf-worker/do/sqlite.ts +14 -4
- package/src/cf-worker/do/sync-storage.ts +151 -31
- package/src/cf-worker/do/transport/do-rpc-server.ts +22 -9
- package/src/cf-worker/do/transport/http-rpc-server.ts +33 -13
- package/src/cf-worker/do/transport/ws-rpc-server.ts +40 -12
- package/src/cf-worker/shared.ts +149 -25
- package/src/cf-worker/worker.ts +138 -108
- package/src/client/transport/do-rpc-client.ts +41 -17
- package/src/client/transport/http-rpc-client.ts +43 -17
- package/src/client/transport/ws-rpc-client.ts +42 -19
- package/src/common/constants.ts +18 -0
- package/src/common/do-rpc-schema.ts +5 -4
- package/src/common/http-rpc-schema.ts +5 -4
- package/src/common/mod.ts +4 -2
- package/src/common/sync-message-types.ts +3 -3
- package/src/common/ws-rpc-schema.ts +4 -3
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { State } from '@livestore/common/schema';
|
|
2
2
|
import { Schema } from '@livestore/utils/effect';
|
|
3
|
+
/**
|
|
4
|
+
* Main event log table storing all LiveStore events.
|
|
5
|
+
*
|
|
6
|
+
* ⚠️ IMPORTANT: Any changes to this schema require bumping PERSISTENCE_FORMAT_VERSION in shared.ts
|
|
7
|
+
*/
|
|
3
8
|
export declare const eventlogTable: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"eventlog_7_$storeId", {
|
|
4
9
|
readonly seqNum: {
|
|
5
10
|
columnType: "integer";
|
|
@@ -133,7 +138,11 @@ export declare const eventlogTable: State.SQLite.TableDef<State.SQLite.SqliteTab
|
|
|
133
138
|
readonly clientId: string;
|
|
134
139
|
readonly sessionId: string;
|
|
135
140
|
}, never>>;
|
|
136
|
-
/**
|
|
141
|
+
/**
|
|
142
|
+
* Context metadata table - one row per Durable Object.
|
|
143
|
+
*
|
|
144
|
+
* ⚠️ IMPORTANT: Any changes to this schema require bumping PERSISTENCE_FORMAT_VERSION in shared.ts
|
|
145
|
+
*/
|
|
137
146
|
export declare const contextTable: State.SQLite.TableDef<State.SQLite.SqliteTableDefForInput<"context_7", {
|
|
138
147
|
readonly storeId: {
|
|
139
148
|
columnType: "text";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../../../src/cf-worker/do/sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,EAAE,MAAM,0BAA0B,CAAA;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;
|
|
1
|
+
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../../../src/cf-worker/do/sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,EAAE,MAAM,0BAA0B,CAAA;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAIhD;;;;GAIG;AACH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAQtB,mEAAmE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAAnE,mEAAmE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAKrE,CAAA;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAOvB,CAAA"}
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
import { EventSequenceNumber, State } from '@livestore/common/schema';
|
|
2
2
|
import { Schema } from '@livestore/utils/effect';
|
|
3
3
|
import { PERSISTENCE_FORMAT_VERSION } from "../shared.js";
|
|
4
|
+
/**
|
|
5
|
+
* Main event log table storing all LiveStore events.
|
|
6
|
+
*
|
|
7
|
+
* ⚠️ IMPORTANT: Any changes to this schema require bumping PERSISTENCE_FORMAT_VERSION in shared.ts
|
|
8
|
+
*/
|
|
4
9
|
export const eventlogTable = State.SQLite.table({
|
|
5
10
|
// NOTE actual table name is determined at runtime to use proper storeId
|
|
6
11
|
name: `eventlog_${PERSISTENCE_FORMAT_VERSION}_$storeId`,
|
|
7
12
|
columns: {
|
|
8
|
-
seqNum: State.SQLite.integer({ primaryKey: true, schema: EventSequenceNumber.
|
|
9
|
-
parentSeqNum: State.SQLite.integer({ schema: EventSequenceNumber.
|
|
13
|
+
seqNum: State.SQLite.integer({ primaryKey: true, schema: EventSequenceNumber.Global.Schema }),
|
|
14
|
+
parentSeqNum: State.SQLite.integer({ schema: EventSequenceNumber.Global.Schema }),
|
|
10
15
|
name: State.SQLite.text({}),
|
|
11
16
|
args: State.SQLite.text({ schema: Schema.parseJson(Schema.Any), nullable: true }),
|
|
12
17
|
/** ISO date format. Currently only used for debugging purposes. */
|
|
@@ -15,12 +20,16 @@ export const eventlogTable = State.SQLite.table({
|
|
|
15
20
|
sessionId: State.SQLite.text({}),
|
|
16
21
|
},
|
|
17
22
|
});
|
|
18
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Context metadata table - one row per Durable Object.
|
|
25
|
+
*
|
|
26
|
+
* ⚠️ IMPORTANT: Any changes to this schema require bumping PERSISTENCE_FORMAT_VERSION in shared.ts
|
|
27
|
+
*/
|
|
19
28
|
export const contextTable = State.SQLite.table({
|
|
20
29
|
name: `context_${PERSISTENCE_FORMAT_VERSION}`,
|
|
21
30
|
columns: {
|
|
22
31
|
storeId: State.SQLite.text({ primaryKey: true }),
|
|
23
|
-
currentHead: State.SQLite.integer({ schema: EventSequenceNumber.
|
|
32
|
+
currentHead: State.SQLite.integer({ schema: EventSequenceNumber.Global.Schema }),
|
|
24
33
|
backendId: State.SQLite.text({}),
|
|
25
34
|
},
|
|
26
35
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../../../src/cf-worker/do/sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAA;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;
|
|
1
|
+
{"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../../../src/cf-worker/do/sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAA;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAEhD,OAAO,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAA;AAEzD;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;IAC9C,wEAAwE;IACxE,IAAI,EAAE,YAAY,0BAA0B,WAAW;IACvD,OAAO,EAAE;QACP,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,mBAAmB,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QAC7F,YAAY,EAAE,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,mBAAmB,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QACjF,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3B,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;QACjF,mEAAmE;QACnE,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAChC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/B,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;KACjC;CACF,CAAC,CAAA;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;IAC7C,IAAI,EAAE,WAAW,0BAA0B,EAAE;IAC7C,OAAO,EAAE;QACP,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;QAChD,WAAW,EAAE,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,mBAAmB,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QAChF,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;KACjC;CACF,CAAC,CAAA"}
|
|
@@ -1,20 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { LiveStoreEvent } from '@livestore/common/schema';
|
|
1
|
+
import { UnknownError } from '@livestore/common';
|
|
3
2
|
import type { CfTypes } from '@livestore/common-cf';
|
|
3
|
+
import type { LiveStoreEvent } from '@livestore/common/schema';
|
|
4
4
|
import { Effect, Option, Stream } from '@livestore/utils/effect';
|
|
5
5
|
import { SyncMetadata } from '../../common/sync-message-types.ts';
|
|
6
|
-
import { type
|
|
6
|
+
import { type StoreId } from '../shared.ts';
|
|
7
7
|
export type SyncStorage = {
|
|
8
8
|
dbName: string;
|
|
9
9
|
getEvents: (cursor: number | undefined) => Effect.Effect<{
|
|
10
10
|
total: number;
|
|
11
11
|
stream: Stream.Stream<{
|
|
12
|
-
eventEncoded: LiveStoreEvent.
|
|
12
|
+
eventEncoded: LiveStoreEvent.Global.Encoded;
|
|
13
13
|
metadata: Option.Option<SyncMetadata>;
|
|
14
|
-
},
|
|
15
|
-
},
|
|
16
|
-
appendEvents: (batch: ReadonlyArray<LiveStoreEvent.
|
|
17
|
-
resetStore: Effect.Effect<void,
|
|
14
|
+
}, UnknownError>;
|
|
15
|
+
}, UnknownError>;
|
|
16
|
+
appendEvents: (batch: ReadonlyArray<LiveStoreEvent.Global.Encoded>, createdAt: string) => Effect.Effect<void, UnknownError>;
|
|
17
|
+
resetStore: Effect.Effect<void, UnknownError>;
|
|
18
18
|
};
|
|
19
|
-
export declare const makeStorage: (ctx: CfTypes.DurableObjectState,
|
|
19
|
+
export declare const makeStorage: (ctx: CfTypes.DurableObjectState, storeId: StoreId, engine: {
|
|
20
|
+
_tag: "d1";
|
|
21
|
+
db: CfTypes.D1Database;
|
|
22
|
+
} | {
|
|
23
|
+
_tag: "do-sqlite";
|
|
24
|
+
}) => SyncStorage;
|
|
20
25
|
//# sourceMappingURL=sync-storage.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync-storage.d.ts","sourceRoot":"","sources":["../../../src/cf-worker/do/sync-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"sync-storage.d.ts","sourceRoot":"","sources":["../../../src/cf-worker/do/sync-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAC9D,OAAO,EAAS,MAAM,EAAE,MAAM,EAAU,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAE/E,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAA;AACjE,OAAO,EAA8B,KAAK,OAAO,EAAE,MAAM,cAAc,CAAA;AAGvE,MAAM,MAAM,WAAW,GAAG;IACxB,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,KAAK,MAAM,CAAC,MAAM,CACtD;QACE,KAAK,EAAE,MAAM,CAAA;QACb,MAAM,EAAE,MAAM,CAAC,MAAM,CACnB;YAAE,YAAY,EAAE,cAAc,CAAC,MAAM,CAAC,OAAO,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;SAAE,EACtF,YAAY,CACb,CAAA;KACF,EACD,YAAY,CACb,CAAA;IACD,YAAY,EAAE,CACZ,KAAK,EAAE,aAAa,CAAC,cAAc,CAAC,MAAM,CAAC,OAAO,CAAC,EACnD,SAAS,EAAE,MAAM,KACd,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAA;IACtC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,YAAY,CAAC,CAAA;CAC9C,CAAA;AAED,eAAO,MAAM,WAAW,GACtB,KAAK,OAAO,CAAC,kBAAkB,EAC/B,SAAS,OAAO,EAChB,QAAQ;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,OAAO,CAAC,UAAU,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,KACrE,WAkSF,CAAA"}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnknownError } from '@livestore/common';
|
|
2
2
|
import { Chunk, Effect, Option, Schema, Stream } from '@livestore/utils/effect';
|
|
3
3
|
import { SyncMetadata } from "../../common/sync-message-types.js";
|
|
4
4
|
import { PERSISTENCE_FORMAT_VERSION } from "../shared.js";
|
|
5
5
|
import { eventlogTable } from "./sqlite.js";
|
|
6
|
-
export const makeStorage = (ctx,
|
|
6
|
+
export const makeStorage = (ctx, storeId, engine) => {
|
|
7
7
|
const dbName = `eventlog_${PERSISTENCE_FORMAT_VERSION}_${toValidTableName(storeId)}`;
|
|
8
8
|
const execDb = (cb) => Effect.tryPromise({
|
|
9
|
-
try: () => cb(
|
|
10
|
-
catch: (error) => new
|
|
9
|
+
try: () => cb(engine._tag === 'd1' ? engine.db : undefined),
|
|
10
|
+
catch: (error) => new UnknownError({ cause: error, payload: { dbName } }),
|
|
11
11
|
}).pipe(Effect.map((_) => _.results), Effect.withSpan('@livestore/sync-cf:durable-object:execDb'));
|
|
12
12
|
// Cloudflare's D1 HTTP endpoint rejects JSON responses once they exceed ~1MB.
|
|
13
13
|
// Keep individual SELECT batches comfortably below that threshold so we can
|
|
@@ -18,6 +18,7 @@ export const makeStorage = (ctx, env, storeId) => {
|
|
|
18
18
|
const D1_INITIAL_PAGE_SIZE = 256;
|
|
19
19
|
const D1_MIN_PAGE_SIZE = 1;
|
|
20
20
|
const decodeEventlogRows = Schema.decodeUnknownSync(Schema.Array(eventlogTable.rowSchema));
|
|
21
|
+
const jsonStringify = Schema.encodeSync(Schema.parseJson());
|
|
21
22
|
const textEncoder = new TextEncoder();
|
|
22
23
|
const decreaseLimit = (limit) => Math.max(D1_MIN_PAGE_SIZE, Math.floor(limit / 2));
|
|
23
24
|
const increaseLimit = (limit) => Math.min(D1_INITIAL_PAGE_SIZE, limit * 2);
|
|
@@ -32,7 +33,7 @@ export const makeStorage = (ctx, env, storeId) => {
|
|
|
32
33
|
}
|
|
33
34
|
return limit;
|
|
34
35
|
};
|
|
35
|
-
const
|
|
36
|
+
const getEventsD1 = (cursor) => Effect.gen(function* () {
|
|
36
37
|
const countStatement = cursor === undefined
|
|
37
38
|
? `SELECT COUNT(*) as total FROM ${dbName}`
|
|
38
39
|
: `SELECT COUNT(*) as total FROM ${dbName} WHERE seqNum > ?`;
|
|
@@ -55,7 +56,7 @@ export const makeStorage = (ctx, env, storeId) => {
|
|
|
55
56
|
if (rawEvents.length === 0) {
|
|
56
57
|
return Option.none();
|
|
57
58
|
}
|
|
58
|
-
const encodedSize = textEncoder.encode(
|
|
59
|
+
const encodedSize = textEncoder.encode(jsonStringify(rawEvents)).byteLength;
|
|
59
60
|
if (encodedSize > D1_TARGET_RESPONSE_BYTES && state.limit > D1_MIN_PAGE_SIZE) {
|
|
60
61
|
const nextLimit = decreaseLimit(state.limit);
|
|
61
62
|
if (nextLimit !== state.limit) {
|
|
@@ -73,8 +74,10 @@ export const makeStorage = (ctx, env, storeId) => {
|
|
|
73
74
|
});
|
|
74
75
|
const stream = Stream.unfoldChunkEffect(initialState, fetchPage);
|
|
75
76
|
return { total, stream };
|
|
76
|
-
}).pipe(
|
|
77
|
-
|
|
77
|
+
}).pipe(UnknownError.mapToUnknownError, Effect.withSpan('@livestore/sync-cf:durable-object:getEvents', {
|
|
78
|
+
attributes: { dbName, cursor, engine: engine._tag },
|
|
79
|
+
}));
|
|
80
|
+
const appendEventsD1 = (batch, createdAt) => Effect.gen(function* () {
|
|
78
81
|
// If there are no events, do nothing.
|
|
79
82
|
if (batch.length === 0)
|
|
80
83
|
return;
|
|
@@ -102,17 +105,88 @@ export const makeStorage = (ctx, env, storeId) => {
|
|
|
102
105
|
.bind(...params)
|
|
103
106
|
.run());
|
|
104
107
|
}
|
|
105
|
-
}).pipe(
|
|
106
|
-
attributes: { dbName, batchLength: batch.length },
|
|
108
|
+
}).pipe(UnknownError.mapToUnknownError, Effect.withSpan('@livestore/sync-cf:durable-object:appendEvents', {
|
|
109
|
+
attributes: { dbName, batchLength: batch.length, engine: engine._tag },
|
|
107
110
|
}));
|
|
108
|
-
const resetStore = Effect.promise(() => ctx.storage.deleteAll()).pipe(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
111
|
+
const resetStore = Effect.promise(() => ctx.storage.deleteAll()).pipe(UnknownError.mapToUnknownError, Effect.withSpan('@livestore/sync-cf:durable-object:resetStore'));
|
|
112
|
+
// DO SQLite engine implementation
|
|
113
|
+
const getEventsDoSqlite = (cursor) => Effect.gen(function* () {
|
|
114
|
+
const selectCountSql = cursor === undefined
|
|
115
|
+
? `SELECT COUNT(*) as total FROM "${dbName}"`
|
|
116
|
+
: `SELECT COUNT(*) as total FROM "${dbName}" WHERE seqNum > ?`;
|
|
117
|
+
const total = yield* Effect.try({
|
|
118
|
+
try: () => {
|
|
119
|
+
const cursorIter = cursor === undefined ? ctx.storage.sql.exec(selectCountSql) : ctx.storage.sql.exec(selectCountSql, cursor);
|
|
120
|
+
let computed = 0;
|
|
121
|
+
for (const row of cursorIter) {
|
|
122
|
+
computed = Number(row.total ?? 0);
|
|
123
|
+
}
|
|
124
|
+
return computed;
|
|
125
|
+
},
|
|
126
|
+
catch: (error) => new UnknownError({ cause: error, payload: { dbName, stage: 'count' } }),
|
|
127
|
+
});
|
|
128
|
+
const DO_PAGE_SIZE = 256;
|
|
129
|
+
const initialState = { cursor };
|
|
130
|
+
const fetchPage = (state) => Effect.try({
|
|
131
|
+
try: () => {
|
|
132
|
+
const sql = state.cursor === undefined
|
|
133
|
+
? `SELECT * FROM "${dbName}" ORDER BY seqNum ASC LIMIT ?`
|
|
134
|
+
: `SELECT * FROM "${dbName}" WHERE seqNum > ? ORDER BY seqNum ASC LIMIT ?`;
|
|
135
|
+
const iter = state.cursor === undefined
|
|
136
|
+
? ctx.storage.sql.exec(sql, DO_PAGE_SIZE)
|
|
137
|
+
: ctx.storage.sql.exec(sql, state.cursor, DO_PAGE_SIZE);
|
|
138
|
+
const rows = [];
|
|
139
|
+
for (const row of iter)
|
|
140
|
+
rows.push(row);
|
|
141
|
+
if (rows.length === 0) {
|
|
142
|
+
return Option.none();
|
|
143
|
+
}
|
|
144
|
+
const decodedRows = Chunk.fromIterable(decodeEventlogRows(rows));
|
|
145
|
+
const eventsChunk = Chunk.map(decodedRows, ({ createdAt, ...eventEncoded }) => ({
|
|
146
|
+
eventEncoded,
|
|
147
|
+
metadata: Option.some(SyncMetadata.make({ createdAt })),
|
|
148
|
+
}));
|
|
149
|
+
const lastSeqNum = Chunk.unsafeLast(decodedRows).seqNum;
|
|
150
|
+
const nextState = { cursor: lastSeqNum };
|
|
151
|
+
return Option.some([eventsChunk, nextState]);
|
|
152
|
+
},
|
|
153
|
+
catch: (error) => new UnknownError({ cause: error, payload: { dbName, stage: 'select' } }),
|
|
154
|
+
});
|
|
155
|
+
const stream = Stream.unfoldChunkEffect(initialState, fetchPage);
|
|
156
|
+
return { total, stream };
|
|
157
|
+
}).pipe(UnknownError.mapToUnknownError, Effect.withSpan('@livestore/sync-cf:durable-object:getEvents', {
|
|
158
|
+
attributes: { dbName, cursor, engine: engine._tag },
|
|
159
|
+
}));
|
|
160
|
+
const appendEventsDoSqlite = (batch, createdAt) => Effect.try({
|
|
161
|
+
try: () => {
|
|
162
|
+
if (batch.length === 0)
|
|
163
|
+
return;
|
|
164
|
+
// Keep params per statement within conservative limits (align with D1 bound params ~100)
|
|
165
|
+
const CHUNK_SIZE = 14;
|
|
166
|
+
for (let i = 0; i < batch.length; i += CHUNK_SIZE) {
|
|
167
|
+
const chunk = batch.slice(i, i + CHUNK_SIZE);
|
|
168
|
+
const placeholders = chunk.map(() => '(?, ?, ?, ?, ?, ?, ?)').join(', ');
|
|
169
|
+
const sql = `INSERT INTO "${dbName}" (seqNum, parentSeqNum, args, name, createdAt, clientId, sessionId) VALUES ${placeholders}`;
|
|
170
|
+
const params = chunk.flatMap((event) => [
|
|
171
|
+
event.seqNum,
|
|
172
|
+
event.parentSeqNum,
|
|
173
|
+
event.args === undefined ? null : JSON.stringify(event.args),
|
|
174
|
+
event.name,
|
|
175
|
+
createdAt,
|
|
176
|
+
event.clientId,
|
|
177
|
+
event.sessionId,
|
|
178
|
+
]);
|
|
179
|
+
ctx.storage.sql.exec(sql, ...params);
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
catch: (error) => new UnknownError({ cause: error, payload: { dbName, stage: 'insert' } }),
|
|
183
|
+
}).pipe(Effect.withSpan('@livestore/sync-cf:durable-object:appendEvents', {
|
|
184
|
+
attributes: { dbName, batchLength: batch.length, engine: engine._tag },
|
|
185
|
+
}), UnknownError.mapToUnknownError);
|
|
186
|
+
if (engine._tag === 'd1') {
|
|
187
|
+
return { dbName, getEvents: getEventsD1, appendEvents: appendEventsD1, resetStore };
|
|
188
|
+
}
|
|
189
|
+
return { dbName, getEvents: getEventsDoSqlite, appendEvents: appendEventsDoSqlite, resetStore };
|
|
116
190
|
};
|
|
117
191
|
const toValidTableName = (str) => str.replaceAll(/[^a-zA-Z0-9]/g, '_');
|
|
118
192
|
//# sourceMappingURL=sync-storage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync-storage.js","sourceRoot":"","sources":["../../../src/cf-worker/do/sync-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"sync-storage.js","sourceRoot":"","sources":["../../../src/cf-worker/do/sync-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAGhD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAE/E,OAAO,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAA;AACjE,OAAO,EAAE,0BAA0B,EAAgB,MAAM,cAAc,CAAA;AACvE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAqB3C,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,GAA+B,EAC/B,OAAgB,EAChB,MAAsE,EACzD,EAAE;IACf,MAAM,MAAM,GAAG,YAAY,0BAA0B,IAAI,gBAAgB,CAAC,OAAO,CAAC,EAAE,CAAA;IAEpF,MAAM,MAAM,GAAG,CAAI,EAA4D,EAAE,EAAE,CACjF,MAAM,CAAC,UAAU,CAAC;QAChB,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM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+1,9 @@
|
|
|
1
|
+
import { type CfTypes } from '@livestore/common-cf';
|
|
1
2
|
import { Effect } from '@livestore/utils/effect';
|
|
2
3
|
import { type DoCtxInput } from '../layer.ts';
|
|
3
4
|
export interface DoRpcHandlerOptions {
|
|
4
5
|
payload: Uint8Array<ArrayBuffer>;
|
|
5
6
|
input: Omit<DoCtxInput, 'from'>;
|
|
6
7
|
}
|
|
7
|
-
export declare const createDoRpcHandler: (options: DoRpcHandlerOptions) => Effect.Effect<
|
|
8
|
+
export declare const createDoRpcHandler: (options: DoRpcHandlerOptions) => Effect.Effect<Uint8Array<ArrayBuffer> | CfTypes.ReadableStream>;
|
|
8
9
|
//# sourceMappingURL=do-rpc-server.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"do-rpc-server.d.ts","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/do-rpc-server.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"do-rpc-server.d.ts","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/do-rpc-server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,OAAO,EAA0B,MAAM,sBAAsB,CAAA;AAC3E,OAAO,EACL,MAAM,EASP,MAAM,yBAAyB,CAAA;AAIhC,OAAO,EAAS,KAAK,UAAU,EAAE,MAAM,aAAa,CAAA;AAIpD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,UAAU,CAAC,WAAW,CAAC,CAAA;IAChC,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,CAAA;CAChC;AAED,eAAO,MAAM,kBAAkB,GAC7B,SAAS,mBAAmB,KAC3B,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,OAAO,CAAC,cAAc,CAmEjB,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UnknownError } from '@livestore/common';
|
|
2
2
|
import { toDurableObjectHandler } from '@livestore/common-cf';
|
|
3
3
|
import { Effect, Headers, HttpServer, Layer, Logger, LogLevel, Option, RpcSerialization, Stream, } from '@livestore/utils/effect';
|
|
4
4
|
import { SyncDoRpc } from "../../../common/do-rpc-schema.js";
|
|
@@ -17,25 +17,31 @@ export const createDoRpcHandler = (options) => Effect.gen(this, function* () {
|
|
|
17
17
|
'SyncDoRpc.Pull': (req, { headers }) => Effect.gen(this, function* () {
|
|
18
18
|
const { rpcSubscriptions } = yield* DoCtx;
|
|
19
19
|
// TODO rename `req.rpcContext` to something more appropriate
|
|
20
|
-
if (req.rpcContext) {
|
|
20
|
+
if (req.rpcContext !== undefined) {
|
|
21
21
|
rpcSubscriptions.set(req.storeId, {
|
|
22
22
|
storeId: req.storeId,
|
|
23
|
-
payload: req.payload,
|
|
24
23
|
subscribedAt: Date.now(),
|
|
25
24
|
requestId: Headers.get(headers, 'x-rpc-request-id').pipe(Option.getOrThrow),
|
|
26
25
|
callerContext: req.rpcContext.callerContext,
|
|
26
|
+
...(req.payload !== undefined ? { payload: req.payload } : {}),
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
// DO-RPC doesn't have HTTP headers context - headers are undefined
|
|
30
|
+
return makeEndingPullStream({ req, payload: req.payload, headers: undefined });
|
|
30
31
|
}).pipe(Stream.unwrap, Stream.map((res) => ({
|
|
31
32
|
...res,
|
|
32
33
|
rpcRequestId: Headers.get(headers, 'x-rpc-request-id').pipe(Option.getOrThrow),
|
|
33
|
-
})), Stream.provideLayer(DoCtx.Default({ ...input, from: { storeId: req.storeId } })), Stream.mapError((cause) =>
|
|
34
|
+
})), Stream.provideLayer(DoCtx.Default({ ...input, from: { storeId: req.storeId } })), Stream.mapError((cause) => cause._tag === 'UnknownError' || cause._tag === 'BackendIdMismatchError'
|
|
35
|
+
? cause
|
|
36
|
+
: new UnknownError({ cause })), Stream.tapErrorCause(Effect.log)),
|
|
34
37
|
'SyncDoRpc.Push': (req) => Effect.gen(this, function* () {
|
|
35
38
|
const { doOptions, ctx, env, storeId } = yield* DoCtx;
|
|
36
|
-
|
|
39
|
+
// DO-RPC doesn't have HTTP headers context - headers are undefined
|
|
40
|
+
const push = makePush({ storeId, payload: req.payload, headers: undefined, options: doOptions, ctx, env });
|
|
37
41
|
return yield* push(req);
|
|
38
|
-
}).pipe(Effect.provide(DoCtx.Default({ ...input, from: { storeId: req.storeId } })), Effect.mapError((cause) =>
|
|
42
|
+
}).pipe(Effect.provide(DoCtx.Default({ ...input, from: { storeId: req.storeId } })), Effect.mapError((cause) => cause._tag === 'UnknownError' || cause._tag === 'ServerAheadError' || cause._tag === 'BackendIdMismatchError'
|
|
43
|
+
? cause
|
|
44
|
+
: new UnknownError({ cause })), Effect.tapCauseLogPretty),
|
|
39
45
|
});
|
|
40
46
|
const handler = toDurableObjectHandler(SyncDoRpc, {
|
|
41
47
|
layer: Layer.mergeAll(RpcLive, RpcSerialization.layerJson, HttpServer.layerContext).pipe(Layer.provide(Logger.consoleWithThread('SyncDo')), Layer.provide(Logger.minimumLogLevel(LogLevel.Debug))),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"do-rpc-server.js","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/do-rpc-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"do-rpc-server.js","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/do-rpc-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAgB,sBAAsB,EAAE,MAAM,sBAAsB,CAAA;AAC3E,OAAO,EACL,MAAM,EACN,OAAO,EACP,UAAU,EACV,KAAK,EACL,MAAM,EACN,QAAQ,EACR,MAAM,EACN,gBAAgB,EAChB,MAAM,GACP,MAAM,yBAAyB,CAAA;AAEhC,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAA;AAC5D,OAAO,EAAE,WAAW,EAAE,MAA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oCtx } from '../layer.ts';
|
|
4
|
-
export declare const createHttpRpcHandler: (
|
|
4
|
+
export declare const createHttpRpcHandler: (args_0: {
|
|
5
5
|
request: CfTypes.Request;
|
|
6
|
+
responseHeaders?: Record<string, string>;
|
|
7
|
+
forwardedHeaders?: Record<string, string>;
|
|
6
8
|
}) => Effect.Effect<CfTypes.Response, import("effect/Cause").TimeoutException, DoCtx | import("effect/Scope").Scope>;
|
|
7
9
|
//# sourceMappingURL=http-rpc-server.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-rpc-server.d.ts","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/http-rpc-server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,EAAE,MAAM,EAA+C,MAAM,yBAAyB,CAAA;
|
|
1
|
+
{"version":3,"file":"http-rpc-server.d.ts","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/http-rpc-server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,EAAE,MAAM,EAA+C,MAAM,yBAAyB,CAAA;AAK7F,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAInC,eAAO,MAAM,oBAAoB;aAKtB,OAAO,CAAC,OAAO;sBACN,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;uBACrB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;oHAiBzC,CAAA"}
|
|
@@ -1,24 +1,33 @@
|
|
|
1
1
|
import { Effect, HttpApp, Layer, RpcSerialization, RpcServer } from '@livestore/utils/effect';
|
|
2
2
|
import { SyncHttpRpc } from "../../../common/http-rpc-schema.js";
|
|
3
3
|
import * as SyncMessage from "../../../common/sync-message-types.js";
|
|
4
|
+
import { headersRecordToMap } from "../../shared.js";
|
|
4
5
|
import { DoCtx } from "../layer.js";
|
|
5
6
|
import { makeEndingPullStream } from "../pull.js";
|
|
6
7
|
import { makePush } from "../push.js";
|
|
7
|
-
export const createHttpRpcHandler = ({ request
|
|
8
|
-
const handlerLayer = createHttpRpcLayer;
|
|
8
|
+
export const createHttpRpcHandler = Effect.fn('createHttpRpcHandler')(function* ({ request, responseHeaders, forwardedHeaders, }) {
|
|
9
|
+
const handlerLayer = createHttpRpcLayer(forwardedHeaders);
|
|
9
10
|
const httpApp = RpcServer.toHttpApp(SyncHttpRpc).pipe(Effect.provide(handlerLayer));
|
|
10
11
|
const webHandler = yield* httpApp.pipe(Effect.map(HttpApp.toWebHandler));
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
const response = yield* Effect.promise(() => webHandler(request)).pipe(Effect.timeout(10000));
|
|
13
|
+
if (responseHeaders !== undefined) {
|
|
14
|
+
for (const [key, value] of Object.entries(responseHeaders)) {
|
|
15
|
+
response.headers.set(key, value);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return response;
|
|
19
|
+
});
|
|
20
|
+
const createHttpRpcLayer = (forwardedHeaders) => {
|
|
21
|
+
const headers = headersRecordToMap(forwardedHeaders);
|
|
22
|
+
// TODO implement admin requests
|
|
23
|
+
return SyncHttpRpc.toLayer({
|
|
24
|
+
'SyncHttpRpc.Pull': (req) => makeEndingPullStream({ req, payload: req.payload, headers }),
|
|
25
|
+
'SyncHttpRpc.Push': (req) => Effect.gen(function* () {
|
|
26
|
+
const { ctx, env, doOptions, storeId } = yield* DoCtx;
|
|
27
|
+
const push = makePush({ payload: undefined, headers, options: doOptions, storeId, ctx, env });
|
|
28
|
+
return yield* push(req);
|
|
29
|
+
}),
|
|
30
|
+
'SyncHttpRpc.Ping': () => Effect.succeed(SyncMessage.Pong.make({})),
|
|
31
|
+
}).pipe(Layer.provideMerge(RpcServer.layerProtocolHttp({ path: '/http-rpc' })), Layer.provideMerge(RpcSerialization.layerJson));
|
|
32
|
+
};
|
|
24
33
|
//# sourceMappingURL=http-rpc-server.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-rpc-server.js","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/http-rpc-server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;
|
|
1
|
+
{"version":3,"file":"http-rpc-server.js","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/http-rpc-server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAE7F,OAAO,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAA;AAChE,OAAO,KAAK,WAAW,MAAM,uCAAuC,CAAA;AACpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACnC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAErC,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,EAAE,CAAC,sBAAsB,CAAC,CAAC,QAAQ,CAAC,EAAE,EAC/E,OAAO,EACP,eAAe,EACf,gBAAgB,GAKjB;IACC,MAAM,YAAY,GAAG,kBAAkB,CAAC,gBAAgB,CAAC,CAAA;IACzD,MAAM,OAAO,GAAG,SAAS,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAA;IACnF,MAAM,UAAU,GAAG,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI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AAG,CAAC,CAAA;QACzB,CAAC,CAAC;QAEJ,kBAAkB,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;KACpE,CAAC,CAAC,IAAI,CACL,KAAK,CAAC,YAAY,CAAC,SAAS,CAAC,iBAAiB,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,EACtE,KAAK,CAAC,YAAY,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAC/C,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import { WsContext } from '@livestore/common-cf';
|
|
1
2
|
import { Layer, RpcServer } from '@livestore/utils/effect';
|
|
2
3
|
import { type DoCtxInput } from '../layer.ts';
|
|
3
|
-
export declare const makeRpcServer: ({ doSelf, doOptions }: Omit<DoCtxInput, "from">) => Layer.Layer<never, never, RpcServer.Protocol>;
|
|
4
|
+
export declare const makeRpcServer: ({ doSelf, doOptions }: Omit<DoCtxInput, "from">) => Layer.Layer<never, never, RpcServer.Protocol | WsContext>;
|
|
4
5
|
//# sourceMappingURL=ws-rpc-server.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws-rpc-server.d.ts","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/ws-rpc-server.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"ws-rpc-server.d.ts","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/ws-rpc-server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAA;AAChD,OAAO,EAAoB,KAAK,EAAE,SAAS,EAAkB,MAAM,yBAAyB,CAAA;AAI5F,OAAO,EAAS,KAAK,UAAU,EAAE,MAAM,aAAa,CAAA;AAIpD,eAAO,MAAM,aAAa,GAAI,uBAAuB,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,8DAoC5E,CAAA"}
|
|
@@ -1,21 +1,43 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { UnknownError } from '@livestore/common';
|
|
2
|
+
import { WsContext } from '@livestore/common-cf';
|
|
3
|
+
import { Effect, identity, Layer, RpcServer, Schema, Stream } from '@livestore/utils/effect';
|
|
3
4
|
import { SyncWsRpc } from "../../../common/ws-rpc-schema.js";
|
|
5
|
+
import { headersRecordToMap, WebSocketAttachmentSchema } from "../../shared.js";
|
|
4
6
|
import { DoCtx } from "../layer.js";
|
|
5
7
|
import { makeEndingPullStream } from "../pull.js";
|
|
6
8
|
import { makePush } from "../push.js";
|
|
7
9
|
export const makeRpcServer = ({ doSelf, doOptions }) => {
|
|
8
|
-
// TODO implement admin requests
|
|
9
10
|
const handlersLayer = SyncWsRpc.toLayer({
|
|
10
|
-
'SyncWsRpc.Pull': (req) =>
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
'SyncWsRpc.Pull': (req) => Effect.gen(function* () {
|
|
12
|
+
const headers = yield* getForwardedHeaders;
|
|
13
|
+
return makeEndingPullStream({ req, payload: req.payload, headers }).pipe(
|
|
14
|
+
// Needed to keep the stream alive on the client side for phase 2 (i.e. not send the `Exit` stream RPC message)
|
|
15
|
+
req.live === true ? Stream.concat(Stream.never) : identity, Stream.provideLayer(DoCtx.Default({ doSelf, doOptions, from: { storeId: req.storeId } })), Stream.mapError((cause) => cause._tag === 'UnknownError' || cause._tag === 'BackendIdMismatchError'
|
|
16
|
+
? cause
|
|
17
|
+
: new UnknownError({ cause })));
|
|
18
|
+
}).pipe(Stream.unwrap),
|
|
13
19
|
'SyncWsRpc.Push': (req) => Effect.gen(function* () {
|
|
14
20
|
const { doOptions, storeId, ctx, env } = yield* DoCtx;
|
|
15
|
-
const
|
|
21
|
+
const headers = yield* getForwardedHeaders;
|
|
22
|
+
const push = makePush({ options: doOptions, storeId, payload: req.payload, headers, ctx, env });
|
|
16
23
|
return yield* push(req);
|
|
17
|
-
}).pipe(Effect.provide(DoCtx.Default({ doSelf, doOptions, from: { storeId: req.storeId } })), Effect.mapError((cause) =>
|
|
24
|
+
}).pipe(Effect.provide(DoCtx.Default({ doSelf, doOptions, from: { storeId: req.storeId } })), Effect.mapError((cause) => cause._tag === 'UnknownError' || cause._tag === 'ServerAheadError' || cause._tag === 'BackendIdMismatchError'
|
|
25
|
+
? cause
|
|
26
|
+
: new UnknownError({ cause })), Effect.tapCauseLogPretty),
|
|
18
27
|
});
|
|
19
28
|
return RpcServer.layer(SyncWsRpc).pipe(Layer.provide(handlersLayer));
|
|
20
29
|
};
|
|
30
|
+
/** Extracts forwarded headers from the WebSocket attachment */
|
|
31
|
+
const getForwardedHeaders = Effect.gen(function* () {
|
|
32
|
+
const { ws } = yield* WsContext;
|
|
33
|
+
const attachment = ws.deserializeAttachment();
|
|
34
|
+
const decoded = Schema.decodeUnknownEither(WebSocketAttachmentSchema)(attachment);
|
|
35
|
+
if (decoded._tag === 'Left') {
|
|
36
|
+
yield* Effect.logError('Failed to decode WebSocket attachment for forwarded headers', { error: decoded.left });
|
|
37
|
+
ws.close(1011, 'invalid-attachment');
|
|
38
|
+
return yield* Effect.die('Invalid WebSocket attachment (headers decode failed)');
|
|
39
|
+
}
|
|
40
|
+
const headers = headersRecordToMap(decoded.right.headers);
|
|
41
|
+
return headers;
|
|
42
|
+
});
|
|
21
43
|
//# sourceMappingURL=ws-rpc-server.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws-rpc-server.js","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/ws-rpc-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"ws-rpc-server.js","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/ws-rpc-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,SAAS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