Skip to content

Commit

Permalink
perf(ios): mark encrypted vs normal js/json files in index to be expl…
Browse files Browse the repository at this point in the history
…icit how to try loading
  • Loading branch information
sgtcoolguy committed Jan 22, 2019
1 parent 81a63a6 commit 35145ee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
65 changes: 34 additions & 31 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/KrollBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
//Defined private method inside TiBindingRunLoop.m (Perhaps to move to .c?)
void TiBindingRunLoopAnnounceStart(TiBindingRunLoop runLoop);

typedef NS_ENUM(NSInteger, FileStatus) {
DoesntExist,
ExistsOnDisk,
ExistsEncrypted
};

typedef NS_ENUM(NSInteger, ModuleType) {
Native,
JS,
Expand Down Expand Up @@ -832,6 +838,7 @@ - (id)krollObjectForProxy:(id)proxy

- (KrollWrapper *)loadCommonJSModule:(NSString *)code withSourceURL:(NSURL *)sourceURL
{
// FIXME: Can we skip all of this now? Doesn't we already properly resolve paths?
// This takes care of resolving paths like `../../foo.js`
sourceURL = [NSURL fileURLWithPath:[[sourceURL path] stringByStandardizingPath]];

Expand Down Expand Up @@ -974,16 +981,22 @@ - (KrollWrapper *)loadCoreModuleAsset:(NSString *)path withContext:(KrollContext

- (NSString *)loadFile:(NSString *)path
{
// do a quick check to see if the file exists - rather than trying to load encrypted asset and falling back to from filesystem!
BOOL exists = [self fileExists:path]; // Can we distinguish which code path to use based on value? (i.e. 1 = filesystem, 2 = encrytped)
if (!exists) {
return nil;
}

// check if file exists by using cheat index.json which tells us if on disk or encrypted.
FileStatus status = [self fileExists:path];
NSURL *url_ = [NSURL URLWithString:path relativeToURL:[[self host] baseURL]];
NSData *data = [TiUtils loadAppResource:url_]; // try to load encrypted file
if (data == nil) {
data = [NSData dataWithContentsOfURL:url_];
NSData* data;
switch (status) {
case ExistsOnDisk:
data = [NSData dataWithContentsOfURL:url_]; // load from disk
break;

case ExistsEncrypted:
data = [TiUtils loadAppResource:url_]; // try to load encrypted file
break;

case DoesntExist:
default:
return nil;
}

if (data != nil) {
Expand Down Expand Up @@ -1098,7 +1111,6 @@ - (NSString *)packageJSONMain:(NSString *)filepath
NSString *mainString = (NSString *)main;
// b. let M = X + (json main field)
m = [[filepath stringByAppendingPathComponent:mainString] stringByStandardizingPath];
// FIXME: Need to tryFileOrDirectoyr on result!
if ([self fileExists:m]) {
packageJSONMainCache[filepath] = m; // cache from package.json to main value
return m;
Expand Down Expand Up @@ -1137,28 +1149,13 @@ - (KrollWrapper *)loadAsDirectory:(NSString *)path withContext:(KrollContext *)k

- (KrollWrapper *)loadAsFileOrDirectory:(NSString *)path withContext:(KrollContext *)kroll
{
// FIXME Can we improve perf a little here by detecting if the target is a file or directory first?
// i.e.
// - if node_modules/whatever exists and is a dir, we can skip checking for node_modules/whatever.js at least
// - if it doesn't exist at all, we can skip checking:
// - node_modules/whatever
// - node_modules/whatever/index.js
// - node_modules/whatever/package.json
// - node_modules/whatever/index.json
// - node_modules/whatever/whatever.js

// a. LOAD_AS_FILE(Y + X)
KrollWrapper *module = [self loadAsFile:path withContext:context];
if (module) {
return module;
}
// b. LOAD_AS_DIRECTORY(Y + X)
module = [self loadAsDirectory:path withContext:context];
if (module) {
return module;
}

return nil;
return [self loadAsDirectory:path withContext:context];
}

- (NSArray *)nodeModulesPaths:(NSString *)path
Expand Down Expand Up @@ -1249,13 +1246,19 @@ + (NSDictionary *)loadIndexJSON
return props;
}

// TODO: Give more detail: DoesntExist, ExistsEncrypted, ExistsOnDisk
// That way we can skip trying encrypted first when loading files we know are not encrypted!
- (BOOL)fileExists:(NSString *)path
- (FileStatus)fileStatus:(NSString *)path
{
// Look up existence of a given file based on index.json!
NSDictionary *files = [KrollBridge loadIndexJSON];
return files[[@"Resources/" stringByAppendingString:path]] != nil;
id type = files[[@"Resources/" stringByAppendingString:path]];
if (type == nil) {
return DoesntExist;
}
return type;
}

- (BOOL)fileExists:(NSString *)path
{
return [self fileStatus:path] != DoesntExist;
}

- (ResolvedModule *)tryFile:(NSString *)path
Expand Down
4 changes: 2 additions & 2 deletions iphone/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6105,14 +6105,14 @@ iOSBuilder.prototype.generateRequireIndex = function generateRequireIndex(callba
if (fs.statSync(file).isDirectory()) {
walk(file);
} else if (/\.js(on)?$/.test(filename)) {
index[file.replace(/\\/g, '/').replace(binAssetsDir + '/', 'Resources/')] = 1;
index[file.replace(/\\/g, '/').replace(binAssetsDir + '/', 'Resources/')] = 1; // 1 for exists on disk
}
}
});
}(this.xcodeAppDir));

this.jsFilesToEncrypt.forEach(function (file) {
index['Resources/' + file.replace(/\\/g, '/')] = 1;
index['Resources/' + file.replace(/\\/g, '/')] = 2; // 2 for encrypted
});

delete index['Resources/_app_props_.json'];
Expand Down

0 comments on commit 35145ee

Please sign in to comment.