Skip to content

Commit

Permalink
remove legacy data-service
Browse files Browse the repository at this point in the history
  • Loading branch information
Anemy committed Jan 2, 2024
1 parent e6f8d81 commit 08f98bd
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 1,678 deletions.
1 change: 1 addition & 0 deletions .depcheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ ignores:
- style-loader
- ts-loader
- mongodb-runner
- "@mongodb-js/compass-utils"
1,977 changes: 355 additions & 1,622 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,9 @@
"@babel/parser": "^7.22.6",
"@babel/traverse": "^7.23.2",
"@mongodb-js/compass-components": "^1.20.0",
"@mongodb-js/compass-utils": "^0.5.8",
"@mongodb-js/connection-form": "^1.20.4",
"@mongodb-js/connection-storage": "^0.7.5",
"@mongodb-js/mongodb-constants": "^0.7.1",
"@mongosh/browser-runtime-electron": "^2.0.2",
"@mongosh/i18n": "^2.0.2",
Expand All @@ -991,7 +993,6 @@
"mongodb-cloud-info": "^2.1.0",
"mongodb-connection-string-url": "^2.6.0",
"mongodb-data-service": "^22.17.0",
"mongodb-data-service-legacy": "npm:[email protected]",
"mongodb-log-writer": "^1.4.0",
"mongodb-query-parser": "^3.1.3",
"mongodb-schema": "^11.2.2",
Expand Down Expand Up @@ -1087,11 +1088,6 @@
"xvfb-maybe": "^0.2.1",
"yargs-parser": "^20.2.9"
},
"overrides": {
"mongodb-connection-model": {
"@mongodb-js/compass-utils": "0.3.4"
}
},
"precommit": [
"check"
]
Expand Down
28 changes: 9 additions & 19 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ import { connect, createConnectionAttempt } from 'mongodb-data-service';
import type {
DataService,
ConnectionAttempt,
ConnectionOptions as ConnectionOptionsFromCurrentDS,
ConnectionOptions,
} from 'mongodb-data-service';
import ConnectionString from 'mongodb-connection-string-url';
import { EventEmitter } from 'events';
import type { MongoClientOptions } from 'mongodb';
import { v4 as uuidv4 } from 'uuid';
import { mongoLogId } from 'mongodb-log-writer';
import type {
ConnectionInfo as ConnectionInfoFromLegacyDS,
ConnectionOptions as ConnectionOptionsFromLegacyDS,
} from 'mongodb-data-service-legacy';
import { extractSecrets } from 'mongodb-data-service-legacy';
import { extractSecrets } from '@mongodb-js/connection-storage/main';

import { CONNECTION_STATUS } from './views/webview-app/extension-app-message-constants';
import { createLogger } from './logging';
Expand All @@ -26,13 +22,6 @@ import type { LoadedConnection } from './storage/connectionStorage';
import { ConnectionStorage } from './storage/connectionStorage';
import LINKS from './utils/links';

