Skip to content

Commit 2b2ef87

Browse files
committed
Initial commit
0 parents  commit 2b2ef87

19 files changed

+1808
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.DS_Store

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# browserify-zlib
2+
3+
Emulates Node's [zlib](http://nodejs.org/api/zlib.html) module for [Browserify](http://browserify.org)
4+
using [pako](https://github.com/nodeca/pako). It uses the actual Node source code and passes the Node zlib tests
5+
by emulating the C++ binding that actually calls zlib.
6+
7+
## License
8+
9+
MIT

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "browserify-zlib",
3+
"version": "0.1.0",
4+
"description": "Full zlib module for browserify",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "test"
8+
},
9+
"dependencies": {
10+
"pako": "~0.1.1"
11+
},
12+
"devDependencies": {
13+
"tape": "^2.12.3",
14+
"brfs": "^1.0.1"
15+
},
16+
"testling": {
17+
"files": "test/*.js",
18+
"browsers": [
19+
"ie/6..latest",
20+
"chrome/22..latest",
21+
"firefox/16..latest",
22+
"safari/latest",
23+
"opera/11.0..latest",
24+
"iphone/6",
25+
"ipad/6",
26+
"android-browser/latest"
27+
]
28+
},
29+
"browserify": {
30+
"transform": [
31+
"brfs"
32+
]
33+
},
34+
"scripts": {
35+
"test": "node_modules/tape/bin/tape test/*.js"
36+
},
37+
"main": "src/index.js",
38+
"author": "Devon Govett <[email protected]>",
39+
"license": "MIT"
40+
}

