Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: connect promisify customization, other improvements #15207

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2454,6 +2455,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 (options == null || typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
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();
}));
}));
}));