Example #1
0
    constructor(source, defer, initFn) {
      super(source, defer);

      this.type = 'segmented';

      // segmented request specific state
      this._initFn = initFn;

      this._desiredSize = null;
      this._maxSegments = config.get('courier:maxSegmentCount');
      this._direction = 'desc';
      this._sortFn = null;
      this._queueCreated = false;
      this._handle = new SegmentedHandle(this);

      this._hitWindow = null;

      // prevent the source from changing between requests,
      // all calls will return the same promise
      this._getFlattenedSource = _.once(this._getFlattenedSource);
    }
Example #2
0
describe('integration', () => {
  const getGraphqlSchema = once(() => createGraphqlSchema(PG_CONFIG, SCHEMA_NAME))

  readdirSync(TEST_FIXTURES).forEach(fileName => {
    if (path.extname(fileName) === '.graphql') {
      const name = path.basename(fileName, '.graphql')

      it(name, async () => {
        const graphqlSchema = await getGraphqlSchema()
        const graphQLPath = path.resolve(TEST_FIXTURES, fileName)
        const jsonPath = path.resolve(TEST_FIXTURES, `${name}.json`)
        const expectedData = JSON.parse(readFileSync(jsonPath, 'utf8'))
        const client = await pg.connectAsync(PG_CONFIG)

        // Begin a block.
        await client.queryAsync('begin')

        const data = await graphql(
          graphqlSchema,
          readFileSync(graphQLPath),
          null,
          { client }
        )

        // End the block by rolling it back so mutations are not saved.
        await client.queryAsync('rollback')

        // Make sure to release our client!
        client.end()

        /* eslint-disable no-console */
        if (data.errors)
          data.errors.forEach(error => console.error(error.stack))
        /* eslint-enable no-console */

        assert.deepEqual(data, expectedData)
      })
    }
  })
})
Example #3
0
exports.open = function(callback) {
  callback = callback || function(error, results) {
    if (error)
      console.log("ERROR opening SPI:", error);
    else
      console.log("READY: " + results);
  };
  SPI = fs.createWriteStream((process.env.NO_SPI ? '/dev/null' : '/dev/spidev0.0'), {flags: 'w', encoding: 'ascii'});
  callback = _.once(callback || function() {});
  SPI.once('open', function() {
    SPI.write(RESET); // start it
    callback(null, 'lightstrip ready', SPI);
  });
  SPI.once('error', function(e) {
    callback(e);
  });
  SPI.on('error', function(e) {
    console.log("lightstrip: SPI error!", e.toString());
  });
  SPI.on('drain', function(e) {
    console.log('lightstrip: SPI buffer drained.  Things can start writing again.');
    waitForDrain = false;
  });

  // We're going to proxy the WriteStream's .write method with a function that basically ignores (drops) additional
  // write requests until the frame or buffer backpressure subsides.
  var realWrite = SPI.write;
  SPI.write = function writeProxy(buffer, callback) {
    if (skippingFrame || waitForDrain)
      return false;

    var completed = realWrite.call(SPI, buffer, callback);
    if (!completed) {
      skippingFrame = true; // ignore future pixel writes until backpressure is resolved
      waitForDrain = true;  // unlocked only on the drain event.
      console.log('lightstrip: SPI buffer filled.  Ignoring everything until buffer drained!');
    }
    return completed;
  };
};
Example #4
0
  constructor(settings) {
    this.name = pkg.name;
    this.version = pkg.version;
    this.build = pkg.build || false;
    this.rootDir = rootDir;
    this.settings = settings || {};

    this.ready = constant(this.mixin(
      require('./config/setup'), // sets this.config, reads this.settings
      require('./http'), // sets this.server
      require('./logging'),
      require('./status'),

      // find plugins and set this.plugins
      require('./plugins/scan'),

      // tell the config we are done loading plugins
      require('./config/complete'),

      // setup this.uiExports and this.bundles
      require('../ui'),

      // ensure that all bundles are built, or that the
      // lazy bundle server is running
      require('../optimize'),

      // finally, initialize the plugins
      require('./plugins/initialize'),

      () => {
        if (this.config.get('server.autoListen')) {
          this.ready = constant(resolve());
          return this.listen();
        }
      }
    ));

    this.listen = once(this.listen);
  }
