var doResize = function (imagePath, opts) { var dfd = Deferred(); async.each(opts.sizes, function (size, cb) { var imgStream = fs.createReadStream(imagePath), basename = path.basename(imagePath), sizeInText = lib.format('_{0}_', size), outputPath = path.join(opts.outputFolder, sizeInText, path.relative(opts.relativePath, imagePath) ); mkdirp(path.dirname(outputPath), function (err) { if (err) { throw err; } gm(imgStream, basename) .resize(size, size, '%') .write(outputPath, function (err) { if (err) { throw err; } cb && cb(); }); }); }, function () { dfd.resolve(); }); return dfd.promise(); };
collectName: function() { var command = 'sw_vers', def = Deferred(), that = this; this.run(command, function(text) { var result = separate([':', '\n'], text), k, v; for (var i = 0; i < result.length; i += 2) { k = result[i].trim(); v = result[i + 1].trim(); if (k === 'ProductName') { that.info.name = v; } else if (k === 'ProductVersion') { that.info.version = v; } } def.resolve(); }); return def.promise(); }
module.exports = function inspect( file ) { return Deferred( function( defer ) { fs.stat( file, function( err, stat ) { if ( err ) { defer.reject( err ); } else if ( !stat.isDirectory() ) { defer.resolve( true ); } else { fs.readdir( file, function( err, files ) { if ( err ) { defer.reject( err ); } else { var dir = {}; Deferred.when.apply( Deferred, files.map( function( sub ) { return inspect( path.join( file, sub ) ).done( function( data ) { dir[ sub ] = data; } ); } ) ).done(function() { defer.resolve( dir ); } ).fail(function( err ) { defer.reject( err ); } ); } } ); } } ); } ).promise(); };
EdgeConnection.prototype.close = function () { var def = defer(), that = this, edge = require('edge'); if (this.edgeHandler===null) { def.resolve(); return def; } var edgeClose = edge.func(this.sqlCompiler, { handler: this.edgeHandler, source: 'close', cmd: 'close', driver: this.driver }); edgeClose({}, function (error) { if (error) { def.reject(error); return; } that.edgeHandler = null; def.resolve(); }); return def.promise(); };
EdgeConnection.prototype.queryBatch = function (query, raw) { var edge = require('edge'), edgeQuery = edge.func(this.sqlCompiler, _.assign({source: query}, this.getDbConn())), def = defer(); process.nextTick(function () { edgeQuery({}, function (error, result) { if (error) { def.reject(error + ' running ' + query); return; } var i; for (i = 0; i < result.length - 1; i++) { if (raw) { def.notify(result[i]); } else { def.notify(simpleObjectify(result[i].meta, result[i].rows)); } } if (raw) { def.resolve(result[i]); } else { def.resolve(simpleObjectify(result[i].meta, result[i].rows)); } }); }); return def; };
Connection.prototype.callSPWithNamedParams = function (options) { var spDef = defer(), cmd = this.getSqlCallSPWithNamedParams(options); //noinspection JSUnresolvedFunction this.edgeConnection.queryBatch(cmd, options.raw) .progress(function (result) { spDef.notify(result); }) .done(function (result) { if (_.some(options.paramList, {out: true})) { //An object is needed for output row var allVar = options.raw ? simpleObjectify(result[0].meta, result[0].rows) : result[0]; _.each(_.keys(allVar), function (k) { _.find(options.paramList, {name: k}).outValue = allVar[k]; }); spDef.resolve(options.paramList); } else { spDef.resolve(result); } }) .fail(function (err) { spDef.reject(err); }); return spDef.promise(); };
collectName: function() { var command = 'wmic os get Caption,CSDVersion /value', def = Deferred(), that = this; this.run(command, function(text) { var result = separate(['=', '\n', '\r'], text), k, v; for (var i = 0; i < result.length; i += 2) { k = result[i].trim(); v = result[i + 1].trim(); if (k === 'Caption') { that.info.name = v; } else if (k === 'CSDVersion') { that.info.version = v; } } def.resolve(); }); return def.promise(); }
collectName: function() { var command = 'lsb_release -a', def = Deferred(), that = this; this.run(command, function(text) { var result = separate([':', '\n'], text), k, v; for (var i = 0; i < result.length; i += 2) { k = result[i].trim(); v = result[i + 1].trim(); if (k === 'Distributor ID') { that.info.name = v; } else if (k === 'Release') { that.info.version = v; } else if (k === 'Codename') { that.info.codename = v; } else if (k === 'Description') { that.info.description = v; } } def.resolve(); }); return def.promise(); }
, createSideNav : function () { var dfd = Deferred(); var START_PG = NAV_ROOT_PG + 1; createPg(SIDE_NAV, NAV_ROOT_PG, START_PG, 'nav/side').then(function () { createAllPg( SIDE_NAV, START_PG, START_PG + 1, 'nav/side/', ['acme', 'platforma'], [{ 'filePath' : 'base/nav/side_acme.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }, { 'filePath' : 'base/nav/side_platforma.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }] ).then (function () { Deferred.when( createPg(NAV_ITEM_TMPL, START_PG + 1, START_PG + 3, 'nav/side/acme/items', { 'data' : {'latitude' : '', 'longitute' : ''} //eg, for moonclock , 'filePath' : 'base/nav/side_acme_item.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg(NAV_ITEM_TMPL, START_PG + 2, START_PG + 4, 'nav/side/platforma/items', { 'data' : {'leaves' : {'financiranje' : 'platforma/financiranje' , 'prostori' : 'platforma/prostori' , 'strategija' : 'platforma/strategija'}} , 'filePath' : 'base/nav/side_platforma_item.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) ).then (function () { dfd.resolve(); }); }); }); return dfd.promise(); }
Connection.prototype.open = function () { var connDef = defer(), that = this; if (this.isOpen) { return connDef.resolve(this).promise(); } this.edgeConnection.open() .done(function () { that.isOpen = true; if (that.schema === that.defaultSchema) { connDef.resolve(that); return; } that.useSchema(that.schema) .done(function () { connDef.resolve(that); }) .fail(function (err) { that.close(); connDef.reject('schema fail' + err); }); }) .fail(function (err) { connDef.reject('open fail' + err); connDef.reject(err); }); return connDef.promise(); };
, createBottomNav : function () { var dfd = Deferred(); var START_PG = NAV_ROOT_PG + 6 + 5; createPg( BOTTOM_NAV, NAV_ROOT_PG, START_PG, 'navs/bottom', { 'filePath' : 'base/nav/bottom.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }).then(function () { createPg( NAV_ITEM_TMPL, START_PG, START_PG + 1, 'nav/bottom/items', { 'data' : {'leaves' : { 'front' : '' , 'acme' : 'acme' , 'platforma' : 'platforma' , 'kalendar' : 'kalendar' } , 'filePath' : 'base/nav/bottom_item.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary } }).then (function () { dfd.resolve(); }); }); return dfd.promise(); }
EdgeConnection.prototype.open = function () { var def = defer(), that = this, edge = require('edge'), edgeOpenInternal = edge.func(this.sqlCompiler, { source: 'open', connectionString: this.connectionString, cmd: 'open', driver: this.driver }); edgeOpenInternal({}, function (error, result) { if (error) { def.reject(error); return; } if (result) { that.edgeHandler = result; def.resolve(); return; } def.reject('shouldnt reach here'); }); return def.promise(); };
Connection.prototype.setTransactionIsolationLevel = function (isolationLevel) { var that = this, res, mappedIsolationLevels = mapIsolationLevels[isolationLevel]; if (this.isolationLevel === isolationLevel) { return defer().resolve().promise(); } if (mappedIsolationLevels === undefined) { return defer().reject(isolationLevel + " is not an allowed isolation level").promise(); } res = this.edgeConnection.queryBatch('SET TRANSACTION ISOLATION LEVEL ' + mappedIsolationLevels); res.done(function () { that.isolationLevel = isolationLevel; }); return res.promise(); };
Connection.prototype.beginTransaction = function (isolationLevel) { var that = this; if (!this.isOpen) { return defer().reject("Cannot beginTransaction on a closed connection").promise(); } if (this.transAnnidationLevel > 0) { this.transAnnidationLevel += 1; return defer().resolve().promise(); } return this.setTransactionIsolationLevel(isolationLevel) .then(function () { var res = that.edgeConnection.queryBatch('START TRANSACTION;'); res.done(function () { that.transAnnidationLevel += 1; that.transError = false; }); return res; }); };
"bind": function(ma, f) { var defer = deferred(); ma.done(function(a) { var mb = f(a); mb.done(defer.resolve); return mb.fail(defer.reject); }); ma.fail(defer.reject); return defer.promise(); }
Connection.prototype.rollBack = function () { var that = this, res; if (!this.isOpen) { return defer().reject("Cannot rollback on a closed connection").promise(); } if (this.transAnnidationLevel > 1) { this.transAnnidationLevel -= 1; this.transError = true; return defer().resolve().promise(); } if (this.transAnnidationLevel === 0) { return defer().reject("Trying to rollBack but no transaction has been open").promise(); } res = this.edgeConnection.queryBatch('ROLLBACK;'); res.done(function () { that.transAnnidationLevel = 0; }); return res.promise(); };
EdgeConnection.prototype.run = function (script) { var os = require('os'), //noinspection JSUnresolvedVariable lines = script.split(os.EOL), blocks = [], curr = '', first = true, that = this, i, s; for (i = 0; i < lines.length; i++) { s = lines[i]; if (s.trim().toUpperCase() === 'GO') { blocks.push(curr); curr = ''; first = true; continue; } if (!first) { //noinspection JSUnresolvedVariable curr += os.EOL; } curr += s; first = false; } if (curr.trim() !== '') { blocks.push(curr); } var def = defer(), index = 0; function loopScript() { if (index === blocks.length) { def.resolve(); } else { that.updateBatch(blocks[index]) .done(function () { index += 1; loopScript(); }) .fail(function (err) { def.reject(err); }); } } loopScript(); return def.promise(); };
var createFn = function (table, values) { var dfd = Deferred(); dbq.basicDbErr( c.query(dbq.createQ(table, _.keys(values))(values)) .on('result', function (dbres) { dbq.basicDbErr( dbres.on('end', function (info) { dfd.resolve(info); })); })); return dfd.promise(); };
Connection.prototype.close = function () { var def = defer(), that = this; if (this.isOpen) { this.edgeConnection.close() .done(function () { that.isOpen = false; def.resolve(); }); } def.resolve(); return def.promise(); };
Connection.prototype.commit = function () { var that = this, res; if (!this.isOpen) { return defer().reject("Cannot commit on a closed connection").promise(); } if (this.transAnnidationLevel > 1) { this.transAnnidationLevel -= 1; return defer().resolve().promise(); } if (this.transAnnidationLevel === 0) { return defer().reject("Trying to commit but no transaction has been open").promise(); } if (this.transError) { return this.rollBack(); } res = this.edgeConnection.queryBatch('COMMIT;'); res.done(function () { that.transAnnidationLevel = 0; }); return res.promise(); };
var createPg = function (nsId, catId, id, url, content, tag) { var dfd = Deferred(); var pageFn = function (contInfo) { var contId = contInfo && contInfo['insertId'] || content['id']; var pg = { "url" : url , "activeNsId" : nsId }; if (id) { pg['id'] = id; } if (content) { pg['activeContId'] = contId; } createFn('Pages', pg).then(function (pgInfo) { var pgId = pgInfo && pgInfo['insertId'] || id; var rev = { "nsId" : nsId , "pgId" : pgId , "userId" : ROOT_USR , "grpId" : ROOT_GRP }; if(catId) { rev['catPgId'] = catId; } if (content) { rev['contId'] = contId; } createFn('Revisions', rev).then(function (revInfo) { if (tag) { createFn('Tags', tag).then (function(tagInfo) { dfd.resolve(revInfo); }) } else { dfd.resolve(revInfo); } }); }); }; if (content) { createFn('Contents', content).then(function (contInfo) { pageFn(contInfo); }); } else { pageFn(); } return dfd.promise(); };
Connection.prototype.checkLogin = function (login, password) { var opt = _.assign({}, this.opt, {user: login, pwd: password}), def = defer(), testConn = new Connection(opt); testConn.open() .done(function () { def.resolve(true); testConn.destroy(); }) .fail(function () { def.resolve(false); }); return def.promise(); };
EdgeConnection.prototype.updateBatch = function (query) { var edge = require('edge'), edgeQuery = edge.func(this.sqlCompiler, _.assign({source: query, cmd: 'nonquery'}, this.getDbConn())), def = defer(); edgeQuery({}, function (error, result) { if (error) { def.reject(error); return; } def.resolve(result); }); return def.promise(); };
http.createServer( function( request, response ) { if ( request.headers["content-length"] || request.headers["transfer-encoding"] ) { request.body = Deferred(function( defer ) { var body = ""; request.on( "data", function( data ) { body += data; }).on( "end", function() { defer.resolve( body ); }); }).promise(); } request.parsedURL = url.parse( request.url, true ); handleRequest( [ path.join( dir, request.parsedURL.pathname.replace( r_jhpInPath, "$1" ) ) ], request, response ); }).on( "error", function( e ) {
function find_member(_id, ref) { return Deferred(function(dfd){ Member.findOne({'_id':_id}, function(err, member){ if (err){ return dfd.reject(err); } else { var index = member.commentaires.indexOf(ref); if (index != -1) { member.commentaires.splice(index); member.save(); } return dfd.resolve(member); } }); }).promise(); }
function find_salle(_id, ref) { return Deferred(function(dfd) { Salle.findOne({'_id' : _id}, function(err, salle){ if (err) { return dfd.reject(err); } else { var index = salle.commentaires.indexOf(ref); if (index != -1) { salle.commentaires.splice(index); salle.save(); } return dfd.resolve(salle); } }); }).promise(); }
, createTopNav : function () { var dfd = Deferred(); var START_PG = NAV_ROOT_PG + 6; createPg(TOP_NAV, NAV_ROOT_PG, START_PG, 'nav/top').then(function () { createAllPg( TOP_NAV, START_PG, START_PG + 1, 'nav/top/', ['acme', 'platforma'], [{ 'filePath' : 'base/nav/top_acme.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }, { 'filePath' : 'base/nav/top_platforma.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }] ).then (function () { Deferred.when( createPg( NAV_ITEM_TMPL, START_PG + 1, START_PG + 3, 'nav/top/acme/items', { 'data' : { 'about' : 'acme/about' // XXX distinct from front? , 'front' : 'acme/front' , 'news' : 'acme/news' , 'kalendar' : 'acme/cal' , 'program': 'acme/program' } , 'filePath' : 'base/nav/top_acme_item.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg( NAV_ITEM_TMPL, START_PG + 2, START_PG + 4, 'nav/top/platforma/items', { 'data' : { 'about' : 'platforma/about' // XXX distinct from front? , 'front' : 'acme/front' , 'index' : 'platforma/index' , 'kalendar' : 'platforma/kalendar' , 'aktivnosti' : 'platforma/aktivnosti' } , 'filePath' : 'base/nav/top_platforma_item.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) ).then( function () { dfd.resolve(); }); }); }); return dfd.promise(); }
EdgeConnection.prototype.queryPackets = function (query, raw, packSize) { var def = defer(), packetSize = packSize || 0, lastMeta, currentSet = -1, callback = function (data) { if (data.resolve) { def.resolve(); return; } if (data.meta) { currentSet += 1; } data.set = currentSet; if (raw) { def.notify(data); } else { if (data.meta) { lastMeta = data.meta; } else { def.notify({rows: simpleObjectify(lastMeta, data.rows), set: currentSet}); } } }; var that = this; process.nextTick(function () { var edge = require('edge'), edgeQuery = edge.func(that.sqlCompiler, _.assign({source: query, callback: callback, packetSize: packetSize}, that.getDbConn())); edgeQuery({}, function (error) { if (error) { def.reject(error + ' running ' + query); return; } //console.log('resolving '+query); //def.resolve(); }); }); return def.promise(); };
, createForm : function () { var dfd = Deferred(); Deferred.when( createPg(FORM, FORM_ROOT_PG, null, 'form/basic', { 'filePath' : 'base/forms/basic_form.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg(FORM, FORM_ROOT_PG, null, 'form/md', { 'filePath' : 'base/forms/markdown_form.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg(FORM, FORM_ROOT_PG, null, 'form/usr', { 'filePath' : 'base/forms/user_form.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg(FORM, FORM_ROOT_PG, null, 'form/group', { 'filePath' : 'base/forms/group_form.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg(FORM, FORM_ROOT_PG, null, 'form/ns', { 'filePath' : 'base/forms/ns_form.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg(FORM, FORM_ROOT_PG, null, 'form/rev', { 'filePath' : 'base/forms/rev_form.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg(FORM, FORM_ROOT_PG, null, 'form/cal', { 'filePath' : 'base/forms/cal_form.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) , createPg(FORM, FORM_ROOT_PG, null, 'form/nav', { 'filePath' : 'base/forms/nav_form.html' , 'mimeType' : 'text/x-handlebars-template' //arbitrary }) ).then(function () { dfd.resolve(); }); return dfd.promise(); }
Connection.prototype.tableDescriptor = function (tableName) { var res = defer(), that = this; this.edgeConnection.queryBatch( 'select 1 as dbo, ' + 'case when T.table_type=\'BASE TABLE\' then \'U\' else \'V\' end as xtype, ' + 'C.COLUMN_NAME as name, C.DATA_TYPE as \'type\', C.CHARACTER_MAXIMUM_LENGTH as max_length,' + 'C.NUMERIC_PRECISION as \'precision\', C.NUMERIC_SCALE as \'scale\', ' + 'case when C.IS_NULLABLE = \'YES\' then 1 else 0 end as \'is_nullable\', ' + 'case when C.COLUMN_KEY=\'PRI\' then 1 else 0 end as \'pk\' ' + ' from INFORMATION_SCHEMA.tables T ' + ' JOIN INFORMATION_SCHEMA.columns C ON C.table_schema=T.table_schema and C.table_name=T.table_name ' + ' where T.table_schema=\'' + that.opt.database + '\' and T.table_name=\'' + tableName + '\'' ) .then(function (result) { if (result.length === 0) { res.reject('Table named ' + tableName + ' does not exist in ' + that.server + ' - ' + that.database); return; } var isDbo = (result[0].dbo !== 0), xType; if (result[0].xtype.trim() === 'U') { xType = 'T'; } else { xType = 'V'; } _.forEach(result, function (col) { delete col.dbo; delete col.xtype; }); res.resolve({tableName: tableName, xtype: xType, isDbo: isDbo, columns: result}); }, function (err) { res.reject(err); }); return res.promise(); };