From 722db760860860b40127dc949382cc746e3400e3 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sun, 29 Nov 2020 12:17:23 -0600 Subject: [PATCH] Modify client to support config file --- Streamer.js | 5 +++-- config.js | 30 ++++++++++++++++++++++++++++++ connector.js | 5 +++-- index.js | 15 +++++++-------- 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 config.js diff --git a/Streamer.js b/Streamer.js index 75ebf35..f47adab 100644 --- a/Streamer.js +++ b/Streamer.js @@ -1,6 +1,7 @@ const WebSocketStream = require('websocket-stream') const CombinedStream = require('combined-stream') const { Buffer } = require('buffer') +const config = require('./config') class Streamer { constructor(socket_uuid, node_uuid, length = 4096, position = 0, descriptor = undefined) { @@ -12,7 +13,7 @@ class Streamer { } write(buffer) { - const write_stream = WebSocketStream(`ws://localhost:5746/?socket_uuid=${this.socket_uuid}&node_uuid=${this.node_uuid}&length=${this.length}&position=${this.position}&writing_file=true&descriptor=${this.descriptor}`, { + const write_stream = WebSocketStream(`ws://${config.config.data_server}/?socket_uuid=${this.socket_uuid}&node_uuid=${this.node_uuid}&length=${this.length}&position=${this.position}&writing_file=true&descriptor=${this.descriptor}`, { perMessageDeflate: false, binary: true, }) @@ -29,7 +30,7 @@ class Streamer { } stream() { - this.ws = WebSocketStream(`ws://localhost:5746/?socket_uuid=${this.socket_uuid}&node_uuid=${this.node_uuid}&length=${this.length}&position=${this.position}`, { + this.ws = WebSocketStream(`ws://${config.config.data_server}/?socket_uuid=${this.socket_uuid}&node_uuid=${this.node_uuid}&length=${this.length}&position=${this.position}`, { perMessageDeflate: false, binary: true, }) diff --git a/config.js b/config.js new file mode 100644 index 0000000..5816ed4 --- /dev/null +++ b/config.js @@ -0,0 +1,30 @@ +const fs = require('fs') +const home_dir = require('os').homedir() +const path = require('path') + +const exists = async file => { + return new Promise(res => { + fs.promises.stat(file).then(() => res(true)).catch(() => res(false)) + }) +} + +class ConfigManager { + config_path = path.resolve(home_dir, '.config', 'pied-vcs.json') + + async initialize() { + if ( !(await exists(this.config_path)) ) { + console.error('Pied VCS configuration file does not exist!') + console.log(`Please create the file "${this.config_path}" with your authentication token and server information.`) + throw new Error('Missing pied-vcs.json file.') + } + + this.config = require(this.config_path) + + const required_fields = ['token', 'mountpoint', 'data_server', 'stream_server'] + if ( !this.config || !required_fields.every(field => this.config[field]) ) { + throw new Error('Invalid config.') + } + } +} + +module.exports = exports = new ConfigManager() diff --git a/connector.js b/connector.js index 2908f09..2bf3b7e 100644 --- a/connector.js +++ b/connector.js @@ -1,10 +1,11 @@ const Message = require('../shared/Message') const WebSocket = require('ws') const Errors = require('../shared/Errors') +const config = require('./config') class Connector { - constructor() { - this.client = new WebSocket('ws://localhost:8100') + initialize() { + this.client = new WebSocket(`ws://${config.config.data_server}`) this.messages = [] this.token; diff --git a/index.js b/index.js index 2662046..28ea09f 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,7 @@ -const uuid = require('uuid').v4 const Fuse = require('fuse-native') -const fs = require('fs').promises +const fs = require('fs') const conn = require('./connector') - -const token = '0257387b959a4cbfa7b3a36fea7d8cfb97e435e2d6454a96992470e5ba107d29b35bed8c21dc4272b4d8d6eebc9a0f5f17b707c37e57442db9ab56775f449128' +const config = require('./config') const ops = { readdir: require('./ops/readdir'), @@ -29,12 +27,13 @@ const ops = { } ;(async () => { - await conn.open() - await conn.authenticate(token) - const mnt = '/tmp/piedev-' + uuid() + await config.initialize() - await fs.mkdir(mnt) + conn.initialize() + await conn.open() + await conn.authenticate(config.config.token) + const mnt = config.config.mountpoint const fuse = new Fuse(mnt, ops, {debug: true, displayFolder: true}) await new Promise((res, rej) => {