Skip to content
This repository was archived by the owner on Jun 6, 2021. It is now read-only.

Commit 27d958e

Browse files
committed
Set coders as sync/async in their constructors
1 parent aa5a007 commit 27d958e

File tree

5 files changed

+47
-56
lines changed

5 files changed

+47
-56
lines changed

index.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ const { StreamEncode, StreamDecode } = require('./build/bindings/iltorb.node');
1111
const { Transform } = require('stream');
1212

1313
class TransformStreamEncode extends Transform {
14-
constructor(params={}, sync=false) {
14+
constructor(params={}, async=true) {
1515
super();
16-
this.sync = sync;
1716
this.encoding = false;
1817
this.corked = false;
1918
this.flushing = false;
20-
this.encoder = new StreamEncode(params);
19+
this.encoder = new StreamEncode(async, params);
2120
}
2221

2322
_transform(chunk, encoding, next) {
@@ -32,7 +31,7 @@ class TransformStreamEncode extends Transform {
3231
if (this.flushing) {
3332
this.flush(true);
3433
}
35-
}, !this.sync);
34+
});
3635
}
3736

3837
_flush(done) {
@@ -42,7 +41,7 @@ class TransformStreamEncode extends Transform {
4241
}
4342
this._push(output);
4443
done();
45-
}, !this.sync);
44+
});
4645
}
4746

4847
_push(output) {
@@ -82,10 +81,9 @@ class TransformStreamEncode extends Transform {
8281
}
8382

8483
class TransformStreamDecode extends Transform {
85-
constructor(sync=false) {
84+
constructor(async=true) {
8685
super();
87-
this.sync = sync;
88-
this.decoder = new StreamDecode();
86+
this.decoder = new StreamDecode(async);
8987
}
9088

9189
_transform(chunk, encoding, next) {
@@ -95,7 +93,7 @@ class TransformStreamDecode extends Transform {
9593
}
9694
this._push(output);
9795
next();
98-
}, !this.sync);
96+
});
9997
}
10098

10199
_flush(done) {
@@ -105,7 +103,7 @@ class TransformStreamDecode extends Transform {
105103
}
106104
this._push(output);
107105
done();
108-
}, !this.sync);
106+
});
109107
}
110108

