Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to rebind FileSystemNode #6740

Closed
datou0412 opened this issue Dec 12, 2019 · 8 comments
Closed

How to rebind FileSystemNode #6740

datou0412 opened this issue Dec 12, 2019 · 8 comments
Labels
question user / developer questions

Comments

@datou0412
Copy link
Contributor

datou0412 commented Dec 12, 2019

Description

I want to customize resolveContent method, i tried this way but failed

node-filesystem.ts

import { injectable } from 'inversify';
import { FileStat } from '@theia/filesystem/lib/common';
import { FileSystemNode } from '@theia/filesystem/lib/node/node-filesystem';

@injectable()
export class SecurityFileSystemNode extends FileSystemNode {
  async resolveContent(uri: string, options?: { encoding?: string }): Promise<{ stat: FileStat, content: string }> {
    console.log('ahahahahahha')
    return super.resolveContent(uri, options);
  }
}

security-backend-module.ts

import { ContainerModule, interfaces } from 'inversify';
import { ConnectionHandler, JsonRpcConnectionHandler } from '@theia/core/lib/common';
import { FileSystem, DispatchingFileSystemClient, FileSystemClient } from '@theia/filesystem/lib/common';
import { SecurityFileSystemNode } from './node-filesystem';

export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
  bind(SecurityFileSystemNode).toSelf().inSingletonScope().onActivation((context, fs) => {
    const client = context.container.get(DispatchingFileSystemClient);
    fs.setClient(client);
    fs.setClient = () => {
      throw new Error('use DispatchingFileSystemClient');
    };
    return fs;
  });

  rebind(FileSystem).toService(SecurityFileSystemNode);
});

image

@kittaakos
Copy link
Contributor

You are on the right path, just use the following as your backend binding in the module:

import { ContainerModule, interfaces } from 'inversify';
import { ConnectionHandler, JsonRpcConnectionHandler } from '@theia/core/lib/common';
import { FileSystem, DispatchingFileSystemClient, FileSystemClient } from '@theia/filesystem/lib/common';
import { SecurityFileSystemNode } from './node-filesystem';

export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
  bind(SecurityFileSystemNode).toSelf().inSingletonScope();
  rebind(FileSystem).toService(SecurityFileSystemNode);
});

I have just tried it and it works. If you still have issues, let us know. Otherwise, please close the issue. Thank you!

@kittaakos kittaakos added the question user / developer questions label Dec 12, 2019
@datou0412
Copy link
Contributor Author

You are on the right path, just use the following as your backend binding in the module:

import { ContainerModule, interfaces } from 'inversify';
import { ConnectionHandler, JsonRpcConnectionHandler } from '@theia/core/lib/common';
import { FileSystem, DispatchingFileSystemClient, FileSystemClient } from '@theia/filesystem/lib/common';
import { SecurityFileSystemNode } from './node-filesystem';

export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
  bind(SecurityFileSystemNode).toSelf().inSingletonScope();
  rebind(FileSystem).toService(SecurityFileSystemNode);
});

I have just tried it and it works. If you still have issues, let us know. Otherwise, please close the issue. Thank you!

Wont work for me!
Can you open any file after this rebinding?

I come to this error when open any file
image

image

@kittaakos
Copy link
Contributor

Can you open any file after this rebinding?

Yes, I could. Please share your backend module with the binding customization and your custom FS class. If you could push your code to a public repo, that would help.

@datou0412
Copy link
Contributor Author

Can you open any file after this rebinding?

Yes, I could. Please share your backend module with the binding customization and your custom FS class. If you could push your code to a public repo, that would help.

I push my code to a public repo https://github.com/datou0412/theia-test

Follow the README to reproduce

@JPinkney
Copy link
Contributor

@kittaakos in your code snippet shouldn't FileSystem be FileSystemNode? like:

export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
  bind(SecurityFileSystemNode).toSelf().inSingletonScope();
  rebind(FileSystemNode).toService(SecurityFileSystemNode);
});

I wonder if that might be the issue

@datou0412
Copy link
Contributor Author

@kittaakos in your code snippet shouldn't FileSystem be FileSystemNode? like:

export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
  bind(SecurityFileSystemNode).toSelf().inSingletonScope();
  rebind(FileSystemNode).toService(SecurityFileSystemNode);
});

I wonder if that might be the issue

wont work --

@kittaakos
Copy link
Contributor

I push my code to a public repo https://github.com/datou0412/theia-test

I could reproduce. Thanks! Please compile against es5.

My changes:

diff --git a/browser-app/package.json b/browser-app/package.json
index cc221ef..c73f34b 100644
--- a/browser-app/package.json
+++ b/browser-app/package.json
@@ -2,7 +2,8 @@
   "name": "theia-test",
   "version": "0.0.1",
   "scripts": {
-    "prepare": "yarn theia build"
+    "prepare": "yarn theia build",
+    "start": "yarn theia start"
   },
   "dependencies": {
     "typescript": "latest",
diff --git a/packages/security/tsconfig.json b/packages/security/tsconfig.json
index 6cab26f..fe0e937 100644
--- a/packages/security/tsconfig.json
+++ b/packages/security/tsconfig.json
@@ -16,7 +16,7 @@
     "resolveJsonModule": true,
     "module": "commonjs",
     "moduleResolution": "node",
-    "target": "es6",
+    "target": "es5",
     "jsx": "react",
     "lib": ["es6", "dom"],
     "sourceMap": true

I also had to wipe the yarn.lock and run yarn from scratch as it was failing with an NPM error, probably some proxy thing.

error An unexpected error occurred: "https://registry.npm.alibaba-inc.com/@types/node/download/@types/node-12.12.17.tgz: ETIMEDOUT".

For more details on why we have to use es5, check these threads or ask on Spectrum:

Closing as answered.

@kittaakos
Copy link
Contributor

Follow-up: #6761

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question user / developer questions
Projects
None yet
Development

No branches or pull requests

3 participants