composer

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 16 Imported by: 1 module, 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 composer holds what the Composer (PHP) repository formats share: the package naming rules, Composer's version normalization (VersionParser), the composer.json reader for dist archives and the Composer 2 metadata model (p2 documents with the "composer/2.0" minification). The hosted handler uses all of it; the proxy and group repositories reuse the metadata and version parts.

Index

Constants

View Source
const (

	// MaxManifestBytes bounds composer.json.
	MaxManifestBytes = 4 << 20
)

Limits on what ReadArchive accepts from an untrusted dist zip. Only composer.json is ever decompressed, so these bound the central directory parse and that one member.

View Source
const MaxNameLen = 255

MaxNameLen is the longest "vendor/package" name.

View Source
const MaxVersionLen = 128

MaxVersionLen is the longest version string (and normalized version) stored.

View Source
const MinifiedMarker = "composer/2.0"

MinifiedMarker is the value of the "minified" key of a p2 document whose version lists are stored as differences (Composer's MetadataMinifier).

Variables

View Source
var ErrChecksum = errors.New("dist does not match its shasum")

ErrChecksum is a dist whose bytes do not match the shasum its source published.

View Source
var ErrDocument = errors.New("unusable composer metadata")

ErrDocument is a p2 document that cannot be used: not JSON, without the requested package or without a usable version.

View Source
var ErrInvalidArchive = errors.New("invalid dist archive")

ErrInvalidArchive marks a dist zip that cannot be published; its message says what is wrong and is meant for the publisher.

View Source
var ErrInvalidVersion = errors.New("invalid version")

ErrInvalidVersion marks a string that is not a Composer version.

View Source
var ErrNoneLeft = errors.New("no version left")

ErrNoneLeft is a document whose every version was filtered out.

Functions

func Decode

func Decode(data []byte) (map[string][]Version, error)

Decode parses a p2 document into full version entries per package name, expanding a minified one. Numbers decode as json.Number.

func DistPath

func DistPath(name, version, ref string) string

DistPath is the path below the repository of the dist of one version: the coordinates and the content reference, so a URL never changes its bytes.

func DistRef

func DistRef(distURL string) (string, bool)

DistRef is the content reference of a dist URL built by DistPath (of this or another repository); false when the URL is not one.

func EncodeDocument

func EncodeDocument(pkgs map[string][]Version, minify bool) ([]byte, error)

EncodeDocument renders a p2 document; with minify the version lists are stored as differences and the document says so.

func IsDev

func IsDev(normalized string) bool

IsDev reports whether a normalized version is a development version: a branch ("dev-main") or a numeric branch ("1.9999999.9999999.9999999-dev"). Composer repositories serve those in the "~dev" metadata document and every other version, pre-releases included, in the stable one.

func MetadataName

func MetadataName(file string) (string, bool, bool)

MetadataName parses the file name of a p2 document ("package.json" or "package~dev.json") into the package part and whether it is the dev document. The last result is false when file is neither.

func Normalize

func Normalize(version string) (string, error)

Normalize ports Composer's VersionParser::normalize: it turns a version as written in a tag or branch ("v1.2.3", "1.0.0-RC1", "dev-main", "1.x-dev") into the form Composer compares ("1.2.3.0", "1.0.0.0-RC1", "dev-main", "1.9999999.9999999.9999999-dev"). It returns ErrInvalidVersion for what Composer rejects.

func NormalizePublished

func NormalizePublished(version string) (string, error)

NormalizePublished validates a version given for an upload and returns its normalized form. Besides what Composer's Normalize accepts it requires a plain version string: no surrounding space, aliases ("x as y"), stability flags ("@dev") or characters that do not belong in a URL path, and a non-empty branch name after "dev-".

func RelinkMember

func RelinkMember(versions []Version, name, root string)

RelinkMember points the dists of entries a repository of this server served at repository root: the file name of the dist (its content reference) is kept because the member keeps serving it under the same path. An entry whose dist is none of a repository's loses it. The entries are modified.

func RelinkSource

func RelinkSource(versions []Version, name, root string)

RelinkSource points the dists of entries a source other than this server published at repository root (the absolute URL of a proxy repository, without a trailing slash). The reference and shasum stay the source's, so Composer still verifies what it downloads against them. An entry whose dist cannot be proxied (no dist, another type, no http(s) URL) loses it and is installed from its "source" by Composer. The entries are modified.

func RenderSource

func RenderSource(data []byte, name, root string, keep func(Version) bool) ([]byte, error)

RenderSource turns the p2 document of package name as a source other than this server published it into the document a proxy repository at root serves: only that package, only the versions keep accepts (nil keeps all), with its dists relinked, minified. The filter sees the expanded entries, so a version that inherited its fields in the minified upstream document is judged by what it inherited. ErrNoneLeft when keep rejects every version.

func ValidName

func ValidName(name string) bool

ValidName reports whether name is a valid Composer package name.

func VerifySHA1

func VerifySHA1(r io.Reader, want string) error

VerifySHA1 reads r and checks it against the hex SHA-1 a source published as the "shasum" of a dist. An empty want (Packagist publishes none for most dists) checks nothing and reads nothing.

Types

type Dist

type Dist struct {
	// URL is the absolute download URL of the zip.
	URL string
	// SHA1 is the hex SHA-1 of the zip, Composer's "shasum".
	SHA1 string
	// Reference identifies the archive content ("reference" in Composer's
	// lock file and download cache).
	Reference string
}

Dist says where a version's archive is and how to check it.

func (Dist) ProxyRef

func (d Dist) ProxyRef() string

ProxyRef is the content reference a proxy repository gives the dist: a digest of where the upstream serves it from and of the reference it names, so the URL changes whenever either does and a client can never be handed one version's bytes under another's URL.

type Kind

type Kind int

Kind is what a request path below a repository names.

const (
	// KindRoot is packages.json.
	KindRoot Kind = iota + 1
	// KindMetadata is a p2 document, p2/vendor/name.json or p2/vendor/name~dev.json.
	KindMetadata
	// KindList is list.json.
	KindList
	// KindSearch is search.json.
	KindSearch
	// KindUpload is packages/upload/vendor/name/version (hosted repositories).
	KindUpload
	// KindDist is dists/vendor/name/version/ref.zip.
	KindDist
)

The request paths of a Composer repository.

type Manifest

type Manifest struct {
	// JSON is composer.json as stored in the archive (a JSON object).
	JSON json.RawMessage
	// Name is the "name" field, a valid package name.
	Name string
	// Version is the optional "version" field, "" when absent.
	Version string
	// Licenses are the identifiers of the "license" field (a string or a list).
	Licenses []string
}

Manifest is what a dist zip says about its package: the composer.json it carries, verbatim, and the fields a repository reads from it.

func ReadArchive

func ReadArchive(r io.ReaderAt, size int64) (*Manifest, error)

ReadArchive reads composer.json from a dist zip: from the archive root, or, when the root has none, from the single top-level directory the whole archive lives in (the layout of GitHub zipballs). It validates the package name.

type Path

type Path struct {
	Kind Kind
	Name string
	// Dev is set on a p2 document of the "~dev" variant.
	Dev bool
	// Version is the version of an upload or a dist, unescaped.
	Version string
	// Ref is the content reference of a dist: the part of its file name before ".zip".
	Ref string
}

Path is a parsed request path. Name is "vendor/package"; the other fields are set per kind.

func ParsePath

func ParsePath(rel string) (Path, bool)

ParsePath maps the path below a repository to what it names; false when the path is none of the protocol's.

type Release

type Release struct {
	// Version is the version as published, Normalized its VersionParser form.
	Version, Normalized string
	// Time is when the version was published.
	Time time.Time
	Dist Dist
}

Release is the repository's knowledge about one version of a package.

type Version

type Version map[string]any

Version is one version entry of a p2 document: the composer.json fields of the package at that version plus "version", "version_normalized", "time" and "dist". Numbers are decoded as json.Number so they survive a round trip.

func Expand

func Expand(versions []Version) []Version

Expand is Composer's MetadataMinifier::expand: it turns a minified version list, in which every entry after the first carries only what changed, into full entries. The input is not modified.

func Filter added in v0.16.0

func Filter(versions []Version, keep func(Version) bool) []Version

Filter returns the entries of versions for which keep is true, in order.

func FindVersion added in v0.16.0

func FindVersion(versions []Version, version string) (Version, bool)

FindVersion returns the entry whose "version" is exactly version.

func Minify

func Minify(versions []Version) []Version

Minify is MetadataMinifier::minify. The input is not modified.

func NewVersion

func NewVersion(name string, composerJSON []byte, rel Release) (Version, error)

NewVersion builds the p2 entry of a release from the package's composer.json.

func ParseDocument

func ParseDocument(data []byte, name string) ([]Version, error)

ParseDocument decodes a p2 document and returns the full entries of package name. Entries that are not objects or have no usable "version" are dropped; ErrDocument is returned when the document is not JSON, lacks the package or leaves no entry.

func (Version) MergeKey

func (v Version) MergeKey() string

MergeKey identifies a version across repositories: its normalized form.

func (Version) Published added in v0.16.0

func (v Version) Published() time.Time

Published is when the entry's source published the version ("time"); the zero time when it states none or one that does not parse.

func (Version) VersionString

func (v Version) VersionString() string

VersionString is the "version" of an entry; "" when it has none.

func (Version) ZipDist

func (v Version) ZipDist() (Dist, bool)

ZipDist is the zip dist block of an entry as its source published it. It is false for an entry without a dist, with one of another type (a tarball) or with an URL that is not an absolute http(s) one.

Directories

Path Synopsis
Package composertest builds Composer dist zips for tests.
Package composertest builds Composer dist zips for tests.

Jump to

Keyboard shortcuts

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