From dd729a26ea373fa554131cf01a4fd7726dc4c324 Mon Sep 17 00:00:00 2001 From: Harshal Mittal Date: Sat, 3 Feb 2024 12:49:47 +0530 Subject: [PATCH] refactor: parse default http ports in url, fixes #59 This change correctly parses default http ports in url for rest and grpc packages. --- packages/js-client-grpc/src/qdrant-client.ts | 4 +++- .../tests/unit/qdrant-client.test.ts | 16 ++++++++++++++++ packages/js-client-rest/src/qdrant-client.ts | 4 +++- .../tests/unit/qdrant-client.test.ts | 16 ++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/js-client-grpc/src/qdrant-client.ts b/packages/js-client-grpc/src/qdrant-client.ts index 0f0d0bc..7c2930c 100644 --- a/packages/js-client-grpc/src/qdrant-client.ts +++ b/packages/js-client-grpc/src/qdrant-client.ts @@ -47,7 +47,9 @@ export class QdrantClient { } const parsedUrl = new URL(url); this._host = parsedUrl.hostname; - this._port = parsedUrl.port ? Number(parsedUrl.port) : port; + const getPort = (url: string): number | null => + url.includes(':443') ? 443 : url.includes(':80') ? 80 : port; + this._port = parsedUrl.port ? Number(parsedUrl.port) : getPort(url); this._scheme = parsedUrl.protocol.replace(':', ''); if (this._prefix.length > 0 && parsedUrl.pathname !== '/') { diff --git a/packages/js-client-grpc/tests/unit/qdrant-client.test.ts b/packages/js-client-grpc/tests/unit/qdrant-client.test.ts index d08271c..69bf61b 100644 --- a/packages/js-client-grpc/tests/unit/qdrant-client.test.ts +++ b/packages/js-client-grpc/tests/unit/qdrant-client.test.ts @@ -41,6 +41,22 @@ test('QdrantClient()', () => { // @ts-expect-error ts(2341) expect(client._restUri).toBe('http://localhost:6334/custom'); + client = new QdrantClient({url: 'https://localhost:443'}); + // @ts-expect-error ts(2341) + expect(client._restUri).toBe('https://localhost:443'); + + client = new QdrantClient({url: 'https://localhost'}); + // @ts-expect-error ts(2341) + expect(client._restUri).toBe('https://localhost:6334'); + + client = new QdrantClient({url: 'http://localhost:80'}); + // @ts-expect-error ts(2341) + expect(client._restUri).toBe('http://localhost:80'); + + client = new QdrantClient({url: 'http://localhost:80', prefix: 'custom'}); + // @ts-expect-error ts(2341) + expect(client._restUri).toBe('http://localhost:80/custom'); + expect(() => new QdrantClient({url: 'my-domain.com'})).toThrow(QdrantClientConfigError); expect(() => new QdrantClient({url: 'my-domain.com:80'})).toThrow(QdrantClientConfigError); diff --git a/packages/js-client-rest/src/qdrant-client.ts b/packages/js-client-rest/src/qdrant-client.ts index 1af241e..59a37fd 100644 --- a/packages/js-client-rest/src/qdrant-client.ts +++ b/packages/js-client-rest/src/qdrant-client.ts @@ -62,7 +62,9 @@ export class QdrantClient { } const parsedUrl = new URL(url); this._host = parsedUrl.hostname; - this._port = parsedUrl.port ? Number(parsedUrl.port) : port; + const getPort = (url: string): number | null => + url.includes(':443') ? 443 : url.includes(':80') ? 80 : port; + this._port = parsedUrl.port ? Number(parsedUrl.port) : getPort(url); this._scheme = parsedUrl.protocol.replace(':', ''); if (this._prefix.length > 0 && parsedUrl.pathname !== '/') { diff --git a/packages/js-client-rest/tests/unit/qdrant-client.test.ts b/packages/js-client-rest/tests/unit/qdrant-client.test.ts index 7fd993e..c4db503 100644 --- a/packages/js-client-rest/tests/unit/qdrant-client.test.ts +++ b/packages/js-client-rest/tests/unit/qdrant-client.test.ts @@ -41,6 +41,22 @@ test('QdrantClient()', () => { // @ts-expect-error ts(2341) expect(client._restUri).toBe('http://localhost:6333/custom'); + client = new QdrantClient({url: 'https://localhost:443'}); + // @ts-expect-error ts(2341) + expect(client._restUri).toBe('https://localhost:443'); + + client = new QdrantClient({url: 'https://localhost'}); + // @ts-expect-error ts(2341) + expect(client._restUri).toBe('https://localhost:6333'); + + client = new QdrantClient({url: 'http://localhost:80'}); + // @ts-expect-error ts(2341) + expect(client._restUri).toBe('http://localhost:80'); + + client = new QdrantClient({url: 'http://localhost:80', prefix: 'custom'}); + // @ts-expect-error ts(2341) + expect(client._restUri).toBe('http://localhost:80/custom'); + expect(() => new QdrantClient({url: 'my-domain.com'})).toThrow(QdrantClientConfigError); expect(() => new QdrantClient({url: 'my-domain.com:80'})).toThrow(QdrantClientConfigError);