<commit_before>/* global AFRAME, THREE */

if (typeof AFRAME === "undefined") {
  throw new Error(
    "Component attempted to register before AFRAME was available."
  );
}

require("./lib/terrainloader.js");

/**
 * Terrain model component.
 */
AFRAME.registerComponent("terrain-model", {
  schema: {
    planeHeight: {
      type: "number",
      default: 346,
    },
    planeWidth: {
      type: "number",
      default: 346,
    },
    segmentsHeight: {
      type: "number",
      default: 199,
    },
    segmentsWidth: {
      type: "number",
      default: 199,
    },
    zPosition: {
      type: "number",
      default: 1.5,
    },
    dem: {
      type: "asset",
    },
    map: {
      type: "asset",
    },
    alphaMap: {
      type: "asset",
    },
    wireframe: {
      type: "boolean",
      default: false,
    },
  },

  init: function () {
    const el = this.el;
    const data = this.data;

    this.heightData = null;
    this.terrainLoader = new THREE.TerrainLoader();
    this.textureLoader = new THREE.TextureLoader();
    this._updatePositionBuffer = this._updatePositionBuffer.bind(this);
    this._toggleWireframe = this._toggleWireframe.bind(this);

    // Setup geometry.
    const { planeWidth, planeHeight, segmentsHeight, segmentsWidth } = data;
    this.geometry = new THREE.PlaneBufferGeometry(
      planeWidth,
      planeHeight,
      segmentsWidth,
      segmentsHeight
    );

    // Setup material.
    this.material = new THREE.MeshLambertMaterial();

    // Create the terrain mesh; rotate it to be parallel with the ground.
    this.mesh = new THREE.Mesh(this.geometry, this.material);
    this.mesh.rotation.x = -90 * (Math.PI / 180);
    el.setObject3D("terrain", this.mesh);
  },

  update: function (oldData) {
    const data = this.data;
    const dem = data.dem;
    const map = data.map;
    const alphaMap = data.alphaMap;
    const zPosition = data.zPosition;
    const wireframe = data.wireframe;

    /**
     * Callback for THREE.TerrainLoader().
     * Sets the z-component of every vector in the position attribute buffer
     * to the (adjusted) height value from the DEM.
     *  positionBuffer.count === the number of vertices in the plane
     * @param {number[]} heightData
     */
    function onTerrainLoad(heightData) {
      this.heightData = heightData;
      this._updatePositionBuffer();
      this.el.emit("demLoaded", { dem });
    }

    // Curried onTextureLoad callback.
    // materialProp is either:
    //   - 'map'      (for this.material.map)
    //   - 'alphaMap' (for this.material.alphaMap)
    const partialOnTextureLoad = (materialProp) => {
      return (loadedTexture) => {
        if (materialProp === "map" && this.data.map !== map) {
          // The texture took too long to load. The entity now has a different
          // map, so the map that was loaded cannot be used.
          loadedTexture.dispose();
          return;
        }
        if (materialProp === "alphaMap" && this.data.alphaMap !== alphaMap) {
          // Same idea as above, except the loaded texture is an alphaMap.
          loadedTexture.dispose();
          return;
        }
        loadedTexture.anisotropy = 16;
        const oldTexture = this.material[materialProp];
        this.material[materialProp] = loadedTexture;
        this.material.needsUpdate = true;
        if (oldTexture) {
          oldTexture.dispose();
        }
        this.el.emit("textureLoaded", { type: materialProp });
      };
    };

    if (dem !== oldData.dem) {
      // DEM has updated, so load the new one.
      this.terrainLoader.load(dem, onTerrainLoad.bind(this));
    }

    if (map !== oldData.map) {
      // Texture has updated, so load the new one.
      this.textureLoader.load(map, partialOnTextureLoad("map"));
    }

    if (alphaMap !== oldData.alphaMap) {
      // Alpha map has updated, so load the new one.
      // Also turn on material transparency.
      // TODO: Investigate how to turn this off if an alphaMap is removed.
      this.material.transparent = true;
      this.textureLoader.load(alphaMap, partialOnTextureLoad("alphaMap"));
    }

    if (zPosition !== oldData.zPosition) {
      // zPosition has changed, so re-scale the mesh.
      this.mesh.scale.set(1, 1, zPosition);
    }

    if (wireframe !== oldData.wireframe) {
      // wire mode has either been activated or disactivated.
      this._toggleWireframe();
    }
  },

  _updatePositionBuffer: function () {
    if (!this.heightData) {
      return;
    }

    let positionBuffer = this.geometry.getAttribute("position");
    for (let i = 0; i < positionBuffer.count; i++) {
      let heightValue = this.heightData[i] / 65535;
      positionBuffer.setZ(i, heightValue);
    }
    positionBuffer.needsUpdate = true;

    // Update wireframe
    let oldWireMesh = this.mesh.getObjectByName("terrain-wireframe");
    if (oldWireMesh) {
      oldWireMesh.geometry.dispose();
      const wireGeometry = new THREE.WireframeGeometry(this.geometry);
      oldWireMesh.geometry = wireGeometry;
    }
    this.el.emit("positionBufferUpdated");
  },

  _toggleWireframe: function () {
    // TODO: Consider creating wireframe on init and hiding/revealing rather
    // than creating the wireframe mesh on demand. Cons: wastes resources for
    // people who don't care about wireframe. Pros: less lag when wireframe is
    // turned on.

    let oldWireMesh = this.mesh.getObjectByName("terrain-wireframe");
    if (!oldWireMesh) {
      // This is an inelegant way to prevent adding a wireframe when the
      // component initializes.
      if (!this.data.wireframe) {
        return;
      }
      // Add wireframe.
      const wireGeometry = new THREE.WireframeGeometry(this.geometry);
      const wireMaterial = new THREE.LineBasicMaterial({
        color: 0x808080,
        linewidth: 1,
      });
      let wireMesh = new THREE.LineSegments(wireGeometry, wireMaterial);
      wireMesh.name = "terrain-wireframe";
      wireMesh.material.opacity = 0.3;
      wireMesh.material.transparent = true;
      this.mesh.add(wireMesh);
      return;
    }

    // Remove wireframe
    oldWireMesh.geometry.dispose();
    oldWireMesh.material.dispose();
    this.mesh.remove(oldWireMesh);
  },

  remove: function () {
    this.geometry.dispose();
    this.material.map.dispose();
    this.material.alphaMap.dispose();
    this.material.dispose();
    this.el.removeObject3D("terrain");
  },
});
<commit_msg>Handle 'map' and 'alphaMap' removal<commit_after>/* global AFRAME, THREE */

if (typeof AFRAME === "undefined") {
  throw new Error(
    "Component attempted to register before AFRAME was available."
  );
}

require("./lib/terrainloader.js");

/**
 * Terrain model component.
 */
AFRAME.registerComponent("terrain-model", {
  schema: {
    planeHeight: {
      type: "number",
      default: 346,
    },
    planeWidth: {
      type: "number",
      default: 346,
    },
    segmentsHeight: {
      type: "number",
      default: 199,
    },
    segmentsWidth: {
      type: "number",
      default: 199,
    },
    zPosition: {
      type: "number",
      default: 1.5,
    },
    dem: {
      type: "asset",
    },
    map: {
      type: "asset",
    },
    alphaMap: {
      type: "asset",
    },
    wireframe: {
      type: "boolean",
      default: false,
    },
  },

  init: function () {
    const el = this.el;
    const data = this.data;

    this.heightData = null;
    this.terrainLoader = new THREE.TerrainLoader();
    this.textureLoader = new THREE.TextureLoader();
    this._replaceTexture = this._replaceTexture.bind(this);
    this._updatePositionBuffer = this._updatePositionBuffer.bind(this);
    this._toggleWireframe = this._toggleWireframe.bind(this);

    // Setup geometry.
    const { planeWidth, planeHeight, segmentsHeight, segmentsWidth } = data;
    this.geometry = new THREE.PlaneBufferGeometry(
      planeWidth,
      planeHeight,
      segmentsWidth,
      segmentsHeight
    );

    // Setup material.
    this.material = new THREE.MeshLambertMaterial();

    // Create the terrain mesh; rotate it to be parallel with the ground.
    this.mesh = new THREE.Mesh(this.geometry, this.material);
    this.mesh.rotation.x = -90 * (Math.PI / 180);
    el.setObject3D("terrain", this.mesh);
  },

  update: function (oldData) {
    const data = this.data;
    const dem = data.dem;
    const map = data.map;
    const alphaMap = data.alphaMap;
    const zPosition = data.zPosition;
    const wireframe = data.wireframe;

    /**
     * Callback for THREE.TerrainLoader().
     * @param {number[]} heightData
     */
    function onTerrainLoad(heightData) {
      this.heightData = heightData;
      this._updatePositionBuffer();
      this.el.emit("demLoaded", { dem });
    }

    /**
     * This is a curried function that returns a callback to pass to THREE.TextureLoader.load()
     * This callback updates the terrain material as necessary. 'map' and 'alphaMap'
     * texture loads are handled almost identically.
     * @param {string} materialProp either 'map' (for this.material.map) or 'alphaMap' (for this.material.alphaMap)
     * @returns {function}
     */
    const curriedOnTextureLoad = (materialProp) => {
      return (loadedTexture) => {
        if (materialProp === "map" && this.data.map !== map) {
          // The texture took too long to load. The entity now has a different
          // map, so the map that was loaded cannot be used.
          loadedTexture.dispose();
          return;
        }
        if (materialProp === "alphaMap" && this.data.alphaMap !== alphaMap) {
          // Same idea as above, except the loaded texture is an alphaMap.
          loadedTexture.dispose();
          return;
        }
        loadedTexture.anisotropy = 16;
        this._replaceTexture(materialProp, loadedTexture);
        this.el.emit("textureLoaded", { type: materialProp });
      };
    };

    if (dem !== oldData.dem) {
      // DEM has updated, so load the new one.
      this.terrainLoader.load(dem, onTerrainLoad.bind(this));
    }

    if (map !== oldData.map) {
      // When map is not set, it defaults to ""
      if (!map) {
        this._replaceTexture("map", null);
      } else {
        // Texture has updated, so load the new one.
        this.textureLoader.load(map, curriedOnTextureLoad("map"));
      }
    }

    if (alphaMap !== oldData.alphaMap) {
      // When alphaMap is not set, it defaults to ""
      if (!alphaMap) {
        this._replaceTexture("alphaMap", null);
      } else {
        // Alpha map has updated, so load the new one.
        // Also turn on material transparency.
        this.material.transparent = true;
        this.textureLoader.load(alphaMap, curriedOnTextureLoad("alphaMap"));
      }
    }

    if (zPosition !== oldData.zPosition) {
      // zPosition has changed, so re-scale the mesh.
      this.mesh.scale.set(1, 1, zPosition);
    }

    if (wireframe !== oldData.wireframe) {
      // wire mode has either been activated or disactivated.
      this._toggleWireframe();
    }
  },

  /**
   * A helper for swapping out a material texture.
   * Ensures the old texture is disposed.
   * @param {string} materialProp e.g. 'map' or 'alphaMap'
   * @param {(THREE.Texture|null)} newTexture
   */
  _replaceTexture: function (materialProp, newTexture) {
    const oldTexture = this.material[materialProp];
    this.material[materialProp] = newTexture;
    this.material.needsUpdate = true;
    if (oldTexture) {
      oldTexture.dispose();
    }
  },

  /**
   * Sets the z-component of every vector in the position attribute buffer
   * to the (adjusted) height value from the DEM. Also adjusts the wireframe.
   * Note:
   *  positionBuffer.count === the number of vertices in the plane
   */
  _updatePositionBuffer: function () {
    if (!this.heightData) {
      return;
    }

    let positionBuffer = this.geometry.getAttribute("position");
    for (let i = 0; i < positionBuffer.count; i++) {
      let heightValue = this.heightData[i] / 65535;
      positionBuffer.setZ(i, heightValue);
    }
    positionBuffer.needsUpdate = true;

    // Update wireframe
    let oldWireMesh = this.mesh.getObjectByName("terrain-wireframe");
    if (oldWireMesh) {
      oldWireMesh.geometry.dispose();
      const wireGeometry = new THREE.WireframeGeometry(this.geometry);
      oldWireMesh.geometry = wireGeometry;
    }
    this.el.emit("positionBufferUpdated");
  },

  /**
   * Creates or destroys a terrain wireframe mesh.
   * A wireframe can be useful for debugging or for visualizing a terrain DEM
   * without a texture.
   */
  _toggleWireframe: function () {
    // TODO: Consider creating wireframe on init and hiding/revealing rather
    // than creating the wireframe mesh on demand. Cons: wastes resources for
    // people who don't care about wireframe. Pros: less lag when wireframe is
    // turned on.

    let oldWireMesh = this.mesh.getObjectByName("terrain-wireframe");
    if (!oldWireMesh) {
      // This is a somewhat inelegant way to prevent adding a wireframe when the
      // component initializes.
      if (!this.data.wireframe) {
        return;
      }
      // Add wireframe.
      const wireGeometry = new THREE.WireframeGeometry(this.geometry);
      const wireMaterial = new THREE.LineBasicMaterial({
        color: 0x808080,
        linewidth: 1,
      });
      let wireMesh = new THREE.LineSegments(wireGeometry, wireMaterial);
      wireMesh.name = "terrain-wireframe";
      wireMesh.material.opacity = 0.3;
      wireMesh.material.transparent = true;
      this.mesh.add(wireMesh);
      return;
    }

    // Remove wireframe
    oldWireMesh.geometry.dispose();
    oldWireMesh.material.dispose();
    this.mesh.remove(oldWireMesh);
  },

  remove: function () {
    this.geometry.dispose();
    this.material.map.dispose();
    this.material.alphaMap.dispose();
    this.material.dispose();
    if (this.data.wireframe) {
      this._toggleWireframe();
    }
    this.el.removeObject3D("terrain");
  },
});
<|endoftext|>


<commit_before>"use strict"

module.exports = {
	Helper : require('./Helper.js'),
	SbEvent : require('./SbEvent.js'),
	Logger : require('./Logger.js'),
	Base : require('./Base.js'),
	SingleLetterAbbreviations : require('./SingleLetterAbbreviations.js'),
	Stopwords : require('./Stopwords.js')
}
<commit_msg>Added a new way to set logger.<commit_after>"use strict"

module.exports = {
	Helper : require('./Helper.js'),
	SbEvent : require('./SbEvent.js'),
	Logger : function(name) {
		return require('./Logger.js')(name);
		},
	Base : require('./Base.js'),
	SingleLetterAbbreviations : require('./SingleLetterAbbreviations.js'),
	Stopwords : require('./Stopwords.js')
}
<|endoftext|>


<commit_before>'use strict';

var _ = require('lodash');
var assert = require('assert-plus');
var fs = require('fs');
var Gremlin = require('gremlin-v3');
var mkdirp = require('mkdirp');
var Work = require('./lib/work.js');

var gremlin = new Gremlin();
var java = gremlin.java;
var Class = java.import('java.lang.Class');

var Promise = require("bluebird");
Promise.longStackTraces();

var classes = {};
var methodsDefinitions = {};

function loadClass(className) {
  return java.getClassLoader().loadClassSync(className);
}

function shortClassName(className) {
  var m = className.match(/\.([\$\w]+)$/);
  assert.ok(m);
  return m[1];
}

// The set of packages and classes we are interested in. We ignore others.
var whiteList = [
  /^com\.tinkerpop\.gremlin/,
  /^java\.util\.Iterator/,
  /^java\.lang\.Object/
];

function inWhiteList(className) {
  return _.find(whiteList, function (ns) { return className.match(ns); });
}

function addIfNotQueued(className, work) {
  if (inWhiteList(className)) {
    work.addTodo(className);
  }
}

