/** * This function returns a deep clone of an object. It will copy its scalar * attributes, and recursively clone its complex objects attributes. * * Example: * ******** * > var o1 = { * > a: {, * > b: 1 * > } * > }; * > o2 = clone(o1); * > * > o1.a.b = 2; * > console.log(o2.a.b); * will log 1. * * @param {object} item The object to clone. * @return {object} The clone. */ function clone(item) { if (!item) return item; var result, i, k, l; if (types.check(item, 'array')) { result = []; for (i = 0, l = item.length; i < l; i++) result.push(clone(item[i])); } else if (types.check(item, 'date')) { result = new Date(item.getTime()); } else if (types.check(item, 'object')) { if (item.nodeType && typeof item.cloneNode === 'function') result = item; else if (!item.prototype) { result = {}; for (i in item) result[i] = clone(item[i]); } else result = item; } else { result = item; } return result; }
exports.addSnapshot = function(id, version, data, callback) { // Checking the snapshot is valid if (!types.check(data, 'snapshot')) return callback(new Error('wrong-data')); // Adding a unique identifier to the snapshot data.id = utils.uuid(); // Retrieving the space and add the snapshot async.waterfall([ function space(next) { entities.space.get(id, next); }, function updateGraph(space, next) { if (!space || space.graphs.length - 1 < version) return next(new Error('inexistant-version')); var snapshots = space.graphs[version].snapshots || []; snapshots.push(data); space.graphs[version].snapshots = snapshots; entities.space.set(id, space, function(err, result) { next(err, data.id); }); } ], callback); };
module.exports = function(min, max) { if (!types.check(min, 'number') || !types.check(max, '?number')) throw Error('sandcrawler.spider.throttle: wrong arguments.'); return function(spider) { spider.beforeScraping(function(req, next) { if (!this.index) return next(); var time = max ? randomNumber(min, max) : min + Math.random(); setTimeout(next, time); }); }; };
scraper.afterScraping(function(req, res, next) { var valid = true; if (typeof definition === 'function') valid = definition(res.data); else valid = types.check(res.data, definition); return next(valid ? null : new Error('invalid-data')); });
middlewares.push(function validate(req, res, next) { // Retrieving schema and data var data = {}, k; for (k in action.validate) data[k] = req.param(k); // Is data retrieved valid? if (!types.check(data, action.validate)) { logger.verbose(req.path + ': ' + 'Bad Request\n' + 'data: ' + JSON.stringify(data, null, 2) + '\n' + 'against: ' + JSON.stringify(action.validate, null, 2)); return res.status(400).send('Bad Request').end(); } else { next(); } });