@powersync/common 0.0.0-dev-20260630141119 → 0.0.0-dev-20260827080125
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/lib/attachments/AttachmentQueue.d.ts +97 -42
- package/lib/attachments/AttachmentQueue.js +91 -18
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/AttachmentTransportAdapter.d.ts +47 -0
- package/lib/attachments/AttachmentTransportAdapter.js +2 -0
- package/lib/attachments/AttachmentTransportAdapter.js.map +1 -0
- package/lib/attachments/BufferedAttachmentTransport.d.ts +24 -0
- package/lib/attachments/BufferedAttachmentTransport.js +32 -0
- package/lib/attachments/BufferedAttachmentTransport.js.map +1 -0
- package/lib/attachments/LocalStorageAdapter.d.ts +19 -0
- package/lib/attachments/Schema.d.ts +21 -2
- package/lib/attachments/Schema.js +14 -10
- package/lib/attachments/Schema.js.map +1 -1
- package/lib/attachments/SyncingService.d.ts +21 -8
- package/lib/attachments/SyncingService.js +59 -30
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/client/CommonPowerSyncDatabase.d.ts +14 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +11 -0
- package/lib/client/sync/CheckpointRequest.d.ts +32 -0
- package/lib/client/sync/CheckpointRequest.js +2 -0
- package/lib/client/sync/CheckpointRequest.js.map +1 -0
- package/lib/client/sync/options.d.ts +34 -0
- package/lib/client/sync/options.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +1 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/attachments/AttachmentQueue.ts +162 -63
- package/src/attachments/AttachmentTransportAdapter.ts +49 -0
- package/src/attachments/BufferedAttachmentTransport.ts +37 -0
- package/src/attachments/LocalStorageAdapter.ts +20 -0
- package/src/attachments/Schema.ts +29 -20
- package/src/attachments/SyncingService.ts +67 -34
- package/src/client/CommonPowerSyncDatabase.ts +15 -1
- package/src/client/connection/PowerSyncBackendConnector.ts +12 -0
- package/src/client/sync/CheckpointRequest.ts +31 -0
- package/src/client/sync/options.ts +35 -0
- package/src/db/crud/SyncStatus.ts +1 -1
- package/src/index.ts +9 -1
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { DifferentialWatchedQuery } from '../client/watched/processors/DifferentialQueryProcessor.js';
|
|
2
|
+
import { Mutex } from '../utils/mutex.js';
|
|
2
3
|
import { LogLevels, PowerSyncLogger } from '../utils/Logger.js';
|
|
3
4
|
import { Transaction } from '../db/DBAdapter.js';
|
|
4
5
|
import { AttachmentContext } from './AttachmentContext.js';
|
|
5
6
|
import { AttachmentErrorHandler } from './AttachmentErrorHandler.js';
|
|
6
7
|
import { AttachmentService } from './AttachmentService.js';
|
|
7
|
-
import {
|
|
8
|
+
import { AttachmentTransportAdapter } from './AttachmentTransportAdapter.js';
|
|
9
|
+
import { BufferedAttachmentTransport } from './BufferedAttachmentTransport.js';
|
|
10
|
+
import { AttachmentData, LocalStorageAdapter, StreamingLocalStorageAdapter } from './LocalStorageAdapter.js';
|
|
8
11
|
import { RemoteStorageAdapter } from './RemoteStorageAdapter.js';
|
|
9
12
|
import { ATTACHMENT_TABLE, AttachmentRecord, AttachmentState } from './Schema.js';
|
|
10
13
|
import { SyncingService } from './SyncingService.js';
|
|
@@ -12,24 +15,22 @@ import { WatchedAttachmentItem } from './WatchedAttachmentItem.js';
|
|
|
12
15
|
import { CommonPowerSyncDatabase } from '../client/CommonPowerSyncDatabase.js';
|
|
13
16
|
|
|
14
17
|
/**
|
|
15
|
-
*
|
|
18
|
+
* Fields common to every {@link AttachmentQueueOptions} variant.
|
|
16
19
|
*
|
|
17
20
|
* @experimental
|
|
18
21
|
* @alpha This is currently experimental and may change without a major version bump.
|
|
19
22
|
*/
|
|
20
|
-
export interface
|
|
23
|
+
export interface BaseAttachmentQueueOptions<TLocal extends LocalStorageAdapter = LocalStorageAdapter> {
|
|
21
24
|
/**
|
|
22
25
|
* PowerSync database instance
|
|
23
26
|
*/
|
|
24
27
|
db: CommonPowerSyncDatabase;
|
|
25
28
|
/**
|
|
26
|
-
*
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Local storage adapter for file persistence
|
|
29
|
+
* Local storage adapter for file persistence. Its type determines whether
|
|
30
|
+
* {@link AttachmentQueue.saveFileFromUri} is available (a
|
|
31
|
+
* {@link StreamingLocalStorageAdapter} enables it).
|
|
31
32
|
*/
|
|
32
|
-
localStorage:
|
|
33
|
+
localStorage: TLocal;
|
|
33
34
|
/**
|
|
34
35
|
* Callback for monitoring attachment changes in your data model
|
|
35
36
|
*/
|
|
@@ -63,6 +64,52 @@ export interface AttachmentQueueOptions {
|
|
|
63
64
|
errorHandler?: AttachmentErrorHandler;
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Configuration options for {@link AttachmentQueue}.
|
|
69
|
+
*
|
|
70
|
+
* Provide **exactly one** remote mechanism:
|
|
71
|
+
* - `remoteStorage` — a {@link RemoteStorageAdapter}, wrapped in the default
|
|
72
|
+
* default buffered transport that delegates upload/download/delete to it.
|
|
73
|
+
* - `transportAdapter` — an {@link AttachmentTransportAdapter} that owns all remote
|
|
74
|
+
* operations directly (e.g. a native file-URI implementation for buffer-free
|
|
75
|
+
* transfer of large files). No `remoteStorage` is needed in this case.
|
|
76
|
+
*
|
|
77
|
+
* Supplying both, or neither, is a type error.
|
|
78
|
+
*
|
|
79
|
+
* @experimental
|
|
80
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
81
|
+
*/
|
|
82
|
+
export type AttachmentQueueOptions<TLocal extends LocalStorageAdapter = LocalStorageAdapter> =
|
|
83
|
+
BaseAttachmentQueueOptions<TLocal> &
|
|
84
|
+
(
|
|
85
|
+
| { remoteStorage: RemoteStorageAdapter; transportAdapter?: never }
|
|
86
|
+
| { transportAdapter: AttachmentTransportAdapter; remoteStorage?: never }
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Fields shared by {@link AttachmentQueue.saveFile} and {@link AttachmentQueue.saveFileFromUri}.
|
|
91
|
+
*
|
|
92
|
+
* @alpha
|
|
93
|
+
*/
|
|
94
|
+
export interface SaveAttachmentOptions {
|
|
95
|
+
/** File extension (e.g., 'jpg', 'pdf') */
|
|
96
|
+
fileExtension: string;
|
|
97
|
+
/** MIME type of the file (e.g., 'image/jpeg') */
|
|
98
|
+
mediaType?: string;
|
|
99
|
+
/** Optional metadata to associate with the attachment */
|
|
100
|
+
metaData?: string;
|
|
101
|
+
/** Optional custom ID. If not provided, a UUID will be generated */
|
|
102
|
+
id?: string;
|
|
103
|
+
/**
|
|
104
|
+
* Optional callback to execute additional database operations within the same transaction as the
|
|
105
|
+
* attachment creation.
|
|
106
|
+
*/
|
|
107
|
+
updateHook?: (transaction: Transaction, attachment: AttachmentRecord) => Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** How the file bytes reach managed storage when creating an upload attachment. */
|
|
111
|
+
type AttachmentSource = { kind: 'data'; data: AttachmentData } | { kind: 'uri'; localUri: string };
|
|
112
|
+
|
|
66
113
|
/**
|
|
67
114
|
* AttachmentQueue manages the lifecycle and synchronization of attachments
|
|
68
115
|
* between local and remote storage.
|
|
@@ -72,7 +119,7 @@ export interface AttachmentQueueOptions {
|
|
|
72
119
|
* @experimental
|
|
73
120
|
* @alpha This is currently experimental and may change without a major version bump.
|
|
74
121
|
*/
|
|
75
|
-
export class AttachmentQueue {
|
|
122
|
+
export class AttachmentQueue<TLocal extends LocalStorageAdapter = LocalStorageAdapter> {
|
|
76
123
|
/** Timer for periodic synchronization operations */
|
|
77
124
|
private periodicSyncTimer?: ReturnType<typeof setInterval>;
|
|
78
125
|
|
|
@@ -80,10 +127,7 @@ export class AttachmentQueue {
|
|
|
80
127
|
private readonly syncingService: SyncingService;
|
|
81
128
|
|
|
82
129
|
/** Adapter for local file storage operations */
|
|
83
|
-
readonly localStorage:
|
|
84
|
-
|
|
85
|
-
/** Adapter for remote file storage operations */
|
|
86
|
-
readonly remoteStorage: RemoteStorageAdapter;
|
|
130
|
+
readonly localStorage: TLocal;
|
|
87
131
|
|
|
88
132
|
/**
|
|
89
133
|
* Callback function to watch for changes in attachment references in your data model.
|
|
@@ -134,6 +178,21 @@ export class AttachmentQueue {
|
|
|
134
178
|
|
|
135
179
|
private watchAttachmentsAbortController!: AbortController;
|
|
136
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Serializes concurrent `syncStorage()` triggers (periodic timer, watch-onDiff,
|
|
183
|
+
* status-changed). Held across the whole batch, but only contended by other
|
|
184
|
+
* sync triggers — foreground `saveFile` / `deleteFile` / watched-attachment
|
|
185
|
+
* processing don't take this lock and proceed in parallel via the
|
|
186
|
+
* `AttachmentService` mutex, which is acquired only briefly per row.
|
|
187
|
+
*/
|
|
188
|
+
private syncLoopMutex: Mutex;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Aborted by `stopSync()` to interrupt an in-flight batch within one
|
|
192
|
+
* attachment's processing time. Polled between rows by `SyncingService`.
|
|
193
|
+
*/
|
|
194
|
+
private syncAbortController?: AbortController;
|
|
195
|
+
|
|
137
196
|
/**
|
|
138
197
|
* Creates a new AttachmentQueue instance.
|
|
139
198
|
*
|
|
@@ -143,6 +202,7 @@ export class AttachmentQueue {
|
|
|
143
202
|
db,
|
|
144
203
|
localStorage,
|
|
145
204
|
remoteStorage,
|
|
205
|
+
transportAdapter,
|
|
146
206
|
watchAttachments,
|
|
147
207
|
logger,
|
|
148
208
|
tableName = ATTACHMENT_TABLE,
|
|
@@ -151,9 +211,9 @@ export class AttachmentQueue {
|
|
|
151
211
|
downloadAttachments = true,
|
|
152
212
|
archivedCacheLimit = 100,
|
|
153
213
|
errorHandler
|
|
154
|
-
}: AttachmentQueueOptions) {
|
|
214
|
+
}: AttachmentQueueOptions<TLocal>) {
|
|
155
215
|
this.db = db;
|
|
156
|
-
this.
|
|
216
|
+
this.syncLoopMutex = db.createMutex();
|
|
157
217
|
this.localStorage = localStorage;
|
|
158
218
|
this.watchAttachments = watchAttachments;
|
|
159
219
|
this.tableName = tableName;
|
|
@@ -163,10 +223,13 @@ export class AttachmentQueue {
|
|
|
163
223
|
this.downloadAttachments = downloadAttachments;
|
|
164
224
|
this.logger = logger ?? db.logger;
|
|
165
225
|
this.attachmentService = new AttachmentService(db, this.logger, tableName, archivedCacheLimit);
|
|
226
|
+
|
|
227
|
+
const transport = transportAdapter ?? new BufferedAttachmentTransport(localStorage, remoteStorage!);
|
|
228
|
+
|
|
166
229
|
this.syncingService = new SyncingService(
|
|
167
230
|
this.attachmentService,
|
|
168
231
|
localStorage,
|
|
169
|
-
|
|
232
|
+
transport,
|
|
170
233
|
this.logger,
|
|
171
234
|
errorHandler
|
|
172
235
|
);
|
|
@@ -194,6 +257,8 @@ export class AttachmentQueue {
|
|
|
194
257
|
async startSync(): Promise<void> {
|
|
195
258
|
await this.stopSync();
|
|
196
259
|
|
|
260
|
+
this.syncAbortController = new AbortController();
|
|
261
|
+
|
|
197
262
|
this.watchActiveAttachments = this.attachmentService.watchActiveAttachments({
|
|
198
263
|
throttleMs: this.syncThrottleDuration
|
|
199
264
|
});
|
|
@@ -324,24 +389,46 @@ export class AttachmentQueue {
|
|
|
324
389
|
*
|
|
325
390
|
* This is called automatically at regular intervals when sync is started,
|
|
326
391
|
* but can also be called manually to trigger an immediate sync.
|
|
392
|
+
*
|
|
393
|
+
* Concurrent invocations are serialized via `syncLoopMutex`.
|
|
327
394
|
*/
|
|
328
395
|
async syncStorage(): Promise<void> {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
396
|
+
const signal = this.syncAbortController?.signal;
|
|
397
|
+
// We have a signal from startSync() to stopSync(), so treat the absence of one like an aborted sync.
|
|
398
|
+
if (signal == null || signal?.aborted) return;
|
|
399
|
+
|
|
400
|
+
try {
|
|
401
|
+
await this.syncLoopMutex.runExclusive(async () => {
|
|
402
|
+
const activeAttachments = await this.attachmentService.withContext((ctx) => ctx.getActiveAttachments());
|
|
403
|
+
await this.localStorage.initialize();
|
|
404
|
+
|
|
405
|
+
await this.syncingService.processAttachments(activeAttachments, { signal });
|
|
406
|
+
|
|
407
|
+
if (signal.aborted) return;
|
|
408
|
+
|
|
409
|
+
await this.attachmentService.withContext((ctx) => this.syncingService.deleteArchivedAttachments(ctx));
|
|
410
|
+
}, signal);
|
|
411
|
+
} catch (error) {
|
|
412
|
+
// A queued batch's acquire rejects when `stopSync` aborts — expected, not an error.
|
|
413
|
+
if (signal.aborted) return;
|
|
414
|
+
throw error;
|
|
415
|
+
}
|
|
335
416
|
}
|
|
336
417
|
|
|
337
418
|
/**
|
|
338
419
|
* Stops the attachment synchronization process.
|
|
339
420
|
*
|
|
340
|
-
* Clears the periodic sync timer
|
|
421
|
+
* Clears the periodic sync timer, closes all active attachment watchers, and
|
|
422
|
+
* aborts any in-flight `syncStorage()` call so it exits within one
|
|
423
|
+
* attachment's processing time instead of running the batch to completion.
|
|
341
424
|
*/
|
|
342
425
|
async stopSync(): Promise<void> {
|
|
343
426
|
clearInterval(this.periodicSyncTimer);
|
|
344
427
|
this.periodicSyncTimer = undefined;
|
|
428
|
+
if (this.syncAbortController) {
|
|
429
|
+
this.syncAbortController.abort();
|
|
430
|
+
this.syncAbortController = undefined;
|
|
431
|
+
}
|
|
345
432
|
if (this.watchActiveAttachments) await this.watchActiveAttachments.close();
|
|
346
433
|
if (this.watchAttachmentsAbortController) {
|
|
347
434
|
this.watchAttachmentsAbortController.abort();
|
|
@@ -367,49 +454,30 @@ export class AttachmentQueue {
|
|
|
367
454
|
return this.attachmentService.withContext(callback);
|
|
368
455
|
}
|
|
369
456
|
/**
|
|
370
|
-
*
|
|
371
|
-
*
|
|
372
|
-
*
|
|
373
|
-
* @returns Promise resolving to the created attachment record
|
|
457
|
+
* Creates a `QUEUED_UPLOAD` attachment record, placing the file at the managed
|
|
458
|
+
* `localUri` from the given `source`, and persists the record
|
|
459
|
+
* alongside the caller's `updateHook` in a single transaction.
|
|
374
460
|
*/
|
|
375
|
-
async
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
metaData,
|
|
380
|
-
id,
|
|
381
|
-
updateHook
|
|
382
|
-
}: {
|
|
383
|
-
/**
|
|
384
|
-
* The file data as ArrayBuffer, Blob, or base64 string
|
|
385
|
-
*/
|
|
386
|
-
data: AttachmentData;
|
|
387
|
-
/**
|
|
388
|
-
* File extension (e.g., 'jpg', 'pdf')
|
|
389
|
-
*/
|
|
390
|
-
fileExtension: string;
|
|
391
|
-
/**
|
|
392
|
-
* MIME type of the file (e.g., 'image/jpeg')
|
|
393
|
-
*/
|
|
394
|
-
mediaType?: string;
|
|
395
|
-
/**
|
|
396
|
-
* Optional metadata to associate with the attachment
|
|
397
|
-
*/
|
|
398
|
-
metaData?: string;
|
|
399
|
-
/**
|
|
400
|
-
* Optional custom ID. If not provided, a UUID will be generated
|
|
401
|
-
*/
|
|
402
|
-
id?: string;
|
|
403
|
-
/**
|
|
404
|
-
* Optional callback to execute additional database operations within the same transaction as the attachment
|
|
405
|
-
* creation.
|
|
406
|
-
*/
|
|
407
|
-
updateHook?: (transaction: Transaction, attachment: AttachmentRecord) => Promise<void>;
|
|
408
|
-
}): Promise<AttachmentRecord> {
|
|
461
|
+
private async createUploadAttachment(
|
|
462
|
+
{ fileExtension, mediaType, metaData, id, updateHook }: SaveAttachmentOptions,
|
|
463
|
+
source: AttachmentSource
|
|
464
|
+
): Promise<AttachmentRecord> {
|
|
409
465
|
const resolvedId = id ?? (await this.generateAttachmentId());
|
|
410
466
|
const filename = `${resolvedId}.${fileExtension}`;
|
|
411
467
|
const localUri = this.localStorage.getLocalUri(filename);
|
|
412
|
-
|
|
468
|
+
|
|
469
|
+
let size: number;
|
|
470
|
+
if (source.kind === 'data') {
|
|
471
|
+
size = await this.localStorage.saveFile(localUri, source.data);
|
|
472
|
+
} else {
|
|
473
|
+
// saveFileFromUri is only exposed for streaming-capable local adapters; guard at
|
|
474
|
+
// runtime too for plain-JS callers.
|
|
475
|
+
const localStorage = this.localStorage as Partial<StreamingLocalStorageAdapter>;
|
|
476
|
+
if (!localStorage.moveFile) {
|
|
477
|
+
throw new Error('The configured local storage adapter does not support moveFile, required by saveFileFromUri.');
|
|
478
|
+
}
|
|
479
|
+
size = await localStorage.moveFile(source.localUri, localUri);
|
|
480
|
+
}
|
|
413
481
|
|
|
414
482
|
const attachment: AttachmentRecord = {
|
|
415
483
|
id: resolvedId,
|
|
@@ -433,6 +501,37 @@ export class AttachmentQueue {
|
|
|
433
501
|
return attachment;
|
|
434
502
|
}
|
|
435
503
|
|
|
504
|
+
/**
|
|
505
|
+
* Saves in-memory file data to local storage and queues it for upload.
|
|
506
|
+
*
|
|
507
|
+
* @param options - File data plus {@link SaveAttachmentOptions}
|
|
508
|
+
* @returns Promise resolving to the created attachment record
|
|
509
|
+
*/
|
|
510
|
+
async saveFile(options: SaveAttachmentOptions & { data: AttachmentData }): Promise<AttachmentRecord> {
|
|
511
|
+
return this.createUploadAttachment(options, { kind: 'data', data: options.data });
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Registers a file that already exists on disk and queues it for upload, moving it
|
|
516
|
+
* into managed storage without loading it into memory.
|
|
517
|
+
*
|
|
518
|
+
* Prefer this over {@link AttachmentQueue.saveFile} for large, app-originated files
|
|
519
|
+
* (recordings, videos): it avoids reading the file into an `ArrayBuffer` just to write
|
|
520
|
+
* it back to disk. Requires the local storage adapter to implement `moveFile`.
|
|
521
|
+
*
|
|
522
|
+
* Only available when the queue is configured with a {@link StreamingLocalStorageAdapter}
|
|
523
|
+
* (one that implements `moveFile`).
|
|
524
|
+
*
|
|
525
|
+
* @param options - The existing file's `localUri` plus {@link SaveAttachmentOptions}
|
|
526
|
+
* @returns Promise resolving to the created attachment record
|
|
527
|
+
*/
|
|
528
|
+
async saveFileFromUri(
|
|
529
|
+
this: AttachmentQueue<StreamingLocalStorageAdapter>,
|
|
530
|
+
options: SaveAttachmentOptions & { localUri: string }
|
|
531
|
+
): Promise<AttachmentRecord> {
|
|
532
|
+
return this.createUploadAttachment(options, { kind: 'uri', localUri: options.localUri });
|
|
533
|
+
}
|
|
534
|
+
|
|
436
535
|
async deleteFile({
|
|
437
536
|
id,
|
|
438
537
|
updateHook
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { AttachmentRecord } from './Schema.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An {@link AttachmentRecord} that is guaranteed to have a `localUri`.
|
|
5
|
+
*
|
|
6
|
+
* The syncing service assigns `localUri` before invoking a transport download,
|
|
7
|
+
* so implementations always receive both the metadata and the destination path.
|
|
8
|
+
*
|
|
9
|
+
* @alpha
|
|
10
|
+
*/
|
|
11
|
+
export type LocatedAttachmentRecord = AttachmentRecord & { localUri: string };
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* AttachmentTransportAdapter owns all remote-side operations for an attachment —
|
|
15
|
+
* transfer (upload/download) and delete — as single operations.
|
|
16
|
+
*
|
|
17
|
+
* A transport owns the entire transfer, letting implementations pick the most
|
|
18
|
+
* efficient mechanism available (buffer, stream, or a platform-native file-URI
|
|
19
|
+
* upload/download API). On platforms like React Native this allows large files to
|
|
20
|
+
* be transferred without ever materializing them in the JS heap.
|
|
21
|
+
*
|
|
22
|
+
* The default transport composes the local and remote storage adapters. Provide a custom transport (via
|
|
23
|
+
* `AttachmentQueue`'s `transportAdapter` option) to own the whole remote side; in
|
|
24
|
+
* that case a separate `remoteStorage` is not required.
|
|
25
|
+
*
|
|
26
|
+
* @experimental
|
|
27
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
28
|
+
*/
|
|
29
|
+
export interface AttachmentTransportAdapter {
|
|
30
|
+
/**
|
|
31
|
+
* Uploads the attachment's local file to remote storage.
|
|
32
|
+
* @param attachment - The attachment to upload. `localUri` points at the source file.
|
|
33
|
+
*/
|
|
34
|
+
upload(attachment: LocatedAttachmentRecord): Promise<void>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Downloads the remote file into `attachment.localUri`.
|
|
38
|
+
* @param attachment - The attachment to download. `localUri` is the destination path,
|
|
39
|
+
* assigned by the syncing service before this call.
|
|
40
|
+
*/
|
|
41
|
+
download(attachment: LocatedAttachmentRecord): Promise<void>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Deletes the attachment's file from remote storage. Local file removal is handled
|
|
45
|
+
* separately by the syncing service.
|
|
46
|
+
* @param attachment - The attachment to delete.
|
|
47
|
+
*/
|
|
48
|
+
delete(attachment: AttachmentRecord): Promise<void>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AttachmentTransportAdapter, LocatedAttachmentRecord } from './AttachmentTransportAdapter.js';
|
|
2
|
+
import { LocalStorageAdapter } from './LocalStorageAdapter.js';
|
|
3
|
+
import { RemoteStorageAdapter } from './RemoteStorageAdapter.js';
|
|
4
|
+
import { AttachmentRecord } from './Schema.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Default {@link AttachmentTransportAdapter}, composing the local and remote
|
|
8
|
+
* storage adapters.
|
|
9
|
+
*
|
|
10
|
+
* The full file body is materialized as an `ArrayBuffer` in JS memory between the
|
|
11
|
+
* two adapter calls. This is fine for small files but can cause memory pressure on
|
|
12
|
+
* large ones — environments that support native transfer should provide a custom
|
|
13
|
+
* transport instead.
|
|
14
|
+
*
|
|
15
|
+
* @experimental
|
|
16
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
17
|
+
*/
|
|
18
|
+
export class BufferedAttachmentTransport implements AttachmentTransportAdapter {
|
|
19
|
+
constructor(
|
|
20
|
+
private localStorage: LocalStorageAdapter,
|
|
21
|
+
private remoteStorage: RemoteStorageAdapter
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
async upload(attachment: LocatedAttachmentRecord): Promise<void> {
|
|
25
|
+
const fileData = await this.localStorage.readFile(attachment.localUri);
|
|
26
|
+
await this.remoteStorage.uploadFile(fileData, attachment);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async download(attachment: LocatedAttachmentRecord): Promise<void> {
|
|
30
|
+
const fileData = await this.remoteStorage.downloadFile(attachment);
|
|
31
|
+
await this.localStorage.saveFile(attachment.localUri, fileData);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async delete(attachment: AttachmentRecord): Promise<void> {
|
|
35
|
+
await this.remoteStorage.deleteFile(attachment);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -76,3 +76,23 @@ export interface LocalStorageAdapter {
|
|
|
76
76
|
*/
|
|
77
77
|
getLocalUri(filename: string): string;
|
|
78
78
|
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A {@link LocalStorageAdapter} that can relocate a file into managed storage without
|
|
82
|
+
* loading it into memory. Required for {@link AttachmentQueue.saveFileFromUri}; only
|
|
83
|
+
* queues configured with a streaming-capable local adapter expose that method.
|
|
84
|
+
*
|
|
85
|
+
* @experimental
|
|
86
|
+
* @alpha This is currently experimental and may change without a major version bump.
|
|
87
|
+
*/
|
|
88
|
+
export interface StreamingLocalStorageAdapter extends LocalStorageAdapter {
|
|
89
|
+
/**
|
|
90
|
+
* Moves a file into managed storage without loading it into memory.
|
|
91
|
+
* Overwrites any existing file at the target. When source and target are the same
|
|
92
|
+
* path, this is a no-op that just reports the size.
|
|
93
|
+
* @param sourceUri - Path of the existing file
|
|
94
|
+
* @param targetUri - Destination path within managed storage
|
|
95
|
+
* @returns Number of bytes in the moved file
|
|
96
|
+
*/
|
|
97
|
+
moveFile(sourceUri: string, targetUri: string): Promise<number>;
|
|
98
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { column } from '../db/schema/Column.js';
|
|
2
|
-
import { Table } from '../db/schema/Table.js';
|
|
2
|
+
import { RowType, Table } from '../db/schema/Table.js';
|
|
3
3
|
import { TableOptions } from '../db/schema/Table.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -66,30 +66,39 @@ export enum AttachmentState {
|
|
|
66
66
|
*/
|
|
67
67
|
export interface AttachmentTableOptions extends Omit<TableOptions, 'name' | 'columns'> {}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* @alpha
|
|
71
|
+
*/
|
|
72
|
+
export const ATTACHMENT_TABLE_COLUMNS = {
|
|
73
|
+
filename: column.text,
|
|
74
|
+
local_uri: column.text,
|
|
75
|
+
timestamp: column.integer,
|
|
76
|
+
size: column.integer,
|
|
77
|
+
media_type: column.text,
|
|
78
|
+
state: column.integer, // Corresponds to AttachmentState
|
|
79
|
+
has_synced: column.integer,
|
|
80
|
+
meta_data: column.text
|
|
81
|
+
};
|
|
82
|
+
|
|
69
83
|
/**
|
|
70
84
|
* AttachmentTable defines the schema for the attachment queue table.
|
|
71
85
|
*
|
|
72
86
|
* @alpha
|
|
73
87
|
*/
|
|
74
|
-
export class AttachmentTable extends Table {
|
|
88
|
+
export class AttachmentTable extends Table<typeof ATTACHMENT_TABLE_COLUMNS> {
|
|
75
89
|
constructor(options?: AttachmentTableOptions) {
|
|
76
|
-
super(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
media_type: column.text,
|
|
83
|
-
state: column.integer, // Corresponds to AttachmentState
|
|
84
|
-
has_synced: column.integer,
|
|
85
|
-
meta_data: column.text
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
...options,
|
|
89
|
-
viewName: options?.viewName ?? ATTACHMENT_TABLE,
|
|
90
|
-
localOnly: true,
|
|
91
|
-
insertOnly: false
|
|
92
|
-
}
|
|
93
|
-
);
|
|
90
|
+
super(ATTACHMENT_TABLE_COLUMNS, {
|
|
91
|
+
...options,
|
|
92
|
+
viewName: options?.viewName ?? ATTACHMENT_TABLE,
|
|
93
|
+
localOnly: true,
|
|
94
|
+
insertOnly: false
|
|
95
|
+
});
|
|
94
96
|
}
|
|
95
97
|
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* AttachmentTableRecord represents the row type of the attachment table.
|
|
101
|
+
*
|
|
102
|
+
* @alpha
|
|
103
|
+
*/
|
|
104
|
+
export type AttachmentTableRecord = RowType<AttachmentTable>;
|