Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update std 1.0 #35

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading