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