Repository navigation
VFS Support #3
Copy link
Copy link
Open
Description
Activity
Compile GH packages example (not fully working - would need proper AST parser) - not sure what gopherjs can all handle:
async function startGoRuntime(code) { const { compile, format } = await import('https://cdn.jsdelivr.net/npm/@live-codes/go2js'); // 1. Define our Global Monkey Patch for "os" // We create a "virtual" archive for the os package const mockOsCode = ` package os import "fmt" func ReadFile(path string) ([]byte, error) { fmt.Println("PATCH: Intercepted ReadFile for:", path) return []byte("This data came from the JS Monkey Patch!"), nil } func Open(path string) (interface{}, error) { return nil, nil } func MkdirAll(path string, perm uint32) error { return nil } func Remove(path string) error { return nil } `; } /** * Fetches raw .go files from GitHub API and combines them */ /** * Extracts github.com import paths from Go source code * Handles both: import "github.com/user/repo" * and import ( "github.com/user/repo" ) */ function parseGithubImports(source) { // Matches strings starting with github.com inside quotes const importRegex = /"github\.com\/([\w-]+\/[\w-]+)(?:\/[\w-]+)*"/g; const imports = new Set(); let match; while ((match = importRegex.exec(source)) !== null) { // match[1] captures the 'owner/repo' part imports.add(`github.com/${match[1]}`); } return Array.from(imports); } async function fetchAndCompileGithub(pkgPath, visited = new Set()) { if (visited.has(pkgPath)) return ""; visited.add(pkgPath); const parts = pkgPath.replace('github.com/', '').split('/'); const [owner, repo] = parts; const apiUrl = `https://api.github.com/repos/${owner}/${repo}/contents/`; const response = await fetch(apiUrl); const files = await response.json(); // 1. Filter for valid Go files const goFiles = files.filter(f => f.name.endsWith('.go') && !f.name.endsWith('_test.go')); let currentPkgSource = ""; for (const file of goFiles) { const res = await fetch(file.download_url); let text = await res.text(); // Strip package declaration and any "use strict" or JS-like artifacts // This regex removes 'package name' and any comments before it text = text.replace(/^[\s\S]*?package\s+\w+\n?/, ""); currentPkgSource += text + "\n"; } // 2. Extract and resolve dependencies (Recursive) const importRegex = /"github\.com\/([\w-]+\/[\w-]+)(?:\/[\w-]+)*"/g; let match; let dependencySource = ""; while ((match = importRegex.exec(currentPkgSource)) !== null) { const depPath = `github.com/${match[1]}`; dependencySource += await fetchAndCompileGithub(depPath, visited); } // 3. Assemble the Virtual Package // We wrap everything in a single 'package main' so the compiler is happy const finalGoSource = `package main\n\nimport "fmt"\n` + dependencySource + currentPkgSource; // 4. Compile ONLY the final result // If we are at the root of the recursion, compile to JS if (visited.size === 1) { const { compile } = await import('https://cdn.jsdelivr.net/npm/@live-codes/go2js'); console.log("Compiling final Go source to JS..."); return await compile(finalGoSource); } return finalGoSource; }//// // --- TEST RUN --- const myProg = ` package main import ( "fmt" "os" ) func main() { data, _ := os.ReadFile("/any/path/at/all.txt") fmt.Println("Result:", string(data)) } `; console.log(await fetchAndCompileGithub('github.com/tidwall/gjson'))
Metadata
Metadata
Assignees
Labels
No labels
Is there a way we can hard monkey patch os.FileRead?
Options to do this could be:
or update main in complie.go:
Dropping this here since @hatemhosny - will think this is useful.