-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (127 loc) · 4.44 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
let fs = require('fs')
let express = require('express')
let path = require('path')
let morgan = require('morgan')
let nodeify = require('bluebird-nodeify')
let mime = require('mime-types')
let rimraf = require('rimraf')
let mkdirp = require('mkdirp')
// let bluebird = require('bluebird')
// bluebird.Promise.longStackTraces()
require('songbird')
let argv = require('yargs').argv
let app = express()
let net = require('net')
let JsonSocket = require('json-socket')
let events = require('events')
let eventEmitter = new events.EventEmitter()
const NODE_ENV = process.env.NODE_ENV
const PORT = process.env.PORT || 8000
let rootDir = argv.dir ? argv.dir : path.resolve(process.cwd())
if (NODE_ENV === 'development') {
app.use(morgan('dev'))
}
let tcpServer = net.createServer()
console.log('TCP Server LISTENING @ 127:0.0.1:9838')
tcpServer.listen(9838)
tcpServer.on('connection', (socket) => { //This is a standard net.Socket
console.log('TcpServer on connection')
socket = new JsonSocket(socket) //Now we've decorated the net.Socket to be a JsonSocket
eventEmitter.on('tcpMessageEvent', (tcpMessageData) => {
console.log('Send message to tcpClient', tcpMessageData)
socket.sendMessage(tcpMessageData)
})
socket.on('message', (message) => {
console.log('Received message from tcpClient', message)
})
})
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())
function sendToTcpClient(updateType, req) {
let tcpMessageData = {
"action": updateType,
"path": req.url,
"type": req.isDir ? "dir" : "file",
"contents": updateType === "delete" ? null : new Buffer(req.read()).toString('base64'),
"updated": Date.now()
}
eventEmitter.emit('tcpMessageEvent', tcpMessageData)
}
app.delete('*', setFileMeta, setDirDetails, (req, res, next) => {
async ()=> {
if (!req.stat) {
return res.status(400).send("Invalid Path")
}
if (req.stat.isDirectory()) {
await rimraf.promise(req.filePath)
} else {
await fs.promise.unlink(req.filePath)
}
res.end()
sendToTcpClient("delete", req)
}().catch(next)
})
// PUT for update
app.put('*', setFileMeta, setDirDetails, (req, res, next) => {
async ()=>{
if (!req.stat) return res.status(405).send("File does not exists")
if (req.isDir) return res.status(405).send("Path is a directory")
await fs.promise.truncate(req.filePath, 0)
req.pipe(fs.createWriteStream(req.filePath))
res.end()
sendToTcpClient("put", req)
}().catch(next)
})
// POST for create
app.post('*', setFileMeta, setDirDetails, (req, res, next) => {
async ()=>{
if (req.stat) return res.status(405).send("File exists")
await mkdirp.promise(req.dirPath)
if (!req.isDir) req.pipe(fs.createWriteStream(req.filePath))
res.end()
sendToTcpClient("post", req)
}().catch(next)
})
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(rootDir, req.url))
if (req.filePath.indexOf(rootDir) !== 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) {
res.status(404).send("Invalid Path")
return
}
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)
}