This commit is contained in:
1 parent
a9d1159f7e
commit
919dda6a31
4 files changed
+41
-12
No files matched your search
@@ -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