GeezerVison is showing github.com — http2: custom promisify for http2.connect · nodejs/node@a6879bf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/core.js +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,8 +20,9 @@ const { onServerStream, | 20 | 20 | Http2ServerResponse, | 21 | 21 | } = require('internal/http2/compat'); | 22 | 22 | const { utcDate } = require('internal/http'); | | 23 | +const { promisify } = require('internal/util'); | 23 | 24 | const { _connectionListener: httpConnectionListener } = require('http'); | 24 | | -const { isUint8Array } = process.binding('util'); | | 25 | +const { isUint8Array, createPromise, promiseResolve } = process.binding('util'); | 25 | 26 | const debug = util.debuglog('http2'); | 26 | 27 |
| 27 | 28 |
| @@ -2479,6 +2480,17 @@ function connect(authority, options, listener) { | 2479 | 2480 | return session; | 2480 | 2481 | } | 2481 | 2482 |
| | 2483 | +// Support util.promisify | | 2484 | +Object.defineProperty(connect, promisify.custom, { | | 2485 | + value: (authority, options) => { | | 2486 | + const promise = createPromise(); | | 2487 | + const server = connect(authority, | | 2488 | + options, | | 2489 | + () => promiseResolve(promise, server)); | | 2490 | + return promise; | | 2491 | + } | | 2492 | +}); | | 2493 | + | 2482 | 2494 | function createSecureServer(options, handler) { | 2483 | 2495 | if (typeof options === 'function') { | 2484 | 2496 | handler = options; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_http2_core-inl.h +10-6Lines changed: 10 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,17 +52,19 @@ inline int Nghttp2Session::OnBeginHeadersCallback(nghttp2_session* session, | 52 | 52 | const nghttp2_frame* frame, | 53 | 53 | void* user_data) { | 54 | 54 | Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data); | | 55 | + // If this is a push promise frame, we want to grab the handle of | | 56 | + // the promised stream. | 55 | 57 | int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | 56 | | - frame->push_promise.promised_stream_id : | 57 | | - frame->hd.stream_id; | | 58 | + frame->push_promise.promised_stream_id : | | 59 | + frame->hd.stream_id; | 58 | 60 | DEBUG_HTTP2("Nghttp2Session %s: beginning headers for stream %d\n", | 59 | 61 | handle->TypeName(handle->type()), id); | 60 | 62 |
| 61 | 63 | Nghttp2Stream* stream = handle->FindStream(id); | 62 | 64 | if (stream == nullptr) { | 63 | | - Nghttp2Stream::Init(id, handle, frame->headers.cat); | | 65 | + Nghttp2Stream::Init(id, handle, frame->headers.cat); | 64 | 66 | } else { | 65 | | - stream->StartHeaders(frame->headers.cat); | | 67 | + stream->StartHeaders(frame->headers.cat); | 66 | 68 | } | 67 | 69 | return 0; | 68 | 70 | } | @@ -77,9 +79,11 @@ inline int Nghttp2Session::OnHeaderCallback(nghttp2_session* session, | 77 | 79 | uint8_t flags, | 78 | 80 | void* user_data) { | 79 | 81 | Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data); | | 82 | + // If this is a push promise frame, we want to grab the handle of | | 83 | + // the promised stream. | 80 | 84 | int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ? | 81 | | - frame->push_promise.promised_stream_id : | 82 | | - frame->hd.stream_id; | | 85 | + frame->push_promise.promised_stream_id : | | 86 | + frame->hd.stream_id; | 83 | 87 | Nghttp2Stream* stream = handle->FindStream(id); | 84 | 88 | nghttp2_header_list* header = header_free_list.pop(); | 85 | 89 | header->name = name; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_http2_core.h +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -141,9 +141,9 @@ class Nghttp2Session { | 141 | 141 | inline ssize_t Write(const uv_buf_t* bufs, unsigned int nbufs); | 142 | 142 |
| 143 | 143 | // Returns the nghttp2 library session | 144 | | - inline nghttp2_session* session() { return session_; } | | 144 | + inline nghttp2_session* session() const { return session_; } | 145 | 145 |
| 146 | | - nghttp2_session_type type() { | | 146 | + nghttp2_session_type type() const { | 147 | 147 | return session_type_; | 148 | 148 | } | 149 | 149 |
| |
Collapse file Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Flags: --expose-http2 | | 2 | +'use strict'; | | 3 | + | | 4 | +const common = require('../common'); | | 5 | +if (!common.hasCrypto) | | 6 | + common.skip('missing crypto'); | | 7 | +const assert = require('assert'); | | 8 | +const http2 = require('http2'); | | 9 | +const util = require('util'); | | 10 | + | | 11 | +const server = http2.createServer(); | | 12 | +server.on('stream', common.mustCall((stream) => { | | 13 | + stream.respond(); | | 14 | + stream.end('ok'); | | 15 | +})); | | 16 | +server.listen(0, common.mustCall(() => { | | 17 | + | | 18 | + const connect = util.promisify(http2.connect); | | 19 | + | | 20 | + connect(`http://localhost:${server.address().port}`) | | 21 | + .catch(common.mustNotCall()) | | 22 | + .then(common.mustCall((client) => { | | 23 | + assert(client); | | 24 | + const req = client.request(); | | 25 | + let data = ''; | | 26 | + req.setEncoding('utf8'); | | 27 | + req.on('data', (chunk) => data += chunk); | | 28 | + req.on('end', common.mustCall(() => { | | 29 | + assert.strictEqual(data, 'ok'); | | 30 | + client.destroy(); | | 31 | + server.close(); | | 32 | + })); | | 33 | + })); | | 34 | +})); |
|