Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial release of node-zlibstream.

  • Loading branch information...
commit 960defafe339a0918d02429ba31f405f59a89b91 0 parents
@carsonmcdonald authored
3  .gitignore
@@ -0,0 +1,3 @@
+build
+.lock-wscript
+lib/zlibstream_binding.node
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Carson McDonald <carson@ioncannon.net>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 Makefile
@@ -0,0 +1,7 @@
+build:
+ node-waf build
+
+clean:
+ node-waf clean
+
+.PHONY: build clean
69 README.md
@@ -0,0 +1,69 @@
+# NAME
+
+node-zlibstream - ZLib buffered streaming for Node.js with dictionary support.
+
+# BUILD/INSTALL
+
+To build you will need to have zlib installed on your machine.
+
+The easy way to install is to use [npm](https://github.com/isaacs/npm):
+
+ npm install zlibstream
+
+To build from source:
+
+ git clone git://github.com/carsonmcdonald/node-zlibstream.git
+ cd node-zlibstream
+ ./configure
+ make
+
+# USAGE
+
+To use without a dictionary:
+
+ var Buffer = require('buffer').Buffer;
+ var ZLibStream = require('zlibstream').ZLibStream;
+
+ var zstream = new ZLibStream();
+
+ var comp1 = zstream.deflate(new Buffer('First chunk of input data'));
+ var out1 = zstream.inflate(comp1);
+ console.log(out1.toString());
+
+ var comp2 = zstream.deflate(new Buffer('Second chunk of input data'));
+ var out2 = zstream.inflate(comp2);
+ console.log(out2.toString());
+
+ zstream.resetDeflate();
+ zstream.resetInflate();
+
+To use with a dictionary:
+
+ var Buffer = require('buffer').Buffer;
+ var ZLibStream = require('zlibstream').ZLibStream;
+
+ var dictionary = new Buffer("chunkinputdataof", 'binary');
+
+ var zstream = new ZLibStream();
+
+ zstream.setInflateDictionary(dictionary);
+ zstream.setDeflateDictionary(dictionary);
+
+ var comp1 = zstream.deflate(new Buffer('First chunk of input data. More chunks and chunks of data for input.'));
+ console.log(comp1.length);
+ var out1 = zstream.inflate(comp1);
+ console.log(out1.toString());
+
+ var comp2 = zstream.deflate(new Buffer('Second chunk of input data. Lots more chunks and chunks of data.'));
+ console.log(comp2.length);
+ var out2 = zstream.inflate(comp2);
+ console.log(out2.toString());
+
+ zstream.resetDeflate();
+ zstream.resetInflate();
+
+In the dictionary example you should try commenting out the dictionary and notice the difference in the length of the compressed data. The dictionary should be made up of commonly repeated data found in the input.
+
+# LICENSE
+
+node-zlib released with an MIT license
1  index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/zlibstream');
1  lib/zlibstream.js
@@ -0,0 +1 @@
+module.exports = require('./zlibstream_binding');
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "zlibstream",
+ "description": "Streaming zlib interface.",
+ "version": "1.0.0",
+ "homepage": "https://github.com/carsonmcdonald/node-zlibstream",
+ "author": "Carson McDonald <carson@ioncannon.net>",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/carsonmcdonald/node-zlibstream.git"
+ },
+ "engines": {
+ "node": ">=0.4.0"
+ },
+ "licenses": [
+ { "type": "MIT" }
+ ],
+ "main": "./lib/zlibstream"
+}
226 src/node-zlibstream.cc
@@ -0,0 +1,226 @@
+#include <v8.h>
+#include <node.h>
+#include <node_buffer.h>
+#include <node_version.h>
+
+#include <zlib.h>
+#include <cstring>
+#include <cstdlib>
+
+#include "node-zlibstream.h"
+
+using namespace v8;
+using namespace node;
+
+#define THROW_BAD_ARGS v8::ThrowException(v8::Exception::TypeError(v8::String::New("Bad arguments")))
+#define THROW_ERROR(msg) v8::ThrowException(v8::Exception::TypeError(v8::String::New((msg == NULL ? "No know error message." : msg))))
+
+Handle<Value> ZLibStream::New(const Arguments& args)
+{
+ if (!args.IsConstructCall())
+ {
+ return FromConstructorTemplate(s_ct, args);
+ }
+
+ HandleScope scope;
+
+ ZLibStream* zlibstream = new ZLibStream();
+ zlibstream->Wrap(args.This());
+
+ return args.This();
+}
+
+Handle<Value> ZLibStream::SetInternalDictionary(const Arguments& args, enum ZLibStream::Flate flate)
+{
+ HandleScope scope;
+
+ if (args.Length() < 1 || !Buffer::HasInstance(args[0]))
+ {
+ return THROW_BAD_ARGS;
+ }
+
+ ZLibStream* zlibstream = ObjectWrap::Unwrap<ZLibStream>(args.This());
+
+ if(flate == INFLATE)
+ {
+ zlibstream->inflate_dictionary = Persistent<Object>::New(args[0]->ToObject());
+ zlibstream->inflate_dictionary_set = true;
+ }
+ else
+ {
+ Bytef* dict = (Bytef*)Buffer::Data(args[0]->ToObject());
+ int dictlen = Buffer::Length(args[0]->ToObject());
+
+ if (deflateSetDictionary(&(zlibstream->deflate_stream), dict, dictlen) != Z_OK)
+ {
+ return THROW_ERROR(zlibstream->deflate_stream.msg);
+ }
+ }
+
+ return Undefined();
+}
+
+Handle<Value> ZLibStream::InternalFlate(const Arguments& args, enum ZLibStream::Flate flate)
+{
+ HandleScope scope;
+
+ if (args.Length() < 1 || !Buffer::HasInstance(args[0]))
+ {
+ return THROW_BAD_ARGS;
+ }
+
+ ZLibStream* zlibstream = ObjectWrap::Unwrap<ZLibStream>(args.This());
+
+ int length = 0;
+ Local<Object> input = args[0]->ToObject();
+ if(flate == INFLATE)
+ {
+ zlibstream->inflate_stream.next_in = (Bytef*)Buffer::Data(input);
+ length = zlibstream->inflate_stream.avail_in = Buffer::Length(input);
+ }
+ else
+ {
+ zlibstream->deflate_stream.next_in = (Bytef*)Buffer::Data(input);
+ length = zlibstream->deflate_stream.avail_in = Buffer::Length(input);
+ }
+
+ void* result = NULL;
+ int compressed = 0, available = 0;
+
+ do
+ {
+ result = realloc(result, compressed + (flate == INFLATE ? 2 : 1) * length);
+ if (!result)
+ {
+ return THROW_ERROR("Could not allocate memory.");
+ }
+
+ int ret = -1;
+ if(flate == INFLATE)
+ {
+ zlibstream->inflate_stream.avail_out = 2 * length;
+ zlibstream->inflate_stream.next_out = (Bytef*)result + compressed;
+
+ ret = inflate(&(zlibstream->inflate_stream), Z_SYNC_FLUSH);
+ if (ret == Z_NEED_DICT)
+ {
+ if(!zlibstream->inflate_dictionary_set)
+ {
+ free(result);
+ return THROW_ERROR("A dictionary is required to inflate this stream but none was provided.");
+ }
+
+ Bytef* dict = (Bytef*)Buffer::Data(zlibstream->inflate_dictionary);
+ int dictlen = Buffer::Length(zlibstream->inflate_dictionary);
+
+ ret = inflateSetDictionary(&(zlibstream->inflate_stream), dict, dictlen);
+ if (ret != Z_OK)
+ {
+ free(result);
+ return THROW_ERROR(zError(ret));
+ }
+
+ ret = inflate(&(zlibstream->inflate_stream), Z_SYNC_FLUSH);
+ if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR)
+ {
+ free(result);
+ return THROW_ERROR(zError(ret));
+ }
+ }
+
+ compressed += (2 * length - zlibstream->inflate_stream.avail_out);
+ available = zlibstream->inflate_stream.avail_out;
+ }
+ else
+ {
+ zlibstream->deflate_stream.avail_out = length;
+ zlibstream->deflate_stream.next_out = (Bytef*)result + compressed;
+
+ ret = deflate(&(zlibstream->deflate_stream), Z_SYNC_FLUSH);
+
+ compressed += (length - zlibstream->deflate_stream.avail_out);
+ available = zlibstream->deflate_stream.avail_out;
+ }
+
+ if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR)
+ {
+ free(result);
+ return THROW_ERROR(zError(ret));
+ }
+
+ } while (available == 0);
+
+ Buffer* output = Buffer::New((char *)result, compressed);
+ free(result);
+
+ return scope.Close(Local<Value>::New(output->handle_));
+}
+
+Handle<Value> ZLibStream::ResetInternal(const Arguments& args, enum ZLibStream::Flate flate)
+{
+ HandleScope scope;
+
+ if (args.Length() != 0)
+ {
+ return THROW_BAD_ARGS;
+ }
+
+ ZLibStream* zlibstream = ObjectWrap::Unwrap<ZLibStream>(args.This());
+
+ int rc = -1;
+ if(flate == INFLATE)
+ {
+ rc = inflateReset(&(zlibstream->inflate_stream));
+
+ if(!zlibstream->inflate_dictionary_set)
+ {
+ zlibstream->inflate_dictionary.Dispose();
+ zlibstream->inflate_dictionary_set = false;
+ }
+ }
+ else
+ {
+ rc = deflateReset(&(zlibstream->deflate_stream));
+ }
+
+ if (rc != Z_OK)
+ {
+ return THROW_ERROR("Could not reset the stream.");
+ }
+
+ return Undefined();
+}
+
+void ZLibStream::Init(Handle<Object> target)
+{
+ HandleScope scope;
+
+ Local<FunctionTemplate> t = FunctionTemplate::New(New);
+
+ s_ct = Persistent<FunctionTemplate>::New(t);
+ s_ct->InstanceTemplate()->SetInternalFieldCount(1);
+ s_ct->SetClassName(String::NewSymbol("ZLibStream"));
+
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "setInflateDictionary", SetInflateDictionary);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "setDeflateDictionary", SetDeflateDictionary);
+
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "inflate", Inflate);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "deflate", Deflate);
+
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "resetInflate", ResetInflate);
+ NODE_SET_PROTOTYPE_METHOD(s_ct, "resetDeflate", ResetDeflate);
+
+ target->Set(String::NewSymbol("ZLibStream"), s_ct->GetFunction());
+}
+
+Persistent<FunctionTemplate> ZLibStream::s_ct;
+
+extern "C"
+{
+ static void init (Handle<Object> target)
+ {
+ ZLibStream::Init(target);
+ }
+
+ NODE_MODULE(zlibstream_binding, init);
+}
60 src/node-zlibstream.h
@@ -0,0 +1,60 @@
+#ifndef NODE_ZLIBSTREAM_H_
+#define NODE_ZLIBSTREAM_H_
+
+#include <node.h>
+#include <node_object_wrap.h>
+#include <v8.h>
+
+#include <zlib.h>
+
+using namespace node;
+using namespace v8;
+
+class ZLibStream : ObjectWrap
+{
+ public:
+ static Persistent<FunctionTemplate> s_ct;
+
+ static void Init(v8::Handle<v8::Object> target);
+
+ enum Flate { INFLATE, DEFLATE };
+
+ protected:
+ static v8::Handle<v8::Value> New(const v8::Arguments& args);
+ inline static v8::Handle<v8::Value> SetInflateDictionary(const v8::Arguments& args) { return SetInternalDictionary(args, INFLATE); }
+ inline static v8::Handle<v8::Value> SetDeflateDictionary(const v8::Arguments& args) { return SetInternalDictionary(args, DEFLATE); }
+ inline static v8::Handle<v8::Value> Inflate(const v8::Arguments& args) { return InternalFlate(args, INFLATE); }
+ inline static v8::Handle<v8::Value> Deflate(const v8::Arguments& args) { return InternalFlate(args, DEFLATE); }
+ inline static v8::Handle<v8::Value> ResetInflate(const v8::Arguments& args) { return ResetInternal(args, INFLATE); }
+ inline static v8::Handle<v8::Value> ResetDeflate(const v8::Arguments& args) { return ResetInternal(args, DEFLATE); }
+
+ ZLibStream() : ObjectWrap()
+ {
+ inflate_stream.zalloc = Z_NULL; inflate_stream.zfree = Z_NULL; inflate_stream.opaque = Z_NULL;
+ inflateInit(&inflate_stream);
+
+ deflate_stream.zalloc = Z_NULL; deflate_stream.zfree = Z_NULL; deflate_stream.opaque = Z_NULL;
+ deflateInit(&deflate_stream, Z_DEFAULT_COMPRESSION);
+
+ inflate_dictionary_set = false;
+ }
+
+ ~ZLibStream()
+ {
+ inflateEnd(&inflate_stream);
+ deflateEnd(&deflate_stream);
+ }
+
+ private:
+ z_stream inflate_stream;
+ z_stream deflate_stream;
+
+ Persistent<Object> inflate_dictionary;
+ bool inflate_dictionary_set;
+
+ static v8::Handle<v8::Value> SetInternalDictionary(const v8::Arguments& args, enum Flate);
+ static v8::Handle<v8::Value> ResetInternal(const v8::Arguments& args, enum Flate);
+ static v8::Handle<v8::Value> InternalFlate(const v8::Arguments& args, enum Flate);
+};
+
+#endif // NODE_ZLIBSTREAM_H_
33 wscript
@@ -0,0 +1,33 @@
+import Options
+from os.path import exists
+from shutil import copy2 as copy
+
+TARGET = 'zlibstream_binding'
+TARGET_FILE = '%s.node' % TARGET
+built = 'build/default/%s' % TARGET_FILE
+dest = 'lib/%s' % TARGET_FILE
+
+def set_options(opt):
+ opt.tool_options("compiler_cxx")
+
+def configure(conf):
+ conf.check_tool("compiler_cxx")
+ conf.check_tool("node_addon")
+ if not conf.check(lib="z", libpath=['/usr/local/lib'], uselib_store="ZLIB"):
+ conf.fatal('Missing zlib');
+
+def build(bld):
+ obj = bld.new_task_gen("cxx", "shlib", "node_addon")
+ obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"]
+ obj.target = TARGET
+ obj.source = "src/node-zlibstream.cc"
+ obj.includes = "src/*.h"
+ obj.uselib = "ZLIB"
+
+def shutdown():
+ if Options.commands['clean']:
+ if exists(TARGET_FILE):
+ unlink(TARGET_FILE)
+ else:
+ if exists(built):
+ copy(built, dest)
Please sign in to comment.
Something went wrong with that request. Please try again.