Skip to content

Commit

Permalink
refactor: Migrate testRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Aug 29, 2024
1 parent 3a95465 commit bc75a0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@stylistic/eslint-plugin": "^2.6.4",
"@types/express": "^4.17.21",
"@types/mime-types": "^2.1.4",
"@types/node": "^22.5.0",
"@types/parseurl": "^1.3.3",
"@types/sinon": "^17.0.3",
"@ui5/project": "^4.0.3",
"ava": "^6.1.3",
Expand Down
21 changes: 12 additions & 9 deletions src/middleware/testRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import mime from "mime-types";
import parseurl from "parseurl";
import {getLogger} from "@ui5/logger";
const log = getLogger("server:middleware:testRunner");
import type {Request, Response, NextFunction} from "express";
import type ReaderCollection from "@ui5/fs/ReaderCollection";

const testRunnerResourceRegEx = /\/test-resources\/sap\/ui\/qunit\/(testrunner\.(html|css)|TestRunner.js)$/;
const resourceCache = Object.create(null);
const resourceCache = Object.create(null) as Record<string, Promise<string> | undefined>;

/**
*
* @param res
* @param resourcePath
* @param resourceContent
*/
function serveResource(res, resourcePath, resourceContent) {
const type = mime.lookup(resourcePath) || "application/octet-stream";
const charset = mime.charset(type);
function serveResource(res: Response, resourcePath: string, resourceContent: string) {
const type = mime.lookup(resourcePath) ?? "application/octet-stream";
const charset = mime.charset(type) ?? "";
const contentType = type + (charset ? "; charset=" + charset : "");

// resources served by this middleware do not change often
Expand All @@ -32,17 +34,18 @@ function serveResource(res, resourcePath, resourceContent) {
* Creates and returns the middleware to serve a resource index.
*
* @param parameters Parameters
* @param parameters.resources Contains the resource reader or collection to access project related files
* @param parameters._resources Contains the resource reader or collection to access project related files
* @param parameters._resources.dependencies Contains the resource reader or collection to access project related files
* @returns Returns a server middleware closure.
*/
function createMiddleware({resources}: {
resources: object;
function createMiddleware({_resources}: {
_resources: {dependencies: ReaderCollection};
}) {
return async function (req, res, next) {
return async function (req: Request, res: Response, next: NextFunction) {
try {
const pathname = parseurl(req).pathname;
const parts = testRunnerResourceRegEx.exec(pathname);
const resourceName = parts && parts[1];
const resourceName = parts?.[1];

if (resourceName) { // either "testrunner.html", "testrunner.css" or "TestRunner.js" (case sensitive!)
log.verbose(`Serving ${pathname}`);
Expand Down
6 changes: 6 additions & 0 deletions src/types/graceful-fs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// "graceful-fs" definitions just inherit the ones from node:fs, but are quite
// inconvenient. So, proxying the ones from node:fs would be better.
declare module "graceful-fs" {
import * as fs from "node:fs";
export default fs;
}

0 comments on commit bc75a0e

Please sign in to comment.