feat: update for Deno 2 (#2655)

Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
Marvin Hagemeister
2024-09-30 18:30:30 +02:00
committed by GitHub
co-authored by Luca Casonato
parent b5389f69a3
commit de8f1b0c59
32 changed files with 3836 additions and 3194 deletions
+23 -9
View File
@@ -35,9 +35,15 @@ const DEFAULT_NOT_ALLOWED_METHOD = () => {
throw new HttpError(405);
};
export type ListenOptions = Partial<Deno.ServeTlsOptions> & {
remoteAddress?: string;
};
export type ListenOptions =
& Partial<
Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem
>
& {
remoteAddress?: string;
};
Deno.serve;
export let getRouter: <State>(app: App<State>) => Router<MiddlewareFn<State>>;
// deno-lint-ignore no-explicit-any
@@ -186,8 +192,7 @@ export class App<State> {
return async (
req: Request,
conn: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo =
DEFAULT_CONN_INFO,
conn: Deno.ServeHandlerInfo = DEFAULT_CONN_INFO,
) => {
const url = new URL(req.url);
// Prevent open redirect attacks
@@ -237,11 +242,20 @@ export class App<State> {
if (!options.onListen) {
options.onListen = (params) => {
const pathname = (this.config.basePath) + "/";
const protocol = options.key && options.cert ? "https:" : "http:";
const protocol = "key" in options && options.key && options.cert
? "https:"
: "http:";
let hostname = params.hostname;
// Windows being windows...
if (
Deno.build.os === "windows" &&
(hostname === "0.0.0.0" || hostname === "::")
) {
hostname = "localhost";
}
// Work around https://github.com/denoland/deno/issues/23650
const hostname = params.hostname.startsWith("::")
? `[${params.hostname}]`
: params.hostname;
hostname = hostname.startsWith("::") ? `[${hostname}]` : hostname;
const address = colors.cyan(
`${protocol}//${hostname}:${params.port}${pathname}`,
);
+3 -3
View File
@@ -42,7 +42,7 @@ export interface FreshContext<State = unknown> {
readonly url: URL;
readonly params: Record<string, string>;
readonly error: unknown;
readonly info: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo;
readonly info: Deno.ServeHandlerInfo;
/**
* Return a redirect response to the specified path. This is the
* preferred way to do redirects in Fresh.
@@ -95,7 +95,7 @@ export class FreshReqContext<State>
state: State = {} as State;
data: unknown = undefined;
error: unknown | null = null;
info: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo;
info: Deno.ServeHandlerInfo | Deno.ServeHandlerInfo;
next: FreshContext<State>["next"];
@@ -112,7 +112,7 @@ export class FreshReqContext<State>
constructor(
req: Request,
url: URL,
info: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo,
info: Deno.ServeHandlerInfo,
params: Record<string, string>,
config: ResolvedFreshConfig,
next: FreshContext<State>["next"],
+6
View File
@@ -40,6 +40,8 @@ Deno.test({
expect(logs).toEqual(["A", "B", "C"]);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
@@ -64,6 +66,8 @@ Deno.test({
);
expect(css).toContain('body { background: url("/foo.jpg?__frsh_c=');
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
@@ -87,4 +91,6 @@ Deno.test({
);
expect(code).toContain('"remote-island"');
},
sanitizeOps: false,
sanitizeResources: false,
});
+6 -15
View File
@@ -131,20 +131,6 @@ export async function bundleJs(
}
if (!options.dev) {
// deno-lint-ignore no-deprecated-deno-api
const children = Object.values(Deno.resources())
.filter((name) => name === "child").length;
await esbuild!.stop();
// To work around a bug in ESBuild, where it doesn't wait for the child
// process to shut down before exiting, we manually wait for the process to
// exit before continuing.
while (
// deno-lint-ignore no-deprecated-deno-api
Object.values(Deno.resources())
.filter((name) => name === "child").length >= children
) {
await new Promise((r) => setTimeout(r, 100));
}
esbuild = null;
}
@@ -155,12 +141,17 @@ export async function bundleJs(
};
}
let initialized = false;
export async function startEsbuild() {
esbuild = Deno.env.get("FRESH_ESBUILD_LOADER") === "portable"
? await import("esbuild-wasm")
: await import("esbuild");
await esbuild.initialize({});
if (!initialized) {
await esbuild.initialize({});
initialized = true;
}
}
function buildIdPlugin(buildId: string): EsbuildPlugin {
+2 -1
View File
@@ -105,10 +105,11 @@ export async function updateCheck(
checkFile.latest_version = await getLatestVersion();
checkFile.last_checked = new Date().toISOString();
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
// Update check is optional and shouldn't abort the program.
// deno-lint-ignore no-console
console.error(
colors.red(` Update check failed: `) + err.message,
colors.red(` Update check failed: `) + message,
);
return;
}
+6 -4
View File
@@ -15,6 +15,11 @@ export type AsyncAnyComponent<P> = {
defaultProps?: Partial<P> | undefined;
};
// deno-lint-ignore no-explicit-any
export function isAsyncAnyComponent(fn: any): fn is AsyncAnyComponent<any> {
return typeof fn === "function" && fn.constructor.name === "AsyncFunction";
}
export function renderMiddleware<State>(
components: Array<
| AnyComponent<PageProps<unknown, State>>
@@ -50,10 +55,7 @@ export function renderMiddleware<State>(
const fn = components[i];
if (
typeof fn === "function" &&
fn.constructor.name === "AsyncFunction"
) {
if (isAsyncAnyComponent(fn)) {
const result = (await fn(props)) as VNode | Response;
if (result instanceof Response) {
return result;
+1 -1
View File
@@ -60,7 +60,7 @@ export const ACTIVE_PARTIALS = new Map<string, PartialComp>();
export class PartialComp extends Component<
{ children?: ComponentChildren; mode: PartialMode; name: string }
> {
componentDidMount() {
override componentDidMount() {
ACTIVE_PARTIALS.set(this.props.name, this);
}