Debian Codegraph @

polyfills/zlib-lib/binding.js

raw
1 import msg from './messages';
2 import zstream from './zstream';
3 import {deflateInit2, deflateEnd, deflateReset, deflate} from './deflate';
4 import {inflateInit2, inflate, inflateEnd, inflateReset} from './inflate';
5 // import constants from './constants';
6
7
8 // zlib modes
9 export var NONE = 0;
10 export var DEFLATE = 1;
11 export var INFLATE = 2;
12 export var GZIP = 3;
13 export var GUNZIP = 4;
14 export var DEFLATERAW = 5;
15 export var INFLATERAW = 6;
16 export var UNZIP = 7;
17 export var Z_NO_FLUSH= 0,
18 Z_PARTIAL_FLUSH= 1,
19 Z_SYNC_FLUSH= 2,
20 Z_FULL_FLUSH= 3,
21 Z_FINISH= 4,
22 Z_BLOCK= 5,
23 Z_TREES= 6,
24
25 /* Return codes for the compression/decompression functions. Negative values
26 * are errors, positive values are used for special but normal events.
27 */
28 Z_OK= 0,
29 Z_STREAM_END= 1,
30 Z_NEED_DICT= 2,
31 Z_ERRNO= -1,
32 Z_STREAM_ERROR= -2,
33 Z_DATA_ERROR= -3,
34 //Z_MEM_ERROR: -4,
35 Z_BUF_ERROR= -5,
36 //Z_VERSION_ERROR: -6,
37
38 /* compression levels */
39 Z_NO_COMPRESSION= 0,
40 Z_BEST_SPEED= 1,
41 Z_BEST_COMPRESSION= 9,
42 Z_DEFAULT_COMPRESSION= -1,
43
44
45 Z_FILTERED= 1,
46 Z_HUFFMAN_ONLY= 2,
47 Z_RLE= 3,
48 Z_FIXED= 4,
49 Z_DEFAULT_STRATEGY= 0,
50
51 /* Possible values of the data_type field (though see inflate()) */
52 Z_BINARY= 0,
53 Z_TEXT= 1,
54 //Z_ASCII: 1, // = Z_TEXT (deprecated)
55 Z_UNKNOWN= 2,
56
57 /* The deflate compression method */
58 Z_DEFLATED= 8;
59 export function Zlib(mode) {
60 if (mode < DEFLATE || mode > UNZIP)
61 throw new TypeError('Bad argument');
62
63 this.mode = mode;
64 this.init_done = false;
65 this.write_in_progress = false;
66 this.pending_close = false;
67 this.windowBits = 0;
68 this.level = 0;
69 this.memLevel = 0;
70 this.strategy = 0;
71 this.dictionary = null;
72 }
73
74 Zlib.prototype.init = function(windowBits, level, memLevel, strategy, dictionary) {
75 this.windowBits = windowBits;
76 this.level = level;
77 this.memLevel = memLevel;
78 this.strategy = strategy;
79 // dictionary not supported.
80
81 if (this.mode === GZIP || this.mode === GUNZIP)
82 this.windowBits += 16;
83
84 if (this.mode === UNZIP)
85 this.windowBits += 32;
86
87 if (this.mode === DEFLATERAW || this.mode === INFLATERAW)
88 this.windowBits = -this.windowBits;
89
90 this.strm = new zstream();
91 var status;
92 switch (this.mode) {
93 case DEFLATE:
94 case GZIP:
95 case DEFLATERAW:
96 status = deflateInit2(
97 this.strm,
98 this.level,
99 Z_DEFLATED,
100 this.windowBits,
101 this.memLevel,
102 this.strategy
103 );
104 break;
105 case INFLATE:
106 case GUNZIP:
107 case INFLATERAW:
108 case UNZIP:
109 status = inflateInit2(
110 this.strm,
111 this.windowBits
112 );
113 break;
114 default:
115 throw new Error('Unknown mode ' + this.mode);
116 }
117
118 if (status !== Z_OK) {
119 this._error(status);
120 return;
121 }
122
123 this.write_in_progress = false;
124 this.init_done = true;
125 };
126
127 Zlib.prototype.params = function() {
128 throw new Error('deflateParams Not supported');
129 };
130
131 Zlib.prototype._writeCheck = function() {
132 if (!this.init_done)
133 throw new Error('write before init');
134
135 if (this.mode === NONE)
136 throw new Error('already finalized');
137
138 if (this.write_in_progress)
139 throw new Error('write already in progress');
140
141 if (this.pending_close)
142 throw new Error('close is pending');
143 };
144
145 Zlib.prototype.write = function(flush, input, in_off, in_len, out, out_off, out_len) {
146 this._writeCheck();
147 this.write_in_progress = true;
148
149 var self = this;
150 process.nextTick(function() {
151 self.write_in_progress = false;
152 var res = self._write(flush, input, in_off, in_len, out, out_off, out_len);
153 self.callback(res[0], res[1]);
154
155 if (self.pending_close)
156 self.close();
157 });
158
159 return this;
160 };
161
162 // set method for Node buffers, used by pako
163 function bufferSet(data, offset) {
164 for (var i = 0; i < data.length; i++) {
165 this[offset + i] = data[i];
166 }
167 }
168
169 Zlib.prototype.writeSync = function(flush, input, in_off, in_len, out, out_off, out_len) {
170 this._writeCheck();
171 return this._write(flush, input, in_off, in_len, out, out_off, out_len);
172 };
173
174 Zlib.prototype._write = function(flush, input, in_off, in_len, out, out_off, out_len) {
175 this.write_in_progress = true;
176
177 if (flush !== Z_NO_FLUSH &&
178 flush !== Z_PARTIAL_FLUSH &&
179 flush !== Z_SYNC_FLUSH &&
180 flush !== Z_FULL_FLUSH &&
181 flush !== Z_FINISH &&
182 flush !== Z_BLOCK) {
183 throw new Error('Invalid flush value');
184 }
185
186 if (input == null) {
187 input = new Buffer(0);
188 in_len = 0;
189 in_off = 0;
190 }
191
192 if (out._set)
193 out.set = out._set;
194 else
195 out.set = bufferSet;
196
197 var strm = this.strm;
198 strm.avail_in = in_len;
199 strm.input = input;
200 strm.next_in = in_off;
201 strm.avail_out = out_len;
202 strm.output = out;
203 strm.next_out = out_off;
204 var status;
205 switch (this.mode) {
206 case DEFLATE:
207 case GZIP:
208 case DEFLATERAW:
209 status = deflate(strm, flush);
210 break;
211 case UNZIP:
212 case INFLATE:
213 case GUNZIP:
214 case INFLATERAW:
215 status = inflate(strm, flush);
216 break;
217 default:
218 throw new Error('Unknown mode ' + this.mode);
219 }
220
221 if (status !== Z_STREAM_END && status !== Z_OK) {
222 this._error(status);
223 }
224
225 this.write_in_progress = false;
226 return [strm.avail_in, strm.avail_out];
227 };
228
229 Zlib.prototype.close = function() {
230 if (this.write_in_progress) {
231 this.pending_close = true;
232 return;
233 }
234
235 this.pending_close = false;
236
237 if (this.mode === DEFLATE || this.mode === GZIP || this.mode === DEFLATERAW) {
238 deflateEnd(this.strm);
239 } else {
240 inflateEnd(this.strm);
241 }
242
243 this.mode = NONE;
244 };
245 var status
246 Zlib.prototype.reset = function() {
247 switch (this.mode) {
248 case DEFLATE:
249 case DEFLATERAW:
250 status = deflateReset(this.strm);
251 break;
252 case INFLATE:
253 case INFLATERAW:
254 status = inflateReset(this.strm);
255 break;
256 }
257
258 if (status !== Z_OK) {
259 this._error(status);
260 }
261 };
262
263 Zlib.prototype._error = function(status) {
264 this.onerror(msg[status] + ': ' + this.strm.msg, status);
265
266 this.write_in_progress = false;
267 if (this.pending_close)
268 this.close();
269 };