Hazel Documentation

repository·master·Indexed 25 days ago

https://github.com/vercel/hazel

A lightweight update server for Electron applications that proxies and caches release data from GitHub Releases. It provides endpoints for Electron's auto-updater to check for and download updates for macOS and Windows, and can be deployed on Vercel Serverless Functions or integrated programmatically into Node.js HTTP servers using the hazel-server package.

Tokens
1K
Snippets
3
Records
7
Agent score
34%

What's inside Hazel

  1. Configure Electron autoUpdater with Hazel

    master

    Once Hazel is deployed, integrate it into your Electron application by setting the feed URL using the deployment address. Use process.platform and app.getVersion() to construct the update URL.

    Note: Only perform this configuration in the production version of your app, not during development.

    const { app, autoUpdater } = require('electron')
    
    const server = <your-deployment-url>
    const url = `${server}/update/${process.platform}/${app.getVersion()}`
    
    autoUpdater.setFeedURL({ url })
  2. Deploy Hazel on Vercel

    master

    Deploy Hazel to Vercel to create an update server for your Electron application. This server pulls release data from GitHub Releases and caches it in memory, providing direct links to GitHub assets to save bandwidth. It supports macOS and Windows and scales on Vercel Serverless Functions.

    https://vercel.com/button
  3. Configure Hazel via Environment Variables

    master

    You can customize Hazel's behavior using the following environment variables:

    VariableDescription
    INTERVALCache refresh interval in minutes (defaults to 15).
    PRESet to 1 to only cache pre-releases.
    TOKENGitHub token required for accessing private repositories.
    URLThe server's URL (automatically populated when running on Vercel, but required for private repos elsewhere).
  4. Use Hazel programmatically in an HTTP server

    master

    You can integrate hazel-server into an existing Node.js HTTP server to implement custom logic, such as analytics, on specific paths.

    const hazel = require('hazel-server')
    
    http.createServer((req, res) => {
      hazel(req, res)
    })
  5. Hazel API Routes Reference

    master

    Hazel provides several endpoints for managing updates and downloads:

    • /: Displays an overview page of the cached repository, including available platforms, file sizes, and direct download links.
    • /download: Automatically detects the visitor's platform/OS via User Agent and attempts to download the appropriate application copy. Returns 404 if the latest version or the specific platform file is not yet cached.
    • /download/:platform: Downloads the application copy for a specific platform (e.g., darwin or win32). Returns the overview page (/) if the cache is not yet filled for that platform.
    • /update/:platform/:version: Checks for available updates by reading the cache. Returns status code 204 if no update is found or if the latest release/platform file is not yet cached.
    • /update/win32/:version/RELEASES: Specifically for Windows (Squirrel.Windows). Returns a cached RELEASES file containing a download link to the .nupkg update file.
  6. Initialize Hazel with a configuration object

    master

    The main entry point of the hazel-server package is a factory function that accepts a config object. This function initializes a cache and sets up a router with several predefined routes for overview, downloads, and updates.

    If the cache initialization fails with an error containing a code, the returned function will act as a middleware that responds with a 400 status code and a JSON error object containing the code and message. If the error does not contain a code, the initialization will throw the error.

    The returned function is a standard Node.js request handler (req, res) => void compatible with most HTTP servers.

  7. Initialize the Hazel server programmatically

    master

    The hazel factory function (exported from lib/server.js) initializes a Hazel server instance using configuration provided via environment variables. This is the primary way to instantiate the server for programmatic use.

    Required or supported environment variables:

    • INTERVAL: The update interval.
    • ACCOUNT: The account identifier.
    • REPOSITORY: The repository identifier.
    • PRE: A prefix for certain operations.
    • TOKEN: The authentication token.
    • URL: The private base URL (used if VERCEL_URL is not present).
    • VERCEL_URL: The Vercel deployment URL.