Skip to content

Commit

Permalink
[FEATURE] manifest.json: Auto-fill supportedLocales
Browse files Browse the repository at this point in the history
JIRA: CPOUI5FOUNDATION-296
  • Loading branch information
flovogt committed Dec 12, 2023
1 parent 794122b commit 309ccc7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const log = getLogger("server:middleware:serveResources");
import replaceStream from "replacestream";
import etag from "etag";
import fresh from "fresh";
import fsInterface from "@ui5/fs/fsInterface";

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

Check failure on line 10 in lib/middleware/serveResources.js

View workflow job for this annotation

GitHub Actions / General checks, tests and coverage reporting

Unnecessary escape character: \m

function isFresh(req, res) {
return fresh(req.headers, {
Expand Down Expand Up @@ -58,6 +60,15 @@ function createMiddleware({resources, middlewareUtil}) {
});
}

if (rManifest.test(resourcePath)) {
// Special handling for manifest.json file by adding additional content to the served manifest.json
const {default: manifestTransformer} = await import("@ui5/builder/processors/manifestTransformer");
await manifestTransformer({
resources: [resource],
fs: fsInterface(resources.all)
});
}

const {contentType, charset} = middlewareUtil.getMimeInfo(resourcePath);
if (!res.getHeader("Content-Type")) {
res.setHeader("Content-Type", contentType);
Expand Down
74 changes: 74 additions & 0 deletions test/lib/server/middleware/serveResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,77 @@ test.serial("Check if utf8 characters are correctly processed in version replace
});
});
});

test.serial("Check if manifest.json file is served properly by using manifestTransformer", async (t) => {
t.plan(4);

const readerWriter = resourceFactory.createAdapter({virBasePath: "/"});

const input = `
{
"_version": "1.58.0",
"sap.app": {
"id": "sap.ui.demo.app"
},
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sap.ui.demo.app.i18n.i18n",
"fallbackLocale": "de"
}
}
}
}
}`;
const expected = `
{
"_version": "1.58.0",
"sap.app": {
"id": "sap.ui.demo.app"
},
"sap.ui5": {
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "sap.ui.demo.app.i18n.i18n",
"supportedLocales": ["en", "de"],
"fallbackLocale": "de"
}
}
}
}
}`;

const resource = await writeResource(readerWriter, "/manifest.json", 1024 * 1024,
input, "utf8"
);
const setStringSpy = sinon.spy(resource, "setString");
const middleware = serveResourcesMiddleware({
middlewareUtil: new MiddlewareUtil({graph: "graph", project: "project"}),
resources: {
all: readerWriter
}
});

const response = fakeResponse;

const setHeaderSpy = sinon.spy(response, "setHeader");
const req = {
url: "/manifest.json",
headers: {}
};
const next = function(err) {
throw new Error(`Next callback called with error: ${err.stack}`);
};

await middleware(req, response, next);
const content = await resource.getString();

t.is(content, expected);
t.is(setHeaderSpy.callCount, 2);
t.is(setStringSpy.callCount, 1);
t.is(setHeaderSpy.getCall(0).lastArg, "application/octet-stream");
});

0 comments on commit 309ccc7

Please sign in to comment.