diff --git a/bin/remixd.ts b/bin/remixd.ts index c65e840a15..35a2fb6571 100644 --- a/bin/remixd.ts +++ b/bin/remixd.ts @@ -1,6 +1,7 @@ #!/usr/bin/env node import WebSocket from '../src/websocket' -import RemixdClient from '../src/services/remixdClient' +import * as servicesList from '../src/serviceList' + const program = require('commander') program @@ -23,10 +24,19 @@ console.log('\x1b[33m%s\x1b[0m', '[WARN] You may now only use IDE at ' + program if (program.sharedFolder) { console.log('\x1b[33m%s\x1b[0m', '[WARN] Any application that runs on your computer can potentially read from and write to all files in the directory.') console.log('\x1b[33m%s\x1b[0m', '[WARN] Symbolic links are not forwarded to Remix IDE\n') - const websocketHandler = new WebSocket(65520, { remixIdeUrl: program.remixIde }, new RemixdClient()) + try { + const websocketHandler = new WebSocket(65520, { remixIdeUrl: program.remixIde }, servicesList['sharedfolder']) + + websocketHandler.start(() => { + const remixdClient = new servicesList['sharedfolder']() - websocketHandler.start() - killCallBack.push(websocketHandler.close.bind(websocketHandler)) + remixdClient.setWebSocket(websocketHandler) + remixdClient.sharedFolder(program.sharedFolder, program.readOnly || false) + }) + killCallBack.push(websocketHandler.close.bind(websocketHandler)) + } catch(error) { + throw new Error(error) + } } // kill diff --git a/src/serviceList.ts b/src/serviceList.ts new file mode 100644 index 0000000000..59f47b9cb9 --- /dev/null +++ b/src/serviceList.ts @@ -0,0 +1,3 @@ +import { RemixdClient as sharedfolder } from './services/remixdClient' + +export { sharedfolder } \ No newline at end of file diff --git a/src/services/remixdClient.ts b/src/services/remixdClient.ts index af6f8dd253..5d7635f0f5 100644 --- a/src/services/remixdClient.ts +++ b/src/services/remixdClient.ts @@ -1,12 +1,12 @@ import WebSocket from '../websocket' import { PluginClient } from '@remixproject/plugin' import { SharedFolderArgs, TrackDownStreamUpdate } from '../../types' +import * as utils from '../utils' -const utils = require('../utils') const isbinaryfile = require('isbinaryfile') const fs = require('fs-extra') -export default class RemixdClient extends PluginClient { +export class RemixdClient extends PluginClient { methods: ['folderIsReadOnly', 'resolveDirectory'] trackDownStreamUpdate: TrackDownStreamUpdate websocket: WebSocket | null @@ -30,14 +30,18 @@ export default class RemixdClient extends PluginClient { } } - resolveDirectory (args: SharedFolderArgs, cb: Function) { + resolveDirectory (args: SharedFolderArgs) { + console.log('args: ', args) console.log('called resolveDirectory!') try { + console.log('called try resolveDirectory!') const path = utils.absolutePath(args.path, this.currentSharedFolder) - cb(null, utils.resolveDirectory(path, this.currentSharedFolder)) + const result = utils.resolveDirectory(path, this.currentSharedFolder) + console.log('result: ', result) } catch (e) { - cb(e.message) + console.log('called catch resolveDirectory!') + throw new Error(e) } } diff --git a/src/utils.ts b/src/utils.ts index b914c7ce36..f646860465 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -5,13 +5,6 @@ const path = require('path') const isbinaryfile = require('isbinaryfile') const pathModule = require('path') -module.exports = { - absolutePath: absolutePath, - relativePath: relativePath, - walkSync: walkSync, - resolveDirectory: resolveDirectory -} - /** * returns the absolute path of the given @arg path * @@ -79,3 +72,5 @@ function resolveDirectory (dir: string, sharedFolder: string): ResolveDirectory }) return ret } + +export { absolutePath, relativePath, walkSync, resolveDirectory } diff --git a/src/websocket.ts b/src/websocket.ts index 58fea0ac74..ffb4958f08 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -1,15 +1,15 @@ import * as WS from 'ws' import * as http from 'http' -import RemixdClient from './services/remixdClient' -import { WebsocketOpt } from '../types' +import { WebsocketOpt, SharedFolder } from '../types' const { buildWebsocketClient } = require('@remixproject/plugin-ws') export default class WebSocket { server: http.Server wsServer: WS.Server + connection: WS - constructor (public port: number, public opt: WebsocketOpt, public remixdClient: RemixdClient) {} + constructor (public port: number, public opt: WebsocketOpt, public remixdClient: SharedFolder) {} start (callback?: Function) { const obj = this @@ -36,8 +36,9 @@ export default class WebSocket { } }) this.wsServer.on('connection', function connection(ws, request) { - const client = buildWebsocketClient(ws, obj.remixdClient) + const client = buildWebsocketClient(ws, new obj.remixdClient()) + obj.connection = ws if(callback) callback(client) }) } diff --git a/types/index.ts b/types/index.ts index 14ecac1dc5..6408076b4f 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,10 +1,14 @@ +import * as ServiceList from '../src/serviceList' + +type ServiceListKeys = keyof typeof ServiceList; +export type SharedFolder = typeof ServiceList[ServiceListKeys] + export type WebsocketOpt = { remixIdeUrl: string } -export type SharedFolderArgs = { - path: string, - [key: string]: string | number | boolean +export type FolderArgs = { + path: string } export type KeyPairString = { @@ -17,14 +21,6 @@ export type ResolveDirectory = { } } -export type WebsocketProfile = { - name: string - methods?: string[] - permission?: boolean - hash?: string - redirect?: { - [key: string]: string - } -} +export type TrackDownStreamUpdate = KeyPairString -export type TrackDownStreamUpdate = KeyPairString \ No newline at end of file +export type SharedFolderArgs = FolderArgs & KeyPairString \ No newline at end of file