From 6f3451354891de616de9b6b85bc364b1c037d3e8 Mon Sep 17 00:00:00 2001 From: Ian Bull Date: Tue, 5 Nov 2024 00:48:31 -0800 Subject: [PATCH] refactor: update std 1.0 (#35) --- src/index.ts | 5 ++--- src/server.ts | 24 ++++++++---------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/index.ts b/src/index.ts index d967e54..93a05f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ const SHIM = `globalThis.process = { env: Deno.env.toObject(), };`; -const STD_VERSION = `0.224.0`; +const STD_VERSION = `1.0`; // REF: https://github.com/denoland/deno/tree/main/ext/node/polyfills const COMPATIBLE_NODE_MODULES = [ "assert", @@ -71,8 +71,7 @@ const COMPATIBLE_NODE_MODULES = [ // replaced with the Deno-specific contents listed below. const DENO_IMPORTS_SHIM = `@deno/astro-adapter/__deno_imports.ts`; const DENO_IMPORTS = - `export { Server } from "jsr:@std/http@${STD_VERSION}/server"; -export { serveFile } from "jsr:@std/http@${STD_VERSION}/file-server"; + `export { serveFile } from "jsr:@std/http@${STD_VERSION}/file-server"; export { fromFileUrl } from "jsr:@std/path@${STD_VERSION}";`; export function getAdapter(args?: Options): AstroAdapter { diff --git a/src/server.ts b/src/server.ts index a310f95..430198b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,11 +4,7 @@ import { App } from "astro/app"; import type { Options } from "./types"; // @ts-expect-error -import { - fromFileUrl, - serveFile, - Server, -} from "@deno/astro-adapter/__deno_imports.ts"; +import { fromFileUrl, serveFile } from "@deno/astro-adapter/__deno_imports.ts"; let _server: Server | undefined = undefined; let _startPromise: Promise | undefined = undefined; @@ -35,10 +31,10 @@ export function start(manifest: SSRManifest, options: Options) { const clientRoot = new URL("../client/", import.meta.url); const app = new App(manifest); - const handler = async (request: Request, connInfo: any) => { + const handler = async (request: Request, handlerInfo: any) => { if (app.match(request)) { - let ip = connInfo?.remoteAddr?.hostname; - Reflect.set(request, Symbol.for("astro.clientAddress"), ip); + const hostname = handlerInfo.remoteAddr?.hostname; + Reflect.set(request, Symbol.for("astro.clientAddress"), hostname); const response = await app.render(request); if (app.setCookieHeaders) { for (const setCookieHeader of app.setCookieHeaders(response)) { @@ -89,13 +85,9 @@ export function start(manifest: SSRManifest, options: Options) { }; const port = options.port ?? 8085; - _server = new Server({ - port, - hostname: options.hostname ?? "0.0.0.0", - handler, - }); - - _startPromise = Promise.resolve(_server.listenAndServe()); + const hostname = options.hostname ?? "0.0.0.0"; + _server = Deno.serve({ port, hostname }, handler); + _startPromise = _server.finished; console.error(`Server running on port ${port}`); } @@ -104,7 +96,7 @@ export function createExports(manifest: SSRManifest, options: Options) { return { async stop() { if (_server) { - _server.close(); + _server.shutdown(); _server = undefined; } await Promise.resolve(_startPromise);