import { type DocClock, type DocClocks, type DocRecord, DocStorageBase, type DocStorageOptions, type DocUpdate, } from '../../storage'; import { HttpConnection } from './http'; interface CloudDocStorageOptions extends DocStorageOptions { serverBaseUrl: string; publicRootDocId?: string; } const isShareModePrivateSystemDoc = (docId: string) => docId.startsWith('db$') || docId.startsWith('userdata$'); export class StaticCloudDocStorage extends DocStorageBase { static readonly identifier = 'StaticCloudDocStorage'; constructor(options: CloudDocStorageOptions) { super({ ...options, readonlyMode: true }); } override connection = new HttpConnection(this.options.serverBaseUrl); override async pushDocUpdate( update: DocUpdate, _origin?: string ): Promise { // http is readonly return { docId: update.docId, timestamp: new Date() }; } override async getDocTimestamp(docId: string): Promise { // http doesn't support this, so we just return a new timestamp return { docId, timestamp: new Date(), }; } override async getDocTimestamps(): Promise { // http doesn't support this return {}; } override deleteDoc(_docId: string): Promise { // http is readonly return Promise.resolve(); } protected override async getDocSnapshot( docId: string ): Promise { try { if ( this.options.publicRootDocId && docId !== this.spaceId && isShareModePrivateSystemDoc(docId) ) { return null; } const path = docId === this.spaceId && this.options.publicRootDocId ? `/api/workspaces/${this.spaceId}/public-docs/${this.options.publicRootDocId}/root-doc` : this.options.publicRootDocId ? `/api/workspaces/${this.spaceId}/public-docs/${docId}` : `/api/workspaces/${this.spaceId}/docs/${docId}`; const arrayBuffer = await this.connection.fetchArrayBuffer(path, { priority: 'high', headers: { Accept: 'application/octet-stream', // this is necessary for ios native fetch to return arraybuffer }, }); if (!arrayBuffer) { return null; } return { docId: docId, bin: new Uint8Array(arrayBuffer), timestamp: new Date(), }; } catch (error) { console.error(error); return null; } } protected override setDocSnapshot( _snapshot: DocRecord, _prevSnapshot: DocRecord | null ): Promise { // http is readonly return Promise.resolve(false); } protected override getDocUpdates(_docId: string): Promise { return Promise.resolve([]); } protected override markUpdatesMerged( _docId: string, _updates: DocRecord[] ): Promise { return Promise.resolve(0); } }