ultimate-express 1.2.9 → 1.2.11

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 +23 -29
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-express",
3
- "version": "1.2.9",
3
+ "version": "1.2.11",
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
@@ -197,26 +197,22 @@ module.exports = class Response extends Writable {
197
197
  this.set('etag', etagFn(data));
198
198
  }
199
199
  const fresh = this.req.fresh;
200
- if(fresh) {
201
- this._res.writeStatus('304');
202
- } else {
203
- this._res.writeStatus(this.statusCode.toString());
204
- }
200
+ this._res.writeStatus(fresh ? '304' : this.statusCode.toString());
205
201
  for(const header in this.headers) {
206
202
  if(header === 'content-length') {
207
203
  continue;
208
204
  }
209
205
  this._res.writeHeader(header, this.headers[header]);
210
206
  }
211
- if(fresh) {
212
- this.headersSent = true;
213
- this.socket.emit('close');
214
- return this._res.end();
215
- }
216
207
  if(!this.headers['content-type']) {
217
208
  this._res.writeHeader('content-type', 'text/html' + (typeof data === 'string' ? `; charset=utf-8` : ''));
218
209
  }
219
210
  this.headersSent = true;
211
+ if(fresh) {
212
+ this._res.end();
213
+ this.socket.emit('close');
214
+ return;
215
+ }
220
216
  }
