@powersync/common 1.40.0 → 1.41.1

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.
Files changed (183) hide show
  1. package/dist/bundle.cjs +10809 -22
  2. package/dist/bundle.cjs.map +1 -0
  3. package/dist/bundle.mjs +10730 -22
  4. package/dist/bundle.mjs.map +1 -0
  5. package/dist/bundle.node.cjs +10809 -0
  6. package/dist/bundle.node.cjs.map +1 -0
  7. package/dist/bundle.node.mjs +10730 -0
  8. package/dist/bundle.node.mjs.map +1 -0
  9. package/dist/index.d.cts +5 -1
  10. package/lib/client/AbstractPowerSyncDatabase.js +1 -0
  11. package/lib/client/AbstractPowerSyncDatabase.js.map +1 -0
  12. package/lib/client/AbstractPowerSyncOpenFactory.js +1 -0
  13. package/lib/client/AbstractPowerSyncOpenFactory.js.map +1 -0
  14. package/lib/client/ConnectionManager.js +1 -0
  15. package/lib/client/ConnectionManager.js.map +1 -0
  16. package/lib/client/CustomQuery.js +1 -0
  17. package/lib/client/CustomQuery.js.map +1 -0
  18. package/lib/client/Query.js +1 -0
  19. package/lib/client/Query.js.map +1 -0
  20. package/lib/client/SQLOpenFactory.js +1 -0
  21. package/lib/client/SQLOpenFactory.js.map +1 -0
  22. package/lib/client/compilableQueryWatch.js +1 -0
  23. package/lib/client/compilableQueryWatch.js.map +1 -0
  24. package/lib/client/connection/PowerSyncBackendConnector.js +1 -0
  25. package/lib/client/connection/PowerSyncBackendConnector.js.map +1 -0
  26. package/lib/client/connection/PowerSyncCredentials.js +1 -0
  27. package/lib/client/connection/PowerSyncCredentials.js.map +1 -0
  28. package/lib/client/constants.js +1 -0
  29. package/lib/client/constants.js.map +1 -0
  30. package/lib/client/runOnSchemaChange.js +1 -0
  31. package/lib/client/runOnSchemaChange.js.map +1 -0
  32. package/lib/client/sync/bucket/BucketStorageAdapter.js +1 -0
  33. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -0
  34. package/lib/client/sync/bucket/CrudBatch.js +1 -0
  35. package/lib/client/sync/bucket/CrudBatch.js.map +1 -0
  36. package/lib/client/sync/bucket/CrudEntry.js +1 -0
  37. package/lib/client/sync/bucket/CrudEntry.js.map +1 -0
  38. package/lib/client/sync/bucket/CrudTransaction.js +1 -0
  39. package/lib/client/sync/bucket/CrudTransaction.js.map +1 -0
  40. package/lib/client/sync/bucket/OpType.js +1 -0
  41. package/lib/client/sync/bucket/OpType.js.map +1 -0
  42. package/lib/client/sync/bucket/OplogEntry.js +1 -0
  43. package/lib/client/sync/bucket/OplogEntry.js.map +1 -0
  44. package/lib/client/sync/bucket/SqliteBucketStorage.js +1 -0
  45. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -0
  46. package/lib/client/sync/bucket/SyncDataBatch.js +1 -0
  47. package/lib/client/sync/bucket/SyncDataBatch.js.map +1 -0
  48. package/lib/client/sync/bucket/SyncDataBucket.js +1 -0
  49. package/lib/client/sync/bucket/SyncDataBucket.js.map +1 -0
  50. package/lib/client/sync/stream/AbstractRemote.d.ts +5 -0
  51. package/lib/client/sync/stream/AbstractRemote.js +9 -2
  52. package/lib/client/sync/stream/AbstractRemote.js.map +1 -0
  53. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +1 -0
  54. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -0
  55. package/lib/client/sync/stream/WebsocketClientTransport.js +1 -0
  56. package/lib/client/sync/stream/WebsocketClientTransport.js.map +1 -0
  57. package/lib/client/sync/stream/core-instruction.js +1 -0
  58. package/lib/client/sync/stream/core-instruction.js.map +1 -0
  59. package/lib/client/sync/stream/streaming-sync-types.js +1 -0
  60. package/lib/client/sync/stream/streaming-sync-types.js.map +1 -0
  61. package/lib/client/sync/sync-streams.js +1 -0
  62. package/lib/client/sync/sync-streams.js.map +1 -0
  63. package/lib/client/triggers/TriggerManager.js +1 -0
  64. package/lib/client/triggers/TriggerManager.js.map +1 -0
  65. package/lib/client/triggers/TriggerManagerImpl.js +1 -0
  66. package/lib/client/triggers/TriggerManagerImpl.js.map +1 -0
  67. package/lib/client/triggers/sanitizeSQL.js +1 -0
  68. package/lib/client/triggers/sanitizeSQL.js.map +1 -0
  69. package/lib/client/watched/GetAllQuery.js +1 -0
  70. package/lib/client/watched/GetAllQuery.js.map +1 -0
  71. package/lib/client/watched/WatchedQuery.js +1 -0
  72. package/lib/client/watched/WatchedQuery.js.map +1 -0
  73. package/lib/client/watched/processors/AbstractQueryProcessor.js +1 -0
  74. package/lib/client/watched/processors/AbstractQueryProcessor.js.map +1 -0
  75. package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -0
  76. package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -0
  77. package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -0
  78. package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -0
  79. package/lib/client/watched/processors/comparators.js +1 -0
  80. package/lib/client/watched/processors/comparators.js.map +1 -0
  81. package/lib/db/DBAdapter.js +1 -0
  82. package/lib/db/DBAdapter.js.map +1 -0
  83. package/lib/db/crud/SyncProgress.js +1 -0
  84. package/lib/db/crud/SyncProgress.js.map +1 -0
  85. package/lib/db/crud/SyncStatus.js +1 -0
  86. package/lib/db/crud/SyncStatus.js.map +1 -0
  87. package/lib/db/crud/UploadQueueStatus.js +1 -0
  88. package/lib/db/crud/UploadQueueStatus.js.map +1 -0
  89. package/lib/db/schema/Column.js +1 -0
  90. package/lib/db/schema/Column.js.map +1 -0
  91. package/lib/db/schema/Index.js +1 -0
  92. package/lib/db/schema/Index.js.map +1 -0
  93. package/lib/db/schema/IndexedColumn.js +1 -0
  94. package/lib/db/schema/IndexedColumn.js.map +1 -0
  95. package/lib/db/schema/RawTable.js +1 -0
  96. package/lib/db/schema/RawTable.js.map +1 -0
  97. package/lib/db/schema/Schema.d.ts +0 -1
  98. package/lib/db/schema/Schema.js +4 -8
  99. package/lib/db/schema/Schema.js.map +1 -0
  100. package/lib/db/schema/Table.js +1 -0
  101. package/lib/db/schema/Table.js.map +1 -0
  102. package/lib/db/schema/TableV2.js +1 -0
  103. package/lib/db/schema/TableV2.js.map +1 -0
  104. package/lib/index.js +1 -0
  105. package/lib/index.js.map +1 -0
  106. package/lib/types/types.js +1 -0
  107. package/lib/types/types.js.map +1 -0
  108. package/lib/utils/AbortOperation.js +1 -0
  109. package/lib/utils/AbortOperation.js.map +1 -0
  110. package/lib/utils/BaseObserver.js +1 -0
  111. package/lib/utils/BaseObserver.js.map +1 -0
  112. package/lib/utils/ControlledExecutor.js +1 -0
  113. package/lib/utils/ControlledExecutor.js.map +1 -0
  114. package/lib/utils/DataStream.js +1 -0
  115. package/lib/utils/DataStream.js.map +1 -0
  116. package/lib/utils/Logger.js +1 -0
  117. package/lib/utils/Logger.js.map +1 -0
  118. package/lib/utils/MetaBaseObserver.js +1 -0
  119. package/lib/utils/MetaBaseObserver.js.map +1 -0
  120. package/lib/utils/async.js +1 -0
  121. package/lib/utils/async.js.map +1 -0
  122. package/lib/utils/mutex.js +1 -0
  123. package/lib/utils/mutex.js.map +1 -0
  124. package/lib/utils/parseQuery.js +1 -0
  125. package/lib/utils/parseQuery.js.map +1 -0
  126. package/package.json +23 -15
  127. package/src/client/AbstractPowerSyncDatabase.ts +1343 -0
  128. package/src/client/AbstractPowerSyncOpenFactory.ts +39 -0
  129. package/src/client/ConnectionManager.ts +402 -0
  130. package/src/client/CustomQuery.ts +56 -0
  131. package/src/client/Query.ts +106 -0
  132. package/src/client/SQLOpenFactory.ts +55 -0
  133. package/src/client/compilableQueryWatch.ts +55 -0
  134. package/src/client/connection/PowerSyncBackendConnector.ts +25 -0
  135. package/src/client/connection/PowerSyncCredentials.ts +5 -0
  136. package/src/client/constants.ts +1 -0
  137. package/src/client/runOnSchemaChange.ts +31 -0
  138. package/src/client/sync/bucket/BucketStorageAdapter.ts +118 -0
  139. package/src/client/sync/bucket/CrudBatch.ts +21 -0
  140. package/src/client/sync/bucket/CrudEntry.ts +172 -0
  141. package/src/client/sync/bucket/CrudTransaction.ts +21 -0
  142. package/src/client/sync/bucket/OpType.ts +23 -0
  143. package/src/client/sync/bucket/OplogEntry.ts +50 -0
  144. package/src/client/sync/bucket/SqliteBucketStorage.ts +395 -0
  145. package/src/client/sync/bucket/SyncDataBatch.ts +11 -0
  146. package/src/client/sync/bucket/SyncDataBucket.ts +49 -0
  147. package/src/client/sync/stream/AbstractRemote.ts +626 -0
  148. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +1258 -0
  149. package/src/client/sync/stream/WebsocketClientTransport.ts +80 -0
  150. package/src/client/sync/stream/core-instruction.ts +99 -0
  151. package/src/client/sync/stream/streaming-sync-types.ts +205 -0
  152. package/src/client/sync/sync-streams.ts +107 -0
  153. package/src/client/triggers/TriggerManager.ts +384 -0
  154. package/src/client/triggers/TriggerManagerImpl.ts +314 -0
  155. package/src/client/triggers/sanitizeSQL.ts +66 -0
  156. package/src/client/watched/GetAllQuery.ts +46 -0
  157. package/src/client/watched/WatchedQuery.ts +121 -0
  158. package/src/client/watched/processors/AbstractQueryProcessor.ts +226 -0
  159. package/src/client/watched/processors/DifferentialQueryProcessor.ts +305 -0
  160. package/src/client/watched/processors/OnChangeQueryProcessor.ts +122 -0
  161. package/src/client/watched/processors/comparators.ts +57 -0
  162. package/src/db/DBAdapter.ts +134 -0
  163. package/src/db/crud/SyncProgress.ts +100 -0
  164. package/src/db/crud/SyncStatus.ts +308 -0
  165. package/src/db/crud/UploadQueueStatus.ts +20 -0
  166. package/src/db/schema/Column.ts +60 -0
  167. package/src/db/schema/Index.ts +39 -0
  168. package/src/db/schema/IndexedColumn.ts +42 -0
  169. package/src/db/schema/RawTable.ts +67 -0
  170. package/src/db/schema/Schema.ts +76 -0
  171. package/src/db/schema/Table.ts +359 -0
  172. package/src/db/schema/TableV2.ts +9 -0
  173. package/src/index.ts +52 -0
  174. package/src/types/types.ts +9 -0
  175. package/src/utils/AbortOperation.ts +17 -0
  176. package/src/utils/BaseObserver.ts +41 -0
  177. package/src/utils/ControlledExecutor.ts +72 -0
  178. package/src/utils/DataStream.ts +211 -0
  179. package/src/utils/Logger.ts +47 -0
  180. package/src/utils/MetaBaseObserver.ts +81 -0
  181. package/src/utils/async.ts +61 -0
  182. package/src/utils/mutex.ts +34 -0
  183. package/src/utils/parseQuery.ts +25 -0