function mapMethod(method, work) {
  var methodMap = {
    name: method.getNameSync(),
    declared: method.getDeclaringClassSync().getNameSync(),
    returns: method.getReturnTypeSync().getNameSync(),
    params: _.map(method.getParameterTypesSync(), function (p) { return p.getNameSync(); }),
    isVarArgs: method.isVarArgsSync(),
    generic: method.toGenericStringSync(),
    string: method.toStringSync(),
  };

  var varArgs = methodMap.isVarArgs ? '...' : '';
  if (methodMap.isVarArgs) {
    var last = _.last(methodMap.params);
    var match = /\[L(.+);/.exec(last);
    assert.ok(match);
    var finalArg = match[1] + '...';
    var params = methodMap.params.slice(0, -1);
    params.push(finalArg);
    methodMap.signature = methodMap.name + '(' + params.join() + ')';
  }
  else {
    methodMap.signature = methodMap.name + '(' + methodMap.params.join() + varArgs + ')';
  }

  addIfNotQueued(methodMap.declared, work);
  addIfNotQueued(methodMap.returns, work);

  return methodMap;
}

function mapClass(className, work) {
  var clazz = loadClass(className);
  assert.strictEqual(clazz.getNameSync(), className);

  var shortName = shortClassName(className);
  var methods = _.map(clazz.getMethodsSync(), function (m) { return mapMethod(m, work); });

  var interfaces = _.map(clazz.getInterfacesSync(), function (intf) { return intf.getNameSync(); });
  interfaces = _.filter(interfaces, function (intf) { return inWhiteList(intf); });

  // HACK: in TP3, some *Traversal classes are not declared to implement Traversal,
  // e.g. ElementTraversal. I believe we will want to treat these classes as if they
  // actually do implement that interface.
  if (className.match(/(\w+)Traversal$/)) {
    var baseTraversal = 'com.tinkerpop.gremlin.process.Traversal';
    if (_.indexOf(interfaces, baseTraversal) === -1) {
      interfaces.push(baseTraversal);
    }
  }

  var javaLangObject = 'java.lang.Object';
  if (interfaces.length === 0 && className !== javaLangObject)
    interfaces.push(javaLangObject);

  _.forEach(interfaces, function (intf) { addIfNotQueued(intf, work); });

  var classMap = {
    fullName: className,
    shortName: shortName,
    interfaces: interfaces,
    methods: methods
  };

  return classMap;
}

function locateMethodDefinitions(className, work) {
  assert.object(methodsDefinitions);

  assert.ok(className in classes);
  var classMap = classes[className];
  assert.strictEqual(className, classMap.fullName);

  _.forEach(classMap.interfaces, function (intf) {
    if (!work.alreadyDone(intf)) {
      assert.ok(intf in classes, 'Unknown interface:' + intf);
      locateMethodDefinitions(intf, work);
    }
  });

  _.forEach(classMap.methods, function (method, index) {
    assert.string(method.signature);
    var definedHere = false;
    if (!(method.signature in methodsDefinitions)) {
      if (!(method.signature in classMap.interfaces)) {
        definedHere = true;
        methodsDefinitions[method.signature] = className;
        if (method.declared !== className) {
          console.log('Method %s located in %s but declared in %s', method.signature, className, method.declared);
        }
      }
    }
    classMap.methods[index].definedHere = definedHere;
  });

  console.log(className);
  work.setDone(className);
}

function loadAllClasses() {
  var work = new Work();
  work.addTodo('java.lang.Object');
  work.addTodo('com.tinkerpop.gremlin.structure.Graph');

  while (!work.isDone()) {
    var className = work.next();
    work.setDone(className);
    classes[className] = mapClass(className, work);
  }
}

function mapMethodDefinitions() {
  var work = new Work(_.keys(classes));
  while (!work.isDone()) {
    var className = work.next();
    locateMethodDefinitions(className, work);
  }
}

function writeTxts() {
  _.forOwn(classes, function (classMap, className) {
    fs.writeFileSync('out/txt/' + classMap.shortName + '.txt', JSON.stringify(classMap, null, '  '));
  });
}

function Block(lines, extra) {
  extra = _.isNumber(extra) ? extra : 2;
  function Extra() {
    var s = '';
    for (var i=0; i<extra; ++i)
      s = s + '\n';
    return s;
  }
  return lines.join('\n') + Extra();
}

function writeRequiredInterfaces(write, classMap) {
  var imports = Block([
    "var ${name} = require('./${name}.js');"
  ], 1);

  return Promise.all(classMap.interfaces)
    .each(function (intf) {
      assert.ok(intf in classes, 'Unknown interface:' + intf);
      var interfaceMap = classes[intf];
      var interfaceName = interfaceMap.shortName + 'Wrapper';
      return write(_.template(imports, { name: interfaceName }));
    })
    .then(function () { return write('\n'); });
}

function writeJsHeader(write, className, classMap) {
  var firstLines = Block([
    "// ${name}.js'",
    "",
    "'use strict';"
  ]);

  var constructor = Block([
    "function ${name}(_jThis) {",
    "  if (!(this instanceof ${name})) {",
    "    return new ${name}(_jThis);",
    "  }",
    "  this.jThis = _jThis;",
    "}"
  ]);

  return write(_.template(firstLines, { name: className }))
    .then(function () {
      return writeRequiredInterfaces(write, classMap);
    })
    .then(function () {
      return write(_.template(constructor, { name: className }));
    });
}

function writeOneDefinedMethod(write, className, method) {
  var text = Block([
    "// ${signature}",
    "${clazz}.prototype.${method} = function() {",
    "};"
  ]);

  var methodName = method.name;
  var signature = method.signature;
  return write(_.template(text, { clazz: className, method: methodName, signature: signature }));
}

function writeOneInheritedMethod(write, className, method) {
  var text = Block([
    "// ${signature}",
    "${clazz}.prototype.${method} = ${defining}.prototype.${method};"
  ]);

  var methodName = method.name;
  var signature = method.signature;
  var defining = classes[methodsDefinitions[signature]].shortName + 'Wrapper';
  return write(_.template(text, { clazz: className, method: methodName, signature: signature, defining: defining }));
}

function writeJsMethods(write, className, classMap) {
  return Promise.all(classMap.methods)
    .each(function (method) {
      if (method.definedHere)
        return writeOneDefinedMethod(write, className, method);
      else
        return writeOneInheritedMethod(write, className, method);
    });
}

function writeClassLib(classMap) {
  var className = classMap.shortName + 'Wrapper';
  var fileName = 'out/lib/' + className + '.js';

  var stream = fs.createWriteStream(fileName);
  var write = Promise.promisify(stream.write, stream);
  var end = Promise.promisify(stream.end, stream);

  return writeJsHeader(write, className, classMap)
    .then(function () { return writeJsMethods(write, className, classMap); })
    .then(function () { return end(); });
}

function writeLib() {
  return Promise.all(_.keys(classes))
    .each(function (className) {
      return writeClassLib(classes[className]);
    });
}

function main() {

  mkdirp.sync('out/txt');
  mkdirp.sync('out/lib');
  mkdirp.sync('out/test');

  loadAllClasses();
  mapMethodDefinitions();

  writeTxts();
  writeLib().done();
}

main();
<commit_msg>Set interfaces to transitive closure of all inherited interfaces.<commit_after>'use strict';

var _ = require('lodash');
var assert = require('assert-plus');
var fs = require('fs');
var Gremlin = require('gremlin-v3');
var Immutable = require('immutable');
var mkdirp = require('mkdirp');
var Work = require('./lib/work.js');

var gremlin = new Gremlin();
var java = gremlin.java;
var Class = java.import('java.lang.Class');

var Promise = require("bluebird");
Promise.longStackTraces();

var classes = {};
var methodsDefinitions = {};

function loadClass(className) {
  return java.getClassLoader().loadClassSync(className);
}

function shortClassName(className) {
  var m = className.match(/\.([\$\w]+)$/);
  assert.ok(m);
  return m[1];
}

// The set of packages and classes we are interested in. We ignore others.
var whiteList = [
  /^com\.tinkerpop\.gremlin/,
  /^java\.util\.Iterator/,
  /^java\.lang\.Object/
];

function inWhiteList(className) {
  return _.find(whiteList, function (ns) { return className.match(ns); });
}

function addIfNotQueued(className, work) {
  if (inWhiteList(className)) {
    work.addTodo(className);
  }
}

function mapMethod(method, work) {
  var methodMap = {
    name: method.getNameSync(),
    declared: method.getDeclaringClassSync().getNameSync(),
    returns: method.getReturnTypeSync().getNameSync(),
    params: _.map(method.getParameterTypesSync(), function (p) { return p.getNameSync(); }),
    isVarArgs: method.isVarArgsSync(),
    generic: method.toGenericStringSync(),
    string: method.toStringSync(),
  };

  var varArgs = methodMap.isVarArgs ? '...' : '';
  if (methodMap.isVarArgs) {
    var last = _.last(methodMap.params);
    var match = /\[L(.+);/.exec(last);
    assert.ok(match);
    var finalArg = match[1] + '...';
    var params = methodMap.params.slice(0, -1);
    params.push(finalArg);
    methodMap.signature = methodMap.name + '(' + params.join() + ')';
  }
  else {
    methodMap.signature = methodMap.name + '(' + methodMap.params.join() + varArgs + ')';
  }

  addIfNotQueued(methodMap.declared, work);
  addIfNotQueued(methodMap.returns, work);

  return methodMap;
}

function mapClass(className, work) {
  var clazz = loadClass(className);
  assert.strictEqual(clazz.getNameSync(), className);

  var shortName = shortClassName(className);
  var methods = _.map(clazz.getMethodsSync(), function (m) { return mapMethod(m, work); });

  var interfaces = _.map(clazz.getInterfacesSync(), function (intf) { return intf.getNameSync(); });
  interfaces = _.filter(interfaces, function (intf) { return inWhiteList(intf); });

  // HACK: in TP3, some *Traversal classes are not declared to implement Traversal,
  // e.g. ElementTraversal. I believe we will want to treat these classes as if they
  // actually do implement that interface.
  if (className.match(/(\w+)Traversal$/)) {
    var baseTraversal = 'com.tinkerpop.gremlin.process.Traversal';
    if (_.indexOf(interfaces, baseTraversal) === -1) {
      interfaces.push(baseTraversal);
    }
  }

  var javaLangObject = 'java.lang.Object';
  if (interfaces.length === 0 && className !== javaLangObject)
    interfaces.push(javaLangObject);

  _.forEach(interfaces, function (intf) { addIfNotQueued(intf, work); });

  var classMap = {
    fullName: className,
    shortName: shortName,
    interfaces: interfaces,
    methods: methods
  };

  return classMap;
}

function locateMethodDefinitions(className, work) {
  assert.object(methodsDefinitions);

  assert.ok(className in classes);
  var classMap = classes[className];
  assert.strictEqual(className, classMap.fullName);

  _.forEach(classMap.interfaces, function (intf) {
    if (!work.alreadyDone(intf)) {
      assert.ok(intf in classes, 'Unknown interface:' + intf);
      locateMethodDefinitions(intf, work);
    }
  });

  _.forEach(classMap.methods, function (method, index) {
    assert.string(method.signature);
    var definedHere = false;
    if (!(method.signature in methodsDefinitions)) {
      if (!(method.signature in classMap.interfaces)) {
        definedHere = true;
        methodsDefinitions[method.signature] = className;
        if (method.declared !== className) {
          console.log('Method %s located in %s but declared in %s', method.signature, className, method.declared);
        }
      }
    }
    classMap.methods[index].definedHere = definedHere;
  });

  console.log(className);
  work.setDone(className);
}

function loadAllClasses() {
  var work = new Work();
  work.addTodo('java.lang.Object');
  work.addTodo('com.tinkerpop.gremlin.structure.Graph');

  while (!work.isDone()) {
    var className = work.next();
    work.setDone(className);
    classes[className] = mapClass(className, work);
  }
}

function interfacesClosure(className, work) {
  assert.ok(!work.alreadyDone(className));
  var transitiveClosure = Immutable.Set(classes[className].interfaces);

  _.forEach(classes[className].interfaces, function (intf) {
    if (!work.alreadyDone(intf))
      interfacesClosure(intf, work);
    assert.ok(work.alreadyDone(intf));
    transitiveClosure = transitiveClosure.union(classes[intf].interfaces);
  });

  console.log('For class %s, before %j, after %j:', className, classes[className].interfaces, transitiveClosure);
  classes[className].interfaces = transitiveClosure.toArray();
  work.setDone(className);
}

function transitiveClosureInterfaces() {
  var work = new Work(_.keys(classes));

  while (!work.isDone()) {
    var className = work.next();
    interfacesClosure(className, work);
  }
}

function mapMethodDefinitions() {
  var work = new Work(_.keys(classes));
  while (!work.isDone()) {
    var className = work.next();
    locateMethodDefinitions(className, work);
  }
}

function writeTxts() {
  _.forOwn(classes, function (classMap, className) {
    fs.writeFileSync('out/txt/' + classMap.shortName + '.txt', JSON.stringify(classMap, null, '  '));
  });
}

function Block(lines, extra) {
  extra = _.isNumber(extra) ? extra : 2;
  function Extra() {
    var s = '';
    for (var i=0; i<extra; ++i)
      s = s + '\n';
    return s;
  }
  return lines.join('\n') + Extra();
}

function writeRequiredInterfaces(write, classMap) {
  var imports = Block([
    "var ${name} = require('./${name}.js');"
  ], 1);

  return Promise.all(classMap.interfaces)
    .each(function (intf) {
      assert.ok(intf in classes, 'Unknown interface:' + intf);
      var interfaceMap = classes[intf];
      var interfaceName = interfaceMap.shortName + 'Wrapper';
      return write(_.template(imports, { name: interfaceName }));
    })
    .then(function () { return write('\n'); });
}

function writeJsHeader(write, className, classMap) {
  var firstLines = Block([
    "// ${name}.js'",
    "",
    "'use strict';"
  ]);

  var constructor = Block([
    "function ${name}(_jThis) {",
    "  if (!(this instanceof ${name})) {",
    "    return new ${name}(_jThis);",
    "  }",
    "  this.jThis = _jThis;",
    "}"
  ]);

  return write(_.template(firstLines, { name: className }))
    .then(function () {
      return writeRequiredInterfaces(write, classMap);
    })
    .then(function () {
      return write(_.template(constructor, { name: className }));
    });
}

function writeOneDefinedMethod(write, className, method) {
  var text = Block([
    "// ${signature}",
    "${clazz}.prototype.${method} = function() {",
    "};"
  ]);

  var methodName = method.name;
  var signature = method.signature;
  return write(_.template(text, { clazz: className, method: methodName, signature: signature }));
}

function writeOneInheritedMethod(write, className, method) {
  var text = Block([
    "// ${signature}",
    "${clazz}.prototype.${method} = ${defining}.prototype.${method};"
  ]);

  var methodName = method.name;
  var signature = method.signature;
  var defining = classes[methodsDefinitions[signature]].shortName + 'Wrapper';
  return write(_.template(text, { clazz: className, method: methodName, signature: signature, defining: defining }));
}

function writeJsMethods(write, className, classMap) {
  return Promise.all(classMap.methods)
    .each(function (method) {
      if (method.definedHere)
        return writeOneDefinedMethod(write, className, method);
      else
        return writeOneInheritedMethod(write, className, method);
    });
}

function writeClassLib(classMap) {
  var className = classMap.shortName + 'Wrapper';
  var fileName = 'out/lib/' + className + '.js';

  var stream = fs.createWriteStream(fileName);
  var write = Promise.promisify(stream.write, stream);
  var end = Promise.promisify(stream.end, stream);

  return writeJsHeader(write, className, classMap)
    .then(function () { return writeJsMethods(write, className, classMap); })
    .then(function () { return end(); });
}

function writeLib() {
  return Promise.all(_.keys(classes))
    .each(function (className) {
      return writeClassLib(classes[className]);
    });
}

function main() {

  mkdirp.sync('out/txt');
  mkdirp.sync('out/lib');
  mkdirp.sync('out/test');

  loadAllClasses();

  transitiveClosureInterfaces();

  mapMethodDefinitions();

  writeTxts();
  writeLib().done();
}

main();
<|endoftext|>


<commit_before>module.exports = {
    managers: {
        auth: {
            AccountManager: require("./src/managers/auth/account-manager"),
            RoleManager: require("./src/managers/auth/role-manager"),
            ApiEndpointManager: require("./src/managers/auth/api-endpoint-manager")
        },
        master: {
            BuyerManager: require("./src/managers/master/buyer-manager"),
            SupplierManager: require("./src/managers/master/supplier-manager"),
            ProductManager: require("./src/managers/master/product-manager"),
            CategoryManager: require('./src/managers/master/category-manager'),
            DivisionManager: require('./src/managers/master/division-manager'),
            UnitManager: require('./src/managers/master/unit-manager'),
            UomManager: require('./src/managers/master/uom-manager'),
            CurrencyManager: require('./src/managers/master/currency-manager'),
            VatManager: require('./src/managers/master/vat-manager'),
            BudgetManager: require('./src/managers/master/budget-manager'),
            ThreadSpecificationManager: require('./src/managers/master/thread-specification-manager'),
            MachineManager: require('./src/managers/master/machine-manager'),
            MachineTypeManager: require('./src/managers/master/machine-type-manager'),
            LotMachineManager: require('./src/managers/master/lot-machine-manager'),
            YarnEquivalentConversion: require('./src/managers/master/yarn-equivalent-conversion-manager'),
            UsterManager: require('./src/managers/master/uster-manager'),
            LampStandardManager: require('./src/managers/master/lamp-standard-manager'),
            AccountBankManager: require('./src/managers/master/account-bank-manager'),
            InstructionManager: require('./src/managers/master/instruction-manager'),
            StepManager: require('./src/managers/master/step-manager'),
            ProcessTypeManager: require('./src/managers/master/process-type-manager'),
            OrderTypeManager: require('./src/managers/master/order-type-manager'),
            ColorTypeManager: require('./src/managers/master/color-type-manager'),
            MaterialConstructionManager: require('./src/managers/master/material-construction-manager'),
            FinishTypeManager: require('./src/managers/master/finish-type-manager'),
            StandardTestManager: require('./src/managers/master/standard-test-manager'),
            YarnMaterialManager: require('./src/managers/master/yarn-material-manager'),
            ComodityManager: require('./src/managers/master/comodity-manager')
        },
        purchasing: {
            PurchaseOrderManager: require('./src/managers/purchasing/purchase-order-manager'),
            PurchaseOrderExternalManager: require('./src/managers/purchasing/purchase-order-external-manager'),
            DeliveryOrderManager: require('./src/managers/purchasing/delivery-order-manager'),
            UnitReceiptNoteManager: require('./src/managers/purchasing/unit-receipt-note-manager'),
            PurchaseRequestManager: require('./src/managers/purchasing/purchase-request-manager'),
            UnitPaymentPriceCorrectionNoteManager: require('./src/managers/purchasing/unit-payment-price-correction-note-manager'),
            UnitPaymentOrderManager: require('./src/managers/purchasing/unit-payment-order-manager'),
            UnitPaymentQuantityCorrectionNoteManager: require('./src/managers/purchasing/unit-payment-quantity-correction-note-manager')
        },
        production: {
            spinning: {
                winding: {
                    WindingQualitySampling: require('./src/managers/production/spinning/winding/winding-quality-sampling-manager'),
                    WindingProductionOutput: require('./src/managers/production/spinning/winding/winding-production-output-manager')
                },
                DailySpinningProductionReportManager: require('./src/managers/production/spinning/daily-spinning-production-report-manager')
            },
            finishingPrinting: {
                DailyOperationManager: require('./src/managers/production/finishing-printing/daily-operation-manager'),
                MonitoringEventManager: require('./src/managers/production/finishing-printing/monitoring-event-manager'),
                MonitoringSpecificationMachineManager: require('./src/managers/production/finishing-printing/monitoring-specification-machine-manager'),
                KanbanManager: require('./src/managers/production/finishing-printing/kanban-manager')
            }
        },
        sales:{
            ProductionOrderManager: require('./src/managers/sales/production-order-manager')
        }

    },
    test: {
        data: {
            auth: {
                account: require("./test/data-util/auth/account-data-util"),
                role: require("./test/data-util/auth/role-data-util")
            },
            master: {
                accountBank: require("./test/data-util/master/account-bank-data-util"),
                budget: require("./test/data-util/master/budget-data-util"),
                buyer: require("./test/data-util/master/buyer-data-util"),
                category: require("./test/data-util/master/category-data-util"),
                currency: require("./test/data-util/master/currency-data-util"),
                division: require("./test/data-util/master/division-data-util"),
                lampStandard: require("./test/data-util/master/lamp-standard-data-util"),
                lotMachine: require("./test/data-util/master/lot-machine-data-util"),
                machine: require("./test/data-util/master/machine-data-util"),
                machineType: require("./test/data-util/master/machine-type-data-util"),
                product: require("./test/data-util/master/product-data-util"),
                supplier: require("./test/data-util/master/supplier-data-util"),
                threadSpecification: require("./test/data-util/master/thread-specification-data-util"),
                unit: require("./test/data-util/master/unit-data-util"),
                uom: require("./test/data-util/master/uom-data-util"),
                uster: require("./test/data-util/master/uster-data-util"),
                vat: require("./test/data-util/master/vat-data-util"),
                yarnEquivalentConversion: require("./test/data-util/master/yarn-equivalent-conversion-data-util"),
                step: require("./test/data-util/master/step-data-util"),
                instruction: require("./test/data-util/master/instruction-data-util"),
                orderType: require('./test/data-util/master/order-type-data-util'),
                processType: require('./test/data-util/master/process-type-data-util'),
                materialConstruction: require('./test/data-util/master/material-construction-data-util'),
                yarnMaterial: require('./test/data-util/master/yarn-material-data-util'),
                finishType: require('./test/data-util/master/finish-type-data-util'),
                standardTest: require('./test/data-util/master/standard-test-data-util'),
                colorType: require('./test/data-util/master/color-type-data-util'),
                comodity: require('./test/data-util/master/comodity-data-util')
            },
            purchasing: {
                purchaseRequest: require("./test/data-util/purchasing/purchase-request-data-util")
            },
            production: {
                dailyOperation: require('./test/data-util/production/finishing-printing/daily-operation-data-util'),
                monitoringEvent: require('./test/data-util/production/finishing-printing/monitoring-event-data-util'),
                monitoringSpecificationMachine: require('./test/data-util/production/finishing-printing/monitoring-specification-machine-data-util'),
                kanban: require('./test/data-util/production/finishing-printing/kanban-data-util')
            },
            sales:{
                productionOrder: require('./test/data-util/sales/production-order-data-util')
            }
        }
    },
    etl: {
        factPembelian: require("./src/etl/fact-pembelian"),
        factTotalHutang: require("./src/etl/fact-total-hutang-etl-manager"),
        dimCategory: require("./src/etl/dim-category-etl-manager"),
        dimDivision: require("./src/etl/dim-unit-etl-manager"),
        dimSupplier: require("./src/etl/dim-supplier-etl-manager"),
        dimUnit: require("./src/etl/dim-unit-etl-manager"),
        dimMachine: require("./src/etl/dim-machine-etl-manager"),
        factMonitoringEvent: require("./src/etl/fact-monitoring-event-etl-manager"),
        factSalesContract: require("./src/etl/fact-sales-contract-etl-manager")
    }
}
<commit_msg>index<commit_after>module.exports = {
    managers: {
        auth: {
            AccountManager: require("./src/managers/auth/account-manager"),
            RoleManager: require("./src/managers/auth/role-manager"),
            ApiEndpointManager: require("./src/managers/auth/api-endpoint-manager")
        },
        master: {
            BuyerManager: require("./src/managers/master/buyer-manager"),
            SupplierManager: require("./src/managers/master/supplier-manager"),
            ProductManager: require("./src/managers/master/product-manager"),
            CategoryManager: require('./src/managers/master/category-manager'),
            DivisionManager: require('./src/managers/master/division-manager'),
            UnitManager: require('./src/managers/master/unit-manager'),
            UomManager: require('./src/managers/master/uom-manager'),
            CurrencyManager: require('./src/managers/master/currency-manager'),
            VatManager: require('./src/managers/master/vat-manager'),
            BudgetManager: require('./src/managers/master/budget-manager'),
            ThreadSpecificationManager: require('./src/managers/master/thread-specification-manager'),
            MachineManager: require('./src/managers/master/machine-manager'),
            MachineTypeManager: require('./src/managers/master/machine-type-manager'),
            LotMachineManager: require('./src/managers/master/lot-machine-manager'),
            YarnEquivalentConversion: require('./src/managers/master/yarn-equivalent-conversion-manager'),
            UsterManager: require('./src/managers/master/uster-manager'),
            LampStandardManager: require('./src/managers/master/lamp-standard-manager'),
            AccountBankManager: require('./src/managers/master/account-bank-manager'),
            InstructionManager: require('./src/managers/master/instruction-manager'),
            StepManager: require('./src/managers/master/step-manager'),
            ProcessTypeManager: require('./src/managers/master/process-type-manager'),
            OrderTypeManager: require('./src/managers/master/order-type-manager'),
            ColorTypeManager: require('./src/managers/master/color-type-manager'),
            MaterialConstructionManager: require('./src/managers/master/material-construction-manager'),
            FinishTypeManager: require('./src/managers/master/finish-type-manager'),
            StandardTestManager: require('./src/managers/master/standard-test-manager'),
            YarnMaterialManager: require('./src/managers/master/yarn-material-manager'),
            ComodityManager: require('./src/managers/master/comodity-manager'),
            QualityManager: require('./src/managers/master/quality-manager')
        },
        purchasing: {
            PurchaseOrderManager: require('./src/managers/purchasing/purchase-order-manager'),
            PurchaseOrderExternalManager: require('./src/managers/purchasing/purchase-order-external-manager'),
            DeliveryOrderManager: require('./src/managers/purchasing/delivery-order-manager'),
            UnitReceiptNoteManager: require('./src/managers/purchasing/unit-receipt-note-manager'),
            PurchaseRequestManager: require('./src/managers/purchasing/purchase-request-manager'),
            UnitPaymentPriceCorrectionNoteManager: require('./src/managers/purchasing/unit-payment-price-correction-note-manager'),
            UnitPaymentOrderManager: require('./src/managers/purchasing/unit-payment-order-manager'),
            UnitPaymentQuantityCorrectionNoteManager: require('./src/managers/purchasing/unit-payment-quantity-correction-note-manager')
        },
        production: {
            spinning: {
                winding: {
                    WindingQualitySampling: require('./src/managers/production/spinning/winding/winding-quality-sampling-manager'),
                    WindingProductionOutput: require('./src/managers/production/spinning/winding/winding-production-output-manager')
                },
                DailySpinningProductionReportManager: require('./src/managers/production/spinning/daily-spinning-production-report-manager')
            },
            finishingPrinting: {
                DailyOperationManager: require('./src/managers/production/finishing-printing/daily-operation-manager'),
                MonitoringEventManager: require('./src/managers/production/finishing-printing/monitoring-event-manager'),
                MonitoringSpecificationMachineManager: require('./src/managers/production/finishing-printing/monitoring-specification-machine-manager'),
                KanbanManager: require('./src/managers/production/finishing-printing/kanban-manager')
            }
        },
        sales:{
            ProductionOrderManager: require('./src/managers/sales/production-order-manager'),
            FinishingPrintingSalesContract: require('./test/data-util/sales/finishing-printing-sales-contract-manager')
        }

    },
    test: {
        data: {
            auth: {
                account: require("./test/data-util/auth/account-data-util"),
                role: require("./test/data-util/auth/role-data-util")
            },
            master: {
                accountBank: require("./test/data-util/master/account-bank-data-util"),
                budget: require("./test/data-util/master/budget-data-util"),
                buyer: require("./test/data-util/master/buyer-data-util"),
                category: require("./test/data-util/master/category-data-util"),
                currency: require("./test/data-util/master/currency-data-util"),
                division: require("./test/data-util/master/division-data-util"),
                lampStandard: require("./test/data-util/master/lamp-standard-data-util"),
                lotMachine: require("./test/data-util/master/lot-machine-data-util"),
                machine: require("./test/data-util/master/machine-data-util"),
                machineType: require("./test/data-util/master/machine-type-data-util"),
                product: require("./test/data-util/master/product-data-util"),
                supplier: require("./test/data-util/master/supplier-data-util"),
                threadSpecification: require("./test/data-util/master/thread-specification-data-util"),
                unit: require("./test/data-util/master/unit-data-util"),
                uom: require("./test/data-util/master/uom-data-util"),
                uster: require("./test/data-util/master/uster-data-util"),
                vat: require("./test/data-util/master/vat-data-util"),
                yarnEquivalentConversion: require("./test/data-util/master/yarn-equivalent-conversion-data-util"),
                step: require("./test/data-util/master/step-data-util"),
                instruction: require("./test/data-util/master/instruction-data-util"),
                orderType: require('./test/data-util/master/order-type-data-util'),
                processType: require('./test/data-util/master/process-type-data-util'),
                materialConstruction: require('./test/data-util/master/material-construction-data-util'),
                yarnMaterial: require('./test/data-util/master/yarn-material-data-util'),
                finishType: require('./test/data-util/master/finish-type-data-util'),
                standardTest: require('./test/data-util/master/standard-test-data-util'),
                colorType: require('./test/data-util/master/color-type-data-util'),
                comodity: require('./test/data-util/master/comodity-data-util'),
                quality: require('./test/data-util/master/quality-data-util')
            },
            purchasing: {
                purchaseRequest: require("./test/data-util/purchasing/purchase-request-data-util")
            },
            production: {
                dailyOperation: require('./test/data-util/production/finishing-printing/daily-operation-data-util'),
                monitoringEvent: require('./test/data-util/production/finishing-printing/monitoring-event-data-util'),
                monitoringSpecificationMachine: require('./test/data-util/production/finishing-printing/monitoring-specification-machine-data-util'),
                kanban: require('./test/data-util/production/finishing-printing/kanban-data-util')
            },
            sales:{
                productionOrder: require('./test/data-util/sales/production-order-data-util'),
                finishingPrintingSalesContract: require('./test/data-util/sales/finishing-printing-sales-contract-data-util')
            }
        }
    },
    etl: {
        factPembelian: require("./src/etl/fact-pembelian"),
        factTotalHutang: require("./src/etl/fact-total-hutang-etl-manager"),
        dimCategory: require("./src/etl/dim-category-etl-manager"),
        dimDivision: require("./src/etl/dim-unit-etl-manager"),
        dimSupplier: require("./src/etl/dim-supplier-etl-manager"),
        dimUnit: require("./src/etl/dim-unit-etl-manager"),
        dimMachine: require("./src/etl/dim-machine-etl-manager"),
        factMonitoringEvent: require("./src/etl/fact-monitoring-event-etl-manager"),
        factSalesContract: require("./src/etl/fact-sales-contract-etl-manager")
    }
}
<|endoftext|>


<commit_before>'use latest';

import P from 'bluebird';
import request from 'superagent-bluebird-promise';
import twilio from 'twilio';

const { TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_PHONE_NUMBER, MY_PHONE_NUMBER, URL } = ctx.data;

const twilioClient = new twilio.RestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
const createMessage = P.promisify(twilioClient.sms.messages.create, twilioClient);

<commit_msg>err key missing appropriately<commit_after>'use latest';

import P from 'bluebird';
import request from 'superagent-bluebird-promise';
import twilio from 'twilio';

const { TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_PHONE_NUMBER, MY_PHONE_NUMBER, URL } = ctx.data;

const twilioClient = new twilio.RestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
const createMessage = P.promisify(twilioClient.sms.messages.create, twilioClient);

export default function(ctx, cb) {

  if (!TWILIO_ACCOUNT_SID || !TWILIO_AUTH_TOKEN || !TWILIO_PHONE_NUMBER || !MY_PHONE_NUMBER) {
    return cb(new Error('Key missing'));
  }

}
<|endoftext|>


<commit_before>// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Kinesis.html
const AWS = require('aws-sdk')

const kinesis = new AWS.Kinesis()

function listStreams () {
  kinesis.listStreams({}, (err, data) => {
    if (err) {
      console.error(err)
    } else {
      console.log(data)
    }
  })
}

function getShardId (streamName) {
  return new Promise((resolve, reject) => {
    const params = {
      StreamName: streamName,
    }
    kinesis.describeStream(params, (err, data) => {
      if (err) {
        reject(err)
      } else {
        if (data.StreamDescription.Shards.length) {
          // For heavy duty cases, we would return all shard ids and spin up a
          // reader for each shards
          resolve(data.StreamDescription.Shards[0].ShardId)
        } else {
          reject('No shards!')
        }
      }
    })
  })
}

function getShardIterator (streamName, shardId) {
  return new Promise((resolve, reject) => {
    const params = {
      ShardId: shardId,
      ShardIteratorType: 'LATEST',
      StreamName: streamName,
    }
    kinesis.getShardIterator(params, (err, data) => {
      if (err) {
        reject(err)
      } else {
        resolve(data.ShardIterator)
      }
    })
  })
}

function readShard (shardIterator) {
  const params = {
    ShardIterator: shardIterator,
    Limit: 100,
  }
  kinesis.getRecords(params, (err, data) => {
    if (err) console.log(err, err.stack)
    else {
      data.Records.forEach((x) => {
        console.log(x.Data)
        console.log(x.Data.toString())
      })
      readShard(data.NextShardIterator)
    }
  })
}

// EXPORTS
//////////

module.exports.listStreams = listStreams

module.exports.main = function (streamName) {
  getShardId(streamName)
  .then((shardId) => getShardIterator(streamName, shardId))
  .then((shardIterator) => readShard(shardIterator))
  .catch((err) => console.log(err, err.stack))
}
<commit_msg>remove debugging<commit_after>// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Kinesis.html
const AWS = require('aws-sdk')

const kinesis = new AWS.Kinesis()

function listStreams () {
  kinesis.listStreams({}, (err, data) => {
    if (err) {
      console.error(err)
    } else {
      console.log(data)
    }
  })
}

function getShardId (streamName) {
  return new Promise((resolve, reject) => {
    const params = {
      StreamName: streamName,
    }
    kinesis.describeStream(params, (err, data) => {
      if (err) {
        reject(err)
      } else {
        if (data.StreamDescription.Shards.length) {
          // For heavy duty cases, we would return all shard ids and spin up a
          // reader for each shards
          resolve(data.StreamDescription.Shards[0].ShardId)
        } else {
          reject('No shards!')
        }
      }
    })
  })
}

function getShardIterator (streamName, shardId) {
  return new Promise((resolve, reject) => {
    const params = {
      ShardId: shardId,
      ShardIteratorType: 'LATEST',
      StreamName: streamName,
    }
    kinesis.getShardIterator(params, (err, data) => {
      if (err) {
        reject(err)
      } else {
        resolve(data.ShardIterator)
      }
    })
  })
}

function readShard (shardIterator) {
  const params = {
    ShardIterator: shardIterator,
    Limit: 100,
  }
  kinesis.getRecords(params, (err, data) => {
    if (err) console.log(err, err.stack)
    else {
      data.Records.forEach((x) => {
        console.log(x.Data.toString())
      })
      readShard(data.NextShardIterator)
    }
  })
}

// EXPORTS
//////////

module.exports.listStreams = listStreams

module.exports.main = function (streamName) {
  getShardId(streamName)
  .then((shardId) => getShardIterator(streamName, shardId))
  .then((shardIterator) => readShard(shardIterator))
  .catch((err) => console.log(err, err.stack))
}
<|endoftext|>


<commit_before>var transformTools = require('browserify-transform-tools');
var resolve = require('resolve');
var path = require('path')

var transform = transformTools.makeRequireTransform(
    "resolvify",
    {
        evaluateArguments: true,
        jsFilesOnly: true
    },
    function(args, opts, cb) {
        var paths = Object.keys(opts.config)
                        .filter(function(opts_key) { return opts_key.length == 1 })
                        .reduce(function(coll, opts_key) {
                            return coll.concat(opts.config[opts_key])
                        },[])
        if (paths.length == 0) { return cb(null) }
        resolved = resolve.sync(args[0], {
            basedir: path.dirname(opts.file),
            moduleDirectory: ['node_modules'].concat(paths)
        });

        return cb(null, "require('"+resolved+"')");
    }
);

module.exports = transform;
<commit_msg>Escape windows path separators<commit_after>var transformTools = require('browserify-transform-tools');
var resolve = require('resolve');
var path = require('path')

var transform = transformTools.makeRequireTransform(
    "resolvify",
    {
        evaluateArguments: true,
        jsFilesOnly: true
    },
    function(args, opts, cb) {
        var paths = Object.keys(opts.config)
                        .filter(function(opts_key) { return opts_key.length == 1 })
                        .reduce(function(coll, opts_key) {
                            return coll.concat(opts.config[opts_key])
                        },[])
        if (paths.length == 0) { return cb(null) }
        resolved = resolve.sync(args[0], {
            basedir: path.dirname(opts.file),
            moduleDirectory: ['node_modules'].concat(paths)
        });

        return cb(null, "require('"+resolved.replace(/\\/g,'\\\\')+"')");
    }
);

module.exports = transform;
<|endoftext|>


<commit_before>'use strict';
var fs = require('fs');
var http = require('http');
var path = require('path');
var os = require('os');

var videoExtensions = require('video-extensions');
var allLanguages = require('./languages');
var md5 = require('MD5');
var request = require('request');

var NOOP = function () {};

var USER_AGENT_STRING = 'SubDB/1.0 (subfil/0.1; '
	+ 'https://github.com/divijbindlish/subfil)';

var generateHash = function (filename, callback) {
	callback = arguments[arguments.length - 1];
	if (typeof callback !== 'function') {
		callback = NOOP;
	}

	var ext = path.extname(filename).slice(1);
	if (videoExtensions.indexOf(ext) === -1) {
		var err = new Error('Invalid video file');
		callback(err, undefined);
		return;
	}

	fs.open(filename, 'r', function (err, fd) {
		if (err) {
			callback(err, undefined);
			return;
		}

		fs.fstat(fd, function (err, stats) {
			if (err) {
				callback(err, undefined);
				return;
			}

			var readsize = 1 << 16;
			var buffer = new Buffer(readsize << 1);

			fs.read(fd, buffer, 0, readsize, 0, function (err) {
				if (err) {
					callback(err, undefined);
					return;
				}

				var position = stats['size'] - readsize;
				fs.read(fd, buffer, readsize, readsize, position, function(err) {
					if (err) {
						callback(err, undefined);
						return;
					}

					var hash = md5(buffer);
					callback(undefined, hash);
				});
			});
		});
	});
};

var getLanguagesForHash = function (hash, callback) {
	callback = arguments[arguments.length - 1];
	if (typeof callback !== 'function') {
		callback = NOOP;
	}

	if (!hash.match(/^[a-f0-9]{32}$/)) {
		var err = new Error('Invalid MD5 hash');
		callback(err, undefined);
		return;
	}

	request({
		url: 'http://api.thesubdb.com',
		qs: {
			action: 'search',
			hash: hash,
		},
		method: 'GET',
		headers: {
			'User-Agent': USER_AGENT_STRING,
		}
	}, function (err, response, body) {
		if (err) {
			callback(err, undefined);
			return;
		}

		if (response.statusCode === 404) {
			err = new Error('No subtitles for hash');
			callback(err, undefined);
			return;
		}

		if (response.statusCode !== 200) {
			err = new Error('Invalid request');
			callback(err, undefined);
			return;
		}

		var languages = response.body.split(',');
		callback(undefined, languages);
	});
};

var downloadForHash = function (hash, language, destination, callback) {
	request({
		url: 'http://api.thesubdb.com',
		qs: {
			action: 'download',
			hash: hash,
		},
		method: 'GET',
		headers: {
			'User-Agent': USER_AGENT_STRING,
		}
	}, function(err, response, body) {
		if (err) {
			callback(err, undefined);
			return;
		}

		if (response.statusCode === 404) {
			err = new Error('No subtitles for hash');
			callback(err, undefined);
			return;
		}

		if (response.statusCode !== 200) {
			err = new Error('Invalid request');
			callback(err, undefined);
			return;
		}

		fs.open(destination, 'w', function (err, fd) {
			if (err) {
				callback(err, undefined);
				return;
			}

			fs.write(fd, body, function (err, bytesWritten, string) {
				if (err) {
					callback(err, undefined);
					return;
				}

				fs.close(fd, function (err) {
					if (err) {
						callback(err, undefined);
						return;
					}

					callback(undefined, destination);
				});
			});
		});
	});
};

var getLanguages = function (name, callback) {
	callback = arguments[arguments.length - 1];
	if (typeof callback !== 'function') {
		callback = NOOP;
	}

	if (name.match(/^[a-f0-9]{32}$/)) {
		// Process for hash
		var hash = name;
		getLanguagesForHash(hash, callback);
	} else {
		// Process for file
		var filename = name;
		generateHash(filename, function (err, hash) {
			if (err) {
				callback(err, undefined);
				return;
			}

			getLanguagesForHash(hash, callback);
		});
	}
};

var download = function (name, language, destination, callback) {
	var separator = /^win/.test(process.platform) ? '\\' : '/';

	callback = arguments[arguments.length - 1];
	if (typeof callback !== 'function') {
		callback = NOOP;
	}

	if (typeof language !== 'string') {
		language = 'en';
	} else {
		if (allLanguages.indexOf(language) === -1) {
			var err = new Error('Invalid language');
			callback(err, undefined);
			return;
		}
	}

	if (name.match(/^[a-f0-9]{32}$/)) {
		// Process for hash
		var hash = name;
		if (typeof destination !== 'string') {
			destination = os.tmpdir() + separator + hash + '.srt';
		}

		downloadForHash(hash, language, destination, callback);
	} else {
		// Process for file
		var filename = name;
		generateHash(filename, function (err, hash) {
			if (err) {
				callback(err, undefined);
				return;
			}

			if (typeof destination !== 'string') {
				destination = path.dirname(filename) + separator
					+ path.basename(filename, path.extname(filename))
					+ '-' + language + '.srt';
			}

			downloadForHash(hash, language, destination, callback);
		});
	}
};

module.exports = {
	getLanguages: getLanguages,
	download: download
};
<commit_msg>Fix issue with language not being sent in querystring<commit_after>'use strict';
var fs = require('fs');
var http = require('http');
var path = require('path');
var os = require('os');

var videoExtensions = require('video-extensions');
var allLanguages = require('./languages');
var md5 = require('MD5');
var request = require('request');

var NOOP = function () {};

var USER_AGENT_STRING = 'SubDB/1.0 (subfil/0.1; '
	+ 'https://github.com/divijbindlish/subfil)';

var generateHash = function (filename, callback) {
	callback = arguments[arguments.length - 1];
	if (typeof callback !== 'function') {
		callback = NOOP;
	}

	var ext = path.extname(filename).slice(1);
	if (videoExtensions.indexOf(ext) === -1) {
		var err = new Error('Invalid video file');
		callback(err, undefined);
		return;
	}

	fs.open(filename, 'r', function (err, fd) {
		if (err) {
			callback(err, undefined);
			return;
		}

		fs.fstat(fd, function (err, stats) {
			if (err) {
				callback(err, undefined);
				return;
			}

			var readsize = 1 << 16;
			var buffer = new Buffer(readsize << 1);

			fs.read(fd, buffer, 0, readsize, 0, function (err) {
				if (err) {
					callback(err, undefined);
					return;
				}

				var position = stats['size'] - readsize;
				fs.read(fd, buffer, readsize, readsize, position, function(err) {
					if (err) {
						callback(err, undefined);
						return;
					}

					var hash = md5(buffer);
					callback(undefined, hash);
				});
			});
		});
	});
};

var getLanguagesForHash = function (hash, callback) {
	callback = arguments[arguments.length - 1];
	if (typeof callback !== 'function') {
		callback = NOOP;
	}

	if (!hash.match(/^[a-f0-9]{32}$/)) {
		var err = new Error('Invalid MD5 hash');
		callback(err, undefined);
		return;
	}

	request({
		url: 'http://api.thesubdb.com',
		qs: {
			action: 'search',
			hash: hash,
		},
		method: 'GET',
		headers: {
			'User-Agent': USER_AGENT_STRING,
		}
	}, function (err, response, body) {
		if (err) {
			callback(err, undefined);
			return;
		}

		if (response.statusCode === 404) {
			err = new Error('No subtitles for hash');
			callback(err, undefined);
			return;
		}

		if (response.statusCode !== 200) {
			err = new Error('Invalid request');
			callback(err, undefined);
			return;
		}

		var languages = response.body.split(',');
		callback(undefined, languages);
	});
};

var downloadForHash = function (hash, language, destination, callback) {
	request({
		url: 'http://api.thesubdb.com',
		qs: {
			action: 'download',
			language: language,
			hash: hash,
		},
		method: 'GET',
		headers: {
			'User-Agent': USER_AGENT_STRING,
		}
	}, function(err, response, body) {
		if (err) {
			callback(err, undefined);
			return;
		}

		if (response.statusCode === 404) {
			err = new Error('No subtitles for hash');
			callback(err, undefined);
			return;
		}

		if (response.statusCode !== 200) {
			err = new Error('Invalid request');
			callback(err, undefined);
			return;
		}

		fs.open(destination, 'w', function (err, fd) {
			if (err) {
				callback(err, undefined);
				return;
			}

			fs.write(fd, body, function (err, bytesWritten, string) {
				if (err) {
					callback(err, undefined);
					return;
				}

				fs.close(fd, function (err) {
					if (err) {
						callback(err, undefined);
						return;
					}

					callback(undefined, destination);
				});
			});
		});
	});
};

var getLanguages = function (name, callback) {
	callback = arguments[arguments.length - 1];
	if (typeof callback !== 'function') {
		callback = NOOP;
	}

	if (name.match(/^[a-f0-9]{32}$/)) {
		// Process for hash
		var hash = name;
		getLanguagesForHash(hash, callback);
	} else {
		// Process for file
		var filename = name;
		generateHash(filename, function (err, hash) {
			if (err) {
				callback(err, undefined);
				return;
			}

			getLanguagesForHash(hash, callback);
		});
	}
};

var download = function (name, language, destination, callback) {
	var separator = /^win/.test(process.platform) ? '\\' : '/';

	callback = arguments[arguments.length - 1];
	if (typeof callback !== 'function') {
		callback = NOOP;
	}

	if (typeof language !== 'string') {
		language = 'en';
	} else {
		if (allLanguages.indexOf(language) === -1) {
			var err = new Error('Invalid language');
			callback(err, undefined);
			return;
		}
	}

	if (name.match(/^[a-f0-9]{32}$/)) {
		// Process for hash
		var hash = name;
		if (typeof destination !== 'string') {
			destination = os.tmpdir() + separator + hash + '.srt';
		}

		downloadForHash(hash, language, destination, callback);
	} else {
		// Process for file
		var filename = name;
		generateHash(filename, function (err, hash) {
			if (err) {
				callback(err, undefined);
				return;
			}

			if (typeof destination !== 'string') {
				destination = path.dirname(filename) + separator
					+ path.basename(filename, path.extname(filename))
					+ '-' + language + '.srt';
			}

			downloadForHash(hash, language, destination, callback);
		});
	}
};

module.exports = {
	getLanguages: getLanguages,
	download: download
};
<|endoftext|>


<commit_before>var postcss = require('postcss');

const DEBUG = false;
const SUPPORT_PROPS = ['float', 'clear', 'text-align', 'padding-inline-start', 'padding-inline-end',
                       'border-inline-start', 'border-inline-end', 'margin-inline-start', 'margin-inline-end',
                       'offset-inline-start', 'offset-inline-end'
                      ];

function log(...args) {
    (args || []).forEach(arg => {
        if (DEBUG) console.log(JSON.stringify(arg, null, 2));
    });
}

function cloneItem(item) {
    let newRule = item.clone();
    newRule.raws.before = item.raws.before;
    newRule.raws.after = item.raws.after;
    newRule.raws.between = item.raws.between;
    return newRule;
}

// process LTR by default, process RTL when reverseFlag is set
function processProps(decl, reverseFlag) {
    let isDirty = false;
    let start = !reverseFlag ? 'left' : 'right';
    let end = !reverseFlag ? 'right' : 'left';
    switch (decl.prop.toLowerCase()) {
    case 'float':
    case 'clear':
    case 'text-align':
        if(['start', 'end'].indexOf(decl.value.toLowerCase()) !== -1) {
            if (decl.value.toLowerCase() === 'start') {
                decl.value = start;
            } else {
                decl.value = end;
            }
            isDirty = true;
        }
        break;
    case 'padding-inline-start':
        decl.prop = 'padding-' + start;
        isDirty = true;
        break;
    case 'padding-inline-end':
        decl.prop = 'padding-' + end;
        isDirty = true;
        break;
    case 'border-inline-start':
        decl.prop = 'border-' + start;
        isDirty = true;
        break;
    case 'border-inline-end':
        decl.prop = 'border-' + end;
        isDirty = true;
        break;
    case 'border-inline-start-color':
        decl.prop = 'border-' + start + '-color';
        isDirty = true;
        break;
    case 'border-inline-end-color':
        decl.prop = 'border-' + end + '-color';
        isDirty = true;
        break;
    case 'border-inline-start-width':
        decl.prop = 'border-' + start + '-width';
        isDirty = true;
        break;
    case 'border-inline-end-width':
        decl.prop = 'border-' + end + '-width';
        isDirty = true;
        break;
    case 'border-inline-start-style':
        decl.prop = 'border-' + start + '-style';
        isDirty = true;
        break;
    case 'border-inline-end-style':
        decl.prop = 'border-' + end + '-style';
        isDirty = true;
        break;
    case 'border-top-inline-start-radius':
        decl.prop = 'border-top-' + start + '-radius';
        isDirty = true;
        break;
    case 'border-top-inline-end-radius':
        decl.prop = 'border-top-' + end + '-radius';
        isDirty = true;
        break;
    case 'border-bottom-inline-start-radius':
        decl.prop = 'border-bottom-' + start + '-radius';
        isDirty = true;
        break;
    case 'border-bottom-inline-end-radius':
        decl.prop = 'border-bottom-' + end + '-radius';
        isDirty = true;
        break;
    case 'margin-inline-start':
        decl.prop = 'margin-' + start;
        isDirty = true;
        break;
    case 'margin-inline-end':
        decl.prop = 'margin-' + end;
        isDirty = true;
        break;
    case 'offset-inline-start':
        decl.prop = start;
        isDirty = true;
        break;
    case 'offset-inline-end':
        decl.prop = end;
        isDirty = true;
        break;
    default:
        break;
    }

    if (isDirty) log(decl.prop + ' : ' + decl.value);

    const newDecl = decl;
    return { newDecl, isDirty };
}

function isDirty(decl) {
    if (SUPPORT_PROPS.indexOf(decl.prop.toLowerCase()) > -1) {
        return true;
    }
}

function updateRtlRule(item) {
    return item.rtlNodes.filter(decl => {
        const { newDecl, isDirty } = processProps(decl, true);
        if (isDirty) {
            return newDecl;
        }
    });
}

function updateLtrRule(item) {
    return item.ltrNodes.filter(decl => {
        const { newDecl, isDirty } = processProps(decl);
        if (isDirty) {
            return newDecl;
        }
    });
}

function updateRtlItem(item, reverseFlag) {
    let rule = reverseFlag ? item.ltrRule : item.rtlRule;
    for ( let i in rule ) {
        if ( !rule.hasOwnProperty(i) ) {
            continue;
        }

        if ( rule[i] instanceof Array ) {
            rule[i] = reverseFlag ? updateLtrRule(item) : updateRtlRule(item);
        }
    }

    return item;
}

function updateLtrItem(item) {
    return updateRtlItem(item, true);
}

module.exports = postcss.plugin('postcss-bidirection', function (opts) {
    opts = opts || {};
    const PATTERN = /\s*[,\n]+\s*/;

    // Work with options here

    return function (root) {
        let tree = [];
        let idx = 0;
        let currentIdx;

        // Rebuild tree for reuse
        root.walk(function (item) {
            if (item.type === 'rule') {
                tree[idx] = {
                    rule:     item,
                    nodes:    [],
                    ltrRule:  cloneItem(item),
                    ltrNodes: [],
                    rtlRule:  cloneItem(item),
                    rtlNodes: [],
                    isBiDi:   false
                };
                currentIdx = idx;
                idx += 1;
            } else if (item.type === 'decl') {
                tree[currentIdx].nodes.push(item);
                tree[currentIdx].ltrNodes.push(cloneItem(item));
                tree[currentIdx].rtlNodes.push(cloneItem(item));
            }
        });

        // Transform CSS AST here
        // Unefficient but works

        // default
        tree.forEach(item => {
            item.nodes = item.nodes.filter(decl => {
              if (isDirty(decl)) {
                  item.isBiDi = true;
              }
              return decl;
            });
        });

        tree.forEach((item) => {
            if (item.isBiDi) {

                // LTR
                // modified from postcss internal clone method
                updateLtrItem(item, true);

                root.insertAfter(item.rule, item.ltrRule);

                // prefix each comma-separated selector
                item.ltrRule.selector = item.ltrRule.selector
                    .split(PATTERN)
                    .map(function(selector){
                        return ("[dir=\"ltr\"] " + selector);
                    })
                    .join(',\n');

                // RTL
                updateRtlItem(item);

                root.insertAfter(item.ltrRule, item.rtlRule);

                // prefix each comma-separated selector
                item.ltrRule.selector = item.ltrRule.selector
                    .split(PATTERN)
                    .map(function(selector){
                        return ("[dir=\"rtl\"] " + selector);
                    })
                    .join(',\n');

                log('<', item.rule.raw.before,
                    '>', item.rtlRule.raw.before);
            }
        });
    };
});
<commit_msg>Regression: Fix oversight in previous commit<commit_after>var postcss = require('postcss');

const DEBUG = false;
const SUPPORT_PROPS = ['float', 'clear', 'text-align', 'padding-inline-start', 'padding-inline-end',
                       'border-inline-start', 'border-inline-end', 'margin-inline-start', 'margin-inline-end',
                       'offset-inline-start', 'offset-inline-end'
                      ];

function log(...args) {
    (args || []).forEach(arg => {
        if (DEBUG) console.log(JSON.stringify(arg, null, 2));
    });
}

function cloneItem(item) {
    let newRule = item.clone();
    newRule.raws.before = item.raws.before;
    newRule.raws.after = item.raws.after;
    newRule.raws.between = item.raws.between;
    return newRule;
}

// process LTR by default, process RTL when reverseFlag is set
function processProps(decl, reverseFlag) {
    let isDirty = false;
    let start = !reverseFlag ? 'left' : 'right';
    let end = !reverseFlag ? 'right' : 'left';
    switch (decl.prop.toLowerCase()) {
    case 'float':
    case 'clear':
    case 'text-align':
        if(['start', 'end'].indexOf(decl.value.toLowerCase()) !== -1) {
            if (decl.value.toLowerCase() === 'start') {
                decl.value = start;
            } else {
                decl.value = end;
            }
            isDirty = true;
        }
        break;
    case 'padding-inline-start':
        decl.prop = 'padding-' + start;
        isDirty = true;
        break;
    case 'padding-inline-end':
        decl.prop = 'padding-' + end;
        isDirty = true;
        break;
    case 'border-inline-start':
        decl.prop = 'border-' + start;
        isDirty = true;
        break;
    case 'border-inline-end':
        decl.prop = 'border-' + end;
        isDirty = true;
        break;
    case 'border-inline-start-color':
        decl.prop = 'border-' + start + '-color';
        isDirty = true;
        break;
    case 'border-inline-end-color':
        decl.prop = 'border-' + end + '-color';
        isDirty = true;
        break;
    case 'border-inline-start-width':
        decl.prop = 'border-' + start + '-width';
        isDirty = true;
        break;
    case 'border-inline-end-width':
        decl.prop = 'border-' + end + '-width';
        isDirty = true;
        break;
    case 'border-inline-start-style':
        decl.prop = 'border-' + start + '-style';
        isDirty = true;
        break;
    case 'border-inline-end-style':
        decl.prop = 'border-' + end + '-style';
        isDirty = true;
        break;
    case 'border-top-inline-start-radius':
        decl.prop = 'border-top-' + start + '-radius';
        isDirty = true;
        break;
    case 'border-top-inline-end-radius':
        decl.prop = 'border-top-' + end + '-radius';
        isDirty = true;
        break;
    case 'border-bottom-inline-start-radius':
        decl.prop = 'border-bottom-' + start + '-radius';
        isDirty = true;
        break;
    case 'border-bottom-inline-end-radius':
        decl.prop = 'border-bottom-' + end + '-radius';
        isDirty = true;
        break;
    case 'margin-inline-start':
        decl.prop = 'margin-' + start;
        isDirty = true;
        break;
    case 'margin-inline-end':
        decl.prop = 'margin-' + end;
        isDirty = true;
        break;
    case 'offset-inline-start':
        decl.prop = start;
        isDirty = true;
        break;
    case 'offset-inline-end':
        decl.prop = end;
        isDirty = true;
        break;
    default:
        break;
    }

    if (isDirty) log(decl.prop + ' : ' + decl.value);

    const newDecl = decl;
    return { newDecl, isDirty };
}

function isDirty(decl) {
    if (SUPPORT_PROPS.indexOf(decl.prop.toLowerCase()) > -1) {
        return true;
    }
}

function updateRtlRule(item) {
    return item.rtlNodes.filter(decl => {
        const { newDecl, isDirty } = processProps(decl, true);
        if (isDirty) {
            return newDecl;
        }
    });
}

function updateLtrRule(item) {
    return item.ltrNodes.filter(decl => {
        const { newDecl, isDirty } = processProps(decl);
        if (isDirty) {
            return newDecl;
        }
    });
}

function updateRtlItem(item, reverseFlag) {
    let rule = reverseFlag ? item.ltrRule : item.rtlRule;
    for ( let i in rule ) {
        if ( !rule.hasOwnProperty(i) ) {
            continue;
        }

        if ( rule[i] instanceof Array ) {
            rule[i] = reverseFlag ? updateLtrRule(item) : updateRtlRule(item);
        }
    }

    return item;
}

function updateLtrItem(item) {
    return updateRtlItem(item, true);
}

module.exports = postcss.plugin('postcss-bidirection', function (opts) {
    opts = opts || {};
    const PATTERN = /\s*[,\n]+\s*/;

    // Work with options here

    return function (root) {
        let tree = [];
        let idx = 0;
        let currentIdx;

        // Rebuild tree for reuse
        root.walk(function (item) {
            if (item.type === 'rule') {
                tree[idx] = {
                    rule:     item,
                    nodes:    [],
                    ltrRule:  cloneItem(item),
                    ltrNodes: [],
                    rtlRule:  cloneItem(item),
                    rtlNodes: [],
                    isBiDi:   false
                };
                currentIdx = idx;
                idx += 1;
            } else if (item.type === 'decl') {
                tree[currentIdx].nodes.push(item);
                tree[currentIdx].ltrNodes.push(cloneItem(item));
                tree[currentIdx].rtlNodes.push(cloneItem(item));
            }
        });

        // Transform CSS AST here
        // Unefficient but works

        // default
        tree.forEach(item => {
            item.nodes = item.nodes.filter(decl => {
              if (isDirty(decl)) {
                  item.isBiDi = true;
              }
              return decl;
            });
        });

        tree.forEach((item) => {
            if (item.isBiDi) {

                // LTR
                // modified from postcss internal clone method
                updateLtrItem(item, true);

                root.insertAfter(item.rule, item.ltrRule);

                // prefix each comma-separated selector
                item.ltrRule.selector = item.ltrRule.selector
                    .split(PATTERN)
                    .map(function(selector){
                        return ("[dir=\"ltr\"] " + selector);
                    })
                    .join(',\n');

                // RTL
                updateRtlItem(item);

                root.insertAfter(item.ltrRule, item.rtlRule);

                // prefix each comma-separated selector
                item.rtlRule.selector = item.rtlRule.selector
                    .split(PATTERN)
                    .map(function(selector){
                        return ("[dir=\"rtl\"] " + selector);
                    })
                    .join(',\n');

                log('<', item.rule.raw.before,
                    '>', item.rtlRule.raw.before);
            }
        });
    };
});
<|endoftext|>


<commit_before>'use strict';

var Transform = require('stream').Transform;
var request = require('request');
var fs = require('fs');
var phantomjs = require('phantomjs');
var proc = require('child_process');

/**
 * Collect
 */
function Collect(filter, reject) {
    // new instance
    if( !(this instanceof Collect)) {
        return new Collect(filter);
    }
    // buffer
    this._data = [];

    // default filter
    filter = filter || function(chunk, next){
        next(chunk);
    };

    // inherits _transform
    this._transform = function(chunk, enc, done) {
        if (chunk instanceof Buffer) {
            this._data.push(chunk);
            done();
        }
        else {
            filter.call(this, chunk, function(obj){
                done(null, obj);
            })
        }
    };

    // inherits _flush
    this._flush = function(done){
        if (this._data.length) {
            filter.call(this, { content:this._data.toString() }, function(obj){
                this.push(obj);
                done();
            }.bind(this));
        }
        else {
            done();
        }
    };

    Transform.call(this, { objectMode:true });
}

/**
 * 继承 Transform
 */
Object.setPrototypeOf(Collect.prototype, Transform.prototype);

/**
 * 处理数据
 * @param  {function} 处理函数
 * @return {stream}    Collect实例
 */
Collect.prototype.use = function(mw){
    if (typeof mw === 'function') {
        return this.pipe(Collect(mw));
    }
    return this;
}

/**
 * 获取源数据
 * @param  {String} url
 * @param  {Object} opts
 * @return {stream} Transform stream.
 */
Collect.src = function(url, opts){
    // Options.
    opts = opts || {};
    // Fetch page content with ajax.
    if (opts.javascript) {
        var delay = opts.delay || 3000;
        var timeout = opts.timeout || 10*1000;
        var child = proc.spawn(phantomjs.path, [ __dirname + "/lib/asrc.js", url, delay, timeout ]);
        // filter phantom_echo content
        return child.stdout.pipe(Collect(function(data, next){
            if (data && data.content) {
                data.content = data.content.split('PHANTOM_ECHO')[1];
                next(data);
            }
            else {
                throw new Error('asrc no data.');
            }
        }));
    }
    // Default user-agent
    var UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36';
    // Request params
    var params = {
        url: url,
        headers: {
            'User-Agent': opts.userAgent || UA
        },
        proxy: opts.proxy || '',
        gzip: true,
        timeout: opts.timeout || 10*1000
    };
    // Set cookie
    if (opts.cookie) {
        var j = request.jar();
        var cookie = request.cookie(opts.cookie);
        j.setCookie(cookie, url);
        params.jar = j;
    }
    return request(params).pipe(Collect());
}

/**
 * 处理结果保存到文件
 * @param  {String} filename
 */
Collect.prototype.dest = function(filename, opts) {
    opts = opts || {};
    var dest = fs.createWriteStream(filename, {
        flags: opts.flags || 'a',
        encoding: opts.encoding || 'utf-8'
    });
    this.pipe(Collect(function(data, next){
        if (data && data.content) {
            next(data.content);
        }
        else if (data instanceof String || data instanceof Buffer) {
            next(data);
        }
    })).pipe(dest);
}

Collect.query = require('./lib/query');

module.exports = Collect;
<commit_msg>error handler<commit_after>'use strict';

var Transform = require('stream').Transform;
var request = require('request');
var fs = require('fs');
var phantomjs = require('phantomjs');
var proc = require('child_process');

/**
 * Collect
 */
function Collect(filter, reject) {
    // new instance
    if( !(this instanceof Collect)) {
        return new Collect(filter);
    }
    // buffer
    this._data = [];

    // default filter
    filter = filter || function(chunk, next){
        next(chunk);
    };

    // inherits _transform
    this._transform = function(chunk, enc, done) {
        if (chunk instanceof Buffer) {
            this._data.push(chunk);
            done();
        }
        else {
            try {
                filter.call(this, chunk, function(obj){
                    done(null, obj);
                })
            }
            catch(err) {
                reject && reject(err);
            }
        }
    };

    // inherits _flush
    this._flush = function(done){
        if (this._data.length) {
            try {
                filter.call(this, { content:this._data.toString() }, function(obj){
                    this.push(obj);
                    done();
                }.bind(this));
            }
            catch(err) {
                reject && reject(err);
            }
        }
        else {
            done();
        }
    };

    Transform.call(this, { objectMode:true });
}

/**
 * 继承 Transform
 */
Object.setPrototypeOf(Collect.prototype, Transform.prototype);

/**
 * 处理数据
 * @param  {function} 处理函数
 * @return {stream}    Collect实例
 */
Collect.prototype.use = function(mw){
    if (typeof mw === 'function') {
        return this.pipe(Collect(mw));
    }
    return this;
}

/**
 * 获取源数据
 * @param  {String} url
 * @param  {Object} opts
 * @return {stream} Transform stream.
 */
Collect.src = function(url, opts){
    // Options.
    opts = opts || {};
    // Fetch page content with ajax.
    if (opts.javascript) {
        var delay = opts.delay || 3000;
        var timeout = opts.timeout || 10*1000;
        var child = proc.spawn(phantomjs.path, [ __dirname + "/lib/asrc.js", url, delay, timeout ]);
        // filter phantom_echo content
        return child.stdout.pipe(Collect(function(data, next){
            if (data && data.content) {
                data.content = data.content.split('PHANTOM_ECHO')[1];
                next(data);
            }
            else {
                throw new Error('asrc no data.');
            }
        }));
    }
    // Default user-agent
    var UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36';
    // Request params
    var params = {
        url: url,
        headers: {
            'User-Agent': opts.userAgent || UA
        },
        proxy: opts.proxy || '',
        gzip: true,
        timeout: opts.timeout || 10*1000
    };
    // Set cookie
    if (opts.cookie) {
        var j = request.jar();
        var cookie = request.cookie(opts.cookie);
        j.setCookie(cookie, url);
        params.jar = j;
    }
    return request(params).pipe(Collect());
}

/**
 * 处理结果保存到文件
 * @param  {String} filename
 */
Collect.prototype.dest = function(filename, opts) {
    opts = opts || {};
    var dest = fs.createWriteStream(filename, {
        flags: opts.flags || 'a',
        encoding: opts.encoding || 'utf-8'
    });
    this.pipe(Collect(function(data, next){
        if (data && data.content) {
            next(data.content);
        }
        else if (data instanceof String || data instanceof Buffer) {
            next(data);
        }
    })).pipe(dest);
}

Collect.query = require('./lib/query');

module.exports = Collect;
<|endoftext|>