-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·336 lines (287 loc) · 7.23 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* configurations
*
* {
* "staticdata": {
* "path": "directory path to the source static files",
* "delimiter": optional,
* "qoute": optional,
* "index": optional {
* "staticFileName": ["indexName"...] > this must be a cloumn name in the file
* }
* }
* }
*
* */
var fs = require('fs');
var async = require('async');
var EventEmitter = require('events').EventEmitter;
var fileWatcher = new EventEmitter();
var gracenode = require('../../');
var log = gracenode.log.create('staticdata');
var config;
var delimiter = ',';
var quote = '"';
var staticData = {}; // static data source object
var sdMap = {}; // static data object map
module.exports.readConfig = function (configIn) {
if (!configIn || !configIn.path) {
return new Error('invalid configuration: \n' + JSON.stringify(configIn, null, 4));
}
config = configIn;
// optional
if (config.delimiter !== undefined) {
delimiter = config.delimiter;
}
if (config.quote !== undefined) {
quote = config.quote;
}
};
module.exports.setup = function (cb) {
log.verbose('setting up static data module...');
gracenode.lib.walkDir(gracenode.getRootPath() + config.path, function (error, list) {
if (error) {
return cb(error);
}
async.eachSeries(list, function (item, nextCallback) {
readFile(item.file, nextCallback);
}, cb);
});
};
/*
* dataName rule:
* configuration path: staticdata/
* example: staticdata/example/test.csv = example/test
*/
module.exports.create = function (dataName) {
// check for existing static data object first
if (sdMap[dataName]) {
return sdMap[dataName];
}
// create a new static data object
if (staticData[dataName]) {
var sd = new StaticData(dataName, staticData[dataName]);
sdMap[dataName] = sd;
return sd;
}
return null;
};
function readFile(path, cb) {
var lastDot = path.lastIndexOf('.');
var type = path.substring(lastDot + 1);
var name = path.substring(path.lastIndexOf(config.path) + config.path.length, lastDot);
fs.readFile(path, function (error, dataBuffer) {
if (error) {
return cb(error);
}
var data = dataBuffer.toString('utf-8');
var bytes = dataBuffer.length;
var kb = bytes / 1024;
var size = bytes + ' bytes';
if (kb >= 1) {
size = Math.round(kb) + ' kb';
}
log.verbose('static data loaded:', path + ' (' + size + ')');
switch (type) {
case 'csv':
data = toObject(data);
break;
case 'json':
try {
data = JSON.parse(data);
} catch (e) {
log.error('Could not turn', name, '(type:', type, ')', 'into object.');
return cb(e);
}
break;
default:
data = { data: data };
break;
}
// check for error
if (data instanceof Error) {
return cb(data);
}
// create index map(s) if asked
var indexMap = null;
var fileName = name + '.' + type;
if (config.index && config.index[fileName]) {
indexMap = mapIndex(data, config.index[fileName]);
log.verbose('indexed: ', config.index[fileName]);
}
// add it to cache
staticData[name] = { data: data, indexMap: indexMap, path: path };
log.verbose('mapped: ' + path + ' > ' + name);
// set up file watch listener
setupChangeListener(path);
cb();
});
}
function setupChangeListener(path) {
log.verbose('file change listener setup:', path);
fs.watch(path, function (event) {
if (event === 'change') {
readFile(path, function (error) {
if (error) {
return log.error(error);
}
log.info('file updated [' + event + ']:', path);
fileWatcher.emit(path);
});
}
});
}
function toObject(data) {
// assume first row as the list of columns
var res = [];
var pattern = new RegExp(quote, 'g');
//Replace all linebreaks with \r to eliminate cross OS eol issues.
data.replace(/(\r\n|\n)/gm, '\r');
var rows = data.replace(pattern, '').split('\r');
var columns = rows[0].split(delimiter);
var columnLen = columns.length;
for (var i = 1, len = rows.length; i < len; i++) {
if (!rows[i]) {
// ignore empty
continue;
}
var item = {};
var cols = rows[i].split(delimiter);
// validate data schema
if (cols.length !== columnLen) {
return new Error('data is corrupt: \ncolumns: \n' + JSON.stringify(columns, null, 4) + '\ndata: \n' + JSON.stringify(cols, null, 4));
}
for (var j = 0; j < columnLen; j++) {
var value = getValue(cols[j]);
item[columns[j]] = value;
}
res.push(item);
}
return res;
}
function getValue(value) {
if (!isNaN(value)) {
return Number(value);
}
switch (value.toLowerCase()) {
case 'true':
return true;
case 'false':
return false;
case 'null':
return null;
case 'undefined':
return undefined;
default:
return value;
}
}
function mapIndex(data, indexNames) {
var map = {};
for (var c = 0, length = data.length; c < length; c++) {
var item = data[c];
for (var i = 0, len = indexNames.length; i < len; i++) {
var indexName = indexNames[i];
if (item[indexName] !== undefined) {
if (!map[indexName]) {
map[indexName] = {};
}
var index = item[indexName];
var itemObj = {};
for (var key in item) {
itemObj[key] = item[key];
}
if (map[indexName][index]) {
// index is not unique
if (!Array.isArray(map[indexName][index])) {
map[indexName][index] = [map[indexName][index]];
}
map[indexName][index].push(itemObj);
} else {
// index is unique or this is the first item of the index
map[indexName][index] = itemObj;
}
}
}
}
return map;
}
function StaticData(name, src) {
this._name = name;
this._src = src.data;
this._indexMap = src.indexMap;
// file change listener
var that = this;
fileWatcher.on(src.path, function () {
that.update(staticData[name]);
});
}
StaticData.prototype.getOneByIndex = function (indexName, key) {
if (!this._indexMap) {
return null;
}
var data = this._indexMap[indexName] || null;
if (!data || data[key] === undefined) {
return null;
}
var res = data[key];
if (typeof res === 'object') {
return getObjValue(res);
}
return res;
};
StaticData.prototype.getManyByIndex = function (indexName, keyList) {
var res = {};
for (var i = 0, len = keyList.length; i < len; i++) {
var key = keyList[i];
res[key] = this.getOneByIndex(indexName, key);
}
return res;
};
StaticData.prototype.getOne = function (index) {
var data = this._src[index];
if (data === undefined) {
return null;
}
if (typeof data === 'object') {
return getObjValue(data);
}
return data;
};
StaticData.prototype.getMany = function (indexList) {
var res = {};
for (var i = 0, len = indexList.length; i < len; i++) {
var key = indexList[i];
res[key] = this.getOne(key);
}
return res;
};
StaticData.prototype.getAll = function () {
return getObjValue(this._src);
};
StaticData.prototype.getAllByIndexName = function (indexName) {
if (this._indexMap[indexName] === undefined) {
return null;
}
return getObjValue(this._indexMap[indexName]);
};
StaticData.prototype.update = function (src) {
log.verbose('static data [' + this._name + '] has been updated');
this._src = src.data;
this._indexMap = src.indexMap;
};
function getObjValue(data) {
var obj;
if (Array.isArray(data)) {
obj = [];
for (var i = 0, len = data.length; i < len; i++) {
obj.push(data[i]);
}
} else {
obj = {};
for (var key in data) {
obj[key] = data[key];
}
}
return obj;
}