-
Notifications
You must be signed in to change notification settings - Fork 15.7k
/
Copy pathelectron_api_asar.cc
249 lines (211 loc) · 8.39 KB
/
electron_api_asar.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <vector>
#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"
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::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.
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::SHA256:
integrity.Set("algorithm", "SHA256");
break;
case asar::HashAlgorithm::NONE:
CHECK(false);
break;
}
integrity.Set("hash", info.integrity.value().hash);
dict.Set("integrity", integrity);
}
args.GetReturnValue().Set(dict.GetHandle());
}
// 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;
}
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));
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);
args.GetReturnValue().Set(dict.GetHandle());
}
// 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;
}
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));
}
// 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;
}
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));
}
// 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));
}
std::unique_ptr<asar::Archive> archive_;
};
static void InitAsarSupport(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto* isolate = args.GetIsolate();
auto require = args[0];
// 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};
electron::util::CompileAndCall(
isolate->GetCurrentContext(), "electron/js2c/asar_bundle",
&asar_bundle_params, &asar_bundle_args, nullptr);
}
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)) {
dict.Set("isAsar", true);
dict.Set("asarPath", asar_path);
dict.Set("filePath", file_path);
} else {
dict.Set("isAsar", false);
}
args.GetReturnValue().Set(dict.GetHandle());
}
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_MODULE_CONTEXT_AWARE(electron_common_asar, Initialize)