Skip to content

Commit

Permalink
Change the function serve signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydenseric committed Jan 23, 2025
1 parent 48f9cdc commit 2dc1a4d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
instead of `application/javascript`.
- Updated the function `serve` that creates the Ruck app HTTP server:
- Migrated from the deprecated Deno standard library function to `Deno.serve`.
- The option `port` is now optional and defaults to `0`. Set `0` to listen on
any available port (later get the listening port via the resolved HTTP
server property `addr.port`).
- It now resolves the created Deno HTTP server instance, of type
`Deno.HttpServer<Deno.NetAddr>`. To migrate:
`Deno.HttpServer<Deno.NetAddr>`.

To migrate:

```diff
const abortController = new AbortController();
Expand Down
1 change: 0 additions & 1 deletion importMap.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"@std/http/": "jsr:/@std/http@^1.0.12/",
"@std/media-types/": "jsr:/@std/media-types@^1.1.0/",
"@std/path/": "jsr:/@std/path@^1.0.8/",
"free_port/": "https://deno.land/x/[email protected]/",
"graphql-react/": "https://unpkg.com/[email protected]/",
"react": "https://esm.sh/[email protected]?dev",
"react-dom/client": "https://esm.sh/[email protected]/client?dev",
Expand Down
6 changes: 4 additions & 2 deletions serve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import TransferContext from "./TransferContext.mjs";
* `public` directory in the CWD.
* @param {HtmlComponent} [options.htmlComponent] React component that renders
* the HTML for Ruck app page responses. Defaults to {@linkcode Html}.
* @param {number} options.port Port to serve on.
* @param {number} [options.port] Port to serve on. Set `0` to listen on any
* available port (later get the listening port via the resolved HTTP server
* property `addr.port`). Defaults to `0`.
* @param {AbortSignal} [options.signal] Abort controller signal to close the
* server.
* @returns {Promise<Deno.HttpServer<Deno.NetAddr>>} Resolves the listening HTTP
Expand All @@ -44,7 +46,7 @@ export default async function serve({
clientImportMap,
publicDir = new URL("public/", toFileUrl(Deno.cwd() + "/")),
htmlComponent = Html,
port,
port = 0,
signal,
}) {
if (
Expand Down
6 changes: 0 additions & 6 deletions serve.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Deno.test("`serve` with option `clientImportMap` not an import map object or `UR
serve({
// @ts-expect-error Testing invalid.
clientImportMap: true,
port: 3000,
}),
TypeError,
"Option `clientImportMap` must be an import map object or `URL` instance.",
Expand All @@ -25,7 +24,6 @@ Deno.test("`serve` with option `clientImportMap` an invalid import map object.",
// @ts-expect-error Testing invalid.
imports: true,
},
port: 3000,
}),
TypeError,
"Option `clientImportMap` must be an import map object.",
Expand All @@ -39,7 +37,6 @@ Deno.test("`serve` with option `publicDir` not a `URL` instance.", async () => {
clientImportMap: new URL("./importMap.json", import.meta.url),
// @ts-expect-error Testing invalid.
publicDir: true,
port: 3000,
}),
TypeError,
"Option `publicDir` must be a `URL` instance.",
Expand All @@ -54,7 +51,6 @@ Deno.test(
serve({
clientImportMap: new URL("./importMap.json", import.meta.url),
publicDir: new URL(import.meta.url),
port: 3000,
}),
TypeError,
"Option `publicDir` must be a URL ending with `/`.",
Expand All @@ -67,7 +63,6 @@ Deno.test("`serve` with option `htmlComponent` not a function.", async () => {
() =>
serve({
clientImportMap: new URL("./importMap.json", import.meta.url),
port: 3000,
// @ts-expect-error Testing invalid.
htmlComponent: true,
}),
Expand Down Expand Up @@ -96,7 +91,6 @@ Deno.test(
() =>
serve({
clientImportMap: new URL("./importMap.json", import.meta.url),
port: 3000,
// @ts-expect-error Testing invalid.
signal: true,
}),
Expand Down
7 changes: 2 additions & 5 deletions useOnClickRouteLink.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import { launch } from "@astral/astral";
import { assert } from "@std/assert/assert";
import { assertStrictEquals } from "@std/assert/strict-equals";
import { getFreePort } from "free_port/mod.ts";
import { createElement as h } from "react";
import { act, create } from "react-test-renderer";

Expand Down Expand Up @@ -94,14 +93,12 @@ Deno.test.ignore("`useOnClickRouteLink` in a DOM environment.", async () => {

clientImportMap.imports["ruck/"] = projectFilesOriginUrl.href;

const ruckAppPort = await getFreePort(3000);
const ruckServer = await serve({
clientImportMap,
publicDir: new URL(
"test/fixtures/useOnClickRouteLink/ruck-project/public/",
import.meta.url,
),
port: ruckAppPort,
signal: abortController.signal,
});

Expand All @@ -114,8 +111,8 @@ Deno.test.ignore("`useOnClickRouteLink` in a DOM environment.", async () => {
browser,
projectFilesOriginUrl,
async (page) => {
const urlPageA = `http://localhost:${ruckAppPort}/`;
const urlPageB = `http://localhost:${ruckAppPort}/b`;
const urlPageA = `http://localhost:${ruckServer.addr.port}/`;
const urlPageB = `http://localhost:${ruckServer.addr.port}/b`;

await page.goto(urlPageA);

Expand Down

0 comments on commit 2dc1a4d

Please sign in to comment.