Commit e0befe94 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

freeze collects static files

parent f22fa649
Loading
Loading
Loading
Loading
+59 −2
Changes for freeze.go: 59 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -35,7 +35,8 @@ func runFreeze(output, directory string, debug bool) error {
	if output == "" {
		output = filepath.Join(directory, "html_output")
	}
	if _, err := loadProjectConfig(filepath.Join(directory, maxConfigFile)); err != nil {
	config, err := loadProjectConfig(filepath.Join(directory, maxConfigFile))
	if err != nil {
		return err
	}
	portNumber, err := freePort()
@@ -54,6 +55,9 @@ func runFreeze(output, directory string, debug bool) error {
	if freezeErr != nil {
		return freezeErr
	}
	if err := copyActiveBundleStaticDirs(filepath.Dir(config.Path), config, output); err != nil {
		return err
	}
	fmt.Printf("%s\n", T("msg.site_copied", output))
	return nil
}
@@ -247,6 +251,53 @@ func freezeSite(startURL, output string) error {
	return firstErr
}

// Bundle static folders are not always reachable through rendered pages, so
// freeze copies them explicitly after the crawl.
func copyActiveBundleStaticDirs(projectDir string, config *projectConfig, outputDir string) error {
	bundlesRoot := filepath.Join(projectDir, ".max", "basex", "webapp", "max", "bundles")
	for _, bundleName := range sortedMapKeys(config.bundlesMap()) {
		name := strings.TrimSpace(bundleName)
		if name == "" {
			continue
		}
		src := filepath.Join(bundlesRoot, name, "static")
		if !isPathInside(bundlesRoot, src) {
			return fmt.Errorf(T("err.invalid_mirrored_path"), src)
		}
		if !dirExists(src) {
			continue
		}
		dst := filepath.Join(outputDir, name, "static")
		if !isPathInside(outputDir, dst) {
			return fmt.Errorf(T("err.invalid_mirrored_path"), dst)
		}
		if err := copyMissingDir(src, dst); err != nil {
			return err
		}
	}
	return nil
}

func copyMissingDir(src, dst string) error {
	return filepath.WalkDir(src, func(path string, d os.DirEntry, err error) error {
		if err != nil {
			return err
		}
		rel, err := filepath.Rel(src, path)
		if err != nil {
			return err
		}
		target := filepath.Join(dst, rel)
		if d.IsDir() {
			return os.MkdirAll(target, 0o755)
		}
		if fileExists(target) {
			return nil
		}
		return copyFile(path, target)
	})
}

// HTML and CSS are rewritten as they are saved so the frozen tree is portable
// and does not point back to the temporary local server.
func saveMirroredResponse(u *url.URL, body []byte, contentType, outputDir string) error {
@@ -312,12 +363,18 @@ func mapURLToLocalPath(u *url.URL, contentType, outputDir string) (string, error
	}
	cleanOutput := filepath.Clean(outputDir)
	cleanPath := filepath.Clean(full)
	if !strings.HasPrefix(cleanPath, cleanOutput+string(os.PathSeparator)) && cleanPath != cleanOutput {
	if !isPathInside(cleanOutput, cleanPath) {
		return "", fmt.Errorf(T("err.invalid_mirrored_path"), cleanPath)
	}
	return cleanPath, nil
}

func isPathInside(root, candidate string) bool {
	cleanRoot := filepath.Clean(root)
	cleanCandidate := filepath.Clean(candidate)
	return cleanCandidate == cleanRoot || strings.HasPrefix(cleanCandidate, cleanRoot+string(os.PathSeparator))
}

func isHTMLContentType(contentType, reqPath string) bool {
	ct := strings.ToLower(contentType)
	rp := strings.ToLower(reqPath)
+1 −1
Changes for main.go: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ import (
//go:embed max_releases.json
var fallbackReleases []byte

const version = "0.4.9"
const version = "0.4.10"

const maxConfigFile = "config.xml"
const minSupportedMaxVersion = "0.0.5"
+83
Changes for main_test.go: 83 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -385,6 +385,89 @@ func TestFreezeSiteCrawlsOnclickLocalLinks(t *testing.T) {
	}
}

// Covers bundle assets that are not necessarily linked from crawled pages: only
// active bundles with a static directory fill missing files in the frozen tree.
func TestFreezeCopiesActiveBundleStaticDirs(t *testing.T) {
	projectDir := t.TempDir()
	configPath := filepath.Join(projectDir, maxConfigFile)
	configText := `<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://certic.unicaen.fr/max/ns/1.0" env="dev">
    <version name="0.0.5"/>
    <languages/>
    <title>Fixture</title>
    <bundles>
        <bundle name="max-db-bundle"/>
        <bundle name="max-no-static"/>
    </bundles>
</configuration>
`
	if err := os.WriteFile(configPath, []byte(configText), 0o644); err != nil {
		t.Fatal(err)
	}

	staticFile := filepath.Join(projectDir, ".max", "basex", "webapp", "max", "bundles", "max-db-bundle", "static", "css", "app.css")
	if err := os.MkdirAll(filepath.Dir(staticFile), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(staticFile, []byte("body{color:red}"), 0o644); err != nil {
		t.Fatal(err)
	}
	crawledFile := filepath.Join(projectDir, ".max", "basex", "webapp", "max", "bundles", "max-db-bundle", "static", "js", "app.js")
	if err := os.MkdirAll(filepath.Dir(crawledFile), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(crawledFile, []byte("from bundle"), 0o644); err != nil {
		t.Fatal(err)
	}

	inactiveFile := filepath.Join(projectDir, ".max", "basex", "webapp", "max", "bundles", "max-inactive", "static", "hidden.txt")
	if err := os.MkdirAll(filepath.Dir(inactiveFile), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(inactiveFile, []byte("inactive"), 0o644); err != nil {
		t.Fatal(err)
	}

	config, err := loadProjectConfig(configPath)
	if err != nil {
		t.Fatal(err)
	}
	output := t.TempDir()
	existingOutputFile := filepath.Join(output, "max-db-bundle", "static", "js", "app.js")
	if err := os.MkdirAll(filepath.Dir(existingOutputFile), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(existingOutputFile, []byte("from crawl"), 0o644); err != nil {
		t.Fatal(err)
	}

	if err := copyActiveBundleStaticDirs(projectDir, config, output); err != nil {
		t.Fatal(err)
	}

	copiedFile := filepath.Join(output, "max-db-bundle", "static", "css", "app.css")
	copied, err := os.ReadFile(copiedFile)
	if err != nil {
		t.Fatal(err)
	}
	if string(copied) != "body{color:red}" {
		t.Fatalf("unexpected copied bundle static content: %s", copied)
	}
	preserved, err := os.ReadFile(existingOutputFile)
	if err != nil {
		t.Fatal(err)
	}
	if string(preserved) != "from crawl" {
		t.Fatalf("expected existing crawled static file to be preserved, got: %s", preserved)
	}
	if fileExists(filepath.Join(output, "max-no-static")) {
		t.Fatalf("expected active bundle without static dir to be skipped")
	}
	if fileExists(filepath.Join(output, "max-inactive")) {
		t.Fatalf("expected inactive bundle static dir not to be copied")
	}
}

// Exercises the JSON-backed project registry from first ping through metadata
// update and deletion, keeping SQLite-era behavior from returning.
func TestProjectsJSONStorageLifecycle(t *testing.T) {