/** * Creates a new {@link Nodejs}. * * @param basedir Base dir where to deploy a library. * @param loader Class loader to use. */ public Nodejs(final File basedir, final ClassLoader loader) { this.basedir = requireNonNull(basedir, "Basedir required."); this.loader = requireNonNull(loader, "ClassLoader required."); this.node = NodeJS.createNodeJS(); V8 v8 = node.getRuntime(); this.scope = new MemoryManager(v8); }
/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }
@Test public void testExecuteNodeScript_Startup() throws IOException { assumeFalse(skipMessage, skipTest()); // conditional skip nodeJS.release(); File testScript = createTemporaryScriptFile("global.passed = true;", "testScript"); nodeJS = NodeJS.createNodeJS(testScript); runMessageLoop(); assertEquals(true, nodeJS.getRuntime().getBoolean("passed")); testScript.delete(); }
@Test public void testExecNodeScript() throws IOException { assumeFalse(skipMessage, skipTest()); // conditional skip nodeJS.release(); File testScript = createTemporaryScriptFile("global.passed = true;", "testScript"); nodeJS = NodeJS.createNodeJS(); nodeJS.exec(testScript); runMessageLoop(); assertEquals(true, nodeJS.getRuntime().getBoolean("passed")); testScript.delete(); }
@Test public void testExecuteNodeScript_viaRequire() throws IOException { assumeFalse(skipMessage, skipTest()); // conditional skip nodeJS.release(); File testScript = createTemporaryScriptFile("global.passed = true;", "testScript"); nodeJS = NodeJS.createNodeJS(); nodeJS.require(testScript).close(); runMessageLoop(); assertEquals(true, nodeJS.getRuntime().getBoolean("passed")); testScript.delete(); }
/** * Creates a new {@link Nodejs}. * * @param basedir Base dir where to deploy a library. * @param loader Class loader to use. */ public Nodejs(final File basedir, final ClassLoader loader) { this.basedir = requireNonNull(basedir, "Basedir required."); this.loader = requireNonNull(loader, "ClassLoader required."); this.node = NodeJS.createNodeJS(); V8 v8 = node.getRuntime(); this.scope = new MemoryManager(v8); }
/** * Unpack and execute a nodejs library. Once unpack this method will execute one of these scripts * in the following order: 1) [library].js; 2) main.js; 3) index.js. * * The first file found will be executed. * * @param library Library to unpack and execute this library. * @throws Throwable If something goes wrong. */ public void exec(final String library, final Throwing.Consumer<V8> callback) throws Throwable { Path basedir = deploy(library); List<String> candidates = Arrays.asList( basedir.getFileName().toString() + ".js", "main.js", "index.js"); Path main = candidates.stream() .map(basedir::resolve) .filter(Files::exists) .findFirst() .orElseThrow(() -> new FileNotFoundException(candidates.toString())); callback.accept(node.getRuntime()); node.exec(main.toFile()); while (node.isRunning()) { node.handleMessage(); } }