macgyver 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +45 -0
- data/Rakefile +1 -0
- data/assets/MacGap.app/Contents/Frameworks/Growl.framework/Growl +0 -0
- data/assets/MacGap.app/Contents/Frameworks/Growl.framework/Versions/A/Growl +0 -0
- data/assets/MacGap.app/Contents/Frameworks/Growl.framework/Versions/A/Headers/Growl.h +5 -0
- data/assets/MacGap.app/Contents/Frameworks/Growl.framework/Versions/A/Headers/GrowlApplicationBridge.h +551 -0
- data/assets/MacGap.app/Contents/Frameworks/Growl.framework/Versions/A/Headers/GrowlDefines.h +341 -0
- data/assets/MacGap.app/Contents/Frameworks/Growl.framework/Versions/A/Resources/Info.plist +40 -0
- data/assets/MacGap.app/Contents/Frameworks/Growl.framework/Versions/A/_CodeSignature/CodeResources +34 -0
- data/assets/MacGap.app/Contents/Info.plist +48 -0
- data/assets/MacGap.app/Contents/MacOS/MacGap +0 -0
- data/assets/MacGap.app/Contents/PkgInfo +1 -0
- data/assets/MacGap.app/Contents/Resources/_debugger.js +1718 -0
- data/assets/MacGap.app/Contents/Resources/_http_agent.js +310 -0
- data/assets/MacGap.app/Contents/Resources/_http_client.js +533 -0
- data/assets/MacGap.app/Contents/Resources/_http_common.js +222 -0
- data/assets/MacGap.app/Contents/Resources/_http_incoming.js +194 -0
- data/assets/MacGap.app/Contents/Resources/_http_outgoing.js +597 -0
- data/assets/MacGap.app/Contents/Resources/_http_server.js +510 -0
- data/assets/MacGap.app/Contents/Resources/_linklist.js +76 -0
- data/assets/MacGap.app/Contents/Resources/_stream_duplex.js +69 -0
- data/assets/MacGap.app/Contents/Resources/_stream_passthrough.js +41 -0
- data/assets/MacGap.app/Contents/Resources/_stream_readable.js +900 -0
- data/assets/MacGap.app/Contents/Resources/_stream_transform.js +204 -0
- data/assets/MacGap.app/Contents/Resources/_stream_writable.js +456 -0
- data/assets/MacGap.app/Contents/Resources/_tls_legacy.js +887 -0
- data/assets/MacGap.app/Contents/Resources/_tls_wrap.js +831 -0
- data/assets/MacGap.app/Contents/Resources/application.icns +0 -0
- data/assets/MacGap.app/Contents/Resources/assert.js +326 -0
- data/assets/MacGap.app/Contents/Resources/buffer.js +724 -0
- data/assets/MacGap.app/Contents/Resources/child_process.js +1107 -0
- data/assets/MacGap.app/Contents/Resources/cluster.js +613 -0
- data/assets/MacGap.app/Contents/Resources/console.js +108 -0
- data/assets/MacGap.app/Contents/Resources/constants.js +22 -0
- data/assets/MacGap.app/Contents/Resources/crypto.js +691 -0
- data/assets/MacGap.app/Contents/Resources/dgram.js +459 -0
- data/assets/MacGap.app/Contents/Resources/dns.js +274 -0
- data/assets/MacGap.app/Contents/Resources/domain.js +292 -0
- data/assets/MacGap.app/Contents/Resources/en.lproj/Credits.rtf +29 -0
- data/assets/MacGap.app/Contents/Resources/en.lproj/InfoPlist.strings +0 -0
- data/assets/MacGap.app/Contents/Resources/en.lproj/MainMenu.nib +0 -0
- data/assets/MacGap.app/Contents/Resources/en.lproj/Window.nib +0 -0
- data/assets/MacGap.app/Contents/Resources/events.js +312 -0
- data/assets/MacGap.app/Contents/Resources/freelist.js +43 -0
- data/assets/MacGap.app/Contents/Resources/fs.js +1732 -0
- data/assets/MacGap.app/Contents/Resources/http.js +119 -0
- data/assets/MacGap.app/Contents/Resources/https.js +134 -0
- data/assets/MacGap.app/Contents/Resources/module.js +529 -0
- data/assets/MacGap.app/Contents/Resources/net.js +1378 -0
- data/assets/MacGap.app/Contents/Resources/nodelike.js +195 -0
- data/assets/MacGap.app/Contents/Resources/os.js +64 -0
- data/assets/MacGap.app/Contents/Resources/path.js +517 -0
- data/assets/MacGap.app/Contents/Resources/public/index.html +38 -0
- data/assets/MacGap.app/Contents/Resources/punycode.js +507 -0
- data/assets/MacGap.app/Contents/Resources/querystring.js +206 -0
- data/assets/MacGap.app/Contents/Resources/readline.js +1311 -0
- data/assets/MacGap.app/Contents/Resources/repl.js +945 -0
- data/assets/MacGap.app/Contents/Resources/smalloc.js +90 -0
- data/assets/MacGap.app/Contents/Resources/stream.js +127 -0
- data/assets/MacGap.app/Contents/Resources/string_decoder.js +189 -0
- data/assets/MacGap.app/Contents/Resources/sys.js +24 -0
- data/assets/MacGap.app/Contents/Resources/timers.js +568 -0
- data/assets/MacGap.app/Contents/Resources/tls.js +220 -0
- data/assets/MacGap.app/Contents/Resources/tty.js +129 -0
- data/assets/MacGap.app/Contents/Resources/url.js +693 -0
- data/assets/MacGap.app/Contents/Resources/util.js +688 -0
- data/assets/MacGap.app/Contents/Resources/vm.js +73 -0
- data/assets/MacGap.app/Contents/Resources/zlib.js +524 -0
- data/assets/index.html +38 -0
- data/bin/macgyver +104 -0
- data/macgyver.gemspec +19 -0
- data/test/public/index.html +27 -0
- metadata +121 -0
@@ -0,0 +1,73 @@
|
|
1
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
2
|
+
//
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
4
|
+
// copy of this software and associated documentation files (the
|
5
|
+
// "Software"), to deal in the Software without restriction, including
|
6
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
8
|
+
// persons to whom the Software is furnished to do so, subject to the
|
9
|
+
// following conditions:
|
10
|
+
//
|
11
|
+
// The above copyright notice and this permission notice shall be included
|
12
|
+
// in all copies or substantial portions of the Software.
|
13
|
+
//
|
14
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
15
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
17
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
18
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
19
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
20
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
var binding = process.binding('contextify');
|
23
|
+
var Script = binding.ContextifyScript;
|
24
|
+
var util = require('util');
|
25
|
+
|
26
|
+
// The binding provides a few useful primitives:
|
27
|
+
// - ContextifyScript(code, { filename = "evalmachine.anonymous",
|
28
|
+
// displayErrors = true } = {})
|
29
|
+
// with methods:
|
30
|
+
// - runInThisContext({ displayErrors = true } = {})
|
31
|
+
// - runInContext(sandbox, { displayErrors = true, timeout = undefined } = {})
|
32
|
+
// - makeContext(sandbox)
|
33
|
+
// - isContext(sandbox)
|
34
|
+
// From this we build the entire documented API.
|
35
|
+
|
36
|
+
Script.prototype.runInNewContext = function(sandbox, options) {
|
37
|
+
var context = exports.createContext(sandbox);
|
38
|
+
return this.runInContext(context, options);
|
39
|
+
};
|
40
|
+
|
41
|
+
exports.Script = Script;
|
42
|
+
|
43
|
+
exports.createScript = function(code, options) {
|
44
|
+
return new Script(code, options);
|
45
|
+
};
|
46
|
+
|
47
|
+
exports.createContext = function(sandbox) {
|
48
|
+
if (util.isUndefined(sandbox)) {
|
49
|
+
sandbox = {};
|
50
|
+
} else if (binding.isContext(sandbox)) {
|
51
|
+
return sandbox;
|
52
|
+
}
|
53
|
+
|
54
|
+
binding.makeContext(sandbox);
|
55
|
+
return sandbox;
|
56
|
+
};
|
57
|
+
|
58
|
+
exports.runInContext = function(code, contextifiedSandbox, options) {
|
59
|
+
var script = new Script(code, options);
|
60
|
+
return script.runInContext(contextifiedSandbox, options);
|
61
|
+
};
|
62
|
+
|
63
|
+
exports.runInNewContext = function(code, sandbox, options) {
|
64
|
+
var script = new Script(code, options);
|
65
|
+
return script.runInNewContext(sandbox, options);
|
66
|
+
};
|
67
|
+
|
68
|
+
exports.runInThisContext = function(code, options) {
|
69
|
+
var script = new Script(code, options);
|
70
|
+
return script.runInThisContext(options);
|
71
|
+
};
|
72
|
+
|
73
|
+
exports.isContext = binding.isContext;
|
@@ -0,0 +1,524 @@
|
|
1
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
2
|
+
//
|
3
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
4
|
+
// copy of this software and associated documentation files (the
|
5
|
+
// "Software"), to deal in the Software without restriction, including
|
6
|
+
// without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
8
|
+
// persons to whom the Software is furnished to do so, subject to the
|
9
|
+
// following conditions:
|
10
|
+
//
|
11
|
+
// The above copyright notice and this permission notice shall be included
|
12
|
+
// in all copies or substantial portions of the Software.
|
13
|
+
//
|
14
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
15
|
+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
17
|
+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
18
|
+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
19
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
20
|
+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
var Transform = require('_stream_transform');
|
23
|
+
|
24
|
+
var binding = process.binding('zlib');
|
25
|
+
var util = require('util');
|
26
|
+
var assert = require('assert').ok;
|
27
|
+
|
28
|
+
// zlib doesn't provide these, so kludge them in following the same
|
29
|
+
// const naming scheme zlib uses.
|
30
|
+
binding.Z_MIN_WINDOWBITS = 8;
|
31
|
+
binding.Z_MAX_WINDOWBITS = 15;
|
32
|
+
binding.Z_DEFAULT_WINDOWBITS = 15;
|
33
|
+
|
34
|
+
// fewer than 64 bytes per chunk is stupid.
|
35
|
+
// technically it could work with as few as 8, but even 64 bytes
|
36
|
+
// is absurdly low. Usually a MB or more is best.
|
37
|
+
binding.Z_MIN_CHUNK = 64;
|
38
|
+
binding.Z_MAX_CHUNK = Infinity;
|
39
|
+
binding.Z_DEFAULT_CHUNK = (16 * 1024);
|
40
|
+
|
41
|
+
binding.Z_MIN_MEMLEVEL = 1;
|
42
|
+
binding.Z_MAX_MEMLEVEL = 9;
|
43
|
+
binding.Z_DEFAULT_MEMLEVEL = 8;
|
44
|
+
|
45
|
+
binding.Z_MIN_LEVEL = -1;
|
46
|
+
binding.Z_MAX_LEVEL = 9;
|
47
|
+
binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;
|
48
|
+
|
49
|
+
// expose all the zlib constants
|
50
|
+
Object.keys(binding).forEach(function(k) {
|
51
|
+
if (k.match(/^Z/)) exports[k] = binding[k];
|
52
|
+
});
|
53
|
+
|
54
|
+
// translation table for return codes.
|
55
|
+
exports.codes = {
|
56
|
+
Z_OK: binding.Z_OK,
|
57
|
+
Z_STREAM_END: binding.Z_STREAM_END,
|
58
|
+
Z_NEED_DICT: binding.Z_NEED_DICT,
|
59
|
+
Z_ERRNO: binding.Z_ERRNO,
|
60
|
+
Z_STREAM_ERROR: binding.Z_STREAM_ERROR,
|
61
|
+
Z_DATA_ERROR: binding.Z_DATA_ERROR,
|
62
|
+
Z_MEM_ERROR: binding.Z_MEM_ERROR,
|
63
|
+
Z_BUF_ERROR: binding.Z_BUF_ERROR,
|
64
|
+
Z_VERSION_ERROR: binding.Z_VERSION_ERROR
|
65
|
+
};
|
66
|
+
|
67
|
+
Object.keys(exports.codes).forEach(function(k) {
|
68
|
+
exports.codes[exports.codes[k]] = k;
|
69
|
+
});
|
70
|
+
|
71
|
+
exports.Deflate = Deflate;
|
72
|
+
exports.Inflate = Inflate;
|
73
|
+
exports.Gzip = Gzip;
|
74
|
+
exports.Gunzip = Gunzip;
|
75
|
+
exports.DeflateRaw = DeflateRaw;
|
76
|
+
exports.InflateRaw = InflateRaw;
|
77
|
+
exports.Unzip = Unzip;
|
78
|
+
|
79
|
+
exports.createDeflate = function(o) {
|
80
|
+
return new Deflate(o);
|
81
|
+
};
|
82
|
+
|
83
|
+
exports.createInflate = function(o) {
|
84
|
+
return new Inflate(o);
|
85
|
+
};
|
86
|
+
|
87
|
+
exports.createDeflateRaw = function(o) {
|
88
|
+
return new DeflateRaw(o);
|
89
|
+
};
|
90
|
+
|
91
|
+
exports.createInflateRaw = function(o) {
|
92
|
+
return new InflateRaw(o);
|
93
|
+
};
|
94
|
+
|
95
|
+
exports.createGzip = function(o) {
|
96
|
+
return new Gzip(o);
|
97
|
+
};
|
98
|
+
|
99
|
+
exports.createGunzip = function(o) {
|
100
|
+
return new Gunzip(o);
|
101
|
+
};
|
102
|
+
|
103
|
+
exports.createUnzip = function(o) {
|
104
|
+
return new Unzip(o);
|
105
|
+
};
|
106
|
+
|
107
|
+
|
108
|
+
// Convenience methods.
|
109
|
+
// compress/decompress a string or buffer in one step.
|
110
|
+
exports.deflate = function(buffer, opts, callback) {
|
111
|
+
if (util.isFunction(opts)) {
|
112
|
+
callback = opts;
|
113
|
+
opts = {};
|
114
|
+
}
|
115
|
+
zlibBuffer(new Deflate(opts), buffer, callback);
|
116
|
+
};
|
117
|
+
|
118
|
+
exports.gzip = function(buffer, opts, callback) {
|
119
|
+
if (util.isFunction(opts)) {
|
120
|
+
callback = opts;
|
121
|
+
opts = {};
|
122
|
+
}
|
123
|
+
zlibBuffer(new Gzip(opts), buffer, callback);
|
124
|
+
};
|
125
|
+
|
126
|
+
exports.deflateRaw = function(buffer, opts, callback) {
|
127
|
+
if (util.isFunction(opts)) {
|
128
|
+
callback = opts;
|
129
|
+
opts = {};
|
130
|
+
}
|
131
|
+
zlibBuffer(new DeflateRaw(opts), buffer, callback);
|
132
|
+
};
|
133
|
+
|
134
|
+
exports.unzip = function(buffer, opts, callback) {
|
135
|
+
if (util.isFunction(opts)) {
|
136
|
+
callback = opts;
|
137
|
+
opts = {};
|
138
|
+
}
|
139
|
+
zlibBuffer(new Unzip(opts), buffer, callback);
|
140
|
+
};
|
141
|
+
|
142
|
+
exports.inflate = function(buffer, opts, callback) {
|
143
|
+
if (util.isFunction(opts)) {
|
144
|
+
callback = opts;
|
145
|
+
opts = {};
|
146
|
+
}
|
147
|
+
zlibBuffer(new Inflate(opts), buffer, callback);
|
148
|
+
};
|
149
|
+
|
150
|
+
exports.gunzip = function(buffer, opts, callback) {
|
151
|
+
if (util.isFunction(opts)) {
|
152
|
+
callback = opts;
|
153
|
+
opts = {};
|
154
|
+
}
|
155
|
+
zlibBuffer(new Gunzip(opts), buffer, callback);
|
156
|
+
};
|
157
|
+
|
158
|
+
exports.inflateRaw = function(buffer, opts, callback) {
|
159
|
+
if (util.isFunction(opts)) {
|
160
|
+
callback = opts;
|
161
|
+
opts = {};
|
162
|
+
}
|
163
|
+
zlibBuffer(new InflateRaw(opts), buffer, callback);
|
164
|
+
};
|
165
|
+
|
166
|
+
function zlibBuffer(engine, buffer, callback) {
|
167
|
+
var buffers = [];
|
168
|
+
var nread = 0;
|
169
|
+
|
170
|
+
engine.on('error', onError);
|
171
|
+
engine.on('end', onEnd);
|
172
|
+
|
173
|
+
engine.end(buffer);
|
174
|
+
flow();
|
175
|
+
|
176
|
+
function flow() {
|
177
|
+
var chunk;
|
178
|
+
while (null !== (chunk = engine.read())) {
|
179
|
+
buffers.push(chunk);
|
180
|
+
nread += chunk.length;
|
181
|
+
}
|
182
|
+
engine.once('readable', flow);
|
183
|
+
}
|
184
|
+
|
185
|
+
function onError(err) {
|
186
|
+
engine.removeListener('end', onEnd);
|
187
|
+
engine.removeListener('readable', flow);
|
188
|
+
callback(err);
|
189
|
+
}
|
190
|
+
|
191
|
+
function onEnd() {
|
192
|
+
var buf = Buffer.concat(buffers, nread);
|
193
|
+
buffers = [];
|
194
|
+
callback(null, buf);
|
195
|
+
engine.close();
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
// generic zlib
|
201
|
+
// minimal 2-byte header
|
202
|
+
function Deflate(opts) {
|
203
|
+
if (!(this instanceof Deflate)) return new Deflate(opts);
|
204
|
+
Zlib.call(this, opts, binding.DEFLATE);
|
205
|
+
}
|
206
|
+
|
207
|
+
function Inflate(opts) {
|
208
|
+
if (!(this instanceof Inflate)) return new Inflate(opts);
|
209
|
+
Zlib.call(this, opts, binding.INFLATE);
|
210
|
+
}
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
// gzip - bigger header, same deflate compression
|
215
|
+
function Gzip(opts) {
|
216
|
+
if (!(this instanceof Gzip)) return new Gzip(opts);
|
217
|
+
Zlib.call(this, opts, binding.GZIP);
|
218
|
+
}
|
219
|
+
|
220
|
+
function Gunzip(opts) {
|
221
|
+
if (!(this instanceof Gunzip)) return new Gunzip(opts);
|
222
|
+
Zlib.call(this, opts, binding.GUNZIP);
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
// raw - no header
|
228
|
+
function DeflateRaw(opts) {
|
229
|
+
if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
|
230
|
+
Zlib.call(this, opts, binding.DEFLATERAW);
|
231
|
+
}
|
232
|
+
|
233
|
+
function InflateRaw(opts) {
|
234
|
+
if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
|
235
|
+
Zlib.call(this, opts, binding.INFLATERAW);
|
236
|
+
}
|
237
|
+
|
238
|
+
|
239
|
+
// auto-detect header.
|
240
|
+
function Unzip(opts) {
|
241
|
+
if (!(this instanceof Unzip)) return new Unzip(opts);
|
242
|
+
Zlib.call(this, opts, binding.UNZIP);
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
// the Zlib class they all inherit from
|
247
|
+
// This thing manages the queue of requests, and returns
|
248
|
+
// true or false if there is anything in the queue when
|
249
|
+
// you call the .write() method.
|
250
|
+
|
251
|
+
function Zlib(opts, mode) {
|
252
|
+
this._opts = opts = opts || {};
|
253
|
+
this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;
|
254
|
+
|
255
|
+
Transform.call(this, opts);
|
256
|
+
|
257
|
+
if (opts.flush) {
|
258
|
+
if (opts.flush !== binding.Z_NO_FLUSH &&
|
259
|
+
opts.flush !== binding.Z_PARTIAL_FLUSH &&
|
260
|
+
opts.flush !== binding.Z_SYNC_FLUSH &&
|
261
|
+
opts.flush !== binding.Z_FULL_FLUSH &&
|
262
|
+
opts.flush !== binding.Z_FINISH &&
|
263
|
+
opts.flush !== binding.Z_BLOCK) {
|
264
|
+
throw new Error('Invalid flush flag: ' + opts.flush);
|
265
|
+
}
|
266
|
+
}
|
267
|
+
this._flushFlag = opts.flush || binding.Z_NO_FLUSH;
|
268
|
+
|
269
|
+
if (opts.chunkSize) {
|
270
|
+
if (opts.chunkSize < exports.Z_MIN_CHUNK ||
|
271
|
+
opts.chunkSize > exports.Z_MAX_CHUNK) {
|
272
|
+
throw new Error('Invalid chunk size: ' + opts.chunkSize);
|
273
|
+
}
|
274
|
+
}
|
275
|
+
|
276
|
+
if (opts.windowBits) {
|
277
|
+
if (opts.windowBits < exports.Z_MIN_WINDOWBITS ||
|
278
|
+
opts.windowBits > exports.Z_MAX_WINDOWBITS) {
|
279
|
+
throw new Error('Invalid windowBits: ' + opts.windowBits);
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
if (opts.level) {
|
284
|
+
if (opts.level < exports.Z_MIN_LEVEL ||
|
285
|
+
opts.level > exports.Z_MAX_LEVEL) {
|
286
|
+
throw new Error('Invalid compression level: ' + opts.level);
|
287
|
+
}
|
288
|
+
}
|
289
|
+
|
290
|
+
if (opts.memLevel) {
|
291
|
+
if (opts.memLevel < exports.Z_MIN_MEMLEVEL ||
|
292
|
+
opts.memLevel > exports.Z_MAX_MEMLEVEL) {
|
293
|
+
throw new Error('Invalid memLevel: ' + opts.memLevel);
|
294
|
+
}
|
295
|
+
}
|
296
|
+
|
297
|
+
if (opts.strategy) {
|
298
|
+
if (opts.strategy != exports.Z_FILTERED &&
|
299
|
+
opts.strategy != exports.Z_HUFFMAN_ONLY &&
|
300
|
+
opts.strategy != exports.Z_RLE &&
|
301
|
+
opts.strategy != exports.Z_FIXED &&
|
302
|
+
opts.strategy != exports.Z_DEFAULT_STRATEGY) {
|
303
|
+
throw new Error('Invalid strategy: ' + opts.strategy);
|
304
|
+
}
|
305
|
+
}
|
306
|
+
|
307
|
+
if (opts.dictionary) {
|
308
|
+
if (!util.isBuffer(opts.dictionary)) {
|
309
|
+
throw new Error('Invalid dictionary: it should be a Buffer instance');
|
310
|
+
}
|
311
|
+
}
|
312
|
+
|
313
|
+
this._binding = new binding.Zlib(mode);
|
314
|
+
|
315
|
+
var self = this;
|
316
|
+
this._hadError = false;
|
317
|
+
this._binding.onerror = function(message, errno) {
|
318
|
+
// there is no way to cleanly recover.
|
319
|
+
// continuing only obscures problems.
|
320
|
+
self._binding = null;
|
321
|
+
self._hadError = true;
|
322
|
+
|
323
|
+
var error = new Error(message);
|
324
|
+
error.errno = errno;
|
325
|
+
error.code = exports.codes[errno];
|
326
|
+
self.emit('error', error);
|
327
|
+
};
|
328
|
+
|
329
|
+
var level = exports.Z_DEFAULT_COMPRESSION;
|
330
|
+
if (util.isNumber(opts.level)) level = opts.level;
|
331
|
+
|
332
|
+
var strategy = exports.Z_DEFAULT_STRATEGY;
|
333
|
+
if (util.isNumber(opts.strategy)) strategy = opts.strategy;
|
334
|
+
|
335
|
+
this._binding.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
|
336
|
+
level,
|
337
|
+
opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
|
338
|
+
strategy,
|
339
|
+
opts.dictionary);
|
340
|
+
|
341
|
+
this._buffer = new Buffer(this._chunkSize);
|
342
|
+
this._offset = 0;
|
343
|
+
this._closed = false;
|
344
|
+
this._level = level;
|
345
|
+
this._strategy = strategy;
|
346
|
+
|
347
|
+
this.once('end', this.close);
|
348
|
+
}
|
349
|
+
|
350
|
+
util.inherits(Zlib, Transform);
|
351
|
+
|
352
|
+
Zlib.prototype.params = function(level, strategy, callback) {
|
353
|
+
if (level < exports.Z_MIN_LEVEL ||
|
354
|
+
level > exports.Z_MAX_LEVEL) {
|
355
|
+
throw new RangeError('Invalid compression level: ' + level);
|
356
|
+
}
|
357
|
+
if (strategy != exports.Z_FILTERED &&
|
358
|
+
strategy != exports.Z_HUFFMAN_ONLY &&
|
359
|
+
strategy != exports.Z_RLE &&
|
360
|
+
strategy != exports.Z_FIXED &&
|
361
|
+
strategy != exports.Z_DEFAULT_STRATEGY) {
|
362
|
+
throw new TypeError('Invalid strategy: ' + strategy);
|
363
|
+
}
|
364
|
+
|
365
|
+
if (this._level !== level || this._strategy !== strategy) {
|
366
|
+
var self = this;
|
367
|
+
this.flush(binding.Z_SYNC_FLUSH, function() {
|
368
|
+
self._binding.params(level, strategy);
|
369
|
+
if (!self._hadError) {
|
370
|
+
self._level = level;
|
371
|
+
self._strategy = strategy;
|
372
|
+
if (callback) callback();
|
373
|
+
}
|
374
|
+
});
|
375
|
+
} else {
|
376
|
+
process.nextTick(callback);
|
377
|
+
}
|
378
|
+
};
|
379
|
+
|
380
|
+
Zlib.prototype.reset = function() {
|
381
|
+
return this._binding.reset();
|
382
|
+
};
|
383
|
+
|
384
|
+
// This is the _flush function called by the transform class,
|
385
|
+
// internally, when the last chunk has been written.
|
386
|
+
Zlib.prototype._flush = function(callback) {
|
387
|
+
this._transform(new Buffer(0), '', callback);
|
388
|
+
};
|
389
|
+
|
390
|
+
Zlib.prototype.flush = function(kind, callback) {
|
391
|
+
var ws = this._writableState;
|
392
|
+
|
393
|
+
if (util.isFunction(kind) || (util.isUndefined(kind) && !callback)) {
|
394
|
+
callback = kind;
|
395
|
+
kind = binding.Z_FULL_FLUSH;
|
396
|
+
}
|
397
|
+
|
398
|
+
if (ws.ended) {
|
399
|
+
if (callback)
|
400
|
+
process.nextTick(callback);
|
401
|
+
} else if (ws.ending) {
|
402
|
+
if (callback)
|
403
|
+
this.once('end', callback);
|
404
|
+
} else if (ws.needDrain) {
|
405
|
+
var self = this;
|
406
|
+
this.once('drain', function() {
|
407
|
+
self.flush(callback);
|
408
|
+
});
|
409
|
+
} else {
|
410
|
+
this._flushFlag = kind;
|
411
|
+
this.write(new Buffer(0), '', callback);
|
412
|
+
}
|
413
|
+
};
|
414
|
+
|
415
|
+
Zlib.prototype.close = function(callback) {
|
416
|
+
if (callback)
|
417
|
+
process.nextTick(callback);
|
418
|
+
|
419
|
+
if (this._closed)
|
420
|
+
return;
|
421
|
+
|
422
|
+
this._closed = true;
|
423
|
+
|
424
|
+
this._binding.close();
|
425
|
+
|
426
|
+
var self = this;
|
427
|
+
process.nextTick(function() {
|
428
|
+
self.emit('close');
|
429
|
+
});
|
430
|
+
};
|
431
|
+
|
432
|
+
Zlib.prototype._transform = function(chunk, encoding, cb) {
|
433
|
+
var flushFlag;
|
434
|
+
var ws = this._writableState;
|
435
|
+
var ending = ws.ending || ws.ended;
|
436
|
+
var last = ending && (!chunk || ws.length === chunk.length);
|
437
|
+
|
438
|
+
if (!util.isNull(chunk) && !util.isBuffer(chunk))
|
439
|
+
return cb(new Error('invalid input'));
|
440
|
+
|
441
|
+
// If it's the last chunk, or a final flush, we use the Z_FINISH flush flag.
|
442
|
+
// If it's explicitly flushing at some other time, then we use
|
443
|
+
// Z_FULL_FLUSH. Otherwise, use Z_NO_FLUSH for maximum compression
|
444
|
+
// goodness.
|
445
|
+
if (last)
|
446
|
+
flushFlag = binding.Z_FINISH;
|
447
|
+
else {
|
448
|
+
flushFlag = this._flushFlag;
|
449
|
+
// once we've flushed the last of the queue, stop flushing and
|
450
|
+
// go back to the normal behavior.
|
451
|
+
if (chunk.length >= ws.length) {
|
452
|
+
this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH;
|
453
|
+
}
|
454
|
+
}
|
455
|
+
|
456
|
+
var availInBefore = chunk && chunk.length;
|
457
|
+
var availOutBefore = this._chunkSize - this._offset;
|
458
|
+
var inOff = 0;
|
459
|
+
|
460
|
+
var req = this._binding.write(flushFlag,
|
461
|
+
chunk, // in
|
462
|
+
inOff, // in_off
|
463
|
+
availInBefore, // in_len
|
464
|
+
this._buffer, // out
|
465
|
+
this._offset, //out_off
|
466
|
+
availOutBefore); // out_len
|
467
|
+
|
468
|
+
req.buffer = chunk;
|
469
|
+
req.callback = callback;
|
470
|
+
|
471
|
+
var self = this;
|
472
|
+
function callback(availInAfter, availOutAfter) {
|
473
|
+
if (self._hadError)
|
474
|
+
return;
|
475
|
+
|
476
|
+
var have = availOutBefore - availOutAfter;
|
477
|
+
assert(have >= 0, 'have should not go down');
|
478
|
+
|
479
|
+
if (have > 0) {
|
480
|
+
var out = self._buffer.slice(self._offset, self._offset + have);
|
481
|
+
self._offset += have;
|
482
|
+
// serve some output to the consumer.
|
483
|
+
self.push(out);
|
484
|
+
}
|
485
|
+
|
486
|
+
// exhausted the output buffer, or used all the input create a new one.
|
487
|
+
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
|
488
|
+
availOutBefore = self._chunkSize;
|
489
|
+
self._offset = 0;
|
490
|
+
self._buffer = new Buffer(self._chunkSize);
|
491
|
+
}
|
492
|
+
|
493
|
+
if (availOutAfter === 0) {
|
494
|
+
// Not actually done. Need to reprocess.
|
495
|
+
// Also, update the availInBefore to the availInAfter value,
|
496
|
+
// so that if we have to hit it a third (fourth, etc.) time,
|
497
|
+
// it'll have the correct byte counts.
|
498
|
+
inOff += (availInBefore - availInAfter);
|
499
|
+
availInBefore = availInAfter;
|
500
|
+
|
501
|
+
var newReq = self._binding.write(flushFlag,
|
502
|
+
chunk,
|
503
|
+
inOff,
|
504
|
+
availInBefore,
|
505
|
+
self._buffer,
|
506
|
+
self._offset,
|
507
|
+
self._chunkSize);
|
508
|
+
newReq.callback = callback; // this same function
|
509
|
+
newReq.buffer = chunk;
|
510
|
+
return;
|
511
|
+
}
|
512
|
+
|
513
|
+
// finished with the chunk.
|
514
|
+
cb();
|
515
|
+
}
|
516
|
+
};
|
517
|
+
|
518
|
+
util.inherits(Deflate, Zlib);
|
519
|
+
util.inherits(Inflate, Zlib);
|
520
|
+
util.inherits(Gzip, Zlib);
|
521
|
+
util.inherits(Gunzip, Zlib);
|
522
|
+
util.inherits(DeflateRaw, Zlib);
|
523
|
+
util.inherits(InflateRaw, Zlib);
|
524
|
+
util.inherits(Unzip, Zlib);
|