fix: dev command loading snapshot (#1735)

This commit is contained in:
Marvin Hagemeister
2023-09-05 12:34:05 +02:00
committed by GitHub
parent 4fcf422a64
commit b1a02108c5
5 changed files with 127 additions and 71 deletions
+68
View File
@@ -0,0 +1,68 @@
import { ServerContext } from "./context.ts";
import { colors } from "./deps.ts";
import { ServeHandler, StartOptions } from "./types.ts";
export async function startFromContext(ctx: ServerContext, opts: StartOptions) {
if (!opts.onListen) {
opts.onListen = (params) => {
console.log();
console.log(
colors.bgRgb8(colors.black(colors.bold(" 🍋 Fresh ready ")), 121),
);
const address = colors.cyan(`http://localhost:${params.port}/`);
const localLabel = colors.bold("Local:");
console.log(` ${localLabel} ${address}\n`);
};
}
const portEnv = Deno.env.get("PORT");
if (portEnv !== undefined) {
opts.port ??= parseInt(portEnv, 10);
}
const handler = ctx.handler();
if (opts.port) {
await bootServer(handler, opts);
} else {
// No port specified, check for a free port. Instead of picking just
// any port we'll check if the next one is free for UX reasons.
// That way the user only needs to increment a number when running
// multiple apps vs having to remember completely different ports.
let firstError;
for (let port = 8000; port < 8020; port++) {
try {
await bootServer(handler, { ...opts, port });
firstError = undefined;
break;
} catch (err) {
if (err instanceof Deno.errors.AddrInUse) {
// Throw first EADDRINUSE error
// if no port is free
if (!firstError) {
firstError = err;
}
continue;
}
throw err;
}
}
if (firstError) {
throw firstError;
}
}
}
async function bootServer(handler: ServeHandler, opts: StartOptions) {
// @ts-ignore Ignore type error when type checking with Deno versions
if (typeof Deno.serve === "function") {
// @ts-ignore Ignore type error when type checking with Deno versions
await Deno.serve(opts, handler).finished;
} else {
// @ts-ignore Deprecated std serve way
await serve(handler, opts);
}
}