Modify client to support config file
This commit is contained in:
parent
120dbe0b0a
commit
722db76086
@ -1,6 +1,7 @@
|
|||||||
const WebSocketStream = require('websocket-stream')
|
const WebSocketStream = require('websocket-stream')
|
||||||
const CombinedStream = require('combined-stream')
|
const CombinedStream = require('combined-stream')
|
||||||
const { Buffer } = require('buffer')
|
const { Buffer } = require('buffer')
|
||||||
|
const config = require('./config')
|
||||||
|
|
||||||
class Streamer {
|
class Streamer {
|
||||||
constructor(socket_uuid, node_uuid, length = 4096, position = 0, descriptor = undefined) {
|
constructor(socket_uuid, node_uuid, length = 4096, position = 0, descriptor = undefined) {
|
||||||
@ -12,7 +13,7 @@ class Streamer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
write(buffer) {
|
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,
|
perMessageDeflate: false,
|
||||||
binary: true,
|
binary: true,
|
||||||
})
|
})
|
||||||
@ -29,7 +30,7 @@ class Streamer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stream() {
|
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,
|
perMessageDeflate: false,
|
||||||
binary: true,
|
binary: true,
|
||||||
})
|
})
|
||||||
|
30
config.js
Normal file
30
config.js
Normal file
@ -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()
|
@ -1,10 +1,11 @@
|
|||||||
const Message = require('../shared/Message')
|
const Message = require('../shared/Message')
|
||||||
const WebSocket = require('ws')
|
const WebSocket = require('ws')
|
||||||
const Errors = require('../shared/Errors')
|
const Errors = require('../shared/Errors')
|
||||||
|
const config = require('./config')
|
||||||
|
|
||||||
class Connector {
|
class Connector {
|
||||||
constructor() {
|
initialize() {
|
||||||
this.client = new WebSocket('ws://localhost:8100')
|
this.client = new WebSocket(`ws://${config.config.data_server}`)
|
||||||
this.messages = []
|
this.messages = []
|
||||||
this.token;
|
this.token;
|
||||||
|
|
||||||
|
15
index.js
15
index.js
@ -1,9 +1,7 @@
|
|||||||
const uuid = require('uuid').v4
|
|
||||||
const Fuse = require('fuse-native')
|
const Fuse = require('fuse-native')
|
||||||
const fs = require('fs').promises
|
const fs = require('fs')
|
||||||
const conn = require('./connector')
|
const conn = require('./connector')
|
||||||
|
const config = require('./config')
|
||||||
const token = '0257387b959a4cbfa7b3a36fea7d8cfb97e435e2d6454a96992470e5ba107d29b35bed8c21dc4272b4d8d6eebc9a0f5f17b707c37e57442db9ab56775f449128'
|
|
||||||
|
|
||||||
const ops = {
|
const ops = {
|
||||||
readdir: require('./ops/readdir'),
|
readdir: require('./ops/readdir'),
|
||||||
@ -29,12 +27,13 @@ const ops = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
;(async () => {
|
;(async () => {
|
||||||
|
await config.initialize()
|
||||||
|
|
||||||
|
conn.initialize()
|
||||||
await conn.open()
|
await conn.open()
|
||||||
await conn.authenticate(token)
|
await conn.authenticate(config.config.token)
|
||||||
const mnt = '/tmp/piedev-' + uuid()
|
|
||||||
|
|
||||||
await fs.mkdir(mnt)
|
|
||||||
|
|
||||||
|
const mnt = config.config.mountpoint
|
||||||
const fuse = new Fuse(mnt, ops, {debug: true, displayFolder: true})
|
const fuse = new Fuse(mnt, ops, {debug: true, displayFolder: true})
|
||||||
|
|
||||||
await new Promise((res, rej) => {
|
await new Promise((res, rej) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user