Skip to content

Commit

Permalink
WIP: remoteMessageChannelFs
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Oct 1, 2024
1 parent f746fa2 commit 0ad4aa3
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 179 deletions.
8 changes: 8 additions & 0 deletions package-lock.json

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

7 changes: 7 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"./vscode/services": {
"types": "./lib/vscode/index.d.ts",
"default": "./lib/vscode/index.js"
},
"./fs": {
"types": "./lib/fs/index.d.ts",
"default": "./lib/fs/index.js"
}
},
"typesVersions": {
Expand All @@ -43,6 +47,9 @@
],
"vscode/services": [
"lib/vscode/index"
],
"fs": [
"lib/fs/index"
]
}
},
Expand Down
30 changes: 13 additions & 17 deletions packages/client/src/fs/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export type FileReadResultStatus = 'success' | 'denied';

export interface FileReadRequestResult {
status: FileReadResultStatus
content: string
content: string | ArrayBuffer
}

export interface FileUpdate {
resourceUri: string
content: string
content: string | ArrayBuffer
}

export type FileUpdateResultStatus = 'equal' | 'updated' | 'created' | 'denied';
Expand Down Expand Up @@ -92,12 +92,6 @@ export interface FileSystemCapabilities {
*/
listFiles(params: DirectoryListingRequest): Promise<DirectoryListingRequestResult>

/**
* Set an optional logger
* @param logger the logger implemenation
*/
setLogger?(logger: Logger): void;

}

/**
Expand All @@ -106,18 +100,20 @@ export interface FileSystemCapabilities {
export interface FileSystemEndpoint extends FileSystemCapabilities {

/**
* Get the type of the client
* Whatever can't be handled in the constructor should be done here
*/
getEndpointType(): EndpointType;

getFileSystem(): FileSystemRealization;
init?(): void;

}
/**
* Set an optional logger
* @param logger the logger implemenation
*/
setLogger?(logger: Logger): void;

