react-native-update-cli 2.22.8 → 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/lib/native-package.d.ts +5 -1
- package/lib/native-package.js +145 -84
- package/lib/utils/latest-version/index.js +28 -7
- package/lib/utils/zip-raw-writer.d.ts +47 -0
- package/lib/utils/zip-raw-writer.js +204 -0
- package/package.json +1 -3
- package/src/native-package.ts +186 -96
- package/src/utils/latest-version/index.ts +27 -4
- package/src/utils/zip-raw-writer.ts +247 -0
package/lib/native-package.d.ts
CHANGED
|
@@ -10,8 +10,12 @@ export type NativePackageEntry = {
|
|
|
10
10
|
* considered by the local diff implementation (or vice versa).
|
|
11
11
|
*/
|
|
12
12
|
export declare function resolveNativePackageEntry(platform: Platform, entryName: string): NativePackageEntry | undefined;
|
|
13
|
+
export interface SlimPackageOptions {
|
|
14
|
+
/** re-deflate through yazl even when a raw copy would do (tests) */
|
|
15
|
+
repack?: boolean;
|
|
16
|
+
}
|
|
13
17
|
/**
|
|
14
18
|
* Repack a native package with only the bundle and resources required by
|
|
15
19
|
* package-origin diffs. Entry names and nested Harmony HAP structure are kept.
|
|
16
20
|
*/
|
|
17
|
-
export declare function createSlimNativePackage(source: string, output: string, platform: Platform): Promise<void>;
|
|
21
|
+
export declare function createSlimNativePackage(source: string, output: string, platform: Platform, options?: SlimPackageOptions): Promise<void>;
|
package/lib/native-package.js
CHANGED
|
@@ -43,6 +43,7 @@ const os_1 = __importDefault(require("os"));
|
|
|
43
43
|
const path_1 = __importDefault(require("path"));
|
|
44
44
|
const yauzl_1 = require("yauzl");
|
|
45
45
|
const yazl_1 = require("yazl");
|
|
46
|
+
const zip_raw_writer_1 = require("./utils/zip-raw-writer");
|
|
46
47
|
const nativePackageRules = {
|
|
47
48
|
android: {
|
|
48
49
|
resolveEntry: (entryName) => {
|
|
@@ -86,23 +87,94 @@ const nativePackageRules = {
|
|
|
86
87
|
function resolveNativePackageEntry(platform, entryName) {
|
|
87
88
|
return nativePackageRules[platform].resolveEntry(entryName);
|
|
88
89
|
}
|
|
89
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Copies each entry's stored bytes, CRC and sizes verbatim (yauzl hands out
|
|
92
|
+
* the raw file data with `decodeFileData: false`), so slimming a package is
|
|
93
|
+
* bound by disk I/O rather than by deflating every asset again.
|
|
94
|
+
*/
|
|
95
|
+
class RawCopySink {
|
|
96
|
+
writer;
|
|
97
|
+
constructor(output) {
|
|
98
|
+
this.writer = new zip_raw_writer_1.RawZipWriter(output);
|
|
99
|
+
}
|
|
100
|
+
copyEntry(sourceZip, entry) {
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
sourceZip.openReadStream(entry, { decodeFileData: false }, (error, readStream) => {
|
|
103
|
+
if (error || !readStream) {
|
|
104
|
+
reject(error ?? new Error(`Unable to read zip entry: ${entry.fileName}`));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this.writer.addRawEntry(entry, readStream).then(resolve, reject);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
addFile(filePath, fileName, like) {
|
|
112
|
+
// a nested slim archive is a zip of deflated entries: storing it as-is
|
|
113
|
+
// loses nothing and skips another pass over its bytes
|
|
114
|
+
return this.writer.addStoredFile(filePath, fileName, like);
|
|
115
|
+
}
|
|
116
|
+
end() {
|
|
117
|
+
return this.writer.end();
|
|
118
|
+
}
|
|
119
|
+
abort() {
|
|
120
|
+
return this.writer.abort();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Re-deflates every kept entry through yazl; only for sources that need zip64. */
|
|
124
|
+
class RepackSink {
|
|
125
|
+
output;
|
|
126
|
+
outputZip = new yazl_1.ZipFile();
|
|
127
|
+
writePromise;
|
|
128
|
+
constructor(output) {
|
|
129
|
+
this.output = output;
|
|
130
|
+
this.writePromise = new Promise((resolve, reject) => {
|
|
131
|
+
this.outputZip.outputStream.once('error', reject);
|
|
132
|
+
this.outputZip.outputStream
|
|
133
|
+
.pipe(fs.createWriteStream(output))
|
|
134
|
+
.once('error', reject)
|
|
135
|
+
.once('close', () => resolve());
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
copyEntry(sourceZip, entry) {
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
sourceZip.openReadStream(entry, (error, readStream) => {
|
|
141
|
+
if (error || !readStream) {
|
|
142
|
+
reject(error ?? new Error(`Unable to read zip entry: ${entry.fileName}`));
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
readStream.once('error', reject);
|
|
146
|
+
readStream.once('end', () => resolve());
|
|
147
|
+
this.outputZip.addReadStream(readStream, entry.fileName, {
|
|
148
|
+
compress: entry.compressionMethod !== 0,
|
|
149
|
+
mtime: entry.getLastModDate(),
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
async addFile(filePath, fileName, like) {
|
|
155
|
+
this.outputZip.addFile(filePath, fileName, {
|
|
156
|
+
compress: like.compressionMethod !== 0,
|
|
157
|
+
mtime: like.getLastModDate(),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
async end() {
|
|
161
|
+
this.outputZip.end();
|
|
162
|
+
await this.writePromise;
|
|
163
|
+
}
|
|
164
|
+
async abort() {
|
|
165
|
+
this.outputZip.end();
|
|
166
|
+
await this.writePromise.catch(() => { });
|
|
167
|
+
await fs.remove(this.output).catch(() => { });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function openZip(source) {
|
|
90
171
|
return new Promise((resolve, reject) => {
|
|
91
|
-
|
|
92
|
-
if (error) {
|
|
93
|
-
reject(error);
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
if (!readStream) {
|
|
97
|
-
reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
|
|
172
|
+
(0, yauzl_1.open)(source, { lazyEntries: true }, (error, zipFile) => {
|
|
173
|
+
if (error || !zipFile) {
|
|
174
|
+
reject(error ?? new Error(`Unable to open zip file: ${source}`));
|
|
98
175
|
return;
|
|
99
176
|
}
|
|
100
|
-
|
|
101
|
-
readStream.once('end', () => resolve());
|
|
102
|
-
outputZip.addReadStream(readStream, entry.fileName, {
|
|
103
|
-
compress: entry.compressionMethod !== 0,
|
|
104
|
-
mtime: entry.getLastModDate(),
|
|
105
|
-
});
|
|
177
|
+
resolve(zipFile);
|
|
106
178
|
});
|
|
107
179
|
});
|
|
108
180
|
}
|
|
@@ -125,94 +197,83 @@ function extractEntryToFile(sourceZip, entry, outputPath) {
|
|
|
125
197
|
});
|
|
126
198
|
});
|
|
127
199
|
}
|
|
128
|
-
async function filterNativePackageArchive(source, output, platform) {
|
|
200
|
+
async function filterNativePackageArchive(source, output, platform, options) {
|
|
129
201
|
await fs.ensureDir(path_1.default.dirname(output));
|
|
130
202
|
const scratchDir = await fs.mkdtemp(path_1.default.join(os_1.default.tmpdir(), 'rnu-native-package-entry-'));
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
203
|
+
const sourceZip = await openZip(source);
|
|
204
|
+
// a subset of the source never outgrows it, so the source's own size and
|
|
205
|
+
// entry count decide whether plain (non-zip64) headers are enough
|
|
206
|
+
const rawCopy = !options.repack &&
|
|
207
|
+
(0, zip_raw_writer_1.fitsRawZip)((await fs.stat(source)).size, sourceZip.entryCount);
|
|
208
|
+
const sink = rawCopy
|
|
209
|
+
? new RawCopySink(output)
|
|
210
|
+
: new RepackSink(output);
|
|
139
211
|
let includedEntries = 0;
|
|
140
212
|
let includedBundles = 0;
|
|
141
213
|
let nestedArchiveIndex = 0;
|
|
142
214
|
try {
|
|
143
215
|
await new Promise((resolve, reject) => {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
216
|
+
let settled = false;
|
|
217
|
+
const fail = (error) => {
|
|
218
|
+
if (settled) {
|
|
147
219
|
return;
|
|
148
220
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
221
|
+
settled = true;
|
|
222
|
+
sourceZip.close();
|
|
223
|
+
reject(error);
|
|
224
|
+
};
|
|
225
|
+
sourceZip.once('error', fail);
|
|
226
|
+
sourceZip.once('end', () => {
|
|
227
|
+
if (!settled) {
|
|
154
228
|
settled = true;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
229
|
+
resolve();
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
sourceZip.on('entry', async (entry) => {
|
|
233
|
+
try {
|
|
234
|
+
if (entry.fileName.endsWith('/')) {
|
|
235
|
+
sourceZip.readEntry();
|
|
236
|
+
return;
|
|
163
237
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (
|
|
175
|
-
entry.fileName
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const nestedOutput = path_1.default.join(scratchDir, `${index}.slim.hap`);
|
|
179
|
-
await extractEntryToFile(sourceZip, entry, nestedSource);
|
|
180
|
-
const nestedResult = await filterNativePackageArchive(nestedSource, nestedOutput, platform);
|
|
181
|
-
if (nestedResult.entries > 0) {
|
|
182
|
-
outputZip.addFile(nestedOutput, entry.fileName, {
|
|
183
|
-
compress: entry.compressionMethod !== 0,
|
|
184
|
-
mtime: entry.getLastModDate(),
|
|
185
|
-
});
|
|
186
|
-
includedEntries += nestedResult.entries;
|
|
187
|
-
includedBundles += nestedResult.bundles;
|
|
188
|
-
}
|
|
189
|
-
sourceZip.readEntry();
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
const resolvedEntry = resolveNativePackageEntry(platform, entry.fileName);
|
|
193
|
-
if (resolvedEntry) {
|
|
194
|
-
await copyEntryToZip(sourceZip, entry, outputZip);
|
|
195
|
-
includedEntries += 1;
|
|
196
|
-
if (resolvedEntry.kind === 'bundle') {
|
|
197
|
-
includedBundles += 1;
|
|
198
|
-
}
|
|
238
|
+
// Harmony .app packages contain one or more nested .hap archives.
|
|
239
|
+
// Rebuild those archives recursively and keep their outer path so
|
|
240
|
+
// the slim package has the same nesting as the original package.
|
|
241
|
+
if (platform === 'harmony' &&
|
|
242
|
+
entry.fileName.toLowerCase().endsWith('.hap')) {
|
|
243
|
+
const index = nestedArchiveIndex++;
|
|
244
|
+
const nestedSource = path_1.default.join(scratchDir, `${index}.source.hap`);
|
|
245
|
+
const nestedOutput = path_1.default.join(scratchDir, `${index}.slim.hap`);
|
|
246
|
+
await extractEntryToFile(sourceZip, entry, nestedSource);
|
|
247
|
+
const nestedResult = await filterNativePackageArchive(nestedSource, nestedOutput, platform, options);
|
|
248
|
+
if (nestedResult.entries > 0) {
|
|
249
|
+
await sink.addFile(nestedOutput, entry.fileName, entry);
|
|
250
|
+
includedEntries += nestedResult.entries;
|
|
251
|
+
includedBundles += nestedResult.bundles;
|
|
199
252
|
}
|
|
200
253
|
sourceZip.readEntry();
|
|
254
|
+
return;
|
|
201
255
|
}
|
|
202
|
-
|
|
203
|
-
|
|
256
|
+
const resolvedEntry = resolveNativePackageEntry(platform, entry.fileName);
|
|
257
|
+
if (resolvedEntry) {
|
|
258
|
+
await sink.copyEntry(sourceZip, entry);
|
|
259
|
+
includedEntries += 1;
|
|
260
|
+
if (resolvedEntry.kind === 'bundle') {
|
|
261
|
+
includedBundles += 1;
|
|
262
|
+
}
|
|
204
263
|
}
|
|
205
|
-
|
|
206
|
-
|
|
264
|
+
sourceZip.readEntry();
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
fail(error);
|
|
268
|
+
}
|
|
207
269
|
});
|
|
270
|
+
sourceZip.readEntry();
|
|
208
271
|
});
|
|
209
|
-
|
|
210
|
-
await writePromise;
|
|
272
|
+
await sink.end();
|
|
211
273
|
return { bundles: includedBundles, entries: includedEntries };
|
|
212
274
|
}
|
|
213
275
|
catch (error) {
|
|
214
|
-
|
|
215
|
-
await writePromise.catch(() => { });
|
|
276
|
+
await sink.abort();
|
|
216
277
|
throw error;
|
|
217
278
|
}
|
|
218
279
|
finally {
|
|
@@ -223,8 +284,8 @@ async function filterNativePackageArchive(source, output, platform) {
|
|
|
223
284
|
* Repack a native package with only the bundle and resources required by
|
|
224
285
|
* package-origin diffs. Entry names and nested Harmony HAP structure are kept.
|
|
225
286
|
*/
|
|
226
|
-
async function createSlimNativePackage(source, output, platform) {
|
|
227
|
-
const result = await filterNativePackageArchive(source, output, platform);
|
|
287
|
+
async function createSlimNativePackage(source, output, platform, options = {}) {
|
|
288
|
+
const result = await filterNativePackageArchive(source, output, platform, options);
|
|
228
289
|
if (result.bundles === 0) {
|
|
229
290
|
await fs.remove(output);
|
|
230
291
|
throw new Error(`Bundle entry not found in native package: ${source}`);
|
|
@@ -4,14 +4,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ONE_DAY = void 0;
|
|
7
|
+
const compare_versions_1 = require("compare-versions");
|
|
7
8
|
const fs_1 = require("fs");
|
|
8
9
|
const global_dirs_1 = require("global-dirs");
|
|
9
10
|
const os_1 = require("os");
|
|
10
11
|
const path_1 = require("path");
|
|
11
12
|
const registry_auth_token_1 = __importDefault(require("registry-auth-token"));
|
|
12
13
|
const registry_url_1 = __importDefault(require("registry-auth-token/registry-url"));
|
|
13
|
-
const gt_1 = __importDefault(require("semver/functions/gt"));
|
|
14
|
-
const max_satisfying_1 = __importDefault(require("semver/ranges/max-satisfying"));
|
|
15
14
|
const url_1 = require("url");
|
|
16
15
|
exports.ONE_DAY = 1000 * 60 * 60 * 24; // eslint-disable-line @typescript-eslint/naming-convention
|
|
17
16
|
const isPackageJson = (obj) => {
|
|
@@ -187,11 +186,33 @@ const getRegistryVersions = async (pkgName, tagOrRange, options) => {
|
|
|
187
186
|
versions.wanted = pkgMetadata.distTags[tagOrRange];
|
|
188
187
|
}
|
|
189
188
|
else if (tagOrRange && pkgMetadata?.versions.length) {
|
|
190
|
-
versions.wanted =
|
|
191
|
-
(0, max_satisfying_1.default)(pkgMetadata.versions, tagOrRange) ?? undefined;
|
|
189
|
+
versions.wanted = maxSatisfying(pkgMetadata.versions, tagOrRange);
|
|
192
190
|
}
|
|
193
191
|
return versions;
|
|
194
192
|
};
|
|
193
|
+
/** the highest of `versions` within `range` (npm range syntax), if any */
|
|
194
|
+
const maxSatisfying = (versions, range) => {
|
|
195
|
+
const matching = versions.filter((version) => {
|
|
196
|
+
try {
|
|
197
|
+
return (0, compare_versions_1.satisfies)(version, range);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
if (!matching.length)
|
|
204
|
+
return undefined;
|
|
205
|
+
return matching.sort(compare_versions_1.compareVersions)[matching.length - 1];
|
|
206
|
+
};
|
|
207
|
+
/** `a > b` for two versions; invalid input counts as "not newer" */
|
|
208
|
+
const gt = (a, b) => {
|
|
209
|
+
try {
|
|
210
|
+
return (0, compare_versions_1.compare)(a, b, '>');
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
195
216
|
const getInstalledVersion = (pkgName, location = 'local') => {
|
|
196
217
|
try {
|
|
197
218
|
if (location === 'globalNpm') {
|
|
@@ -248,17 +269,17 @@ const getInfo = async (pkg, options) => {
|
|
|
248
269
|
...(await getRegistryVersions(pkgInfo.name, pkgInfo.wantedTagOrRange, options)),
|
|
249
270
|
};
|
|
250
271
|
const local = pkgInfo.local && pkgInfo.wanted
|
|
251
|
-
? (
|
|
272
|
+
? gt(pkgInfo.wanted, pkgInfo.local)
|
|
252
273
|
? pkgInfo.wanted
|
|
253
274
|
: false
|
|
254
275
|
: false;
|
|
255
276
|
const globalNpm = pkgInfo.globalNpm && pkgInfo.wanted
|
|
256
|
-
? (
|
|
277
|
+
? gt(pkgInfo.wanted, pkgInfo.globalNpm)
|
|
257
278
|
? pkgInfo.wanted
|
|
258
279
|
: false
|
|
259
280
|
: false;
|
|
260
281
|
const globalYarn = pkgInfo.globalYarn && pkgInfo.wanted
|
|
261
|
-
? (
|
|
282
|
+
? gt(pkgInfo.wanted, pkgInfo.globalYarn)
|
|
262
283
|
? pkgInfo.wanted
|
|
263
284
|
: false
|
|
264
285
|
: false;
|
|
@@ -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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-update-cli",
|
|
3
|
-
"version": "2.22.
|
|
3
|
+
"version": "2.22.9",
|
|
4
4
|
"description": "command line tool for react-native-update (remote updates for react native)",
|
|
5
5
|
"main": "lib/exports.js",
|
|
6
6
|
"types": "lib/exports.d.ts",
|
|
@@ -79,7 +79,6 @@
|
|
|
79
79
|
"protobufjs": "^8.7.2",
|
|
80
80
|
"read": "^4.1.0",
|
|
81
81
|
"registry-auth-token": "^5.1.1",
|
|
82
|
-
"semver": "^7.8.5",
|
|
83
82
|
"tty-table": "5.0",
|
|
84
83
|
"yauzl": "^3.4.0",
|
|
85
84
|
"yazl": "3.3.1"
|
|
@@ -99,7 +98,6 @@
|
|
|
99
98
|
"@types/node": "^26.3.0",
|
|
100
99
|
"@types/node-fetch": "^2.6.13",
|
|
101
100
|
"@types/progress": "^2.0.7",
|
|
102
|
-
"@types/semver": "^7.8.0",
|
|
103
101
|
"@types/yauzl": "^3.4.0",
|
|
104
102
|
"@types/yazl": "^3.3.1",
|
|
105
103
|
"node-hdiffpatch": "^2.4.0",
|
package/src/native-package.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
} from 'yauzl';
|
|
9
9
|
import { ZipFile as YazlZipFile } from 'yazl';
|
|
10
10
|
import type { Platform } from './types';
|
|
11
|
+
import { fitsRawZip, RawZipWriter } from './utils/zip-raw-writer';
|
|
11
12
|
|
|
12
13
|
export type NativePackageEntry = {
|
|
13
14
|
diffPath: string;
|
|
@@ -66,28 +67,115 @@ export function resolveNativePackageEntry(
|
|
|
66
67
|
return nativePackageRules[platform].resolveEntry(entryName);
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
entry: Entry
|
|
72
|
-
|
|
73
|
-
): Promise<void
|
|
70
|
+
/** Where the kept entries go: a raw copy (default) or a yazl repack. */
|
|
71
|
+
interface SlimSink {
|
|
72
|
+
copyEntry(sourceZip: YauzlZipFile, entry: Entry): Promise<void>;
|
|
73
|
+
/** add a local file under `fileName`, dated like `like` */
|
|
74
|
+
addFile(filePath: string, fileName: string, like: Entry): Promise<void>;
|
|
75
|
+
end(): Promise<void>;
|
|
76
|
+
abort(): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Copies each entry's stored bytes, CRC and sizes verbatim (yauzl hands out
|
|
81
|
+
* the raw file data with `decodeFileData: false`), so slimming a package is
|
|
82
|
+
* bound by disk I/O rather than by deflating every asset again.
|
|
83
|
+
*/
|
|
84
|
+
class RawCopySink implements SlimSink {
|
|
85
|
+
private readonly writer: RawZipWriter;
|
|
86
|
+
constructor(output: string) {
|
|
87
|
+
this.writer = new RawZipWriter(output);
|
|
88
|
+
}
|
|
89
|
+
copyEntry(sourceZip: YauzlZipFile, entry: Entry): Promise<void> {
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
sourceZip.openReadStream(
|
|
92
|
+
entry,
|
|
93
|
+
{ decodeFileData: false },
|
|
94
|
+
(error, readStream) => {
|
|
95
|
+
if (error || !readStream) {
|
|
96
|
+
reject(
|
|
97
|
+
error ?? new Error(`Unable to read zip entry: ${entry.fileName}`),
|
|
98
|
+
);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this.writer.addRawEntry(entry, readStream).then(resolve, reject);
|
|
102
|
+
},
|
|
103
|
+
);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
addFile(filePath: string, fileName: string, like: Entry): Promise<void> {
|
|
107
|
+
// a nested slim archive is a zip of deflated entries: storing it as-is
|
|
108
|
+
// loses nothing and skips another pass over its bytes
|
|
109
|
+
return this.writer.addStoredFile(filePath, fileName, like);
|
|
110
|
+
}
|
|
111
|
+
end(): Promise<void> {
|
|
112
|
+
return this.writer.end();
|
|
113
|
+
}
|
|
114
|
+
abort(): Promise<void> {
|
|
115
|
+
return this.writer.abort();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Re-deflates every kept entry through yazl; only for sources that need zip64. */
|
|
120
|
+
class RepackSink implements SlimSink {
|
|
121
|
+
private readonly outputZip = new YazlZipFile();
|
|
122
|
+
private readonly writePromise: Promise<void>;
|
|
123
|
+
constructor(readonly output: string) {
|
|
124
|
+
this.writePromise = new Promise<void>((resolve, reject) => {
|
|
125
|
+
this.outputZip.outputStream.once('error', reject);
|
|
126
|
+
this.outputZip.outputStream
|
|
127
|
+
.pipe(fs.createWriteStream(output))
|
|
128
|
+
.once('error', reject)
|
|
129
|
+
.once('close', () => resolve());
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
copyEntry(sourceZip: YauzlZipFile, entry: Entry): Promise<void> {
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
sourceZip.openReadStream(entry, (error, readStream) => {
|
|
135
|
+
if (error || !readStream) {
|
|
136
|
+
reject(
|
|
137
|
+
error ?? new Error(`Unable to read zip entry: ${entry.fileName}`),
|
|
138
|
+
);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
readStream.once('error', reject);
|
|
142
|
+
readStream.once('end', () => resolve());
|
|
143
|
+
this.outputZip.addReadStream(readStream, entry.fileName, {
|
|
144
|
+
compress: entry.compressionMethod !== 0,
|
|
145
|
+
mtime: entry.getLastModDate(),
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
async addFile(
|
|
151
|
+
filePath: string,
|
|
152
|
+
fileName: string,
|
|
153
|
+
like: Entry,
|
|
154
|
+
): Promise<void> {
|
|
155
|
+
this.outputZip.addFile(filePath, fileName, {
|
|
156
|
+
compress: like.compressionMethod !== 0,
|
|
157
|
+
mtime: like.getLastModDate(),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
async end(): Promise<void> {
|
|
161
|
+
this.outputZip.end();
|
|
162
|
+
await this.writePromise;
|
|
163
|
+
}
|
|
164
|
+
async abort(): Promise<void> {
|
|
165
|
+
this.outputZip.end();
|
|
166
|
+
await this.writePromise.catch(() => {});
|
|
167
|
+
await fs.remove(this.output).catch(() => {});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function openZip(source: string): Promise<YauzlZipFile> {
|
|
74
172
|
return new Promise((resolve, reject) => {
|
|
75
|
-
|
|
76
|
-
if (error) {
|
|
77
|
-
reject(error);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
if (!readStream) {
|
|
81
|
-
reject(new Error(`Unable to read zip entry: ${entry.fileName}`));
|
|
173
|
+
openZipFile(source, { lazyEntries: true }, (error, zipFile) => {
|
|
174
|
+
if (error || !zipFile) {
|
|
175
|
+
reject(error ?? new Error(`Unable to open zip file: ${source}`));
|
|
82
176
|
return;
|
|
83
177
|
}
|
|
84
|
-
|
|
85
|
-
readStream.once('error', reject);
|
|
86
|
-
readStream.once('end', () => resolve());
|
|
87
|
-
outputZip.addReadStream(readStream, entry.fileName, {
|
|
88
|
-
compress: entry.compressionMethod !== 0,
|
|
89
|
-
mtime: entry.getLastModDate(),
|
|
90
|
-
});
|
|
178
|
+
resolve(zipFile);
|
|
91
179
|
});
|
|
92
180
|
});
|
|
93
181
|
}
|
|
@@ -117,114 +205,110 @@ function extractEntryToFile(
|
|
|
117
205
|
});
|
|
118
206
|
}
|
|
119
207
|
|
|
208
|
+
export interface SlimPackageOptions {
|
|
209
|
+
/** re-deflate through yazl even when a raw copy would do (tests) */
|
|
210
|
+
repack?: boolean;
|
|
211
|
+
}
|
|
212
|
+
|
|
120
213
|
async function filterNativePackageArchive(
|
|
121
214
|
source: string,
|
|
122
215
|
output: string,
|
|
123
216
|
platform: Platform,
|
|
217
|
+
options: SlimPackageOptions,
|
|
124
218
|
): Promise<{ bundles: number; entries: number }> {
|
|
125
219
|
await fs.ensureDir(path.dirname(output));
|
|
126
220
|
const scratchDir = await fs.mkdtemp(
|
|
127
221
|
path.join(os.tmpdir(), 'rnu-native-package-entry-'),
|
|
128
222
|
);
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
223
|
+
const sourceZip = await openZip(source);
|
|
224
|
+
// a subset of the source never outgrows it, so the source's own size and
|
|
225
|
+
// entry count decide whether plain (non-zip64) headers are enough
|
|
226
|
+
const rawCopy =
|
|
227
|
+
!options.repack &&
|
|
228
|
+
fitsRawZip((await fs.stat(source)).size, sourceZip.entryCount);
|
|
229
|
+
const sink: SlimSink = rawCopy
|
|
230
|
+
? new RawCopySink(output)
|
|
231
|
+
: new RepackSink(output);
|
|
137
232
|
let includedEntries = 0;
|
|
138
233
|
let includedBundles = 0;
|
|
139
234
|
let nestedArchiveIndex = 0;
|
|
140
235
|
|
|
141
236
|
try {
|
|
142
237
|
await new Promise<void>((resolve, reject) => {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
238
|
+
let settled = false;
|
|
239
|
+
const fail = (error: unknown) => {
|
|
240
|
+
if (settled) {
|
|
146
241
|
return;
|
|
147
242
|
}
|
|
243
|
+
settled = true;
|
|
244
|
+
sourceZip.close();
|
|
245
|
+
reject(error);
|
|
246
|
+
};
|
|
148
247
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
248
|
+
sourceZip.once('error', fail);
|
|
249
|
+
sourceZip.once('end', () => {
|
|
250
|
+
if (!settled) {
|
|
154
251
|
settled = true;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
resolve();
|
|
252
|
+
resolve();
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
sourceZip.on('entry', async (entry) => {
|
|
256
|
+
try {
|
|
257
|
+
if (entry.fileName.endsWith('/')) {
|
|
258
|
+
sourceZip.readEntry();
|
|
259
|
+
return;
|
|
164
260
|
}
|
|
165
|
-
});
|
|
166
|
-
sourceZip.on('entry', async (entry) => {
|
|
167
|
-
try {
|
|
168
|
-
if (entry.fileName.endsWith('/')) {
|
|
169
|
-
sourceZip.readEntry();
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Harmony .app packages contain one or more nested .hap archives.
|
|
174
|
-
// Rebuild those archives recursively and keep their outer path so
|
|
175
|
-
// the slim package has the same nesting as the original package.
|
|
176
|
-
if (
|
|
177
|
-
platform === 'harmony' &&
|
|
178
|
-
entry.fileName.toLowerCase().endsWith('.hap')
|
|
179
|
-
) {
|
|
180
|
-
const index = nestedArchiveIndex++;
|
|
181
|
-
const nestedSource = path.join(scratchDir, `${index}.source.hap`);
|
|
182
|
-
const nestedOutput = path.join(scratchDir, `${index}.slim.hap`);
|
|
183
|
-
await extractEntryToFile(sourceZip, entry, nestedSource);
|
|
184
|
-
const nestedResult = await filterNativePackageArchive(
|
|
185
|
-
nestedSource,
|
|
186
|
-
nestedOutput,
|
|
187
|
-
platform,
|
|
188
|
-
);
|
|
189
|
-
if (nestedResult.entries > 0) {
|
|
190
|
-
outputZip.addFile(nestedOutput, entry.fileName, {
|
|
191
|
-
compress: entry.compressionMethod !== 0,
|
|
192
|
-
mtime: entry.getLastModDate(),
|
|
193
|
-
});
|
|
194
|
-
includedEntries += nestedResult.entries;
|
|
195
|
-
includedBundles += nestedResult.bundles;
|
|
196
|
-
}
|
|
197
|
-
sourceZip.readEntry();
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
261
|
|
|
201
|
-
|
|
262
|
+
// Harmony .app packages contain one or more nested .hap archives.
|
|
263
|
+
// Rebuild those archives recursively and keep their outer path so
|
|
264
|
+
// the slim package has the same nesting as the original package.
|
|
265
|
+
if (
|
|
266
|
+
platform === 'harmony' &&
|
|
267
|
+
entry.fileName.toLowerCase().endsWith('.hap')
|
|
268
|
+
) {
|
|
269
|
+
const index = nestedArchiveIndex++;
|
|
270
|
+
const nestedSource = path.join(scratchDir, `${index}.source.hap`);
|
|
271
|
+
const nestedOutput = path.join(scratchDir, `${index}.slim.hap`);
|
|
272
|
+
await extractEntryToFile(sourceZip, entry, nestedSource);
|
|
273
|
+
const nestedResult = await filterNativePackageArchive(
|
|
274
|
+
nestedSource,
|
|
275
|
+
nestedOutput,
|
|
202
276
|
platform,
|
|
203
|
-
|
|
277
|
+
options,
|
|
204
278
|
);
|
|
205
|
-
if (
|
|
206
|
-
await
|
|
207
|
-
includedEntries +=
|
|
208
|
-
|
|
209
|
-
includedBundles += 1;
|
|
210
|
-
}
|
|
279
|
+
if (nestedResult.entries > 0) {
|
|
280
|
+
await sink.addFile(nestedOutput, entry.fileName, entry);
|
|
281
|
+
includedEntries += nestedResult.entries;
|
|
282
|
+
includedBundles += nestedResult.bundles;
|
|
211
283
|
}
|
|
212
284
|
sourceZip.readEntry();
|
|
213
|
-
|
|
214
|
-
fail(error);
|
|
285
|
+
return;
|
|
215
286
|
}
|
|
216
|
-
});
|
|
217
287
|
|
|
218
|
-
|
|
288
|
+
const resolvedEntry = resolveNativePackageEntry(
|
|
289
|
+
platform,
|
|
290
|
+
entry.fileName,
|
|
291
|
+
);
|
|
292
|
+
if (resolvedEntry) {
|
|
293
|
+
await sink.copyEntry(sourceZip, entry);
|
|
294
|
+
includedEntries += 1;
|
|
295
|
+
if (resolvedEntry.kind === 'bundle') {
|
|
296
|
+
includedBundles += 1;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
sourceZip.readEntry();
|
|
300
|
+
} catch (error) {
|
|
301
|
+
fail(error);
|
|
302
|
+
}
|
|
219
303
|
});
|
|
304
|
+
|
|
305
|
+
sourceZip.readEntry();
|
|
220
306
|
});
|
|
221
307
|
|
|
222
|
-
|
|
223
|
-
await writePromise;
|
|
308
|
+
await sink.end();
|
|
224
309
|
return { bundles: includedBundles, entries: includedEntries };
|
|
225
310
|
} catch (error) {
|
|
226
|
-
|
|
227
|
-
await writePromise.catch(() => {});
|
|
311
|
+
await sink.abort();
|
|
228
312
|
throw error;
|
|
229
313
|
} finally {
|
|
230
314
|
await fs.remove(scratchDir);
|
|
@@ -239,8 +323,14 @@ export async function createSlimNativePackage(
|
|
|
239
323
|
source: string,
|
|
240
324
|
output: string,
|
|
241
325
|
platform: Platform,
|
|
326
|
+
options: SlimPackageOptions = {},
|
|
242
327
|
): Promise<void> {
|
|
243
|
-
const result = await filterNativePackageArchive(
|
|
328
|
+
const result = await filterNativePackageArchive(
|
|
329
|
+
source,
|
|
330
|
+
output,
|
|
331
|
+
platform,
|
|
332
|
+
options,
|
|
333
|
+
);
|
|
244
334
|
if (result.bundles === 0) {
|
|
245
335
|
await fs.remove(output);
|
|
246
336
|
throw new Error(`Bundle entry not found in native package: ${source}`);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { compare, compareVersions, satisfies } from 'compare-versions';
|
|
1
2
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
2
3
|
import { npm, yarn } from 'global-dirs';
|
|
3
4
|
import type {
|
|
@@ -10,8 +11,6 @@ import { homedir } from 'os';
|
|
|
10
11
|
import { dirname, join, parse, resolve as pathResolve } from 'path';
|
|
11
12
|
import registryAuthToken from 'registry-auth-token';
|
|
12
13
|
import getRegistryUrl from 'registry-auth-token/registry-url';
|
|
13
|
-
import gt from 'semver/functions/gt';
|
|
14
|
-
import maxSatisfying from 'semver/ranges/max-satisfying';
|
|
15
14
|
import { URL } from 'url';
|
|
16
15
|
|
|
17
16
|
interface RegistryVersions {
|
|
@@ -401,12 +400,36 @@ const getRegistryVersions = async (
|
|
|
401
400
|
if (tagOrRange && pkgMetadata?.distTags[tagOrRange]) {
|
|
402
401
|
versions.wanted = pkgMetadata.distTags[tagOrRange];
|
|
403
402
|
} else if (tagOrRange && pkgMetadata?.versions.length) {
|
|
404
|
-
versions.wanted =
|
|
405
|
-
maxSatisfying(pkgMetadata.versions, tagOrRange) ?? undefined;
|
|
403
|
+
versions.wanted = maxSatisfying(pkgMetadata.versions, tagOrRange);
|
|
406
404
|
}
|
|
407
405
|
return versions;
|
|
408
406
|
};
|
|
409
407
|
|
|
408
|
+
/** the highest of `versions` within `range` (npm range syntax), if any */
|
|
409
|
+
const maxSatisfying = (
|
|
410
|
+
versions: string[],
|
|
411
|
+
range: string,
|
|
412
|
+
): string | undefined => {
|
|
413
|
+
const matching = versions.filter((version) => {
|
|
414
|
+
try {
|
|
415
|
+
return satisfies(version, range);
|
|
416
|
+
} catch {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
if (!matching.length) return undefined;
|
|
421
|
+
return matching.sort(compareVersions)[matching.length - 1];
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
/** `a > b` for two versions; invalid input counts as "not newer" */
|
|
425
|
+
const gt = (a: string, b: string): boolean => {
|
|
426
|
+
try {
|
|
427
|
+
return compare(a, b, '>');
|
|
428
|
+
} catch {
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
|
|
410
433
|
const getInstalledVersion = (
|
|
411
434
|
pkgName: string,
|
|
412
435
|
location: keyof InstalledVersions = 'local',
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// A minimal zip writer that copies entries *as stored* — their compressed
|
|
2
|
+
// bytes, CRC, sizes and timestamps straight from the source archive's central
|
|
3
|
+
// directory — so repacking a subset of an apk / ipa / hap costs disk I/O, not
|
|
4
|
+
// a second deflate of every asset (yazl can only add uncompressed data and
|
|
5
|
+
// re-deflates it). Plain zip only: no zip64, no data descriptors, no extra
|
|
6
|
+
// fields; callers must check `fitsRawZip` before choosing this writer.
|
|
7
|
+
import fs from 'fs-extra';
|
|
8
|
+
import type { Readable } from 'stream';
|
|
9
|
+
import type { Entry } from 'yauzl';
|
|
10
|
+
|
|
11
|
+
const LOCAL_HEADER_SIGNATURE = 0x04034b50;
|
|
12
|
+
const CENTRAL_HEADER_SIGNATURE = 0x02014b50;
|
|
13
|
+
const EOCD_SIGNATURE = 0x06054b50;
|
|
14
|
+
const VERSION_NEEDED_DEFAULT = 20;
|
|
15
|
+
/** "made by" for entries this writer creates itself: unix, spec 2.0 */
|
|
16
|
+
const VERSION_MADE_BY_UNIX = (3 << 8) | 20;
|
|
17
|
+
const FLAG_UTF8_NAMES = 0x800;
|
|
18
|
+
const FLAG_DATA_DESCRIPTOR = 0x8;
|
|
19
|
+
/** general-purpose bits carried over from a copied entry: encryption, deflate strength */
|
|
20
|
+
const FLAG_KEEP_MASK = 0x0007;
|
|
21
|
+
const MAX_UINT32 = 0xffffffff;
|
|
22
|
+
const MAX_UINT16 = 0xffff;
|
|
23
|
+
/** external attributes of a plain `-rw-r--r--` file */
|
|
24
|
+
const UNIX_REGULAR_FILE_ATTRIBUTES = 0o100644 << 16;
|
|
25
|
+
|
|
26
|
+
export interface RawZipEntry {
|
|
27
|
+
fileName: string;
|
|
28
|
+
compressionMethod: number;
|
|
29
|
+
crc32: number;
|
|
30
|
+
compressedSize: number;
|
|
31
|
+
uncompressedSize: number;
|
|
32
|
+
lastModFileTime: number;
|
|
33
|
+
lastModFileDate: number;
|
|
34
|
+
generalPurposeBitFlag?: number;
|
|
35
|
+
versionMadeBy?: number;
|
|
36
|
+
versionNeededToExtract?: number;
|
|
37
|
+
internalFileAttributes?: number;
|
|
38
|
+
externalFileAttributes?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether an archive of `sourceSize` bytes with `entryCount` entries can be
|
|
43
|
+
* (partially) copied without zip64 structures: an output that only keeps a
|
|
44
|
+
* subset of the entries is never larger than its source.
|
|
45
|
+
*/
|
|
46
|
+
export function fitsRawZip(sourceSize: number, entryCount: number): boolean {
|
|
47
|
+
return sourceSize < MAX_UINT32 && entryCount < MAX_UINT16;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let crcTable: Int32Array | undefined;
|
|
51
|
+
function getCrcTable(): Int32Array {
|
|
52
|
+
if (!crcTable) {
|
|
53
|
+
crcTable = new Int32Array(256);
|
|
54
|
+
for (let n = 0; n < 256; n++) {
|
|
55
|
+
let c = n;
|
|
56
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
57
|
+
crcTable[n] = c;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return crcTable;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** CRC-32 (IEEE) continued from `seed` (the running value, not inverted) */
|
|
64
|
+
export function crc32Update(seed: number, chunk: Buffer): number {
|
|
65
|
+
const table = getCrcTable();
|
|
66
|
+
let crc = ~seed;
|
|
67
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
68
|
+
crc = table[(crc ^ chunk[i]) & 0xff] ^ (crc >>> 8);
|
|
69
|
+
}
|
|
70
|
+
return ~crc >>> 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class RawZipWriter {
|
|
74
|
+
private readonly out: fs.WriteStream;
|
|
75
|
+
private readonly central: Buffer[] = [];
|
|
76
|
+
private offset = 0;
|
|
77
|
+
private count = 0;
|
|
78
|
+
private failure: Error | null = null;
|
|
79
|
+
private readonly closed: Promise<void>;
|
|
80
|
+
|
|
81
|
+
constructor(readonly output: string) {
|
|
82
|
+
this.out = fs.createWriteStream(output);
|
|
83
|
+
this.closed = new Promise<void>((resolve, reject) => {
|
|
84
|
+
this.out.once('close', resolve);
|
|
85
|
+
this.out.once('error', (error) => {
|
|
86
|
+
this.failure = error;
|
|
87
|
+
reject(error);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
// surfaced by the next write / end(), never as an unhandled rejection
|
|
91
|
+
this.closed.catch(() => {});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Copy one entry with its file data exactly as stored in the source. */
|
|
95
|
+
async addRawEntry(meta: RawZipEntry, data: Readable): Promise<void> {
|
|
96
|
+
if (
|
|
97
|
+
meta.compressedSize > MAX_UINT32 ||
|
|
98
|
+
meta.uncompressedSize > MAX_UINT32 ||
|
|
99
|
+
this.offset > MAX_UINT32 ||
|
|
100
|
+
this.count >= MAX_UINT16
|
|
101
|
+
) {
|
|
102
|
+
throw new Error(`zip64 required for ${meta.fileName}`);
|
|
103
|
+
}
|
|
104
|
+
const name = Buffer.from(meta.fileName, 'utf8');
|
|
105
|
+
if (name.length > MAX_UINT16) {
|
|
106
|
+
throw new Error(`file name too long: ${meta.fileName}`);
|
|
107
|
+
}
|
|
108
|
+
const flags =
|
|
109
|
+
((meta.generalPurposeBitFlag ?? 0) &
|
|
110
|
+
FLAG_KEEP_MASK &
|
|
111
|
+
~FLAG_DATA_DESCRIPTOR) |
|
|
112
|
+
FLAG_UTF8_NAMES;
|
|
113
|
+
const versionNeeded = Math.max(
|
|
114
|
+
VERSION_NEEDED_DEFAULT,
|
|
115
|
+
meta.versionNeededToExtract ?? 0,
|
|
116
|
+
);
|
|
117
|
+
const localOffset = this.offset;
|
|
118
|
+
|
|
119
|
+
const local = Buffer.alloc(30 + name.length);
|
|
120
|
+
local.writeUInt32LE(LOCAL_HEADER_SIGNATURE, 0);
|
|
121
|
+
local.writeUInt16LE(versionNeeded, 4);
|
|
122
|
+
local.writeUInt16LE(flags, 6);
|
|
123
|
+
local.writeUInt16LE(meta.compressionMethod, 8);
|
|
124
|
+
local.writeUInt16LE(meta.lastModFileTime, 10);
|
|
125
|
+
local.writeUInt16LE(meta.lastModFileDate, 12);
|
|
126
|
+
local.writeUInt32LE(meta.crc32 >>> 0, 14);
|
|
127
|
+
local.writeUInt32LE(meta.compressedSize, 18);
|
|
128
|
+
local.writeUInt32LE(meta.uncompressedSize, 22);
|
|
129
|
+
local.writeUInt16LE(name.length, 26);
|
|
130
|
+
local.writeUInt16LE(0, 28);
|
|
131
|
+
name.copy(local, 30);
|
|
132
|
+
await this.write(local);
|
|
133
|
+
|
|
134
|
+
let copied = 0;
|
|
135
|
+
for await (const chunk of data as AsyncIterable<Buffer>) {
|
|
136
|
+
copied += chunk.length;
|
|
137
|
+
await this.write(chunk);
|
|
138
|
+
}
|
|
139
|
+
if (copied !== meta.compressedSize) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`${meta.fileName}: read ${copied} bytes, expected ${meta.compressedSize}`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const header = Buffer.alloc(46 + name.length);
|
|
146
|
+
header.writeUInt32LE(CENTRAL_HEADER_SIGNATURE, 0);
|
|
147
|
+
header.writeUInt16LE(meta.versionMadeBy ?? VERSION_MADE_BY_UNIX, 4);
|
|
148
|
+
header.writeUInt16LE(versionNeeded, 6);
|
|
149
|
+
header.writeUInt16LE(flags, 8);
|
|
150
|
+
header.writeUInt16LE(meta.compressionMethod, 10);
|
|
151
|
+
header.writeUInt16LE(meta.lastModFileTime, 12);
|
|
152
|
+
header.writeUInt16LE(meta.lastModFileDate, 14);
|
|
153
|
+
header.writeUInt32LE(meta.crc32 >>> 0, 16);
|
|
154
|
+
header.writeUInt32LE(meta.compressedSize, 20);
|
|
155
|
+
header.writeUInt32LE(meta.uncompressedSize, 24);
|
|
156
|
+
header.writeUInt16LE(name.length, 28);
|
|
157
|
+
header.writeUInt16LE(0, 30); // extra
|
|
158
|
+
header.writeUInt16LE(0, 32); // comment
|
|
159
|
+
header.writeUInt16LE(0, 34); // disk number start
|
|
160
|
+
header.writeUInt16LE(meta.internalFileAttributes ?? 0, 36);
|
|
161
|
+
header.writeUInt32LE(
|
|
162
|
+
(meta.externalFileAttributes ?? UNIX_REGULAR_FILE_ATTRIBUTES) >>> 0,
|
|
163
|
+
38,
|
|
164
|
+
);
|
|
165
|
+
header.writeUInt32LE(localOffset, 42);
|
|
166
|
+
name.copy(header, 46);
|
|
167
|
+
this.central.push(header);
|
|
168
|
+
this.count += 1;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Add a local file uncompressed (its CRC is computed in a first pass).
|
|
173
|
+
* `like` lends its timestamp and attributes, e.g. the archive entry this
|
|
174
|
+
* file replaces.
|
|
175
|
+
*/
|
|
176
|
+
async addStoredFile(
|
|
177
|
+
filePath: string,
|
|
178
|
+
fileName: string,
|
|
179
|
+
like?: Pick<
|
|
180
|
+
Entry,
|
|
181
|
+
| 'lastModFileTime'
|
|
182
|
+
| 'lastModFileDate'
|
|
183
|
+
| 'externalFileAttributes'
|
|
184
|
+
| 'versionMadeBy'
|
|
185
|
+
>,
|
|
186
|
+
): Promise<void> {
|
|
187
|
+
let crc = 0;
|
|
188
|
+
let size = 0;
|
|
189
|
+
for await (const chunk of fs.createReadStream(
|
|
190
|
+
filePath,
|
|
191
|
+
) as AsyncIterable<Buffer>) {
|
|
192
|
+
crc = crc32Update(crc, chunk);
|
|
193
|
+
size += chunk.length;
|
|
194
|
+
}
|
|
195
|
+
await this.addRawEntry(
|
|
196
|
+
{
|
|
197
|
+
fileName,
|
|
198
|
+
compressionMethod: 0,
|
|
199
|
+
crc32: crc,
|
|
200
|
+
compressedSize: size,
|
|
201
|
+
uncompressedSize: size,
|
|
202
|
+
lastModFileTime: like?.lastModFileTime ?? 0,
|
|
203
|
+
lastModFileDate: like?.lastModFileDate ?? 0x21, // 1980-01-01
|
|
204
|
+
externalFileAttributes: like?.externalFileAttributes,
|
|
205
|
+
versionMadeBy: like?.versionMadeBy,
|
|
206
|
+
},
|
|
207
|
+
fs.createReadStream(filePath),
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Write the central directory and close the file. */
|
|
212
|
+
async end(): Promise<void> {
|
|
213
|
+
const cdOffset = this.offset;
|
|
214
|
+
for (const header of this.central) await this.write(header);
|
|
215
|
+
const cdSize = this.offset - cdOffset;
|
|
216
|
+
if (cdOffset > MAX_UINT32 || cdSize > MAX_UINT32) {
|
|
217
|
+
throw new Error('zip64 required for the central directory');
|
|
218
|
+
}
|
|
219
|
+
const eocd = Buffer.alloc(22);
|
|
220
|
+
eocd.writeUInt32LE(EOCD_SIGNATURE, 0);
|
|
221
|
+
eocd.writeUInt16LE(0, 4);
|
|
222
|
+
eocd.writeUInt16LE(0, 6);
|
|
223
|
+
eocd.writeUInt16LE(this.count, 8);
|
|
224
|
+
eocd.writeUInt16LE(this.count, 10);
|
|
225
|
+
eocd.writeUInt32LE(cdSize, 12);
|
|
226
|
+
eocd.writeUInt32LE(cdOffset, 16);
|
|
227
|
+
eocd.writeUInt16LE(0, 20);
|
|
228
|
+
await this.write(eocd);
|
|
229
|
+
this.out.end();
|
|
230
|
+
await this.closed;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Stop writing and remove the partial output. */
|
|
234
|
+
async abort(): Promise<void> {
|
|
235
|
+
this.out.destroy();
|
|
236
|
+
await this.closed.catch(() => {});
|
|
237
|
+
await fs.remove(this.output).catch(() => {});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private write(chunk: Buffer): Promise<void> {
|
|
241
|
+
if (this.failure) return Promise.reject(this.failure);
|
|
242
|
+
this.offset += chunk.length;
|
|
243
|
+
return new Promise((resolve, reject) => {
|
|
244
|
+
this.out.write(chunk, (error) => (error ? reject(error) : resolve()));
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
}
|