Example #5
0
gulp.task('watch', function () {
  var doWatch = _.once(function () {
    // separate watcher for browserification
    gulp.watch('./client/**', [ 'browserify' ]);
    // watcher for restarting app
    var nodemonOpts = { 
      script: 'index.js',
      ext: 'js hbs',
      env: { 'NODE_ENV': 'development' },
      ignore: [
        './node_modules/**',
        './client/**'
      ], 
      nodeArgs: ['--debug'] 
    };
    nodemon(nodemonOpts)
      .on('restart', function () {
        gutil.log('watcher restarted server');
      });
  });
  gulp.start('clean', 'bootstrap-copy', 'browserify', doWatch);
});
Example #6
0
    QUnit.test('`lodash ' + command +'`', function(assert) {
      var counter = 0,
          dirs = _.includes(command, 'c.js'),
          done = assert.async(),
          expected = /(\w+)(?=\.js$)/.exec(command)[0];

      var start = _.after(2, _.once(function() {
        if (dirs) {
          fs.rmrfSync(outputPath);
        }
        process.chdir(cwd);
        done();
      }));

      process.chdir(__dirname);

      build(command.split(' '), function(data) {
        var basename = path.basename(data.outputPath, '.js');
        assert.strictEqual(basename, expected + (counter++ ? '.min' : ''), command);
        start();
      });
    });
Example #7
0
	function(chunk, enc, cb) {

		const cbOnce = _.once(cb);

		try {

			request({
				url: config.stateServerUrl,
				qs: { apiKey: config.apiKey },
				json: true,
				method: 'POST',
				body: formattedPacket
			})
			.then(result => console.log('request result: ' + result) && result)
			.then(cbOnce);

		} catch (e) {
			console.error('Error saving to DB');
			console.error(e);
			cbOnce();
		}
	},
Example #8
0
  var defaultListeners = function(child, args, callback) {
    callback = callback || function(err) { console.error(err); };
    callback = _.once(callback);

    if (args.encodingStderr !== 'buffer') child.stderr.setEncoding(args.encodingStderr || 'utf8');

    if (args.encodingStdout) child.stdout.setEncoding(args.encodingStdout);
    //else if (args.printStdout && !args.encodingStdout) child.stdout.setEncoding('utf8');

    //if (args.printStdout) child.stdout.pipe(process.stdout);

    if (args.encodingStderr !== 'buffer') child.stderr.pipe(process.stderr);

    //child.stdin.on('finish', function() {});
    child.stdin.on('error', function(err) {
      callback(err);
    });

    child.on('error', function(err) {
      callback(err);
    });
  };
Example #9
0
    QUnit.test('`lodash ' + command +'`', function(assert) {
      var done = assert.async(),
          start = _.after(2, _.once(done));

      build(command.split(' '), function(data) {
        var actualId,
            basename = path.basename(data.outputPath, '.js'),
            context = createContext('amd');

        context.define = function(id, factory) {
          actualId = id;
          context._ = factory();
        };

        context.define.amd = {};
        vm.runInContext(data.source, context);

        assert.strictEqual(actualId, expectedId, basename);
        assert.ok(_.isFunction(context._), basename);
        start();
      });
    });
Example #10
0
 function run(task) {
     running += 1;
     task(_.once(function (err, data) {
         if (err) {
             console.error(err);
             failures.push(err);
         } else {
             results.push(data);
         }
         bar.tick();
         waiting -= 1;
         running -= 1;
         if (waiting <= 0) {
             // console.log('fin!', failures, results);
             console.log('Took', (Date.now() - start)/1000/60, 'minutes');
             return done(failures, results);
         }
         process.nextTick(function () {
             advance();
         });
     }));
 }
Example #11
0
var createGetRemixWhiteboardActivities = module.exports.createGetRemixWhiteboardActivities = function(ctx, asset, metadata, callback) {
  var done = _.after(asset.users.length, callback);
  var errorCallback = _.once(callback);
  _.each(asset.users, function(user) {
    // If the user is among the asset creators, skip activity creation for that user only.
    if (user.id === ctx.user.id) {
      return done();
    }

    ActivitiesAPI.createActivity(ctx.course, user, 'get_remix_whiteboard', asset.id, CollabosphereConstants.ACTIVITY.OBJECT_TYPES.ASSET, metadata, ctx.user, function(err) {
      if (err) {
        log.error({
          'err': err,
          'user': user.id,
          'asset': asset.id
        }, 'Failed to create a get_remix_whiteboard activity');
        return errorCallback(err);
      }
      return done();
    });
  });
};
Example #12
0
  setupWatching(extraPaths, extraIgnores) {
    const chokidar = require('chokidar');
    const { fromRoot } = require('../../utils');

    const watchPaths = [
      fromRoot('src/core_plugins'),
      fromRoot('src/server'),
      fromRoot('src/ui'),
      fromRoot('src/utils'),
      fromRoot('x-pack/common'),
      fromRoot('x-pack/plugins'),
      fromRoot('x-pack/server'),
      fromRoot('x-pack/webpackShims'),
      fromRoot('config'),
      ...extraPaths
    ].map(path => resolve(path));

    this.watcher = chokidar.watch(uniq(watchPaths), {
      cwd: fromRoot('.'),
      ignored: [
        /[\\\/](\..*|node_modules|bower_components|public|__[a-z0-9_]+__|coverage)[\\\/]/,
        /\.test\.js$/,
        ...extraIgnores
      ]
    });

    this.watcher.on('add', this.onWatcherAdd);
    this.watcher.on('error', this.onWatcherError);

    this.watcher.on('ready', once(() => {
      // start sending changes to workers
      this.watcher.removeListener('add', this.onWatcherAdd);
      this.watcher.on('all', this.onWatcherChange);

      this.log.good('watching for changes', `(${this.addedCount} files)`);
      this.startCluster();
    }));
  }
Example #13
0
function prodWorker(done) {
    const callback = _.once(done);

    if (!worker) {
        return process.nextTick(() => callback('noworker'));
    }

    const timeout = setTimeout(() => callback('timeout'), 1000);

    onWorkerReady(() => {
        clearTimeout(timeout);
        // send a message to the worker to start bundling js
        worker.send('production');
        worker.on('message', message => {
            if (message === 'done') {
                worker.kill();
                callback();
            }
        });
    });

    worker.on('close', () => callback('noworker'));
}
Example #14
0
Utils.parseBody = function(stream, options, callback) {

  callback = _.once(callback);
  var data = '';

  stream.setEncoding('utf8');

  stream.on('data', function(str) {
    data += str;
  });

  stream.on('error', function(err) {
    callback(err);
  });

  stream.on('end', function() {
    utils.JSON.parse(data, options, function(err, request) {
      if(err) return callback(err);
      callback(null, request);
    });
  });

};
	checkEmptyState = (() => {
		const showNotification = _.debounce(_.once(() => {
			const info = $('<p>').text(i18n('filteRedditEmptyNotificationInfo'));
			const toggle = $('<button>').text(i18n('filteRedditEmptyNotificationToggleShowReason'))
				.click(() => { this.toggleDisplayReasons(); });

			Notifications.showNotification({
				moduleID: FilteReddit.module.moduleID,
				notificationID: 'everyThingHidden',
				header: i18n('filteRedditEmptyNotificationHeader'),
				message: $('<div>').append(info).append(toggle).get(0),
				closeDelay: Infinity,
			});
		}), 3000);

		return () => {
			if (Array.from(this.things).filter(v => v.isVisible()).length) {
				showNotification.cancel();
			} else if (!this.displayReasons) {
				showNotification();
			}
		};
	})();
Example #16
0
    QUnit.test('`lodash exports=amd' + (command ? ' ' + command + '`' : '` using the default `moduleId`'), function(assert) {
      var done = assert.async(),
          start = _.after(2, _.once(done));

      build(['template=' + path.join(templatePath, '*.jst'), 'exports=amd'].concat(command || []), function(data) {
        var actualId,
            basename = path.basename(data.outputPath, '.js'),
            context = createContext('amd');

        context.define = function(requires, factory) {
          factory(_);
          actualId = requires[0];
        };

        context.define.amd = {};
        vm.runInContext(data.source, context);

        assert.strictEqual(actualId, expectedId, basename);

        delete _.templates;
        start();
      });
    });
Example #17
0
  constructor(uiExports, spec) {
    this.uiExports = uiExports;
    this.spec = spec || {};

    this.id = this.spec.id;
    if (!this.id) {
      throw new Error('Every app must specify it\'s id');
    }

    this.main = this.spec.main;
    this.title = this.spec.title;
    this.description = this.spec.description;
    this.icon = this.spec.icon;
    this.hidden = this.spec.hidden;
    this.autoloadOverrides = this.spec.autoload;
    this.templateName = this.spec.templateName || 'uiApp';

    // once this resolves, no reason to run it again
    this.getModules = _.once(this.getModules);

    // variables that are injected into the browser, must serialize to JSON
    this.getInjectedVars = this.spec.injectVars || _.noop;
  }
Example #18
0
  QUnit.test('`lodash modularize include=partial exports=node`', function(assert) {
    var done = assert.async();

    var start = _.once(function() {
      process.chdir(cwd);
      done();
    });

    setup();

    build(['modularize', 'include=partial', 'exports=node', '-o', outputPath], function() {
      emptyObject(require.cache);

      var modulePath = path.join(outputPath, 'partial');

      var lodash = {};
      lodash.partial = require(modulePath);

      assert.deepEqual(lodash.partial.placeholder, {}, 'should have a `placeholder` property');
      testMethod(assert, lodash, 'partial');
      start();
    });
  });
	checkEmptyState = (() => {
		const showNotification = _.debounce(_.once(() => {
			const info = $('<p>').text(i18n('filteRedditEmptyNotificationInfo'));
			const toggle = $('<button>').text(i18n('filteRedditEmptyNotificationToggleShowReason'))
				.click(() => { this.toggleShowFilterReason(); });

			Notifications.showNotification({
				moduleID: FilteReddit.module.moduleID,
				notificationID: 'everyThingHidden',
				header: i18n('filteRedditEmptyNotificationHeader'),
				message: $('<div>').append(info).append(toggle).get(0),
				closeDelay: Infinity,
			});
		}), 4000);

		return _.throttle(() => {
			if (this.getFiltered().length !== this.things.size) {
				showNotification.cancel();
			} else if (!this.showFilterReason) {
				showNotification();
			}
		}, 100, { leading: false });
	})();
Example #20
0
Utils.parseStream = function(stream, options, onRequest) {

  var onError = _.once(onRequest);
  var onSuccess = _.partial(onRequest, null);

  var result = JSONStream.parse();

  result.on('data', function(data) {

    // apply reviver walk function to prevent stringify/parse again
    if(_.isFunction(options.reviver)) {
      data = Utils.walk({'': data}, '', options.reviver);
    }

    onSuccess(data);
  });

  result.on('error', onError);
  stream.on('error', onError);

  stream.pipe(result);

};
Example #21
0
 return function (callback) {
   var filename = browser + '-' + name + '.png';
   var target = path.join(dict.get('currentPath'), filename);
   var driver = wd.remote();
   callback = _.once(callback);
   driver.init({browserName: browser}, function (err) {
     if (err) {
       return callback(err);
     }
     driver.get(url, function (err) {
       if (err) {
         return callback(err);
       }
       driver.saveScreenshot(target, function (err) {
         if (err) {
           return callback(err);
         }
         driver.quit();
         callback();
       });
     });
   });
 };
Example #22
0
    next(null, function(flowScope, message, content, cb) {

        debug('Running uploadToS3 middleware')

        var done = _.once(cb)

        if (!flowScope.qs3.hasOwnProperty('s3id')) return cb(new Error('An s3 id is required'))

        debug('Uploading %d bytes to bucket: %s and key: %s', message.content.length, config.bucket, flowScope.qs3.s3id)

        s3Client.putObject({
            Bucket: config.bucket,
            Key: flowScope.qs3.s3id,
            Body: message.content,
            ContentDisposition: message.properties.headers.contentDisposition,
            ContentType: message.properties.contentType
        }, function(err) {
            err ? done(recoverable(err)) : done()
        }).on('error', function(err) {
            debug('Error uploading message to bucket: %s and key: %s', config.bucket, flowScope.qs3.s3id)
            done(recoverable(err))
        })
    })
Example #23
0
  QUnit.test('module aliases for `exports=node`', function(assert) {
    var done = assert.async();

    var start = _.once(function() {
      process.chdir(cwd);
      done();
    });

    setup();

    build(['modularize', 'exports=node', '-o', outputPath], function() {
      emptyObject(require.cache);

      var lodash = require(outputPath);

      _.each(categories, function(category) {
        var funcNames = mapping.category[_.capitalize(category)],
            object = require(path.join(outputPath, category));

        _.each(funcNames, function(funcName) {
          var aliases = _.without(getAliases(funcName), 'wrapperLodash');
          _.each(aliases, function(alias) {
            var objects = [(category == 'seq' ? lodash.prototype : lodash), object];
            _.each(objects, function(object, index) {
              var value = (!index && alias == 'toIterator')
                ? object[Symbol.iterator]
                : object[alias];

              assert.ok(_.isFunction(value), '`' + alias + '` is an alias of `' + funcName + '`');
            });
          });
        });
      });

      start();
    });
  });
Example #24
0
 return function(req, res, next){
   next = _.once(next);
   var cbf = function(err, data){
     if(err){
       next(err);
       return;
     }
     if(res.headersSent){
       err = new Error('the header has been sent');
       err.msg = '该请求已经发送';
       console.error(err);
       return;
     }
     if(data){
       if(template){
         res.locals.TEMPLATE = template;
         render(res, template, data, next);
       }else{
         if(_.isObject(data)){
           json(res, data, next);
         }else{
           response(res, data, next);
         }
       }
     }else{
       res.send('');
     }
   };
   var d = domain.create();
   d.on('error', function(err){
     next(err);
   });
   d.run(function(){
     handler(req, res, cbf);
   });
   
 };
/**
 * Load msgs not send and send it back or throw it do deadQueue
 */
function processFailures() {
  var now = Date.now();
  var cb = _.once(function () {
    setTimeout(processFailures, properties.sendTimeout + 1000); // 1000 is to avoid running again before all responses were processed
  });

  var stream = db.createReadStream({end: {time: now - properties.sendTimeout}});

  stream.on('data', function (data) {
    if (queueConcurrentSend > properties.maxQueueConcurrentSend) {
      stream.pause();
    }

    if ((data.value.sendAt + properties.sendTimeout) < now) { // if we need to re-send
      if (data.key.time + properties.maxTimeout < now) { // too old, send to dead queue
        queueConcurrentSend++;
        internalSend(cid, properties.deadQueue, data.key, data.value.msg, function () {
          queueConcurrentSend--;
          if (queueConcurrentSend === (properties.maxQueueConcurrentSend/2)) stream.resume();
        });
      } else {
        internalSend(data.value.msg.from, data.value.msg.to, data.key, data.value.msg, function () {
          queueConcurrentSend--;
          if (queueConcurrentSend === (properties.maxQueueConcurrentSend/2)) stream.resume();
        });
      }
    }
  });

  stream.on('error', function (err) {
    logger.warn('Read stream error : ', err);
    cb();
  });

  stream.on('end', cb);
  stream.on('close', cb);
}
  return (options, callback) => {
    options = options || {};

    if (!options.appGuid && options.name && api.actualDeploymentConfig) {
      var a = _.find(api.actualDeploymentConfig.apps, { name: options.name });
      if (a) {
        options.appGuid = a.guid;
      }
    }

    if (!options.appGuid) {
      return callback(new Error('Please provide an appGuid! \n' + JSON.stringify(options, null, 2)));
    }

    options.startTimeout = options.startTimeout || 120; //seconds

    var timer;

    const clb = _.once((err, res) => {
      clearTimeout(timer);
      if (err) return callback(err);
      callback(null, res);
    });

    timer = setTimeout(() => {
      clb(new Error(`Starting for app ${options.name || options.appGuid} took longer than ${options.startTimeout} seconds!`));
    }, 1000 * options.startTimeout);
    options.onReady = () => {
      api.startApp({ appGuid: options.appGuid, name: options.name }, (err) => {
        if (err) {
          debug(err);
          api.startApp({ appGuid: options.appGuid, name: options.name }, _.noop);
        }
      });
    };
    api.tailAppLogsAndWaitFor(options, clb);
  };
