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
38 changes: 23 additions & 15 deletions compress.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <node.h>
#include <node_events.h>
#include <node_object_wrap.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
Expand All @@ -11,21 +11,23 @@ using namespace v8;
using namespace node;


class Gzip : public EventEmitter {
class Gzip : public ObjectWrap {
public:
static Persistent<FunctionTemplate> s_ct;
static void
Initialize (v8::Handle<v8::Object> target)
{
HandleScope scope;

Local<FunctionTemplate> t = FunctionTemplate::New(New);

t->Inherit(EventEmitter::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);
s_ct = Persistent<FunctionTemplate>::New(t);
s_ct->InstanceTemplate()->SetInternalFieldCount(1);
s_ct->SetClassName(String::NewSymbol("Gzip"));

NODE_SET_PROTOTYPE_METHOD(t, "init", GzipInit);
NODE_SET_PROTOTYPE_METHOD(t, "deflate", GzipDeflate);
NODE_SET_PROTOTYPE_METHOD(t, "end", GzipEnd);
NODE_SET_PROTOTYPE_METHOD(s_ct, "init", GzipInit);
NODE_SET_PROTOTYPE_METHOD(s_ct, "deflate", GzipDeflate);
NODE_SET_PROTOTYPE_METHOD(s_ct, "end", GzipEnd);

target->Set(String::NewSymbol("Gzip"), t->GetFunction());
}
Expand Down Expand Up @@ -195,7 +197,7 @@ class Gzip : public EventEmitter {
}


Gzip () : EventEmitter ()
Gzip () : ObjectWrap ()
{
}

Expand All @@ -209,21 +211,25 @@ class Gzip : public EventEmitter {
};


class Gunzip : public EventEmitter {
Persistent<FunctionTemplate> Gzip::s_ct;

class Gunzip : public ObjectWrap {
public:
static Persistent<FunctionTemplate> s_ct;
static void
Initialize (v8::Handle<v8::Object> target)
{
HandleScope scope;

Local<FunctionTemplate> t = FunctionTemplate::New(New);

t->Inherit(EventEmitter::constructor_template);
t->InstanceTemplate()->SetInternalFieldCount(1);
s_ct = Persistent<FunctionTemplate>::New(t);
s_ct->InstanceTemplate()->SetInternalFieldCount(1);
s_ct->SetClassName(String::NewSymbol("Gunzip"));

NODE_SET_PROTOTYPE_METHOD(t, "init", GunzipInit);
NODE_SET_PROTOTYPE_METHOD(t, "inflate", GunzipInflate);
NODE_SET_PROTOTYPE_METHOD(t, "end", GunzipEnd);
NODE_SET_PROTOTYPE_METHOD(s_ct, "init", GunzipInit);
NODE_SET_PROTOTYPE_METHOD(s_ct, "inflate", GunzipInflate);
NODE_SET_PROTOTYPE_METHOD(s_ct, "end", GunzipEnd);

target->Set(String::NewSymbol("Gunzip"), t->GetFunction());
}
Expand Down Expand Up @@ -354,7 +360,7 @@ class Gunzip : public EventEmitter {
return scope.Close(String::New(""));
}

Gunzip () : EventEmitter ()
Gunzip () : ObjectWrap ()
{
}

Expand All @@ -368,6 +374,8 @@ class Gunzip : public EventEmitter {

};

Persistent<FunctionTemplate> Gunzip::s_ct;

extern "C" void
init (Handle<Object> target)
{
Expand Down
30 changes: 15 additions & 15 deletions filetest.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
var compress=require("./compress");
var sys=require("sys");
var posix=require("posix");
var util=require("util");
var fs=require("fs");

// Read in our test file
var data=posix.cat("filetest.js", encoding="binary").wait();
sys.puts("Got : "+data.length);
var data = fs.readFileSync("filetest.js", "binary");
util.puts("Got : "+data.length);

// Set output file
var fd = posix.open("filetest.js.gz", process.O_WRONLY | process.O_TRUNC | process.O_CREAT, 0644).wait();
sys.puts("Openned file");
var fd = fs.openSync("filetest.js.gz", 'w', 0644);
util.puts("Openned file");

// Create gzip stream
var gzip=new compress.Gzip;
gzip.init();

// Pump data to be compressed
gzdata=gzip.deflate(data, "binary"); // Do this as many times as required
sys.puts("Compressed size : "+gzdata.length);
posix.write(fd, gzdata, encoding="binary").wait();
util.puts("Compressed size : "+gzdata.length);
var pos = fs.writeSync(fd, gzdata, 0, "binary");

// Get the last bit
gzlast=gzip.end();
sys.puts("Last bit : "+gzlast.length);
posix.write(fd, gzlast, encoding="binary").wait();
posix.close(fd).wait();
sys.puts("File closed");
util.puts("Last bit : "+gzlast.length);
fs.writeSync(fd, gzlast, pos, "binary");
fs.closeSync(fd);
util.puts("File closed");

// See if we can uncompress it ok
var gunzip=new compress.Gunzip;
gunzip.init();
var testdata = posix.cat("filetest.js.gz", encoding="binary").wait();
sys.puts("Test opened : "+testdata.length);
sys.puts(gunzip.inflate(testdata, "binary").length);
var testdata = fs.readFileSync("filetest.js.gz", "binary");
util.puts("Test opened : "+testdata.length);
util.puts(gunzip.inflate(testdata, "binary").length);
gunzip.end();


Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "compress",
"description": "A streaming compression / gzip library for node.js",
"version": "0.1.9",
"author": "Rhys",
"repository": {
"type": "git",
"url": "http://github.com/waveto/node-compress.git"
},
"scripts": {
"pre-install": "node-waf configure && node-waf"
},
"engine": [ "node >=0.2.5" ],
"main": "./build/default/compress"
}
14 changes: 7 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
var compress=require("./compress");
var sys=require("sys");
var posix=require("posix");
var util=require("util");
var fs=require("fs");

// Create gzip stream
var gzip=new compress.Gzip;
gzip.init();

// Pump data to be compressed
var gzdata1 = gzip.deflate("My data that needs ", "binary");
sys.puts("Compressed size : "+gzdata1.length);
util.puts("Compressed size : "+gzdata1.length);

var gzdata2 = gzip.deflate("to be compressed. 01234567890.", "binary");
sys.puts("Compressed size : "+gzdata2.length);
util.puts("Compressed size : "+gzdata2.length);

var gzdata3=gzip.end();
sys.puts("Last bit : "+gzdata3.length);
util.puts("Last bit : "+gzdata3.length);

// Take the output stream, and chop it up into two
var gzdata = gzdata1+gzdata2+gzdata3;
sys.puts("Total compressed size : "+gzdata.length);
util.puts("Total compressed size : "+gzdata.length);
var d1 = gzdata.substr(0, 25);
var d2 = gzdata.substr(25);

Expand All @@ -29,7 +29,7 @@ var data1 = gunzip.inflate(d1, "binary");
var data2 = gunzip.inflate(d2, "binary");
var data3 = gunzip.end();

sys.puts(data1+data2+data3);
util.puts(data1+data2+data3);



Expand Down
11 changes: 8 additions & 3 deletions wscript
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Options
import os
from os import unlink, symlink, popen
from os.path import exists
from os.path import exists
from os import mkdir
from os.path import abspath

srcdir = "."
blddir = "build"
Expand Down Expand Up @@ -29,5 +32,7 @@ def shutdown():
if Options.commands['clean']:
if exists('compress.node'): unlink('compress.node')
else:
if exists('build/default/compress.node') and not exists('compress.node'):
symlink('build/default/compress.node', 'compress.node')
if not exists('build/default'):
mkdir('build/default')
if exists('build/Release/compress.node') and not exists('build/default/compress.node'):
symlink(os.getcwd()+'/build/Release/compress.node', 'build/default/compress.node')