Added aliases for std::chrono typedefs (eg. 'Seconds' for std::chrono::seconds). These aliases are used together with templated helper functions to replace time_t with std::chrono::seconds for most cases, in particular for 'modified' and 'expires' values in Response.
113 lines
4.2 KiB
C++
113 lines
4.2 KiB
C++
#include "node_request.hpp"
|
|
#include <mbgl/storage/response.hpp>
|
|
#include <mbgl/util/chrono.hpp>
|
|
|
|
#include <cmath>
|
|
#include <iostream>
|
|
|
|
namespace node_mbgl {
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// Static Node Methods
|
|
|
|
Nan::Persistent<v8::Function> NodeRequest::constructor;
|
|
|
|
NAN_MODULE_INIT(NodeRequest::Init) {
|
|
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
|
|
|
|
tpl->InstanceTemplate()->SetInternalFieldCount(1);
|
|
tpl->SetClassName(Nan::New("Request").ToLocalChecked());
|
|
|
|
constructor.Reset(tpl->GetFunction());
|
|
Nan::Set(target, Nan::New("Request").ToLocalChecked(), tpl->GetFunction());
|
|
}
|
|
|
|
NAN_METHOD(NodeRequest::New) {
|
|
auto req = new NodeRequest(*reinterpret_cast<mbgl::FileSource::Callback*>(info[0].As<v8::External>()->Value()));
|
|
req->Wrap(info.This());
|
|
info.GetReturnValue().Set(info.This());
|
|
}
|
|
|
|
v8::Handle<v8::Object> NodeRequest::Create(const mbgl::Resource& resource, mbgl::FileSource::Callback callback) {
|
|
Nan::EscapableHandleScope scope;
|
|
|
|
v8::Local<v8::Value> argv[] = {
|
|
Nan::New<v8::External>(const_cast<mbgl::FileSource::Callback*>(&callback))
|
|
};
|
|
auto instance = Nan::New(constructor)->NewInstance(1, argv);
|
|
|
|
Nan::Set(instance, Nan::New("url").ToLocalChecked(), Nan::New(resource.url).ToLocalChecked());
|
|
Nan::Set(instance, Nan::New("kind").ToLocalChecked(), Nan::New<v8::Integer>(int(resource.kind)));
|
|
|
|
return scope.Escape(instance);
|
|
}
|
|
|
|
NAN_METHOD(NodeRequest::Respond) {
|
|
using Error = mbgl::Response::Error;
|
|
using Milliseconds = mbgl::Milliseconds;
|
|
|
|
mbgl::Response response;
|
|
|
|
if (info.Length() < 1) {
|
|
response.error = std::make_unique<Error>(Error::Reason::NotFound);
|
|
|
|
} else if (info[0]->BooleanValue()) {
|
|
// Store the error string.
|
|
const Nan::Utf8String message { info[0]->ToString() };
|
|
response.error = std::make_unique<Error>(
|
|
Error::Reason::Other, std::string{ *message, size_t(message.length()) });
|
|
|
|
} else if (info.Length() < 2 || !info[1]->IsObject()) {
|
|
return Nan::ThrowTypeError("Second argument must be a response object");
|
|
|
|
} else {
|
|
auto res = info[1]->ToObject();
|
|
|
|
if (Nan::Has(res, Nan::New("modified").ToLocalChecked()).FromJust()) {
|
|
const double modified = Nan::Get(res, Nan::New("modified").ToLocalChecked()).ToLocalChecked()->ToNumber()->Value();
|
|
if (!std::isnan(modified)) {
|
|
response.modified = mbgl::asSeconds(Milliseconds(Milliseconds::rep(modified)));
|
|
}
|
|
}
|
|
|
|
if (Nan::Has(res, Nan::New("expires").ToLocalChecked()).FromJust()) {
|
|
const double expires = Nan::Get(res, Nan::New("expires").ToLocalChecked()).ToLocalChecked()->ToNumber()->Value();
|
|
if (!std::isnan(expires)) {
|
|
response.expires = mbgl::asSeconds(Milliseconds(Milliseconds::rep(expires)));
|
|
}
|
|
}
|
|
|
|
if (Nan::Has(res, Nan::New("etag").ToLocalChecked()).FromJust()) {
|
|
auto etagHandle = Nan::Get(res, Nan::New("etag").ToLocalChecked()).ToLocalChecked();
|
|
if (etagHandle->BooleanValue()) {
|
|
const Nan::Utf8String etag { etagHandle->ToString() };
|
|
response.etag = std::string { *etag, size_t(etag.length()) };
|
|
}
|
|
}
|
|
|
|
if (Nan::Has(res, Nan::New("data").ToLocalChecked()).FromJust()) {
|
|
auto dataHandle = Nan::Get(res, Nan::New("data").ToLocalChecked()).ToLocalChecked();
|
|
if (node::Buffer::HasInstance(dataHandle)) {
|
|
response.data = std::make_shared<std::string>(
|
|
node::Buffer::Data(dataHandle),
|
|
node::Buffer::Length(dataHandle)
|
|
);
|
|
} else {
|
|
return Nan::ThrowTypeError("Response data must be a Buffer");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Send the response object to the NodeFileSource object
|
|
Nan::ObjectWrap::Unwrap<NodeRequest>(info.Data().As<v8::Object>())->callback(response);
|
|
info.GetReturnValue().SetUndefined();
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// Instance
|
|
|
|
NodeRequest::NodeRequest(mbgl::FileSource::Callback callback_)
|
|
: callback(callback_) {}
|
|
|
|
}
|