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/lib/utils/zip-range.js
CHANGED
|
@@ -8,6 +8,7 @@ exports.locateZipEntry = locateZipEntry;
|
|
|
8
8
|
exports.readZipEntryWithLocation = readZipEntryWithLocation;
|
|
9
9
|
exports.bundleLocationFields = bundleLocationFields;
|
|
10
10
|
exports.parseContentRange = parseContentRange;
|
|
11
|
+
exports.rangeValidator = rangeValidator;
|
|
11
12
|
exports.fetchZipEntryData = fetchZipEntryData;
|
|
12
13
|
exports.parseEndOfCentralDirectory = parseEndOfCentralDirectory;
|
|
13
14
|
exports.openRemoteZip = openRemoteZip;
|
|
@@ -144,9 +145,18 @@ function bundleLocationFields(location) {
|
|
|
144
145
|
bundleCompression: location.compressionMethod,
|
|
145
146
|
};
|
|
146
147
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
148
|
+
const DEFAULT_HEADER_TIMEOUT_MS = 20_000;
|
|
149
|
+
const DEFAULT_DATA_TIMEOUT_MS = 30_000;
|
|
150
|
+
const DEFAULT_DATA_TIMEOUT_PER_MB_MS = 1_000;
|
|
151
|
+
/** deadline for a request of `bytes`: flat for header-sized reads, scaled otherwise */
|
|
152
|
+
function timeoutFor(bytes, options) {
|
|
153
|
+
if (bytes <= CHUNK_BYTES) {
|
|
154
|
+
return options.headerTimeoutMs ?? DEFAULT_HEADER_TIMEOUT_MS;
|
|
155
|
+
}
|
|
156
|
+
return ((options.dataTimeoutMs ?? DEFAULT_DATA_TIMEOUT_MS) +
|
|
157
|
+
((options.dataTimeoutPerMbMs ?? DEFAULT_DATA_TIMEOUT_PER_MB_MS) * bytes) /
|
|
158
|
+
(1024 * 1024));
|
|
159
|
+
}
|
|
150
160
|
/** The server answered a Range request with the whole body (HTTP 200). */
|
|
151
161
|
class RangeUnsupportedError extends Error {
|
|
152
162
|
response;
|
|
@@ -166,30 +176,72 @@ function parseContentRange(header) {
|
|
|
166
176
|
return null;
|
|
167
177
|
return { start, end, total };
|
|
168
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* The validator to send as `If-Range` on later requests for the same object,
|
|
181
|
+
* so a replaced archive answers 200 (handled like a server without Range)
|
|
182
|
+
* instead of mixing bytes of two versions. Weak ETags are not allowed there.
|
|
183
|
+
*/
|
|
184
|
+
function rangeValidator(headers) {
|
|
185
|
+
const etag = headers.get('etag')?.trim();
|
|
186
|
+
if (etag && !/^W\//i.test(etag))
|
|
187
|
+
return etag;
|
|
188
|
+
return headers.get('last-modified')?.trim() || undefined;
|
|
189
|
+
}
|
|
190
|
+
/** An aborted fetch as a plain Error (never mistaken for a 200 answer). */
|
|
191
|
+
function describeAbort(error, what, timeoutMs) {
|
|
192
|
+
const name = error?.name;
|
|
193
|
+
if (name === 'TimeoutError' || name === 'AbortError') {
|
|
194
|
+
return new Error(`timed out after ${timeoutMs} ms fetching ${what}`);
|
|
195
|
+
}
|
|
196
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
197
|
+
}
|
|
169
198
|
/** One Range request; `end` is exclusive. Resolves with a 206 response. */
|
|
170
|
-
async function fetchRange(url, start, end) {
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
199
|
+
async function fetchRange(url, start, end, options) {
|
|
200
|
+
const what = `bytes=${start}-${end - 1}`;
|
|
201
|
+
const headers = {
|
|
202
|
+
Range: what,
|
|
203
|
+
'Accept-Encoding': 'identity',
|
|
204
|
+
};
|
|
205
|
+
if (options.ifRange)
|
|
206
|
+
headers['If-Range'] = options.ifRange;
|
|
207
|
+
let response;
|
|
208
|
+
try {
|
|
209
|
+
response = await fetch(url, {
|
|
210
|
+
headers,
|
|
211
|
+
signal: AbortSignal.timeout(options.timeoutMs),
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
throw describeAbort(error, what, options.timeoutMs);
|
|
216
|
+
}
|
|
177
217
|
if (response.status === 200)
|
|
178
218
|
throw new RangeUnsupportedError(response);
|
|
179
219
|
if (response.status !== 206 || !response.body) {
|
|
180
220
|
await response.body?.cancel().catch(() => { });
|
|
181
|
-
throw new Error(`HTTP ${response.status} for
|
|
221
|
+
throw new Error(`HTTP ${response.status} for ${what}`);
|
|
182
222
|
}
|
|
183
223
|
const range = parseContentRange(response.headers.get('content-range'));
|
|
184
224
|
if (!range || range.start !== start || range.end !== end - 1) {
|
|
185
225
|
await response.body.cancel().catch(() => { });
|
|
186
226
|
throw new Error(`unexpected Content-Range ${response.headers.get('content-range')}`);
|
|
187
227
|
}
|
|
228
|
+
if (options.expectTotal !== undefined &&
|
|
229
|
+
range.total !== options.expectTotal) {
|
|
230
|
+
await response.body.cancel().catch(() => { });
|
|
231
|
+
throw new Error(`archive size changed: ${range.total} bytes, expected ${options.expectTotal}`);
|
|
232
|
+
}
|
|
188
233
|
return Object.assign(response, { range });
|
|
189
234
|
}
|
|
190
|
-
async function fetchRangeBuffer(url, start, end) {
|
|
191
|
-
const response = await fetchRange(url, start, end);
|
|
192
|
-
|
|
235
|
+
async function fetchRangeBuffer(url, start, end, options) {
|
|
236
|
+
const response = await fetchRange(url, start, end, options);
|
|
237
|
+
let data;
|
|
238
|
+
try {
|
|
239
|
+
// Buffer.from(ArrayBuffer) is a view: the body is held exactly once
|
|
240
|
+
data = Buffer.from(await response.arrayBuffer());
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
throw describeAbort(error, `bytes=${start}-${end - 1}`, options.timeoutMs);
|
|
244
|
+
}
|
|
193
245
|
if (data.length !== end - start) {
|
|
194
246
|
throw new Error(`range returned ${data.length} bytes, expected ${end - start}`);
|
|
195
247
|
}
|
|
@@ -199,7 +251,7 @@ async function fetchRangeBuffer(url, start, end) {
|
|
|
199
251
|
* Fetch one entry whose compressed bytes are already located (the ppk's own
|
|
200
252
|
* publish-time report) and decompress it. Returns the raw entry content.
|
|
201
253
|
*/
|
|
202
|
-
async function fetchZipEntryData(url, location) {
|
|
254
|
+
async function fetchZipEntryData(url, location, options = {}) {
|
|
203
255
|
const { dataOffset, compressedSize, compressionMethod } = location;
|
|
204
256
|
if (compressionMethod !== exports.ZIP_STORED && compressionMethod !== exports.ZIP_DEFLATED) {
|
|
205
257
|
throw new Error(`unsupported compression method ${compressionMethod}`);
|
|
@@ -210,7 +262,7 @@ async function fetchZipEntryData(url, location) {
|
|
|
210
262
|
compressedSize <= 0) {
|
|
211
263
|
throw new Error('invalid bundle location');
|
|
212
264
|
}
|
|
213
|
-
const { data, total } = await fetchRangeBuffer(url, dataOffset, dataOffset + compressedSize);
|
|
265
|
+
const { data, total } = await fetchRangeBuffer(url, dataOffset, dataOffset + compressedSize, { timeoutMs: timeoutFor(compressedSize, options) });
|
|
214
266
|
return {
|
|
215
267
|
data: compressionMethod === exports.ZIP_DEFLATED ? (0, zlib_1.inflateRawSync)(data) : data,
|
|
216
268
|
fetchedBytes: compressedSize,
|
|
@@ -219,23 +271,28 @@ async function fetchZipEntryData(url, location) {
|
|
|
219
271
|
}
|
|
220
272
|
/**
|
|
221
273
|
* yauzl reader over HTTP Range. Header reads (EOCD, central directory, local
|
|
222
|
-
* file headers) are widened to CHUNK_BYTES and cached
|
|
223
|
-
* overlapping
|
|
224
|
-
*
|
|
225
|
-
* bytes following the local file header) before
|
|
274
|
+
* file headers) are widened to CHUNK_BYTES and cached. Chunks are kept as
|
|
275
|
+
* fetched — only overlapping ones are merged, touching ones are read across —
|
|
276
|
+
* so an entry's bytes are held once, and a read reuses whatever prefix the
|
|
277
|
+
* cache holds (typically the bytes following the local file header) before
|
|
278
|
+
* requesting the rest. Every request after the first carries If-Range and
|
|
279
|
+
* must report the same archive size.
|
|
226
280
|
*/
|
|
227
281
|
class HttpRangeReader extends yauzl_1.RandomAccessReader {
|
|
228
282
|
url;
|
|
229
283
|
totalSize;
|
|
284
|
+
options;
|
|
285
|
+
/** sorted by `start`, never overlapping */
|
|
230
286
|
chunks = [];
|
|
231
287
|
/** span a header read inside it should widen to (see `hintEntry`) */
|
|
232
288
|
hint = null;
|
|
233
289
|
requests = 0;
|
|
234
290
|
fetchedBytes = 0;
|
|
235
|
-
constructor(url, totalSize, tail) {
|
|
291
|
+
constructor(url, totalSize, tail, options = {}) {
|
|
236
292
|
super();
|
|
237
293
|
this.url = url;
|
|
238
294
|
this.totalSize = totalSize;
|
|
295
|
+
this.options = options;
|
|
239
296
|
if (tail)
|
|
240
297
|
this.addChunk(tail.start, tail.data);
|
|
241
298
|
}
|
|
@@ -255,7 +312,7 @@ class HttpRangeReader extends yauzl_1.RandomAccessReader {
|
|
|
255
312
|
HINT_SLACK_BYTES;
|
|
256
313
|
this.hint = { start, end: Math.min(end, this.totalSize) };
|
|
257
314
|
}
|
|
258
|
-
/** Cache `data` at `start`, merging with any chunk it
|
|
315
|
+
/** Cache `data` at `start`, merging with any chunk it overlaps. */
|
|
259
316
|
addChunk(start, data) {
|
|
260
317
|
if (data.length === 0)
|
|
261
318
|
return;
|
|
@@ -265,7 +322,7 @@ class HttpRangeReader extends yauzl_1.RandomAccessReader {
|
|
|
265
322
|
const rest = [];
|
|
266
323
|
for (const chunk of this.chunks) {
|
|
267
324
|
const chunkEnd = chunk.start + chunk.data.length;
|
|
268
|
-
if (chunk.start
|
|
325
|
+
if (chunk.start < hi && chunkEnd > lo) {
|
|
269
326
|
pieces.push(chunk);
|
|
270
327
|
lo = Math.min(lo, chunk.start);
|
|
271
328
|
hi = Math.max(hi, chunkEnd);
|
|
@@ -288,83 +345,126 @@ class HttpRangeReader extends yauzl_1.RandomAccessReader {
|
|
|
288
345
|
rest.sort((a, b) => a.start - b.start);
|
|
289
346
|
this.chunks = rest;
|
|
290
347
|
}
|
|
291
|
-
/**
|
|
292
|
-
|
|
348
|
+
/** Fetch `[start, end)` into the cache in one request. */
|
|
349
|
+
async prefetch(start, end) {
|
|
350
|
+
this.addChunk(start, await this.fetchBuffer(start, end));
|
|
351
|
+
}
|
|
352
|
+
requestOptions(bytes) {
|
|
353
|
+
return {
|
|
354
|
+
timeoutMs: timeoutFor(bytes, this.options),
|
|
355
|
+
ifRange: this.options.ifRange,
|
|
356
|
+
expectTotal: this.totalSize,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
async fetchBuffer(start, end) {
|
|
360
|
+
this.requests++;
|
|
361
|
+
const { data } = await fetchRangeBuffer(this.url, start, end, this.requestOptions(end - start));
|
|
362
|
+
this.fetchedBytes += data.length;
|
|
363
|
+
return data;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Contiguous cached pieces starting exactly at `start`, up to `end`: views
|
|
367
|
+
* into one chunk or into a run of touching chunks, possibly ending short.
|
|
368
|
+
*/
|
|
369
|
+
cachedPieces(start, end) {
|
|
370
|
+
const pieces = [];
|
|
371
|
+
let at = start;
|
|
293
372
|
for (const chunk of this.chunks) {
|
|
294
373
|
const chunkEnd = chunk.start + chunk.data.length;
|
|
295
|
-
if (
|
|
296
|
-
|
|
297
|
-
|
|
374
|
+
if (chunkEnd <= at)
|
|
375
|
+
continue;
|
|
376
|
+
if (chunk.start > at)
|
|
377
|
+
break;
|
|
378
|
+
const stop = Math.min(end, chunkEnd);
|
|
379
|
+
pieces.push(chunk.data.subarray(at - chunk.start, stop - chunk.start));
|
|
380
|
+
at = stop;
|
|
381
|
+
if (at >= end)
|
|
382
|
+
break;
|
|
298
383
|
}
|
|
299
|
-
return
|
|
300
|
-
}
|
|
301
|
-
cached(start, end) {
|
|
302
|
-
const prefix = this.cachedPrefix(start, end);
|
|
303
|
-
return prefix && prefix.length === end - start ? prefix : null;
|
|
384
|
+
return pieces;
|
|
304
385
|
}
|
|
305
|
-
/** start of the cached
|
|
386
|
+
/** start of the cached run (touching chunks) holding byte `end - 1` */
|
|
306
387
|
cachedSuffixStart(end) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
388
|
+
let i = this.chunks.findIndex((c) => end - 1 >= c.start && end - 1 < c.start + c.data.length);
|
|
389
|
+
if (i < 0)
|
|
390
|
+
return null;
|
|
391
|
+
while (i > 0 &&
|
|
392
|
+
this.chunks[i - 1].start + this.chunks[i - 1].data.length ===
|
|
393
|
+
this.chunks[i].start) {
|
|
394
|
+
i--;
|
|
311
395
|
}
|
|
312
|
-
return
|
|
396
|
+
return this.chunks[i].start;
|
|
397
|
+
}
|
|
398
|
+
/** start of the first cached chunk at or after `position` */
|
|
399
|
+
nextCachedStart(position) {
|
|
400
|
+
const chunk = this.chunks.find((c) => c.start >= position);
|
|
401
|
+
return chunk ? chunk.start : null;
|
|
313
402
|
}
|
|
314
403
|
_readStreamForRange(start, end) {
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
|
|
404
|
+
const pieces = this.cachedPieces(start, end);
|
|
405
|
+
const have = pieces.reduce((sum, piece) => sum + piece.length, 0);
|
|
406
|
+
if (have === end - start)
|
|
407
|
+
return stream_1.Readable.from(pieces);
|
|
318
408
|
const out = new stream_1.PassThrough();
|
|
319
|
-
|
|
320
|
-
out.write(
|
|
321
|
-
const from = start +
|
|
409
|
+
for (const piece of pieces)
|
|
410
|
+
out.write(piece);
|
|
411
|
+
const from = start + have;
|
|
412
|
+
const options = this.requestOptions(end - from);
|
|
322
413
|
this.requests++;
|
|
323
|
-
fetchRange(this.url, from, end)
|
|
414
|
+
fetchRange(this.url, from, end, options)
|
|
324
415
|
.then((response) => {
|
|
325
416
|
this.fetchedBytes += end - from;
|
|
326
417
|
const body = stream_1.Readable.fromWeb(response.body);
|
|
327
|
-
body.on('error', (error) => out.destroy(error));
|
|
418
|
+
body.on('error', (error) => out.destroy(describeAbort(error, `bytes=${from}-${end - 1}`, options.timeoutMs)));
|
|
328
419
|
body.pipe(out);
|
|
329
420
|
})
|
|
330
421
|
.catch((error) => out.destroy(error));
|
|
331
422
|
return out;
|
|
332
423
|
}
|
|
333
424
|
read(buffer, offset, length, position, callback) {
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
425
|
+
const requestEnd = position + length;
|
|
426
|
+
const copyOut = (pieces) => {
|
|
427
|
+
let at = offset;
|
|
428
|
+
for (const piece of pieces) {
|
|
429
|
+
piece.copy(buffer, at);
|
|
430
|
+
at += piece.length;
|
|
431
|
+
}
|
|
432
|
+
return at - offset === length;
|
|
433
|
+
};
|
|
434
|
+
const cached = this.cachedPieces(position, requestEnd);
|
|
435
|
+
if (copyOut(cached)) {
|
|
337
436
|
setImmediate(() => callback(null, length));
|
|
338
437
|
return;
|
|
339
438
|
}
|
|
340
|
-
// fetch only what the cache lacks: skip
|
|
341
|
-
// suffix (yauzl's EOCD read may just exceed the tail), and
|
|
342
|
-
// so the next header read is free
|
|
343
|
-
|
|
344
|
-
|
|
439
|
+
// fetch only what the cache lacks: skip the cached prefix, stop at a
|
|
440
|
+
// cached suffix (yauzl's EOCD read may just exceed the tail), and
|
|
441
|
+
// otherwise widen so the next header read is free — to exactly the hinted
|
|
442
|
+
// entry when the hint covers this read, else by CHUNK_BYTES — but never
|
|
443
|
+
// into bytes already cached beyond the request
|
|
444
|
+
const from = position + cached.reduce((sum, piece) => sum + piece.length, 0);
|
|
345
445
|
const suffixStart = this.cachedSuffixStart(requestEnd);
|
|
346
446
|
let end;
|
|
347
447
|
if (suffixStart !== null && suffixStart > from) {
|
|
348
448
|
end = suffixStart;
|
|
349
449
|
}
|
|
350
450
|
else {
|
|
351
|
-
|
|
352
|
-
if (this.hint &&
|
|
451
|
+
const hinted = this.hint !== null &&
|
|
353
452
|
position >= this.hint.start &&
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
453
|
+
requestEnd <= this.hint.end;
|
|
454
|
+
end = hinted
|
|
455
|
+
? this.hint.end
|
|
456
|
+
: Math.max(requestEnd, from + CHUNK_BYTES);
|
|
457
|
+
end = Math.min(end, this.totalSize);
|
|
458
|
+
const next = this.nextCachedStart(requestEnd);
|
|
459
|
+
if (next !== null && next < end)
|
|
460
|
+
end = next;
|
|
357
461
|
}
|
|
358
|
-
this.
|
|
359
|
-
fetchRangeBuffer(this.url, from, end).then(({ data }) => {
|
|
360
|
-
this.fetchedBytes += data.length;
|
|
462
|
+
this.fetchBuffer(from, end).then((data) => {
|
|
361
463
|
this.addChunk(from, data);
|
|
362
|
-
|
|
363
|
-
if (!full) {
|
|
464
|
+
if (!copyOut(this.cachedPieces(position, requestEnd))) {
|
|
364
465
|
callback(new Error('range read did not cover the requested bytes'));
|
|
365
466
|
return;
|
|
366
467
|
}
|
|
367
|
-
full.copy(buffer, offset);
|
|
368
468
|
callback(null, length);
|
|
369
469
|
}, (error) => callback(error));
|
|
370
470
|
}
|
|
@@ -373,8 +473,6 @@ exports.HttpRangeReader = HttpRangeReader;
|
|
|
373
473
|
/** End-of-central-directory record: where the central directory lives. */
|
|
374
474
|
const EOCD_SIGNATURE = 0x06054b50;
|
|
375
475
|
const EOCD_SIZE = 22;
|
|
376
|
-
/** central directories larger than this are left to yauzl's paged reads */
|
|
377
|
-
const MAX_PREFETCH_BYTES = 8 * 1024 * 1024;
|
|
378
476
|
function parseEndOfCentralDirectory(tail) {
|
|
379
477
|
for (let i = tail.length - EOCD_SIZE; i >= 0; i--) {
|
|
380
478
|
if (tail.readUInt32LE(i) !== EOCD_SIGNATURE)
|
|
@@ -395,10 +493,18 @@ function parseEndOfCentralDirectory(tail) {
|
|
|
395
493
|
* Open a remote zip by fetching its tail (EOCD + central directory for
|
|
396
494
|
* typical archives) and letting yauzl read the rest on demand.
|
|
397
495
|
*/
|
|
398
|
-
async function openRemoteZip(url) {
|
|
399
|
-
const
|
|
400
|
-
|
|
401
|
-
|
|
496
|
+
async function openRemoteZip(url, options = {}) {
|
|
497
|
+
const timeoutMs = timeoutFor(TAIL_BYTES, options);
|
|
498
|
+
let response;
|
|
499
|
+
try {
|
|
500
|
+
response = await fetch(url, {
|
|
501
|
+
headers: { Range: `bytes=-${TAIL_BYTES}`, 'Accept-Encoding': 'identity' },
|
|
502
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
catch (error) {
|
|
506
|
+
throw describeAbort(error, `bytes=-${TAIL_BYTES}`, timeoutMs);
|
|
507
|
+
}
|
|
402
508
|
if (!response.ok || !response.body) {
|
|
403
509
|
await response.body?.cancel().catch(() => { });
|
|
404
510
|
throw new Error(`HTTP ${response.status}`);
|
|
@@ -410,27 +516,30 @@ async function openRemoteZip(url) {
|
|
|
410
516
|
await response.body.cancel().catch(() => { });
|
|
411
517
|
throw new Error(`unexpected Content-Range ${response.headers.get('content-range')}`);
|
|
412
518
|
}
|
|
413
|
-
|
|
519
|
+
let data;
|
|
520
|
+
try {
|
|
521
|
+
data = Buffer.from(await response.arrayBuffer());
|
|
522
|
+
}
|
|
523
|
+
catch (error) {
|
|
524
|
+
throw describeAbort(error, `bytes=-${TAIL_BYTES}`, timeoutMs);
|
|
525
|
+
}
|
|
414
526
|
if (data.length !== range.end - range.start + 1) {
|
|
415
527
|
throw new Error(`tail returned ${data.length} bytes, expected ${range.end - range.start + 1}`);
|
|
416
528
|
}
|
|
417
|
-
const reader = new HttpRangeReader(url, range.total, {
|
|
418
|
-
start: range.start,
|
|
419
|
-
data,
|
|
420
|
-
});
|
|
529
|
+
const reader = new HttpRangeReader(url, range.total, { start: range.start, data }, { ...options, ifRange: rangeValidator(response.headers) });
|
|
421
530
|
reader.requests = 1;
|
|
422
531
|
reader.fetchedBytes = data.length;
|
|
423
532
|
// Large archives (apk / ipa with thousands of files) keep their central
|
|
424
533
|
// directory well before the tail; yauzl would otherwise page it in with one
|
|
425
|
-
// sequential request per CHUNK_BYTES. Fetch the missing part at once
|
|
534
|
+
// sequential request per CHUNK_BYTES. Fetch the missing part at once — one
|
|
535
|
+
// buffer of exactly its size — as long as the EOCD describes a directory
|
|
536
|
+
// that ends inside the tail (a corrupt record could claim the whole file).
|
|
426
537
|
const eocd = parseEndOfCentralDirectory(data);
|
|
427
538
|
if (eocd &&
|
|
428
539
|
eocd.cdOffset < range.start &&
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
reader.
|
|
432
|
-
reader.fetchedBytes += directory.length;
|
|
433
|
-
reader.addChunk(eocd.cdOffset, directory);
|
|
540
|
+
eocd.cdOffset + eocd.cdSize >= range.start &&
|
|
541
|
+
eocd.cdOffset + eocd.cdSize <= range.total - EOCD_SIZE) {
|
|
542
|
+
await reader.prefetch(eocd.cdOffset, range.start);
|
|
434
543
|
}
|
|
435
544
|
const zipFile = await new Promise((resolve, reject) => {
|
|
436
545
|
(0, yauzl_1.fromRandomAccessReader)(reader, range.total, { lazyEntries: true, autoClose: false }, (err, file) => (err ? reject(err) : resolve(file)));
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Readable } from 'stream';
|
|
2
|
+
import type { Entry } from 'yauzl';
|
|
3
|
+
export interface RawZipEntry {
|
|
4
|
+
fileName: string;
|
|
5
|
+
compressionMethod: number;
|
|
6
|
+
crc32: number;
|
|
7
|
+
compressedSize: number;
|
|
8
|
+
uncompressedSize: number;
|
|
9
|
+
lastModFileTime: number;
|
|
10
|
+
lastModFileDate: number;
|
|
11
|
+
generalPurposeBitFlag?: number;
|
|
12
|
+
versionMadeBy?: number;
|
|
13
|
+
versionNeededToExtract?: number;
|
|
14
|
+
internalFileAttributes?: number;
|
|
15
|
+
externalFileAttributes?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Whether an archive of `sourceSize` bytes with `entryCount` entries can be
|
|
19
|
+
* (partially) copied without zip64 structures: an output that only keeps a
|
|
20
|
+
* subset of the entries is never larger than its source.
|
|
21
|
+
*/
|
|
22
|
+
export declare function fitsRawZip(sourceSize: number, entryCount: number): boolean;
|
|
23
|
+
/** CRC-32 (IEEE) continued from `seed` (the running value, not inverted) */
|
|
24
|
+
export declare function crc32Update(seed: number, chunk: Buffer): number;
|
|
25
|
+
export declare class RawZipWriter {
|
|
26
|
+
readonly output: string;
|
|
27
|
+
private readonly out;
|
|
28
|
+
private readonly central;
|
|
29
|
+
private offset;
|
|
30
|
+
private count;
|
|
31
|
+
private failure;
|
|
32
|
+
private readonly closed;
|
|
33
|
+
constructor(output: string);
|
|
34
|
+
/** Copy one entry with its file data exactly as stored in the source. */
|
|
35
|
+
addRawEntry(meta: RawZipEntry, data: Readable): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Add a local file uncompressed (its CRC is computed in a first pass).
|
|
38
|
+
* `like` lends its timestamp and attributes, e.g. the archive entry this
|
|
39
|
+
* file replaces.
|
|
40
|
+
*/
|
|
41
|
+
addStoredFile(filePath: string, fileName: string, like?: Pick<Entry, 'lastModFileTime' | 'lastModFileDate' | 'externalFileAttributes' | 'versionMadeBy'>): Promise<void>;
|
|
42
|
+
/** Write the central directory and close the file. */
|
|
43
|
+
end(): Promise<void>;
|
|
44
|
+
/** Stop writing and remove the partial output. */
|
|
45
|
+
abort(): Promise<void>;
|
|
46
|
+
private write;
|
|
47
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RawZipWriter = void 0;
|
|
7
|
+
exports.fitsRawZip = fitsRawZip;
|
|
8
|
+
exports.crc32Update = crc32Update;
|
|
9
|
+
// A minimal zip writer that copies entries *as stored* — their compressed
|
|
10
|
+
// bytes, CRC, sizes and timestamps straight from the source archive's central
|
|
11
|
+
// directory — so repacking a subset of an apk / ipa / hap costs disk I/O, not
|
|
12
|
+
// a second deflate of every asset (yazl can only add uncompressed data and
|
|
13
|
+
// re-deflates it). Plain zip only: no zip64, no data descriptors, no extra
|
|
14
|
+
// fields; callers must check `fitsRawZip` before choosing this writer.
|
|
15
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
16
|
+
const LOCAL_HEADER_SIGNATURE = 0x04034b50;
|
|
17
|
+
const CENTRAL_HEADER_SIGNATURE = 0x02014b50;
|
|
18
|
+
const EOCD_SIGNATURE = 0x06054b50;
|
|
19
|
+
const VERSION_NEEDED_DEFAULT = 20;
|
|
20
|
+
/** "made by" for entries this writer creates itself: unix, spec 2.0 */
|
|
21
|
+
const VERSION_MADE_BY_UNIX = (3 << 8) | 20;
|
|
22
|
+
const FLAG_UTF8_NAMES = 0x800;
|
|
23
|
+
const FLAG_DATA_DESCRIPTOR = 0x8;
|
|
24
|
+
/** general-purpose bits carried over from a copied entry: encryption, deflate strength */
|
|
25
|
+
const FLAG_KEEP_MASK = 0x0007;
|
|
26
|
+
const MAX_UINT32 = 0xffffffff;
|
|
27
|
+
const MAX_UINT16 = 0xffff;
|
|
28
|
+
/** external attributes of a plain `-rw-r--r--` file */
|
|
29
|
+
const UNIX_REGULAR_FILE_ATTRIBUTES = 0o100644 << 16;
|
|
30
|
+
/**
|
|
31
|
+
* Whether an archive of `sourceSize` bytes with `entryCount` entries can be
|
|
32
|
+
* (partially) copied without zip64 structures: an output that only keeps a
|
|
33
|
+
* subset of the entries is never larger than its source.
|
|
34
|
+
*/
|
|
35
|
+
function fitsRawZip(sourceSize, entryCount) {
|
|
36
|
+
return sourceSize < MAX_UINT32 && entryCount < MAX_UINT16;
|
|
37
|
+
}
|
|
38
|
+
let crcTable;
|
|
39
|
+
function getCrcTable() {
|
|
40
|
+
if (!crcTable) {
|
|
41
|
+
crcTable = new Int32Array(256);
|
|
42
|
+
for (let n = 0; n < 256; n++) {
|
|
43
|
+
let c = n;
|
|
44
|
+
for (let k = 0; k < 8; k++)
|
|
45
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
46
|
+
crcTable[n] = c;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return crcTable;
|
|
50
|
+
}
|
|
51
|
+
/** CRC-32 (IEEE) continued from `seed` (the running value, not inverted) */
|
|
52
|
+
function crc32Update(seed, chunk) {
|
|
53
|
+
const table = getCrcTable();
|
|
54
|
+
let crc = ~seed;
|
|
55
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
56
|
+
crc = table[(crc ^ chunk[i]) & 0xff] ^ (crc >>> 8);
|
|
57
|
+
}
|
|
58
|
+
return ~crc >>> 0;
|
|
59
|
+
}
|
|
60
|
+
class RawZipWriter {
|
|
61
|
+
output;
|
|
62
|
+
out;
|
|
63
|
+
central = [];
|
|
64
|
+
offset = 0;
|
|
65
|
+
count = 0;
|
|
66
|
+
failure = null;
|
|
67
|
+
closed;
|
|
68
|
+
constructor(output) {
|
|
69
|
+
this.output = output;
|
|
70
|
+
this.out = fs_extra_1.default.createWriteStream(output);
|
|
71
|
+
this.closed = new Promise((resolve, reject) => {
|
|
72
|
+
this.out.once('close', resolve);
|
|
73
|
+
this.out.once('error', (error) => {
|
|
74
|
+
this.failure = error;
|
|
75
|
+
reject(error);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
// surfaced by the next write / end(), never as an unhandled rejection
|
|
79
|
+
this.closed.catch(() => { });
|
|
80
|
+
}
|
|
81
|
+
/** Copy one entry with its file data exactly as stored in the source. */
|
|
82
|
+
async addRawEntry(meta, data) {
|
|
83
|
+
if (meta.compressedSize > MAX_UINT32 ||
|
|
84
|
+
meta.uncompressedSize > MAX_UINT32 ||
|
|
85
|
+
this.offset > MAX_UINT32 ||
|
|
86
|
+
this.count >= MAX_UINT16) {
|
|
87
|
+
throw new Error(`zip64 required for ${meta.fileName}`);
|
|
88
|
+
}
|
|
89
|
+
const name = Buffer.from(meta.fileName, 'utf8');
|
|
90
|
+
if (name.length > MAX_UINT16) {
|
|
91
|
+
throw new Error(`file name too long: ${meta.fileName}`);
|
|
92
|
+
}
|
|
93
|
+
const flags = ((meta.generalPurposeBitFlag ?? 0) &
|
|
94
|
+
FLAG_KEEP_MASK &
|
|
95
|
+
~FLAG_DATA_DESCRIPTOR) |
|
|
96
|
+
FLAG_UTF8_NAMES;
|
|
97
|
+
const versionNeeded = Math.max(VERSION_NEEDED_DEFAULT, meta.versionNeededToExtract ?? 0);
|
|
98
|
+
const localOffset = this.offset;
|
|
99
|
+
const local = Buffer.alloc(30 + name.length);
|
|
100
|
+
local.writeUInt32LE(LOCAL_HEADER_SIGNATURE, 0);
|
|
101
|
+
local.writeUInt16LE(versionNeeded, 4);
|
|
102
|
+
local.writeUInt16LE(flags, 6);
|
|
103
|
+
local.writeUInt16LE(meta.compressionMethod, 8);
|
|
104
|
+
local.writeUInt16LE(meta.lastModFileTime, 10);
|
|
105
|
+
local.writeUInt16LE(meta.lastModFileDate, 12);
|
|
106
|
+
local.writeUInt32LE(meta.crc32 >>> 0, 14);
|
|
107
|
+
local.writeUInt32LE(meta.compressedSize, 18);
|
|
108
|
+
local.writeUInt32LE(meta.uncompressedSize, 22);
|
|
109
|
+
local.writeUInt16LE(name.length, 26);
|
|
110
|
+
local.writeUInt16LE(0, 28);
|
|
111
|
+
name.copy(local, 30);
|
|
112
|
+
await this.write(local);
|
|
113
|
+
let copied = 0;
|
|
114
|
+
for await (const chunk of data) {
|
|
115
|
+
copied += chunk.length;
|
|
116
|
+
await this.write(chunk);
|
|
117
|
+
}
|
|
118
|
+
if (copied !== meta.compressedSize) {
|
|
119
|
+
throw new Error(`${meta.fileName}: read ${copied} bytes, expected ${meta.compressedSize}`);
|
|
120
|
+
}
|
|
121
|
+
const header = Buffer.alloc(46 + name.length);
|
|
122
|
+
header.writeUInt32LE(CENTRAL_HEADER_SIGNATURE, 0);
|
|
123
|
+
header.writeUInt16LE(meta.versionMadeBy ?? VERSION_MADE_BY_UNIX, 4);
|
|
124
|
+
header.writeUInt16LE(versionNeeded, 6);
|
|
125
|
+
header.writeUInt16LE(flags, 8);
|
|
126
|
+
header.writeUInt16LE(meta.compressionMethod, 10);
|
|
127
|
+
header.writeUInt16LE(meta.lastModFileTime, 12);
|
|
128
|
+
header.writeUInt16LE(meta.lastModFileDate, 14);
|
|
129
|
+
header.writeUInt32LE(meta.crc32 >>> 0, 16);
|
|
130
|
+
header.writeUInt32LE(meta.compressedSize, 20);
|
|
131
|
+
header.writeUInt32LE(meta.uncompressedSize, 24);
|
|
132
|
+
header.writeUInt16LE(name.length, 28);
|
|
133
|
+
header.writeUInt16LE(0, 30); // extra
|
|
134
|
+
header.writeUInt16LE(0, 32); // comment
|
|
135
|
+
header.writeUInt16LE(0, 34); // disk number start
|
|
136
|
+
header.writeUInt16LE(meta.internalFileAttributes ?? 0, 36);
|
|
137
|
+
header.writeUInt32LE((meta.externalFileAttributes ?? UNIX_REGULAR_FILE_ATTRIBUTES) >>> 0, 38);
|
|
138
|
+
header.writeUInt32LE(localOffset, 42);
|
|
139
|
+
name.copy(header, 46);
|
|
140
|
+
this.central.push(header);
|
|
141
|
+
this.count += 1;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Add a local file uncompressed (its CRC is computed in a first pass).
|
|
145
|
+
* `like` lends its timestamp and attributes, e.g. the archive entry this
|
|
146
|
+
* file replaces.
|
|
147
|
+
*/
|
|
148
|
+
async addStoredFile(filePath, fileName, like) {
|
|
149
|
+
let crc = 0;
|
|
150
|
+
let size = 0;
|
|
151
|
+
for await (const chunk of fs_extra_1.default.createReadStream(filePath)) {
|
|
152
|
+
crc = crc32Update(crc, chunk);
|
|
153
|
+
size += chunk.length;
|
|
154
|
+
}
|
|
155
|
+
await this.addRawEntry({
|
|
156
|
+
fileName,
|
|
157
|
+
compressionMethod: 0,
|
|
158
|
+
crc32: crc,
|
|
159
|
+
compressedSize: size,
|
|
160
|
+
uncompressedSize: size,
|
|
161
|
+
lastModFileTime: like?.lastModFileTime ?? 0,
|
|
162
|
+
lastModFileDate: like?.lastModFileDate ?? 0x21, // 1980-01-01
|
|
163
|
+
externalFileAttributes: like?.externalFileAttributes,
|
|
164
|
+
versionMadeBy: like?.versionMadeBy,
|
|
165
|
+
}, fs_extra_1.default.createReadStream(filePath));
|
|
166
|
+
}
|
|
167
|
+
/** Write the central directory and close the file. */
|
|
168
|
+
async end() {
|
|
169
|
+
const cdOffset = this.offset;
|
|
170
|
+
for (const header of this.central)
|
|
171
|
+
await this.write(header);
|
|
172
|
+
const cdSize = this.offset - cdOffset;
|
|
173
|
+
if (cdOffset > MAX_UINT32 || cdSize > MAX_UINT32) {
|
|
174
|
+
throw new Error('zip64 required for the central directory');
|
|
175
|
+
}
|
|
176
|
+
const eocd = Buffer.alloc(22);
|
|
177
|
+
eocd.writeUInt32LE(EOCD_SIGNATURE, 0);
|
|
178
|
+
eocd.writeUInt16LE(0, 4);
|
|
179
|
+
eocd.writeUInt16LE(0, 6);
|
|
180
|
+
eocd.writeUInt16LE(this.count, 8);
|
|
181
|
+
eocd.writeUInt16LE(this.count, 10);
|
|
182
|
+
eocd.writeUInt32LE(cdSize, 12);
|
|
183
|
+
eocd.writeUInt32LE(cdOffset, 16);
|
|
184
|
+
eocd.writeUInt16LE(0, 20);
|
|
185
|
+
await this.write(eocd);
|
|
186
|
+
this.out.end();
|
|
187
|
+
await this.closed;
|
|
188
|
+
}
|
|
189
|
+
/** Stop writing and remove the partial output. */
|
|
190
|
+
async abort() {
|
|
191
|
+
this.out.destroy();
|
|
192
|
+
await this.closed.catch(() => { });
|
|
193
|
+
await fs_extra_1.default.remove(this.output).catch(() => { });
|
|
194
|
+
}
|
|
195
|
+
write(chunk) {
|
|
196
|
+
if (this.failure)
|
|
197
|
+
return Promise.reject(this.failure);
|
|
198
|
+
this.offset += chunk.length;
|
|
199
|
+
return new Promise((resolve, reject) => {
|
|
200
|
+
this.out.write(chunk, (error) => (error ? reject(error) : resolve()));
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.RawZipWriter = RawZipWriter;
|