Skip to content

Commit 2388ba4

Browse files
authored
adjust loader inference (#41)
* adjust loader inference * remove the content assumption
1 parent 6cbee20 commit 2388ba4

File tree

2 files changed

+359
-41
lines changed

2 files changed

+359
-41
lines changed

platform/script/loader/inference.go

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ import (
88
"strings"
99
)
1010

11-
// InferLoader analyzes the input and returns an appropriate loader based on type inference.
12-
// It supports the following input types:
13-
// - string: Infers from URI scheme (http/https -> HTTP, file -> Disk) or content (inline string with base64 decoding)
14-
// - []byte: Returns FromBytes loader
15-
// - io.Reader: Returns FromIoReader loader
16-
// - Loader: Returns as-is
11+
// InferLoader determines the appropriate loader for the given input.
1712
//
18-
// Returns an error if the input type is unsupported or if loader creation fails.
13+
// For string inputs, it applies the following checks in order:
14+
// 1. URI scheme detection (http/https -> HTTP, file -> Disk)
15+
// 2. File path format detection (absolute paths, known extensions)
16+
// 3. Base64 validation (attempts decode)
17+
// 4. String fallback
18+
//
19+
// Other input types:
20+
// - []byte: FromBytes loader
21+
// - io.Reader: FromIoReader loader
22+
// - Loader: returned unchanged
23+
//
24+
// Returns an error for unsupported input types or loader creation failures.
1925
func InferLoader(input any) (Loader, error) {
2026
switch v := input.(type) {
2127
case string:
@@ -32,21 +38,20 @@ func InferLoader(input any) (Loader, error) {
3238
}
3339

3440
// inferFromString analyzes a string input and returns the appropriate loader.
35-
// It checks for URI schemes and falls back to treating the string as inline content.
41+
// It follows this order: scheme check -> file path check -> base64 check -> string fallback.
3642
func inferFromString(input string) (Loader, error) {
3743
input = strings.TrimSpace(input)
3844
if input == "" {
3945
return nil, fmt.Errorf("empty string input")
4046
}
4147

42-
// Try to parse as URL to detect scheme
48+
// 1. Check for URI scheme
4349
if parsed, err := url.Parse(input); err == nil && parsed.Scheme != "" {
4450
switch parsed.Scheme {
4551
case "http", "https":
4652
return NewFromHTTP(input)
4753
case "file":
4854
path := parsed.Path
49-
// Convert relative paths to absolute paths
5055
if !filepath.IsAbs(path) {
5156
absPath, err := filepath.Abs(path)
5257
if err != nil {
@@ -55,26 +60,44 @@ func inferFromString(input string) (Loader, error) {
5560
path = absPath
5661
}
5762
return NewFromDisk(path)
58-
default:
59-
// Unknown scheme, treat as inline content
60-
return NewFromStringBase64(input)
6163
}
6264
}
6365

64-
// Check if it looks like a file path
65-
if filepath.IsAbs(input) || strings.Contains(input, "/") || strings.Contains(input, "\\") {
66+
// 2. Check if it's a valid file path
67+
if isValidFilePath(input) {
68+
path := input
6669
// Convert relative paths to absolute paths
6770
if !filepath.IsAbs(input) {
6871
absPath, err := filepath.Abs(input)
6972
if err != nil {
70-
// If we can't resolve the path, treat as inline content
71-
return NewFromStringBase64(input)
73+
return nil, fmt.Errorf("failed to resolve relative path %q: %w", input, err)
7274
}
73-
input = absPath
75+
path = absPath
7476
}
75-
return NewFromDisk(input)
77+
return NewFromDisk(path)
7678
}
7779

78-
// Default to treating as inline string content
80+
// 3. Use the base64 check to determine if the input is valid base64
81+
// (it returns FromBytes if valid, otherwise falls back to FromString)
7982
return NewFromStringBase64(input)
8083
}
84+
85+
// isValidFilePath checks if a string looks like a valid file path format.
86+
func isValidFilePath(s string) bool {
87+
// Don't consider strings with newlines/carriage returns as file paths
88+
if strings.ContainsAny(s, "\n\r") {
89+
return false
90+
}
91+
92+
// Check for specific script file extensions (case insensitive)
93+
validExtensions := []string{".wasm", ".risor", ".star", ".starlark"}
94+
lower := strings.ToLower(s)
95+
for _, ext := range validExtensions {
96+
if strings.HasSuffix(lower, ext) {
97+
return true
98+
}
99+
}
100+
101+
// Basic file path patterns without filesystem check
102+
return filepath.IsAbs(s)
103+
}

0 commit comments

Comments
 (0)