run

package
v1.0.0-rc.9 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 8 Imported by: 0 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 run is the one "prepare a run" step shared by cmd/ngchat and cmd/smoke: the environment both read, the site client with its routing cookie, the credential the jar is seeded with, the chat-URL check, and the client.Config that follows. The two binaries used to hand-write this sequence and drifted (different variable names, one skipping the store migration); keeping it here means a change to the order or the messages happens once and is tested once, through narrowed interfaces rather than a keyring or a network.

Index

Constants

View Source
const (
	// DefaultWSURL and DefaultSiteURL are production, the only endpoints
	// that exist outside Newgrounds; the variables below override them
	// for the internal dev stack.
	DefaultWSURL   = "wss://chat.newgrounds.com/ws"
	DefaultSiteURL = "https://www.newgrounds.com"

	// Channel is the one room a run joins. The server has one channel
	// today and switching is out of scope (docs/roadmap.md).
	Channel = "general"
)

Variables

View Source
var ErrNoCredential = errors.New(
	"no NGCHAT_NG_COOKIE and no usable stored login")

ErrNoCredential is returned by Prepare when nothing usable is stored and Options.Login is nil. It wraps the store's own answer, so errors.Is still tells auth.ErrNotFound (log in) from auth.ErrKeyringUnavailable (unlock the keyring, or log in and the value lands in the file).

Functions

func ClientConfig

func ClientConfig(env Env, site *auth.Site, log *slog.Logger) client.Config

ClientConfig is the client.Config both binaries run: the chat URL and routing cookie from Env, the one Channel, and a minter over the site Prepare just seeded. log is nil for no logging.

func NewSite

func NewSite(env Env) (*auth.Site, error)

NewSite builds the site client for the run with the routing cookie seeded, so it rides on every request the way it does in a browser. It is separate from Prepare because login and logout need the site too, and neither must be blocked by a stale chat URL or a missing credential. Errors name the variable at fault.

Types

type CredentialStore

type CredentialStore interface {
	Load(key string) (string, error)
	Migrate() (bool, error)
}

CredentialStore is what Prepare needs from auth.Store, narrowed so the outcome matrix can be scripted without a keyring.

type Env

type Env struct {
	// SiteURL is NGCHAT_SITE_URL, the origin whose cookie jar mints the
	// chat JWT.
	SiteURL string
	// WSURL is NGCHAT_WS_URL, the chat endpoint the JWT is sent to.
	WSURL string
	// Routing is NGCHAT_ROUTING_COOKIE: the dev proxy's routing cookie,
	// which must ride on every request, site and chat alike. Empty in
	// production.
	Routing string
	// CookieHeader is NGCHAT_NG_COOKIE, a raw cookie header that bypasses
	// the stored login. Development and the smoke harness only.
	CookieHeader string
}

Env is what a run reads from the environment, held as a value so the step can be tested without os.Getenv and so a caller can see, in one place, every variable that steers a run.

func FromEnv

func FromEnv() Env

FromEnv reads Env from the process environment, defaulting the URLs to production so a run with nothing set is a run against the real site, as README.md promises.

func (Env) LogValue

func (e Env) LogValue() slog.Value

LogValue gives slog the same redacted view as String, as a group so each field is still queryable, with the header's presence kept and its value dropped.

func (Env) String

func (e Env) String() string

String prints the URLs and routing cookie and hides the cookie header, so fmt's %v and %+v of an Env are safe to write anywhere. Whether a header is set is still shown, since that is what a debug log needs to explain which credential a run used.

type Options

type Options struct {
	Env   Env
	Store CredentialStore
	Site  Site
	// Login runs when the store has nothing usable: not found, or a
	// keyring that gave no answer. It must leave the jar authenticated
	// (auth.Login does), since the first mint follows with no further
	// setup. nil means the caller cannot prompt, and Prepare returns
	// ErrNoCredential instead; that is how the headless smoke harness
	// stays headless (ADR 0008).
	Login func(ctx context.Context) error
	// Notices receives the lines a user should see on the way: a
	// removed v0.1 slot, a keyring that could not be read. nil discards
	// them. Never a secret.
	Notices io.Writer
}

Options is what Prepare works with. Store and Site are interfaces rather than the auth types so the decision table is tested with fakes; Env is a value so no test reads the real environment.

type Site

type Site interface {
	CredentialKey() string
	SeedCookies(header string) error
	SetRemember(value string)
	CheckChatURL(raw string) error
}

Site is what Prepare needs from *auth.Site, narrowed so the jar can be a fake that records what it was seeded with.

type Source

type Source int

Source says where the credential a run minted with came from. The smoke harness prints it because an exported NGCHAT_NG_COOKIE silently wins over a fresh `ngchat login`, which is the one thing an operator verifying the login flow needs to know.

const (
	// FromHeader: NGCHAT_NG_COOKIE was set and seeded as-is.
	FromHeader Source = iota + 1
	// FromStore: the remember cookie `ngchat login` persisted.
	FromStore
	// FromLogin: nothing usable was stored and Options.Login ran.
	FromLogin
)

func Prepare

func Prepare(ctx context.Context, o Options) (Source, error)

Prepare gets the jar into a state that can mint, in this order:

  1. The chat URL is checked, before anything is minted, because the token minted from the site's cookie goes to it as the first frame.
  2. NGCHAT_NG_COOKIE, when set, is seeded as-is and nothing stored is read.
  3. Otherwise the v0.1 cookie-header slot is removed (it cannot be converted, ADR 0003), the stored remember cookie is loaded, and when there is none Options.Login runs.

A keyring that gave no answer counts as "log in": whatever it holds cannot be used this run, and a fresh login lands in the file, which the store reads first from then on. Any other store error propagates.

func (Source) String

func (s Source) String() string

String names the source for a log line.

Jump to

Keyboard shortcuts

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