Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node 0.11.x support #99

Merged
merged 15 commits into from
Oct 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
]
}]
]
, "include_dirs": [ "<!(node -e \"require('nan')\")" ]
# The following breaks the debug build, so just ignore the warning for now.
#, 'msbuild_settings': {
# 'ClCompile': { 'ExceptionHandling': 'Sync' }
Expand Down
40 changes: 28 additions & 12 deletions lib/resolver_sequence_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,39 @@ exports.DNSServiceGetAddrInfo = function DNSServiceGetAddrInfo(options) {
var _getaddrinfo;
try {
var cares = process.binding('cares_wrap');
_getaddrinfo = function getaddrinfo_v06x(host, family, cb) {
function getaddrinfo_complete(err, addresses, cb) {
if (addresses) {
cb(undefined, addresses);
} else if (err === 'ENOENT') {
cb(undefined, []);
} else {
cb(errnoException(err, 'getaddrinfo'));
}
}
function getaddrinfo_0_11(host, family, cb) {
var err = cares.getaddrinfo({
oncomplete: function (err, addresses) {
getaddrinfo_complete(err, addresses, cb);
}
}, host, family);
if (err) throw errnoException(err, 'getaddrinfo', host);
}
function getaddrinfo_0_10(host, family, cb) {
var wrap = cares.getaddrinfo(host, family);
if ( ! wrap) {
throw errnoException(process._errno || global.errno, 'getaddrinfo');
}
wrap.oncomplete = function(addresses) {
var errno = process._errno || global.errno;

if (addresses) {
cb(undefined, addresses);
} else if (errno === 'ENOENT') {
cb(undefined, []);
} else {
cb(errnoException(errno, 'getaddrinfo'));
}
wrap.oncomplete = function (addresses) {
getaddrinfo_complete((process._errno || global.errno), addresses, cb);
}
};
}
// node 0.11+ cares.getaddrinfo function uses request object.
// use appropriate version based on node version number
if (Number(process.version.match(/^v(\d+\.\d+)/)[1]) > 0.1) {
_getaddrinfo = getaddrinfo_0_11;
} else {
_getaddrinfo = getaddrinfo_0_10;
}
} catch (ex) {
_getaddrinfo = process.binding('net').getaddrinfo;
}
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@
, "github": "TobyEalden"
}
]
, "dependencies":
{ "bindings": "~1.2.1"
, "nan": "~1.3.0"
}
, "gypfile": true
}

10 changes: 5 additions & 5 deletions src/demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ namespace {

Handle<Value>
demangle(Arguments const& args) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not compile in 0.11. Arguments is gone.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. And it doesn't. The file can be removed. It's a C++ symbol demangler for node. I used it in some crazy coverage testing experiment.

HandleScope scope;
NanScope();
String::Utf8Value str(args[0]->ToString());
#ifdef __GNUC__
int status;
char * ret = abi::__cxa_demangle(*str, NULL, NULL, & status);
Local<String> demangled = String::New(ret);
Local<String> demangled = NanNew(ret);
::free(ret);
#endif
return scope.Close(demangled);
NanReturnValue(demangled);
}

} // end of anaonymous namespace

