MCPcopy
hub / github.com/coder/mux / createStaticTestServer

Function createStaticTestServer

src/node/orpc/server.test.ts:165–212  ·  view source on GitHub ↗
(
  options: {
    files?: Record<string, string>;
    context?: Partial<ORPCContext>;
    authToken?: string;
  } = {}
)

Source from the content-addressed store, hash-verified

163}
164
165async function createStaticTestServer(
166 options: {
167 files?: Record<string, string>;
168 context?: Partial<ORPCContext>;
169 authToken?: string;
170 } = {}
171): Promise<{
172 server: Awaited<ReturnType<typeof createOrpcServer>>;
173 tempDir: string;
174 close: () => Promise<void>;
175}> {
176 const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "mux-static-app-proxy-"));
177 const files = {
178 "index.html":
179 "<!doctype html><html><head><title>mux</title></head><body><div>ok</div></body></html>",
180 ...options.files,
181 };
182
183 for (const [filePath, contents] of Object.entries(files)) {
184 const absolutePath = path.join(tempDir, filePath);
185 await fs.mkdir(path.dirname(absolutePath), { recursive: true });
186 await fs.writeFile(absolutePath, contents, "utf-8");
187 }
188
189 let server: Awaited<ReturnType<typeof createOrpcServer>> | null = null;
190 try {
191 server = await createOrpcServer({
192 host: "127.0.0.1",
193 port: 0,
194 context: (options.context ?? {}) as ORPCContext,
195 authToken: options.authToken,
196 serveStatic: true,
197 staticDir: tempDir,
198 });
199 } catch (error) {
200 await fs.rm(tempDir, { recursive: true, force: true });
201 throw error;
202 }
203
204 return {
205 server,
206 tempDir,
207 close: async () => {
208 await server?.close();
209 await fs.rm(tempDir, { recursive: true, force: true });
210 },
211 };
212}
213
214type TestOrpcServer = Awaited<ReturnType<typeof createOrpcServer>>;
215

Callers 1

server.test.tsFile · 0.85

Calls 3

createOrpcServerFunction · 0.90
writeFileMethod · 0.65
closeMethod · 0.65

Tested by

no test coverage detected