@@ -0,0 +1,626 @@
1
+ import type { BSON } from 'bson';
2
+ import { type fetch } from 'cross-fetch';
3
+ import Logger, { ILogger } from 'js-logger';
4
+ import { RSocket, RSocketConnector, Requestable } from 'rsocket-core';
5
+ import PACKAGE from '../../../../package.json' with { type: 'json' };
6
+ import { AbortOperation } from '../../../utils/AbortOperation.js';
7
+ import { DataStream } from '../../../utils/DataStream.js';
8
+ import { PowerSyncCredentials } from '../../connection/PowerSyncCredentials.js';
9
+ import { StreamingSyncRequest } from './streaming-sync-types.js';
10
+ import { WebsocketClientTransport } from './WebsocketClientTransport.js';
11
+
12
+ export type BSONImplementation = typeof BSON;
13
+
14
+ export type RemoteConnector = {
15
+ fetchCredentials: () => Promise<PowerSyncCredentials | null>;
16
+ invalidateCredentials?: () => void;
17
+ };
18
+
19
+ const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
20
+ const POWERSYNC_JS_VERSION = PACKAGE.version;
21
+
22
+ const SYNC_QUEUE_REQUEST_LOW_WATER = 5;
23
+
24
+ // Keep alive message is sent every period
25
+ const KEEP_ALIVE_MS = 20_000;
26
+
27
+ // One message of any type must be received in this period.
28
+ const SOCKET_TIMEOUT_MS = 30_000;
29
+
30
+ // One keepalive message must be received in this period.
31
+ // If there is a backlog of messages (for example on slow connections), keepalive messages could be delayed
32
+ // significantly. Therefore this is longer than the socket timeout.
33
+ const KEEP_ALIVE_LIFETIME_MS = 90_000;
34
+
35
+ export const DEFAULT_REMOTE_LOGGER = Logger.get('PowerSyncRemote');
36
+
37
+ export type SyncStreamOptions = {
38
+ path: string;
39
+ data: StreamingSyncRequest;
40
+ headers?: Record<string, string>;
41
+ abortSignal?: AbortSignal;
42
+ fetchOptions?: Request;
43
+ };
44
+
45
+ export enum FetchStrategy {
46
+ /**
47
+ * Queues multiple sync events before processing, reducing round-trips.
48
+ * This comes at the cost of more processing overhead, which may cause ACK timeouts on older/weaker devices for big enough datasets.
49
+ */
50
+ Buffered = 'buffered',
51
+
52
+ /**
53
+ * Processes each sync event immediately before requesting the next.
54
+ * This reduces processing overhead and improves real-time responsiveness.
55
+ */
56
+ Sequential = 'sequential'
57
+ }
58
+
59
+ export type SocketSyncStreamOptions = SyncStreamOptions & {
60
+ fetchStrategy: FetchStrategy;
61
+ };
62
+
63
+ export type FetchImplementation = typeof fetch;
64
+
65
+ /**
66
+ * Class wrapper for providing a fetch implementation.
67
+ * The class wrapper is used to distinguish the fetchImplementation
68
+ * option in [AbstractRemoteOptions] from the general fetch method
69
+ * which is typeof "function"
70
+ */
71
+ export class FetchImplementationProvider {
72
+ getFetch(): FetchImplementation {
73
+ throw new Error('Unspecified fetch implementation');
74
+ }
75
+ }
76
+
77
+ export type AbstractRemoteOptions = {
78
+ /**
79
+ * Transforms the PowerSync base URL which might contain
80
+ * `http(s)://` to the corresponding WebSocket variant
81
+ * e.g. `ws(s)://`
82
+ */
83
+ socketUrlTransformer: (url: string) => string;
84
+
85
+ /**
86
+ * Optionally provide the fetch implementation to use.
87
+ * Note that this usually needs to be bound to the global scope.
88
+ * Binding should be done before passing here.
89
+ */
90
+ fetchImplementation: FetchImplementation | FetchImplementationProvider;
91
+
92
+ /**
93
+ * Optional options to pass directly to all `fetch` calls.
94
+ *
95
+ * This can include fields such as `dispatcher` (e.g. for proxy support),
96
+ * `cache`, or any other fetch-compatible options.
97
+ */
98
+ fetchOptions?: {};
99
+ };
100
+
101
+ export const DEFAULT_REMOTE_OPTIONS: AbstractRemoteOptions = {
102
+ socketUrlTransformer: (url) =>
103
+ url.replace(/^https?:\/\//, function (match) {
104
+ return match === 'https://' ? 'wss://' : 'ws://';
105
+ }),
106
+ fetchImplementation: new FetchImplementationProvider(),
107
+ fetchOptions: {}
108
+ };
109
+
110
+ export abstract class AbstractRemote {
111
+ protected credentials: PowerSyncCredentials | null = null;
112
+ protected options: AbstractRemoteOptions;
113
+
114
+ constructor(
115
+ protected connector: RemoteConnector,
116
+ protected logger: ILogger = DEFAULT_REMOTE_LOGGER,
117
+ options?: Partial<AbstractRemoteOptions>
118
+ ) {
119
+ this.options = {
120
+ ...DEFAULT_REMOTE_OPTIONS,
121
+ ...(options ?? {})
122
+ };
123
+ }
124
+
125
+ /**
126
+ * @returns a fetch implementation (function)
127
+ * which can be called to perform fetch requests
128
+ */
129
+ get fetch(): FetchImplementation {
130
+ const { fetchImplementation } = this.options;
131
+ return fetchImplementation instanceof FetchImplementationProvider
132
+ ? fetchImplementation.getFetch()
133
+ : fetchImplementation;
134
+ }
135
+
136
+ /**
137
+ * Get credentials currently cached, or fetch new credentials if none are
138
+ * available.
139
+ *
140
+ * These credentials may have expired already.
141
+ */
142
+ async getCredentials(): Promise<PowerSyncCredentials | null> {
143
+ if (this.credentials) {
144
+ return this.credentials;
145
+ }
146
+
147
+ return this.prefetchCredentials();
148
+ }
149
+
150
+ /**
151
+ * Fetch a new set of credentials and cache it.
152
+ *
153
+ * Until this call succeeds, `getCredentials` will still return the
154
+ * old credentials.
155
+ *
156
+ * This may be called before the current credentials have expired.
157
+ */
158
+ async prefetchCredentials() {
159
+ this.credentials = await this.fetchCredentials();
160
+
161
+ return this.credentials;
162
+ }
163
+
164
+ /**
165
+ * Get credentials for PowerSync.
166
+ *
167
+ * This should always fetch a fresh set of credentials - don't use cached
168
+ * values.
169
+ */
170
+ async fetchCredentials() {
171
+ const credentials = await this.connector.fetchCredentials();
172
+ if (credentials?.endpoint.match(POWERSYNC_TRAILING_SLASH_MATCH)) {
173
+ throw new Error(
174
+ `A trailing forward slash "/" was found in the fetchCredentials endpoint: "${credentials.endpoint}". Remove the trailing forward slash "/" to fix this error.`
175
+ );
176
+ }
177
+
178
+ return credentials;
179
+ }
180
+
181
+ /***
182
+ * Immediately invalidate credentials.
183
+ *
184
+ * This may be called when the current credentials have expired.
185
+ */
186
+ invalidateCredentials() {
187
+ this.credentials = null;
188
+ this.connector.invalidateCredentials?.();
189
+ }
190
+
191
+ getUserAgent() {
192
+ return `powersync-js/${POWERSYNC_JS_VERSION}`;
193
+ }
194
+
195
+ protected async buildRequest(path: string) {
196
+ const credentials = await this.getCredentials();
197
+ if (credentials != null && (credentials.endpoint == null || credentials.endpoint == '')) {
198
+ throw new Error('PowerSync endpoint not configured');
199
+ } else if (credentials?.token == null || credentials?.token == '') {
200
+ const error: any = new Error(`Not signed in`);
201
+ error.status = 401;
202
+ throw error;
203
+ }
204
+
205
+ const userAgent = this.getUserAgent();
206
+
207
+ return {
208
+ url: credentials.endpoint + path,
209
+ headers: {
210
+ 'content-type': 'application/json',
211
+ Authorization: `Token ${credentials.token}`,
212
+ 'x-user-agent': userAgent
213
+ }
214
+ };
215
+ }
216
+
217
+ async post(path: string, data: any, headers: Record<string, string> = {}): Promise<any> {
218
+ const request = await this.buildRequest(path);
219
+ const res = await this.fetch(request.url, {
220
+ method: 'POST',
221
+ headers: {
222
+ ...headers,
223
+ ...request.headers
224
+ },
225
+ body: JSON.stringify(data)
226
+ });
227
+
228
+ if (res.status === 401) {
229
+ this.invalidateCredentials();
230
+ }
231
+
232
+ if (!res.ok) {
233
+ throw new Error(`Received ${res.status} - ${res.statusText} when posting to ${path}: ${await res.text()}}`);
234
+ }
235
+
236
+ return res.json();
237
+ }
238
+
239
+ async get(path: string, headers?: Record<string, string>): Promise<any> {
240
+ const request = await this.buildRequest(path);
241
+ const res = await this.fetch(request.url, {
242
+ method: 'GET',
243
+ headers: {
244
+ ...headers,
245
+ ...request.headers
246
+ }
247
+ });
248
+
249
+ if (res.status === 401) {
250
+ this.invalidateCredentials();
251
+ }
252
+
253
+ if (!res.ok) {
254
+ throw new Error(`Received ${res.status} - ${res.statusText} when getting from ${path}: ${await res.text()}}`);
255
+ }
256
+
257
+ return res.json();
258
+ }
259
+
260
+ /**
261
+ * Provides a BSON implementation. The import nature of this varies depending on the platform
262
+ */
263
+ abstract getBSON(): Promise<BSONImplementation>;
264
+
265
+ /**
266
+ * @returns A text decoder decoding UTF-8. This is a method to allow patching it for Hermes which doesn't support the
267
+ * builtin, without forcing us to bundle a polyfill with `@powersync/common`.
268
+ */
269
+ protected createTextDecoder(): TextDecoder {
270
+ return new TextDecoder();
271
+ }
272
+
273
+ protected createSocket(url: string): WebSocket {
274
+ return new WebSocket(url);
275
+ }
276
+
277
+ /**
278
+ * Returns a data stream of sync line data.
279
+ *
280
+ * @param map Maps received payload frames to the typed event value.
281
+ * @param bson A BSON encoder and decoder. When set, the data stream will be requested with a BSON payload
282
+ * (required for compatibility with older sync services).
283
+ */
284
+ async socketStreamRaw<T>(
285
+ options: SocketSyncStreamOptions,
286
+ map: (buffer: Uint8Array) => T,
287
+ bson?: typeof BSON
288
+ ): Promise<DataStream<T>> {
289
+ const { path, fetchStrategy = FetchStrategy.Buffered } = options;
290
+ const mimeType = bson == null ? 'application/json' : 'application/bson';
291
+
292
+ function toBuffer(js: any): Buffer {
293
+ let contents: any;
294
+ if (bson != null) {
295
+ contents = bson.serialize(js);
296
+ } else {
297
+ contents = JSON.stringify(js);
298
+ }
299
+
300
+ return Buffer.from(contents);
301
+ }
302
+
303
+ const syncQueueRequestSize = fetchStrategy == FetchStrategy.Buffered ? 10 : 1;
304
+ const request = await this.buildRequest(path);
305
+
306
+ // Add the user agent in the setup payload - we can't set custom
307
+ // headers with websockets on web. The browser userAgent is however added
308
+ // automatically as a header.
309
+ const userAgent = this.getUserAgent();
310
+
311
+ const stream = new DataStream<T, Uint8Array>({
312
+ logger: this.logger,
313
+ pressure: {
314
+ lowWaterMark: SYNC_QUEUE_REQUEST_LOW_WATER
315
+ },
316
+ mapLine: map
317
+ });
318
+
319
+ // Handle upstream abort
320
+ if (options.abortSignal?.aborted) {
321
+ throw new AbortOperation('Connection request aborted');
322
+ } else {
323
+ options.abortSignal?.addEventListener(
324
+ 'abort',
325
+ () => {
326
+ stream.close();
327
+ },
328
+ { once: true }
329
+ );
330
+ }
331
+
332
+ let keepAliveTimeout: any;
333
+ const resetTimeout = () => {
334
+ clearTimeout(keepAliveTimeout);
335
+ keepAliveTimeout = setTimeout(() => {
336
+ this.logger.error(`No data received on WebSocket in ${SOCKET_TIMEOUT_MS}ms, closing connection.`);
337
+ stream.close();
338
+ }, SOCKET_TIMEOUT_MS);
339
+ };
340
+ resetTimeout();
341
+
342
+ // Typescript complains about this being `never` if it's not assigned here.
343
+ // This is assigned in `wsCreator`.
344
+ let disposeSocketConnectionTimeout = () => {};
345
+
346
+ const url = this.options.socketUrlTransformer(request.url);
347
+ const connector = new RSocketConnector({
348
+ transport: new WebsocketClientTransport({
349
+ url,
350
+ wsCreator: (url) => {
351
+ const socket = this.createSocket(url);
352
+ disposeSocketConnectionTimeout = stream.registerListener({
353
+ closed: () => {
354
+ // Allow closing the underlying WebSocket if the stream was closed before the
355
+ // RSocket connect completed. This should effectively abort the request.
356
+ socket.close();
357
+ }
358
+ });
359
+
360
+ socket.addEventListener('message', (event) => {
361
+ resetTimeout();
362
+ });
363
+
364
+ return socket;
365
+ }
366
+ }),
367
+ setup: {
368
+ keepAlive: KEEP_ALIVE_MS,
369
+ lifetime: KEEP_ALIVE_LIFETIME_MS,
370
+ dataMimeType: mimeType,
371
+ metadataMimeType: mimeType,
372
+ payload: {
373
+ data: null,
374
+ metadata: toBuffer({
375
+ token: request.headers.Authorization,
376
+ user_agent: userAgent
377
+ })
378
+ }
379
+ }
380
+ });
381
+
382
+ let rsocket: RSocket;
383
+ try {
384
+ rsocket = await connector.connect();
385
+ // The connection is established, we no longer need to monitor the initial timeout
386
+ disposeSocketConnectionTimeout();
387
+ } catch (ex) {
388
+ this.logger.error(`Failed to connect WebSocket`, ex);
389
+ clearTimeout(keepAliveTimeout);
390
+ if (!stream.closed) {
391
+ await stream.close();
392
+ }
393
+ throw ex;
394
+ }
395
+
396
+ resetTimeout();
397
+
398
+ let socketIsClosed = false;
399
+ const closeSocket = () => {
400
+ clearTimeout(keepAliveTimeout);
401
+ if (socketIsClosed) {
402
+ return;
403
+ }
404
+ socketIsClosed = true;
405
+ rsocket.close();
406
+ };
407
+ // Helps to prevent double close scenarios
408
+ rsocket.onClose(() => (socketIsClosed = true));
409
+ // We initially request this amount and expect these to arrive eventually
410
+ let pendingEventsCount = syncQueueRequestSize;
411
+
412
+ const disposeClosedListener = stream.registerListener({
413
+ closed: () => {
414
+ closeSocket();
415
+ disposeClosedListener();
416
+ }
417
+ });
418
+
419
+ const socket = await new Promise<Requestable>((resolve, reject) => {
420
+ let connectionEstablished = false;
421
+
422
+ const res = rsocket.requestStream(
423
+ {
424
+ data: toBuffer(options.data),
425
+ metadata: toBuffer({
426
+ path
427
+ })
428
+ },
429
+ syncQueueRequestSize, // The initial N amount
430
+ {
431
+ onError: (e) => {
432
+ if (e.message.includes('PSYNC_')) {
433
+ if (e.message.includes('PSYNC_S21')) {
434
+ this.invalidateCredentials();
435
+ }
436
+ } else {
437
+ // Possible that connection is with an older service, always invalidate to be safe
438
+ if (e.message !== 'Closed. ') {
439
+ this.invalidateCredentials();
440
+ }
441
+ }
442
+
443
+ // Don't log closed as an error
444
+ if (e.message !== 'Closed. ') {
445
+ this.logger.error(e);
446
+ }
447
+ // RSocket will close the RSocket stream automatically
448
+ // Close the downstream stream as well - this will close the RSocket connection and WebSocket
449
+ stream.close();
450
+ // Handles cases where the connection failed e.g. auth error or connection error
451
+ if (!connectionEstablished) {
452
+ reject(e);
453
+ }
454
+ },
455
+ onNext: (payload) => {
456
+ // The connection is active
457
+ if (!connectionEstablished) {
458
+ connectionEstablished = true;
459
+ resolve(res);
460
+ }
461
+ const { data } = payload;
462
+ // Less events are now pending
463
+ pendingEventsCount--;
464
+ if (!data) {
465
+ return;
466
+ }
467
+
468
+ stream.enqueueData(data);
469
+ },
470
+ onComplete: () => {
471
+ stream.close();
472
+ },
473
+ onExtension: () => {}
474
+ }
475
+ );
476
+ });
477
+
478
+ const l = stream.registerListener({
479
+ lowWater: async () => {
480
+ // Request to fill up the queue
481
+ const required = syncQueueRequestSize - pendingEventsCount;
482
+ if (required > 0) {
483
+ socket.request(syncQueueRequestSize - pendingEventsCount);
484
+ pendingEventsCount = syncQueueRequestSize;
485
+ }
486
+ },
487
+ closed: () => {
488
+ l();
489
+ }
490
+ });
491
+
492
+ return stream;
493
+ }
494
+
495
+ /**
496
+ * Connects to the sync/stream http endpoint, mapping and emitting each received string line.
497
+ */
498
+ async postStreamRaw<T>(options: SyncStreamOptions, mapLine: (line: string) => T): Promise<DataStream<T>> {
499
+ const { data, path, headers, abortSignal } = options;
500
+
501
+ const request = await this.buildRequest(path);
502
+
503
+ /**
504
+ * This abort controller will abort pending fetch requests.
505
+ * If the request has resolved, it will be used to close the readable stream.
506
+ * Which will cancel the network request.
507
+ *
508
+ * This nested controller is required since:
509
+ * Aborting the active fetch request while it is being consumed seems to throw
510
+ * an unhandled exception on the window level.
511
+ */
512
+ if (abortSignal?.aborted) {
513
+ throw new AbortOperation('Abort request received before making postStreamRaw request');
514
+ }
515
+
516
+ const controller = new AbortController();
517
+ let requestResolved = false;
518
+ abortSignal?.addEventListener('abort', () => {
519
+ if (!requestResolved) {
520
+ // Only abort via the abort controller if the request has not resolved yet
521
+ controller.abort(
522
+ abortSignal.reason ??
523
+ new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.')
524
+ );
525
+ }
526
+ });
527
+
528
+ const res = await this.fetch(request.url, {
529
+ method: 'POST',
530
+ headers: { ...headers, ...request.headers },
531
+ body: JSON.stringify(data),
532
+ signal: controller.signal,
533
+ cache: 'no-store',
534
+ ...(this.options.fetchOptions ?? {}),
535
+ ...options.fetchOptions
536
+ }).catch((ex) => {
537
+ if (ex.name == 'AbortError') {
538
+ throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
539
+ }
540
+ throw ex;
541
+ });
542
+
543
+ if (!res) {
544
+ throw new Error('Fetch request was aborted');
545
+ }
546
+
547
+ requestResolved = true;
548
+
549
+ if (!res.ok || !res.body) {
550
+ const text = await res.text();
551
+ this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
552
+ const error: any = new Error(`HTTP ${res.statusText}: ${text}`);
553
+ error.status = res.status;
554
+ throw error;
555
+ }
556
+
557
+ // Create a new stream splitting the response at line endings while also handling cancellations
558
+ // by closing the reader.
559
+ const reader = res.body.getReader();
560
+ // This will close the network request and read stream
561
+ const closeReader = async () => {
562
+ try {
563
+ await reader.cancel();
564
+ } catch (ex) {
565
+ // an error will throw if the reader hasn't been used yet
566
+ }
567
+ reader.releaseLock();
568
+ };
569
+
570
+ abortSignal?.addEventListener('abort', () => {
571
+ closeReader();
572
+ });
573
+
574
+ const decoder = this.createTextDecoder();
575
+ let buffer = '';
576
+
577
+ const stream = new DataStream<T, string>({
578
+ logger: this.logger,
579
+ mapLine: mapLine
580
+ });
581
+
582
+ const l = stream.registerListener({
583
+ lowWater: async () => {
584
+ try {
585
+ let didCompleteLine = false;
586
+ while (!didCompleteLine) {
587
+ const { done, value } = await reader.read();
588
+ if (done) {
589
+ const remaining = buffer.trim();
590
+ if (remaining.length != 0) {
591
+ stream.enqueueData(remaining);
592
+ }
593
+
594
+ stream.close();
595
+ await closeReader();
596
+ return;
597
+ }
598
+
599
+ const data = decoder.decode(value, { stream: true });
600
+ buffer += data;
601
+
602
+ const lines = buffer.split('\n');
603
+ for (var i = 0; i < lines.length - 1; i++) {
604
+ var l = lines[i].trim();
605
+ if (l.length > 0) {
606
+ stream.enqueueData(l);
607
+ didCompleteLine = true;
608
+ }
609
+ }
610
+
611
+ buffer = lines[lines.length - 1];
612
+ }
613
+ } catch (ex) {
614
+ stream.close();
615
+ throw ex;
616
+ }
617
+ },
618
+ closed: () => {
619
+ closeReader();
620
+ l?.();
621
+ }
622
+ });
623
+
624
+ return stream;
625
+ }
626
+ }