react-native-update-cli 2.22.7 → 2.22.9
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.
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/cli.json +5 -0
- package/lib/api.d.ts +7 -0
- package/lib/api.js +106 -38
- package/lib/app.js +3 -2
- package/lib/bin.js +16 -2
- package/lib/bundle-runner.d.ts +11 -4
- package/lib/bundle-runner.js +176 -90
- package/lib/bundle.d.ts +1 -0
- package/lib/bundle.js +2 -0
- package/lib/diff.d.ts +9 -0
- package/lib/diff.js +162 -120
- package/lib/locales/en.d.ts +1 -0
- package/lib/locales/en.js +1 -0
- package/lib/locales/zh.d.ts +1 -0
- package/lib/locales/zh.js +1 -0
- package/lib/native-package.d.ts +5 -1
- package/lib/native-package.js +145 -84
- package/lib/package.d.ts +7 -3
- package/lib/package.js +22 -12
- package/lib/types.d.ts +2 -0
- package/lib/utils/dep-versions.d.ts +7 -0
- package/lib/utils/dep-versions.js +66 -31
- package/lib/utils/hbcTransform.d.ts +21 -0
- package/lib/utils/hbcTransform.js +146 -5
- package/lib/utils/hermes-base.d.ts +21 -4
- package/lib/utils/hermes-base.js +146 -37
- package/lib/utils/http-helper.d.ts +6 -1
- package/lib/utils/http-helper.js +23 -11
- package/lib/utils/index.d.ts +30 -1
- package/lib/utils/index.js +98 -24
- package/lib/utils/latest-version/index.d.ts +11 -8
- package/lib/utils/latest-version/index.js +91 -25
- package/lib/utils/zip-entries.js +20 -15
- package/lib/utils/zip-options.d.ts +2 -0
- package/lib/utils/zip-options.js +9 -4
- package/lib/utils/zip-range.d.ts +47 -15
- package/lib/utils/zip-range.js +192 -83
- package/lib/utils/zip-raw-writer.d.ts +47 -0
- package/lib/utils/zip-raw-writer.js +204 -0
- package/lib/versions.js +19 -9
- package/package.json +10 -12
- package/src/api.ts +126 -46
- package/src/app.ts +3 -2
- package/src/bin.ts +17 -2
- package/src/bundle-runner.ts +225 -113
- package/src/bundle.ts +3 -0
- package/src/diff.ts +210 -153
- package/src/locales/en.ts +2 -0
- package/src/locales/zh.ts +2 -0
- package/src/native-package.ts +186 -96
- package/src/package.ts +26 -13
- package/src/types.ts +2 -0
- package/src/utils/dep-versions.ts +76 -33
- package/src/utils/hbcTransform.ts +194 -5
- package/src/utils/hermes-base.ts +156 -45
- package/src/utils/http-helper.ts +24 -13
- package/src/utils/index.ts +136 -26
- package/src/utils/latest-version/index.ts +109 -33
- package/src/utils/zip-entries.ts +18 -16
- package/src/utils/zip-options.ts +8 -3
- package/src/utils/zip-range.ts +244 -88
- package/src/utils/zip-raw-writer.ts +247 -0
- package/src/versions.ts +17 -7
package/src/utils/zip-range.ts
CHANGED
|
@@ -179,6 +179,32 @@ export function bundleLocationFields(
|
|
|
179
179
|
// HTTP Range
|
|
180
180
|
// ---------------------------------------------------------------------------
|
|
181
181
|
|
|
182
|
+
/** Deadlines for the Range requests; every field has a default. */
|
|
183
|
+
export interface RangeOptions {
|
|
184
|
+
/** reads of at most CHUNK_BYTES (tail, headers, directory pages), default 20 s */
|
|
185
|
+
headerTimeoutMs?: number;
|
|
186
|
+
/** base deadline for larger reads (entry data, a prefetched directory), default 30 s */
|
|
187
|
+
dataTimeoutMs?: number;
|
|
188
|
+
/** added to `dataTimeoutMs` per MB requested, default 1 s */
|
|
189
|
+
dataTimeoutPerMbMs?: number;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const DEFAULT_HEADER_TIMEOUT_MS = 20_000;
|
|
193
|
+
const DEFAULT_DATA_TIMEOUT_MS = 30_000;
|
|
194
|
+
const DEFAULT_DATA_TIMEOUT_PER_MB_MS = 1_000;
|
|
195
|
+
|
|
196
|
+
/** deadline for a request of `bytes`: flat for header-sized reads, scaled otherwise */
|
|
197
|
+
function timeoutFor(bytes: number, options: RangeOptions): number {
|
|
198
|
+
if (bytes <= CHUNK_BYTES) {
|
|
199
|
+
return options.headerTimeoutMs ?? DEFAULT_HEADER_TIMEOUT_MS;
|
|
200
|
+
}
|
|
201
|
+
return (
|
|
202
|
+
(options.dataTimeoutMs ?? DEFAULT_DATA_TIMEOUT_MS) +
|
|
203
|
+
((options.dataTimeoutPerMbMs ?? DEFAULT_DATA_TIMEOUT_PER_MB_MS) * bytes) /
|
|
204
|
+
(1024 * 1024)
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
182
208
|
/** The server answered a Range request with the whole body (HTTP 200). */
|
|
183
209
|
export class RangeUnsupportedError extends Error {
|
|
184
210
|
constructor(readonly response: Response) {
|
|
@@ -201,22 +227,59 @@ export function parseContentRange(header: string | null): ContentRange | null {
|
|
|
201
227
|
return { start, end, total };
|
|
202
228
|
}
|
|
203
229
|
|
|
230
|
+
/**
|
|
231
|
+
* The validator to send as `If-Range` on later requests for the same object,
|
|
232
|
+
* so a replaced archive answers 200 (handled like a server without Range)
|
|
233
|
+
* instead of mixing bytes of two versions. Weak ETags are not allowed there.
|
|
234
|
+
*/
|
|
235
|
+
export function rangeValidator(headers: Headers): string | undefined {
|
|
236
|
+
const etag = headers.get('etag')?.trim();
|
|
237
|
+
if (etag && !/^W\//i.test(etag)) return etag;
|
|
238
|
+
return headers.get('last-modified')?.trim() || undefined;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** An aborted fetch as a plain Error (never mistaken for a 200 answer). */
|
|
242
|
+
function describeAbort(error: unknown, what: string, timeoutMs: number): Error {
|
|
243
|
+
const name = (error as { name?: string } | null)?.name;
|
|
244
|
+
if (name === 'TimeoutError' || name === 'AbortError') {
|
|
245
|
+
return new Error(`timed out after ${timeoutMs} ms fetching ${what}`);
|
|
246
|
+
}
|
|
247
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
interface RequestOptions {
|
|
251
|
+
timeoutMs: number;
|
|
252
|
+
ifRange?: string;
|
|
253
|
+
/** archive size seen by the first request; a different one is an error */
|
|
254
|
+
expectTotal?: number;
|
|
255
|
+
}
|
|
256
|
+
|
|
204
257
|
/** One Range request; `end` is exclusive. Resolves with a 206 response. */
|
|
205
258
|
async function fetchRange(
|
|
206
259
|
url: string,
|
|
207
260
|
start: number,
|
|
208
261
|
end: number,
|
|
262
|
+
options: RequestOptions,
|
|
209
263
|
): Promise<Response & { range: ContentRange }> {
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
264
|
+
const what = `bytes=${start}-${end - 1}`;
|
|
265
|
+
const headers: Record<string, string> = {
|
|
266
|
+
Range: what,
|
|
267
|
+
'Accept-Encoding': 'identity',
|
|
268
|
+
};
|
|
269
|
+
if (options.ifRange) headers['If-Range'] = options.ifRange;
|
|
270
|
+
let response: Response;
|
|
271
|
+
try {
|
|
272
|
+
response = await fetch(url, {
|
|
273
|
+
headers,
|
|
274
|
+
signal: AbortSignal.timeout(options.timeoutMs),
|
|
275
|
+
});
|
|
276
|
+
} catch (error) {
|
|
277
|
+
throw describeAbort(error, what, options.timeoutMs);
|
|
278
|
+
}
|
|
216
279
|
if (response.status === 200) throw new RangeUnsupportedError(response);
|
|
217
280
|
if (response.status !== 206 || !response.body) {
|
|
218
281
|
await response.body?.cancel().catch(() => {});
|
|
219
|
-
throw new Error(`HTTP ${response.status} for
|
|
282
|
+
throw new Error(`HTTP ${response.status} for ${what}`);
|
|
220
283
|
}
|
|
221
284
|
const range = parseContentRange(response.headers.get('content-range'));
|
|
222
285
|
if (!range || range.start !== start || range.end !== end - 1) {
|
|
@@ -225,6 +288,15 @@ async function fetchRange(
|
|
|
225
288
|
`unexpected Content-Range ${response.headers.get('content-range')}`,
|
|
226
289
|
);
|
|
227
290
|
}
|
|
291
|
+
if (
|
|
292
|
+
options.expectTotal !== undefined &&
|
|
293
|
+
range.total !== options.expectTotal
|
|
294
|
+
) {
|
|
295
|
+
await response.body.cancel().catch(() => {});
|
|
296
|
+
throw new Error(
|
|
297
|
+
`archive size changed: ${range.total} bytes, expected ${options.expectTotal}`,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
228
300
|
return Object.assign(response, { range });
|
|
229
301
|
}
|
|
230
302
|
|
|
@@ -232,9 +304,16 @@ async function fetchRangeBuffer(
|
|
|
232
304
|
url: string,
|
|
233
305
|
start: number,
|
|
234
306
|
end: number,
|
|
307
|
+
options: RequestOptions,
|
|
235
308
|
): Promise<{ data: Buffer; total: number }> {
|
|
236
|
-
const response = await fetchRange(url, start, end);
|
|
237
|
-
|
|
309
|
+
const response = await fetchRange(url, start, end, options);
|
|
310
|
+
let data: Buffer;
|
|
311
|
+
try {
|
|
312
|
+
// Buffer.from(ArrayBuffer) is a view: the body is held exactly once
|
|
313
|
+
data = Buffer.from(await response.arrayBuffer());
|
|
314
|
+
} catch (error) {
|
|
315
|
+
throw describeAbort(error, `bytes=${start}-${end - 1}`, options.timeoutMs);
|
|
316
|
+
}
|
|
238
317
|
if (data.length !== end - start) {
|
|
239
318
|
throw new Error(
|
|
240
319
|
`range returned ${data.length} bytes, expected ${end - start}`,
|
|
@@ -253,6 +332,7 @@ export async function fetchZipEntryData(
|
|
|
253
332
|
ZipEntryLocation,
|
|
254
333
|
'dataOffset' | 'compressedSize' | 'compressionMethod'
|
|
255
334
|
>,
|
|
335
|
+
options: RangeOptions = {},
|
|
256
336
|
): Promise<{ data: Buffer; fetchedBytes: number; totalBytes: number }> {
|
|
257
337
|
const { dataOffset, compressedSize, compressionMethod } = location;
|
|
258
338
|
if (compressionMethod !== ZIP_STORED && compressionMethod !== ZIP_DEFLATED) {
|
|
@@ -270,6 +350,7 @@ export async function fetchZipEntryData(
|
|
|
270
350
|
url,
|
|
271
351
|
dataOffset,
|
|
272
352
|
dataOffset + compressedSize,
|
|
353
|
+
{ timeoutMs: timeoutFor(compressedSize, options) },
|
|
273
354
|
);
|
|
274
355
|
return {
|
|
275
356
|
data: compressionMethod === ZIP_DEFLATED ? inflateRawSync(data) : data,
|
|
@@ -278,15 +359,28 @@ export async function fetchZipEntryData(
|
|
|
278
359
|
};
|
|
279
360
|
}
|
|
280
361
|
|
|
362
|
+
export interface HttpRangeReaderOptions extends RangeOptions {
|
|
363
|
+
/** validator of the archive (see `rangeValidator`), sent as If-Range */
|
|
364
|
+
ifRange?: string;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
interface Chunk {
|
|
368
|
+
start: number;
|
|
369
|
+
data: Buffer;
|
|
370
|
+
}
|
|
371
|
+
|
|
281
372
|
/**
|
|
282
373
|
* yauzl reader over HTTP Range. Header reads (EOCD, central directory, local
|
|
283
|
-
* file headers) are widened to CHUNK_BYTES and cached
|
|
284
|
-
* overlapping
|
|
285
|
-
*
|
|
286
|
-
* bytes following the local file header) before
|
|
374
|
+
* file headers) are widened to CHUNK_BYTES and cached. Chunks are kept as
|
|
375
|
+
* fetched — only overlapping ones are merged, touching ones are read across —
|
|
376
|
+
* so an entry's bytes are held once, and a read reuses whatever prefix the
|
|
377
|
+
* cache holds (typically the bytes following the local file header) before
|
|
378
|
+
* requesting the rest. Every request after the first carries If-Range and
|
|
379
|
+
* must report the same archive size.
|
|
287
380
|
*/
|
|
288
381
|
export class HttpRangeReader extends RandomAccessReader {
|
|
289
|
-
|
|
382
|
+
/** sorted by `start`, never overlapping */
|
|
383
|
+
private chunks: Chunk[] = [];
|
|
290
384
|
/** span a header read inside it should widen to (see `hintEntry`) */
|
|
291
385
|
private hint: { start: number; end: number } | null = null;
|
|
292
386
|
requests = 0;
|
|
@@ -295,7 +389,8 @@ export class HttpRangeReader extends RandomAccessReader {
|
|
|
295
389
|
constructor(
|
|
296
390
|
private readonly url: string,
|
|
297
391
|
readonly totalSize: number,
|
|
298
|
-
tail?:
|
|
392
|
+
tail?: Chunk,
|
|
393
|
+
private readonly options: HttpRangeReaderOptions = {},
|
|
299
394
|
) {
|
|
300
395
|
super();
|
|
301
396
|
if (tail) this.addChunk(tail.start, tail.data);
|
|
@@ -319,16 +414,16 @@ export class HttpRangeReader extends RandomAccessReader {
|
|
|
319
414
|
this.hint = { start, end: Math.min(end, this.totalSize) };
|
|
320
415
|
}
|
|
321
416
|
|
|
322
|
-
/** Cache `data` at `start`, merging with any chunk it
|
|
417
|
+
/** Cache `data` at `start`, merging with any chunk it overlaps. */
|
|
323
418
|
addChunk(start: number, data: Buffer): void {
|
|
324
419
|
if (data.length === 0) return;
|
|
325
420
|
let lo = start;
|
|
326
421
|
let hi = start + data.length;
|
|
327
|
-
const pieces:
|
|
328
|
-
const rest:
|
|
422
|
+
const pieces: Chunk[] = [{ start, data }];
|
|
423
|
+
const rest: Chunk[] = [];
|
|
329
424
|
for (const chunk of this.chunks) {
|
|
330
425
|
const chunkEnd = chunk.start + chunk.data.length;
|
|
331
|
-
if (chunk.start
|
|
426
|
+
if (chunk.start < hi && chunkEnd > lo) {
|
|
332
427
|
pieces.push(chunk);
|
|
333
428
|
lo = Math.min(lo, chunk.start);
|
|
334
429
|
hi = Math.max(hi, chunkEnd);
|
|
@@ -350,47 +445,90 @@ export class HttpRangeReader extends RandomAccessReader {
|
|
|
350
445
|
this.chunks = rest;
|
|
351
446
|
}
|
|
352
447
|
|
|
353
|
-
/**
|
|
354
|
-
|
|
448
|
+
/** Fetch `[start, end)` into the cache in one request. */
|
|
449
|
+
async prefetch(start: number, end: number): Promise<void> {
|
|
450
|
+
this.addChunk(start, await this.fetchBuffer(start, end));
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
private requestOptions(bytes: number): RequestOptions {
|
|
454
|
+
return {
|
|
455
|
+
timeoutMs: timeoutFor(bytes, this.options),
|
|
456
|
+
ifRange: this.options.ifRange,
|
|
457
|
+
expectTotal: this.totalSize,
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
private async fetchBuffer(start: number, end: number): Promise<Buffer> {
|
|
462
|
+
this.requests++;
|
|
463
|
+
const { data } = await fetchRangeBuffer(
|
|
464
|
+
this.url,
|
|
465
|
+
start,
|
|
466
|
+
end,
|
|
467
|
+
this.requestOptions(end - start),
|
|
468
|
+
);
|
|
469
|
+
this.fetchedBytes += data.length;
|
|
470
|
+
return data;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Contiguous cached pieces starting exactly at `start`, up to `end`: views
|
|
475
|
+
* into one chunk or into a run of touching chunks, possibly ending short.
|
|
476
|
+
*/
|
|
477
|
+
private cachedPieces(start: number, end: number): Buffer[] {
|
|
478
|
+
const pieces: Buffer[] = [];
|
|
479
|
+
let at = start;
|
|
355
480
|
for (const chunk of this.chunks) {
|
|
356
481
|
const chunkEnd = chunk.start + chunk.data.length;
|
|
357
|
-
if (
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
482
|
+
if (chunkEnd <= at) continue;
|
|
483
|
+
if (chunk.start > at) break;
|
|
484
|
+
const stop = Math.min(end, chunkEnd);
|
|
485
|
+
pieces.push(chunk.data.subarray(at - chunk.start, stop - chunk.start));
|
|
486
|
+
at = stop;
|
|
487
|
+
if (at >= end) break;
|
|
363
488
|
}
|
|
364
|
-
return
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
private cached(start: number, end: number): Buffer | null {
|
|
368
|
-
const prefix = this.cachedPrefix(start, end);
|
|
369
|
-
return prefix && prefix.length === end - start ? prefix : null;
|
|
489
|
+
return pieces;
|
|
370
490
|
}
|
|
371
491
|
|
|
372
|
-
/** start of the cached
|
|
492
|
+
/** start of the cached run (touching chunks) holding byte `end - 1` */
|
|
373
493
|
private cachedSuffixStart(end: number): number | null {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
494
|
+
let i = this.chunks.findIndex(
|
|
495
|
+
(c) => end - 1 >= c.start && end - 1 < c.start + c.data.length,
|
|
496
|
+
);
|
|
497
|
+
if (i < 0) return null;
|
|
498
|
+
while (
|
|
499
|
+
i > 0 &&
|
|
500
|
+
this.chunks[i - 1].start + this.chunks[i - 1].data.length ===
|
|
501
|
+
this.chunks[i].start
|
|
502
|
+
) {
|
|
503
|
+
i--;
|
|
378
504
|
}
|
|
379
|
-
return
|
|
505
|
+
return this.chunks[i].start;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** start of the first cached chunk at or after `position` */
|
|
509
|
+
private nextCachedStart(position: number): number | null {
|
|
510
|
+
const chunk = this.chunks.find((c) => c.start >= position);
|
|
511
|
+
return chunk ? chunk.start : null;
|
|
380
512
|
}
|
|
381
513
|
|
|
382
514
|
_readStreamForRange(start: number, end: number): Readable {
|
|
383
|
-
const
|
|
384
|
-
|
|
515
|
+
const pieces = this.cachedPieces(start, end);
|
|
516
|
+
const have = pieces.reduce((sum, piece) => sum + piece.length, 0);
|
|
517
|
+
if (have === end - start) return Readable.from(pieces);
|
|
385
518
|
const out = new PassThrough();
|
|
386
|
-
|
|
387
|
-
const from = start +
|
|
519
|
+
for (const piece of pieces) out.write(piece);
|
|
520
|
+
const from = start + have;
|
|
521
|
+
const options = this.requestOptions(end - from);
|
|
388
522
|
this.requests++;
|
|
389
|
-
fetchRange(this.url, from, end)
|
|
523
|
+
fetchRange(this.url, from, end, options)
|
|
390
524
|
.then((response) => {
|
|
391
525
|
this.fetchedBytes += end - from;
|
|
392
526
|
const body = Readable.fromWeb(response.body as any);
|
|
393
|
-
body.on('error', (error) =>
|
|
527
|
+
body.on('error', (error) =>
|
|
528
|
+
out.destroy(
|
|
529
|
+
describeAbort(error, `bytes=${from}-${end - 1}`, options.timeoutMs),
|
|
530
|
+
),
|
|
531
|
+
);
|
|
394
532
|
body.pipe(out);
|
|
395
533
|
})
|
|
396
534
|
.catch((error) => out.destroy(error));
|
|
@@ -404,43 +542,50 @@ export class HttpRangeReader extends RandomAccessReader {
|
|
|
404
542
|
position: number,
|
|
405
543
|
callback: (err: Error | null, bytesRead?: number) => void,
|
|
406
544
|
): void {
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
|
|
545
|
+
const requestEnd = position + length;
|
|
546
|
+
const copyOut = (pieces: Buffer[]): boolean => {
|
|
547
|
+
let at = offset;
|
|
548
|
+
for (const piece of pieces) {
|
|
549
|
+
piece.copy(buffer, at);
|
|
550
|
+
at += piece.length;
|
|
551
|
+
}
|
|
552
|
+
return at - offset === length;
|
|
553
|
+
};
|
|
554
|
+
const cached = this.cachedPieces(position, requestEnd);
|
|
555
|
+
if (copyOut(cached)) {
|
|
410
556
|
setImmediate(() => callback(null, length));
|
|
411
557
|
return;
|
|
412
558
|
}
|
|
413
|
-
// fetch only what the cache lacks: skip
|
|
414
|
-
// suffix (yauzl's EOCD read may just exceed the tail), and
|
|
415
|
-
// so the next header read is free
|
|
416
|
-
|
|
559
|
+
// fetch only what the cache lacks: skip the cached prefix, stop at a
|
|
560
|
+
// cached suffix (yauzl's EOCD read may just exceed the tail), and
|
|
561
|
+
// otherwise widen so the next header read is free — to exactly the hinted
|
|
562
|
+
// entry when the hint covers this read, else by CHUNK_BYTES — but never
|
|
563
|
+
// into bytes already cached beyond the request
|
|
417
564
|
const from =
|
|
418
|
-
position +
|
|
565
|
+
position + cached.reduce((sum, piece) => sum + piece.length, 0);
|
|
419
566
|
const suffixStart = this.cachedSuffixStart(requestEnd);
|
|
420
567
|
let end: number;
|
|
421
568
|
if (suffixStart !== null && suffixStart > from) {
|
|
422
569
|
end = suffixStart;
|
|
423
570
|
} else {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
this.hint &&
|
|
571
|
+
const hinted =
|
|
572
|
+
this.hint !== null &&
|
|
427
573
|
position >= this.hint.start &&
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
574
|
+
requestEnd <= this.hint.end;
|
|
575
|
+
end = hinted
|
|
576
|
+
? (this.hint as { end: number }).end
|
|
577
|
+
: Math.max(requestEnd, from + CHUNK_BYTES);
|
|
578
|
+
end = Math.min(end, this.totalSize);
|
|
579
|
+
const next = this.nextCachedStart(requestEnd);
|
|
580
|
+
if (next !== null && next < end) end = next;
|
|
432
581
|
}
|
|
433
|
-
this.
|
|
434
|
-
|
|
435
|
-
({ data }) => {
|
|
436
|
-
this.fetchedBytes += data.length;
|
|
582
|
+
this.fetchBuffer(from, end).then(
|
|
583
|
+
(data) => {
|
|
437
584
|
this.addChunk(from, data);
|
|
438
|
-
|
|
439
|
-
if (!full) {
|
|
585
|
+
if (!copyOut(this.cachedPieces(position, requestEnd))) {
|
|
440
586
|
callback(new Error('range read did not cover the requested bytes'));
|
|
441
587
|
return;
|
|
442
588
|
}
|
|
443
|
-
full.copy(buffer, offset);
|
|
444
589
|
callback(null, length);
|
|
445
590
|
},
|
|
446
591
|
(error) => callback(error),
|
|
@@ -451,8 +596,6 @@ export class HttpRangeReader extends RandomAccessReader {
|
|
|
451
596
|
/** End-of-central-directory record: where the central directory lives. */
|
|
452
597
|
const EOCD_SIGNATURE = 0x06054b50;
|
|
453
598
|
const EOCD_SIZE = 22;
|
|
454
|
-
/** central directories larger than this are left to yauzl's paged reads */
|
|
455
|
-
const MAX_PREFETCH_BYTES = 8 * 1024 * 1024;
|
|
456
599
|
|
|
457
600
|
export function parseEndOfCentralDirectory(
|
|
458
601
|
tail: Buffer,
|
|
@@ -479,10 +622,20 @@ export type RemoteZip =
|
|
|
479
622
|
* Open a remote zip by fetching its tail (EOCD + central directory for
|
|
480
623
|
* typical archives) and letting yauzl read the rest on demand.
|
|
481
624
|
*/
|
|
482
|
-
export async function openRemoteZip(
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
625
|
+
export async function openRemoteZip(
|
|
626
|
+
url: string,
|
|
627
|
+
options: RangeOptions = {},
|
|
628
|
+
): Promise<RemoteZip> {
|
|
629
|
+
const timeoutMs = timeoutFor(TAIL_BYTES, options);
|
|
630
|
+
let response: Response;
|
|
631
|
+
try {
|
|
632
|
+
response = await fetch(url, {
|
|
633
|
+
headers: { Range: `bytes=-${TAIL_BYTES}`, 'Accept-Encoding': 'identity' },
|
|
634
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
635
|
+
});
|
|
636
|
+
} catch (error) {
|
|
637
|
+
throw describeAbort(error, `bytes=-${TAIL_BYTES}`, timeoutMs);
|
|
638
|
+
}
|
|
486
639
|
if (!response.ok || !response.body) {
|
|
487
640
|
await response.body?.cancel().catch(() => {});
|
|
488
641
|
throw new Error(`HTTP ${response.status}`);
|
|
@@ -495,35 +648,38 @@ export async function openRemoteZip(url: string): Promise<RemoteZip> {
|
|
|
495
648
|
`unexpected Content-Range ${response.headers.get('content-range')}`,
|
|
496
649
|
);
|
|
497
650
|
}
|
|
498
|
-
|
|
651
|
+
let data: Buffer;
|
|
652
|
+
try {
|
|
653
|
+
data = Buffer.from(await response.arrayBuffer());
|
|
654
|
+
} catch (error) {
|
|
655
|
+
throw describeAbort(error, `bytes=-${TAIL_BYTES}`, timeoutMs);
|
|
656
|
+
}
|
|
499
657
|
if (data.length !== range.end - range.start + 1) {
|
|
500
658
|
throw new Error(
|
|
501
659
|
`tail returned ${data.length} bytes, expected ${range.end - range.start + 1}`,
|
|
502
660
|
);
|
|
503
661
|
}
|
|
504
|
-
const reader = new HttpRangeReader(
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
662
|
+
const reader = new HttpRangeReader(
|
|
663
|
+
url,
|
|
664
|
+
range.total,
|
|
665
|
+
{ start: range.start, data },
|
|
666
|
+
{ ...options, ifRange: rangeValidator(response.headers) },
|
|
667
|
+
);
|
|
508
668
|
reader.requests = 1;
|
|
509
669
|
reader.fetchedBytes = data.length;
|
|
510
670
|
// Large archives (apk / ipa with thousands of files) keep their central
|
|
511
671
|
// directory well before the tail; yauzl would otherwise page it in with one
|
|
512
|
-
// sequential request per CHUNK_BYTES. Fetch the missing part at once
|
|
672
|
+
// sequential request per CHUNK_BYTES. Fetch the missing part at once — one
|
|
673
|
+
// buffer of exactly its size — as long as the EOCD describes a directory
|
|
674
|
+
// that ends inside the tail (a corrupt record could claim the whole file).
|
|
513
675
|
const eocd = parseEndOfCentralDirectory(data);
|
|
514
676
|
if (
|
|
515
677
|
eocd &&
|
|
516
678
|
eocd.cdOffset < range.start &&
|
|
517
|
-
|
|
679
|
+
eocd.cdOffset + eocd.cdSize >= range.start &&
|
|
680
|
+
eocd.cdOffset + eocd.cdSize <= range.total - EOCD_SIZE
|
|
518
681
|
) {
|
|
519
|
-
|
|
520
|
-
url,
|
|
521
|
-
eocd.cdOffset,
|
|
522
|
-
range.start,
|
|
523
|
-
);
|
|
524
|
-
reader.requests++;
|
|
525
|
-
reader.fetchedBytes += directory.length;
|
|
526
|
-
reader.addChunk(eocd.cdOffset, directory);
|
|
682
|
+
await reader.prefetch(eocd.cdOffset, range.start);
|
|
527
683
|
}
|
|
528
684
|
const zipFile = await new Promise<ZipFile>((resolve, reject) => {
|
|
529
685
|
fromRandomAccessReader(
|