Hono on Azure Functions

marplex

marplex

Posted on May 8, 2024

Hono on Azure Functions

hono-azurefunc-adapter is one of the simplest yet incredibly useful js library I have ever written.

Hono is a web application framework built on web standards. It's incredibly fast and lightweight. Because it's built using web standard APIs, the same code will run on multiple runtimes (Cloudflare, Fastly, Deno, Bun, AWS, or Node.js).

For platforms that don't directly support web standards, Hono comes with adapters. For example, running in Node.js requires an adapter that converts requests and responses into node types and objects.

Building the Hono adapter

There are a lot of community-made adapters for running Hono on many more environments. Unfortunately, no one has ever made one for Azure Functions. I decided to built it, free and open-source.

The entire library has just only 54 lines of code.
Its so simple and maintainable. Yet it allows to easily port, with minimal/no code rewrites, APIs built with Hono on the powerful Azure Functions platform.

Simplicity wins

It is incredible to think of how many new possibilities this library unlocks with just 54 lines of code.

It's true, simple things are always the most difficult.
Although hono-azurefunc-adapter now appears clean and concise, it took a while to get to this point. I spent a lot of time polishing, refactoring, and rethinking how to accomplish the same things with fewer lines of code. I had to really dig deep into how the (partly document) Azure Functions API works.

Hono github star history

Hono has rapidly become one of the most widely used frameworks for building Javascript web APIs. Hats off to yusukebe and all the other contributors! Now it's finally possible to run it on Azure Functions, effortlessly with just azureHonoHandler(honoApp.fetch)!

How to use

It's very simple. Install hono-azurefunc-adapter with:

npm i @marplex/hono-azurefunc-adapter
Enter fullscreen mode Exit fullscreen mode

Now create the http trigger for Azure Functions. honoApp is your exported Hono application object.

import honoApp from "./app";

import { azureHonoHandler } from "@marplex/hono-azurefunc-adapter";
import { app } from "@azure/functions";

app.http("httpTrigger", {
  methods: [
    "GET",
    "POST",
    "DELETE",
    "HEAD",
    "PATCH",
    "PUT",
  ],
  authLevel: "anonymous",
  route: "{*proxy}",
  handler: azureHonoHandler(honoApp.fetch),
});
Enter fullscreen mode Exit fullscreen mode

That's it, you're done.

Limitations

There are some limitations and other things you should keep in mind when running Hono inside Azure Functions.

Route Prefix

The default Azure Functions route prefix is /api. Be sure to start all your Hono routes with /api or change the default Azure Functions route prefix in host.json

{
    "extensions": {
        "http": {
            "routePrefix": ""
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Crypto

In Node <=18 environments, if you are using hono/bearer-auth or any other library that uses crypto, be sure to define global.crypto = require("crypto"); before registering the http trigger.

Request signal

Azure Functions does not expose any signal or event for listening to http request interruptions. c.req.raw.signal is useless and its never aborted.

Conclusion

I think Azure is one of the most trusted enterprise-ready cloud providers. By building hono-azurefunc-adapter, I hope this will finally allow many to port the same popular Hono APIs to Azure Functions, especially for private enterprise needs.


hono-azurefunc-adapter is available on NPM and GitHub Packages. This project is fully open source and MIT licensed, so do what you want! Contributions are welcome 🥳

💖 💪 🙅 🚩
marplex
marplex

Posted on May 8, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Hono on Azure Functions
javascript Hono on Azure Functions

May 8, 2024