/**
* Defines the API for the file system in the local environment
*/
export interface FileSystemRealization extends FileSystemCapabilities {
/**
* Get the type of the client
*/
getEndpointType(): EndpointType;

/**
* Provide info about the file system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,57 @@
* ------------------------------------------------------------------------------------------ */

import { Logger } from 'monaco-languageclient/tools';
import { DirectoryListingRequest, DirectoryListingRequestResult, EndpointType, FileReadRequest, FileReadRequestResult, FileSystemEndpoint, FileSystemRealization, FileUpdate, FileUpdateResult, StatsRequest, StatsRequestResult } from '../definitions.js';
import { DirectoryListingRequest, DirectoryListingRequestResult, EndpointType, FileReadRequest, FileReadRequestResult, FileSystemEndpoint, FileUpdate, FileUpdateResult, StatsRequest, StatsRequestResult } from '../definitions.js';

export class EmptyFileSystemEndpoint implements FileSystemEndpoint {

private fileSystem: FileSystemRealization;
private endpointType: EndpointType;
private logger?: Logger;

constructor(fileSystem: FileSystemRealization) {
this.fileSystem = fileSystem;
constructor(endpointType: EndpointType) {
this.endpointType = endpointType;
}

init(): void { }

getFileSystemInfo(): string {
return 'This file system performs no operations.';
}

setLogger(logger: Logger): void {
this.logger = logger;
}

getEndpointType(): EndpointType {
return EndpointType.EMPTY;
}

getFileSystem(): FileSystemRealization {
return this.fileSystem;
return this.endpointType;
}

readFile(params: FileReadRequest): Promise<FileReadRequestResult> {
this.logger?.info(`Reading file: ${params.resourceUri}`);
return this.fileSystem.readFile(params);
return Promise.resolve({
status: 'denied',
content: ''
});
}

writeFile(params: FileUpdate): Promise<FileUpdateResult> {
this.logger?.info(`Writing file: ${params.resourceUri}`);
return this.fileSystem.writeFile(params);
return Promise.resolve({ status: 'denied' });
}

syncFile(params: FileUpdate): Promise<FileUpdateResult> {
this.logger?.info(`Syncing file: ${params.resourceUri}`);
return this.fileSystem.writeFile(params);
return Promise.resolve({ status: 'denied' });
}

getFileStats(params: StatsRequest): Promise<StatsRequestResult> {
this.logger?.info(`Getting file stats for: "${params.resourceUri}" (${params.type})`);
return this.fileSystem.getFileStats(params);
return Promise.reject('No stats available.');
}

listFiles(params: DirectoryListingRequest): Promise<DirectoryListingRequestResult> {
this.logger?.info(`Listing files for directory: "${params.directoryUri}"`);
return this.fileSystem.listFiles(params);
return Promise.reject('No file listing possible.');
}

}
7 changes: 7 additions & 0 deletions packages/client/src/fs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2024 TypeFox and others.
* Licensed under the MIT License. See LICENSE in the package root for license information.
* ------------------------------------------------------------------------------------------ */

export * from './definitions.js';
export * from './endpoints/defaultEndpoint.js';
37 changes: 0 additions & 37 deletions packages/client/src/fs/realizations/emptyFs.ts

This file was deleted.

5 changes: 2 additions & 3 deletions packages/client/test/fs/endpoints/emptyEndpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
* ------------------------------------------------------------------------------------------ */

import { describe, expect, test } from 'vitest';
import { EmptyFileSystemEndpoint } from '../../../src/fs/endpoints/emptyEndpoint.js';
import { EmptyFileSystem } from '../../../src/fs/realizations/emptyFs.js';
import { EmptyFileSystemEndpoint, EndpointType } from 'monaco-languageclient/fs';

describe('EmptyFileSystemEndpoint Tests', () => {

const endpoint = new EmptyFileSystemEndpoint(new EmptyFileSystem());
const endpoint = new EmptyFileSystemEndpoint(EndpointType.EMPTY);

test('readFile', async () => {
const result = await endpoint.readFile({ resourceUri: '/tmp/test.js' });
Expand Down
1 change: 1 addition & 0 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"devDependencies": {
"@types/express": "~4.17.21",
"@types/ws": "~8.5.12",
"@types/emscripten": "~1.39.13",
"langium-cli": "~3.2.0",
"ts-node": "~10.9.1",
"vscode-languageserver-types": "~3.17.5"
Expand Down
43 changes: 2 additions & 41 deletions packages/examples/src/clangd/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,8 @@ import '@codingame/monaco-vscode-cpp-default-extension';
import { createUserConfig } from './config.js';
import helloCppCode from './hello.cpp?raw';
import testerHCode from './tester.h?raw';
import { ComChannelEndpoint, RawPayload, WorkerMessage, ComRouter } from 'wtd-core';
import { ClangdWorkerHandler } from './workerHandler.js';

/**
* Answer the file create request
*/
class FileHandlerMain implements ComRouter {

private endpointFs?: ComChannelEndpoint;

setComChannelEndpoint(comChannelEndpoint: ComChannelEndpoint): void {
this.endpointFs = comChannelEndpoint;
}

async fs_driver_init(message: WorkerMessage) {
await this.endpointFs?.sentAnswer({
message: WorkerMessage.createFromExisting(message, {
overrideCmd: 'fs_driver_init_confirm',
}),
awaitAnswer: false
});

await this.endpointFs?.sentMessage({
message: WorkerMessage.fromPayload(new RawPayload({
hello: 'worker',
}), 'fs_follower_init'),
awaitAnswer: true,
expectedAnswer: 'fs_follower_init_confirm'
});
}
}
import { MainRemoteMessageChannelFs } from './mainRemoteMessageChannelFs.js';

export const runClangdWrapper = () => {
const wrapper = new MonacoEditorLanguageClientWrapper();
Expand All @@ -50,17 +21,7 @@ export const runClangdWrapper = () => {
const channelLs = new MessageChannel();
const channelFs = new MessageChannel();

const fileHandlerMain = new FileHandlerMain();
const endpointFs = new ComChannelEndpoint({
endpointId: 21,
endpointName: 'port_main_fs',
endpointConfig: {
$type: 'DirectImplConfig',
impl: channelFs.port1
},
verbose: true
});
endpointFs.connect(fileHandlerMain);
new MainRemoteMessageChannelFs(channelFs.port1);

try {
document.querySelector('#button-start')?.addEventListener('click', async () => {
Expand Down
69 changes: 69 additions & 0 deletions packages/examples/src/clangd/client/mainRemoteMessageChannelFs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2024 TypeFox and others.
* Licensed under the MIT License. See LICENSE in the package root for license information.
* ------------------------------------------------------------------------------------------ */

import { ComChannelEndpoint, RawPayload, WorkerMessage, ComRouter } from 'wtd-core';

/**
* Answer the file create request
*/
export class FileHandlerMain implements ComRouter {

private endpointFs?: ComChannelEndpoint;

setComChannelEndpoint(comChannelEndpoint: ComChannelEndpoint): void {
this.endpointFs = comChannelEndpoint;
}

async fs_driver_init(message: WorkerMessage) {
await this.endpointFs?.sentAnswer({
message: WorkerMessage.createFromExisting(message, {
overrideCmd: 'fs_driver_init_confirm',
}),
awaitAnswer: false
});

// send double confirmation
await this.endpointFs?.sentMessage({
message: WorkerMessage.fromPayload(new RawPayload({
hello: 'worker',
}), 'fs_follower_init'),
awaitAnswer: true,
expectedAnswer: 'fs_follower_init_confirm'
});
}

async syncFile(message: WorkerMessage) {
const rawPayload = message.payloads[0] as RawPayload;
console.log(rawPayload.message.raw.resourceUri);

await this.endpointFs?.sentAnswer({
message: WorkerMessage.createFromExisting(message, {
overrideCmd: 'syncFile_confirm',
overridePayloads: new RawPayload({
status: 'created',
message: `Created: ${rawPayload.message.raw.resourceUri}`
})
})
});
}
}

export class MainRemoteMessageChannelFs {

constructor(port: MessagePort) {

const fileHandlerMain = new FileHandlerMain();
const endpointFs = new ComChannelEndpoint({
endpointId: 21,
endpointName: 'port_main_fs',
endpointConfig: {
$type: 'DirectImplConfig',
impl: port
},
verbose: true
});
endpointFs.connect(fileHandlerMain);
}
}
Loading

0 comments on commit 0ad4aa3

Please sign in to comment.