Merge pull request #1096 from atom/generic-asar-support

Support asar archives in scripts forked with child_process.fork
This commit is contained in:
Cheng Zhao 2015-02-04 12:18:13 -08:00
commit f1bca5dd0f
10 changed files with 140 additions and 27 deletions

View file

@ -41,7 +41,6 @@
'atom/common/api/lib/original-fs.coffee',
'atom/common/api/lib/shell.coffee',
'atom/common/lib/init.coffee',
'atom/common/lib/asar.coffee',
'atom/renderer/lib/chrome-api.coffee',
'atom/renderer/lib/init.coffee',
'atom/renderer/lib/inspector.coffee',
@ -55,6 +54,10 @@
'atom/renderer/api/lib/screen.coffee',
'atom/renderer/api/lib/web-frame.coffee',
],
'coffee2c_sources': [
'atom/common/lib/asar.coffee',
'atom/common/lib/asar_init.coffee',
],
'lib_sources': [
'atom/app/atom_content_client.cc',
'atom/app/atom_content_client.h',
@ -333,6 +336,7 @@
'chromium_src/library_loaders/libspeechd_loader.cc',
'chromium_src/library_loaders/libspeechd.h',
'<@(native_mate_files)',
'<(SHARED_INTERMEDIATE_DIR)/atom_natives.h',
],
'lib_sources_win': [
'chromium_src/chrome/browser/ui/views/color_chooser_dialog.cc',
@ -386,7 +390,7 @@
'target_name': '<(project_name)',
'type': 'executable',
'dependencies': [
'generated_sources',
'compile_coffee',
'<(project_name)_lib',
],
'sources': [
@ -524,6 +528,7 @@
'target_name': '<(project_name)_lib',
'type': 'static_library',
'dependencies': [
'atom_coffee2c',
'vendor/brightray/brightray.gyp:brightray',
'vendor/node/node.gyp:node_lib',
],
@ -542,6 +547,8 @@
'chromium_src',
'vendor/brightray',
'vendor/native_mate',
# Include atom_natives.h.
'<(SHARED_INTERMEDIATE_DIR)',
# Include directories for uv and node.
'vendor/node/src',
'vendor/node/deps/http_parser',
@ -612,7 +619,7 @@
],
}, # target <(product_name)_lib
{
'target_name': 'generated_sources',
'target_name': 'compile_coffee',
'type': 'none',
'sources': [
'<@(coffee_sources)',
@ -622,7 +629,7 @@
'rule_name': 'coffee',
'extension': 'coffee',
'inputs': [
'script/compile-coffee.py',
'tools/compile-coffee.py',
],
'conditions': [
['OS=="mac"', {
@ -631,7 +638,7 @@
],
'action': [
'python',
'script/compile-coffee.py',
'tools/compile-coffee.py',
'<(RULE_INPUT_PATH)',
'<(PRODUCT_DIR)/<(product_name).app/Contents/Resources/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT).js',
],
@ -641,7 +648,7 @@
],
'action': [
'python',
'script/compile-coffee.py',
'tools/compile-coffee.py',
'<(RULE_INPUT_PATH)',
'<(PRODUCT_DIR)/resources/<(RULE_INPUT_DIRNAME)/<(RULE_INPUT_ROOT).js',
],
@ -649,7 +656,28 @@
],
},
],
}, # target generated_sources
}, # target compile_coffee
{
'target_name': 'atom_coffee2c',
'type': 'none',
'actions': [
{
'action_name': 'atom_coffee2c',
'inputs': [
'<@(coffee2c_sources)',
],
'outputs': [
'<(SHARED_INTERMEDIATE_DIR)/atom_natives.h',
],
'action': [
'python',
'tools/coffee2c.py',
'<@(_outputs)',
'<@(_inputs)',
],
}
],
}, # target atom_coffee2c
{
'target_name': '<(project_name)_dump_symbols',
'type': 'none',

View file

@ -2,11 +2,15 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <vector>
#include "atom_natives.h" // NOLINT: This file is generated with coffee2c.
#include "atom/common/asar/archive.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "native_mate/arguments.h"
#include "native_mate/callback.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/wrappable.h"
@ -106,10 +110,33 @@ class Archive : public mate::Wrappable {
DISALLOW_COPY_AND_ASSIGN(Archive);
};
void InitAsarSupport(v8::Isolate* isolate,
v8::Handle<v8::Value> process,
v8::Handle<v8::Value> require) {
// Evaluate asar_init.coffee.
v8::Local<v8::Script> asar_init = v8::Script::Compile(v8::String::NewFromUtf8(
isolate,
node::asar_init_native,
v8::String::kNormalString,
sizeof(node::asar_init_native) -1));
v8::Local<v8::Value> result = asar_init->Run();
// Initialize asar support.
base::Callback<void(v8::Handle<v8::Value>,
v8::Handle<v8::Value>,
std::string)> init;
if (mate::ConvertFromV8(isolate, result, &init)) {
init.Run(process,
require,
std::string(node::asar_native, sizeof(node::asar_native) - 1));
}
}
void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
v8::Handle<v8::Context> context, void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("createArchive", &Archive::Create);
dict.SetMethod("initAsarSupport", &InitAsarSupport);
}
} // namespace

