-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgzip-buffer.js
149 lines (129 loc) · 4.3 KB
/
gzip-buffer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*!
* gzip-buffer
* Copyright(c) 2011 Russell Bradberry <[email protected]>
* MIT Licensed
*/
var zlib = require('zlib'),
Stream = require('stream').Stream,
util = require('util');
/**
* Collects a stream into a buffer and when the stream ends it emits the collected buffer
* @constructor
* @private
*/
var StreamCollector = function(){
this.writable = true;
this._data = new Buffer(0);
};
util.inherits(StreamCollector, Stream);
/**
* Writes data to the buffer
* @param {Object} chunk The chunk to buffer
*/
StreamCollector.prototype.write = function(chunk){
if (chunk !== undefined){
if (!Buffer.isBuffer(chunk)){
chunk = new Buffer(chunk);
}
var newBuffer = new Buffer(chunk.length + this._data.length);
this._data.copy(newBuffer);
chunk.copy(newBuffer, this._data.length);
this._data = newBuffer;
}
};
/**
* Ends the stream writing the final chunk
* @param {Object} chunk The chunk to buffer
*/
StreamCollector.prototype.end = function(chunk){
this.write(chunk);
this.emit('end', this._data);
};
/**
* Creates the StreamCollector, zips or unzips it and calls back with the data
* @param {Object} data The data to zip or unzip
* @param {Object} gz The zipping mechanism
* @param {Function} callback The callback to call once the stream has finished
* @private
*/
function process(data, gz, options, callback){
var stream = new StreamCollector();
if (typeof options === 'function'){
callback = options;
options = null;
}
callback = callback || function(){};
gz = gz(options);
stream.on('end', function(data){
callback(data);
});
gz.pipe(stream);
gz.end(data);
}
/**
* Compresses data using deflate and calls back with the compressed data
* @param {Object} data The data to compress
* @param {Object} options Options to pass to the zlib class
* @param {Function} callback The callback function that returns the data
*/
exports.deflate = function(data, options, callback){
process(data, zlib.createDeflate, options, callback);
};
/**
* Uncompresses data using inflate and calls back with the compressed data
* @param {Object} data The data to uncompress
* @param {Object} options Options to pass to the zlib class
* @param {Function} callback The callback function that returns the data
*/
exports.inflate = function(data, options, callback){
process(data, zlib.createInflate, options, callback);
};
/**
* Compresses data using deflateRaw and calls back with the compressed data
* @param {Object} data The data to compress
* @param {Object} options Options to pass to the zlib class
* @param {Function} callback The callback function that returns the data
*/
exports.deflateRaw = function(data, options, callback){
process(data, zlib.createDeflateRaw, options, callback);
};
/**
* Uncompresses data using inflateRaw and calls back with the compressed data
* @param {Object} data The data to uncompress
* @param {Object} options Options to pass to the zlib class
* @param {Function} callback The callback function that returns the data
*/
exports.inflateRaw = function(data, options, callback){
process(data, zlib.createInflateRaw, options, callback);
};
/**
* Compresses data using gzip and calls back with the compressed data
* @param {Object} data The data to compress
* @param {Object} options Options to pass to the zlib class
* @param {Function} callback The callback function that returns the data
*/
exports.gzip = function(data, options, callback){
process(data, zlib.createGzip, options, callback);
};
/**
* Uncompresses data using gunzip and calls back with the compressed data
* @param {Object} data The data to uncompress
* @param {Object} options Options to pass to the zlib class
* @param {Function} callback The callback function that returns the data
*/
exports.gunzip = function(data, options, callback){
process(data, zlib.createGunzip, options, callback);
};
/**
* Uncompresses data using the header of the data and calls back with the compressed data
* @param {Object} data The data to uncompress
* @param {Object} options Options to pass to the zlib class
* @param {Function} callback The callback function that returns the data
*/
exports.unzip = function(data, options, callback){
process(data, zlib.createUnzip, options, callback);
};
/**
* Library version.
*/
exports.version = '0.0.2';