-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
162 lines (136 loc) · 4.43 KB
/
index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
let fs = require('fs')
let path = require('path')
let express = require('express')
let morgan = require('morgan')
let nodeify = require('bluebird-nodeify')
let mime = require('mime-types')
let rimraf = require('rimraf')
let mkdirp = require('mkdirp')
let argv = require('yargs').argv
let nssocket = require('nssocket')
// augment songbird
require('songbird')
let dirName = argv.dir || process.cwd()
const NODE_ENV = process.env.NODE_ENV || 'development'
const PORT = process.env.PORT || 8000
const TCP_PORT = 6785
const ROOT_DIR = path.resolve(dirName)
const TCP_EVENT_MAP = {
'create': 'SERVER-CREATE',
'update': 'SERVER-UPDATE',
'remove' : 'SERVER-DELETE',
'ack': 'CLIENT-ACK'
}
let app = express()
let tcpSocket
let tcpServer = nssocket.createServer(function (socket) {
console.log(`Client connected on port ${TCP_PORT}`)
tcpSocket = socket
// log any acknowledgements from client to console
tcpSocket.data([TCP_EVENT_MAP.ack], function (data) {
console.log(data.message)
})
}).listen(TCP_PORT);
if(NODE_ENV === 'development') {
app.use(morgan('dev'))
}
app.listen(PORT, () => console.log(`LISTENING @ http://127.0.0.1:${PORT}`))
app.get('*', setFileMeta, sendHeaders, (req, res) => {
if(res.body) {
res.json(res.body)
return
}
fs.createReadStream(req.filePath).pipe(res)
})
app.head('*', setFileMeta, sendHeaders, (req, res) => res.end())
app.delete('*', setFileMeta, (req, res, next) => {
async () => {
if(!req.stat) return res.send(400, 'Invalid path')
let isDir = req.stat.isDirectory();
if(isDir){
await rimraf.promise(req.filePath)
} else await fs.promise.unlink(req.filePath)
// if a client exists sync the data to client
if(tcpSocket) {
let payload = getTCPPayload('delete', req.url, isDir)
tcpSocket.send([TCP_EVENT_MAP.remove], payload);
}
res.end()
}().catch(next)
})
app.put('*', setFileMeta, setDirDetails, (req, res, next) => {
async () => {
if(req.stat) return res.send(405, 'File exists')
await mkdirp.promise(req.dirPath)
if (!req.isDir) {
req.pipe(fs.createWriteStream(req.filePath))
}
// if a client exists sync the data to client
if(tcpSocket) {
// TODO : Find if there is a better way to read the req content
// instead of reading from file :(
let data = await fs.promise.readFile(req.filePath, {encoding: 'base64'})
let payload = getTCPPayload('create', req.url, req.isDir, data)
tcpSocket.send([TCP_EVENT_MAP.create], payload);
}
res.end()
}().catch(next)
})
app.post('*', setFileMeta, setDirDetails, (req, res, next) => {
async () => {
if(!req.stat) return res.send(405, 'File does not exists')
if(req.isDir) return res.send(405, 'Path is a directory')
await fs.promise.truncate(req.filePath, 0)
req.pipe(fs.createWriteStream(req.filePath))
// if a client exists sync the data to client
if(tcpSocket) {
// TODO : Find if there is a better way to read the req content
// instead of reading from file :(
let data = await fs.promise.readFile(req.filePath, {encoding: 'base64'})
let payload = getTCPPayload('update', req.url, req.isDir, data)
tcpSocket.send([TCP_EVENT_MAP.update], payload);
}
res.end()
}().catch(next)
})
function getTCPPayload(action, path, isDir, contents) {
return {
"action": action,
"path": path,
"type": isDir ? 'dir': 'file',
"contents": contents || null,
"updated": new Date().getTime()
};
}
function setDirDetails(req, res, next) {
let filePath = req.filePath;
let endsWithSlash = filePath.charAt(filePath.length-1) === path.sep
let hasExt = path.extname(filePath) !== ''
req.isDir = endsWithSlash || !hasExt
req.dirPath = req.isDir ? filePath : path.dirname(filePath)
next()
}
function setFileMeta (req, res, next) {
req.filePath = path.resolve(path.join(ROOT_DIR, req.url))
if(req.filePath.indexOf(ROOT_DIR) !== 0) {
res.send(400, 'Invalid path')
return
}
fs.promise
.stat(req.filePath).then(stat => req.stat = stat, () => req.stat = null)
.nodeify(next)
}
function sendHeaders (req, res, next) {
nodeify(async () => {
if(req.stat.isDirectory()) {
let files = await fs.promise.readdir(req.filePath)
res.body = JSON.stringify(files)
res.setHeader('Content-Length', res.body.length)
res.setHeader('Content-type', 'application/json')
return
}
res.setHeader('Content-Length', req.stat.size)
let contentType = mime.contentType(path.extname(req.filePath))
res.setHeader('Content-type', contentType)
}(), next)
}