Upload CloudDrop files directly to MEGA when the server offers it
CI / verify (push) Waiting to run

Files above 16 MB use CloudDrop's mega-lease protocol when the server has
it switched on: the server picks the MEGA account and upload address, the
uploader encrypts the file with MEGA's scheme in worker threads and sends
it straight to MEGA, then the server registers the finished file. Each
file stays whole in one account. Throttling, progress and cancellation
work as before; without the server feature, or with
MHU_CLOUDDROP_LEASE=0, the existing upload path is used unchanged.

A start far below this machine's earlier lease throughput is restarted on
another MEGA server when CloudDrop uploads are not just sharing a busy
line. New error messages have English translations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7HXdgTtkBuSJPpiTRCGa8
This commit is contained in:
SucukdeluxeandClaude Opus 5.5 committed 2026-11-22 05:03:51 +01:00
1 parent 8de07502db
commit 702376f80d
5 files changed
+694

No files matched your search

+66
View File
@@ -4,6 +4,8 @@ const path = require('path');
const crypto = require('crypto');
const { setTimeout: delay } = require('node:timers/promises');
const { request, Agent } = require('undici');
const { uploadViaLease } = require('./clouddrop-lease');
const { version: APP_VERSION } = require('../package.json');
const BASE_URL = 'https://clouddrop.cc';
const API_BASE = `${BASE_URL}/api/cloud`;
@@ -34,6 +36,10 @@ const JOB_POLL_MAX_INTERVAL_MS = 30_000;
const JOB_POLL_ERROR_MAX_DELAY_MS = 60_000;
const JOB_POLL_MAX_MS = 24 * 60 * 60_000;
const EXPRESS_REUPLOAD_ERROR = /^dead:(?:express_[a-z_]+|relay_chunks_missing)$/;
const LEASE_ENABLED_CACHE_MS = 5 * 60_000;
const LEASE_DISABLED_CACHE_MS = 60_000;
const LEASE_FALLBACK_CODES = new Set(['MEGA_LEASE_DISABLED']);
const leaseAvailability = new Map();
// Cap concurrent TCP connections to clouddrop.cc at 50 to stay well under
// the server's per-IP limit of 100 concurrent connections (cd_conn).
@@ -97,6 +103,62 @@ class ClouddropUploader {
this.dispatcher = options.dispatcher || clouddropAgent;
this.sleep = options.sleep || ((ms, signal) => delay(ms, undefined, { signal }));
this.now = options.now || Date.now;
this.leaseOptions = options.leaseOptions || {};
this.leaseDisabled = options.lease === false || process.env.MHU_CLOUDDROP_LEASE === '0';
}
_leaseCacheKey() {
return sha256Hex(Buffer.from(this.apiKey)).slice(0, 24);
}
async _leaseEnabled(signal) {
if (this.leaseDisabled) return false;
const key = this._leaseCacheKey();
const cached = leaseAvailability.get(key);
if (cached && cached.until > this.now()) return cached.enabled;
let enabled = false;
try {
const res = await request(`${API_BASE}/mega-lease/config`, {
method: 'GET',
dispatcher: this.dispatcher,
signal,
headers: this._headers(),
headersTimeout: 15_000,
bodyTimeout: 15_000
});
const reply = await this._readReply(res);
enabled = reply.ok && Boolean(reply.payload) && reply.payload.enabled === true;
} catch (error) {
if (signal && signal.aborted) throw error;
}
leaseAvailability.set(key, { enabled, until: this.now() + (enabled ? LEASE_ENABLED_CACHE_MS : LEASE_DISABLED_CACHE_MS) });
return enabled;
}
async _uploadLease(filePath, fileName, fileSize, progressCb, signal, throttle) {
try {
return await uploadViaLease({
apiKey: this.apiKey,
baseUrl: BASE_URL,
dispatcher: this.dispatcher,
userAgent: USER_AGENT,
filePath,
fileName,
fileSize,
progressCb,
signal,
throttle,
sleep: this.sleep,
now: this.now,
options: { appVersion: APP_VERSION, ...this.leaseOptions }
});
} catch (error) {
if (error && (LEASE_FALLBACK_CODES.has(error.code) || error.status === 404) && !error.leaseStarted) {
leaseAvailability.set(this._leaseCacheKey(), { enabled: false, until: this.now() + LEASE_DISABLED_CACHE_MS });
return null;
}
throw error;
}
}
_headers(extra) {
@@ -178,6 +240,10 @@ class ClouddropUploader {
if (fileSize <= SIMPLE_UPLOAD_LIMIT) {
return shareResult(await this._uploadSimple(filePath, fileName, fileSize, progressCb, signal, throttle));
}
if (await this._leaseEnabled(signal)) {
const fileId = await this._uploadLease(filePath, fileName, fileSize, progressCb, signal, throttle);
if (fileId) return shareResult(fileId);
}
const outcome = await this._uploadChunked(filePath, fileName, fileSize, progressCb, signal, throttle);
return typeof outcome === 'string' ? shareResult(outcome) : outcome;
}