@nxtedition/lib 28.0.18 → 28.0.20

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/http.js +121 -8
  2. package/package.json +2 -2
package/http.js CHANGED
@@ -216,6 +216,10 @@ export async function requestMiddleware(ctx, next) {
216
216
  stats.pending++
217
217
  }
218
218
 
219
+ if (ctx[kPendingIndex] !== -1) {
220
+ throw new Error('context is already pending')
221
+ }
222
+
219
223
  ctx[kPendingIndex] = pending.push(ctx) - 1
220
224
  try {
221
225
  const isHealthcheck = req.url === '/healthcheck' || req.url === '/_up'
@@ -366,11 +370,12 @@ export async function requestMiddleware(ctx, next) {
366
370
 
367
371
  if (ctx[kPendingIndex] !== -1) {
368
372
  const idx = ctx[kPendingIndex]
373
+ ctx[kPendingIndex] = -1
374
+
369
375
  const tmp = pending.pop()
370
376
  if (tmp !== ctx) {
371
377
  pending[idx] = tmp
372
378
  pending[idx][kPendingIndex] = idx
373
- tmp[kPendingIndex] = -1
374
379
  }
375
380
  }
376
381
 
@@ -464,10 +469,12 @@ export class ServerResponse extends http.ServerResponse {
464
469
  return this.#bytesWritten
465
470
  }
466
471
 
472
+ /** @deprecated */
467
473
  setHeaders() {
468
474
  throw new Error('not supported')
469
475
  }
470
476
 
477
+ /** @deprecated */
471
478
  appendHeader() {
472
479
  throw new Error('not supported')
473
480
  }
@@ -530,7 +537,6 @@ export class ServerResponse extends http.ServerResponse {
530
537
  /**
531
538
  * @param {number} statusCode
532
539
  * @param {Record<string, string>} [headers]
533
- * @returns
534
540
  */
535
541
  writeHead(statusCode, headers) {
536
542
  if (this.#headersSent) {
@@ -554,7 +560,6 @@ export class ServerResponse extends http.ServerResponse {
554
560
 
555
561
  /**
556
562
  * @param {net.Socket} socket
557
- * @returns
558
563
  */
559
564
  assignSocket(socket) {
560
565
  if (!this.destroyed) {
@@ -565,6 +570,11 @@ export class ServerResponse extends http.ServerResponse {
565
570
  return super.assignSocket(socket)
566
571
  }
567
572
 
573
+ /**
574
+ * @param {Buffer|string} chunk
575
+ * @param {string} [encoding]
576
+ * @param {(err: Error) => void} [callback]
577
+ */
568
578
  write(chunk, encoding, callback) {
569
579
  if (!this.destroyed) {
570
580
  if (this.#data === -1) {
@@ -587,6 +597,11 @@ export class ServerResponse extends http.ServerResponse {
587
597
  return super.write(chunk, encoding, callback)
588
598
  }
589
599
 
600
+ /**
601
+ * @param {Buffer|string} chunk
602
+ * @param {string} [encoding]
603
+ * @param {(err: Error) => void} [callback]
604
+ */
590
605
  end(chunk, encoding, callback) {
591
606
  if (!this.destroyed) {
592
607
  this.#end = performance.now() - this.#created
@@ -612,9 +627,7 @@ export class ServerResponse extends http.ServerResponse {
612
627
  }
613
628
 
614
629
  /**
615
- *
616
630
  * @param {Error} [err]
617
- * @returns
618
631
  */
619
632
  destroy(err) {
620
633
  if (!this.destroyed) {
@@ -672,11 +685,14 @@ export class Http2ServerRequest extends http2.Http2ServerRequest {
672
685
  export class Http2ServerResponse extends http2.Http2ServerResponse {
673
686
  #created = performance.now()
674
687
  #bytesWritten = 0
675
- #connect = -1
688
+ #connect = 0
676
689
  #headers = -1
677
690
  #data = -1
678
691
  #end = -1
679
692
 
693
+ #headersObj = kEmptyObj
694
+ #headersSent = false
695
+
680
696
  /**
681
697
  * @returns {{
682
698
  * created: number,
@@ -696,19 +712,108 @@ export class Http2ServerResponse extends http2.Http2ServerResponse {
696
712
  }
697
713
  }
698
714
 
715
+ /**
716
+ * @returns {number}
717
+ */
699
718
  get bytesWritten() {
700
719
  return this.#bytesWritten
701
720
  }
702
721
 
703
- flushHeaders() {
722
+ /** @deprecated */
723
+ setHeaders() {
724
+ throw new Error('not supported')
725
+ }
726
+
727
+ /** @deprecated */
728
+ appendHeader() {
729
+ throw new Error('not supported')
730
+ }
731
+
732
+ /**
733
+ * @param {string} key
734
+ * @param {string} val
735
+ */
736
+ setHeader(key, val) {
737
+ if (this.#headersSent) {
738
+ throw new Error('headers already sent')
739
+ }
740
+
741
+ if (this.#headersObj === kEmptyObj) {
742
+ this.#headersObj = {}
743
+ }
744
+
745
+ this.#headersObj[key] = val === undefined ? undefined : `${val}`
746
+ }
747
+
748
+ /**
749
+ * @param {string} key
750
+ * @param {string} val
751
+ */
752
+ removeHeader(key, val) {
753
+ if (this.#headersSent) {
754
+ throw new Error('headers already sent')
755
+ }
756
+
757
+ if (this.#headersObj !== kEmptyObj) {
758
+ delete this.#headersObj[key]
759
+ }
760
+ }
761
+
762
+ /**
763
+ * @returns {string[]}
764
+ */
765
+ getHeaderNames() {
766
+ if (this.#headersSent) {
767
+ throw new Error('headers already sent')
768
+ }
769
+
770
+ return this.#headersObj === kEmptyObj ? kEmptyArr : Object.keys(this.#headersObj)
771
+ }
772
+
773
+ /**
774
+ * @returns {Record<string, string>}
775
+ */
776
+ getHeaders() {
777
+ return this.#headersObj
778
+ }
779
+
780
+ /**
781
+ * @returns {boolean}
782
+ */
783
+ get headersSent() {
784
+ return this.#headersSent
785
+ }
786
+
787
+ /**
788
+ * @param {number} statusCode
789
+ * @param {Record<string, string>} [headers]
790
+ * @returns
791
+ */
792
+ writeHead(statusCode, headers) {
793
+ if (this.#headersSent) {
794
+ throw new Error('headers already sent')
795
+ }
796
+
797
+ if (this.#headersObj !== kEmptyObj) {
798
+ headers = headers ? Object.assign(this.#headersObj, headers) : this.#headersObj
799
+ }
800
+
704
801
  if (!this.destroyed) {
705
802
  if (this.#headers === -1) {
706
803
  this.#headers = performance.now() - this.#created
707
804
  }
708
805
  }
709
- return super.flushHeaders()
806
+
807
+ this.#headersSent = true
808
+
809
+ return super.writeHead(statusCode, headers)
710
810
  }
711
811
 
812
+ /**
813
+ * @param {Buffer|string} chunk
814
+ * @param {string} [encoding]
815
+ * @param {(err: Error) => void} [callback]
816
+ */
712
817
  write(chunk, encoding, callback) {
713
818
  if (!this.destroyed) {
714
819
  if (this.#data === -1) {
@@ -731,6 +836,11 @@ export class Http2ServerResponse extends http2.Http2ServerResponse {
731
836
  return super.write(chunk, encoding, callback)
732
837
  }
733
838
 
839
+ /**
840
+ * @param {Buffer|string} chunk
841
+ * @param {string} [encoding]
842
+ * @param {(err: Error) => void} [callback]
843
+ */
734
844
  end(chunk, encoding, callback) {
735
845
  if (!this.destroyed) {
736
846
  this.#end = performance.now() - this.#created
@@ -755,6 +865,9 @@ export class Http2ServerResponse extends http2.Http2ServerResponse {
755
865
  return super.end(chunk, encoding, callback)
756
866
  }
757
867
 
868
+ /**
869
+ * @param {Error} [err]
870
+ */
758
871
  destroy(err) {
759
872
  if (!this.destroyed) {
760
873
  if (this.#end === -1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "28.0.18",
3
+ "version": "28.0.20",
4
4
  "license": "UNLICENSED",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -92,5 +92,5 @@
92
92
  "pino": ">=7.0.0",
93
93
  "rxjs": "^7.0.0"
94
94
  },
95
- "gitHead": "0e9eba55a86700d53a902b3478366d6cb9791634"
95
+ "gitHead": "b33dfcfc5f68626f846f364805f51a0e6de74cce"
96
96
  }