poad 2.76.4 → 2.78.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -155,8 +155,14 @@ function plan(src, dst, { prune = false } = {}) {
155
155
  * la commande, reprend là où la destination en est (clé de reprise : destination + empreinte du contenu).
156
156
  * Sinon : une requête, en flux, sous un délai d'inactivité (utils/storage.js).
157
157
  */
158
- async function copyBytes(from, to, bucket, o, { onResume, sleep } = {}) {
158
+ async function copyBytes(from, to, bucket, o, { onResume, sleep, onBytes } = {}) {
159
159
  const t = require('./storage-tus');
160
+ // Octets de ce fichier déjà sur la cible (envoyés, ou trouvés là à la reprise) : onBytes(écart) à chaque avancée
161
+ let sent = 0;
162
+ const reach = (n) => {
163
+ if (onBytes && n > sent) onBytes(n - sent);
164
+ if (n > sent) sent = n;
165
+ };
160
166
  if (Number.isFinite(o.size) && o.size > t.CHUNK && to.raw && !to.noTus) {
161
167
  const type = o.mimetype || 'application/octet-stream';
162
168
  let source = await t.rangeSource(from, bucket, o.name, o.size).catch(() => null);
@@ -166,7 +172,7 @@ async function copyBytes(from, to, bucket, o, { onResume, sleep } = {}) {
166
172
  source = t.streamSource(res.body, o.size);
167
173
  }
168
174
  try {
169
- await t.tusUpload(to, source, { bucket, key: o.name, type, cacheControl: o.cacheControl || undefined, upsert: true, base: to.base, version: o.etag || o.key || null, onResume, sleep });
175
+ await t.tusUpload(to, source, { bucket, key: o.name, type, cacheControl: o.cacheControl || undefined, upsert: true, base: to.base, version: o.etag || o.key || null, onResume: (offset) => (reach(offset), onResume && onResume(offset)), onProgress: (offset) => reach(offset), sleep });
170
176
  return o.size;
171
177
  } catch (e) {
172
178
  if (e.code !== 'tus_unsupported') throw e;
@@ -175,8 +181,12 @@ async function copyBytes(from, to, bucket, o, { onResume, sleep } = {}) {
175
181
  }
176
182
  }
177
183
  const res = await from.get(bucket, o.name);
178
- await to.put(bucket, o.name, res.body, { type: o.mimetype || res.headers.get('content-type') || 'application/octet-stream', cacheControl: o.cacheControl || undefined, length: Number.isFinite(o.size) ? o.size : undefined });
179
- return o.size || Number(res.headers.get('content-length')) || 0;
184
+ // Compté au fil de la lecture (fetch lit la source au rythme où la cible reçoit)
185
+ const body = onBytes && res.body ? res.body.pipeThrough(new TransformStream({ transform: (c, ctl) => (reach(sent + c.length), ctl.enqueue(c)) })) : res.body;
186
+ await to.put(bucket, o.name, body, { type: o.mimetype || res.headers.get('content-type') || 'application/octet-stream', cacheControl: o.cacheControl || undefined, length: Number.isFinite(o.size) ? o.size : undefined });
187
+ const n = o.size || Number(res.headers.get('content-length')) || 0;
188
+ reach(n);
189
+ return n;
180
190
  }
181
191
 
182
192
  /** Une nouvelle tentative a-t-elle un sens (coupure, délai, 5xx, 429) ? Un refus (4xx) est définitif. Pure. */
@@ -294,6 +304,8 @@ async function sync(from, to, opts = {}) {
294
304
  };
295
305
  const result = { mode, reason, from: srcLoc, to: dstLoc, buckets: plans, totals, errors: [], dryRun: Boolean(opts.dryRun) };
296
306
  if (opts.dryRun) return result;
307
+ // Ce qui va partir : fichiers et octets (barre de progression de l'appelant)
308
+ if (opts.onPlan) opts.onPlan({ files: totals.added + totals.updated, bytes: mode === 'bytes' ? plans.reduce((n, p) => n + [...p.add, ...p.update].reduce((m, o) => m + (Number.isFinite(o.size) ? o.size : 0), 0), 0) : 0, mode });
297
309
  if (opts.confirm && (totals.updated || totals.removed)) await opts.confirm(totals);
298
310
 
299
311
  // Références : la source note aussi que son lieu sert à un autre environnement du projet ; ses
@@ -350,8 +362,18 @@ async function sync(from, to, opts = {}) {
350
362
  const errs = await adaptive(
351
363
  work,
352
364
  async ([action, o]) => {
353
- // Lu après l'attente : « totals.bytes += await … » perdrait les transferts parallèles
354
- const n = await copyBytes(from, to, p.bucket, o, { sleep: opts.sleep, onResume: (offset) => offset > 0 && event({ bucket: p.bucket, name: o.name, action: 'resume', offset, size: o.size }) });
365
+ event({ bucket: p.bucket, name: o.name, action: 'start', size: o.size });
366
+ // Octets comptés pendant cet essai : retirés s'il échoue (l'essai suivant les recompte)
367
+ let counted = 0;
368
+ const onBytes = opts.onBytes ? (d) => ((counted += d), opts.onBytes(d)) : undefined;
369
+ let n;
370
+ try {
371
+ // Lu après l'attente : « totals.bytes += await … » perdrait les transferts parallèles
372
+ n = await copyBytes(from, to, p.bucket, o, { sleep: opts.sleep, onBytes, onResume: (offset) => offset > 0 && event({ bucket: p.bucket, name: o.name, action: 'resume', offset, size: o.size }) });
373
+ } catch (e) {
374
+ if (counted && opts.onBytes) opts.onBytes(-counted);
375
+ throw e;
376
+ }
355
377
  totals.bytes += n;
356
378
  sent.push([action, o]);
357
379
  done(action, o);
package/utils/storage.js CHANGED
@@ -218,9 +218,25 @@ function client({ base, key, apikey, renew, fetch: doFetch = fetch, timeout = TI
218
218
  /** Réponse fetch brute, quel que soit son code (envois en morceaux, utils/storage-tus.js) */
219
219
  async function raw(method, route, { headers: extra = {}, body } = {}) {
220
220
  await fresh();
221
- const wd = watchdog(timeoutFor(body && body.length, timeout * 4));
221
+ // Morceau en mémoire (TUS, 6 Mio) : envoyé par pièces de 64 Kio, le délai repart à chaque pièce. Une
222
+ // connexion très lente qui avance n'est jamais coupée ; une connexion figée l'est après timeout × 4.
223
+ const big = Buffer.isBuffer(body) && body.length > 256 * 1024;
224
+ const wd = watchdog(big ? timeout * 4 : timeoutFor(body && body.length, timeout * 4));
225
+ let sendBody = body;
226
+ if (big) {
227
+ let at = 0;
228
+ sendBody = new ReadableStream({
229
+ pull(c) {
230
+ if (at >= body.length) return c.close();
231
+ const end = Math.min(body.length, at + 64 * 1024);
232
+ c.enqueue(new Uint8Array(body.buffer, body.byteOffset + at, end - at));
233
+ at = end;
234
+ wd.bump();
235
+ },
236
+ });
237
+ }
222
238
  try {
223
- return await doFetch(`${base}/storage/v1${route}`, { method, headers: headers({ ...(body ? { 'content-length': String(body.length) } : {}), ...extra }), body, signal: wd.signal });
239
+ return await doFetch(`${base}/storage/v1${route}`, { method, headers: headers({ ...(body ? { 'content-length': String(body.length) } : {}), ...extra }), body: sendBody, ...(big ? { duplex: 'half' } : {}), signal: wd.signal });
224
240
  } catch (err) {
225
241
  throw failed(err, wd);
226
242
  } finally {