Search in sources :

Example 1 with UrlModuleSourceProvider

use of org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider in project hackpad by dropbox.

the class PipeThread method installRequire.

public Require installRequire(Context cx, List<String> modulePath, boolean sandboxed) {
    RequireBuilder rb = new RequireBuilder();
    rb.setSandboxed(sandboxed);
    List<URI> uris = new ArrayList<URI>();
    if (modulePath != null) {
        for (String path : modulePath) {
            try {
                URI uri = new URI(path);
                if (!uri.isAbsolute()) {
                    // call resolve("") to canonify the path
                    uri = new File(path).toURI().resolve("");
                }
                if (!uri.toString().endsWith("/")) {
                    // make sure URI always terminates with slash to
                    // avoid loading from unintended locations
                    uri = new URI(uri + "/");
                }
                uris.add(uri);
            } catch (URISyntaxException usx) {
                throw new RuntimeException(usx);
            }
        }
    }
    rb.setModuleScriptProvider(new SoftCachingModuleScriptProvider(new UrlModuleSourceProvider(uris, null)));
    Require require = rb.createRequire(cx, this);
    require.install(this);
    return require;
}
Also used : RequireBuilder(org.mozilla.javascript.commonjs.module.RequireBuilder) Require(org.mozilla.javascript.commonjs.module.Require) ArrayList(java.util.ArrayList) UrlModuleSourceProvider(org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider) SoftCachingModuleScriptProvider(org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider)

Aggregations

ArrayList (java.util.ArrayList)1 Require (org.mozilla.javascript.commonjs.module.Require)1 RequireBuilder (org.mozilla.javascript.commonjs.module.RequireBuilder)1 SoftCachingModuleScriptProvider (org.mozilla.javascript.commonjs.module.provider.SoftCachingModuleScriptProvider)1 UrlModuleSourceProvider (org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider)1