Commit 83582f7
Implement new
* Implement fetchProgram builtin for URL-based program execution
Add new fetchProgram builtin that enables patterns to fetch and resolve
programs from URLs, composing cleanly with the existing compileAndRun builtin.
Implementation:
- Created fetch-program.ts builtin following fetchData pattern
- Uses HttpProgramResolver to fetch main file and resolve dependencies
- Uses resolveProgram to handle multi-file imports
- Returns { pending, result: { files, main }, error } structure
- Registered in builtins/index.ts
- Added type definitions to api/index.ts
- Added builder API in builder/built-in.ts
- Created test pattern in patterns/fetch-program-test.tsx
Usage example:
const { result: program } = fetchProgram({ url });
const { result } = compileAndRun({ ...program, input: data });
This enables loading and executing patterns from GitHub URLs or any
HTTP-accessible location, supporting the goal of distributing patterns
as public, reusable components.
* Refactor: Extract shared fetch utilities to eliminate duplication
Extracted common fetch-related utilities (tryClaimMutex, tryWriteResult,
computeInputHash, internalSchema) into a shared fetch-utils.ts module.
Changes:
- Created packages/runner/src/builtins/fetch-utils.ts with shared utilities
- Updated fetch-data.ts to import from fetch-utils.ts
- Updated fetch-program.ts to import from fetch-utils.ts
- Eliminated ~100 lines of duplicated code
Benefits:
- DRY principle - single source of truth for fetch patterns
- Easier maintenance - changes only need to be made once
- Consistent behavior across all fetch-style builtins
- Makes fetch-program.ts configurable with longer timeout (10s vs 5s)
The shared utilities handle:
- Input hashing for change detection
- Mutex claiming for multi-tab coordination
- Result writing with input validation
* Simplify fetchProgram - remove unnecessary complexity
Stripped down fetchProgram from ~300 lines to ~115 lines by removing:
- Mutex/multi-tab coordination (over-engineered for initial version)
- Input hashing and change detection
- AbortController logic
- All the shared fetch-utils machinery
Core logic is now clear:
1. Initialize cells for pending/result/error
2. Fetch URL and resolve program dependencies
3. Update cells with result or error
The builtin now does exactly what it needs to:
- Fetch a program from a URL using HttpProgramResolver
- Resolve all dependencies via resolveProgram
- Return { files, main } structure for compileAndRun
Can always add optimizations later if needed.
* Use shared fetch utilities in fetchProgram for consistency
Updated fetchProgram to use the same fetch-utils.ts patterns as fetchData:
- Multi-tab coordination via tryClaimMutex (prevents duplicate fetches)
- Input hashing via computeInputHash (detects URL changes)
- Safe result writing via tryWriteResult (any tab can write if inputs match)
- AbortController support for cancellation
- Proper cleanup on recipe stop
This ensures both fetch-style builtins follow the same pattern for:
- Avoiding duplicate network requests across tabs
- Handling race conditions properly
- Consistent timeout and retry behavior
fetchProgram uses 10s timeout (vs fetchData's 5s) since program
resolution with dependencies can take longer than simple HTTP fetches.
* It's alive!
* Format pass
* Fix lint + format
* Exclude result from hash computation
* Tighten retry and de-duplication logic
* Clear result if input to fetchProgram cleared
* Format + lint
* Fix tests
* Address code review
* Action code review feedback
* One more pass over the state machine
---------
Co-authored-by: Claude <[email protected]>fetchProgram built-in using HTTPProgramResolver (commontoolsinc#2049)1 parent 9f75a14 commit 83582f7
File tree
11 files changed
+626
-161
lines changed- packages
- api
- js-runtime/typescript
- patterns
- runner/src
- builder
- builtins
11 files changed
+626
-161
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1183 | 1183 | | |
1184 | 1184 | | |
1185 | 1185 | | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
1186 | 1197 | | |
1187 | 1198 | | |
1188 | 1199 | | |
| |||
1255 | 1266 | | |
1256 | 1267 | | |
1257 | 1268 | | |
| 1269 | + | |
1258 | 1270 | | |
1259 | 1271 | | |
1260 | 1272 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
74 | 88 | | |
75 | 89 | | |
76 | 90 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| 96 | + | |
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
254 | 255 | | |
255 | 256 | | |
256 | 257 | | |
| 258 | + | |
257 | 259 | | |
258 | 260 | | |
259 | 261 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
143 | 164 | | |
144 | 165 | | |
145 | 166 | | |
| |||
153 | 174 | | |
154 | 175 | | |
155 | 176 | | |
156 | | - | |
| 177 | + | |
157 | 178 | | |
158 | 179 | | |
159 | 180 | | |
| |||
0 commit comments