<commit_before>var _ = require('lodash');
var Q = require('q');
var path = require('path');
var axios = require('axios');
var url = require('url');

var types = require('../types');
var errors = require('../errors');
var base64 = require('../utils/base64');
var arrayBuffer = require('../utils/arraybuffer');

function Driver(options) {
    this.options = _.defaults({}, options || {}, {
        repository: null,
        host: "https://api.github.com",
        rawhost: "https://raw.githubusercontent.com",
        username: null,
        token: null,
        includeTokenInRaw: false

    });
    if (!this.options.repository) throw "GitHub driver requires a 'repository' option";
}

// Map some file informations
Driver.prototype.mapFile = function(ref, infos) {
    var rawUrl = infos.download_url || url.resolve(this.options.rawhost, this.options.repository+'/'+ref+'/'+infos.path);
    if (this.options.includeTokenInRaw) rawUrl = rawUrl+'?token='+encodeURIComponent(base64.encode(this.options.username + ':' + this.options.token));

    return {
        "name": infos.name,
        "path": infos.path,
        "content": _.isString(infos.content)? arrayBuffer.fromBase64(infos.content.replace(/(\r\n|\n|\r)/gm,"")) : undefined,
        "sha": infos.sha,
        "type": infos.type,
        "size": infos.size,
        "url": rawUrl
    };
}

// Execute an HTTP API request
Driver.prototype.api = function (httpMethod, method, args, options) {
    var that = this;
    var opts = {
        method: httpMethod,
        url: url.resolve(this.options.host, "/repos/"+this.options.repository+"/"+method)+"?t="+Date.now(),
        headers: {
            'Accept': 'application/vnd.github.v3+json',
            'Content-type': 'application/json;charset=UTF-8'
        }
    };

    if (this.options.username) {
        opts.headers['Authorization'] = 'Basic ' + base64.encode(this.options.username + ':' + this.options.token);
    }

    if (httpMethod == 'get') opts.params = args;
    else opts.data = args;

    return Q(axios(opts))
    .get('data')
    .fail(function(response) {
        if (response instanceof Error) throw response;

        var e = new Error(response.data.message || "Error "+response.status+": "+response.data);
        e.statusCode = response.status;

        throw e;
    });
};

// Get informations about a file
Driver.prototype.stat = function(p, opts) {
    return this.api('get', 'contents'+path.join('/', p), {
        ref: opts.ref
    })
    .then(_.bind(this.mapFile, this, opts.ref));
};

// List files in a directory
Driver.prototype.readdir = function(p, opts) {
    var that = this;

    return this.api('get', 'contents'+path.join('/', p), {
        ref: opts.ref
    })
    .then(function(files) {
        return _.chain(files)
            .map(function(file) {
                return [file.name, that.mapFile(opts.ref, file)];
            })
            .object()
            .value();
    });
};

// Write a file
Driver.prototype.write = function(p, content, opts) {
    var encodedContent, that = this;

    encodedContent = arrayBuffer.enforceBase64(content);

    return that.stat(p, { ref: opts.ref })
    .then(function(infos) {
        return that.api("put", "contents/"+p, {
            branch: opts.ref,
            message: opts.message,
            content: encodedContent,
            sha: infos.sha,
            committer: that.options.commiter
        });
    })
    .then(function(result) {
        result.content.content = encodedContent;
        return that.mapFile(opts.ref, result.content);
    });
};

// Create a new file
Driver.prototype.create = function(p, content, opts) {
    var encodedContent, that = this;

    encodedContent = arrayBuffer.enforceBase64(content);

    return this.stat(p, { ref: opts.ref })
    .then(function() {
        throw errors.fileAlreadyExist(p);
    }, function() {
        return that.api("put", "contents/"+p, {
            message: opts.message,
            branch: opts.ref,
            content: encodedContent,
            commiter: that.options.commiter
        });
    })
    .then(function(result) {
        result.content.content = encodedContent;
        return that.mapFile(opts.ref, result.content);
    });
};

// Delete a file
Driver.prototype.unlink = function(p, opts) {
    var that = this;

    return that.stat(p, { ref: opts.ref })
    .then(function(infos) {
        return that.api('delete', 'contents'+path.join('/', p), {
            message: opts.message,
            sha: infos.sha,
            ref: opts.ref
        });
    });
};

// List branches
Driver.prototype.listBranches = function(p, opts) {
    var that = this;

    return this.api('get', 'branches')
    .then(function(branches) {
        return _.map(branches, function(branch) {
            return {
                name: branch.name,
                commit: branch.commit.sha
            };
        });
    });
};

// Create a branch
Driver.prototype.createBranch = function(name, from) {
    var that = this;

    return this.api("get", "branches/"+from)
    .then(function(branch) {
        return that.api("post", "git/refs", {
            "ref": "refs/heads/"+name,
            "sha": branch.commit.sha
        });
    });
};

// Remove a branch
Driver.prototype.removeBranch = function(name) {
    return this.api("delete", "git/refs/heads/"+name);
};

// Merge branches
Driver.prototype.mergeBranches = function(from, to, opts) {
    var that = this;

    return this.api("post", "merges", {
        base: to,
        head: from,
        commit_message: opts.message
    });
};

// List commits
// todo: handle pagination and opts.start/opts.limit
Driver.prototype.listCommits = function(opts) {
    var that = this;

    return this.api("get", "commits", { sha: opts.ref })
    .then(function(commits) {
        return _.map(commits, function(commit) {
            return {
                "sha": commit.sha,
                "author": _.omit(commit.commit.author, "date"),
                "message": commit.commit.message,
                "date": commit.commit.author.date
            };
        });
    });
};

// Get a single commit
Driver.prototype.getCommit = function(sha) {
    var that = this;

    return this.api("get", "commits/"+sha)
    .then(function(commit) {
        return {
            "sha": commit.sha,
            "author": _.omit(commit.commit.author, "date"),
            "message": commit.commit.message,
            "date": commit.commit.author.date,
            "files": _.map(commit.files, function(file) {
                return {
                    filename: file.filename,
                    patch: file.patch
                };
            })
        };
    });
};

Driver.id = "github";
module.exports = Driver;

<commit_msg>Add commiter when deleting files<commit_after>var _ = require('lodash');
var Q = require('q');
var path = require('path');
var axios = require('axios');
var url = require('url');

var types = require('../types');
var errors = require('../errors');
var base64 = require('../utils/base64');
var arrayBuffer = require('../utils/arraybuffer');

function Driver(options) {
    this.options = _.defaults({}, options || {}, {
        repository: null,
        host: "https://api.github.com",
        rawhost: "https://raw.githubusercontent.com",
        username: null,
        token: null,
        includeTokenInRaw: false

    });
    if (!this.options.repository) throw "GitHub driver requires a 'repository' option";
}

// Map some file informations
Driver.prototype.mapFile = function(ref, infos) {
    var rawUrl = infos.download_url || url.resolve(this.options.rawhost, this.options.repository+'/'+ref+'/'+infos.path);
    if (this.options.includeTokenInRaw) rawUrl = rawUrl+'?token='+encodeURIComponent(base64.encode(this.options.username + ':' + this.options.token));

    return {
        "name": infos.name,
        "path": infos.path,
        "content": _.isString(infos.content)? arrayBuffer.fromBase64(infos.content.replace(/(\r\n|\n|\r)/gm,"")) : undefined,
        "sha": infos.sha,
        "type": infos.type,
        "size": infos.size,
        "url": rawUrl
    };
}

// Execute an HTTP API request
Driver.prototype.api = function (httpMethod, method, args, options) {
    var that = this;
    var opts = {
        method: httpMethod,
        url: url.resolve(this.options.host, "/repos/"+this.options.repository+"/"+method)+"?t="+Date.now(),
        headers: {
            'Accept': 'application/vnd.github.v3+json',
            'Content-type': 'application/json;charset=UTF-8'
        }
    };

    if (this.options.username) {
        opts.headers['Authorization'] = 'Basic ' + base64.encode(this.options.username + ':' + this.options.token);
    }

    if (httpMethod == 'get') opts.params = args;
    else opts.data = args;

    return Q(axios(opts))
    .get('data')
    .fail(function(response) {
        if (response instanceof Error) throw response;

        var e = new Error(response.data.message || "Error "+response.status+": "+response.data);
        e.statusCode = response.status;

        throw e;
    });
};

// Get informations about a file
Driver.prototype.stat = function(p, opts) {
    return this.api('get', 'contents'+path.join('/', p), {
        ref: opts.ref
    })
    .then(_.bind(this.mapFile, this, opts.ref));
};

// List files in a directory
Driver.prototype.readdir = function(p, opts) {
    var that = this;

    return this.api('get', 'contents'+path.join('/', p), {
        ref: opts.ref
    })
    .then(function(files) {
        return _.chain(files)
            .map(function(file) {
                return [file.name, that.mapFile(opts.ref, file)];
            })
            .object()
            .value();
    });
};

// Write a file
Driver.prototype.write = function(p, content, opts) {
    var encodedContent, that = this;

    encodedContent = arrayBuffer.enforceBase64(content);

    return that.stat(p, { ref: opts.ref })
    .then(function(infos) {
        return that.api("put", "contents/"+p, {
            branch: opts.ref,
            message: opts.message,
            content: encodedContent,
            sha: infos.sha,
            committer: that.options.commiter
        });
    })
    .then(function(result) {
        result.content.content = encodedContent;
        return that.mapFile(opts.ref, result.content);
    });
};

// Create a new file
Driver.prototype.create = function(p, content, opts) {
    var encodedContent, that = this;

    encodedContent = arrayBuffer.enforceBase64(content);

    return this.stat(p, { ref: opts.ref })
    .then(function() {
        throw errors.fileAlreadyExist(p);
    }, function() {
        return that.api("put", "contents/"+p, {
            message: opts.message,
            branch: opts.ref,
            content: encodedContent,
            commiter: that.options.commiter
        });
    })
    .then(function(result) {
        result.content.content = encodedContent;
        return that.mapFile(opts.ref, result.content);
    });
};

