Skip to content
Closed
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
"url": "git://github.com/Topface/node-lzf.git"
},
"dependencies": {
"nan": "1.3.0"
"nan": "^2.0.9"
},
"directories": {
"lib": "./lib"
},
"main": "./index",
"scripts": {
"test": "node test/test.js"
}
},
"license": "BSD-2-Clause"
}
43 changes: 19 additions & 24 deletions src/lzf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,70 @@ using namespace node;


// Handle<Value> ThrowNodeError(const char* what = NULL) {
// return NanThrowError(Exception::Error(NanNew<String>(what)));
// return Nan::ThrowError(Exception::Error(Nan::New<String>(what)));
// }
NAN_METHOD(compress) {
if (args.Length() < 1 || !Buffer::HasInstance(args[0])) {
return NanThrowError("First argument must be a Buffer");
if (info.Length() < 1 || !Buffer::HasInstance(info[0])) {
return Nan::ThrowError("First argument must be a Buffer");
}
NanScope();

Local<Object> bufferIn = args[0]->ToObject();
Local<Object> bufferIn = info[0]->ToObject();
size_t bytesIn = Buffer::Length(bufferIn);
Copy link
Contributor

Choose a reason for hiding this comment

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

@bobrik Do you remeber, the purpose of convertion of args[0] to Object? There is size_t Length(v8::Local<v8::Value> val); and char* Data(v8::Local<v8::Value> val); which should work fine without conversion.

The question is related to the pull request because Local<Object> Value::ToObject() method is now deprecated and should be replaced with another call or removed at all.

char * dataPointer = Buffer::Data(bufferIn);
size_t bytesCompressed = bytesIn + 100;
char * bufferOut = (char*) malloc(bytesCompressed);

if (!bufferOut) {
return NanThrowError("LZF malloc failed!");
return Nan::ThrowError("LZF malloc failed!");
}

unsigned result = lzf_compress(dataPointer, bytesIn, bufferOut, bytesCompressed);

if (!result) {
free(bufferOut);
return NanThrowError("Compression failed, probably too small buffer");
return Nan::ThrowError("Compression failed, probably too small buffer");
}

Local<Object> BufferOut = NanNewBufferHandle(bufferOut, result);
free(bufferOut);
Nan::MaybeLocal<Object> BufferOut = Nan::NewBuffer(bufferOut, result);
Copy link
Contributor

Choose a reason for hiding this comment

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

Will Nan::NewBuffer() internally call realloc() for the buffer? If not, it should be called before Nan::NewBuffer(), because now the code always returns bytesIn + 100 bytes (with result bytes stored in it).


NanReturnValue(BufferOut);
info.GetReturnValue().Set(BufferOut.ToLocalChecked());
}


NAN_METHOD(decompress) {
if (args.Length() < 1 || !Buffer::HasInstance(args[0])) {
return NanThrowError("First argument must be a Buffer");
if (info.Length() < 1 || !Buffer::HasInstance(info[0])) {
return Nan::ThrowError("First argument must be a Buffer");
}

Local<Object> bufferIn = args[0]->ToObject();
Local<Object> bufferIn = info[0]->ToObject();

size_t bytesUncompressed = 999 * 1024 * 1024; // it's about max size that V8 supports

if (args.Length() > 1 && args[1]->IsNumber()) { // accept dest buffer size
bytesUncompressed = args[1]->Uint32Value();
if (info.Length() > 1 && info[1]->IsNumber()) { // accept dest buffer size
bytesUncompressed = info[1]->Uint32Value();
}


char * bufferOut = (char*) malloc(bytesUncompressed);
if (!bufferOut) {
return NanThrowError("LZF malloc failed!");
return Nan::ThrowError("LZF malloc failed!");
}

unsigned result = lzf_decompress(Buffer::Data(bufferIn), Buffer::Length(bufferIn), bufferOut, bytesUncompressed);

if (!result) {
return NanThrowError("Unrompression failed, probably too small buffer");
return Nan::ThrowError("Unrompression failed, probably too small buffer");
}

Local<Object> BufferOut = NanNewBufferHandle(bufferOut, result);
Nan::MaybeLocal<Object> BufferOut = Nan::NewBuffer(bufferOut, result);

free(bufferOut);

NanScope();
NanReturnValue(BufferOut);
info.GetReturnValue().Set(BufferOut.ToLocalChecked());
}

extern "C" void
init (Handle<Object> target) {
NODE_SET_METHOD(target, "compress", compress);
NODE_SET_METHOD(target, "decompress", decompress);
Nan::SetMethod(target, "compress", compress);
Nan::SetMethod(target, "decompress", decompress);
}

NODE_MODULE(lzf, init)
4 changes: 3 additions & 1 deletion src/lzf/lzfP.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ using namespace std;
# if defined (WIN32)
# define LZF_USE_OFFSETS defined(_M_X64)
# else
# if __cplusplus > 199711L
# if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_8
# include <tr1/cstdint>
# elif __cplusplus > 199711L
# include <cstdint>
# else
# include <stdint.h>
Expand Down