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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
.lock-wscript
lib/*.node
.lock-wscript
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
lib/*.node
.lock-wscript
44 changes: 27 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
{
"name": "zlib",
"description": "Simple, synchronous deflate/inflate for buffers",
"version": "1.0.3",
"homepage": "https://github.com/kkaefer/node-zlib",
"author": "Konstantin Käfer <kkaefer@gmail.com>",
"repository": {
"type": "git",
"url": "git://github.com/kkaefer/node-zlib.git"
},
"engines": {
"node": ">=0.2.0"
},
"licenses": [
{ "type": "BSD" }
],
"main": "./lib/zlib"
}
"name": "zlibcontext",
"description": "Simple, synchronous deflate/inflate for buffers (ZLibContext modification)",
"version": "1.0.9",
"homepage": "https://github.com/kkaefer/node-zlib",
"author": "Fedor Indutny <fedor.indutny@gmail.com>",
"contributors": [
"Konstantin Käfer <kkaefer@gmail.com>"
],
"repository": {
"type": "git",
"url": "git://github.com/kkaefer/node-zlib.git"
},
"engines": {
"node": ">=0.6.0"
},
"licenses": [
{
"type": "BSD"
}
],
"scripts": {
"install": "./configure && make"
},
"main": "./lib/zlib",
"dependencies": {},
"devDependencies": {}
}
95 changes: 0 additions & 95 deletions src/node-zlib.cc

This file was deleted.

146 changes: 146 additions & 0 deletions src/node_zlib.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#include <v8.h>
#include <node.h>
#include <node_buffer.h>

#include <zlib.h>
#include <cstring>
#include <cstdlib>

#include "node_zlib.h"

using namespace v8;
using namespace node;

Persistent<FunctionTemplate> ZLibContext::constructor_template;

void ZLibContext::Initialize(Handle<Object> target) {
Local<FunctionTemplate> t = FunctionTemplate::New(ZLibContext::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("ZLibContext"));

// Instance methods
NODE_SET_PROTOTYPE_METHOD(constructor_template, "deflate",
ZLibContext::Deflate);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "inflate",
ZLibContext::Inflate);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "resetDeflate",
ZLibContext::ResetDeflate);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "resetInflate",
ZLibContext::ResetInflate);

target->Set(String::NewSymbol("ZLibContext"), constructor_template->GetFunction());
};

Handle<Value> ZLibContext::New(const Arguments &args) {
if (!args.IsConstructCall()) {
return FromConstructorTemplate(constructor_template, args);
}

HandleScope scope;

ZLibContext *z = new ZLibContext();
z->Wrap(args.This());

// First argument can be initial dictionary
if (args.Length() >= 1 && Buffer::HasInstance(args[0])) {
Local<Object> dict = args[0]->ToObject();
z->pdict = Persistent<Object>::New(dict);
z->bdict = (Bytef*)Buffer::Data(dict);
z->dictLen = Buffer::Length(dict);
z->hasDict = true;
}

return args.This();
};

inline Handle<Value> ZLib_error(const char* msg = NULL) {
return ThrowException(Exception::Error(
String::New(msg ? msg : "Unknown Error")));
};

#define ZLib_Xreset(Method, method) \
Handle<Value> ZLibContext::Reset##Method(const Arguments& args) { \
HandleScope scope; \
ZLibContext *z = ObjectWrap::Unwrap<ZLibContext>(args.This()); \
\
if (method##Reset(&(z->method##_s)) != Z_OK) { \
return ZLib_error("ZLib stream is beyond repair"); \
} \
\
z->dictSet = false; \
return scope.Close(Boolean::New(true)); \
}

#define ZLib_Xflate(Method, method, factor) \
Handle<Value> ZLibContext::Method(const Arguments& args) { \
HandleScope scope; \
\
if (args.Length() < 1 || !Buffer::HasInstance(args[0])) { \
return ZLib_error("Expected Buffer as first argument"); \
} \
\
Local<Object> input = args[0]->ToObject(); \
\
ZLibContext *z = ObjectWrap::Unwrap<ZLibContext>(args.This()); \
\
int ret; \
if (factor == 1 && z->hasDict && !z->dictSet) { \
ret = method##SetDictionary(&(z->method##_s), z->bdict, z->dictLen); \
\
if (ret != Z_OK) { \
return ZLib_error("Failed to set dictionary!"); \
} \
\
z->dictSet = true; \
} \
\
z->method##_s.next_in = (Bytef*)Buffer::Data(input); \
int length = z->method##_s.avail_in = Buffer::Length(input); \
\
char* result = NULL; \
\
int compressed = 0; \
do { \
result = (char*)realloc(result, compressed + factor * length); \
if (!result) return ZLib_error("Could not allocate memory"); \
\
z->method##_s.avail_out = factor * length; \
z->method##_s.next_out = (Bytef*)result + compressed; \
\
ret = method(&(z->method##_s), Z_SYNC_FLUSH); \
\
if (ret == Z_NEED_DICT) { \
if (!z->hasDict) { \
free(result); \
return ZLib_error("Dictionary is required"); \
} \
ret = method##SetDictionary(&(z->method##_s), z->bdict, \
z->dictLen); \
\
if (ret != Z_OK) { \
return ZLib_error("Failed to set dictionary"); \
} \
\
ret = method(&(z->method##_s), Z_SYNC_FLUSH); \
} \
\
if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) { \
free(result); \
return ZLib_error(z->method##_s.msg); \
} \
\
compressed += (factor * length - z->method##_s.avail_out); \
} while (z->method##_s.avail_out == 0); \
\
Buffer* output = Buffer::New(result, compressed); \
free(result); \
return scope.Close(Local<Value>::New(output->handle_)); \
}

ZLib_Xflate(Deflate, deflate, 1);
ZLib_Xflate(Inflate, inflate, 2);
ZLib_Xreset(Deflate, deflate);
ZLib_Xreset(Inflate, inflate);

NODE_MODULE(zlib_bindings, ZLibContext::Initialize);
58 changes: 58 additions & 0 deletions src/node_zlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* ZLib binding for node.js
*/
#ifndef NODE_ZLIB_H
#define NODE_ZLIB_H

#include <node.h>
#include <node_object_wrap.h>
#include <v8.h>

#include <zlib.h>

using namespace node;
using namespace v8;

class ZLibContext : public ObjectWrap {
public:

static void Initialize(Handle<Object> target);

static Persistent<FunctionTemplate> constructor_template;

protected:
ZLibContext() : ObjectWrap(), deflate_s(), inflate_s() {
deflate_s.zalloc = inflate_s.zalloc = Z_NULL;
deflate_s.zfree = inflate_s.zfree = Z_NULL;
deflate_s.opaque = inflate_s.opaque = Z_NULL;

deflateInit(&deflate_s, Z_DEFAULT_COMPRESSION);
inflateInit(&inflate_s);

dictSet = hasDict = false;
dictLen = 0;
}

~ZLibContext() { }


static Handle<Value> New(const Arguments &args);

static Handle<Value> Deflate(const Arguments &args);
static Handle<Value> Inflate(const Arguments &args);

static Handle<Value> ResetDeflate(const Arguments &args);
static Handle<Value> ResetInflate(const Arguments &args);

z_stream deflate_s;
z_stream inflate_s;

bool hasDict;
bool dictSet;
int dictLen;
Bytef* bdict;
Persistent<Object> pdict;

};

#endif // NODE_ZLIB_H
2 changes: 1 addition & 1 deletion test/deflate.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/inflate.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading