<commit_before>// include closure long.js on the page
var goog = { provide: function () { }, math: { } };
var fs = require('fs');
eval(fs.readFileSync('external/long.js') + '');

// include sha256.js on the page
eval(fs.readFileSync('external/jssha256.js') + '');

// include the (external) curve25519 library
eval(fs.readFileSync('external/curve255.js') + '');

// include the curve25519 library
eval(fs.readFileSync('core/curve25519.js') + '');

// include the crypto library
eval(fs.readFileSync('core/crypto.debug.js') + '');

// export functions via node
exports.curve25519 = curve25519;
exports.crypto = nxtCrypto;<commit_msg>nodejs bootstrap should not refer to any debug files<commit_after>// include closure long.js on the page
var goog = { provide: function () { }, math: { } };
var fs = require('fs');
eval(fs.readFileSync('external/long.js') + '');

// include sha256.js on the page
eval(fs.readFileSync('external/jssha256.js') + '');

// include the (external) curve25519 library
eval(fs.readFileSync('external/curve255.js') + '');

// include the curve25519 library
eval(fs.readFileSync('core/curve25519.js') + '');

// include the crypto library
eval(fs.readFileSync('core/crypto.js') + '');

// export functions via node
exports.curve25519 = curve25519;
exports.crypto = nxtCrypto;<|endoftext|>


<commit_before>/*jslint eqeq: true*/
/*jshint strict: false*/
/*global configure, group, beforeEachTest, asyncBeforeEachTest, afterEachTest, asyncAfterEachTest, test, asyncTest, whenAsyncDone, -getUiTestContainerElement, -getUiTestContainerElementId, proxy, equal, notEqual, isTrue, isFalse, isTruthy, isNotTruthy*/

configure({
    name: 'Sample Test Suite',
    hidePassedTests: true
});

group('How failed tests are reported', function(){
    test('This test will intentionally fail', function(){
        isTrue(false, 'false is true');
    });
});

group('How passed tests are reported', function(){
    test('Hello World!', function(){
        var hw = 'Hello World!';
        isTrue(hw === 'Hello World!', 'Yes, it works!');
    });
});

group('Boolean assertions', function(){
    test('true', function(){
        isTrue(true, 'true is true');
    });
    test('false', function(){
        isFalse(false, 'false is false');
    });
});

group('Truthy assertions', function(){
    var undef;
    var def = {};
    var one = 1;
    var zero = 0;
    test('When using truthy boolean evaluation', function(){
        isNotTruthy(undef, 'undefined is not truthy');
        isTruthy(def, 'a valid reference to an object is truthy');
        isTruthy(one, '1 is truthy');
        isNotTruthy(zero, '0 is not truthy');
        isTruthy('1' == 1, '"1" == 1 is truthy');
    });
});

group('Strict deep recursive comparison assertions', function(){
    test('Does a equal b', function(){
        var char = 'b';
        var a = {a: 'a', b: 'b'};
        var b = {a: 'a', b: b};
        var c = {a: 'a', b: 'b'};
        var three = 3;
        notEqual(a, b, 'var a and var b are not equal.');
        equal(a, c, 'var a and var c are equal');
        equal(char, 'b', 'var char and "b" are equal');
        equal(3, three, '3 and var three are equal');
    });
});

group('Synchronous test with "beforeEachTest"', function(){
    var count = 0;
    beforeEachTest(function(){
        count = 100;
    });
    test('count is 100', function(){
        equal(count, 100, 'count equals 100');
    });
});

group('Passing a value from beforeEachTest to test', function(){
    beforeEachTest(function(valObj){
        valObj.value = 10;
    });
    test('the test can access the value', function(valObj){
        equal(valObj.value, 10, 'value equals 10');
    });
});

group('Synchronous tests with "afterEachTest"', function(){
    var count = 0;
    afterEachTest(function(){
        count = 100;
    });
    test('count is 0', function(){
        equal(count, 0, 'count equals 0.');
    });
    test('count is 100', function(){
        equal(count, 100, 'count equals 100.');
    });
});

group('Asynchronous test', function(){
    var count = 0;
    asyncTest('count is 100', 1, function(){
        setTimeout(function(){
            count = 100;
        }, 1);
        whenAsyncDone(function(){
            equal(count, 100, 'count equals 100');
        });
    });
});

group('Asynchronous tests with "asyncBeforeEachTest"', function(){
    var count = 0;
    asyncBeforeEachTest(1, function(){
        setTimeout(function(){
            count = 10;
        }, 1);
    });
    asyncTest('count is 100', 1, function(){
        setTimeout(function(){
            count *= 10;
        }, 1);
        whenAsyncDone(function(){
            equal(count, 100, 'count equals 100');
        });
    });
    asyncTest('count is 20', 1, function(){
        setTimeout(function(){
            count *= 2;
        }, 1);
        whenAsyncDone(function(){
            equal(count, 20, 'count x 2 = 20');
        });
    });
});

group('Passing a value from asyncBeforeEachTest to asyncTest using', function(){
    asyncBeforeEachTest(1, function(valObj){
        setTimeout(function(){
            valObj.value = 10;
        }, 1);
    });
    asyncTest('the asyncTest can access the value', 1, function(valObj){
        setTimeout(function(){
            //some asynchronous process...
        }, 1);
        whenAsyncDone(function(){
            equal(valObj.value, 10, 'value equals 10');
        });
    });
});

group('2 asynchronous tests with "asyncAfterEachTest"', function(){
    var count = 0;
    asyncAfterEachTest(1, function(){
        setTimeout(function(){
            count = 1;
        }, 1);
    });
    asyncTest('count is 10', 1, function(){
        setTimeout(function(){
            count = 10;
        }, 1);
        whenAsyncDone(function(){
            isTrue(count === 10, 'count equals 10');
        });
    });
    asyncTest('count is 100', 1, function(){
        setTimeout(function(){
            count *= 100;
        }, 1);
        whenAsyncDone(function(){
            isTrue(count === 100, 'Yes, count now is 100');
        });
    });
});

group('proxy captures calling information which can be tested against', function(){
    test('proxy a function calling it twice', function(){
        var fn = proxy(function(s){
            return s;
        });
        fn('Somewhere over');
        fn('the rainbow');
        isTrue(fn.wasCalled(2), 'fn.wasCalled(2) returned true.');
        equal(fn.getCalledCount(), 2, 'fn.getCalledCount() returned 2.');
        var info1 = fn.getData(0);
        isTrue(info1.argsPassed[0] === 'Somewhere over', 'fn was passed "Somewhere over"');
        isTrue(info1.returned === 'Somewhere over', 'fn returned "Somewhere over"');
        isTrue(info1.context === undefined, 'fn was called the 1st time with the global context');
        var info2 = fn.getData(1);
        isTrue(info2.argsPassed[0] === 'the rainbow', 'fn was passed "the rainbow"');
        isTrue(info2.returned === 'the rainbow', 'fn returned "Somewhere over"');
        isTrue(info2.context === undefined, 'fn was called the 2nd time with the global context');
    });
    test('proxy two functions calling each once', function(){
        var fn1 = proxy(function(s){
            return s;
        });
        var fn2 = proxy(function(s){
            return s;
        });
        fn1('Somewhere over ');
        fn2('the rainbow');
        isTrue(fn1.wasCalled(1), 'fn1.wasCalled(1) returned true.');
        equal(fn1.getCalledCount(), 1, 'fn1.getCalledCount() returned 1.');
        isTrue(fn2.wasCalled(1), 'fn2.wasCalled(1) returned true.');
        equal(fn2.getCalledCount(), 1, 'fn2.getCalledCount() returned 1.');
        var fn1Info = fn1.getData(0);
        isTrue(fn1Info.argsPassed[0] === 'Somewhere over ', 'fn1 was passed "Somewhere over "');
        isTrue(fn1Info.returned === 'Somewhere over ', 'fn1 returned "Somewhere over "');
        isTrue(fn1Info.context === undefined, 'fn1 was called with the global context');
        var fn2Info = fn2.getData(0);
        isTrue(fn2Info.argsPassed[0] === 'the rainbow', 'fn2 was passed "the rainbow"');
        isTrue(fn2Info.returned === 'the rainbow', 'fn2 returned "the rainbow"');
        isTrue(fn2Info.context === undefined, 'fn2 was called with the global context');
    });
    test('proxy a property method calling it twice', function(){
        var foo = {
            title: '',
            name: proxy(function(s){this.title += s; return s;})
        };
        proxy(foo, 'name');
        foo.name('Somewhere over ');
        foo.name('the rainbow');
        equal(foo.title, 'Somewhere over the rainbow', 'foo.title = "Somewhere over the rainbow"');
        isTrue(foo.name.wasCalled(2), 'foo.name.wasCalled(2) returned true.');
        equal(foo.name.getCalledCount(), 2, 'foo.name.getCalledCount() returned 2.');
        var info1 = foo.name.getData(0);
        isTrue(info1.argsPassed[0] === 'Somewhere over ', 'foo.name was passed "Somewhere over"');
        isTrue(info1.returned === 'Somewhere over ', 'foo.name returned "Somewhere over "');
        isTrue(info1.context === foo, 'foo.name was called the 1st time with the foo context');
        var info2 = foo.name.getData(1);
        isTrue(info2.argsPassed[0] === 'the rainbow', 'foo.name was passed "the rainbow"');
        isTrue(info2.returned === 'the rainbow', 'foo.name returned "the rainbow"');
        isTrue(info2.context === foo, 'foo.name was called the 2nd time with the foo context');
    });
});
<commit_msg>Making tests read like sentences.<commit_after>/*jslint eqeq: true*/
/*jshint strict: false*/
/*global configure, group, beforeEachTest, asyncBeforeEachTest, afterEachTest, asyncAfterEachTest, test, asyncTest, whenAsyncDone, -getUiTestContainerElement, -getUiTestContainerElementId, proxy, equal, notEqual, isTrue, isFalse, isTruthy, isNotTruthy*/

configure({
    name: 'Sample Test Suite',
    hidePassedTests: true
});

group('When runnig a test', function(){
    test('and it passes', function(){
        var hw = 'Hello World!';
        isTrue(hw === 'Hello World!', 'it looks like this');
    });
    test('and it fails', function(){
        isTrue(false, 'it looks like this');
    });
});

group('When evaluating boolean assertions', function(){
    test('bollean true', function(){
        isTrue(true, 'is true');
    });
    test('boolean false', function(){
        isFalse(false, 'is false');
    });
});

