forked from apla/dataflo.ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
531 lines (413 loc) · 12.8 KB
/
common.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
var util = require ('util');
var root;
if (!util.inherits) {
util.inherits = function (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create (superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}});
};
}
if (!util.extend) {
util.extend = function extend () {
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && !typeof target === 'function')
target = {};
var isPlainObject = function(obj) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if (!obj || toString.call(obj) !== "[object Object]" || obj.nodeType || obj.setInterval)
return false;
var has_own_constructor = hasOwnProperty.call(obj, "constructor");
var has_is_property_of_method = hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf");
// Not own constructor property must be Object
if (obj.constructor && !has_own_constructor && !has_is_property_of_method)
return false;
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var last_key;
for (key in obj)
last_key = key;
return typeof last_key === "undefined" || hasOwnProperty.call(obj, last_key);
};
for (; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = arguments[i]) !== null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy)
continue;
// Recurse if we're merging object literal values or arrays
if (deep && copy && (isPlainObject(copy) || Array.isArray(copy))) {
var clone = src && (isPlainObject(src) || Array.isArray(src)) ? src : Array.isArray(copy) ? [] : {};
// Never move original objects, clone them
target[name] = extend(deep, clone, copy);
// Don't bring in undefined values
} else if (typeof copy !== "undefined")
target[name] = copy;
}
}
}
// Return the modified object
return target;
}
}
if (!util.clone) {
util.clone = function(object) {
var result;
if (object.constructor === Array) {
result = object.map(function(item) {
return util.clone(item);
});
} else if (object.constructor === Object) {
result = {};
util.extend(result, object);
} else {
result = object;
}
return result;
}
}
try {
if (process.pid) {
global.$isClientSide = false;
global.$isServerSide = true;
global.$mainModule = process.mainModule.exports;
global.$scope = 'process.mainModule.exports';
global.$stash = {};
global.$isPhoneGap = false;
} else {
throw 'WTF?';
}
} catch (e) {
window.$isClientSide = true;
window.$isServerSide = false;
window.$mainModule = window;
window.$scope = 'window';
window.$stash = {};
try {
if (PhoneGap || Cordova || cordova) window.$isPhoneGap = true;
} catch (e) {
window.$isPhoneGap = false;
}
}
Number.prototype.hours = Number.prototype.hour
= function () {return this * 60 * 60 * 1e3}
Number.prototype.minutes = Number.prototype.minute
= function () {return this * 60 * 1e3}
Number.prototype.seconds = Number.prototype.second
= function () {return this * 1e3}
Number.prototype.times = function (cb) {
var a = [];
for (var i = 0; i < this; i++)
a[i] = cb (i);
return a;
}
// overwrite subject with values from object (merge object with subject)
var mergeObjects = module.exports.mergeObjects = function (object, subjectParent, subjectKey) {
// subject parent here for js's lack of pass by reference
if (subjectParent[subjectKey] === void 0)
subjectParent[subjectKey] = {};
var subject = subjectParent[subjectKey];
for (var objectField in object) {
subject[objectField] = object[objectField];
}
}
var pathToVal = module.exports.pathToVal = function (dict, path, value, method) {
// console.log ('pathToVal ('+ dict + ', '+ path + ', '+value+')');
var chunks = (path.constructor === Array ? path : path.split ('.'));
if (chunks.length == 1) {
var oldValue = dict[chunks[0]];
if (value !== void(0)){
if (method !== void 0) {
method (value, dict, chunks[0]);
} else {
dict[chunks[0]] = value;
}
}
// console.log (''+oldValue);
return oldValue;
}
return pathToVal (dict[chunks.shift()], chunks, value, method)
}
// - - -
var configCache = {};
function loadIncludes(config, cb, level) {
var DEFAULT_ROOT = 'etc/',
DELIMITER = ' > ',
tagRe = /<([^>]+)>/,
cnt = 0,
len = 0;
var levelHash = {};
level.split(DELIMITER).forEach(function(key) {
levelHash[key] = true;
});
function onLoad() {
cnt += 1;
if (cnt >= len) {
cb(null, config);
}
}
function onError(err) {
cb(err, config);
}
function iterateTree(tree, cb) {
if (null == tree) { return; }
var step = function (node, key, tree) {
cb(tree, key);
iterateTree(node, cb);
};
if (Array === tree.constructor) {
tree.forEach(step);
} else if (Object === tree.constructor) {
Object.keys(tree).forEach(function (key) {
step(tree[key], key, tree)
});
}
}
function iterateNode(node, key) {
var value = node[key];
if ('string' === typeof value) {
var match = value.match(tagRe);
if (match) {
len += 1;
var path = match[1];
if (0 !== path.indexOf('/')) {
path = DEFAULT_ROOT + path;
}
if (path in levelHash) {
console.error('\n\n\nError: on level "' + level + '" key "' + key + '" linked to "' + value + '" in node:\n', node);
throw new Error('circular linking');
}
delete node[key];
if (configCache[path]) {
node[key] = util.clone(configCache[path]);
onLoad();
} else {
root.fileIO(path).readFile(function (err, data) {
if (err) {
onError(err);
} else {
loadIncludes(JSON.parse(data), function(tree, includeConfig) {
configCache[path] = includeConfig;
node[key] = util.clone(configCache[path]);
onLoad();
}, level + DELIMITER + path);
}
});
}
}
}
}
iterateTree(config, iterateNode);
// console.log('including:', level, config);
!len && cb(null, config);
}
var findInterpolation = module.exports.findInterpolation = function (params, prefix) {
// parse task params
// TODO: modify this function because recursive changes of parameters works dirty (indexOf for value)
if (prefix == void 0) prefix = '';
if (prefix) prefix += '.';
var found = {};
if (params.constructor == Array) { // params is array
params.forEach(function (val, index, arr) {
if (val.indexOf && val.interpolate) { // string
var tmp = val.interpolate ({}, false, true);
if (tmp !== void 0) {
found[prefix + index] = tmp;
}
} else if (!val.toFixed) { // array and object (check for function and boolean)
var result = findInterpolation (val, prefix+index);
for (var attrname in result) {
found[attrname] = result[attrname];
}
}
});
} else { // params is hash
for (var key in params) {
var val = params[key];
if (val.indexOf && val.interpolate) { // string
var tmp = val.interpolate ({}, false, true);
if (tmp !== void 0) {
found[prefix + key] = tmp;
}
} else if (!val.toFixed) { // array and object (check for function and boolean)
var result = findInterpolation (val, prefix+key);
for (var attrname in result) {
found[attrname] = result[attrname];
}
}
}
}
return found;
}
var define;
if (typeof define === "undefined")
define = function () {}
define (function (require, exports, module) {
return {
pathToVal: pathToVal,
findInterpolation: findInterpolation,
loadIncludes: loadIncludes,
mergeObjects: mergeObjects
};
});
String.prototype.interpolate = function (dict, marks, checkOnly) {
if (!marks)
marks = {
start: '{$', end: '}', path: '.'
};
var result;
var interpolatePaths = [];
var template = this;
var pos = this.indexOf (marks.start);
while (pos > -1) {
var end = (result || this).indexOf (marks.end, pos);
var str = (result || this).substr (pos + 2, end - pos - 2);
if (checkOnly && str) {
interpolatePaths.push (str);
pos = this.indexOf (marks.start, end);
continue;
}
// console.log ("found replacement: key => ???, requires => $"+this+"\n");
var fix;
if (str.indexOf (marks.path) > -1) { // treat as path
fix = pathToVal (dict, str);
} else { // scalar
fix = dict[str];
}
if (fix === void(0))
throw (result || this);
if (fix.indexOf && fix.indexOf (marks.start) > -1)
throw 'interpolation mark "' + marks.start + '" within interpolation string (' + fix + ') is denied';
if (pos == 0 && end == ((result || this).length - 1)) {
result = fix;
} else {
result = (result || this).substr (0, pos) + fix + (result || this).substr (end + 1);
// console.log ('!!!', (result || this).toString(), fix.toString(), pos, end, end - pos + 1);
}
if ((((result === false || result === 0 || result === "") ? true : result) || this).indexOf)
pos = (((result === false || result === 0 || result === "") ? true : result) || this).indexOf (marks.start);
else
break;
}
if (checkOnly)
return interpolatePaths;
return result;
};
if ($isServerSide) {
var path = require ('path');
var io = require ('./io/easy');
var project = function () {
// TODO: root directory object
var script = process.argv[1];
var rootPath = script.match (/(.*)\/(bin|t|lib)\//);
if (!rootPath) {//win
rootPath = script.match (/(.*)\\(bin|t|lib)\\/)
}
if (!rootPath)
return;
root = new io (rootPath[1]);
this.root = root;
var self = this;
root.fileIO ('etc/project').readFile (function (err, data) {
if (err) {
console.error ("can't access etc/project file. create one and define project id");
// process.kill ();
return;
}
var configData = (""+data).match (/(\w+)(\W[^]*)/);
configData.shift ();
var parser = configData.shift ();
// console.log ('parsing etc/project using "' + parser + '" parser');
if (parser == 'json') {
try {
var config = JSON.parse (configData[0]);
} catch (e) {
console.log ('WARNING: project config cannot parsed');
throw e;
}
// TODO: read config fixup
} else {
console.error ('parser ' + parser + ' unknown');
// process.kill ();
return;
}
self.id = config.id;
loadIncludes(config, function (err, config) {
if (err) {
console.error(err);
console.warn("Couldn't load inlcudes.");
self.emit ('ready');
return;
}
self.config = config;
root.fileIO ('var/instance').readFile (function (err, data) {
if (err) {
console.error ("PROBABLY HARMFUL: can't access var/instance: "+err);
self.emit ('ready');
return;
}
var instance = (""+data).split (/\n/)[0];
self.instance = instance;
console.log ('instance is: ', instance);
root.fileIO ('etc/' + instance + '/fixup').readFile (function (err, data) {
if (err) {
console.error ("PROBABLY HARMFUL: can't access "+'etc/' + instance + '/fixup'+" file. "
+ "create one and define local configuration fixup. "
);
self.emit ('ready');
// process.kill ();
return;
}
var fixupData = (""+data).match (/(\w+)(\W[^]*)/);
fixupData.shift ();
var fixupParser = fixupData.shift ();
var fixupData = (""+data).match (/(\w+)(\W[^]*)/);
fixupData.shift ();
var fixupParser = fixupData.shift ();
// console.log ('parsing etc/' + instance + '/fixup using "' + fixupParser + '" parser');
// TODO: error handling
if (fixupParser == 'json') {
var config = JSON.parse (fixupData[0]);
util.extend (true, self.config, config);
} else {
console.log ('parser ' + fixupParser + ' unknown');
// process.kill ();
return;
}
console.log ('project ready');
self.emit ('ready');
});
});
}, 'root');
});
// TODO: walk filetree to find directory root if script located in
// subdir of bin or t
// console.log (root);
}
var EventEmitter = require ('events').EventEmitter;
util.inherits (project, EventEmitter);
util.extend (project.prototype, {
connectors: {},
connections: {}
});
global.project = new project ();
}