npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gosub-goproxy

v0.0.16

Published

GOPROXY implementation that allows Go modules to live in subdirectories

Downloads

6

Readme

GOSUB

GOPROXY implementation that allows Go modules to live in subdirectories

Test

Overview

Gosub is a module proxy server for the Go language. It's designed to solve this issue in the go command:

It's a small (~10KB) standalone JS module, ideal for an edge runtime like Cloudflare, Netlify or Vercel. You're also welcome to use my shared server at gosub.moreplease.com.

Gosub currently only supports Github, but if you're interested in using it on Gitlab, Bitbucket, etc, please get in touch.

Installation & usage

You'll need to set up an indirection, AKA "vanity import" for your Go module. This is a <meta name="go-import" ...> tag on your web server that tells the Go toolchain how to find your module.

You can point your vanity import at Gosub in three different ways:

  • Deploy the goproxy function, configured to serve a specific Go module;
  • Deploy the gosub function, which can dynamically serve any Go module;
  • Use my shared server: gosub.moreplease.com

Note that by default, all Go modules are cached by proxy.golang.org; most users will get your module from there, rather than directly from your proxy server.

I'll use my own "UTF-64" module as a worked example:

goproxy mode (static config)

type GoproxyConfig = {
  url: string; // Repo URL (currently only Github is supported)
  module?: string; // If set, Go module must match this
  directory?: string; // Subdirectory within the git repo
  tagPrefix?: string; // Prefix for version tags in git (default is "V")
  tagSuffix?: string; // Suffix for version tags in git
};

type GoproxyEnv = {
  GITHUB_TOKEN?: string; // Optional API token for GitHub
}

type Goproxy = (request: Request) => Promise<Response | undefined>

function goproxy(
  base: string,
  config: GoproxyConfig,
  env?: GoproxyEnv
): Goproxy;

The goproxy factory returns an async handler function for a Request (from the Fetch API). The handler returns:

  • undefined on requests that don't match its base path;
  • 404 on matching but invalid requests;
  • GOPROXY data on valid requests.

We call the GitHub API internally to fetch data. Any GitHub error responses are passed through without modification.

To avoid being rate-limited, you should pass a GitHub API token in the GITHUB_TOKEN environment variable. By default we'll try to use the system environment (we try both process.env and Deno.env). To set the environment explicitly, use the env parameter; to prevent any environment lookup, use the value {}.

Example / demo

The root page for my module's domain has the "vanity import" tag:

  <meta
    name="go-import"
    content="utf64.moreplease.com mod https://utf64.moreplease.com/go"
  />

This instructs the Go toolchain to look for a mod (module server) at /go on the same domain. I deploy the goproxy function as follows:

import { goproxy } from "gosub-goproxy";

const handler = goproxy("/go", {
  url: "https://github.com/more-please/more-stuff",
  module: "utf64.moreplease.com",
  directory: "utf64/go",
  tagPrefix: "utf64-go-",
});

The repo has version tags like utf64-go-0.0.11, etc. Goproxy ignores tags without the utf64-go- prefix.

The GOPROXY path to list module versions is $base/$module/@v/list, or in this case:

gosub mode (dynamic config)

function gosubEncode(config: GoproxyConfig): string;
function gosubDecode(path: string): GoproxyConfig | undefined;
function gosub(base: string = "/", env?: GoproxyEnv): Goproxy;

This is a wrapper for goproxy that takes its configuration from the URL. Call the gosubEncode function to convert a config struct into a URL pathname that gosub understands.

Example / demo

Recall the configuration for utf64.moreplease.com:

{
  url: "https://github.com/more-please/more-stuff",
  module: "utf64.moreplease.com",
  directory: "utf64/go",
  tagPrefix: "utf64-go-",
}

This encodes to:

github.com/more-please/more-stuff:d=utf64%2Fgo&m=utf64.moreplease.com&p=utf64-go-;

Note that we can omit module from the config, in which case we get:

github.com/more-please/more-stuff:d=utf64%2Fgo&p=go-;

The only advantage of setting module explicitly is that it blocks nonsense lookups like wrong-module-name/@v/list.

Putting it all together, the @v/list command for utf64.moreplease.com using my gosub server deployed at gosub.moreplease.com is:

Shared server

As noted above, you're welcome to use the gosub server at gosub.moreplease.com. That site also has a handy web form where you can plug in your repo URL and other config details to generate the proxy path.

All users share the same GitHub API key, so it may get overloaded. However, once your Go module has been cached by proxy.golang.org your users mostly won't be hitting my site directly.

Acknowledgements

Partly inspired by stumbling across GitPkg, which solves a very similar problem in the NPM ecosystem. In particular, I copied the nifty idea of having a shared server that's programmable via URL parameters.