From 3b08e446eb51bdeee3d0499b9fa82291e7df36ec Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Thu, 21 Sep 2023 20:01:11 +0100 Subject: [PATCH] Assume 443 on empty port. Fixes #9 --- package.json | 3 +++ src/index.ts | 4 ++-- test/index.test.ts | 16 ++++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index fdbad7c..ac40e93 100644 --- a/package.json +++ b/package.json @@ -40,5 +40,8 @@ "packageManager": "pnpm@8.6.12", "engines": { "node": ">=18" + }, + "dependencies": { + "pnpm": "8.8.0" } } diff --git a/src/index.ts b/src/index.ts index 00b3644..d280734 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); diff --git a/test/index.test.ts b/test/index.test.ts index 02c53f1..c5f7697 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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)); @@ -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]), @@ -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));