Files
hakurei/internal/pkg/file.go
Ophestra 861801597d
All checks were successful
Test / Create distribution (push) Successful in 49s
Test / Sandbox (push) Successful in 2m37s
Test / Hakurei (push) Successful in 3m52s
Test / ShareFS (push) Successful in 3m56s
Test / Hpkg (push) Successful in 4m26s
Test / Sandbox (race detector) (push) Successful in 4m56s
Test / Hakurei (race detector) (push) Successful in 5m52s
Test / Flake checks (push) Successful in 1m39s
internal/pkg: expose response body
This uses the new measured reader provided by Cache. This should make httpArtifact zero-copy.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2026-01-25 16:10:34 +09:00

59 lines
1.5 KiB
Go

package pkg
import (
"bytes"
"crypto/sha512"
"fmt"
"io"
)
// A fileArtifact is an [Artifact] that cures into data known ahead of time.
type fileArtifact []byte
var _ KnownChecksum = new(fileArtifact)
// fileArtifactNamed embeds fileArtifact alongside a caller-supplied name.
type fileArtifactNamed struct {
fileArtifact
// Caller-supplied user-facing reporting name.
name string
}
var _ fmt.Stringer = new(fileArtifactNamed)
var _ KnownChecksum = new(fileArtifactNamed)
// String returns the caller-supplied reporting name.
func (a *fileArtifactNamed) String() string { return a.name }
// NewFile returns a [FileArtifact] that cures into a caller-supplied byte slice.
//
// Caller must not modify data after NewFile returns.
func NewFile(name string, data []byte) FileArtifact {
f := fileArtifact(data)
if name != "" {
return &fileArtifactNamed{f, name}
}
return &f
}
// Kind returns the hardcoded [Kind] constant.
func (a *fileArtifact) Kind() Kind { return KindFile }
// Params writes the result of Cure.
func (a *fileArtifact) Params(ctx *IContext) { ctx.GetHash().Write(*a) }
// Dependencies returns a nil slice.
func (a *fileArtifact) Dependencies() []Artifact { return nil }
// Checksum computes and returns the checksum of caller-supplied data.
func (a *fileArtifact) Checksum() Checksum {
h := sha512.New384()
h.Write(*a)
return Checksum(h.Sum(nil))
}
// Cure returns the caller-supplied data.
func (a *fileArtifact) Cure(*RContext) (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(*a)), nil
}