Skip to content

Commit c5440af

Browse files
committed
squash! follow an alternative approach
Throw an Error synchronously instead of fiddling with 'error' events.
1 parent 7d78533 commit c5440af

File tree

4 files changed

+28
-46
lines changed

4 files changed

+28
-46
lines changed

doc/api/zlib.md

+4
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ added: v0.5.8
437437

438438
Returns a new [DeflateRaw][] object with an [options][].
439439

440+
**Note:** zlib library rejects requests for 256-byte windows (i.e.,
441+
`{ windowBits: 8 }` in `options`). An `Error` will be thrown when creating
442+
a [DeflateRaw][] object with this specific value of the `windowBits` option.
443+
440444
## zlib.createGunzip([options])
441445
<!-- YAML
442446
added: v0.5.8

lib/zlib.js

+8-23
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,7 @@ function zlibOnError(message, errno) {
146146
var error = new Error(message);
147147
error.errno = errno;
148148
error.code = codes[errno];
149-
150-
if (this._initSuccess) {
151-
this.emit('error', error);
152-
} else {
153-
process.nextTick(() => {
154-
this.emit('error', error);
155-
});
156-
}
149+
this.emit('error', error);
157150
}
158151

159152
function flushCallback(level, strategy, callback) {
@@ -229,7 +222,6 @@ class Zlib extends Transform {
229222
this._handle = new binding.Zlib(mode);
230223
this._handle.onerror = zlibOnError.bind(this);
231224
this._hadError = false;
232-
this._initSuccess = false;
233225

234226
var level = constants.Z_DEFAULT_COMPRESSION;
235227
if (typeof opts.level === 'number') level = opts.level;
@@ -243,18 +235,13 @@ class Zlib extends Transform {
243235
var memLevel = constants.Z_DEFAULT_MEMLEVEL;
244236
if (typeof opts.memLevel === 'number') memLevel = opts.memLevel;
245237

246-
this._initSuccess = this._handle.init(windowBits,
247-
level,
248-
memLevel,
249-
strategy,
250-
opts.dictionary);
251-
252-
if (this._initSuccess) {
253-
this._buffer = Buffer.allocUnsafe(this._chunkSize);
254-
} else {
255-
this._buffer = null;
256-
}
238+
this._handle.init(windowBits,
239+
level,
240+
memLevel,
241+
strategy,
242+
opts.dictionary);
257243

244+
this._buffer = Buffer.allocUnsafe(this._chunkSize);
258245
this._offset = 0;
259246
this._level = level;
260247
this._strategy = strategy;
@@ -486,9 +473,7 @@ function _close(engine, callback) {
486473
if (!engine._handle)
487474
return;
488475

489-
if (engine._initSuccess)
490-
engine._handle.close();
491-
476+
engine._handle.close();
492477
engine._handle = null;
493478
}
494479

src/node_zlib.cc

+10-15
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
namespace node {
4141

4242
using v8::Array;
43-
using v8::Boolean;
4443
using v8::Context;
4544
using v8::FunctionCallbackInfo;
4645
using v8::FunctionTemplate;
@@ -481,11 +480,9 @@ class ZCtx : public AsyncWrap {
481480
memcpy(dictionary, Buffer::Data(dictionary_), dictionary_len);
482481
}
483482

484-
bool result = Init(ctx, level, windowBits, memLevel, strategy,
485-
dictionary, dictionary_len);
483+
Init(ctx, level, windowBits, memLevel, strategy,
484+
dictionary, dictionary_len);
486485
SetDictionary(ctx);
487-
488-
args.GetReturnValue().Set(Boolean::New(args.GetIsolate(), result));
489486
}
490487

491488
static void Params(const FunctionCallbackInfo<Value>& args) {
@@ -502,7 +499,7 @@ class ZCtx : public AsyncWrap {
502499
SetDictionary(ctx);
503500
}
504501

505-
static bool Init(ZCtx *ctx, int level, int windowBits, int memLevel,
502+
static void Init(ZCtx *ctx, int level, int windowBits, int memLevel,
506503
int strategy, char* dictionary, size_t dictionary_len) {
507504
ctx->level_ = level;
508505
ctx->windowBits_ = windowBits;
@@ -554,21 +551,19 @@ class ZCtx : public AsyncWrap {
554551
CHECK(0 && "wtf?");
555552
}
556553

557-
if (ctx->err_ != Z_OK) {
558-
ZCtx::Error(ctx, "Init error");
559-
if (dictionary != nullptr)
560-
delete[] dictionary;
561-
return false;
562-
}
563-
564-
565554
ctx->dictionary_ = reinterpret_cast<Bytef *>(dictionary);
566555
ctx->dictionary_len_ = dictionary_len;
567556

568557
ctx->write_in_progress_ = false;
569558
ctx->init_done_ = true;
570559

571-
return true;
560+
if (ctx->err_ != Z_OK) {
561+
if (dictionary != nullptr) {
562+
delete[] dictionary;
563+
ctx->dictionary_ = nullptr;
564+
}
565+
ctx->env()->ThrowError("Init error");
566+
}
572567
}
573568

574569
static void SetDictionary(ZCtx* ctx) {
+6-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44

55
const assert = require('assert');
66
const zlib = require('zlib');
77

8-
// For raw deflate or gzip encoding, a request for a 256-byte window is
9-
// rejected as invalid, since only zlib headers provide means of transmitting
10-
// the window size to the decompressor.
8+
// For raw deflate encoding, requests for 256-byte windows are rejected as
9+
// invalid by zlib.
1110
// (http://zlib.net/manual.html#Advanced)
12-
const deflate = zlib.createDeflateRaw({ windowBits: 8 });
13-
deflate.on('error', common.mustCall((error) => {
14-
assert.ok(/Init error/.test(error));
15-
}));
11+
assert.throws(() => {
12+
zlib.createDeflateRaw({ windowBits: 8 });
13+
}, /Init error/);

0 commit comments

Comments
 (0)