Skip to content

Commit

Permalink
refactor: update std 1.0 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
irbull authored Nov 5, 2024
1 parent f8d18fd commit 6f34513
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 8 additions & 16 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> | undefined = undefined;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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}`);
}

Expand All @@ -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);
Expand Down

0 comments on commit 6f34513

Please sign in to comment.