electron/shell/common/api/electron_api_asar.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

248 lines
8.4 KiB
C++
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
2014-09-24 10:44:00 +00:00
#include <vector>
2020-08-03 21:26:27 +00:00
#include "gin/handle.h"
#include "shell/common/asar/archive.h"
#include "shell/common/asar/asar_util.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
2020-08-03 21:26:27 +00:00
namespace {
class Archive : public node::ObjectWrap {
public:
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);
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);
return tpl;
}
// disable copy
Archive(const Archive&) = delete;
Archive& operator=(const Archive&) = delete;
protected:
explicit Archive(std::shared_ptr<asar::Archive> archive)
2020-08-03 21:26:27 +00:00
: archive_(std::move(archive)) {}
2016-04-25 01:17:54 +00:00
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;
}
std::shared_ptr<asar::Archive> archive = asar::GetOrCreateAsarArchive(path);
if (!archive) {
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());
}
2014-09-24 04:02:33 +00:00
// Reads the offset and size of file.
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 (!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);
dict.Set("offset", info.offset);
if (info.integrity.has_value()) {
gin_helper::Dictionary integrity(isolate, v8::Object::New(isolate));
asar::HashAlgorithm algorithm = info.integrity.value().algorithm;
switch (algorithm) {
case asar::HashAlgorithm::kSHA256:
integrity.Set("algorithm", "SHA256");
break;
case asar::HashAlgorithm::kNone:
CHECK(false);
break;
}
integrity.Set("hash", info.integrity.value().hash);
dict.Set("integrity", integrity);
}
args.GetReturnValue().Set(dict.GetHandle());
}
2014-09-24 04:02:33 +00:00
// Returns a fake result of fs.stat(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;
}
2014-09-24 04:02:33 +00:00
asar::Archive::Stats stats;
if (!wrap->archive_ || !wrap->archive_->Stat(path, &stats)) {
args.GetReturnValue().Set(v8::False(isolate));
return;
}
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
2014-09-24 04:02:33 +00:00
dict.Set("size", stats.size);
dict.Set("offset", stats.offset);
dict.Set("type", static_cast<int>(stats.type));
args.GetReturnValue().Set(dict.GetHandle());
2014-09-24 04:02:33 +00:00
}
2014-09-24 10:44:00 +00:00
// Returns all files under a directory.
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;
}
2014-09-24 10:44:00 +00:00
std::vector<base::FilePath> files;
if (!wrap->archive_ || !wrap->archive_->Readdir(path, &files)) {
args.GetReturnValue().Set(v8::False(isolate));
return;
}
args.GetReturnValue().Set(gin::ConvertToV8(isolate, files));
2014-09-24 10:44:00 +00:00
}
2014-09-30 06:53:41 +00:00
// Returns the path of file with symbol link resolved.
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;
}
2014-09-30 06:53:41 +00:00
base::FilePath realpath;
if (!wrap->archive_ || !wrap->archive_->Realpath(path, &realpath)) {
args.GetReturnValue().Set(v8::False(isolate));
return;
}
args.GetReturnValue().Set(gin::ConvertToV8(isolate, realpath));
2014-09-30 06:53:41 +00:00
}
// Copy the file out into a temporary file and returns the new 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 (!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.
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));
2015-05-11 03:02:17 +00:00
}
std::shared_ptr<asar::Archive> archive_;
};
static void InitAsarSupport(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto* isolate = args.GetIsolate();
auto require = args[0];
2020-08-03 21:26:27 +00:00
// Evaluate asar_bundle.js.
std::vector<v8::Local<v8::String>> asar_bundle_params = {
node::FIXED_ONE_BYTE_STRING(isolate, "require")};
std::vector<v8::Local<v8::Value>> asar_bundle_args = {require};
chore: upgrade to Node.js v20 (#40545) * chore: upgrade to Node.js v20 * src: allow embedders to override NODE_MODULE_VERSION https://github.com/nodejs/node/pull/49279 * src: fix missing trailing , https://github.com/nodejs/node/pull/46909 * src,tools: initialize cppgc https://github.com/nodejs/node/pull/45704 * tools: allow passing absolute path of config.gypi in js2c https://github.com/nodejs/node/pull/49162 * tools: port js2c.py to C++ https://github.com/nodejs/node/pull/46997 * doc,lib: disambiguate the old term, NativeModule https://github.com/nodejs/node/pull/45673 * chore: fixup Node.js BSSL tests * https://github.com/nodejs/node/pull/49492 * https://github.com/nodejs/node/pull/44498 * deps: upgrade to libuv 1.45.0 https://github.com/nodejs/node/pull/48078 * deps: update V8 to 10.7 https://github.com/nodejs/node/pull/44741 * test: use gcUntil() in test-v8-serialize-leak https://github.com/nodejs/node/pull/49168 * module: make CJS load from ESM loader https://github.com/nodejs/node/pull/47999 * src: make BuiltinLoader threadsafe and non-global https://github.com/nodejs/node/pull/45942 * chore: address changes to CJS/ESM loading * module: make CJS load from ESM loader (https://github.com/nodejs/node/pull/47999) * lib: improve esm resolve performance (https://github.com/nodejs/node/pull/46652) * bootstrap: optimize modules loaded in the built-in snapshot https://github.com/nodejs/node/pull/45849 * test: mark test-runner-output as flaky https://github.com/nodejs/node/pull/49854 * lib: lazy-load deps in modules/run_main.js https://github.com/nodejs/node/pull/45849 * url: use private properties for brand check https://github.com/nodejs/node/pull/46904 * test: refactor `test-node-output-errors` https://github.com/nodejs/node/pull/48992 * assert: deprecate callTracker https://github.com/nodejs/node/pull/47740 * src: cast v8::Object::GetInternalField() return value to v8::Value https://github.com/nodejs/node/pull/48943 * test: adapt test-v8-stats for V8 update https://github.com/nodejs/node/pull/45230 * tls: ensure TLS Sockets are closed if the underlying wrap closes https://github.com/nodejs/node/pull/49327 * test: deflake test-tls-socket-close https://github.com/nodejs/node/pull/49575 * net: fix crash due to simultaneous close/shutdown on JS Stream Sockets https://github.com/nodejs/node/pull/49400 * net: use asserts in JS Socket Stream to catch races in future https://github.com/nodejs/node/pull/49400 * lib: fix BroadcastChannel initialization location https://github.com/nodejs/node/pull/46864 * src: create BaseObject with node::Realm https://github.com/nodejs/node/pull/44348 * src: implement DataQueue and non-memory resident Blob https://github.com/nodejs/node/pull/45258 * sea: add support for V8 bytecode-only caching https://github.com/nodejs/node/pull/48191 * chore: fixup patch indices * gyp: put filenames in variables https://github.com/nodejs/node/pull/46965 * build: modify js2c.py into GN executable * fix: (WIP) handle string replacement of fs -> original-fs * [v20.x] backport vm-related memory fixes https://github.com/nodejs/node/pull/49874 * src: make BuiltinLoader threadsafe and non-global https://github.com/nodejs/node/pull/45942 * src: avoid copying string in fs_permission https://github.com/nodejs/node/pull/47746 * look upon my works ye mighty and dispair * chore: patch cleanup * [api] Remove AllCan Read/Write https://chromium-review.googlesource.com/c/v8/v8/+/5006387 * fix: missing include for NODE_EXTERN * chore: fixup patch indices * fix: fail properly when js2c fails in Node.js * build: fix js2c root_gen_dir * fix: lib/fs.js -> lib/original-fs.js * build: fix original-fs file xforms * fixup! module: make CJS load from ESM loader * build: get rid of CppHeap for now * build: add patch to prevent extra fs lookup on esm load * build: greatly simplify js2c modifications Moves our original-fs modifications back into a super simple python script action, wires up the output of that action into our call to js2c * chore: update to handle moved internal/modules/helpers file * test: update @types/node test * feat: enable preventing cppgc heap creation * feat: optionally prevent calling V8::EnableWebAssemblyTrapHandler * fix: no cppgc initialization in the renderer * gyp: put filenames in variables https://github.com/nodejs/node/pull/46965 * test: disable single executable tests * fix: nan tests failing on node headers missing file * tls,http2: send fatal alert on ALPN mismatch https://github.com/nodejs/node/pull/44031 * test: disable snapshot tests * https://github.com/nodejs/node/pull/47887 * https://github.com/nodejs/node/pull/49684 * https://github.com/nodejs/node/pull/44193 * build: use deps/v8 for v8/tools Node.js hard depends on these in their builtins * test: fix edge snapshot stack traces https://github.com/nodejs/node/pull/49659 * build: remove js2c //base dep * build: use electron_js2c_toolchain to build node_js2c * fix: don't create SafeSet outside packageResolve Fixes failure in parallel/test-require-delete-array-iterator: === release test-require-delete-array-iterator === Path: parallel/test-require-delete-array-iterator node:internal/per_context/primordials:426 constructor(i) { super(i); } // eslint-disable-line no-useless-constructor ^ TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) at new Set (<anonymous>) at new SafeSet (node:internal/per_context/primordials:426:22) * fix: failing crashReporter tests on Linux These were failing because our change from node::InitializeNodeWithArgs to node::InitializeOncePerProcess meant that we now inadvertently called PlatformInit, which reset signal handling. This meant that our intentional crash function ElectronBindings::Crash no longer worked and the renderer process no longer crashed when process.crash() was called. We don't want to use Node.js' default signal handling in the renderer process, so we disable it by passing kNoDefaultSignalHandling to node::InitializeOncePerProcess. * build: only create cppgc heap on non-32 bit platforms * chore: clean up util:CompileAndCall * src: fix compatility with upcoming V8 12.1 APIs https://github.com/nodejs/node/pull/50709 * fix: use thread_local BuiltinLoader * chore: fixup v8 patch indices --------- Co-authored-by: Keeley Hammond <vertedinde@electronjs.org> Co-authored-by: Samuel Attard <marshallofsound@electronjs.org>
2023-11-30 14:51:35 +00:00
electron::util::CompileAndCall(isolate->GetCurrentContext(),
"electron/js2c/asar_bundle",
&asar_bundle_params, &asar_bundle_args);
}
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;
}
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
base::FilePath asar_path, file_path;
if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
dict.Set("isAsar", true);
dict.Set("asarPath", asar_path);
dict.Set("filePath", file_path);
} else {
dict.Set("isAsar", false);
}
args.GetReturnValue().Set(dict.GetHandle());
}
2018-04-18 01:55:30 +00:00
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
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
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_asar, Initialize)