-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver_library.js
More file actions
251 lines (217 loc) · 8.68 KB
/
Copy pathserver_library.js
File metadata and controls
251 lines (217 loc) · 8.68 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
(function () {
'use strict';
/**************************************************************************/
//
// This is for API vs 1.0.0
//
/**************************************************************************/
var ID = "_id", lib = {}, MongoClient = require('mongodb').MongoClient, assert = require('assert');
var checkCollection, acceptedRegex = "", sanatize, j, grabDbName, grabDocument, respond, list,
accepted$ = [
"all", "and", "bitsAllClear", "bitsAllSet", "bitsAnyClear",
"bitsAnySet", "collStats", "comment", "elemMatch",
"eq", "exists", "explain", "geoIntersects", "geoWithin", "gt",
"gte", "hint", "in", "limit", "lte", "match", "max", "maxScan",
"maxTimeMS", "meta", "min", "mod", "natural", "ne", "near",
"nearShpere", "nin", "nor", "not", "or", "orderby", "query",
"regex", "returnKey", "showDiskLoc", "size", "skip", "slice",
"snapshot", "text", "type", "where"
];
for (j = 0; j < accepted$.length; j += 1) {
acceptedRegex += "^\\$" + accepted$[j] + "$|";
}
acceptedRegex = acceptedRegex.replace(/\|$/, "");
acceptedRegex = new RegExp(acceptedRegex, 'i');
//define the query sanatize function
//sanatize the query
sanatize = function (query, ver) {
var keys, i;
if (typeof query === "object") {
keys = Object.keys(query);
for (i = 0; i < keys.length; i += 1) {
if (keys[i].match(/^\s*\$/)) {
//straight up kill it if it is not an accepted type
if (!keys[i].match(acceptedRegex)) {
delete query[keys[i]];
}
} else if (typeof query[keys[i]] === "object") {
query[keys[i]] = sanatize(query[keys[i]]);
}
}
} else {
//If it is not an object then just query it all
// should probably generate an error message
query = {};
}
return query;
};
checkCollection = function (collection) {
if (
collection.toLowerCase() === 'name' ||
collection.toLowerCase() === 'lvl_1.0.0' ||
collection.toLowerCase() === 'lvl_1.0.1' ||
collection.toLowerCase() === 'lvl_1.1.2' ||
collection.toLowerCase() === 'lvl_2.0.1' ||
collection.toLowerCase() === 'lvl_2.1.2'
) {
return true;
}
throw new Error("That collection is not allowed or does not exist");
};
grabDbName = function (request, response, ver) {
var myDbName = request.params.db_name, query, fields, sort,
collectionName = request.params.collection_name, url;
checkCollection(collectionName);
//Deal with the objects
myDbName = 'kinome';
url = 'mongodb://localhost:27017/' + myDbName;
console.log('db name:', myDbName);
request.query.find = request.query.find || "{}";
request.query.fields = request.query.fields || "-1";
request.query.sort = request.query.sort || "[]";
request.query.find = decodeURIComponent(request.query.find);
request.query.fields = decodeURIComponent(request.query.fields);
request.query.sort = decodeURIComponent(request.query.sort);
//Objectify them
query = JSON.parse(request.query.find);
query = sanatize(query);
fields = JSON.parse(request.query.fields);
sort = JSON.parse(request.query.sort);
//if there is a sort option, sanatize it
if (sort.length > 0) {
sanatize(sort);
}
//Start up connection and run the query
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
var spec;
var collection = db.collection(collectionName);
console.log("Connected successfully to server");
//options
var limit = request.query.limit * 1 || 9000000; //9 mil more than enough
var skip = request.query.skip * 1 || 0;
var maxTimeMS = request.query.maxTimeMS * 1 || 1000 * 60 * 60; //1 hr
//special stuff
console.log(query, fields, sort);
if (fields === -1) {
spec = collection.find(query, {
limit: limit,
skip: skip,
maxTimeMS: maxTimeMS
});
} else {
spec = collection.find(query, sanatize(fields), {
limit: limit,
skip: skip,
maxTimeMS: maxTimeMS
});
}
//run the sort operation and then return the final result
spec.sort(sort).toArray(function (err, docs) {
respond[ver](response, docs, err);
});
});
};
grabDocument = function (request, response, ver) {
var myDbName = request.params.db_name, fields,
collectionName = request.params.collection_name,
query = {id: decodeURIComponent(request.params.doc_id)}, url;
//Deal with the objects
myDbName = 'kinome';
url = 'mongodb://localhost:27017/' + myDbName;
checkCollection(collectionName);
request.query.fields = request.query.fields || "-1";
request.query.fields = decodeURIComponent(request.query.fields);
//Objectify them
query = sanatize(query);
fields = JSON.parse(request.query.fields);
console.log(fields);
//Start up connection and run the query
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
var spec;
var collection = db.collection(collectionName);
console.log("Connected successfully to server");
//options
var maxTimeMS = request.query.maxTimeMS * 1 || 1000 * 60 * 60; //1 hr
//special stuff
console.log(query, myDbName, collectionName);
if (fields === -1) {
spec = collection.find({"_id": query.id}, {
maxTimeMS: maxTimeMS
});
} else {
spec = collection.find({"_id": query.id}, sanatize(fields), {
maxTimeMS: maxTimeMS
});
}
//run the sort operation and then return the final result
spec.toArray(function (err, docs) {
respond[ver](response, docs[0], err);
});
});
};
list = function (request, response, ver) {
var myDbName, url, query, fields,
collectionName = request.params.collection_name;
//Deal with the objects
myDbName = 'kinome';
url = 'mongodb://localhost:27017/' + myDbName;
//Objectify them
query = {};
fields = {"_id": 1};
checkCollection(collectionName);
//Start up connection and run the query
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
var spec;
var collection = db.collection(collectionName);
//options
var limit = 9000000; //9 mil more than enough
var skip = 0;
var maxTimeMS = 1000 * 60 * 60; //1 hr
spec = collection.find(query, fields, {
limit: limit,
skip: skip,
maxTimeMS: maxTimeMS
});
//run the sort operation and then return the final result
spec.toArray(function (err, docs) {
if (Array.isArray(docs)) {
docs = docs.map(function (doc) {
return "http://db.kinomecore.com/2.0.0/" + collectionName + "/" + doc[ID];
});
}
respond[ver](response, docs, err);
});
});
};
//Respond functions (internal only)
respond = {};
respond["1.0.0"] = function (response, data, error) {
if (error) {
assert.equal(error, null);
} else {
response.send(data);
}
};
respond["2.0.0"] = function (response, data, error) {
if (error) {
response.send({error: true, message: error});
} else {
if (!Array.isArray(data)) {
data = [data];
}
response.send({error: false, message: null, data: data});
}
};
lib = {
grabDbName: function (request, response) {
return grabDbName(request, response, "1.0.0");
},
grabDocument: function (request, response) {
return grabDocument(request, response, "1.0.0");
}
};
module.exports = lib;
}());