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

Migrating to Events API #73

Open
5 of 7 tasks
pi0 opened this issue Mar 23, 2022 · 7 comments
Open
5 of 7 tasks

Migrating to Events API #73

pi0 opened this issue Mar 23, 2022 · 7 comments
Labels
enhancement New feature or request

Comments

@pi0
Copy link
Member

pi0 commented Mar 23, 2022

h3 was born with the idea of being a platform-agnostic server framework for all JavaScript environments and backward compatibility with legacy server middleware format for Node.js and Express compatibility.

Initially, we started with the most backward-compatible format (req, res) and improved it by async support async (req, res) and also directly returning a response for a pleasant experience with (req, res) => 'Hello World' format.

unjs/unenv provides a createCall and mocks of Node.js HTTP built-ins. Thanks to this mocking framework, h3 is able to work nicely on almost any serverless or Worker platform.

However, this kind of setup keeps h3 to be away from natively supporting non Node.js environments with the cost of unenv overhead for any code not using Node.js.

Additionally, (req, res) format is not even suitable to extend h3 supporting Node.js features like http2 without compatibility API and ws support.

Migration to (event) format will be progressive and backward compatibility wherever necessary however we potentially have to introduce breaking changes as semver-minor until reaching h3@1.0.0.

Progress

@PhaxeNor
Copy link

Would this potentially add support for waitUntil in the fetch listener? So it could be utilized with Cloudflare Workers lifecycle for instance. (https://developer.mozilla.org/en-US/docs/Web/API/ExtendableEvent/waitUntil)

@nathanchase
Copy link

So what's the roadblock to get h3 (and Nuxt 3, as a result) to serve files via http2 protocol?

@Hebilicious
Copy link
Member

Hebilicious commented Apr 27, 2023

I manually implemented something like this to play with Request/Response compatible libraries :

const getRequestFromEvent = async (event: H3Event) => {
  const url = new URL(getRequestURL(event))
  const method = getMethod(event)
  const body = method === "POST" ? await readRawBody(event) : undefined
  return new Request(url, { headers: makeHeaders(getRequestHeaders(event)), method, body })
}

A much cleaner and robust version of these would be greatly appreciated.

Something like this ...

const sendResponse = (event: H3Event, response: Response): Response => {
//implement, maybe this doesn't need to return a Response
}

... might be useful too.

For reference hattip seems to share similar design goals with h3, but is using a different API based on standard Request/Response.

@pi0
Copy link
Member Author

pi0 commented Jun 20, 2023

Linking amazing PR: #395 for reference

@alimozdemir
Copy link

Hello,

Is there any estimated date for completing this functionality?

@joshmossas
Copy link
Contributor

joshmossas commented Dec 2, 2023

If anyone is trying to get http2 to work I was able to get up and running with node-spdy.

Here's some example code:

import { readFileSync } from 'fs';
import { createApp, eventHandler, toNodeListener } from 'h3';
import spdy from 'spdy';

const app = createApp();
app.use(
    '/',
    eventHandler(() => {
        return 'Hello world';
    }),
);

const options: spdy.ServerOptions = {
    cert: readFileSync('./<path-to-certificate>'),
    key: readFileSync('./<path-to-key>'),
};

const server = spdy.createServer(options, toNodeListener(app));

server.listen(3000, () => console.log('Server running on localhost:3000'));

If you need unencrypted traffic, (which is what I needed to get this running on Google Cloud Run) then you set the server options like so:

const options: spdy.ServerOptions = {
    spdy: {
        plain: true,
    },
};

Anyway hopefully this saves someone else the hours of trial and error I just went through 😅

@bao-tran-saltovn
Copy link

So when will we have http2 support on Nuxt 3. I am able to achieve http2 on my project by using node-spdy like joshmosas mention but this repository is not undergoing active development.

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

7 participants