KEMBAR78
GitHub - fastly/compute-starter-kit-typescript-hono: Starter Kit for using Hono with Fastly Compute
Skip to content

fastly/compute-starter-kit-typescript-hono

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hono Starter Kit for TypeScript on Fastly Compute

Deploy to Fastly

Get to know Hono on the Fastly Compute environment with a basic starter written in TypeScript.

This starter demonstrates how to:

  • Define typed environment bindings (Backend, Logger, etc.)
  • Add routing and middleware with Hono
  • Build synthetic responses at the edge
  • Forward requests to backends with caching overrides
  • Serve static files compiled directly into your Compute app

For more starter kits and patterns, see the Fastly Documentation Hub.

Features

  • TypeScript source with tsconfig.json pre-configured for Compute
  • Usage of the @fastly/hono-fastly-compute helper library
  • Restrict allowed HTTP methods
  • Route matching with Hono
  • Static file embedding via includeBytes
  • Logging to a Fastly endpoint
  • Backend fetch with CacheOverride

Code Walkthrough

The entry point src/index.ts includes several common patterns you'll use when building Compute apps with Hono:

  • Defining environment context bindings

    const fire = buildFire({
      httpme: 'Backend:http-me',        // I have a backend named `http-me`
      myEndpoint: 'Logger:my_endpoint', // I have a logging endpoint named `my_endpoint`
    });
    type Env = {
      Bindings: typeof fire.Bindings,
    };
    const app = new Hono<Env>();
    
    // set up routes...
    
    fire(app);

    Defined environment resources are available on c.env within handlers and middleware (env on Hono), for example:

    c.env.myEndpoint.log('Hello from the edge!');

    For more details on context bindings, see @fastly/compute-js-context (typed environment bindings).

  • Static file response

    const welcomePage = includeBytes('./src/welcome-to-compute.html');
    return c.body(welcomePage, 200, { 'Content-Type': 'text/html' });
  • Streaming log endpoint

    c.env.myEndpoint.log('Hello from the edge!');
  • Method filtering

    app.use(createMiddleware(async (c, next) => {
      if (!["HEAD", "GET", "PURGE"].includes(c.req.method)) {
        return c.text("This method is not allowed", 405);
      }
      await next();
    }));
  • Backend fetch with cache override

    const cacheOverride = new CacheOverride('override', { ttl: 60 });
    const beresp = await fetch(bereq, { backend: c.env.httpme, cacheOverride });

Getting Started

Create a new app from this starter kit:

npm create @fastly/compute@latest -- --language=typescript --starter-kit=hono

Run locally with the local development environment:

npm run start

Deploy to Fastly (you'll be prompted to create a new service if you don't have one yet):

npm run deploy

Next Steps

  • Try adding a new route in index.ts, e.g. app.get('/hello', ...)
  • Configure a real backend in fastly.toml and call it from your code
  • Explore @fastly/compute-js-context for richer typed bindings
  • Add middleware from the Hono ecosystem

Security

Please see our SECURITY.md for guidance on reporting security-related issues.

About

Starter Kit for using Hono with Fastly Compute

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published