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

Long live to Hono! 🔥 #723

Merged
merged 48 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
cb2b97e
Removing fresh
mcandeia Jul 23, 2024
283b0c4
Set head as null
mcandeia Jul 23, 2024
5d6d8d6
Only render if head is present
mcandeia Jul 24, 2024
a721207
Create new deco middleware
mcandeia Jul 24, 2024
1ff6074
Refactoring to use hono middlewares
mcandeia Jul 25, 2024
2404937
Rebase with main
mcandeia Jul 25, 2024
d6b9c0a
Duplicate fresh middlewares migrating to hono
mcandeia Jul 25, 2024
dee4d51
Add create handler
mcandeia Jul 25, 2024
f8b6bb4
Rename routing to hono
mcandeia Jul 25, 2024
6d6215e
Add setup hono app
mcandeia Jul 25, 2024
f5ccb37
Rename to setup
mcandeia Jul 25, 2024
d3e231d
Rename app to setup.tsx
mcandeia Jul 25, 2024
be2358d
Add dev server with camudo
mcandeia Jul 25, 2024
89ac7e0
Extract dev server path
mcandeia Jul 25, 2024
ce1f11d
Use own reference of pageprops
mcandeia Jul 25, 2024
f561eee
Rollback fresh middlewares to old state
mcandeia Jul 25, 2024
fd784a0
Mocked tailwind style and Head
mcandeia Jul 26, 2024
2c3d99a
Add tailwind script
mcandeia Jul 26, 2024
e26f4bc
Check partial mode null coalescing
mcandeia Jul 26, 2024
fe46e43
Missing htmx bindings and renderer func
mcandeia Jul 31, 2024
e9b7911
Added HTMX bindings and Renderer func
mcandeia Aug 1, 2024
474754b
Added server option to hono
mcandeia Aug 1, 2024
5a8ad7f
Renderer improvements
mcandeia Aug 1, 2024
38ff1e8
Remove fresh and build render func
mcandeia Aug 1, 2024
3a1433e
Rebase with main
mcandeia Aug 1, 2024
7ce1db4
Add cacheFrom vary comment
mcandeia Aug 1, 2024
5720b57
Add serve static files
mcandeia Aug 2, 2024
dd38d5c
Fix block preview route
mcandeia Aug 2, 2024
1e2464e
Improve schema generation with hmr hooks
mcandeia Aug 2, 2024
d65d8e3
Defaulting to site namespace
mcandeia Aug 2, 2024
ead2b68
Always use dev mod on css
mcandeia Aug 2, 2024
226a76b
Fix head usage on json viewer
mcandeia Aug 2, 2024
9125b50
Idiomatic schema generation
mcandeia Aug 3, 2024
ccaf60e
Update hono and migrate fresh plugin
mcandeia Aug 3, 2024
120b4eb
Remove fresh routes and middlewares
mcandeia Aug 3, 2024
e1798f1
Rename bindings to be more concise
mcandeia Aug 4, 2024
711a812
Check for env before assigning the new render func
mcandeia Aug 4, 2024
9e82fcf
rebase with main
mcandeia Aug 5, 2024
d2dd2a5
Remove any absolute import using deco
mcandeia Aug 5, 2024
4351e31
Do not use head for livecontrols
mcandeia Aug 5, 2024
3e28312
Remove IS_BROWSER usage
mcandeia Aug 5, 2024
1701f3c
put back deco alias
mcandeia Aug 6, 2024
4858970
Incorporate form data changes into invoke
mcandeia Aug 6, 2024
ed268b1
Allow passing site and namespace as init parameter
mcandeia Aug 6, 2024
e85c20f
Use std path mod instead of join.ts
mcandeia Aug 6, 2024
f6ab5f6
Support fresh's head when rendering to string
mcandeia Aug 6, 2024
c3017fd
Do not set ctx.res again on observability middleware
mcandeia Aug 6, 2024
cdaff37
Rename app.ts => mod.ts
mcandeia Aug 6, 2024
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
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
Loading