extern "C"
void
init(Handle<Object> target) {
target->Set(String::NewSymbol("demangle"),
FunctionTemplate::New(demangle)->GetFunction());
target->Set(NanNew("demangle"),
NanNew<FunctionTemplate>(demangle)->GetFunction());
}
71 changes: 35 additions & 36 deletions src/dns_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,41 @@ using namespace node;
namespace node_mdns {

// === dns_sd ===========================================
Handle<Value> DNSServiceRegister(Arguments const& args);
Handle<Value> DNSServiceRefSockFD(Arguments const& args);
Handle<Value> DNSServiceProcessResult(Arguments const& args);
Handle<Value> DNSServiceBrowse(Arguments const& args);
Handle<Value> DNSServiceRefDeallocate(Arguments const& args);
Handle<Value> DNSServiceResolve(Arguments const& args);
Handle<Value> DNSServiceEnumerateDomains(Arguments const& args);
NAN_METHOD(DNSServiceRegister);
NAN_METHOD(DNSServiceRefSockFD);
NAN_METHOD(DNSServiceProcessResult);
NAN_METHOD(DNSServiceBrowse);
NAN_METHOD(DNSServiceRefDeallocate);
NAN_METHOD(DNSServiceResolve);
NAN_METHOD(DNSServiceEnumerateDomains);
#ifdef HAVE_DNSSERVICEGETADDRINFO
Handle<Value> DNSServiceGetAddrInfo(Arguments const& args);
NAN_METHOD(DNSServiceGetAddrInfo);
#endif
Handle<Value> TXTRecordCreate(Arguments const& args);
Handle<Value> TXTRecordDeallocate(Arguments const& args);
//Handle<Value> TXTRecordGetCount(Arguments const& args);
Handle<Value> TXTRecordSetValue(Arguments const& args);
Handle<Value> TXTRecordGetLength(Arguments const& args);
NAN_METHOD(TXTRecordCreate);
NAN_METHOD(TXTRecordDeallocate);
//NAN_METHOD(TXTRecordGetCount);
NAN_METHOD(TXTRecordSetValue);
NAN_METHOD(TXTRecordGetLength);

// === posix ============================================
#ifdef NODE_MDNS_HAVE_INTERFACE_NAME_CONVERSION
Handle<Value> if_nametoindex(Arguments const& args);
Handle<Value> if_indextoname(Arguments const& args);
NAN_METHOD(if_nametoindex);
NAN_METHOD(if_indextoname);
#endif

// === additions ========================================
Handle<Value> txtRecordBufferToObject(Arguments const& args);
Handle<Value> exportConstants(Arguments const& args);
Handle<Value> buildException(Arguments const& args);
NAN_METHOD(txtRecordBufferToObject);
NAN_METHOD(exportConstants);
NAN_METHOD(buildException);

// === locals ===========================================
void defineFunction(Handle<Object> target, const char * name, InvocationCallback f);

void defineFunction(Handle<Object> target, const char * name, NanFunctionCallback f);
void addConstants(Handle<Object> target);

void
init(Handle<Object> target) {
HandleScope scope;
NanScope();

ServiceRef::Initialize( target );
TxtRecordRef::Initialize( target );
Expand Down Expand Up @@ -87,24 +88,23 @@ init(Handle<Object> target) {

inline
void
defineFunction(Handle<Object> target, const char * name, InvocationCallback f) {
target->Set(String::NewSymbol(name),
FunctionTemplate::New(f)->GetFunction());
defineFunction(Handle<Object> target, const char * name, NanFunctionCallback f) {
target->Set(NanNew(name),
NanNew<FunctionTemplate>(f)->GetFunction());
}

Handle<Value>
buildException(Arguments const& args) {
HandleScope scope;
NAN_METHOD(buildException) {
NanScope();
if (argumentCountMismatch(args, 1)) {
return throwArgumentCountMismatchException(args, 1);
NanReturnValue(throwArgumentCountMismatchException(args, 1));
}
if ( ! args[0]->IsInt32()) {
return throwTypeError("argument 1 must be an integer "
"(DNSServiceErrorType)");
NanReturnValue(throwTypeError("argument 1 must be an integer "
"(DNSServiceErrorType)"));
}

DNSServiceErrorType error = args[0]->Int32Value();
return scope.Close(buildException(error));
NanReturnValue(buildException(error));
}

void
Expand Down Expand Up @@ -302,18 +302,17 @@ addConstants(Handle<Object> target) {
#endif
}

Handle<Value>
exportConstants(Arguments const& args) {
HandleScope scope;
NAN_METHOD(exportConstants) {
NanScope();
if (argumentCountMismatch(args, 1)) {
return throwArgumentCountMismatchException(args, 1);
NanReturnValue(throwArgumentCountMismatchException(args, 1));
}
if ( ! args[0]->IsObject()) {
return throwTypeError("argument 1 must be an object.");
NanReturnValue(throwTypeError("argument 1 must be an object."));
}

addConstants(args[0]->ToObject());
return Undefined();
NanReturnUndefined();
}

} // end of namespace node_mdns
Expand Down
43 changes: 21 additions & 22 deletions src/dns_service_browse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,68 @@ OnServiceChanged(DNSServiceRef sdRef, DNSServiceFlags flags,
const char * serviceName, const char * serviceType,
const char * replyDomain, void * context)
{
HandleScope scope;
NanScope();
ServiceRef * serviceRef = static_cast<ServiceRef*>(context);
Handle<Function> callback = serviceRef->GetCallback();
Handle<Object> this_ = serviceRef->GetThis();

const size_t argc(8);
Local<Value> args[argc];
args[0] = Local<Object>::New(serviceRef->handle_);
args[1] = Integer::New(flags);
args[2] = Integer::NewFromUnsigned(interfaceIndex);
args[3] = Integer::New(errorCode);
args[0] = NanObjectWrapHandle(serviceRef);
args[1] = NanNew<Integer>(flags);
args[2] = NanNew<Uint32>(interfaceIndex);
args[3] = NanNew<Integer>(errorCode);
args[4] = stringOrUndefined(serviceName);
args[5] = stringOrUndefined(serviceType);
args[6] = stringOrUndefined(replyDomain);
if (serviceRef->GetContext().IsEmpty()) {
args[7] = Local<Value>::New(Undefined());
args[7] = NanUndefined();
} else {
args[7] = Local<Value>::New(serviceRef->GetContext());
args[7] = NanNew<Value>(serviceRef->GetContext());
}
callback->Call(this_, argc, args);
NanMakeCallback(this_, callback, argc, args);
}

Handle<Value>
DNSServiceBrowse(Arguments const& args) {
HandleScope scope;
NAN_METHOD(DNSServiceBrowse) {
NanScope();
if (argumentCountMismatch(args, 7)) {
return throwArgumentCountMismatchException(args, 7);
NanReturnValue(throwArgumentCountMismatchException(args, 7));
}

if ( ! ServiceRef::HasInstance(args[0])) {
return throwTypeError("argument 1 must be a DNSServiceRef (sdRef)");
NanReturnValue(throwTypeError("argument 1 must be a DNSServiceRef (sdRef)"));
}
ServiceRef * serviceRef = ObjectWrap::Unwrap<ServiceRef>(args[0]->ToObject());
if (serviceRef->IsInitialized()) {
return throwError("DNSServiceRef is already initialized");
NanReturnValue(throwError("DNSServiceRef is already initialized"));
}

if ( ! args[1]->IsInt32()) {
return throwError("argument 2 must be an integer (DNSServiceFlags)");
NanReturnValue(throwError("argument 2 must be an integer (DNSServiceFlags)"));
}
DNSServiceFlags flags = args[1]->ToInteger()->Int32Value();

if ( ! args[2]->IsUint32() && ! args[2]->IsInt32()) {
return throwTypeError("argument 3 must be an integer (interfaceIndex)");
NanReturnValue(throwTypeError("argument 3 must be an integer (interfaceIndex)"));
}
uint32_t interfaceIndex = args[2]->ToInteger()->Uint32Value();

if ( ! args[3]->IsString()) {
return throwTypeError("argument 4 must be a string (service type)");
NanReturnValue(throwTypeError("argument 4 must be a string (service type)"));
}
String::Utf8Value serviceType(args[3]->ToString());

bool has_domain = false;
if ( ! args[4]->IsNull() && ! args[4]->IsUndefined()) {
if ( ! args[4]->IsString()) {
return throwTypeError("argument 5 must be a string (domain)");
NanReturnValue(throwTypeError("argument 5 must be a string (domain)"));
}
has_domain = true;
}
String::Utf8Value domain(args[4]);

if ( ! args[5]->IsFunction()) {
return throwTypeError("argument 6 must be a function (callBack)");
NanReturnValue(throwTypeError("argument 6 must be a function (callBack)"));
}
serviceRef->SetCallback(Local<Function>::Cast(args[5]));

Expand All @@ -92,14 +91,14 @@ DNSServiceBrowse(Arguments const& args) {
OnServiceChanged, serviceRef);

if (error != kDNSServiceErr_NoError) {
return throwMdnsError(error);
NanReturnValue(throwMdnsError(error));
}

if ( ! serviceRef->SetSocketFlags()) {
return throwError("Failed to set socket flags (O_NONBLOCK, FD_CLOEXEC)");
NanReturnValue(throwError("Failed to set socket flags (O_NONBLOCK, FD_CLOEXEC)"));
}

return Undefined();
NanReturnUndefined();
}

} // end of namespace node_mdns
Loading