-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathAbstractRemote.ts
540 lines (474 loc) · 16 KB
/
AbstractRemote.ts
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import type { BSON } from 'bson';
import { Buffer } from 'buffer';
import ndjsonStream from 'can-ndjson-stream';
import { type fetch } from 'cross-fetch';
import Logger, { ILogger } from 'js-logger';
import { RSocket, RSocketConnector, Requestable } from 'rsocket-core';
import { WebsocketClientTransport } from 'rsocket-websocket-client';
import PACKAGE from '../../../../package.json' with { type: 'json' };
import { AbortOperation } from '../../../utils/AbortOperation.js';
import { DataStream } from '../../../utils/DataStream.js';
import { PowerSyncCredentials } from '../../connection/PowerSyncCredentials.js';
import { StreamingSyncLine, StreamingSyncRequest } from './streaming-sync-types.js';
export type BSONImplementation = typeof BSON;
export type RemoteConnector = {
fetchCredentials: () => Promise<PowerSyncCredentials | null>;
};
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
const POWERSYNC_JS_VERSION = PACKAGE.version;
// Refresh at least 30 sec before it expires
const REFRESH_CREDENTIALS_SAFETY_PERIOD_MS = 30_000;
const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
// Keep alive message is sent every period
const KEEP_ALIVE_MS = 20_000;
// The ACK must be received in this period
const KEEP_ALIVE_LIFETIME_MS = 30_000;
export const DEFAULT_REMOTE_LOGGER = Logger.get('PowerSyncRemote');
export type SyncStreamOptions = {
path: string;
data: StreamingSyncRequest;
headers?: Record<string, string>;
abortSignal?: AbortSignal;
fetchOptions?: Request;
};
export enum FetchStrategy {
/**
* Queues multiple sync events before processing, reducing round-trips.
* This comes at the cost of more processing overhead, which may cause ACK timeouts on older/weaker devices for big enough datasets.
*/
Buffered = 'buffered',
/**
* Processes each sync event immediately before requesting the next.
* This reduces processing overhead and improves real-time responsiveness.
*/
Sequential = 'sequential'
}
export type SocketSyncStreamOptions = SyncStreamOptions & {
fetchStrategy: FetchStrategy;
};
export type FetchImplementation = typeof fetch;
/**
* Class wrapper for providing a fetch implementation.
* The class wrapper is used to distinguish the fetchImplementation
* option in [AbstractRemoteOptions] from the general fetch method
* which is typeof "function"
*/
export class FetchImplementationProvider {
getFetch(): FetchImplementation {
throw new Error('Unspecified fetch implementation');
}
}
export type AbstractRemoteOptions = {
/**
* Transforms the PowerSync base URL which might contain
* `http(s)://` to the corresponding WebSocket variant
* e.g. `ws(s)://`
*/
socketUrlTransformer: (url: string) => string;
/**
* Optionally provide the fetch implementation to use.
* Note that this usually needs to be bound to the global scope.
* Binding should be done before passing here.
*/
fetchImplementation: FetchImplementation | FetchImplementationProvider;
/**
* Optional options to pass directly to all `fetch` calls.
*
* This can include fields such as `dispatcher` (e.g. for proxy support),
* `cache`, or any other fetch-compatible options.
*/
fetchOptions?: {};
};
export const DEFAULT_REMOTE_OPTIONS: AbstractRemoteOptions = {
socketUrlTransformer: (url) =>
url.replace(/^https?:\/\//, function (match) {
return match === 'https://' ? 'wss://' : 'ws://';
}),
fetchImplementation: new FetchImplementationProvider(),
fetchOptions: {}
};
export abstract class AbstractRemote {
protected credentials: PowerSyncCredentials | null = null;
protected options: AbstractRemoteOptions;
constructor(
protected connector: RemoteConnector,
protected logger: ILogger = DEFAULT_REMOTE_LOGGER,
options?: Partial<AbstractRemoteOptions>
) {
this.options = {
...DEFAULT_REMOTE_OPTIONS,
...(options ?? {})
};
}
/**
* @returns a fetch implementation (function)
* which can be called to perform fetch requests
*/
get fetch(): FetchImplementation {
const { fetchImplementation } = this.options;
return fetchImplementation instanceof FetchImplementationProvider
? fetchImplementation.getFetch()
: fetchImplementation;
}
async getCredentials(): Promise<PowerSyncCredentials | null> {
const { expiresAt } = this.credentials ?? {};
if (expiresAt && expiresAt > new Date(new Date().valueOf() + REFRESH_CREDENTIALS_SAFETY_PERIOD_MS)) {
return this.credentials!;
}
this.credentials = await this.connector.fetchCredentials();
if (this.credentials?.endpoint.match(POWERSYNC_TRAILING_SLASH_MATCH)) {
throw new Error(
`A trailing forward slash "/" was found in the fetchCredentials endpoint: "${this.credentials.endpoint}". Remove the trailing forward slash "/" to fix this error.`
);
}
return this.credentials;
}
getUserAgent() {
return `powersync-js/${POWERSYNC_JS_VERSION}`;
}
protected async buildRequest(path: string) {
const credentials = await this.getCredentials();
if (credentials != null && (credentials.endpoint == null || credentials.endpoint == '')) {
throw new Error('PowerSync endpoint not configured');
} else if (credentials?.token == null || credentials?.token == '') {
const error: any = new Error(`Not signed in`);
error.status = 401;
throw error;
}
const userAgent = this.getUserAgent();
return {
url: credentials.endpoint + path,
headers: {
'content-type': 'application/json',
Authorization: `Token ${credentials.token}`,
'x-user-agent': userAgent
}
};
}
async post(path: string, data: any, headers: Record<string, string> = {}): Promise<any> {
const request = await this.buildRequest(path);
const res = await this.fetch(request.url, {
method: 'POST',
headers: {
...headers,
...request.headers
},
body: JSON.stringify(data)
});
if (!res.ok) {
throw new Error(`Received ${res.status} - ${res.statusText} when posting to ${path}: ${await res.text()}}`);
}
return res.json();
}
async get(path: string, headers?: Record<string, string>): Promise<any> {
const request = await this.buildRequest(path);
const res = await this.fetch(request.url, {
method: 'GET',
headers: {
...headers,
...request.headers
}
});
if (!res.ok) {
throw new Error(`Received ${res.status} - ${res.statusText} when getting from ${path}: ${await res.text()}}`);
}
return res.json();
}
async postStreaming(
path: string,
data: any,
headers: Record<string, string> = {},
signal?: AbortSignal
): Promise<any> {
const request = await this.buildRequest(path);
const res = await this.fetch(request.url, {
method: 'POST',
headers: { ...headers, ...request.headers },
body: JSON.stringify(data),
signal,
cache: 'no-store'
}).catch((ex) => {
this.logger.error(`Caught ex when POST streaming to ${path}`, ex);
throw ex;
});
if (!res.ok) {
const text = await res.text();
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
const error: any = new Error(`HTTP ${res.statusText}: ${text}`);
error.status = res.status;
throw error;
}
return res;
}
/**
* Provides a BSON implementation. The import nature of this varies depending on the platform
*/
abstract getBSON(): Promise<BSONImplementation>;
protected createSocket(url: string): WebSocket {
return new WebSocket(url);
}
/**
* Connects to the sync/stream websocket endpoint
*/
async socketStream(options: SocketSyncStreamOptions): Promise<DataStream<StreamingSyncLine>> {
const { path, fetchStrategy = FetchStrategy.Buffered } = options;
const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
const request = await this.buildRequest(path);
const bson = await this.getBSON();
// Add the user agent in the setup payload - we can't set custom
// headers with websockets on web. The browser userAgent is however added
// automatically as a header.
const userAgent = this.getUserAgent();
const connector = new RSocketConnector({
transport: new WebsocketClientTransport({
url: this.options.socketUrlTransformer(request.url),
wsCreator: (url) => this.createSocket(url)
}),
setup: {
keepAlive: KEEP_ALIVE_MS,
lifetime: KEEP_ALIVE_LIFETIME_MS,
dataMimeType: 'application/bson',
metadataMimeType: 'application/bson',
payload: {
data: null,
metadata: Buffer.from(
bson.serialize({
token: request.headers.Authorization,
user_agent: userAgent
})
)
}
}
});
let rsocket: RSocket;
try {
rsocket = await connector.connect();
} catch (ex) {
/**
* On React native the connection exception can be `undefined` this causes issues
* with detecting the exception inside async-mutex
*/
throw new Error(`Could not connect to PowerSync instance: ${JSON.stringify(ex)}`);
}
const stream = new DataStream({
logger: this.logger,
pressure: {
lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER
}
});
let socketIsClosed = false;
const closeSocket = () => {
if (socketIsClosed) {
return;
}
socketIsClosed = true;
rsocket.close();
};
// Helps to prevent double close scenarios
rsocket.onClose(() => (socketIsClosed = true));
// We initially request this amount and expect these to arrive eventually
let pendingEventsCount = syncQueueRequestSize;
const disposeClosedListener = stream.registerListener({
closed: () => {
closeSocket();
disposeClosedListener();
}
});
const socket = await new Promise<Requestable>((resolve, reject) => {
let connectionEstablished = false;
const res = rsocket.requestStream(
{
data: Buffer.from(bson.serialize(options.data)),
metadata: Buffer.from(
bson.serialize({
path
})
)
},
syncQueueRequestSize, // The initial N amount
{
onError: (e) => {
// Don't log closed as an error
if (e.message !== 'Closed. ') {
this.logger.error(e);
}
// RSocket will close the RSocket stream automatically
// Close the downstream stream as well - this will close the RSocket connection and WebSocket
stream.close();
// Handles cases where the connection failed e.g. auth error or connection error
if (!connectionEstablished) {
reject(e);
}
},
onNext: (payload) => {
// The connection is active
if (!connectionEstablished) {
connectionEstablished = true;
resolve(res);
}
const { data } = payload;
// Less events are now pending
pendingEventsCount--;
if (!data) {
return;
}
const deserializedData = bson.deserialize(data);
stream.enqueueData(deserializedData);
},
onComplete: () => {
stream.close();
},
onExtension: () => {}
}
);
});
const l = stream.registerListener({
lowWater: async () => {
// Request to fill up the queue
const required = syncQueueRequestSize - pendingEventsCount;
if (required > 0) {
socket.request(syncQueueRequestSize - pendingEventsCount);
pendingEventsCount = syncQueueRequestSize;
}
},
closed: () => {
l();
}
});
/**
* Handle abort operations here.
* Unfortunately cannot insert them into the connection.
*/
if (options.abortSignal?.aborted) {
stream.close();
} else {
options.abortSignal?.addEventListener('abort', () => {
stream.close();
});
}
return stream;
}
/**
* Connects to the sync/stream http endpoint
*/
async postStream(options: SyncStreamOptions): Promise<DataStream<StreamingSyncLine>> {
const { data, path, headers, abortSignal } = options;
const request = await this.buildRequest(path);
/**
* This abort controller will abort pending fetch requests.
* If the request has resolved, it will be used to close the readable stream.
* Which will cancel the network request.
*
* This nested controller is required since:
* Aborting the active fetch request while it is being consumed seems to throw
* an unhandled exception on the window level.
*/
const controller = new AbortController();
let requestResolved = false;
abortSignal?.addEventListener('abort', () => {
if (!requestResolved) {
// Only abort via the abort controller if the request has not resolved yet
controller.abort(
abortSignal.reason ??
new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.')
);
}
});
const res = await this.fetch(request.url, {
method: 'POST',
headers: { ...headers, ...request.headers },
body: JSON.stringify(data),
signal: controller.signal,
cache: 'no-store',
...(this.options.fetchOptions ?? {}),
...options.fetchOptions
}).catch((ex) => {
if (ex.name == 'AbortError') {
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
}
throw ex;
});
if (!res) {
throw new Error('Fetch request was aborted');
}
requestResolved = true;
if (!res.ok || !res.body) {
const text = await res.text();
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
const error: any = new Error(`HTTP ${res.statusText}: ${text}`);
error.status = res.status;
throw error;
}
/**
* The can-ndjson-stream does not handle aborted streams well.
* This will intercept the readable stream and close the stream if
* aborted.
*/
const reader = res.body.getReader();
// This will close the network request and read stream
const closeReader = async () => {
try {
await reader.cancel();
} catch (ex) {
// an error will throw if the reader hasn't been used yet
}
reader.releaseLock();
};
abortSignal?.addEventListener('abort', () => {
closeReader();
});
const outputStream = new ReadableStream({
start: (controller) => {
const processStream = async () => {
while (!abortSignal?.aborted) {
try {
const { done, value } = await reader.read();
// When no more data needs to be consumed, close the stream
if (done) {
break;
}
// Enqueue the next data chunk into our target stream
controller.enqueue(value);
} catch (ex) {
this.logger.error('Caught exception when reading sync stream', ex);
break;
}
}
if (!abortSignal?.aborted) {
// Close the downstream readable stream
await closeReader();
}
controller.close();
};
processStream();
}
});
const jsonS = ndjsonStream(outputStream);
const stream = new DataStream({
logger: this.logger
});
const r = jsonS.getReader();
const l = stream.registerListener({
lowWater: async () => {
try {
const { done, value } = await r.read();
// Exit if we're done
if (done) {
stream.close();
l?.();
return;
}
stream.enqueueData(value);
} catch (ex) {
stream.close();
throw ex;
}
},
closed: () => {
closeReader();
l?.();
}
});
return stream;
}
}