fmt
This commit is contained in:
parent
615a6dead0
commit
a77dd1f8e1
9 changed files with 193 additions and 97 deletions
|
@ -13,14 +13,18 @@ interface RequestLike {
|
|||
*/
|
||||
export class DenoHttpServer {
|
||||
private server: Deno.HttpServer | null = null;
|
||||
private routes: Map<string, (req: Request) => Promise<Response> | Response> = new Map();
|
||||
private routes: Map<string, (req: Request) => Promise<Response> | Response> =
|
||||
new Map();
|
||||
|
||||
/**
|
||||
* Register a GET route handler
|
||||
* @param path The path to handle
|
||||
* @param handler The handler function
|
||||
*/
|
||||
get(path: string, handler: (req: RequestLike, res: ResponseBuilder) => void): void {
|
||||
get(
|
||||
path: string,
|
||||
handler: (req: RequestLike, res: ResponseBuilder) => void,
|
||||
): void {
|
||||
this.routes.set(path, async (request: Request) => {
|
||||
const url = new URL(request.url);
|
||||
const searchParams = url.searchParams;
|
||||
|
@ -51,12 +55,16 @@ export class DenoHttpServer {
|
|||
* @param hostname Optional hostname to bind to
|
||||
* @param callback Optional callback when server is ready
|
||||
*/
|
||||
listen(port: number, hostname?: string | (() => void), callback?: () => void): Server {
|
||||
listen(
|
||||
port: number,
|
||||
hostname?: string | (() => void),
|
||||
callback?: () => void,
|
||||
): Server {
|
||||
// Handle optional hostname parameter
|
||||
let hostnameStr: string | undefined;
|
||||
let callbackFn = callback;
|
||||
|
||||
if (typeof hostname === 'function') {
|
||||
if (typeof hostname === "function") {
|
||||
callbackFn = hostname;
|
||||
hostnameStr = undefined;
|
||||
} else {
|
||||
|
@ -79,7 +87,7 @@ export class DenoHttpServer {
|
|||
|
||||
// Route not found
|
||||
return new Response("Not Found", { status: 404 });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Return a dummy server object that mimics Node's HTTP server
|
||||
|
|
|
@ -94,7 +94,9 @@ export default async function open(
|
|||
if (!success) {
|
||||
const errorDetails = new TextDecoder().decode(stderr).trim();
|
||||
const stdoutDetails = new TextDecoder().decode(stdout).trim();
|
||||
let errorMessage = `Failed to open "${target}". Command "${command} ${args.join(" ")}" exited with code ${code}.`;
|
||||
let errorMessage = `Failed to open "${target}". Command "${command} ${
|
||||
args.join(" ")
|
||||
}" exited with code ${code}.`;
|
||||
if (errorDetails) errorMessage += `\nStderr: ${errorDetails}`;
|
||||
if (stdoutDetails) errorMessage += `\nStdout: ${stdoutDetails}`; // Include stdout too
|
||||
throw new Error(errorMessage);
|
||||
|
@ -109,10 +111,11 @@ export default async function open(
|
|||
// xdg-open often returns immediately. Add a small delay as a basic wait.
|
||||
await delay(1000); // Wait 1 second (adjust as necessary)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) {
|
||||
throw new Error(`Failed to open "${target}": Command not found: ${command}`);
|
||||
throw new Error(
|
||||
`Failed to open "${target}": Command not found: ${command}`,
|
||||
);
|
||||
}
|
||||
// Re-throw other errors or wrap them
|
||||
throw error instanceof Error ? error : new Error(String(error));
|
||||
|
|
|
@ -20,17 +20,17 @@ export function log(str: string, ...rest: unknown[]) {
|
|||
|
||||
// Helper function to safely get a message identifier for logging
|
||||
function getMessageIdentifier(message: unknown): string | number | undefined {
|
||||
if (typeof message !== 'object' || message === null) return undefined;
|
||||
if (typeof message !== "object" || message === null) return undefined;
|
||||
|
||||
// Check if it's a request or notification with a method
|
||||
if ('method' in message && message.method !== undefined) {
|
||||
if ("method" in message && message.method !== undefined) {
|
||||
return String(message.method);
|
||||
}
|
||||
|
||||
// Check if it's a response with an id
|
||||
if ('id' in message && message.id !== undefined) {
|
||||
if ("id" in message && message.id !== undefined) {
|
||||
const id = message.id;
|
||||
return typeof id === 'string' || typeof id === 'number' ? id : undefined;
|
||||
return typeof id === "string" || typeof id === "number" ? id : undefined;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
|
@ -295,8 +295,12 @@ export function findAvailablePort(
|
|||
serverOrPort?: number | net.Server,
|
||||
): Promise<number> {
|
||||
// Handle if server parameter is a number (preferred port)
|
||||
const preferredPort = typeof serverOrPort === "number" ? serverOrPort : undefined;
|
||||
const serverToUse = typeof serverOrPort !== "number" ? (serverOrPort as net.Server) : net.createServer();
|
||||
const preferredPort = typeof serverOrPort === "number"
|
||||
? serverOrPort
|
||||
: undefined;
|
||||
const serverToUse = typeof serverOrPort !== "number"
|
||||
? (serverOrPort as net.Server)
|
||||
: net.createServer();
|
||||
let hasResolved = false;
|
||||
|
||||
// Maximum number of port attempts before giving up
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue