@gfean/react-native-bundle-drop 0.1.39 → 0.1.47
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/CLIENT_README.md +21 -12
- package/README.md +19 -12
- package/lib/api/client.d.ts +1 -0
- package/lib/api/client.js +24 -0
- package/lib/api/clientApi.d.ts +8 -0
- package/lib/api/clientApi.js +42 -0
- package/lib/api/types.d.ts +36 -0
- package/lib/api/types.js +2 -0
- package/lib/api/utils.d.ts +1 -0
- package/lib/api/utils.js +8 -0
- package/lib/bootstrap.d.ts +1 -0
- package/lib/bootstrap.js +10 -0
- package/lib/context.d.ts +5 -0
- package/lib/context.js +16 -0
- package/lib/errors.d.ts +16 -0
- package/lib/errors.js +18 -0
- package/lib/fs/fsUtils.d.ts +4 -0
- package/lib/fs/fsUtils.js +38 -0
- package/lib/fs/iosPointer.d.ts +1 -0
- package/lib/fs/iosPointer.js +20 -0
- package/lib/index.d.ts +7 -49
- package/lib/index.js +18 -414
- package/lib/install/installFromZip.d.ts +12 -0
- package/lib/install/installFromZip.js +117 -0
- package/lib/manager/downloadAndInstall.d.ts +14 -0
- package/lib/manager/downloadAndInstall.js +144 -0
- package/lib/manager/initUpdateManager.d.ts +7 -0
- package/lib/manager/initUpdateManager.js +60 -0
- package/lib/manager/reporting.d.ts +5 -0
- package/lib/manager/reporting.js +40 -0
- package/lib/manager/updateCheck.d.ts +5 -0
- package/lib/manager/updateCheck.js +58 -0
- package/lib/manager/updateState.d.ts +14 -0
- package/lib/manager/updateState.js +62 -0
- package/lib/native/bundleDropNative.d.ts +2 -0
- package/lib/native/bundleDropNative.js +22 -0
- package/lib/scripts/download-bundle.js +29 -12
- package/lib/scripts/upload-bundle.js +26 -19
- package/lib/useBundleDrop.d.ts +21 -20
- package/lib/useBundleDrop.js +67 -73
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,418 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.useBundleDrop = exports.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
exports.
|
|
10
|
-
exports.
|
|
11
|
-
exports.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const isIOS = react_native_1.Platform.OS === 'ios';
|
|
25
|
-
const runtimeVersion = config.runtimeVersion?.[isIOS ? 'ios' : 'android'];
|
|
26
|
-
const BUNDLE_DROP_ROOT = isIOS ? `${react_native_fs_1.default.LibraryDirectoryPath}/bundle-drop` : `${react_native_fs_1.default.DocumentDirectoryPath}`;
|
|
27
|
-
async function ensureDir(dir) {
|
|
28
|
-
if (!(await react_native_fs_1.default.exists(dir))) {
|
|
29
|
-
await react_native_fs_1.default.mkdir(dir);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
async function atomicWriteJson(path, data) {
|
|
33
|
-
const tmp = `${path}.tmp`;
|
|
34
|
-
await react_native_fs_1.default.writeFile(tmp, JSON.stringify(data), 'utf8');
|
|
35
|
-
if (await react_native_fs_1.default.exists(path)) {
|
|
36
|
-
await react_native_fs_1.default.unlink(path);
|
|
37
|
-
}
|
|
38
|
-
await react_native_fs_1.default.moveFile(tmp, path);
|
|
39
|
-
}
|
|
40
|
-
async function setCurrentBundlePointer(bundlePath, hash) {
|
|
41
|
-
if (!isIOS)
|
|
42
|
-
return; // pointer is iOS-only
|
|
43
|
-
const root = `${react_native_fs_1.default.LibraryDirectoryPath}/bundle-drop`;
|
|
44
|
-
await ensureDir(root);
|
|
45
|
-
await atomicWriteJson(`${root}/current.json`, { hash, bundlePath, updatedAt: new Date().toISOString() });
|
|
46
|
-
}
|
|
47
|
-
const buildQuery = (params) => Object.entries(params)
|
|
48
|
-
.filter(([, v]) => v !== undefined && v !== null)
|
|
49
|
-
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
|
|
50
|
-
.join('&');
|
|
51
|
-
if (react_native_1.Platform.OS === 'android') {
|
|
52
|
-
// Patch early so assets bundled with the app (before OTA) still resolve.
|
|
53
|
-
(0, injectImageResolver_1.injectBundleDropImageResolver)();
|
|
54
|
-
(0, patchImageAutoResolve_1.patchImageRenderToForceResolve)();
|
|
55
|
-
}
|
|
56
|
-
async function getDownloadedBundlePath() {
|
|
57
|
-
if (!BundleDrop?.getDownloadedBundlePath) {
|
|
58
|
-
console.warn('BundleDrop.getDownloadedBundlePath is not defined');
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
try {
|
|
62
|
-
const path = await BundleDrop.getDownloadedBundlePath();
|
|
63
|
-
return path;
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
console.error('❌ Failed to get bundle path:', e);
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
async function checkForUpdate(channelName = 'General', onStatusUpdate) {
|
|
71
|
-
const { serverUrl, project, org } = config;
|
|
72
|
-
const local = await (0, bundleInfo_1.readBundleInfo)();
|
|
73
|
-
const platform = react_native_1.Platform.OS === 'ios' ? 'ios' : 'android';
|
|
74
|
-
// Include local metadata so server can short-circuit if already up-to-date.
|
|
75
|
-
const query = buildQuery({
|
|
76
|
-
platform,
|
|
77
|
-
orgSlug: org.slug,
|
|
78
|
-
project: project.slug,
|
|
79
|
-
channelName,
|
|
80
|
-
currentBundleVersion: local?.bundleVersion,
|
|
81
|
-
currentHash: local?.hash,
|
|
82
|
-
runtimeVersion,
|
|
83
|
-
});
|
|
84
|
-
try {
|
|
85
|
-
onStatusUpdate?.('🔍 Checking for updates...');
|
|
86
|
-
const res = await axios_1.default.get(`${serverUrl}/bundle/checkForUpdate?${query}`, {
|
|
87
|
-
headers: {
|
|
88
|
-
Accept: 'application/json',
|
|
89
|
-
...(project.apiKey ? { 'x-api-key': project.apiKey } : {}),
|
|
90
|
-
},
|
|
91
|
-
timeout: 15000,
|
|
92
|
-
});
|
|
93
|
-
if (res.data.upToDate) {
|
|
94
|
-
onStatusUpdate?.(`You've got the latest version for the given channel ${res.data.channelName ?? ''}`);
|
|
95
|
-
}
|
|
96
|
-
if (res.data.incompatible) {
|
|
97
|
-
onStatusUpdate?.('⛔️ No compatible update for this binary');
|
|
98
|
-
}
|
|
99
|
-
return res.data;
|
|
100
|
-
}
|
|
101
|
-
catch (e) {
|
|
102
|
-
console.warn('⚠️ checkForUpdate failed, will fallback to download:', e?.toString?.() || e);
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
async function getInstalledBundleInfo() {
|
|
107
|
-
return (0, bundleInfo_1.readBundleInfo)();
|
|
108
|
-
}
|
|
109
|
-
async function downloadAndInstallBundle(platformOrStatus, onStatusUpdate, options) {
|
|
110
|
-
const { serverUrl, project, org } = config;
|
|
111
|
-
const channelName = options?.channelName || 'General';
|
|
112
|
-
// Allow passing status callback as first arg for convenience.
|
|
113
|
-
const platform = typeof platformOrStatus === 'string'
|
|
114
|
-
? platformOrStatus
|
|
115
|
-
: (react_native_1.Platform.OS === 'ios' ? 'ios' : 'android');
|
|
116
|
-
const statusCb = typeof platformOrStatus === 'function' ? platformOrStatus : onStatusUpdate;
|
|
117
|
-
const jsBundlePath = `${react_native_fs_1.default.DocumentDirectoryPath}/main.jsbundle`;
|
|
118
|
-
let bundleZipUrl = `${serverUrl}/bundle/latest?platform=${platform}&orgSlug=${org.slug}&project=${project.slug}&channelName=${channelName}`;
|
|
119
|
-
// Optional pre-flight check to avoid unnecessary downloads.
|
|
120
|
-
const checkResult = await checkForUpdate(channelName, statusCb);
|
|
121
|
-
if (!options?.forceDownload && checkResult?.upToDate) {
|
|
122
|
-
statusCb?.('✅ You have the latest version');
|
|
123
|
-
return jsBundlePath;
|
|
124
|
-
}
|
|
125
|
-
if (!options?.forceDownload && checkResult?.incompatible) {
|
|
126
|
-
statusCb?.('⛔️ No compatible update for this binary');
|
|
127
|
-
return null;
|
|
128
|
-
}
|
|
129
|
-
if (checkResult?.downloadUrl) {
|
|
130
|
-
bundleZipUrl = checkResult.downloadUrl;
|
|
131
|
-
}
|
|
132
|
-
const applyBehavior = options?.applyBehavior || 'immediate';
|
|
133
|
-
try {
|
|
134
|
-
// Fetch bundle zip from server.
|
|
135
|
-
statusCb?.('⬇️ Downloading full bundle ZIP!...');
|
|
136
|
-
const zipResponse = await axios_1.default.get(bundleZipUrl, {
|
|
137
|
-
responseType: 'arraybuffer',
|
|
138
|
-
headers: {
|
|
139
|
-
Accept: '*/*',
|
|
140
|
-
...(project.apiKey ? { 'x-api-key': project.apiKey } : {}),
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
const zipBuffer = buffer_1.Buffer.from(zipResponse.data);
|
|
144
|
-
let files;
|
|
145
|
-
try {
|
|
146
|
-
files = (0, fflate_1.unzipSync)(new Uint8Array(zipBuffer));
|
|
147
|
-
}
|
|
148
|
-
catch (e) {
|
|
149
|
-
console.error('❌ unzipSync failed: Invalid ZIP data', e);
|
|
150
|
-
throw e;
|
|
151
|
-
}
|
|
152
|
-
// Try to read metadata from the bundle itself as a fallback when server check is unavailable.
|
|
153
|
-
const metadataFromZip = {};
|
|
154
|
-
const metadataFile = platform === 'android' ? 'metadata-android.json' : 'metadata-ios.json';
|
|
155
|
-
if (files[metadataFile]) {
|
|
156
|
-
try {
|
|
157
|
-
const raw = buffer_1.Buffer.from(files[metadataFile]).toString();
|
|
158
|
-
const parsed = JSON.parse(raw);
|
|
159
|
-
metadataFromZip.bundleVersion = parsed.bundleVersion ?? metadataFromZip.bundleVersion;
|
|
160
|
-
metadataFromZip.version = parsed.version ?? metadataFromZip.version;
|
|
161
|
-
metadataFromZip.hash = parsed.hash ?? metadataFromZip.hash;
|
|
162
|
-
metadataFromZip.runtimeVersion = parsed.runtimeVersion ?? metadataFromZip.runtimeVersion;
|
|
163
|
-
}
|
|
164
|
-
catch (e) {
|
|
165
|
-
console.warn('⚠️ Failed to parse bundle metadata from zip', e);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
const fileNames = Object.keys(files);
|
|
169
|
-
statusCb?.(`📁 ${fileNames.length} files extracted from ZIP`);
|
|
170
|
-
statusCb?.('💾 Writing files...');
|
|
171
|
-
// Build a runtime manifest from extracted assets to handle filename-only resolutions.
|
|
172
|
-
const runtimeManifest = {};
|
|
173
|
-
const hash = checkResult?.hash ?? metadataFromZip.hash;
|
|
174
|
-
if (!hash) {
|
|
175
|
-
throw new Error('Missing bundle hash');
|
|
176
|
-
}
|
|
177
|
-
// Platform-specific bundle location
|
|
178
|
-
const bundleDir = isIOS ? `${BUNDLE_DROP_ROOT}/bundles/${hash}` : `${react_native_fs_1.default.DocumentDirectoryPath}`;
|
|
179
|
-
if (isIOS) {
|
|
180
|
-
await ensureDir(bundleDir);
|
|
181
|
-
}
|
|
182
|
-
const bundlePath = isIOS ? `${bundleDir}/main.jsbundle` : `${bundleDir}/main.jsbundle`;
|
|
183
|
-
for (const [filename, fileData] of Object.entries(files)) {
|
|
184
|
-
const dataBuffer = buffer_1.Buffer.from(fileData);
|
|
185
|
-
if (filename === 'main.jsbundle') {
|
|
186
|
-
// Write main bundle atomically
|
|
187
|
-
const tmp = `${bundlePath}.tmp`;
|
|
188
|
-
await react_native_fs_1.default.writeFile(tmp, dataBuffer.toString('base64'), 'base64');
|
|
189
|
-
if (await react_native_fs_1.default.exists(bundlePath)) {
|
|
190
|
-
await react_native_fs_1.default.unlink(bundlePath);
|
|
191
|
-
}
|
|
192
|
-
await react_native_fs_1.default.moveFile(tmp, bundlePath);
|
|
193
|
-
// Legacy copy for iOS fallback (documents/main.jsbundle + assets)
|
|
194
|
-
if (isIOS) {
|
|
195
|
-
const legacyPath = `${react_native_fs_1.default.DocumentDirectoryPath}/main.jsbundle`;
|
|
196
|
-
const legacyTmp = `${legacyPath}.tmp`;
|
|
197
|
-
await react_native_fs_1.default.writeFile(legacyTmp, dataBuffer.toString('base64'), 'base64');
|
|
198
|
-
if (await react_native_fs_1.default.exists(legacyPath)) {
|
|
199
|
-
await react_native_fs_1.default.unlink(legacyPath);
|
|
200
|
-
}
|
|
201
|
-
await react_native_fs_1.default.moveFile(legacyTmp, legacyPath);
|
|
202
|
-
}
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
// Write other files under bundleDir (e.g., metadata, assets)
|
|
206
|
-
const filePath = `${bundleDir}/${filename}`;
|
|
207
|
-
const dirPath = filePath.substring(0, filePath.lastIndexOf('/'));
|
|
208
|
-
if (!(await react_native_fs_1.default.exists(dirPath))) {
|
|
209
|
-
await react_native_fs_1.default.mkdir(dirPath);
|
|
210
|
-
}
|
|
211
|
-
await react_native_fs_1.default.writeFile(filePath, dataBuffer.toString('base64'), 'base64');
|
|
212
|
-
// Legacy copy for iOS assets/metadata into Documents for fallback bundle
|
|
213
|
-
if (isIOS) {
|
|
214
|
-
const legacyPath = `${react_native_fs_1.default.DocumentDirectoryPath}/${filename}`;
|
|
215
|
-
const legacyDir = legacyPath.substring(0, legacyPath.lastIndexOf('/'));
|
|
216
|
-
if (!(await react_native_fs_1.default.exists(legacyDir))) {
|
|
217
|
-
await react_native_fs_1.default.mkdir(legacyDir);
|
|
218
|
-
}
|
|
219
|
-
await react_native_fs_1.default.writeFile(legacyPath, dataBuffer.toString('base64'), 'base64');
|
|
220
|
-
}
|
|
221
|
-
// Collect runtime manifest entries for assets; support paths with or without "assets/" prefix.
|
|
222
|
-
if (filename.includes('drawable') || filename.startsWith('assets/') || filename.startsWith('res/')) {
|
|
223
|
-
const noExt = filename.replace(/\.[^.]+$/, '');
|
|
224
|
-
const base = noExt.split('/').pop();
|
|
225
|
-
const withAssetsPrefix = filename.startsWith('assets/') ? filename : `assets/${filename}`;
|
|
226
|
-
// Android should not include hash prefixes; iOS uses local file assets
|
|
227
|
-
const mappedPath = isIOS ? `${hash}/${filename}` : filename;
|
|
228
|
-
runtimeManifest[noExt.replace(/^assets\//, '')] = mappedPath;
|
|
229
|
-
runtimeManifest[withAssetsPrefix.replace(/\.[^.]+$/, '')] = mappedPath;
|
|
230
|
-
if (base && !runtimeManifest[base]) {
|
|
231
|
-
runtimeManifest[base] = mappedPath;
|
|
232
|
-
}
|
|
233
|
-
const baseWithExt = filename.split('/').pop();
|
|
234
|
-
if (baseWithExt && !runtimeManifest[baseWithExt]) {
|
|
235
|
-
runtimeManifest[baseWithExt] = mappedPath;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
// ✅ Load image manifest (Android)
|
|
240
|
-
if (react_native_1.Platform.OS === 'android' && files['image-manifest.json']) {
|
|
241
|
-
try {
|
|
242
|
-
const manifest = buffer_1.Buffer.from(files['image-manifest.json']).toString();
|
|
243
|
-
const parsed = JSON.parse(manifest);
|
|
244
|
-
Object.assign(image_manifest_1.imageManifest, parsed);
|
|
245
|
-
statusCb?.(`🧩 Image manifest injected: ${Object.keys(parsed).length} items`);
|
|
246
|
-
}
|
|
247
|
-
catch (e) {
|
|
248
|
-
console.warn('❌ Failed to parse image manifest:', e);
|
|
249
|
-
statusCb?.('❌ Failed to parse image manifest');
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
// Merge runtime manifest to catch any assets missing from the bundle-time manifest.
|
|
253
|
-
if (react_native_1.Platform.OS === 'android' && Object.keys(runtimeManifest).length > 0) {
|
|
254
|
-
Object.assign(image_manifest_1.imageManifest, runtimeManifest);
|
|
255
|
-
}
|
|
256
|
-
if (react_native_1.Platform.OS === 'android') {
|
|
257
|
-
// Re-patch with the freshest manifest so newly downloaded assets resolve.
|
|
258
|
-
(0, injectImageResolver_1.injectBundleDropImageResolver)(image_manifest_1.imageManifest);
|
|
259
|
-
}
|
|
260
|
-
statusCb?.(applyBehavior === 'immediate'
|
|
261
|
-
? '✅ Update complete! Reloading app...'
|
|
262
|
-
: '✅ Update downloaded. Will apply on next launch/apply call.');
|
|
263
|
-
// Persist installed metadata for future skip logic.
|
|
264
|
-
const installedInfo = {
|
|
265
|
-
bundleVersion: checkResult?.bundleVersion ?? metadataFromZip.bundleVersion,
|
|
266
|
-
version: checkResult?.version ?? metadataFromZip.version,
|
|
267
|
-
hash,
|
|
268
|
-
channelName,
|
|
269
|
-
platform,
|
|
270
|
-
installedAt: new Date().toISOString(),
|
|
271
|
-
pendingApply: applyBehavior !== 'immediate',
|
|
272
|
-
runtimeVersion: metadataFromZip.runtimeVersion ?? checkResult?.runtimeVersion ?? runtimeVersion ?? undefined,
|
|
273
|
-
};
|
|
274
|
-
(0, bundleInfo_1.writeBundleInfo)(installedInfo);
|
|
275
|
-
await setCurrentBundlePointer(bundlePath, hash);
|
|
276
|
-
if (applyBehavior === 'immediate') {
|
|
277
|
-
//eslint-disable-next-line -- function declarations are hoisted and safe to use
|
|
278
|
-
await reportInstalledIfReady({ hasBundle: true, info: installedInfo });
|
|
279
|
-
}
|
|
280
|
-
return bundlePath;
|
|
281
|
-
}
|
|
282
|
-
catch (err) {
|
|
283
|
-
console.error('❌ OTA update failed:', err);
|
|
284
|
-
statusCb?.(`❌ OTA update failed. ${err?.toString()}`);
|
|
285
|
-
throw err;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
const restartAppAndInstallUpdate = async (onStatusUpdate, options) => {
|
|
289
|
-
// Convenience wrapper: download then reload if immediate.
|
|
290
|
-
const response = await downloadAndInstallBundle(undefined, onStatusUpdate, options);
|
|
291
|
-
if (response && (options?.applyBehavior ?? 'immediate') === 'immediate') {
|
|
292
|
-
BundleDrop?.restartReactNative?.();
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
|
-
exports.restartAppAndInstallUpdate = restartAppAndInstallUpdate;
|
|
296
|
-
const downloadUpdate = async (options, onStatusUpdate) => {
|
|
297
|
-
return downloadAndInstallBundle(undefined, onStatusUpdate, {
|
|
298
|
-
...options,
|
|
299
|
-
applyBehavior: options?.applyBehavior || 'manual',
|
|
300
|
-
});
|
|
301
|
-
};
|
|
302
|
-
exports.downloadUpdate = downloadUpdate;
|
|
303
|
-
const applyReadyUpdate = async (onStatusUpdate) => {
|
|
304
|
-
const path = await getDownloadedBundlePath();
|
|
305
|
-
const info = await (0, bundleInfo_1.readBundleInfo)();
|
|
306
|
-
if (!path) {
|
|
307
|
-
onStatusUpdate?.('⚠️ No downloaded bundle to apply');
|
|
308
|
-
return false;
|
|
309
|
-
}
|
|
310
|
-
if (info?.pendingApply === false) {
|
|
311
|
-
onStatusUpdate?.('ℹ️ Bundle already applied');
|
|
312
|
-
return false;
|
|
313
|
-
}
|
|
314
|
-
onStatusUpdate?.('♻️ Applying downloaded bundle...');
|
|
315
|
-
await (0, bundleInfo_1.updateBundleInfo)({ pendingApply: false, installedAt: new Date().toISOString() });
|
|
316
|
-
//eslint-disable-next-line -- function declarations are hoisted and safe to use
|
|
317
|
-
await reportInstalledIfReady({ hasBundle: true, info: { ...info, pendingApply: false } });
|
|
318
|
-
BundleDrop?.restartReactNative?.();
|
|
319
|
-
return true;
|
|
320
|
-
};
|
|
321
|
-
exports.applyReadyUpdate = applyReadyUpdate;
|
|
322
|
-
const getUpdateState = async () => {
|
|
323
|
-
const info = await (0, bundleInfo_1.readBundleInfo)();
|
|
324
|
-
const path = await getDownloadedBundlePath();
|
|
325
|
-
return {
|
|
326
|
-
hasBundle: !!path,
|
|
327
|
-
info,
|
|
328
|
-
pendingApply: info?.pendingApply ?? false,
|
|
329
|
-
};
|
|
330
|
-
};
|
|
331
|
-
exports.getUpdateState = getUpdateState;
|
|
332
|
-
async function reportInstalledIfReady(state) {
|
|
333
|
-
const current = state ?? (await (0, exports.getUpdateState)());
|
|
334
|
-
const info = state?.info ?? current.info;
|
|
335
|
-
const hasBundle = state?.hasBundle ?? current.hasBundle;
|
|
336
|
-
if (!info?.hash)
|
|
337
|
-
return;
|
|
338
|
-
if (info.pendingApply)
|
|
339
|
-
return;
|
|
340
|
-
if (hasBundle === false)
|
|
341
|
-
return;
|
|
342
|
-
if (info.lastInstalledReportedHash === info.hash)
|
|
343
|
-
return;
|
|
344
|
-
try {
|
|
345
|
-
await axios_1.default.post(`${config.serverUrl}/bundle/${encodeURIComponent(info.hash)}/installed`, {
|
|
346
|
-
orgSlug: config.org.slug,
|
|
347
|
-
projectSlug: config.project.slug,
|
|
348
|
-
channelName: info.channelName,
|
|
349
|
-
platform: info.platform,
|
|
350
|
-
}, {
|
|
351
|
-
headers: {
|
|
352
|
-
Accept: 'application/json',
|
|
353
|
-
...(config.project.apiKey ? { 'x-api-key': config.project.apiKey } : {}),
|
|
354
|
-
},
|
|
355
|
-
timeout: 10000,
|
|
356
|
-
});
|
|
357
|
-
await (0, bundleInfo_1.updateBundleInfo)({ lastInstalledReportedHash: info.hash });
|
|
358
|
-
}
|
|
359
|
-
catch (e) {
|
|
360
|
-
console.warn('⚠️ Failed to report bundle install:', e?.toString?.() || e);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* Initialize update behavior at app start.
|
|
365
|
-
* - manual: no auto-download/apply (optional checkOnly)
|
|
366
|
-
* - immediate: download and apply now; if a pending bundle exists, apply it
|
|
367
|
-
* - on-next-launch: download in background; apply on next launch if pending
|
|
368
|
-
*/
|
|
369
|
-
async function initUpdateManager(opts) {
|
|
370
|
-
const policy = opts?.policy || 'manual';
|
|
371
|
-
const channelName = opts?.channelName || 'General';
|
|
372
|
-
const statusCb = opts?.onStatusUpdate;
|
|
373
|
-
// If a bundle is pending apply, honor it based on policy.
|
|
374
|
-
const pending = await (0, exports.getUpdateState)();
|
|
375
|
-
if (pending.pendingApply &&
|
|
376
|
-
(policy === 'immediate' || policy === 'on-next-launch')) {
|
|
377
|
-
if (policy === 'immediate') {
|
|
378
|
-
statusCb?.('♻️ Applying previously downloaded update...');
|
|
379
|
-
await (0, exports.applyReadyUpdate)(statusCb);
|
|
380
|
-
await reportInstalledIfReady();
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
if (policy === 'on-next-launch') {
|
|
384
|
-
// On next launch, the native layer should already load the downloaded bundle.
|
|
385
|
-
// Avoid forcing another reload; just clear the pending flag.
|
|
386
|
-
await (0, bundleInfo_1.updateBundleInfo)({ pendingApply: false });
|
|
387
|
-
statusCb?.('✅ Using downloaded bundle on launch');
|
|
388
|
-
await reportInstalledIfReady();
|
|
389
|
-
return;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
// Manual policy: optionally just check and return.
|
|
393
|
-
if (policy === 'manual') {
|
|
394
|
-
if (!opts?.checkOnly)
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
const check = await checkForUpdate(channelName, statusCb);
|
|
398
|
-
if (check?.upToDate) {
|
|
399
|
-
statusCb?.('✅ You have the latest version');
|
|
400
|
-
return;
|
|
401
|
-
}
|
|
402
|
-
if (policy === 'immediate') {
|
|
403
|
-
await (0, exports.restartAppAndInstallUpdate)(statusCb || (() => { }), {
|
|
404
|
-
channelName,
|
|
405
|
-
applyBehavior: 'immediate',
|
|
406
|
-
forceDownload: false,
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
|
-
else if (policy === 'on-next-launch') {
|
|
410
|
-
await (0, exports.downloadUpdate)({ channelName, applyBehavior: 'on-next-launch', forceDownload: false }, statusCb || (() => { }));
|
|
411
|
-
statusCb?.('✅ Update downloaded for next launch');
|
|
412
|
-
}
|
|
413
|
-
// Final check to report install for already-applied bundles (deduped by lastInstalledReportedHash).
|
|
414
|
-
await reportInstalledIfReady();
|
|
415
|
-
}
|
|
416
|
-
// Hooks
|
|
3
|
+
exports.useBundleDrop = exports.isBundleDropError = exports.BundleDropError = exports.getDownloadedBundlePath = exports.initUpdateManager = exports.applyUpdate = exports.getUpdateState = exports.downloadUpdate = exports.getInstalledBundleInfo = exports.checkForUpdate = exports.getAvailableChannels = void 0;
|
|
4
|
+
require("./bootstrap");
|
|
5
|
+
var updateCheck_1 = require("./manager/updateCheck");
|
|
6
|
+
Object.defineProperty(exports, "getAvailableChannels", { enumerable: true, get: function () { return updateCheck_1.getAvailableChannels; } });
|
|
7
|
+
Object.defineProperty(exports, "checkForUpdate", { enumerable: true, get: function () { return updateCheck_1.checkForUpdate; } });
|
|
8
|
+
Object.defineProperty(exports, "getInstalledBundleInfo", { enumerable: true, get: function () { return updateCheck_1.getInstalledBundleInfo; } });
|
|
9
|
+
var downloadAndInstall_1 = require("./manager/downloadAndInstall");
|
|
10
|
+
Object.defineProperty(exports, "downloadUpdate", { enumerable: true, get: function () { return downloadAndInstall_1.downloadUpdate; } });
|
|
11
|
+
var updateState_1 = require("./manager/updateState");
|
|
12
|
+
Object.defineProperty(exports, "getUpdateState", { enumerable: true, get: function () { return updateState_1.getUpdateState; } });
|
|
13
|
+
Object.defineProperty(exports, "applyUpdate", { enumerable: true, get: function () { return updateState_1.applyUpdate; } });
|
|
14
|
+
var initUpdateManager_1 = require("./manager/initUpdateManager");
|
|
15
|
+
Object.defineProperty(exports, "initUpdateManager", { enumerable: true, get: function () { return initUpdateManager_1.initUpdateManager; } });
|
|
16
|
+
var bundleDropNative_1 = require("./native/bundleDropNative");
|
|
17
|
+
Object.defineProperty(exports, "getDownloadedBundlePath", { enumerable: true, get: function () { return bundleDropNative_1.getDownloadedBundlePathNative; } });
|
|
18
|
+
var errors_1 = require("./errors");
|
|
19
|
+
Object.defineProperty(exports, "BundleDropError", { enumerable: true, get: function () { return errors_1.BundleDropError; } });
|
|
20
|
+
Object.defineProperty(exports, "isBundleDropError", { enumerable: true, get: function () { return errors_1.isBundleDropError; } });
|
|
417
21
|
var useBundleDrop_1 = require("./useBundleDrop");
|
|
418
22
|
Object.defineProperty(exports, "useBundleDrop", { enumerable: true, get: function () { return useBundleDrop_1.useBundleDrop; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BundleInfo } from '../bundleInfo';
|
|
2
|
+
type InstallFromZipParams = {
|
|
3
|
+
zipArrayBuffer: ArrayBuffer;
|
|
4
|
+
hash?: string;
|
|
5
|
+
platform?: 'ios' | 'android';
|
|
6
|
+
statusCb?: (status: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare function installFromZip(params: InstallFromZipParams): Promise<{
|
|
9
|
+
bundlePath: string;
|
|
10
|
+
metadataFromZip: Partial<BundleInfo>;
|
|
11
|
+
}>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
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.installFromZip = installFromZip;
|
|
7
|
+
const buffer_1 = require("buffer");
|
|
8
|
+
const fflate_1 = require("fflate");
|
|
9
|
+
const react_native_fs_1 = __importDefault(require("react-native-fs"));
|
|
10
|
+
const context_1 = require("../context");
|
|
11
|
+
const image_manifest_1 = require("../image-manifest");
|
|
12
|
+
const injectImageResolver_1 = require("../injectImageResolver");
|
|
13
|
+
const fsUtils_1 = require("../fs/fsUtils");
|
|
14
|
+
async function installFromZip(params) {
|
|
15
|
+
const { zipArrayBuffer, hash, statusCb, platform = context_1.platform } = params;
|
|
16
|
+
const zipBuffer = buffer_1.Buffer.from(zipArrayBuffer);
|
|
17
|
+
let files;
|
|
18
|
+
try {
|
|
19
|
+
files = (0, fflate_1.unzipSync)(new Uint8Array(zipBuffer));
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
console.error('❌ unzipSync failed: Invalid ZIP data', e);
|
|
23
|
+
throw e;
|
|
24
|
+
}
|
|
25
|
+
const metadataFromZip = {};
|
|
26
|
+
const metadataFile = platform === 'android' ? 'metadata-android.json' : 'metadata-ios.json';
|
|
27
|
+
if (files[metadataFile]) {
|
|
28
|
+
try {
|
|
29
|
+
const raw = buffer_1.Buffer.from(files[metadataFile]).toString();
|
|
30
|
+
const parsed = JSON.parse(raw);
|
|
31
|
+
metadataFromZip.bundleVersion = parsed.bundleVersion ?? metadataFromZip.bundleVersion;
|
|
32
|
+
metadataFromZip.version = parsed.version ?? metadataFromZip.version;
|
|
33
|
+
metadataFromZip.hash = parsed.hash ?? metadataFromZip.hash;
|
|
34
|
+
metadataFromZip.runtimeVersion = parsed.runtimeVersion ?? metadataFromZip.runtimeVersion;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
console.warn('⚠️ Failed to parse bundle metadata from zip', e);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const fileNames = Object.keys(files);
|
|
41
|
+
statusCb?.(`📁 ${fileNames.length} files extracted from ZIP`);
|
|
42
|
+
statusCb?.('💾 Writing files...');
|
|
43
|
+
const resolvedHash = hash ?? metadataFromZip.hash;
|
|
44
|
+
if (!resolvedHash) {
|
|
45
|
+
throw new Error('Missing bundle hash');
|
|
46
|
+
}
|
|
47
|
+
// Build a runtime manifest from extracted assets to handle filename-only resolutions.
|
|
48
|
+
const runtimeManifest = {};
|
|
49
|
+
// Platform-specific bundle location
|
|
50
|
+
const bundleDir = context_1.isIOS ? `${context_1.BUNDLE_DROP_ROOT}/bundles/${resolvedHash}` : context_1.BUNDLE_DROP_ROOT;
|
|
51
|
+
if (context_1.isIOS) {
|
|
52
|
+
await (0, fsUtils_1.ensureDir)(bundleDir);
|
|
53
|
+
}
|
|
54
|
+
const bundlePath = `${bundleDir}/main.jsbundle`;
|
|
55
|
+
for (const [filename, fileData] of Object.entries(files)) {
|
|
56
|
+
const dataBuffer = buffer_1.Buffer.from(fileData);
|
|
57
|
+
const base64 = dataBuffer.toString('base64');
|
|
58
|
+
if (filename === 'main.jsbundle') {
|
|
59
|
+
await (0, fsUtils_1.writeFileAtomicBase64)(bundlePath, base64);
|
|
60
|
+
// Legacy copy for iOS fallback (documents/main.jsbundle + assets)
|
|
61
|
+
if (context_1.isIOS) {
|
|
62
|
+
const legacyPath = `${react_native_fs_1.default.DocumentDirectoryPath}/main.jsbundle`;
|
|
63
|
+
await (0, fsUtils_1.writeFileAtomicBase64)(legacyPath, base64);
|
|
64
|
+
}
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
// Write other files under bundleDir (e.g., metadata, assets)
|
|
68
|
+
const filePath = `${bundleDir}/${filename}`;
|
|
69
|
+
await (0, fsUtils_1.ensureParentDir)(filePath);
|
|
70
|
+
await react_native_fs_1.default.writeFile(filePath, base64, 'base64');
|
|
71
|
+
// Legacy copy for iOS assets/metadata into Documents for fallback bundle
|
|
72
|
+
if (context_1.isIOS) {
|
|
73
|
+
const legacyPath = `${react_native_fs_1.default.DocumentDirectoryPath}/${filename}`;
|
|
74
|
+
await (0, fsUtils_1.ensureParentDir)(legacyPath);
|
|
75
|
+
await react_native_fs_1.default.writeFile(legacyPath, base64, 'base64');
|
|
76
|
+
}
|
|
77
|
+
// Collect runtime manifest entries for assets; support paths with or without "assets/" prefix.
|
|
78
|
+
if (filename.includes('drawable') || filename.startsWith('assets/') || filename.startsWith('res/')) {
|
|
79
|
+
const noExt = filename.replace(/\.[^.]+$/, '');
|
|
80
|
+
const base = noExt.split('/').pop();
|
|
81
|
+
const withAssetsPrefix = filename.startsWith('assets/') ? filename : `assets/${filename}`;
|
|
82
|
+
// Android should not include hash prefixes; iOS uses local file assets
|
|
83
|
+
const mappedPath = context_1.isIOS ? `${resolvedHash}/${filename}` : filename;
|
|
84
|
+
runtimeManifest[noExt.replace(/^assets\//, '')] = mappedPath;
|
|
85
|
+
runtimeManifest[withAssetsPrefix.replace(/\.[^.]+$/, '')] = mappedPath;
|
|
86
|
+
if (base && !runtimeManifest[base]) {
|
|
87
|
+
runtimeManifest[base] = mappedPath;
|
|
88
|
+
}
|
|
89
|
+
const baseWithExt = filename.split('/').pop();
|
|
90
|
+
if (baseWithExt && !runtimeManifest[baseWithExt]) {
|
|
91
|
+
runtimeManifest[baseWithExt] = mappedPath;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// ✅ Load image manifest (Android)
|
|
96
|
+
if (platform === 'android' && files['image-manifest.json']) {
|
|
97
|
+
try {
|
|
98
|
+
const manifest = buffer_1.Buffer.from(files['image-manifest.json']).toString();
|
|
99
|
+
const parsed = JSON.parse(manifest);
|
|
100
|
+
Object.assign(image_manifest_1.imageManifest, parsed);
|
|
101
|
+
statusCb?.(`🧩 Image manifest injected: ${Object.keys(parsed).length} items`);
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
console.warn('❌ Failed to parse image manifest:', e);
|
|
105
|
+
statusCb?.('❌ Failed to parse image manifest');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Merge runtime manifest to catch any assets missing from the bundle-time manifest.
|
|
109
|
+
if (platform === 'android' && Object.keys(runtimeManifest).length > 0) {
|
|
110
|
+
Object.assign(image_manifest_1.imageManifest, runtimeManifest);
|
|
111
|
+
}
|
|
112
|
+
if (platform === 'android') {
|
|
113
|
+
// Re-patch with the freshest manifest so newly downloaded assets resolve.
|
|
114
|
+
(0, injectImageResolver_1.injectBundleDropImageResolver)(image_manifest_1.imageManifest);
|
|
115
|
+
}
|
|
116
|
+
return { bundlePath, metadataFromZip };
|
|
117
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type DownloadUpdateResult = {
|
|
2
|
+
status: 'staged';
|
|
3
|
+
bundlePath: string;
|
|
4
|
+
hash: string;
|
|
5
|
+
} | {
|
|
6
|
+
status: 'upToDate';
|
|
7
|
+
} | {
|
|
8
|
+
status: 'incompatible';
|
|
9
|
+
};
|
|
10
|
+
type DownloadOptions = {
|
|
11
|
+
channelName?: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const downloadUpdate: (options?: DownloadOptions, onStatusUpdate?: (status: string) => void) => Promise<DownloadUpdateResult>;
|
|
14
|
+
export {};
|