// Delete a file
Driver.prototype.unlink = function(p, opts) {
    var that = this;

    return that.stat(p, { ref: opts.ref })
    .then(function(infos) {
        return that.api('delete', 'contents'+path.join('/', p), {
            message: opts.message,
            sha: infos.sha,
            ref: opts.ref,
            commiter: that.options.commiter
        });
    });
};

// List branches
Driver.prototype.listBranches = function(p, opts) {
    var that = this;

    return this.api('get', 'branches')
    .then(function(branches) {
        return _.map(branches, function(branch) {
            return {
                name: branch.name,
                commit: branch.commit.sha
            };
        });
    });
};

// Create a branch
Driver.prototype.createBranch = function(name, from) {
    var that = this;

    return this.api("get", "branches/"+from)
    .then(function(branch) {
        return that.api("post", "git/refs", {
            "ref": "refs/heads/"+name,
            "sha": branch.commit.sha
        });
    });
};

// Remove a branch
Driver.prototype.removeBranch = function(name) {
    return this.api("delete", "git/refs/heads/"+name);
};

// Merge branches
Driver.prototype.mergeBranches = function(from, to, opts) {
    var that = this;

    return this.api("post", "merges", {
        base: to,
        head: from,
        commit_message: opts.message
    });
};

// List commits
// todo: handle pagination and opts.start/opts.limit
Driver.prototype.listCommits = function(opts) {
    var that = this;

    return this.api("get", "commits", { sha: opts.ref })
    .then(function(commits) {
        return _.map(commits, function(commit) {
            return {
                "sha": commit.sha,
                "author": _.omit(commit.commit.author, "date"),
                "message": commit.commit.message,
                "date": commit.commit.author.date
            };
        });
    });
};

// Get a single commit
Driver.prototype.getCommit = function(sha) {
    var that = this;

    return this.api("get", "commits/"+sha)
    .then(function(commit) {
        return {
            "sha": commit.sha,
            "author": _.omit(commit.commit.author, "date"),
            "message": commit.commit.message,
            "date": commit.commit.author.date,
            "files": _.map(commit.files, function(file) {
                return {
                    filename: file.filename,
                    patch: file.patch
                };
            })
        };
    });
};

Driver.id = "github";
module.exports = Driver;

<|endoftext|>


<commit_before>// optimize images
// just  write over files because there are already copies
module.exports = function(grunt) {
  "use strict";

  return {
    dist: {
      options: '<%= happyplan.imagemin %>',
      files: [{
        expand: true,
        cwd: "<%= happyplan.path.dist.assets.images %>",
        src: ["**/*"],
        dest: "<%= happyplan.path.dist.assets.images %>"
      },
      {
        expand: true,
        cwd: "<%= happyplan.path.dist.media %>",
        src: ["**/*"],
        dest: "<%= happyplan.path.dist.media %>"
      }]
    }
  }
}
<commit_msg>Fix imagemin when there is child folders<commit_after>// optimize images
// just  write over files because there are already copies
module.exports = function(grunt) {
  "use strict";

  return {
    dist: {
      options: '<%= happyplan.imagemin %>',
      files: [{
        expand: true,
        cwd: "<%= happyplan.path.dist.assets.images %>",
        src: ["**/*.*"],
        dest: "<%= happyplan.path.dist.assets.images %>"
      },
      {
        expand: true,
        cwd: "<%= happyplan.path.dist.media %>",
        src: ["**/*.*"],
        dest: "<%= happyplan.path.dist.media %>"
      }]
    }
  }
}
<|endoftext|>


<commit_before>var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var blobstoreAbi = require('./blobstore.abi.json');
var blobstoreContract = web3.eth.contract(blobstoreAbi);
var blobstoreAddress = '0x3c531591cb807e01404574076f429d205f5ee981';
var blobstore = blobstoreContract.at(blobstoreAddress);

// https://github.com/bluedroplet/blobstore-ethereum/blob/c65287e0ff249fec047834e7895cb46c0e090228/blobstore.sol
// Solidity version: 0.1.7-f86451cd/.-Emscripten/clang/int linked to libethereum-1.1.0-35b67881/.-Emscripten/clang/int

var getBlobHash = function(blob) {
  return '0x' + web3.sha3(blob.toString('ascii'));
}

var getBlobBlock = function(hash) {
  // Determine the block that includes the transaction for this blob.
  return blobstore.getBlobBlock(hash, {}, 'latest').toFixed();
}

var storeBlob = function(blob) {
  // Determine hash of blob.
  var hash = getBlobHash(blob);
  // Check if this blob is in a block yet.
  if (getBlobBlock(hash) == 0) {
    // Calculate maximum transaction gas.
    var gas = 44800 + 78 * blob.length;
    // Broadcast the transaction. If this blob is already in a pending
    // transaction, or has just been mined, this will be handled by the
    // contract.
    blobstore.storeBlob('0x' + blob.toString('hex'), {gas: gas});
  }
  return hash;
}

var getBlob = function(hash, callback) {
  var blobBlock = getBlobBlock(hash);
  if (blobBlock == 0) {
    // The blob isn't in a block yet. See if it is in a pending transaction.
    var txids = web3.eth.getBlock('pending').transactions;
    for (var i in txids) {
      var tx = web3.eth.getTransaction(txids[i]);
      if (tx.to != blobstoreAddress) {
        continue;
      }
      // Extract the blob from the transaction.
      var length = parseInt(tx.input.substr(74, 64), 16);
      var blob = new Buffer(tx.input.substr(138, length * 2), 'hex');
      // Does it have the correct hash?
      if (getBlobHash(blob) == hash) {
        callback(null, blob);
        return;
      }
    }
    // We didn't find the blob. Check in the blocks one more time in case it
    // just got mined and we missed it.
    blobBlock = getBlobBlock(hash);
    if (blobBlock == 0) {
      // We didn't find it. Report the Error.
      callback('error');
      return;
    }
  }
  // Start searching for the log at least an hour in the past in case of block
  // re-arrangement.
  var fromBlock = Math.min(blobBlock, web3.eth.blockNumber - 200);
  var filter = web3.eth.filter({fromBlock: fromBlock, toBlock: 'latest', address: blobstoreAddress, topics: [hash]});
  filter.get(function(error, result) {
    if (result != 0) {
      var length = parseInt(result[0].data.substr(66, 64), 16);
      callback(null, new Buffer(result[0].data.substr(130, length * 2), 'hex'));
    }
    else {
      callback('error');
    }
  });
}

module.exports = {
  storeBlob: storeBlob,
  getBlobBlock: getBlobBlock,
  getBlob: getBlob
};
<commit_msg>Fix typo.<commit_after>var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var blobstoreAbi = require('./blobstore.abi.json');
var blobstoreContract = web3.eth.contract(blobstoreAbi);
var blobstoreAddress = '0x3c531591cb807e01404574076f429d205f5ee981';
var blobstore = blobstoreContract.at(blobstoreAddress);

// https://github.com/bluedroplet/blobstore-ethereum/blob/c65287e0ff249fec047834e7895cb46c0e090228/blobstore.sol
// Solidity version: 0.1.7-f86451cd/.-Emscripten/clang/int linked to libethereum-1.1.0-35b67881/.-Emscripten/clang/int

var getBlobHash = function(blob) {
  return '0x' + web3.sha3(blob.toString('ascii'));
}

var getBlobBlock = function(hash) {
  // Determine the block that includes the transaction for this blob.
  return blobstore.getBlobBlock(hash, {}, 'latest').toFixed();
}

var storeBlob = function(blob) {
  // Determine hash of blob.
  var hash = getBlobHash(blob);
  // Check if this blob is in a block yet.
  if (getBlobBlock(hash) == 0) {
    // Calculate maximum transaction gas.
    var gas = 44800 + 78 * blob.length;
    // Broadcast the transaction. If this blob is already in a pending
    // transaction, or has just been mined, this will be handled by the
    // contract.
    blobstore.storeBlob('0x' + blob.toString('hex'), {gas: gas});
  }
  return hash;
}

var getBlob = function(hash, callback) {
  var blobBlock = getBlobBlock(hash);
  if (blobBlock == 0) {
    // The blob isn't in a block yet. See if it is in a pending transaction.
    var txids = web3.eth.getBlock('pending').transactions;
    for (var i in txids) {
      var tx = web3.eth.getTransaction(txids[i]);
      if (tx.to != blobstoreAddress) {
        continue;
      }
      // Extract the blob from the transaction.
      var length = parseInt(tx.input.substr(74, 64), 16);
      var blob = new Buffer(tx.input.substr(138, length * 2), 'hex');
      // Does it have the correct hash?
      if (getBlobHash(blob) == hash) {
        callback(null, blob);
        return;
      }
    }
    // We didn't find the blob. Check in the blocks one more time in case it
    // just got mined and we missed it.
    blobBlock = getBlobBlock(hash);
    if (blobBlock == 0) {
      // We didn't find it. Report the Error.
      callback('error');
      return;
    }
  }
  // Start searching for the log at least an hour in the past in case of block
  // re-arrangement.
  var fromBlock = Math.min(blobBlock, web3.eth.blockNumber - 200);
  var filter = web3.eth.filter({fromBlock: fromBlock, toBlock: 'latest', address: blobstoreAddress, topics: [hash]});
  filter.get(function(error, result) {
    if (result.length != 0) {
      var length = parseInt(result[0].data.substr(66, 64), 16);
      callback(null, new Buffer(result[0].data.substr(130, length * 2), 'hex'));
    }
    else {
      callback('error');
    }
  });
}

module.exports = {
  storeBlob: storeBlob,
  getBlobBlock: getBlobBlock,
  getBlob: getBlob
};
<|endoftext|>


<commit_before>/**
 * Copyright 2014 wildbits.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
app.views = app.views || {};

app.views.Map = (function() {

    var NASA_WMS_HOST = 'thredds.jpl.nasa.gov';

    var PROXY_NASA_WMS_HOST = 'proxy-thredds-jpl-nasa-gov.herokuapp.com';

    var SALINITY_PATH = '/thredds/wms/ncml_aggregation/SalinityDensity/aquarius/aggregate__AQUARIUS_L3_SSS_SMI_MONTHLY_V3.ncml';

    var TEMPERATURE_PATH = '/thredds/wms/ncml_aggregation/OceanTemperature/amsre/aggregate__AMSRE_L3_SST_1DEG_1MO.ncml';

    var MAX_ZOOM = 6;

    var INIT_ZOOM = 3;

    var MIN_ZOOM = 2;

    var MONTHS = {
        'january': '01', february: '02', march: '03', april: '04', may: '05', june: '06', july: '07', august: '08',
        'september': '09', october: '10', november: '11', december: '12'
    };

    var TEMP_TIME = {
        january: '2010-01-16T12:00:00.000Z',
        february: '2010-02-15T00:00:00.000Z',
        march: '2010-03-16T12:00:00.000Z',
        april: '2010-04-16T00:00:00.000Z',
        may: '2010-05-16T12:00:00.000Z',
        june: '2010-06-16T00:00:00.000Z',
        july: '2010-07-16T12:00:00.000Z',
        august: '2010-08-16T12:00:00.000Z',
        september: '2010-09-16T00:00:00.000Z',
        october: '2010-10-16T12:00:00.000Z',
        november: '2010-11-16T00:00:00.000Z',
        december: '2010-12-16T12:00:00.000Z'
    };

    var getIsoTime = function (year, month) {
        return year + '-' + month + '-01T00:00:00.000Z';
    };

    var getTime = function (month) {
        var lastYear = new Date().getFullYear() - 1;
        return getIsoTime(lastYear, MONTHS[month]);
    };

    var initPos = [47.3604383, 8.5418468];

    var icon = L.icon({
        // original img is 98 x 131
        iconUrl: 'img/marker.png',
        iconRetinaUrl: 'img/marker.png',
        iconSize: [49, 65],
        iconAnchor: [24, 65]
    });

    var buildSalinityTile = function (month) {
        var url = 'http://' + NASA_WMS_HOST + SALINITY_PATH;
        return L.tileLayer.wms(url, {
            detectRetina: true,
            zoom: INIT_ZOOM,
            minZoom: MIN_ZOOM,
            maxZoom: MAX_ZOOM,
            layers: 'l3m_data',
            format: 'image/png',
            version: '1.3.0',
            transparent: true,
            styles: 'boxfill/ferret',
            time: getTime(month),
            attribution: '© Nasa PO.DAAC'
        });
    };

    var buildSalinityFeatureInfo = function (month) {
        var url = 'http://' + PROXY_NASA_WMS_HOST + SALINITY_PATH;
        return new app.map.WmsFeatureInfo(url, {
            crs: 'CRS:84',
            version: '1.3.0',
            query_layers: 'l3m_data',
            padding: 0.5,
            time: getTime(month)
        });
    };

    var buildTemperatureFeatureInfo = function (month) {
        var url = 'http://' + PROXY_NASA_WMS_HOST + TEMPERATURE_PATH;
        return new app.map.WmsFeatureInfo(url, {
            crs: 'CRS:84',
            version: '1.3.0',
            query_layers: 'tos',
            padding: 0.5,
            time: TEMP_TIME[month]
        });
    };

    return function (tag) {

        var setCenter = function (e) {
            marker.setLatLng(e.latlng);
            map.panTo(e.latlng);
        };

        var locate = function () {
            moved = false;
            map.locate({
                maxZoom: INIT_ZOOM,
                enableHighAccuracy: true
            });
        };

        var m = 'january';

        var moved = false;

        var salinityTile = undefined, salinityFeature = undefined, temperatureFeature = undefined;

        var map = L.map(tag, {
            zoomControl: false,
            zoom: INIT_ZOOM,
            minZoom: MIN_ZOOM,
            maxZoom: MAX_ZOOM,
            worldCopyJump: true
        });

        var marker = L.marker(initPos, {
            icon: icon
        });

        marker.addTo(map);

        var baseTile = L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}', {
            detectRetina: true,
            zoom: INIT_ZOOM,
            minZoom: MIN_ZOOM,
            maxZoom: MAX_ZOOM,
            attribution: '© Esri et al.'
        });

        baseTile.addTo(map);

        map.setView(initPos, INIT_ZOOM);

        map.on('locationfound', function (e) {
            if (!moved) {
                setCenter(e);
            }
        });

        map.on('dblclick', function (e) {
            moved = true;
            setCenter(e);
        });

        var eventTypes = [];

        return {

            on: function (type, callback) {
                eventTypes.push(type);
                marker.on(type, callback);
            },

            setMonth: function (month) {
                m = month;
                if (salinityTile) {
                    map.removeLayer(salinityTile)
                }
                salinityTile = buildSalinityTile(m);
                salinityTile.addTo(map);

                salinityFeature = buildSalinityFeatureInfo(m);

                temperatureFeature = buildTemperatureFeatureInfo(m);
            },

            locate: locate,

            setView: function (latitude, longitude) {
                var latLng = new L.LatLng(latitude, longitude);
                map.setView(latLng, INIT_ZOOM);
                marker.setLatLng(latLng);
            },

            getMonth: function () {
                return m;
            },

            getMarkerCenter: function () {
                return marker.getLatLng();
            },

            getSalinity: function (latLng, callback) {
                salinityFeature.getFeatureInfo(latLng, callback);
            },

            getTemperature: function (latLng, callback) {
                temperatureFeature.getFeatureInfo(latLng, callback);
            },

            unRegister: function () {
                map.off('dblclick');
                map.off('locationfound');
                for (var i = 0 ; i < eventTypes.length ; i++) {
                    marker.off(eventTypes[i]);
                }
            }

        };

    };

})();<commit_msg>LEAD-1 - uri scheme is hardcoded when accessing proxy-thredds-jpl-nasa-gov.herokuapp.com<commit_after>/**
 * Copyright 2014 wildbits.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
app.views = app.views || {};

app.views.Map = (function() {

    var NASA_WMS_HOST = 'thredds.jpl.nasa.gov';

    var PROXY_NASA_WMS_HOST = 'proxy-thredds-jpl-nasa-gov.herokuapp.com';

    var SALINITY_PATH = '/thredds/wms/ncml_aggregation/SalinityDensity/aquarius/aggregate__AQUARIUS_L3_SSS_SMI_MONTHLY_V3.ncml';

    var TEMPERATURE_PATH = '/thredds/wms/ncml_aggregation/OceanTemperature/amsre/aggregate__AMSRE_L3_SST_1DEG_1MO.ncml';

    var MAX_ZOOM = 6;

    var INIT_ZOOM = 3;

    var MIN_ZOOM = 2;

    var MONTHS = {
        'january': '01', february: '02', march: '03', april: '04', may: '05', june: '06', july: '07', august: '08',
        'september': '09', october: '10', november: '11', december: '12'
    };

    var TEMP_TIME = {
        january: '2010-01-16T12:00:00.000Z',
        february: '2010-02-15T00:00:00.000Z',
        march: '2010-03-16T12:00:00.000Z',
        april: '2010-04-16T00:00:00.000Z',
        may: '2010-05-16T12:00:00.000Z',
        june: '2010-06-16T00:00:00.000Z',
        july: '2010-07-16T12:00:00.000Z',
        august: '2010-08-16T12:00:00.000Z',
        september: '2010-09-16T00:00:00.000Z',
        october: '2010-10-16T12:00:00.000Z',
        november: '2010-11-16T00:00:00.000Z',
        december: '2010-12-16T12:00:00.000Z'
    };

    var getIsoTime = function (year, month) {
        return year + '-' + month + '-01T00:00:00.000Z';
    };

    var getTime = function (month) {
        var lastYear = new Date().getFullYear() - 1;
        return getIsoTime(lastYear, MONTHS[month]);
    };

    var initPos = [47.3604383, 8.5418468];

    var icon = L.icon({
        // original img is 98 x 131
        iconUrl: 'img/marker.png',
        iconRetinaUrl: 'img/marker.png',
        iconSize: [49, 65],
        iconAnchor: [24, 65]
    });

    var buildSalinityTile = function (month) {
        var url = window.location.protocol + '//' + NASA_WMS_HOST + SALINITY_PATH;
        return L.tileLayer.wms(url, {
            detectRetina: true,
            zoom: INIT_ZOOM,
            minZoom: MIN_ZOOM,
            maxZoom: MAX_ZOOM,
            layers: 'l3m_data',
            format: 'image/png',
            version: '1.3.0',
            transparent: true,
            styles: 'boxfill/ferret',
            time: getTime(month),
            attribution: '© Nasa PO.DAAC'
        });
    };

    var buildSalinityFeatureInfo = function (month) {
        var url = window.location.protocol + '//' + PROXY_NASA_WMS_HOST + SALINITY_PATH;
        return new app.map.WmsFeatureInfo(url, {
            crs: 'CRS:84',
            version: '1.3.0',
            query_layers: 'l3m_data',
            padding: 0.5,
            time: getTime(month)
        });
    };

    var buildTemperatureFeatureInfo = function (month) {
        var url = window.location.protocol + '//' + PROXY_NASA_WMS_HOST + TEMPERATURE_PATH;
        return new app.map.WmsFeatureInfo(url, {
            crs: 'CRS:84',
            version: '1.3.0',
            query_layers: 'tos',
            padding: 0.5,
            time: TEMP_TIME[month]
        });
    };

    return function (tag) {

        var setCenter = function (e) {
            marker.setLatLng(e.latlng);
            map.panTo(e.latlng);
        };

        var locate = function () {
            moved = false;
            map.locate({
                maxZoom: INIT_ZOOM,
                enableHighAccuracy: true
            });
        };

        var m = 'january';

        var moved = false;

        var salinityTile = undefined, salinityFeature = undefined, temperatureFeature = undefined;

        var map = L.map(tag, {
            zoomControl: false,
            zoom: INIT_ZOOM,
            minZoom: MIN_ZOOM,
            maxZoom: MAX_ZOOM,
            worldCopyJump: true
        });

        var marker = L.marker(initPos, {
            icon: icon
        });

        marker.addTo(map);

        var baseTile = L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}', {
            detectRetina: true,
            zoom: INIT_ZOOM,
            minZoom: MIN_ZOOM,
            maxZoom: MAX_ZOOM,
            attribution: '© Esri et al.'
        });

        baseTile.addTo(map);

        map.setView(initPos, INIT_ZOOM);

        map.on('locationfound', function (e) {
            if (!moved) {
                setCenter(e);
            }
        });

        map.on('dblclick', function (e) {
            moved = true;
            setCenter(e);
        });

        var eventTypes = [];

        return {

            on: function (type, callback) {
                eventTypes.push(type);
                marker.on(type, callback);
            },

            setMonth: function (month) {
                m = month;
                if (salinityTile) {
                    map.removeLayer(salinityTile)
                }
                salinityTile = buildSalinityTile(m);
                salinityTile.addTo(map);

                salinityFeature = buildSalinityFeatureInfo(m);

                temperatureFeature = buildTemperatureFeatureInfo(m);
            },

            locate: locate,

            setView: function (latitude, longitude) {
                var latLng = new L.LatLng(latitude, longitude);
                map.setView(latLng, INIT_ZOOM);
                marker.setLatLng(latLng);
            },

            getMonth: function () {
                return m;
            },

            getMarkerCenter: function () {
                return marker.getLatLng();
            },

            getSalinity: function (latLng, callback) {
                salinityFeature.getFeatureInfo(latLng, callback);
            },

            getTemperature: function (latLng, callback) {
                temperatureFeature.getFeatureInfo(latLng, callback);
            },

            unRegister: function () {
                map.off('dblclick');
                map.off('locationfound');
                for (var i = 0 ; i < eventTypes.length ; i++) {
                    marker.off(eventTypes[i]);
                }
            }

        };

    };

})();<|endoftext|>


<commit_before>;(function (FMS, Backbone, _, $) {
    _.extend( FMS, {
        DetailsView: FMS.FMSView.extend({
            template: 'details',
            id: 'details-page',
            prev: 'photo',
            next: 'submit',

            onClickButtonPrev: function() {
                this.model.set('title', $('#form_title').val());
                this.model.set('details', $('#form_detail').val());
                this.model.set('category', $('#form_category').val());
                this.navigate( this.prev );
            },

            onClickButtonNext: function() {
                this.clearValidationErrors();
                var valid = 1;

                if ( !$('#form_title').val() ) {
                    valid = 0;
                    this.validationError( 'form_title', FMS.validationStrings.title );
                }

                if ( !$('#form_detail').val() ) {
                    valid = 0;
                    this.validationError( 'form_detail', FMS.validationStrings.detail );
                }

                var cat = $('#form_category').val();
                if ( cat == '-- Pick a category --' ) {
                    valid = 0;
                    this.validationError( 'form_category', FMS.validationStrings.category );
                }

                if ( valid ) {
                    this.clearValidationErrors();
                    this.model.set('title', $('#form_title').val());
                    this.model.set('detail', $('#form_detail').val());
                    this.model.set('category', $('#form_category').val());
                    this.navigate( this.next );
                }
            }
        })
    });
})(FMS, Backbone, _, $);
<commit_msg>details not detail<commit_after>;(function (FMS, Backbone, _, $) {
    _.extend( FMS, {
        DetailsView: FMS.FMSView.extend({
            template: 'details',
            id: 'details-page',
            prev: 'photo',
            next: 'submit',

            onClickButtonPrev: function() {
                this.model.set('title', $('#form_title').val());
                this.model.set('details', $('#form_detail').val());
                this.model.set('category', $('#form_category').val());
                this.navigate( this.prev );
            },

            onClickButtonNext: function() {
                this.clearValidationErrors();
                var valid = 1;

                if ( !$('#form_title').val() ) {
                    valid = 0;
                    this.validationError( 'form_title', FMS.validationStrings.title );
                }

                if ( !$('#form_detail').val() ) {
                    valid = 0;
                    this.validationError( 'form_detail', FMS.validationStrings.detail );
                }

                var cat = $('#form_category').val();
                if ( cat == '-- Pick a category --' ) {
                    valid = 0;
                    this.validationError( 'form_category', FMS.validationStrings.category );
                }

                if ( valid ) {
                    this.clearValidationErrors();
                    this.model.set('title', $('#form_title').val());
                    this.model.set('details', $('#form_detail').val());
                    this.model.set('category', $('#form_category').val());
                    this.navigate( this.next );
                }
            }
        })
    });
})(FMS, Backbone, _, $);
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 *
 * HTTP endpoints to manage server nics
 */

