Skip to content

Commit

Permalink
Support node v 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
puritys committed May 24, 2015
1 parent 8ed7088 commit bd7f1b8
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 149 deletions.
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
],
"include_dirs": [
"include/linux",
"src/nodeExt/includes"
"src/nodeExt/includes",
"<!(node -e \"require('nan')\")"
]
}
],
Expand Down
78 changes: 53 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
{
"version": "2.3.4",
"description": "Implement php function for nodejs ",
"main": "phplike",
"keywords": ["php", "php-like", "curl", "exec", "xml parser", "socket", "mysql"],
"repository": {
"type": "git",
"url": "git@github.com:puritys/nodejs-phplike.git"
},
"contributors": "puritys.chen@outlook.com",
"homepage": "https://github.com/puritys/nodejs-phplike",
"license" : "MIT",
"name" : "phplike",
"files": ["src", "index.js", "module.js", "binding.gyp", "package.json", "install.js", "binary"],
"scripts": {
"install": "node install.js",
"test": "mocha tests",
"build": "node-gyp rebuild"
},
"dependencies": {
"node-gyp": ">=1.0.2",
"bindings": ">=1.2.1"
},
"gypfile": true,
"os" : ["linux", "mac", "darwin", "win", "win32", "win64" ],
"cpu" : [ "x64", "ia32", "arm" ]
"version": "2.3.4",
"description": "Implement php function for nodejs ",
"main": "phplike",
"keywords": [
"php",
"php-like",
"curl",
"exec",
"xml parser",
"socket",
"mysql"
],
"repository": {
"type": "git",
"url": "git@github.com:puritys/nodejs-phplike.git"
},
"contributors": "puritys.chen@outlook.com",
"homepage": "https://github.com/puritys/nodejs-phplike",
"license": "MIT",
"name": "phplike",
"files": [
"src",
"index.js",
"module.js",
"binding.gyp",
"package.json",
"install.js",
"binary"
],
"scripts": {
"install": "node install.js",
"test": "mocha tests",
"build": "node-gyp rebuild"
},
"dependencies": {
"bindings": ">=1.2.1",
"nan": "^1.8.4",
"node-gyp": ">=1.0.2"
},
"gypfile": true,
"os": [
"linux",
"mac",
"darwin",
"win",
"win32",
"win64"
],
"cpu": [
"x64",
"ia32",
"arm"
]
}
2 changes: 1 addition & 1 deletion src/js/socket.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var cpp = require('./requireNative.js');

function fsockopen(hostname, port) {
return cpp.nodeSocketConnet(hostname, port);
return cpp.nodeSocketConnect(hostname, port);
}

