Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"runtime"
"slices"
"strings"
"sync"

"github.com/publiccodeyml/libpubliccode/v5/data"
netutil "github.com/publiccodeyml/libpubliccode/v5/internal"
Expand All @@ -29,8 +30,6 @@ func init() {

var errMissingURLScheme = errors.New("missing URL scheme")

var oembedSchemes []*regexp.Regexp

// downloadError is a failed request for a file the manifest points to.
// URL is the address requested, which may be a raw URL resolved from a
// relative path, and Err the cause, which does not name it.
Expand All @@ -47,18 +46,26 @@ func (e *downloadError) Unwrap() error {
return e.Err
}

func init() {
// oembedSchemes compiles the oEmbed provider patterns on the first video
// check, so importing the package doesn't pay for them.
var oembedSchemes = sync.OnceValue(compileOembedSchemes)

func compileOembedSchemes() []*regexp.Regexp {
var schemes []string

if err := json.Unmarshal(data.OembedSchemes, &schemes); err != nil {
panic("failed to parse oEmbed schemes: " + err.Error()) //nolint:forbidigo,lll // embedded at compile time, a failure here is a programming error
}

patterns := make([]*regexp.Regexp, 0, len(schemes))

for _, scheme := range schemes {
pattern := "^" + regexp.QuoteMeta(scheme) + "$"
pattern = strings.ReplaceAll(pattern, `\*`, `.*`)
oembedSchemes = append(oembedSchemes, regexp.MustCompile(pattern))
patterns = append(patterns, regexp.MustCompile(pattern))
}

return patterns
}

func getBasicAuth(domain Domain) string {
Expand Down Expand Up @@ -244,7 +251,7 @@ func (p *Parser) validLogo(u url.URL, network bool) (bool, error) {
}

func matchesOembedScheme(link string) bool {
for _, re := range oembedSchemes {
for _, re := range oembedSchemes() {
if re.MatchString(link) {
return true
}
Expand Down