-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 960defa
Showing
10 changed files
with
438 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
build | |||
.lock-wscript | |||
lib/zlibstream_binding.node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
build: | |||
node-waf build | |||
|
|||
clean: | |||
node-waf clean | |||
|
|||
.PHONY: build clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
module.exports = require('./lib/zlibstream'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
module.exports = require('./zlibstream_binding'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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" | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} |
Oops, something went wrong.