react-native-update-cli 2.22.5 → 2.22.7
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/lib/bundle-runner.d.ts +26 -0
- package/lib/bundle-runner.js +188 -89
- package/lib/locales/en.d.ts +2 -0
- package/lib/locales/en.js +2 -0
- package/lib/locales/zh.d.ts +2 -0
- package/lib/locales/zh.js +2 -0
- package/lib/package.js +9 -0
- package/lib/utils/hermes-base.d.ts +43 -3
- package/lib/utils/hermes-base.js +257 -47
- package/lib/utils/zip-range.d.ts +111 -0
- package/lib/utils/zip-range.js +466 -0
- package/lib/versions.js +12 -3
- package/package.json +1 -1
- package/src/bundle-runner.ts +252 -116
- package/src/locales/en.ts +4 -0
- package/src/locales/zh.ts +4 -0
- package/src/package.ts +16 -1
- package/src/utils/hermes-base.ts +326 -50
- package/src/utils/zip-range.ts +571 -0
- package/src/versions.ts +18 -3
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
// Byte-range access to zip archives.
|
|
2
|
+
//
|
|
3
|
+
// Two halves of the same trick: at publish time `locateZipEntry` records where
|
|
4
|
+
// the bundle's compressed bytes sit inside the ppk (so the server can hand
|
|
5
|
+
// that location to the next build), and at build time `fetchZipEntryData` /
|
|
6
|
+
// `openRemoteZip` read just the bundle of a remote archive through HTTP Range
|
|
7
|
+
// requests instead of downloading the whole ppk / apk / ipa. Every helper
|
|
8
|
+
// throws on anything unexpected; callers fall back to a full download.
|
|
9
|
+
import fs from 'fs-extra';
|
|
10
|
+
import { PassThrough, Readable } from 'stream';
|
|
11
|
+
import {
|
|
12
|
+
type Entry,
|
|
13
|
+
fromRandomAccessReader,
|
|
14
|
+
open as openZipFile,
|
|
15
|
+
RandomAccessReader,
|
|
16
|
+
type ZipFile,
|
|
17
|
+
} from 'yauzl';
|
|
18
|
+
import { inflateRawSync } from 'zlib';
|
|
19
|
+
import { readEntry } from './zip-entries';
|
|
20
|
+
|
|
21
|
+
export const ZIP_STORED = 0;
|
|
22
|
+
export const ZIP_DEFLATED = 8;
|
|
23
|
+
|
|
24
|
+
const LOCAL_HEADER_SIGNATURE = 0x04034b50;
|
|
25
|
+
const LOCAL_HEADER_SIZE = 30;
|
|
26
|
+
/**
|
|
27
|
+
* Exactly what yauzl reads to find the EOCD (22 + 65535 comment + 20 zip64
|
|
28
|
+
* locator), so its first read is a cache hit; small archives fit entirely.
|
|
29
|
+
*/
|
|
30
|
+
const TAIL_BYTES = 22 + 0xffff + 20;
|
|
31
|
+
/** minimum span fetched for header reads, so header + directory share a request */
|
|
32
|
+
const CHUNK_BYTES = 64 * 1024;
|
|
33
|
+
/** extra bytes past a hinted entry, covering a longer local extra field */
|
|
34
|
+
const HINT_SLACK_BYTES = 4 * 1024;
|
|
35
|
+
|
|
36
|
+
export interface ZipEntryLocation {
|
|
37
|
+
fileName: string;
|
|
38
|
+
/** absolute offset of the compressed data, past the local file header */
|
|
39
|
+
dataOffset: number;
|
|
40
|
+
compressedSize: number;
|
|
41
|
+
uncompressedSize: number;
|
|
42
|
+
/** 0 = stored, 8 = deflate; anything else cannot be inflated here */
|
|
43
|
+
compressionMethod: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Data location of the first entry matching `matches` in a local zip. */
|
|
47
|
+
export async function locateZipEntry(
|
|
48
|
+
zipPath: string,
|
|
49
|
+
matches: (name: string) => boolean,
|
|
50
|
+
): Promise<ZipEntryLocation | null> {
|
|
51
|
+
const found = await openFirstEntry(zipPath, matches);
|
|
52
|
+
if (!found) return null;
|
|
53
|
+
found.zipFile.close();
|
|
54
|
+
return locationOfEntry(zipPath, found.entry);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Content and data location of the first matching entry in one pass over the
|
|
59
|
+
* archive (publish needs both: the bundle to hash and cache, its location to
|
|
60
|
+
* report to the server).
|
|
61
|
+
*/
|
|
62
|
+
export async function readZipEntryWithLocation(
|
|
63
|
+
zipPath: string,
|
|
64
|
+
matches: (name: string) => boolean,
|
|
65
|
+
): Promise<{ data: Buffer; location: ZipEntryLocation } | null> {
|
|
66
|
+
const found = await openFirstEntry(zipPath, matches);
|
|
67
|
+
if (!found) return null;
|
|
68
|
+
let data: Buffer;
|
|
69
|
+
try {
|
|
70
|
+
data = await readEntry(found.entry, found.zipFile);
|
|
71
|
+
} finally {
|
|
72
|
+
found.zipFile.close();
|
|
73
|
+
}
|
|
74
|
+
return { data, location: await locationOfEntry(zipPath, found.entry) };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function locationOfEntry(
|
|
78
|
+
zipPath: string,
|
|
79
|
+
entry: Entry,
|
|
80
|
+
): Promise<ZipEntryLocation> {
|
|
81
|
+
const header = Buffer.alloc(LOCAL_HEADER_SIZE);
|
|
82
|
+
const fd = await fs.open(zipPath, 'r');
|
|
83
|
+
try {
|
|
84
|
+
const { bytesRead } = await fs.read(
|
|
85
|
+
fd,
|
|
86
|
+
header,
|
|
87
|
+
0,
|
|
88
|
+
LOCAL_HEADER_SIZE,
|
|
89
|
+
entry.relativeOffsetOfLocalHeader,
|
|
90
|
+
);
|
|
91
|
+
if (bytesRead !== LOCAL_HEADER_SIZE) {
|
|
92
|
+
throw new Error('truncated local file header');
|
|
93
|
+
}
|
|
94
|
+
} finally {
|
|
95
|
+
await fs.close(fd);
|
|
96
|
+
}
|
|
97
|
+
if (header.readUInt32LE(0) !== LOCAL_HEADER_SIGNATURE) {
|
|
98
|
+
throw new Error('invalid local file header signature');
|
|
99
|
+
}
|
|
100
|
+
const fileNameLength = header.readUInt16LE(26);
|
|
101
|
+
const extraFieldLength = header.readUInt16LE(28);
|
|
102
|
+
return {
|
|
103
|
+
fileName: entry.fileName,
|
|
104
|
+
dataOffset:
|
|
105
|
+
entry.relativeOffsetOfLocalHeader +
|
|
106
|
+
LOCAL_HEADER_SIZE +
|
|
107
|
+
fileNameLength +
|
|
108
|
+
extraFieldLength,
|
|
109
|
+
compressedSize: entry.compressedSize,
|
|
110
|
+
uncompressedSize: entry.uncompressedSize,
|
|
111
|
+
compressionMethod: entry.compressionMethod,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Open a local zip and stop at the first matching entry (caller closes). */
|
|
116
|
+
function openFirstEntry(
|
|
117
|
+
zipPath: string,
|
|
118
|
+
matches: (name: string) => boolean,
|
|
119
|
+
): Promise<{ zipFile: ZipFile; entry: Entry } | null> {
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
openZipFile(
|
|
122
|
+
zipPath,
|
|
123
|
+
{ lazyEntries: true, autoClose: false },
|
|
124
|
+
(err, zipFile) => {
|
|
125
|
+
if (err) return reject(err);
|
|
126
|
+
let settled = false;
|
|
127
|
+
zipFile.on('error', (error) => {
|
|
128
|
+
if (!settled) {
|
|
129
|
+
settled = true;
|
|
130
|
+
reject(error);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
zipFile.on('end', () => {
|
|
134
|
+
if (!settled) {
|
|
135
|
+
settled = true;
|
|
136
|
+
zipFile.close();
|
|
137
|
+
resolve(null);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
zipFile.on('entry', (entry: Entry) => {
|
|
141
|
+
if (settled) return;
|
|
142
|
+
if (matches(entry.fileName)) {
|
|
143
|
+
settled = true;
|
|
144
|
+
resolve({ zipFile, entry });
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
zipFile.readEntry();
|
|
148
|
+
});
|
|
149
|
+
zipFile.readEntry();
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The `bundleOffset` / `bundleCompressedSize` / `bundleCompression` fields for
|
|
157
|
+
* version/create and package/create, or nothing when the entry cannot be
|
|
158
|
+
* fetched by a single Range request (unknown, empty or exotic compression).
|
|
159
|
+
*/
|
|
160
|
+
export function bundleLocationFields(
|
|
161
|
+
location: ZipEntryLocation | null | undefined,
|
|
162
|
+
): Record<string, number> {
|
|
163
|
+
if (
|
|
164
|
+
!location ||
|
|
165
|
+
location.compressedSize <= 0 ||
|
|
166
|
+
(location.compressionMethod !== ZIP_STORED &&
|
|
167
|
+
location.compressionMethod !== ZIP_DEFLATED)
|
|
168
|
+
) {
|
|
169
|
+
return {};
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
bundleOffset: location.dataOffset,
|
|
173
|
+
bundleCompressedSize: location.compressedSize,
|
|
174
|
+
bundleCompression: location.compressionMethod,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
// HTTP Range
|
|
180
|
+
// ---------------------------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
/** The server answered a Range request with the whole body (HTTP 200). */
|
|
183
|
+
export class RangeUnsupportedError extends Error {
|
|
184
|
+
constructor(readonly response: Response) {
|
|
185
|
+
super(`HTTP ${response.status} without Content-Range`);
|
|
186
|
+
this.name = 'RangeUnsupportedError';
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
interface ContentRange {
|
|
191
|
+
start: number;
|
|
192
|
+
end: number;
|
|
193
|
+
total: number;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function parseContentRange(header: string | null): ContentRange | null {
|
|
197
|
+
const match = /^bytes\s+(\d+)-(\d+)\/(\d+)$/i.exec(header?.trim() ?? '');
|
|
198
|
+
if (!match) return null;
|
|
199
|
+
const [start, end, total] = match.slice(1).map(Number);
|
|
200
|
+
if (end < start || end >= total) return null;
|
|
201
|
+
return { start, end, total };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** One Range request; `end` is exclusive. Resolves with a 206 response. */
|
|
205
|
+
async function fetchRange(
|
|
206
|
+
url: string,
|
|
207
|
+
start: number,
|
|
208
|
+
end: number,
|
|
209
|
+
): Promise<Response & { range: ContentRange }> {
|
|
210
|
+
const response = await fetch(url, {
|
|
211
|
+
headers: {
|
|
212
|
+
Range: `bytes=${start}-${end - 1}`,
|
|
213
|
+
'Accept-Encoding': 'identity',
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
if (response.status === 200) throw new RangeUnsupportedError(response);
|
|
217
|
+
if (response.status !== 206 || !response.body) {
|
|
218
|
+
await response.body?.cancel().catch(() => {});
|
|
219
|
+
throw new Error(`HTTP ${response.status} for bytes=${start}-${end - 1}`);
|
|
220
|
+
}
|
|
221
|
+
const range = parseContentRange(response.headers.get('content-range'));
|
|
222
|
+
if (!range || range.start !== start || range.end !== end - 1) {
|
|
223
|
+
await response.body.cancel().catch(() => {});
|
|
224
|
+
throw new Error(
|
|
225
|
+
`unexpected Content-Range ${response.headers.get('content-range')}`,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
return Object.assign(response, { range });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function fetchRangeBuffer(
|
|
232
|
+
url: string,
|
|
233
|
+
start: number,
|
|
234
|
+
end: number,
|
|
235
|
+
): Promise<{ data: Buffer; total: number }> {
|
|
236
|
+
const response = await fetchRange(url, start, end);
|
|
237
|
+
const data = Buffer.from(await response.arrayBuffer());
|
|
238
|
+
if (data.length !== end - start) {
|
|
239
|
+
throw new Error(
|
|
240
|
+
`range returned ${data.length} bytes, expected ${end - start}`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
return { data, total: response.range.total };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Fetch one entry whose compressed bytes are already located (the ppk's own
|
|
248
|
+
* publish-time report) and decompress it. Returns the raw entry content.
|
|
249
|
+
*/
|
|
250
|
+
export async function fetchZipEntryData(
|
|
251
|
+
url: string,
|
|
252
|
+
location: Pick<
|
|
253
|
+
ZipEntryLocation,
|
|
254
|
+
'dataOffset' | 'compressedSize' | 'compressionMethod'
|
|
255
|
+
>,
|
|
256
|
+
): Promise<{ data: Buffer; fetchedBytes: number; totalBytes: number }> {
|
|
257
|
+
const { dataOffset, compressedSize, compressionMethod } = location;
|
|
258
|
+
if (compressionMethod !== ZIP_STORED && compressionMethod !== ZIP_DEFLATED) {
|
|
259
|
+
throw new Error(`unsupported compression method ${compressionMethod}`);
|
|
260
|
+
}
|
|
261
|
+
if (
|
|
262
|
+
!Number.isInteger(dataOffset) ||
|
|
263
|
+
!Number.isInteger(compressedSize) ||
|
|
264
|
+
dataOffset < 0 ||
|
|
265
|
+
compressedSize <= 0
|
|
266
|
+
) {
|
|
267
|
+
throw new Error('invalid bundle location');
|
|
268
|
+
}
|
|
269
|
+
const { data, total } = await fetchRangeBuffer(
|
|
270
|
+
url,
|
|
271
|
+
dataOffset,
|
|
272
|
+
dataOffset + compressedSize,
|
|
273
|
+
);
|
|
274
|
+
return {
|
|
275
|
+
data: compressionMethod === ZIP_DEFLATED ? inflateRawSync(data) : data,
|
|
276
|
+
fetchedBytes: compressedSize,
|
|
277
|
+
totalBytes: total,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* yauzl reader over HTTP Range. Header reads (EOCD, central directory, local
|
|
283
|
+
* file headers) are widened to CHUNK_BYTES and cached — adjacent or
|
|
284
|
+
* overlapping chunks are merged so a read never re-fetches bytes it already
|
|
285
|
+
* has — and entry data reuses whatever prefix the cache holds (typically the
|
|
286
|
+
* bytes following the local file header) before requesting the rest.
|
|
287
|
+
*/
|
|
288
|
+
export class HttpRangeReader extends RandomAccessReader {
|
|
289
|
+
private chunks: { start: number; data: Buffer }[] = [];
|
|
290
|
+
/** span a header read inside it should widen to (see `hintEntry`) */
|
|
291
|
+
private hint: { start: number; end: number } | null = null;
|
|
292
|
+
requests = 0;
|
|
293
|
+
fetchedBytes = 0;
|
|
294
|
+
|
|
295
|
+
constructor(
|
|
296
|
+
private readonly url: string,
|
|
297
|
+
readonly totalSize: number,
|
|
298
|
+
tail?: { start: number; data: Buffer },
|
|
299
|
+
) {
|
|
300
|
+
super();
|
|
301
|
+
if (tail) this.addChunk(tail.start, tail.data);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* The next entry to be read: yauzl first reads its local file header, and
|
|
306
|
+
* widening that read to the entry's data makes the data stream a cache hit
|
|
307
|
+
* — one request for header + bundle instead of two. The central directory
|
|
308
|
+
* may understate the local header's extra field, hence the slack.
|
|
309
|
+
*/
|
|
310
|
+
hintEntry(entry: Entry): void {
|
|
311
|
+
const start = entry.relativeOffsetOfLocalHeader;
|
|
312
|
+
const end =
|
|
313
|
+
start +
|
|
314
|
+
LOCAL_HEADER_SIZE +
|
|
315
|
+
entry.fileNameLength +
|
|
316
|
+
entry.extraFieldLength +
|
|
317
|
+
entry.compressedSize +
|
|
318
|
+
HINT_SLACK_BYTES;
|
|
319
|
+
this.hint = { start, end: Math.min(end, this.totalSize) };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Cache `data` at `start`, merging with any chunk it touches. */
|
|
323
|
+
addChunk(start: number, data: Buffer): void {
|
|
324
|
+
if (data.length === 0) return;
|
|
325
|
+
let lo = start;
|
|
326
|
+
let hi = start + data.length;
|
|
327
|
+
const pieces: { start: number; data: Buffer }[] = [{ start, data }];
|
|
328
|
+
const rest: { start: number; data: Buffer }[] = [];
|
|
329
|
+
for (const chunk of this.chunks) {
|
|
330
|
+
const chunkEnd = chunk.start + chunk.data.length;
|
|
331
|
+
if (chunk.start <= hi && chunkEnd >= lo) {
|
|
332
|
+
pieces.push(chunk);
|
|
333
|
+
lo = Math.min(lo, chunk.start);
|
|
334
|
+
hi = Math.max(hi, chunkEnd);
|
|
335
|
+
} else {
|
|
336
|
+
rest.push(chunk);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (pieces.length === 1) {
|
|
340
|
+
rest.push(pieces[0]);
|
|
341
|
+
} else {
|
|
342
|
+
const merged = Buffer.alloc(hi - lo);
|
|
343
|
+
// the new data is written last so it wins over stale overlaps
|
|
344
|
+
for (const piece of pieces.reverse()) {
|
|
345
|
+
piece.data.copy(merged, piece.start - lo);
|
|
346
|
+
}
|
|
347
|
+
rest.push({ start: lo, data: merged });
|
|
348
|
+
}
|
|
349
|
+
rest.sort((a, b) => a.start - b.start);
|
|
350
|
+
this.chunks = rest;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** cached bytes starting exactly at `start`, up to `end` (may be shorter) */
|
|
354
|
+
private cachedPrefix(start: number, end: number): Buffer | null {
|
|
355
|
+
for (const chunk of this.chunks) {
|
|
356
|
+
const chunkEnd = chunk.start + chunk.data.length;
|
|
357
|
+
if (start >= chunk.start && start < chunkEnd) {
|
|
358
|
+
return chunk.data.subarray(
|
|
359
|
+
start - chunk.start,
|
|
360
|
+
Math.min(end, chunkEnd) - chunk.start,
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return null;
|
|
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;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** start of the cached chunk holding byte `end - 1`, when there is one */
|
|
373
|
+
private cachedSuffixStart(end: number): number | null {
|
|
374
|
+
for (const chunk of this.chunks) {
|
|
375
|
+
if (end - 1 >= chunk.start && end - 1 < chunk.start + chunk.data.length) {
|
|
376
|
+
return chunk.start;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
_readStreamForRange(start: number, end: number): Readable {
|
|
383
|
+
const prefix = this.cachedPrefix(start, end);
|
|
384
|
+
if (prefix && prefix.length === end - start) return Readable.from([prefix]);
|
|
385
|
+
const out = new PassThrough();
|
|
386
|
+
if (prefix) out.write(prefix);
|
|
387
|
+
const from = start + (prefix?.length ?? 0);
|
|
388
|
+
this.requests++;
|
|
389
|
+
fetchRange(this.url, from, end)
|
|
390
|
+
.then((response) => {
|
|
391
|
+
this.fetchedBytes += end - from;
|
|
392
|
+
const body = Readable.fromWeb(response.body as any);
|
|
393
|
+
body.on('error', (error) => out.destroy(error));
|
|
394
|
+
body.pipe(out);
|
|
395
|
+
})
|
|
396
|
+
.catch((error) => out.destroy(error));
|
|
397
|
+
return out;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
read(
|
|
401
|
+
buffer: Buffer,
|
|
402
|
+
offset: number,
|
|
403
|
+
length: number,
|
|
404
|
+
position: number,
|
|
405
|
+
callback: (err: Error | null, bytesRead?: number) => void,
|
|
406
|
+
): void {
|
|
407
|
+
const hit = this.cached(position, position + length);
|
|
408
|
+
if (hit) {
|
|
409
|
+
hit.copy(buffer, offset);
|
|
410
|
+
setImmediate(() => callback(null, length));
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
// fetch only what the cache lacks: skip a cached prefix, stop at a cached
|
|
414
|
+
// suffix (yauzl's EOCD read may just exceed the tail), and otherwise widen
|
|
415
|
+
// so the next header read is free
|
|
416
|
+
const requestEnd = position + length;
|
|
417
|
+
const from =
|
|
418
|
+
position + (this.cachedPrefix(position, requestEnd)?.length ?? 0);
|
|
419
|
+
const suffixStart = this.cachedSuffixStart(requestEnd);
|
|
420
|
+
let end: number;
|
|
421
|
+
if (suffixStart !== null && suffixStart > from) {
|
|
422
|
+
end = suffixStart;
|
|
423
|
+
} else {
|
|
424
|
+
end = Math.min(this.totalSize, Math.max(requestEnd, from + CHUNK_BYTES));
|
|
425
|
+
if (
|
|
426
|
+
this.hint &&
|
|
427
|
+
position >= this.hint.start &&
|
|
428
|
+
position < this.hint.end
|
|
429
|
+
) {
|
|
430
|
+
end = Math.max(end, this.hint.end);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
this.requests++;
|
|
434
|
+
fetchRangeBuffer(this.url, from, end).then(
|
|
435
|
+
({ data }) => {
|
|
436
|
+
this.fetchedBytes += data.length;
|
|
437
|
+
this.addChunk(from, data);
|
|
438
|
+
const full = this.cached(position, position + length);
|
|
439
|
+
if (!full) {
|
|
440
|
+
callback(new Error('range read did not cover the requested bytes'));
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
full.copy(buffer, offset);
|
|
444
|
+
callback(null, length);
|
|
445
|
+
},
|
|
446
|
+
(error) => callback(error),
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** End-of-central-directory record: where the central directory lives. */
|
|
452
|
+
const EOCD_SIGNATURE = 0x06054b50;
|
|
453
|
+
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
|
+
|
|
457
|
+
export function parseEndOfCentralDirectory(
|
|
458
|
+
tail: Buffer,
|
|
459
|
+
): { cdOffset: number; cdSize: number } | null {
|
|
460
|
+
for (let i = tail.length - EOCD_SIZE; i >= 0; i--) {
|
|
461
|
+
if (tail.readUInt32LE(i) !== EOCD_SIGNATURE) continue;
|
|
462
|
+
const commentLength = tail.readUInt16LE(i + 20);
|
|
463
|
+
if (i + EOCD_SIZE + commentLength !== tail.length) continue;
|
|
464
|
+
const cdSize = tail.readUInt32LE(i + 12);
|
|
465
|
+
const cdOffset = tail.readUInt32LE(i + 16);
|
|
466
|
+
// zip64 markers: let yauzl resolve the real values itself
|
|
467
|
+
if (cdSize === 0xffffffff || cdOffset === 0xffffffff) return null;
|
|
468
|
+
return { cdOffset, cdSize };
|
|
469
|
+
}
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
export type RemoteZip =
|
|
474
|
+
| { kind: 'zip'; zipFile: ZipFile; reader: HttpRangeReader }
|
|
475
|
+
/** the server ignored Range: the whole archive is streaming in `response` */
|
|
476
|
+
| { kind: 'full'; response: Response };
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Open a remote zip by fetching its tail (EOCD + central directory for
|
|
480
|
+
* typical archives) and letting yauzl read the rest on demand.
|
|
481
|
+
*/
|
|
482
|
+
export async function openRemoteZip(url: string): Promise<RemoteZip> {
|
|
483
|
+
const response = await fetch(url, {
|
|
484
|
+
headers: { Range: `bytes=-${TAIL_BYTES}`, 'Accept-Encoding': 'identity' },
|
|
485
|
+
});
|
|
486
|
+
if (!response.ok || !response.body) {
|
|
487
|
+
await response.body?.cancel().catch(() => {});
|
|
488
|
+
throw new Error(`HTTP ${response.status}`);
|
|
489
|
+
}
|
|
490
|
+
if (response.status !== 206) return { kind: 'full', response };
|
|
491
|
+
const range = parseContentRange(response.headers.get('content-range'));
|
|
492
|
+
if (!range) {
|
|
493
|
+
await response.body.cancel().catch(() => {});
|
|
494
|
+
throw new Error(
|
|
495
|
+
`unexpected Content-Range ${response.headers.get('content-range')}`,
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
const data = Buffer.from(await response.arrayBuffer());
|
|
499
|
+
if (data.length !== range.end - range.start + 1) {
|
|
500
|
+
throw new Error(
|
|
501
|
+
`tail returned ${data.length} bytes, expected ${range.end - range.start + 1}`,
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
const reader = new HttpRangeReader(url, range.total, {
|
|
505
|
+
start: range.start,
|
|
506
|
+
data,
|
|
507
|
+
});
|
|
508
|
+
reader.requests = 1;
|
|
509
|
+
reader.fetchedBytes = data.length;
|
|
510
|
+
// Large archives (apk / ipa with thousands of files) keep their central
|
|
511
|
+
// 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.
|
|
513
|
+
const eocd = parseEndOfCentralDirectory(data);
|
|
514
|
+
if (
|
|
515
|
+
eocd &&
|
|
516
|
+
eocd.cdOffset < range.start &&
|
|
517
|
+
range.start - eocd.cdOffset <= MAX_PREFETCH_BYTES
|
|
518
|
+
) {
|
|
519
|
+
const { data: directory } = await fetchRangeBuffer(
|
|
520
|
+
url,
|
|
521
|
+
eocd.cdOffset,
|
|
522
|
+
range.start,
|
|
523
|
+
);
|
|
524
|
+
reader.requests++;
|
|
525
|
+
reader.fetchedBytes += directory.length;
|
|
526
|
+
reader.addChunk(eocd.cdOffset, directory);
|
|
527
|
+
}
|
|
528
|
+
const zipFile = await new Promise<ZipFile>((resolve, reject) => {
|
|
529
|
+
fromRandomAccessReader(
|
|
530
|
+
reader,
|
|
531
|
+
range.total,
|
|
532
|
+
{ lazyEntries: true, autoClose: false },
|
|
533
|
+
(err, file) => (err ? reject(err) : resolve(file)),
|
|
534
|
+
);
|
|
535
|
+
});
|
|
536
|
+
return { kind: 'zip', zipFile, reader };
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Read the first matching entry of an opened remote zip, then close it. With
|
|
541
|
+
* `reader` given, the entry's header and data are fetched in one request.
|
|
542
|
+
*/
|
|
543
|
+
export function readRemoteZipEntry(
|
|
544
|
+
zipFile: ZipFile,
|
|
545
|
+
matches: (name: string) => boolean,
|
|
546
|
+
reader?: HttpRangeReader,
|
|
547
|
+
): Promise<Buffer | null> {
|
|
548
|
+
return new Promise((resolve, reject) => {
|
|
549
|
+
let settled = false;
|
|
550
|
+
const finish = (error: Error | null, result: Buffer | null = null) => {
|
|
551
|
+
if (settled) return;
|
|
552
|
+
settled = true;
|
|
553
|
+
zipFile.close();
|
|
554
|
+
error ? reject(error) : resolve(result);
|
|
555
|
+
};
|
|
556
|
+
zipFile.on('error', (error) => finish(error));
|
|
557
|
+
zipFile.on('end', () => finish(null, null));
|
|
558
|
+
zipFile.on('entry', (entry: Entry) => {
|
|
559
|
+
if (!matches(entry.fileName)) {
|
|
560
|
+
zipFile.readEntry();
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
reader?.hintEntry(entry);
|
|
564
|
+
readEntry(entry, zipFile).then(
|
|
565
|
+
(data) => finish(null, data),
|
|
566
|
+
(error) => finish(error),
|
|
567
|
+
);
|
|
568
|
+
});
|
|
569
|
+
zipFile.readEntry();
|
|
570
|
+
});
|
|
571
|
+
}
|
package/src/versions.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { depVersions } from './utils/dep-versions';
|
|
|
10
10
|
import { getCommitInfo } from './utils/git';
|
|
11
11
|
import { getHbcVersion } from './utils/hbcTransform';
|
|
12
12
|
import {
|
|
13
|
+
BUNDLE_ENTRY_NAMES,
|
|
13
14
|
cachePut,
|
|
14
15
|
extractBundleFromArchive,
|
|
15
16
|
type HermesBaseMeta,
|
|
@@ -17,6 +18,10 @@ import {
|
|
|
17
18
|
} from './utils/hermes-base';
|
|
18
19
|
import { t } from './utils/i18n';
|
|
19
20
|
import { getBooleanOption, getStringListOption } from './utils/options';
|
|
21
|
+
import {
|
|
22
|
+
bundleLocationFields,
|
|
23
|
+
readZipEntryWithLocation,
|
|
24
|
+
} from './utils/zip-range';
|
|
20
25
|
|
|
21
26
|
interface VersionCommandOptions {
|
|
22
27
|
[key: string]: unknown;
|
|
@@ -474,12 +479,22 @@ async function describePpkBundle(
|
|
|
474
479
|
// server version — strict, tolerant or too old to know the field — accepts.
|
|
475
480
|
const meta: Record<string, unknown> = {};
|
|
476
481
|
try {
|
|
477
|
-
|
|
482
|
+
// one pass over the ppk: the bundle itself plus where its compressed
|
|
483
|
+
// bytes live, so the next build can fetch just them with one HTTP Range
|
|
484
|
+
// request
|
|
485
|
+
const found = ppkPath.toLowerCase().endsWith('.ppk')
|
|
486
|
+
? await readZipEntryWithLocation(ppkPath, (name) =>
|
|
487
|
+
BUNDLE_ENTRY_NAMES.includes(name),
|
|
488
|
+
).catch(() => null)
|
|
489
|
+
: null;
|
|
490
|
+
const bundle = found?.data ?? (await extractBundleFromArchive(ppkPath));
|
|
478
491
|
if (bundle) {
|
|
479
|
-
|
|
492
|
+
const bundleHash = sha256Hex(bundle);
|
|
493
|
+
meta.bundleHash = bundleHash;
|
|
480
494
|
const hbcVersion = getHbcVersion(bundle) ?? base?.bytecodeVersion;
|
|
481
495
|
if (hbcVersion != null) meta.bytecodeVersion = hbcVersion;
|
|
482
|
-
await cachePut(bundle).catch(() => {});
|
|
496
|
+
await cachePut(bundle, undefined, bundleHash).catch(() => {});
|
|
497
|
+
Object.assign(meta, bundleLocationFields(found?.location));
|
|
483
498
|
}
|
|
484
499
|
} catch {
|
|
485
500
|
// best effort: metadata never blocks a publish
|