feat: add Harmony Connector for HarmonyOS 6 USB file transfer
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { cp, mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { createServer } from "node:http";
|
||||
import path from "node:path";
|
||||
import { root, output } from "./suite.mjs";
|
||||
const directory = path.join(output, "harmony-ui-preview");
|
||||
await mkdir(directory, { recursive: true });
|
||||
await cp(path.join(root, "curios/harmony-connector/dist/ui"), directory, {
|
||||
recursive: true,
|
||||
});
|
||||
await cp(
|
||||
path.join(root, "tools/fixtures/harmony-ui.mjs"),
|
||||
path.join(directory, "fixture.mjs"),
|
||||
);
|
||||
const html = (
|
||||
await readFile(path.join(directory, "index.html"), "utf8")
|
||||
).replace('src="./app.js"', 'src="./fixture.mjs"');
|
||||
await writeFile(path.join(directory, "index.html"), html);
|
||||
createServer(async (request, response) => {
|
||||
try {
|
||||
const pathname = new URL(request.url, "http://localhost").pathname;
|
||||
const file = path.resolve(
|
||||
directory,
|
||||
"." + decodeURIComponent(pathname === "/" ? "/index.html" : pathname),
|
||||
);
|
||||
if (!file.startsWith(directory + path.sep)) {
|
||||
response.writeHead(403).end();
|
||||
return;
|
||||
}
|
||||
const data = await readFile(file);
|
||||
response.setHeader(
|
||||
"Content-Type",
|
||||
{
|
||||
".html": "text/html",
|
||||
".css": "text/css",
|
||||
".js": "text/javascript",
|
||||
".mjs": "text/javascript",
|
||||
}[path.extname(file)] || "application/octet-stream",
|
||||
);
|
||||
response.end(data);
|
||||
} catch {
|
||||
response.writeHead(404).end();
|
||||
}
|
||||
}).listen(18770, "127.0.0.1", () =>
|
||||
console.log("UI fixture only: http://127.0.0.1:18770"),
|
||||
);
|
||||
Reference in New Issue
Block a user