diff --git a/lib/importer.js b/lib/importer.js index f71027b..f321938 100644 --- a/lib/importer.js +++ b/lib/importer.js @@ -14,9 +14,7 @@ module.exports = function (config, mongoInstance, helpers, markers) { function handler(text, result) { var new_text = text.replace(result.regExp, result.replacement); - if (config.debug) { - console.log('handleImportMarker.handler new_text', new_text); - } + config.debug && config.debug('handleImportMarker.handler new_text', new_text); return new_text; } @@ -51,17 +49,13 @@ module.exports = function (config, mongoInstance, helpers, markers) { function onSuccess(leftover) { handleImportMarkers(doc, options, function handleLeftover(err, remainder) { if (err) { - if (config.errors) { - console.error('ERR importer.importer handleImportMarkers', err); - } + config.error && config.error('ERR importer.importer handleImportMarkers', err); return cb(err); } if (leftover) { return mongoInstance.ensureContent(leftover.replacement, function parent(err, parent_result) { if (err) { - if (config.errors) { - console.error('ERR importer.importer ensureContent', err); - } + config.error && config.error('ERR importer.importer ensureContent', err); return cb(err); } leftover.replacement.query = { _id: parent_result._id }; @@ -69,25 +63,19 @@ module.exports = function (config, mongoInstance, helpers, markers) { remainder = remainder.replace(leftover.regExp, ""); return mongoInstance.setMongoAttribute(remainder, leftover.replacement, function savedAttribute(err) { if (err) { - if (config.errors) { - console.error('ERR importer.importer setMongoAttribute', err); - } + config.error && config.error('ERR importer.importer setMongoAttribute', err); return cb(err); } return cb(null, remainder); }); }); } - if (config.debug) { - console.log('no import_leftover tag found'); - } + config.debug && config.debug('no import_leftover tag found'); return cb(null, remainder); }); }, function onFailure(err) { - if (config.errors) { - console.error('ERR importer.importer onFailure', err); - } + config.error && config.error('ERR importer.importer onFailure', err); return cb(err); } ); @@ -97,9 +85,7 @@ module.exports = function (config, mongoInstance, helpers, markers) { function importRemainder(context, result, remainder, callback) { return mongoInstance.ensureContent(context, function parent(err, parent_result) { if (err) { - if (config.errors) { - console.error('ERR importer.importer ensureContent', err); - } + config.error && config.error('ERR importer.importer ensureContent', err); return callback(err); } function replaceWithEmptyContent(err) { @@ -115,9 +101,7 @@ module.exports = function (config, mongoInstance, helpers, markers) { try { data = JSON.parse(remainder); } catch (error) { - if (config.errors) { - console.error('ERR importer.replaceMarkers JSON.parse(remainder)', remainder, error); - } + config.error && config.error('ERR importer.replaceMarkers JSON.parse(remainder)', remainder, error); return callback(error); } if (data._id) { @@ -127,9 +111,7 @@ module.exports = function (config, mongoInstance, helpers, markers) { context.update = true; return mongoInstance.setMongoContent(parent_result, context, function (err) { if (err) { - if (config.errors) { - console.error('ERR importer.importRemainder setMongoContent', err); - } + config.error && config.error('ERR importer.importRemainder setMongoContent', err); return callback(err); } var documentId = 'json:' + context.collection + ':' + context.name; @@ -142,9 +124,7 @@ module.exports = function (config, mongoInstance, helpers, markers) { context.query = { _id: parent_result._id }; return mongoInstance.setMongoAttribute(remainder, context, function savedAttribute(err) { if (err) { - if (config.errors) { - console.error('ERR2 importer.importer setMongoAttribute', err); - } + config.error && config.error('ERR2 importer.importer setMongoAttribute', err); return callback(err); } return replaceWithEmptyContent(null); @@ -175,9 +155,7 @@ module.exports = function (config, mongoInstance, helpers, markers) { handleImportMarkers(sub_doc, options, function handleLeftover(err, remainder) { if (err) { - if (config.errors) { - console.error('ERR importer.replaceMarkers import_content_marker', err); - } + config.error && config.error('ERR importer.replaceMarkers import_content_marker', err); return callback(err); } return importRemainder(context, result, remainder, callback); @@ -197,17 +175,13 @@ module.exports = function (config, mongoInstance, helpers, markers) { }; fs.readFile(filename, 'utf-8', function handleFileContent(err, sub_doc) { if (err) { - if (config.errors) { - console.error('ERR importer.replaceMarkers readFile', err); - } + config.error && config.error('ERR importer.replaceMarkers readFile', err); return callback(err); } // process with leftover marker support return importer(sub_doc, context, function handleLeftover(err, remainder) { if (err) { - if (config.errors) { - console.error('ERR importer.replaceMarkers importer', err); - } + config.error && config.error('ERR importer.replaceMarkers importer', err); return callback(err); } return importRemainder(context, result, remainder, callback); diff --git a/lib/markers.js b/lib/markers.js index fcb9dea..269a6a7 100644 --- a/lib/markers.js +++ b/lib/markers.js @@ -40,9 +40,7 @@ module.exports = function markers(config) { function createTag(type, collection, name, attribute) { var tag = ''; - if (config.debug) { - config.debug('markers.createTag created:', tag); - } + config.debug && config.debug('markers.createTag created:', tag); return tag; } diff --git a/lib/mongoData.js b/lib/mongoData.js index a03f7f8..5ef3631 100644 --- a/lib/mongoData.js +++ b/lib/mongoData.js @@ -10,17 +10,13 @@ module.exports = function (config, db, shareModel) { * query (mandatory) */ function getMongoContent(options, callback) { - if (config.debug) { - config.debug('getMongoContent options', options); - } + config.debug && config.debug('getMongoContent options', options); if ( !options.collection) { return callback(new Error('Data not found / missing collection')); } return db.collection(options.collection, function collection(err, col) { if (err) { - if (config.error) { - config.error('ERR2 getMongoContent', err); - } + config.error && config.error('ERR2 getMongoContent', err); return callback(err); } if (!options.query) { @@ -30,17 +26,13 @@ module.exports = function (config, db, shareModel) { try { options.query._id = new ObjectID.createFromHexString(options.query._id); } catch (error) { - if (config.error) { - config.error('ERR3 getMongoContent', error); - } + config.error && config.error('ERR3 getMongoContent', error); return callback(error); } } return col.findOne(options.query, function foundOne(err, result) { if (err) { - if (config.error) { - config.error('ERR4 getMongoContent', err); - } + config.error && config.error('ERR4 getMongoContent', err); return callback(err); } if (!result) { @@ -59,20 +51,14 @@ module.exports = function (config, db, shareModel) { * attribute (mandatory) */ function getMongoAttribute(options, callback) { - if (config.debug) { - console.log('getMongoAttribute options', options); - } + config.debug && config.debug('getMongoAttribute options', options); return getMongoContent(options, function document(err, result) { if (err) { - if (config.errors) { - console.error('ERR1 getMongoAttribute', err); - } + config.error && config.error('ERR1 getMongoAttribute', err); return callback(err); } var attribute_options = null; - if (config.debug) { - console.log('getMongoAttribute result', result); - } + config.debug && config.debug('getMongoAttribute result', result); if (result && result.hasOwnProperty(options.attribute) && result[options.attribute].guid) { @@ -80,26 +66,18 @@ module.exports = function (config, db, shareModel) { collection: options.collection, query: {_id: result[options.attribute].guid} }; - if (config.debug) { - console.log('getMongoAttribute attribute_options', attribute_options); - } + config.debug && config.debug('getMongoAttribute attribute_options', attribute_options); getMongoContent(attribute_options, function attribute(err, attribute_result) { if (err) { - if (config.errors) { - console.error('ERR2 getMongoAttribute', err); - } + config.error && config.error('ERR2 getMongoAttribute', err); return callback(err); } - if (config.debug) { - console.log('getMongoAttribute attribute_result', attribute_result); - } + config.debug && config.debug('getMongoAttribute attribute_result', attribute_result); return callback(err, attribute_result); }); } else { - if (config.debug) { - console.log('getMongoAttribute try direct lookup'); - } + config.debug && config.debug('getMongoAttribute try direct lookup'); attribute_options = { collection: options.collection, query: { @@ -107,20 +85,14 @@ module.exports = function (config, db, shareModel) { name: result.name + '.' + options.attribute } }; - if (config.debug) { - console.log('getMongoAttribute attribute_options', attribute_options); - } + config.debug && config.debug('getMongoAttribute attribute_options', attribute_options); return getMongoContent(attribute_options, function attribute(err, attribute_result) { if (err) { - if (config.errors) { - console.error('ERR getMongoAttribute', err); - } + config.error && config.error('ERR getMongoAttribute', err); return callback(err); } - if (config.debug) { - console.log('getMongoAttribute direct attribute_result', attribute_result); - } + config.debug && config.debug('getMongoAttribute direct attribute_result', attribute_result); return callback(err, attribute_result); }); } @@ -131,20 +103,14 @@ module.exports = function (config, db, shareModel) { if (data._id && !(data._id instanceof Object)) { data._id = new ObjectID.createFromHexString(data._id); } - if (config.debug) { - console.log('saveData saving', data._id); - } + config.debug && config.debug('saveData saving', data._id); collection.save(data, {safe: true}, function (err, result2, result3) { if (err) { - if (config.errors) { - console.error('ERR saveData', err); - } + config.error && config.error('ERR saveData', err); return callback(err); } - if (config.debug) { - console.log('saveData saved', data._id, result2, result3); - } + config.debug && config.debug('saveData saved', data._id, result2, result3); return callback(null, data, collection); }); } @@ -155,17 +121,13 @@ module.exports = function (config, db, shareModel) { if (updating) { //noinspection JSUnresolvedFunction setImmediate(function rescheduling() { - if (config.debug) { - console.log('Updating, rescheduling'); - } + config.debug && config.debug('Updating, rescheduling'); return updateData(collection, data, callback); }); } else { updating = true; var stopUpdating = function (err, result, col) { - if (config.debug) { - console.log('Stop updating'); - } + config.debug && config.debug('Stop updating'); updating = false; if (err) { return callback(err); @@ -175,9 +137,7 @@ module.exports = function (config, db, shareModel) { return collection.findOne({ _id: data._id}, function foundOne(err, result) { if (err) { - if (config.errors) { - console.error('ERR updateData', err); - } + config.error && config.error('ERR updateData', err); return callback(err); } _.extend(result, data); @@ -194,14 +154,10 @@ module.exports = function (config, db, shareModel) { * update (optional) : extends existing content */ function setMongoContent(data, options, callback) { - if (config.debug) { - console.log('setMongoContent options', options); - } + config.debug && config.debug('setMongoContent options', options); return db.collection(options.collection, function handleCollection(err, collection) { if (err) { - if (config.errors) { - console.error('ERR2 setMongoContent', err); - } + config.error && config.error('ERR2 setMongoContent', err); return callback(err); } if (options.operation) { @@ -212,14 +168,10 @@ module.exports = function (config, db, shareModel) { dumpData = updateData; } if (!data._id) { - if (config.debug) { - console.log('setMongoContent lookup by query', options.query, 'updating:', options.update); - } + config.debug && config.debug('setMongoContent lookup by query', options.query, 'updating:', options.update); collection.findOne(options.query, function foundOne(err, result) { if (err) { - if (config.errors) { - console.error('ERR3 setMongoContent', err); - } + config.error && config.error('ERR3 setMongoContent', err); return callback(err); } if (result) { @@ -236,9 +188,7 @@ module.exports = function (config, db, shareModel) { function updateShareDocumentPath(documentId, data, path, callback) { shareModel.getSnapshot(documentId, function (err, doc) { if (err) { - if (config.errors) { - console.error('WARN setMongoAttribute updateShareDocumentPath shareModel.getSnapshot', documentId, err); - } + config.warn && config.warn('WARN setMongoAttribute updateShareDocumentPath shareModel.getSnapshot', documentId, err); return callback && callback(); } var sub_data = data; @@ -271,14 +221,10 @@ module.exports = function (config, db, shareModel) { } return shareModel.applyOp(documentId, { op: [op], v: doc.v }, function (err, result) { if (err) { - if (config.errors) { - console.error('ERR updateShareDocumentPath shareModel.applyOp', documentId, err); - } + config.error && config.error('ERR updateShareDocumentPath shareModel.applyOp', documentId, err); return callback && callback(); } - if (config.debug) { - console.log('updateShareDocumentPath shareModel.applyOp', documentId, op, err, result); - } + config.debug && config.debug('updateShareDocumentPath shareModel.applyOp', documentId, op, err, result); return callback && callback(); }); @@ -293,9 +239,7 @@ module.exports = function (config, db, shareModel) { var ops = []; return shareModel.getSnapshot(documentId, function (err, doc) { if (err) { - if (config.errors) { - console.error('WARN updateShareDocument shareModel.getSnapshot', documentId, err); - } + config.warn && config.warn('WARN updateShareDocument shareModel.getSnapshot', documentId, err); return callback && callback(); } if (doc.type.name === 'text') { @@ -323,14 +267,10 @@ module.exports = function (config, db, shareModel) { } return shareModel.applyOp(documentId, { op: ops, v: doc.v }, function (err, result) { if (err) { - if (config.errors) { - console.error('WARN updateShareDocument shareModel.applyOp', documentId, err); - } + config.warn && config.warn('WARN updateShareDocument shareModel.applyOp', documentId, err); return callback && callback(); } - if (config.debug) { - console.log('updateShareDocument shareModel.applyOp', documentId, ops, err, result); - } + config.debug && config.debug('updateShareDocument shareModel.applyOp', documentId, ops, err, result); return callback && callback(); }); }); @@ -343,9 +283,7 @@ module.exports = function (config, db, shareModel) { if (ensuring) { //noinspection JSUnresolvedFunction setImmediate(function rescheduling() { - if (config.debug) { - console.log('Ensuring, rescheduling', options); - } + config.debug && config.debug('Ensuring, rescheduling', options); return ensureContent(options, callback); }); } else { @@ -356,9 +294,7 @@ module.exports = function (config, db, shareModel) { }; } var stopEnsuring = function (err, result, col) { - if (config.debug) { - console.log('Stop ensuring', options); - } + config.debug && config.debug('Stop ensuring', options); ensuring = false; if (err) { return callback(err); @@ -392,14 +328,10 @@ module.exports = function (config, db, shareModel) { * no_share (optional): prevent share to update itself. */ function setMongoAttribute(data, options, callback) { - if (config.debug) { - console.log('setMongoAttribute options', options); - } + config.debug && config.debug('setMongoAttribute options', options); ensureContent(options, function document(err, result, col) { if (err) { - if (config.errors) { - console.error('ERR1 setMongoAttribute', err); - } + config.error && config.error('ERR1 setMongoAttribute', err); return callback(err); } var attribute_options = { @@ -419,14 +351,10 @@ module.exports = function (config, db, shareModel) { } - if (config.debug) { - console.log('getMongoAttribute parent found, get child and save', result, attribute_options); - } + config.debug && config.debug('getMongoAttribute parent found, get child and save', result, attribute_options); return ensureContent(attribute_options, function attribute(err, attribute_result) { if (err) { - if (config.errors) { - console.error('ERR2 setMongoAttribute ensureContent', err); - } + config.error && config.error('ERR2 setMongoAttribute ensureContent', err); return callback(err); } var updateContent = true; @@ -448,9 +376,7 @@ module.exports = function (config, db, shareModel) { } return saveData(col, attribute_result, function saved(err) { if (err) { - if (config.errors) { - console.error('ERR3 setMongoAttribute', err); - } + config.error && config.error('ERR3 setMongoAttribute', err); return callback(err); } var documentId = 'json:' + options.collection + ':' + result.name; @@ -472,9 +398,7 @@ module.exports = function (config, db, shareModel) { if (updateContent) { updateData(col, result, function saved(err) { if (err) { - if (config.errors) { - console.error('ERR3 setMongoAttribute', err); - } + config.error && config.error('ERR3 setMongoAttribute', err); return callback(err); } var path = [options.attribute, 'guid']; // reset just guid attribute; @@ -500,7 +424,3 @@ module.exports = function (config, db, shareModel) { updateShareDocument: updateShareDocument }; }; - - - - diff --git a/lib/preview.js b/lib/preview.js index 18e1582..82dd841 100644 --- a/lib/preview.js +++ b/lib/preview.js @@ -93,16 +93,12 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { }; return mongoDataInstance.getMongoContent(context, function handleContext(err, context_result) { if (err) { - if (config.errors) { - console.error('ERR template_tag getMongoContent', context); - } + config.error && config.error('ERR template_tag getMongoContent', context); return callback(err); } return mongoDataInstance.getMongoAttribute(template, function handleTemplate(err, template_result) { if (err) { - if (config.errors) { - console.error('ERR template_tag getMongoAttribute', template, err); - } + config.error && config.error('ERR template_tag getMongoAttribute', template, err); return callback(err); } var compiled_template = null; @@ -131,9 +127,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { try { compiled_template = handlebars.compile(template_result[template.attribute]); } catch (error1) { - if (config.errors) { - console.error('ERR template_tag Handlebars.compile', template, error1); - } + config.error && config.error('ERR template_tag Handlebars.compile', template, error1); return callback(error1); } var extendHandlebars_context = function (result) { @@ -151,9 +145,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { try { compiled_template(handlebars_context); } catch (error2) { - if (config.errors) { - console.error('ERR template_tag Handlebars.render', template, context, error2); - } + config.error && config.error('ERR template_tag Handlebars.render', template, context, error2); return callback(error2); } var promises = []; @@ -168,9 +160,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { }; return mongoDataInstance.getMongoAttribute(attribute_context, function cacheTemplateKey(err, template_key_result) { if (err) { - if (config.errors) { - console.error('ERR handlebar.registerHelper getMongoAttribute', err); - } + config.error && config.error('ERR handlebar.registerHelper getMongoAttribute', err); deferred.reject(err); } var value = template_key_result[key]; @@ -184,20 +174,14 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { try { rendered = compiled_template(handlebars_context); } catch (err) { - if (config.errors) { - console.error('ERR template_tag Handlebars.render', template, context, err); - } + config.error && config.error('ERR template_tag Handlebars.render', template, context, err); return callback(err); } - if (config.debug) { - console.log('// handle markers on rendered template'); - } + config.debug && config.debug('// handle markers on rendered template'); context.query = {_id: context_result._id}; return getPreviewHTML(rendered, context, function handlePreviewResult(err, preview_html) { if (err) { - if (config.errors) { - console.error('ERR template_tag getPreviewHTML', err); - } + config.error && config.error('ERR template_tag getPreviewHTML', err); return callback(err); } return callback(null, { @@ -207,9 +191,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { }); }, function onFailure(err) { - if (config.errors) { - console.error('ERR template_tag resolving promises', err); - } + config.error && config.error('ERR template_tag resolving promises', err); return callback(err); } ); @@ -252,9 +234,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { }; getPreviewHTML = function (content, options, callback) { - if (config.debug) { - console.log('getPreviewHTML', content); - } + config.debug && config.debug('getPreviewHTML', content); var promises = replaceMarkers(content, options); function handler(text, result) { diff --git a/lib/responder.js b/lib/responder.js index 821d985..7a2bc9e 100644 --- a/lib/responder.js +++ b/lib/responder.js @@ -3,11 +3,11 @@ * options.ext: determines content-type * options.attribute: sends result[options.attribute] in stead of result */ -module.exports = function (options, res, next) { +module.exports = function (config, options, res, next) { "use strict"; return function responder(err, result) { if (err) { - console.log('ERR responder', options, err); + config.error && config.error('ERR responder', options, err); if (/Data not found*/.test(err.message)) { res.status(404); } diff --git a/lib/routes.js b/lib/routes.js index 195cf28..58695c3 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -9,9 +9,7 @@ module.exports = function (app, handlers, markers, config) { route = config.api.data + '/:collection/:guid/:attribute.:ext(css|less|js|html)'; app.get(route, function getAttributeByGUID(req, res, next) { - if (config.debug) { - console.log('/data/:collection/:guid/:attribute.:ext(less|js|html)'); - } + config.debug && config.debug('/data/:collection/:guid/:attribute.:ext(less|js|html)'); var options = { collection: req.params.collection, attribute: req.params.attribute, @@ -19,7 +17,7 @@ module.exports = function (app, handlers, markers, config) { query: {_id: req.params.guid} }; handlers.getAttribute(options, - responder(options, res, next) + responder(config, options, res, next) ); } ); @@ -27,16 +25,14 @@ module.exports = function (app, handlers, markers, config) { route = config.api.data + '/:collection/:guid.:ext(json)'; app.get(route, function getContentByGUID(req, res, next) { - if (config.debug) { - console.log('/data/:collection/:guid.:ext(json)'); - } + config.debug && config.debug('/data/:collection/:guid.:ext(json)'); var options = { collection: req.params.collection, ext: req.params.ext, query: {_id: req.params.guid} }; handlers.getContent(options, - responder(options, res, next) + responder(config, options, res, next) ); } ); @@ -44,9 +40,7 @@ module.exports = function (app, handlers, markers, config) { route = config.api.content + '/:collection/:name/:attribute.:ext(css|less|js|html)'; app.get(route, function getAttributeByName(req, res, next) { - if (config.debug) { - console.log('/content/:collection/:name/:attribute.:ext(less|js|html)'); - } + config.debug && config.debug('/content/:collection/:name/:attribute.:ext(less|js|html)'); var options = { collection: req.params.collection, attribute: req.params.attribute, @@ -54,7 +48,7 @@ module.exports = function (app, handlers, markers, config) { query: {name: req.params.name} }; handlers.getAttribute(options, - responder(options, res, next) + responder(config, options, res, next) ); } ); @@ -62,16 +56,14 @@ module.exports = function (app, handlers, markers, config) { route = config.api.content + '/content/:collection/:name.:ext(json)'; app.get(route, function getContentByName(req, res, next) { - if (config.debug) { - console.log('/content/:collection/:name.:ext(json)'); - } + config.debug && config.debug('/content/:collection/:name.:ext(json)'); var options = { collection: req.params.collection, ext: req.params.ext, query: {name: req.params.name} }; handlers.getContent(options, - responder(options, res, next) + responder(config, options, res, next) ); } ); @@ -80,9 +72,7 @@ module.exports = function (app, handlers, markers, config) { route = config.api.preview + '/:collection/:name.:ext(html|md)'; app.get(route, function getPreviewContent(req, res, next) { - if (config.debug) { - console.log('/page/:collection/:name.:ext(html|md)'); - } + config.debug && config.debug('/page/:collection/:name.:ext(html|md)'); var options = { collection: req.params.collection, ext: req.params.ext, @@ -95,12 +85,12 @@ module.exports = function (app, handlers, markers, config) { var attribute_parts = options.query.name.split('.'); var markdownDocument = markers.createTag('markdown', options.collection, attribute_parts[0], attribute_parts[1]); return handlers.getPreviewHTML(markdownDocument, { req: options.req }, - responder(options, res, next) + responder(config, options, res, next) ); } return handlers.getContent(options, function handleResult(err, result) { if (err) { - return responder(options, res, next)(err, result); + return responder(config, options, res, next)(err, result); } if (result) { var attribute_parts = options.query.name.split('.'); @@ -116,11 +106,9 @@ module.exports = function (app, handlers, markers, config) { req: options.req }; - if (config.debug) { - console.log('getPreviewContent content', attribute_value); - } + config.debug && config.debug('getPreviewContent content', attribute_value); handlers.getPreviewHTML(attribute_value, preview_options, - responder(options, res, next) + responder(config, options, res, next) ); } else { return next(); @@ -136,20 +124,16 @@ module.exports = function (app, handlers, markers, config) { route = config.api.importer + '/:filename'; app.get(route, function importFile(req, res, next) { var filename = path.resolve(config.statics.importer_path, req.params.filename); - if (config.debug) { - console.log('/importer/:filename', filename); - } + config.debug && config.debug('/importer/:filename', filename); fs.readFile(filename, 'utf-8', function handleFileContent(err, sub_doc) { if (err) { - if (config.errors) { - console.error('ERR readFile', filename, err); - } + config.error && config.error('ERR readFile', filename, err); next(err); } // process with leftover marker support var options = {}; handlers.importer(sub_doc, options, - responder(options, res, next) + responder(config, options, res, next) ); }); }); diff --git a/lib/shareHandlers.js b/lib/shareHandlers.js index 2184b0f..422e29a 100644 --- a/lib/shareHandlers.js +++ b/lib/shareHandlers.js @@ -7,9 +7,7 @@ module.exports = function (config, model, mongoDataInstance) { var notFound = false; if (err) { if (/Data not found*/.test(err.message)) { - if (config.debug) { - console.log('handleMongoGetResult.handleResult Document/Attribute not found, It will be created on first OT'); - } + config.debug && config.debug('handleMongoGetResult.handleResult Document/Attribute not found, It will be created on first OT'); result = {}; if (options.attribute) { if (options.type === 'json') { @@ -20,16 +18,12 @@ module.exports = function (config, model, mongoDataInstance) { } notFound = true; } else { - if (config.errors) { - console.error('ERR1 handleMongoGetResult.handleResult Error retrieving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err); - } + config.error && config.error('ERR1 handleMongoGetResult.handleResult Error retrieving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err); } } if (result || notFound) { var operation = null; - if (config.debug) { - console.log('handleMongoGetResult options', options, result); - } + config.debug && config.debug('handleMongoGetResult options', options, result); var data = result; if (options.attribute) { data = result[options.attribute]; @@ -49,13 +43,9 @@ module.exports = function (config, model, mongoDataInstance) { } if (operation) { model.applyOp(options.documentId, operation, function appliedOp(error, version) { - if (config.debug) { - console.log('getResult applyOp version', version); - } + config.debug && config.debug('getResult applyOp version', version); if (error) { - if (config.error) { - console.error('ERR2 handleMongoGetResult', error); - } + config.error && config.error('ERR2 handleMongoGetResult', error); } }); } @@ -66,9 +56,7 @@ module.exports = function (config, model, mongoDataInstance) { } model.on('create', function populateDocument(documentId, data) { - if (config.debug) { - console.log('Populating a doc in channel', documentId, data); - } + config.debug && config.debug('Populating a doc in channel', documentId, data); var splitId = documentId.split(':'); var options = { documentId: documentId, @@ -91,27 +79,19 @@ module.exports = function (config, model, mongoDataInstance) { function handleMongoSetResult(options, current, callback) { function handleResult(err, result) { if (err) { - if (config.errors) { - console.error('ERR1 handleMongoSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err); - } + config.error && config.error('ERR1 handleMongoSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err); return callback && callback(err); } - if (config.debug) { - console.log('current', current, 'result', result, 'options', options); - } + config.debug && config.debug('current', current, 'result', result, 'options', options); if ((!current || !current.name) && (result.name || options.name)) { var name = result.name || options.name; var operation = { op: [ { p: ['name'], oi: name, od: null } ], v: options.operation.v }; model.applyOp(options.documentId, operation, function appliedOp(error, version) { - if (config.debug) { - console.log('setResult applyOp version', version); - } + config.debug && config.debug('setResult applyOp version', version); if (error) { - if (config.error) { - console.error('ERR2 handleMongoSetResult', error); - } + config.error && config.error('ERR2 handleMongoSetResult', error); return callback && callback(error); } return callback && callback(null, version); @@ -127,14 +107,10 @@ module.exports = function (config, model, mongoDataInstance) { function handleMongoAttributeSetResult(options, current, callback) { function handleResult(err, result) { if (err) { - if (config.errors) { - console.error('ERR1 handleMongoAttributeSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err); - } + config.error && config.error('ERR1 handleMongoAttributeSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err); return callback(err); } - if (config.debug) { - console.log('current', current, 'result', result); - } + config.debug && config.debug('current', current, 'result', result); return callback(null, null); } @@ -146,16 +122,12 @@ module.exports = function (config, model, mongoDataInstance) { return function saveContent() { var args = timers[documentId]; delete timers[documentId]; - if (config.debug) { - console.log('running timer', documentId); - } + config.debug && config.debug('running timer', documentId); mongoDataInstance.setMongoContent(args.current, args.options, handleMongoSetResult(args.options, args.current, function handleApplyOpResult(err, version) { if (err) { - if (config.errors) { - console.error('ERR2 applyOp', version, err); - } + config.error && config.error('ERR2 applyOp', version, err); } })); }; @@ -165,9 +137,7 @@ module.exports = function (config, model, mongoDataInstance) { return function saveAttribute() { var args = timers[documentId]; delete timers[documentId]; - if (config.debug) { - console.log('running timer', documentId); - } + config.debug && config.debug('running timer', documentId); var data = args.current; if (args.options.type === 'json') { data = JSON.parse(args.current); @@ -176,9 +146,7 @@ module.exports = function (config, model, mongoDataInstance) { handleMongoAttributeSetResult(args.options, data, function handleApplyOpResult(err, version) { if (err) { - if (config.errors) { - console.error('ERR1 applyOp', documentId, version, err); - } + config.error && config.error('ERR1 applyOp', documentId, version, err); } })); }; @@ -187,9 +155,7 @@ module.exports = function (config, model, mongoDataInstance) { // 'applyOp' event is fired when an operational transform is applied to to a shareDoc // a shareDoc has changed and needs to be saved to mongo model.on('applyOp', function persistDocument(documentId, operation, current) { - if (config.debug) { - console.log('applyOp', documentId, operation, current); - } + config.debug && config.debug('applyOp', documentId, operation, current); if (operation.v === 0) { return; } @@ -220,9 +186,7 @@ module.exports = function (config, model, mongoDataInstance) { if (timers[documentId]) { timer.timer_id = timers[documentId].timer_id; timers[documentId] = timer; - if (config.debug) { - console.log('resetting timer', documentId); - } + config.debug && config.debug('resetting timer', documentId); } else { timers[documentId] = timer; if (attribute) { @@ -234,9 +198,7 @@ module.exports = function (config, model, mongoDataInstance) { handleSetTimeout(documentId), config.savedelay); } - if (config.debug) { - console.log('setting timer', documentId); - } + config.debug && config.debug('setting timer', documentId); } }); }; diff --git a/server.js b/server.js index 0aa132a..5b96366 100644 --- a/server.js +++ b/server.js @@ -17,13 +17,38 @@ var helpers = require('./lib/helpers.js'); var config = { debug: function () { if (process.env.DEBUG) { - console.log(JSON.stringify(arguments)); + var error = arguments[0] && arguments[0].message || + arguments[1] && arguments[1].message || + arguments[2] && arguments[2].message; + var args = Array.prototype.slice.call(arguments); + var log = { level: 'debug', message: args, timestamp: Date.now(), error: error}; + console.log(JSON.stringify(log)); } }, - error: function () { - console.error(JSON.stringify(arguments)); + info: function () { + var error = arguments[0] && arguments[0].message || + arguments[1] && arguments[1].message || + arguments[2] && arguments[2].message; + var args = Array.prototype.slice.call(arguments); + var log = { level: 'info', message: args, timestamp: Date.now(), error: error}; + console.log(JSON.stringify(log)); + }, + warn: function () { + var error = arguments[0] && arguments[0].message || + arguments[1] && arguments[1].message || + arguments[2] && arguments[2].message; + var args = Array.prototype.slice.call(arguments); + var log = { level: 'warn', message: args, timestamp: Date.now(), error: error}; + console.warn(JSON.stringify(log)); + }, + error: function () { + var error = arguments[0] && arguments[0].message || + arguments[1] && arguments[1].message || + arguments[2] && arguments[2].message; + var args = Array.prototype.slice.call(arguments); + var log = { level: 'error', message: args, timestamp: Date.now(), error: error}; + console.error(JSON.stringify(log)); }, - errors: true, port: process.env.npm_package_config_port || 8000, mongo: { server: "mongodb://localhost:27017/Prototyper", @@ -41,7 +66,24 @@ var config = { }, share: { sockjs: { - websocket: true + prefix: '', + response_limit: 128 * 1024, + websocket: true, + jsessionid: false, + heartbeat_delay: 25000, + disconnect_delay: 5000, + log: function(severity, line) { + if (process.env.DEBUG) { + if (severity === 'info') { + config.info && config.info(severity,line); + } else if (severity === 'error') { + config.error && config.error(severity, line); + } else { + config.debug && config.debug(severity, line); + } + } + }, + sockjs_url: 'https://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js' }, staticpath: '/lib/share', db: {type: 'none'} @@ -67,9 +109,7 @@ var config = { } }; -if (config.debug) { - config.debug('config loaded'); -} +config.debug && config.debug('config loaded'); var app = express(); @@ -77,8 +117,12 @@ express.static.mime.define({ 'text/css': ['css', 'less'] }); -if (config.debug) { - app.use(connect.logger()); +if (process.env.DEBUG) { + //app.use(connect.logger()); + var stream = { write: function(str) { + config.debug && config.debug(str); + }}; + app.use(connect.logger({ immediate: false, format: 'dev', stream: stream })); } //noinspection JSUnresolvedFunction app.use(express.compress()); @@ -99,67 +143,47 @@ app.use('/lib/ace', express.static(config.statics.ace_client)); //noinspection JSUnresolvedFunction app.use('/lib/async', express.static(config.statics.async_client)); -if (config.debug) { - config.debug('static routes set'); -} +config.debug && config.debug('static routes set'); var markerInstance = markers(config); var helperInstance = helpers(markerInstance); MongoClient.connect(config.mongo.server, config.mongo.options, function connection(err, db) { if (err) { - if (config.error) { - config.error('ERR connection to database', err); - } + config.error && config.error('ERR connection to database', err); return process.exit(3); } - if (config.debug) { - config.debug('database connected'); - } + config.debug && config.debug('database connected'); var share = shareServer(config, app, db); var model = share.model; var server = share.server; - if (config.debug) { - config.debug('share attached'); - } + config.debug && config.debug('share attached'); var mongoDataInstance = mongoData(config, db, model); - if (config.debug) { - config.debug('mongodata initialized'); - } + config.debug && config.debug('mongodata initialized'); shareHandlers(config, model, mongoDataInstance); - if (config.debug) { - config.debug('shareHandlers attached'); - } + config.debug && config.debug('shareHandlers attached'); var previewInstance = preview(config, mongoDataInstance, helperInstance, markerInstance); - if (config.debug) { - config.debug('previews initialized'); - } + config.debug && config.debug('previews initialized'); var importerInstance = importer(config, mongoDataInstance, helperInstance, markerInstance); - if (config.debug) { - config.debug('importer initialized'); - } + config.debug && config.debug('importer initialized'); var handlerInstance = handlers(mongoDataInstance, previewInstance, importerInstance); - if (config.debug) { - config.debug('handlers initialized'); - } + config.debug && config.debug('handlers initialized'); app = addRoutes(app, handlerInstance, markers, config); - if (config.debug) { - config.debug('routes added'); - } + config.debug && config.debug('routes added'); function exit(code) { db.close(); @@ -167,9 +191,7 @@ MongoClient.connect(config.mongo.server, config.mongo.options, function connecti } server.on('error', function (err) { - if (config.error) { - config.error('Server error', err); - } + config.error && config.error('Server error', err); if (err.code && err.code === 'EADDRINUSE') { exit(2); } @@ -177,15 +199,11 @@ MongoClient.connect(config.mongo.server, config.mongo.options, function connecti return server.listen(config.port, function handleServerResult(err) { if (err) { - if (config.error) { - console.error('Server error', err); - } + console.error('Server error', err); return exit(1); } - if (config.debug) { - config.debug('routes', app.routes); - } - return console.log('Server running at http://127.0.0.1:' + config.port); + config.debug && config.debug('routes', app.routes); + return config.info && config.info('Server running at http://127.0.0.1:' + config.port); }); }); diff --git a/test/test.helpers.js b/test/test.helpers.js index 4f1d954..cec80d9 100644 --- a/test/test.helpers.js +++ b/test/test.helpers.js @@ -3,7 +3,8 @@ var libpath = process.env.PROTOTYPER_COV ? '../lib-cov' : '../lib'; var helpers = require(libpath + '/helpers.js'); var markers = require(libpath + '/markers.js'); var chai = require('chai'); -chai.Assertion.includeStack = true; // defaults to false +chai.config.includeStack = true; // defaults to false +chai.config.showDiff = false; // defaults to false var expect = chai.expect; var when = require('when'); diff --git a/test/test.mongoData.js b/test/test.mongoData.js index 3842149..c286505 100644 --- a/test/test.mongoData.js +++ b/test/test.mongoData.js @@ -2,6 +2,7 @@ var libPath = process.env.PROTOTYPER_COV ? '../lib-cov' : '../lib'; var chai = require('chai'); chai.config.includeStack = true; // defaults to false +chai.config.showDiff = false; // defaults to false var expect = chai.expect; var mongoData = require(libPath + '/mongoData.js');