221
217
  if(!data && this.headers['content-length']) {
222
218
  this._res.endWithoutBody(this.headers['content-length'].toString());
@@ -365,15 +361,15 @@ module.exports = class Response extends Writable {
365
361
  }
366
362
 
367
363
  // headers
368
- if(!this.get('Content-Type')) {
364
+ if(!this.headers['content-type']) {
369
365
  const m = mime.lookup(fullpath);
370
366
  if(m) this.type(m);
371
367
  }
372
368
  if(options.cacheControl) {
373
- this.set('Cache-Control', `public, max-age=${options.maxAge / 1000}` + (options.immutable ? ', immutable' : ''));
369
+ this.headers['cache-control'] = `public, max-age=${options.maxAge / 1000}` + (options.immutable ? ', immutable' : '');
374
370
  }
375
371
  if(options.lastModified) {
376
- this.set('Last-Modified', stat.mtime.toUTCString());
372
+ this.headers['last-modified'] = stat.mtime.toUTCString();
377
373
  }
378
374
  if(options.headers) {
379
375
  for(const header in options.headers) {
@@ -386,7 +382,7 @@ module.exports = class Response extends Writable {
386
382
 
387
383
  // etag
388
384
  if(options.etag && !this.headers['etag'] && etagFn) {
389
- this.set('etag', etagFn(stat));
385
+ this.headers['etag'] = etagFn(stat);
390
386
  }
391
387
  if(!options.etag) {
392
388
  this.req.noEtag = true;
@@ -401,7 +397,7 @@ module.exports = class Response extends Writable {
401
397
  // range requests
402
398
  let offset = 0, len = stat.size, ranged = false;
403
399
  if(options.acceptRanges) {
404
- this.set('accept-ranges', 'bytes');
400
+ this.headers['accept-ranges'] = 'bytes';
405
401
  if(this.req.headers.range) {
406
402
  let ranges = this.req.range(stat.size, {
407
403
  combine: true
@@ -414,12 +410,12 @@ module.exports = class Response extends Writable {
414
410
 
415
411
  if(ranges === -1) {
416
412
  this.status(416);
417
- this.set('Content-Range', `bytes */${stat.size}`);
413
+ this.headers['content-range'] = `bytes */${stat.size}`;
418
414
  return done(new Error('Range Not Satisfiable'));
419
415
  }
420
416
  if(ranges !== -2 && ranges.length === 1) {
421
417
  this.status(206);
422
- this.set('Content-Range', `bytes ${ranges[0].start}-${ranges[0].end}/${stat.size}`);
418
+ this.headers['content-range'] = `bytes ${ranges[0].start}-${ranges[0].end}/${stat.size}`;
423
419
  offset = ranges[0].start;
424
420
  len = ranges[0].end - ranges[0].start + 1;
425
421
  ranged = true;
@@ -505,7 +501,7 @@ module.exports = class Response extends Writable {
505
501
  if(field === 'set-cookie' && Array.isArray(value)) {
506
502
  value = value.join('; ');
507
503
  } else if(field === 'content-type') {
508
- if(value.startsWith('text/') || value === 'application/json' || value === 'application/javascript') {
504
+ if(!value.includes('charset=') && (value.startsWith('text/') || value === 'application/json' || value === 'application/javascript')) {
509
505
  value += '; charset=utf-8';
510
506
  }
511
507
  }
@@ -593,7 +589,7 @@ module.exports = class Response extends Writable {
593
589
  return this.cookie(name, '', opts);
594
590
  }
595
591
  attachment(filename) {
596
- this.set('Content-Disposition', `attachment; filename="${filename}"`);
592
+ this.headers['Content-Disposition'] = `attachment; filename="${filename}"`;
597
593
  this.type(filename.split('.').pop());
598
594
  return this;
599
595
  }
@@ -615,8 +611,8 @@ module.exports = class Response extends Writable {
615
611
  return this;
616
612
  }
617
613
  json(body) {
618
- if(!this.get('Content-Type')) {
619
- this.set('Content-Type', 'application/json; charset=utf-8');
614
+ if(!this.headers['content-type']) {
615
+ this.headers['content-type'] = 'application/json; charset=utf-8';
620
616
  }
621
617
  const escape = this.app.get('json escape');
622
618
  const replacer = this.app.get('json replacer');
@@ -627,9 +623,9 @@ module.exports = class Response extends Writable {
627
623
  let callback = this.req.query[this.app.get('jsonp callback name')];
628
624
  let body = stringify(object, this.app.get('json replacer'), this.app.get('json spaces'), this.app.get('json escape'));
629
625
 
630
- if(!this.get('Content-Type')) {
631
- this.set('Content-Type', 'application/javascript; charset=utf-8');
632
- this.set('X-Content-Type-Options', 'nosniff');
626
+ if(!this.headers['content-type']) {
627
+ this.headers['content-type'] = 'application/javascript; charset=utf-8';
628
+ this.headers['X-Content-Type-Options'] = 'nosniff';
633
629
  }
634
630
 
635
631
  if(Array.isArray(callback)) {
@@ -637,8 +633,6 @@ module.exports = class Response extends Writable {
637
633
  }
638
634
 
639
635
  if(typeof callback === 'string' && callback.length !== 0) {
640
- this.set('Content-Type', 'application/javascript; charset=utf-8');
641
- this.set('X-Content-Type-Options', 'nosniff');
642
636
  callback = callback.replace(/[^\[\]\w$.]/g, '');
643
637
 
644
638
  if(body === undefined) {
@@ -664,7 +658,7 @@ module.exports = class Response extends Writable {
664
658
  if(!path) path = this.req.get('Referer');
665
659
  if(!path) path = '/';
666
660
  }
667
- return this.set('Location', encodeUrl(path));
661
+ return this.headers['location'] = encodeUrl(path);
668
662
  }
669
663
  redirect(status, url) {
670
664
  if(typeof status !== 'number' && !url) {
@@ -673,7 +667,7 @@ module.exports = class Response extends Writable {
673
667
  }
674
668
  this.location(url);
675
669
  this.status(status);
676
- this.set('Content-Type', 'text/plain; charset=utf-8');
670
+ this.headers['content-type'] = 'text/plain; charset=utf-8';
677
671
  return this.send(`${statuses.message[status] ?? status}. Redirecting to ${url}`);
678
672
  }
679
673
 
@@ -684,7 +678,7 @@ module.exports = class Response extends Writable {
684
678
  if(ct.startsWith('text/') || ct === 'application/json' || ct === 'application/javascript') {
685
679
  ct += '; charset=UTF-8';
686
680
  }
687
- return this.set('Content-Type', ct);
681
+ return this.set('content-type', ct);
688
682
  }
689
683
  contentType(type) {
690
684
  return this.type(type);