var common = require('../common');
var restify = require('restify');
var util = require('util');
var validation = require('../validation/endpoints');
var ModelServer = require('../models/server');

function Nic() {}

Nic.update = function (req, res, next) {
    var rules = {
        'action': ['isStringType'],
        'nics': ['isArrayType']
    };

    if (validation.ensureParamsValid(req, res, rules)) {
        next();
        return;
    }

    var action = req.params.action;
    var self = this;
    var server = req.stash.server;
    var uuid = server.uuid;

    if (action !== 'update' && action !== 'replace' && action !== 'delete') {
        res.send(500, validation.formatValidationErrors([ {
            param: 'action',
            message: 'action must be \'update\', \'replace\', or \'delete\''
        }]));
        next();
        return;
    }

    var wfParams = {
        nic_action: action,
        nics: req.params.nics,
        server: server,
        server_uuid: uuid,
        target: uuid
    };

    self.log.info('Instantiating server-update-nics workflow');
    ModelServer.getWorkflow().getClient().createJob(
        'server-update-nics',
        wfParams,
        function (error, job) {
            if (error) {
                self.log.error('Error in workflow: %s', error.message);
                next(new restify.InternalError(error.message));
                return;
            }

            self.log.info('server-update-nics workflow started with job '
                + 'UUID ' + job.uuid);
            res.send(202, { job_uuid: job.uuid });
            return;
        });
};

Nic.updateTask = function (req, res, next) {
    var self = this;
    var server = req.stash.server;

    req.log.info({server: server.uuid, params: req.params},
        'sending nic_update task');
    server.sendProvisionerTask(
        'server_update_nics',
        req.params,
        ModelServer.createProvisionerEventHandler(self, req.params.jobid),
        function (error, task_id) {
            res.send({ id: task_id });
            return next();
        });
};

function attachTo(http) {
    var before = [
        function (req, res, next) {
            if (!req.params.server_uuid) {
                next();
                return;
            }

            req.stash = {};
            req.stash.server = new ModelServer(req.params.server_uuid);
            req.stash.server.getRaw(function (error, server) {
                // Check if any servers were returned
                if (!server) {
                    var errorMsg
                        = 'Server ' + req.params.server_uuid + ' not found';
                    next(
                        new restify.ResourceNotFoundError(errorMsg));
                    return;
                }
                next();
            });
        }
    ];

    // Update nics on the server
    http.put(
        { path: '/servers/:server_uuid/nics', name: 'NicUpdate' },
        before, Nic.update);

    // Start provisioner task for updating nics
    http.post(
        { path: '/servers/:server_uuid/nics/update', name: 'UpdateNicTask' },
        before, Nic.updateTask);
}

exports.attachTo = attachTo;
<commit_msg>CNAPI-234: NicUpdate endpoint shouldn't allow updating nics on servers that aren't setup<commit_after>/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 *
 * HTTP endpoints to manage server nics
 */

var common = require('../common');
var restify = require('restify');
var util = require('util');
var validation = require('../validation/endpoints');
var ModelServer = require('../models/server');

function Nic() {}

Nic.update = function (req, res, next) {
    var rules = {
        'action': ['isStringType'],
        'nics': ['isArrayType']
    };

    if (validation.ensureParamsValid(req, res, rules)) {
        next();
        return;
    }

    var action = req.params.action;
    var self = this;
    var server = req.stash.server;
    var uuid = server.uuid;

    if (action !== 'update' && action !== 'replace' && action !== 'delete') {
        res.send(500, validation.formatValidationErrors([ {
            param: 'action',
            message: 'action must be \'update\', \'replace\', or \'delete\''
        }]));
        next();
        return;
    }

    if (server.value.setup !== true) {
        next(new restify.InternalError('Server is not setup'));
        return;
    }

    var wfParams = {
        nic_action: action,
        nics: req.params.nics,
        server: server,
        server_uuid: uuid,
        target: uuid
    };

    self.log.info('Instantiating server-update-nics workflow');
    ModelServer.getWorkflow().getClient().createJob(
        'server-update-nics',
        wfParams,
        function (error, job) {
            if (error) {
                self.log.error('Error in workflow: %s', error.message);
                next(new restify.InternalError(error.message));
                return;
            }

            self.log.info('server-update-nics workflow started with job '
                + 'UUID ' + job.uuid);
            res.send(202, { job_uuid: job.uuid });
            return;
        });
};

Nic.updateTask = function (req, res, next) {
    var self = this;
    var server = req.stash.server;

    req.log.info({server: server.uuid, params: req.params},
        'sending nic_update task');
    server.sendProvisionerTask(
        'server_update_nics',
        req.params,
        ModelServer.createProvisionerEventHandler(self, req.params.jobid),
        function (error, task_id) {
            res.send({ id: task_id });
            return next();
        });
};

function attachTo(http) {
    var before = [
        function (req, res, next) {
            if (!req.params.server_uuid) {
                next();
                return;
            }

            req.stash = {};
            req.stash.server = new ModelServer(req.params.server_uuid);
            req.stash.server.getRaw(function (error, server) {
                // Check if any servers were returned
                if (!server) {
                    var errorMsg
                        = 'Server ' + req.params.server_uuid + ' not found';
                    next(
                        new restify.ResourceNotFoundError(errorMsg));
                    return;
                }
                next();
            });
        }
    ];

    // Update nics on the server
    http.put(
        { path: '/servers/:server_uuid/nics', name: 'NicUpdate' },
        before, Nic.update);

    // Start provisioner task for updating nics
    http.post(
        { path: '/servers/:server_uuid/nics/update', name: 'UpdateNicTask' },
        before, Nic.updateTask);
}

exports.attachTo = attachTo;
<|endoftext|>


<commit_before>(function () {
  'use strict';

  /**
   * @fileoverview Defines a helper object for handling configuration
   *  differences between environments.
   * @author Mathias Kretschek <mathias@kretschek.com.br>
   */

  var _ = require('underscore'),
    e12e;

  /**
   * Returns a value based on the environment in which the code is running.
   *
   * If no arguments are passed, returns the current environment.
   *
   * @see README.md for usage details.
   *
   * @param {string|array|object=} opt_a A string naming a matching
   *  environment, an array of matching environments or a map object mapping
   *  environments (keys) to values (value). If the value in the map is a
   *  function, it will be called without arguments and its return value
   *  will be used.
   * @param {*} opt_b The value to be returned if the current
   *  environment is matched or a fallback value in case a value is not
   *  found in the map passed in the first param. If it's a function, it
   *  will be called without arguments and its return value will be used.
   * @param {*} opt_c The fallback value in case the current environment is
   *  not matched by the string or array passed to the first param. Has no
   *  effect if the first param is a map. If it's a function, it will be
   *  called without arguments and its return value will be used.
   *
   * @returns {*} Returns a string indicating the current environment, a
   *  boolean indicating if the current environment was matched or an
   *  appropriate value for the current environment, depending on the
   *  number of params given. See above for more details.
   */
  e12e = function (opt_a, opt_b, opt_c) {
    if (arguments.length === 0) {
      return e12e.env;
    }

    opt_b = (opt_b && typeof opt_b === 'function') ? opt_b() : opt_b;
    opt_c = (opt_c && typeof opt_c === 'function') ? opt_c() : opt_c;

    if (typeof opt_a === 'object' && !Array.isArray(opt_a)) {
      var val = opt_a[e12e.env];
      return val === undefined ? opt_b :
        typeof val === 'function' ? val() : val;
    } else {
      if (arguments.length === 1) {
        return envIs(opt_a);
      }
      return envIs(opt_a) ? opt_b : opt_c;
    }
  };

  /**
   * Environmentize configuration object.
   */
  e12e.settings = {
    /**
    * Array containing all expected environments.
    * @type {Array<string>}
    */
    environments : [
      'development',
      'test',
      'integration',
      'staging',
      'production'
    ],

    /**
     * The default environment in case `process.env.NODE_ENV` is not
     * defined.
     * @type {string}
     */
    defaultEnv : 'development'
  };

  /**
   * Configures the environmentize function and creates methods for
   * each supported environment.
   *
   *

   * // Creates three methods: `e12e.dev()`, `e12e.ci()` and `e12e.prod()`.
   * e12e.setup({
   *  environments : ['dev', 'ci', 'prod']
   * });
   *

   *
   * It also resets {@code e12e.env} in case the default environment was
   * changed.
   *
   * @param {object=} opt_options An object that updates the current
   *  options. Note that the options are not reset before updating them.
   *  If an object is not given, will simply recreate the environment
   *  methods.
   */
  e12e.setup = function (opt_options) {
    var s = e12e.settings,
      oldEnvs;

    if (opt_options) {
      removeEnvMethods(s.environments);
      _.extend(s, opt_options);
    }
    createEnvMethods(s.environments);

    e12e.env = process.env.NODE_ENV || s.defaultEnv;
  };

  /**
   * Checks if {@code env} matches the current environment.
   *
   * @param {string|Array<string>} env The environment or array of
   *  environments against which the current environment should be matched.
   * @return {boolean} Whether the current environment matches {@code env}.
   */
  function envIs(env) {
    if (typeof env === 'string') {
      return e12e.env === env;
    } else if (Array.isArray(env)) {
      return !!~env.indexOf(e12e.env);
    }
    return false;
  }

  /**
   * Removes the environment methods for the given environments.
   * @param {Array<string>} envs Array containing the environments whose
   *  methods should be removed.
   */
  function removeEnvMethods(envs) {
    var len, i;
    for (i = 0, len = envs.length; i < len; i += 1) {
      delete e12e[envs[i]];
    }
  }

  /**
   * Creates environment methods for the given environments.
   * @param {Array<string>} envs Array containing environments for whom
   *  methods should be created.
   */
  function createEnvMethods(envs) {
    var env, len, i;
    for (i = 0, len = envs.length; i < len; i += 1) {
      env = envs[i];
      e12e[env] = e12e.bind(null, env);
    }
  }

  // Creates default environment methods and sets `e12e.env`.
  e12e.setup();

  module.exports = e12e;
})();
<commit_msg>Cleaned up unused var.<commit_after>(function () {
  'use strict';

  /**
   * @fileoverview Defines a helper object for handling configuration
   *  differences between environments.
   * @author Mathias Kretschek <mathias@kretschek.com.br>
   */

  var _ = require('underscore'),
    e12e;

  /**
   * Returns a value based on the environment in which the code is running.
   *
   * If no arguments are passed, returns the current environment.
   *
   * @see README.md for usage details.
   *
   * @param {string|array|object=} opt_a A string naming a matching
   *  environment, an array of matching environments or a map object mapping
   *  environments (keys) to values (value). If the value in the map is a
   *  function, it will be called without arguments and its return value
   *  will be used.
   * @param {*} opt_b The value to be returned if the current
   *  environment is matched or a fallback value in case a value is not
   *  found in the map passed in the first param. If it's a function, it
   *  will be called without arguments and its return value will be used.
   * @param {*} opt_c The fallback value in case the current environment is
   *  not matched by the string or array passed to the first param. Has no
   *  effect if the first param is a map. If it's a function, it will be
   *  called without arguments and its return value will be used.
   *
   * @returns {*} Returns a string indicating the current environment, a
   *  boolean indicating if the current environment was matched or an
   *  appropriate value for the current environment, depending on the
   *  number of params given. See above for more details.
   */
  e12e = function (opt_a, opt_b, opt_c) {
    if (arguments.length === 0) {
      return e12e.env;
    }

    opt_b = (opt_b && typeof opt_b === 'function') ? opt_b() : opt_b;
    opt_c = (opt_c && typeof opt_c === 'function') ? opt_c() : opt_c;

    if (typeof opt_a === 'object' && !Array.isArray(opt_a)) {
      var val = opt_a[e12e.env];
      return val === undefined ? opt_b :
        typeof val === 'function' ? val() : val;
    } else {
      if (arguments.length === 1) {
        return envIs(opt_a);
      }
      return envIs(opt_a) ? opt_b : opt_c;
    }
  };

  /**
   * Environmentize configuration object.
   */
  e12e.settings = {
    /**
    * Array containing all expected environments.
    * @type {Array<string>}
    */
    environments : [
      'development',
      'test',
      'integration',
      'staging',
      'production'
    ],

    /**
     * The default environment in case `process.env.NODE_ENV` is not
     * defined.
     * @type {string}
     */
    defaultEnv : 'development'
  };

  /**
   * Configures the environmentize function and creates methods for
   * each supported environment.
   *
   *

   * // Creates three methods: `e12e.dev()`, `e12e.ci()` and `e12e.prod()`.
   * e12e.setup({
   *  environments : ['dev', 'ci', 'prod']
   * });
   *

   *
   * It also resets {@code e12e.env} in case the default environment was
   * changed.
   *
   * @param {object=} opt_options An object that updates the current
   *  options. Note that the options are not reset before updating them.
   *  If an object is not given, will simply recreate the environment
   *  methods.
   */
  e12e.setup = function (opt_options) {
    var s = e12e.settings;

    if (opt_options) {
      removeEnvMethods(s.environments);
      _.extend(s, opt_options);
    }
    createEnvMethods(s.environments);

    e12e.env = process.env.NODE_ENV || s.defaultEnv;
  };

  /**
   * Checks if {@code env} matches the current environment.
   *
   * @param {string|Array<string>} env The environment or array of
   *  environments against which the current environment should be matched.
   * @return {boolean} Whether the current environment matches {@code env}.
   */
  function envIs(env) {
    if (typeof env === 'string') {
      return e12e.env === env;
    } else if (Array.isArray(env)) {
      return !!~env.indexOf(e12e.env);
    }
    return false;
  }

  /**
   * Removes the environment methods for the given environments.
   * @param {Array<string>} envs Array containing the environments whose
   *  methods should be removed.
   */
  function removeEnvMethods(envs) {
    var len, i;
    for (i = 0, len = envs.length; i < len; i += 1) {
      delete e12e[envs[i]];
    }
  }

  /**
   * Creates environment methods for the given environments.
   * @param {Array<string>} envs Array containing environments for whom
   *  methods should be created.
   */
  function createEnvMethods(envs) {
    var env, len, i;
    for (i = 0, len = envs.length; i < len; i += 1) {
      env = envs[i];
      e12e[env] = e12e.bind(null, env);
    }
  }

  // Creates default environment methods and sets `e12e.env`.
  e12e.setup();

  module.exports = e12e;
})();
<|endoftext|>


<commit_before>/**
 * Created by rulkens on 25/11/15.
 */
<commit_msg>noise functions<commit_after>/*
 * A speed-improved perlin and simplex noise algorithms for 2D.
 *
 * Based on example code by Stefan Gustavson (stegu@itn.liu.se).
 * Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
 * Better rank ordering method by Stefan Gustavson in 2012.
 * Converted to Javascript by Joseph Gentle.
 *
 * Version 2012-03-09
 *
 * This code was placed in the public domain by its original author,
 * Stefan Gustavson. You may use it as you see fit, but
 * attribution is appreciated.
 *
 */

module.exports = {
    seed : seed,
    perlin2  : perlin2,
    perlin3  : perlin3,
    simplex2 : simplex2,
    simplex3 : simplex3
};

