| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1231,6 +1231,13 @@ deprecated: v13.0.0 | |||
| 1231 | 1231 | ||
| 1232 | 1232 | See [`response.socket`][]. | |
| 1233 | 1233 | ||
| 1234 | + ### response.cork() | ||
| 1235 | + <!-- YAML | ||
| 1236 | + added: REPLACEME | ||
| 1237 | + --> | ||
| 1238 | + | ||
| 1239 | + See [`writable.cork()`][]. | ||
| 1240 | + | ||
| 1234 | 1241 | ### response.end(\[data\[, encoding\]\]\[, callback\]) | |
| 1235 | 1242 | <!-- YAML | |
| 1236 | 1243 | added: v0.1.90 | |
@@ -1516,6 +1523,13 @@ response.statusMessage = 'Not found'; | |||
| 1516 | 1523 | After response header was sent to the client, this property indicates the | |
| 1517 | 1524 | status message which was sent out. | |
| 1518 | 1525 | ||
| 1526 | + ### response.uncork() | ||
| 1527 | + <!-- YAML | ||
| 1528 | + added: REPLACEME | ||
| 1529 | + --> | ||
| 1530 | + | ||
| 1531 | + See [`writable.uncork()`][]. | ||
| 1532 | + | ||
| 1519 | 1533 | ### response.writableEnded | |
| 1520 | 1534 | <!-- YAML | |
| 1521 | 1535 | added: v12.9.0 | |
@@ -2358,3 +2372,5 @@ not abort the request or do anything besides add a `'timeout'` event. | |||
| 2358 | 2372 | [`socket.unref()`]: net.html#net_socket_unref | |
| 2359 | 2373 | [`url.parse()`]: url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost | |
| 2360 | 2374 | [`HPE_HEADER_OVERFLOW`]: errors.html#errors_hpe_header_overflow | |
| 2375 | + [`writable.cork()`]: stream.html#stream_writable_cork | ||
| 2376 | + [`writable.uncork()`]: stream.html#stream_writable_uncork | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,6 +56,8 @@ const { validateString } = require('internal/validators'); | |||
| 56 | 56 | const HIGH_WATER_MARK = getDefaultHighWaterMark(); | |
| 57 | 57 | const { CRLF, debug } = common; | |
| 58 | 58 | ||
| 59 | + const kCorked = Symbol('corked'); | ||
| 60 | + | ||
| 59 | 61 | const RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i; | |
| 60 | 62 | const RE_TE_CHUNKED = common.chunkExpression; | |
| 61 | 63 | ||
@@ -99,6 +101,7 @@ function OutgoingMessage() { | |||
| 99 | 101 | ||
| 100 | 102 | this.finished = false; | |
| 101 | 103 | this._headerSent = false; | |
| 104 | + this[kCorked] = 0; | ||
| 102 | 105 | ||
| 103 | 106 | this.socket = null; | |
| 104 | 107 | this._header = null; | |
@@ -137,6 +140,13 @@ Object.defineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', { | |||
| 137 | 140 | } | |
| 138 | 141 | }); | |
| 139 | 142 | ||
| 143 | + Object.defineProperty(OutgoingMessage.prototype, 'writableCorked', { | ||
| 144 | + get() { | ||
| 145 | + const corked = this.socket ? this.socket.writableCorked : 0; | ||
| 146 | + return corked + this[kCorked]; | ||
| 147 | + } | ||
| 148 | + }); | ||
| 149 | + | ||
| 140 | 150 | Object.defineProperty(OutgoingMessage.prototype, '_headers', { | |
| 141 | 151 | get: internalUtil.deprecate(function() { | |
| 142 | 152 | return this.getHeaders(); | |
@@ -213,6 +223,21 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() { | |||
| 213 | 223 | return headers; | |
| 214 | 224 | }; | |
| 215 | 225 | ||
| 226 | + OutgoingMessage.prototype.cork = function() { | ||
| 227 | + if (this.socket) { | ||
| 228 | + this.socket.cork(); | ||
| 229 | + } else { | ||
| 230 | + this[kCorked]++; | ||
| 231 | + } | ||
| 232 | + }; | ||
| 233 | + | ||
| 234 | + OutgoingMessage.prototype.uncork = function() { | ||
| 235 | + if (this.socket) { | ||
| 236 | + this.socket.uncork(); | ||
| 237 | + } else if (this[kCorked]) { | ||
| 238 | + this[kCorked]--; | ||
| 239 | + } | ||
| 240 | + }; | ||
| 216 | 241 | ||
| 217 | 242 | OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) { | |
| 218 | 243 | ||
@@ -710,7 +735,10 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { | |||
| 710 | 735 | return this; | |
| 711 | 736 | } | |
| 712 | 737 | ||
| 713 | - var uncork; | ||
| 738 | + if (this.socket) { | ||
| 739 | + this.socket.cork(); | ||
| 740 | + } | ||
| 741 | + | ||
| 714 | 742 | if (chunk) { | |
| 715 | 743 | if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) { | |
| 716 | 744 | throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk); | |
@@ -721,10 +749,6 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { | |||
| 721 | 749 | else | |
| 722 | 750 | this._contentLength = chunk.length; | |
| 723 | 751 | } | |
| 724 | - if (this.socket) { | ||
| 725 | - this.socket.cork(); | ||
| 726 | - uncork = true; | ||
| 727 | - } | ||
| 728 | 752 | write_(this, chunk, encoding, null, true); | |
| 729 | 753 | } else if (!this._header) { | |
| 730 | 754 | this._contentLength = 0; | |
@@ -743,8 +767,12 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { | |||
| 743 | 767 | this._send('', 'latin1', finish); | |
| 744 | 768 | } | |
| 745 | 769 | ||
| 746 | - if (uncork) | ||
| 770 | + if (this.socket) { | ||
| 771 | + // Fully uncork connection on end(). | ||
| 772 | + this.socket._writableState.corked = 1; | ||
| 747 | 773 | this.socket.uncork(); | |
| 774 | + } | ||
| 775 | + this[kCorked] = 0; | ||
| 748 | 776 | ||
| 749 | 777 | this.finished = true; | |
| 750 | 778 | ||
@@ -805,6 +833,11 @@ OutgoingMessage.prototype._flush = function _flush() { | |||
| 805 | 833 | }; | |
| 806 | 834 | ||
| 807 | 835 | OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) { | |
| 836 | + while (this[kCorked]) { | ||
| 837 | + this[kCorked]--; | ||
| 838 | + socket.cork(); | ||
| 839 | + } | ||
| 840 | + | ||
| 808 | 841 | const outputLength = this.outputData.length; | |
| 809 | 842 | if (outputLength <= 0) | |
| 810 | 843 | return undefined; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -503,6 +503,10 @@ class Http2ServerResponse extends Stream { | |||
| 503 | 503 | return this[kState].statusCode; | |
| 504 | 504 | } | |
| 505 | 505 | ||
| 506 | + get writableCorked() { | ||
| 507 | + return this[kStream].writableCorked; | ||
| 508 | + } | ||
| 509 | + | ||
| 506 | 510 | set statusCode(code) { | |
| 507 | 511 | code |= 0; | |
| 508 | 512 | if (code >= 100 && code < 200) | |
@@ -627,6 +631,14 @@ class Http2ServerResponse extends Stream { | |||
| 627 | 631 | return this; | |
| 628 | 632 | } | |
| 629 | 633 | ||
| 634 | + cork() { | ||
| 635 | + this[kStream].cork(); | ||
| 636 | + } | ||
| 637 | + | ||
| 638 | + uncork() { | ||
| 639 | + this[kStream].uncork(); | ||
| 640 | + } | ||
| 641 | + | ||
| 630 | 642 | write(chunk, encoding, cb) { | |
| 631 | 643 | if (typeof encoding === 'function') { | |
| 632 | 644 | cb = encoding; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const http = require('http'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + | ||
| 6 | + const server = http.createServer((req, res) => { | ||
| 7 | + let corked = false; | ||
| 8 | + const originalWrite = res.socket.write; | ||
| 9 | + res.socket.write = common.mustCall((...args) => { | ||
| 10 | + assert.strictEqual(corked, false); | ||
| 11 | + return originalWrite.call(res.socket, ...args); | ||
| 12 | + }, 5); | ||
| 13 | + corked = true; | ||
| 14 | + res.cork(); | ||
| 15 | + assert.strictEqual(res.writableCorked, res.socket.writableCorked); | ||
| 16 | + res.cork(); | ||
| 17 | + assert.strictEqual(res.writableCorked, res.socket.writableCorked); | ||
| 18 | + res.writeHead(200, { 'a-header': 'a-header-value' }); | ||
| 19 | + res.uncork(); | ||
| 20 | + assert.strictEqual(res.writableCorked, res.socket.writableCorked); | ||
| 21 | + corked = false; | ||
| 22 | + res.end('asd'); | ||
| 23 | + assert.strictEqual(res.writableCorked, res.socket.writableCorked); | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + server.listen(0, () => { | ||
| 27 | + http.get({ port: server.address().port }, (res) => { | ||
| 28 | + res.on('data', common.mustCall()); | ||
| 29 | + res.on('end', common.mustCall(() => { | ||
| 30 | + server.close(); | ||
| 31 | + })); | ||
| 32 | + }); | ||
| 33 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments