@@ -7,12 +7,10 @@
|
||||
|
||||
namespace node_mbgl {
|
||||
|
||||
NodeRequest::NodeRequest(
|
||||
mbgl::FileSource::Callback callback_,
|
||||
NodeAsyncRequest* asyncRequest_)
|
||||
NodeRequest::NodeRequest(mbgl::FileSource::Callback callback_, NodeAsyncRequest* asyncRequest_)
|
||||
: callback(std::move(callback_)),
|
||||
asyncRequest(asyncRequest_) {
|
||||
asyncRequest->request = this;
|
||||
asyncRequest(asyncRequest_) {
|
||||
asyncRequest->request = this;
|
||||
}
|
||||
|
||||
NodeRequest::~NodeRequest() {
|
||||
@@ -53,13 +51,9 @@ void NodeRequest::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
||||
request->Ref();
|
||||
Nan::Set(info.This(), Nan::New("url").ToLocalChecked(), info[3]);
|
||||
Nan::Set(info.This(), Nan::New("kind").ToLocalChecked(), info[4]);
|
||||
v8::Local<v8::Value> argv[] = { info.This() };
|
||||
v8::Local<v8::Value> argv[] = {info.This()};
|
||||
request->asyncResource->runInAsyncScope(
|
||||
Nan::To<v8::Object>(target->handle()->GetInternalField(1)).ToLocalChecked(),
|
||||
"request",
|
||||
1,
|
||||
argv
|
||||
);
|
||||
Nan::To<v8::Object>(target->handle()->GetInternalField(1)).ToLocalChecked(), "request", 1, argv);
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
@@ -84,15 +78,11 @@ void NodeRequest::HandleCallback(const Nan::FunctionCallbackInfo<v8::Value>& inf
|
||||
|
||||
if (Nan::Has(err, msg).FromJust()) {
|
||||
response.error = std::make_unique<mbgl::Response::Error>(
|
||||
mbgl::Response::Error::Reason::Other,
|
||||
*Nan::Utf8String(Nan::Get(err, msg).ToLocalChecked())
|
||||
);
|
||||
mbgl::Response::Error::Reason::Other, *Nan::Utf8String(Nan::Get(err, msg).ToLocalChecked()));
|
||||
}
|
||||
} else if (info[0]->IsString()) {
|
||||
response.error = std::make_unique<mbgl::Response::Error>(
|
||||
mbgl::Response::Error::Reason::Other,
|
||||
*Nan::Utf8String(info[0])
|
||||
);
|
||||
response.error = std::make_unique<mbgl::Response::Error>(mbgl::Response::Error::Reason::Other,
|
||||
*Nan::Utf8String(info[0]));
|
||||
} else if (info.Length() < 2 || !info[1]->IsObject()) {
|
||||
request->unrefRequest();
|
||||
return Nan::ThrowTypeError("Second argument must be a response object");
|
||||
@@ -100,33 +90,30 @@ void NodeRequest::HandleCallback(const Nan::FunctionCallbackInfo<v8::Value>& inf
|
||||
auto res = Nan::To<v8::Object>(info[1]).ToLocalChecked();
|
||||
|
||||
if (Nan::Has(res, Nan::New("modified").ToLocalChecked()).FromJust()) {
|
||||
const double modified = Nan::To<double>(Nan::Get(res, Nan::New("modified").ToLocalChecked()).ToLocalChecked()).FromJust();
|
||||
const double modified =
|
||||
Nan::To<double>(Nan::Get(res, Nan::New("modified").ToLocalChecked()).ToLocalChecked()).FromJust();
|
||||
if (!std::isnan(modified)) {
|
||||
response.modified = mbgl::Timestamp{ mbgl::Seconds(
|
||||
static_cast<mbgl::Seconds::rep>(modified / 1000)) };
|
||||
response.modified = mbgl::Timestamp{mbgl::Seconds(static_cast<mbgl::Seconds::rep>(modified / 1000))};
|
||||
}
|
||||
}
|
||||
|
||||
if (Nan::Has(res, Nan::New("expires").ToLocalChecked()).FromJust()) {
|
||||
const double expires = Nan::To<double>(Nan::Get(res, Nan::New("expires").ToLocalChecked()).ToLocalChecked()).FromJust();
|
||||
const double expires =
|
||||
Nan::To<double>(Nan::Get(res, Nan::New("expires").ToLocalChecked()).ToLocalChecked()).FromJust();
|
||||
if (!std::isnan(expires)) {
|
||||
response.expires = mbgl::Timestamp{ mbgl::Seconds(
|
||||
static_cast<mbgl::Seconds::rep>(expires / 1000)) };
|
||||
response.expires = mbgl::Timestamp{mbgl::Seconds(static_cast<mbgl::Seconds::rep>(expires / 1000))};
|
||||
}
|
||||
}
|
||||
|
||||
if (Nan::Has(res, Nan::New("etag").ToLocalChecked()).FromJust()) {
|
||||
const Nan::Utf8String etag(Nan::Get(res, Nan::New("etag").ToLocalChecked()).ToLocalChecked());
|
||||
response.etag = std::string { *etag, size_t(etag.length()) };
|
||||
response.etag = std::string{*etag, size_t(etag.length())};
|
||||
}
|
||||
|
||||
if (Nan::Has(res, Nan::New("data").ToLocalChecked()).FromJust()) {
|
||||
auto data = Nan::Get(res, Nan::New("data").ToLocalChecked()).ToLocalChecked();
|
||||
if (node::Buffer::HasInstance(data)) {
|
||||
response.data = std::make_shared<std::string>(
|
||||
node::Buffer::Data(data),
|
||||
node::Buffer::Length(data)
|
||||
);
|
||||
response.data = std::make_shared<std::string>(node::Buffer::Data(data), node::Buffer::Length(data));
|
||||
} else {
|
||||
request->unrefRequest();
|
||||
return Nan::ThrowTypeError("Response data must be a Buffer");
|
||||
|
||||
Reference in New Issue
Block a user