Skip to content

Commit

Permalink
Refactoring invalid types
Browse files Browse the repository at this point in the history
  • Loading branch information
d-gubert committed Dec 8, 2023
1 parent eec81c3 commit 3a54001
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 81 deletions.
4 changes: 2 additions & 2 deletions deno-runtime/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async function handleRequest({ type, payload }: Messenger.JsonRpcRequest): Promi
const { id, method, params } = payload;

switch (method) {
case 'construct': {
case 'app:construct': {
const [appPackage] = params as [IParseAppPackageResult];

if (!appPackage?.info?.id || !appPackage?.info?.classFile || !appPackage?.files) {
Expand Down Expand Up @@ -142,7 +142,7 @@ function handleResponse(response: Messenger.JsonRpcResponse): void {
}

async function main() {
setTimeout(() => Messenger.sendNotification({ method: 'ready' }), 1_780);
Messenger.sendNotification({ method: 'ready' });

const decoder = new TextDecoder();

Expand Down
12 changes: 6 additions & 6 deletions src/server/AppManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import type { IMarketplaceInfo } from './marketplace';
import { DisabledApp } from './misc/DisabledApp';
import { defaultPermissions } from './permissions/AppPermissions';
import { ProxiedApp } from './ProxiedApp';
import { AppsEngineEmptyRuntime } from './runtime/AppsEngineEmptyRuntime';
import type { IAppStorageItem } from './storage';
import { AppLogStorage, AppMetadataStorage } from './storage';
import { AppSourceStorage } from './storage/AppSourceStorage';
import { AppInstallationSource } from './storage/IAppStorageItem';
import { AppsEngineDenoRuntime } from './runtime/AppsEngineDenoRuntime';
import { AppRuntimeManager } from './managers/AppRuntimeManager';
import type { DenoRuntimeSubprocessController } from './runtime/AppsEngineDenoRuntime';

export interface IAppInstallParameters {
enable: boolean;
Expand Down Expand Up @@ -100,7 +100,7 @@ export class AppManager {

private readonly signatureManager: AppSignatureManager;

private readonly runtime: AppsEngineDenoRuntime;
private readonly runtime: AppRuntimeManager;

private isLoaded: boolean;

Expand Down Expand Up @@ -149,7 +149,7 @@ export class AppManager {
this.uiActionButtonManager = new UIActionButtonManager(this);
this.videoConfProviderManager = new AppVideoConfProviderManager(this);
this.signatureManager = new AppSignatureManager(this);
this.runtime = new AppsEngineDenoRuntime(this);
this.runtime = new AppRuntimeManager(this);

this.isLoaded = false;
AppManager.Instance = this;
Expand Down Expand Up @@ -230,7 +230,7 @@ export class AppManager {
return this.signatureManager;
}

public getRuntime(): AppsEngineDenoRuntime {
public getRuntime(): AppRuntimeManager {
return this.runtime;
}

Expand Down Expand Up @@ -273,7 +273,7 @@ export class AppManager {
app.getLogger().error(e);
await this.logStorage.storeEntries(app.getID(), app.getLogger());

const prl = new ProxiedApp(this, item, app, new AppsEngineEmptyRuntime(app));
const prl = new ProxiedApp(this, item, {} as DenoRuntimeSubprocessController);
this.apps.set(item.id, prl);
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/server/managers/AppRuntimeManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { AppManager } from '../AppManager';
import type { IParseAppPackageResult } from '../compiler';
import { DenoRuntimeSubprocessController } from '../runtime/AppsEngineDenoRuntime';

export type AppRuntimeParams = {
appId: string;
appSource: string;
};

export type ExecRequestContext = {
method: string;
params: unknown[];
};

export type ExecRequestOptions = {
timeout?: number;
};

export class AppRuntimeManager {
private readonly subprocesses: Record<string, DenoRuntimeSubprocessController> = {};

constructor(private readonly manager: AppManager) {}

public async startRuntimeForApp(appPackage: IParseAppPackageResult, options = { force: false }): Promise<DenoRuntimeSubprocessController> {
const { id: appId } = appPackage.info;

if (appId in this.subprocesses && !options.force) {
throw new Error('App already has an associated runtime');
}

this.subprocesses[appId] = new DenoRuntimeSubprocessController(this.manager, appPackage);

await this.subprocesses[appId].setupApp();

return this.subprocesses[appId];
}

public async runInSandbox(appId: string, execRequest: ExecRequestContext, options?: ExecRequestOptions): Promise<unknown> {
const subprocess = this.subprocesses[appId];

if (!subprocess) {
throw new Error('App does not have an associated runtime');
}

return subprocess.sendRequest(execRequest);
}
}
2 changes: 2 additions & 0 deletions src/server/managers/AppVideoConfProviderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ export class AppVideoConfProviderManager {
}

public async isFullyConfigured(providerName: string): Promise<boolean> {
console.log(1)
const providerInfo = this.retrieveProviderInfo(providerName);
console.log(2)
if (!providerInfo) {
throw new VideoConfProviderNotRegisteredError(providerName);
}
Expand Down
59 changes: 7 additions & 52 deletions src/server/runtime/AppsEngineDenoRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ import { EventEmitter } from 'stream';

import * as jsonrpc from 'jsonrpc-lite';

import type { AppAccessorManager, AppApiManager } from '../managers';
import type { AppManager } from '../AppManager';
import type { AppBridges } from '../bridges';
import type { IParseAppPackageResult } from '../compiler';
import type { AppStatus } from '../../definition/AppStatus';
import type { AppAccessorManager, AppApiManager } from '../managers';

export type AppRuntimeParams = {
appId: string;
appSource: string;
};

const ALLOWED_ACCESSOR_METHODS = [
export const ALLOWED_ACCESSOR_METHODS = [
'getConfigurationExtend',
'getEnvironmentRead',
'getEnvironmentWrite',
Expand All @@ -38,7 +33,7 @@ const ALLOWED_ACCESSOR_METHODS = [
>
>;

function isValidOrigin(accessor: string): accessor is typeof ALLOWED_ACCESSOR_METHODS[number] {
export function isValidOrigin(accessor: string): accessor is typeof ALLOWED_ACCESSOR_METHODS[number] {
return ALLOWED_ACCESSOR_METHODS.includes(accessor as any);
}

Expand All @@ -65,7 +60,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
private readonly deno: child_process.ChildProcess;

private readonly options = {
timeout: 10_000,
timeout: 10000,
};

private state: 'uninitialized' | 'ready' | 'invalid' | 'unknown';
Expand All @@ -77,7 +72,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
private readonly bridges: AppBridges;

// We need to keep the appSource around in case the Deno process needs to be restarted
constructor(private readonly appPackage: IParseAppPackageResult, manager: AppManager) {
constructor(manager: AppManager, private readonly appPackage: IParseAppPackageResult) {
super();

this.state = 'uninitialized';
Expand Down Expand Up @@ -114,13 +109,13 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
}

public async getStatus(): Promise<AppStatus> {
return this.sendRequest({ method: 'getStatus', params: [] }) as Promise<AppStatus>;
return this.sendRequest({ method: 'app:getStatus', params: [] }) as Promise<AppStatus>;
}

public async setupApp() {
await this.waitUntilReady();

this.sendRequest({ method: 'construct', params: [this.appPackage] });
this.sendRequest({ method: 'app:construct', params: [this.appPackage] });
}

public async sendRequest(message: Pick<jsonrpc.RequestObject, 'method' | 'params'>): Promise<unknown> {
Expand Down Expand Up @@ -198,7 +193,6 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
* const accessorMethods = ['getEnvironmentReader', 'getEnvironmentVariables']
* ```
**/

// Prevent app from trying to get properties from the manager that
// are not intended for public access
if (!isValidOrigin(managerOrigin)) {
Expand Down Expand Up @@ -350,42 +344,3 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
console.error(chunk.toString());
}
}

export type ExecRequestContext = {
method: string;
params: unknown[];
};

export type ExecRequestOptions = {
timeout?: number;
};

export class AppsEngineDenoRuntime {
private readonly subprocesses: Record<string, DenoRuntimeSubprocessController> = {};

constructor(private readonly manager: AppManager) {}

public async startRuntimeForApp(appPackage: IParseAppPackageResult, options = { force: false }): Promise<DenoRuntimeSubprocessController> {
const { id: appId } = appPackage.info;

if (appId in this.subprocesses && !options.force) {
throw new Error('App already has an associated runtime');
}

this.subprocesses[appId] = new DenoRuntimeSubprocessController(appPackage, this.manager);

await this.subprocesses[appId].setupApp();

return this.subprocesses[appId];
}

public async runInSandbox(appId: string, execRequest: ExecRequestContext, options?: ExecRequestOptions): Promise<unknown> {
const subprocess = this.subprocesses[appId];

if (!subprocess) {
throw new Error('App does not have an associated runtime');
}

return subprocess.sendRequest(execRequest);
}
}
5 changes: 2 additions & 3 deletions tests/server/compiler/AppFabricationFulfillment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Expect, Test } from 'alsatian';

import type { App } from '../../../src/definition/App';
import { AppStatus } from '../../../src/definition/AppStatus';
import type { IAppInfo } from '../../../src/definition/metadata';
import { AppInterface } from '../../../src/definition/metadata';
import type { AppManager } from '../../../src/server/AppManager';
import { AppFabricationFulfillment } from '../../../src/server/compiler';
import { ProxiedApp } from '../../../src/server/ProxiedApp';
import { AppsEngineEmptyRuntime } from '../../../src/server/runtime/AppsEngineEmptyRuntime';
import type { IAppStorageItem } from '../../../src/server/storage';
import type { DenoRuntimeSubprocessController } from '../../../src/server/runtime/AppsEngineDenoRuntime';

export class AppFabricationFulfillmentTestFixture {
@Test()
Expand Down Expand Up @@ -42,7 +41,7 @@ export class AppFabricationFulfillmentTestFixture {
Expect(() => aff.setImplementedInterfaces(expectedInter)).not.toThrow();
Expect(aff.getImplementedInferfaces()).toEqual(expectedInter);

const fakeApp = new ProxiedApp({} as AppManager, { status: AppStatus.UNKNOWN } as IAppStorageItem, {} as App, new AppsEngineEmptyRuntime(null));
const fakeApp = new ProxiedApp({} as AppManager, { status: AppStatus.UNKNOWN } as IAppStorageItem, {} as DenoRuntimeSubprocessController);
Expect(() => aff.setApp(fakeApp)).not.toThrow();
Expect(aff.getApp()).toEqual(fakeApp);
}
Expand Down
8 changes: 7 additions & 1 deletion tests/server/runtime/DenoRuntimeSubprocessController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TestData, TestInfastructureSetup } from '../../test-data/utilities';
import { DenoRuntimeSubprocessController } from '../../../src/server/runtime/AppsEngineDenoRuntime';
import type { AppManager } from '../../../src/server/AppManager';
import { UserStatusConnection, UserType } from '../../../src/definition/users';
import { AppImplements } from '../../../src/server/compiler';

@TestFixture('DenoRuntimeSubprocessController')
export class DenuRuntimeSubprocessControllerTestFixture {
Expand Down Expand Up @@ -32,7 +33,12 @@ export class DenuRuntimeSubprocessControllerTestFixture {
public setup() {
const app = TestData.getMockApp('deno-controller', 'Deno Controller test');

this.controller = new DenoRuntimeSubprocessController(app.getID(), this.simpleAppSource, this.manager);
this.controller = new DenoRuntimeSubprocessController(this.manager, {
info: app.getInfo(),
files: { 'main.ts': this.simpleAppSource },
languageContent: {},
implemented: new AppImplements(),
});
}

@AsyncTest('correctly identifies a call to the HTTP accessor')
Expand Down
19 changes: 2 additions & 17 deletions tests/test-data/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { TestSourceStorage } from './storage/TestSourceStorage';
import type { IApi, IApiRequest, IApiResponse } from '../../src/definition/api';
import { ApiSecurity, ApiVisibility } from '../../src/definition/api';
import type { IApiEndpointInfo } from '../../src/definition/api/IApiEndpointInfo';
import type { App } from '../../src/definition/App';
import { AppStatus } from '../../src/definition/AppStatus';
import type { AppVideoConference } from '../../src/definition/videoConferences/AppVideoConference';
import type { VideoConference } from '../../src/definition/videoConferences/IVideoConference';
Expand All @@ -34,6 +33,7 @@ import type {
AppVideoConfProviderManager,
} from '../../src/server/managers';
import type { UIActionButtonManager } from '../../src/server/managers/UIActionButtonManager';
import type { DenoRuntimeSubprocessController } from '../../src/server/runtime/AppsEngineDenoRuntime';

export class TestInfastructureSetup {
private appStorage: TestsAppStorage;
Expand Down Expand Up @@ -418,22 +418,7 @@ export class TestData {
}

public static getMockApp(id: string, name: string): ProxiedApp {
return new ProxiedApp(
{} as AppManager,
{ status: AppStatus.UNKNOWN } as IAppStorageItem,
{
getName() {
return name;
},
getID() {
return id;
},
getRuntime() {
return { runInSandbox: (mod: string) => mod };
},
} as unknown as App,
{ runInSandbox: (mod: string) => mod } as any,
);
return new ProxiedApp({} as AppManager, { status: AppStatus.UNKNOWN, info: { id, name } } as IAppStorageItem, {} as DenoRuntimeSubprocessController);
}
}

Expand Down

0 comments on commit 3a54001

Please sign in to comment.