2015-04-19 19:55:46 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-04-02 14:33:21 +00:00
|
|
|
|
|
|
|
#import "RCTJavaScriptLoader.h"
|
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
2015-04-25 21:52:18 +00:00
|
|
|
#import "RCTConvert.h"
|
2016-11-11 13:21:37 +00:00
|
|
|
#import "RCTJSCWrapper.h"
|
2015-04-02 14:33:21 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-10-21 17:53:35 +00:00
|
|
|
#import "RCTPerformanceLogger.h"
|
2016-10-11 19:14:37 +00:00
|
|
|
#import "RCTMultipartDataTask.h"
|
2015-04-02 14:33:21 +00:00
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2016-11-01 17:14:00 +00:00
|
|
|
static uint32_t const RCTRAMBundleMagicNumber = 0xFB0BD1E5;
|
|
|
|
static uint64_t const RCTBCBundleMagicNumber = 0xFF4865726D657300;
|
2016-03-17 17:34:46 +00:00
|
|
|
|
2016-07-12 12:13:32 +00:00
|
|
|
NSString *const RCTJavaScriptLoaderErrorDomain = @"RCTJavaScriptLoaderErrorDomain";
|
|
|
|
|
2016-11-11 13:21:37 +00:00
|
|
|
RCTScriptTag RCTParseTypeFromHeader(RCTBundleHeader header)
|
2016-11-01 17:14:00 +00:00
|
|
|
{
|
2016-11-11 13:21:37 +00:00
|
|
|
if (NSSwapLittleIntToHost(header.RAMMagic) == RCTRAMBundleMagicNumber) {
|
2016-11-01 17:14:00 +00:00
|
|
|
return RCTScriptRAMBundle;
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:21:37 +00:00
|
|
|
if (NSSwapLittleLongLongToHost(header.BCMagic) == RCTBCBundleMagicNumber) {
|
2016-11-01 17:14:00 +00:00
|
|
|
return RCTScriptBCBundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RCTScriptString;
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:53:31 +00:00
|
|
|
NSString *RCTStringForScriptTag(RCTScriptTag tag)
|
|
|
|
{
|
|
|
|
switch (tag) {
|
|
|
|
case RCTScriptString:
|
|
|
|
return @"String";
|
|
|
|
case RCTScriptRAMBundle:
|
|
|
|
return @"RAM Bundle";
|
|
|
|
case RCTScriptBCBundle:
|
|
|
|
return @"BC Bundle";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:42:30 +00:00
|
|
|
@implementation RCTLoadingProgress
|
|
|
|
|
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
NSMutableString *desc = [NSMutableString new];
|
|
|
|
[desc appendString:_status ?: @"Loading"];
|
|
|
|
|
|
|
|
if (_total > 0) {
|
|
|
|
[desc appendFormat:@" %ld%% (%@/%@)", (long)(100 * [_done integerValue] / [_total integerValue]), _done, _total];
|
|
|
|
}
|
|
|
|
[desc appendString:@"…"];
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
@implementation RCTJavaScriptLoader
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2016-10-13 18:42:30 +00:00
|
|
|
+ (void)loadBundleAtURL:(NSURL *)scriptURL onProgress:(RCTSourceLoadProgressBlock)onProgress onComplete:(RCTSourceLoadBlock)onComplete
|
2016-07-12 12:13:32 +00:00
|
|
|
{
|
|
|
|
int64_t sourceLength;
|
|
|
|
NSError *error;
|
|
|
|
NSData *data = [self attemptSynchronousLoadOfBundleAtURL:scriptURL
|
2016-11-11 13:21:37 +00:00
|
|
|
runtimeBCVersion:JSNoBytecodeFileFormatVersion
|
2016-07-12 12:13:32 +00:00
|
|
|
sourceLength:&sourceLength
|
|
|
|
error:&error];
|
|
|
|
if (data) {
|
|
|
|
onComplete(nil, data, sourceLength);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BOOL isCannotLoadSyncError =
|
|
|
|
[error.domain isEqualToString:RCTJavaScriptLoaderErrorDomain]
|
|
|
|
&& error.code == RCTJavaScriptLoaderErrorCannotBeLoadedSynchronously;
|
|
|
|
|
|
|
|
if (isCannotLoadSyncError) {
|
2016-10-13 18:42:30 +00:00
|
|
|
attemptAsynchronousLoadOfBundleAtURL(scriptURL, onProgress, onComplete);
|
2016-07-12 12:13:32 +00:00
|
|
|
} else {
|
|
|
|
onComplete(error, nil, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSData *)attemptSynchronousLoadOfBundleAtURL:(NSURL *)scriptURL
|
2016-11-11 13:21:37 +00:00
|
|
|
runtimeBCVersion:(int32_t)runtimeBCVersion
|
2016-07-12 12:13:32 +00:00
|
|
|
sourceLength:(int64_t *)sourceLength
|
|
|
|
error:(NSError **)error
|
2015-04-02 14:33:21 +00:00
|
|
|
{
|
2016-05-23 18:03:17 +00:00
|
|
|
NSString *unsanitizedScriptURLString = scriptURL.absoluteString;
|
2015-04-25 21:52:18 +00:00
|
|
|
// Sanitize the script URL
|
2016-07-12 12:13:32 +00:00
|
|
|
scriptURL = sanitizeURL(scriptURL);
|
2015-04-25 21:52:18 +00:00
|
|
|
|
2015-10-08 19:28:38 +00:00
|
|
|
if (!scriptURL) {
|
2016-07-12 12:13:32 +00:00
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
|
|
|
|
code:RCTJavaScriptLoaderErrorNoScriptURL
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey:
|
2016-08-02 15:05:33 +00:00
|
|
|
[NSString stringWithFormat:@"No script URL provided. Make sure the packager is "
|
|
|
|
@"running or you have embedded a JS bundle in your application bundle."
|
2016-07-12 12:13:32 +00:00
|
|
|
@"unsanitizedScriptURLString:(%@)", unsanitizedScriptURLString]}];
|
|
|
|
}
|
|
|
|
return nil;
|
2015-04-11 22:08:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 19:28:38 +00:00
|
|
|
// Load local script file
|
2016-07-12 12:13:32 +00:00
|
|
|
if (!scriptURL.fileURL) {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
|
|
|
|
code:RCTJavaScriptLoaderErrorCannotBeLoadedSynchronously
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey:
|
2016-07-13 23:39:12 +00:00
|
|
|
[NSString stringWithFormat:@"Cannot load %@ URLs synchronously",
|
|
|
|
scriptURL.scheme]}];
|
2016-07-07 20:31:21 +00:00
|
|
|
}
|
2016-07-12 12:13:32 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2016-07-07 20:31:21 +00:00
|
|
|
|
2016-07-12 12:13:32 +00:00
|
|
|
// Load the first 4 bytes to check if the bundle is regular or RAM ("Random Access Modules" bundle).
|
|
|
|
// The RAM bundle has a magic number in the 4 first bytes `(0xFB0BD1E5)`.
|
|
|
|
// The benefit of RAM bundle over a regular bundle is that we can lazily inject
|
|
|
|
// modules into JSC as they're required.
|
|
|
|
FILE *bundle = fopen(scriptURL.path.UTF8String, "r");
|
|
|
|
if (!bundle) {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
|
|
|
|
code:RCTJavaScriptLoaderErrorFailedOpeningFile
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey:
|
|
|
|
[NSString stringWithFormat:@"Error opening bundle %@", scriptURL.path]}];
|
2016-07-07 20:31:21 +00:00
|
|
|
}
|
2016-07-12 12:13:32 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2016-07-07 20:31:21 +00:00
|
|
|
|
2016-11-11 13:21:37 +00:00
|
|
|
RCTBundleHeader header = {};
|
|
|
|
size_t readResult = fread(&header, sizeof(header), 1, bundle);
|
2016-07-12 12:13:32 +00:00
|
|
|
fclose(bundle);
|
|
|
|
if (readResult != 1) {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
|
|
|
|
code:RCTJavaScriptLoaderErrorFailedReadingFile
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey:
|
|
|
|
[NSString stringWithFormat:@"Error reading bundle %@", scriptURL.path]}];
|
2016-07-07 20:31:21 +00:00
|
|
|
}
|
2016-07-12 12:13:32 +00:00
|
|
|
return nil;
|
2015-10-08 19:28:38 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 13:21:37 +00:00
|
|
|
RCTScriptTag tag = RCTParseTypeFromHeader(header);
|
|
|
|
switch (tag) {
|
|
|
|
case RCTScriptRAMBundle:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RCTScriptString:
|
2016-07-12 12:13:32 +00:00
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
|
|
|
|
code:RCTJavaScriptLoaderErrorCannotBeLoadedSynchronously
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey:
|
2016-11-01 17:14:00 +00:00
|
|
|
@"Cannot load text/javascript files synchronously"}];
|
2015-04-25 21:52:18 +00:00
|
|
|
}
|
2016-07-12 12:13:32 +00:00
|
|
|
return nil;
|
2016-11-11 13:21:37 +00:00
|
|
|
|
|
|
|
case RCTScriptBCBundle:
|
|
|
|
if (header.BCVersion != runtimeBCVersion) {
|
|
|
|
if (error) {
|
|
|
|
NSString *errDesc =
|
|
|
|
[NSString stringWithFormat:@"BC Version Mismatch. Expect: %d, Actual: %d",
|
|
|
|
runtimeBCVersion, header.BCVersion];
|
|
|
|
|
|
|
|
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
|
|
|
|
code:RCTJavaScriptLoaderErrorBCVersion
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey: errDesc}];
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-12 12:13:32 +00:00
|
|
|
}
|
2015-04-25 21:52:18 +00:00
|
|
|
|
2016-07-12 12:13:32 +00:00
|
|
|
struct stat statInfo;
|
|
|
|
if (stat(scriptURL.path.UTF8String, &statInfo) != 0) {
|
|
|
|
if (error) {
|
|
|
|
*error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
|
|
|
|
code:RCTJavaScriptLoaderErrorFailedStatingFile
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey:
|
|
|
|
[NSString stringWithFormat:@"Error stating bundle %@", scriptURL.path]}];
|
2015-04-25 21:52:18 +00:00
|
|
|
}
|
2016-07-12 12:13:32 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if (sourceLength) {
|
|
|
|
*sourceLength = statInfo.st_size;
|
|
|
|
}
|
2016-11-11 13:21:37 +00:00
|
|
|
return [NSData dataWithBytes:&header length:sizeof(header)];
|
2016-07-12 12:13:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 18:42:30 +00:00
|
|
|
static void attemptAsynchronousLoadOfBundleAtURL(NSURL *scriptURL, RCTSourceLoadProgressBlock onProgress, RCTSourceLoadBlock onComplete)
|
2016-07-12 12:13:32 +00:00
|
|
|
{
|
|
|
|
scriptURL = sanitizeURL(scriptURL);
|
|
|
|
|
|
|
|
if (scriptURL.fileURL) {
|
|
|
|
// Reading in a large bundle can be slow. Dispatch to the background queue to do it.
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
NSError *error = nil;
|
|
|
|
NSData *source = [NSData dataWithContentsOfFile:scriptURL.path
|
|
|
|
options:NSDataReadingMappedIfSafe
|
|
|
|
error:&error];
|
|
|
|
onComplete(error, source, source.length);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2015-04-02 14:33:21 +00:00
|
|
|
|
2016-10-11 19:14:37 +00:00
|
|
|
|
|
|
|
RCTMultipartDataTask *task = [[RCTMultipartDataTask alloc] initWithURL:scriptURL partHandler:^(NSInteger statusCode, NSDictionary *headers, NSData *data, NSError *error, BOOL done) {
|
|
|
|
if (!done) {
|
2016-10-13 18:42:30 +00:00
|
|
|
if (onProgress) {
|
|
|
|
onProgress(progressEventFromData(data));
|
|
|
|
}
|
2016-10-11 19:14:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle general request errors
|
|
|
|
if (error) {
|
|
|
|
if ([error.domain isEqualToString:NSURLErrorDomain]) {
|
|
|
|
error = [NSError errorWithDomain:RCTJavaScriptLoaderErrorDomain
|
|
|
|
code:RCTJavaScriptLoaderErrorURLLoadFailed
|
|
|
|
userInfo:
|
|
|
|
@{
|
|
|
|
NSLocalizedDescriptionKey:
|
|
|
|
[@"Could not connect to development server.\n\n"
|
|
|
|
"Ensure the following:\n"
|
|
|
|
"- Node server is running and available on the same network - run 'npm start' from react-native root\n"
|
|
|
|
"- Node server URL is correctly set in AppDelegate\n\n"
|
|
|
|
"URL: " stringByAppendingString:scriptURL.absoluteString],
|
|
|
|
NSLocalizedFailureReasonErrorKey: error.localizedDescription,
|
|
|
|
NSUnderlyingErrorKey: error,
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
onComplete(error, nil, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For multipart responses packager sets X-Http-Status header in case HTTP status code
|
|
|
|
// is different from 200 OK
|
|
|
|
NSString *statusCodeHeader = [headers valueForKey:@"X-Http-Status"];
|
|
|
|
if (statusCodeHeader) {
|
|
|
|
statusCode = [statusCodeHeader integerValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (statusCode != 200) {
|
|
|
|
error = [NSError errorWithDomain:@"JSServer"
|
|
|
|
code:statusCode
|
|
|
|
userInfo:userInfoForRawResponse([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding])];
|
|
|
|
onComplete(error, nil, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onComplete(nil, data, data.length);
|
|
|
|
}];
|
|
|
|
|
|
|
|
[task startTask];
|
2015-04-02 14:33:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 12:13:32 +00:00
|
|
|
static NSURL *sanitizeURL(NSURL *url)
|
|
|
|
{
|
|
|
|
// Why we do this is lost to time. We probably shouldn't; passing a valid URL is the caller's responsibility not ours.
|
|
|
|
return [RCTConvert NSURL:url.absoluteString];
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:42:30 +00:00
|
|
|
static RCTLoadingProgress *progressEventFromData(NSData *rawData)
|
|
|
|
{
|
|
|
|
NSString *text = [[NSString alloc] initWithData:rawData encoding:NSUTF8StringEncoding];
|
|
|
|
id info = RCTJSONParse(text, nil);
|
|
|
|
if (!info || ![info isKindOfClass:[NSDictionary class]]) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTLoadingProgress *progress = [RCTLoadingProgress new];
|
|
|
|
progress.status = [info valueForKey:@"status"];
|
|
|
|
progress.done = [info valueForKey:@"done"];
|
|
|
|
progress.total = [info valueForKey:@"total"];
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
2016-07-12 12:13:32 +00:00
|
|
|
static NSDictionary *userInfoForRawResponse(NSString *rawText)
|
|
|
|
{
|
|
|
|
NSDictionary *parsedResponse = RCTJSONParse(rawText, nil);
|
|
|
|
if (![parsedResponse isKindOfClass:[NSDictionary class]]) {
|
|
|
|
return @{NSLocalizedDescriptionKey: rawText};
|
|
|
|
}
|
|
|
|
NSArray *errors = parsedResponse[@"errors"];
|
|
|
|
if (![errors isKindOfClass:[NSArray class]]) {
|
|
|
|
return @{NSLocalizedDescriptionKey: rawText};
|
|
|
|
}
|
|
|
|
NSMutableArray<NSDictionary *> *fakeStack = [NSMutableArray new];
|
|
|
|
for (NSDictionary *err in errors) {
|
|
|
|
[fakeStack addObject:
|
|
|
|
@{
|
|
|
|
@"methodName": err[@"description"] ?: @"",
|
|
|
|
@"file": err[@"filename"] ?: @"",
|
|
|
|
@"lineNumber": err[@"lineNumber"] ?: @0
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
return @{NSLocalizedDescriptionKey: parsedResponse[@"message"] ?: @"No message provided", @"stack": [fakeStack copy]};
|
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
@end
|