Skip to content

Commit

Permalink
http2: custom promisify for http2.connect
Browse files Browse the repository at this point in the history
... and some other cleanups

PR-URL: #15207
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
jasnell committed Sep 20, 2017
1 parent 727d7b5 commit a6879bf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
14 changes: 13 additions & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ const { onServerStream,
Http2ServerResponse,
} = require('internal/http2/compat');
const { utcDate } = require('internal/http');
const { promisify } = require('internal/util');
const { _connectionListener: httpConnectionListener } = require('http');
const { isUint8Array } = process.binding('util');
const { isUint8Array, createPromise, promiseResolve } = process.binding('util');
const debug = util.debuglog('http2');


Expand Down Expand Up @@ -2479,6 +2480,17 @@ function connect(authority, options, listener) {
return session;
}

// Support util.promisify
Object.defineProperty(connect, promisify.custom, {
value: (authority, options) => {
const promise = createPromise();
const server = connect(authority,
options,
() => promiseResolve(promise, server));
return promise;
}
});

function createSecureServer(options, handler) {
if (typeof options === 'function') {
handler = options;
Expand Down
16 changes: 10 additions & 6 deletions src/node_http2_core-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ inline int Nghttp2Session::OnBeginHeadersCallback(nghttp2_session* session,
const nghttp2_frame* frame,
void* user_data) {
Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data);
// If this is a push promise frame, we want to grab the handle of
// the promised stream.
int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ?
frame->push_promise.promised_stream_id :
frame->hd.stream_id;
frame->push_promise.promised_stream_id :
frame->hd.stream_id;
DEBUG_HTTP2("Nghttp2Session %s: beginning headers for stream %d\n",
handle->TypeName(handle->type()), id);

Nghttp2Stream* stream = handle->FindStream(id);
if (stream == nullptr) {
Nghttp2Stream::Init(id, handle, frame->headers.cat);
Nghttp2Stream::Init(id, handle, frame->headers.cat);
} else {
stream->StartHeaders(frame->headers.cat);
stream->StartHeaders(frame->headers.cat);
}
return 0;
}
Expand All @@ -77,9 +79,11 @@ inline int Nghttp2Session::OnHeaderCallback(nghttp2_session* session,
uint8_t flags,
void* user_data) {
Nghttp2Session* handle = static_cast<Nghttp2Session*>(user_data);
// If this is a push promise frame, we want to grab the handle of
// the promised stream.
int32_t id = (frame->hd.type == NGHTTP2_PUSH_PROMISE) ?
frame->push_promise.promised_stream_id :
frame->hd.stream_id;
frame->push_promise.promised_stream_id :
frame->hd.stream_id;
Nghttp2Stream* stream = handle->FindStream(id);
nghttp2_header_list* header = header_free_list.pop();
header->name = name;
Expand Down
4 changes: 2 additions & 2 deletions src/node_http2_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ class Nghttp2Session {
inline ssize_t Write(const uv_buf_t* bufs, unsigned int nbufs);

// Returns the nghttp2 library session
inline nghttp2_session* session() { return session_; }
inline nghttp2_session* session() const { return session_; }

nghttp2_session_type type() {
nghttp2_session_type type() const {
return session_type_;
}

Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-http2-client-promisify-connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Flags: --expose-http2
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const util = require('util');

const server = http2.createServer();
server.on('stream', common.mustCall((stream) => {
stream.respond();
stream.end('ok');
}));
server.listen(0, common.mustCall(() => {

const connect = util.promisify(http2.connect);

connect(`http://localhost:${server.address().port}`)
.catch(common.mustNotCall())
.then(common.mustCall((client) => {
assert(client);
const req = client.request();
let data = '';
req.setEncoding('utf8');
req.on('data', (chunk) => data += chunk);
req.on('end', common.mustCall(() => {
assert.strictEqual(data, 'ok');
client.destroy();
server.close();
}));
}));
}));

0 comments on commit a6879bf

Please sign in to comment.