fix: initialize asar support in worker threads (#33216)
* fix: initialize asar support in worker threads Use `ObjectWrap` instead of gin's Wrap in `electron_api_asar.cc` because gin isn't fully initialized (and apparently not possible to initialize without ruining the isolate configuration and array buffer allocator) in worker threads. In the worker thread call `setupAsarSupport` just as we do for the main process. * Update lib/asar/fs-wrapper.ts Co-authored-by: Darshan Sen <raisinten@gmail.com> * Update patches/node/worker_thread_add_asar_support.patch Co-authored-by: Darshan Sen <raisinten@gmail.com> * Add a test Co-authored-by: Darshan Sen <raisinten@gmail.com> Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
This commit is contained in:
parent
27ddf19f3c
commit
06a00b74e8
8 changed files with 209 additions and 70 deletions
|
@ -5,11 +5,8 @@
|
|||
#include <vector>
|
||||
|
||||
#include "gin/handle.h"
|
||||
#include "gin/object_template_builder.h"
|
||||
#include "gin/wrappable.h"
|
||||
#include "shell/common/asar/archive.h"
|
||||
#include "shell/common/asar/asar_util.h"
|
||||
#include "shell/common/gin_converters/callback_converter.h"
|
||||
#include "shell/common/gin_converters/file_path_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
|
@ -17,45 +14,73 @@
|
|||
|
||||
namespace {
|
||||
|
||||
class Archive : public gin::Wrappable<Archive> {
|
||||
class Archive : public node::ObjectWrap {
|
||||
public:
|
||||
static gin::Handle<Archive> Create(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
auto archive = std::make_unique<asar::Archive>(path);
|
||||
if (!archive->Init())
|
||||
return gin::Handle<Archive>();
|
||||
return gin::CreateHandle(isolate, new Archive(isolate, std::move(archive)));
|
||||
}
|
||||
static v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
|
||||
v8::Isolate* isolate) {
|
||||
auto tpl = v8::FunctionTemplate::New(isolate, Archive::New);
|
||||
tpl->SetClassName(
|
||||
v8::String::NewFromUtf8(isolate, "Archive").ToLocalChecked());
|
||||
tpl->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
|
||||
// gin::Wrappable
|
||||
static gin::WrapperInfo kWrapperInfo;
|
||||
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) override {
|
||||
return gin::ObjectTemplateBuilder(isolate)
|
||||
.SetMethod("getFileInfo", &Archive::GetFileInfo)
|
||||
.SetMethod("stat", &Archive::Stat)
|
||||
.SetMethod("readdir", &Archive::Readdir)
|
||||
.SetMethod("realpath", &Archive::Realpath)
|
||||
.SetMethod("copyFileOut", &Archive::CopyFileOut)
|
||||
.SetMethod("getFdAndValidateIntegrityLater", &Archive::GetFD);
|
||||
}
|
||||
NODE_SET_PROTOTYPE_METHOD(tpl, "getFileInfo", &Archive::GetFileInfo);
|
||||
NODE_SET_PROTOTYPE_METHOD(tpl, "stat", &Archive::Stat);
|
||||
NODE_SET_PROTOTYPE_METHOD(tpl, "readdir", &Archive::Readdir);
|
||||
NODE_SET_PROTOTYPE_METHOD(tpl, "realpath", &Archive::Realpath);
|
||||
NODE_SET_PROTOTYPE_METHOD(tpl, "copyFileOut", &Archive::CopyFileOut);
|
||||
NODE_SET_PROTOTYPE_METHOD(tpl, "getFdAndValidateIntegrityLater",
|
||||
&Archive::GetFD);
|
||||
|
||||
const char* GetTypeName() override { return "Archive"; }
|
||||
return tpl;
|
||||
}
|
||||
|
||||
// disable copy
|
||||
Archive(const Archive&) = delete;
|
||||
Archive& operator=(const Archive&) = delete;
|
||||
|
||||
protected:
|
||||
Archive(v8::Isolate* isolate, std::unique_ptr<asar::Archive> archive)
|
||||
explicit Archive(std::unique_ptr<asar::Archive> archive)
|
||||
: archive_(std::move(archive)) {}
|
||||
|
||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
|
||||
base::FilePath path;
|
||||
if (!gin::ConvertFromV8(isolate, args[0], &path)) {
|
||||
isolate->ThrowException(v8::Exception::Error(node::FIXED_ONE_BYTE_STRING(
|
||||
isolate, "failed to convert path to V8")));
|
||||
return;
|
||||
}
|
||||
|
||||
auto archive = std::make_unique<asar::Archive>(path);
|
||||
if (!archive->Init()) {
|
||||
isolate->ThrowException(v8::Exception::Error(node::FIXED_ONE_BYTE_STRING(
|
||||
isolate, "failed to initialize archive")));
|
||||
return;
|
||||
}
|
||||
|
||||
auto* archive_wrap = new Archive(std::move(archive));
|
||||
archive_wrap->Wrap(args.This());
|
||||
args.GetReturnValue().Set(args.This());
|
||||
}
|
||||
|
||||
// Reads the offset and size of file.
|
||||
v8::Local<v8::Value> GetFileInfo(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
static void GetFileInfo(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
|
||||
|
||||
base::FilePath path;
|
||||
if (!gin::ConvertFromV8(isolate, args[0], &path)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
asar::Archive::FileInfo info;
|
||||
if (!archive_ || !archive_->GetFileInfo(path, &info))
|
||||
return v8::False(isolate);
|
||||
if (!wrap->archive_ || !wrap->archive_->GetFileInfo(path, &info)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||
dict.Set("size", info.size);
|
||||
dict.Set("unpacked", info.unpacked);
|
||||
|
@ -74,65 +99,104 @@ class Archive : public gin::Wrappable<Archive> {
|
|||
integrity.Set("hash", info.integrity.value().hash);
|
||||
dict.Set("integrity", integrity);
|
||||
}
|
||||
return dict.GetHandle();
|
||||
args.GetReturnValue().Set(dict.GetHandle());
|
||||
}
|
||||
|
||||
// Returns a fake result of fs.stat(path).
|
||||
v8::Local<v8::Value> Stat(v8::Isolate* isolate, const base::FilePath& path) {
|
||||
static void Stat(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
|
||||
base::FilePath path;
|
||||
if (!gin::ConvertFromV8(isolate, args[0], &path)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
asar::Archive::Stats stats;
|
||||
if (!archive_ || !archive_->Stat(path, &stats))
|
||||
return v8::False(isolate);
|
||||
if (!wrap->archive_ || !wrap->archive_->Stat(path, &stats)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||
dict.Set("size", stats.size);
|
||||
dict.Set("offset", stats.offset);
|
||||
dict.Set("isFile", stats.is_file);
|
||||
dict.Set("isDirectory", stats.is_directory);
|
||||
dict.Set("isLink", stats.is_link);
|
||||
return dict.GetHandle();
|
||||
args.GetReturnValue().Set(dict.GetHandle());
|
||||
}
|
||||
|
||||
// Returns all files under a directory.
|
||||
v8::Local<v8::Value> Readdir(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
static void Readdir(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
|
||||
base::FilePath path;
|
||||
if (!gin::ConvertFromV8(isolate, args[0], &path)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<base::FilePath> files;
|
||||
if (!archive_ || !archive_->Readdir(path, &files))
|
||||
return v8::False(isolate);
|
||||
return gin::ConvertToV8(isolate, files);
|
||||
if (!wrap->archive_ || !wrap->archive_->Readdir(path, &files)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
args.GetReturnValue().Set(gin::ConvertToV8(isolate, files));
|
||||
}
|
||||
|
||||
// Returns the path of file with symbol link resolved.
|
||||
v8::Local<v8::Value> Realpath(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
static void Realpath(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
|
||||
base::FilePath path;
|
||||
if (!gin::ConvertFromV8(isolate, args[0], &path)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
base::FilePath realpath;
|
||||
if (!archive_ || !archive_->Realpath(path, &realpath))
|
||||
return v8::False(isolate);
|
||||
return gin::ConvertToV8(isolate, realpath);
|
||||
if (!wrap->archive_ || !wrap->archive_->Realpath(path, &realpath)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
args.GetReturnValue().Set(gin::ConvertToV8(isolate, realpath));
|
||||
}
|
||||
|
||||
// Copy the file out into a temporary file and returns the new path.
|
||||
v8::Local<v8::Value> CopyFileOut(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
static void CopyFileOut(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
|
||||
base::FilePath path;
|
||||
if (!gin::ConvertFromV8(isolate, args[0], &path)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
base::FilePath new_path;
|
||||
if (!archive_ || !archive_->CopyFileOut(path, &new_path))
|
||||
return v8::False(isolate);
|
||||
return gin::ConvertToV8(isolate, new_path);
|
||||
if (!wrap->archive_ || !wrap->archive_->CopyFileOut(path, &new_path)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
args.GetReturnValue().Set(gin::ConvertToV8(isolate, new_path));
|
||||
}
|
||||
|
||||
// Return the file descriptor.
|
||||
int GetFD() const {
|
||||
if (!archive_)
|
||||
return -1;
|
||||
return archive_->GetUnsafeFD();
|
||||
static void GetFD(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
auto* wrap = node::ObjectWrap::Unwrap<Archive>(args.Holder());
|
||||
|
||||
args.GetReturnValue().Set(gin::ConvertToV8(
|
||||
isolate, wrap->archive_ ? wrap->archive_->GetUnsafeFD() : -1));
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<asar::Archive> archive_;
|
||||
};
|
||||
|
||||
// static
|
||||
gin::WrapperInfo Archive::kWrapperInfo = {gin::kEmbedderNativeGin};
|
||||
static void InitAsarSupport(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
auto require = args[0];
|
||||
|
||||
void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
|
||||
// Evaluate asar_bundle.js.
|
||||
std::vector<v8::Local<v8::String>> asar_bundle_params = {
|
||||
node::FIXED_ONE_BYTE_STRING(isolate, "require")};
|
||||
|
@ -142,8 +206,15 @@ void InitAsarSupport(v8::Isolate* isolate, v8::Local<v8::Value> require) {
|
|||
&asar_bundle_params, &asar_bundle_args, nullptr);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,
|
||||
const base::FilePath& path) {
|
||||
static void SplitPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto* isolate = args.GetIsolate();
|
||||
|
||||
base::FilePath path;
|
||||
if (!gin::ConvertFromV8(isolate, args[0], &path)) {
|
||||
args.GetReturnValue().Set(v8::False(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
||||
base::FilePath asar_path, file_path;
|
||||
if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
|
||||
|
@ -153,17 +224,24 @@ v8::Local<v8::Value> SplitPath(v8::Isolate* isolate,
|
|||
} else {
|
||||
dict.Set("isAsar", false);
|
||||
}
|
||||
return dict.GetHandle();
|
||||
args.GetReturnValue().Set(dict.GetHandle());
|
||||
}
|
||||
|
||||
void Initialize(v8::Local<v8::Object> exports,
|
||||
v8::Local<v8::Value> unused,
|
||||
v8::Local<v8::Context> context,
|
||||
void* priv) {
|
||||
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
||||
dict.SetMethod("createArchive", &Archive::Create);
|
||||
dict.SetMethod("splitPath", &SplitPath);
|
||||
dict.SetMethod("initAsarSupport", &InitAsarSupport);
|
||||
auto* isolate = exports->GetIsolate();
|
||||
|
||||
auto cons = Archive::CreateFunctionTemplate(isolate)
|
||||
->GetFunction(context)
|
||||
.ToLocalChecked();
|
||||
cons->SetName(node::FIXED_ONE_BYTE_STRING(isolate, "Archive"));
|
||||
|
||||
exports->Set(context, node::FIXED_ONE_BYTE_STRING(isolate, "Archive"), cons)
|
||||
.Check();
|
||||
NODE_SET_METHOD(exports, "splitPath", &SplitPath);
|
||||
NODE_SET_METHOD(exports, "initAsarSupport", &InitAsarSupport);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "node_errors.h"
|
||||
#include "node_internals.h"
|
||||
#include "node_native_module_env.h"
|
||||
#include "node_object_wrap.h"
|
||||
#include "node_options-inl.h"
|
||||
#include "node_options.h"
|
||||
#include "node_platform.h"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue