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

Remove default port to improve security and flexibility for self-hosted instances #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 21 additions & 11 deletions packages/js-client-rest/src/qdrant-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class QdrantClient {
private _restUri: string;
private _openApiClient: OpenApiClient;

constructor({url, host, apiKey, https, prefix, port = 6333, timeout = 300_000, ...args}: QdrantClientParams = {}) {
constructor({url, host, apiKey, https, prefix, port=0, timeout = 300_000, ...args}: QdrantClientParams = {}) {
this._https = https ?? typeof apiKey === 'string';
this._scheme = this._https ? 'https' : 'http';
this._prefix = prefix ?? '';
Expand All @@ -49,11 +49,16 @@ export class QdrantClient {
`Only one of \`url\`, \`host\` params can be set. Url is ${url}, host is ${host}`,
);
}
if (host && (host.startsWith('http://') || host.startsWith('https://') || /:\d+$/.test(host))) {
throw new QdrantClientConfigError(
'The `host` param is not expected to contain neither protocol (http:// or https://) nor port (:6333).\n' +
'Try to use the `url` parameter instead.',
);

if (host) {
if (host.startsWith('http://') || host.startsWith('https://')) {
throw new QdrantClientConfigError(
'The `host` param is not expected to contain a protocol (http:// or https://).\n' +
'Try to use the `url` parameter instead.',
);
}
this._host = host;
this._port = port || null; // Use port if provided, otherwise null
} else if (url) {
if (!(url.startsWith('http://') || url.startsWith('https://'))) {
throw new QdrantClientConfigError(
Expand All @@ -62,7 +67,7 @@ export class QdrantClient {
}
const parsedUrl = new URL(url);
this._host = parsedUrl.hostname;
this._port = parsedUrl.port ? Number(parsedUrl.port) : port;
this._port = parsedUrl.port ? Number(parsedUrl.port) : null;
this._scheme = parsedUrl.protocol.replace(':', '');

if (this._prefix.length > 0 && parsedUrl.pathname !== '/') {
Expand All @@ -72,8 +77,8 @@ export class QdrantClient {
);
}
} else {
this._port = port;
this._host = host ?? '127.0.0.1';
this._host = '127.0.0.1';
this._port = port || 6333; // Use default port 6333 if not provided
}

const headers = new Headers([['user-agent', 'qdrant-js']]);
Expand All @@ -92,8 +97,13 @@ export class QdrantClient {
headers.set('api-key', apiKey);
}

const address = this._port ? `${this._host}:${this._port}` : this._host;
this._restUri = `${this._scheme}://${address}${this._prefix}`;
// Construct the REST URI
this._restUri = `${this._scheme}://${this._host}`;
if (this._port) {
this._restUri += `:${this._port}`;
}
this._restUri += this._prefix;

const connections = args.maxConnections;
const restArgs: RestArgs = {headers, timeout, connections};

Expand Down