feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
@@ -0,0 +1,140 @@
|
||||
// Package documentrefs freezes mutable Asset references without interpreting
|
||||
// ordinary text or code examples. Resolvers must authorize the source Domain.
|
||||
package documentrefs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
var ErrUnavailable = errors.New("embedded asset is unavailable")
|
||||
|
||||
type Resolver func(context.Context, string) (string, error)
|
||||
|
||||
// Freeze retains original bytes for unchanged tokens and deduplicates hashes
|
||||
// in appearance order. Repeated addresses resolve only once per payload.
|
||||
func Freeze(ctx context.Context, payload []byte, contentType string, resolve Resolver) ([]byte, []string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil || !strings.EqualFold(mediaType, "text/html") {
|
||||
return payload, nil, nil
|
||||
}
|
||||
z := html.NewTokenizer(bytes.NewReader(payload))
|
||||
var output bytes.Buffer
|
||||
seen := map[string]bool{}
|
||||
resolved := map[string]string{}
|
||||
var hashes []string
|
||||
lookup := func(address string) (string, error) {
|
||||
if hash := resolved[address]; hash != "" {
|
||||
return hash, nil
|
||||
}
|
||||
if resolve == nil {
|
||||
return "", ErrUnavailable
|
||||
}
|
||||
hash, err := resolve(ctx, address)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(hash) != 64 {
|
||||
return "", fmt.Errorf("%w: %s", ErrUnavailable, address)
|
||||
}
|
||||
if _, err := hex.DecodeString(hash); err != nil {
|
||||
return "", fmt.Errorf("%w: %s", ErrUnavailable, address)
|
||||
}
|
||||
hash = strings.ToLower(hash)
|
||||
resolved[address] = hash
|
||||
if !seen[hash] {
|
||||
seen[hash] = true
|
||||
hashes = append(hashes, hash)
|
||||
}
|
||||
return hash, nil
|
||||
}
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
typ := z.Next()
|
||||
if typ == html.ErrorToken {
|
||||
if errors.Is(z.Err(), io.EOF) {
|
||||
return output.Bytes(), hashes, nil
|
||||
}
|
||||
return nil, nil, z.Err()
|
||||
}
|
||||
if typ != html.StartTagToken && typ != html.SelfClosingTagToken {
|
||||
output.Write(z.Raw())
|
||||
continue
|
||||
}
|
||||
// Token() may alter the tokenizer buffer. Copy original bytes first.
|
||||
raw := append([]byte(nil), z.Raw()...)
|
||||
token := z.Token()
|
||||
changed := false
|
||||
assetRef := attribute(token.Attr, "asset-ref")
|
||||
legacy := attribute(token.Attr, "immutable-ref")
|
||||
if assetRef == "" && strings.HasPrefix(legacy, "asset:") {
|
||||
assetRef = legacy
|
||||
}
|
||||
if strings.HasPrefix(assetRef, "asset:") {
|
||||
hash, err := lookup(assetRef)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
setAttribute(&token, "asset-ref", assetRef)
|
||||
setAttribute(&token, "immutable-ref", "sha256:"+hash)
|
||||
changed = true
|
||||
}
|
||||
for _, name := range []string{"src", "href", "poster", "data"} {
|
||||
provenance := "data-pageswiki-asset-" + name
|
||||
address := attribute(token.Attr, provenance)
|
||||
if address == "" {
|
||||
candidate := attribute(token.Attr, name)
|
||||
if strings.HasPrefix(candidate, "asset:") {
|
||||
address = candidate
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(address, "asset:") {
|
||||
continue
|
||||
}
|
||||
hash, err := lookup(address)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
setAttribute(&token, provenance, address)
|
||||
setAttribute(&token, name, "/_/obj/sha256:"+hash)
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
output.WriteString(token.String())
|
||||
} else {
|
||||
output.Write(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func attribute(attrs []html.Attribute, name string) string {
|
||||
for _, a := range attrs {
|
||||
if strings.EqualFold(a.Key, name) {
|
||||
return strings.TrimSpace(a.Val)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func setAttribute(token *html.Token, name, value string) {
|
||||
for i := range token.Attr {
|
||||
if strings.EqualFold(token.Attr[i].Key, name) {
|
||||
token.Attr[i].Key = name
|
||||
token.Attr[i].Val = value
|
||||
return
|
||||
}
|
||||
}
|
||||
token.Attr = append(token.Attr, html.Attribute{Key: name, Val: value})
|
||||
}
|
||||
Reference in new issue
Block a user