function Grad (x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

Grad.prototype.dot2 = function (x, y) {
    return this.x * x + this.y * y;
};

Grad.prototype.dot3 = function (x, y, z) {
    return this.x * x + this.y * y + this.z * z;
};

var grad3 = [new Grad(1, 1, 0), new Grad(-1, 1, 0), new Grad(1, -1, 0), new Grad(-1, -1, 0),
    new Grad(1, 0, 1), new Grad(-1, 0, 1), new Grad(1, 0, -1), new Grad(-1, 0, -1),
    new Grad(0, 1, 1), new Grad(0, -1, 1), new Grad(0, 1, -1), new Grad(0, -1, -1)];

var p = [151, 160, 137, 91, 90, 15,
    131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
    190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
    88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
    77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244,
    102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196,
    135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123,
    5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42,
    223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
    129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228,
    251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
    49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
    138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180];
// To remove the need for index wrapping, double the permutation table length
var perm = new Array(512);
var gradP = new Array(512);

// This isn't a very good seeding function, but it works ok. It supports 2^16
// different seed values. Write something better if you need more seeds.
function seed (seed) {
    if (seed > 0 && seed < 1) {
        // Scale the seed out
        seed *= 65536;
    }

    seed = Math.floor(seed);
    if (seed < 256) {
        seed |= seed << 8;
    }

    for (var i = 0; i < 256; i++) {
        var v;
        if (i & 1) {
            v = p[i] ^ (seed & 255);
        } else {
            v = p[i] ^ ((seed >> 8) & 255);
        }

        perm[i] = perm[i + 256] = v;
        gradP[i] = gradP[i + 256] = grad3[v % 12];
    }
}

seed(0);

/*
 for(var i=0; i<256; i++) {
 perm[i] = perm[i + 256] = p[i];
 gradP[i] = gradP[i + 256] = grad3[perm[i] % 12];
 }*/

// Skewing and unskewing factors for 2, 3, and 4 dimensions
var F2 = 0.5 * (Math.sqrt(3) - 1);
var G2 = (3 - Math.sqrt(3)) / 6;

var F3 = 1 / 3;
var G3 = 1 / 6;

// 2D simplex noise
function simplex2 (xin, yin) {
    var n0, n1, n2; // Noise contributions from the three corners
    // Skew the input space to determine which simplex cell we're in
    var s = (xin + yin) * F2; // Hairy factor for 2D
    var i = Math.floor(xin + s);
    var j = Math.floor(yin + s);
    var t = (i + j) * G2;
    var x0 = xin - i + t; // The x,y distances from the cell origin, unskewed.
    var y0 = yin - j + t;
    // For the 2D case, the simplex shape is an equilateral triangle.
    // Determine which simplex we are in.
    var i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
    if (x0 > y0) { // lower triangle, XY order: (0,0)->(1,0)->(1,1)
        i1 = 1;
        j1 = 0;
    } else {    // upper triangle, YX order: (0,0)->(0,1)->(1,1)
        i1 = 0;
        j1 = 1;
    }
    // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
    // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
    // c = (3-sqrt(3))/6
    var x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
    var y1 = y0 - j1 + G2;
    var x2 = x0 - 1 + 2 * G2; // Offsets for last corner in (x,y) unskewed coords
    var y2 = y0 - 1 + 2 * G2;
    // Work out the hashed gradient indices of the three simplex corners
    i &= 255;
    j &= 255;
    var gi0 = gradP[i + perm[j]];
    var gi1 = gradP[i + i1 + perm[j + j1]];
    var gi2 = gradP[i + 1 + perm[j + 1]];
    // Calculate the contribution from the three corners
    var t0 = 0.5 - x0 * x0 - y0 * y0;
    if (t0 < 0) {
        n0 = 0;
    } else {
        t0 *= t0;
        n0 = t0 * t0 * gi0.dot2(x0, y0);  // (x,y) of grad3 used for 2D gradient
    }
    var t1 = 0.5 - x1 * x1 - y1 * y1;
    if (t1 < 0) {
        n1 = 0;
    } else {
        t1 *= t1;
        n1 = t1 * t1 * gi1.dot2(x1, y1);
    }
    var t2 = 0.5 - x2 * x2 - y2 * y2;
    if (t2 < 0) {
        n2 = 0;
    } else {
        t2 *= t2;
        n2 = t2 * t2 * gi2.dot2(x2, y2);
    }
    // Add contributions from each corner to get the final noise value.
    // The result is scaled to return values in the interval [-1,1].
    return 70 * (n0 + n1 + n2);
}

// 3D simplex noise
function simplex3 (xin, yin, zin) {
    var n0, n1, n2, n3; // Noise contributions from the four corners

    // Skew the input space to determine which simplex cell we're in
    var s = (xin + yin + zin) * F3; // Hairy factor for 2D
    var i = Math.floor(xin + s);
    var j = Math.floor(yin + s);
    var k = Math.floor(zin + s);

    var t = (i + j + k) * G3;
    var x0 = xin - i + t; // The x,y distances from the cell origin, unskewed.
    var y0 = yin - j + t;
    var z0 = zin - k + t;

    // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
    // Determine which simplex we are in.
    var i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
    var i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
    if (x0 >= y0) {
        if (y0 >= z0) {
            i1 = 1;
            j1 = 0;
            k1 = 0;
            i2 = 1;
            j2 = 1;
            k2 = 0;
        }
        else if (x0 >= z0) {
            i1 = 1;
            j1 = 0;
            k1 = 0;
            i2 = 1;
            j2 = 0;
            k2 = 1;
        }
        else {
            i1 = 0;
            j1 = 0;
            k1 = 1;
            i2 = 1;
            j2 = 0;
            k2 = 1;
        }
    } else {
        if (y0 < z0) {
            i1 = 0;
            j1 = 0;
            k1 = 1;
            i2 = 0;
            j2 = 1;
            k2 = 1;
        }
        else if (x0 < z0) {
            i1 = 0;
            j1 = 1;
            k1 = 0;
            i2 = 0;
            j2 = 1;
            k2 = 1;
        }
        else {
            i1 = 0;
            j1 = 1;
            k1 = 0;
            i2 = 1;
            j2 = 1;
            k2 = 0;
        }
    }
    // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
    // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
    // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
    // c = 1/6.
    var x1 = x0 - i1 + G3; // Offsets for second corner
    var y1 = y0 - j1 + G3;
    var z1 = z0 - k1 + G3;

    var x2 = x0 - i2 + 2 * G3; // Offsets for third corner
    var y2 = y0 - j2 + 2 * G3;
    var z2 = z0 - k2 + 2 * G3;

    var x3 = x0 - 1 + 3 * G3; // Offsets for fourth corner
    var y3 = y0 - 1 + 3 * G3;
    var z3 = z0 - 1 + 3 * G3;

    // Work out the hashed gradient indices of the four simplex corners
    i &= 255;
    j &= 255;
    k &= 255;
    var gi0 = gradP[i + perm[j + perm[k]]];
    var gi1 = gradP[i + i1 + perm[j + j1 + perm[k + k1]]];
    var gi2 = gradP[i + i2 + perm[j + j2 + perm[k + k2]]];
    var gi3 = gradP[i + 1 + perm[j + 1 + perm[k + 1]]];

    // Calculate the contribution from the four corners
    var t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
    if (t0 < 0) {
        n0 = 0;
    } else {
        t0 *= t0;
        n0 = t0 * t0 * gi0.dot3(x0, y0, z0);  // (x,y) of grad3 used for 2D gradient
    }
    var t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
    if (t1 < 0) {
        n1 = 0;
    } else {
        t1 *= t1;
        n1 = t1 * t1 * gi1.dot3(x1, y1, z1);
    }
    var t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
    if (t2 < 0) {
        n2 = 0;
    } else {
        t2 *= t2;
        n2 = t2 * t2 * gi2.dot3(x2, y2, z2);
    }
    var t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
    if (t3 < 0) {
        n3 = 0;
    } else {
        t3 *= t3;
        n3 = t3 * t3 * gi3.dot3(x3, y3, z3);
    }
    // Add contributions from each corner to get the final noise value.
    // The result is scaled to return values in the interval [-1,1].
    return 32 * (n0 + n1 + n2 + n3);

};

// ##### Perlin noise stuff

function fade (t) {
    return t * t * t * (t * (t * 6 - 15) + 10);
}

function lerp (a, b, t) {
    return (1 - t) * a + t * b;
}

// 2D Perlin Noise
function perlin2 (x, y) {
    // Find unit grid cell containing point
    var X = Math.floor(x), Y = Math.floor(y);
    // Get relative xy coordinates of point within that cell
    x = x - X;
    y = y - Y;
    // Wrap the integer cells at 255 (smaller integer period can be introduced here)
    X = X & 255;
    Y = Y & 255;

    // Calculate noise contributions from each of the four corners
    var n00 = gradP[X + perm[Y]].dot2(x, y);
    var n01 = gradP[X + perm[Y + 1]].dot2(x, y - 1);
    var n10 = gradP[X + 1 + perm[Y]].dot2(x - 1, y);
    var n11 = gradP[X + 1 + perm[Y + 1]].dot2(x - 1, y - 1);

    // Compute the fade curve value for x
    var u = fade(x);

    // Interpolate the four results
    return lerp(
        lerp(n00, n10, u),
        lerp(n01, n11, u),
        fade(y));
}

// 3D Perlin Noise
function perlin3 (x, y, z) {
    // Find unit grid cell containing point
    var X = Math.floor(x), Y = Math.floor(y), Z = Math.floor(z);
    // Get relative xyz coordinates of point within that cell
    x = x - X;
    y = y - Y;
    z = z - Z;
    // Wrap the integer cells at 255 (smaller integer period can be introduced here)
    X = X & 255;
    Y = Y & 255;
    Z = Z & 255;

    // Calculate noise contributions from each of the eight corners
    var n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z);
    var n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1);
    var n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z);
    var n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1);
    var n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z);
    var n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1);
    var n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z);
    var n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1);

    // Compute the fade curve value for x, y, z
    var u = fade(x);
    var v = fade(y);
    var w = fade(z);

    // Interpolate
    return lerp(
        lerp(
            lerp(n000, n100, u),
            lerp(n001, n101, u), w),
        lerp(
            lerp(n010, n110, u),
            lerp(n011, n111, u), w),
        v);
}<|endoftext|>


<commit_before>#!/usr/bin/env node
'use strict';

var helpers = require('./helpers');
var fs = require('fs');
var vm = require('vm');
var program = require('commander');
var elasticsearch = require('elasticsearch');
var appInfo = require('../package.json');
var ProgressBar = require('progress');
var bar, key;

var esOptions = {
	index: 'a comma-separated list of index names to search; use _all or empty string to perform the operation on all indices',
	type: 'a comma-separated list of document types to search; leave empty to perform the operation on all types',
	body: 'the body to send along with this request.',
	analyzer: 'The analyzer to use for the query string',
	analyzeWildcard: 'specify whether wildcard and prefix queries should be analyzed (default: false)',
	fields: 'a comma-separated list of fields to return as part of a hit (default: "*")',
	from: 'starting offset (default: 0)',
	q: 'query in the Lucene query string syntax',
	routing: 'a comma-separated list of specific routing values',
	scroll: 'specify how long a consistent view of the index should be maintained for scrolled search (default: 1m)',
	size: 'number of hits to return during each scan',
	sort: 'a comma-separated list of <field>:<direction> pairs',
	timeout: 'explicit operation timeout'
};

var esClientOptions = {
    apiVersion: 'the major version of the Elasticsearch nodes you will be connecting to (default: 2.3)',
    maxRetries: 'How many times should the client try to connect to other nodes before returning a ConnectionFault error (default: 3)',
    requestTimeout: 'Milliseconds before an HTTP request will be aborted and retried. This can also be set per request (default: 30000)',
    deadTimeout: 'Milliseconds that a dead connection will wait before attempting to revive itself (default: 60000)',
    pingTimeout: 'Milliseconds that a ping request can take before timing out (default: 3000)',
    maxSockets: 'Maximum number of concurrent requests that can be made to any node (default: 10)',
    minSockets: 'Minimum number of sockets to keep connected to a node (default: 10)',
    selector: 'Select a connection from the ConnectionPool using roundRobin (default) or random'
};

// setup command line options
program
	.version(appInfo.version, '-v, --version')
	.option('-u, --url <url>', 'the elasticsearch url to connect to')
	.option('-f, --file <file>', 'the file to write data to')
	.option('-m, --max <number>', 'the maximum number of items to export. different than the scroll size', parseInt)
	.option('--transformMeta <js>', 'a javascript function that returns an object that is the transformed meta object')
	.option('--transformSource <js>', 'a javascript function that returns an object that is the transformed source object')
	.option('--transformMetaInit <js>', 'a javascript function that returns an init object that contains helpers for the transform function')
	.option('--transformSourceInit <js>', 'a javascript function that returns an init object that contains helpers for the transform function');

// add es options
for (key in esOptions) {
	if (esOptions.hasOwnProperty(key)) {
		program.option('--' + key + ' <' + key + '>', 'ES OPTION: ' + esOptions[key]);
	}
}

// add client options
for (key in esClientOptions) {
    if (esClientOptions.hasOwnProperty(key)) {
        program.option('--' + key + ' <' + key + '>', 'ES CLIENT OPTION: ' + esClientOptions[key]);
    }
}

// parse arguments
program.parse(process.argv);

// validate url and file
helpers.validateUrlAndFile(program);

// setup a default scroll value
if (!program.scroll) {
	program.scroll = '1m';
}

// these can be strings or the contents of files
['transformMeta', 'transformSource', 'transformMetaInit', 'transformSourceInit'].forEach(function (key) {
	if (program.hasOwnProperty(key) &&
			typeof program[key] === 'string' &&
			fs.existsSync(program[key])) {
		program[key] = fs.readFileSync(program[key], 'utf-8');
	}
});

// get init objects
['transformMetaInit', 'transformSourceInit'].forEach(function (key) {
	if (program.hasOwnProperty(key)) {
		program[key] = vm.runInNewContext('(function () {' + program[key] + ';return {};}());');
	}
});

// init client
var clientConfig = {
	hosts: program.url.split(',')
};
for (key in esClientOptions) {
    if (esClientOptions.hasOwnProperty(key) && program.hasOwnProperty(key)) {
        clientConfig[key] = program[key];
    }
}

var client = new elasticsearch.Client(clientConfig);

// build our search object
var search = {
	fields: ['*'],
	_source: true,
	searchType: 'scan'
};
for (key in esOptions) {
	if (esOptions.hasOwnProperty(key) && program.hasOwnProperty(key)) {
		search[key] = program[key];
	}
}

// declare our processing functions
var processed = 0;
var barUpdateSize = 100000;
var processResults = function (error, response) {
    var content = '', hitMax = false, scrollOptions;
    if (error && typeof response === 'string') {
		console.log('\nattempting to parse invalid json returned from elasticsearch server');
		response = vm.runInThisContext('(function () {return ' + response + ';}());');
		if (typeof response !== 'object') {
			helpers.exit('attempt to parse invalid json as javascript failed.');
		}
	} else if (error) {
		helpers.exit(error);
	}
	if (response.hits.total === 0) {
		helpers.exit('no results were returned, so exiting.');
	}
	// init progress bar if needed
	if (!bar) {
		var halfAPercent = Math.round(response.hits.total * 0.005);
		if( halfAPercent < barUpdateSize) barUpdateSize = halfAPercent;
		bar = new ProgressBar('processing :current of :total [:bar] :percent :elapseds', {
			width: 20,
			total: response.hits.total
		});
		bar.render();
	}

	// process results
	response.hits.hits.forEach(function (hit) {
		var meta = {index:{}};
		var source = hit._source || {};
		var fields = hit.fields || {};
		// if we passed in a max, stop processing
		if (typeof program.max === 'number' && processed >= program.max) {
			hitMax = true;
			return;
		}
		// build meta
		for (var key in hit) {
			if (hit.hasOwnProperty(key) && key !== '_source' && key !== 'fields') {
				meta.index[key] = hit[key];
			}
		}
		for (key in fields) {
			if (fields.hasOwnProperty(key)) {
				meta.index[key] = fields[key];
			}
		}
		// transform meta
		if (program.transformMeta) {
			meta = vm.runInNewContext('(function () {' + program.transformMeta + ';return data;}());', {
				init: program.transformMetaInit,
				data: meta
			});
		}

		// transform source
		if (program.transformSource) {
			source = vm.runInNewContext('(function () {' + program.transformSource + ';return data;}());', {
				init: program.transformSourceInit,
				data: source
			});
		}

		if (typeof meta !== 'object' || typeof source !== 'object') {
			helpers.exit({
				message: 'an invalid bulk item was created after transforming data',
				meta: meta,
				source: source
			});
		}
		delete meta.index._score; // Remove the score, causes errors in ES 2.3 (maybe earlier versions too)
		content += JSON.stringify(meta) + '\n' + JSON.stringify(source) + '\n';
		processed++;
		if( (processed % barUpdateSize) == 0)
			bar.tick(barUpdateSize);
	});
	fs.appendFileSync(program.file, content, 'utf-8');
	// continue to scroll
	if (response.hits.total !== processed && !hitMax) {
		scrollOptions = {scrollId: response._scroll_id};
		if (program.scroll) {
			scrollOptions.scroll = program.scroll;
		}
		client.scroll(scrollOptions, processResults);
	} else {
		bar.tick(processed - Math.floor(processed / barUpdateSize)*barUpdateSize);
		console.log('Done!');
		process.exit();
	}
};

// empty our file
fs.writeFileSync(program.file, '', 'utf-8');

// perform our search and start scrolling
client.search(search, processResults);

<commit_msg>--file supports stdout<commit_after>#!/usr/bin/env node
'use strict';

var helpers = require('./helpers');
var fs = require('fs');
var vm = require('vm');
var program = require('commander');
var elasticsearch = require('elasticsearch');
var appInfo = require('../package.json');
var ProgressBar = require('progress');
var bar, key;

var esOptions = {
	index: 'a comma-separated list of index names to search; use _all or empty string to perform the operation on all indices',
	type: 'a comma-separated list of document types to search; leave empty to perform the operation on all types',
	body: 'the body to send along with this request.',
	analyzer: 'The analyzer to use for the query string',
	analyzeWildcard: 'specify whether wildcard and prefix queries should be analyzed (default: false)',
	fields: 'a comma-separated list of fields to return as part of a hit (default: "*")',
	from: 'starting offset (default: 0)',
	q: 'query in the Lucene query string syntax',
	routing: 'a comma-separated list of specific routing values',
	scroll: 'specify how long a consistent view of the index should be maintained for scrolled search (default: 1m)',
	size: 'number of hits to return during each scan',
	sort: 'a comma-separated list of <field>:<direction> pairs',
	timeout: 'explicit operation timeout'
};

var esClientOptions = {
    apiVersion: 'the major version of the Elasticsearch nodes you will be connecting to (default: 2.3)',
    maxRetries: 'how many times should the client try to connect to other nodes before returning a ConnectionFault error (default: 3)',
    requestTimeout: 'milliseconds before an HTTP request will be aborted and retried. This can also be set per request (default: 30000)',
    deadTimeout: 'milliseconds that a dead connection will wait before attempting to revive itself (default: 60000)',
    pingTimeout: 'milliseconds that a ping request can take before timing out (default: 3000)',
    maxSockets: 'maximum number of concurrent requests that can be made to any node (default: 10)',
    minSockets: 'minimum number of sockets to keep connected to a node (default: 10)',
    selector: 'select a connection from the ConnectionPool using roundRobin (default) or random'
};

// setup command line options
program
	.version(appInfo.version, '-v, --version')
	.option('-u, --url <url>', 'the elasticsearch url to connect to')
	.option('-f, --file <file>', 'the file to write data to')
	.option('-m, --max <number>', 'the maximum number of items to export. different than the scroll size', parseInt)
	.option('--transformMeta <js>', 'a javascript function that returns an object that is the transformed meta object')
	.option('--transformSource <js>', 'a javascript function that returns an object that is the transformed source object')
	.option('--transformMetaInit <js>', 'a javascript function that returns an init object that contains helpers for the transform function')
	.option('--transformSourceInit <js>', 'a javascript function that returns an init object that contains helpers for the transform function');

// add es options
for (key in esOptions) {
	if (esOptions.hasOwnProperty(key)) {
		program.option('--' + key + ' <' + key + '>', 'ES OPTION: ' + esOptions[key]);
	}
}

