Skip to content
Merged
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
18 changes: 1 addition & 17 deletions packages/core/src/connection/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class UserPasswordAuthenticator implements OidcAuthFlow {
}

refresh = () => {
this.validateOpenidConfig();
this.openidConfig.scopes.push('offline_access');
return this.requestAccessToken()
.then((tokenResp: RequestAccessTokenResponse) => {
return {
Expand All @@ -197,22 +197,6 @@ class UserPasswordAuthenticator implements OidcAuthFlow {
});
};

validateOpenidConfig = () => {
if (
this.openidConfig.provider.grant_types_supported !== undefined &&
!this.openidConfig.provider.grant_types_supported.includes('password')
) {
throw new Error('grant_type password not supported');
}
if (this.openidConfig.provider.token_endpoint.includes('https://login.microsoftonline.com')) {
throw new Error(
'microsoft/azure recommends to avoid authentication using ' +
'username and password, so this method is not supported by this client'
);
}
this.openidConfig.scopes.push('offline_access');
};

requestAccessToken = () => {
const url = this.openidConfig.provider.token_endpoint;
const params = new URLSearchParams({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/connection/http.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import OpenidConfigurationGetter from '../v2/misc/openidConfigurationGetter.js';

import { isAbortError } from 'abort-controller-x';
import { Agent } from 'http';
import type { Agent } from 'http';
import {
WeaviateInsufficientPermissionsError,
WeaviateInvalidInputError,
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import { DbVersion } from './utils/dbVersion.js';
import { Backend, BackupCompressionLevel, BackupStatus } from './v2/backup/index.js';
import MetaGetter from './v2/misc/metaGetter.js';

import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';
import type { Agent } from 'http';
import { LiveChecker, OpenidConfigurationGetter, ReadyChecker } from './v2/misc/index.js';

import weaviateV2 from './v2/index.js';
Expand Down Expand Up @@ -133,6 +132,12 @@ export const cleanHost = (host: string, protocol: 'rest' | 'grpc') => {
export type Context<TMedia> = {
transportsMaker: TransportsMaker;
toBase64FromMedia: ToBase64FromMedia<TMedia>;
/**
* Creates the HTTP(S) keep-alive agent for the REST/GraphQL connection. Supplied by the Node shim
* (`@weaviate/node`); omitted by the browser shim (`@weaviate/web`) so no Node `http`/`https`
* builtins are pulled into the browser bundle. When undefined, `fetch` runs without a custom agent.
*/
agentMaker?: (secure: boolean) => Agent | undefined;
};

const client = async <TMedia>(
Expand All @@ -151,7 +156,7 @@ const client = async <TMedia>(
if (!params.headers) params.headers = {};

const scheme = httpSecure ? 'https' : 'http';
const agent = httpSecure ? new HttpsAgent({ keepAlive: true }) : new HttpAgent({ keepAlive: true });
const agent = context.agentMaker?.(httpSecure);

const { connection, dbVersionProvider, dbVersionSupport } = await ConnectionGRPC.use(
context.transportsMaker,
Expand Down
3 changes: 3 additions & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import weaviate, {
permissions,
reconfigure,
} from '@weaviate/core';
import { Agent as HttpAgent } from 'http';
import { Agent as HttpsAgent } from 'https';
import { toBase64FromMedia } from './base64.js';
import { transportsMaker } from './transports.js';

const context: Context<string | Buffer> = {
transportsMaker,
toBase64FromMedia,
agentMaker: (secure) => (secure ? new HttpsAgent({ keepAlive: true }) : new HttpAgent({ keepAlive: true })),
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/test/web/connect.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect } from 'vitest';
import weaviate from '@weaviate/web';
import { requireAtLeast } from '../../version';
import { requireAtLeast } from '../version';

requireAtLeast(1, 39, 0).describe('connectToLocal', () => {
it('should connect to a local Weaviate instance using grpc-web', async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { transportsMaker } from './transports.js';
const context: Context<string | Blob> = {
transportsMaker,
toBase64FromMedia,
// No `agentMaker` on purpose: the browser uses fetch-based gRPC-Web (and fetch for REST), so no Node
// `http`/`https` Agent is needed. Omitting it keeps those Node builtins out of the browser bundle.
};

export type ConnectToLocalOptions = Omit<ConnectToLocalOptionsCore, 'grpcPort'>;
Expand Down
Loading