group('When evaluating truthy assertions', function(){
    test('undefined', function(){
        var undef;
        isNotTruthy(undef, 'is not truthy');
    });
    test('objects', function(){
        var def = {};
        isTruthy(def, 'are truthy');
    });
    test('numeric values other than 0', function(){
        var one = 1;
        isTruthy(one, 'are truthy');
    });
    test('numeric vaules that are 0', function(){
        var zero = 0;
        isNotTruthy(zero, 'are not truthy');
    });
    test('non empty strings', function(){
        isTruthy('not empty string', 'are truthy');
    });
    test('empty strings', function(){
        isNotTruthy('', 'are not truthy');
    });
});

group('When evaluating strict, deep recursive comparison assertions', function(){
    var char = 'b';
    var a = {a: 'a', b: 'b'};
    var b = {a: 'a', b: b};
    var c = {a: 'a', b: 'b'};
    test('2 objects with exactly the same properties and property values', function(){
        equal(a, c, 'are equal');
    });
    test('2 objects with different properties or property values', function(){
        notEqual(a, b, 'are not equal');
    });
    test('2 value types whose values are the same', function(){
        equal(char, 'b', 'are equal');
    });
    test('2 value types whose values are  not the same', function(){
        notEqual(char, 'a', 'are not equal');
    });
});

group('When running a synchronous test with beforeEachTest', function(){
    var count = 0;
    beforeEachTest(function(){
        count = 100;
    });
    test('beforeEachTest is called', function(){
        equal(count, 100, 'before test is called');
    });
});

group('When passing a value from beforeEachTest to test', function(){
    beforeEachTest(function(valObj){
        valObj.value = 10;
    });
    test('the test', function(valObj){
        equal(valObj.value, 10, 'can access the value');
    });
});

group('When running a synchronous test with afterEachTest', function(){
    var count = 0;
    afterEachTest(function(){
        count = 100;
    });
    test('the first test', function(){
        equal(count, 0, 'is not afftected');
    });
    test('but subsequent tests', function(){
        equal(count, 100, 'are affected');
    });
});

group('When running an asynchronous test', function(){
    var count = 0;
    asyncTest('calling whenAsyncDone', 1, function(){
        setTimeout(function(){
            count = 100;
        }, 1);
        whenAsyncDone(function(){
            equal(count, 100, 'causes the whenAsyncDone callback function to be called when the asynchronous test has ended');
        });
    });
});

group('When running an asynchronous tests with asyncBeforeEachTest', function(){
    var count = 0;
    asyncBeforeEachTest(1, function(){
        setTimeout(function(){
            count = 10;
        }, 1);
    });
    asyncTest('asyncBeforeEachTest is called', 1, function(){
        setTimeout(function(){
            count *= 10;
        }, 1);
        whenAsyncDone(function(){
            equal(count, 100, 'before asyncTest is called ');
        });
    });
});

group('When passing a value from asyncBeforeEachTest to asyncTest', function(){
    asyncBeforeEachTest(1, function(valObj){
        setTimeout(function(){
            valObj.value = 10;
        }, 1);
    });
    asyncTest('the asyncTest', 1, function(valObj){
        setTimeout(function(){
            //some asynchronous process...
        }, 1);
        whenAsyncDone(function(){
            equal(valObj.value, 10, 'can access the value');
        });
    });
});

group('When running an asynchronous tests with asyncAfterEachTest', function(){
    var count = 0;
    asyncAfterEachTest(1, function(){
        setTimeout(function(){
            count = 1;
        }, 1);
    });
    asyncTest('the first asyncTest', 1, function(){
        setTimeout(function(){
            count = 10;
        }, 1);
        whenAsyncDone(function(){
            isTrue(count === 10, 'is not affected');
        });
    });
    asyncTest('but subsequent asyncTests', 1, function(){
        setTimeout(function(){
            count *= 100;
        }, 1);
        whenAsyncDone(function(){
            isTrue(count === 100, 'are affected');
        });
    });
});

group('When using proxy', function(){
    test('on a function and the function is called', function(){
        var fn = proxy(function(){
            return 'JavaScript is amazing!';
        });
        fn('Tell me something about JavaScript');
        isTrue(fn.wasCalled(1), 'we can ask it if it was called "n" times');
        equal(fn.getCalledCount(), 1, 'we can ask it how many times it was called');
        var info1 = fn.getData(0);
        isTrue(info1.argsPassed[0] === 'Tell me something about JavaScript', 'we can find out what arguements it was passed');
        isTrue(info1.returned === 'JavaScript is amazing!', 'we can find out what it returned');
        isTrue(info1.context === undefined, 'we can verify the context that was used');
    });
    test('on a method and the method is called', function(){
        var someObject = {
            someMethod: function(){
                return 'JavaScript is amazing!';
            }
        };
        proxy(someObject, 'someMethod');
        someObject.someMethod('Tell me something about JavaScript');
        isTrue(someObject.someMethod.wasCalled(1), 'we can ask it if it was called "n" times');
        equal(someObject.someMethod.getCalledCount(), 1, 'we can ask it how many times it was called');
        var info1 = someObject.someMethod.getData(0);
        isTrue(info1.argsPassed[0] === 'Tell me something about JavaScript', 'we can find out what arguements it was passed');
        isTrue(info1.returned === 'JavaScript is amazing!', 'we can find out what it returned');
        isTrue(info1.context === someObject, 'we can verify the context that was used');
    });
});
<|endoftext|>


<commit_before>/* global process */
/*global Buffer */
/**
 * @file Report downloader class which provides a single function to download a salesforce report.
 * @author Divya van Mahajan <dvm@vanmahajan.com>
 */
'use strict';

var stringify = require('csv-stringify');
var jsforce = require('jsforce');
var fs = require('fs');
var moment = require('moment');
require('moment-range');

// External inputs
var reportID;
var datefield;
var indexfieldOffset=0;
var StartDate = moment();
var EndDate = moment();

// Internal global state
var OutputFile = 'ReportOutput_' + StartDate.format("YYYY-MM-DD") + '_to_' + EndDate.format("YYYY-MM-DD") + '.csv';
var WAIT_BETWEEN_REQUESTS = 3000; // 3 seconds
var UserName = process.env.SF_USER;
var SFPassToken = process.env.SF_PASSWD_WITH_TOKEN;
var MAX_CONCURRENT = 20;
var i = 1;
var n = 0;
var async_report_requests=0;
var async_report_success=0;
var global_record_count = 0;
var global_written_count = 0;

var conn = new jsforce.Connection();

/**
 * Download the report. Command line wrapper
 * SF_USER - Environment variable with the user name.
 * SF_PASSWD_WITH_TOKEN - Environment variable with the password and security token.
 *
 * Command line:
 * downloadreport reportid datefield indexfieldoffset startdate enddate
 */
module.exports.downloadCommand = function() {
  if (process.argv.length <7 ) {
      console.error('Usage: '+process.argv[0]+' '+process.argv[1]+' reportid datefield indexfieldOffset 2016-01-01 2016-01-05');
      return;
  } else {
    module.exports.downloadreport(process.argv[2],process.argv[3],process.argv[4],
      process.argv[5],process.argv[6],process.env.SF_USER,process.env.SF_PASSWD_WITH_TOKEN);
  }
}

/**
 * Download the report.
 *
 * @protected
 * @param {String} _reportID - Salesforce report identifier. You can get this from the URL when you are viewing the report.
 * @param {String} _datefield - Internal name of the date field used in the standard date filter. << TODO >>
 * @param {String} _indexfieldOffset - Column that should be displayed while running the report (starts at 0). By default the first column is shown.
 * @param {Date} _startDate - Starting date in the format YYYY-MM-DD.
 * @param {Date} _endDate - Ending date in the format YYYY-MM-DD.
 * @param {String} _user - username.
 * @param {String} _password - password with security token.
 * @param {Object} SFOptions - Initialization options for jsforce (see http://jsforce.github.io/jsforce/doc/Connection.html)
 */
module.exports.downloadreport = function(_reportID,_datefield,_indexfieldOffset,_startDate,_endDate,_username,_password,SFOptions) {
  conn = new jsforce.Connection(SFOptions);

  reportID=_reportID;
  datefield=_datefield;
  indexfieldOffset=_indexfieldOffset;
  StartDate=moment(_startDate);
  EndDate=moment(_endDate);
  UserName = _username;
  SFPassToken = _password;

  async_report_requests=0;
  async_report_success=0;
  global_record_count=0;
  global_written_count=0;
  i = 1;
  n = 0;

  OutputFile = 'data/ReportOutput_' + StartDate.format("YYYY-MM-DD") + '_to_' + EndDate.format("YYYY-MM-DD") + '.csv';

  conn.login(UserName, SFPassToken).
      then(function () {
          return conn.identity();
      }, function (err) {
          console.error(err);
          console.error('Please check you have set the following environment variables');
          console.error('SF_USER');
          console.error('SF_PASSWD_WITH_TOKEN=password and security token');
      }).then(function (res) {
          console.log('Logged into Salesforce');
          //console.log("organization ID: " + res.organization_id);
          //console.log("user ID: " + res.user_id);
          console.log("username: " + res.username + "(" + res.display_name + ")");

      }).then(function () {
          return getReportForDateRange(StartDate, EndDate, "days");
      }, writeOutErrorFn('login')).then(function () {
          console.log("=============================");
          console.log("Output to:" + OutputFile);
          console.log("Start:" + StartDate);
          console.log("End:" + EndDate);
          console.log("Report:" + reportID);
          console.log('Done:' + global_record_count + " records, " + global_written_count + " written.");
          console.log('Async reports requested:'+async_report_requests+' - (succeeded:'+async_report_success+',failed:'+(async_report_requests - async_report_success)+').')
      }, writeOutErrorFn('jsforce_report.downloadreport'))
      .catch(function (err) {
          console.error(err);
      });
  console.log("Starting here....");
  console.log("Report:" + reportID);
  console.log("Output to:" + OutputFile);
  console.log("Start:" + StartDate.format('YYYY-DD-MM'));
  console.log("End:" + EndDate.format('YYYY-DD-MM'));
}

function writeOutErrorFn(message) {
    return function (err) {
        console.error(message + ":" + err);
    };
}

function getReportForDateRange(startdate, enddate, interval) {
    var indexfieldOffset = 5; // Index of field to display
    var range1 = moment.range(startdate, enddate);
    var promisegroups = new Array(MAX_CONCURRENT);

    if (interval == undefined) {
        interval = "days";
    }
    var data = '';
    //console.log('GenUtilization');
    var promise = prepareCSV(reportID, data).then(function (stringifier) {
        var getInstancePromises = [];
        var concurrentPromises = new Array(MAX_CONCURRENT);
        var instances = [];
        var i = 0;
        var j = 0;
        range1.by(interval, function (start) {
            var t = j; // Make a local copy of j so it stays frozen
            //t=i;i=i+1;
            var st = start.clone();
            var end = start.clone();
            end.add(1, interval).subtract(1, "ms");
            if (concurrentPromises[j] == undefined) {
                console.log(t + ":Start Range: " + st.format() + " - " + end.format());
                concurrentPromises[j] = startAsyncReport(start, end).then(
                    processAsyncReportInstanceFn(instances, t, st, end, stringifier)
                    , writeOutErrorFn(t +":Error starting report for range: " + start.format() + " - " + end.format() + ":")
                    );
            } else {
                concurrentPromises[j] = concurrentPromises[j].then(function () {
                    console.log(t + ":Chain Range: " + start.format() + " - " + end.format());
                    var promise2 = startAsyncReport(start, end).then(
                        processAsyncReportInstanceFn(instances, t, st, end, stringifier)
                        , writeOutErrorFn(t +":Error starting report for range: " + start.format() + " - " + end.format() + ":"));
                    return promise2;
                });
            }
            j = (j + 1) % MAX_CONCURRENT;
            async_report_requests++;
        });
        return Promise.all(concurrentPromises).then(function () {
            stringifier.end();
        });
    });
    return promise;
}

function prepareCSV(reportID) {
    var data = '';
    var row;
    //console.log('Prepare CSV');
    // Write out the data
    var stringifier = stringify({ delimiter: ',' });

    stringifier.on('readable', function () {
        while (row = stringifier.read()) {
            data += row;
        }
    });
    stringifier.on('error', function (err) {
        console.log(err.message);
    });
    stringifier.on('finish', function () {
        fs.writeFile(OutputFile, data);
    });

    //stringifier.pipe(fs.createWriteStream('data/outputDateRange.csv'));
    var report = conn.analytics.report(reportID);
    return report.describe().then(function (result) {
        var columns = ["lastUpdated"];
        result.reportMetadata.detailColumns.map(function (cname) {
            columns.push(cname + "_label");
            columns.push(cname + "_value");
        });
        stringifier.write(columns);
        return stringifier;
    }, function (err) {
        console.err('prepareCSV: Cannot get report metadata:' + err);
    });
}

function startAsyncReport(startdate, enddate) {
    var metadata = {
        reportMetadata: {
            "standardDateFilter": {
                "column": datefield,
                "durationValue": "CUSTOM",
                "endDate": enddate.format(),
                "startDate": startdate.format()
            }
        }
    };
    var report = conn.analytics.report(reportID);
    var reportoptions = {
        "details": true,
        "metadata": metadata
    };
    var promise = report.executeAsync(reportoptions);
    return promise;
}

//
//
// Create a callbackfunction for processing after an async report is submitted.
//
function processAsyncReportInstanceFn(instances, t, st, end, stringifier) {
    return function (instance) {
        async_report_success++;
        instances.push(instance); // Can be removed for large report data sets
        var report = conn.analytics.report(reportID);
        var reportinstance = report.instance(instance.id);
        var promise0 = delay(WAIT_BETWEEN_REQUESTS).then(function () {
            //var promise1=reportinstance.retrieve().then(function(results) {
            var promise1 = waitForInstance(reportinstance, WAIT_BETWEEN_REQUESTS).then(function (results) {
                console.log(t + ":Returned Range: " + st.format() + " - " + end.format() + ":" + results.attributes.status);
                fs.writeFile('tmp/output-' + n + '.json', JSON.stringify(results));
                writeResult(stringifier, results);
                n = n + 1;
                console.log(results.factMap["T!T"].aggregates[0].value + " records")

                var firstrow = results.factMap["T!T"].rows[0].dataCells[indexfieldOffset];
                var lastrow = results.factMap["T!T"].rows.pop().dataCells[indexfieldOffset];
                //console.log(JSON.stringify(lastrow));
                var label = lastrow.label;
                var val = lastrow.value;
                console.log("First: " + firstrow.label + " " + firstrow.value)
                console.log("Last : " + label + " " + val);
                console.log("Package size:" + results.factMap["T!T"].rows.length);
                global_record_count = global_record_count + results.factMap["T!T"].rows.length;
                if (results.allData == false) {
                    console.error(t + ":Incomplete results for range:" + st.format() + " - " + end.format());
                }
            }, writeOutErrorFn('Error reading instance ' + instance.id + " (" + st.format() + " - " + end.format() + ")"));
            return promise1;
        });
        return promise0;
    };
}

function waitForInstance(reportinstance) {
    var waitpromise = Promise.resolve();
    function checkStatus() {
        return waitpromise.then(function () {
            return delay(WAIT_BETWEEN_REQUESTS)
                .then(function () {
                    return reportinstance.retrieve()
                })
                .then(function (result) {
                    var status = result.attributes.status;
                    if (status != "Success" && status != "Error") {
                        return checkStatus();
                    }
                    return result;
                }, function (err) {
                    console.error('Cannot retrieve instance status:' + err);
                });
        });
    }
    return checkStatus();
}

function delay(time) {
    return new Promise(function (fulfill) {
        setTimeout(fulfill, time);
    });
}

function writeResult(stringifier, results) {
    //console.log('Writeresult:'+stringifier);
    var rows = results.factMap["T!T"].rows;

    //console.log(rows.length);
    fs.writeFile('data/rowsA.json', JSON.stringify(rows[0]));

    for (var k = 0; k < rows.length; k++) {
        //console.log(JSON.stringify(rowval));
        //console.log('Writeresult:'+k);
        var datacells = rows[k]["dataCells"];
        var rowout = [moment().format()]; // Updated right now!
        var k1;
        for (k1 = 0; k1 < datacells.length; k1++) {
            rowout.push(datacells[k1].label);
            rowout.push(datacells[k1].value);
        }
        //console.log(JSON.stringify(rowout));
        stringifier.write(rowout);
        //console.log('Writeresult1:'+k);
    }
    global_written_count = global_written_count + rows.length;

    //	console.log('Writeresult:done');

}
<commit_msg>Minor fixes to remove tmp files<commit_after>/* global process */
/*global Buffer */
/**
 * @file Report downloader class which provides a single function to download a salesforce report.
 * @author Divya van Mahajan <dvm@vanmahajan.com>
 */
'use strict';

var stringify = require('csv-stringify');
var jsforce = require('jsforce');
var fs = require('fs');
var moment = require('moment');
require('moment-range');

// External inputs
var reportID;
var datefield;
var indexfieldOffset=0;
var StartDate = moment();
var EndDate = moment();

// Internal global state
var OutputFile = 'ReportOutput_' + StartDate.format("YYYY-MM-DD") + '_to_' + EndDate.format("YYYY-MM-DD") + '.csv';
var WAIT_BETWEEN_REQUESTS = 3000; // 3 seconds
var UserName = process.env.SF_USER;
var SFPassToken = process.env.SF_PASSWD_WITH_TOKEN;
var MAX_CONCURRENT = 20;
var i = 1;
var n = 0;
var async_report_requests=0;
var async_report_success=0;
var global_record_count = 0;
var global_written_count = 0;

var conn = new jsforce.Connection();

/**
 * Download the report. Command line wrapper
 * SF_USER - Environment variable with the user name.
 * SF_PASSWD_WITH_TOKEN - Environment variable with the password and security token.
 *
 * Command line:
 * downloadreport reportid datefield indexfieldoffset startdate enddate
 */
module.exports.downloadCommand = function() {
  if (process.argv.length <7 ) {
      console.error('Usage: '+process.argv[0]+' '+process.argv[1]+' reportid datefield indexfieldOffset 2016-01-01 2016-01-05');
      return;
  } else {
    module.exports.downloadreport(process.argv[2],process.argv[3],process.argv[4],
      process.argv[5],process.argv[6],process.env.SF_USER,process.env.SF_PASSWD_WITH_TOKEN);
  }
}

/**
 * Download the report.
 *
 * @protected
 * @param {String} _reportID - Salesforce report identifier. You can get this from the URL when you are viewing the report.
 * @param {String} _datefield - Internal name of the date field used in the standard date filter. << TODO >>
 * @param {String} _indexfieldOffset - Column that should be displayed while running the report (starts at 0). By default the first column is shown.
 * @param {Date} _startDate - Starting date in the format YYYY-MM-DD.
 * @param {Date} _endDate - Ending date in the format YYYY-MM-DD.
 * @param {String} _user - username.
 * @param {String} _password - password with security token.
 * @param {Object} SFOptions - Initialization options for jsforce (see http://jsforce.github.io/jsforce/doc/Connection.html)
 */
module.exports.downloadreport = function(_reportID,_datefield,_indexfieldOffset,_startDate,_endDate,_username,_password,SFOptions) {
  conn = new jsforce.Connection(SFOptions);

  reportID=_reportID;
  datefield=_datefield;
  indexfieldOffset=_indexfieldOffset;
  StartDate=moment(_startDate);
  EndDate=moment(_endDate);
  UserName = _username;
  SFPassToken = _password;

  async_report_requests=0;
  async_report_success=0;
  global_record_count=0;
  global_written_count=0;
  i = 1;
  n = 0;

  OutputFile = 'data/ReportOutput_' + StartDate.format("YYYY-MM-DD") + '_to_' + EndDate.format("YYYY-MM-DD") + '.csv';

  conn.login(UserName, SFPassToken).
      then(function () {
          return conn.identity();
      }, function (err) {
          console.error(err);
          console.error('Please check you have set the following environment variables');
          console.error('SF_USER');
          console.error('SF_PASSWD_WITH_TOKEN=password and security token');
      }).then(function (res) {
          console.log('Logged into Salesforce');
          //console.log("organization ID: " + res.organization_id);
          //console.log("user ID: " + res.user_id);
          console.log("username: " + res.username + "(" + res.display_name + ")");

      }).then(function () {
          return getReportForDateRange(StartDate, EndDate, "days");
      }, writeOutErrorFn('login')).then(function () {
          console.log("=============================");
          console.log("Output to:" + OutputFile);
          console.log("Start:" + StartDate);
          console.log("End:" + EndDate);
          console.log("Report:" + reportID);
          console.log('Done:' + global_record_count + " records, " + global_written_count + " written.");
          console.log('Async reports requested:'+async_report_requests+' - (succeeded:'+async_report_success+',failed:'+(async_report_requests - async_report_success)+').')
      }, writeOutErrorFn('jsforce_report.downloadreport'))
      .catch(function (err) {
          console.error(err);
      });
  console.log("Starting here....");
  console.log("Report:" + reportID);
  console.log("Output to:" + OutputFile);
  console.log("Start:" + StartDate.format('YYYY-DD-MM'));
  console.log("End:" + EndDate.format('YYYY-DD-MM'));
}

function writeOutErrorFn(message) {
    return function (err) {
        console.error(message + ":" + err);
    };
}

function getReportForDateRange(startdate, enddate, interval) {
    var indexfieldOffset = 5; // Index of field to display
    var range1 = moment.range(startdate, enddate);
    var promisegroups = new Array(MAX_CONCURRENT);

    if (interval == undefined) {
        interval = "days";
    }
    var data = '';
    //console.log('GenUtilization');
    var promise = prepareCSV(reportID, data).then(function (stringifier) {
        var getInstancePromises = [];
        var concurrentPromises = new Array(MAX_CONCURRENT);
        var instances = [];
        var i = 0;
        var j = 0;
        range1.by(interval, function (start) {
            var t = j; // Make a local copy of j so it stays frozen
            //t=i;i=i+1;
            var st = start.clone();
            var end = start.clone();
            end.add(1, interval).subtract(1, "ms");
            if (concurrentPromises[j] == undefined) {
                console.log(t + ":Start Range: " + st.format() + " - " + end.format());
                concurrentPromises[j] = startAsyncReport(start, end).then(
                    processAsyncReportInstanceFn(instances, t, st, end, stringifier)
                    , writeOutErrorFn(t +":Error starting report for range: " + start.format() + " - " + end.format() + ":")
                    );
            } else {
                concurrentPromises[j] = concurrentPromises[j].then(function () {
                    console.log(t + ":Chain Range: " + start.format() + " - " + end.format());
                    var promise2 = startAsyncReport(start, end).then(
                        processAsyncReportInstanceFn(instances, t, st, end, stringifier)
                        , writeOutErrorFn(t +":Error starting report for range: " + start.format() + " - " + end.format() + ":"));
                    return promise2;
                });
            }
            j = (j + 1) % MAX_CONCURRENT;
            async_report_requests++;
        });
        return Promise.all(concurrentPromises).then(function () {
            stringifier.end();
        });
    });
    return promise;
}

function prepareCSV(reportID) {
    var data = '';
    var row;
    //console.log('Prepare CSV');
    // Write out the data
    var stringifier = stringify({ delimiter: ',' });

    stringifier.on('readable', function () {
        while (row = stringifier.read()) {
            data += row;
        }
    });
    stringifier.on('error', function (err) {
        console.log(err.message);
    });
    stringifier.on('finish', function () {
        fs.writeFile(OutputFile, data);
    });

    //stringifier.pipe(fs.createWriteStream('data/outputDateRange.csv'));
    var report = conn.analytics.report(reportID);
    return report.describe().then(function (result) {
        var columns = ["lastUpdated"];
        result.reportMetadata.detailColumns.map(function (cname) {
            columns.push(cname + "_label");
            columns.push(cname + "_value");
        });
        stringifier.write(columns);
        return stringifier;
    }, function (err) {
        console.err('prepareCSV: Cannot get report metadata:' + err);
    });
}

function startAsyncReport(startdate, enddate) {
    var metadata = {
        reportMetadata: {
            "standardDateFilter": {
                "column": datefield,
                "durationValue": "CUSTOM",
                "endDate": enddate.format(),
                "startDate": startdate.format()
            }
        }
    };
    var report = conn.analytics.report(reportID);
    var reportoptions = {
        "details": true,
        "metadata": metadata
    };
    var promise = report.executeAsync(reportoptions);
    return promise;
}

//
//
// Create a callbackfunction for processing after an async report is submitted.
//
function processAsyncReportInstanceFn(instances, t, st, end, stringifier) {
    return function (instance) {
        async_report_success++;
        instances.push(instance); // Can be removed for large report data sets
        var report = conn.analytics.report(reportID);
        var reportinstance = report.instance(instance.id);
        var promise0 = delay(WAIT_BETWEEN_REQUESTS).then(function () {
            //var promise1=reportinstance.retrieve().then(function(results) {
            var promise1 = waitForInstance(reportinstance, WAIT_BETWEEN_REQUESTS).then(function (results) {
                console.log(t + ":Returned Range: " + st.format() + " - " + end.format() + ":" + results.attributes.status);
                // fs.writeFile('tmp/output-' + n + '.json', JSON.stringify(results));
                writeResult(stringifier, results);
                n = n + 1;
                console.log(results.factMap["T!T"].aggregates[0].value + " records")

                var firstrow = results.factMap["T!T"].rows[0].dataCells[indexfieldOffset];
                var lastrow = results.factMap["T!T"].rows.pop().dataCells[indexfieldOffset];
                //console.log(JSON.stringify(lastrow));
                var label = lastrow.label;
                var val = lastrow.value;
                console.log("First: " + firstrow.label + " " + firstrow.value)
                console.log("Last : " + label + " " + val);
                console.log("Package size:" + results.factMap["T!T"].rows.length);
                global_record_count = global_record_count + results.factMap["T!T"].rows.length;
                if (results.allData == false) {
                    console.error(t + ":Incomplete results for range:" + st.format() + " - " + end.format());
                }
            }, writeOutErrorFn('Error reading instance ' + instance.id + " (" + st.format() + " - " + end.format() + ")"));
            return promise1;
        });
        return promise0;
    };
}

function waitForInstance(reportinstance) {
    var waitpromise = Promise.resolve();
    function checkStatus() {
        return waitpromise.then(function () {
            return delay(WAIT_BETWEEN_REQUESTS)
                .then(function () {
                    return reportinstance.retrieve()
                })
                .then(function (result) {
                    var status = result.attributes.status;
                    if (status != "Success" && status != "Error") {
                        return checkStatus();
                    }
                    return result;
                }, function (err) {
                    console.error('Cannot retrieve instance status:' + err);
                });
        });
    }
    return checkStatus();
}

function delay(time) {
    return new Promise(function (fulfill) {
        setTimeout(fulfill, time);
    });
}

function writeResult(stringifier, results) {
    //console.log('Writeresult:'+stringifier);
    var rows = results.factMap["T!T"].rows;

    //console.log(rows.length);
    // fs.writeFile('data/rowsA.json', JSON.stringify(rows[0]));

    for (var k = 0; k < rows.length; k++) {
        //console.log(JSON.stringify(rowval));
        //console.log('Writeresult:'+k);
        var datacells = rows[k]["dataCells"];
        var rowout = [moment().format()]; // Updated right now!
        var k1;
        for (k1 = 0; k1 < datacells.length; k1++) {
            rowout.push(datacells[k1].label);
            rowout.push(datacells[k1].value);
        }
        //console.log(JSON.stringify(rowout));
        stringifier.write(rowout);
        //console.log('Writeresult1:'+k);
    }
    global_written_count = global_written_count + rows.length;

    //	console.log('Writeresult:done');

}
<|endoftext|>


<commit_before>/*issue form*/
(function ($) {
    $(document).ready(function() {

        if ($('form[name="dte_btsbundle_issue"]').length > 0) {
            $('#dte_btsbundle_issue_project').on('change', function(e) {
                var projectId = $('#dte_btsbundle_issue_project').find(':selected').val();

                loadProjectMembers(projectId);
                loadProjectStories(projectId);
            });

            $('#dte_btsbundle_issue_type').on('change', function(e) {
                var type = $('#dte_btsbundle_issue_type').find(':selected').val();

                if (type !== '4') {
                    $('#dte_btsbundle_issue_parent').find(':selected').removeAttr('selected');
                    $('#dte_btsbundle_issue_parent').attr('readonly', 'readonly');
                } else {
                    $('#dte_btsbundle_issue_parent').removeAttr('readonly');
                }
            });

            function loadProjectMembers(projectId)
            {
                $.get('/project/' + projectId + '/members', function(data) {
                    populateDropdown($('#dte_btsbundle_issue_assignee'), data);
                });
            }

            function loadProjectStories(projectId)
            {
                $.get('/project/' + projectId + '/stories', function(data) {
                    populateDropdown($('#dte_btsbundle_issue_parent'), data);
                });
            }

            function populateDropdown(element, data)
            {
                element.find('option:not(:first)').remove();
                $.each(data, function(key, value) {
                     element
                         .append($('<option></option>')
                         .attr('value', value.id)
                         .text(value.label));
                });
            }
        }
    });
})(jQuery);

/*comments*/
(function ($) {
    $(document).ready(function() {
        if ($('form[name="dte_btsbundle_comment"]').length > 0) {
            var form    = $('form[name="dte_btsbundle_comment"]');
            var issueId = parseInt($('#comments').attr('data-issue-id'));
            var url     = '/issue/' + issueId + '/comment/';

            refresh();

            form.submit(function(e) {
                e.preventDefault();

                var body = $('#dte_btsbundle_comment_body').val();
                if ('' !== body) {
                    addComment(form.serialize());

                    clearAddForm();
                    disableAddForm();
                }

                return false;
            });

            function registerFormsEvents() {
                $('.edit_comment_button').click(function(e){
                    e.preventDefault();

                    var commentId = $(this).attr('data-comment-id');

                    showEditForm(commentId);

                    return false;
                });

                $('.delete_comment_button').click(function(e){
                    e.preventDefault();

                    var commentId = $(this).attr('data-comment-id');

                    $('#comment-body-formblock-delete-' + commentId).find('form').submit();

                    return false;
                });

                $('.comment-form').submit(function(e) {
                    e.preventDefault();

                    var commentId = $(this).attr('data-comment-id');

                    var body = $(this).find('textarea[name="dte_btsbundle_comment[body]"]').val();
                    if ('' !== body) {
                        updateComment(commentId, $(this).serialize());
                    }

                    return false;
                });

                $('.comment-form-delete').submit(function(e) {
                    e.preventDefault();

                    var commentId = $(this).attr('data-comment-id');

                    deleteComment(commentId, $(this).serialize());

                    return false;
                });
            }

            function showEditForm(commentId)
            {
                $('#comment-body-textblock-' + commentId).addClass('hidden');
                $('#comment-body-formblock-' + commentId).removeClass('hidden');
            }

            function clearAddForm()
            {
                $('#dte_btsbundle_comment_body').val('');
            }

            function disableAddForm()
            {
                $('#dte_btsbundle_comment_button').attr('disabled', 'disabled');
                $('#dte_btsbundle_comment_body').attr('disabled', 'disabled');
            }

            function enableAddForm()
            {
                $('#dte_btsbundle_comment_button').removeAttr('disabled');
                $('#dte_btsbundle_comment_body').removeAttr('disabled');
            }

            function refresh()
            {
                $.get(url, function(data) {
                    $('#comments').html(data);

                    registerFormsEvents();
                });
            }

            function addComment(data)
            {
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: data,
                    success: function() {
                        refresh();
                        enableAddForm();
                    },
                });
            }

            function updateComment(commentId, data)
            {
                $.ajax({
                    type: 'PUT',
                    url: url +  commentId,
                    data: data,
                    success: function() {
                        refresh();
                    },
                });
            }

            function deleteComment(commentId,data)
            {
                 $.ajax({
                    type: 'DELETE',
                    url: url +  commentId,
                    data: data,
                    success: function() {
                        refresh();
                    },
                });
            }

        }
    });
})(jQuery);<commit_msg>CS fixed<commit_after>/*issue form*/
(function ($) {
    $(document).ready(function() {

        if ($('form[name="dte_btsbundle_issue"]').length > 0) {
            $('#dte_btsbundle_issue_project').on('change', function(e) {
                var projectId = $('#dte_btsbundle_issue_project').find(':selected').val();

                loadProjectMembers(projectId);
                loadProjectStories(projectId);
            });

            $('#dte_btsbundle_issue_type').on('change', function(e) {
                var type = $('#dte_btsbundle_issue_type').find(':selected').val();

                if (type !== '4') {
                    $('#dte_btsbundle_issue_parent').find(':selected').removeAttr('selected');
                    $('#dte_btsbundle_issue_parent').attr('readonly', 'readonly');
                } else {
                    $('#dte_btsbundle_issue_parent').removeAttr('readonly');
                }
            });

            function loadProjectMembers(projectId)
            {
                $.get('/project/' + projectId + '/members', function(data) {
                    populateDropdown($('#dte_btsbundle_issue_assignee'), data);
                });
            }

            function loadProjectStories(projectId)
            {
                $.get('/project/' + projectId + '/stories', function(data) {
                    populateDropdown($('#dte_btsbundle_issue_parent'), data);
                });
            }

            function populateDropdown(element, data)
            {
                element.find('option:not(:first)').remove();
                $.each(data, function(key, value) {
                     element
                         .append($('<option></option>')
                         .attr('value', value.id)
                         .text(value.label));
                });
            }
        }
    });
})(jQuery);

/*comments*/
(function ($) {
    $(document).ready(function() {
        if ($('form[name="dte_btsbundle_comment"]').length > 0) {
            var form    = $('form[name="dte_btsbundle_comment"]');
            var issueId = parseInt($('#comments').attr('data-issue-id'));
            var url     = '/issue/' + issueId + '/comment/';

            refresh();

            form.submit(function(e) {
                e.preventDefault();

                var body = $('#dte_btsbundle_comment_body').val();
                if ('' !== body) {
                    addComment(form.serialize());

                    clearAddForm();
                    disableAddForm();
                }

                return false;
            });

            function registerFormsEvents()
            {
                $('.edit_comment_button').click(function(e) {
                    e.preventDefault();

                    var commentId = $(this).attr('data-comment-id');

                    showEditForm(commentId);

                    return false;
                });

                $('.delete_comment_button').click(function(e) {
                    e.preventDefault();

                    var commentId = $(this).attr('data-comment-id');

                    $('#comment-body-formblock-delete-' + commentId).find('form').submit();

                    return false;
                });

                $('.comment-form').submit(function(e) {
                    e.preventDefault();

                    var commentId = $(this).attr('data-comment-id');

                    var body = $(this).find('textarea[name="dte_btsbundle_comment[body]"]').val();
                    if ('' !== body) {
                        updateComment(commentId, $(this).serialize());
                    }

                    return false;
                });

                $('.comment-form-delete').submit(function(e) {
                    e.preventDefault();

                    var commentId = $(this).attr('data-comment-id');

                    deleteComment(commentId, $(this).serialize());

                    return false;
                });
            }

            function showEditForm(commentId)
            {
                $('#comment-body-textblock-' + commentId).addClass('hidden');
                $('#comment-body-formblock-' + commentId).removeClass('hidden');
            }

            function clearAddForm()
            {
                $('#dte_btsbundle_comment_body').val('');
            }

            function disableAddForm()
            {
                $('#dte_btsbundle_comment_button').attr('disabled', 'disabled');
                $('#dte_btsbundle_comment_body').attr('disabled', 'disabled');
            }

            function enableAddForm()
            {
                $('#dte_btsbundle_comment_button').removeAttr('disabled');
                $('#dte_btsbundle_comment_body').removeAttr('disabled');
            }

            function refresh()
            {
                $.get(url, function(data) {
                    $('#comments').html(data);

                    registerFormsEvents();
                });
            }

            function addComment(data)
            {
                $.ajax({
                    type: 'POST',
                    url: url,
                    data: data,
                    success: function() {
                        refresh();
                        enableAddForm();
                    },
                });
            }

            function updateComment(commentId, data)
            {
                $.ajax({
                    type: 'PUT',
                    url: url +  commentId,
                    data: data,
                    success: function() {
                        refresh();
                    },
                });
            }

            function deleteComment(commentId, data)
            {
                $.ajax({
                    type: 'DELETE',
                    url: url +  commentId,
                    data: data,
                    success: function() {
                        refresh();
                    },
                });
            }
        }
    });
})(jQuery);<|endoftext|>


<commit_before>/**
 * Copyright (c) 2011 Reginaldo Silva (reginaldo@ubercomp.com)
 * Created: 11/09/11 20:22
 */
"use strict";
lm32.start = function(load_linux) {
    // TODO should be argument
    var terminal_div = 'termDiv'

    var FLASH_BASE        = 0x04000000;
    var FLASH_SECTOR_SIZE = 256*1024;
    var FLASH_SIZE        = 32*1024*1024;

    var RAM_BASE          = 0x08000000;
    var RAM_SIZE          = 64*1024*1024;

    var TIMER0_BASE       = 0x80002000;
    var TIMER0_IRQ        = 1;

    var UART0_BASE        = 0x80000000;
    var UART0_IRQ         = 0;

    var HWSETUP_BASE      = 0x0bffe000;

    var EBA_BASE = FLASH_BASE;
    var DEBA_BASE = FLASH_BASE;
    var BOOT_PC = load_linux ? FLASH_BASE : RAM_BASE;
    var mmu = new lm32.MMU();
    var vm_clock = new lm32.ghw.VMClock();

    var ram = new lm32.RAM(RAM_SIZE, true);

    var flash = new lm32.PFlashCFI01(load_linux, FLASH_SECTOR_SIZE,
        FLASH_SIZE / FLASH_SECTOR_SIZE, 2,
        0x01, 0x7e, 0x43, 0x00, true);

    var cpu_params = {
        mmu: mmu,
        bootstrap_pc: BOOT_PC,
        bootstrap_eba: EBA_BASE,
        bootstrap_deba: DEBA_BASE
    };
    var cpu = new lm32.Lm32Cpu(cpu_params);
    var set_irq = cpu.set_irq.bind(cpu);

    var timer0 = new lm32.Lm32Timer({
        id: 0,
        vm_clock: vm_clock,
        irq_line: TIMER0_IRQ,
        set_irq: set_irq
    });

    // UART and Terminal
    var terminal;

    var uart0 = new lm32.UART({
        putchar: function(c) {
            // TODO treat special keys (up, down, left, right), and cursor
            if(c == 0x08) {
                // backspace
                terminal.backspace();
                return;
            }
            if(c == 0x0d) {
                // carriage return
                return;
            }
            terminal.write(String.fromCharCode(c));
        },
        irq_line: UART0_IRQ,
        set_irq: set_irq
    });

    var send_char = uart0.send_char.bind(uart0);
    function start_terminal() {
        function termHandler() {
            var line = this.inputChar;
            if (line != "") {
                send_char(line);
            }
        }
        terminal = new Terminal({
            handler: termHandler,
            termDiv: terminal_div
        });
        terminal.open();
        terminal.charMode = true;
    }
    start_terminal();

    // Gluing everything together
    mmu.add_memory(RAM_BASE, RAM_SIZE, ram.get_mmio_handlers());
    mmu.add_memory(FLASH_BASE, FLASH_SIZE, flash.get_mmio_handlers());
    mmu.add_memory(UART0_BASE, uart0.iomem_size, uart0.get_mmio_handlers());
    mmu.add_memory(TIMER0_BASE, timer0.iomem_size, timer0.get_mmio_handlers());

    if(load_linux) {
        console.log('Loading initrd to RAM at ' + lm32.bits.format(RAM_BASE + 0x400000));
        mmu.load_binary('../linux/initrd.img', RAM_BASE + 0x400000);
    } else {
        console.log('Loading U-Boot to RAM');
        mmu.load_binary('../linux/u-boot.bin', RAM_BASE);
    }
    mmu.load_binary('../linux/hwsetup.bin', HWSETUP_BASE);

    window.flash = flash;
    window.mmu = mmu;
    window.cpu = cpu;

    var f = function() {
        //console.log("Started running at pc: " + lm32.bits.format(cpu.pc));
        cpu.step(1000);
        //console.log("Finished running at pc: " + lm32.bits.format(cpu.pc));
        setTimeout(f, 0);
    }
    f();
};

<commit_msg>The whole system boots now<commit_after>/**
 * Copyright (c) 2011 Reginaldo Silva (reginaldo@ubercomp.com)
 * Created: 11/09/11 20:22
 */
"use strict";
lm32.start = function(load_linux) {
    // TODO should be argument
    var terminal_div = 'termDiv'

    var FLASH_BASE        = 0x04000000;
    var FLASH_SECTOR_SIZE = 256*1024;
    var FLASH_SIZE        = 32*1024*1024;

    var RAM_BASE          = 0x08000000;
    var RAM_SIZE          = 64*1024*1024;

    var TIMER0_BASE       = 0x80002000;
    var TIMER0_IRQ        = 1;

    var TIMER1_BASE       = 0x80010000;
    var TIMER1_IRQ        = 20;

    var TIMER2_BASE       = 0x80012000;
    var TIMER2_IRQ        = 21;

    var UART0_BASE        = 0x80000000;
    var UART0_IRQ         = 0;

    var HWSETUP_BASE      = 0x0bffe000;
    var CMDLINE_BASE      = 0x0bfff000;
    var INITRD_BASE       = 0x08400000;

    var EBA_BASE = FLASH_BASE;
    var DEBA_BASE = FLASH_BASE;
    var BOOT_PC = load_linux ? FLASH_BASE : RAM_BASE;
    var KERNEL_BASE = RAM_BASE;

    var mmu = new lm32.MMU();

    var ram = new lm32.RAM(RAM_SIZE, true);

    var flash = new lm32.PFlashCFI01(load_linux,
        FLASH_SECTOR_SIZE,
        FLASH_SIZE / FLASH_SECTOR_SIZE, 2,
        0x01, 0x7e, 0x43, 0x00, true);

    var cpu_params = {
        mmu: mmu,
        bootstrap_pc: BOOT_PC,
        bootstrap_eba: EBA_BASE,
        bootstrap_deba: DEBA_BASE
    };

    var cpu = new lm32.Lm32Cpu(cpu_params);
    var set_irq = cpu.pic.irq_handler;

    var timer0 = new lm32.Lm32Timer({
        id: 0,
        irq_line: TIMER0_IRQ,
        set_irq: set_irq
    });

    var timer1 = new lm32.Lm32Timer({
        id: 1,
        irq_line: TIMER1_IRQ,
        set_irq: set_irq
    });

    var timer2 = new lm32.Lm32Timer({
        id: 2,
        irq_line: TIMER2_IRQ,
        set_irq: set_irq
    });

    // UART and Terminal
    var terminal;

    var uart0 = new lm32.UART({
        putchar: function(c) {
            // TODO treat special keys (up, down, left, right), and cursor
            if(c == 0x08) {
                // backspace
                terminal.backspace();
                return;
            }
            if(c == 0x0d) {
                return;
            }
            terminal.write(String.fromCharCode(c));
        },
        irq_line: UART0_IRQ,
        set_irq: set_irq
    });

    var send_char = uart0.send_char.bind(uart0);
    function start_terminal() {
        function termHandler() {
            var line = this.inputChar;
            if (line != "") {
                send_char(line);
            }
        }
        terminal = new Terminal({
            handler: termHandler,
            termDiv: terminal_div
        });
        terminal.open();
        terminal.charMode = true;
    }
    start_terminal();

    // Gluing everything together
    mmu.add_memory(RAM_BASE, RAM_SIZE, ram.get_mmio_handlers());
    mmu.add_memory(FLASH_BASE, FLASH_SIZE, flash.get_mmio_handlers());
    mmu.add_memory(UART0_BASE, uart0.iomem_size, uart0.get_mmio_handlers());
    mmu.add_memory(TIMER0_BASE, timer0.iomem_size, timer0.get_mmio_handlers());
    mmu.add_memory(TIMER1_BASE, timer1.iomem_size, timer1.get_mmio_handlers());
    mmu.add_memory(TIMER2_BASE, timer2.iomem_size, timer2.get_mmio_handlers());

    if(load_linux) {
        // load initrd
        console.log('Loading initrd to RAM at ' + lm32.bits.format(0x08400000));
        mmu.load_binary('../linux/initrd.img', 0x08400000);
    } else {
        // load u-boot
        console.log('Loading U-boot to RAM at ' + lm32.bits.format(RAM_BASE));
        mmu.load_binary('../linux/u-boot.bin', RAM_BASE);
    }

    window.flash = flash;
    window.mmu = mmu;
    window.cpu = cpu;

    function on_tick(ticks) {
        (timer0.on_tick.bind(timer0))(ticks);
        (timer1.on_tick.bind(timer1))(ticks);
        (timer2.on_tick.bind(timer2))(ticks);
    }
    cpu.set_timers([timer0, timer1, timer2]);
    var step = cpu.step.bind(cpu);
    var f = function() {

        var ticks = step(50000);
        //on_tick(ticks);
        setTimeout(f, 0);
    }
    f();
};

<|endoftext|>


<commit_before>// Copyright 2016, EMC, Inc.

'use strict';

var di = require('di'),
    jwt = require('jsonwebtoken'),
    passport = require('passport'),
    JwtStrategy = require('passport-jwt').Strategy,
    LocalStrategy = require('passport-local').Strategy,
    BasicStrategy = require('passport-http').BasicStrategy,
    ExtractJwt = require('passport-jwt').ExtractJwt,
    AnonStrategy = require('passport-anonymous').Strategy;

var BAD_REQUEST_STATUS = 400;
var UNAUTHORIZED_STATUS = 401;
var ERROR_STATUS = 500;

module.exports = authServiceFactory;
di.annotate(authServiceFactory, new di.Provide('Auth.Services'));
di.annotate(authServiceFactory,
    new di.Inject(
        'Assert',
        'Services.Configuration',
        '_',
        'Services.Waterline'
    )
);

function authServiceFactory(
    assert,
    configuration,
    _,
    waterline
) {
    var redfishSessions = [];

    function AuthService() {
    }

    AuthService.prototype.init = function(){
        var self = this;

        self.algorithm = 'HS256';
        self.secretOrKey = '';
        self.ignoreTokenExpiration = false;
        self.encoder = 'base64';

        self.loadFromConf();

        self.jwtSignOptions = {
            algorithm : self.algorithm,
            secretOrKey : self.secretOrKey,
            expiresIn : self.expiresIn
        };

        self.jwtVerifyOptions = {
            algorithm : self.algorithm,
            secretOrKey : self.secretOrKey,
            ignoreExpiration : self.ignoreTokenExpiration
        };

        // We do not use passport.session middleware and run our strategies
        // with session: false, but this is a required function for req.logIn
        // to succeed.  The 'user' value below is the value passed to req.logIn
        // and it is returned to indicate success.
        passport.serializeUser(function(user, done) {
            done(null, user);
        });
    };

    AuthService.prototype.getPassportMidware = function () {
        var self = this;
        var passportMidware = passport.initialize();

        passport.use(new LocalStrategy(self.localStrategyAuth.bind(self)));
        passport.use(new JwtStrategy(
            _.merge(self.jwtVerifyOptions,
                    { jwtFromRequest: ExtractJwt.versionOneCompatibility({
                        authScheme: 'JWT',
                        tokenQueryParameterName: 'auth_token' })
                    }),
            self.jwtStrategyAuth.bind(self)));
        passport.use('redfish', new JwtStrategy(
            _.merge(self.jwtVerifyOptions,
                    { jwtFromRequest: ExtractJwt.fromHeader('x-auth-token') }),
            self.jwtStrategyRedfish.bind(self)));
        passport.use(new BasicStrategy(self.localStrategyAuth.bind(self)));
        passport.use(new AnonStrategy());

        return passportMidware;
    };

    AuthService.prototype.loadFromConf = function () {
        var secret = configuration.get('authTokenSecret', 'RackHDRocks!');
        assert.string(secret, 'Invalid token secret in configure file');
        this.secretOrKey = new Buffer(secret, this.encoder);

        this.expiresIn = configuration.get('authTokenExpireIn', 24 * 60 * 60 /* 24 hours */);
        assert.number(this.expiresIn, 'Invalid token expire value in configure file');

        if (this.expiresIn === 0){
            this.ignoreTokenExpiration = true;
        }
    };

    AuthService.prototype.localStrategyAuth = function (username, password, done) {
        waterline.localusers.findOne({username: username}).then(function(user) {
            if(user && user.comparePassword(password)) {
                return done(null, username);
            }
            return done(null, false, {message: 'Unauthorized'});
        }).catch(function(err) {
            return done(err);
        });
    };

    AuthService.prototype.createJwtToken = function (user) {
        var self = this;
        return jwt.sign({
            user: user
        },
        self.secretOrKey,
        self.jwtSignOptions
        );
    };

    AuthService.prototype.jwtStrategyAuth = function(jwtPayload, done) {
        waterline.localusers.findOne({username: jwtPayload.user}).then(function(user) {
            if(user) {
                return done(null, user.username);
            }
            return done(null, false, {message: 'Unauthorized'});
        }).catch(function(err) {
            return done(err);
        });
    };

    AuthService.prototype.jwtStrategyRedfish = function(jwtPayload, done) {
        var found = _.find(redfishSessions, function(entry) {
            return (entry.user === jwtPayload.user) &&
                   (entry.id === jwtPayload.id);
        });
        if( found ) {
            return done(null, found.user);
        } else {
            return done(null, false);
        }
    };

    AuthService.prototype.authMiddlewareJwt = function(req, res, next) {
        passport.authenticate(      // middleware for token authentication
            'jwt',
            {session: false},
            function errorHandler(err, user, challenges){
                if(err){
                    res.status(ERROR_STATUS).send({message: 'Internal server error'});
                }
                else if (!user){
                    res.status(UNAUTHORIZED_STATUS).send({message: challenges.message});
                }
                else{   // nothing wrong, move on to next middleware
                    // remove the authToken query string if there are any.
                    delete req.query.auth_token; /* jshint ignore: line */
                    next();
                }
            }
        )(req, res, next);
    };

    AuthService.prototype.authMiddlewareLogin = function(req, res, next) {
        var self = this;

        passport.authenticate('local', {
                session: false
            },
            function errorHandler(err, user, challenges){ // Callback to handle auth error
                if(err){
                    res.status(ERROR_STATUS).send({message: 'Internal server error'});
                }
                else if (!user){
                    if(challenges.message === 'Missing credentials'){
                        res.status(BAD_REQUEST_STATUS).send(challenges);
                    }
                    else if(challenges.message === 'Invalid username or password') {
                        res.status(UNAUTHORIZED_STATUS).send(challenges);
                    }
                    else {
                        res.status(UNAUTHORIZED_STATUS).send(challenges);
                    }
                }
                else {
                    var token = {
                        'token': self.createJwtToken(user)
                    };

                    res.send(token);
                }
            }
        )(req, res, next);
    };

    AuthService.prototype.authMiddlewareRedfish = function(req, res, next) {
        passport.authenticate(
            ['basic', 'redfish'],
            {session: false},
            function(err, user, challenges) {
                if(err){
                    res.status(ERROR_STATUS).send({message: 'Internal server error'});
                }
                else if (!user){
                    res.status(UNAUTHORIZED_STATUS).send({message: challenges.message});
                }
                else {
                    next();
                }
            }
        )(req, res, next);
    };

    AuthService.prototype.authenticateWithMethod = function(req, res, next, method) {
        passport.authenticate(method, {session: false}, function(err, user, challenges) {
                if(err){
                    res.status(ERROR_STATUS).send({message: 'Internal server error'});
                }
                else if (!user){
                    res.status(UNAUTHORIZED_STATUS).send({message: challenges.message});
                }
                else {
                    req.login(user, function(err) {
                        if(err) {
                            return next(err);
                        }
                        next();
                    });
                }
            }
        )(req, res, next);
    };

    AuthService.prototype.addRedfishSession = function(user, id) {
        var self = this;
        redfishSessions.push( {user: user, id: id} );
        return jwt.sign( {user: user, id: id}, self.secretOrKey, self.jwtSignOptions );
    };

    AuthService.prototype.getRedfishSession = function(id) {
        if(!id) {
            return redfishSessions;
        }
        return _.find(redfishSessions, function(entry) {
            return entry.id === id;
        });
    };

    AuthService.prototype.delRedfishSession = function(id) {
        _.remove(redfishSessions, function(entry) {
            return entry.id === id;
        });
    };

    return new AuthService();
}
<commit_msg>return empty object when access api without token or with error token<commit_after>// Copyright 2016, EMC, Inc.

'use strict';

var di = require('di'),
    jwt = require('jsonwebtoken'),
    passport = require('passport'),
    JwtStrategy = require('passport-jwt').Strategy,
    LocalStrategy = require('passport-local').Strategy,
    BasicStrategy = require('passport-http').BasicStrategy,
    ExtractJwt = require('passport-jwt').ExtractJwt,
    AnonStrategy = require('passport-anonymous').Strategy;

var BAD_REQUEST_STATUS = 400;
var UNAUTHORIZED_STATUS = 401;
var ERROR_STATUS = 500;

module.exports = authServiceFactory;
di.annotate(authServiceFactory, new di.Provide('Auth.Services'));
di.annotate(authServiceFactory,
    new di.Inject(
        'Assert',
        'Services.Configuration',
        '_',
        'Services.Waterline'
    )
);

function authServiceFactory(
    assert,
    configuration,
    _,
    waterline
) {
    var redfishSessions = [];

    function AuthService() {
    }

    AuthService.prototype.init = function(){
        var self = this;

        self.algorithm = 'HS256';
        self.secretOrKey = '';
        self.ignoreTokenExpiration = false;
        self.encoder = 'base64';

        self.loadFromConf();

        self.jwtSignOptions = {
            algorithm : self.algorithm,
            secretOrKey : self.secretOrKey,
            expiresIn : self.expiresIn
        };

        self.jwtVerifyOptions = {
            algorithm : self.algorithm,
            secretOrKey : self.secretOrKey,
            ignoreExpiration : self.ignoreTokenExpiration
        };

        // We do not use passport.session middleware and run our strategies
        // with session: false, but this is a required function for req.logIn
        // to succeed.  The 'user' value below is the value passed to req.logIn
        // and it is returned to indicate success.
        passport.serializeUser(function(user, done) {
            done(null, user);
        });
    };

    AuthService.prototype.getPassportMidware = function () {
        var self = this;
        var passportMidware = passport.initialize();

        passport.use(new LocalStrategy(self.localStrategyAuth.bind(self)));
        passport.use(new JwtStrategy(
            _.merge(self.jwtVerifyOptions,
                    { jwtFromRequest: ExtractJwt.versionOneCompatibility({
                        authScheme: 'JWT',
                        tokenQueryParameterName: 'auth_token' })
                    }),
            self.jwtStrategyAuth.bind(self)));
        passport.use('redfish', new JwtStrategy(
            _.merge(self.jwtVerifyOptions,
                    { jwtFromRequest: ExtractJwt.fromHeader('x-auth-token') }),
            self.jwtStrategyRedfish.bind(self)));
        passport.use(new BasicStrategy(self.localStrategyAuth.bind(self)));
        passport.use(new AnonStrategy());

        return passportMidware;
    };

    AuthService.prototype.loadFromConf = function () {
        var secret = configuration.get('authTokenSecret', 'RackHDRocks!');
        assert.string(secret, 'Invalid token secret in configure file');
        this.secretOrKey = new Buffer(secret, this.encoder);

        this.expiresIn = configuration.get('authTokenExpireIn', 24 * 60 * 60 /* 24 hours */);
        assert.number(this.expiresIn, 'Invalid token expire value in configure file');

        if (this.expiresIn === 0){
            this.ignoreTokenExpiration = true;
        }
    };

    AuthService.prototype.localStrategyAuth = function (username, password, done) {
        waterline.localusers.findOne({username: username}).then(function(user) {
            if(user && user.comparePassword(password)) {
                return done(null, username);
            }
            return done(null, false, {message: 'Unauthorized'});
        }).catch(function(err) {
            return done(err);
        });
    };

    AuthService.prototype.createJwtToken = function (user) {
        var self = this;
        return jwt.sign({
            user: user
        },
        self.secretOrKey,
        self.jwtSignOptions
        );
    };

    AuthService.prototype.jwtStrategyAuth = function(jwtPayload, done) {
        waterline.localusers.findOne({username: jwtPayload.user}).then(function(user) {
            if(user) {
                return done(null, user.username);
            }
            return done(null, false, {message: 'Unauthorized'});
        }).catch(function(err) {
            return done(err);
        });
    };

    AuthService.prototype.jwtStrategyRedfish = function(jwtPayload, done) {
        var found = _.find(redfishSessions, function(entry) {
            return (entry.user === jwtPayload.user) &&
                   (entry.id === jwtPayload.id);
        });
        if( found ) {
            return done(null, found.user);
        } else {
            return done(null, false);
        }
    };

    AuthService.prototype.authMiddlewareJwt = function(req, res, next) {
        passport.authenticate(      // middleware for token authentication
            'jwt',
            {session: false},
            function errorHandler(err, user, challenges){
                if(err){
                    res.status(ERROR_STATUS).send({message: 'Internal server error'});
                }
                else if (!user){
                    res.status(UNAUTHORIZED_STATUS).send({message: challenges.message});
                }
                else{   // nothing wrong, move on to next middleware
                    // remove the authToken query string if there are any.
                    delete req.query.auth_token; /* jshint ignore: line */
                    next();
                }
            }
        )(req, res, next);
    };

    AuthService.prototype.authMiddlewareLogin = function(req, res, next) {
        var self = this;

        passport.authenticate('local', {
                session: false
            },
            function errorHandler(err, user, challenges){ // Callback to handle auth error
                if(err){
                    res.status(ERROR_STATUS).send({message: 'Internal server error'});
                }
                else if (!user){
                    if(challenges.message === 'Missing credentials'){
                        res.status(BAD_REQUEST_STATUS).send(challenges);
                    }
                    else if(challenges.message === 'Invalid username or password') {
                        res.status(UNAUTHORIZED_STATUS).send(challenges);
                    }
                    else {
                        res.status(UNAUTHORIZED_STATUS).send(challenges);
                    }
                }
                else {
                    var token = {
                        'token': self.createJwtToken(user)
                    };

                    res.send(token);
                }
            }
        )(req, res, next);
    };

    AuthService.prototype.authMiddlewareRedfish = function(req, res, next) {
        passport.authenticate(
            ['basic', 'redfish'],
            {session: false},
            function(err, user, challenges) {
                if(err){
                    res.status(ERROR_STATUS).send({message: 'Internal server error'});
                }
                else if (!user){
                    res.status(UNAUTHORIZED_STATUS).send({message: challenges.message});
                }
                else {
                    next();
                }
            }
        )(req, res, next);
    };

    AuthService.prototype.authenticateWithMethod = function(req, res, next, method) {
        passport.authenticate(method, {session: false}, function(err, user, challenges) {
                if(err){
                    res.status(ERROR_STATUS).send({message: 'Internal server error'});
                }
                else if (!user){
                    res.status(UNAUTHORIZED_STATUS).send({message: challenges[0].message});
                }
                else {
                    req.login(user, function(err) {
                        if(err) {
                            return next(err);
                        }
                        next();
                    });
                }
            }
        )(req, res, next);
    };

    AuthService.prototype.addRedfishSession = function(user, id) {
        var self = this;
        redfishSessions.push( {user: user, id: id} );
        return jwt.sign( {user: user, id: id}, self.secretOrKey, self.jwtSignOptions );
    };

    AuthService.prototype.getRedfishSession = function(id) {
        if(!id) {
            return redfishSessions;
        }
        return _.find(redfishSessions, function(entry) {
            return entry.id === id;
        });
    };

    AuthService.prototype.delRedfishSession = function(id) {
        _.remove(redfishSessions, function(entry) {
            return entry.id === id;
        });
    };

    return new AuthService();
}
<|endoftext|>


<commit_before>// A simple utility that maintains a worker pool and invokes
// computations in parallel on data.
//
// Load this into your main program, then call Multicore.init() to set
// things up.  Then call Multicore.build() to perform work.
//
// Functions are transmitted from the master to the workers as numeric
// IDs; the master and the workers must agree on those IDs, and the
// workers must set up local function tables.  See parinvoke-worker.js
// for more information.

const Multicore =
    {
	init: _Multicore_init,
	build: _Multicore_build
    };

var _Multicore_workers = [];
var _Multicore_numWorkers = 0;
var _Multicore_callback = null;
var _Multicore_mem = null;
var _Multicore_alloc = 0;
var _Multicore_barrier = null;
var _Multicore_barrierLoc = null;
var _Multicore_previousOutputMem = null;
var _Multicore_funcLoc = 0;
var _Multicore_sizeLoc = 0;
var _Multicore_nextLoc = 0;
var _Multicore_limLoc = 0;

// Multicore.init()
//
// numWorkers must be a positive integer.
// workerScript must be a URL.
// readyCallback must be a function, it will be called when the workers
//   have been set up.

function _Multicore_init(numWorkers, workerScript, readyCallback) {
    _Multicore_numWorkers = numWorkers;
    _Multicore_mem = new SharedInt32Array(0x10000);
    _Multicore_alloc = 0;
    _Multicore_barrierLoc = _Multicore_alloc;
    _Multicore_barrier = new MasterBarrier(0x1337,
					   _Multicore_numWorkers,
					   _Multicore_mem,
					   _Multicore_barrierLoc,
					   _Multicore_barrierQuiescent);
    _Multicore_alloc += MasterBarrier.NUMLOCS;
    _Multicore_funcLoc = _Multicore_alloc++;
    _Multicore_sizeLoc = _Multicore_alloc++;
    _Multicore_nextLoc = _Multicore_alloc++;
    _Multicore_limLoc = _Multicore_alloc++;
    _Multicore_callback = readyCallback;
    for ( var i=0 ; i < numWorkers ; i++ ) {
	var w = new Worker(workerScript);
	w.onmessage = _Multicore_msg;
	w.postMessage(["start",
		       _Multicore_mem.buffer,
		       _Multicore_barrierLoc, _Multicore_funcLoc, _Multicore_sizeLoc, _Multicore_nextLoc, _Multicore_limLoc],
		      [_Multicore_mem.buffer]);
	_Multicore_workers.push(w);
    }
}

