com.coveo:nashorn-commonjs-modules

CommonJS modules implementation for Nashorn

License

License

MIT
Categories

Categories

JavaScript Languages
GroupId

GroupId

com.coveo
ArtifactId

ArtifactId

nashorn-commonjs-modules
Last Version

Last Version

1.0.9
Release Date

Release Date

Type

Type

jar
Description

Description

com.coveo:nashorn-commonjs-modules
CommonJS modules implementation for Nashorn
Project URL

Project URL

http://github.com/coveo/nashorn-commonjs-modules
Source Code Management

Source Code Management

http://github.com/coveo/nashorn-commonjs-modules

Download nashorn-commonjs-modules

How to add to project

<!-- https://jarcasting.com/artifacts/com.coveo/nashorn-commonjs-modules/ -->
<dependency>
    <groupId>com.coveo</groupId>
    <artifactId>nashorn-commonjs-modules</artifactId>
    <version>1.0.9</version>
</dependency>
// https://jarcasting.com/artifacts/com.coveo/nashorn-commonjs-modules/
implementation 'com.coveo:nashorn-commonjs-modules:1.0.9'
// https://jarcasting.com/artifacts/com.coveo/nashorn-commonjs-modules/
implementation ("com.coveo:nashorn-commonjs-modules:1.0.9")
'com.coveo:nashorn-commonjs-modules:jar:1.0.9'
<dependency org="com.coveo" name="nashorn-commonjs-modules" rev="1.0.9">
  <artifact name="nashorn-commonjs-modules" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.coveo', module='nashorn-commonjs-modules', version='1.0.9')
)
libraryDependencies += "com.coveo" % "nashorn-commonjs-modules" % "1.0.9"
[com.coveo/nashorn-commonjs-modules "1.0.9"]

Dependencies

compile (1)

Group / Artifact Type Version
commons-io : commons-io jar 2.4

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.mockito : mockito-core jar 1.10.19

Project Modules

There are no modules declared in this project.

Build Status license Maven Central

CommonJS Modules Support for Nashorn

This library adds support for CommonJS modules (aka require) inside a Nashorn script engine. It is based on the specification for NodeJS modules and it supports loading modules from the node_modules folder just as Node does. Of course, it doesn't provide an implementation for Node's APIs, so any module that depends on those won't work.

It is somehow similar to jvm-npm which I used before, but it is 100% implemented in Java and supports loading files through other means than the filesystem; you only need to implement a simple interface and you should be good to go. Also, having the implementation in Java allows using it with a JS interpreter on which access to Java packages has been disabled for sandboxing purposes.

Out-of-the-box, the library supports loading modules from the filesystem and from Java resources.

Getting the library using Maven

Add this dependency to your pom.xml to reference the library:

<dependency>
  <groupId>com.coveo</groupId>
  <artifactId>nashorn-commonjs-modules</artifactId>
  <version>1.0.9</version>
</dependency>

Usage

Enabling require in an existing Nashorn interpreter can be done very easily:

NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
Require.enable(engine, myRootFolder);

This will expose a new global require function at the engine scope. Any code that is then run using this engine can make use of require.

The second argument specifies the root Folder from which modules are made available. Folder is an interface exposing a few calls that need to be implemented by backing providers to enable loading files and accessing subfolders.

As of now, the library comes with built-in support for loading modules either through the filesystem or through Java resources.

Loading modules from the filesystem

Use the FilesystemFolder.create method to create an implementation of Folder rooted at a particular location in the filesystem:

FilesystemFolder rootFolder = FilesystemFolder.create(new File("/path/to/my/folder"), "UTF-8");
Require.enable(engine, rootFolder);

You need to specify the encoding of the files. Most of the time UTF-8 will be a reasonable choice.

The resulting folder is rooted at the path you specified, and JavaScript code won't be able to "escape" that root by using ../../... In other words, it behaves as is the root folder was the root of the filesystem.

Loading modules from Java resources

Use the ResourceFolder.create method to create an implementation of Folder backed by Java resources:

ResourceFolder rootFolder = ResourceFolder.create(getClass().getClassLoader(), "com/coveo/nashorn_modules/test1", "UTF-8");
Require.enable(engine, rootFolder);

As for FilesystemFolder, you need to specify the encoding for the files that are read.

com.coveo

Coveo

Coveo's Open Source Lair

Versions

Version
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0