@syncular/client 0.0.6-95 → 0.1.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 +713 -11
- package/dist/auth-leases.d.ts +11 -0
- package/dist/auth-leases.d.ts.map +1 -0
- package/dist/auth-leases.js +140 -0
- package/dist/auth-leases.js.map +1 -0
- package/dist/blob-limits.d.ts +15 -0
- package/dist/blob-limits.d.ts.map +1 -0
- package/dist/blob-limits.js +66 -0
- package/dist/blob-limits.js.map +1 -0
- package/dist/bridge-client.d.ts +69 -0
- package/dist/bridge-client.d.ts.map +1 -0
- package/dist/bridge-client.js +282 -0
- package/dist/bridge-client.js.map +1 -0
- package/dist/client-config.d.ts +6 -0
- package/dist/client-config.d.ts.map +1 -0
- package/dist/client-config.js +18 -0
- package/dist/client-config.js.map +1 -0
- package/dist/client.d.ts +65 -397
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +150 -960
- package/dist/client.js.map +1 -1
- package/dist/command-history.d.ts +35 -0
- package/dist/command-history.d.ts.map +1 -0
- package/dist/command-history.js +378 -0
- package/dist/command-history.js.map +1 -0
- package/dist/console-diagnostics.d.ts +33 -0
- package/dist/console-diagnostics.d.ts.map +1 -0
- package/dist/console-diagnostics.js +496 -0
- package/dist/console-diagnostics.js.map +1 -0
- package/dist/crdt-yjs/index.d.ts +4 -0
- package/dist/crdt-yjs/index.d.ts.map +1 -0
- package/dist/crdt-yjs/index.js +4 -0
- package/dist/crdt-yjs/index.js.map +1 -0
- package/dist/crdt-yjs/webview-host-facade.d.ts +126 -0
- package/dist/crdt-yjs/webview-host-facade.d.ts.map +1 -0
- package/dist/crdt-yjs/webview-host-facade.js +284 -0
- package/dist/crdt-yjs/webview-host-facade.js.map +1 -0
- package/dist/crdt-yjs/yjs-document-field-adapter.d.ts +153 -0
- package/dist/crdt-yjs/yjs-document-field-adapter.d.ts.map +1 -0
- package/dist/crdt-yjs/yjs-document-field-adapter.js +406 -0
- package/dist/crdt-yjs/yjs-document-field-adapter.js.map +1 -0
- package/dist/crdt-yjs/yjs-prosemirror-bridge.d.ts +73 -0
- package/dist/crdt-yjs/yjs-prosemirror-bridge.d.ts.map +1 -0
- package/dist/crdt-yjs/yjs-prosemirror-bridge.js +169 -0
- package/dist/crdt-yjs/yjs-prosemirror-bridge.js.map +1 -0
- package/dist/database.d.ts +83 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +916 -0
- package/dist/database.js.map +1 -0
- package/dist/diagnostics.d.ts +10 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +83 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/errors.d.ts +24 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +211 -0
- package/dist/errors.js.map +1 -0
- package/dist/generated-bridge.d.ts +365 -0
- package/dist/generated-bridge.d.ts.map +1 -0
- package/dist/generated-bridge.js +250 -0
- package/dist/generated-bridge.js.map +1 -0
- package/dist/index.d.ts +13 -23
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -25
- package/dist/index.js.map +1 -1
- package/dist/mutations.d.ts +4 -72
- package/dist/mutations.d.ts.map +1 -1
- package/dist/mutations.js +2 -568
- package/dist/mutations.js.map +1 -1
- package/dist/network.d.ts +3 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/network.js +17 -0
- package/dist/network.js.map +1 -0
- package/dist/react/index.d.ts +169 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +628 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react-native/index.d.ts +35 -0
- package/dist/react-native/index.d.ts.map +1 -0
- package/dist/react-native/index.js +49 -0
- package/dist/react-native/index.js.map +1 -0
- package/dist/runtime-contract.d.ts +13 -0
- package/dist/runtime-contract.d.ts.map +1 -0
- package/dist/runtime-contract.js +24 -0
- package/dist/runtime-contract.js.map +1 -0
- package/dist/rust-client.d.ts +106 -0
- package/dist/rust-client.d.ts.map +1 -0
- package/dist/rust-client.js +936 -0
- package/dist/rust-client.js.map +1 -0
- package/dist/sentry.d.ts +35 -0
- package/dist/sentry.d.ts.map +1 -0
- package/dist/sentry.js +155 -0
- package/dist/sentry.js.map +1 -0
- package/dist/sql-safety.d.ts +3 -0
- package/dist/sql-safety.d.ts.map +1 -0
- package/dist/sql-safety.js +62 -0
- package/dist/sql-safety.js.map +1 -0
- package/dist/syncular-runtime-artifacts.json +61 -0
- package/dist/tauri/index.d.ts +35 -0
- package/dist/tauri/index.d.ts.map +1 -0
- package/dist/tauri/index.js +114 -0
- package/dist/tauri/index.js.map +1 -0
- package/dist/types.d.ts +1059 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/wasm/.syncular-wasm-profile +1 -0
- package/dist/wasm/syncular-runtime-artifact.json +21 -0
- package/dist/wasm/syncular.d.ts +207 -0
- package/dist/wasm/syncular.js +2341 -0
- package/dist/wasm/syncular_bg.wasm +0 -0
- package/dist/wasm/syncular_bg.wasm.d.ts +97 -0
- package/dist/wasm-bindings/runtime-contract.d.ts +22 -0
- package/dist/wasm-bindings/runtime-contract.d.ts.map +1 -0
- package/dist/wasm-bindings/runtime-contract.js +112 -0
- package/dist/wasm-bindings/runtime-contract.js.map +1 -0
- package/dist/wasm-core/.syncular-wasm-profile +1 -0
- package/dist/wasm-core/syncular-runtime-artifact.json +17 -0
- package/dist/wasm-core/syncular.d.ts +162 -0
- package/dist/wasm-core/syncular.js +1847 -0
- package/dist/wasm-core/syncular_bg.wasm +0 -0
- package/dist/wasm-core/syncular_bg.wasm.d.ts +77 -0
- package/dist/wasm-perf/.syncular-wasm-profile +1 -0
- package/dist/wasm-perf/syncular-runtime-artifact.json +21 -0
- package/dist/wasm-perf/syncular.d.ts +207 -0
- package/dist/wasm-perf/syncular.js +2341 -0
- package/dist/wasm-perf/syncular_bg.wasm +0 -0
- package/dist/wasm-perf/syncular_bg.wasm.d.ts +97 -0
- package/dist/wasm-runtime.d.ts +18 -0
- package/dist/wasm-runtime.d.ts.map +1 -0
- package/dist/wasm-runtime.js +28 -0
- package/dist/wasm-runtime.js.map +1 -0
- package/dist/worker-client.d.ts +123 -0
- package/dist/worker-client.d.ts.map +1 -0
- package/dist/worker-client.js +1686 -0
- package/dist/worker-client.js.map +1 -0
- package/dist/worker-entry.d.ts +2 -0
- package/dist/worker-entry.d.ts.map +1 -0
- package/dist/worker-entry.js +479 -0
- package/dist/worker-entry.js.map +1 -0
- package/dist/worker-protocol.d.ts +64 -0
- package/dist/worker-protocol.d.ts.map +1 -0
- package/dist/worker-protocol.js +16 -0
- package/dist/worker-protocol.js.map +1 -0
- package/dist/worker-realtime.d.ts +39 -0
- package/dist/worker-realtime.d.ts.map +1 -0
- package/dist/worker-realtime.js +677 -0
- package/dist/worker-realtime.js.map +1 -0
- package/package.json +124 -17
- package/src/auth-leases.ts +251 -0
- package/src/blob-limits.ts +98 -0
- package/src/bridge-client.ts +512 -0
- package/src/client-config.ts +29 -0
- package/src/client.ts +256 -1414
- package/src/command-history.ts +623 -0
- package/src/console-diagnostics.ts +617 -0
- package/src/crdt-yjs/index.ts +3 -0
- package/src/crdt-yjs/webview-host-facade.ts +477 -0
- package/src/crdt-yjs/yjs-document-field-adapter.ts +733 -0
- package/src/crdt-yjs/yjs-prosemirror-bridge.ts +272 -0
- package/src/database.ts +1342 -0
- package/src/diagnostics.ts +116 -0
- package/src/errors.ts +269 -0
- package/src/generated-bridge.ts +741 -0
- package/src/index.ts +21 -25
- package/src/mutations.ts +12 -821
- package/src/network.ts +32 -0
- package/src/react/index.ts +1036 -0
- package/src/react-native/index.ts +152 -0
- package/src/runtime-contract.ts +48 -0
- package/src/rust-client.ts +1488 -0
- package/src/sentry.ts +215 -0
- package/src/sql-safety.ts +61 -0
- package/src/tauri/index.ts +211 -0
- package/src/types.ts +1356 -0
- package/src/wasm-bindings/generated-wasm-bindings.d.ts +70 -0
- package/src/wasm-bindings/runtime-contract.ts +158 -0
- package/src/wasm-runtime.ts +76 -0
- package/src/worker-client.ts +2215 -0
- package/src/worker-entry.ts +584 -0
- package/src/worker-protocol.ts +109 -0
- package/src/worker-realtime.ts +843 -0
- package/dist/blobs/index.d.ts +0 -7
- package/dist/blobs/index.d.ts.map +0 -1
- package/dist/blobs/index.js +0 -7
- package/dist/blobs/index.js.map +0 -1
- package/dist/blobs/manager.d.ts +0 -345
- package/dist/blobs/manager.d.ts.map +0 -1
- package/dist/blobs/manager.js +0 -749
- package/dist/blobs/manager.js.map +0 -1
- package/dist/blobs/migrate.d.ts +0 -14
- package/dist/blobs/migrate.d.ts.map +0 -1
- package/dist/blobs/migrate.js +0 -59
- package/dist/blobs/migrate.js.map +0 -1
- package/dist/blobs/types.d.ts +0 -62
- package/dist/blobs/types.d.ts.map +0 -1
- package/dist/blobs/types.js +0 -5
- package/dist/blobs/types.js.map +0 -1
- package/dist/conflicts.d.ts +0 -31
- package/dist/conflicts.d.ts.map +0 -1
- package/dist/conflicts.js +0 -112
- package/dist/conflicts.js.map +0 -1
- package/dist/create-client.d.ts +0 -118
- package/dist/create-client.d.ts.map +0 -1
- package/dist/create-client.js +0 -178
- package/dist/create-client.js.map +0 -1
- package/dist/engine/SyncEngine.d.ts +0 -272
- package/dist/engine/SyncEngine.d.ts.map +0 -1
- package/dist/engine/SyncEngine.js +0 -1860
- package/dist/engine/SyncEngine.js.map +0 -1
- package/dist/engine/index.d.ts +0 -6
- package/dist/engine/index.d.ts.map +0 -1
- package/dist/engine/index.js +0 -6
- package/dist/engine/index.js.map +0 -1
- package/dist/engine/types.d.ts +0 -345
- package/dist/engine/types.d.ts.map +0 -1
- package/dist/engine/types.js +0 -7
- package/dist/engine/types.js.map +0 -1
- package/dist/handlers/collection.d.ts +0 -6
- package/dist/handlers/collection.d.ts.map +0 -1
- package/dist/handlers/collection.js +0 -21
- package/dist/handlers/collection.js.map +0 -1
- package/dist/handlers/create-handler.d.ts +0 -120
- package/dist/handlers/create-handler.d.ts.map +0 -1
- package/dist/handlers/create-handler.js +0 -145
- package/dist/handlers/create-handler.js.map +0 -1
- package/dist/handlers/types.d.ts +0 -83
- package/dist/handlers/types.d.ts.map +0 -1
- package/dist/handlers/types.js +0 -5
- package/dist/handlers/types.js.map +0 -1
- package/dist/migrate.d.ts +0 -19
- package/dist/migrate.d.ts.map +0 -1
- package/dist/migrate.js +0 -238
- package/dist/migrate.js.map +0 -1
- package/dist/outbox.d.ts +0 -112
- package/dist/outbox.d.ts.map +0 -1
- package/dist/outbox.js +0 -294
- package/dist/outbox.js.map +0 -1
- package/dist/plugins/incrementing-version.d.ts +0 -34
- package/dist/plugins/incrementing-version.d.ts.map +0 -1
- package/dist/plugins/incrementing-version.js +0 -83
- package/dist/plugins/incrementing-version.js.map +0 -1
- package/dist/plugins/index.d.ts +0 -3
- package/dist/plugins/index.d.ts.map +0 -1
- package/dist/plugins/index.js +0 -3
- package/dist/plugins/index.js.map +0 -1
- package/dist/plugins/types.d.ts +0 -49
- package/dist/plugins/types.d.ts.map +0 -1
- package/dist/plugins/types.js +0 -15
- package/dist/plugins/types.js.map +0 -1
- package/dist/proxy/connection.d.ts +0 -33
- package/dist/proxy/connection.d.ts.map +0 -1
- package/dist/proxy/connection.js +0 -153
- package/dist/proxy/connection.js.map +0 -1
- package/dist/proxy/dialect.d.ts +0 -46
- package/dist/proxy/dialect.d.ts.map +0 -1
- package/dist/proxy/dialect.js +0 -58
- package/dist/proxy/dialect.js.map +0 -1
- package/dist/proxy/driver.d.ts +0 -42
- package/dist/proxy/driver.d.ts.map +0 -1
- package/dist/proxy/driver.js +0 -78
- package/dist/proxy/driver.js.map +0 -1
- package/dist/proxy/index.d.ts +0 -10
- package/dist/proxy/index.d.ts.map +0 -1
- package/dist/proxy/index.js +0 -10
- package/dist/proxy/index.js.map +0 -1
- package/dist/proxy/mutations.d.ts +0 -9
- package/dist/proxy/mutations.d.ts.map +0 -1
- package/dist/proxy/mutations.js +0 -11
- package/dist/proxy/mutations.js.map +0 -1
- package/dist/pull-engine.d.ts +0 -48
- package/dist/pull-engine.d.ts.map +0 -1
- package/dist/pull-engine.js +0 -648
- package/dist/pull-engine.js.map +0 -1
- package/dist/push-engine.d.ts +0 -18
- package/dist/push-engine.d.ts.map +0 -1
- package/dist/push-engine.js +0 -167
- package/dist/push-engine.js.map +0 -1
- package/dist/query/FingerprintCollector.d.ts +0 -18
- package/dist/query/FingerprintCollector.d.ts.map +0 -1
- package/dist/query/FingerprintCollector.js +0 -28
- package/dist/query/FingerprintCollector.js.map +0 -1
- package/dist/query/QueryContext.d.ts +0 -33
- package/dist/query/QueryContext.d.ts.map +0 -1
- package/dist/query/QueryContext.js +0 -16
- package/dist/query/QueryContext.js.map +0 -1
- package/dist/query/fingerprint.d.ts +0 -61
- package/dist/query/fingerprint.d.ts.map +0 -1
- package/dist/query/fingerprint.js +0 -91
- package/dist/query/fingerprint.js.map +0 -1
- package/dist/query/index.d.ts +0 -7
- package/dist/query/index.d.ts.map +0 -1
- package/dist/query/index.js +0 -7
- package/dist/query/index.js.map +0 -1
- package/dist/query/tracked-select.d.ts +0 -18
- package/dist/query/tracked-select.d.ts.map +0 -1
- package/dist/query/tracked-select.js +0 -90
- package/dist/query/tracked-select.js.map +0 -1
- package/dist/schema.d.ts +0 -83
- package/dist/schema.d.ts.map +0 -1
- package/dist/schema.js +0 -7
- package/dist/schema.js.map +0 -1
- package/dist/subscription-state.d.ts +0 -46
- package/dist/subscription-state.d.ts.map +0 -1
- package/dist/subscription-state.js +0 -185
- package/dist/subscription-state.js.map +0 -1
- package/dist/sync-loop.d.ts +0 -34
- package/dist/sync-loop.d.ts.map +0 -1
- package/dist/sync-loop.js +0 -253
- package/dist/sync-loop.js.map +0 -1
- package/dist/sync.d.ts +0 -32
- package/dist/sync.d.ts.map +0 -1
- package/dist/sync.js +0 -55
- package/dist/sync.js.map +0 -1
- package/dist/utils/id.d.ts +0 -17
- package/dist/utils/id.d.ts.map +0 -1
- package/dist/utils/id.js +0 -46
- package/dist/utils/id.js.map +0 -1
- package/src/blobs/index.ts +0 -7
- package/src/blobs/manager.ts +0 -1027
- package/src/blobs/migrate.ts +0 -67
- package/src/blobs/types.ts +0 -84
- package/src/client.test.ts +0 -417
- package/src/conflicts.ts +0 -171
- package/src/create-client.test.ts +0 -83
- package/src/create-client.ts +0 -330
- package/src/engine/SyncEngine.test.ts +0 -254
- package/src/engine/SyncEngine.ts +0 -2375
- package/src/engine/index.ts +0 -6
- package/src/engine/types.ts +0 -419
- package/src/handlers/collection.ts +0 -36
- package/src/handlers/create-handler.ts +0 -330
- package/src/handlers/types.ts +0 -108
- package/src/migrate.ts +0 -318
- package/src/outbox.ts +0 -383
- package/src/plugins/incrementing-version.ts +0 -133
- package/src/plugins/index.ts +0 -2
- package/src/plugins/types.ts +0 -63
- package/src/proxy/connection.ts +0 -191
- package/src/proxy/dialect.ts +0 -76
- package/src/proxy/driver.ts +0 -126
- package/src/proxy/index.ts +0 -10
- package/src/proxy/mutations.ts +0 -18
- package/src/pull-engine.test.ts +0 -151
- package/src/pull-engine.ts +0 -855
- package/src/push-engine.ts +0 -216
- package/src/query/FingerprintCollector.ts +0 -29
- package/src/query/QueryContext.ts +0 -54
- package/src/query/fingerprint.ts +0 -109
- package/src/query/index.ts +0 -10
- package/src/query/tracked-select.ts +0 -139
- package/src/schema.ts +0 -94
- package/src/subscription-state.ts +0 -259
- package/src/sync-loop.ts +0 -379
- package/src/sync.ts +0 -170
- package/src/utils/id.ts +0 -55
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
import { createSyncularSyncAttempt, syncularDiagnosticAttemptFields, } from './diagnostics';
|
|
2
|
+
import { createSyncularWorkerErrorPayload, SYNCULAR_WORKER_PROTOCOL_VERSION, } from './worker-protocol';
|
|
3
|
+
export class SyncularWorkerRealtimeController {
|
|
4
|
+
controllerOptions;
|
|
5
|
+
#socket;
|
|
6
|
+
#state = 'disconnected';
|
|
7
|
+
#options;
|
|
8
|
+
#reconnectTimer;
|
|
9
|
+
#reconnectCatchupTimer;
|
|
10
|
+
#heartbeatTimer;
|
|
11
|
+
#reconnectAttempts = 0;
|
|
12
|
+
#stopped = true;
|
|
13
|
+
#syncInFlight;
|
|
14
|
+
#syncAgainMessage;
|
|
15
|
+
#syncJitterTimers = new Set();
|
|
16
|
+
#setTimeout;
|
|
17
|
+
#clearTimeout;
|
|
18
|
+
#random;
|
|
19
|
+
constructor(controllerOptions) {
|
|
20
|
+
this.controllerOptions = controllerOptions;
|
|
21
|
+
this.#setTimeout =
|
|
22
|
+
controllerOptions.setTimeout ??
|
|
23
|
+
globalThis.setTimeout.bind(globalThis);
|
|
24
|
+
this.#clearTimeout =
|
|
25
|
+
controllerOptions.clearTimeout ??
|
|
26
|
+
globalThis.clearTimeout.bind(globalThis);
|
|
27
|
+
this.#random = controllerOptions.random ?? Math.random;
|
|
28
|
+
}
|
|
29
|
+
start(options) {
|
|
30
|
+
if (!this.controllerOptions.getConfig()) {
|
|
31
|
+
throw createSyncularWorkerErrorPayload('worker.not_open', 'Syncular worker client is not open');
|
|
32
|
+
}
|
|
33
|
+
this.stop();
|
|
34
|
+
this.#options = options;
|
|
35
|
+
this.#stopped = false;
|
|
36
|
+
this.#reconnectAttempts = 0;
|
|
37
|
+
this.#connect();
|
|
38
|
+
}
|
|
39
|
+
stop() {
|
|
40
|
+
this.#stopped = true;
|
|
41
|
+
this.#syncAgainMessage = undefined;
|
|
42
|
+
this.#syncInFlight = undefined;
|
|
43
|
+
this.#clearSyncJitterTimers();
|
|
44
|
+
this.#clearReconnectCatchupTimer();
|
|
45
|
+
this.#clearReconnectTimer();
|
|
46
|
+
this.#clearHeartbeatTimer();
|
|
47
|
+
this.#closeSocket();
|
|
48
|
+
this.#setState('disconnected');
|
|
49
|
+
}
|
|
50
|
+
sendPresence(action, scopeKey, metadata) {
|
|
51
|
+
const socket = this.#socket;
|
|
52
|
+
if (!socket || this.#state !== 'connected') {
|
|
53
|
+
this.#diagnostic({
|
|
54
|
+
level: 'warn',
|
|
55
|
+
code: 'realtime.presence_not_connected',
|
|
56
|
+
message: 'Syncular realtime presence send skipped while disconnected',
|
|
57
|
+
details: { action, scopeKey },
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
socket.send(JSON.stringify({
|
|
62
|
+
type: 'presence',
|
|
63
|
+
action,
|
|
64
|
+
scopeKey,
|
|
65
|
+
...(metadata === undefined ? {} : { metadata }),
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
#connect() {
|
|
69
|
+
const config = this.controllerOptions.getConfig();
|
|
70
|
+
const options = this.#options;
|
|
71
|
+
if (!config || !options || this.#stopped)
|
|
72
|
+
return;
|
|
73
|
+
this.#clearReconnectTimer();
|
|
74
|
+
this.#clearHeartbeatTimer();
|
|
75
|
+
this.#setState('connecting');
|
|
76
|
+
let socket;
|
|
77
|
+
try {
|
|
78
|
+
socket = this.controllerOptions.createWebSocket(resolveSyncularRealtimeUrl(config, options, this.controllerOptions.getLocationOrigin()));
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
this.#diagnostic({
|
|
82
|
+
level: 'warn',
|
|
83
|
+
code: 'realtime.connect_failed',
|
|
84
|
+
message: 'Syncular realtime websocket creation failed',
|
|
85
|
+
details: { attempt: this.#reconnectAttempts + 1 },
|
|
86
|
+
});
|
|
87
|
+
this.#scheduleReconnect();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.#socket = socket;
|
|
91
|
+
socket.onopen = () => {
|
|
92
|
+
if (socket !== this.#socket)
|
|
93
|
+
return;
|
|
94
|
+
this.#reconnectAttempts = 0;
|
|
95
|
+
this.#setState('connected');
|
|
96
|
+
this.#resetHeartbeatTimer();
|
|
97
|
+
};
|
|
98
|
+
socket.onmessage = (event) => {
|
|
99
|
+
if (socket !== this.#socket)
|
|
100
|
+
return;
|
|
101
|
+
this.#resetHeartbeatTimer();
|
|
102
|
+
void this.#handleMessage(socket, event.data);
|
|
103
|
+
};
|
|
104
|
+
socket.onerror = () => {
|
|
105
|
+
if (socket !== this.#socket)
|
|
106
|
+
return;
|
|
107
|
+
this.#diagnostic({
|
|
108
|
+
level: 'warn',
|
|
109
|
+
code: 'realtime.socket_error',
|
|
110
|
+
message: 'Syncular realtime websocket reported an error',
|
|
111
|
+
});
|
|
112
|
+
this.#reconnect();
|
|
113
|
+
};
|
|
114
|
+
socket.onclose = () => {
|
|
115
|
+
if (socket !== this.#socket)
|
|
116
|
+
return;
|
|
117
|
+
this.#reconnect();
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
async #handleMessage(socket, data) {
|
|
121
|
+
const bytes = await readRealtimeMessageBytes(data);
|
|
122
|
+
if (socket !== this.#socket)
|
|
123
|
+
return;
|
|
124
|
+
if (bytes && isSyncPackBytes(bytes)) {
|
|
125
|
+
this.#diagnostic({
|
|
126
|
+
level: 'debug',
|
|
127
|
+
code: 'realtime.sync_wakeup',
|
|
128
|
+
message: 'Syncular realtime binary sync-pack received',
|
|
129
|
+
details: {
|
|
130
|
+
bytes: bytes.byteLength,
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
this.#clearReconnectCatchupTimer();
|
|
134
|
+
this.#scheduleSync({ syncPackBytes: bytes });
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const text = bytes ? new TextDecoder().decode(bytes) : data;
|
|
138
|
+
if (text == null)
|
|
139
|
+
return;
|
|
140
|
+
if (typeof text !== 'string')
|
|
141
|
+
return;
|
|
142
|
+
let message;
|
|
143
|
+
try {
|
|
144
|
+
message = JSON.parse(text);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const helloMessage = readSyncularRealtimeHelloMessage(message);
|
|
150
|
+
if (helloMessage) {
|
|
151
|
+
this.#diagnostic({
|
|
152
|
+
level: 'debug',
|
|
153
|
+
code: 'realtime.hello',
|
|
154
|
+
message: 'Syncular realtime session accepted',
|
|
155
|
+
details: { ...helloMessage },
|
|
156
|
+
});
|
|
157
|
+
this.#scheduleHelloCatchup(helloMessage);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const presenceMessage = readSyncularRealtimePresenceMessage(message);
|
|
161
|
+
if (presenceMessage) {
|
|
162
|
+
this.controllerOptions.postEvent({
|
|
163
|
+
protocolVersion: SYNCULAR_WORKER_PROTOCOL_VERSION,
|
|
164
|
+
type: 'presenceEvent',
|
|
165
|
+
...presenceMessage,
|
|
166
|
+
});
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const syncMessage = readSyncularRealtimeSyncMessage(message);
|
|
170
|
+
if (syncMessage) {
|
|
171
|
+
this.#diagnostic({
|
|
172
|
+
level: 'debug',
|
|
173
|
+
code: 'realtime.sync_wakeup',
|
|
174
|
+
message: 'Syncular realtime sync wakeup received',
|
|
175
|
+
details: {
|
|
176
|
+
reason: syncMessage.reason ?? null,
|
|
177
|
+
requiresPull: syncMessage.requiresPull === true,
|
|
178
|
+
droppedCount: syncMessage.droppedCount ?? 0,
|
|
179
|
+
payloadBytes: syncMessage.payloadBytes ?? null,
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
this.#clearReconnectCatchupTimer();
|
|
183
|
+
this.#scheduleRealtimeWakeupSync(syncMessage);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
#reconnect() {
|
|
187
|
+
this.#clearReconnectCatchupTimer();
|
|
188
|
+
this.#clearHeartbeatTimer();
|
|
189
|
+
this.#closeSocket();
|
|
190
|
+
this.#setState('disconnected');
|
|
191
|
+
this.#scheduleReconnect();
|
|
192
|
+
}
|
|
193
|
+
#scheduleReconnect() {
|
|
194
|
+
if (this.#stopped)
|
|
195
|
+
return;
|
|
196
|
+
this.#clearReconnectTimer();
|
|
197
|
+
const initial = this.#options?.initialReconnectDelayMs ?? 1_000;
|
|
198
|
+
const max = this.#options?.maxReconnectDelayMs ?? 30_000;
|
|
199
|
+
const factor = this.#options?.reconnectBackoffFactor ?? 2;
|
|
200
|
+
const baseDelay = Math.min(initial * factor ** this.#reconnectAttempts, max);
|
|
201
|
+
const jitterRatio = Math.max(0, this.#options?.reconnectJitterRatio ?? 0);
|
|
202
|
+
const jitter = baseDelay * jitterRatio * this.#random();
|
|
203
|
+
const delay = Math.min(baseDelay + jitter, max);
|
|
204
|
+
this.#diagnostic({
|
|
205
|
+
level: 'info',
|
|
206
|
+
code: 'realtime.reconnect_scheduled',
|
|
207
|
+
message: 'Syncular realtime reconnect scheduled',
|
|
208
|
+
details: {
|
|
209
|
+
attempt: this.#reconnectAttempts + 1,
|
|
210
|
+
baseDelayMs: baseDelay,
|
|
211
|
+
jitterMs: jitter,
|
|
212
|
+
delayMs: delay,
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
this.#reconnectAttempts += 1;
|
|
216
|
+
this.#reconnectTimer = this.#setTimeout(() => this.#connect(), delay);
|
|
217
|
+
}
|
|
218
|
+
#scheduleHelloCatchup(message) {
|
|
219
|
+
this.#clearReconnectCatchupTimer();
|
|
220
|
+
if (!message.requiresSync)
|
|
221
|
+
return;
|
|
222
|
+
const maxJitter = Math.max(0, this.#options?.pullRecoveryJitterMs ?? 0);
|
|
223
|
+
const delay = maxJitter > 0 ? maxJitter * this.#random() : 10;
|
|
224
|
+
const cursor = Number.isFinite(message.latestCursor)
|
|
225
|
+
? message.latestCursor
|
|
226
|
+
: message.cursor;
|
|
227
|
+
const syncMessage = {
|
|
228
|
+
reason: 'reconnect-catchup',
|
|
229
|
+
requiresPull: true,
|
|
230
|
+
...(Number.isFinite(cursor) ? { cursor } : {}),
|
|
231
|
+
};
|
|
232
|
+
this.#diagnostic({
|
|
233
|
+
level: 'debug',
|
|
234
|
+
code: 'realtime.reconnect_catchup_scheduled',
|
|
235
|
+
message: 'Syncular realtime reconnect catch-up scheduled from hello',
|
|
236
|
+
details: {
|
|
237
|
+
cursor: syncMessage.cursor ?? null,
|
|
238
|
+
delayMs: delay,
|
|
239
|
+
maxJitterMs: maxJitter,
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
this.#reconnectCatchupTimer = this.#setTimeout(() => {
|
|
243
|
+
this.#reconnectCatchupTimer = undefined;
|
|
244
|
+
this.#scheduleSync(syncMessage);
|
|
245
|
+
}, delay);
|
|
246
|
+
}
|
|
247
|
+
#scheduleSync(message) {
|
|
248
|
+
if (this.#stopped)
|
|
249
|
+
return;
|
|
250
|
+
if (this.#syncInFlight) {
|
|
251
|
+
this.#syncAgainMessage = mergeRealtimeSyncMessages(this.#syncAgainMessage, message);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
this.#syncInFlight = this.#runSync(message);
|
|
255
|
+
}
|
|
256
|
+
#scheduleRealtimeWakeupSync(message) {
|
|
257
|
+
const maxJitter = Math.max(0, this.#options?.pullRecoveryJitterMs ?? 0);
|
|
258
|
+
if (maxJitter <= 0 || !realtimeMessageRequiresPull(message)) {
|
|
259
|
+
this.#scheduleSync(message);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const delay = maxJitter * this.#random();
|
|
263
|
+
this.#diagnostic({
|
|
264
|
+
level: 'debug',
|
|
265
|
+
code: 'realtime.pull_recovery_jitter_scheduled',
|
|
266
|
+
message: 'Syncular realtime HTTP pull recovery delayed by jitter',
|
|
267
|
+
details: {
|
|
268
|
+
delayMs: delay,
|
|
269
|
+
maxJitterMs: maxJitter,
|
|
270
|
+
reason: message.reason ?? null,
|
|
271
|
+
cursor: message.cursor ?? null,
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
const timer = this.#setTimeout(() => {
|
|
275
|
+
this.#syncJitterTimers.delete(timer);
|
|
276
|
+
this.#scheduleSync(message);
|
|
277
|
+
}, delay);
|
|
278
|
+
this.#syncJitterTimers.add(timer);
|
|
279
|
+
}
|
|
280
|
+
async #runSync(message) {
|
|
281
|
+
const socket = this.#socket;
|
|
282
|
+
let result;
|
|
283
|
+
try {
|
|
284
|
+
result = await this.#syncForMessage(message);
|
|
285
|
+
if (this.#stopped)
|
|
286
|
+
return;
|
|
287
|
+
this.#ackRealtimeCursor(socket, message, result);
|
|
288
|
+
this.controllerOptions.postEvent({
|
|
289
|
+
protocolVersion: SYNCULAR_WORKER_PROTOCOL_VERSION,
|
|
290
|
+
type: 'bootstrapChanged',
|
|
291
|
+
bootstrap: result.bootstrap,
|
|
292
|
+
});
|
|
293
|
+
if (result.changedTables.length > 0 || result.changedRows.length > 0) {
|
|
294
|
+
this.controllerOptions.postEvent({
|
|
295
|
+
protocolVersion: SYNCULAR_WORKER_PROTOCOL_VERSION,
|
|
296
|
+
type: 'rowsChanged',
|
|
297
|
+
source: 'remotePull',
|
|
298
|
+
changedTables: result.changedTables,
|
|
299
|
+
changedRows: result.changedRows,
|
|
300
|
+
changedRowsTruncated: result.changedRowsTruncated,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
const events = this.controllerOptions
|
|
304
|
+
.getClient()
|
|
305
|
+
.drainLiveQueryEvents();
|
|
306
|
+
if (events.length > 0) {
|
|
307
|
+
this.controllerOptions.postEvent({
|
|
308
|
+
protocolVersion: SYNCULAR_WORKER_PROTOCOL_VERSION,
|
|
309
|
+
type: 'liveQueryEvents',
|
|
310
|
+
events,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
this.#diagnostic({
|
|
316
|
+
level: 'warn',
|
|
317
|
+
code: 'realtime.sync_pull_failed',
|
|
318
|
+
message: 'Syncular realtime-triggered pull failed',
|
|
319
|
+
});
|
|
320
|
+
// Realtime is best-effort. Explicit sync methods still surface errors.
|
|
321
|
+
}
|
|
322
|
+
finally {
|
|
323
|
+
this.#syncInFlight = undefined;
|
|
324
|
+
const nextMessage = this.#syncAgainMessage;
|
|
325
|
+
this.#syncAgainMessage = undefined;
|
|
326
|
+
if (nextMessage &&
|
|
327
|
+
!this.#stopped &&
|
|
328
|
+
!syncResultCoversRealtimeMessage(result, nextMessage)) {
|
|
329
|
+
this.#scheduleSync(nextMessage);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
#ackRealtimeCursor(socket, message, result) {
|
|
334
|
+
if (!socket || socket !== this.#socket)
|
|
335
|
+
return;
|
|
336
|
+
const subscriptionCursor = result.subscriptions.reduce((cursor, subscription) => Number.isFinite(subscription.nextCursor)
|
|
337
|
+
? Math.max(cursor, subscription.nextCursor)
|
|
338
|
+
: cursor, -1);
|
|
339
|
+
const messageCursor = Number.isFinite(message?.cursor)
|
|
340
|
+
? message.cursor
|
|
341
|
+
: -1;
|
|
342
|
+
const cursor = Math.max(subscriptionCursor, messageCursor);
|
|
343
|
+
if (!Number.isSafeInteger(cursor) || cursor < 0)
|
|
344
|
+
return;
|
|
345
|
+
try {
|
|
346
|
+
socket.send(JSON.stringify({ type: 'ack', cursor }));
|
|
347
|
+
this.#diagnostic({
|
|
348
|
+
level: 'debug',
|
|
349
|
+
code: 'realtime.ack_sent',
|
|
350
|
+
message: 'Syncular realtime cursor ack sent',
|
|
351
|
+
details: { cursor },
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
catch {
|
|
355
|
+
this.#diagnostic({
|
|
356
|
+
level: 'warn',
|
|
357
|
+
code: 'realtime.ack_failed',
|
|
358
|
+
message: 'Syncular realtime cursor ack failed',
|
|
359
|
+
details: { cursor },
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
async #syncForMessage(message) {
|
|
364
|
+
const client = this.controllerOptions.getClient();
|
|
365
|
+
if (message && realtimeMessageRequiresPull(message)) {
|
|
366
|
+
const syncAttempt = createSyncularSyncAttempt();
|
|
367
|
+
this.#diagnostic({
|
|
368
|
+
level: 'debug',
|
|
369
|
+
code: 'realtime.pull_required',
|
|
370
|
+
message: 'Syncular realtime event requires HTTP pull recovery',
|
|
371
|
+
...syncularDiagnosticAttemptFields(syncAttempt),
|
|
372
|
+
details: {
|
|
373
|
+
cursor: message.cursor ?? null,
|
|
374
|
+
reason: message.reason ?? null,
|
|
375
|
+
droppedCount: message.droppedCount ?? 0,
|
|
376
|
+
payloadBytes: message.payloadBytes ?? null,
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
return client.syncPull({ syncAttempt });
|
|
380
|
+
}
|
|
381
|
+
if (message?.syncPackBytes &&
|
|
382
|
+
typeof client.applyRealtimeSyncPack === 'function') {
|
|
383
|
+
try {
|
|
384
|
+
const result = await client.applyRealtimeSyncPack(message.syncPackBytes);
|
|
385
|
+
this.#diagnostic({
|
|
386
|
+
level: 'debug',
|
|
387
|
+
code: 'realtime.binary_applied',
|
|
388
|
+
message: 'Syncular realtime binary sync-pack applied',
|
|
389
|
+
details: {
|
|
390
|
+
bytes: message.syncPackBytes.byteLength,
|
|
391
|
+
totalMs: result.timings.totalMs,
|
|
392
|
+
syncPackDecodeMs: result.timings.syncPackDecodeMs,
|
|
393
|
+
pullTransformMs: result.timings.pullTransformMs,
|
|
394
|
+
integrityVerifyMs: result.timings.integrityVerifyMs,
|
|
395
|
+
pullApplyMs: result.timings.pullApplyMs,
|
|
396
|
+
commitApplyMs: result.timings.commitApplyMs,
|
|
397
|
+
subscriptionStateMs: result.timings.subscriptionStateMs,
|
|
398
|
+
notifyMs: result.timings.notifyMs,
|
|
399
|
+
changedRows: result.changedRows.length,
|
|
400
|
+
changedRowsTruncated: result.changedRowsTruncated,
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
return result;
|
|
404
|
+
}
|
|
405
|
+
catch {
|
|
406
|
+
this.#diagnostic({
|
|
407
|
+
level: 'warn',
|
|
408
|
+
code: 'realtime.binary_apply_failed',
|
|
409
|
+
message: 'Syncular realtime binary sync-pack apply failed; using HTTP pull recovery',
|
|
410
|
+
details: {
|
|
411
|
+
bytes: message.syncPackBytes.byteLength,
|
|
412
|
+
},
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
const syncAttempt = createSyncularSyncAttempt();
|
|
417
|
+
this.#diagnostic({
|
|
418
|
+
level: 'debug',
|
|
419
|
+
code: 'realtime.pull_required',
|
|
420
|
+
message: 'Syncular realtime event requires HTTP pull recovery',
|
|
421
|
+
...syncularDiagnosticAttemptFields(syncAttempt),
|
|
422
|
+
details: {
|
|
423
|
+
cursor: message?.cursor ?? null,
|
|
424
|
+
reason: message?.syncPackBytes ? 'binary-apply-failed' : null,
|
|
425
|
+
droppedCount: message?.droppedCount ?? 0,
|
|
426
|
+
payloadBytes: message?.payloadBytes ?? null,
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
return client.syncPull({ syncAttempt });
|
|
430
|
+
}
|
|
431
|
+
#clearReconnectTimer() {
|
|
432
|
+
if (!this.#reconnectTimer)
|
|
433
|
+
return;
|
|
434
|
+
this.#clearTimeout(this.#reconnectTimer);
|
|
435
|
+
this.#reconnectTimer = undefined;
|
|
436
|
+
}
|
|
437
|
+
#clearReconnectCatchupTimer() {
|
|
438
|
+
if (!this.#reconnectCatchupTimer)
|
|
439
|
+
return;
|
|
440
|
+
this.#clearTimeout(this.#reconnectCatchupTimer);
|
|
441
|
+
this.#reconnectCatchupTimer = undefined;
|
|
442
|
+
}
|
|
443
|
+
#clearHeartbeatTimer() {
|
|
444
|
+
if (!this.#heartbeatTimer)
|
|
445
|
+
return;
|
|
446
|
+
this.#clearTimeout(this.#heartbeatTimer);
|
|
447
|
+
this.#heartbeatTimer = undefined;
|
|
448
|
+
}
|
|
449
|
+
#clearSyncJitterTimers() {
|
|
450
|
+
for (const timer of this.#syncJitterTimers) {
|
|
451
|
+
this.#clearTimeout(timer);
|
|
452
|
+
}
|
|
453
|
+
this.#syncJitterTimers.clear();
|
|
454
|
+
}
|
|
455
|
+
#resetHeartbeatTimer() {
|
|
456
|
+
this.#clearHeartbeatTimer();
|
|
457
|
+
const heartbeatTimeoutMs = this.#options?.heartbeatTimeoutMs ?? 60_000;
|
|
458
|
+
if (heartbeatTimeoutMs <= 0)
|
|
459
|
+
return;
|
|
460
|
+
this.#heartbeatTimer = this.#setTimeout(() => this.#reconnect(), heartbeatTimeoutMs);
|
|
461
|
+
}
|
|
462
|
+
#closeSocket() {
|
|
463
|
+
if (!this.#socket)
|
|
464
|
+
return;
|
|
465
|
+
const socket = this.#socket;
|
|
466
|
+
this.#socket = undefined;
|
|
467
|
+
socket.onopen = null;
|
|
468
|
+
socket.onmessage = null;
|
|
469
|
+
socket.onerror = null;
|
|
470
|
+
socket.onclose = null;
|
|
471
|
+
try {
|
|
472
|
+
socket.close();
|
|
473
|
+
}
|
|
474
|
+
catch {
|
|
475
|
+
// best effort
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
#setState(state) {
|
|
479
|
+
if (this.#state === state)
|
|
480
|
+
return;
|
|
481
|
+
this.#state = state;
|
|
482
|
+
this.controllerOptions.postEvent({
|
|
483
|
+
protocolVersion: SYNCULAR_WORKER_PROTOCOL_VERSION,
|
|
484
|
+
type: 'realtimeState',
|
|
485
|
+
state,
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
#diagnostic(event) {
|
|
489
|
+
this.controllerOptions.postDiagnostic?.({
|
|
490
|
+
...event,
|
|
491
|
+
source: 'realtime',
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function realtimeMessageRequiresPull(message) {
|
|
496
|
+
return message.requiresPull === true || (message.droppedCount ?? 0) > 0;
|
|
497
|
+
}
|
|
498
|
+
function mergeRealtimeSyncMessages(current, next) {
|
|
499
|
+
if (!current)
|
|
500
|
+
return next ?? {};
|
|
501
|
+
if (!next)
|
|
502
|
+
return current;
|
|
503
|
+
const cursor = current.cursor === undefined
|
|
504
|
+
? next.cursor
|
|
505
|
+
: next.cursor === undefined
|
|
506
|
+
? current.cursor
|
|
507
|
+
: Math.max(current.cursor, next.cursor);
|
|
508
|
+
return {
|
|
509
|
+
cursor,
|
|
510
|
+
reason: next.reason ?? current.reason,
|
|
511
|
+
requiresPull: current.requiresPull === true || next.requiresPull === true,
|
|
512
|
+
droppedCount: Math.max(current.droppedCount ?? 0, next.droppedCount ?? 0),
|
|
513
|
+
payloadBytes: next.payloadBytes ?? current.payloadBytes,
|
|
514
|
+
syncPackBytes: next.syncPackBytes ?? current.syncPackBytes,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
function syncResultCoversRealtimeMessage(result, message) {
|
|
518
|
+
if (!result || message.cursor === undefined)
|
|
519
|
+
return false;
|
|
520
|
+
return syncResultCursor(result) >= message.cursor;
|
|
521
|
+
}
|
|
522
|
+
function syncResultCursor(result) {
|
|
523
|
+
return result.subscriptions.reduce((cursor, subscription) => Number.isFinite(subscription.nextCursor)
|
|
524
|
+
? Math.max(cursor, subscription.nextCursor)
|
|
525
|
+
: cursor, -1);
|
|
526
|
+
}
|
|
527
|
+
export function resolveSyncularRealtimeUrl(config, options, locationOrigin) {
|
|
528
|
+
const url = new URL(options.wsUrl ?? config.baseUrl, locationOrigin);
|
|
529
|
+
if (!options.wsUrl) {
|
|
530
|
+
url.pathname = `${url.pathname.replace(/\/$/, '')}/realtime`;
|
|
531
|
+
}
|
|
532
|
+
if (url.protocol === 'http:')
|
|
533
|
+
url.protocol = 'ws:';
|
|
534
|
+
if (url.protocol === 'https:')
|
|
535
|
+
url.protocol = 'wss:';
|
|
536
|
+
url.searchParams.set('clientId', config.clientId);
|
|
537
|
+
url.searchParams.set('transportPath', 'direct');
|
|
538
|
+
url.searchParams.set('syncPackEncoding', 'binary-sync-pack-v1');
|
|
539
|
+
for (const [key, value] of Object.entries(options.params ?? {})) {
|
|
540
|
+
if (value)
|
|
541
|
+
url.searchParams.set(key, value);
|
|
542
|
+
}
|
|
543
|
+
return url.toString();
|
|
544
|
+
}
|
|
545
|
+
export function isSyncularRealtimeSyncMessage(value) {
|
|
546
|
+
return readSyncularRealtimeSyncMessage(value) !== null;
|
|
547
|
+
}
|
|
548
|
+
function readSyncularRealtimeHelloMessage(value) {
|
|
549
|
+
if (!value || typeof value !== 'object')
|
|
550
|
+
return null;
|
|
551
|
+
if (value.event !== 'hello')
|
|
552
|
+
return null;
|
|
553
|
+
const data = value.data;
|
|
554
|
+
if (!data || typeof data !== 'object')
|
|
555
|
+
return {};
|
|
556
|
+
const record = data;
|
|
557
|
+
return {
|
|
558
|
+
protocolVersion: typeof record.protocolVersion === 'number'
|
|
559
|
+
? record.protocolVersion
|
|
560
|
+
: undefined,
|
|
561
|
+
sessionId: typeof record.sessionId === 'string' ? record.sessionId : undefined,
|
|
562
|
+
shardKey: typeof record.shardKey === 'string' ? record.shardKey : undefined,
|
|
563
|
+
cursor: typeof record.cursor === 'number' ? record.cursor : undefined,
|
|
564
|
+
latestCursor: typeof record.latestCursor === 'number' ? record.latestCursor : undefined,
|
|
565
|
+
scopeCount: typeof record.scopeCount === 'number' ? record.scopeCount : undefined,
|
|
566
|
+
requiresSync: typeof record.requiresSync === 'boolean'
|
|
567
|
+
? record.requiresSync
|
|
568
|
+
: undefined,
|
|
569
|
+
syncPackEncoding: typeof record.syncPackEncoding === 'string'
|
|
570
|
+
? record.syncPackEncoding
|
|
571
|
+
: record.syncPackEncoding === null
|
|
572
|
+
? null
|
|
573
|
+
: undefined,
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
function readSyncularRealtimeSyncMessage(value) {
|
|
577
|
+
if (!value || typeof value !== 'object')
|
|
578
|
+
return null;
|
|
579
|
+
if (value.event !== 'sync')
|
|
580
|
+
return null;
|
|
581
|
+
const data = value.data;
|
|
582
|
+
if (!data || typeof data !== 'object')
|
|
583
|
+
return {};
|
|
584
|
+
const record = data;
|
|
585
|
+
const cursor = typeof record.cursor === 'number' ? record.cursor : undefined;
|
|
586
|
+
const reason = typeof record.reason === 'string' ? record.reason : null;
|
|
587
|
+
const requiresPull = typeof record.requiresPull === 'boolean' ? record.requiresPull : undefined;
|
|
588
|
+
const droppedCount = typeof record.droppedCount === 'number' &&
|
|
589
|
+
Number.isSafeInteger(record.droppedCount)
|
|
590
|
+
? record.droppedCount
|
|
591
|
+
: undefined;
|
|
592
|
+
const payloadBytes = typeof record.payloadBytes === 'number' &&
|
|
593
|
+
Number.isSafeInteger(record.payloadBytes)
|
|
594
|
+
? record.payloadBytes
|
|
595
|
+
: undefined;
|
|
596
|
+
return {
|
|
597
|
+
cursor,
|
|
598
|
+
reason,
|
|
599
|
+
requiresPull,
|
|
600
|
+
droppedCount,
|
|
601
|
+
payloadBytes,
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
function readSyncularRealtimePresenceMessage(value) {
|
|
605
|
+
if (!value || typeof value !== 'object')
|
|
606
|
+
return null;
|
|
607
|
+
if (value.event !== 'presence')
|
|
608
|
+
return null;
|
|
609
|
+
const data = value.data;
|
|
610
|
+
if (!data || typeof data !== 'object')
|
|
611
|
+
return null;
|
|
612
|
+
const presence = data.presence;
|
|
613
|
+
if (!presence || typeof presence !== 'object')
|
|
614
|
+
return null;
|
|
615
|
+
const record = presence;
|
|
616
|
+
const action = record.action;
|
|
617
|
+
if (action !== 'join' &&
|
|
618
|
+
action !== 'leave' &&
|
|
619
|
+
action !== 'update' &&
|
|
620
|
+
action !== 'snapshot') {
|
|
621
|
+
return null;
|
|
622
|
+
}
|
|
623
|
+
if (typeof record.scopeKey !== 'string' || !record.scopeKey)
|
|
624
|
+
return null;
|
|
625
|
+
return {
|
|
626
|
+
action,
|
|
627
|
+
scopeKey: record.scopeKey,
|
|
628
|
+
clientId: typeof record.clientId === 'string' ? record.clientId : undefined,
|
|
629
|
+
actorId: typeof record.actorId === 'string' ? record.actorId : undefined,
|
|
630
|
+
metadata: objectRecordOrUndefined(record.metadata),
|
|
631
|
+
entries: Array.isArray(record.entries)
|
|
632
|
+
? record.entries.flatMap(readPresenceEntry)
|
|
633
|
+
: undefined,
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
function readPresenceEntry(value) {
|
|
637
|
+
if (!value || typeof value !== 'object')
|
|
638
|
+
return [];
|
|
639
|
+
const record = value;
|
|
640
|
+
if (typeof record.clientId !== 'string')
|
|
641
|
+
return [];
|
|
642
|
+
if (typeof record.actorId !== 'string')
|
|
643
|
+
return [];
|
|
644
|
+
return [
|
|
645
|
+
{
|
|
646
|
+
clientId: record.clientId,
|
|
647
|
+
actorId: record.actorId,
|
|
648
|
+
joinedAt: typeof record.joinedAt === 'number' ? record.joinedAt : Date.now(),
|
|
649
|
+
metadata: objectRecordOrUndefined(record.metadata),
|
|
650
|
+
},
|
|
651
|
+
];
|
|
652
|
+
}
|
|
653
|
+
function objectRecordOrUndefined(value) {
|
|
654
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
655
|
+
return undefined;
|
|
656
|
+
}
|
|
657
|
+
return { ...value };
|
|
658
|
+
}
|
|
659
|
+
async function readRealtimeMessageBytes(data) {
|
|
660
|
+
if (data instanceof ArrayBuffer)
|
|
661
|
+
return new Uint8Array(data);
|
|
662
|
+
if (ArrayBuffer.isView(data)) {
|
|
663
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
664
|
+
}
|
|
665
|
+
if (typeof Blob !== 'undefined' && data instanceof Blob) {
|
|
666
|
+
return new Uint8Array(await data.arrayBuffer());
|
|
667
|
+
}
|
|
668
|
+
return null;
|
|
669
|
+
}
|
|
670
|
+
function isSyncPackBytes(bytes) {
|
|
671
|
+
return (bytes.byteLength >= 4 &&
|
|
672
|
+
bytes[0] === 0x53 &&
|
|
673
|
+
bytes[1] === 0x53 &&
|
|
674
|
+
bytes[2] === 0x50 &&
|
|
675
|
+
bytes[3] === 0x31);
|
|
676
|
+
}
|
|
677
|
+
//# sourceMappingURL=worker-realtime.js.map
|