import uuidv1 from 'uuid/v1' import { socket } from './socket.connection' export function run_system_command(opt) { return syscall_async('run_system_command', opt) } export function disk_usage(opt) { return syscall_async('run_system_command', { cmd: 'du', ...opt }) } export function list_directory(opt) { return syscall_async('list_directory', opt).then(res => res.files) } export function list_sequences(opt) { return syscall_async('list_sequences', opt).then(res => res.sequences) } export function run_script(opt) { return syscall_async('run_script', opt) } export function upload_file(opt) { return syscall_async('upload_file', opt) } export const syscall_async = (tag, payload, ttl=10000) => { ttl = payload.ttl || ttl return new Promise( (resolve, reject) => { const uuid = uuidv1() const timeout = setTimeout(() => { socket.off('system_res', cb) reject('timeout') }, ttl) const cb = (data) => { if (!data.uuid) return if (data.uuid === uuid) { clearTimeout(timeout) socket.off('system_res', cb) resolve(data) } } socket.emit('system', { cmd: tag, payload, uuid }) socket.on('system_res', cb) }) }