Moved closure-api into a new project

This commit is contained in:
2026-06-12 12:39:43 +08:00
parent c98fcfb640
commit d3072e7e18
14 changed files with 150 additions and 573 deletions
+4 -4
View File
@@ -72,13 +72,13 @@ func (c *CompileCache) worker() {
cancel()
c.mu.Lock()
if err != nil {
c.states[job.Hash] = CompileFailed
c.errors[job.Hash] = err
} else {
if err == nil {
c.states[job.Hash] = CompileReady
c.results[job.Hash] = out
delete(c.errors, job.Hash)
} else {
c.states[job.Hash] = CompileFailed
c.errors[job.Hash] = err
}
c.mu.Unlock()
}
+19 -11
View File
@@ -56,12 +56,25 @@ func (c *Client) Compile(ctx context.Context, reqBody CompileRequest) ([]byte, e
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
return nil, fmt.Errorf("closure failed: %s: %s", resp.Status, body)
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
if err != nil {
return nil, err
}
return io.ReadAll(resp.Body)
if resp.StatusCode/100 != 2 {
return nil, fmt.Errorf("closure failed: %s: %s", resp.Status, respBody)
}
var cr CompileResponse
if err := json.Unmarshal(respBody, &cr); err != nil {
return nil, fmt.Errorf("closure returned invalid json: %w: %s", err, respBody)
}
if !cr.OK {
return nil, fmt.Errorf("closure compile failed: errors=%q warnings=%q", len(cr.Errors), len(cr.Warnings))
}
return []byte(cr.JS), nil
}
func (c *Client) DebugPrintCurl(ctx context.Context, reqBody CompileRequest) {
@@ -75,13 +88,8 @@ func (c *Client) DebugPrintCurl(ctx context.Context, reqBody CompileRequest) {
return
}
tmpDir := os.Getenv("CLOSURE_DEBUG_DIR")
if tmpDir == "" {
tmpDir = os.TempDir()
}
path := filepath.Join(
tmpDir,
os.TempDir(),
fmt.Sprintf("closure-request-%d.json", time.Now().UnixNano()),
)
@@ -92,7 +100,7 @@ func (c *Client) DebugPrintCurl(ctx context.Context, reqBody CompileRequest) {
fmt.Fprintf(
os.Stderr,
"closure debug curl:\n curl -v -X POST %s -H 'Content-Type: application/json' --data-binary @%s\n",
"closure debug curl:\n curl %s -H 'Content-Type: application/json' --data-binary @%s\n",
shellQuote(c.endpoint),
shellQuote(path),
)
+7
View File
@@ -39,3 +39,10 @@ type CompileCache struct {
errors map[string]error
jobs chan CompileJob
}
type CompileResponse struct {
OK bool `json:"ok"`
JS string `json:"js"`
Warnings []string `json:"warnings"`
Errors []string `json:"errors"`
}