ultimate-express 1.1.6 → 1.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/response.js +17 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-express",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "The Ultimate Express. Fastest http server with full Express compatibility, based on uWebSockets.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/response.js CHANGED
@@ -64,6 +64,8 @@ module.exports = class Response extends Writable {
64
64
  this.locals = {};
65
65
  this.aborted = false;
66
66
  this.statusCode = 200;
67
+ this.chunkedTransfer = true;
68
+ this.totalSize = 0;
67
69
  this.headers = {
68
70
  'keep-alive': 'timeout=10'
69
71
  };
@@ -98,8 +100,6 @@ module.exports = class Response extends Writable {
98
100
  err.code = 'ECONNABORTED';
99
101
  return this.destroy(err);
100
102
  }
101
- const isString = typeof chunk === 'string';
102
- let isChunkedTransfer = true, totalSize;
103
103
  this._res.cork(() => {
104
104
  if(!this.headersSent) {
105
105
  this.writeHead(this.statusCode);
@@ -107,14 +107,14 @@ module.exports = class Response extends Writable {
107
107
  for(const header in this.headers) {
108
108
  if(header === 'content-length') {
109
109
  // if content-length is set, disable chunked transfer encoding, since size is known
110
- isChunkedTransfer = false;
111
- totalSize = parseInt(this.headers[header]);
110
+ this.chunkedTransfer = false;
111
+ this.totalSize = parseInt(this.headers[header]);
112
112
  continue;
113
113
  }
114
114
  this._res.writeHeader(header, this.headers[header]);
115
115
  }
116
116
  if(!this.headers['content-type']) {
117
- this._res.writeHeader('content-type', 'text/html' + (isString ? `; charset=utf-8` : ''));
117
+ this._res.writeHeader('content-type', 'text/html' + (typeof chunk === 'string' ? `; charset=utf-8` : ''));
118
118
  }
119
119
  this.headersSent = true;
120
120
  }
@@ -122,14 +122,14 @@ module.exports = class Response extends Writable {
122
122
  chunk = Buffer.from(chunk);
123
123
  chunk = chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength);
124
124
  }
125
- if(isChunkedTransfer) {
125
+ if(this.chunkedTransfer) {
126
126
  // chunked transfer encoding
127
127
  this._res.write(chunk);
128
128
  callback();
129
129
  } else {
130
130
  // fixed size transfer encoding
131
131
  const lastOffset = this._res.getWriteOffset();
132
- const [ok, done] = this._res.tryEnd(chunk, totalSize);
132
+ const [ok, done] = this._res.tryEnd(chunk, this.totalSize);
133
133
  if(done) {
134
134
  this.destroy();
135
135
  this.socket.emit('close');
@@ -139,7 +139,10 @@ module.exports = class Response extends Writable {
139
139
  if(!ok) {
140
140
  // wait until uWS is ready to accept more data
141
141
  this._res.onWritable((offset) => {
142
- const [ok, done] = this._res.tryEnd(chunk.slice(offset - lastOffset), totalSize);
142
+ if(this.aborted) {
143
+ return true;
144
+ }
145
+ const [ok, done] = this._res.tryEnd(chunk.slice(offset - lastOffset), this.totalSize);
143
146
  if(done) {
144
147
  this.destroy();
145
148
  this.socket.emit('close');
@@ -147,6 +150,8 @@ module.exports = class Response extends Writable {
147
150
  } else if(ok) {
148
151
  callback();
149
152
  }
153
+
154
+ return ok;
150
155
  });
151
156
  } else {
152
157
  callback();
@@ -742,7 +747,10 @@ function pipeStreamOverResponse(res, readStream, totalSize, callback) {
742
747
  res._res.ab = ab;
743
748
  res._res.abOffset = lastOffset;
744
749
 
745
- res._res.onWritable((offset) => {
750
+ res._res.onWritable((offset) => {
751
+ if(res.aborted) {
752
+ return true;
753
+ }
746
754
  const [ok, done] = res._res.tryEnd(res._res.ab.slice(offset - res._res.abOffset), totalSize);
747
755
  if (done) {
748
756
  readStream.destroy();