111109
_push(output) {
@@ -214,7 +212,7 @@ function compressSync(input, params) {
214212
params = {};
215213
}
216214
params = Object.assign({}, params, {size_hint: input.length});
217-
const stream = new TransformStreamEncode(params, true);
215+
const stream = new TransformStreamEncode(params, false);
218216
const chunks = [];
219217
let length = 0;
220218
stream.on('error', function(e) {
@@ -232,7 +230,7 @@ function decompressSync(input) {
232230
if (!Buffer.isBuffer(input)) {
233231
throw new Error('Brotli input is not a buffer.');
234232
}
235-
const stream = new TransformStreamDecode(true);
233+
const stream = new TransformStreamDecode(false);
236234
const chunks = [];
237235
let length = 0;
238236
stream.on('error', function(e) {

src/dec/stream_decode.cc

+12-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
napi_ref StreamDecode::constructor;
44

5-
StreamDecode::StreamDecode(napi_env env) {
5+
StreamDecode::StreamDecode(napi_env env, napi_value async) {
6+
napi_get_value_bool(env, async, &isAsync);
67
state = BrotliDecoderCreateInstance(Allocator::Alloc, Allocator::Free, &alloc);
78
alloc.ReportMemoryToV8(env);
89
}
@@ -29,11 +30,12 @@ napi_value StreamDecode::Init(napi_env env, napi_value exports) {
2930
}
3031

3132
napi_value StreamDecode::New(napi_env env, napi_callback_info info) {
32-
size_t argc = 0;
33+
size_t argc = 1;
34+
napi_value argv[1];
3335
napi_value jsthis;
34-
napi_get_cb_info(env, info, &argc, nullptr, &jsthis, nullptr);
36+
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
3537

36-
StreamDecode* obj = new StreamDecode(env);
38+
StreamDecode* obj = new StreamDecode(env, argv[0]);
3739

3840
napi_wrap(env,
3941
jsthis,
@@ -46,8 +48,8 @@ napi_value StreamDecode::New(napi_env env, napi_callback_info info) {
4648
}
4749

4850
napi_value StreamDecode::Transform(napi_env env, napi_callback_info info) {
49-
size_t argc = 3;
50-
napi_value argv[3];
51+
size_t argc = 2;
52+
napi_value argv[2];
5153
napi_value jsthis;
5254
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
5355

@@ -59,10 +61,7 @@ napi_value StreamDecode::Transform(napi_env env, napi_callback_info info) {
5961
napi_create_reference(env, argv[0], 1, &obj->bufref);
6062
napi_create_reference(env, argv[1], 1, &obj->cbref);
6163

62-
bool isAsync;
63-
napi_get_value_bool(env, argv[2], &isAsync);
64-
65-
if (isAsync) {
64+
if (obj->isAsync) {
6665
napi_value resource_name;
6766
napi_create_string_utf8(env, "DecodeResource", NAPI_AUTO_LENGTH, &resource_name);
6867

@@ -85,8 +84,8 @@ napi_value StreamDecode::Transform(napi_env env, napi_callback_info info) {
8584
}
8685

8786
napi_value StreamDecode::Flush(napi_env env, napi_callback_info info) {
88-
size_t argc = 2;
89-
napi_value argv[2];
87+
size_t argc = 1;
88+
napi_value argv[1];
9089
napi_value jsthis;
9190
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
9291

@@ -95,13 +94,10 @@ napi_value StreamDecode::Flush(napi_env env, napi_callback_info info) {
9594

9695
napi_create_reference(env, argv[0], 1, &obj->cbref);
9796

98-
bool isAsync;
99-
napi_get_value_bool(env, argv[1], &isAsync);
100-
10197
obj->next_in = nullptr;
10298
obj->available_in = 0;
10399

104-
if (isAsync) {
100+
if (obj->isAsync) {
105101
napi_value resource_name;
106102
napi_create_string_utf8(env, "DecodeResource", NAPI_AUTO_LENGTH, &resource_name);
107103

src/dec/stream_decode.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ class StreamDecode : public StreamCoder {
1111
static napi_value Init(napi_env env, napi_value exports);
1212
static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);
1313

14-
napi_async_work work = NULL;
15-
napi_ref bufref = NULL;
16-
napi_ref cbref = NULL;
14+
bool isAsync = true;
15+
bool hasError = false;
16+
BrotliDecoderState* state;
1717
const uint8_t* next_in;
1818
size_t available_in;
19-
BrotliDecoderState* state;
20-
bool hasError = false;
19+
napi_ref bufref = NULL;
20+
napi_ref cbref = NULL;
21+
napi_async_work work = NULL;
2122

2223
private:
23-
explicit StreamDecode(napi_env env);
24+
explicit StreamDecode(napi_env env, napi_value async);
2425

2526
static napi_value New(napi_env env, napi_callback_info info);
2627
static napi_value Transform(napi_env env, napi_callback_info info);

src/enc/stream_encode.cc

+11-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
napi_ref StreamEncode::constructor;
44

5-
StreamEncode::StreamEncode(napi_env env, napi_value params) {
5+
StreamEncode::StreamEncode(napi_env env, napi_value async, napi_value params) {
6+
napi_get_value_bool(env, async, &isAsync);
67
state = BrotliEncoderCreateInstance(Allocator::Alloc, Allocator::Free, &alloc);
78

89
SetParameter(env, params, "mode", BROTLI_PARAM_MODE);
@@ -66,12 +67,12 @@ napi_value StreamEncode::Init(napi_env env, napi_value exports) {
6667
}
6768

6869
napi_value StreamEncode::New(napi_env env, napi_callback_info info) {
69-
size_t argc = 1;
70-
napi_value argv[1];
70+
size_t argc = 2;
71+
napi_value argv[2];
7172
napi_value jsthis;
7273
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
7374

74-
StreamEncode* obj = new StreamEncode(env, argv[0]);
75+
StreamEncode* obj = new StreamEncode(env, argv[0], argv[1]);
7576

7677
napi_wrap(env,
7778
jsthis,
@@ -84,8 +85,8 @@ napi_value StreamEncode::New(napi_env env, napi_callback_info info) {
8485
}
8586

8687
napi_value StreamEncode::Transform(napi_env env, napi_callback_info info) {
87-
size_t argc = 3;
88-
napi_value argv[3];
88+
size_t argc = 2;
89+
napi_value argv[2];
8990
napi_value jsthis;
9091
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
9192

@@ -97,12 +98,9 @@ napi_value StreamEncode::Transform(napi_env env, napi_callback_info info) {
9798
napi_create_reference(env, argv[0], 1, &obj->bufref);
9899
napi_create_reference(env, argv[1], 1, &obj->cbref);
99100

100-
bool isAsync;
101-
napi_get_value_bool(env, argv[2], &isAsync);
102-
103101
obj->op = BROTLI_OPERATION_PROCESS;
104102

105-
if (isAsync) {
103+
if (obj->isAsync) {
106104
napi_value resource_name;
107105
napi_create_string_utf8(env, "EncodeResource", NAPI_AUTO_LENGTH, &resource_name);
108106

@@ -125,8 +123,8 @@ napi_value StreamEncode::Transform(napi_env env, napi_callback_info info) {
125123
}
126124

127125
napi_value StreamEncode::Flush(napi_env env, napi_callback_info info) {
128-
size_t argc = 3;
129-
napi_value argv[3];
126+
size_t argc = 2;
127+
napi_value argv[2];
130128
napi_value jsthis;
131129
napi_get_cb_info(env, info, &argc, argv, &jsthis, nullptr);
132130

@@ -138,17 +136,14 @@ napi_value StreamEncode::Flush(napi_env env, napi_callback_info info) {
138136

139137
napi_create_reference(env, argv[1], 1, &obj->cbref);
140138

141-
bool isAsync;
142-
napi_get_value_bool(env, argv[2], &isAsync);
143-
144139
obj->op = isFinish
145140
? BROTLI_OPERATION_FINISH
146141
: BROTLI_OPERATION_FLUSH;
147142

148143
obj->next_in = nullptr;
149144
obj->available_in = 0;
150145

151-
if (isAsync) {
146+
if (obj->isAsync) {
152147
napi_value resource_name;
153148
napi_create_string_utf8(env, "EncodeResource", NAPI_AUTO_LENGTH, &resource_name);
154149

src/enc/stream_encode.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ class StreamEncode : public StreamCoder {
1111
static napi_value Init(napi_env env, napi_value exports);
1212
static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);
1313

14-
napi_async_work work = NULL;
15-
napi_ref bufref = NULL;
16-
napi_ref cbref = NULL;
14+
bool isAsync = true;
15+
bool hasError = false;
16+
BrotliEncoderState* state;
1717
BrotliEncoderOperation op;
1818
const uint8_t* next_in;
1919
size_t available_in;
20-
BrotliEncoderState* state;
21-
bool hasError = false;
20+
napi_ref bufref = NULL;
21+
napi_ref cbref = NULL;
22+
napi_async_work work = NULL;
2223

2324
private:
24-
explicit StreamEncode(napi_env env, napi_value params);
25+
explicit StreamEncode(napi_env env, napi_value async, napi_value params);
2526
void SetParameter(napi_env env, napi_value params, const char* key, BrotliEncoderParameter p);
2627

2728
static napi_value New(napi_env env, napi_callback_info info);

0 commit comments

Comments
 (0)