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

Static middleware. For serving static assets. #59

Open
brillout opened this issue Apr 2, 2023 · 8 comments
Open

Static middleware. For serving static assets. #59

brillout opened this issue Apr 2, 2023 · 8 comments
Labels
enhancement New feature or request

Comments

@brillout
Copy link
Member

brillout commented Apr 2, 2023

In principle, it should be possible to implement a static middleware @hattip/static that works everywhere.

Although I'm not sure how much effort it would be to support each platform. E.g. for Cloudflare Workers we need to use its KV store. If each platform has its own approach, @hattip/static may become hard to maintain. (We can support only a couple of providers while, in the long term, asking others to abide to some kind of shared convention.)

@brillout brillout added the enhancement New feature or request label Apr 2, 2023
@cyco130
Copy link
Member

cyco130 commented Apr 2, 2023

Definitely on the roadmap.

Relevant discussion: #45

@brillout
Copy link
Member Author

brillout commented Apr 5, 2023

Most(/all?) deployment providers support a static directory configuration. That could do the trick in the meantime. Although this means that HatTip would need a static config — not sure where/how this would fit with HatTip's current flexible architecture.

On a tangent: maybe we can, for example, make wrangler.toml optional.

@masx200
Copy link

masx200 commented May 30, 2023

https://deno.land/std@0.190.0/http/file_server.ts?source

This solution can be used on deno.

@masx200
Copy link

masx200 commented May 30, 2023

Can we design an adapter from Koa middleware to Hattip middleware?

We can design to put "http. RequestListener" is converted into hattip middleware.

@cyco130
Copy link
Member

cyco130 commented May 30, 2023

Can we design an adapter from Koa middleware to Hattip middleware?

We can design to put "http. RequestListener" is converted into hattip middleware.

I think it's possible. And I think Nitro is doing something like that. But I don't think it would be efficient. Besides, Koa middleware probably use other Node stuff that will require polyfills.

Koa has a good selection of well written, modular middlewares. But I think it would be much cleaner if we ported them (like we did in the CORS middleware) instead of trying to adapt them.

@cyco130
Copy link
Member

cyco130 commented May 30, 2023

https://deno.land/std@0.190.0/http/file_server.ts?source

This solution can be used on deno.

Current state for Rakkas:

  • For Node, it uses sirv (because vavite already comes with it)
  • For Deno, it uses http/file_server from std
  • For Bun, it has a (not so great) custom server
  • For other targets, each target has their own solution as part of the platform

@masx200
Copy link

masx200 commented Jun 2, 2023

You can wrap the koa or express library into a middleware and implement it by opening an HTTP server, which is very simple.

import { Middleware, NextFunction, RetHandler } from "../src/Middleware.ts";

import { Context } from "../mod.ts";
import { RequestListener } from "./RequestListener.ts";
import { createServer } from "node:http";

export function requestListenerToMiddleWare(
    requestListener: RequestListener,
): [Middleware, () => Promise<void>, () => void] {
    //@ts-ignore
    const server = createServer(requestListener);
    const host = `127.${Math.floor(Math.random() * 253 + 1)}.${
        Math.floor(
            Math.random() * 253 + 1,
        )
    }.${Math.floor(Math.random() * 253 + 1)}`;
    const port = Math.floor(Math.random() * 55535 + 10000);
    return [
        async (context: Context, next: NextFunction): Promise<RetHandler> => {
            //@ts-ignore

            const origin = `http://${host}:${port}`;

            const urlobj = new URL(context.request.url);
          //  urlobj.origin;
            const response = await fetch(
                origin + urlobj.href.slice(urlobj.origin.length),
                {
                    ...context.request,
                    //@ts-ignore

                    duplex: "half",
                },
            );
            if (response.status === 404) return next();

            return response;
        },
        async () =>
            new Promise((s) => {
                server.listen(port, host, () => {
                    console.log(
                        `http server listening host:${host} port:` + port,
                    );

                    s();
                });
            }),
        () =>
            server.close(() => {
                console.log(`http server closed host:${host} port:` + port);
            }),
    ];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants