[
  {
    "path": ".eslintrc.json",
    "content": "{\n  \"extends\": \"semistandard\",\n  \"globals\": {\n    \"java\": false,\n    \"NativeRequire\": true\n  },\n  \"rules\": {\n    \"no-global-assign\": [\n      \"error\",\n      {\n        \"exceptions\": [\n          \"module\",\n          \"require\"\n        ]\n      }\n    ],\n    \"no-native-reassign\": [\n      \"error\",\n      {\n        \"exceptions\": [\n          \"module\",\n          \"require\"\n        ]\n      }\n    ],\n    \"no-extend-native\": [\n      \"error\",\n      {\n        \"exceptions\": [\n          \"String\"\n        ]\n      }\n    ],\n    \"no-new-func\": \"off\"\n  }\n}"
  },
  {
    "path": ".gitignore",
    "content": "target\nnode_modules\n.idea\n*.iml\n*.log\n"
  },
  {
    "path": ".travis.yml",
    "content": "sudo: required\ndist: trusty\nlanguage: java\njdk:\n  - openjdk8\nbefore_script:\nscript:\n   - make ci\nbranches:\n  only:\n    - master\nnotifications:\n  irc: \"irc.freenode.org#brass-monkey\""
  },
  {
    "path": "LICENSE",
    "content": "Copyright 2014-2016 Red Hat, Inc.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n"
  },
  {
    "path": "Makefile",
    "content": "ci: test\n\ntest: lint\n\tnpm run test\n\nlint: node_modules\n\tnpm run lint\n\nclean:\n\trm -rf node_modules target\n\nnode_modules: package.json\n\tnpm install\n\n.PHONY: node_modules"
  },
  {
    "path": "README.md",
    "content": "# jvm-npm\n\nSupport for NPM module loading in Javascript runtimes on the JVM.\nImplementation is based on http://nodejs.org/api/modules.html and\nshould be fully compatible. This, of course, does not include the\nfull node.js API, so don't expect all of the standard NPM modules\nthat depend on it to work. If you want to use existing NPM modules\nthat depend on the Node.js API, consider the not-yet-fully-baked\n[nodyn project](http://nodyn.io). If you are writing your own NPM\nmodules in DynJS, Rhino or Nashorn, this should work just fine.\n\nThis module is known to work with DynJS, and has been briefly tested\non Nashorn, and should work with Rhino as well. \n\n## Usage\n\nUsing the global `load()` functions supplied by DynJS, Nashorn and\nRhino, load `jvm-npm.js` into the global execution context:\n\n    dynjs> load('./jvm-npm.js');\n    dynjs> var x = require('some_module');\n\nOr with Nashorn:\n\n    nashorn> load('./jvm-npm.js');\n    nashorn> var x = require('some_module');\n    \nOf course, you will need to ensure that the jvm-npm.js file exists\nin the current directory.\n\nSee the `examples` directory for simple, runnable usage examples.\nAgain, this will only work out of the box for pure JS NPM modules.\nAnything that depends on the Node.js API will not work with just\nthis file.\n"
  },
  {
    "path": "examples/simple/app.js",
    "content": "load('../../src/main/javascript/jvm-npm.js');\nvar hello = require('./my-module');\nhello('Bob!');\n"
  },
  {
    "path": "examples/simple/my-module.js",
    "content": "module.exports = function sayHello(name) {\n  java.lang.System.out.println(\"Hello \" + name);\n};\n\n"
  },
  {
    "path": "examples/underscore/README.md",
    "content": "# Example: underscore.js\n\nTo run the example, you should have either dynjs or jrunscript\n(Nashorn) in your PATH. And you need to install the underscore NPM\nmodule. First do that.\n\n    $ npm install\n\nThis will look at `package.json`, determine the app dependencies and\ndownload them for you in `./node_modules`. Next choose your runtime,\nand run the app.\n\n    $ dynjs app.js\n\nOr \n\n    $ nashorn app.js\n\nEither should work.\n"
  },
  {
    "path": "examples/underscore/app.js",
    "content": "load('../../src/main/javascript/jvm-npm.js');\nvar _ = require('underscore');\n\nprint('Here we do some stuff with underscore');\n\nexample(\"_.each\", function() {\n  _.each([1, 2, 3], print);\n});\n\nexample(\"_.map\", function() {\n  print(_.map([1, 2, 3], function(num){ return num * 3; }));\n});\n\nexample(\"_.reduce\", function() {\n  print(_.reduce([1, 2, 3], function(memo, num){ \n    return memo + num; \n  }, 0));\n});\n\nexample(\"_.reduceRight\", function() {\n  var list = [[0, 1], [2, 3], [4, 5]];\n  var flat = _.reduceRight(list, function(a, b) { \n    return a.concat(b); \n  }, []);\n  print(flat);\n});\n\nexample(\"_.find\", function() {\n  var even = _.find([1, 2, 3, 4, 5, 6], function(num){ \n    return num % 2 === 0; \n  });\n  print(even);\n});\n\nexample(\"_.filter\", function() {\n  var even = _.filter([1, 2, 3, 4, 5, 6], function(num){ \n    return num % 2 === 0; \n  });\n  print(even);\n});\n\nfunction example(name, ex) {\n  print([\"\", name].join(\"\\n\"));\n  print(ex);\n  ex();\n}\n"
  },
  {
    "path": "examples/underscore/package.json",
    "content": "{\n  \"name\": \"jvm-npm-underscore\",\n  \"description\": \"example application using underscore.js on the JVM\",\n  \"version\": \"0.0.1\",\n  \"private\": true,\n  \"dependencies\": {\n    \"underscore\": \"1.6.0\"\n  }\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"jvm-npm\",\n  \"version\": \"0.1.1\",\n  \"description\": \"Support for NPM module loading in Javascript runtimes on the JVM\",\n  \"main\": \"src/main/javascript/jvm-npm.js\",\n  \"directories\": {\n    \"example\": \"examples\"\n  },\n  \"scripts\": {\n    \"test\": \"mvn integration-test\",\n    \"lint\": \"eslint src/main/javascript/jvm-npm.js\"\n  },\n  \"files\": [\n    \"package.json\",\n    \"pom.xml\",\n    \"README.md\",\n    \"src\"\n  ],\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/nodyn/jvm-npm.git\"\n  },\n  \"author\": \"Lance Ball <lball@redhat.com> (http://lanceball.com)\",\n  \"contributors\": [\n    \"Bob McWhirter <bmcwhirter@redhat.com>\",\n    \"Helio Frota <hesilva@redhat.com> (http://lanceball.com)\"],\n  \"license\": \"Apache-2.0\",\n  \"bugs\": {\n    \"url\": \"https://github.com/nodyn/jvm-npm/issues\"\n  },\n  \"homepage\": \"https://github.com/nodyn/jvm-npm#readme\",\n  \"devDependencies\": {\n    \"eslint\": \"^3.5.0\",\n    \"eslint-config-semistandard\": \"^7.0.0\",\n    \"eslint-config-standard\": \"^6.0.1\",\n    \"eslint-plugin-promise\": \"^2.0.1\",\n    \"eslint-plugin-react\": \"^6.2.2\",\n    \"eslint-plugin-standard\": \"^2.0.0\"\n  }\n}\n"
  },
  {
    "path": "pom.xml",
    "content": "<?xml version=\"1.0\"?>\n<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n\n\n  <modelVersion>4.0.0</modelVersion>\n\n  <groupId>io.nodyn</groupId>\n  <artifactId>jvm-npm</artifactId>\n  <version>0.1.0-SNAPSHOT</version>\n  <url>http://github.com/nodyn/jvm-npm</url>\n  <name>jvm-npm</name>\n\n  <parent>\n    <groupId>org.sonatype.oss</groupId>\n    <artifactId>oss-parent</artifactId>\n    <version>7</version>\n  </parent>\n\n  <properties>\n    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n    <version.dynjs>0.2.3-SNAPSHOT</version.dynjs>\n    <version.jasmine-cli>0.0.12</version.jasmine-cli>\n  </properties>\n\n  <developers>\n    <developer>\n      <id>lanceball</id>\n      <name>Lance Ball</name>\n      <url>http://lanceball.com</url>\n      <email>lball@redhat.com</email>\n    </developer>\n  </developers>\n\n  <scm>\n    <connection>scm:git:git@github.com:nodyn/jvm-npm.git</connection>\n    <developerConnection>scm:git:git@github.com:nodyn/jvm-npm.git</developerConnection>\n    <url>git@github.com:nodyn/jvm-npm.git</url>\n    <tag>HEAD</tag>\n  </scm>\n\n  <licenses>\n    <license>\n      <name>The Apache Software License, Version 2.0</name>\n      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>\n      <distribution>repo</distribution>\n    </license>\n  </licenses>\n\n  <dependencies>\n    <dependency>\n      <groupId>org.dynjs</groupId>\n      <artifactId>dynjs</artifactId>\n      <version>${version.dynjs}</version>\n      <scope>test</scope>\n    </dependency>\n    <dependency>\n      <groupId>com.github.akiellor.jasmine</groupId>\n      <artifactId>jasmine-cli</artifactId>\n      <version>${version.jasmine-cli}</version>\n      <scope>test</scope>\n      <exclusions>\n        <exclusion>\n          <groupId>org.dynjs</groupId>\n          <artifactId>dynjs</artifactId>\n        </exclusion>\n        <exclusion>\n          <groupId>org.projectodd.rephract</groupId>\n          <artifactId>rephract</artifactId>\n        </exclusion>\n      </exclusions>\n    </dependency>\n  </dependencies>\n\n  <build>\n    <extensions>\n      <extension>\n        <groupId>org.apache.maven.wagon</groupId>\n        <artifactId>wagon-webdav-jackrabbit</artifactId>\n        <version>1.0-beta-7</version>\n      </extension>\n    </extensions>\n    <resources>\n      <resource>\n        <directory>src/main/javascript</directory>\n      </resource>\n    </resources>\n    <plugins>\n      <plugin>\n        <groupId>org.codehaus.mojo</groupId>\n        <artifactId>build-helper-maven-plugin</artifactId>\n        <executions>\n          <execution>\n            <goals>\n              <goal>attach-artifact</goal>\n            </goals>\n            <phase>package</phase>\n            <configuration>\n              <artifacts>\n                <artifact>\n                  <file>target/classes/jvm-npm.js</file>\n                  <type>js</type>\n                </artifact>\n              </artifacts>\n            </configuration>\n          </execution>\n        </executions>\n      </plugin>\n      <plugin>\n        <groupId>org.apache.maven.plugins</groupId>\n        <artifactId>maven-compiler-plugin</artifactId>\n        <version>2.3.2</version>\n        <configuration>\n          <source>1.7</source>\n          <target>1.7</target>\n          <encoding>${project.build.sourceEncoding}</encoding>\n        </configuration>\n      </plugin>\n      <plugin>\n        <groupId>org.apache.maven.plugins</groupId>\n        <artifactId>maven-enforcer-plugin</artifactId>\n        <version>1.1</version>\n        <executions>\n          <execution>\n            <id>enforce-maven</id>\n            <goals>\n              <goal>enforce</goal>\n            </goals>\n            <configuration>\n              <rules>\n                <requireMavenVersion>\n                  <version>3.0.0</version>\n                </requireMavenVersion>\n              </rules>\n            </configuration>\n          </execution>\n        </executions>\n      </plugin>\n      <plugin>\n        <groupId>org.codehaus.mojo</groupId>\n        <artifactId>exec-maven-plugin</artifactId>\n        <version>1.2.1</version>\n        <executions>\n          <execution>\n            <id>Jasmine Integration Tests</id>\n            <phase>integration-test</phase>\n            <goals>\n              <goal>java</goal>\n            </goals>\n            <configuration>\n              <mainClass>org.jasmine.cli.Main</mainClass>\n              <classpathScope>test</classpathScope>\n              <systemPropertyVariables>\n                <dynjs.compile.mode>OFF</dynjs.compile.mode>\n              </systemPropertyVariables>\n              <arguments>\n                <argument>--pattern</argument>\n                <argument>./src/test/javascript/**/*Spec.js</argument>\n                <argument>--format</argument>\n                <argument>DOC</argument>\n                <argument>--compile-mode</argument>\n                <argument>OFF</argument>\n              </arguments>\n            </configuration>\n          </execution>\n        </executions>\n      </plugin>\n    </plugins>\n  </build>\n</project>\n"
  },
  {
    "path": "src/main/javascript/jvm-npm.js",
    "content": "/**\n *  Copyright 2014-2016 Red Hat, Inc.\n *\n *  Licensed under the Apache License, Version 2.0 (the \"License\")\n *  you may not use this file except in compliance with the License.\n *  You may obtain a copy of the License at\n *\n *  http://www.apache.org/licenses/LICENSE-2.0\n *\n *  Unless required by applicable law or agreed to in writing, software\n *  distributed under the License is distributed on an \"AS IS\" BASIS,\n *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *  See the License for the specific language governing permissions and\n *  limitations under the License.\n */\n// Since we intend to use the Function constructor.\n/* jshint evil: true */\n\nmodule = (typeof module === 'undefined') ? {} : module;\n\n(function () {\n  var System = java.lang.System;\n  var Scanner = java.util.Scanner;\n  var File = java.io.File;\n\n  NativeRequire = (typeof NativeRequire === 'undefined') ? {} : NativeRequire;\n  if (typeof require === 'function' && !NativeRequire.require) {\n    NativeRequire.require = require;\n  }\n\n  function Module (id, parent, core) {\n    this.id = id;\n    this.core = core;\n    this.parent = parent;\n    this.children = [];\n    this.filename = id;\n    this.loaded = false;\n\n    Object.defineProperty(this, 'exports', {\n      get: function () {\n        return this._exports;\n      }.bind(this),\n      set: function (val) {\n        Require.cache[this.filename] = val;\n        this._exports = val;\n      }.bind(this)\n    });\n    this.exports = {};\n\n    if (parent && parent.children) parent.children.push(this);\n\n    this.require = function (id) {\n      return Require(id, this);\n    }.bind(this);\n  }\n\n  Module._load = function _load (file, parent, core, main) {\n    var module = new Module(file, parent, core);\n    var body = readFile(module.filename, module.core);\n    var dir = new File(module.filename).getParent();\n    var func = new Function('exports', 'module', 'require', '__filename', '__dirname', body);\n    func.apply(module,\n      [module.exports, module, module.require, module.filename, dir]);\n    module.loaded = true;\n    module.main = main;\n    return module.exports;\n  };\n\n  Module.runMain = function runMain (main) {\n    var file = Require.resolve(main);\n    Module._load(file, undefined, false, true);\n  };\n\n  function Require (id, parent) {\n    var core;\n    var native_;\n    var file = Require.resolve(id, parent);\n\n    if (!file) {\n      if (typeof NativeRequire.require === 'function') {\n        if (Require.debug) {\n          System.out.println(['Cannot resolve', id, 'defaulting to native'].join(' '));\n        }\n        native_ = NativeRequire.require(id);\n        if (native_) return native_;\n      }\n      System.err.println('Cannot find module ' + id);\n      throw new ModuleError('Cannot find module ' + id, 'MODULE_NOT_FOUND');\n    }\n\n    if (file.core) {\n      file = file.path;\n      core = true;\n    }\n    try {\n      if (Require.cache[file]) {\n        return Require.cache[file];\n      } else if (file.endsWith('.js')) {\n        return Module._load(file, parent, core);\n      } else if (file.endsWith('.json')) {\n        return loadJSON(file);\n      }\n    } catch (ex) {\n      if (ex instanceof java.lang.Exception) {\n        throw new ModuleError('Cannot load module ' + id, 'LOAD_ERROR', ex);\n      } else {\n        System.out.println('Cannot load module ' + id + ' LOAD_ERROR');\n        throw ex;\n      }\n    }\n  }\n\n  Require.resolve = function (id, parent) {\n    var roots = findRoots(parent);\n    for (var i = 0; i < roots.length; ++i) {\n      var root = roots[i];\n      var result = resolveCoreModule(id, root) ||\n      resolveAsFile(id, root, '.js') ||\n      resolveAsFile(id, root, '.json') ||\n      resolveAsDirectory(id, root) ||\n      resolveAsNodeModule(id, root);\n      if (result) {\n        return result;\n      }\n    }\n    return false;\n  };\n\n  Require.root = System.getProperty('user.dir');\n  Require.NODE_PATH = undefined;\n\n  function findRoots (parent) {\n    var r = [];\n    r.push(findRoot(parent));\n    return r.concat(Require.paths());\n  }\n\n  function parsePaths (paths) {\n    if (!paths) {\n      return [];\n    }\n    if (paths === '') {\n      return [];\n    }\n    var osName = java.lang.System.getProperty('os.name').toLowerCase();\n    var separator;\n\n    if (osName.indexOf('win') >= 0) {\n      separator = ';';\n    } else {\n      separator = ':';\n    }\n\n    return paths.split(separator);\n  }\n\n  Require.paths = function () {\n    var r = [];\n    r.push(java.lang.System.getProperty('user.home') + '/.node_modules');\n    r.push(java.lang.System.getProperty('user.home') + '/.node_libraries');\n\n    if (Require.NODE_PATH) {\n      r = r.concat(parsePaths(Require.NODE_PATH));\n    } else {\n      var NODE_PATH = java.lang.System.getenv().NODE_PATH;\n      if (NODE_PATH) {\n        r = r.concat(parsePaths(NODE_PATH));\n      }\n    }\n    // r.push( $PREFIX + \"/node/library\" )\n    return r;\n  };\n\n  function findRoot (parent) {\n    if (!parent || !parent.id) { return Require.root; }\n    var pathParts = parent.id.split(/[\\/|\\\\,]+/g);\n    pathParts.pop();\n    return pathParts.join('/');\n  }\n\n  Require.debug = true;\n  Require.cache = {};\n  Require.extensions = {};\n  require = Require;\n\n  module.exports = Module;\n\n  function loadJSON (file) {\n    var json = JSON.parse(readFile(file));\n    Require.cache[file] = json;\n    return json;\n  }\n\n  function resolveAsNodeModule (id, root) {\n    var base = [root, 'node_modules'].join('/');\n    return resolveAsFile(id, base) ||\n      resolveAsDirectory(id, base) ||\n      (root ? resolveAsNodeModule(id, new File(root).getParent()) : false);\n  }\n\n  function resolveAsDirectory (id, root) {\n    var base = [root, id].join('/');\n    var file = new File([base, 'package.json'].join('/'));\n    if (file.exists()) {\n      try {\n        var body = readFile(file.getCanonicalPath());\n        var package_ = JSON.parse(body);\n        if (package_.main) {\n          return (resolveAsFile(package_.main, base) ||\n            resolveAsDirectory(package_.main, base));\n        }\n        // if no package.main exists, look for index.js\n        return resolveAsFile('index.js', base);\n      } catch (ex) {\n        throw new ModuleError('Cannot load JSON file', 'PARSE_ERROR', ex);\n      }\n    }\n    return resolveAsFile('index.js', base);\n  }\n\n  function resolveAsFile (id, root, ext) {\n    var file;\n    if (id.length > 0 && id[0] === '/') {\n      file = new File(normalizeName(id, ext));\n      if (!file.exists()) {\n        return resolveAsDirectory(id);\n      }\n    } else {\n      file = new File([root, normalizeName(id, ext)].join('/'));\n    }\n    if (file.exists()) {\n      return file.getCanonicalPath();\n    }\n  }\n\n  function resolveCoreModule (id, root) {\n    var name = normalizeName(id);\n    var classloader = java.lang.Thread.currentThread().getContextClassLoader();\n    if (classloader.getResource(name)) {\n      return { path: name, core: true };\n    }\n  }\n\n  function normalizeName (fileName, ext) {\n    var extension = ext || '.js';\n    if (fileName.endsWith(extension)) {\n      return fileName;\n    }\n    return fileName + extension;\n  }\n\n  function readFile (filename, core) {\n    var input;\n    try {\n      if (core) {\n        var classloader = java.lang.Thread.currentThread().getContextClassLoader();\n        input = classloader.getResourceAsStream(filename);\n      } else {\n        input = new File(filename);\n      }\n      // TODO: I think this is not very efficient\n      return new Scanner(input).useDelimiter('\\\\A').next();\n    } catch (e) {\n      throw new ModuleError('Cannot read file [' + input + ']: ', 'IO_ERROR', e);\n    }\n  }\n\n  function ModuleError (message, code, cause) {\n    this.code = code || 'UNDEFINED';\n    this.message = message || 'Error loading module';\n    this.cause = cause;\n  }\n\n  // Helper function until ECMAScript 6 is complete\n  if (typeof String.prototype.endsWith !== 'function') {\n    String.prototype.endsWith = function (suffix) {\n      if (!suffix) return false;\n      return this.indexOf(suffix, this.length - suffix.length) !== -1;\n    };\n  }\n\n  ModuleError.prototype = new Error();\n  ModuleError.prototype.constructor = ModuleError;\n}());\n"
  },
  {
    "path": "src/test/javascript/specs/lib/a_package/index.js",
    "content": "var fileModule = require('file_module');\nvar pkgModule = require('pkg_module');\n\nmodule.exports.parent_test = require('./lib/parent');\nmodule.exports.file_module = fileModule;\nmodule.exports.pkg_module = pkgModule;\n"
  },
  {
    "path": "src/test/javascript/specs/lib/a_package/lib/parent.js",
    "content": "var parent = module.parent;\n\nvar mod = require('dep_module');\n\nmodule.exports.parentChanged = parent !== module.parent;\n"
  },
  {
    "path": "src/test/javascript/specs/lib/cheese/lib/index.js",
    "content": "module.exports.flavor = 'nacho';\n"
  },
  {
    "path": "src/test/javascript/specs/lib/cheese/package.json",
    "content": "{\n  \"main\": \"./lib\"\n}\n"
  },
  {
    "path": "src/test/javascript/specs/lib/cyclic/a.js",
    "content": "var b = require('./b');\nmodule.exports = {\n  fromA: 'Hello from A',\n};\n\n"
  },
  {
    "path": "src/test/javascript/specs/lib/cyclic/b.js",
    "content": "var a = require('./a');\nmodule.exports = {\n  fromB: 'Hello from B',\n};\n\n"
  },
  {
    "path": "src/test/javascript/specs/lib/cyclic/index.js",
    "content": "module.exports = {\n  a: require('./a'),\n  b: require('./b')\n};\n\n"
  },
  {
    "path": "src/test/javascript/specs/lib/cyclic2/stream.js",
    "content": "\n\nmodule.exports = Stream;\n\nfunction Stream() {\n\n}\n\nvar Readable = require( 'stream_readable.js' );\n\nStream.Readable = Readable;"
  },
  {
    "path": "src/test/javascript/specs/lib/cyclic2/stream_readable.js",
    "content": "\nmodule.exports = Readable;\n\nvar Stream = require('stream.js');\n\nReadable.Stream = Stream\n\nfunction Readable() {\n}\n"
  },
  {
    "path": "src/test/javascript/specs/lib/isolation/module-a.js",
    "content": "\nvar doNotLeak = \"swiss\";\ndoLeak = \"cheddar\";\n\nvar b = require( 'module-b.js' );"
  },
  {
    "path": "src/test/javascript/specs/lib/isolation/module-b.js",
    "content": "\nvar mine = doLeak;\n\nexpect(mine).toBe(\"cheddar\");\n\ntry {\n  mine = doNotLeak\n  // should have thrown a ref-error\n  expect(false).toBe(true);\n} catch (err) {\n  expect(err instanceof ReferenceError).toBe(true);\n}"
  },
  {
    "path": "src/test/javascript/specs/lib/isolation/module-c.js",
    "content": "\nfunction doNotLeak() {\n}\n\nvar b = require( 'module-d.js' );"
  },
  {
    "path": "src/test/javascript/specs/lib/isolation/module-d.js",
    "content": "\ntry {\n  var mine = doNotLeak;\n  // should have thrown a reference error\n  expect(false).toBe(true);\n} catch (err) {\n  expect(err instanceof ReferenceError).toBe(true);\n}\n\n"
  },
  {
    "path": "src/test/javascript/specs/lib/mod_exports.js",
    "content": "exports.data = \"Hello!\";\n"
  },
  {
    "path": "src/test/javascript/specs/lib/my_package/index.js",
    "content": "module.exports = {\n  data: \"Hello!\"\n};\n"
  },
  {
    "path": "src/test/javascript/specs/lib/native_test_module.js",
    "content": "module.exports = \"Foo!\";\n"
  },
  {
    "path": "src/test/javascript/specs/lib/other_package/lib/foo.js",
    "content": "module.exports.flavor = \"cool ranch\";\nmodule.exports.subdir = require('./subdir/bar').dirname;\n"
  },
  {
    "path": "src/test/javascript/specs/lib/other_package/lib/subdir/bar.js",
    "content": "module.exports.dirname = __dirname;\n"
  },
  {
    "path": "src/test/javascript/specs/lib/other_package/package.json",
    "content": "{\n  \"main\": \"lib/foo\"\n}\n"
  },
  {
    "path": "src/test/javascript/specs/lib/outer.js",
    "content": "var inner = require('./sub/inner');\n\nmodule.exports = {\n  quadruple: function(val) { return 2*inner.double(val); },\n  children: module.children,\n  filename: module.filename,\n  innerParent: inner.parent,\n};\n"
  },
  {
    "path": "src/test/javascript/specs/lib/simple_module.js",
    "content": "module.exports = {\n  dirname: __dirname,\n  filename: __filename,\n};\n\nfunction privateFunction() {}\n"
  },
  {
    "path": "src/test/javascript/specs/lib/some_data.json",
    "content": "{\n  \"description\": \"This is a JSON file\",\n  \"data\": [1,2,3]\n}\n\n"
  },
  {
    "path": "src/test/javascript/specs/lib/sub/inner.js",
    "content": "module.exports = {\n  double: function(x) { return 2*x; },\n  parent: module.parent\n};\n\n"
  },
  {
    "path": "src/test/javascript/specs/lib/throws.js",
    "content": "// This module should throw a ReferenceError\n//\n\nvar foo = bar;\n"
  },
  {
    "path": "src/test/javascript/specs/requireSpec.js",
    "content": "/**\n *  Copyright 2014 Lance Ball\n *\n *  Licensed under the Apache License, Version 2.0 (the \"License\");\n *  you may not use this file except in compliance with the License.\n *  You may obtain a copy of the License at\n *\n *  http://www.apache.org/licenses/LICENSE-2.0\n *\n *  Unless required by applicable law or agreed to in writing, software\n *  distributed under the License is distributed on an \"AS IS\" BASIS,\n *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n *  See the License for the specific language governing permissions and\n *  limitations under the License.\n */\n// Make the native require function look in our local directory\n// for modules loaded with NativeRequire.require()\n\nvar cwd = [java.lang.System.getProperty('user.dir'),\n           'src/test/javascript/specs'].join('/');\n\nvar home = java.lang.System.getProperty('user.home');\n\nrequire.pushLoadPath(cwd);\n\n// Load the NPM module loader into the global scope\nload('src/main/javascript/jvm-npm.js');\n\n// Tell require where it's root is\nrequire.root = cwd;\n\n\nbeforeEach(function() {\n  require.cache = [];\n});\n\ndescribe(\"NativeRequire\", function() {\n\n  it(\"should be a global object\", function(){\n    expect(typeof NativeRequire).toBe('object');\n  });\n\n  it(\"should expose DynJS' builtin require() function\", function(){\n    expect(typeof NativeRequire.require).toBe('function');\n    var f = NativeRequire.require('./lib/native_test_module');\n    expect(f).toBe(\"Foo!\");\n    expect(NativeRequire.require instanceof org.dynjs.runtime.builtins.Require)\n      .toBe(true);\n  });\n\n  it(\"should fall back to builtin require() if not found\", function() {\n    var called = false;\n    NativeRequire.require = function() {\n      called = true;\n      return \"Got native module\";\n    };\n    var native = require('not_found');\n    expect(native).toBe(\"Got native module\");\n    expect(called).toBe(true);\n  });\n\n});\n\ndescribe(\"NPM global require()\", function() {\n\n  it(\"should be a function\", function() {\n    expect(typeof require).toBe('function');\n  });\n\n  it(\"should have a resolve() property that is a function\", function() {\n    expect(typeof require.resolve).toBe('function');\n  });\n\n  it(\"should have a cache property that is an Object\", function() {\n    expect(typeof require.cache).toBe('object');\n  });\n\n  it(\"should have an extensions property that is an Object\", function() {\n    expect(typeof require.extensions).toBe('object');\n  });\n\n  it(\"should find and load files with a .js extension\", function() {\n    // Ensure that the npm require() is not using NativeRequire\n    var that=this;\n    NativeRequire.require = function() {\n      that.fail(\"NPM require() should not use DynJS native require\");\n    };\n    expect(require('./lib/native_test_module')).toBe(\"Foo!\");\n  });\n\n  it(\"should throw an Error if a file can't be found\", function() {\n    expect(function() {require('./not_found.js');}).toThrow(new Error('Cannot find module ./not_found.js'));\n    try {\n      require('./not_found.js');\n    } catch(e) {\n      expect(e.code).toBe('MODULE_NOT_FOUND');\n    }\n  });\n\n  it(\"should not wrap errors encountered when loading a module\", function() {\n    try {\n      require('./lib/throws');\n    } catch(ex) {\n      print(ex);\n      expect(ex instanceof ReferenceError).toBeTruthy();\n    }\n  });\n\n  it(\"should support nested requires\", function() {\n    var outer = require('./lib/outer');\n    expect(outer.quadruple(2)).toBe(8);\n  });\n\n  it(\"should support an ID with an extension\", function() {\n    var outer = require('./lib/outer.js');\n    expect(outer.quadruple(2)).toBe(8);\n  });\n\n  it(\"should return the a .json file as a JSON object\", function() {\n    var json = require('./lib/some_data.json');\n    expect(json.description).toBe(\"This is a JSON file\");\n    expect(json.data).toEqual([1,2,3]);\n  });\n\n  it(\"should cache modules in require.cache\", function() {\n    var outer = require('./lib/outer.js');\n    expect(outer).toBe(require.cache[outer.filename]);\n    var outer2 = require('./lib/outer.js');\n    expect(outer2).toBe(outer);\n  });\n\n  it(\"should handle cyclic dependencies\", function() {\n    var main = require('./lib/cyclic');\n    expect(main.a.fromA).toBe('Hello from A');\n    expect(main.b.fromB).toBe('Hello from B');\n  });\n\n  describe(\"folders as modules\", function() {\n    it(\"should find package.json in a module folder\", function() {\n      var package = require('./lib/other_package');\n      expect(package.flavor).toBe('cool ranch');\n      expect(package.subdir).toBe([cwd, 'lib/other_package/lib/subdir'].join('/'));\n    });\n\n    it('should load package.json main property even if it is a directory', function() {\n      var cheese = require('./lib/cheese');\n      expect(cheese.flavor).toBe('nacho');\n    });\n\n    it(\"should find index.js in a directory, if no package.json exists\", function() {\n      var package = require('./lib/my_package');\n      expect(package.data).toBe('Hello!');\n    });\n  });\n\n  describe(\"node_modules folders\", function() {\n\n    it(\"should load file modules from the node_modules folder in cwd\", function() {\n      var top = require('./lib/a_package');\n      expect(top.file_module).toBe('Hello from a file module');\n    });\n\n    it(\"should load package modules from the node_modules folder\", function() {\n      var top = require('./lib/a_package');\n      expect(top.pkg_module.pkg).toBe('Hello from a package module');\n    });\n\n    it(\"should find node_module packages in the parent path\", function() {\n      var top = require('./lib/a_package');\n      expect(top.pkg_module.file).toBe('Hello from a file module');\n    });\n\n    it(\"should find node_module packages from a sibling path\", function() {\n      var top = require('./lib/a_package');\n      expect(top.parent_test.parentChanged).toBe(false);\n    });\n\n    it('should find node_module packages all the way up above cwd', function() {\n      var m = require('root_module');\n      expect(m.message).toBe('You are at the root');\n    });\n\n  });\n\n});\n\ndescribe(\"NPM Module execution context\", function() {\n\n  it(\"should have a __dirname property\", function() {\n    var top = require('./lib/simple_module');\n    expect(top.dirname).toBe([cwd, 'lib'].join('/'));\n  });\n\n  it(\"should have a __filename property\", function() {\n    var top = require('./lib/simple_module');\n    expect(top.filename).toBe([cwd, 'lib/simple_module.js'].join('/'));\n  });\n\n  it(\"should not expose private module functions globally\", function() {\n    var top = require('./lib/simple_module');\n    expect(top.privateFunction).toBe(undefined);\n  });\n\n  it(\"should have a parent property\", function() {\n    var outer = require('./lib/outer');\n    expect(outer.innerParent.id).toBe([cwd, 'lib/outer.js'].join('/'));\n  });\n\n  it(\"should have a filename property\", function() {\n    var outer = require('./lib/outer');\n    expect(outer.filename).toBe([cwd, 'lib/outer.js'].join('/'));\n  });\n\n  it(\"should have a children property\", function() {\n    var outer = require('./lib/outer');\n    expect(outer.children.length).toBe(1);\n    expect(outer.children[0].id).toBe([cwd, 'lib/sub/inner.js'].join('/'));\n  });\n\n  it(\"should support setting the 'free' exports variable\", function() {\n    var modExports = require('./lib/mod_exports');\n    expect(modExports.data).toBe(\"Hello!\");\n  });\n\n});\n\ndescribe(\"module isolation\", function() {\n  it(\"should expose global variables and not expose 'var' declared variables\", function() {\n    var top = require( './lib/isolation/module-a.js');\n    expect(doLeak).toBe(\"cheddar\");\n    try {\n      var shouldFail = doNotLeak;\n      // should have thrown\n      expect(true).toBe(false);\n    } catch (err) {\n      expect( err instanceof ReferenceError ).toBe(true);\n    }\n  });\n\n  it(\"should not leak function declarations\", function() {\n    var top = require('./lib/isolation/module-c.js');\n    try {\n      var shouldFail = doNotLeak;\n      // should have thrown\n      expect(true).toBe(false);\n    } catch (err) {\n      expect( err instanceof ReferenceError ).toBe(true);\n    }\n  });\n});\n\ndescribe(\"cyclic with replacement of module.exports\", function() {\n  it( \"should have the same sense of an object in all places\", function() {\n    var Stream = require( \"./lib/cyclic2/stream.js\" );\n\n    expect( typeof Stream ).toBe( \"function\"  );\n    expect( typeof Stream.Readable ).toBe( \"function\" );\n    expect( typeof Stream.Readable.Stream ).toBe( \"function\" );\n\n  });\n});\n\ndescribe(\"Core modules\", function() {\n  it(\"should be found on the classpath\", function() {\n    var core = require('core');\n    expect(core).not.toBeFalsy();\n  });\n\n  it( \"should have the same sense of an object in all places\", function() {\n    var Core = require( \"core.js\" );\n\n    expect( typeof Core ).toBe( \"function\"  );\n    expect( typeof Core.Child ).toBe( \"function\" );\n    expect( typeof Core.Child.Core ).toBe( \"function\" );\n\n  });\n});\n\ndescribe(\"Path management\", function() {\n  it( \"should respect NODE_PATH variable\", function() {\n    require.NODE_PATH = 'foo:bar';\n    var results = require.paths();\n    expect( results[0] ).toBe( home + \"/.node_modules\" );\n    expect( results[1] ).toBe( home + \"/.node_libraries\" );\n    expect( results[2] ).toBe( 'foo' );\n    expect( results[3] ).toBe( 'bar' );\n    //java.lang.System.err.println( results );\n\n  });\n});\n\ndescribe(\"The Module module\", function() {\n  it('should exist', function() {\n    var Module = require('jvm-npm');\n    expect(Module).toBeTruthy();\n  });\n\n  it('should have a runMain function', function() {\n    var Module = require('jvm-npm');\n    expect(typeof Module.runMain).toBe('function');\n  });\n});\n"
  },
  {
    "path": "src/test/resources/_core.js",
    "content": "\nmodule.exports = Child;\n\nvar Core = require('core');\n\nChild.Core = Core;\n\nfunction Child() {\n}\n"
  },
  {
    "path": "src/test/resources/core.js",
    "content": "\n\nmodule.exports = Core;\n\nfunction Core() {\n\n}\n\nvar Child = require( '_core' );\n\nCore.Child = Child;\n"
  }
]