Skip to content

Commit

Permalink
[INTERNAL] Only replace versions in files the build would process as …
Browse files Browse the repository at this point in the history
…well
  • Loading branch information
RandomByte committed Jun 24, 2019
1 parent 1ee6723 commit 8ec1a7e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
14 changes: 10 additions & 4 deletions lib/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const etag = require("etag");
const fresh = require("fresh");
const parseurl = require("parseurl");

const rProperties = /\.properties$/;
const rProperties = /\.properties$/i;
const rReplaceVersion = /\.(library|js|json)$/i;

function isFresh(req, res) {
return fresh(req.headers, {
Expand Down Expand Up @@ -35,12 +36,13 @@ function createMiddleware({resourceCollections}) {

let type;
let charset;
if (rProperties.test(resource.getPath())) {
const resourcePath = resource.getPath();
if (rProperties.test(resourcePath)) {
// Special handling for *.properties files which are encoded with charset ISO-8859-1.
type = "text/plain";
charset = "ISO-8859-1";
} else {
type = mime.lookup(resource.getPath()) || "application/octet-stream";
type = mime.lookup(resourcePath) || "application/octet-stream";
}

if (!res.getHeader("Content-Type")) {
Expand All @@ -63,7 +65,11 @@ function createMiddleware({resourceCollections}) {

let stream = resource.getStream();

if (charset === "UTF-8" && (type.startsWith("text/") || type === "application/javascript")) {
// Only execute version replacement for UTF-8 encoded resources because replaceStream will always output
// UTF-8 anyways.
// Also, only process .library, *.js and *.json files. Just like it's done in Application-
// and LibraryBuilder
if (charset === "UTF-8" && rReplaceVersion.test(resourcePath)) {
if (resource._project) {
stream = stream.pipe(replaceStream("${version}", resource._project.version));
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/application.a/webapp/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>Application A - Version ${version}</title>
<title>Application A</title>
</head>
<body>

Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/application.a/webapp/versionTest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Not replaced: ${version}</title>
</head>
<body>

</body>
</html>
1 change: 1 addition & 0 deletions test/fixtures/application.a/webapp/versionTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(`${version}`);
2 changes: 1 addition & 1 deletion test/lib/server/acceptRemoteConnections.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ test("Get resource from application.a (/index.html) with enabled remote connecti
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /html/, "Correct content type");
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
});
});
2 changes: 1 addition & 1 deletion test/lib/server/h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ test("Get resource from application.a (/index.html)", (t) => {
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /html/, "Correct content type");
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
});
});
30 changes: 28 additions & 2 deletions test/lib/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,30 @@ test("Get resource from application.a (/index.html)", (t) => {
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /html/, "Correct content type");
t.regex(res.text, /<title>Application A - Version 1.0.0<\/title>/, "Correct response");
t.regex(res.text, /<title>Application A<\/title>/, "Correct response");
});
});


test("Get resource from application.a with not replaced version placeholder(/versionTest.html)", (t) => {
return request.get("/versionTest.html").then((res) => {
if (res.error) {
t.fail(res.error.text);
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /html/, "Correct content type");
t.regex(res.text, /<title>Not replaced: \${version}<\/title>/, "Correct response");
});
});

test("Get resource from application.a with replaced version placeholder (/versionTest.js)", (t) => {
return request.get("/versionTest.js").then((res) => {
if (res.error) {
t.fail(res.error.text);
}
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.regex(res.headers["content-type"], /application\/javascript/, "Correct content type");
t.deepEqual(res.text, "console.log(`1.0.0`);\n", "Correct response");
});
});

Expand Down Expand Up @@ -119,6 +142,9 @@ test("Get app_pages from discovery middleware (/discovery/app_pages)", (t) => {
"app_pages": [
{
"entry": "index.html"
},
{
"entry": "versionTest.html"
}
]
}, "Correct response");
Expand Down Expand Up @@ -480,7 +506,7 @@ test("Get index of resources", (t) => {
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
t.is(res.headers["content-type"], "text/html", "Correct content type");
t.is(/<title>(.*)<\/title>/i.exec(res.text)[1], "Index of /", "Found correct title");
t.deepEqual(res.text.match(/<td/g).length, 30, "Found correct amount of <td> elements");
t.deepEqual(res.text.match(/<td/g).length, 42, "Found correct amount of <td> elements");
}),
request.get("/resources").then((res) => {
t.deepEqual(res.statusCode, 200, "Correct HTTP status code");
Expand Down

0 comments on commit 8ec1a7e

Please sign in to comment.