From 8ec1a7ed347dcbdf27e9b95089f9ce6145b4c76d Mon Sep 17 00:00:00 2001 From: Merlin Beutlberger Date: Wed, 19 Jun 2019 12:42:51 +0200 Subject: [PATCH] [INTERNAL] Only replace versions in files the build would process as well --- lib/middleware/serveResources.js | 14 ++++++--- test/fixtures/application.a/webapp/index.html | 2 +- .../application.a/webapp/versionTest.html | 9 ++++++ .../application.a/webapp/versionTest.js | 1 + test/lib/server/acceptRemoteConnections.js | 2 +- test/lib/server/h2.js | 2 +- test/lib/server/main.js | 30 +++++++++++++++++-- 7 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/application.a/webapp/versionTest.html create mode 100644 test/fixtures/application.a/webapp/versionTest.js diff --git a/lib/middleware/serveResources.js b/lib/middleware/serveResources.js index cf0ec320..5976d92b 100644 --- a/lib/middleware/serveResources.js +++ b/lib/middleware/serveResources.js @@ -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, { @@ -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")) { @@ -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 { diff --git a/test/fixtures/application.a/webapp/index.html b/test/fixtures/application.a/webapp/index.html index 3d8715b9..1b875590 100644 --- a/test/fixtures/application.a/webapp/index.html +++ b/test/fixtures/application.a/webapp/index.html @@ -1,7 +1,7 @@ - Application A - Version ${version} + Application A diff --git a/test/fixtures/application.a/webapp/versionTest.html b/test/fixtures/application.a/webapp/versionTest.html new file mode 100644 index 00000000..f6b13e97 --- /dev/null +++ b/test/fixtures/application.a/webapp/versionTest.html @@ -0,0 +1,9 @@ + + + + Not replaced: ${version} + + + + + diff --git a/test/fixtures/application.a/webapp/versionTest.js b/test/fixtures/application.a/webapp/versionTest.js new file mode 100644 index 00000000..edceeba4 --- /dev/null +++ b/test/fixtures/application.a/webapp/versionTest.js @@ -0,0 +1 @@ +console.log(`${version}`); diff --git a/test/lib/server/acceptRemoteConnections.js b/test/lib/server/acceptRemoteConnections.js index c5a6e6f5..1565ed6d 100644 --- a/test/lib/server/acceptRemoteConnections.js +++ b/test/lib/server/acceptRemoteConnections.js @@ -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, /Application A - Version 1.0.0<\/title>/, "Correct response"); + t.regex(res.text, /<title>Application A<\/title>/, "Correct response"); }); }); diff --git a/test/lib/server/h2.js b/test/lib/server/h2.js index 1d016b0a..45715bd6 100644 --- a/test/lib/server/h2.js +++ b/test/lib/server/h2.js @@ -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"); }); }); diff --git a/test/lib/server/main.js b/test/lib/server/main.js index dd7d9283..1baad208 100644 --- a/test/lib/server/main.js +++ b/test/lib/server/main.js @@ -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"); }); }); @@ -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"); @@ -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");