forked from Botanical/BotanJS
Removed old impl
This commit is contained in:
@@ -20,17 +20,18 @@ FROM eclipse-temurin:21-jre
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
ARG JS_SRC_DIR
|
||||
ARG JAVA_SRC_DIR
|
||||
ARG CLOSURE_NAME
|
||||
|
||||
RUN useradd -r -u 10001 closure
|
||||
|
||||
COPY --from=build /src/target/${CLOSURE_NAME}-0.1.0.jar /app/runtime.jar
|
||||
COPY $JAVA_SRC_DIR/example ./example
|
||||
COPY $JS_SRC_DIR ./src
|
||||
|
||||
USER closure
|
||||
|
||||
ENV CLOSURED_ROOT=/work
|
||||
ENV CLOSURED_ROOT=/app/src
|
||||
ENV CLOSURED_PORT=8080
|
||||
ENV CLOSURED_WORKERS=2
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@ import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public final class Main {
|
||||
private record InlineSource(String name, String source) {}
|
||||
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
private static final Path ROOT = Path.of(System.getenv().getOrDefault("CLOSURED_ROOT", ".")).toAbsolutePath().normalize();
|
||||
@@ -92,11 +94,14 @@ public final class Main {
|
||||
List<SourceFile> externs = new ArrayList<>();
|
||||
externs.addAll(CommandLineRunner.getBuiltinExterns(options.getEnvironment()));
|
||||
externs.addAll(readFiles(req, "externs"));
|
||||
externs.addAll(readInlineSources(req, "externSources"));
|
||||
|
||||
List<SourceFile> inputs = readFiles(req, "js");
|
||||
List<SourceFile> inputs = new ArrayList<>();
|
||||
inputs.addAll(readFiles(req, "js"));
|
||||
inputs.addAll(readInlineSources(req, "jsSources"));
|
||||
|
||||
if (inputs.isEmpty()) {
|
||||
throw new BadRequest("request must include at least one js file");
|
||||
throw new BadRequest("request must include at least one js file or js source");
|
||||
}
|
||||
|
||||
com.google.javascript.jscomp.Compiler compiler =
|
||||
@@ -154,6 +159,52 @@ public final class Main {
|
||||
return out;
|
||||
}
|
||||
|
||||
private static List<SourceFile> readInlineSources(JsonNode req, String field) throws BadRequest {
|
||||
JsonNode arr = req.get(field);
|
||||
List<SourceFile> out = new ArrayList<>();
|
||||
|
||||
if (arr == null || arr.isNull()) {
|
||||
return out;
|
||||
}
|
||||
|
||||
if (!arr.isArray()) {
|
||||
throw new BadRequest(field + " must be an array");
|
||||
}
|
||||
|
||||
for (JsonNode item : arr) {
|
||||
String name;
|
||||
String source;
|
||||
|
||||
if (item.isTextual()) {
|
||||
// Optional convenience form:
|
||||
// "jsSources": ["console.log(1);"]
|
||||
name = field + "-" + out.size() + ".js";
|
||||
source = item.textValue();
|
||||
} else if (item.isObject()) {
|
||||
JsonNode nameNode = item.get("name");
|
||||
JsonNode sourceNode = item.get("source");
|
||||
|
||||
if (sourceNode == null || !sourceNode.isTextual()) {
|
||||
throw new BadRequest(field + " item must include textual source");
|
||||
}
|
||||
|
||||
if (nameNode != null && nameNode.isTextual() && !nameNode.textValue().isBlank()) {
|
||||
name = nameNode.textValue();
|
||||
} else {
|
||||
name = field + "-" + out.size() + ".js";
|
||||
}
|
||||
|
||||
source = sourceNode.textValue();
|
||||
} else {
|
||||
throw new BadRequest(field + " items must be strings or objects");
|
||||
}
|
||||
|
||||
out.add(SourceFile.fromCode(name, source));
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
private static Path safePath(String value) throws BadRequest {
|
||||
Path p = ROOT.resolve(value).normalize();
|
||||
if (!p.startsWith(ROOT)) {
|
||||
|
||||
Reference in New Issue
Block a user