-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
88 lines (80 loc) · 2.78 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
let fs = require('fs')
let path = require('path')
let rimraf = require('rimraf')
let mkdirp = require('mkdirp')
let argv = require('yargs')
.default({dir: process.cwd()})
.argv
let net = require('net')
let JsonSocket = require('json-socket')
require('songbird')
let socket = new JsonSocket(new net.Socket()) //Decorate a standard net.Socket with JsonSocket
console.log("Socket client connecting to port 127:0.0.1:9838")
socket.connect(9838, '127.0.0.1')
function handleMessage(message) {
if (message.action === 'delete') {
handleDelete(message)
} else if (message.action === 'post') {
handlePost(message)
} else if (message.action === 'put') {
handlePut(message)
} else {
console.log('NOOP')
}
}
socket.on('connect', () => { //Don't send until we're connected
socket.on('message', (message) => {
console.log('Received message from tcpServer: ', message)
message.filePath = path.resolve(path.join(argv.dir, message.path))
fs.promise.stat(message.filePath)
.then(stat => message.stat = stat, () => message.stat = null)
.then(() => handleMessage(message))
})
})
function handleDelete(message) {
async() => {
console.log("DELETE", message)
if (!message.stat) {
console.log(`${message.action}: File doesn not exist ${message.filePath}`)
return
}
if (message.type === 'dir') {
await rimraf.promise(message.filePath)
} else {
await fs.promise.unlink(message.filePath)
}
}().catch(err => console.log(err))
}
// PUT for update
function handlePut(message) {
async() => {
console.log("PUT", message)
if (!message.stat) {
console.log(`${message.action}: File doesn not exist ${message.filePath}`)
return
}
if (message.type === 'dir') {
console.log(`${message.action}: ${message.filePath} is a directory`)
return
}
await fs.promise.truncate(message.filePath, 0)
let fw = fs.createWriteStream(message.filePath)
fw.end(new Buffer(message.contents, 'base64').toString())
}().catch(err => console.log(err))
}
// POST for create
function handlePost(message) {
async() => {
console.log("POST", message)
if (message.stat) {
console.log(`${message.action}: File exists ${message.filePath}`)
return
}
let dirPath = message.type === 'dir' ? message.filePath : path.dirname(message.filePath)
await mkdirp.promise(dirPath)
if (message.type === 'file') {
let fw = fs.createWriteStream(message.filePath)
fw.end(new Buffer(message.contents, 'base64').toString())
}
}().catch(err => console.log(err))
}