View file

@ -1,4 +1,4 @@
asar = process.atomBinding 'asar'
asar = process.binding 'atom_common_asar'
child_process = require 'child_process'
path = require 'path'
util = require 'util'

View file

@ -0,0 +1,22 @@
return (process, require, asarSource) ->
{createArchive} = process.binding 'atom_common_asar'
# Make asar.coffee accessible via "require".
process.binding('natives').ATOM_SHELL_ASAR = asarSource
# Monkey-patch the fs module.
require('ATOM_SHELL_ASAR').wrapFsWithAsar require('fs')
# Make graceful-fs work with asar.
source = process.binding 'natives'
source.originalFs = source.fs
source.fs = """
var src = '(function (exports, require, module, __filename, __dirname) { ' +
process.binding('natives').originalFs +
' });';
var vm = require('vm');
var fn = vm.runInThisContext(src, { filename: 'fs.js' });
fn(exports, require, module);
var asar = require('ATOM_SHELL_ASAR');
asar.wrapFsWithAsar(exports);
"""

View file

@ -35,21 +35,3 @@ global.clearImmediate = timers.clearImmediate
if process.type is 'browser'
global.setTimeout = wrapWithActivateUvLoop timers.setTimeout
global.setInterval = wrapWithActivateUvLoop timers.setInterval
# Add support for asar packages.
asar = require './asar'
asar.wrapFsWithAsar fs
# Make graceful-fs work with asar.
source = process.binding 'natives'
source.originalFs = source.fs
source.fs = """
var src = '(function (exports, require, module, __filename, __dirname) { ' +
process.binding('natives').originalFs +
' });';
var vm = require('vm');
var fn = vm.runInThisContext(src, { filename: 'fs.js' });
fn(exports, require, module);
var asar = require(#{JSON.stringify(__dirname)} + '/asar');
asar.wrapFsWithAsar(exports);
"""

View file

@ -347,6 +347,14 @@ describe 'asar package', ->
throws = -> child_process.fork p
assert.throws throws, /ENOENT/
it 'supports asar in the forked js', (done) ->
file = path.join fixtures, 'asar', 'a.asar', 'file1'
child = child_process.fork path.join(fixtures, 'module', 'asar.js')
child.on 'message', (content) ->
assert.equal content, fs.readFileSync(file).toString()
done()
child.send file
describe 'asar protocol', ->
url = require 'url'
remote = require 'remote'

4
spec/fixtures/module/asar.js vendored Normal file
View file

@ -0,0 +1,4 @@
var fs = require('fs');
process.on('message', function(file) {
process.send(fs.readFileSync(file).toString());
});

42
tools/coffee2c.py Executable file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python
import os
import subprocess
import sys
SOURCE_ROOT = os.path.dirname(os.path.dirname(__file__))
def main():
natives = sys.argv[1]
coffee_source_files = sys.argv[2:]
output_dir = os.path.dirname(natives)
js_source_files = compile_coffee(coffee_source_files, output_dir)
call_js2c(natives, js_source_files)
def compile_coffee(coffee_source_files, output_dir):
js_source_files = []
for source_file in coffee_source_files:
output_filename = os.path.splitext(source_file)[0] + '.js'
output_path = os.path.join(output_dir, output_filename)
js_source_files.append(output_path)
call_compile_coffee(source_file, output_path)
return js_source_files
def call_compile_coffee(source_file, output_filename):
compile_coffee = os.path.join(SOURCE_ROOT, 'tools', 'compile-coffee.py')
subprocess.check_call([sys.executable, compile_coffee, source_file,
output_filename])
def call_js2c(natives, js_source_files):
js2c = os.path.join(SOURCE_ROOT, 'vendor', 'node', 'tools', 'js2c.py')
subprocess.check_call([sys.executable, js2c, natives] + js_source_files)
if __name__ == '__main__':
sys.exit(main())

2
vendor/node vendored

@ -1 +1 @@
Subproject commit 70498428ced2b8ae4ce020051f06d104b5c6c4de
Subproject commit 9cd0fad904c3ebf0af0aeec2097cad3e039a7c6d