()
| 81 | } |
| 82 | |
| 83 | async function startGMApiMockServer(): Promise<GMApiMockServer> { |
| 84 | const server = createServer((req, res) => { |
| 85 | res.setHeader("Access-Control-Allow-Origin", "*"); |
| 86 | |
| 87 | if (req.method === "OPTIONS") { |
| 88 | res.writeHead(204); |
| 89 | res.end(); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | const url = new URL(req.url || "/", `http://${req.headers.host}`); |
| 94 | |
| 95 | if (url.pathname === "/get") { |
| 96 | res.writeHead(200, { "Content-Type": "application/json" }); |
| 97 | const args = Object.fromEntries(url.searchParams.entries()); |
| 98 | res.end( |
| 99 | JSON.stringify({ |
| 100 | url: `http://${req.headers.host}${url.pathname}`, |
| 101 | args, |
| 102 | }) |
| 103 | ); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if (url.pathname === "/repos/scriptscat/scriptcat") { |
| 108 | res.writeHead(200, { "Content-Type": "application/json" }); |
| 109 | res.end( |
| 110 | JSON.stringify({ |
| 111 | name: "scriptcat", |
| 112 | full_name: "scriptscat/scriptcat", |
| 113 | description: "ScriptCat", |
| 114 | }) |
| 115 | ); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (url.pathname === "/favicon.ico") { |
| 120 | res.writeHead(200, { "Content-Type": "image/png" }); |
| 121 | res.end( |
| 122 | Buffer.from( |
| 123 | "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=", |
| 124 | "base64" |
| 125 | ) |
| 126 | ); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | const bytesMatch = url.pathname.match(/^\/bytes\/(\d+)$/); |
| 131 | if (bytesMatch) { |
| 132 | const size = Number(bytesMatch[1]); |
| 133 | res.writeHead(200, { "Content-Type": "application/octet-stream" }); |
| 134 | res.end(Buffer.alloc(size, "a")); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | const delayMatch = url.pathname.match(/^\/delay\/(\d+)$/); |
| 139 | if (delayMatch) { |
| 140 | const delayMs = Number(delayMatch[1]) * 1000; |
no test coverage detected