@@ -60,12 +60,11 @@ int StreamBase::Shutdown(const FunctionCallbackInfo<Value>& args) {
6060inline void SetWriteResultPropertiesOnWrapObject (
6161 Environment* env,
6262 Local<Object> req_wrap_obj,
63- const StreamWriteResult& res,
64- size_t bytes) {
63+ const StreamWriteResult& res) {
6564 req_wrap_obj->Set (
6665 env->context (),
6766 env->bytes_string (),
68- Number::New (env->isolate (), bytes)).FromJust ();
67+ Number::New (env->isolate (), res. bytes )).FromJust ();
6968 req_wrap_obj->Set (
7069 env->context (),
7170 env->async (),
@@ -91,7 +90,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
9190 MaybeStackBuffer<uv_buf_t , 16 > bufs (count);
9291
9392 size_t storage_size = 0 ;
94- uint32_t bytes = 0 ;
9593 size_t offset;
9694
9795 if (!all_buffers) {
@@ -123,7 +121,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
123121 Local<Value> chunk = chunks->Get (i);
124122 bufs[i].base = Buffer::Data (chunk);
125123 bufs[i].len = Buffer::Length (chunk);
126- bytes += bufs[i].len ;
127124 }
128125 }
129126
@@ -140,7 +137,6 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
140137 if (Buffer::HasInstance (chunk)) {
141138 bufs[i].base = Buffer::Data (chunk);
142139 bufs[i].len = Buffer::Length (chunk);
143- bytes += bufs[i].len ;
144140 continue ;
145141 }
146142
@@ -160,12 +156,11 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
160156 bufs[i].base = str_storage;
161157 bufs[i].len = str_size;
162158 offset += str_size;
163- bytes += str_size;
164159 }
165160 }
166161
167162 StreamWriteResult res = Write (*bufs, count, nullptr , req_wrap_obj);
168- SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res, bytes );
163+ SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res);
169164 if (res.wrap != nullptr && storage) {
170165 res.wrap ->SetAllocatedStorage (storage.release (), storage_size);
171166 }
@@ -193,7 +188,7 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
193188
194189 if (res.async )
195190 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);
197192
198193 return res.err ;
199194}
@@ -228,6 +223,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
228223 // Try writing immediately if write size isn't too big
229224 char stack_storage[16384 ]; // 16kb
230225 size_t data_size;
226+ size_t synchronously_written = 0 ;
231227 uv_buf_t buf;
232228
233229 bool try_write = storage_size <= sizeof (stack_storage) &&
@@ -243,7 +239,11 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
243239 uv_buf_t * bufs = &buf;
244240 size_t count = 1 ;
245241 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;
247247
248248 // Immediate failure or success
249249 if (err != 0 || count == 0 ) {
@@ -299,8 +299,9 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
299299 }
300300
301301 StreamWriteResult res = Write (&buf, 1 , send_handle, req_wrap_obj);
302+ res.bytes += synchronously_written;
302303
303- SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res, data_size );
304+ SetWriteResultPropertiesOnWrapObject (env, req_wrap_obj, res);
304305 if (res.wrap != nullptr ) {
305306 res.wrap ->SetAllocatedStorage (data.release (), data_size);
306307 }
0 commit comments