Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed May 18, 2023
1 parent 57fc09a commit 1d5f677
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 86 deletions.
65 changes: 14 additions & 51 deletions src/kernels/jupyter/connection/jupyterConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Licensed under the MIT License.

import { inject, injectable } from 'inversify';
import { IExtensionSyncActivationService } from '../../../platform/activation/types';
import { IDisposableRegistry } from '../../../platform/common/types';
import { noop } from '../../../platform/common/utils/misc';
import { RemoteJupyterServerUriProviderError } from '../../errors/remoteJupyterServerUriProviderError';
import { BaseError } from '../../../platform/errors/types';
Expand All @@ -15,45 +13,24 @@ import {
generateUriFromRemoteProvider
} from '../jupyterUtils';
import {
IJupyterServerUri,
IJupyterServerUriStorage,
IJupyterSessionManager,
IJupyterSessionManagerFactory,
IJupyterUriProviderRegistration
} from '../types';

const MILLISECONDS_IN_A_WEEK = 7 * 24 * 60 * 60 * 1000;

/**
* Creates IJupyterConnection objects for URIs and 3rd party handles/ids.
*/
@injectable()
export class JupyterConnection implements IExtensionSyncActivationService {
private uriToJupyterServerUri = new Map<string, IJupyterServerUri>();
private pendingTimeouts: (NodeJS.Timeout | number)[] = [];
export class JupyterConnection {
constructor(
@inject(IJupyterUriProviderRegistration)
private readonly jupyterPickerRegistration: IJupyterUriProviderRegistration,
@inject(IJupyterSessionManagerFactory)
private readonly jupyterSessionManagerFactory: IJupyterSessionManagerFactory,
@inject(IDisposableRegistry)
private readonly disposables: IDisposableRegistry,
@inject(IJupyterServerUriStorage) private readonly serverUriStorage: IJupyterServerUriStorage
) {
disposables.push(this);
}
public activate() {
// When server URI changes, clear our pending URI timeouts
this.serverUriStorage.onDidChange(() => this.clearTimeouts(), this, this.disposables);
}
public dispose() {
this.clearTimeouts();
}
private clearTimeouts() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.pendingTimeouts.forEach((t) => clearTimeout(t as any));
this.pendingTimeouts = [];
}
) {}

public async createConnectionInfo(options: { serverId: string } | { uri: string }) {
const uri = 'uri' in options ? options.uri : await this.getUriFromServerId(options.serverId);
Expand All @@ -72,7 +49,7 @@ export class JupyterConnection implements IExtensionSyncActivationService {
return savedList.find((item) => item.serverId === serverId)?.uri;
}
private async createConnectionInfoFromUri(uri: string) {
const server = await this.updateServerUri(uri);
const server = await this.getJupyterServerUri(uri);
const idAndHandle = extractJupyterServerHandleAndId(uri);
return createRemoteConnectionInfo(uri, server, idAndHandle?.id);
}
Expand All @@ -93,33 +70,19 @@ export class JupyterConnection implements IExtensionSyncActivationService {
}
}

private async updateServerUri(uri: string) {
private async getJupyterServerUri(uri: string) {
const idAndHandle = extractJupyterServerHandleAndId(uri);
if (idAndHandle) {
try {
const serverUri = await this.jupyterPickerRegistration.getJupyterServerUri(
idAndHandle.id,
idAndHandle.handle
);
this.uriToJupyterServerUri.set(uri, serverUri);
// See if there's an expiration date, required to get the server info with new auth information.
if (serverUri.expiration) {
const timeout = serverUri.expiration.getTime() - Date.now();
// Week seems long enough (in case the expiration is ridiculous)
if (timeout > 0 && timeout < MILLISECONDS_IN_A_WEEK) {
this.pendingTimeouts.push(setTimeout(() => this.updateServerUri(uri).catch(noop), timeout));
}
}
return serverUri;
} catch (ex) {
if (ex instanceof BaseError) {
throw ex;
}
const serverId = await computeServerId(
generateUriFromRemoteProvider(idAndHandle.id, idAndHandle.handle)
);
throw new RemoteJupyterServerUriProviderError(idAndHandle.id, idAndHandle.handle, ex, serverId);
if (!idAndHandle) {
return;
}
try {
return await this.jupyterPickerRegistration.getJupyterServerUri(idAndHandle.id, idAndHandle.handle);
} catch (ex) {
if (ex instanceof BaseError) {
throw ex;
}
const serverId = await computeServerId(generateUriFromRemoteProvider(idAndHandle.id, idAndHandle.handle));
throw new RemoteJupyterServerUriProviderError(idAndHandle.id, idAndHandle.handle, ex, serverId);
}
}
}
6 changes: 0 additions & 6 deletions src/kernels/jupyter/connection/jupyterConnection.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ suite('Jupyter Connection', async () => {
jupyterConnection = new JupyterConnection(
instance(registrationPicker),
instance(sessionManagerFactory),
disposables,
instance(serverUriStorage)
);

Expand All @@ -57,16 +56,11 @@ suite('Jupyter Connection', async () => {
disposables.push(serverConnectionChangeEvent);

when(serverUriStorage.onDidChange).thenReturn(serverConnectionChangeEvent.event);

jupyterConnection.activate();
});
teardown(() => {
disposeAllDisposables(disposables);
});

test('Ensure event handler is added', () => {
verify(serverUriStorage.onDidChange).once();
});
test('Validation will result in fetching kernels and kernelspecs', async () => {
const uri = 'http://localhost:8888/?token=1234';
when(sessionManager.dispose()).thenResolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,12 @@ class MockProvider implements IJupyterUriProvider {
}
public async getServerUri(handle: string): Promise<IJupyterServerUri> {
if (handle === '1') {
const currentDate = new Date();
return {
// eslint-disable-next-line
baseUrl: 'http://foobar:3000',
token: '',
displayName: 'dummy',
authorizationHeader: { Bearer: this.currentBearer.toString() },
expiration: new Date(
currentDate.getFullYear(),
currentDate.getMonth(),
undefined,
currentDate.getHours(),
currentDate.getMinutes() + 1 // Expire after one minute
)
authorizationHeader: { Bearer: this.currentBearer.toString() }
};
}

Expand Down
1 change: 0 additions & 1 deletion src/kernels/jupyter/serviceRegistry.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export function registerTypes(serviceManager: IServiceManager, _isDevMode: boole
serviceManager.addSingleton<IJupyterRequestCreator>(IJupyterRequestCreator, JupyterRequestCreator);
serviceManager.addSingleton<IJupyterRequestAgentCreator>(IJupyterRequestAgentCreator, RequestAgentCreator);
serviceManager.addSingleton<JupyterConnection>(JupyterConnection, JupyterConnection);
serviceManager.addBinding(JupyterConnection, IExtensionSyncActivationService);
serviceManager.addSingleton<ILiveRemoteKernelConnectionUsageTracker>(
ILiveRemoteKernelConnectionUsageTracker,
LiveRemoteKernelConnectionUsageTracker
Expand Down
1 change: 0 additions & 1 deletion src/kernels/jupyter/serviceRegistry.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export function registerTypes(serviceManager: IServiceManager, _isDevMode: boole
serviceManager.addSingleton<IJupyterServerProvider>(IJupyterServerProvider, JupyterServerProvider);
serviceManager.addSingleton<IJupyterRequestCreator>(IJupyterRequestCreator, JupyterRequestCreator);
serviceManager.addSingleton<JupyterConnection>(JupyterConnection, JupyterConnection);
serviceManager.addBinding(JupyterConnection, IExtensionSyncActivationService);
serviceManager.addSingleton<ILiveRemoteKernelConnectionUsageTracker>(
ILiveRemoteKernelConnectionUsageTracker,
LiveRemoteKernelConnectionUsageTracker
Expand Down
4 changes: 0 additions & 4 deletions src/kernels/jupyter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ export interface IJupyterServerUri {
* Authorization header to be used when connecting to the server.
*/
authorizationHeader: Record<string, string>;
/**
* Date/time when the authorization header expires and should be refreshed.
*/
expiration?: Date;
displayName: string;
/**
* The local directory that maps to the remote directory of the Jupyter Server.
Expand Down
4 changes: 0 additions & 4 deletions src/standalone/api/extension.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export interface IJupyterServerUri {
* Authorization header to be used when connecting to the server.
*/
authorizationHeader: Record<string, string>;
/**
* Date/time when header expires and should be refreshed.
*/
expiration?: Date;
displayName: string;
/**
* The local directory that maps to the remote directory of the Jupyter Server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,10 @@ export class RemoteServerPickerExample implements IJupyterUriProvider {
// some other stuff
if (stdout) {
const output = JSON.parse(stdout.toString());
const currentDate = new Date();
resolve({
baseUrl: Compute_ServerUri,
token: '', //output.accessToken,
authorizationHeader: { Authorization: `Bearer ${output.accessToken}` },
expiration: new Date(
currentDate.getFullYear(),
currentDate.getMonth(),
undefined,
currentDate.getHours(),
currentDate.getMinutes() + 1 // Expire after one minute
)
authorizationHeader: { Authorization: `Bearer ${output.accessToken}` }
});
} else {
reject('Unable to get az token');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export interface IJupyterServerUri {
token: string;
// tslint:disable-next-line: no-any
authorizationHeader: any; // JSON object for authorization header.
expiration?: Date; // Date/time when header expires and should be refreshed.
}

export type JupyterServerUriHandle = string;
Expand Down

0 comments on commit 1d5f677

Please sign in to comment.