From 3bce2fb8d367666d191f2b9c460e2802bdcbf053 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 30 Jul 2021 14:09:04 +0200 Subject: [PATCH] feat: deployctl is not needed anymore (#45) Deno Deploy now supports Deno.listen and Deno.serveHttp. --- .vscode/settings.json | 2 +- README.md | 4 ++-- cli.ts | 2 +- docs/getting-started.md | 10 +++------- docs/project-structure.md | 2 +- example/README.md | 8 +------- example/tsconfig.json | 1 + makefile | 2 +- src/cli/init.ts | 9 ++------- src/server/constants.ts | 6 ++++++ src/server/context.ts | 3 ++- src/server/deps.ts | 2 +- src/server/mod.ts | 22 +++++++++++++++++++--- tests/fixture/README.md | 8 +------- 14 files changed, 42 insertions(+), 39 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3f90500c..33929ca6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "deno.enable": true, "deno.lint": true, - "deno.unstable": false, + "deno.unstable": true, "deno.config": "./tsconfig.json" } diff --git a/README.md b/README.md index 8dfa899e..cb981bd5 100644 --- a/README.md +++ b/README.md @@ -43,10 +43,10 @@ The `fresh` CLI can scaffold a new project for you. To scaffold a project in the fresh init myproject ``` -To now start the project, use [`deployctl`](https://deno.land/x/deploy): +To now start the project, call `deno run`: ``` -deployctl run --no-check --watch main.ts +deno run -A --unstable --config tsconfig.json --watch main.ts ``` To deploy the script to [Deno Deploy](https://deno.com/deploy), push your diff --git a/cli.ts b/cli.ts index e88457d1..fd7a4538 100644 --- a/cli.ts +++ b/cli.ts @@ -39,7 +39,7 @@ switch (subcommand) { break; default: if (args.version) { - console.log(`deployctl ${VERSION}`); + console.log(`fresh ${VERSION}`); Deno.exit(0); } if (args.help) { diff --git a/docs/getting-started.md b/docs/getting-started.md index 710fe814..3477d986 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -4,15 +4,11 @@ Welcome to the `fresh` documentation. ## Requirements -The documentation assumes you have Deno 1.9.2 or later, and `deployctl` 0.3.0 -installed. +The documentation assumes you have Deno 1.12.0 or later installed. To install Deno, follow the installation instructions in the manual: https://deno.land/manual/getting_started/installation -To install `deployctl`, follow the installation instructions here: -https://deno.land/x/deploy#install - ## Setup `fresh` comes with a CLI tool, conveniently also called `fresh`. It is used to @@ -46,10 +42,10 @@ To learn more about how projects are structured, take a look at the ## Running the project locally -You can now run your new project locally with `deployctl`: +You can now run your new project locally using the `deno` CLI: ``` -deployctl run --watch --no-check ./main.ts +deno run -A --unstable --config tsconfig.json --watch main.ts ``` The `--watch` option will cause your script to be reloaded on any changes to the diff --git a/docs/project-structure.md b/docs/project-structure.md index 7d11b6e0..eac56e12 100644 --- a/docs/project-structure.md +++ b/docs/project-structure.md @@ -5,7 +5,7 @@ important files are `main.ts`, `routes.gen.ts`, and the pages and api routes in the `pages` subfolder. The `main.ts` file is the entrypoint to your project. It is the file you link in -Deno Deploy, or run locally with `deployctl`. +Deno Deploy, or run locally with the `deno` CLI. The `routes.gen.ts` file is a manifest of all pages and api routes in a project. When moving around or creating new pages, this file needs to be updated. This is diff --git a/example/README.md b/example/README.md index 5ace786b..e8d3e69a 100644 --- a/example/README.md +++ b/example/README.md @@ -2,16 +2,10 @@ ### Usage -Install deployctl: - -``` -deno install --allow-read --allow-write --allow-env --allow-net --allow-run --no-check -r -f https://deno.land/x/deploy/deployctl.ts -``` - Start the project: ``` -deployctl run --no-check --watch main.ts +deno run -A --unstable --config tsconfig.json --watch main.ts ``` After adding, removing, or moving a page in the `pages` directory, run: diff --git a/example/tsconfig.json b/example/tsconfig.json index 798d070e..c68288d3 100644 --- a/example/tsconfig.json +++ b/example/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "lib": ["dom", "deno.ns", "deno.unstable"], "jsxFactory": "h", "jsxFragmentFactory": "Fragment" } diff --git a/makefile b/makefile index 815327f2..2bfa0e34 100644 --- a/makefile +++ b/makefile @@ -8,4 +8,4 @@ test: deno test --no-check -A --config tsconfig.json watch_example: - deployctl run --watch --no-check example/main.ts \ No newline at end of file + deno run -A --unstable --config example/tsconfig.json --watch example/main.ts \ No newline at end of file diff --git a/src/cli/init.ts b/src/cli/init.ts index 6ac4c375..0a348745 100644 --- a/src/cli/init.ts +++ b/src/cli/init.ts @@ -137,6 +137,7 @@ export default (_req: Request): Response => { const TSCONFIG_JSON = JSON.stringify( { "compilerOptions": { + "lib": ["dom", "deno.ns", "deno.unstable"], "jsxFactory": "h", "jsxFragmentFactory": "Fragment", }, @@ -162,16 +163,10 @@ start(routes); ### Usage -Install deployctl: - -\`\`\` -deno install --allow-read --allow-write --allow-env --allow-net --allow-run --no-check -r -f https://deno.land/x/deploy/deployctl.ts -\`\`\` - Start the project: \`\`\` -deployctl run --no-check --watch main.ts +deno run -A --unstable --config tsconfig.json --watch main.ts \`\`\` After adding, removing, or moving a page in the \`pages\` directory, run: diff --git a/src/server/constants.ts b/src/server/constants.ts index 5c64a577..6ff21a7c 100644 --- a/src/server/constants.ts +++ b/src/server/constants.ts @@ -2,3 +2,9 @@ export const INTERNAL_PREFIX = "/_frsh"; export const BUILD_ID = Deno.env.get("DENO_DEPLOYMENT_ID") || crypto.randomUUID(); export const JS_PREFIX = `/js`; + +declare global { + interface Crypto { + randomUUID(): string; + } +} diff --git a/src/server/context.ts b/src/server/context.ts index 5b0a41da..e303b355 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -50,7 +50,8 @@ export class ServerContext { if (path.startsWith("/api/")) { const handlers = Object.fromEntries( Object.entries(module as ApiRouteModule).filter(([method]) => - method === "default" || router.METHODS.includes(method) + method === "default" || + (router.METHODS as readonly string[]).includes(method) ), ); const apiRoute: ApiRoute = { diff --git a/src/server/deps.ts b/src/server/deps.ts index 71e26160..4332d84e 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -14,4 +14,4 @@ export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.9. // @deno-types="https://unpkg.com/esbuild-wasm@0.11.19/esm/browser.d.ts" import * as esbuild from "https://gist.githubusercontent.com/lucacasonato/358c6b7e8198bfb2cf3d220e49fdcf5f/raw/3714cb0f59606eefc29ed0fea36d4cd93549938b/esbuild-wasm.js"; export { esbuild }; -export { denoPlugin } from "https://deno.land/x/esbuild_deno_loader@0.2.0/mod.ts"; +export { denoPlugin } from "https://deno.land/x/esbuild_deno_loader@0.3.0/mod.ts"; diff --git a/src/server/mod.ts b/src/server/mod.ts index 2d7ce3b9..3992225c 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -14,7 +14,23 @@ export { router, ServerContext }; export function start(routes: Routes) { const ctx = ServerContext.fromRoutes(routes); const app = router.router(ctx.routes()); - addEventListener("fetch", (event: FetchEvent) => { - event.respondWith(app(event.request)); - }); + serve(app); +} + +async function serve(handler: (req: Request) => Response | Promise) { + async function handleConn(conn: Deno.Conn) { + const httpConn = Deno.serveHttp(conn); + for await (const e of httpConn) { + e.respondWith(handler(e.request)) + .catch((e) => console.error("Failed handing a request:", e)); + } + } + + const listener = Deno.listen({ port: 8000 }); + const addr = listener.addr as Deno.NetAddr; + console.log(`Listening on http://${addr.hostname}:${addr.port}`); + for await (const conn of listener) { + handleConn(conn) + .catch((e) => console.error("Failed serving a connection:", e)); + } } diff --git a/tests/fixture/README.md b/tests/fixture/README.md index 5ace786b..e8d3e69a 100644 --- a/tests/fixture/README.md +++ b/tests/fixture/README.md @@ -2,16 +2,10 @@ ### Usage -Install deployctl: - -``` -deno install --allow-read --allow-write --allow-env --allow-net --allow-run --no-check -r -f https://deno.land/x/deploy/deployctl.ts -``` - Start the project: ``` -deployctl run --no-check --watch main.ts +deno run -A --unstable --config tsconfig.json --watch main.ts ``` After adding, removing, or moving a page in the `pages` directory, run: