@@ -60,12 +60,11 @@ int StreamBase::Shutdown(const FunctionCallbackInfo<Value>& args) {
60
60
inline void SetWriteResultPropertiesOnWrapObject (
61
61
Environment* env,
62
62
Local<Object> req_wrap_obj,
63
- const StreamWriteResult& res,
64
- size_t bytes) {
63
+ const StreamWriteResult& res) {
65
64
req_wrap_obj->Set (
66
65
env->context (),
67
66
env->bytes_string (),
68
- Number::New (env->isolate (), bytes)).FromJust ();
67
+ Number::New (env->isolate (), res. bytes )).FromJust ();
69
68
req_wrap_obj->Set (
70
69
env->context (),
71
70
env->async (),
@@ -91,7 +90,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
91
90
MaybeStackBuffer<uv_buf_t , 16 > bufs (count);
92
91
93
92
size_t storage_size = 0 ;
94
- uint32_t bytes = 0 ;
95
93
size_t offset;
96
94
97
95
if (!all_buffers) {
@@ -123,7 +121,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
123
121
Local<Value> chunk = chunks->Get (i);
124
122
bufs[i].base = Buffer::Data (chunk);
125
123
bufs[i].len = Buffer::Length (chunk);
126
- bytes += bufs[i].len ;
127
124
}
128
125
}
129
126
@@ -140,7 +137,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
140
137
if (Buffer::HasInstance (chunk)) {
141
138
bufs[i].base = Buffer::Data (chunk);
142
139
bufs[i].len = Buffer::Length (chunk);
143
- bytes += bufs[i].len ;
144
140
continue ;
145
141
}
146
142
@@ -160,12 +156,11 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
160
156
bufs[i].base = str_storage;
161
157
bufs[i].len = str_size;
162
158
offset += str_size;
163
- bytes += str_size;
164
159
}
165
160
}
166
161
167
162
StreamWriteResult res = Write (*bufs, count, nullptr , req_wrap_obj);
168
- SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res, bytes );
163
+ SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res);
169
164
if (res.wrap != nullptr && storage) {
170
165
res.wrap ->SetAllocatedStorage (storage.release (), storage_size);
171
166
}
@@ -193,7 +188,7 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
193
188
194
189
if (res.async )
195
190
req_wrap_obj->Set (env->context (), env->buffer_string (), args[1 ]).FromJust ();
196
- SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res, buf. len );
191
+ SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res);
197
192
198
193
return res.err ;
199
194
}
@@ -228,6 +223,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
228
223
// Try writing immediately if write size isn't too big
229
224
char stack_storage[16384 ]; // 16kb
230
225
size_t data_size;
226
+ size_t synchronously_written = 0 ;
231
227
uv_buf_t buf;
232
228
233
229
bool try_write = storage_size <= sizeof (stack_storage) &&
@@ -243,7 +239,11 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
243
239
uv_buf_t * bufs = &buf;
244
240
size_t count = 1 ;
245
241
err = DoTryWrite (&bufs, &count);
246
- bytes_written_ += data_size;
242
+ // Keep track of the bytes written here, because we're taking a shortcut
243
+ // by using `DoTryWrite()` directly instead of using the utilities
244
+ // provided by `Write()`.
245
+ synchronously_written = count == 0 ? data_size : data_size - buf.len ;
246
+ bytes_written_ += synchronously_written;
247
247
248
248
// Immediate failure or success
249
249
if (err != 0 || count == 0 ) {
@@ -299,8 +299,9 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
299
299
}
300
300
301
301
StreamWriteResult res = Write (&buf, 1 , send_handle, req_wrap_obj);
302
+ res.bytes += synchronously_written;
302
303
303
- SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res, data_size );
304
+ SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res);
304
305
if (res.wrap != nullptr ) {
305
306
res.wrap ->SetAllocatedStorage (data.release (), data_size);
306
307
}
0 commit comments