| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1223,6 +1223,13 @@ added: v0.3.0 | |||
| 1223 | 1223 | ||
| 1224 | 1224 | See [`response.socket`][]. | |
| 1225 | 1225 | ||
| 1226 | + ### `response.cork()` | ||
| 1227 | + <!-- YAML | ||
| 1228 | + added: REPLACEME | ||
| 1229 | + --> | ||
| 1230 | + | ||
| 1231 | + See [`writable.cork()`][]. | ||
| 1232 | + | ||
| 1226 | 1233 | ### `response.end([data[, encoding]][, callback])` | |
| 1227 | 1234 | <!-- YAML | |
| 1228 | 1235 | added: v0.1.90 | |
@@ -1508,6 +1515,13 @@ response.statusMessage = 'Not found'; | |||
| 1508 | 1515 | After response header was sent to the client, this property indicates the | |
| 1509 | 1516 | status message which was sent out. | |
| 1510 | 1517 | ||
| 1518 | + ### `response.uncork()` | ||
| 1519 | + <!-- YAML | ||
| 1520 | + added: REPLACEME | ||
| 1521 | + --> | ||
| 1522 | + | ||
| 1523 | + See [`writable.uncork()`][]. | ||
| 1524 | + | ||
| 1511 | 1525 | ### `response.writableEnded` | |
| 1512 | 1526 | <!-- YAML | |
| 1513 | 1527 | added: v12.9.0 | |
@@ -2333,3 +2347,5 @@ not abort the request or do anything besides add a `'timeout'` event. | |||
| 2333 | 2347 | [`socket.unref()`]: net.html#net_socket_unref | |
| 2334 | 2348 | [`url.parse()`]: url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost | |
| 2335 | 2349 | [`HPE_HEADER_OVERFLOW`]: errors.html#errors_hpe_header_overflow | |
| 2350 | + [`writable.cork()`]: stream.html#stream_writable_cork | ||
| 2351 | + [`writable.uncork()`]: stream.html#stream_writable_uncork | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,6 +55,8 @@ const { validateString } = require('internal/validators'); | |||
| 55 | 55 | const HIGH_WATER_MARK = getDefaultHighWaterMark(); | |
| 56 | 56 | const { CRLF, debug } = common; | |
| 57 | 57 | ||
| 58 | + const kCorked = Symbol('corked'); | ||
| 59 | + | ||
| 58 | 60 | const RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i; | |
| 59 | 61 | const RE_TE_CHUNKED = common.chunkExpression; | |
| 60 | 62 | ||
@@ -98,6 +100,7 @@ function OutgoingMessage() { | |||
| 98 | 100 | ||
| 99 | 101 | this.finished = false; | |
| 100 | 102 | this._headerSent = false; | |
| 103 | + this[kCorked] = 0; | ||
| 101 | 104 | ||
| 102 | 105 | this.socket = null; | |
| 103 | 106 | this.connection = 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(); | |
@@ -204,6 +214,21 @@ OutgoingMessage.prototype._renderHeaders = function _renderHeaders() { | |||
| 204 | 214 | return headers; | |
| 205 | 215 | }; | |
| 206 | 216 | ||
| 217 | + OutgoingMessage.prototype.cork = function() { | ||
| 218 | + if (this.socket) { | ||
| 219 | + this.socket.cork(); | ||
| 220 | + } else { | ||
| 221 | + this[kCorked]++; | ||
| 222 | + } | ||
| 223 | + }; | ||
| 224 | + | ||
| 225 | + OutgoingMessage.prototype.uncork = function() { | ||
| 226 | + if (this.socket) { | ||
| 227 | + this.socket.uncork(); | ||
| 228 | + } else if (this[kCorked]) { | ||
| 229 | + this[kCorked]--; | ||
| 230 | + } | ||
| 231 | + }; | ||
| 207 | 232 | ||
| 208 | 233 | OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) { | |
| 209 | 234 | ||
@@ -694,7 +719,10 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { | |||
| 694 | 719 | return this; | |
| 695 | 720 | } | |
| 696 | 721 | ||
| 697 | - var uncork; | ||
| 722 | + if (this.socket) { | ||
| 723 | + this.socket.cork(); | ||
| 724 | + } | ||
| 725 | + | ||
| 698 | 726 | if (chunk) { | |
| 699 | 727 | if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) { | |
| 700 | 728 | throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk); | |
@@ -705,10 +733,6 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { | |||
| 705 | 733 | else | |
| 706 | 734 | this._contentLength = chunk.length; | |
| 707 | 735 | } | |
| 708 | - if (this.connection) { | ||
| 709 | - this.connection.cork(); | ||
| 710 | - uncork = true; | ||
| 711 | - } | ||
| 712 | 736 | write_(this, chunk, encoding, null, true); | |
| 713 | 737 | } else if (!this._header) { | |
| 714 | 738 | this._contentLength = 0; | |
@@ -727,8 +751,12 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { | |||
| 727 | 751 | this._send('', 'latin1', finish); | |
| 728 | 752 | } | |
| 729 | 753 | ||
| 730 | - if (uncork) | ||
| 731 | - this.connection.uncork(); | ||
| 754 | + if (this.socket) { | ||
| 755 | + // Fully uncork connection on end(). | ||
| 756 | + this.socket._writableState.corked = 1; | ||
| 757 | + this.socket.uncork(); | ||
| 758 | + } | ||
| 759 | + this[kCorked] = 0; | ||
| 732 | 760 | ||
| 733 | 761 | this.finished = true; | |
| 734 | 762 | ||
@@ -789,6 +817,11 @@ OutgoingMessage.prototype._flush = function _flush() { | |||
| 789 | 817 | }; | |
| 790 | 818 | ||
| 791 | 819 | OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) { | |
| 820 | + while (this[kCorked]) { | ||
| 821 | + this[kCorked]--; | ||
| 822 | + socket.cork(); | ||
| 823 | + } | ||
| 824 | + | ||
| 792 | 825 | const outputLength = this.outputData.length; | |
| 793 | 826 | if (outputLength <= 0) | |
| 794 | 827 | 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