function _Multicore_barrierQuiescent() {
    var fn;
    if (fn = _Multicore_callback) {
	_Multicore_callback = null;
	fn();
    }
    else
	throw new Error("No barrier callback installed!");
}

function _Multicore_msg(ev) {
    switch (ev.data[0]) {
    case "MasterBarrier.dispatch":
	MasterBarrier.dispatch(ev.data[1]);
	break;
    default:
	console.log(ev.data);
	break;
    }
}

// Multicore.build()
//
// doneCallback is a function, it will be invoked in the master once the
//   work is finished.
// fnIdent is the numeric identifier of the remote function to invoke.
//   See top of file for more information.
// outputMem is a SharedTypedArray or SharedArrayBuffer that will receive
//   results of the computation.
// indexSpace is an array of length-2 arrays determining the index space
//   of the computation; workers will be invoked on subvolumes of this
//   space in an unpredictable order.
//
// TODO: this wants to take additional arguments to be passed to the worker.
//
// TODO: really we should take care of transmission in a better way: we should
//   transmit not just the base type, but also byteoffset / byterange!

function _Multicore_build(doneCallback, fnIdent, outputMem, indexSpace) {
    for ( var x of indexSpace )
	if (x.length != 2 || typeof x[0] != 'number' || typeof x[1] != 'number' || (x[0]|0) != x[0] || (x[1]|0) != x[1])
	    throw new Error("Bad indexSpace element " + x)
    var items;
    switch (indexSpace.length) {
    case 1:
	items = sliceSpace(indexSpace[0][0], indexSpace[0][1]);
	break;
    case 2:
	items = cross(sliceSpace(indexSpace[0][0], indexSpace[0][1]), sliceSpace(indexSpace[1][0], indexSpace[1][1]));
	break;
    default:
	throw new Error("Only 1D and 2D supported as of now");
    }
    var itemSize = indexSpace.length * 2;
    if (outputMem != _Multicore_previousOutputMem) {
	_Multicore_callback =
	    function () {
		_Multicore_callback = doneCallback;
		installItems(fnIdent, itemSize, items);
		_Multicore_barrier.release();
	    };
	_Multicore_mem[_Multicore_funcLoc] = fnIdent;
	_Multicore_mem[_Multicore_nextLoc] = -1000000;
	var value = outputMem instanceof SharedArrayBuffer ? outputMem : outputMem.buffer;
	for ( var w of _Multicore_workers )
	    w.postMessage(["compute", value], [value]);
	_Multicore_previousOutputMem = outputMem;
    }
    else {
	_Multicore_callback = doneCallback;
	installItems(fnIdent, itemSize, items);
    }
    _Multicore_barrier.release();

    function sliceSpace(lo, lim) {
	var items = [];
	var numItem = (lim - lo);
	var sliceHeight = Math.floor(numItem / (4*_Multicore_numWorkers));
	var extra = numItem % (4*_Multicore_numWorkers);
	while (lo < lim) {
	    var hi = lo + sliceHeight;
	    if (extra) {
		hi++;
		extra--;
	    }
	    items.push([lo, hi]);
	    lo = hi;
	}
	return items;
    }

    function cross(as, bs) {
	var items = [];
	for ( var a of as )
	    for ( var b of bs )
		items.push([a, b]);
	return items;
    }

    function installItems(fn, wordsPerItem, items) {
	// Set up the work queue in shared memory
	var m = _Multicore_mem;
	m[_Multicore_sizeLoc] = wordsPerItem;
	m[_Multicore_funcLoc] = fn;
	m[_Multicore_nextLoc] = _Multicore_alloc;
	m[_Multicore_limLoc] = _Multicore_alloc + wordsPerItem*items.length;
	var p = _Multicore_alloc;
	switch (wordsPerItem) {
	case 2:
	    for ( var i of items )
		for ( var j of i )
		    m[p++] = j;
	    break;
	case 4:
	    for ( var i of items )
		for ( var j of i )
		    for ( var k of j )
			m[p++] = k;
	    break;
	}
    }
}
<commit_msg>Cleanup some code<commit_after>// 22 November 2014 / lhansen@mozilla.com

// A simple data-parallel framework that maintains a worker pool and
// invokes computations in parallel on shared memory.
//
// Load this into your main program, then call Multicore.init() to set
// things up.  Then call Multicore.build() to perform work.
//
// Functions are transmitted from the master to the workers as numeric
// IDs; the master and the workers must agree on those IDs, and the
// workers must set up local function tables.  See parinvoke-worker.js
// for more information.

const Multicore =
    {
	init: _Multicore_init,
	build: _Multicore_build
    };

var _Multicore_workers = [];
var _Multicore_numWorkers = 0;
var _Multicore_callback = null;
var _Multicore_mem = null;
var _Multicore_alloc = 0;
var _Multicore_barrier = null;
var _Multicore_barrierLoc = null;
var _Multicore_previousOutputMem = null;
var _Multicore_funcLoc = 0;
var _Multicore_sizeLoc = 0;
var _Multicore_nextLoc = 0;
var _Multicore_limLoc = 0;

// Multicore.init()
//
// numWorkers must be a positive integer.
// workerScript must be a URL.
// readyCallback must be a function, it will be called when the workers
//   have been set up.

function _Multicore_init(numWorkers, workerScript, readyCallback) {
    _Multicore_numWorkers = numWorkers;
    _Multicore_mem = new SharedInt32Array(0x10000);
    _Multicore_alloc = 0;
    _Multicore_barrierLoc = _Multicore_alloc;
    _Multicore_barrier = new MasterBarrier(0x1337,
					   _Multicore_numWorkers,
					   _Multicore_mem,
					   _Multicore_barrierLoc,
					   barrierQuiescent);
    _Multicore_alloc += MasterBarrier.NUMLOCS;
    _Multicore_funcLoc = _Multicore_alloc++;
    _Multicore_sizeLoc = _Multicore_alloc++;
    _Multicore_nextLoc = _Multicore_alloc++;
    _Multicore_limLoc = _Multicore_alloc++;
    _Multicore_callback = readyCallback;
    for ( var i=0 ; i < numWorkers ; i++ ) {
	var w = new Worker(workerScript);
	w.onmessage = messageHandler;
	w.postMessage(["start",
		       _Multicore_mem.buffer,
		       _Multicore_barrierLoc, _Multicore_funcLoc, _Multicore_sizeLoc, _Multicore_nextLoc, _Multicore_limLoc],
		      [_Multicore_mem.buffer]);
	_Multicore_workers.push(w);
    }

    function barrierQuiescent() {
	var fn;
	if (fn = _Multicore_callback) {
	    _Multicore_callback = null;
	    fn();
	}
	else
	    throw new Error("No barrier callback installed!");
    }

    function messageHandler(ev) {
	switch (ev.data[0]) {
	case "MasterBarrier.dispatch":
	    MasterBarrier.dispatch(ev.data[1]);
	    break;
	default:
	    console.log(ev.data);
	    break;
	}
    }
}

// Multicore.build()
//
// doneCallback is a function, it will be invoked in the master once the
//   work is finished.
// fnIdent is the numeric identifier of the remote function to invoke.
//   See top of file for more information.
// outputMem is a SharedTypedArray or SharedArrayBuffer that will receive
//   results of the computation.
// indexSpace is an array of length-2 arrays determining the index space
//   of the computation; workers will be invoked on subvolumes of this
//   space in an unpredictable order.
//
// TODO: this wants to take additional arguments to be passed to the worker.
//
// TODO: really we should take care of transmission in a better way: we should
//   transmit not just the base type, but also byteoffset / byterange!

function _Multicore_build(doneCallback, fnIdent, outputMem, indexSpace) {
    for ( var x of indexSpace )
	if (x.length != 2 || typeof x[0] != 'number' || typeof x[1] != 'number' || (x[0]|0) != x[0] || (x[1]|0) != x[1])
	    throw new Error("Bad indexSpace element " + x)
    var items;
    switch (indexSpace.length) {
    case 1:
	items = sliceSpace(indexSpace[0][0], indexSpace[0][1]);
	break;
    case 2:
	items = cross(sliceSpace(indexSpace[0][0], indexSpace[0][1]), sliceSpace(indexSpace[1][0], indexSpace[1][1]));
	break;
    default:
	throw new Error("Only 1D and 2D supported as of now");
    }
    var itemSize = indexSpace.length * 2;
    if (outputMem != _Multicore_previousOutputMem) {
	_Multicore_callback =
	    function () {
		_Multicore_callback = doneCallback;
		installItems(fnIdent, itemSize, items);
		_Multicore_barrier.release();
	    };
	_Multicore_mem[_Multicore_funcLoc] = fnIdent;
	_Multicore_mem[_Multicore_nextLoc] = -1000000;
	var value = outputMem instanceof SharedArrayBuffer ? outputMem : outputMem.buffer;
	for ( var w of _Multicore_workers )
	    w.postMessage(["compute", value], [value]);
	_Multicore_previousOutputMem = outputMem;
    }
    else {
	_Multicore_callback = doneCallback;
	installItems(fnIdent, itemSize, items);
    }
    _Multicore_barrier.release();

    function sliceSpace(lo, lim) {
	var items = [];
	var numItem = (lim - lo);
	var sliceHeight = Math.floor(numItem / (4*_Multicore_numWorkers));
	var extra = numItem % (4*_Multicore_numWorkers);
	while (lo < lim) {
	    var hi = lo + sliceHeight;
	    if (extra) {
		hi++;
		extra--;
	    }
	    items.push([lo, hi]);
	    lo = hi;
	}
	return items;
    }

    function cross(as, bs) {
	var items = [];
	for ( var a of as )
	    for ( var b of bs )
		items.push([a, b]);
	return items;
    }

    function installItems(fn, wordsPerItem, items) {
	// Set up the work queue in shared memory
	var m = _Multicore_mem;
	m[_Multicore_sizeLoc] = wordsPerItem;
	m[_Multicore_funcLoc] = fn;
	m[_Multicore_nextLoc] = _Multicore_alloc;
	m[_Multicore_limLoc] = _Multicore_alloc + wordsPerItem*items.length;
	var p = _Multicore_alloc;
	switch (wordsPerItem) {
	case 2:
	    for ( var i of items )
		for ( var j of i )
		    m[p++] = j;
	    break;
	case 4:
	    for ( var i of items )
		for ( var j of i )
		    for ( var k of j )
			m[p++] = k;
	    break;
	}
    }
}

<|endoftext|>