ultimate-express 2.0.1 → 2.0.2

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.
package/README.md CHANGED
@@ -57,7 +57,7 @@ For full table with other runtimes, check [here](https://github.com/dimdenGD/bun
57
57
  | express | 10,411.313 | 11,245.57 | 10,598.74 | 9,389.63 |
58
58
 
59
59
  Other benchmarks:
60
- - [TechEmpower / FrameworkBenchmarks](https://www.techempower.com/benchmarks/#hw=ph&test=composite&section=data-r23&l=zik0sf-cn3)
60
+ - [TechEmpower / FrameworkBenchmarks](https://www.techempower.com/benchmarks/#section=data-r23&test=plaintext&l=zik0sf-pa7)
61
61
  - [the-benchmarker / web-frameworks](https://web-frameworks-benchmark.netlify.app/result?l=javascript)
62
62
 
63
63
  ### Performance on real-world application
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-express",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
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": {
@@ -86,8 +86,9 @@ function static(root, options) {
86
86
 
87
87
  if(stat.isDirectory()) {
88
88
  if(!req.endsWithSlash) {
89
- if(options.redirect) return res.redirect(301, req._originalPath + '/');
90
- else {
89
+ if(options.redirect) {
90
+ return res.redirect(301, req._originalPath + '/', true);
91
+ } else {
91
92
  if(!options.fallthrough) {
92
93
  res.status(404);
93
94
  return next(new Error('Not found'));
package/src/request.js CHANGED
@@ -135,6 +135,10 @@ module.exports = class Request extends Readable {
135
135
  return match ? match[0] : '';
136
136
  }
137
137
 
138
+ set baseUrl(x) {
139
+ return this._originalPath = x;
140
+ }
141
+
138
142
  get #host() {
139
143
  const trust = this.app.get('trust proxy fn');
140
144
  if(!trust) {
package/src/response.js CHANGED
@@ -153,7 +153,7 @@ module.exports = class Response extends Writable {
153
153
  if (this.chunkedTransfer) {
154
154
  this.#pendingChunks.push(chunk);
155
155
  const size = this.#pendingChunks.reduce((acc, chunk) => acc + chunk.byteLength, 0);
156
- const now = Date.now();
156
+ const now = performance.now();
157
157
  // the first chunk is sent immediately (!this.#lastWriteChunkTime)
158
158
  // the other chunks are sent when watermark is reached (size >= HIGH_WATERMARK)
159
159
  // or if elapsed 50ms of last send (now - this.#lastWriteChunkTime > 50)
@@ -173,7 +173,7 @@ module.exports = class Response extends Writable {
173
173
  const size = this.#pendingChunks.reduce((acc, chunk) => acc + chunk.byteLength, 0);
174
174
  this._res.write(Buffer.concat(this.#pendingChunks, size));
175
175
  this.#pendingChunks = [];
176
- this.#lastWriteChunkTime = now;
176
+ this.#lastWriteChunkTime = performance.now();
177
177
  }
178
178
  });
179
179
  }, 50);
@@ -775,28 +775,44 @@ module.exports = class Response extends Writable {
775
775
  }
776
776
  return this.headers['location'] = encodeUrl(path);
777
777
  }
778
- redirect(status, url) {
778
+ redirect(status, url, forceHtml = false) {
779
779
  if(typeof status !== 'number' && !url) {
780
780
  url = status;
781
781
  status = 302;
782
782
  }
783
783
  this.location(url);
784
784
  this.status(status);
785
- this.headers['content-type'] = 'text/plain; charset=utf-8';
786
785
  let body;
787
786
  // Support text/{plain,html} by default
788
- this.format({
789
- text: function() {
790
- body = statuses.message[status] + '. Redirecting to ' + url
791
- },
792
- html: function() {
793
- let u = escapeHtml(url);
794
- body = '<p>' + statuses.message[status] + '. Redirecting to ' + u + '</p>'
795
- },
796
- default: function() {
797
- body = '';
798
- }
799
- });
787
+ if(forceHtml) {
788
+ this.set('Content-Type', 'text/html; charset=UTF-8');
789
+ body =
790
+ '<!DOCTYPE html>\n' +
791
+ '<html lang="en">\n' +
792
+ '<head>\n' +
793
+ '<meta charset="utf-8">\n' +
794
+ '<title>Redirecting</title>\n' +
795
+ '</head>\n' +
796
+ '<body>\n' +
797
+ `<pre>Redirecting to ${url.replaceAll("<", "&lt;").replaceAll(">", "&gt;")}</pre>\n` +
798
+ '</body>\n' +
799
+ '</html>\n';
800
+ } else {
801
+ this.format({
802
+ text: () => {
803
+ this.set('Content-Type', 'text/plain; charset=UTF-8');
804
+ body = statuses.message[status] + '. Redirecting to ' + url
805
+ },
806
+ html: () => {
807
+ this.set('Content-Type', 'text/html; charset=UTF-8');
808
+ body = `<p>${statuses.message[status]}. Redirecting to ${url.replaceAll("<", "&lt;").replaceAll(">", "&gt;")}</p>`;
809
+ },
810
+ default: () => {
811
+ this.set('Content-Type', 'text/plain; charset=UTF-8');
812
+ body = '';
813
+ }
814
+ });
815
+ }
800
816
  if (this.req.method === 'HEAD') {
801
817
  this.end();
802
818
  } else {