export function launderConnectionOptionTypeFromLegacyToCurrent(
opts: ConnectionOptionsFromLegacyDS
): ConnectionOptionsFromCurrentDS {
// Ensure that, at most, the types for OIDC mismatch here.
return opts as Omit<typeof opts, 'oidc'>;
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJSON = require('../package.json');

Expand Down Expand Up @@ -221,15 +210,18 @@ export default class ConnectionController {
}

async saveNewConnectionAndConnect(
originalConnectionInfo: ConnectionInfoFromLegacyDS,
connection: {
connectionOptions: ConnectionOptions;
id: string;
},
connectionType: ConnectionTypes
): Promise<ConnectionAttemptResult> {
const savedConnectionWithoutSecrets =
await this._connectionStorage.saveNewConnection(originalConnectionInfo);
await this._connectionStorage.saveNewConnection(connection);

this._connections[savedConnectionWithoutSecrets.id] = {
...savedConnectionWithoutSecrets,
connectionOptions: originalConnectionInfo.connectionOptions, // The connection options with secrets.
connectionOptions: connection.connectionOptions, // The connection options with secrets.
};

log.info(
Expand Down Expand Up @@ -289,9 +281,7 @@ export default class ConnectionController {

let dataService;
try {
dataService = await connectionAttempt.connect(
launderConnectionOptionTypeFromLegacyToCurrent(connectionOptions)
);
dataService = await connectionAttempt.connect(connectionOptions);

if (!dataService || connectionAttempt.isClosed()) {
return {
Expand Down
22 changes: 11 additions & 11 deletions src/storage/connectionStorage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as vscode from 'vscode';
import type {
ConnectionInfo as ConnectionInfoFromLegacyDS,
ConnectionOptions as ConnectionOptionsFromLegacyDS,
} from 'mongodb-data-service-legacy';
import {
getConnectionTitle,
extractSecrets,
mergeSecrets,
} from 'mongodb-data-service-legacy';
} from '@mongodb-js/connection-storage/main';
import type { ConnectionOptions } from 'mongodb-data-service';

import { createLogger } from '../logging';
import type StorageController from './storageController';
Expand All @@ -26,7 +23,7 @@ export interface StoreConnectionInfo {
name: string; // Possibly user given name, not unique.
storageLocation: StorageLocation;
secretStorageLocation?: SecretStorageLocationType;
connectionOptions?: ConnectionOptionsFromLegacyDS;
connectionOptions?: ConnectionOptions;
}

type StoreConnectionInfoWithConnectionOptions = StoreConnectionInfo &
Expand All @@ -46,16 +43,19 @@ export class ConnectionStorage {
}

// Returns the saved connection (without secrets).
async saveNewConnection(originalConnectionInfo: ConnectionInfoFromLegacyDS) {
const name = getConnectionTitle(originalConnectionInfo);
async saveNewConnection(connection: {
connectionOptions: ConnectionOptions;
id: string;
}): Promise<LoadedConnection> {
const name = getConnectionTitle(connection);
const newConnectionInfo = {
id: originalConnectionInfo.id,
id: connection.id,
name,
// To begin we just store it on the session, the storage controller
// handles changing this based on user preference.
storageLocation: StorageLocation.NONE,
secretStorageLocation: SecretStorageLocation.SecretStorage,
connectionOptions: originalConnectionInfo.connectionOptions,
connectionOptions: connection.connectionOptions,
};

return await this.saveConnectionWithSecrets(newConnectionInfo);
Expand Down Expand Up @@ -147,7 +147,7 @@ export class ConnectionStorage {
): Promise<LoadedConnection> {
// We don't want to store secrets to disc.
const { connectionInfo: safeConnectionInfo, secrets } = extractSecrets(
newStoreConnectionInfoWithSecrets as ConnectionInfoFromLegacyDS
newStoreConnectionInfoWithSecrets
);
const savedConnectionInfo = await this.saveConnection({
...newStoreConnectionInfoWithSecrets,
Expand Down
5 changes: 1 addition & 4 deletions src/test/suite/connectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as mongodbDataService from 'mongodb-data-service';

import ConnectionController, {
DataServiceEventTypes,
launderConnectionOptionTypeFromLegacyToCurrent,
} from '../../connectionController';
import formatError from '../../utils/formatError';
import { StorageController, StorageVariables } from '../../storage';
Expand Down Expand Up @@ -756,9 +755,7 @@ suite('Connection Controller Test Suite', function () {
await sleep(50);

return mongodbDataService.connect({
connectionOptions: launderConnectionOptionTypeFromLegacyToCurrent(
connectionOptions.connectionOptions
),
connectionOptions: connectionOptions.connectionOptions,
});
}
);
Expand Down
3 changes: 0 additions & 3 deletions src/test/suite/views/webview-app/jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ const { TextEncoder, TextDecoder } = require('util');
chai.use(require('sinon-chai'));
Enzyme.configure({ adapter: new Adapter() });

// eslint-disable-next-line no-undef
jest.mock('@iconify-icons/codicon/book', () => {});

// Note applied with js dom so we do manually. (Required by node_modules/mongodb-connection-string-url/node_modules/whatwg-url/lib/encoding.js)
Object.assign(global, { TextDecoder, TextEncoder });

Expand Down
6 changes: 4 additions & 2 deletions src/views/webview-app/connection-form.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import type { ComponentProps } from 'react';
import CompassConnectionForm from '@mongodb-js/connection-form';
import {
CancelLoader,
Expand All @@ -9,7 +10,6 @@ import {
useDarkMode,
} from '@mongodb-js/compass-components';
import { v4 as uuidv4 } from 'uuid';
import type { ConnectionInfo } from 'mongodb-data-service-legacy';

const modalContentStyles = css({
// Override LeafyGreen width to accommodate the strict connection-form size.
Expand Down Expand Up @@ -55,7 +55,9 @@ const initialConnectionInfo = createNewConnectionInfo();
const ConnectionForm: React.FunctionComponent<{
isConnecting: boolean;
onCancelConnectClicked: () => void;
onConnectClicked: (onConnectClicked: ConnectionInfo) => void;
onConnectClicked: ComponentProps<
typeof CompassConnectionForm
>['onConnectClicked'];
onClose: () => void;
open: boolean;
connectionErrorMessage: string;
Expand Down
7 changes: 5 additions & 2 deletions src/views/webview-app/extension-app-message-constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConnectionInfo } from 'mongodb-data-service-legacy';
import type { ConnectionOptions } from 'mongodb-data-service';

export enum CONNECTION_STATUS {
LOADING = 'LOADING', // When the connection status has not yet been shared from the extension.
Expand Down Expand Up @@ -41,7 +41,10 @@ export interface ConnectionStatusMessage extends BasicWebviewMessage {

export interface ConnectMessage extends BasicWebviewMessage {
command: MESSAGE_TYPES.CONNECT;
connectionInfo: ConnectionInfo;
connectionInfo: {
id: string;
connectionOptions: ConnectionOptions;
};
connectionAttemptId: string;
}

Expand Down
7 changes: 6 additions & 1 deletion src/views/webview-app/overview-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ const OverviewPage: React.FC = () => {
<ConnectionForm
isConnecting={isConnecting}
onCancelConnectClicked={handleCancelConnectClicked}
onConnectClicked={handleConnectClicked}
onConnectClicked={({ id, connectionOptions }) =>
handleConnectClicked({
id,
connectionOptions,
})
}
onClose={closeConnectionForm}
open={connectionFormOpened}
connectionErrorMessage={connectionErrorMessage}
Expand Down
9 changes: 6 additions & 3 deletions src/views/webview-app/use-connection-form.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';
import { v4 as uuidv4 } from 'uuid';
import type { ConnectionInfo } from 'mongodb-data-service-legacy';
import type { ConnectionOptions } from 'mongodb-data-service';
import {
sendConnectToExtension,
sendCancelConnectToExtension,
Expand Down Expand Up @@ -45,14 +45,17 @@ export default function useConnectionForm() {
handleCancelConnectClicked: () => {
sendCancelConnectToExtension();
},
handleConnectClicked: (connectionInfo: ConnectionInfo) => {
handleConnectClicked: (connectionAttempt: {
id: string;
connectionOptions: ConnectionOptions;
}) => {
// Clears the error message from previous connect attempt
setConnectionErrorMessage('');

const nextAttemptId = uuidv4();
setConnectionAttemptId(nextAttemptId);
setIsConnecting(true);
sendConnectToExtension(connectionInfo, nextAttemptId);
sendConnectToExtension(connectionAttempt, nextAttemptId);
},
};
}
4 changes: 2 additions & 2 deletions src/views/webview-app/vscode-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ConnectionInfo } from 'mongodb-data-service-legacy';
import {
MESSAGE_TYPES,
type MESSAGE_FROM_WEBVIEW_TO_EXTENSION,
type ConnectMessage,
} from './extension-app-message-constants';

interface VSCodeApi {
Expand All @@ -12,7 +12,7 @@ declare const acquireVsCodeApi: () => VSCodeApi;
const vscode = acquireVsCodeApi();

export const sendConnectToExtension = (
connectionInfo: ConnectionInfo,
connectionInfo: ConnectMessage['connectionInfo'],
connectionAttemptId: string
) => {
vscode.postMessage({
Expand Down
9 changes: 6 additions & 3 deletions src/views/webviewController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import path from 'path';
import crypto from 'crypto';
import type { ConnectionInfo } from 'mongodb-data-service-legacy';
import type { ConnectionOptions } from 'mongodb-data-service';

import type ConnectionController from '../connectionController';
import { ConnectionTypes } from '../connectionController';
Expand Down Expand Up @@ -98,13 +98,16 @@ export default class WebviewController {

handleWebviewConnectAttempt = async (
panel: vscode.WebviewPanel,
connectionInfo: ConnectionInfo,
connection: {
connectionOptions: ConnectionOptions;
id: string;
},
connectionAttemptId: string
) => {
try {
const { successfullyConnected, connectionErrorMessage } =
await this._connectionController.saveNewConnectionAndConnect(
connectionInfo,
connection,
ConnectionTypes.CONNECTION_FORM
);

Expand Down

0 comments on commit 08f98bd

Please sign in to comment.