Skip to content

Commit

Permalink
Assume 443 on empty port. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Sep 28, 2023
1 parent eb36f79 commit 5caa365
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export function connect(
options?: SocketOptions,
): Socket {
if (typeof address === 'string') {
const url = new URL(address);
const url = new URL("https://" + address);
// eslint-disable-next-line no-param-reassign -- there is no harm reassigning to this param
address = {
hostname: url.hostname,
port: parseInt(url.port),
port: parseInt(url.port == "" ? "443" : url.port),
};
}
return new Socket(address, options);
Expand Down
16 changes: 14 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void tap.test(

const address = await listenAndGetSocketAddress(server);

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

const writer = socket.writable.getWriter();
await t.resolves(writer.write(message));
Expand All @@ -89,6 +89,18 @@ void tap.test(
},
);

void tap.test(
'connect on port 443 works',
async (t) => {
t.plan(2);

const socket = connect(`google.com:443`);

await t.resolves(socket.close());
await t.resolves(socket.closed);
},
);

for (const data of [
new Uint8Array([0, 1, 2]),
new Uint16Array([0, 1, 2]),
Expand Down Expand Up @@ -119,7 +131,7 @@ for (const data of [

const address = await listenAndGetSocketAddress(server);

const socket = connect(`tcp://localhost:${address.port}`);
const socket = connect(`localhost:${address.port}`);
const { reader, writer } = getReaderWriterFromSocket(socket);

await t.resolves(writer.write(message));
Expand Down

0 comments on commit 5caa365

Please sign in to comment.