This commit is contained in:
1 parent
a9d1159f7e
commit
919dda6a31
4 files changed
+39
-10
No files matched your search
@@ -7,6 +7,8 @@ const { ClouddropLogApi, CloudLogError } = require('./clouddrop-log-api');
|
|||||||
const MAX_DOCUMENT_BYTES = 700000;
|
const MAX_DOCUMENT_BYTES = 700000;
|
||||||
const hash = value => createHash('sha256').update(value).digest('hex');
|
const hash = value => createHash('sha256').update(value).digest('hex');
|
||||||
const validId = value => typeof value === 'string' && /^[a-zA-Z0-9_-]{1,100}$/.test(value);
|
const validId = value => typeof value === 'string' && /^[a-zA-Z0-9_-]{1,100}$/.test(value);
|
||||||
|
const blocksRetry = code => ['INVALID_KEY', 'ACCESS_DENIED', 'UNRESTRICTED_KEY_REQUIRED',
|
||||||
|
'LOCAL_STATE_INVALID', 'REMOTE_CONTENT_CHANGED', 'REVISION_REQUIRED'].includes(code);
|
||||||
|
|
||||||
class CloudLogSync extends EventEmitter {
|
class CloudLogSync extends EventEmitter {
|
||||||
constructor({ directory, loadSettings, saveSettings, apiFactory, now = Date.now, delayMs = 750 }) {
|
constructor({ directory, loadSettings, saveSettings, apiFactory, now = Date.now, delayMs = 750 }) {
|
||||||
@@ -139,7 +141,7 @@ class CloudLogSync extends EventEmitter {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.phase = 'error';
|
this.phase = 'error';
|
||||||
this.errorCode = this._errorCode(error);
|
this.errorCode = this._errorCode(error);
|
||||||
this.retryBlocked = ['INVALID_KEY', 'ACCESS_DENIED', 'UNRESTRICTED_KEY_REQUIRED', 'LOCAL_STATE_INVALID'].includes(this.errorCode);
|
this.retryBlocked = blocksRetry(this.errorCode);
|
||||||
this.notify();
|
this.notify();
|
||||||
throw new CloudLogError(this.errorCode);
|
throw new CloudLogError(this.errorCode);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -154,9 +156,9 @@ class CloudLogSync extends EventEmitter {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.phase = 'error';
|
this.phase = 'error';
|
||||||
this.errorCode = this._errorCode(error);
|
this.errorCode = this._errorCode(error);
|
||||||
this.retryBlocked = ['INVALID_KEY', 'ACCESS_DENIED', 'UNRESTRICTED_KEY_REQUIRED', 'LOCAL_STATE_INVALID'].includes(this.errorCode);
|
this.retryBlocked = blocksRetry(this.errorCode);
|
||||||
this.notify();
|
this.notify();
|
||||||
if (!['INVALID_KEY', 'ACCESS_DENIED', 'UNRESTRICTED_KEY_REQUIRED', 'LOCAL_STATE_INVALID'].includes(this.errorCode)) {
|
if (!blocksRetry(this.errorCode)) {
|
||||||
this.timer = setTimeout(() => { this.timer = null; void this.start(); }, 30000);
|
this.timer = setTimeout(() => { this.timer = null; void this.start(); }, 30000);
|
||||||
this.timer.unref?.();
|
this.timer.unref?.();
|
||||||
}
|
}
|
||||||
@@ -271,7 +273,7 @@ class CloudLogSync extends EventEmitter {
|
|||||||
this.phase = 'error';
|
this.phase = 'error';
|
||||||
this.errorCode = this._errorCode(error);
|
this.errorCode = this._errorCode(error);
|
||||||
this.failures++;
|
this.failures++;
|
||||||
this.retryBlocked = ['INVALID_KEY', 'ACCESS_DENIED', 'UNRESTRICTED_KEY_REQUIRED'].includes(this.errorCode);
|
this.retryBlocked = blocksRetry(this.errorCode);
|
||||||
} finally {
|
} finally {
|
||||||
if (this.controller === controller) this.controller = null;
|
if (this.controller === controller) this.controller = null;
|
||||||
this.notify();
|
this.notify();
|
||||||
|
|||||||
@@ -37,15 +37,23 @@ class ClouddropLogApi {
|
|||||||
}
|
}
|
||||||
chunks.push(chunk);
|
chunks.push(chunk);
|
||||||
}
|
}
|
||||||
|
let data;
|
||||||
|
try { data = JSON.parse(Buffer.concat(chunks).toString('utf8')); }
|
||||||
|
catch {
|
||||||
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||||
|
throw new CloudLogError('INVALID_RESPONSE', response.statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||||
const code = response.statusCode === 401 ? 'INVALID_KEY'
|
const code = response.statusCode === 401 ? 'INVALID_KEY'
|
||||||
: response.statusCode === 403 ? 'ACCESS_DENIED'
|
: response.statusCode === 403 ? 'ACCESS_DENIED'
|
||||||
: response.statusCode === 429 ? 'RATE_LIMIT' : 'REMOTE_ERROR';
|
: response.statusCode === 429 ? 'RATE_LIMIT'
|
||||||
|
: response.statusCode === 428 ? 'REVISION_REQUIRED'
|
||||||
|
: response.statusCode === 423 ? 'REMOTE_FILE_LOCKED'
|
||||||
|
: response.statusCode === 409 && data?.code === 'CONTENT_CHANGED' ? 'REMOTE_CONTENT_CHANGED'
|
||||||
|
: 'REMOTE_ERROR';
|
||||||
throw new CloudLogError(code, response.statusCode);
|
throw new CloudLogError(code, response.statusCode);
|
||||||
}
|
}
|
||||||
let data;
|
|
||||||
try { data = JSON.parse(Buffer.concat(chunks).toString('utf8')); }
|
|
||||||
catch { throw new CloudLogError('INVALID_RESPONSE', response.statusCode); }
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,10 +126,23 @@ class ClouddropLogApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async writeFile(id, content, signal) {
|
async writeFile(id, content, signal) {
|
||||||
|
for (let attempt = 0; attempt < 3; attempt++) {
|
||||||
|
const current = await this.readFile(id, signal);
|
||||||
|
if (current?.writable === false) throw new CloudLogError('ACCESS_DENIED');
|
||||||
|
if (typeof current?.content !== 'string' || !/^[a-f0-9]{64}$/.test(current.revision || '')) {
|
||||||
|
throw new CloudLogError('INVALID_RESPONSE');
|
||||||
|
}
|
||||||
|
if (!content.startsWith(current.content)) throw new CloudLogError('REMOTE_CONTENT_CHANGED');
|
||||||
|
try {
|
||||||
const data = await this.call(`/files/${encodeURIComponent(id)}/content`, {
|
const data = await this.call(`/files/${encodeURIComponent(id)}/content`, {
|
||||||
method: 'PUT', signal, body: { content }
|
method: 'PUT', signal, body: { content, revision: current.revision }
|
||||||
});
|
});
|
||||||
if (data.ok !== true || data.size !== Buffer.byteLength(content)) throw new CloudLogError('INVALID_RESPONSE');
|
if (data?.ok !== true || data.size !== Buffer.byteLength(content)) throw new CloudLogError('INVALID_RESPONSE');
|
||||||
|
return;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code !== 'REMOTE_CONTENT_CHANGED' || attempt === 2) throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async readFile(id, signal) {
|
async readFile(id, signal) {
|
||||||
|
|||||||
@@ -6416,6 +6416,9 @@ const cloudLogErrors = {
|
|||||||
LOCAL_WRITE_FAILED: 'Die lokale Synchronisierungsdatei konnte nicht gespeichert werden. Bitte Speicherplatz und Zugriffsrechte prüfen.',
|
LOCAL_WRITE_FAILED: 'Die lokale Synchronisierungsdatei konnte nicht gespeichert werden. Bitte Speicherplatz und Zugriffsrechte prüfen.',
|
||||||
LOCAL_STATE_INVALID: 'Die lokale Synchronisierungsdatei konnte nicht gelesen werden.',
|
LOCAL_STATE_INVALID: 'Die lokale Synchronisierungsdatei konnte nicht gelesen werden.',
|
||||||
REMOTE_ERROR: 'Clouddrop hat die Aktualisierung abgelehnt. Ausstehende Einträge bleiben lokal gespeichert.',
|
REMOTE_ERROR: 'Clouddrop hat die Aktualisierung abgelehnt. Ausstehende Einträge bleiben lokal gespeichert.',
|
||||||
|
REVISION_REQUIRED: 'Clouddrop verlangt eine Dateirevision. Bitte den Uploader aktualisieren. Ausstehende Einträge bleiben lokal gespeichert.',
|
||||||
|
REMOTE_CONTENT_CHANGED: 'Die Cloud-Datei wurde zwischenzeitlich geändert. Sie wird nicht überschrieben; ausstehende Einträge bleiben lokal gespeichert. Bitte den Dateiinhalt prüfen.',
|
||||||
|
REMOTE_FILE_LOCKED: 'Die Cloud-Datei ist gesperrt. Die Synchronisierung wird automatisch erneut versucht; ausstehende Einträge bleiben lokal gespeichert.',
|
||||||
INVALID_RESPONSE: 'Clouddrop hat eine unerwartete Antwort geliefert.',
|
INVALID_RESPONSE: 'Clouddrop hat eine unerwartete Antwort geliefert.',
|
||||||
VERIFICATION_FAILED: 'Die Testdatei konnte nicht verifiziert werden.',
|
VERIFICATION_FAILED: 'Die Testdatei konnte nicht verifiziert werden.',
|
||||||
RESPONSE_TOO_LARGE: 'Die Clouddrop-Antwort überschreitet die zulässige Größe.',
|
RESPONSE_TOO_LARGE: 'Die Clouddrop-Antwort überschreitet die zulässige Größe.',
|
||||||
|
|||||||
@@ -42,6 +42,9 @@
|
|||||||
['Die lokale Synchronisierungsdatei konnte nicht gespeichert werden. Bitte Speicherplatz und Zugriffsrechte prüfen.', 'The local sync file could not be saved. Check disk space and access permissions.'],
|
['Die lokale Synchronisierungsdatei konnte nicht gespeichert werden. Bitte Speicherplatz und Zugriffsrechte prüfen.', 'The local sync file could not be saved. Check disk space and access permissions.'],
|
||||||
['Die lokale Synchronisierungsdatei konnte nicht gelesen werden.', 'The local sync file could not be read.'],
|
['Die lokale Synchronisierungsdatei konnte nicht gelesen werden.', 'The local sync file could not be read.'],
|
||||||
['Clouddrop hat die Aktualisierung abgelehnt. Ausstehende Einträge bleiben lokal gespeichert.', 'Clouddrop rejected the update. Pending entries remain saved locally.'],
|
['Clouddrop hat die Aktualisierung abgelehnt. Ausstehende Einträge bleiben lokal gespeichert.', 'Clouddrop rejected the update. Pending entries remain saved locally.'],
|
||||||
|
['Clouddrop verlangt eine Dateirevision. Bitte den Uploader aktualisieren. Ausstehende Einträge bleiben lokal gespeichert.', 'Clouddrop requires a file revision. Please update the uploader. Pending entries remain saved locally.'],
|
||||||
|
['Die Cloud-Datei wurde zwischenzeitlich geändert. Sie wird nicht überschrieben; ausstehende Einträge bleiben lokal gespeichert. Bitte den Dateiinhalt prüfen.', 'The cloud file has changed. It will not be overwritten; pending entries remain saved locally. Please check the file contents.'],
|
||||||
|
['Die Cloud-Datei ist gesperrt. Die Synchronisierung wird automatisch erneut versucht; ausstehende Einträge bleiben lokal gespeichert.', 'The cloud file is locked. Synchronization will retry automatically; pending entries remain saved locally.'],
|
||||||
['Clouddrop hat eine unerwartete Antwort geliefert.', 'Clouddrop returned an unexpected response.'],
|
['Clouddrop hat eine unerwartete Antwort geliefert.', 'Clouddrop returned an unexpected response.'],
|
||||||
['Die Testdatei konnte nicht verifiziert werden.', 'The test file could not be verified.'],
|
['Die Testdatei konnte nicht verifiziert werden.', 'The test file could not be verified.'],
|
||||||
['Die Clouddrop-Antwort überschreitet die zulässige Größe.', 'The Clouddrop response exceeds the allowed size.'],
|
['Die Clouddrop-Antwort überschreitet die zulässige Größe.', 'The Clouddrop response exceeds the allowed size.'],
|
||||||
|
|||||||
Reference in new issue
Block a user