MCPcopy Index your code
hub / github.com/nodejs/node / writeHead

Method writeHead

lib/internal/http2/compat.js:713–779  ·  view source on GitHub ↗
(statusCode, statusMessage, headers)

Source from the content-addressed store, hash-verified

711 }
712
713 writeHead(statusCode, statusMessage, headers) {
714 const state = this[kState];
715
716 if (state.closed || this.stream.destroyed || this.stream.closed)
717 return this;
718 if (this[kStream].headersSent)
719 throw new ERR_HTTP2_HEADERS_SENT();
720
721 if (typeof statusMessage === 'string')
722 statusMessageWarn();
723
724 if (headers === undefined && typeof statusMessage === 'object')
725 headers = statusMessage;
726
727 let i;
728 if (ArrayIsArray(headers)) {
729 if (this[kHeaders]) {
730 // Headers in obj should override previous headers but still
731 // allow explicit duplicates. To do so, we first remove any
732 // existing conflicts, then use appendHeader. This is the
733 // slow path, which only applies when you use setHeader and
734 // then pass headers in writeHead too.
735
736 // We need to handle both the tuple and flat array formats, just
737 // like the logic further below.
738 if (headers.length && ArrayIsArray(headers[0])) {
739 for (let n = 0; n < headers.length; n += 1) {
740 const key = headers[n + 0][0];
741 this.removeHeader(key);
742 }
743 } else {
744 for (let n = 0; n < headers.length; n += 2) {
745 const key = headers[n + 0];
746 this.removeHeader(key);
747 }
748 }
749 }
750
751 // Append all the headers provided in the array:
752 if (headers.length && ArrayIsArray(headers[0])) {
753 for (i = 0; i < headers.length; i++) {
754 const header = headers[i];
755 this[kAppendHeader](header[0], header[1]);
756 }
757 } else {
758 if (headers.length % 2 !== 0) {
759 throw new ERR_INVALID_ARG_VALUE('headers', headers);
760 }
761
762 for (i = 0; i < headers.length; i += 2) {
763 this[kAppendHeader](headers[i], headers[i + 1]);
764 }
765 }
766 } else if (typeof headers === 'object') {
767 const keys = ObjectKeys(headers);
768 let key = '';
769 for (i = 0; i < keys.length; i++) {
770 key = keys[i];

Callers 15

flushHeadersMethod · 0.95
writeMethod · 0.95
endMethod · 0.95
_http_server.jsFile · 0.80
parserOnIncomingFunction · 0.80
mainFunction · 0.80
test-keep-alive.jsFile · 0.80

Calls 2

removeHeaderMethod · 0.95
statusMessageWarnFunction · 0.85

Tested by 4

testFunction · 0.64
testFunction · 0.64
testFunction · 0.64
testFunction · 0.64