7 #include <node_object_wrap.h>
25 using v8::FunctionCallbackInfo;
26 using v8::FunctionTemplate;
29 using v8::NewStringType;
31 using v8::ObjectTemplate;
46 auto* isolate = exports->GetIsolate ();
47 auto context = isolate->GetCurrentContext ();
49 auto addonDataTpl = ObjectTemplate::New (isolate);
50 addonDataTpl->SetInternalFieldCount (1);
52 auto addonData = addonDataTpl->NewInstance (context).ToLocalChecked ();
53 auto name = String::NewFromUtf8 (isolate,
"FileSaver", NewStringType::kNormal).ToLocalChecked ();
54 auto tpl = FunctionTemplate::New (isolate, New, addonData);
55 tpl->SetClassName (name);
57 std::vector<std::pair<std::string, v8::FunctionCallback>> methods{
62 {
"getCurrentSizeAt", GetCurrentSizeAt},
63 {
"getTargets", GetTargets},
64 {
"getTotalFiles", GetTotalFiles},
65 {
"getTotalKnownFiles", GetTotalKnownFiles},
66 {
"getFilesPerSecond", GetFilesPerSecond},
67 {
"getNumWorkers", GetNumWorkers},
68 {
"getElapsed", GetElapsed},
70 {
"isPathFinished", IsPathFinished},
71 {
"areAllTargetsFinished", AreAllTargetsFinished},
73 {
"setNumWorkers", SetNumWorkers},
76 tpl->InstanceTemplate ()->SetInternalFieldCount (
static_cast<int> (methods.size ()));
77 for (
auto& pair : methods)
79 NODE_SET_PROTOTYPE_METHOD (tpl, pair.first.c_str (), pair.second);
82 auto constructor = tpl->GetFunction (context).ToLocalChecked ();
83 addonData->SetInternalField (0, constructor);
84 exports->Set (context, name, constructor).FromJust ();
91 static void New (
const v8::FunctionCallbackInfo<v8::Value>& args)
93 Isolate* isolate = args.GetIsolate ();
94 Local<Context> context = isolate->GetCurrentContext ();
96 if (args.IsConstructCall ())
99 obj->Wrap (args.This ());
100 args.GetReturnValue ().Set (args.This ());
104 auto cons = args.Data ().As<Object> ()->GetInternalField (0).As<Function> ();
105 auto result = cons->NewInstance (context, 0,
nullptr).ToLocalChecked ();
106 args.GetReturnValue ().Set (result);
110 static void Start (
const v8::FunctionCallbackInfo<v8::Value>& args)
112 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
113 nodeFileSaver->fileSaver.start ();
116 static void Stop (
const v8::FunctionCallbackInfo<v8::Value>& args)
118 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
119 nodeFileSaver->fileSaver.stop ();
122 static void Scan (
const v8::FunctionCallbackInfo<v8::Value>& args)
124 auto* isolate = args.GetIsolate ();
125 auto context = isolate->GetCurrentContext ();
126 auto target = args[0]->ToString (context);
127 if (target.IsEmpty ())
129 isolate->ThrowException (Exception::TypeError (
130 String::NewFromUtf8 (isolate,
"Missing required target argument", NewStringType::kNormal)
131 .ToLocalChecked ()));
136 output.resize (
static_cast<unsigned long> (target.ToLocalChecked ()->Length ()));
137 target.ToLocalChecked ()->WriteUtf8 (isolate, output.data ());
138 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
139 nodeFileSaver->fileSaver.scan (output);
142 static void GetCurrentSizeAt (
const v8::FunctionCallbackInfo<v8::Value>& args)
144 auto* isolate = args.GetIsolate ();
145 auto context = isolate->GetCurrentContext ();
146 auto target = args[0]->ToString (context);
147 if (target.IsEmpty ())
149 isolate->ThrowException (Exception::TypeError (
150 String::NewFromUtf8 (isolate,
"Missing required target argument", NewStringType::kNormal)
151 .ToLocalChecked ()));
156 output.resize (
static_cast<unsigned long> (target.ToLocalChecked ()->Length ()));
157 target.ToLocalChecked ()->WriteUtf8 (isolate, output.data ());
158 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
159 auto result = nodeFileSaver->fileSaver.getCurrentSizeAt (output);
161 args.GetReturnValue ().Set (
static_cast<double> (result));
164 static void GetTargets (
const v8::FunctionCallbackInfo<v8::Value>& args)
166 auto* isolate = args.GetIsolate ();
167 auto context = isolate->GetCurrentContext ();
168 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
169 auto result = nodeFileSaver->fileSaver.getTargets ();
171 auto resultArray = v8::Array::New (isolate,
static_cast<int> (result.size ()));
173 for (
const auto& pth : result)
176 String::NewFromUtf8 (isolate, pth.string ().c_str (), NewStringType::kNormal).ToLocalChecked ();
177 auto r = resultArray->Set (context, i, value);
179 if (!r.FromMaybe (
false))
181 isolate->ThrowException (Exception::Error (
182 String::NewFromUtf8 (isolate,
"Failed to create result array", NewStringType::kNormal)
183 .ToLocalChecked ()));
189 args.GetReturnValue ().Set (resultArray);
192 static void GetTotalFiles (
const v8::FunctionCallbackInfo<v8::Value>& args)
194 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
195 auto result = nodeFileSaver->fileSaver.getTotalFiles ();
197 args.GetReturnValue ().Set (
static_cast<double> (result));
200 static void GetTotalKnownFiles (
const v8::FunctionCallbackInfo<v8::Value>& args)
202 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
203 auto result = nodeFileSaver->fileSaver.getTotalKnownFiles ();
205 args.GetReturnValue ().Set (
static_cast<double> (result));
208 static void GetFilesPerSecond (
const v8::FunctionCallbackInfo<v8::Value>& args)
210 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
211 auto result = nodeFileSaver->fileSaver.getFilesPerSecond ();
213 args.GetReturnValue ().Set (
static_cast<double> (result));
216 static void GetNumWorkers (
const v8::FunctionCallbackInfo<v8::Value>& args)
218 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
219 auto result = nodeFileSaver->fileSaver.getNumWorkers ();
221 args.GetReturnValue ().Set (
static_cast<double> (result));
224 static void GetElapsed (
const v8::FunctionCallbackInfo<v8::Value>& args)
226 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
227 auto result = nodeFileSaver->fileSaver.getElapsed ();
229 args.GetReturnValue ().Set (
static_cast<double> (result));
232 static void IsPathFinished (
const v8::FunctionCallbackInfo<v8::Value>& args)
234 auto* isolate = args.GetIsolate ();
235 auto context = isolate->GetCurrentContext ();
236 auto target = args[0]->ToString (context);
237 if (target.IsEmpty ())
239 isolate->ThrowException (Exception::TypeError (
240 String::NewFromUtf8 (isolate,
"Missing required target argument", NewStringType::kNormal)
241 .ToLocalChecked ()));
246 output.resize (
static_cast<unsigned long> (target.ToLocalChecked ()->Length ()));
247 target.ToLocalChecked ()->WriteUtf8 (isolate, output.data ());
248 boost::filesystem::path outputPath{output};
250 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
251 auto result = nodeFileSaver->fileSaver.isPathFinished (outputPath);
253 args.GetReturnValue ().Set (
static_cast<bool> (result));
256 static void AreAllTargetsFinished (
const v8::FunctionCallbackInfo<v8::Value>& args)
258 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
259 auto result = nodeFileSaver->fileSaver.areAllTargetsFinished ();
261 args.GetReturnValue ().Set (
static_cast<bool> (result));
264 static void SetNumWorkers (
const v8::FunctionCallbackInfo<v8::Value>& args)
266 auto* isolate = args.GetIsolate ();
267 auto context = isolate->GetCurrentContext ();
268 auto numWorkers = args[0]->ToUint32 (context);
269 if (numWorkers.IsEmpty ())
271 isolate->ThrowException (Exception::TypeError (
272 String::NewFromUtf8 (isolate,
"Missing required numWorkers argument", NewStringType::kNormal)
273 .ToLocalChecked ()));
277 auto* nodeFileSaver = ObjectWrap::Unwrap<NodeFileSaver> (args.Holder ());
278 nodeFileSaver->fileSaver.setNumWorkers (numWorkers.ToLocalChecked ()->Value ());
289 NODE_MODULE (NODE_GYP_MODULE_NAME,
initialize)
static void initialize(Local< Object > exports)
Defines Node.js bindings to FileSaver.
void initialize(Local< Object > exports)