Skip to content

Commit 99ac502

Browse files
dom96Ethan Arrowood
andauthored
Assume 443 on empty port. Fixes #9 (#11)
* Assume 443 on empty port. Fixes #9 * fix eslint, prettier, and improve test * rely on external 443 port --------- Co-authored-by: Ethan Arrowood <ethan.arrowood@vercel.com>
1 parent abd421e commit 99ac502

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export function connect(
1010
options?: SocketOptions,
1111
): Socket {
1212
if (typeof address === 'string') {
13-
const url = new URL(address);
13+
const url = new URL(`https://${address}`);
1414
// eslint-disable-next-line no-param-reassign -- there is no harm reassigning to this param
1515
address = {
1616
hostname: url.hostname,
17-
port: parseInt(url.port),
17+
port: parseInt(url.port === '' ? '443' : url.port),
1818
};
1919
}
2020
return new Socket(address, options);

test/index.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void tap.test(
7979

8080
const address = await listenAndGetSocketAddress(server);
8181

82-
const socket = connect(`tcp://localhost:${address.port}`);
82+
const socket = connect(`localhost:${address.port}`);
8383

8484
const writer = socket.writable.getWriter();
8585
await t.resolves(writer.write(message));
@@ -89,6 +89,11 @@ void tap.test(
8989
},
9090
);
9191

92+
void tap.test('connect on port 443 works', async (t) => {
93+
const socket = connect(`github.com:443`);
94+
await t.resolves(socket.close());
95+
});
96+
9297
for (const data of [
9398
new Uint8Array([0, 1, 2]),
9499
new Uint16Array([0, 1, 2]),
@@ -117,7 +122,7 @@ for (const data of [
117122

118123
const address = await listenAndGetSocketAddress(server);
119124

120-
const socket = connect(`tcp://localhost:${address.port}`);
125+
const socket = connect(`localhost:${address.port}`);
121126
const { reader, writer } = getReaderWriterFromSocket(socket);
122127

123128
await t.resolves(writer.write(message));

0 commit comments

Comments
 (0)