async function handleRequest(req) {
		const reqId = req.requestId;
		let reqPath = req.path.substring(1);

		console.log(`XREQPATH ${reqPath}`);
		console.log(req);

		if (!reqPath || (reqPath.endsWith("/") && reqPath.length === 1)) {
			reqPath = getRelativePath();
		}

		console.log(`XREQPATH1 ${reqPath}`);

		const ext = Url.extname(reqPath);
		let url = null;

		switch (reqPath) {
			case CONSOLE_SCRIPT:
				if (
					isConsole ||
					appSettings.value.console === appSettings.CONSOLE_LEGACY
				) {
					url = `${ASSETS_DIRECTORY}/js/build/console.build.js`;
				} else {
					url = `${DATA_STORAGE}/eruda.js`;
				}
				sendFileContent(url, reqId, "application/javascript");
				break;

			case EXECUTING_SCRIPT: {
				const text = activeFile?.session.getValue() || "";
				sendText(text, reqId, "application/javascript");
				break;
			}

			case MARKDOWN_STYLE:
				url = appSettings.value.markdownStyle;
				if (url) sendFileContent(url, reqId, "text/css");
				else sendText("img {max-width: 100%;}", reqId, "text/css");
				break;

			default:
				sendByExt();
				break;
		}

		async function sendByExt() {
			if (isConsole) {
				if (reqPath === "console.html") {
					sendText(
						mustache.render($_console, {
							CONSOLE_SCRIPT,
							EXECUTING_SCRIPT,
						}),
						reqId,
						MIMETYPE_HTML,
					);
					return;
				}

				if (reqPath === "favicon.ico") {
					sendIco(ASSETS_DIRECTORY, reqId);
					return;
				}
			}

			if (activeFile.mode === "single") {
				if (filename === reqPath) {
					sendText(
						activeFile.session.getValue(),
						reqId,
						mimeType.lookup(filename),
					);
				} else {
					error(reqId);
				}
				return;
			}

			let url = activeFile.uri;

			let file = activeFile.SAFMode === "single" ? activeFile : null;

			if (pathName) {
				const projectFolder = addedFolder[0];
				const query = url.split("?")[1];
				let rootFolder = "";

				if (
					projectFolder !== undefined &&
					pathName.includes(projectFolder.url)
				) {
					rootFolder = projectFolder.url;
				} else {
					rootFolder = pathName;
				}

				if (
					(rootFolder.startsWith("ftp:") || rootFolder.startsWith("sftp:")) &&
					rootFolder.includes("?")
				) {
					rootFolder = rootFolder.split("?")[0];
				}

				rootFolder = rootFolder.replace(/\/+$/, ""); // remove trailing slash
				reqPath = reqPath.replace(/^\/+/, ""); // remove leading slash

				const rootParts = rootFolder.split("/");
				const pathParts = reqPath.split("/");

				if (pathParts[0] === rootParts[rootParts.length - 1]) {
					pathParts.shift();
				}

				function removePrefix(str, prefix) {
					if (str.startsWith(prefix)) {
						return str.slice(prefix.length);
					}
					return str;
				}

				function findOverlap(a, b) {
					// Start with the smallest possible overlap (1 character) and increase
					let maxOverlap = "";

					// Check all possible overlapping lengths
					for (let i = 1; i <= Math.min(a.length, b.length); i++) {
						// Get the ending substring of a with length i
						const endOfA = a.slice(-i);
						// Get the starting substring of b with length i
						const startOfB = b.slice(0, i);

						// If they match, we have a potential overlap
						if (endOfA === startOfB) {
							maxOverlap = endOfA;
						}
					}

					return maxOverlap;
				}

				console.log(`RootFolder ${rootFolder}`);
				console.log(`PARTS ${pathParts.join("/")}`);
				const overlap = findOverlap(rootFolder, pathParts.join("/"));

				let fullPath;
				if (overlap !== "") {
					fullPath = Url.join(
						rootFolder,
						removePrefix(pathParts.join("/"), overlap),
					);
				} else {
					fullPath = Url.join(rootFolder, pathParts.join("/"));
				}

				console.log(`Full PATH ${fullPath}`);

				// Add back the query if present
				url = query ? `${fullPath}?${query}` : fullPath;

				file = editorManager.getFile(url, "uri");
			} else if (!activeFile.uri) {
				file = activeFile;
			}

			switch (ext) {
				case ".htm":
				case ".html":
					if (file && file.loaded && file.isUnsaved) {
						sendHTML(file.session.getValue(), reqId);
					} else {
						sendFileContent(url, reqId, MIMETYPE_HTML);
					}
					break;

				case ".md":
					if (file) {
						const html = markdownIt({ html: true })
							.use(MarkdownItGitHubAlerts)
							.use(anchor, {
								slugify: (s) =>
									s
										.trim()
										.toLowerCase()
										.replace(/[^a-z0-9]+/g, "-"),
							})
							.use(markdownItTaskLists)
							.use(markdownItFootnote)
							.render(file.session.getValue());
						const doc = mustache.render($_markdown, {
							html,
							filename,
							MARKDOWN_STYLE,
						});
						sendText(doc, reqId, MIMETYPE_HTML);
					}
					break;

				default:
					if (file && file.loaded && file.isUnsaved) {
						if (file.filename.endsWith(".html")) {
							sendHTML(file.session.getValue(), reqId);
						} else {
							sendText(
								file.session.getValue(),
								reqId,
								mimeType.lookup(file.filename),
							);
						}
					} else if (url) {
						if (reqPath === "favicon.ico") {
							sendIco(ASSETS_DIRECTORY, reqId);
						} else {
							sendFile(url, reqId);
						}
					} else {
						error(reqId);
					}
					break;
			}
		}
	}
Requests handler
@param {object} req
@param {string} req.requestId
@param {string} req.path