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

Nan #14

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Nan #14

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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"url": "git://github.com/Topface/node-lzf.git"
},
"dependencies": {
"nan": "^2.0.9"
"nan": "^2.18.0"
},
"directories": {
"lib": "./lib"
Expand Down
21 changes: 13 additions & 8 deletions src/lzf.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* node-lzf (C) 2011 Ian Babrou <[email protected]> */
/* node-lzf (C) 2024 Maintained Türkay Tanrikulu <[email protected]> */

#include <node_buffer.h>
#include <stdlib.h>
Expand All @@ -11,11 +12,9 @@

#include "lzf/lzf.h"


using namespace v8;
using namespace node;


// Handle<Value> ThrowNodeError(const char* what = NULL) {
// return Nan::ThrowError(Exception::Error(Nan::New<String>(what)));
// }
Expand All @@ -27,7 +26,7 @@ NAN_METHOD(compress) {
Local<Value> bufferIn = info[0];
size_t bytesIn = Buffer::Length(bufferIn);
char * dataPointer = Buffer::Data(bufferIn);
size_t bytesCompressed = bytesIn + 100;
size_t bytesCompressed = bytesIn + (bytesIn / 16) + 64 + 3;
char * bufferOut = (char*) malloc(bytesCompressed);

if (!bufferOut) {
Expand Down Expand Up @@ -58,7 +57,7 @@ NAN_METHOD(decompress) {
size_t bytesUncompressed = 999 * 1024 * 1024; // it's about max size that V8 supports

if (info.Length() > 1 && info[1]->IsNumber()) { // accept dest buffer size
bytesUncompressed = info[1]->Uint32Value();
bytesUncompressed = Nan::To<uint32_t>(info[1]).FromJust();
}


Expand All @@ -79,10 +78,16 @@ NAN_METHOD(decompress) {
info.GetReturnValue().Set(BufferOut.ToLocalChecked());
}

extern "C" void
init (Handle<Object> target) {
Nan::SetMethod(target, "compress", compress);
Nan::SetMethod(target, "decompress", decompress);
extern "C" void init(Local<Object> exports, Local<Value> module, Local<Context> context) {
Nan::HandleScope scope;

if (!exports->IsObject() || exports->IsNull()) {
Nan::ThrowTypeError("Target object is not valid");
return;
}

Nan::SetMethod(exports.As<Object>(), "compress", compress);
Nan::SetMethod(exports.As<Object>(), "decompress", decompress);
}

NODE_MODULE(lzf, init)