Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 495 lines (390 sloc) 13.924 kb
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
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
ff4a9d3 @bnoordhuis core: use proper #include directives
bnoordhuis authored
23 #include "v8.h"
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
24 #include <errno.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28
531eba1 @bnoordhuis zlib: fix include of zlib.h
bnoordhuis authored
29 #include "zlib.h"
ff4a9d3 @bnoordhuis core: use proper #include directives
bnoordhuis authored
30 #include "node.h"
31 #include "node_buffer.h"
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
32
33
34 namespace node {
35 using namespace v8;
36
37
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
38 static Persistent<String> callback_sym;
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
39 static Persistent<String> onerror_sym;
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
40
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
41 enum node_zlib_mode {
42 DEFLATE = 1,
43 INFLATE,
44 GZIP,
45 GUNZIP,
46 DEFLATERAW,
47 INFLATERAW,
48 UNZIP
49 };
50
51
52 void InitZlib(v8::Handle<v8::Object> target);
53
54
55 /**
56 * Deflate/Inflate
57 */
44eb279 zlib: don't use C++ templates
ssuda authored
58 class ZCtx : public ObjectWrap {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
59 public:
60
44eb279 zlib: don't use C++ templates
ssuda authored
61 ZCtx(node_zlib_mode mode) : ObjectWrap(), dictionary_(NULL), mode_(mode) {}
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
62
63 ~ZCtx() {
44eb279 zlib: don't use C++ templates
ssuda authored
64 if (mode_ == DEFLATE || mode_ == GZIP || mode_ == DEFLATERAW) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
65 (void)deflateEnd(&strm_);
bf539f9 @isaacs zlib: Call inflateEnd for UNZIP. Fixes memory leak.
isaacs authored
66 } else if (mode_ == INFLATE || mode_ == GUNZIP || mode_ == INFLATERAW ||
67 mode_ == UNZIP) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
68 (void)inflateEnd(&strm_);
69 }
e609195 @indutny [zlib] added dictionary support
indutny authored
70
71 if (dictionary_ != NULL) delete[] dictionary_;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
72 }
73
74 // write(flush, in, in_off, in_len, out, out_off, out_len)
07701e7 @indutny zlib: C++ style fixes
indutny authored
75 static Handle<Value> Write(const Arguments& args) {
d104bfd @isaacs zlib: Fix test so that it's not trivially passing, then pass it.
isaacs authored
76 HandleScope scope;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
77 assert(args.Length() == 7);
78
44eb279 zlib: don't use C++ templates
ssuda authored
79 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
80 assert(ctx->init_done_ && "write before init");
81
8cca30f @isaacs zlib binding cleanup
isaacs authored
82 assert(!ctx->write_in_progress_ && "write already in progress");
83 ctx->write_in_progress_ = true;
84
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
85 unsigned int flush = args[0]->Uint32Value();
86 Bytef *in;
87 Bytef *out;
88 size_t in_off, in_len, out_off, out_len;
89
90 if (args[1]->IsNull()) {
91 // just a flush
92 Bytef nada[1] = { 0 };
93 in = nada;
94 in_len = 0;
95 in_off = 0;
96 } else {
97 assert(Buffer::HasInstance(args[1]));
98 Local<Object> in_buf;
99 in_buf = args[1]->ToObject();
07701e7 @indutny zlib: C++ style fixes
indutny authored
100 in_off = args[2]->Uint32Value();
101 in_len = args[3]->Uint32Value();
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
102
103 assert(in_off + in_len <= Buffer::Length(in_buf));
104 in = reinterpret_cast<Bytef *>(Buffer::Data(in_buf) + in_off);
105 }
106
107 assert(Buffer::HasInstance(args[4]));
108 Local<Object> out_buf = args[4]->ToObject();
07701e7 @indutny zlib: C++ style fixes
indutny authored
109 out_off = args[5]->Uint32Value();
110 out_len = args[6]->Uint32Value();
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
111 assert(out_off + out_len <= Buffer::Length(out_buf));
112 out = reinterpret_cast<Bytef *>(Buffer::Data(out_buf) + out_off);
113
8cca30f @isaacs zlib binding cleanup
isaacs authored
114 // build up the work request
115 uv_work_t* work_req = &(ctx->work_req_);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
116
117 ctx->strm_.avail_in = in_len;
07701e7 @indutny zlib: C++ style fixes
indutny authored
118 ctx->strm_.next_in = in;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
119 ctx->strm_.avail_out = out_len;
120 ctx->strm_.next_out = out;
121 ctx->flush_ = flush;
122
123 // set this so that later on, I can easily tell how much was written.
124 ctx->chunk_size_ = out_len;
125
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
126 uv_queue_work(uv_default_loop(),
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
127 work_req,
44eb279 zlib: don't use C++ templates
ssuda authored
128 ZCtx::Process,
129 ZCtx::After);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
130
0ad2717 @bnoordhuis Make sure that zlib contexts are not garbage collected when busy
bnoordhuis authored
131 ctx->Ref();
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
132
8cca30f @isaacs zlib binding cleanup
isaacs authored
133 return ctx->handle_;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
134 }
135
136
137 // thread pool!
138 // This function may be called multiple times on the uv_work pool
139 // for a single write() call, until all of the input bytes have
140 // been consumed.
07701e7 @indutny zlib: C++ style fixes
indutny authored
141 static void Process(uv_work_t* work_req) {
44eb279 zlib: don't use C++ templates
ssuda authored
142 ZCtx *ctx = container_of(work_req, ZCtx, work_req_);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
143
144 // If the avail_out is left at 0, then it means that it ran out
145 // of room. If there was avail_out left over, then it means
146 // that all of the input was consumed.
44eb279 zlib: don't use C++ templates
ssuda authored
147 switch (ctx->mode_) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
148 case DEFLATE:
149 case GZIP:
150 case DEFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
151 ctx->err_ = deflate(&ctx->strm_, ctx->flush_);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
152 break;
153 case UNZIP:
154 case INFLATE:
155 case GUNZIP:
156 case INFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
157 ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
e609195 @indutny [zlib] added dictionary support
indutny authored
158
159 // If data was encoded with dictionary
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
160 if (ctx->err_ == Z_NEED_DICT) {
e609195 @indutny [zlib] added dictionary support
indutny authored
161 assert(ctx->dictionary_ != NULL && "Stream has no dictionary");
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
162 if (ctx->dictionary_ != NULL) {
163
164 // Load it
165 ctx->err_ = inflateSetDictionary(&ctx->strm_,
166 ctx->dictionary_,
167 ctx->dictionary_len_);
168 assert(ctx->err_ == Z_OK && "Failed to set dictionary");
169 if (ctx->err_ == Z_OK) {
170
171 // And try to decode again
172 ctx->err_ = inflate(&ctx->strm_, ctx->flush_);
173 }
174 }
e609195 @indutny [zlib] added dictionary support
indutny authored
175 }
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
176 break;
177 default:
178 assert(0 && "wtf?");
179 }
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
180
181 // pass any errors back to the main thread to deal with.
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
182
183 // now After will emit the output, and
184 // either schedule another call to Process,
185 // or shift the queue and call Process.
186 }
187
188 // v8 land!
07701e7 @indutny zlib: C++ style fixes
indutny authored
189 static void After(uv_work_t* work_req) {
d104bfd @isaacs zlib: Fix test so that it's not trivially passing, then pass it.
isaacs authored
190 HandleScope scope;
44eb279 zlib: don't use C++ templates
ssuda authored
191 ZCtx *ctx = container_of(work_req, ZCtx, work_req_);
07701e7 @indutny zlib: C++ style fixes
indutny authored
192
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
193 // Acceptable error states depend on the type of zlib stream.
194 switch (ctx->err_) {
195 case Z_OK:
196 case Z_STREAM_END:
197 case Z_BUF_ERROR:
198 // normal statuses, not fatal
199 break;
200 default:
201 // something else.
202 ZCtx::Error(ctx, "Zlib error");
203 return;
204 }
205
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
206 Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out);
207 Local<Integer> avail_in = Integer::New(ctx->strm_.avail_in);
208
8cca30f @isaacs zlib binding cleanup
isaacs authored
209 ctx->write_in_progress_ = false;
210
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
211 // call the write() cb
8cca30f @isaacs zlib binding cleanup
isaacs authored
212 assert(ctx->handle_->Get(callback_sym)->IsFunction() &&
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
213 "Invalid callback");
214 Local<Value> args[2] = { avail_in, avail_out };
a26bee8 @isaacs MakeCallback: Consistent symbol usage
isaacs authored
215 MakeCallback(ctx->handle_, callback_sym, ARRAY_SIZE(args), args);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
216
0ad2717 @bnoordhuis Make sure that zlib contexts are not garbage collected when busy
bnoordhuis authored
217 ctx->Unref();
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
218 }
219
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
220 static void Error(ZCtx *ctx, const char *msg_) {
221 const char *msg;
222 if (ctx->strm_.msg != NULL) {
223 msg = ctx->strm_.msg;
224 } else {
225 msg = msg_;
226 }
227
228 assert(ctx->handle_->Get(onerror_sym)->IsFunction() &&
229 "Invalid error handler");
230 HandleScope scope;
231 Local<Value> args[2] = { String::New(msg),
232 Local<Value>::New(Number::New(ctx->err_)) };
a26bee8 @isaacs MakeCallback: Consistent symbol usage
isaacs authored
233 MakeCallback(ctx->handle_, onerror_sym, ARRAY_SIZE(args), args);
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
234
235 // no hope of rescue.
236 ctx->Unref();
237 }
238
07701e7 @indutny zlib: C++ style fixes
indutny authored
239 static Handle<Value> New(const Arguments& args) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
240 HandleScope scope;
44eb279 zlib: don't use C++ templates
ssuda authored
241 if (args.Length() < 1 || !args[0]->IsInt32()) {
242 return ThrowException(Exception::TypeError(String::New("Bad argument")));
243 }
244 node_zlib_mode mode = (node_zlib_mode) args[0]->Int32Value();
245
246 if (mode < DEFLATE || mode > UNZIP) {
247 return ThrowException(Exception::TypeError(String::New("Bad argument")));
248 }
249
250 ZCtx *ctx = new ZCtx(mode);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
251 ctx->Wrap(args.This());
252 return args.This();
253 }
254
255 // just pull the ints out of the args and call the other Init
07701e7 @indutny zlib: C++ style fixes
indutny authored
256 static Handle<Value> Init(const Arguments& args) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
257 HandleScope scope;
258
e609195 @indutny [zlib] added dictionary support
indutny authored
259 assert((args.Length() == 4 || args.Length() == 5) &&
260 "init(windowBits, level, memLevel, strategy, [dictionary])");
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
261
44eb279 zlib: don't use C++ templates
ssuda authored
262 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
263
264 int windowBits = args[0]->Uint32Value();
265 assert((windowBits >= 8 && windowBits <= 15) && "invalid windowBits");
266
267 int level = args[1]->Uint32Value();
268 assert((level >= -1 && level <= 9) && "invalid compression level");
269
270 int memLevel = args[2]->Uint32Value();
271 assert((memLevel >= 1 && memLevel <= 9) && "invalid memlevel");
272
273 int strategy = args[3]->Uint32Value();
274 assert((strategy == Z_FILTERED ||
275 strategy == Z_HUFFMAN_ONLY ||
276 strategy == Z_RLE ||
277 strategy == Z_FIXED ||
278 strategy == Z_DEFAULT_STRATEGY) && "invalid strategy");
279
e609195 @indutny [zlib] added dictionary support
indutny authored
280 char* dictionary = NULL;
281 size_t dictionary_len = 0;
282 if (args.Length() >= 5 && Buffer::HasInstance(args[4])) {
283 Local<Object> dictionary_ = args[4]->ToObject();
284
285 dictionary_len = Buffer::Length(dictionary_);
286 dictionary = new char[dictionary_len];
287
288 memcpy(dictionary, Buffer::Data(dictionary_), dictionary_len);
289 }
290
291 Init(ctx, level, windowBits, memLevel, strategy,
292 dictionary, dictionary_len);
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
293 SetDictionary(ctx);
294 return Undefined();
295 }
296
297 static Handle<Value> Reset(const Arguments &args) {
298 HandleScope scope;
299
44eb279 zlib: don't use C++ templates
ssuda authored
300 ZCtx *ctx = ObjectWrap::Unwrap<ZCtx>(args.This());
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
301
302 Reset(ctx);
303 SetDictionary(ctx);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
304 return Undefined();
305 }
306
07701e7 @indutny zlib: C++ style fixes
indutny authored
307 static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
9e6957b @indutny Merge branch 'v0.6'
indutny authored
308 int strategy, char* dictionary, size_t dictionary_len) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
309 ctx->level_ = level;
310 ctx->windowBits_ = windowBits;
311 ctx->memLevel_ = memLevel;
312 ctx->strategy_ = strategy;
313
314 ctx->strm_.zalloc = Z_NULL;
315 ctx->strm_.zfree = Z_NULL;
316 ctx->strm_.opaque = Z_NULL;
317
318 ctx->flush_ = Z_NO_FLUSH;
319
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
320 ctx->err_ = Z_OK;
321
44eb279 zlib: don't use C++ templates
ssuda authored
322 if (ctx->mode_ == GZIP || ctx->mode_ == GUNZIP) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
323 ctx->windowBits_ += 16;
324 }
325
44eb279 zlib: don't use C++ templates
ssuda authored
326 if (ctx->mode_ == UNZIP) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
327 ctx->windowBits_ += 32;
328 }
329
44eb279 zlib: don't use C++ templates
ssuda authored
330 if (ctx->mode_ == DEFLATERAW || ctx->mode_ == INFLATERAW) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
331 ctx->windowBits_ *= -1;
332 }
333
44eb279 zlib: don't use C++ templates
ssuda authored
334 switch (ctx->mode_) {
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
335 case DEFLATE:
336 case GZIP:
337 case DEFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
338 ctx->err_ = deflateInit2(&ctx->strm_,
339 ctx->level_,
340 Z_DEFLATED,
341 ctx->windowBits_,
342 ctx->memLevel_,
343 ctx->strategy_);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
344 break;
345 case INFLATE:
346 case GUNZIP:
347 case INFLATERAW:
348 case UNZIP:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
349 ctx->err_ = inflateInit2(&ctx->strm_, ctx->windowBits_);
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
350 break;
351 default:
352 assert(0 && "wtf?");
353 }
354
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
355 if (ctx->err_ != Z_OK) {
356 ZCtx::Error(ctx, "Init error");
357 }
358
e609195 @indutny [zlib] added dictionary support
indutny authored
359
360 ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
361 ctx->dictionary_len_ = dictionary_len;
362
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
363 ctx->write_in_progress_ = false;
364 ctx->init_done_ = true;
365 }
366
367 static void SetDictionary(ZCtx* ctx) {
368 if (ctx->dictionary_ == NULL) return;
e609195 @indutny [zlib] added dictionary support
indutny authored
369
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
370 ctx->err_ = Z_OK;
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
371
44eb279 zlib: don't use C++ templates
ssuda authored
372 switch (ctx->mode_) {
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
373 case DEFLATE:
374 case DEFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
375 ctx->err_ = deflateSetDictionary(&ctx->strm_,
376 ctx->dictionary_,
377 ctx->dictionary_len_);
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
378 break;
379 default:
380 break;
e609195 @indutny [zlib] added dictionary support
indutny authored
381 }
382
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
383 if (ctx->err_ != Z_OK) {
384 ZCtx::Error(ctx, "Failed to set dictionary");
385 }
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
386 }
387
388 static void Reset(ZCtx* ctx) {
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
389 ctx->err_ = Z_OK;
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
390
44eb279 zlib: don't use C++ templates
ssuda authored
391 switch (ctx->mode_) {
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
392 case DEFLATE:
393 case DEFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
394 ctx->err_ = deflateReset(&ctx->strm_);
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
395 break;
396 case INFLATE:
397 case INFLATERAW:
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
398 ctx->err_ = inflateReset(&ctx->strm_);
71ae175 @indutny zlib: reset() method for deflate/inflate streams
indutny authored
399 break;
400 default:
401 break;
402 }
403
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
404 if (ctx->err_ != Z_OK) {
405 ZCtx::Error(ctx, "Failed to reset stream");
406 }
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
407 }
408
409 private:
410
411 bool init_done_;
412
413 z_stream strm_;
414 int level_;
415 int windowBits_;
416 int memLevel_;
417 int strategy_;
418
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
419 int err_;
420
e609195 @indutny [zlib] added dictionary support
indutny authored
421 Bytef* dictionary_;
422 size_t dictionary_len_;
423
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
424 int flush_;
425
426 int chunk_size_;
8cca30f @isaacs zlib binding cleanup
isaacs authored
427
428 bool write_in_progress_;
429
430 uv_work_t work_req_;
44eb279 zlib: don't use C++ templates
ssuda authored
431 node_zlib_mode mode_;
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
432 };
433
434
435 void InitZlib(Handle<Object> target) {
436 HandleScope scope;
437
44eb279 zlib: don't use C++ templates
ssuda authored
438 Local<FunctionTemplate> z = FunctionTemplate::New(ZCtx::New);
439
440 z->InstanceTemplate()->SetInternalFieldCount(1);
441
442 NODE_SET_PROTOTYPE_METHOD(z, "write", ZCtx::Write);
443 NODE_SET_PROTOTYPE_METHOD(z, "init", ZCtx::Init);
444 NODE_SET_PROTOTYPE_METHOD(z, "reset", ZCtx::Reset);
445
446 z->SetClassName(String::NewSymbol("Zlib"));
447 target->Set(String::NewSymbol("Zlib"), z->GetFunction());
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
448
449 callback_sym = NODE_PSYMBOL("callback");
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
450 onerror_sym = NODE_PSYMBOL("onerror");
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
451
452 NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH);
453 NODE_DEFINE_CONSTANT(target, Z_PARTIAL_FLUSH);
454 NODE_DEFINE_CONSTANT(target, Z_SYNC_FLUSH);
455 NODE_DEFINE_CONSTANT(target, Z_FULL_FLUSH);
456 NODE_DEFINE_CONSTANT(target, Z_FINISH);
457 NODE_DEFINE_CONSTANT(target, Z_BLOCK);
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
458
459 // return/error codes
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
460 NODE_DEFINE_CONSTANT(target, Z_OK);
461 NODE_DEFINE_CONSTANT(target, Z_STREAM_END);
462 NODE_DEFINE_CONSTANT(target, Z_NEED_DICT);
463 NODE_DEFINE_CONSTANT(target, Z_ERRNO);
464 NODE_DEFINE_CONSTANT(target, Z_STREAM_ERROR);
465 NODE_DEFINE_CONSTANT(target, Z_DATA_ERROR);
466 NODE_DEFINE_CONSTANT(target, Z_MEM_ERROR);
467 NODE_DEFINE_CONSTANT(target, Z_BUF_ERROR);
468 NODE_DEFINE_CONSTANT(target, Z_VERSION_ERROR);
01d46f3 @isaacs Fix #3052 Handle errors properly in zlib
isaacs authored
469
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
470 NODE_DEFINE_CONSTANT(target, Z_NO_COMPRESSION);
471 NODE_DEFINE_CONSTANT(target, Z_BEST_SPEED);
472 NODE_DEFINE_CONSTANT(target, Z_BEST_COMPRESSION);
473 NODE_DEFINE_CONSTANT(target, Z_DEFAULT_COMPRESSION);
474 NODE_DEFINE_CONSTANT(target, Z_FILTERED);
475 NODE_DEFINE_CONSTANT(target, Z_HUFFMAN_ONLY);
476 NODE_DEFINE_CONSTANT(target, Z_RLE);
477 NODE_DEFINE_CONSTANT(target, Z_FIXED);
478 NODE_DEFINE_CONSTANT(target, Z_DEFAULT_STRATEGY);
479 NODE_DEFINE_CONSTANT(target, ZLIB_VERNUM);
480
44eb279 zlib: don't use C++ templates
ssuda authored
481 NODE_DEFINE_CONSTANT(target, DEFLATE);
482 NODE_DEFINE_CONSTANT(target, INFLATE);
483 NODE_DEFINE_CONSTANT(target, GZIP);
484 NODE_DEFINE_CONSTANT(target, GUNZIP);
485 NODE_DEFINE_CONSTANT(target, DEFLATERAW);
486 NODE_DEFINE_CONSTANT(target, INFLATERAW);
487 NODE_DEFINE_CONSTANT(target, UNZIP);
488
5b8e1da @isaacs Initial pass at zlib bindings
isaacs authored
489 target->Set(String::NewSymbol("ZLIB_VERSION"), String::New(ZLIB_VERSION));
490 }
491
492 } // namespace node
493
cdcb111 @bnoordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored
494 NODE_MODULE(node_zlib, node::InitZlib)
Something went wrong with that request. Please try again.