Skip to content

Commit

Permalink
http2: custom promisify for http2.connect
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Sep 13, 2017
1 parent d2b2a24 commit ea2ab2d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 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 @@ -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
4 changes: 2 additions & 2 deletions src/node_http2_core-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ inline int Nghttp2Session::OnHeaderCallback(nghttp2_session* session,
// 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
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 ea2ab2d

Please sign in to comment.