diff --git a/src/dev/dev_command.ts b/src/dev/dev_command.ts index ec608044..dd3d5c93 100644 --- a/src/dev/dev_command.ts +++ b/src/dev/dev_command.ts @@ -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 diff --git a/src/server/boot.ts b/src/server/boot.ts index 06bd4899..bf44efdf 100644 --- a/src/server/boot.ts +++ b/src/server/boot.ts @@ -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, +) { 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, +) { // @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 diff --git a/src/server/config.ts b/src/server/config.ts index 7d789ea2..da382e8a 100644 --- a/src/server/config.ts +++ b/src/server/config.ts @@ -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"); diff --git a/src/server/mod.ts b/src/server/mod.ts index 0aef119d..ac35c723 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -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); } diff --git a/src/server/types.ts b/src/server/types.ts index 1800fc80..452c7d78 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -23,7 +23,7 @@ export interface DenoConfig { // --- APPLICATION CONFIGURATION --- -export type StartOptions = Partial & FreshOptions; +export type StartOptions = FreshOptions; export interface FreshOptions { build?: { @@ -44,6 +44,60 @@ export interface FreshOptions { plugins?: Plugin[]; staticDir?: string; router?: RouterOptions; + server?: Partial; + + // 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; + + /** + * 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; } export interface RouterOptions { diff --git a/tests/fixture_config/deno.json b/tests/fixture_config/deno.json new file mode 100644 index 00000000..9d782057 --- /dev/null +++ b/tests/fixture_config/deno.json @@ -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" + } +} diff --git a/tests/fixture_config/dev.ts b/tests/fixture_config/dev.ts new file mode 100755 index 00000000..59dfdfde --- /dev/null +++ b/tests/fixture_config/dev.ts @@ -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, +}); diff --git a/tests/fixture_config/fresh.gen.ts b/tests/fixture_config/fresh.gen.ts new file mode 100644 index 00000000..b321c0e0 --- /dev/null +++ b/tests/fixture_config/fresh.gen.ts @@ -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; diff --git a/tests/fixture_config/main.ts b/tests/fixture_config/main.ts new file mode 100644 index 00000000..0df18bfc --- /dev/null +++ b/tests/fixture_config/main.ts @@ -0,0 +1,25 @@ +/// +/// +/// +/// +/// + +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, +}); diff --git a/tests/fixture_config/routes/index.tsx b/tests/fixture_config/routes/index.tsx new file mode 100644 index 00000000..9025646d --- /dev/null +++ b/tests/fixture_config/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Home() { + return

hello

; +} diff --git a/tests/main_test.ts b/tests/main_test.ts index b29fefae..21fc8464 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -5,6 +5,7 @@ import { assertMatch, assertStringIncludes, delay, + join, puppeteer, retry, } from "./deps.ts"; @@ -971,3 +972,97 @@ Deno.test("De-duplicates nodes by key", async () => { assert(//.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 */ } + } + }); +});