Skip to content

Commit

Permalink
Installed @deephaven/jsapi-types, improved DhService base types, and …
Browse files Browse the repository at this point in the history
…tweaked download tmp caching (#79-3)
  • Loading branch information
bmingles committed Aug 8, 2024
1 parent 63ecaed commit b2f6990
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 3,773 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
}
},
"devDependencies": {
"@deephaven/jsapi-types": "^1.0.0-dev0.35.3",
"@types/node": "20.14.10",
"@types/vscode": "^1.91.0",
"@types/ws": "^8.5.10",
Expand Down
3,746 changes: 0 additions & 3,746 deletions src/dh/dhc-types.d.ts

This file was deleted.

19 changes: 8 additions & 11 deletions src/dh/dhc.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import type { dh as DhType } from './dhc-types';
import type { dh as DhType } from '@deephaven/jsapi-types';
import {
downloadFromURL,
getTempDir,
NoConsoleTypesError,
polyfillDh,
urlToDirectoryName,
} from '../util';
import { ConnectionAndSession } from '../common';

Expand All @@ -32,12 +33,7 @@ export function getEmbedWidgetUrl(

export async function initDhcApi(serverUrl: string): Promise<typeof DhType> {
polyfillDh();

const tempDir = getTempDir();

const dhc = await getDhc(serverUrl, tempDir, true);

return dhc;
return getDhc(serverUrl, true);
}

export async function initDhcSession(
Expand Down Expand Up @@ -71,16 +67,17 @@ export async function initDhcSession(
*/
async function getDhc(
serverUrl: string,
outDir: string,
download: boolean
): Promise<typeof DhType> {
const tmpDir = getTempDir(false, urlToDirectoryName(serverUrl));

if (download) {
const dhInternal = await downloadFromURL(
path.join(serverUrl, 'jsapi/dh-internal.js')
);
// Convert to .cjs
fs.writeFileSync(
path.join(outDir, 'dh-internal.cjs'),
path.join(tmpDir, 'dh-internal.cjs'),
dhInternal.replace(
`export{__webpack_exports__dhinternal as dhinternal};`,
`module.exports={dhinternal:__webpack_exports__dhinternal};`
Expand All @@ -91,7 +88,7 @@ async function getDhc(
path.join(serverUrl, 'jsapi/dh-core.js')
);
fs.writeFileSync(
path.join(outDir, 'dh-core.cjs'),
path.join(tmpDir, 'dh-core.cjs'),
// Convert to .cjs
dhCore
.replace(
Expand All @@ -102,5 +99,5 @@ async function getDhc(
);
}

return require(path.join(outDir, 'dh-core.cjs'));
return require(path.join(tmpDir, 'dh-core.cjs'));
}
20 changes: 18 additions & 2 deletions src/services/DhService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import type { dh as DhcType } from '../dh/dhc-types';
import type { dh as DhcType } from '@deephaven/jsapi-types';
import { hasErrorCode } from '../util/typeUtils';
import { ConnectionAndSession, Disposable } from '../common';
import {
Expand Down Expand Up @@ -36,7 +36,23 @@ type CommandResultBase = {
error: string;
};

export abstract class DhService<TDH, TClient>
/**
* Helper type to make it easier to use `DhService` derived classes as newable
* factory functions. This type should mirror the constructor of `DhService`.
*/
export type DhServiceConstructor<
T extends DhService<TDH, TClient>,
TDH = unknown,
TClient = unknown,
> = new (
serverUrl: string,
panelRegistry: ExtendedMap<string, vscode.WebviewPanel>,
diagnosticsCollection: vscode.DiagnosticCollection,
outputChannel: vscode.OutputChannel,
toaster: Toaster
) => T;

export abstract class DhService<TDH = unknown, TClient = unknown>
extends EventDispatcher<'disconnect'>
implements Disposable
{
Expand Down
5 changes: 3 additions & 2 deletions src/services/DhServiceRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as vscode from 'vscode';
import { CacheService } from './CacheService';
import { DhcService, DhcServiceConstructor } from './DhcService';
import { type DhServiceConstructor } from './DhService';
import { DhcService } from './DhcService';
import { ensureHasTrailingSlash, ExtendedMap, Toaster } from '../util';

export class DhServiceRegistry<T extends DhcService> extends CacheService<
T,
'disconnect'
> {
constructor(
serviceFactory: DhcServiceConstructor<T>,
serviceFactory: DhServiceConstructor<T>,
panelRegistry: ExtendedMap<string, vscode.WebviewPanel>,
diagnosticsCollection: vscode.DiagnosticCollection,
outputChannel: vscode.OutputChannel,
Expand Down
12 changes: 2 additions & 10 deletions src/services/DhcService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import type { dh as DhcType } from '../dh/dhc-types';
import type { dh as DhcType } from '@deephaven/jsapi-types';
import DhService from './DhService';
import {
AUTH_HANDLER_TYPE_ANONYMOUS,
Expand All @@ -8,19 +8,11 @@ import {
initDhcApi,
initDhcSession,
} from '../dh/dhc';
import { ExtendedMap, getPanelHtml, Logger, Toaster } from '../util';
import { getPanelHtml, Logger } from '../util';
import { ConnectionAndSession } from '../common';

const logger = new Logger('DhcService');

export type DhcServiceConstructor<T extends DhcService> = new (
serverUrl: string,
panelRegistry: ExtendedMap<string, vscode.WebviewPanel>,
diagnosticsCollection: vscode.DiagnosticCollection,
outputChannel: vscode.OutputChannel,
toaster: Toaster
) => T;

export class DhcService extends DhService<typeof DhcType, DhcType.CoreClient> {
private psk?: string;

Expand Down
3 changes: 3 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module dhinternal.io.deephaven.proto.ticket_pb {
export type TypedTicket = unknown;
}
7 changes: 5 additions & 2 deletions src/util/downloadUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { Logger } from './Logger';

const logger = new Logger('downloadUtils');

export function getTempDir(recreate = false) {
const tempDir = path.join(__dirname, 'tmp');
export function getTempDir(recreate: boolean, subDirectory?: string) {
let tempDir = path.join(__dirname, '..', 'tmp');
if (subDirectory != null) {
tempDir = path.join(tempDir, subDirectory);
}

if (recreate) {
try {
Expand Down
13 changes: 13 additions & 0 deletions src/util/urlUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from 'vitest';
import { urlToDirectoryName } from './urlUtils';

describe('urlUtils', () => {
it.each([
['http://localhost:4000', 'localhost_4000'],
['https://localhost:5000', 'localhost_5000'],
['http://www.acme.com:6000', 'www_acme_com_6000'],
['https://www.acme.com:7000', 'www_acme_com_7000'],
])('should convert url to host_port string: %s, %s', (given, expected) => {
expect(urlToDirectoryName(given)).toBe(expected);
});
});
12 changes: 12 additions & 0 deletions src/util/urlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ export function getServerUrlAndPath(uri: vscode.Uri) {
path,
};
}

/**
* Converts url to `${hostname}_${port}` replacing `.` with `_`
* @param url The URL to convert
*/
export function urlToDirectoryName(url: string | URL): string {
if (typeof url === 'string') {
url = new URL(url);
}

return url.host.replace(/[:.]/g, '_');
}

0 comments on commit b2f6990

Please sign in to comment.