Skip to content

Commit

Permalink
Long live to Hono! 🔥 (#723)
Browse files Browse the repository at this point in the history
* Removing fresh

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Set head as null

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Only render if head is present

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Create new deco middleware

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Refactoring to use hono middlewares

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Rebase with main

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Duplicate fresh middlewares migrating to hono

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Add create handler

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Rename routing to hono

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Add setup hono app

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Rename to setup

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Rename app to setup.tsx

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Add dev server with camudo

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Extract dev server path

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Use own reference of pageprops

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Rollback fresh middlewares to old state

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Mocked tailwind style and Head

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Add tailwind script

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Check partial mode null coalescing

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Missing htmx bindings and renderer func

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Added HTMX bindings and Renderer func

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Added server option to hono

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Renderer improvements

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Remove fresh and build render func

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Rebase with main

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Add cacheFrom vary comment

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Add serve static files

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Fix block preview route

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Improve schema generation with hmr hooks

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Defaulting to site namespace

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Always use dev mod on css

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Fix head usage on json viewer

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Idiomatic schema generation

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Update hono and migrate fresh plugin

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Remove fresh routes and middlewares

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Rename bindings to be more concise

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Check for env before assigning the new render func

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* rebase with main

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Remove any absolute import using deco

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Do not use head for livecontrols

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Remove IS_BROWSER usage

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* put back deco alias

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Incorporate form data changes into invoke

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Allow passing site and namespace as init parameter

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Use std path mod instead of join.ts

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Support fresh's head when rendering to string

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Do not set ctx.res again on observability middleware

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

* Rename app.ts => mod.ts

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>

---------

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
  • Loading branch information
mcandeia authored Aug 6, 2024
1 parent 02f1c28 commit a7ffafa
Show file tree
Hide file tree
Showing 69 changed files with 2,711 additions and 1,850 deletions.
4 changes: 2 additions & 2 deletions blocks/function.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// deno-lint-ignore-file no-explicit-any
import { wrapCaughtErrors } from "../blocks/loader.ts";
import type { HttpContext } from "../blocks/handler.ts";
import { wrapCaughtErrors } from "../blocks/loader.ts";
import {
newSingleFlightGroup,
type SingleFlightKeyFunc,
} from "../blocks/utils.tsx";
import JsonViewer from "../components/JsonViewer.tsx";
import type { HandlerContext } from "../deps.ts";
import type { Block, BlockModule } from "../engine/block.ts";
import type { HandlerContext } from "../engine/manifest/manifest.ts";
import type { DecoState, LoaderFunction } from "../types.ts";

export type Function<TProps = any, TState = any> = LoaderFunction<
Expand Down
4 changes: 2 additions & 2 deletions blocks/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// deno-lint-ignore-file no-explicit-any
import type { Handler as DenoHandler, ServeHandler } from "../deps.ts";
import type { Handler as DenoHandler } from "../deps.ts";
import type { Block, BlockModule, InstanceOf } from "../engine/block.ts";
import type { BaseContext } from "../engine/core/resolver.ts";
import type { PromiseOrValue } from "../engine/core/utils.ts";
Expand Down Expand Up @@ -30,7 +30,7 @@ export type HttpHandler = <State = any, TConfig = any>(
type HandlerFunc<TConfig = any, TState = any> = (
config: TConfig,
ctx: FnContext<TState>,
) => DenoHandler | ServeHandler;
) => DenoHandler;

const handlerBlock: Block<BlockModule<HandlerFunc>> = {
type: "handlers",
Expand Down
1 change: 1 addition & 0 deletions blocks/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const fnContextFromHttpContext = <TState = {}>(
): FnContext<TState> => {
let device: Device | null = null;
let isBot: boolean | null = null;

return {
...ctx?.context?.state?.global,
revision: ctx.revision,
Expand Down
9 changes: 7 additions & 2 deletions clients/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from "../utils/invoke.types.ts";
import type { DotNestedKeys } from "../utils/object.ts";
import type { Fulfilled, Rejected } from "../utils/promise.ts";
import type { invokeKey } from "./withManifest.ts";
import type { invokeKey, InvokerRequestInit } from "./withManifest.ts";

export type InvocationProxyHandler = {
(props?: any, init?: RequestInit | undefined): Promise<any>;
Expand Down Expand Up @@ -86,7 +86,12 @@ export const newHandler = <TManifest extends AppManifest>(
): InvokeAwaiter<TManifest, any, any> {
const ext = part === "x" ? "tsx" : "ts";
return new InvokeAwaiter<TManifest, any, any>(
(payload, init) => invoker(payload.key, payload.props, init),
(payload, init) =>
invoker(
payload.key,
payload.props,
init as InvokerRequestInit,
) as Promise<InvokeResult<Invoke<TManifest, any, any>, TManifest>>,
{
key: `${currentParts.join("/")}.${ext}`,
props,
Expand Down
34 changes: 27 additions & 7 deletions clients/withManifest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// deno-lint-ignore-file no-explicit-any
import { IS_BROWSER } from "$fresh/runtime.ts";
import type { App, AppManifest, ManifestOf } from "../blocks/app.ts";
import type { StreamProps } from "../mod.ts";
import type {
Expand Down Expand Up @@ -88,7 +87,7 @@ const fetchWithProps = async (
props: unknown,
init?: InvokerRequestInit | undefined,
) => {
if (!IS_BROWSER) {
if (typeof document === "undefined") {
console.warn(
"👋 Oops! Runtime.invoke should be called only on the client-side, but it seems to be called on the server-side instead. No worries, mistakes happen! 😉",
);
Expand Down Expand Up @@ -121,7 +120,7 @@ const fetchWithProps = async (
body,
...init,
headers,
});
} as RequestInit);

if (response.status === 204) {
return;
Expand Down Expand Up @@ -265,7 +264,12 @@ export const invoke = <
return invokeKey((payload as any).key, (payload as any).props, {
...init ?? {},
fetcher,
});
}) as Promise<
InvokeResult<
TPayload,
TManifest
>
>;
}
const reqs: Record<
string,
Expand All @@ -278,9 +282,19 @@ export const invoke = <
reqs[key] = val;
}
}
return batchInvoke(reqs, { ...init ?? {}, fetcher });
return batchInvoke(reqs, { ...init ?? {}, fetcher }) as Promise<
InvokeResult<
TPayload,
TManifest
>
>;
}
return batchInvoke(payload, { ...init ?? {}, fetcher });
return batchInvoke(payload, { ...init ?? {}, fetcher }) as Promise<
InvokeResult<
TPayload,
TManifest
>
>;
};

export const create = <
Expand Down Expand Up @@ -308,7 +322,13 @@ export const create = <
TPayload,
TManifest
>
> => invokeKey(key, props, init);
> =>
invokeKey(key, props, init) as Promise<
InvokeResult<
TPayload,
TManifest
>
>;

/**
* Creates a set of strongly-typed utilities to be used across the repositories where pointing to an existing function is supported.
Expand Down
5 changes: 4 additions & 1 deletion components/JsonViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Head } from "../deps.ts";
import { useFramework } from "../components/section.tsx";

export interface Props {
body: string;
Expand Down Expand Up @@ -35,13 +35,16 @@ const snippet = (json: string) => {

node?.addEventListener(
"load",
// deno-lint-ignore no-explicit-any
() => (globalThis.window as any).jQuery("#json-renderer").JSONView(json),
);
};

export default function JsonViewer(p: Props) {
const { Head } = useFramework();
return (
<>
{/** @ts-ignore: could not type it well */}
<Head>
<script
type="module"
Expand Down
30 changes: 14 additions & 16 deletions components/LiveControls.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { context } from "../deco.ts";
import { DomInspectorActivators, Head, inspectVSCode } from "../deps.ts";
import { DomInspectorActivators, inspectVSCode } from "../deps.ts";
import type { Flag, Site } from "../types.ts";

const IS_LOCALHOST = context.deploymentId === undefined;
Expand Down Expand Up @@ -172,21 +172,19 @@ const main = () => {
function LiveControls({ site, page, flags }: Props) {
return (
<>
<Head>
<script
type="application/json"
id="__DECO_STATE"
dangerouslySetInnerHTML={{
__html: JSON.stringify({ page, site, flags }),
}}
/>
<script
type="module"
dangerouslySetInnerHTML={{
__html: `${domInspectorModule}\n(${main})()`,
}}
/>
</Head>
<script
type="application/json"
id="__DECO_STATE"
dangerouslySetInnerHTML={{
__html: JSON.stringify({ page, site, flags }),
}}
/>
<script
type="module"
dangerouslySetInnerHTML={{
__html: `${domInspectorModule}\n(${main})()`,
}}
/>
</>
);
}
Expand Down
23 changes: 19 additions & 4 deletions components/section.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { PartialProps } from "$fresh/src/runtime/Partial.tsx";
import { Component, type ComponentType, createContext } from "preact";
import {
Component,
type ComponentChildren,
type ComponentType,
createContext,
} from "preact";
import { useContext } from "preact/hooks";
import type { HttpContext } from "../blocks/handler.ts";
import type { RequestState } from "../blocks/utils.tsx";
Expand All @@ -18,7 +22,9 @@ export interface SectionContext extends HttpContext<RequestState> {
device: Device;
framework: "fresh" | "htmx";
deploymentId?: string;
// deno-lint-ignore no-explicit-any
FallbackWrapper: ComponentType<any>;
bindings: Framework;
}

export const SectionContext = createContext<SectionContext | undefined>(
Expand Down Expand Up @@ -93,6 +99,7 @@ export class ErrorBoundary extends Component<BoundaryProps, BoundaryState> {
}

export interface Framework {
Head?: (headProps: { children: ComponentChildren }) => null;
Wrapper: ComponentType<
{ id: string; partialMode?: "replace" | "append" | "prepend" }
>;
Expand All @@ -113,6 +120,13 @@ export const bindings = {
htmx: HTMXBindings,
};

export const useFramework = () => {
const {
bindings: mbindings,
} = useContext(SectionContext) ?? { bindings: bindings.fresh };
return mbindings;
};

export const alwaysThrow =
(err: unknown): ComponentFunc => (_props: unknown) => {
throw err;
Expand All @@ -130,7 +144,7 @@ export const withSection = <TProps, TLoaderProps = TProps>(
ctx: HttpContext<
RequestState & {
renderSalt?: string;
partialMode?: PartialProps["mode"];
partialMode?: "replace" | "prepend" | "append";
framework?: "fresh" | "htmx";
}
>,
Expand All @@ -141,7 +155,7 @@ export const withSection = <TProps, TLoaderProps = TProps>(
const renderSaltFromState = ctx.context?.state?.renderSalt;
const frameworkFromState = ctx.context?.state?.framework;
// TODO @gimenes This is a fresh thing only. We need to remove it on other framework bindings
const partialMode = ctx.context.state.partialMode ||
const partialMode = ctx?.context?.state?.partialMode ||
"replace";
const metadata = {
resolveChain: ctx.resolveChain,
Expand Down Expand Up @@ -175,6 +189,7 @@ export const withSection = <TProps, TLoaderProps = TProps>(
deploymentId,
renderSalt,
framework,
bindings: bindings[framework],
FallbackWrapper: ({ children, ...props }) => (
<binding.LoadingFallback id={id} {...props}>
{children}
Expand Down
20 changes: 14 additions & 6 deletions deco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import type { DecofileProvider } from "./engine/decofile/provider.ts";
import type { AppManifest } from "./mod.ts";
import { randId } from "./utils/rand.ts";

export interface DecoRuntimeState {
manifest: AppManifest;
export interface DecoRuntimeState<
TAppManifest extends AppManifest = AppManifest,
> {
manifest: TAppManifest;
// deno-lint-ignore no-explicit-any
resolver: ReleaseResolver<any>;
importMap: ImportMap;
Expand Down Expand Up @@ -45,7 +47,7 @@ export const DaemonMode = {
};

// The global deco context
export interface DecoContext {
export interface DecoContext<TAppManifest extends AppManifest = AppManifest> {
deploymentId: string | undefined;
isDeploy: boolean;
decodMode?: DecodMode;
Expand All @@ -56,7 +58,7 @@ export interface DecoContext {
base?: string;
namespace?: string;
release?: DecofileProvider;
runtime?: Promise<DecoRuntimeState>;
runtime?: Promise<DecoRuntimeState<TAppManifest>>;
instance: InstanceInfo;
request?: RequestContext;
}
Expand All @@ -76,7 +78,8 @@ const getHostingPlatform = (): WellKnownHostingPlatform => {
}
};

const defaultContext: Omit<DecoContext, "schema"> = {
// deno-lint-ignore no-explicit-any
let defaultContext: Omit<DecoContext<any>, "schema"> = {
deploymentId,
siteId: 0,
isDeploy: isDeploy,
Expand All @@ -92,8 +95,13 @@ const defaultContext: Omit<DecoContext, "schema"> = {
const asyncLocalStorage = new AsyncLocalStorage<DecoContext>();

export const Context = {
setDefault: <T extends AppManifest = AppManifest>(
ctx: DecoContext<T>,
): void => {
defaultContext = ctx;
},
// Function to retrieve the active context
active: (): DecoContext => {
active: <T extends AppManifest = AppManifest>(): DecoContext<T> => {
// Retrieve the context associated with the async ID
return asyncLocalStorage.getStore() ?? defaultContext;
},
Expand Down
12 changes: 6 additions & 6 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
}
},
"imports": {
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0?pin=102",
"preact/": "https://esm.sh/preact@10.16.0/",
"preact": "https://esm.sh/preact@10.16.0?pin=102",
"partytown/": "https://deno.land/x/partytown@0.3.0/",
"std/": "https://deno.land/std@0.203.0/",
"deco/": "./",
"$fresh/": "https://denopkg.com/denoland/fresh@1.6.8/",
"deco/": "./"
"partytown/": "https://deno.land/x/partytown@0.3.0/",
"preact": "https://esm.sh/preact@10.16.0?pin=102",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.4.0?pin=102",
"preact/": "https://esm.sh/preact@10.16.0/",
"std/": "https://deno.land/std@0.203.0/"
},
"compilerOptions": {
"jsx": "react-jsx",
Expand Down
17 changes: 0 additions & 17 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
export { Head, IS_BROWSER } from "$fresh/runtime.ts";
export type {
FreshContext,
Handler as FreshHandler,
HandlerContext,
Handlers,
MiddlewareHandler,
MiddlewareHandlerContext,
PageProps,
RouteConfig,
} from "$fresh/server.ts";
export type { ServeHandler } from "$fresh/src/server/deps.ts";
export type {
IslandModule,
MiddlewareModule,
RouteModule,
} from "$fresh/src/server/types.ts";
export { DomInspectorActivators } from "https://deno.land/x/inspect_vscode@0.2.1/inspector.ts";
export * as inspectVSCode from "https://deno.land/x/inspect_vscode@0.2.1/mod.ts";
export * as weakcache from "https://deno.land/x/weakcache@v1.1.4/index.js";
Expand Down
2 changes: 1 addition & 1 deletion engine/decofile/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { context } from "deco/live.ts";
import { fromFileUrl } from "std/path/mod.ts";
import { context } from "../../live.ts";
import { randId as ulid } from "../../utils/rand.ts";
import { assertAllowedAuthority as assertAllowedAuthorityFor } from "../trustedAuthority.ts";
import { newFsProviderFromPath } from "./fs.ts";
Expand Down
4 changes: 2 additions & 2 deletions engine/manifest/fresh.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ResolverMap } from "../../engine/core/resolver.ts";
import type { FreshContext } from "../../engine/manifest/manifest.ts";
import defaultResolvers from "../../engine/manifest/defaults.ts";
import type { RouteContext } from "../../engine/manifest/manifest.ts";

const freshResolvers = {
...defaultResolvers,
render: function render(props, { context: { render } }) {
return render(props);
},
} satisfies ResolverMap<FreshContext>;
} satisfies ResolverMap<RouteContext>;

export default freshResolvers;
Loading

0 comments on commit a7ffafa

Please sign in to comment.