httpfs

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpfs is a read-only fs.FS over HTTP, for games that load their assets from a web server instead of embedding them: a browser build (js/wasm, where net/http uses fetch) or an editor webview reading the workspace. It uses the standard library only.

A file name is resolved against the base URL: with base "https://example.com/assets/", "hero/hero.png" is "https://example.com/assets/hero/hero.png". Directories cannot be listed; the asset system never lists them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WatchChanges

func WatchChanges(url string) <-chan []string

WatchChanges listens to the server-sent event stream at url (a development server that knows when asset files change) and returns a channel of changed asset keys: each event whose data is {"paths": ["hero/hero.png", ...]} becomes one batch, in order. Pass each batch to asset.Server.Changed on the game goroutine for hot reload in a browser build.

It reads the stream on its own goroutine with net/http (body streaming works on js/wasm) and never blocks the caller. When the connection drops or cannot be made it reconnects with a capped backoff, so a restarted dev server is picked up. An event that does not parse is logged and dropped. The goroutine runs for the life of the process and the channel is never closed; the buffer holds a few batches, after which the reader goroutine waits for the game to drain it.

Types

type FS

type FS struct {
	// contains filtered or unexported fields
}

FS reads files with HTTP GET and stats them with HTTP HEAD. It implements fs.FS, fs.ReadFileFS and fs.StatFS. Calls block until the response is in, so on js/wasm keep them on the asset workers (the asset system's default), not on the game goroutine.

func New

func New(baseURL string, client *http.Client) (*FS, error)

New returns an FS that reads files under baseURL. A nil client means http.DefaultClient. It returns an error if baseURL does not parse; a missing trailing slash is added, so the base always names a directory.

func (*FS) Open

func (f *FS) Open(name string) (fs.File, error)

Open reads the whole file with GET and returns it as an in-memory file.

func (*FS) ReadFile

func (f *FS) ReadFile(name string) ([]byte, error)

ReadFile reads the whole file with GET.

func (*FS) Stat

func (f *FS) Stat(name string) (fs.FileInfo, error)

Stat requests the file with HEAD. Size comes from Content-Length (-1 if the server sends none), ModTime from Last-Modified (zero if absent).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL