react-native-update-cli 2.24.1 → 2.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -4
- package/README.zh-CN.md +11 -4
- package/cli.json +7 -1
- package/lib/api.d.ts +2 -0
- package/lib/api.js +63 -18
- package/lib/app.js +25 -3
- package/lib/bin.js +39 -45
- package/lib/bundle-runner.d.ts +27 -0
- package/lib/bundle-runner.js +240 -21
- package/lib/bundle.d.ts +1 -1
- package/lib/bundle.js +19 -6
- package/lib/commands.d.ts +6 -0
- package/lib/commands.js +65 -0
- package/lib/locales/en.d.ts +5 -14
- package/lib/locales/en.js +5 -14
- package/lib/locales/zh.d.ts +5 -13
- package/lib/locales/zh.js +5 -13
- package/lib/package.js +4 -0
- package/lib/provider.d.ts +1 -1
- package/lib/provider.js +3 -1
- package/lib/symbolicate.js +11 -3
- package/lib/types.d.ts +1 -0
- package/lib/user.js +6 -0
- package/lib/utils/add-gitignore.js +7 -1
- package/lib/utils/dep-versions.d.ts +7 -0
- package/lib/utils/dep-versions.js +78 -26
- package/lib/utils/git.js +9 -6
- package/lib/utils/hermes-base.js +2 -1
- package/lib/utils/i18n.d.ts +5 -0
- package/lib/utils/i18n.js +14 -1
- package/lib/utils/index.d.ts +2 -1
- package/lib/utils/index.js +9 -4
- package/lib/utils/latest-version/index.js +8 -0
- package/lib/utils/runtime.d.ts +21 -0
- package/lib/utils/runtime.js +138 -5
- package/lib/utils/slim-sourcemap.d.ts +11 -0
- package/lib/utils/slim-sourcemap.js +22 -0
- package/lib/utils/zip-range.js +3 -2
- package/lib/versions.js +16 -15
- package/package.json +4 -2
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -17
- package/src/api.ts +0 -470
- package/src/app.ts +0 -241
- package/src/auto-update.ts +0 -647
- package/src/bin-cresc.ts +0 -8
- package/src/bin.ts +0 -115
- package/src/bundle-pack.ts +0 -61
- package/src/bundle-runner.ts +0 -1430
- package/src/bundle.ts +0 -371
- package/src/cache.ts +0 -20
- package/src/diff.ts +0 -1010
- package/src/exports.ts +0 -17
- package/src/index.ts +0 -1
- package/src/install.ts +0 -50
- package/src/locales/en.ts +0 -257
- package/src/locales/zh.ts +0 -234
- package/src/native-package.ts +0 -338
- package/src/package.ts +0 -482
- package/src/provider.ts +0 -294
- package/src/symbolicate.ts +0 -159
- package/src/types.ts +0 -150
- package/src/user.ts +0 -39
- package/src/utils/add-gitignore.ts +0 -35
- package/src/utils/app-info-parser/aab.ts +0 -241
- package/src/utils/app-info-parser/apk.ts +0 -92
- package/src/utils/app-info-parser/app.ts +0 -7
- package/src/utils/app-info-parser/index.ts +0 -54
- package/src/utils/app-info-parser/ipa.ts +0 -109
- package/src/utils/app-info-parser/resource-finder.ts +0 -508
- package/src/utils/app-info-parser/utils.ts +0 -161
- package/src/utils/app-info-parser/xml-parser/binary.ts +0 -693
- package/src/utils/app-info-parser/xml-parser/manifest.ts +0 -228
- package/src/utils/app-info-parser/zip.ts +0 -108
- package/src/utils/check-lockfile.ts +0 -93
- package/src/utils/check-plugin.ts +0 -38
- package/src/utils/command-result.ts +0 -24
- package/src/utils/constants.ts +0 -25
- package/src/utils/dep-versions.ts +0 -83
- package/src/utils/git.ts +0 -71
- package/src/utils/hbcTransform.ts +0 -649
- package/src/utils/hermes-base.ts +0 -1137
- package/src/utils/http-helper.ts +0 -152
- package/src/utils/i18n.ts +0 -39
- package/src/utils/index.ts +0 -698
- package/src/utils/latest-version/index.ts +0 -573
- package/src/utils/options.ts +0 -78
- package/src/utils/plugin-config.ts +0 -32
- package/src/utils/runtime.ts +0 -187
- package/src/utils/slim-sourcemap.ts +0 -83
- package/src/utils/zip-entries.ts +0 -209
- package/src/utils/zip-options.ts +0 -178
- package/src/utils/zip-range.ts +0 -727
- package/src/utils/zip-raw-writer.ts +0 -247
- package/src/versions.ts +0 -894
package/src/utils/zip-range.ts
DELETED
|
@@ -1,727 +0,0 @@
|
|
|
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
|
-
/** 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
|
-
|
|
208
|
-
/** The server answered a Range request with the whole body (HTTP 200). */
|
|
209
|
-
export class RangeUnsupportedError extends Error {
|
|
210
|
-
constructor(readonly response: Response) {
|
|
211
|
-
super(`HTTP ${response.status} without Content-Range`);
|
|
212
|
-
this.name = 'RangeUnsupportedError';
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
interface ContentRange {
|
|
217
|
-
start: number;
|
|
218
|
-
end: number;
|
|
219
|
-
total: number;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export function parseContentRange(header: string | null): ContentRange | null {
|
|
223
|
-
const match = /^bytes\s+(\d+)-(\d+)\/(\d+)$/i.exec(header?.trim() ?? '');
|
|
224
|
-
if (!match) return null;
|
|
225
|
-
const [start, end, total] = match.slice(1).map(Number);
|
|
226
|
-
if (end < start || end >= total) return null;
|
|
227
|
-
return { start, end, total };
|
|
228
|
-
}
|
|
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
|
-
|
|
257
|
-
/** One Range request; `end` is exclusive. Resolves with a 206 response. */
|
|
258
|
-
async function fetchRange(
|
|
259
|
-
url: string,
|
|
260
|
-
start: number,
|
|
261
|
-
end: number,
|
|
262
|
-
options: RequestOptions,
|
|
263
|
-
): Promise<Response & { range: ContentRange }> {
|
|
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
|
-
}
|
|
279
|
-
if (response.status === 200) throw new RangeUnsupportedError(response);
|
|
280
|
-
if (response.status !== 206 || !response.body) {
|
|
281
|
-
await response.body?.cancel().catch(() => {});
|
|
282
|
-
throw new Error(`HTTP ${response.status} for ${what}`);
|
|
283
|
-
}
|
|
284
|
-
const range = parseContentRange(response.headers.get('content-range'));
|
|
285
|
-
if (!range || range.start !== start || range.end !== end - 1) {
|
|
286
|
-
await response.body.cancel().catch(() => {});
|
|
287
|
-
throw new Error(
|
|
288
|
-
`unexpected Content-Range ${response.headers.get('content-range')}`,
|
|
289
|
-
);
|
|
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
|
-
}
|
|
300
|
-
return Object.assign(response, { range });
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
async function fetchRangeBuffer(
|
|
304
|
-
url: string,
|
|
305
|
-
start: number,
|
|
306
|
-
end: number,
|
|
307
|
-
options: RequestOptions,
|
|
308
|
-
): Promise<{ data: Buffer; total: number }> {
|
|
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
|
-
}
|
|
317
|
-
if (data.length !== end - start) {
|
|
318
|
-
throw new Error(
|
|
319
|
-
`range returned ${data.length} bytes, expected ${end - start}`,
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
return { data, total: response.range.total };
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Fetch one entry whose compressed bytes are already located (the ppk's own
|
|
327
|
-
* publish-time report) and decompress it. Returns the raw entry content.
|
|
328
|
-
*/
|
|
329
|
-
export async function fetchZipEntryData(
|
|
330
|
-
url: string,
|
|
331
|
-
location: Pick<
|
|
332
|
-
ZipEntryLocation,
|
|
333
|
-
'dataOffset' | 'compressedSize' | 'compressionMethod'
|
|
334
|
-
>,
|
|
335
|
-
options: RangeOptions = {},
|
|
336
|
-
): Promise<{ data: Buffer; fetchedBytes: number; totalBytes: number }> {
|
|
337
|
-
const { dataOffset, compressedSize, compressionMethod } = location;
|
|
338
|
-
if (compressionMethod !== ZIP_STORED && compressionMethod !== ZIP_DEFLATED) {
|
|
339
|
-
throw new Error(`unsupported compression method ${compressionMethod}`);
|
|
340
|
-
}
|
|
341
|
-
if (
|
|
342
|
-
!Number.isInteger(dataOffset) ||
|
|
343
|
-
!Number.isInteger(compressedSize) ||
|
|
344
|
-
dataOffset < 0 ||
|
|
345
|
-
compressedSize <= 0
|
|
346
|
-
) {
|
|
347
|
-
throw new Error('invalid bundle location');
|
|
348
|
-
}
|
|
349
|
-
const { data, total } = await fetchRangeBuffer(
|
|
350
|
-
url,
|
|
351
|
-
dataOffset,
|
|
352
|
-
dataOffset + compressedSize,
|
|
353
|
-
{ timeoutMs: timeoutFor(compressedSize, options) },
|
|
354
|
-
);
|
|
355
|
-
return {
|
|
356
|
-
data: compressionMethod === ZIP_DEFLATED ? inflateRawSync(data) : data,
|
|
357
|
-
fetchedBytes: compressedSize,
|
|
358
|
-
totalBytes: total,
|
|
359
|
-
};
|
|
360
|
-
}
|
|
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
|
-
|
|
372
|
-
/**
|
|
373
|
-
* yauzl reader over HTTP Range. Header reads (EOCD, central directory, local
|
|
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.
|
|
380
|
-
*/
|
|
381
|
-
export class HttpRangeReader extends RandomAccessReader {
|
|
382
|
-
/** sorted by `start`, never overlapping */
|
|
383
|
-
private chunks: Chunk[] = [];
|
|
384
|
-
/** span a header read inside it should widen to (see `hintEntry`) */
|
|
385
|
-
private hint: { start: number; end: number } | null = null;
|
|
386
|
-
requests = 0;
|
|
387
|
-
fetchedBytes = 0;
|
|
388
|
-
|
|
389
|
-
constructor(
|
|
390
|
-
private readonly url: string,
|
|
391
|
-
readonly totalSize: number,
|
|
392
|
-
tail?: Chunk,
|
|
393
|
-
private readonly options: HttpRangeReaderOptions = {},
|
|
394
|
-
) {
|
|
395
|
-
super();
|
|
396
|
-
if (tail) this.addChunk(tail.start, tail.data);
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* The next entry to be read: yauzl first reads its local file header, and
|
|
401
|
-
* widening that read to the entry's data makes the data stream a cache hit
|
|
402
|
-
* — one request for header + bundle instead of two. The central directory
|
|
403
|
-
* may understate the local header's extra field, hence the slack.
|
|
404
|
-
*/
|
|
405
|
-
hintEntry(entry: Entry): void {
|
|
406
|
-
const start = entry.relativeOffsetOfLocalHeader;
|
|
407
|
-
const end =
|
|
408
|
-
start +
|
|
409
|
-
LOCAL_HEADER_SIZE +
|
|
410
|
-
entry.fileNameLength +
|
|
411
|
-
entry.extraFieldLength +
|
|
412
|
-
entry.compressedSize +
|
|
413
|
-
HINT_SLACK_BYTES;
|
|
414
|
-
this.hint = { start, end: Math.min(end, this.totalSize) };
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
/** Cache `data` at `start`, merging with any chunk it overlaps. */
|
|
418
|
-
addChunk(start: number, data: Buffer): void {
|
|
419
|
-
if (data.length === 0) return;
|
|
420
|
-
let lo = start;
|
|
421
|
-
let hi = start + data.length;
|
|
422
|
-
const pieces: Chunk[] = [{ start, data }];
|
|
423
|
-
const rest: Chunk[] = [];
|
|
424
|
-
for (const chunk of this.chunks) {
|
|
425
|
-
const chunkEnd = chunk.start + chunk.data.length;
|
|
426
|
-
if (chunk.start < hi && chunkEnd > lo) {
|
|
427
|
-
pieces.push(chunk);
|
|
428
|
-
lo = Math.min(lo, chunk.start);
|
|
429
|
-
hi = Math.max(hi, chunkEnd);
|
|
430
|
-
} else {
|
|
431
|
-
rest.push(chunk);
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
if (pieces.length === 1) {
|
|
435
|
-
rest.push(pieces[0]);
|
|
436
|
-
} else {
|
|
437
|
-
const merged = Buffer.alloc(hi - lo);
|
|
438
|
-
// the new data is written last so it wins over stale overlaps
|
|
439
|
-
for (const piece of pieces.reverse()) {
|
|
440
|
-
piece.data.copy(merged, piece.start - lo);
|
|
441
|
-
}
|
|
442
|
-
rest.push({ start: lo, data: merged });
|
|
443
|
-
}
|
|
444
|
-
rest.sort((a, b) => a.start - b.start);
|
|
445
|
-
this.chunks = rest;
|
|
446
|
-
}
|
|
447
|
-
|
|
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;
|
|
480
|
-
for (const chunk of this.chunks) {
|
|
481
|
-
const chunkEnd = chunk.start + chunk.data.length;
|
|
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;
|
|
488
|
-
}
|
|
489
|
-
return pieces;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
/** start of the cached run (touching chunks) holding byte `end - 1` */
|
|
493
|
-
private cachedSuffixStart(end: number): number | null {
|
|
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--;
|
|
504
|
-
}
|
|
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;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
_readStreamForRange(start: number, end: number): Readable {
|
|
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);
|
|
518
|
-
const out = new PassThrough();
|
|
519
|
-
for (const piece of pieces) out.write(piece);
|
|
520
|
-
const from = start + have;
|
|
521
|
-
const options = this.requestOptions(end - from);
|
|
522
|
-
this.requests++;
|
|
523
|
-
fetchRange(this.url, from, end, options)
|
|
524
|
-
.then((response) => {
|
|
525
|
-
this.fetchedBytes += end - from;
|
|
526
|
-
const body = Readable.fromWeb(response.body as any);
|
|
527
|
-
body.on('error', (error) =>
|
|
528
|
-
out.destroy(
|
|
529
|
-
describeAbort(error, `bytes=${from}-${end - 1}`, options.timeoutMs),
|
|
530
|
-
),
|
|
531
|
-
);
|
|
532
|
-
body.pipe(out);
|
|
533
|
-
})
|
|
534
|
-
.catch((error) => out.destroy(error));
|
|
535
|
-
return out;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
read(
|
|
539
|
-
buffer: Buffer,
|
|
540
|
-
offset: number,
|
|
541
|
-
length: number,
|
|
542
|
-
position: number,
|
|
543
|
-
callback: (err: Error | null, bytesRead?: number) => void,
|
|
544
|
-
): void {
|
|
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)) {
|
|
556
|
-
setImmediate(() => callback(null, length));
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
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
|
|
564
|
-
const from =
|
|
565
|
-
position + cached.reduce((sum, piece) => sum + piece.length, 0);
|
|
566
|
-
const suffixStart = this.cachedSuffixStart(requestEnd);
|
|
567
|
-
let end: number;
|
|
568
|
-
if (suffixStart !== null && suffixStart > from) {
|
|
569
|
-
end = suffixStart;
|
|
570
|
-
} else {
|
|
571
|
-
const hinted =
|
|
572
|
-
this.hint !== null &&
|
|
573
|
-
position >= this.hint.start &&
|
|
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;
|
|
581
|
-
}
|
|
582
|
-
this.fetchBuffer(from, end).then(
|
|
583
|
-
(data) => {
|
|
584
|
-
this.addChunk(from, data);
|
|
585
|
-
if (!copyOut(this.cachedPieces(position, requestEnd))) {
|
|
586
|
-
callback(new Error('range read did not cover the requested bytes'));
|
|
587
|
-
return;
|
|
588
|
-
}
|
|
589
|
-
callback(null, length);
|
|
590
|
-
},
|
|
591
|
-
(error) => callback(error),
|
|
592
|
-
);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
/** End-of-central-directory record: where the central directory lives. */
|
|
597
|
-
const EOCD_SIGNATURE = 0x06054b50;
|
|
598
|
-
const EOCD_SIZE = 22;
|
|
599
|
-
|
|
600
|
-
export function parseEndOfCentralDirectory(
|
|
601
|
-
tail: Buffer,
|
|
602
|
-
): { cdOffset: number; cdSize: number } | null {
|
|
603
|
-
for (let i = tail.length - EOCD_SIZE; i >= 0; i--) {
|
|
604
|
-
if (tail.readUInt32LE(i) !== EOCD_SIGNATURE) continue;
|
|
605
|
-
const commentLength = tail.readUInt16LE(i + 20);
|
|
606
|
-
if (i + EOCD_SIZE + commentLength !== tail.length) continue;
|
|
607
|
-
const cdSize = tail.readUInt32LE(i + 12);
|
|
608
|
-
const cdOffset = tail.readUInt32LE(i + 16);
|
|
609
|
-
// zip64 markers: let yauzl resolve the real values itself
|
|
610
|
-
if (cdSize === 0xffffffff || cdOffset === 0xffffffff) return null;
|
|
611
|
-
return { cdOffset, cdSize };
|
|
612
|
-
}
|
|
613
|
-
return null;
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
export type RemoteZip =
|
|
617
|
-
| { kind: 'zip'; zipFile: ZipFile; reader: HttpRangeReader }
|
|
618
|
-
/** the server ignored Range: the whole archive is streaming in `response` */
|
|
619
|
-
| { kind: 'full'; response: Response };
|
|
620
|
-
|
|
621
|
-
/**
|
|
622
|
-
* Open a remote zip by fetching its tail (EOCD + central directory for
|
|
623
|
-
* typical archives) and letting yauzl read the rest on demand.
|
|
624
|
-
*/
|
|
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
|
-
}
|
|
639
|
-
if (!response.ok || !response.body) {
|
|
640
|
-
await response.body?.cancel().catch(() => {});
|
|
641
|
-
throw new Error(`HTTP ${response.status}`);
|
|
642
|
-
}
|
|
643
|
-
if (response.status !== 206) return { kind: 'full', response };
|
|
644
|
-
const range = parseContentRange(response.headers.get('content-range'));
|
|
645
|
-
if (!range) {
|
|
646
|
-
await response.body.cancel().catch(() => {});
|
|
647
|
-
throw new Error(
|
|
648
|
-
`unexpected Content-Range ${response.headers.get('content-range')}`,
|
|
649
|
-
);
|
|
650
|
-
}
|
|
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
|
-
}
|
|
657
|
-
if (data.length !== range.end - range.start + 1) {
|
|
658
|
-
throw new Error(
|
|
659
|
-
`tail returned ${data.length} bytes, expected ${range.end - range.start + 1}`,
|
|
660
|
-
);
|
|
661
|
-
}
|
|
662
|
-
const reader = new HttpRangeReader(
|
|
663
|
-
url,
|
|
664
|
-
range.total,
|
|
665
|
-
{ start: range.start, data },
|
|
666
|
-
{ ...options, ifRange: rangeValidator(response.headers) },
|
|
667
|
-
);
|
|
668
|
-
reader.requests = 1;
|
|
669
|
-
reader.fetchedBytes = data.length;
|
|
670
|
-
// Large archives (apk / ipa with thousands of files) keep their central
|
|
671
|
-
// directory well before the tail; yauzl would otherwise page it in with one
|
|
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).
|
|
675
|
-
const eocd = parseEndOfCentralDirectory(data);
|
|
676
|
-
if (
|
|
677
|
-
eocd &&
|
|
678
|
-
eocd.cdOffset < range.start &&
|
|
679
|
-
eocd.cdOffset + eocd.cdSize >= range.start &&
|
|
680
|
-
eocd.cdOffset + eocd.cdSize <= range.total - EOCD_SIZE
|
|
681
|
-
) {
|
|
682
|
-
await reader.prefetch(eocd.cdOffset, range.start);
|
|
683
|
-
}
|
|
684
|
-
const zipFile = await new Promise<ZipFile>((resolve, reject) => {
|
|
685
|
-
fromRandomAccessReader(
|
|
686
|
-
reader,
|
|
687
|
-
range.total,
|
|
688
|
-
{ lazyEntries: true, autoClose: false },
|
|
689
|
-
(err, file) => (err ? reject(err) : resolve(file)),
|
|
690
|
-
);
|
|
691
|
-
});
|
|
692
|
-
return { kind: 'zip', zipFile, reader };
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
/**
|
|
696
|
-
* Read the first matching entry of an opened remote zip, then close it. With
|
|
697
|
-
* `reader` given, the entry's header and data are fetched in one request.
|
|
698
|
-
*/
|
|
699
|
-
export function readRemoteZipEntry(
|
|
700
|
-
zipFile: ZipFile,
|
|
701
|
-
matches: (name: string) => boolean,
|
|
702
|
-
reader?: HttpRangeReader,
|
|
703
|
-
): Promise<Buffer | null> {
|
|
704
|
-
return new Promise((resolve, reject) => {
|
|
705
|
-
let settled = false;
|
|
706
|
-
const finish = (error: Error | null, result: Buffer | null = null) => {
|
|
707
|
-
if (settled) return;
|
|
708
|
-
settled = true;
|
|
709
|
-
zipFile.close();
|
|
710
|
-
error ? reject(error) : resolve(result);
|
|
711
|
-
};
|
|
712
|
-
zipFile.on('error', (error) => finish(error));
|
|
713
|
-
zipFile.on('end', () => finish(null, null));
|
|
714
|
-
zipFile.on('entry', (entry: Entry) => {
|
|
715
|
-
if (!matches(entry.fileName)) {
|
|
716
|
-
zipFile.readEntry();
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
719
|
-
reader?.hintEntry(entry);
|
|
720
|
-
readEntry(entry, zipFile).then(
|
|
721
|
-
(data) => finish(null, data),
|
|
722
|
-
(error) => finish(error),
|
|
723
|
-
);
|
|
724
|
-
});
|
|
725
|
-
zipFile.readEntry();
|
|
726
|
-
});
|
|
727
|
-
}
|