Skip to content

HTTPS clone URL

Subversion checkout URL

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