Cache asar archives on JavaScript side.
This commit is contained in:
parent
4006b6407c
commit
dbbfef38b1
3 changed files with 25 additions and 20 deletions
|
@ -20,16 +20,15 @@ class Archive : public mate::Wrappable {
|
||||||
public:
|
public:
|
||||||
static v8::Handle<v8::Value> Create(v8::Isolate* isolate,
|
static v8::Handle<v8::Value> Create(v8::Isolate* isolate,
|
||||||
const base::FilePath& path) {
|
const base::FilePath& path) {
|
||||||
static asar::ArchiveFactory archive_factory;
|
scoped_ptr<asar::Archive> archive(new asar::Archive(path));
|
||||||
asar::Archive* archive = archive_factory.GetOrCreate(path);
|
if (!archive->Init())
|
||||||
if (!archive)
|
|
||||||
return v8::False(isolate);
|
return v8::False(isolate);
|
||||||
return (new Archive(archive))->GetWrapper(isolate);
|
return (new Archive(archive.Pass()))->GetWrapper(isolate);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit Archive(asar::Archive* archive) : archive_(archive) {}
|
explicit Archive(scoped_ptr<asar::Archive> archive)
|
||||||
virtual ~Archive() {}
|
: archive_(archive.Pass()) {}
|
||||||
|
|
||||||
// Reads the offset and size of file.
|
// Reads the offset and size of file.
|
||||||
v8::Handle<v8::Value> GetFileInfo(v8::Isolate* isolate,
|
v8::Handle<v8::Value> GetFileInfo(v8::Isolate* isolate,
|
||||||
|
@ -87,7 +86,7 @@ class Archive : public mate::Wrappable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
asar::Archive* archive_;
|
scoped_ptr<asar::Archive> archive_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Archive);
|
DISALLOW_COPY_AND_ASSIGN(Archive);
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,8 +10,7 @@ namespace asar {
|
||||||
|
|
||||||
ArchiveFactory::ArchiveFactory() {}
|
ArchiveFactory::ArchiveFactory() {}
|
||||||
|
|
||||||
ArchiveFactory::~ArchiveFactory() {
|
ArchiveFactory::~ArchiveFactory() {}
|
||||||
}
|
|
||||||
|
|
||||||
Archive* ArchiveFactory::GetOrCreate(const base::FilePath& path) {
|
Archive* ArchiveFactory::GetOrCreate(const base::FilePath& path) {
|
||||||
if (!archives_.contains(path)) {
|
if (!archives_.contains(path)) {
|
||||||
|
|
|
@ -3,6 +3,13 @@ fs = require 'fs'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
util = require 'util'
|
util = require 'util'
|
||||||
|
|
||||||
|
# Cache asar archive objects.
|
||||||
|
cachedArchives = {}
|
||||||
|
getOrCreateArchive = (p) ->
|
||||||
|
unless cachedArchives[p]?
|
||||||
|
cachedArchives[p] = asar.createArchive p
|
||||||
|
cachedArchives[p]
|
||||||
|
|
||||||
# Separate asar package's path from full path.
|
# Separate asar package's path from full path.
|
||||||
splitPath = (p) ->
|
splitPath = (p) ->
|
||||||
components = p.split path.sep
|
components = p.split path.sep
|
||||||
|
@ -49,7 +56,7 @@ fs.lstatSync = (p) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return lstatSync p unless isAsar
|
return lstatSync p unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
throw new Error("Invalid package #{asarPath}") unless archive
|
throw new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
stats = archive.stat filePath
|
stats = archive.stat filePath
|
||||||
|
@ -62,10 +69,10 @@ fs.lstat = (p, callback) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return lstat p, callback unless isAsar
|
return lstat p, callback unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
stats = asar.createArchive(asarPath).stat filePath
|
stats = getOrCreateArchive(asarPath).stat filePath
|
||||||
return callback createNotFoundError(asarPath, filePath) unless stats
|
return callback createNotFoundError(asarPath, filePath) unless stats
|
||||||
|
|
||||||
callback undefined, asarStatsToFsStats stats
|
callback undefined, asarStatsToFsStats stats
|
||||||
|
@ -91,9 +98,9 @@ fs.statSyncNoException = (p) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return statSyncNoException p unless isAsar
|
return statSyncNoException p unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return false unless archive
|
return false unless archive
|
||||||
stats = asar.createArchive(asarPath).stat filePath
|
stats = archive.stat filePath
|
||||||
return false unless stats
|
return false unless stats
|
||||||
asarStatsToFsStats stats
|
asarStatsToFsStats stats
|
||||||
|
|
||||||
|
@ -102,7 +109,7 @@ fs.exists = (p, callback) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return exists p, callback unless isAsar
|
return exists p, callback unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
callback archive.stat(filePath) isnt false
|
callback archive.stat(filePath) isnt false
|
||||||
|
@ -112,7 +119,7 @@ fs.existsSync = (p) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return existsSync p unless isAsar
|
return existsSync p unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return false unless archive
|
return false unless archive
|
||||||
|
|
||||||
archive.stat(filePath) isnt false
|
archive.stat(filePath) isnt false
|
||||||
|
@ -122,7 +129,7 @@ fs.readFile = (p, options, callback) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return readFile.apply this, arguments unless isAsar
|
return readFile.apply this, arguments unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
info = archive.getFileInfo filePath
|
info = archive.getFileInfo filePath
|
||||||
|
@ -154,7 +161,7 @@ fs.readFileSync = (p, options) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return readFileSync.apply this, arguments unless isAsar
|
return readFileSync.apply this, arguments unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
throw new Error("Invalid package #{asarPath}") unless archive
|
throw new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
info = archive.getFileInfo filePath
|
info = archive.getFileInfo filePath
|
||||||
|
@ -185,7 +192,7 @@ fs.readdir = (p, callback) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return readdir.apply this, arguments unless isAsar
|
return readdir.apply this, arguments unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
files = archive.readdir filePath
|
files = archive.readdir filePath
|
||||||
|
@ -199,7 +206,7 @@ fs.readdirSync = (p) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
return readdirSync.apply this, arguments unless isAsar
|
return readdirSync.apply this, arguments unless isAsar
|
||||||
|
|
||||||
archive = asar.createArchive asarPath
|
archive = getOrCreateArchive asarPath
|
||||||
throw new Error("Invalid package #{asarPath}") unless archive
|
throw new Error("Invalid package #{asarPath}") unless archive
|
||||||
|
|
||||||
files = archive.readdir filePath
|
files = archive.readdir filePath
|
||||||
|
|
Loading…
Reference in a new issue