@syncular/client 0.15.45 → 0.15.46
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 +11 -0
- package/dist/http.d.ts +5 -1
- package/dist/http.js +68 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/remote.d.ts +76 -0
- package/dist/remote.js +441 -0
- package/dist/transport.d.ts +11 -0
- package/package.json +3 -3
- package/src/http.ts +99 -7
- package/src/index.ts +1 -0
- package/src/remote.ts +724 -0
- package/src/transport.ts +19 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.46",
|
|
4
4
|
"description": "Syncular TypeScript client core — offline-first sync over SQLite (WASM/OPFS, Bun, Node)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -89,7 +89,7 @@
|
|
|
89
89
|
},
|
|
90
90
|
"dependencies": {
|
|
91
91
|
"@sqlite.org/sqlite-wasm": "^3.53.0-build1",
|
|
92
|
-
"@syncular/core": "0.15.
|
|
92
|
+
"@syncular/core": "0.15.46"
|
|
93
93
|
},
|
|
94
94
|
"peerDependencies": {
|
|
95
95
|
"better-sqlite3": ">=11"
|
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
}
|
|
101
101
|
},
|
|
102
102
|
"devDependencies": {
|
|
103
|
-
"@syncular/server": "0.15.
|
|
103
|
+
"@syncular/server": "0.15.46",
|
|
104
104
|
"@types/better-sqlite3": "^7.6.13",
|
|
105
105
|
"better-sqlite3": "^12.11.1"
|
|
106
106
|
}
|
package/src/http.ts
CHANGED
|
@@ -9,6 +9,8 @@ import { SSP2_CONTENT_TYPE } from './content-type';
|
|
|
9
9
|
import { ClientSyncError } from './errors';
|
|
10
10
|
import type {
|
|
11
11
|
RealtimeConnector,
|
|
12
|
+
RemoteOperationTransport,
|
|
13
|
+
RemoteOperationRealtimeConnector,
|
|
12
14
|
SegmentDownloader,
|
|
13
15
|
SyncTransport,
|
|
14
16
|
} from './transport';
|
|
@@ -57,6 +59,78 @@ export function httpSyncTransport(
|
|
|
57
59
|
};
|
|
58
60
|
}
|
|
59
61
|
|
|
62
|
+
/** POST one registered authoritative operation to `<mount>/operations`. */
|
|
63
|
+
export function httpRemoteOperationTransport(
|
|
64
|
+
operationsUrl: string,
|
|
65
|
+
options?: HttpTransportOptions,
|
|
66
|
+
): RemoteOperationTransport {
|
|
67
|
+
const doFetch = options?.fetch ?? fetch;
|
|
68
|
+
return async (request) => {
|
|
69
|
+
const response = await doFetch(operationsUrl, {
|
|
70
|
+
method: 'POST',
|
|
71
|
+
headers: {
|
|
72
|
+
'Content-Type': 'application/vnd.syncular.operations.v1+json',
|
|
73
|
+
...options?.headers,
|
|
74
|
+
},
|
|
75
|
+
body: request.slice().buffer as ArrayBuffer,
|
|
76
|
+
});
|
|
77
|
+
if (!response.ok) await throwHttpError(response);
|
|
78
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** WebSocket connector for registered query snapshots. */
|
|
83
|
+
export function webSocketRemoteOperationConnector(
|
|
84
|
+
realtimeUrl: string,
|
|
85
|
+
): RemoteOperationRealtimeConnector {
|
|
86
|
+
return (handlers) =>
|
|
87
|
+
new Promise((resolve, reject) => {
|
|
88
|
+
const socket = new WebSocket(realtimeUrl);
|
|
89
|
+
let opened = false;
|
|
90
|
+
socket.binaryType = 'arraybuffer';
|
|
91
|
+
socket.onopen = () => {
|
|
92
|
+
opened = true;
|
|
93
|
+
resolve({
|
|
94
|
+
send: (bytes) => socket.send(bytes.slice().buffer as ArrayBuffer),
|
|
95
|
+
close: () => socket.close(),
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
socket.onmessage = (event) => {
|
|
99
|
+
if (event.data instanceof ArrayBuffer) {
|
|
100
|
+
handlers.onMessage(new Uint8Array(event.data));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
socket.onerror = () => {
|
|
104
|
+
if (!opened) {
|
|
105
|
+
reject(
|
|
106
|
+
new ClientSyncError(
|
|
107
|
+
'sync.transport_failed',
|
|
108
|
+
'remote operation realtime socket failed to connect',
|
|
109
|
+
true,
|
|
110
|
+
),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
socket.close();
|
|
115
|
+
} catch {
|
|
116
|
+
handlers.onClose?.();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
socket.onclose = () => {
|
|
120
|
+
if (!opened) {
|
|
121
|
+
reject(
|
|
122
|
+
new ClientSyncError(
|
|
123
|
+
'sync.transport_failed',
|
|
124
|
+
'remote operation realtime socket closed while connecting',
|
|
125
|
+
true,
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
handlers.onClose?.();
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
60
134
|
/**
|
|
61
135
|
* §5.5 direct endpoint with the `X-Syncular-Scopes` re-authorization
|
|
62
136
|
* header, plus the §5.4 `fetchUrl` capability (advertises accept bit 3).
|
|
@@ -224,8 +298,10 @@ export function webSocketRealtimeConnector(
|
|
|
224
298
|
return (handlers) =>
|
|
225
299
|
new Promise((resolve, reject) => {
|
|
226
300
|
const socket = new WebSocket(realtimeUrl);
|
|
301
|
+
let opened = false;
|
|
227
302
|
socket.binaryType = 'arraybuffer';
|
|
228
303
|
socket.onopen = () => {
|
|
304
|
+
opened = true;
|
|
229
305
|
resolve({
|
|
230
306
|
send: (text) => socket.send(text),
|
|
231
307
|
sendBytes: (bytes) => {
|
|
@@ -239,15 +315,31 @@ export function webSocketRealtimeConnector(
|
|
|
239
315
|
else handlers.onBinary(new Uint8Array(event.data as ArrayBuffer));
|
|
240
316
|
};
|
|
241
317
|
socket.onerror = () => {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
318
|
+
if (!opened) {
|
|
319
|
+
reject(
|
|
320
|
+
new ClientSyncError(
|
|
321
|
+
'sync.transport_failed',
|
|
322
|
+
'realtime socket failed to connect',
|
|
323
|
+
true,
|
|
324
|
+
),
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
try {
|
|
328
|
+
socket.close();
|
|
329
|
+
} catch {
|
|
330
|
+
handlers.onClose?.();
|
|
331
|
+
}
|
|
249
332
|
};
|
|
250
333
|
socket.onclose = () => {
|
|
334
|
+
if (!opened) {
|
|
335
|
+
reject(
|
|
336
|
+
new ClientSyncError(
|
|
337
|
+
'sync.transport_failed',
|
|
338
|
+
'realtime socket closed while connecting',
|
|
339
|
+
true,
|
|
340
|
+
),
|
|
341
|
+
);
|
|
342
|
+
}
|
|
251
343
|
handlers.onClose?.();
|
|
252
344
|
};
|
|
253
345
|
});
|
package/src/index.ts
CHANGED