feat: Support startServer for unix domain sockets (#2392)
Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
This commit is contained in:
+35
-10
@@ -2,17 +2,10 @@ import { DENO_DEPLOYMENT_ID } from "./build_id.ts";
|
|||||||
import { colors } from "./deps.ts";
|
import { colors } from "./deps.ts";
|
||||||
import { ServeHandler } from "./types.ts";
|
import { ServeHandler } from "./types.ts";
|
||||||
|
|
||||||
export async function startServer(
|
const defaultOnListenFor = <T>(getAddress: (params: T) => string) => {
|
||||||
handler: Deno.ServeHandler,
|
return (params: T) => {
|
||||||
opts: Partial<Deno.ServeTlsOptions> & { basePath: string },
|
|
||||||
) {
|
|
||||||
if (!opts.onListen) {
|
|
||||||
opts.onListen = (params) => {
|
|
||||||
const pathname = opts.basePath + "/";
|
|
||||||
const https = !!(opts.key && opts.cert);
|
|
||||||
const protocol = https ? "https:" : "http:";
|
|
||||||
const address = colors.cyan(
|
const address = colors.cyan(
|
||||||
`${protocol}//localhost:${params.port}${pathname}`,
|
getAddress(params),
|
||||||
);
|
);
|
||||||
const localLabel = colors.bold("Local:");
|
const localLabel = colors.bold("Local:");
|
||||||
|
|
||||||
@@ -30,6 +23,38 @@ export async function startServer(
|
|||||||
console.log(` ${localLabel} ${address}\n`);
|
console.log(` ${localLabel} ${address}\n`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
export async function startUnixServer(
|
||||||
|
handler: Deno.ServeUnixHandler,
|
||||||
|
opts: Deno.ServeUnixOptions,
|
||||||
|
) {
|
||||||
|
if (!opts.onListen) {
|
||||||
|
opts.onListen = defaultOnListenFor((params) => {
|
||||||
|
return `socket://${params.path}`;
|
||||||
|
});
|
||||||
|
// @ts-ignore Ignore type error when type checking with Deno versions
|
||||||
|
if (typeof Deno.serve === "function") {
|
||||||
|
await Deno.serve(
|
||||||
|
opts,
|
||||||
|
handler,
|
||||||
|
).finished;
|
||||||
|
}
|
||||||
|
throw new Error(
|
||||||
|
`unix domain sockets are not supported in your current deno version.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export async function startServer(
|
||||||
|
handler: Deno.ServeHandler,
|
||||||
|
opts: Partial<Deno.ServeTlsOptions> & { basePath: string },
|
||||||
|
) {
|
||||||
|
if (!opts.onListen) {
|
||||||
|
opts.onListen = defaultOnListenFor((params) => {
|
||||||
|
const pathname = opts.basePath + "/";
|
||||||
|
const https = !!(opts.key && opts.cert);
|
||||||
|
const protocol = https ? "https:" : "http:";
|
||||||
|
return `${protocol}//localhost:${params.port}${pathname}`;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const portEnv = Deno.env.get("PORT");
|
const portEnv = Deno.env.get("PORT");
|
||||||
|
|||||||
Reference in New Issue
Block a user