Skip to content

Commit

Permalink
refactor: parse default http ports in url, fixes qdrant#59
Browse files Browse the repository at this point in the history
This change correctly parses default http
ports in url for rest and grpc packages.
  • Loading branch information
harshalmittal4 committed Feb 3, 2024
1 parent 48db336 commit dd729a2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/js-client-grpc/src/qdrant-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '/') {
Expand Down
16 changes: 16 additions & 0 deletions packages/js-client-grpc/tests/unit/qdrant-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion packages/js-client-rest/src/qdrant-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '/') {
Expand Down
16 changes: 16 additions & 0 deletions packages/js-client-rest/tests/unit/qdrant-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit dd729a2

Please sign in to comment.