Skip to content

Commit

Permalink
[INTERNAL] serveThemes: Refactor to async function
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Jan 28, 2020
1 parent 6189df3 commit 88bd6ef
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
64 changes: 32 additions & 32 deletions lib/middleware/serveThemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,48 @@ function createMiddleware({resources}) {
fs: fsInterface(resources.all)
});

return function theme(req, res, next) {
const pathname = parseurl(req).pathname;
const filename = basename(pathname);
if (!themeResources.includes(filename)) {
next();
return;
}
return async function theme(req, res, next) {
try {
const pathname = parseurl(req).pathname;
const filename = basename(pathname);
if (!themeResources.includes(filename)) {
next();
return;
}

const sourceLessPath = dirname(pathname) + "/library.source.less";
resources.all.byPath(sourceLessPath).then((sourceLessResource) => {
const sourceLessPath = dirname(pathname) + "/library.source.less";
const sourceLessResource = await resources.all.byPath(sourceLessPath);
if (!sourceLessResource) { // Not found
next();
return;
}
return builder.build([sourceLessResource]).then(function(createdResources) {
// Pick requested file resource
const resource = createdResources.find((res) => res.getPath().endsWith(filename));
if (!resource) {
next(new Error(`Theme Build did not return request file "${pathname}"`));
return;
}

const resourcePath = resource.getPath();
const {contentType} = getMimeInfo(resourcePath);
res.setHeader("Content-Type", contentType);
const createdResources = await builder.build([sourceLessResource]);
// Pick requested file resource
const resource = createdResources.find((res) => res.getPath().endsWith(filename));
if (!resource) {
next(new Error(`Theme Build did not return request file "${pathname}"`));
return;
}

const resourcePath = resource.getPath();
const {contentType} = getMimeInfo(resourcePath);
res.setHeader("Content-Type", contentType);

return resource.getBuffer().then((content) => {
res.setHeader("ETag", etag(content));
const content = await resource.getBuffer();
res.setHeader("ETag", etag(content));

if (isFresh(req, res)) {
// client has a fresh copy of the resource
res.statusCode = 304;
res.end();
return;
}
if (isFresh(req, res)) {
// client has a fresh copy of the resource
res.statusCode = 304;
res.end();
return;
}

res.end(content.toString());
});
});
}).catch(function(err) {
res.end(content.toString());
} catch (err) {
next(err);
});
}
};
}

Expand Down
19 changes: 19 additions & 0 deletions test/lib/server/middleware/serveThemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,22 @@ test.serial.cb("Error handling: Request resource that ThemeBuild doesn't return"
t.end();
});
});

test.serial.cb("Error handling: Unexpected exception within middleware should call next with error", (t) => {
const error = new Error("Unexpected Error");

const {middleware, byPath} = createMiddleware();
byPath.rejects(error);

const req = {
url: "/resources/sap/ui/test/themes/base/library.css",
headers: {}
};

const res = {};

middleware(req, res, function(err) {
t.is(err, error);
t.end();
});
});

0 comments on commit 88bd6ef

Please sign in to comment.