react-native-update-cli 2.28.0 → 2.28.1

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.
Files changed (2) hide show
  1. package/lib/api.js +97 -32
  2. package/package.json +1 -1
package/lib/api.js CHANGED
@@ -216,11 +216,12 @@ exports.get = queryWithoutBody('GET');
216
216
  exports.post = queryWithBody('POST');
217
217
  exports.put = queryWithBody('PUT');
218
218
  exports.doDelete = queryWithBody('DELETE');
219
- // Upload deadline: generous for a slow link (1 s/MB on top of a 30 s base,
220
- // never below 60 s) but bounded, so a stalled connection cannot hang a CI job
221
- // forever. The bar keeps ticking while bytes flow; the deadline is absolute.
219
+ // Upload deadline: generous for a slow link (5 s/MB, i.e. ~200 KB/s, on top
220
+ // of a 30 s base, never below 60 s) but bounded, so a stalled connection cannot
221
+ // hang a CI job forever. The bar keeps ticking while bytes flow; the deadline
222
+ // is absolute.
222
223
  const UPLOAD_TIMEOUT_BASE_MS = 30_000;
223
- const UPLOAD_TIMEOUT_PER_MB_MS = 1_000;
224
+ const UPLOAD_TIMEOUT_PER_MB_MS = 5_000;
224
225
  const UPLOAD_TIMEOUT_MIN_MS = 60_000;
225
226
  const UPLOAD_MAX_RETRIES = 1;
