Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 9cf5098046c86c2b24601acbe9c5d56ae2326560 @kkaefer committed Mar 11, 2011
Showing with 137 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +15 −0 Makefile
  3. +1 −0 index.js
  4. +1 −0 lib/zlib.js
  5. +86 −0 src/node-zlib.cc
  6. +32 −0 wscript
2 .gitignore
@@ -0,0 +1,2 @@
+build
+.lock-wscript
15 Makefile
@@ -0,0 +1,15 @@
+build:
+ node-waf build
+
+clean:
+ node-waf clean
+
+ifndef only
+test: build
+ @expresso -I lib test/*.test.js
+else
+test: build
+ @expresso -I lib test/${only}.test.js
+endif
+
+.PHONY: build clean test
1 index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/zlib');
1 lib/zlib.js
@@ -0,0 +1 @@
+module.exports = require('./zlib_bindings');
86 src/node-zlib.cc
@@ -0,0 +1,86 @@
+#include <v8.h>
+#include <node.h>
+#include <node_buffer.h>
+#include <node_version.h>
+
+#include <zlib.h>
+
+using namespace v8;
+using namespace node;
+
+z_stream stream;
+
+class ZLib {
+public:
+
+ static Handle<Value> Error() {
+ if (stream.msg) {
+ return ThrowException(Exception::Error(String::New(stream.msg)));
+ }
+ else {
+ return ThrowException(Exception::Error(String::New("Unknown error")));
+ }
+ }
+
+ static Handle<Value> Error(const char* msg) {
+ return ThrowException(Exception::Error(String::New(msg)));
+ }
+
+ static Handle<Value> Deflate(const Arguments& args) {
+ HandleScope scope;
+
+ if (args.Length() < 1 || !Buffer::HasInstance(args[0])) {
+ return Error("Expected Buffer as first argument");
+ }
+
+#if NODE_VERSION_AT_LEAST(0,3,0)
+ Local<Object> buffer = args[0]->ToObject();
+ stream.next_in = (Bytef*)Buffer::Data(buffer);
+ int length = stream.avail_in = Buffer::Length(buffer);
+#else
+ Buffer* buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
+ stream.next_in = (Bytef*)buffer->data();
+ int length = stream.avail_in = buffer->length();
+#endif
+
+ int status;
+ char* result = NULL;
+
+ int compressed = 0;
+ do {
+ result = (char*)realloc(result, compressed + length);
+ if (!result) return Error("Failed to allocate memory");
+
+ stream.avail_out = length;
+ stream.next_out = (Bytef*)result + compressed;
+
+ status = deflate(&stream, Z_FINISH);
+ if (status != Z_STREAM_END && status != Z_OK) return Error();
+
+ compressed += (length - stream.avail_out);
+ } while (stream.avail_out == 0);
+
+ status = deflateReset(&stream);
+ if (status != Z_OK) return Error();
+
+#if NODE_VERSION_AT_LEAST(0,3,0)
+ Buffer* resultBuffer = Buffer::New(result, compressed);
+#else
+ Buffer *resultBuffer = Buffer::New(compressed);
+ memcpy(buffer->data(), result, compressed);
+#endif
+ free(result);
+
+ return scope.Close(Local<Value>::New(resultBuffer->handle_));
+ }
+};
+
+
+extern "C" void init (v8::Handle<Object> target) {
+ stream.zalloc = Z_NULL;
+ stream.zfree = Z_NULL;
+ stream.opaque = Z_NULL;
+ deflateInit(&stream, Z_DEFAULT_COMPRESSION);
+
+ NODE_SET_METHOD(target, "deflate", ZLib::Deflate);
+}
32 wscript
@@ -0,0 +1,32 @@
+import Options
+from os.path import exists
+from shutil import copy2 as copy
+
+TARGET = 'zlib_bindings'
+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-zlib.cc"
+ obj.uselib = "ZLIB"
+
+def shutdown():
+ if Options.commands['clean']:
+ if exists(TARGET_FILE):
+ unlink(TARGET_FILE)
+ else:
+ if exists(built):
+ copy(built, dest)

0 comments on commit 9cf5098

Please sign in to comment.