Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: parse default http ports in url, fixes #59 #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if url has localhost:8000? url.includes(':80') will still match

Copy link
Author

@harshalmittal4 harshalmittal4 Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case parsedUrl.port on line 52 will return 8000, so getPort will not be invoked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, in which case getPort will be invoked? If port is not specified explicitly? If so, why are we doing those checks for url.includes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invoked in case of urls having default ports (443 for https and 80 for http) or no port specified.
#61 (comment)

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