Skip to content

Commit

Permalink
task: adding leapkit core new source
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Feb 22, 2025
1 parent b0ccb5e commit cb99de5
Show file tree
Hide file tree
Showing 179 changed files with 1,819 additions and 11,894 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
⚠️ Important: Leapkit Has moved to use the leapkit/leapkit monorepo. That's where this framework will advance.

# Leapkit Core

<img width="300" alt="logo" src="https://github.com/LeapKit/template/assets/645522/d5bcb8ed-c763-4b39-8cfb-aed694b87646">
Expand Down
33 changes: 15 additions & 18 deletions assets/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,23 @@ import (
"strings"
)

var normalized = func(name string) string {
name = strings.TrimPrefix(name, "/public/")
name = strings.TrimPrefix(name, "public/")

return name
}

var withPrefix = func(name string) string {
return path.Join("/public/", name)
}

// PathFor returns the fingerprinted path for a given
// file. If the path passed contains the hash it will
// return the same path.

// filename to open should be the file without the prefix
// filename for the map should be the file without the prefix
// filename returned should be the file with the prefix
func (m *manager) PathFor(fname string) (string, error) {
normalized := normalized(fname)
result := m.fileToHash[normalized]
if result != "" {
return withPrefix(result), nil
func (m *manager) PathFor(name string) (string, error) {
normalized := m.normalize(name)
if hashed, ok := m.fileToHash[normalized]; ok {
return path.Join(m.servingPath, hashed), nil
}

// Compute the hash of the file
bb, err := m.ReadFile(normalized)
if err != nil {
return "", fmt.Errorf("could not open %s: %w", normalized, os.ErrNotExist)
return "", fmt.Errorf("could not open %q: %w", normalized, os.ErrNotExist)
}

hash := md5.Sum(bb)
Expand All @@ -53,5 +41,14 @@ func (m *manager) PathFor(fname string) (string, error) {
m.fileToHash[normalized] = filename
m.HashToFile[filename] = normalized

return withPrefix(filename), nil
return path.Join("/", m.servingPath, filename), nil
}

func (m *manager) normalize(name string) string {
name = strings.TrimPrefix(path.Clean(name), "/")
servingPath := strings.TrimPrefix(path.Clean(m.servingPath), "/")

name = strings.TrimPrefix(path.Clean(name), servingPath)

return strings.TrimPrefix(name, "/")
}
140 changes: 118 additions & 22 deletions assets/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,46 @@ import (
"testing"
"testing/fstest"

"github.com/leapkit/core/assets"
"go.leapkit.dev/core/assets"
)

func TestFingerprint(t *testing.T) {
assetsPath := "/files/"
m := assets.NewManager(fstest.MapFS{
"main.js": {Data: []byte("AAA")},
"other/main.js": {Data: []byte("AAA")},
})
"main.js": {Data: []byte("AAA")},
"other/main.js": {Data: []byte("AAA")},
"custom/main.go": {Data: []byte("AAAA")},
}, assetsPath)

t.Run("is deterministic", func(t *testing.T) {
a, _ := m.PathFor("public/main.js")
b, _ := m.PathFor("public/main.js")
a, _ := m.PathFor("files/main.js")
b, _ := m.PathFor("files/main.js")
if a != b {
t.Errorf("Expected %s to equal %s", a, b)
}

if !strings.Contains(a, "/public/") {
t.Errorf("Expected %s to have /public/ prefix", a)
if !strings.Contains(a, assetsPath) {
t.Errorf("Expected %s to have %s prefix", a, assetsPath)
}

a, _ = m.PathFor("public/other/main.js")
b, _ = m.PathFor("public/other/main.js")
a, _ = m.PathFor("files/other/main.js")
b, _ = m.PathFor("files/other/main.js")
if a != b {
t.Errorf("Expected %s to equal %s", a, b)
}

if !strings.Contains(a, "/public/") {
t.Errorf("Expected %s to have /public/ prefix", a)
if !strings.Contains(a, assetsPath) {
t.Errorf("Expected %s to have %s prefix", a, assetsPath)
}
})

t.Run("adds starting slash", func(t *testing.T) {
a, err := m.PathFor("public/main.js")
a, err := m.PathFor("files/main.js")
if err != nil {
t.Fatal(err)
}

b, err := m.PathFor("/public/main.js")
b, err := m.PathFor("/files/main.js")
if err != nil {
t.Fatal(err)
}
Expand All @@ -52,11 +54,11 @@ func TestFingerprint(t *testing.T) {
}
})

t.Run("adds starting /public", func(t *testing.T) {
t.Run("adds starting /files", func(t *testing.T) {
a, _ := m.PathFor("main.js")
t.Log(a)
if !strings.HasPrefix(a, "/public") {
t.Errorf("Expected %s to start with /public", a)
if !strings.HasPrefix(a, "/files") {
t.Errorf("Expected %s to start with /files", a)
}
})

Expand All @@ -66,17 +68,17 @@ func TestFingerprint(t *testing.T) {
t.Fatal(err)
}

if !strings.HasPrefix(a, "/public/main-") {
t.Errorf("Expected %s to contain /public/other/main-<hash>", a)
if !strings.HasPrefix(a, "/files/main-") {
t.Errorf("Expected %s to contain /files/other/main-<hash>", a)
}

b, _ := m.PathFor("public/other/main.js")
b, _ := m.PathFor("files/other/main.js")
if err != nil {
t.Fatal(err)
}

if !strings.HasPrefix(b, "/public/other/main-") {
t.Errorf("Expected %s to contain /public/other/main-<hash>", b)
if !strings.HasPrefix(b, "/files/other/main-") {
t.Errorf("Expected %s to contain /files/other/main-<hash>", b)
}

if a == b {
Expand All @@ -90,4 +92,98 @@ func TestFingerprint(t *testing.T) {
t.Errorf("File must not exists: %s", a)
}
})

t.Run("file does not exist", func(t *testing.T) {
a, err := m.PathFor("custom/main.go")
if err == nil {
t.Errorf("File must not exists: %s", a)
}
})

t.Run("manager looks for files in root", func(t *testing.T) {
m := assets.NewManager(fstest.MapFS{
"main.js": {Data: []byte("AAA")},
"other/main.js": {Data: []byte("AAA")},
}, "")

a, err := m.PathFor("main.js")
if err != nil {
t.Fatal(err)
}

if !strings.HasPrefix(a, "/") {
t.Errorf("Expected %s to start with /", a)
}

b, err := m.PathFor("other/main.js")
if err != nil {
t.Fatal(err)
}

if !strings.HasPrefix(b, "/") {
t.Errorf("Expected %s to start with /", b)
}
})

t.Run("PathFor normalize file path", func(t *testing.T) {
cases := []struct {
servingPath string
name string
expected string
}{
{"public", "main.js", "/public/main-"},
{"public", "/main.js", "/public/main-"},
{"public", "public/main.js", "/public/main-"},
{"public", "/public/main.js", "/public/main-"},
{"public", "/other/main.js", "/public/other/main-"},
{"public", "other/main.js", "/public/other/main-"},

{"/public", "main.js", "/public/main-"},
{"/public", "/main.js", "/public/main-"},
{"/public", "public/main.js", "/public/main-"},
{"/public", "/public/main.js", "/public/main-"},
{"/public", "public/other/main.js", "/public/other/main-"},
{"/public", "/public/other/main.js", "/public/other/main-"},

{"/public/", "main.js", "/public/main-"},
{"/public/", "/main.js", "/public/main-"},
{"/public/", "public/main.js", "/public/main-"},
{"/public/", "/public/main.js", "/public/main-"},
{"/public/", "public/other/main.js", "/public/other/main-"},
{"/public/", "/public/other/main.js", "/public/other/main-"},

{"public/other", "main.js", "/public/other/main-"},
{"public/other", "/main.js", "/public/other/main-"},
{"public/other", "public/other/main.js", "/public/other/main-"},
{"public/other", "/public/other/main.js", "/public/other/main-"},
{"/public/other", "public/other/main.js", "/public/other/main-"},
{"/public/other", "/public/other/main.js", "/public/other/main-"},

{"/public/other", "main.js", "/public/other/main-"},
{"/public/other", "/main.js", "/public/other/main-"},
{"/public/other", "public/other/main.js", "/public/other/main-"},
{"/public/other", "/public/other/main.js", "/public/other/main-"},

{"/public/other/", "main.js", "/public/other/main-"},
{"/public/other/", "/main.js", "/public/other/main-"},
{"/public/other/", "public/other/main.js", "/public/other/main-"},
{"/public/other/", "/public/other/main.js", "/public/other/main-"},
}

for i, c := range cases {
manager := assets.NewManager(fstest.MapFS{
"main.js": {Data: []byte("AAA")},
"other/main.js": {Data: []byte("AAA")},
}, c.servingPath)

result, err := manager.PathFor(c.name)
if err != nil {
t.Errorf("%d, Expected no error, got %s", i, err)
}

if !strings.HasPrefix(result, c.expected) {
t.Errorf("%d, Expected %s to start with /public/", i, result)
}
}
})
}
31 changes: 10 additions & 21 deletions assets/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,41 @@ package assets
import (
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
)

func (m *manager) HandlerPattern() string {
return m.servingPath
}
if m.servingPath == "/" {
return ""
}

func (m *manager) HandlerFn(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, m, strings.TrimPrefix(r.URL.Path, m.handlerPrefix()))
return m.servingPath
}

func (m *manager) Open(name string) (file fs.File, err error) {
func (m *manager) Open(name string) (fs.File, error) {
ext := filepath.Ext(name)
if ext == ".go" {
return nil, os.ErrNotExist
}

// Converting hashed into original file name
smp := m.HashToFile[name]
if smp != "" {
name = smp
if original, ok := m.HashToFile[name]; ok {
name = original
}

fn := m.embedded.Open
if env := os.Getenv("GO_ENV"); env == "development" {
fn = m.folder.Open
}

name = strings.TrimPrefix(name, m.handlerPrefix())
name = strings.TrimPrefix(name, m.servingPath)

file, err = fn(name)
return file, err
return m.embedded.Open(name)
}

func (m *manager) ReadFile(name string) ([]byte, error) {
x, err := m.Open(name)
if err != nil {
return nil, err
}
defer x.Close()

return io.ReadAll(x)
}

func (m *manager) handlerPrefix() string {
return strings.TrimSuffix(m.servingPath, "*")
}
39 changes: 20 additions & 19 deletions assets/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package assets

import (
"io/fs"
"os"
"path"
"strings"
"sync"
)

type manager struct {
embedded fs.FS
folder fs.FS

outputFolder string
inputFolder string

servingPath string

Expand All @@ -20,21 +17,25 @@ type manager struct {
HashToFile map[string]string
}

// NewManager returns a new manager that wraps the given embed.FS and the input and output folders.
func NewManager(embedded fs.FS) *manager {
// TODO: options to change:
// - input
// - output.
// - serving path.
return &manager{
embedded: embedded,
folder: os.DirFS("public"),
// NewManager returns a new manager that wraps the given fs.FS.
func NewManager(embedded fs.FS, servingPath string) *manager {
servingPath = path.Clean(servingPath)
if servingPath == "." {
servingPath = "/"
}

inputFolder: "internal/assets",
outputFolder: "public",
servingPath: "/public/*",
if !strings.HasPrefix(servingPath, "/") {
servingPath = "/" + servingPath
}

fileToHash: map[string]string{},
HashToFile: map[string]string{},
if !strings.HasSuffix(servingPath, "/") {
servingPath += "/"
}

return &manager{
embedded: embedded,
servingPath: servingPath,
fileToHash: map[string]string{},
HashToFile: map[string]string{},
}
}
Loading

0 comments on commit cb99de5

Please sign in to comment.