This commit is contained in:
1 parent
a9d1159f7e
commit
919dda6a31
4 files changed
+41
-12
No files matched your search
@@ -7,6 +7,8 @@ const { ClouddropLogApi, CloudLogError } = require('./clouddrop-log-api');
|
||||
const MAX_DOCUMENT_BYTES = 700000;
|
||||
const hash = value => createHash('sha256').update(value).digest('hex');
|
||||
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 {
|
||||
constructor({ directory, loadSettings, saveSettings, apiFactory, now = Date.now, delayMs = 750 }) {
|
||||
@@ -139,7 +141,7 @@ class CloudLogSync extends EventEmitter {
|
||||
} catch (error) {
|
||||
this.phase = '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();
|
||||
throw new CloudLogError(this.errorCode);
|
||||
} finally {
|
||||
@@ -154,9 +156,9 @@ class CloudLogSync extends EventEmitter {
|
||||
} catch (error) {
|
||||
this.phase = '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();
|
||||
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.unref?.();
|
||||
}
|
||||
@@ -271,7 +273,7 @@ class CloudLogSync extends EventEmitter {
|
||||
this.phase = 'error';
|
||||
this.errorCode = this._errorCode(error);
|
||||
this.failures++;
|
||||
this.retryBlocked = ['INVALID_KEY', 'ACCESS_DENIED', 'UNRESTRICTED_KEY_REQUIRED'].includes(this.errorCode);
|
||||
this.retryBlocked = blocksRetry(this.errorCode);
|
||||
} finally {
|
||||
if (this.controller === controller) this.controller = null;
|
||||
this.notify();
|
||||
|
||||
@@ -37,15 +37,23 @@ class ClouddropLogApi {
|
||||
}
|
||||
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) {
|
||||
const code = response.statusCode === 401 ? 'INVALID_KEY'
|
||||
: 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);
|
||||
}
|
||||
let data;
|
||||
try { data = JSON.parse(Buffer.concat(chunks).toString('utf8')); }
|
||||
catch { throw new CloudLogError('INVALID_RESPONSE', response.statusCode); }
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -118,10 +126,23 @@ class ClouddropLogApi {
|
||||
}
|
||||
|
||||
async writeFile(id, content, signal) {
|
||||
const data = await this.call(`/files/${encodeURIComponent(id)}/content`, {
|
||||
method: 'PUT', signal, body: { content }
|
||||
});
|
||||
if (data.ok !== true || data.size !== Buffer.byteLength(content)) throw new CloudLogError('INVALID_RESPONSE');
|
||||
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`, {
|
||||
method: 'PUT', signal, body: { content, revision: current.revision }
|
||||
});
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user