Skip to content

Commit c272583

Browse files
committed
Support asynchronous connect function
1 parent 43a065f commit c272583

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
## API
77

8-
### resolveALPN(options)
8+
### resolveALPN(options, connect = tls.connect)
99

1010
Returns an object with an `alpnProtocol` property. The `socket` property may be also present.
1111

@@ -48,6 +48,11 @@ console.log(result); // {alpnProtocol: 'h2', socket: tls.TLSSocket}
4848
result.socket.destroy();
4949
```
5050

51+
#### connect
52+
53+
Type: `Function<TLSSocket> | AsyncFunction<TLSSocket>`\
54+
Default: [`tls.connect`](https://nodejs.org/dist/latest-v16.x/docs/api/tls.html#tls_tls_connect_options_callback)
55+
5156
## License
5257

5358
MIT

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22
const tls = require('tls');
33

4-
module.exports = (options = {}) => new Promise((resolve, reject) => {
4+
module.exports = (options = {}, connect = tls.connect) => new Promise((resolve, reject) => {
55
let timeout = false;
66

7+
let socket;
8+
79
const callback = async () => {
810
socket.off('timeout', onTimeout);
911
socket.off('error', reject);
@@ -26,8 +28,14 @@ module.exports = (options = {}) => new Promise((resolve, reject) => {
2628
callback();
2729
};
2830

29-
const socket = tls.connect(options, callback);
31+
(async () => {
32+
try {
33+
socket = await connect(options, callback);
3034

31-
socket.on('error', reject);
32-
socket.once('timeout', onTimeout);
35+
socket.on('error', reject);
36+
socket.once('timeout', onTimeout);
37+
} catch (error) {
38+
reject(error);
39+
}
40+
})();
3341
});

test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,49 @@ test('works with timeout', async t => {
112112

113113
t.true(timeout);
114114
});
115+
116+
test('accept custom createConnection function', async t => {
117+
const custom = Symbol('custom');
118+
119+
const result = await resolveALPN({
120+
...s.options,
121+
resolveSocket: true
122+
}, (options, callback) => {
123+
const socket = tls.connect(options, callback);
124+
socket[custom] = true;
125+
126+
return socket;
127+
});
128+
129+
t.is(result.alpnProtocol, 'h2');
130+
t.true(result.socket instanceof tls.TLSSocket);
131+
t.true(result.socket[custom]);
132+
133+
result.socket.destroy();
134+
});
135+
136+
test('async createConnection function', async t => {
137+
const custom = Symbol('custom');
138+
139+
const result = await resolveALPN({
140+
...s.options,
141+
resolveSocket: true
142+
}, async (options, callback) => {
143+
return new Promise((resolve, reject) => {
144+
const socket = tls.connect(options, callback);
145+
socket[custom] = true;
146+
147+
socket.once('error', reject);
148+
socket.once('connect', () => {
149+
socket.off('error', reject);
150+
resolve(socket);
151+
});
152+
});
153+
});
154+
155+
t.is(result.alpnProtocol, 'h2');
156+
t.true(result.socket instanceof tls.TLSSocket);
157+
t.true(result.socket[custom]);
158+
159+
result.socket.destroy();
160+
});

0 commit comments

Comments
 (0)