@livestore/sync-cf 0.3.0-dev.1 → 0.3.0-dev.10
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 +27 -34
- package/dist/cf-worker/durable-object.d.ts.map +1 -1
- package/dist/cf-worker/durable-object.js +149 -128
- package/dist/cf-worker/durable-object.js.map +1 -1
- package/dist/cf-worker/index.d.ts +1 -6
- package/dist/cf-worker/index.d.ts.map +1 -1
- package/dist/cf-worker/index.js +30 -64
- package/dist/cf-worker/index.js.map +1 -1
- package/dist/cf-worker/make-worker.d.ts +6 -0
- package/dist/cf-worker/make-worker.d.ts.map +1 -0
- package/dist/cf-worker/make-worker.js +31 -0
- package/dist/cf-worker/make-worker.js.map +1 -0
- package/dist/cf-worker/mod.d.ts +3 -0
- package/dist/cf-worker/mod.d.ts.map +1 -0
- package/dist/cf-worker/mod.js +3 -0
- package/dist/cf-worker/mod.js.map +1 -0
- package/dist/cf-worker/types.d.ts +2 -0
- package/dist/cf-worker/types.d.ts.map +1 -0
- package/dist/cf-worker/types.js +2 -0
- package/dist/cf-worker/types.js.map +1 -0
- package/dist/cf-worker/worker.d.ts +6 -0
- package/dist/cf-worker/worker.d.ts.map +1 -0
- package/dist/cf-worker/worker.js +29 -0
- package/dist/cf-worker/worker.js.map +1 -0
- package/dist/common/mod.d.ts +2 -0
- package/dist/common/mod.d.ts.map +1 -0
- package/dist/common/mod.js +2 -0
- package/dist/common/mod.js.map +1 -0
- package/dist/common/ws-message-types.d.ts +54 -207
- package/dist/common/ws-message-types.d.ts.map +1 -1
- package/dist/common/ws-message-types.js +3 -3
- package/dist/common/ws-message-types.js.map +1 -1
- package/dist/sync-impl/mod.d.ts +2 -0
- package/dist/sync-impl/mod.d.ts.map +1 -0
- package/dist/sync-impl/mod.js +2 -0
- package/dist/sync-impl/mod.js.map +1 -0
- package/dist/sync-impl/ws-impl.d.ts +3 -12
- package/dist/sync-impl/ws-impl.d.ts.map +1 -1
- package/dist/sync-impl/ws-impl.js +5 -4
- package/dist/sync-impl/ws-impl.js.map +1 -1
- package/package.json +15 -13
- package/src/cf-worker/durable-object.ts +212 -161
- package/src/cf-worker/mod.ts +2 -0
- package/src/cf-worker/worker.ts +39 -0
- package/src/common/ws-message-types.ts +3 -3
- package/src/sync-impl/ws-impl.ts +8 -16
- package/src/cf-worker/index.ts +0 -84
- /package/src/common/{index.ts → mod.ts} +0 -0
- /package/src/sync-impl/{index.ts → mod.ts} +0 -0
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
import { DbSchema
|
|
2
|
-
import { Option, Schema } from '@livestore/utils/effect';
|
|
1
|
+
import { DbSchema } from '@livestore/common/schema';
|
|
2
|
+
import { Effect, Option, Schema } from '@livestore/utils/effect';
|
|
3
3
|
import { DurableObject } from 'cloudflare:workers';
|
|
4
|
-
import
|
|
4
|
+
import { WSMessage } from '../common/mod.js';
|
|
5
5
|
export interface Env {
|
|
6
|
-
WEBSOCKET_SERVER: DurableObjectNamespace
|
|
6
|
+
WEBSOCKET_SERVER: DurableObjectNamespace;
|
|
7
7
|
DB: D1Database;
|
|
8
8
|
ADMIN_SECRET: string;
|
|
9
9
|
}
|
|
10
|
-
type WebSocketClient = WebSocket;
|
|
11
10
|
export declare const mutationLogTable: DbSchema.TableDef<{
|
|
12
11
|
name: "__unused";
|
|
13
12
|
columns: {
|
|
14
|
-
|
|
13
|
+
id: {
|
|
15
14
|
columnType: "integer";
|
|
16
|
-
schema: Schema.Schema<number
|
|
15
|
+
schema: Schema.Schema<number & import("effect/Brand").Brand<"GlobalEventId">, number, never>;
|
|
17
16
|
default: Option.None<never>;
|
|
18
17
|
nullable: false;
|
|
19
18
|
primaryKey: true;
|
|
20
19
|
};
|
|
21
|
-
|
|
20
|
+
parentId: {
|
|
22
21
|
columnType: "integer";
|
|
23
|
-
schema: Schema.Schema<number
|
|
22
|
+
schema: Schema.Schema<number & import("effect/Brand").Brand<"GlobalEventId">, number, never>;
|
|
24
23
|
default: Option.None<never>;
|
|
25
24
|
nullable: false;
|
|
26
25
|
primaryKey: false;
|
|
@@ -46,7 +45,6 @@ export declare const mutationLogTable: DbSchema.TableDef<{
|
|
|
46
45
|
nullable: false;
|
|
47
46
|
primaryKey: false;
|
|
48
47
|
};
|
|
49
|
-
id: DbSchema.SqliteDsl.ColumnDefinition<string, string>;
|
|
50
48
|
};
|
|
51
49
|
indexes?: ReadonlyArray<DbSchema.SqliteDsl.Index>;
|
|
52
50
|
ast: import("@livestore/db-schema/dist/ast/sqlite.js").Table;
|
|
@@ -55,37 +53,32 @@ export declare const mutationLogTable: DbSchema.TableDef<{
|
|
|
55
53
|
disableAutomaticIdColumn: false;
|
|
56
54
|
deriveMutations: never;
|
|
57
55
|
isSingleColumn: false;
|
|
58
|
-
requiredInsertColumnNames: "createdAt" | "
|
|
56
|
+
requiredInsertColumnNames: "createdAt" | "mutation" | "args" | "id" | "parentId";
|
|
59
57
|
}, Schema.Schema<{
|
|
60
|
-
readonly
|
|
61
|
-
readonly
|
|
58
|
+
readonly id: number & import("effect/Brand").Brand<"GlobalEventId">;
|
|
59
|
+
readonly parentId: number & import("effect/Brand").Brand<"GlobalEventId">;
|
|
62
60
|
readonly mutation: string;
|
|
63
61
|
readonly args: any;
|
|
64
62
|
readonly createdAt: string;
|
|
65
|
-
readonly id: string;
|
|
66
63
|
}, {
|
|
67
|
-
readonly
|
|
68
|
-
readonly
|
|
64
|
+
readonly id: number;
|
|
65
|
+
readonly parentId: number;
|
|
69
66
|
readonly mutation: string;
|
|
70
67
|
readonly args: string;
|
|
71
68
|
readonly createdAt: string;
|
|
72
|
-
readonly id: string;
|
|
73
69
|
}, never>>;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
webSocketClose: (ws: WebSocketClient, code: number, _reason: string, _wasClean: boolean) => Promise<void>;
|
|
89
|
-
}
|
|
90
|
-
export {};
|
|
70
|
+
/**
|
|
71
|
+
* Needs to be bumped when the storage format changes (e.g. mutationLogTable schema changes)
|
|
72
|
+
*
|
|
73
|
+
* Changing this version number will lead to a "soft reset".
|
|
74
|
+
*/
|
|
75
|
+
export declare const PERSISTENCE_FORMAT_VERSION = 2;
|
|
76
|
+
export type MakeDurableObjectClassOptions = {
|
|
77
|
+
onPush?: (message: WSMessage.PushReq) => Effect.Effect<void> | Promise<void>;
|
|
78
|
+
onPull?: (message: WSMessage.PullReq) => Effect.Effect<void> | Promise<void>;
|
|
79
|
+
};
|
|
80
|
+
export type MakeDurableObjectClass = (options?: MakeDurableObjectClassOptions) => {
|
|
81
|
+
new (ctx: DurableObjectState, env: Env): DurableObject<Env>;
|
|
82
|
+
};
|
|
83
|
+
export declare const makeDurableObject: MakeDurableObjectClass;
|
|
91
84
|
//# sourceMappingURL=durable-object.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable-object.d.ts","sourceRoot":"","sources":["../../src/cf-worker/durable-object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"durable-object.d.ts","sourceRoot":"","sources":["../../src/cf-worker/durable-object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAA+B,MAAM,0BAA0B,CAAA;AAEhF,OAAO,EAAE,MAAM,EAAoB,MAAM,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAClF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAG5C,MAAM,WAAW,GAAG;IAClB,gBAAgB,EAAE,sBAAsB,CAAA;IACxC,EAAE,EAAE,UAAU,CAAA;IACd,YAAY,EAAE,MAAM,CAAA;CACrB;AASD,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAO3B,CAAA;AAEF;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,IAAI,CAAA;AAE3C,MAAM,MAAM,6BAA6B,GAAG;IAC1C,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,CAAC,OAAO,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5E,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,CAAC,OAAO,KAAK,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAC7E,CAAA;AAED,MAAM,MAAM,sBAAsB,GAAG,CAAC,OAAO,CAAC,EAAE,6BAA6B,KAAK;IAChF,KAAK,GAAG,EAAE,kBAAkB,EAAE,GAAG,EAAE,GAAG,GAAG,aAAa,CAAC,GAAG,CAAC,CAAA;CAC5D,CAAA;AAED,eAAO,MAAM,iBAAiB,EAAE,sBAkM/B,CAAA"}
|
|
@@ -3,178 +3,199 @@ import { DbSchema, EventId } from '@livestore/common/schema';
|
|
|
3
3
|
import { shouldNeverHappen } from '@livestore/utils';
|
|
4
4
|
import { Effect, Logger, LogLevel, Option, Schema } from '@livestore/utils/effect';
|
|
5
5
|
import { DurableObject } from 'cloudflare:workers';
|
|
6
|
-
import { WSMessage } from '../common/
|
|
6
|
+
import { WSMessage } from '../common/mod.js';
|
|
7
7
|
const encodeOutgoingMessage = Schema.encodeSync(Schema.parseJson(WSMessage.BackendToClientMessage));
|
|
8
8
|
const encodeIncomingMessage = Schema.encodeSync(Schema.parseJson(WSMessage.ClientToBackendMessage));
|
|
9
9
|
const decodeIncomingMessage = Schema.decodeUnknownEither(Schema.parseJson(WSMessage.ClientToBackendMessage));
|
|
10
10
|
// NOTE actual table name is determined at runtime by `WebSocketServer.dbName`
|
|
11
11
|
export const mutationLogTable = DbSchema.table('__unused', {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
id: DbSchema.integer({ primaryKey: true, schema: EventId.GlobalEventId }),
|
|
13
|
+
parentId: DbSchema.integer({ schema: EventId.GlobalEventId }),
|
|
14
14
|
mutation: DbSchema.text({}),
|
|
15
15
|
args: DbSchema.text({ schema: Schema.parseJson(Schema.Any) }),
|
|
16
|
-
/** ISO date format */
|
|
16
|
+
/** ISO date format. Currently only used for debugging purposes. */
|
|
17
17
|
createdAt: DbSchema.text({}),
|
|
18
18
|
});
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.ctx.setWebSocketAutoResponse(new WebSocketRequestResponsePair(encodeIncomingMessage(WSMessage.Ping.make({ requestId: 'ping' })), encodeOutgoingMessage(WSMessage.Pong.make({ requestId: 'ping' }))));
|
|
31
|
-
const colSpec = makeColumnSpec(mutationLogTable.sqliteDef.ast);
|
|
32
|
-
this.env.DB.exec(`CREATE TABLE IF NOT EXISTS ${this.dbName} (${colSpec}) strict`);
|
|
33
|
-
return new Response(null, {
|
|
34
|
-
status: 101,
|
|
35
|
-
webSocket: client,
|
|
36
|
-
});
|
|
37
|
-
}).pipe(Effect.tapCauseLogPretty, Effect.runPromise);
|
|
38
|
-
webSocketMessage = (ws, message) => Effect.gen(this, function* () {
|
|
39
|
-
const decodedMessageRes = decodeIncomingMessage(message);
|
|
40
|
-
if (decodedMessageRes._tag === 'Left') {
|
|
41
|
-
console.error('Invalid message received', decodedMessageRes.left);
|
|
42
|
-
return;
|
|
19
|
+
/**
|
|
20
|
+
* Needs to be bumped when the storage format changes (e.g. mutationLogTable schema changes)
|
|
21
|
+
*
|
|
22
|
+
* Changing this version number will lead to a "soft reset".
|
|
23
|
+
*/
|
|
24
|
+
export const PERSISTENCE_FORMAT_VERSION = 2;
|
|
25
|
+
export const makeDurableObject = (options) => {
|
|
26
|
+
return class WebSocketServerBase extends DurableObject {
|
|
27
|
+
storage = undefined;
|
|
28
|
+
constructor(ctx, env) {
|
|
29
|
+
super(ctx, env);
|
|
43
30
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
31
|
+
fetch = async (request) => Effect.gen(this, function* () {
|
|
32
|
+
if (this.storage === undefined) {
|
|
33
|
+
const storeId = getStoreId(request);
|
|
34
|
+
const dbName = `mutation_log_${PERSISTENCE_FORMAT_VERSION}_${toValidTableName(storeId)}`;
|
|
35
|
+
this.storage = makeStorage(this.ctx, this.env, dbName);
|
|
36
|
+
}
|
|
37
|
+
const { 0: client, 1: server } = new WebSocketPair();
|
|
38
|
+
// See https://developers.cloudflare.com/durable-objects/examples/websocket-hibernation-server
|
|
39
|
+
this.ctx.acceptWebSocket(server);
|
|
40
|
+
this.ctx.setWebSocketAutoResponse(new WebSocketRequestResponsePair(encodeIncomingMessage(WSMessage.Ping.make({ requestId: 'ping' })), encodeOutgoingMessage(WSMessage.Pong.make({ requestId: 'ping' }))));
|
|
41
|
+
const colSpec = makeColumnSpec(mutationLogTable.sqliteDef.ast);
|
|
42
|
+
this.env.DB.exec(`CREATE TABLE IF NOT EXISTS ${this.storage.dbName} (${colSpec}) strict`);
|
|
43
|
+
return new Response(null, {
|
|
44
|
+
status: 101,
|
|
45
|
+
webSocket: client,
|
|
46
|
+
});
|
|
47
|
+
}).pipe(Effect.tapCauseLogPretty, Effect.runPromise);
|
|
48
|
+
webSocketMessage = (ws, message) => Effect.gen(this, function* () {
|
|
49
|
+
const decodedMessageRes = decodeIncomingMessage(message);
|
|
50
|
+
if (decodedMessageRes._tag === 'Left') {
|
|
51
|
+
console.error('Invalid message received', decodedMessageRes.left);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const decodedMessage = decodedMessageRes.right;
|
|
55
|
+
const requestId = decodedMessage.requestId;
|
|
56
|
+
const storage = this.storage;
|
|
57
|
+
if (storage === undefined) {
|
|
58
|
+
throw new Error('storage not initialized');
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
switch (decodedMessage._tag) {
|
|
62
|
+
case 'WSMessage.PullReq': {
|
|
63
|
+
if (options?.onPull) {
|
|
64
|
+
yield* Effect.tryAll(() => options.onPull(decodedMessage));
|
|
65
|
+
}
|
|
66
|
+
const cursor = decodedMessage.cursor;
|
|
67
|
+
const CHUNK_SIZE = 100;
|
|
68
|
+
// TODO use streaming
|
|
69
|
+
const remainingEvents = [...(yield* Effect.promise(() => storage.getEvents(cursor)))];
|
|
70
|
+
// NOTE we want to make sure the WS server responds at least once with `InitRes` even if `events` is empty
|
|
71
|
+
while (true) {
|
|
72
|
+
const events = remainingEvents.splice(0, CHUNK_SIZE);
|
|
73
|
+
ws.send(encodeOutgoingMessage(WSMessage.PullRes.make({ events, remaining: remainingEvents.length, requestId })));
|
|
74
|
+
if (remainingEvents.length === 0) {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
59
77
|
}
|
|
78
|
+
break;
|
|
60
79
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// TODO check whether we could use the Durable Object storage for this to speed up the lookup
|
|
65
|
-
const latestEvent = yield* Effect.promise(() => this.storage.getLatestEvent());
|
|
66
|
-
const expectedParentId = latestEvent?.id ?? EventId.ROOT;
|
|
67
|
-
let i = 0;
|
|
68
|
-
for (const mutationEventEncoded of decodedMessage.batch) {
|
|
69
|
-
if (mutationEventEncoded.parentId.global !== expectedParentId.global + i) {
|
|
70
|
-
const err = WSMessage.Error.make({
|
|
71
|
-
message: `Invalid parent id. Received ${mutationEventEncoded.parentId.global} but expected ${expectedParentId.global}`,
|
|
72
|
-
requestId,
|
|
73
|
-
});
|
|
74
|
-
yield* Effect.fail(err).pipe(Effect.ignoreLogged);
|
|
75
|
-
ws.send(encodeOutgoingMessage(err));
|
|
76
|
-
return;
|
|
80
|
+
case 'WSMessage.PushReq': {
|
|
81
|
+
if (options?.onPush) {
|
|
82
|
+
yield* Effect.tryAll(() => options.onPush(decodedMessage));
|
|
77
83
|
}
|
|
78
|
-
// TODO
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
84
|
+
// TODO check whether we could use the Durable Object storage for this to speed up the lookup
|
|
85
|
+
const latestEvent = yield* Effect.promise(() => storage.getLatestEvent());
|
|
86
|
+
const expectedParentId = latestEvent?.id ?? EventId.ROOT.global;
|
|
87
|
+
let i = 0;
|
|
88
|
+
for (const mutationEventEncoded of decodedMessage.batch) {
|
|
89
|
+
if (mutationEventEncoded.parentId !== expectedParentId + i) {
|
|
90
|
+
const err = WSMessage.Error.make({
|
|
91
|
+
message: `Invalid parent id. Received ${mutationEventEncoded.parentId} but expected ${expectedParentId}`,
|
|
92
|
+
requestId,
|
|
93
|
+
});
|
|
94
|
+
yield* Effect.fail(err).pipe(Effect.ignoreLogged);
|
|
95
|
+
ws.send(encodeOutgoingMessage(err));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// TODO handle clientId unique conflict
|
|
99
|
+
const createdAt = new Date().toISOString();
|
|
100
|
+
// NOTE we're currently not blocking on this to allow broadcasting right away
|
|
101
|
+
const storePromise = storage.appendEvent(mutationEventEncoded, createdAt);
|
|
102
|
+
ws.send(encodeOutgoingMessage(WSMessage.PushAck.make({ mutationId: mutationEventEncoded.id, requestId })));
|
|
103
|
+
// console.debug(`Broadcasting mutation event to ${this.subscribedWebSockets.size} clients`)
|
|
104
|
+
const connectedClients = this.ctx.getWebSockets();
|
|
105
|
+
if (connectedClients.length > 0) {
|
|
106
|
+
const broadcastMessage = encodeOutgoingMessage(
|
|
107
|
+
// TODO refactor to batch api
|
|
108
|
+
WSMessage.PushBroadcast.make({
|
|
109
|
+
mutationEventEncoded,
|
|
110
|
+
metadata: Option.some({ createdAt }),
|
|
111
|
+
}));
|
|
112
|
+
for (const conn of connectedClients) {
|
|
113
|
+
console.log('Broadcasting to client', conn === ws ? 'self' : 'other');
|
|
114
|
+
// if (conn !== ws) {
|
|
115
|
+
conn.send(broadcastMessage);
|
|
116
|
+
// }
|
|
117
|
+
}
|
|
97
118
|
}
|
|
119
|
+
yield* Effect.promise(() => storePromise);
|
|
120
|
+
i++;
|
|
98
121
|
}
|
|
99
|
-
|
|
100
|
-
i++;
|
|
122
|
+
break;
|
|
101
123
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
124
|
+
case 'WSMessage.AdminResetRoomReq': {
|
|
125
|
+
if (decodedMessage.adminSecret !== this.env.ADMIN_SECRET) {
|
|
126
|
+
ws.send(encodeOutgoingMessage(WSMessage.Error.make({ message: 'Invalid admin secret', requestId })));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
yield* Effect.promise(() => storage.resetRoom());
|
|
130
|
+
ws.send(encodeOutgoingMessage(WSMessage.AdminResetRoomRes.make({ requestId })));
|
|
131
|
+
break;
|
|
108
132
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
133
|
+
case 'WSMessage.AdminInfoReq': {
|
|
134
|
+
if (decodedMessage.adminSecret !== this.env.ADMIN_SECRET) {
|
|
135
|
+
ws.send(encodeOutgoingMessage(WSMessage.Error.make({ message: 'Invalid admin secret', requestId })));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
ws.send(encodeOutgoingMessage(WSMessage.AdminInfoRes.make({ requestId, info: { durableObjectId: this.ctx.id.toString() } })));
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
default: {
|
|
142
|
+
console.error('unsupported message', decodedMessage);
|
|
143
|
+
return shouldNeverHappen();
|
|
117
144
|
}
|
|
118
|
-
ws.send(encodeOutgoingMessage(WSMessage.AdminInfoRes.make({ requestId, info: { durableObjectId: this.ctx.id.toString() } })));
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
default: {
|
|
122
|
-
console.error('unsupported message', decodedMessage);
|
|
123
|
-
return shouldNeverHappen();
|
|
124
145
|
}
|
|
125
146
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
147
|
+
catch (error) {
|
|
148
|
+
ws.send(encodeOutgoingMessage(WSMessage.Error.make({ message: error.message, requestId })));
|
|
149
|
+
}
|
|
150
|
+
}).pipe(Effect.withSpan('@livestore/sync-cf:durable-object:webSocketMessage'), Effect.tapCauseLogPretty, Logger.withMinimumLogLevel(LogLevel.Debug), Effect.provide(Logger.pretty), Effect.runPromise);
|
|
151
|
+
webSocketClose = async (ws, code, _reason, _wasClean) => {
|
|
152
|
+
// If the client closes the connection, the runtime will invoke the webSocketClose() handler.
|
|
153
|
+
ws.close(code, 'Durable Object is closing WebSocket');
|
|
154
|
+
};
|
|
134
155
|
};
|
|
135
|
-
}
|
|
156
|
+
};
|
|
136
157
|
const makeStorage = (ctx, env, dbName) => {
|
|
137
158
|
const getLatestEvent = async () => {
|
|
138
|
-
const rawEvents = await env.DB.prepare(`SELECT * FROM ${dbName} ORDER BY
|
|
159
|
+
const rawEvents = await env.DB.prepare(`SELECT * FROM ${dbName} ORDER BY id DESC LIMIT 1`).all();
|
|
139
160
|
if (rawEvents.error) {
|
|
140
161
|
throw new Error(rawEvents.error);
|
|
141
162
|
}
|
|
142
|
-
const events = Schema.decodeUnknownSync(Schema.Array(mutationLogTable.schema))(rawEvents.results)
|
|
143
|
-
...e,
|
|
144
|
-
// TODO remove local ids
|
|
145
|
-
id: { global: e.idGlobal, local: 0 },
|
|
146
|
-
parentId: { global: e.parentIdGlobal, local: 0 },
|
|
147
|
-
}));
|
|
163
|
+
const events = Schema.decodeUnknownSync(Schema.Array(mutationLogTable.schema))(rawEvents.results);
|
|
148
164
|
return events[0];
|
|
149
165
|
};
|
|
150
166
|
const getEvents = async (cursor) => {
|
|
151
|
-
const whereClause = cursor === undefined ? '' : `WHERE
|
|
152
|
-
const sql = `SELECT * FROM ${dbName} ${whereClause} ORDER BY
|
|
167
|
+
const whereClause = cursor === undefined ? '' : `WHERE id > ${cursor}`;
|
|
168
|
+
const sql = `SELECT * FROM ${dbName} ${whereClause} ORDER BY id ASC`;
|
|
153
169
|
// TODO handle case where `cursor` was not found
|
|
154
170
|
const rawEvents = await env.DB.prepare(sql).all();
|
|
155
171
|
if (rawEvents.error) {
|
|
156
172
|
throw new Error(rawEvents.error);
|
|
157
173
|
}
|
|
158
|
-
const events = Schema.decodeUnknownSync(Schema.Array(mutationLogTable.schema))(rawEvents.results).map(({ createdAt, ...
|
|
159
|
-
mutationEventEncoded
|
|
160
|
-
...e,
|
|
161
|
-
// TODO remove local ids
|
|
162
|
-
id: { global: e.idGlobal, local: 0 },
|
|
163
|
-
parentId: { global: e.parentIdGlobal, local: 0 },
|
|
164
|
-
},
|
|
174
|
+
const events = Schema.decodeUnknownSync(Schema.Array(mutationLogTable.schema))(rawEvents.results).map(({ createdAt, ...mutationEventEncoded }) => ({
|
|
175
|
+
mutationEventEncoded,
|
|
165
176
|
metadata: Option.some({ createdAt }),
|
|
166
177
|
}));
|
|
167
178
|
return events;
|
|
168
179
|
};
|
|
169
180
|
const appendEvent = async (event, createdAt) => {
|
|
170
|
-
const sql = `INSERT INTO ${dbName} (
|
|
181
|
+
const sql = `INSERT INTO ${dbName} (id, parentId, args, mutation, createdAt) VALUES (?, ?, ?, ?, ?)`;
|
|
171
182
|
await env.DB.prepare(sql)
|
|
172
|
-
.bind(event.id
|
|
183
|
+
.bind(event.id, event.parentId, JSON.stringify(event.args), event.mutation, createdAt)
|
|
173
184
|
.run();
|
|
174
185
|
};
|
|
175
186
|
const resetRoom = async () => {
|
|
176
187
|
await ctx.storage.deleteAll();
|
|
177
188
|
};
|
|
178
|
-
return { getLatestEvent, getEvents, appendEvent, resetRoom };
|
|
189
|
+
return { dbName, getLatestEvent, getEvents, appendEvent, resetRoom };
|
|
190
|
+
};
|
|
191
|
+
const getStoreId = (request) => {
|
|
192
|
+
const url = new URL(request.url);
|
|
193
|
+
const searchParams = url.searchParams;
|
|
194
|
+
const storeId = searchParams.get('storeId');
|
|
195
|
+
if (storeId === null) {
|
|
196
|
+
throw new Error('storeId search param is required');
|
|
197
|
+
}
|
|
198
|
+
return storeId;
|
|
179
199
|
};
|
|
200
|
+
const toValidTableName = (str) => str.replaceAll(/[^a-zA-Z0-9]/g, '_');
|
|
180
201
|
//# sourceMappingURL=durable-object.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durable-object.js","sourceRoot":"","sources":["../../src/cf-worker/durable-object.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAsB,MAAM,0BAA0B,CAAA;AAChF,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAClF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAElD,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"durable-object.js","sourceRoot":"","sources":["../../src/cf-worker/durable-object.ts"],"names":[],"mapp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AiB,EAAE,EAAE;QACrF,MAAM,GAAG,GAAG,eAAe,MAAM,mEAAmE,CAAA;QACpG,MAAM,GAAG,CAAC,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC;aACtB,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,QAAQ,EAAE,SAAS,CAAC;aACrF,GAAG,EAAE,CAAA;IACV,CAAC,CAAA;IAED,MAAM,SAAS,GAAG,KAAK,IAAI,EAAE;QAC3B,MAAM,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,CAAA;IAC/B,CAAC,CAAA;IAED,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS,EAAE,CAAA;AACtE,CAAC,CAAA;AAED,MAAM,UAAU,GAAG,CAAC,OAAgB,EAAE,EAAE;IACtC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IAChC,MAAM,YAAY,GAAG,GAAG,CAAC,YAAY,CAAA;IACrC,MAAM,OAAO,GAAG,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;IAC3C,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAA;IACrD,CAAC;IACD,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA;AAED,MAAM,gBAAgB,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,eAAe,EAAE,GAAG,CAAC,CAAA"}
|
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
/// <reference no-default-lib="true"/>
|
|
2
|
-
|
|
3
|
-
export * from './durable-object.js';
|
|
4
|
-
declare const _default: {
|
|
5
|
-
fetch: (request: Request, env: Env, _ctx: ExecutionContext) => Promise<Response>;
|
|
6
|
-
};
|
|
7
|
-
export default _default;
|
|
2
|
+
export {};
|
|
8
3
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cf-worker/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cf-worker/index.ts"],"names":[],"mappings":""}
|
package/dist/cf-worker/index.js
CHANGED
|
@@ -1,67 +1,33 @@
|
|
|
1
1
|
/// <reference no-default-lib="true"/>
|
|
2
2
|
/// <reference lib="esnext" />
|
|
3
|
-
export
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
// }
|
|
30
|
-
// )
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
// return HttpServer.response.empty({ status: 400 })
|
|
34
|
-
// }),
|
|
35
|
-
// (_) => HttpServer.app.toWebHandler(_)(request),
|
|
36
|
-
// // request
|
|
37
|
-
// )
|
|
38
|
-
// Worker
|
|
39
|
-
export default {
|
|
40
|
-
fetch: async (request, env, _ctx) => {
|
|
41
|
-
const url = new URL(request.url);
|
|
42
|
-
const searchParams = url.searchParams;
|
|
43
|
-
const roomId = searchParams.get('room');
|
|
44
|
-
if (roomId === null) {
|
|
45
|
-
return new Response('Room ID is required', { status: 400 });
|
|
46
|
-
}
|
|
47
|
-
// This example will refer to the same Durable Object instance,
|
|
48
|
-
// since the name "foo" is hardcoded.
|
|
49
|
-
const id = env.WEBSOCKET_SERVER.idFromName(roomId);
|
|
50
|
-
const durableObject = env.WEBSOCKET_SERVER.get(id);
|
|
51
|
-
if (url.pathname.endsWith('/websocket')) {
|
|
52
|
-
const upgradeHeader = request.headers.get('Upgrade');
|
|
53
|
-
if (!upgradeHeader || upgradeHeader !== 'websocket') {
|
|
54
|
-
return new Response('Durable Object expected Upgrade: websocket', { status: 426 });
|
|
55
|
-
}
|
|
56
|
-
return durableObject.fetch(request);
|
|
57
|
-
}
|
|
58
|
-
return new Response(null, {
|
|
59
|
-
status: 400,
|
|
60
|
-
statusText: 'Bad Request',
|
|
61
|
-
headers: {
|
|
62
|
-
'Content-Type': 'text/plain',
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
},
|
|
66
|
-
};
|
|
3
|
+
export {};
|
|
4
|
+
// export * from './durable-object.js'
|
|
5
|
+
// export default {
|
|
6
|
+
// fetch: async (request: Request, env: Env, _ctx: ExecutionContext): Promise<Response> => {
|
|
7
|
+
// const url = new URL(request.url)
|
|
8
|
+
// const searchParams = url.searchParams
|
|
9
|
+
// const roomId = searchParams.get('room')
|
|
10
|
+
// if (roomId === null) {
|
|
11
|
+
// return new Response('Room ID is required', { status: 400 })
|
|
12
|
+
// }
|
|
13
|
+
// // This example will refer to the same Durable Object instance,
|
|
14
|
+
// // since the name "foo" is hardcoded.
|
|
15
|
+
// const id = env.WEBSOCKET_SERVER.idFromName(roomId)
|
|
16
|
+
// const durableObject = env.WEBSOCKET_SERVER.get(id)
|
|
17
|
+
// if (url.pathname.endsWith('/websocket')) {
|
|
18
|
+
// const upgradeHeader = request.headers.get('Upgrade')
|
|
19
|
+
// if (!upgradeHeader || upgradeHeader !== 'websocket') {
|
|
20
|
+
// return new Response('Durable Object expected Upgrade: websocket', { status: 426 })
|
|
21
|
+
// }
|
|
22
|
+
// return durableObject.fetch(request)
|
|
23
|
+
// }
|
|
24
|
+
// return new Response(null, {
|
|
25
|
+
// status: 400,
|
|
26
|
+
// statusText: 'Bad Request',
|
|
27
|
+
// headers: {
|
|
28
|
+
// 'Content-Type': 'text/plain',
|
|
29
|
+
// },
|
|
30
|
+
// })
|
|
31
|
+
// },
|
|
32
|
+
// }
|
|
67
33
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cf-worker/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,8BAA8B
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cf-worker/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,8BAA8B;;AAI9B,sCAAsC;AAEtC,mBAAmB;AACnB,8FAA8F;AAC9F,uCAAuC;AACvC,4CAA4C;AAC5C,8CAA8C;AAE9C,6BAA6B;AAC7B,oEAAoE;AACpE,QAAQ;AAER,sEAAsE;AACtE,4CAA4C;AAC5C,yDAAyD;AACzD,yDAAyD;AAEzD,iDAAiD;AACjD,6DAA6D;AAC7D,+DAA+D;AAC/D,6FAA6F;AAC7F,UAAU;AAEV,4CAA4C;AAC5C,QAAQ;AAER,kCAAkC;AAClC,qBAAqB;AACrB,mCAAmC;AACnC,mBAAmB;AACnB,wCAAwC;AACxC,WAAW;AACX,SAAS;AACT,OAAO;AACP,IAAI"}
|