| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1012,6 +1012,15 @@ added: v8.4.0 | |||
| 1012 | 1012 | Set to `true` if the `Http2Stream` instance was aborted abnormally. When set, | |
| 1013 | 1013 | the `'aborted'` event will have been emitted. | |
| 1014 | 1014 | ||
| 1015 | + #### http2stream.bufferSize | ||
| 1016 | + <!-- YAML | ||
| 1017 | + added: REPLACEME | ||
| 1018 | + --> | ||
| 1019 | + * {number} | ||
| 1020 | + | ||
| 1021 | + This property shows the number of characters currently buffered to be written. | ||
| 1022 | + See [`net.Socket.bufferSize`][] for details. | ||
| 1023 | + | ||
| 1015 | 1024 | #### http2stream.close(code[, callback]) | |
| 1016 | 1025 | <!-- YAML | |
| 1017 | 1026 | added: v8.4.0 | |
@@ -3435,6 +3444,7 @@ following additional properties: | |||
| 3435 | 3444 | [`http2session.close()`]: #http2_http2session_close_callback | |
| 3436 | 3445 | [`http2stream.pushStream()`]: #http2_http2stream_pushstream_headers_options_callback | |
| 3437 | 3446 | [`net.Server.close()`]: net.html#net_server_close_callback | |
| 3447 | + [`net.Socket.bufferSize`]: net.html#net_socket_buffersize | ||
| 3438 | 3448 | [`net.Socket.prototype.ref()`]: net.html#net_socket_ref | |
| 3439 | 3449 | [`net.Socket.prototype.unref()`]: net.html#net_socket_unref | |
| 3440 | 3450 | [`net.Socket`]: net.html#net_class_net_socket | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1685,6 +1685,12 @@ class Http2Stream extends Duplex { | |||
| 1685 | 1685 | return `Http2Stream ${util.format(obj)}`; | |
| 1686 | 1686 | } | |
| 1687 | 1687 | ||
| 1688 | + get bufferSize() { | ||
| 1689 | + // `bufferSize` properties of `net.Socket` are `undefined` when | ||
| 1690 | + // their `_handle` are falsy. Here we avoid the behavior. | ||
| 1691 | + return this[kState].writeQueueSize + this.writableLength; | ||
| 1692 | + } | ||
| 1693 | + | ||
| 1688 | 1694 | get endAfterHeaders() { | |
| 1689 | 1695 | return this[kState].endAfterHeaders; | |
| 1690 | 1696 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,51 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const { mustCall, hasCrypto, skip } = require('../common'); | ||
| 4 | + if (!hasCrypto) | ||
| 5 | + skip('missing crypto'); | ||
| 6 | + const assert = require('assert'); | ||
| 7 | + const { createServer, connect } = require('http2'); | ||
| 8 | + const Countdown = require('../common/countdown'); | ||
| 9 | + | ||
| 10 | + // This test ensures that `bufferSize` of Http2Session and Http2Stream work | ||
| 11 | + // as expected. | ||
| 12 | + { | ||
| 13 | + const SOCKETS = 2; | ||
| 14 | + const TIMES = 10; | ||
| 15 | + const BUFFER_SIZE = 30; | ||
| 16 | + const server = createServer(); | ||
| 17 | + | ||
| 18 | + // Other `bufferSize` tests for net module and tls module | ||
| 19 | + // don't assert `bufferSize` of server-side sockets. | ||
| 20 | + server.on('stream', mustCall((stream) => { | ||
| 21 | + stream.on('data', mustCall()); | ||
| 22 | + stream.on('end', mustCall()); | ||
| 23 | + }, SOCKETS)); | ||
| 24 | + | ||
| 25 | + server.listen(0, mustCall(() => { | ||
| 26 | + const authority = `http://localhost:${server.address().port}`; | ||
| 27 | + const client = connect(authority); | ||
| 28 | + const countdown = new Countdown(SOCKETS, () => { | ||
| 29 | + client.close(); | ||
| 30 | + server.close(); | ||
| 31 | + }); | ||
| 32 | + | ||
| 33 | + client.once('connect', mustCall()); | ||
| 34 | + | ||
| 35 | + for (let j = 0; j < SOCKETS; j += 1) { | ||
| 36 | + const stream = client.request({ ':method': 'POST' }); | ||
| 37 | + stream.on('data', () => {}); | ||
| 38 | + stream.on('close', mustCall(() => { | ||
| 39 | + countdown.dec(); | ||
| 40 | + })); | ||
| 41 | + | ||
| 42 | + for (let i = 0; i < TIMES; i += 1) { | ||
| 43 | + stream.write(Buffer.allocUnsafe(BUFFER_SIZE), mustCall()); | ||
| 44 | + const expectedSocketBufferSize = BUFFER_SIZE * (i + 1); | ||
| 45 | + assert.strictEqual(stream.bufferSize, expectedSocketBufferSize); | ||
| 46 | + } | ||
| 47 | + stream.end(); | ||
| 48 | + stream.close(); | ||
| 49 | + } | ||
| 50 | + })); | ||
| 51 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,72 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + if (!common.hasCrypto) | ||
| 4 | + common.skip('missing crypto'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + const fixtures = require('../common/fixtures'); | ||
| 7 | + const makeDuplexPair = require('../common/duplexpair'); | ||
| 8 | + const tls = require('tls'); | ||
| 9 | + const net = require('net'); | ||
| 10 | + | ||
| 11 | + // This test ensures that `bufferSize` also works for those tlsSockets | ||
| 12 | + // created from `socket` of `Duplex`, with which, TLSSocket will wrap | ||
| 13 | + // sockets in `StreamWrap`. | ||
| 14 | + { | ||
| 15 | + const iter = 10; | ||
| 16 | + | ||
| 17 | + function createDuplex(port) { | ||
| 18 | + const { clientSide, serverSide } = makeDuplexPair(); | ||
| 19 | + | ||
| 20 | + return new Promise((resolve, reject) => { | ||
| 21 | + const socket = net.connect({ | ||
| 22 | + port, | ||
| 23 | + }, common.mustCall(() => { | ||
| 24 | + clientSide.pipe(socket); | ||
| 25 | + socket.pipe(clientSide); | ||
| 26 | + clientSide.on('close', common.mustCall(() => socket.destroy())); | ||
| 27 | + socket.on('close', common.mustCall(() => clientSide.destroy())); | ||
| 28 | + | ||
| 29 | + resolve(serverSide); | ||
| 30 | + })); | ||
| 31 | + }); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + const server = tls.createServer({ | ||
| 35 | + key: fixtures.readKey('agent2-key.pem'), | ||
| 36 | + cert: fixtures.readKey('agent2-cert.pem') | ||
| 37 | + }, common.mustCall((socket) => { | ||
| 38 | + let str = ''; | ||
| 39 | + socket.setEncoding('utf-8'); | ||
| 40 | + socket.on('data', (chunk) => { str += chunk; }); | ||
| 41 | + | ||
| 42 | + socket.on('end', common.mustCall(() => { | ||
| 43 | + assert.strictEqual(str, 'a'.repeat(iter - 1)); | ||
| 44 | + server.close(); | ||
| 45 | + })); | ||
| 46 | + })); | ||
| 47 | + | ||
| 48 | + server.listen(0, common.mustCall(() => { | ||
| 49 | + const { port } = server.address(); | ||
| 50 | + createDuplex(port).then((socket) => { | ||
| 51 | + const client = tls.connect({ | ||
| 52 | + socket, | ||
| 53 | + rejectUnauthorized: false, | ||
| 54 | + }, common.mustCall(() => { | ||
| 55 | + assert.strictEqual(client.bufferSize, 0); | ||
| 56 | + | ||
| 57 | + for (let i = 1; i < iter; i++) { | ||
| 58 | + client.write('a'); | ||
| 59 | + assert.strictEqual(client.bufferSize, i + 1); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + // It seems that tlsSockets created from sockets of `Duplex` emit no | ||
| 63 | + // "finish" events. We use "end" event instead. | ||
| 64 | + client.on('end', common.mustCall(() => { | ||
| 65 | + assert.strictEqual(client.bufferSize, undefined); | ||
| 66 | + })); | ||
| 67 | + | ||
| 68 | + client.end(); | ||
| 69 | + })); | ||
| 70 | + }); | ||
| 71 | + })); | ||
| 72 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments