Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/react-native/React/Base/RCTBundleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,13 @@ typedef NSMutableArray<NSURLQueryItem *> *_Nullable (^RCTPackagerOptionsUpdater)
andDefaultGetter:(nullable RCTBridgelessBundleURLGetter)defaultGetter;
- (void)resetBundleURL;
@property (nonatomic, nullable) NSURL *bundleURL;

/**
* File URL of the most recently downloaded bundle persisted to disk. Only set
* when the bundle was fetched from a remote URL (e.g. the packager); nil when
* the bundle was loaded from a local file, in which case its location on disk
* is the bundleURL itself.
*/
@property (nonatomic, nullable) NSURL *downloadedBundleFileURL;
@property (nonatomic, nonnull) RCTBundleConfiguration *bundleConfig;
@end
8 changes: 8 additions & 0 deletions packages/react-native/React/Base/RCTJavaScriptLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ NS_ENUM(NSInteger){
*/
@property (nonatomic, readonly) NSInteger filesChangedCount;

/**
* File URL of the downloaded bundle persisted to disk, from which self.data is
* memory-mapped. Only set when the bundle was fetched from a remote URL (e.g. the
* packager); nil when the bundle was loaded from a local file, in which case the
* bundle's location on disk is self.url.
*/
@property (strong, nonatomic, readonly) NSURL *downloadedBundleFileURL;

@end

typedef void (^RCTSourceLoadProgressBlock)(RCTLoadingProgress *progressData);
Expand Down
29 changes: 28 additions & 1 deletion packages/react-native/React/Base/RCTJavaScriptLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ @interface RCTSource () {
NSData *_data;
NSUInteger _length;
NSInteger _filesChangedCount;
NSURL *_downloadedBundleFileURL;
}

@end
Expand Down Expand Up @@ -210,6 +211,15 @@ static void parseHeaders(NSDictionary *headers, RCTSource *source)
source->_filesChangedCount = [headers[@"X-Metro-Files-Changed-Count"] integerValue];
}

static NSURL *persistDownloadedBundle(NSData *data, NSError **error)
{
NSString *bundlePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"ReactNativeDevBundle.js"];
if (![data writeToFile:bundlePath options:NSDataWritingAtomic error:error]) {
return nil;
}
return [NSURL fileURLWithPath:bundlePath];
}

static void attemptAsynchronousLoadOfBundleAtURL(
NSURL *scriptURL,
RCTSourceLoadProgressBlock onProgress,
Expand Down Expand Up @@ -314,7 +324,24 @@ static void attemptAsynchronousLoadOfBundleAtURL(
}
}

RCTSource *source = RCTSourceCreate(sourceURL, data, data.length);
NSError *persistError;
NSURL *downloadedBundleFileURL = persistDownloadedBundle(data, &persistError);
if (downloadedBundleFileURL == nil) {
onComplete(persistError, nil);
return;
}

NSError *mappingError;
NSData *bundleData = [NSData dataWithContentsOfFile:downloadedBundleFileURL.path
options:NSDataReadingMappedIfSafe
error:&mappingError];
if (bundleData == nil) {
onComplete(mappingError, nil);
return;
}

RCTSource *source = RCTSourceCreate(sourceURL, bundleData, bundleData.length);
source->_downloadedBundleFileURL = downloadedBundleFileURL;
parseHeaders(headers, source);
onComplete(nil, source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,18 @@ - (void)loadBundleAtURL:(NSURL *)sourceURL
onProgress:(RCTSourceLoadProgressBlock)onProgress
onComplete:(RCTSourceLoadBlock)loadCallback
{
__weak RCTHost *weakSelf = self;
RCTSourceLoadBlock onComplete = ^(NSError *error, RCTSource *source) {
RCTHost *strongSelf = weakSelf;
if (strongSelf != nil && source != nil) {
strongSelf->_bundleManager.downloadedBundleFileURL = source.downloadedBundleFileURL;
}
loadCallback(error, source);
};
if ([_hostDelegate respondsToSelector:@selector(loadBundleAtURL:onProgress:onComplete:)]) {
[_hostDelegate loadBundleAtURL:sourceURL onProgress:onProgress onComplete:loadCallback];
[_hostDelegate loadBundleAtURL:sourceURL onProgress:onProgress onComplete:onComplete];
} else {
[RCTJavaScriptLoader loadBundleAtURL:sourceURL onProgress:onProgress onComplete:loadCallback];
[RCTJavaScriptLoader loadBundleAtURL:sourceURL onProgress:onProgress onComplete:onComplete];
}
}

Expand Down
Loading