Skip to content

Commit a6879bf

Browse files
committed
http2: custom promisify for http2.connect
... and some other cleanups PR-URL: #15207 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 727d7b5 commit a6879bf

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

lib/internal/http2/core.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ const { onServerStream,
2020
Http2ServerResponse,
2121
} = require('internal/http2/compat');
2222
const { utcDate } = require('internal/http');
23+
const { promisify } = require('internal/util');
2324
const { _connectionListener: httpConnectionListener } = require('http');
24-
const { isUint8Array } = process.binding('util');
25+
const { isUint8Array, createPromise, promiseResolve } = process.binding('util');
2526
const debug = util.debuglog('http2');
2627

2728

@@ -2479,6 +2480,17 @@ function connect(authority, options, listener) {
24792480
return session;
24802481
}
24812482

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+
24822494
function createSecureServer(options, handler) {
24832495
if (typeof options === 'function') {
24842496
handler = options;

src/node_http2_core-inl.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ inline int Nghttp2Session::OnBeginHeadersCallback(nghttp2_session* session,
5252
const nghttp2_frame* frame,
5353
void* user_data) {
5454
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.
5557
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;
5860
DEBUG_HTTP2("Nghttp2Session %s: beginning headers for stream %d\n",
5961
handle->TypeName(handle->type()), id);
6062

6163
Nghttp2Stream* stream = handle->FindStream(id);
6264
if (stream == nullptr) {
63-
Nghttp2Stream::Init(id, handle, frame->headers.cat);
65+
Nghttp2Stream::Init(id, handle, frame->headers.cat);
6466
} else {
65-
stream->StartHeaders(frame->headers.cat);
67+
stream->StartHeaders(frame->headers.cat);
6668
}
6769
return 0;
6870
}
@@ -77,9 +79,11 @@ inline int Nghttp2Session::OnHeaderCallback(nghttp2_session* session,
7779
uint8_t flags,
7880
void* user_data) {
7981
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.
8084
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;
8387
Nghttp2Stream* stream = handle->FindStream(id);
8488
nghttp2_header_list* header = header_free_list.pop();
8589
header->name = name;

src/node_http2_core.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ class Nghttp2Session {
141141
inline ssize_t Write(const uv_buf_t* bufs, unsigned int nbufs);
142142

143143
// Returns the nghttp2 library session
144-
inline nghttp2_session* session() { return session_; }
144+
inline nghttp2_session* session() const { return session_; }
145145

146-
nghttp2_session_type type() {
146+
nghttp2_session_type type() const {
147147
return session_type_;
148148
}
149149

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}));

0 commit comments

Comments
 (0)