Example #27
0
var attrs = function() {

  // we need to iterate the class hierarchy ourselves because we've defined
  // specific requirements around the preferring attributes that are defined
  // later. _.keysIn and for/in will not get us the desired order.
  var hierarchy = function(cls) { // jscs:ignore jsDoc
    var result = [];
    while (cls) { result.unshift(cls); cls = cls.__super__; }
    return result;
  };

  // return the function with the value locked in
  return _.once(function() {
    var regex = /(.*)Attr$/;
    return _(hierarchy(this))
      .map('__class__.prototype') // get all prototypes
      .map(_.keys).flatten() // get all keys
      .invoke('match', regex).map(0).filter() // find attrs
      .reverse() // reverse to prefer later attrs in unique
      .uniq(_.propertyOf(this.__class__.prototype)) // unique based on dbattr
      .invoke('match', regex).map(1) // get the real attr name
      .reverse().value(); // return to normal order
  });
};
Example #28
0
    QUnit.test('`lodash settings=...' + (command ? ' ' + command : '') + '`', function(assert) {
      var done = assert.async(),
          start = _.after(2, _.once(done));

      build(['template=' + path.join(templatePath, '*.tpl'), 'settings={interpolate:/{{([\\s\\S]+?)}}/}'].concat(command || []), function(data) {
        var actualId,
            basename = path.basename(data.outputPath, '.js'),
            context = createContext('amd');

        context.define = function(requires, factory) {
          factory(_);
          actualId = requires[0];
        };

        context.define.amd = {};
        vm.runInContext(data.source, context);

        assert.strictEqual(actualId, expectedId, basename);
        assert.strictEqual(_.templates.f({ 'name': 'mustache' }), 'hall\xe5 mustache!', basename);

        delete _.templates;
        start();
      });
    });
Example #29
0
  return fromNode(cb => {
    const timeout = setTimeout(() => {
      cb(new Error('Server timedout waiting for the optimizer to become ready'));
    }, config.get('optimize.lazyProxyTimeout'));

    const waiting = once(() => {
      server.log(['info', 'optimize'], 'Waiting for optimizer completion');
    });

    if (!process.connected) return;

    process.send(['WORKER_BROADCAST', { optimizeReady: '?' }]);
    process.on('message', (msg) => {
      switch (get(msg, 'optimizeReady')) {
        case true:
          clearTimeout(timeout);
          cb();
          break;
        case false:
          waiting();
          break;
      }
    });
  });
Example #30
0
importObj.getStream = function(source, callback) {
    var stream, response, callback = _.once(callback);
    stream = response = needle.get(source.url, {
        follow_max: 4, open_timeout: 5000,
        headers: { "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36" }
    }).on("headers", function(headers) {
        var filename = headers["content-disposition"] || url.parse(source.url).pathname;
        var detectedType = "generic";
        if (headers["content-type"] && headers["content-type"].match("xml")) detectedType = "xmlRss";
        if (headers["content-type"] && headers["content-type"].match("json")) detectedType = "json";
        if (filename.match(".txt.gz$")) detectedType = "dump";

        if (["json", "xmlRss"].indexOf(detectedType) == -1) stream = stream.pipe(gunzip()).pipe(gunzip()); // Some sources can be gunzipped twice (one for request, another for being a .txt.gz)

        stream.on("end", function() { response.end() }); // make sure response is closed

        callback(null, stream, detectedType);
    }).on("error", function(e) { callback(e) })
    .on("end", function() { 
        // TODO: we can check statusCode / etc?
        callback(new Error("empty response / couldn't detect type"));
		importObj.emit('linkError', source);
    })
};