function sendcmd(msg, socket, length) {
Expand Down
9 changes: 5 additions & 4 deletions src/nodeExt/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
{
"target_name": "phplike",
"sources": [
"phplikeCurl.cc",
"phplike.cc",
"phplikeXml.cc",
"phplikeSocket.cc"
"phplikeCurl.cc",
"phplikeSocket.cc",
"phplike.cc"
],
"cflags": ["-std=gnu++0x"],
"cflags_cc": ["-fexceptions"],
Expand All @@ -21,7 +21,8 @@
"LINUX_DEFINE"
],
"include_dirs": [
"include/linux", "includes"
"include/linux", "./includes",
"<!(node -e \"require('nan')\")"
]
}],
['OS=="win"', {
Expand Down
5 changes: 3 additions & 2 deletions src/nodeExt/includes/common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include "v8.h"
#include "node.h"
#include <v8.h>
#include <node.h>
#include <nan.h>
#include <string>

#ifdef OS_LINUX
Expand Down
9 changes: 5 additions & 4 deletions src/nodeExt/includes/phplike.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@



NAN_METHOD(node_usleep);
NAN_METHOD(node_exec);
NAN_METHOD(node_md5);
NAN_METHOD(node_curl_request);
NAN_METHOD(nodeCurlGetHeader);

Handle<Value> node_usleep(const Arguments& args);
Handle<Value> node_exec(const Arguments& args);
Handle<Value> node_curl_request(const Arguments& args);
Handle<Value> nodeCurlGetHeader(const Arguments& args);

7 changes: 3 additions & 4 deletions src/nodeExt/includes/phplikeSocket.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "../../cpp/socket/src/socket.h"


Handle<Value> nodeSocketConnet(const Arguments &args);
Handle<Value> nodeSocketSend(const Arguments &args);
Handle<Value> nodeSocketReceive(const Arguments &args);
NAN_METHOD(nodeSocketSend);
NAN_METHOD(nodeSocketReceive);
NAN_METHOD(nodeSocketConnect);



8 changes: 4 additions & 4 deletions src/nodeExt/includes/phplikeXml.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ using namespace tinyxml2;
class phpXMLDocument : public node::ObjectWrap {
public:
static void init(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
static Handle<Value> load(const Arguments &args);
static Handle<Value> loadXML(const Arguments &args);
static NAN_METHOD(New);//Handle<Value> New(const Arguments &args);
static NAN_METHOD(load);//Handle<Value> load(const Arguments &args);
static NAN_METHOD(loadXML);//Handle<Value> loadXML(const Arguments &args);
static void loadChild(Handle<Object> object, XMLNode* node);
static Handle<Object> getNodeInfo(XMLNode* node, XMLNode* firstChild);
static Handle<Object> getTextNodeInfo(XMLNode* node);
static void setAttributesIntoJs(Handle<Object> obj, XMLNode* node);
static Handle<Value> parseXML(phpXMLDocument *doc);
static Handle<Object> parseXML(phpXMLDocument *doc);

tinyxml2::XMLDocument doc;

Expand Down
30 changes: 13 additions & 17 deletions src/nodeExt/phplike.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,49 @@



Handle<Value> node_usleep(const Arguments& args)
{
unsigned int microSeconds = args[0]->Uint32Value();

NAN_METHOD(node_usleep) {
NanScope();
#ifdef OS_LINUX
unsigned int microSeconds = args[0]->Uint32Value();
usleep(microSeconds);
#endif

#ifdef OS_WIN
unsigned int microSeconds = args[0]->Uint32Value();
unsigned int milliSeconds = microSeconds / 1000;
Sleep(milliSeconds);
#endif

return True();
NanReturnValue(NanNew<Boolean>(true));
}

Handle<Value> node_exec(const Arguments& args)
{
NAN_METHOD(node_exec) {
NanScope();
String::Utf8Value cmd(args[0]);
bool showMessage = args[1]->BooleanValue();
string s = exec(string(*cmd), showMessage);
return String::New(s.c_str());
NanReturnValue(NanNew<String>(s.c_str()));
}

Handle<Value> node_md5(const Arguments& args)
{
NAN_METHOD(node_md5) {
NanScope();
String::Utf8Value text(args[0]);
string s = md5(string(*text));
return String::New(s.c_str());
NanReturnValue(NanNew<String>(s.c_str()));
}



void init (Handle<Object> target){

HandleScope scope;
NanScope();
NODE_SET_METHOD(target, "usleep", node_usleep);
NODE_SET_METHOD(target, "exec", node_exec);
NODE_SET_METHOD(target, "request", node_curl_request);
NODE_SET_METHOD(target, "nodeCurlGetHeader", nodeCurlGetHeader);
NODE_SET_METHOD(target, "md5", node_md5);
NODE_SET_METHOD(target, "nodeSocketConnet", nodeSocketConnet);
NODE_SET_METHOD(target, "nodeSocketConnect", nodeSocketConnect);
NODE_SET_METHOD(target, "nodeSocketSend", nodeSocketSend);
NODE_SET_METHOD(target, "nodeSocketReceive", nodeSocketReceive);



phpXMLDocument::init(target);
}

Expand Down
40 changes: 23 additions & 17 deletions src/nodeExt/phplikeCurl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
string resHeader;


Handle<Value> node_curl_request(const Arguments& args) {
//Handle<Value> node_curl_request(Handle<Object> args) {
NAN_METHOD(node_curl_request) {
int i, n, curlType = 0;
NanScope();
phplikeCppCurl *pCurl = new phplikeCppCurl();
String::Utf8Value method(args[0]);
String::Utf8Value url(args[1]);
Expand All @@ -32,7 +34,7 @@ Handle<Value> node_curl_request(const Arguments& args) {
propertyNames = jsParam->GetPropertyNames();
n = propertyNames->Length();
for (i = 0; i < n ; i++) {
Handle<Value> b = propertyNames->Get(Integer::New(i));
Handle<Value> b = propertyNames->Get(NanNew<Integer>(i));
string c = string(*String::Utf8Value(b));
Handle<Value> v = jsParam->Get(b);
param[c] = string(*String::Utf8Value(v));
Expand All @@ -48,7 +50,7 @@ Handle<Value> node_curl_request(const Arguments& args) {
propertyNames = jsHeader->GetPropertyNames();
n = propertyNames->Length();
for (i = 0; i < n ; i++) {
Handle<Value> b = propertyNames->Get(Integer::New(i));
Handle<Value> b = propertyNames->Get(NanNew<Integer>(i));
string c = string(*String::Utf8Value(b));
Handle<Value> v = jsHeader->Get(b);
header[c] = string(*String::Utf8Value(v));
Expand All @@ -62,7 +64,7 @@ Handle<Value> node_curl_request(const Arguments& args) {
propertyNames = jsOptions->GetPropertyNames();
n = propertyNames->Length();
for (i = 0; i < n ; i++) {
Handle<Value> b = propertyNames->Get(Integer::New(i));
Handle<Value> b = propertyNames->Get(NanNew<Integer>(i));
string c = string(*String::Utf8Value(b));
Handle<Value> v = jsOptions->Get(b);
options[c] = string(*String::Utf8Value(v));
Expand All @@ -78,15 +80,15 @@ Handle<Value> node_curl_request(const Arguments& args) {

for (i = 0; i < n ; i++) {
vector<string> fileInfo;
//Handle<Value> jsFileInfo = propertyNames->Get(Integer::New(i));
Handle<Value> b = propertyNames->Get(Integer::New(i));
//Handle<Value> jsFileInfo = propertyNames->Get(NanNew<Integer>(i));
Handle<Value> b = propertyNames->Get(NanNew<Integer>(i));
string c = string(*String::Utf8Value(b));

Handle<Array> v = Handle<Array>::Cast(jsFileUpload->Get(b));

// handle file array
Handle<Value> fileName = v->Get(Integer::New(0));
Handle<Value> filePath = v->Get(Integer::New(1));
Handle<Value> fileName = v->Get(NanNew<Integer>(0));
Handle<Value> filePath = v->Get(NanNew<Integer>(1));
fileInfo.push_back(string(*String::Utf8Value(fileName)));
fileInfo.push_back(string(*String::Utf8Value(filePath)));

Expand All @@ -106,25 +108,29 @@ Handle<Value> node_curl_request(const Arguments& args) {

resHeader = pCurl->resHeader;
if (pCurl->contentLength <= 0) {
return String::New("");
NanReturnValue(NanNew<String>(""));
}

// save binary data into js string.
if (
jsOptions->Has(String::New("BINARY_RESPONSE"))
&& string(*String::Utf8Value(jsOptions->Get(String::New("BINARY_RESPONSE")))) == "1"
jsOptions->Has(NanNew<String>("BINARY_RESPONSE"))
&& string(*String::Utf8Value(jsOptions->Get(NanNew<String>("BINARY_RESPONSE")))) == "1"
) {
node::Buffer *buffer = node::Buffer::New(pCurl->contentLength);
memcpy(node::Buffer::Data(buffer), pCurl->resContentPointer, pCurl->contentLength);
return buffer->handle_;
//node::Buffer *buffer = node::Buffer::New(pCurl->contentLength);
//NanNewBufferHandle((char*)value.data(), value.size());
NanReturnValue(NanNewBufferHandle(pCurl->resContentPointer, pCurl->contentLength));
//memcpy(node::Buffer::Data(buffer), pCurl->resContentPointer, pCurl->contentLength);
//return buffer->handle_;
}

return String::New(pCurl->resContentPointer, pCurl->contentLength);
NanReturnValue(NanNew<String>(pCurl->resContentPointer, pCurl->contentLength));
}


Handle<Value> nodeCurlGetHeader(const Arguments& args) {
return String::New(resHeader.c_str());
//Handle<Value> nodeCurlGetHeader(Handle<Object> args) {
NAN_METHOD(nodeCurlGetHeader) {
NanScope();
NanReturnValue(NanNew<String>(resHeader.c_str()));
}


Loading

0 comments on commit bd7f1b8

Please sign in to comment.