226
227
  function uploadTimeoutMs(fileSize) {
@@ -262,12 +263,12 @@ function loadNodeFetch() {
262
263
  return (mod.default ?? mod);
263
264
  }
264
265
  /**
265
- * Send the file with a size-scaled deadline and one retry on a transient
266
- * network error. `buildRequest` is invoked per attempt with a fresh file
267
- * stream (a consumed stream/form cannot be replayed).
266
+ * Send the file (or one slice of it) with a size-scaled deadline and retries
267
+ * on a transient network error. `buildRequest` is invoked per attempt with a
268
+ * fresh file stream (a consumed stream/form cannot be replayed).
268
269
  */
269
- async function sendUpload(fn, realUrl, fileSize, bar, buildRequest) {
270
- const timeoutMs = uploadTimeoutMs(fileSize);
270
+ async function sendUpload(fn, realUrl, fileSize, bar, buildRequest, slice = { start: 0, end: fileSize, retries: UPLOAD_MAX_RETRIES }) {
271
+ const timeoutMs = uploadTimeoutMs(slice.end - slice.start);
271
272
  const nodeFetch = loadNodeFetch();
272
273
  // HTTP(S)_PROXY / NO_PROXY, like every other request of the CLI
273
274
  const agent = (0, runtime_1.proxyAgentFor)(realUrl);
@@ -278,8 +279,14 @@ async function sendUpload(fn, realUrl, fileSize, bar, buildRequest) {
278
279
  timedOut = true;
279
280
  controller.abort();
280
281
  }, timeoutMs);
281
- const fileStream = fs_1.default.createReadStream(fn);
282
+ const fileStream = fs_1.default.createReadStream(fn, {
283
+ start: slice.start,
284
+ // an empty file has no last byte; end = start reads nothing
285
+ end: Math.max(slice.start, slice.end - 1),
286
+ });
287
+ let sent = 0;
282
288
  fileStream.on('data', (data) => {
289
+ sent += data.length;
283
290
  bar.tick(data.length);
284
291
  });
285
292
  try {
@@ -292,11 +299,11 @@ async function sendUpload(fn, realUrl, fileSize, bar, buildRequest) {
292
299
  catch (rawError) {
293
300
  fileStream.destroy();
294
301
  const error = timedOut ? new UploadTimeoutError(timeoutMs) : rawError;
295
- if (attempt < UPLOAD_MAX_RETRIES && isTransientUploadError(error)) {
302
+ if (attempt < slice.retries && isTransientUploadError(error)) {
296
303
  const reason = error instanceof Error ? error.message : String(error);
297
304
  console.warn(`\nUpload interrupted (${reason}), retrying...`);
298
- // restart the bar from zero for the second pass
299
- bar.curr = 0;
305
+ // take this attempt's bytes back off the bar before the next pass
306
+ bar.curr = Math.max(0, bar.curr - sent);
300
307
  continue;
301
308
  }
302
309
  throw error;
@@ -306,16 +313,56 @@ async function sendUpload(fn, realUrl, fileSize, bar, buildRequest) {
306
313
  }
307
314
  }
308
315
  }
316
+ /** Parts in flight at once; each is its own TCP connection. */
317
+ const CHUNK_CONCURRENCY = 4;
318
+ /** A part is small, so it can afford more retries than the whole file. */
319
+ const CHUNK_MAX_RETRIES = 2;
320
+ /** A part the store answered with an HTTP error: retrying will not help. */
321
+ class ChunkRejectedError extends Error {
322
+ }
323
+ /**
324
+ * Post the parts of a chunked upload, CHUNK_CONCURRENCY at a time. The first
325
+ * failure stops scheduling new parts; the ones in flight finish, then it is
326
+ * thrown.
327
+ */
328
+ async function sendChunks(fn, realUrl, fileSize, bar, chunked, postForm) {
329
+ let next = 0;
330
+ let failure;
331
+ const worker = async () => {
332
+ while (failure === undefined && next < chunked.parts.length) {
333
+ const index = next++;
334
+ const start = index * chunked.partSize;
335
+ const end = Math.min(fileSize, start + chunked.partSize);
336
+ try {
337
+ const res = await sendUpload(fn, realUrl, fileSize, bar, postForm(chunked.parts[index], end - start), { start, end, retries: CHUNK_MAX_RETRIES });
338
+ if (res.status > 299) {
339
+ throw new ChunkRejectedError(`${res.status}: ${res.statusText || 'Upload failed'} (part ${index + 1}/${chunked.parts.length})`);
340
+ }
341
+ }
342
+ catch (error) {
343
+ failure ??= error;
344
+ }
345
+ }
346
+ };
347
+ await Promise.all(Array.from({ length: Math.min(CHUNK_CONCURRENCY, chunked.parts.length) }, worker));
348
+ if (failure !== undefined) {
349
+ throw failure;
350
+ }
351
+ }
309
352
  async function uploadFile(fn, key, appId) {
353
+ const fileSize = fs_1.default.statSync(fn).size;
310
354
  // appId 用于服务端路由:绑定了自托管节点(rnu-node)的应用,
311
- // 上传指令会指向节点或其对象存储
355
+ // 上传指令会指向节点或其对象存储。chunked + size 让支持的服务端(GCS)
356
+ // 改发分片并行上传的指令;不认识这两个字段的服务端照旧整文件上传
312
357
  const resp = await (0, exports.post)('/upload', {
313
358
  ext: path_1.default.extname(fn),
314
359
  ...(appId ? { appId: Number(appId) } : {}),
360
+ ...(key ? {} : { chunked: true, size: fileSize }),
315
361
  });
316
362
  const { url, backupUrl, formData, maxSize } = resp;
317
363
  let realUrl = url;
318
- if (backupUrl) {
364
+ // GCS hands out the same url twice: nothing to switch to, no probe needed
365
+ if (backupUrl && backupUrl !== url) {
319
366
  if (global.USE_ACC_OSS) {
320
367
  realUrl = backupUrl;
321
368
  }
@@ -332,7 +379,6 @@ async function uploadFile(fn, key, appId) {
332
379
  }
333
380
  // console.log({realUrl});
334
381
  }
335
- const fileSize = fs_1.default.statSync(fn).size;
336
382
  if (maxSize && fileSize > (0, filesize_parser_1.default)(maxSize)) {
337
383
  const readableFileSize = `${(fileSize / 1048576).toFixed(1)}m`;
338
384
  throw new Error((0, i18n_1.t)('fileSizeExceeded', {
@@ -342,7 +388,9 @@ async function uploadFile(fn, key, appId) {
342
388
  }));
343
389
  }
344
390
  // progress/form-data are only needed here; keep them off the startup path
345
- const ProgressBarImpl = require('progress');
391
+ const progressModule = require('progress');
392
+ const ProgressBarImpl = (progressModule.default ??
393
+ progressModule);
346
394
  const bar = new ProgressBarImpl(' Uploading [:bar] :percent :etas', {
347
395
  complete: '=',
348
396
  incomplete: ' ',
@@ -377,23 +425,40 @@ async function uploadFile(fn, key, appId) {
377
425
  }
378
426
  return { hash: resp.key };
379
427
  }
380
- const FormData = require('form-data');
381
- let res;
382
- try {
383
- res = await sendUpload(fn, realUrl, fileSize, bar, (fileStream) => {
384
- const form = new FormData();
385
- for (const [k, v] of Object.entries(formData)) {
386
- form.append(k, v);
387
- }
388
- if (key) {
389
- form.append('key', key);
428
+ const formDataModule = require('form-data');
429
+ const FormData = (formDataModule.default ??
430
+ formDataModule);
431
+ const postForm = (fields, byteCount) => (fileStream) => {
432
+ const form = new FormData();
433
+ for (const [k, v] of Object.entries(fields)) {
434
+ form.append(k, v);
435
+ }
436
+ // With every part's length known node-fetch sends Content-Length
437
+ // instead of a chunked body: what object stores expect, and the only
438
+ // framing that survives http-proxy-agent's rewrite of the buffered
439
+ // request head.
440
+ form.append('file', fileStream, { knownLength: byteCount });
441
+ return { method: 'POST', body: form };
442
+ };
443
+ if (resp.chunked && !key) {
444
+ try {
445
+ await sendChunks(fn, realUrl, fileSize, bar, resp.chunked, postForm);
446
+ }
447
+ catch (error) {
448
+ if (error instanceof ChunkRejectedError) {
449
+ throw createRequestError(error.message, realUrl);
390
450
  }
391
- // With every part's length known node-fetch sends Content-Length instead
392
- // of a chunked body: what object stores expect, and the only framing
393
- // that survives http-proxy-agent's rewrite of the buffered request head.
394
- form.append('file', fileStream, { knownLength: fileSize });
395
- return { method: 'POST', body: form };
451
+ return rethrowUploadError(error);
452
+ }
453
+ await (0, exports.post)('/upload/complete', {
454
+ key: resp.chunked.key,
455
+ parts: resp.chunked.parts.length,
396
456
  });
457
+ return { hash: resp.chunked.key };
458
+ }
459
+ let res;
460
+ try {
461
+ res = await sendUpload(fn, realUrl, fileSize, bar, postForm(key ? { ...formData, key } : formData, fileSize));
397
462
  }
398
463
  catch (error) {
399
464
  return rethrowUploadError(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-update-cli",
3
- "version": "2.28.0",
3
+ "version": "2.28.1",
4
4
  "description": "command line tool for react-native-update (remote updates for react native)",
5
5
  "main": "lib/exports.js",
6
6
  "types": "lib/exports.d.ts",