-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
173 lines (169 loc) · 5.31 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
163
164
165
166
167
168
169
170
171
172
173
var http = require("http")
var url = require("url")
var querystring = require("querystring")
var CSV = require("./CSV")
var Type = require("./Type")
var Cookie = require("./Cookie")
var HttpError = require("./HttpError")
var home = require("fs").readFileSync("index.html", "utf-8")
var data = require("./data.json")
http.createServer(function route(req, res) {
var u = url.parse(req.url)
var q = u.search ? querystring.parse(u.search.slice(1)) : {}
var args = u.pathname.match(/\/(api)\/([^\/]+)(?:\/([^\/]+))?/) // `/api/:collection/:id`
try {
if (args == null) {
if (u.pathname === "/") {
res.writeHead(200)
res.end(home)
return
}
else throw new HttpError(404, "Not found")
}
var resStatus = req.headers['rem-response-status']
if (resStatus) throw new HttpError(Number(resStatus), "Rem-Response-Status")
if (args[1] !== "api") throw new HttpError(404, "Not found")
var key = args[2]
var id = Number(args[3])
var db = getData(req.headers.cookie)
var items = db[key] || []
req.method = req.method.toUpperCase()
if (req.method === "GET") {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": req.headers.origin || '',
"Access-Control-Allow-Credentials": "true",
})
var offset = isNaN(parseInt(q.offset, 10)) ? 0 : parseInt(q.offset, 10)
var limit = isNaN(parseInt(q.limit, 10)) ? 10 : parseInt(q.limit, 10)
var payload = get(id, items)
var output = payload instanceof Array ? {
data: payload.slice(offset, offset + limit),
offset: offset,
limit: limit,
total: items.length,
} : payload
res.end(JSON.stringify(output, null, 2))
}
else if (req.method === "PUT" || req.method === "POST" || req.method === "DELETE") {
var body = ""
req.on("data",function(data) {
body += data.toString()
})
req.on("end",function commit() {
try {
var item = body !== "" ? JSON.parse(body) : null
if (req.method === "DELETE") remove(id, items)
else if (item != null) {
if (req.method === "PUT") put(id, items, item)
if (req.method === "POST") post(id, items, item)
}
else throw new HttpError(400, "Missing JSON input")
db[key] = items
var output = stageData(db)
if (output.length <= 4093) {
res.writeHead(200, {
"Content-Type": "application/json",
"Set-Cookie": output,
"Access-Control-Allow-Origin": req.headers.origin || '',
"Access-Control-Allow-Credentials": "true",
})
res.end(JSON.stringify(item, null, 2))
}
else {
var error = new HttpError(500, "Database size limit exceeded! Clearing data")
for (var k in db) db[k] = []
res.writeHead(500, {
"Content-Type": "application/json",
"Set-Cookie": output,
"Access-Control-Allow-Origin": req.headers.origin || '',
"Access-Control-Allow-Credentials": "true",
})
res.end(JSON.stringify({message: error.message, stack: e.stack}, null, 2))
}
}
catch (e) {
console.log(e)
res.writeHead((e && e.method) || 400, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": req.headers.origin || '',
"Access-Control-Allow-Credentials": "true",
})
res.end(JSON.stringify({message: e.message, stack: e.stack}, null, 2))
}
})
}
else if (req.method === "OPTIONS") {
res.writeHead(200, {
"Access-Control-Allow-Origin": req.headers.origin || '',
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS",
"Access-Control-Allow-Headers": "Content-Type,Rem-Response-Status",
"Access-Control-Max-Age": 600, // seconds
})
res.end("")
}
else throw new HttpError(405, "Method not allowed")
}
catch (e) {
res.writeHead(e.method || 400, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": req.headers.origin || '',
"Access-Control-Allow-Credentials": "true",
})
res.end(JSON.stringify({message: e.message, stack: e.stack}, null, 2))
}
}).listen(process.env.PORT || 8000)
function getData(cookieString) {
var map = Cookie.parse(cookieString)
if (Object.keys(map).length > 0) {
for (var k in map) {
var csv = CSV.parse(map[k])
map[k] = Type.decode(csv)
}
return map
}
return JSON.parse(JSON.stringify(data))
}
function stageData(db) {
var cookies = []
for (var k in db) {
var typed = Type.encode(db[k])
var csv = CSV.create(typed)
cookies.push(k + "=" + encodeURIComponent(csv) + "; Path=/;")
}
return cookies
}
function get(id, items) {
if (!id) return items
for (var i = 0; i < items.length; i++) {
if (items[i].id === id) return items[i]
}
throw new HttpError(404, "Item not found")
}
function put(id, items, item) {
if (!id) throw new HttpError(400, "ID must be provided")
item.id = id
var found = false
for (var i = 0; i < items.length; i++) {
if (items[i].id === id) {
items[i] = item
found = true
break
}
}
if (!found) items.push(item)
}
function post(id, items, item) {
if (id) throw new HttpError(400, "Cannot post with ID")
var last = items.slice().sort(function(a, b) {return b.id - a.id})[0]
var newId = last ? last.id : 0
item.id = Number(newId) + 1
items.push(item)
}
function remove(id, items) {
if (!id) throw new HttpError(400, "ID must be provided")
for (var i = 0; i < items.length; i++) {
if (items[i].id === id) items.splice(i, 1)
}
}