UNPKG

@bitblit/ratchet-common

Version:

Common tools for general use

94 lines 3.73 kB
import { FileTransferResultType } from './file-transfer-result-type.js'; import { Logger } from '../../logger/logger.js'; import { RequireRatchet } from '../../lang/require-ratchet.js'; import { WebStreamRatchet } from '../../lang/web-stream-ratchet.js'; export class RemoteFileTracker { opts; _remoteStatusData; constructor(opts) { this.opts = opts; RequireRatchet.notNullOrUndefined(opts, 'opts'); RequireRatchet.notNullOrUndefined(opts.provider, 'opts.provider'); RequireRatchet.notNullOrUndefined(opts.key, 'opts.key'); } get remoteStatusData() { return Object.assign({}, this._remoteStatusData); } async sync() { this._remoteStatusData = await this.opts.provider.readRemoteStatus(this.opts.key); return this._remoteStatusData; } async pullRemoteData(ifNewerThan) { const rval = await this.opts.provider.pullRemoteData(this.opts.key, ifNewerThan); if (rval) { this._remoteStatusData = rval.status; } return rval; } static async dataAsString(data) { let rval = null; if (data?.content) { rval = await WebStreamRatchet.webReadableStreamToString(data.content); } return rval; } static async dataAsUint8Array(data) { let rval = null; if (data?.content) { rval = await WebStreamRatchet.webReadableStreamToUint8Array(data.content); } else { Logger.warn('Was unable to read as array since content is missing'); } return rval; } static async dataAsObject(data) { let rval = null; if (data?.content) { const txt = await this.dataAsString(data); rval = JSON.parse(txt); } return rval; } async pushStreamToRemote(src, inPushOpts) { RequireRatchet.notNullOrUndefined(src, 'src'); const pushOpts = Object.assign({ force: false, backup: false }, inPushOpts); const result = await this.opts.provider.sendDataToRemote(src, this.opts.key, pushOpts, this._remoteStatusData); if (result.type === FileTransferResultType.Updated) { Logger.info('Sent %d bytes to remote, re-reading sync'); await this.sync(); } return this._remoteStatusData; } async pushStringToRemote(src, inPushOpts) { RequireRatchet.notNullOrUndefined(src, 'src'); const asString = WebStreamRatchet.stringToWebReadableStream(src); return this.pushStreamToRemote(asString, inPushOpts); } async pushUint8ArrayToRemote(src, inPushOpts) { RequireRatchet.notNullOrUndefined(src, 'src'); const asString = WebStreamRatchet.uint8ArrayToWebReadableStream(src); return this.pushStreamToRemote(asString, inPushOpts); } async pushObjectJsonToRemote(src, inPushOpts) { RequireRatchet.notNullOrUndefined(src, 'src'); const asString = JSON.stringify(src); return this.pushStringToRemote(asString, inPushOpts); } async modifiedSinceLastSync(skipUpdateLocal) { const newData = await this.opts.provider.readRemoteStatus(this.opts.key); const rval = !RemoteFileTracker.statusMatch(newData, this._remoteStatusData); if (!skipUpdateLocal) { this._remoteStatusData = newData; } return rval; } static statusMatch(rsd1, rsd2) { return (rsd1 && rsd2 && rsd1.remoteHash === rsd2.remoteHash && rsd1.remoteSizeInBytes === rsd2.remoteSizeInBytes && rsd1.remoteLastUpdatedEpochMs === rsd2.remoteLastUpdatedEpochMs); } } //# sourceMappingURL=remote-file-tracker.js.map