src/binding.js

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
var msg = require('pako/lib/zlib/messages');
2+
var zstream = require('pako/lib/zlib/zstream');
3+
var zlib_deflate = require('pako/lib/zlib/deflate.js');
4+
var zlib_inflate = require('pako/lib/zlib/inflate.js');
5+
var constants = require('pako/lib/zlib/constants');
6+
7+
for (var key in constants) {
8+
exports[key] = constants[key];
9+
}
10+
11+
// zlib modes
12+
exports.NONE = 0;
13+
exports.DEFLATE = 1;
14+
exports.INFLATE = 2;
15+
exports.GZIP = 3;
16+
exports.GUNZIP = 4;
17+
exports.DEFLATERAW = 5;
18+
exports.INFLATERAW = 6;
19+
exports.UNZIP = 7;
20+
21+
function Zlib(mode) {
22+
if (mode < exports.DEFLATE || mode > exports.UNZIP)
23+
throw new TypeError("Bad argument");
24+
25+
this.mode = mode;
26+
this.init_done = false;
27+
this.write_in_progress = false;
28+
this.pending_close = false;
29+
this.windowBits = 0;
30+
this.level = 0;
31+
this.memLevel = 0;
32+
this.strategy = 0;
33+
this.dictionary = null;
34+
}
35+
36+
Zlib.prototype.init = function(windowBits, level, memLevel, strategy, dictionary) {
37+
this.windowBits = windowBits;
38+
this.level = level;
39+
this.memLevel = memLevel;
40+
this.strategy = strategy;
41+
// dictionary not supported.
42+
43+
if (this.mode === exports.GZIP || this.mode === exports.GUNZIP)
44+
this.windowBits += 16;
45+
46+
if (this.mode === exports.UNZIP)
47+
this.windowBits += 32;
48+
49+
if (this.mode === exports.DEFLATERAW || this.mode === exports.INFLATERAW)
50+
this.windowBits = -this.windowBits;
51+
52+
this.strm = new zstream();
53+
54+
switch (this.mode) {
55+
case exports.DEFLATE:
56+
case exports.GZIP:
57+
case exports.DEFLATERAW:
58+
var status = zlib_deflate.deflateInit2(
59+
this.strm,
60+
this.level,
61+
exports.Z_DEFLATED,
62+
this.windowBits,
63+
this.memLevel,
64+
this.strategy
65+
);
66+
break;
67+
case exports.INFLATE:
68+
case exports.GUNZIP:
69+
case exports.INFLATERAW:
70+
case exports.UNZIP:
71+
var status = zlib_inflate.inflateInit2(
72+
this.strm,
73+
this.windowBits
74+
);
75+
break;
76+
default:
77+
throw new Error("Unknown mode " + this.mode);
78+
}
79+
80+
if (status !== exports.Z_OK) {
81+
this._error(status);
82+
return;
83+
}
84+
85+
this.write_in_progress = false;
86+
this.init_done = true;
87+
};
88+
89+
Zlib.prototype.params = function() {
90+
throw new Error("deflateParams Not supported");
91+
};
92+
93+
Zlib.prototype._writeCheck = function() {
94+
if (!this.init_done)
95+
throw new Error("write before init");
96+
97+
if (this.mode === exports.NONE)
98+
throw new Error("already finalized");
99+
100+
if (this.write_in_progress)
101+
throw new Error("write already in progress");
102+
103+
if (this.pending_close)
104+
throw new Error("close is pending");
105+
};
106+
107+
Zlib.prototype.write = function(flush, input, in_off, in_len, out, out_off, out_len) {
108+
this._writeCheck();
109+
this.write_in_progress = true;
110+
111+
var self = this;
112+
process.nextTick(function() {
113+
self.write_in_progress = false;
114+
var res = self._write(flush, input, in_off, in_len, out, out_off, out_len);
115+
self.callback(res[0], res[1]);
116+
117+
if (self.pending_close)
118+
self.close();
119+
});
120+
121+
return this;
122+
};
123+
124+
// set method for Node buffers, used by pako
125+
function bufferSet(data, offset) {
126+
for (var i = 0; i < data.length; i++) {
127+
this[offset + i] = data[i];
128+
}
129+
}
130+
131+
Zlib.prototype.writeSync = function(flush, input, in_off, in_len, out, out_off, out_len) {
132+
this._writeCheck();
133+
return this._write(flush, input, in_off, in_len, out, out_off, out_len);
134+
};
135+
136+
Zlib.prototype._write = function(flush, input, in_off, in_len, out, out_off, out_len) {
137+
this.write_in_progress = true;
138+
139+
if (flush !== exports.Z_NO_FLUSH &&
140+
flush !== exports.Z_PARTIAL_FLUSH &&
141+
flush !== exports.Z_SYNC_FLUSH &&
142+
flush !== exports.Z_FULL_FLUSH &&
143+
flush !== exports.Z_FINISH &&
144+
flush !== exports.Z_BLOCK) {
145+
throw new Error("Invalid flush value");
146+
}
147+
148+
if (input == null) {
149+
input = new Buffer(0);
150+
in_len = 0;
151+
in_off = 0;
152+
}
153+
154+
if (out._set)
155+
out.set = out._set;
156+
else
157+
out.set = bufferSet;
158+
159+
var strm = this.strm;
160+
strm.avail_in = in_len;
161+
strm.next_in = input;
162+
strm.next_in_index = in_off;
163+
strm.avail_out = out_len;
164+
strm.next_out = out;
165+
strm.next_out_index = out_off;
166+
167+
switch (this.mode) {
168+
case exports.DEFLATE:
169+
case exports.GZIP:
170+
case exports.DEFLATERAW:
171+
var status = zlib_deflate.deflate(strm, flush);
172+
break;
173+
case exports.UNZIP:
174+
case exports.INFLATE:
175+
case exports.GUNZIP:
176+
case exports.INFLATERAW:
177+
var status = zlib_inflate.inflate(strm, flush);
178+
break;
179+
default:
180+
throw new Error("Unknown mode " + this.mode);
181+
}
182+
183+
if (status !== exports.Z_STREAM_END && status !== exports.Z_OK) {
184+
this._error(status);
185+
}
186+
187+
this.write_in_progress = false;
188+
return [strm.avail_in, strm.avail_out];
189+
};
190+
191+
Zlib.prototype.close = function() {
192+
if (this.write_in_progress) {
193+
this.pending_close = true;
194+
return;
195+
}
196+
197+
this.pending_close = false;
198+
199+
if (this.mode === exports.DEFLATE || this.mode === exports.GZIP || this.mode === exports.DEFLATERAW) {
200+
zlib_deflate.deflateEnd(this.strm);
201+
} else {
202+
zlib_inflate.inflateEnd(this.strm);
203+
}
204+
205+
this.mode = exports.NONE;
206+
};
207+
208+
Zlib.prototype.reset = function() {
209+
switch (this.mode) {
210+
case DEFLATE:
211+
case DEFLATERAW:
212+
var status = zlib_deflate.deflateReset(this.strm);
213+
break;
214+
case INFLATE:
215+
case INFLATERAW:
216+
var status = zlib_inflate.inflateReset(this.strm);
217+
break;
218+
}
219+
220+
if (status !== exports.Z_OK) {
221+
this._error(status);
222+
}
223+
};
224+
225+
Zlib.prototype._error = function(status) {
226+
this.onerror(msg[status] + ': ' + this.strm.msg, status);
227+
228+
this.write_in_progress = false;
229+
if (this.pending_close)
230+
this.close();
231+
};
232+
233+
exports.Zlib = Zlib;

0 commit comments

Comments
 (0)