-
Notifications
You must be signed in to change notification settings - Fork 0
Resolve JS module on first use if empty #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| package com.indexdata.reservoir.module.impl; | ||
|
|
||
| import com.indexdata.reservoir.module.Module; | ||
| import com.indexdata.reservoir.server.Storage; | ||
| import com.indexdata.reservoir.server.entity.CodeModuleEntity; | ||
| import io.vertx.core.Future; | ||
| import io.vertx.core.Vertx; | ||
| import io.vertx.core.http.HttpMethod; | ||
| import io.vertx.core.json.JsonObject; | ||
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
|
|
@@ -22,7 +24,7 @@ public class ModuleJavaScript implements Module { | |
| private Vertx vertx; | ||
|
|
||
| @Override | ||
| public Future<Void> initialize(Vertx vertx, CodeModuleEntity entity) { | ||
| public Future<CodeModuleEntity> initialize(Vertx vertx, CodeModuleEntity entity) { | ||
| id = entity.getId(); | ||
| if (id == null || id.isEmpty()) { | ||
| return Future.failedFuture( | ||
|
|
@@ -31,29 +33,41 @@ public Future<Void> initialize(Vertx vertx, CodeModuleEntity entity) { | |
| this.vertx = vertx; | ||
| String url = entity.getUrl(); | ||
| String script = entity.getScript(); | ||
| if (script == null || script.isEmpty()) { | ||
| var hasUrl = url != null && !url.isEmpty(); | ||
| var hasScript = script != null && !script.isEmpty(); | ||
| if (!hasUrl && !hasScript) { | ||
| return Future.failedFuture( | ||
| new IllegalArgumentException("Module config must include 'url' or 'script'")); | ||
| } | ||
| if (url != null && !url.isEmpty()) { | ||
| // url always points to an ES module | ||
| defaultFunctionName = entity.getFunction(); | ||
| final boolean isModule = url.endsWith("mjs"); | ||
| if (!isModule) { | ||
| return Future.failedFuture(new IllegalArgumentException( | ||
| "url must end with .mjs to designate ES module")); | ||
| if (hasUrl) { | ||
| //module was never resolved (migration) | ||
| if (!hasScript) { | ||
| return new CodeModuleEntity.CodeModuleBuilder(entity.asJson()) | ||
| .resolve(vertx) | ||
| .compose(this::initAsEsModule); | ||
| } | ||
| Context.Builder cb = Context.newBuilder("js") | ||
| .allowExperimentalOptions(true) | ||
| .option("js.esm-eval-returns-exports", "true"); | ||
| context = cb.build(); | ||
| String moduleName = url.substring(url.lastIndexOf("/") + 1); | ||
| module = context.eval(Source.newBuilder("js", script, moduleName).buildLiteral()); | ||
| return this.initAsEsModule(entity); | ||
| } else { | ||
| context = Context.create("js"); | ||
| function = context.eval("js", script); | ||
| return Future.succeededFuture(entity); | ||
| } | ||
| return Future.succeededFuture(); | ||
| } | ||
|
|
||
| private Future<CodeModuleEntity> initAsEsModule(CodeModuleEntity entity) { | ||
| defaultFunctionName = entity.getFunction(); | ||
| final boolean isModule = entity.getUrl().endsWith("mjs"); | ||
| if (!isModule) { | ||
| return Future.failedFuture(new IllegalArgumentException( | ||
| "url must end with .mjs to designate ES module")); | ||
| } | ||
| Context.Builder cb = Context.newBuilder("js") | ||
| .allowExperimentalOptions(true) | ||
| .option("js.esm-eval-returns-exports", "true"); | ||
| context = cb.build(); | ||
| String moduleName = entity.getUrl().substring(entity.getUrl().lastIndexOf("/") + 1); | ||
| module = context.eval(Source.newBuilder("js", entity.getScript(), moduleName).buildLiteral()); | ||
|
Comment on lines
+57
to
+69
|
||
| return Future.succeededFuture(entity); | ||
| } | ||
|
|
||
| private Value getFunction(String functionName) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inline comment "module was never resolved (migration)" explains the purpose but lacks detail about when this scenario occurs and what the expected behavior is. Consider expanding this comment to explain that this handles legacy modules that were stored with only URLs before automatic resolution was implemented, and that the code will fetch the script and persist it to the database for future use.