Clean cached asar archives when quitting.

This commit is contained in:
Cheng Zhao 2014-09-25 21:49:01 +08:00
parent d559275711
commit 05317ad81e
2 changed files with 17 additions and 4 deletions

View file

@ -75,6 +75,11 @@ class Archive : public mate::Wrappable {
return mate::ConvertToV8(isolate, new_path); return mate::ConvertToV8(isolate, new_path);
} }
// Free the resources used by archive.
void Destroy() {
archive_.reset();
}
// mate::Wrappable: // mate::Wrappable:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) { mate::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate) return mate::ObjectTemplateBuilder(isolate)
@ -82,7 +87,8 @@ class Archive : public mate::Wrappable {
.SetMethod("getFileInfo", &Archive::GetFileInfo) .SetMethod("getFileInfo", &Archive::GetFileInfo)
.SetMethod("stat", &Archive::Stat) .SetMethod("stat", &Archive::Stat)
.SetMethod("readdir", &Archive::Readdir) .SetMethod("readdir", &Archive::Readdir)
.SetMethod("copyFileOut", &Archive::CopyFileOut); .SetMethod("copyFileOut", &Archive::CopyFileOut)
.SetMethod("destroy", &Archive::Destroy);
} }
private: private:

View file

@ -6,12 +6,19 @@ util = require 'util'
# Cache asar archive objects. # Cache asar archive objects.
cachedArchives = {} cachedArchives = {}
getOrCreateArchive = (p) -> getOrCreateArchive = (p) ->
unless cachedArchives[p]? archive = cachedArchives[p]
cachedArchives[p] = asar.createArchive p return archive if archive?
cachedArchives[p] archive = asar.createArchive p
return false unless archive
cachedArchives[p] = archive
# Clean cache on quit.
process.on 'exit', ->
archive.destroy() for p, archive of cachedArchives
# Separate asar package's path from full path. # Separate asar package's path from full path.
splitPath = (p) -> splitPath = (p) ->
return [false, p] unless typeof p is 'string'
components = p.split path.sep components = p.split path.sep
for c, i in components by -1 for c, i in components by -1
if path.extname(c) is '.asar' if path.extname(c) is '.asar'