@@ -33,7 +33,17 @@ const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
33
33
const { outHeadersKey } = require ( 'internal/http' ) ;
34
34
const { async_id_symbol } = require ( 'internal/async_hooks' ) . symbols ;
35
35
const { nextTick } = require ( 'internal/process/next_tick' ) ;
36
- const errors = require ( 'internal/errors' ) ;
36
+ const {
37
+ ERR_HTTP_HEADERS_SENT ,
38
+ ERR_HTTP_INVALID_HEADER_VALUE ,
39
+ ERR_HTTP_TRAILER_INVALID ,
40
+ ERR_INVALID_HTTP_TOKEN ,
41
+ ERR_INVALID_ARG_TYPE ,
42
+ ERR_INVALID_CHAR ,
43
+ ERR_METHOD_NOT_IMPLEMENTED ,
44
+ ERR_STREAM_CANNOT_PIPE ,
45
+ ERR_STREAM_WRITE_AFTER_END
46
+ } = require ( 'internal/errors' ) . codes ;
37
47
38
48
const { CRLF , debug } = common ;
39
49
const { utcDate } = internalHttp ;
@@ -165,7 +175,7 @@ Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
165
175
166
176
OutgoingMessage . prototype . _renderHeaders = function _renderHeaders ( ) {
167
177
if ( this . _header ) {
168
- throw new errors . Error ( ' ERR_HTTP_HEADERS_SENT' , 'render' ) ;
178
+ throw new ERR_HTTP_HEADERS_SENT ( 'render' ) ;
169
179
}
170
180
171
181
var headersMap = this [ outHeadersKey ] ;
@@ -424,7 +434,7 @@ function _storeHeader(firstLine, headers) {
424
434
// header fields, regardless of the header fields present in the
425
435
// message, and thus cannot contain a message body or 'trailers'.
426
436
if ( this . chunkedEncoding !== true && state . trailer ) {
427
- throw new errors . Error ( ' ERR_HTTP_TRAILER_INVALID' ) ;
437
+ throw new ERR_HTTP_TRAILER_INVALID ( ) ;
428
438
}
429
439
430
440
this . _header = state . header + CRLF ;
@@ -488,12 +498,12 @@ function matchHeader(self, state, field, value) {
488
498
function validateHeader ( name , value ) {
489
499
let err ;
490
500
if ( typeof name !== 'string' || ! name || ! checkIsHttpToken ( name ) ) {
491
- err = new errors . TypeError ( ' ERR_INVALID_HTTP_TOKEN' , 'Header name' , name ) ;
501
+ err = new ERR_INVALID_HTTP_TOKEN ( 'Header name' , name ) ;
492
502
} else if ( value === undefined ) {
493
- err = new errors . TypeError ( ' ERR_HTTP_INVALID_HEADER_VALUE' , value , name ) ;
503
+ err = new ERR_HTTP_INVALID_HEADER_VALUE ( value , name ) ;
494
504
} else if ( checkInvalidHeaderChar ( value ) ) {
495
505
debug ( 'Header "%s" contains invalid characters' , name ) ;
496
- err = new errors . TypeError ( ' ERR_INVALID_CHAR' , 'header content' , name ) ;
506
+ err = new ERR_INVALID_CHAR ( 'header content' , name ) ;
497
507
}
498
508
if ( err !== undefined ) {
499
509
Error . captureStackTrace ( err , validateHeader ) ;
@@ -503,7 +513,7 @@ function validateHeader(name, value) {
503
513
504
514
OutgoingMessage . prototype . setHeader = function setHeader ( name , value ) {
505
515
if ( this . _header ) {
506
- throw new errors . Error ( ' ERR_HTTP_HEADERS_SENT' , 'set' ) ;
516
+ throw new ERR_HTTP_HEADERS_SENT ( 'set' ) ;
507
517
}
508
518
validateHeader ( name , value ) ;
509
519
@@ -529,7 +539,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
529
539
530
540
OutgoingMessage . prototype . getHeader = function getHeader ( name ) {
531
541
if ( typeof name !== 'string' ) {
532
- throw new errors . TypeError ( ' ERR_INVALID_ARG_TYPE' , 'name' , 'string' ) ;
542
+ throw new ERR_INVALID_ARG_TYPE ( 'name' , 'string' ) ;
533
543
}
534
544
535
545
if ( ! this [ outHeadersKey ] ) return ;
@@ -565,7 +575,7 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
565
575
566
576
OutgoingMessage . prototype . hasHeader = function hasHeader ( name ) {
567
577
if ( typeof name !== 'string' ) {
568
- throw new errors . TypeError ( ' ERR_INVALID_ARG_TYPE' , 'name' , 'string' ) ;
578
+ throw new ERR_INVALID_ARG_TYPE ( 'name' , 'string' ) ;
569
579
}
570
580
571
581
return ! ! ( this [ outHeadersKey ] && this [ outHeadersKey ] [ name . toLowerCase ( ) ] ) ;
@@ -574,11 +584,11 @@ OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
574
584
575
585
OutgoingMessage . prototype . removeHeader = function removeHeader ( name ) {
576
586
if ( typeof name !== 'string' ) {
577
- throw new errors . TypeError ( ' ERR_INVALID_ARG_TYPE' , 'name' , 'string' ) ;
587
+ throw new ERR_INVALID_ARG_TYPE ( 'name' , 'string' ) ;
578
588
}
579
589
580
590
if ( this . _header ) {
581
- throw new errors . Error ( ' ERR_HTTP_HEADERS_SENT' , 'remove' ) ;
591
+ throw new ERR_HTTP_HEADERS_SENT ( 'remove' ) ;
582
592
}
583
593
584
594
var key = name . toLowerCase ( ) ;
@@ -605,7 +615,7 @@ OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
605
615
606
616
607
617
OutgoingMessage . prototype . _implicitHeader = function _implicitHeader ( ) {
608
- throw new errors . Error ( ' ERR_METHOD_NOT_IMPLEMENTED' , '_implicitHeader()' ) ;
618
+ throw new ERR_METHOD_NOT_IMPLEMENTED ( '_implicitHeader()' ) ;
609
619
} ;
610
620
611
621
Object . defineProperty ( OutgoingMessage . prototype , 'headersSent' , {
@@ -622,7 +632,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
622
632
623
633
function write_ ( msg , chunk , encoding , callback , fromEnd ) {
624
634
if ( msg . finished ) {
625
- const err = new errors . Error ( ' ERR_STREAM_WRITE_AFTER_END' ) ;
635
+ const err = new ERR_STREAM_WRITE_AFTER_END ( ) ;
626
636
nextTick ( msg . socket && msg . socket [ async_id_symbol ] ,
627
637
writeAfterEndNT . bind ( msg ) ,
628
638
err ,
@@ -642,8 +652,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
642
652
}
643
653
644
654
if ( ! fromEnd && typeof chunk !== 'string' && ! ( chunk instanceof Buffer ) ) {
645
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'first argument' ,
646
- [ 'string' , 'Buffer' ] ) ;
655
+ throw new ERR_INVALID_ARG_TYPE ( 'first argument' , [ 'string' , 'Buffer' ] ) ;
647
656
}
648
657
649
658
@@ -711,12 +720,11 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
711
720
value = headers [ key ] ;
712
721
}
713
722
if ( typeof field !== 'string' || ! field || ! checkIsHttpToken ( field ) ) {
714
- throw new errors . TypeError ( 'ERR_INVALID_HTTP_TOKEN' , 'Trailer name' ,
715
- field ) ;
723
+ throw new ERR_INVALID_HTTP_TOKEN ( 'Trailer name' , field ) ;
716
724
}
717
725
if ( checkInvalidHeaderChar ( value ) ) {
718
726
debug ( 'Trailer "%s" contains invalid characters' , field ) ;
719
- throw new errors . TypeError ( ' ERR_INVALID_CHAR' , 'trailer content' , field ) ;
727
+ throw new ERR_INVALID_CHAR ( 'trailer content' , field ) ;
720
728
}
721
729
this . _trailer += field + ': ' + escapeHeaderValue ( value ) + CRLF ;
722
730
}
@@ -742,8 +750,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
742
750
var uncork ;
743
751
if ( chunk ) {
744
752
if ( typeof chunk !== 'string' && ! ( chunk instanceof Buffer ) ) {
745
- throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'first argument' ,
746
- [ 'string' , 'Buffer' ] ) ;
753
+ throw new ERR_INVALID_ARG_TYPE ( 'first argument' , [ 'string' , 'Buffer' ] ) ;
747
754
}
748
755
if ( ! this . _header ) {
749
756
if ( typeof chunk === 'string' )
@@ -874,7 +881,7 @@ OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
874
881
875
882
OutgoingMessage . prototype . pipe = function pipe ( ) {
876
883
// OutgoingMessage should be write-only. Piping from it is disabled.
877
- this . emit ( 'error' , new errors . Error ( ' ERR_STREAM_CANNOT_PIPE' ) ) ;
884
+ this . emit ( 'error' , new ERR_STREAM_CANNOT_PIPE ( ) ) ;
878
885
} ;
879
886
880
887
module . exports = {
0 commit comments