RuneStone 1.0.0
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
// A local fake Gitea release server. The host installs plugin ZIPs through its Gitea path from this server.
|
||||
import { createServer } from "node:http";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
/**
|
||||
* Serve the releases in `packages`: a list of `{ repo, tag, file }`, with `file` the path of a plugin ZIP.
|
||||
* The server answers `GET /api/v1/repos/test/<repo>/releases/tags/<tag>` and serves the ZIP at its download link.
|
||||
* Return the base URL, the repository link of each package, the request log, and `close`.
|
||||
*/
|
||||
export async function startGiteaServer(packages, owner = "test") {
|
||||
const bodies = new Map();
|
||||
for (const item of packages) bodies.set(item.repo, { ...item, name: path.basename(item.file), bytes: await readFile(item.file) });
|
||||
const requests = [];
|
||||
const server = createServer((request, response) => {
|
||||
const url = new URL(request.url, "http://127.0.0.1");
|
||||
requests.push(`${request.method} ${url.pathname}`);
|
||||
const release = /^\/api\/v1\/repos\/([^/]+)\/([^/]+)\/releases\/tags\/([^/]+)$/.exec(url.pathname);
|
||||
const download = /^\/download\/([^/]+)\/([^/]+)$/.exec(url.pathname);
|
||||
if (request.method === "GET" && release && release[1] === owner && bodies.get(release[2])?.tag === decodeURIComponent(release[3])) {
|
||||
const item = bodies.get(release[2]);
|
||||
const body = JSON.stringify({
|
||||
tag_name: item.tag, draft: false,
|
||||
assets: [{ name: item.name, size: item.bytes.length, browser_download_url: `${base}/download/${item.repo}/${encodeURIComponent(item.name)}` }],
|
||||
});
|
||||
response.writeHead(200, { "content-type": "application/json", "content-length": Buffer.byteLength(body) });
|
||||
response.end(body);
|
||||
return;
|
||||
}
|
||||
if (request.method === "GET" && download && bodies.get(download[1])?.name === decodeURIComponent(download[2])) {
|
||||
const item = bodies.get(download[1]);
|
||||
response.writeHead(200, { "content-type": "application/zip", "content-length": item.bytes.length });
|
||||
response.end(item.bytes);
|
||||
return;
|
||||
}
|
||||
response.writeHead(404, { "content-type": "text/plain" });
|
||||
response.end("not found");
|
||||
});
|
||||
await new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolve); });
|
||||
const base = `http://127.0.0.1:${server.address().port}`;
|
||||
return {
|
||||
base,
|
||||
requests,
|
||||
repository: repo => `${base}/${owner}/${repo}`,
|
||||
close: () => new Promise(resolve => server.close(() => resolve())),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user