Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules/
npm-debug.log
build
.idea
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Updated to compile under all versions of NodeJS
Forked from https://github.com/pingjiang/node-rar.

I needed this module for nodejs >= 0.12. My pull request has been in for many months
with no update and I believe the project may be abandoned, so I published this under
an updated name so I could continue to use it in my projects. If the pull request is
accepted in the original project, or the original project is updated, I will remove
this package and repository. The module now works under all versions of NodeJS.

# Node Rar Addon [![Build Status](https://secure.travis-ci.org/pingjiang/node-rar.png?branch=master)](http://travis-ci.org/pingjiang/node-rar)

Node Rar Addon for reading RAR archives using the bundled UnRAR library.
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{
"name": "node-rar",
"version": "0.0.2",
"name": "node-rar-updated",
"version": "0.0.1",
"main": "lib/rar.js",
"description": "Node Rar Addon for reading RAR archives using the bundled UnRAR library.",
"homepage": "https://github.com/pingjiang/node-rar",
"bugs": "https://github.com/pingjiang/node-rar/issues",
"description": ["Updated Node Rar Addon for reading RAR archives using the ",
"bundled UnRAR library. Works with all versions of NodeJS. Forked from original ",
"node-rar at https://github.com/pingjiang/node-rar"],
"homepage": "https://github.com/davidcroda/node-rar",
"bugs": "https://github.com/davidcroda/node-rar/issues",
"author": {
"name": "pingjiang",
"email": "pingjiang1989@gmail.com",
"url": "https://github.com/pingjiang"
"name": "davidcroda",
"email": "davidcroda@gmail.com",
"url": "https://github.com/davidcroda"
},
"repository": {
"type": "git",
"url": "https://github.com/pingjiang/node-rar"
"url": "https://github.com/davidcroda/node-rar"
},
"license": "MIT",
"keywords": [
Expand Down
93 changes: 46 additions & 47 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#include <v8.h>
#include "rar.hpp"
#include <iostream>
#include <nan.h>

#ifdef _DEBUG
#define _D(msg) do {\
std::cout << __FILE__ << ":" << __LINE__ << ">> " << msg << std::endl;\
} while(0)

#else

#define _D(msg)
Expand All @@ -30,18 +31,18 @@ int _processArchive(int mode, int op, char* filepath, char* toDir, char* passwor
reset_RAROpenArchiveDataEx(&archiveData);
archiveData.ArcName = filepath;
archiveData.OpenMode = mode;

HANDLE handler = RAROpenArchiveEx(&archiveData);
if (archiveData.OpenResult != ERAR_SUCCESS) {
_D("open archive error: " << archiveData.OpenResult);
return archiveData.OpenResult;
}
if (password != NULL) {

if (password != NULL) {
_D("password: " << password);
RARSetPassword(handler, password);
}

int result = 0;
while (result == 0) {
struct RARHeaderDataEx entry;
Expand All @@ -52,13 +53,13 @@ int _processArchive(int mode, int op, char* filepath, char* toDir, char* passwor
}
if (result != 0)
break;
Local<Object> entryObj = Object::New();
entryObj->Set(String::NewSymbol("FileName"), String::New(entry.FileName));
Local<Object> entryObj = NanNew<Object>();
entryObj->Set(NanNew<String>("FileName"), NanNew<String>(entry.FileName));
_D("FileName: " << entry.FileName);
if (!cb.IsEmpty()) {
const unsigned argc = 1;
Local<Value> argv[argc] = { entryObj };
cb->Call(Context::GetCurrent()->Global(), argc, argv);
NanMakeCallback(NanGetCurrentContext()->Global(), cb, argc, argv);
} else {
_D("cb is empty");
}
Expand All @@ -70,85 +71,83 @@ int _processArchive(int mode, int op, char* filepath, char* toDir, char* passwor
return result;
}

Handle<Value> DUMMY(const Arguments& args) {
HandleScope scope;

return scope.Close(Undefined());
NAN_METHOD(DUMMY) {
NanScope();
NanReturnUndefined();
}

// processArchive(options, cb)
Handle<Value> processArchive(const Arguments& args) {
HandleScope scope;

NAN_METHOD(processArchive) {
NanScope();

if (args.Length() < 1) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
NanThrowError("Wrong arguments");
NanReturnUndefined();
}

int openMode = 0;
bool isTest = false;
Local<Object> options = args[0]->IsString() ? Object::New() : args[0]->ToObject();
Local<Object> options = args[0]->IsString() ? NanNew<Object>() : args[0]->ToObject();
if (args[0]->IsString()) {
options->Set(String::NewSymbol("filepath"), args[0]->ToString());
options->Set(NanNew<String>("filepath"), args[0]->ToString());
}
Local<Value> openModeValue = options->Get(String::NewSymbol("openMode"));
Local<Value> openModeValue = options->Get(NanNew<String>("openMode"));
if (openModeValue->IsNumber()) {
openMode = openModeValue->NumberValue();
}
Local<Value> filepathValue = options->Get(String::NewSymbol("filepath"));
Local<Value> filepathValue = options->Get(NanNew<String>("filepath"));
if (!filepathValue->IsString()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments `filepath`")));
return scope.Close(Undefined());
NanThrowError("Wrong arguments `filepath`");
NanReturnUndefined();
}
String::Utf8Value value(filepathValue);
const char* filepathStr = (const char*)*value;
char archiveFilePath[2048];
char archiveFilePath[2048];
strncpy(archiveFilePath, filepathStr, 2048);
Local<Value> passwordValue = options->Get(String::NewSymbol("password"));

Local<Value> passwordValue = options->Get(NanNew<String>("password"));
char passwordBuf[128];
if (passwordValue->IsString()) {
String::Utf8Value value1(passwordValue);
const char* passwordStr = (const char*)*value1;
strncpy(passwordBuf, passwordStr, 128);
strncpy(passwordBuf, passwordStr, 128);
}
Local<Function> cb = (args.Length()> 1 && args[1]->IsFunction()) ? Local<Function>::Cast(args[1]) : FunctionTemplate::New(DUMMY)->GetFunction();
Local<Value> isTestValue = options->Get(String::NewSymbol("test"));
Local<Function> cb = (args.Length() > 1 && args[1]->IsFunction()) ? args[1].As<Function>() : NanNew<FunctionTemplate>(DUMMY)->GetFunction();

Local<Value> isTestValue = options->Get(NanNew<String>("test"));
if (!isTestValue->IsUndefined()) {
isTest = true;
}
char toDirBuf[1024] = { 0 };
Local<Value> toDirValue = options->Get(String::NewSymbol("toDir"));

char toDirBuf[1024] = { 0 };
Local<Value> toDirValue = options->Get(NanNew<String>("toDir"));
if (openMode == 1) {
if (toDirValue->IsString()) {
if (toDirValue->IsString()) {
String::Utf8Value value2(toDirValue);
const char* toDirStr = (const char*)*value2;
strncpy(toDirBuf, toDirStr, 1024);
strncpy(toDirBuf, toDirStr, 1024);
} else {
if (!isTest) {
ThrowException(Exception::TypeError(String::New("Wrong arguments `toDir` for extract mode")));
return scope.Close(Undefined());
NanThrowError("Wrong arguments `toDir` for extract mode");
NanReturnUndefined();
}
}
}
int ret = _processArchive(openMode, isTest ? 1 : (openMode == 0 ? 0 : 2), archiveFilePath,
toDirValue->IsString() ? toDirBuf : NULL,

int ret = _processArchive(openMode, isTest ? 1 : (openMode == 0 ? 0 : 2), archiveFilePath,
toDirValue->IsString() ? toDirBuf : NULL,
passwordValue->IsString() ? passwordBuf : NULL, cb);

if (ret != 0) {
ThrowException(Exception::Error(String::New("Process archive error")));
NanThrowError("Process archive error");
_D("error code is " << ret);
}
return scope.Close(Undefined());

NanReturnUndefined();
}

void init(Handle<Object> exports) {
setlocale(LC_ALL,"");

exports->Set(String::NewSymbol("processArchive"), FunctionTemplate::New(processArchive)->GetFunction());
exports->Set(NanNew<String>("processArchive"), NanNew<FunctionTemplate>(processArchive)->GetFunction());
}

NODE_MODULE(unrar, init);