Skip to content

Commit

Permalink
compress js runtime bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvergnaud committed Dec 3, 2018
1 parent 1d58987 commit dd0d8ae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
29 changes: 25 additions & 4 deletions Server/src/main/java/prompto/server/ResourceServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,27 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
super.service(request, response);
return;
}

boolean tryGzip = writeBody && acceptsGzip(request);

Resource resource = getResource(request);
Resource resource = getResource(request, tryGzip);

if (resource==null || !resource.exists()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
}
response.setStatus(HttpServletResponse.SC_OK);
writeHeaders(response, resource);
if(writeBody)
writeBody(request, response, resource);
}

protected Resource getResource(HttpServletRequest request) {
private boolean acceptsGzip(HttpServletRequest request) {
String accept = request.getHeader(HttpHeader.ACCEPT_ENCODING.asString());
return accept!=null && accept.contains("gzip");
}

protected Resource getResource(HttpServletRequest request, boolean tryGzip) {
String servletPath;
String pathInfo;
Boolean included = request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) != null;
Expand All @@ -103,7 +110,12 @@ protected Resource getResource(HttpServletRequest request) {
String pathInContext = URIUtil.addPaths(servletPath, pathInfo);
if(pathInContext=="/" && welcomePage!=null)
pathInContext = welcomePage;
Resource resource = getClassPathResource(pathInContext);
Resource resource = null;
if(tryGzip)
resource = getClassPathResource(pathInContext + ".gz");
if(resource!=null)
return resource;
resource = getClassPathResource(pathInContext);
if(resource!=null)
return resource;
else
Expand Down Expand Up @@ -190,10 +202,19 @@ private void writeBodySync(HttpServletRequest request, HttpOutput out, Resource
protected void writeHeaders(HttpServletResponse response, Resource resource) {
writeContentType(response, resource);
writeContentLength(response, resource);
writeContentEncoding(response, resource);
writeCacheControl(response, resource);
writeEtags(response, resource);
}

private void writeContentEncoding(HttpServletResponse response, Resource resource) {
if(resource.getName().endsWith(".gz")) {
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
if(resource.getName().endsWith(".js.gz"))
response.setContentType("application/javascript");
}
}

private void writeContentType(HttpServletResponse response, Resource resource) {
if(resource instanceof MimeTypeProvider) {
String mimeType = ((MimeTypeProvider)resource).getMimeType();
Expand Down
18 changes: 18 additions & 0 deletions Server/src/main/resources/js/lib/prompto.core.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11894,6 +11894,10 @@ CodeWriter.prototype.newInstanceWriter = function(type) {
return new CodeWriter(this.dialect, this.context.newInstanceContext(null, type), this.indenter);
};

CodeWriter.prototype.newDocumentWriter = function() {
return new CodeWriter(this.dialect, this.context.newDocumentContext(null, false), this.indenter);
};

CodeWriter.prototype.newMemberWriter = function() {
return new CodeWriter (this.dialect, this.context.newChildContext(), this.indenter);
};
Expand Down Expand Up @@ -186709,6 +186713,7 @@ SortedExpression.prototype.toEDialect = function(writer) {
writer.append("descending ");
this.source.toDialect(writer);
if(this.key!=null) {
writer = this.contextualizeWriter(writer);
writer.append(" with ");
var keyExp = this.key;
if(keyExp instanceof UnresolvedIdentifier) try {
Expand All @@ -186731,6 +186736,7 @@ SortedExpression.prototype.toODialect = function(writer) {
writer.append("(");
this.source.toDialect(writer);
if(this.key!=null) {
writer = this.contextualizeWriter(writer);
writer.append(", key = ");
this.key.toDialect(writer);
}
Expand All @@ -186741,6 +186747,18 @@ SortedExpression.prototype.toMDialect = function(writer) {
this.toODialect(writer);
}

SortedExpression.prototype.contextualizeWriter = function(writer) {
var type = this.source.check(writer.context);
var itemType = type.itemType;
if (itemType instanceof CategoryType)
return writer.newInstanceWriter(itemType);
else if (itemType instanceof DocumentType)
return writer.newDocumentWriter();
else
return writer;
};


SortedExpression.prototype.check = function(context) {
var type = this.source.check(context);
if(!(type instanceof ListType || type instanceof TupleType || type instanceof SetType)) {
Expand Down
Binary file not shown.

0 comments on commit dd0d8ae

Please sign in to comment.