@livestore/sync-cf 0.3.0-dev.22 → 0.3.0-dev.24
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/dist/.tsbuildinfo +1 -1
- package/dist/cf-worker/durable-object.d.ts +11 -2
- package/dist/cf-worker/durable-object.d.ts.map +1 -1
- package/dist/cf-worker/durable-object.js +8 -6
- package/dist/cf-worker/durable-object.js.map +1 -1
- package/dist/cf-worker/worker.d.ts +7 -1
- package/dist/cf-worker/worker.d.ts.map +1 -1
- package/dist/cf-worker/worker.js +37 -8
- package/dist/cf-worker/worker.js.map +1 -1
- package/dist/common/mod.d.ts +5 -0
- package/dist/common/mod.d.ts.map +1 -1
- package/dist/common/mod.js +5 -0
- package/dist/common/mod.js.map +1 -1
- package/dist/common/ws-message-types.d.ts +105 -33
- package/dist/common/ws-message-types.d.ts.map +1 -1
- package/dist/common/ws-message-types.js +13 -13
- package/dist/common/ws-message-types.js.map +1 -1
- package/dist/sync-impl/ws-impl.d.ts +2 -5
- package/dist/sync-impl/ws-impl.d.ts.map +1 -1
- package/dist/sync-impl/ws-impl.js +16 -6
- package/dist/sync-impl/ws-impl.js.map +1 -1
- package/package.json +3 -3
- package/src/cf-worker/durable-object.ts +8 -6
- package/src/cf-worker/worker.ts +74 -28
- package/src/common/mod.ts +7 -0
- package/src/common/ws-message-types.ts +14 -13
- package/src/sync-impl/ws-impl.ts +122 -110
|
@@ -4,11 +4,11 @@ export const PullReq = Schema.TaggedStruct('WSMessage.PullReq', {
|
|
|
4
4
|
requestId: Schema.String,
|
|
5
5
|
/** Omitting the cursor will start from the beginning */
|
|
6
6
|
cursor: Schema.optional(Schema.Number),
|
|
7
|
-
});
|
|
7
|
+
}).annotations({ title: '@livestore/sync-cf:PullReq' });
|
|
8
8
|
export const SyncMetadata = Schema.Struct({
|
|
9
9
|
/** ISO date format */
|
|
10
10
|
createdAt: Schema.String,
|
|
11
|
-
});
|
|
11
|
+
}).annotations({ title: '@livestore/sync-cf:SyncMetadata' });
|
|
12
12
|
export const PullRes = Schema.TaggedStruct('WSMessage.PullRes', {
|
|
13
13
|
batch: Schema.Array(Schema.Struct({
|
|
14
14
|
mutationEventEncoded: MutationEvent.AnyEncodedGlobal,
|
|
@@ -16,42 +16,42 @@ export const PullRes = Schema.TaggedStruct('WSMessage.PullRes', {
|
|
|
16
16
|
})),
|
|
17
17
|
requestId: Schema.Struct({ context: Schema.Literal('pull', 'push'), requestId: Schema.String }),
|
|
18
18
|
remaining: Schema.Number,
|
|
19
|
-
});
|
|
19
|
+
}).annotations({ title: '@livestore/sync-cf:PullRes' });
|
|
20
20
|
export const PushReq = Schema.TaggedStruct('WSMessage.PushReq', {
|
|
21
21
|
requestId: Schema.String,
|
|
22
22
|
batch: Schema.Array(MutationEvent.AnyEncodedGlobal),
|
|
23
|
-
});
|
|
23
|
+
}).annotations({ title: '@livestore/sync-cf:PushReq' });
|
|
24
24
|
export const PushAck = Schema.TaggedStruct('WSMessage.PushAck', {
|
|
25
25
|
requestId: Schema.String,
|
|
26
|
-
});
|
|
26
|
+
}).annotations({ title: '@livestore/sync-cf:PushAck' });
|
|
27
27
|
export const Error = Schema.TaggedStruct('WSMessage.Error', {
|
|
28
28
|
requestId: Schema.String,
|
|
29
29
|
message: Schema.String,
|
|
30
|
-
});
|
|
30
|
+
}).annotations({ title: '@livestore/sync-cf:Error' });
|
|
31
31
|
export const Ping = Schema.TaggedStruct('WSMessage.Ping', {
|
|
32
32
|
requestId: Schema.Literal('ping'),
|
|
33
|
-
});
|
|
33
|
+
}).annotations({ title: '@livestore/sync-cf:Ping' });
|
|
34
34
|
export const Pong = Schema.TaggedStruct('WSMessage.Pong', {
|
|
35
35
|
requestId: Schema.Literal('ping'),
|
|
36
|
-
});
|
|
36
|
+
}).annotations({ title: '@livestore/sync-cf:Pong' });
|
|
37
37
|
export const AdminResetRoomReq = Schema.TaggedStruct('WSMessage.AdminResetRoomReq', {
|
|
38
38
|
requestId: Schema.String,
|
|
39
39
|
adminSecret: Schema.String,
|
|
40
|
-
});
|
|
40
|
+
}).annotations({ title: '@livestore/sync-cf:AdminResetRoomReq' });
|
|
41
41
|
export const AdminResetRoomRes = Schema.TaggedStruct('WSMessage.AdminResetRoomRes', {
|
|
42
42
|
requestId: Schema.String,
|
|
43
|
-
});
|
|
43
|
+
}).annotations({ title: '@livestore/sync-cf:AdminResetRoomRes' });
|
|
44
44
|
export const AdminInfoReq = Schema.TaggedStruct('WSMessage.AdminInfoReq', {
|
|
45
45
|
requestId: Schema.String,
|
|
46
46
|
adminSecret: Schema.String,
|
|
47
|
-
});
|
|
47
|
+
}).annotations({ title: '@livestore/sync-cf:AdminInfoReq' });
|
|
48
48
|
export const AdminInfoRes = Schema.TaggedStruct('WSMessage.AdminInfoRes', {
|
|
49
49
|
requestId: Schema.String,
|
|
50
50
|
info: Schema.Struct({
|
|
51
51
|
durableObjectId: Schema.String,
|
|
52
52
|
}),
|
|
53
|
-
});
|
|
54
|
-
export const Message = Schema.Union(PullReq, PullRes, PushReq, PushAck, Error, Ping, Pong, AdminResetRoomReq, AdminResetRoomRes, AdminInfoReq, AdminInfoRes);
|
|
53
|
+
}).annotations({ title: '@livestore/sync-cf:AdminInfoRes' });
|
|
54
|
+
export const Message = Schema.Union(PullReq, PullRes, PushReq, PushAck, Error, Ping, Pong, AdminResetRoomReq, AdminResetRoomRes, AdminInfoReq, AdminInfoRes).annotations({ title: '@livestore/sync-cf:Message' });
|
|
55
55
|
export const BackendToClientMessage = Schema.Union(PullRes, PushAck, AdminResetRoomRes, AdminInfoRes, Error, Pong);
|
|
56
56
|
export const ClientToBackendMessage = Schema.Union(PullReq, PushReq, AdminResetRoomReq, AdminInfoReq, Ping);
|
|
57
57
|
//# sourceMappingURL=ws-message-types.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws-message-types.js","sourceRoot":"","sources":["../../src/common/ws-message-types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,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CAAA;AAKtD,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;AAGlH,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,IAAI,CAAC,CAAA"}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Scope } from '@livestore/utils/effect';
|
|
3
|
-
import { Effect } from '@livestore/utils/effect';
|
|
1
|
+
import type { SyncBackendConstructor } from '@livestore/common';
|
|
4
2
|
import type { SyncMetadata } from '../common/ws-message-types.js';
|
|
5
3
|
export interface WsSyncOptions {
|
|
6
4
|
url: string;
|
|
7
|
-
storeId: string;
|
|
8
5
|
}
|
|
9
|
-
export declare const
|
|
6
|
+
export declare const makeCfSync: (options: WsSyncOptions) => SyncBackendConstructor<SyncMetadata>;
|
|
10
7
|
//# sourceMappingURL=ws-impl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws-impl.d.ts","sourceRoot":"","sources":["../../src/sync-impl/ws-impl.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"ws-impl.d.ts","sourceRoot":"","sources":["../../src/sync-impl/ws-impl.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAe,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAoB5E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAA;AAEjE,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,eAAO,MAAM,UAAU,GACpB,SAAS,aAAa,KAAG,sBAAsB,CAAC,YAAY,CA2HzD,CAAA"}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
/// <reference lib="dom" />
|
|
2
|
-
import { InvalidPullError, InvalidPushError } from '@livestore/common';
|
|
2
|
+
import { InvalidPullError, InvalidPushError, UnexpectedError } from '@livestore/common';
|
|
3
3
|
import { EventId } from '@livestore/common/schema';
|
|
4
4
|
import { LS_DEV, shouldNeverHappen } from '@livestore/utils';
|
|
5
|
-
import { Deferred, Effect, Option, PubSub, Queue, Schedule, Schema, Stream, SubscriptionRef, WebSocket, } from '@livestore/utils/effect';
|
|
5
|
+
import { Deferred, Effect, Option, PubSub, Queue, Schedule, Schema, Stream, SubscriptionRef, UrlParams, WebSocket, } from '@livestore/utils/effect';
|
|
6
6
|
import { nanoid } from '@livestore/utils/nanoid';
|
|
7
|
-
import { WSMessage } from '../common/mod.js';
|
|
8
|
-
export const
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
import { SearchParamsSchema, WSMessage } from '../common/mod.js';
|
|
8
|
+
export const makeCfSync = (options) => ({ storeId, payload }) => Effect.gen(function* () {
|
|
9
|
+
const urlParamsData = yield* Schema.encode(SearchParamsSchema)({
|
|
10
|
+
storeId,
|
|
11
|
+
payload,
|
|
12
|
+
}).pipe(UnexpectedError.mapToUnexpectedError);
|
|
13
|
+
const urlParams = UrlParams.fromInput(urlParamsData);
|
|
14
|
+
const wsUrl = `${options.url}/websocket?${UrlParams.toString(urlParams)}`;
|
|
11
15
|
const { isConnected, incomingMessages, send } = yield* connect(wsUrl);
|
|
12
16
|
/**
|
|
13
17
|
* We need to account for the scenario where push-caused PullRes message arrive before the pull-caused PullRes message.
|
|
@@ -79,6 +83,12 @@ export const makeWsSync = (options) => Effect.gen(function* () {
|
|
|
79
83
|
const createdAt = new Date().toISOString();
|
|
80
84
|
return { metadata: Array.from({ length: batch.length }, () => Option.some({ createdAt })) };
|
|
81
85
|
}),
|
|
86
|
+
metadata: {
|
|
87
|
+
name: '@livestore/cf-sync',
|
|
88
|
+
description: 'LiveStore sync backend implementation using Cloudflare Workers & Durable Objects',
|
|
89
|
+
protocol: 'ws',
|
|
90
|
+
url: options.url,
|
|
91
|
+
},
|
|
82
92
|
};
|
|
83
93
|
return api;
|
|
84
94
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws-impl.js","sourceRoot":"","sources":["../../src/sync-impl/ws-impl.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAG3B,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"ws-impl.js","sourceRoot":"","sources":["../../src/sync-impl/ws-impl.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAG3B,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACvF,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAC5D,OAAO,EACL,QAAQ,EACR,MAAM,EACN,MAAM,EACN,MAAM,EACN,KAAK,EACL,QAAQ,EACR,MAAM,EACN,MAAM,EACN,eAAe,EACf,SAAS,EACT,SAAS,GACV,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAEhD,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAOhE,MAAM,CAAC,MAAM,UAAU,GACrB,CAAC,OAAsB,EAAwC,EAAE,CACjE,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,CACvB,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAClB,MAAM,aAAa,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC;QAC7D,OAAO;QACP,OAAO;KACR,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,oBAAoB,CAAC,CAAA;IAE7C,MAAM,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC,aAAa,CAAC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|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livestore/sync-cf",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.24",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"exports": {
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@livestore/common": "0.3.0-dev.
|
|
18
|
-
"@livestore/utils": "0.3.0-dev.
|
|
17
|
+
"@livestore/common": "0.3.0-dev.24",
|
|
18
|
+
"@livestore/utils": "0.3.0-dev.24"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@cloudflare/workers-types": "^4.20250303.0"
|
|
@@ -28,6 +28,7 @@ export const mutationLogTable = DbSchema.table('mutation_log_${PERSISTENCE_FORMA
|
|
|
28
28
|
/** ISO date format. Currently only used for debugging purposes. */
|
|
29
29
|
createdAt: DbSchema.text({}),
|
|
30
30
|
clientId: DbSchema.text({}),
|
|
31
|
+
sessionId: DbSchema.text({}),
|
|
31
32
|
})
|
|
32
33
|
|
|
33
34
|
const WebSocketAttachmentSchema = Schema.parseJson(
|
|
@@ -43,7 +44,7 @@ export const PULL_CHUNK_SIZE = 100
|
|
|
43
44
|
*
|
|
44
45
|
* Changing this version number will lead to a "soft reset".
|
|
45
46
|
*/
|
|
46
|
-
export const PERSISTENCE_FORMAT_VERSION =
|
|
47
|
+
export const PERSISTENCE_FORMAT_VERSION = 4
|
|
47
48
|
|
|
48
49
|
export type MakeDurableObjectClassOptions = {
|
|
49
50
|
onPush?: (message: WSMessage.PushReq) => Effect.Effect<void> | Promise<void>
|
|
@@ -340,15 +341,15 @@ const makeStorage = (ctx: DurableObjectState, env: Env, storeId: string): SyncSt
|
|
|
340
341
|
|
|
341
342
|
// CF D1 limits:
|
|
342
343
|
// Maximum bound parameters per query 100, Maximum arguments per SQL function 32
|
|
343
|
-
// Thus we need to split the batch into chunks of max (100/
|
|
344
|
-
const CHUNK_SIZE =
|
|
344
|
+
// Thus we need to split the batch into chunks of max (100/7=)14 events each.
|
|
345
|
+
const CHUNK_SIZE = 14
|
|
345
346
|
|
|
346
347
|
for (let i = 0; i < batch.length; i += CHUNK_SIZE) {
|
|
347
348
|
const chunk = batch.slice(i, i + CHUNK_SIZE)
|
|
348
349
|
|
|
349
|
-
// Create a list of placeholders ("(?, ?, ?, ?, ?)
|
|
350
|
-
const valuesPlaceholders = chunk.map(() => '(?, ?, ?, ?, ?, ?)').join(', ')
|
|
351
|
-
const sql = `INSERT INTO ${dbName} (id, parentId, args, mutation, createdAt, clientId) VALUES ${valuesPlaceholders}`
|
|
350
|
+
// Create a list of placeholders ("(?, ?, ?, ?, ?, ?, ?)"), corresponding to each event.
|
|
351
|
+
const valuesPlaceholders = chunk.map(() => '(?, ?, ?, ?, ?, ?, ?)').join(', ')
|
|
352
|
+
const sql = `INSERT INTO ${dbName} (id, parentId, args, mutation, createdAt, clientId, sessionId) VALUES ${valuesPlaceholders}`
|
|
352
353
|
// Flatten the event properties into a parameters array.
|
|
353
354
|
const params = chunk.flatMap((event) => [
|
|
354
355
|
event.id,
|
|
@@ -357,6 +358,7 @@ const makeStorage = (ctx: DurableObjectState, env: Env, storeId: string): SyncSt
|
|
|
357
358
|
event.mutation,
|
|
358
359
|
createdAt,
|
|
359
360
|
event.clientId,
|
|
361
|
+
event.sessionId,
|
|
360
362
|
])
|
|
361
363
|
|
|
362
364
|
yield* execDb((db) =>
|
package/src/cf-worker/worker.ts
CHANGED
|
@@ -1,39 +1,85 @@
|
|
|
1
|
+
import { UnexpectedError } from '@livestore/common'
|
|
2
|
+
import type { Schema } from '@livestore/utils/effect'
|
|
3
|
+
import { Effect, UrlParams } from '@livestore/utils/effect'
|
|
4
|
+
|
|
5
|
+
import { SearchParamsSchema } from '../common/mod.js'
|
|
1
6
|
import type { Env } from './durable-object.js'
|
|
2
7
|
|
|
3
8
|
export type CFWorker = {
|
|
4
9
|
fetch: (request: Request, env: Env, ctx: ExecutionContext) => Promise<Response>
|
|
5
10
|
}
|
|
6
11
|
|
|
7
|
-
export
|
|
12
|
+
export type MakeWorkerOptions = {
|
|
13
|
+
validatePayload?: (payload: Schema.JsonValue | undefined) => void | Promise<void>
|
|
14
|
+
/** @default false */
|
|
15
|
+
enableCORS?: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const makeWorker = (options: MakeWorkerOptions = {}): CFWorker => {
|
|
8
19
|
return {
|
|
9
|
-
fetch: async (request, env, _ctx) =>
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
return new Response(
|
|
20
|
+
fetch: async (request, env, _ctx) =>
|
|
21
|
+
Effect.gen(function* () {
|
|
22
|
+
const url = new URL(request.url)
|
|
23
|
+
const urlParams = UrlParams.fromInput(url.searchParams)
|
|
24
|
+
const paramsResult = yield* UrlParams.schemaStruct(SearchParamsSchema)(urlParams).pipe(Effect.either)
|
|
25
|
+
|
|
26
|
+
const corsHeaders: HeadersInit = options.enableCORS
|
|
27
|
+
? {
|
|
28
|
+
'Access-Control-Allow-Origin': '*',
|
|
29
|
+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
|
30
|
+
'Access-Control-Allow-Headers': request.headers.get('Access-Control-Request-Headers') ?? '*',
|
|
31
|
+
}
|
|
32
|
+
: {}
|
|
33
|
+
|
|
34
|
+
if (request.method === 'OPTIONS' && options.enableCORS) {
|
|
35
|
+
return new Response(null, {
|
|
36
|
+
status: 204,
|
|
37
|
+
headers: corsHeaders,
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (paramsResult._tag === 'Left') {
|
|
42
|
+
return new Response(`Invalid search params: ${paramsResult.left.toString()}`, {
|
|
43
|
+
status: 500,
|
|
44
|
+
headers: corsHeaders,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const { storeId, payload } = paramsResult.right
|
|
49
|
+
|
|
50
|
+
if (options.validatePayload !== undefined) {
|
|
51
|
+
const result = yield* Effect.promise(async () => options.validatePayload!(payload)).pipe(
|
|
52
|
+
UnexpectedError.mapToUnexpectedError,
|
|
53
|
+
Effect.either,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
if (result._tag === 'Left') {
|
|
57
|
+
console.error('Invalid payload', result.left)
|
|
58
|
+
return new Response(result.left.toString(), { status: 400, headers: corsHeaders })
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const id = env.WEBSOCKET_SERVER.idFromName(storeId)
|
|
63
|
+
const durableObject = env.WEBSOCKET_SERVER.get(id)
|
|
64
|
+
|
|
65
|
+
if (url.pathname.endsWith('/websocket')) {
|
|
66
|
+
const upgradeHeader = request.headers.get('Upgrade')
|
|
67
|
+
if (!upgradeHeader || upgradeHeader !== 'websocket') {
|
|
68
|
+
return new Response('Durable Object expected Upgrade: websocket', { status: 426, headers: corsHeaders })
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return durableObject.fetch(request)
|
|
25
72
|
}
|
|
26
73
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
})
|
|
37
|
-
},
|
|
74
|
+
console.error('Invalid path', url.pathname)
|
|
75
|
+
return new Response(null, {
|
|
76
|
+
status: 400,
|
|
77
|
+
statusText: 'Bad Request',
|
|
78
|
+
headers: {
|
|
79
|
+
...corsHeaders,
|
|
80
|
+
'Content-Type': 'text/plain',
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
}).pipe(Effect.tapCauseLogPretty, Effect.runPromise),
|
|
38
84
|
}
|
|
39
85
|
}
|
package/src/common/mod.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
+
import { Schema } from '@livestore/utils/effect'
|
|
2
|
+
|
|
1
3
|
export * as WSMessage from './ws-message-types.js'
|
|
4
|
+
|
|
5
|
+
export const SearchParamsSchema = Schema.Struct({
|
|
6
|
+
storeId: Schema.String,
|
|
7
|
+
payload: Schema.compose(Schema.StringFromUriComponent, Schema.parseJson(Schema.JsonValue)).pipe(Schema.UndefinedOr),
|
|
8
|
+
})
|
|
@@ -5,14 +5,14 @@ export const PullReq = Schema.TaggedStruct('WSMessage.PullReq', {
|
|
|
5
5
|
requestId: Schema.String,
|
|
6
6
|
/** Omitting the cursor will start from the beginning */
|
|
7
7
|
cursor: Schema.optional(Schema.Number),
|
|
8
|
-
})
|
|
8
|
+
}).annotations({ title: '@livestore/sync-cf:PullReq' })
|
|
9
9
|
|
|
10
10
|
export type PullReq = typeof PullReq.Type
|
|
11
11
|
|
|
12
12
|
export const SyncMetadata = Schema.Struct({
|
|
13
13
|
/** ISO date format */
|
|
14
14
|
createdAt: Schema.String,
|
|
15
|
-
})
|
|
15
|
+
}).annotations({ title: '@livestore/sync-cf:SyncMetadata' })
|
|
16
16
|
|
|
17
17
|
export type SyncMetadata = typeof SyncMetadata.Type
|
|
18
18
|
|
|
@@ -25,59 +25,59 @@ export const PullRes = Schema.TaggedStruct('WSMessage.PullRes', {
|
|
|
25
25
|
),
|
|
26
26
|
requestId: Schema.Struct({ context: Schema.Literal('pull', 'push'), requestId: Schema.String }),
|
|
27
27
|
remaining: Schema.Number,
|
|
28
|
-
})
|
|
28
|
+
}).annotations({ title: '@livestore/sync-cf:PullRes' })
|
|
29
29
|
|
|
30
30
|
export type PullRes = typeof PullRes.Type
|
|
31
31
|
|
|
32
32
|
export const PushReq = Schema.TaggedStruct('WSMessage.PushReq', {
|
|
33
33
|
requestId: Schema.String,
|
|
34
34
|
batch: Schema.Array(MutationEvent.AnyEncodedGlobal),
|
|
35
|
-
})
|
|
35
|
+
}).annotations({ title: '@livestore/sync-cf:PushReq' })
|
|
36
36
|
|
|
37
37
|
export type PushReq = typeof PushReq.Type
|
|
38
38
|
|
|
39
39
|
export const PushAck = Schema.TaggedStruct('WSMessage.PushAck', {
|
|
40
40
|
requestId: Schema.String,
|
|
41
|
-
})
|
|
41
|
+
}).annotations({ title: '@livestore/sync-cf:PushAck' })
|
|
42
42
|
|
|
43
43
|
export type PushAck = typeof PushAck.Type
|
|
44
44
|
|
|
45
45
|
export const Error = Schema.TaggedStruct('WSMessage.Error', {
|
|
46
46
|
requestId: Schema.String,
|
|
47
47
|
message: Schema.String,
|
|
48
|
-
})
|
|
48
|
+
}).annotations({ title: '@livestore/sync-cf:Error' })
|
|
49
49
|
|
|
50
50
|
export type Error = typeof Error.Type
|
|
51
51
|
|
|
52
52
|
export const Ping = Schema.TaggedStruct('WSMessage.Ping', {
|
|
53
53
|
requestId: Schema.Literal('ping'),
|
|
54
|
-
})
|
|
54
|
+
}).annotations({ title: '@livestore/sync-cf:Ping' })
|
|
55
55
|
|
|
56
56
|
export type Ping = typeof Ping.Type
|
|
57
57
|
|
|
58
58
|
export const Pong = Schema.TaggedStruct('WSMessage.Pong', {
|
|
59
59
|
requestId: Schema.Literal('ping'),
|
|
60
|
-
})
|
|
60
|
+
}).annotations({ title: '@livestore/sync-cf:Pong' })
|
|
61
61
|
|
|
62
62
|
export type Pong = typeof Pong.Type
|
|
63
63
|
|
|
64
64
|
export const AdminResetRoomReq = Schema.TaggedStruct('WSMessage.AdminResetRoomReq', {
|
|
65
65
|
requestId: Schema.String,
|
|
66
66
|
adminSecret: Schema.String,
|
|
67
|
-
})
|
|
67
|
+
}).annotations({ title: '@livestore/sync-cf:AdminResetRoomReq' })
|
|
68
68
|
|
|
69
69
|
export type AdminResetRoomReq = typeof AdminResetRoomReq.Type
|
|
70
70
|
|
|
71
71
|
export const AdminResetRoomRes = Schema.TaggedStruct('WSMessage.AdminResetRoomRes', {
|
|
72
72
|
requestId: Schema.String,
|
|
73
|
-
})
|
|
73
|
+
}).annotations({ title: '@livestore/sync-cf:AdminResetRoomRes' })
|
|
74
74
|
|
|
75
75
|
export type AdminResetRoomRes = typeof AdminResetRoomRes.Type
|
|
76
76
|
|
|
77
77
|
export const AdminInfoReq = Schema.TaggedStruct('WSMessage.AdminInfoReq', {
|
|
78
78
|
requestId: Schema.String,
|
|
79
79
|
adminSecret: Schema.String,
|
|
80
|
-
})
|
|
80
|
+
}).annotations({ title: '@livestore/sync-cf:AdminInfoReq' })
|
|
81
81
|
|
|
82
82
|
export type AdminInfoReq = typeof AdminInfoReq.Type
|
|
83
83
|
|
|
@@ -86,7 +86,7 @@ export const AdminInfoRes = Schema.TaggedStruct('WSMessage.AdminInfoRes', {
|
|
|
86
86
|
info: Schema.Struct({
|
|
87
87
|
durableObjectId: Schema.String,
|
|
88
88
|
}),
|
|
89
|
-
})
|
|
89
|
+
}).annotations({ title: '@livestore/sync-cf:AdminInfoRes' })
|
|
90
90
|
|
|
91
91
|
export type AdminInfoRes = typeof AdminInfoRes.Type
|
|
92
92
|
|
|
@@ -102,7 +102,8 @@ export const Message = Schema.Union(
|
|
|
102
102
|
AdminResetRoomRes,
|
|
103
103
|
AdminInfoReq,
|
|
104
104
|
AdminInfoRes,
|
|
105
|
-
)
|
|
105
|
+
).annotations({ title: '@livestore/sync-cf:Message' })
|
|
106
|
+
|
|
106
107
|
export type Message = typeof Message.Type
|
|
107
108
|
export type MessageEncoded = typeof Message.Encoded
|
|
108
109
|
|