// add client options
for (key in esClientOptions) {
    if (esClientOptions.hasOwnProperty(key)) {
        program.option('--' + key + ' <' + key + '>', 'ES CLIENT OPTION: ' + esClientOptions[key]);
    }
}

// parse arguments
program.parse(process.argv);

// validate url and file
helpers.validateUrlAndFile(program);

// setup a default scroll value
if (!program.scroll) {
	program.scroll = '1m';
}

// these can be strings or the contents of files
['transformMeta', 'transformSource', 'transformMetaInit', 'transformSourceInit'].forEach(function (key) {
	if (program.hasOwnProperty(key) &&
			typeof program[key] === 'string' &&
			fs.existsSync(program[key])) {
		program[key] = fs.readFileSync(program[key], 'utf-8');
	}
});

// get init objects
['transformMetaInit', 'transformSourceInit'].forEach(function (key) {
	if (program.hasOwnProperty(key)) {
		program[key] = vm.runInNewContext('(function () {' + program[key] + ';return {};}());');
	}
});

// init client
var clientConfig = {
	hosts: program.url.split(',')
};
for (key in esClientOptions) {
    if (esClientOptions.hasOwnProperty(key) && program.hasOwnProperty(key)) {
        var value = program[key];
        if (isNaN(value)) {
            clientConfig[key] = program[key];
        } else {
            clientConfig[key] = parseInt(program[key]);
        }
    }
}

var client = new elasticsearch.Client(clientConfig);
var stdout = program.file == '-';

// build our search object
var search = {
	fields: ['*'],
	_source: true,
	searchType: 'scan'
};
for (key in esOptions) {
	if (esOptions.hasOwnProperty(key) && program.hasOwnProperty(key)) {
		search[key] = program[key];
	}
}

// declare our processing functions
var processed = 0;
var barUpdateSize = 100000;
var processResults = function (error, response) {
    var content = '', hitMax = false, scrollOptions;
    if (error && typeof response === 'string') {
		console.log('\nattempting to parse invalid json returned from elasticsearch server');
		response = vm.runInThisContext('(function () {return ' + response + ';}());');
		if (typeof response !== 'object') {
			helpers.exit('attempt to parse invalid json as javascript failed.');
		}
	} else if (error) {
		helpers.exit(error);
	}
	if (response.hits.total === 0) {
		helpers.exit('no results were returned, so exiting.');
	}
	// init progress bar if needed
	if (!bar) {
		var halfAPercent = Math.round(response.hits.total * 0.005);
		if( halfAPercent < barUpdateSize) barUpdateSize = halfAPercent;
		bar = new ProgressBar('processing :current of :total [:bar] :percent :elapseds', {
			width: 20,
			total: response.hits.total
		});
		bar.render();
	}

	// process results
	response.hits.hits.forEach(function (hit) {
		var meta = {index:{}};
		var source = hit._source || {};
		var fields = hit.fields || {};
		// if we passed in a max, stop processing
		if (typeof program.max === 'number' && processed >= program.max) {
			hitMax = true;
			return;
		}
		// build meta
		for (var key in hit) {
			if (hit.hasOwnProperty(key) && key !== '_source' && key !== 'fields') {
				meta.index[key] = hit[key];
			}
		}
		for (key in fields) {
			if (fields.hasOwnProperty(key)) {
				meta.index[key] = fields[key];
			}
		}
		// transform meta
		if (program.transformMeta) {
			meta = vm.runInNewContext('(function () {' + program.transformMeta + ';return data;}());', {
				init: program.transformMetaInit,
				data: meta
			});
		}

		// transform source
		if (program.transformSource) {
			source = vm.runInNewContext('(function () {' + program.transformSource + ';return data;}());', {
				init: program.transformSourceInit,
				data: source
			});
		}

		if (typeof meta !== 'object' || typeof source !== 'object') {
			helpers.exit({
				message: 'an invalid bulk item was created after transforming data',
				meta: meta,
				source: source
			});
		}
		delete meta.index._score; // Remove the score, causes errors in ES 2.3 (maybe earlier versions too)
		content += JSON.stringify(meta) + '\n' + JSON.stringify(source) + '\n';
		processed++;
		if( (processed % barUpdateSize) == 0)
			bar.tick(barUpdateSize);
	});
    if (stdout) {
        process.stdout.write(content)
    } else {
        fs.appendFileSync(program.file, content, 'utf-8');
    }
	// continue to scroll
	if (response.hits.total !== processed && !hitMax) {
		scrollOptions = {scrollId: response._scroll_id};
		if (program.scroll) {
			scrollOptions.scroll = program.scroll;
		}
		client.scroll(scrollOptions, processResults);
	} else {
		bar.tick(processed - Math.floor(processed / barUpdateSize)*barUpdateSize);
		console.log('Done!');
		process.exit();
	}
};

if (!stdout) {
    // empty our file
    fs.writeFileSync(program.file, '', 'utf-8');
}

// perform our search and start scrolling
client.search(search, processResults);

<|endoftext|>


<commit_before>'use strict';

//This file contains the ES6 extensions to the core Promises/A+ API

var Promise = require('./core.js')
var asap = require('asap')

module.exports = Promise

/* Static Functions */

function ValuePromise(value) {
  this.then = function (onFulfilled) {
    if (typeof onFulfilled !== 'function') return this
    return new Promise(function (resolve, reject) {
      asap(function () {
        try {
          resolve(onFulfilled(value))
        } catch (ex) {
          reject(ex);
        }
      })
    })
  }
}
ValuePromise.prototype = Promise.prototype

var TRUE = new ValuePromise(true)
var FALSE = new ValuePromise(false)
var NULL = new ValuePromise(null)
var UNDEFINED = new ValuePromise(undefined)
var ZERO = new ValuePromise(0)
var EMPTYSTRING = new ValuePromise('')

Promise.resolve = function (value) {
  if (value instanceof Promise) return value

  if (value === null) return NULL
  if (value === undefined) return UNDEFINED
  if (value === true) return TRUE
  if (value === false) return FALSE
  if (value === 0) return ZERO
  if (value === '') return EMPTYSTRING

  if (typeof value === 'object' || typeof value === 'function') {
    try {
      var then = value.then
      if (typeof then === 'function') {
        return new Promise(then.bind(value))
      }
    } catch (ex) {
      return new Promise(function (resolve, reject) {
        reject(ex)
      })
    }
  }

  return new ValuePromise(value)
}

Promise.all = function (arr) {
  var args = Array.prototype.slice.call(arr)

  return new Promise(function (resolve, reject) {
    if (args.length === 0) return resolve([])
    var remaining = args.length
    function res(i, val) {
      try {
        if (val && (typeof val === 'object' || typeof val === 'function')) {
          var then = val.then
          if (typeof then === 'function') {
            then.call(val, function (val) { res(i, val) }, reject)
            return
          }
        }
        args[i] = val
        if (--remaining === 0) {
          resolve(args);
        }
      } catch (ex) {
        reject(ex)
      }
    }
    for (var i = 0; i < args.length; i++) {
      res(i, args[i])
    }
  })
}

Promise.reject = function (value) {
  return new Promise(function (resolve, reject) {
    reject(value);
  });
}

Promise.race = function (values) {
  return new Promise(function (resolve, reject) {
    values.forEach(function(value){
      Promise.resolve(value).then(resolve, reject);
    })
  });
}

/* Prototype Methods */

Promise.prototype['catch'] = function (onRejected) {
  return this.then(null, onRejected);
}
<commit_msg>Remove unused try-catch<commit_after>'use strict';

//This file contains the ES6 extensions to the core Promises/A+ API

var Promise = require('./core.js')
var asap = require('asap')

module.exports = Promise

/* Static Functions */

function ValuePromise(value) {
  this.then = function (onFulfilled) {
    if (typeof onFulfilled !== 'function') return this
    return new Promise(function (resolve, reject) {
      asap(function () {
        try {
          resolve(onFulfilled(value))
        } catch (ex) {
          reject(ex);
        }
      })
    })
  }
}
ValuePromise.prototype = Promise.prototype

var TRUE = new ValuePromise(true)
var FALSE = new ValuePromise(false)
var NULL = new ValuePromise(null)
var UNDEFINED = new ValuePromise(undefined)
var ZERO = new ValuePromise(0)
var EMPTYSTRING = new ValuePromise('')

Promise.resolve = function (value) {
  if (value instanceof Promise) return value

  if (value === null) return NULL
  if (value === undefined) return UNDEFINED
  if (value === true) return TRUE
  if (value === false) return FALSE
  if (value === 0) return ZERO
  if (value === '') return EMPTYSTRING

  if (typeof value === 'object' || typeof value === 'function') {
    try {
      var then = value.then
      if (typeof then === 'function') {
        return new Promise(then.bind(value))
      }
    } catch (ex) {
      return new Promise(function (resolve, reject) {
        reject(ex)
      })
    }
  }

  return new ValuePromise(value)
}

Promise.all = function (arr) {
  var args = Array.prototype.slice.call(arr)

  return new Promise(function (resolve, reject) {
    if (args.length === 0) return resolve([])
    var remaining = args.length
    function res(i, val) {
      if (val && (typeof val === 'object' || typeof val === 'function')) {
        var then = val.then
        if (typeof then === 'function') {
          then.call(val, function (val) { res(i, val) }, reject)
          return
        }
      }
      args[i] = val
      if (--remaining === 0) {
        resolve(args);
      }
    }
    for (var i = 0; i < args.length; i++) {
      res(i, args[i])
    }
  })
}

Promise.reject = function (value) {
  return new Promise(function (resolve, reject) {
    reject(value);
  });
}

Promise.race = function (values) {
  return new Promise(function (resolve, reject) {
    values.forEach(function(value){
      Promise.resolve(value).then(resolve, reject);
    })
  });
}

/* Prototype Methods */

Promise.prototype['catch'] = function (onRejected) {
  return this.then(null, onRejected);
}
<|endoftext|>


<commit_before>function rec() {
	setTimeout(function(){
		console.log(new Date())
		rec()
	}, 5000)
}
rec()<commit_msg>update time on scheduler<commit_after>function rec() {
	setTimeout(function(){
		console.log(new Date())
		rec()
	}, 60000)
}
rec()<|endoftext|>