fix: allow setting server config from fresh config (#1889)
This commit is contained in:
@@ -54,7 +54,7 @@ export async function dev(
|
||||
config.dev = true;
|
||||
config.loadSnapshot = false;
|
||||
const ctx = await getServerContext(config);
|
||||
await startFromContext(ctx, options);
|
||||
await startFromContext(ctx, config.server);
|
||||
} else {
|
||||
// Legacy entry point: Back then `dev.ts` would call `main.ts` but
|
||||
// this causes duplicate plugin instantiation if both `dev.ts` and
|
||||
|
||||
+9
-3
@@ -1,8 +1,11 @@
|
||||
import { ServerContext } from "./context.ts";
|
||||
import { colors } from "./deps.ts";
|
||||
import { ServeHandler, StartOptions } from "./types.ts";
|
||||
import { ServeHandler } from "./types.ts";
|
||||
|
||||
export async function startFromContext(ctx: ServerContext, opts: StartOptions) {
|
||||
export async function startFromContext(
|
||||
ctx: ServerContext,
|
||||
opts: Partial<Deno.ServeTlsOptions>,
|
||||
) {
|
||||
if (!opts.onListen) {
|
||||
opts.onListen = (params) => {
|
||||
console.log();
|
||||
@@ -56,7 +59,10 @@ export async function startFromContext(ctx: ServerContext, opts: StartOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
async function bootServer(handler: ServeHandler, opts: StartOptions) {
|
||||
async function bootServer(
|
||||
handler: ServeHandler,
|
||||
opts: Partial<Deno.ServeTlsOptions>,
|
||||
) {
|
||||
// @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
|
||||
|
||||
@@ -66,8 +66,34 @@ export async function getFreshConfigWithDefaults(
|
||||
staticDir: "",
|
||||
render: opts.render,
|
||||
router: opts.router,
|
||||
server: opts.server ?? {},
|
||||
};
|
||||
|
||||
if (opts.cert) {
|
||||
config.server.cert = opts.cert;
|
||||
}
|
||||
if (opts.hostname) {
|
||||
config.server.hostname = opts.hostname;
|
||||
}
|
||||
if (opts.key) {
|
||||
config.server.key = opts.key;
|
||||
}
|
||||
if (opts.onError) {
|
||||
config.server.onError = opts.onError;
|
||||
}
|
||||
if (opts.onListen) {
|
||||
config.server.onListen = opts.onListen;
|
||||
}
|
||||
if (opts.port) {
|
||||
config.server.port = opts.port;
|
||||
}
|
||||
if (opts.reusePort) {
|
||||
config.server.reusePort = opts.reusePort;
|
||||
}
|
||||
if (opts.signal) {
|
||||
config.server.signal = opts.signal;
|
||||
}
|
||||
|
||||
config.build.outDir = opts.build?.outDir
|
||||
? parseFileOrUrl(opts.build.outDir, base)
|
||||
: join(base, "_fresh");
|
||||
|
||||
+1
-1
@@ -100,5 +100,5 @@ export async function start(routes: Manifest, opts: StartOptions = {}) {
|
||||
skipSnapshot: false,
|
||||
dev: false,
|
||||
});
|
||||
await startFromContext(ctx, opts);
|
||||
await startFromContext(ctx, opts.server ?? opts);
|
||||
}
|
||||
|
||||
+56
-1
@@ -23,7 +23,7 @@ export interface DenoConfig {
|
||||
|
||||
// --- APPLICATION CONFIGURATION ---
|
||||
|
||||
export type StartOptions = Partial<Deno.ServeTlsOptions> & FreshOptions;
|
||||
export type StartOptions = FreshOptions;
|
||||
|
||||
export interface FreshOptions {
|
||||
build?: {
|
||||
@@ -44,6 +44,60 @@ export interface FreshOptions {
|
||||
plugins?: Plugin[];
|
||||
staticDir?: string;
|
||||
router?: RouterOptions;
|
||||
server?: Partial<Deno.ServeTlsOptions>;
|
||||
|
||||
// Older versions of Fresh merged the `Deno.ServeTlsOptions` directly.
|
||||
// We've moved this to `server`.
|
||||
|
||||
/**
|
||||
* Server private key in PEM format
|
||||
* @deprecated Use `server.cert` instead
|
||||
*/
|
||||
cert?: string;
|
||||
/**
|
||||
* Cert chain in PEM format
|
||||
* @deprecated Use `server.key` instead
|
||||
*/
|
||||
key?: string;
|
||||
/**
|
||||
* The port to listen on.
|
||||
* @default {8000}
|
||||
* @deprecated Use `server.port` instead
|
||||
*/
|
||||
port?: number;
|
||||
/**
|
||||
* A literal IP address or host name that can be resolved to an IP address.
|
||||
*
|
||||
* __Note about `0.0.0.0`__ While listening `0.0.0.0` works on all platforms,
|
||||
* the browsers on Windows don't work with the address `0.0.0.0`.
|
||||
* You should show the message like `server running on localhost:8080` instead of
|
||||
* `server running on 0.0.0.0:8080` if your program supports Windows.
|
||||
*
|
||||
* @default {"0.0.0.0"}
|
||||
* @deprecated Use `server.hostname` instead
|
||||
*/
|
||||
hostname?: string;
|
||||
/**
|
||||
* An {@linkcode AbortSignal} to close the server and all connections.
|
||||
* @deprecated Use `server.signal` instead
|
||||
*/
|
||||
signal?: AbortSignal;
|
||||
/**
|
||||
* Sets `SO_REUSEPORT` on POSIX systems.
|
||||
* @deprecated Use `server.reusePort` instead
|
||||
*/
|
||||
reusePort?: boolean;
|
||||
/**
|
||||
* The handler to invoke when route handlers throw an error.
|
||||
* @deprecated Use `server.onError` instead
|
||||
*/
|
||||
onError?: (error: unknown) => Response | Promise<Response>;
|
||||
|
||||
/**
|
||||
* The callback which is called when the server starts listening.
|
||||
* @deprecated Use `server.onListen` instead
|
||||
*/
|
||||
onListen?: (params: { hostname: string; port: number }) => void;
|
||||
}
|
||||
|
||||
export interface InternalFreshOptions {
|
||||
@@ -60,6 +114,7 @@ export interface InternalFreshOptions {
|
||||
plugins: Plugin[];
|
||||
staticDir: string;
|
||||
router?: RouterOptions;
|
||||
server: Partial<Deno.ServeTlsOptions>;
|
||||
}
|
||||
|
||||
export interface RouterOptions {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"lock": false,
|
||||
"imports": {
|
||||
"$fresh/": "../../",
|
||||
"preact": "https://esm.sh/preact@10.15.1",
|
||||
"preact/": "https://esm.sh/preact@10.15.1/",
|
||||
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
|
||||
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
|
||||
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
|
||||
},
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "preact"
|
||||
}
|
||||
}
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||
|
||||
import dev from "$fresh/dev.ts";
|
||||
|
||||
const TEST_CONFIG_SERVER = Deno.env.get("TEST_CONFIG_SERVER") === "true";
|
||||
const onListen = (params: { hostname: string; port: number }) => {
|
||||
console.log("it works");
|
||||
console.log(`http://localhost:${params.port}`);
|
||||
};
|
||||
const onListen2 = (params: { hostname: string; port: number }) => {
|
||||
console.log("it works #2");
|
||||
console.log(`http://localhost:${params.port}`);
|
||||
};
|
||||
|
||||
await dev(import.meta.url, "./main.ts", {
|
||||
server: {
|
||||
onListen: TEST_CONFIG_SERVER ? onListen2 : undefined,
|
||||
},
|
||||
onListen: TEST_CONFIG_SERVER ? undefined : onListen,
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
// DO NOT EDIT. This file is generated by Fresh.
|
||||
// This file SHOULD be checked into source version control.
|
||||
// This file is automatically updated during development when running `dev.ts`.
|
||||
|
||||
import * as $0 from "./routes/index.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
"./routes/index.tsx": $0,
|
||||
},
|
||||
islands: {},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
export default manifest;
|
||||
@@ -0,0 +1,25 @@
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
/// <reference lib="dom.asynciterable" />
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
import { start } from "$fresh/server.ts";
|
||||
import routes from "./fresh.gen.ts";
|
||||
|
||||
const TEST_CONFIG_SERVER = Deno.env.get("TEST_CONFIG_SERVER") === "true";
|
||||
const onListen = (params: { hostname: string; port: number }) => {
|
||||
console.log("it works");
|
||||
console.log(`http://localhost:${params.port}`);
|
||||
};
|
||||
const onListen2 = (params: { hostname: string; port: number }) => {
|
||||
console.log("it works #2");
|
||||
console.log(`http://localhost:${params.port}`);
|
||||
};
|
||||
|
||||
await start(routes, {
|
||||
server: {
|
||||
onListen: TEST_CONFIG_SERVER ? onListen2 : undefined,
|
||||
},
|
||||
onListen: TEST_CONFIG_SERVER ? undefined : onListen,
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function Home() {
|
||||
return <h1>hello</h1>;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
assertMatch,
|
||||
assertStringIncludes,
|
||||
delay,
|
||||
join,
|
||||
puppeteer,
|
||||
retry,
|
||||
} from "./deps.ts";
|
||||
@@ -971,3 +972,97 @@ Deno.test("De-duplicates <Head /> nodes by key", async () => {
|
||||
assert(/<meta property="og:title" content="Other title"\/>/.test(html));
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("pass options in config", async (t) => {
|
||||
const fixture = join(Deno.cwd(), "tests", "fixture_config");
|
||||
|
||||
await t.step("config.onListen", async () => {
|
||||
const { lines, serverProcess, output } = await startFreshServer({
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
join(fixture, "main.ts"),
|
||||
],
|
||||
});
|
||||
|
||||
try {
|
||||
assert(output.find((line) => line === "it works"));
|
||||
} finally {
|
||||
serverProcess.kill("SIGTERM");
|
||||
await serverProcess.status;
|
||||
|
||||
// Drain the lines stream
|
||||
for await (const _ of lines) { /* noop */ }
|
||||
}
|
||||
});
|
||||
|
||||
await t.step("config.server.onListen", async () => {
|
||||
const { lines, serverProcess, output } = await startFreshServer({
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
join(fixture, "main.ts"),
|
||||
],
|
||||
env: {
|
||||
TEST_CONFIG_SERVER: "true",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
assert(output.find((line) => line === "it works #2"));
|
||||
} finally {
|
||||
serverProcess.kill("SIGTERM");
|
||||
await serverProcess.status;
|
||||
|
||||
// Drain the lines stream
|
||||
for await (const _ of lines) { /* noop */ }
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("pass options in config dev.ts", async (t) => {
|
||||
const fixture = join(Deno.cwd(), "tests", "fixture_config");
|
||||
|
||||
await t.step("config.onListen", async () => {
|
||||
const { lines, serverProcess, output } = await startFreshServer({
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
join(fixture, "dev.ts"),
|
||||
],
|
||||
});
|
||||
|
||||
try {
|
||||
assert(output.find((line) => line === "it works"));
|
||||
} finally {
|
||||
serverProcess.kill("SIGTERM");
|
||||
await serverProcess.status;
|
||||
|
||||
// Drain the lines stream
|
||||
for await (const _ of lines) { /* noop */ }
|
||||
}
|
||||
});
|
||||
|
||||
await t.step("config.server.onListen", async () => {
|
||||
const { lines, serverProcess, output } = await startFreshServer({
|
||||
args: [
|
||||
"run",
|
||||
"-A",
|
||||
join(fixture, "dev.ts"),
|
||||
],
|
||||
env: {
|
||||
TEST_CONFIG_SERVER: "true",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
assert(output.find((line) => line === "it works #2"));
|
||||
} finally {
|
||||
serverProcess.kill("SIGTERM");
|
||||
await serverProcess.status;
|
||||
|
||||
// Drain the lines stream
|
||||
for await (const _ of lines) { /* noop */ }
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user