1+ import { getUncompressedSnippetString } from "@tscircuit/create-snippet-url"
12import { CircuitRunner } from "@tscircuit/eval/eval"
2- import { getErrorSvg } from "./getErrorSvg"
3- import { getIndexPageHtml } from "./get-index-page-html"
3+ import { circuitJsonPreviewHtml } from "./circuit-json-preview-html"
44import { getHtmlForGeneratedUrlPage } from "./get-html-for-generated-url-page"
5+ import { getIndexPageHtml } from "./get-index-page-html"
6+ import { getErrorSvg } from "./getErrorSvg"
57
68type Result < T , E = Error > = [ T , null ] | [ null , E ]
79
@@ -11,33 +13,63 @@ async function unwrapPromise<T>(promise: Promise<T>): Promise<Result<T>> {
1113 . catch < [ null , Error ] > ( ( err ) => [ null , err ] )
1214}
1315
16+ function unwrapSyncError < T > ( fn : ( ) => T ) : Result < T > {
17+ try {
18+ return [ fn ( ) , null ]
19+ } catch ( err ) {
20+ return [ null , err as Error ]
21+ }
22+ }
23+
24+
1425export default async ( req : Request ) => {
1526 const url = new URL ( req . url . replace ( "/api" , "/" ) )
27+ const host = `${ url . protocol } //${ url . host } `
1628
1729 if ( url . pathname === "/health" ) {
1830 return new Response ( JSON . stringify ( { ok : true } ) )
1931 }
2032
33+ if ( url . pathname === "/generate_url" ) {
34+ const code = url . searchParams . get ( "code" )
35+ return new Response ( await getHtmlForGeneratedUrlPage ( code ! , host ) , {
36+ headers : { "Content-Type" : "text/html" } ,
37+ } )
38+ }
39+
2140 if ( url . pathname === "/" && ! url . searchParams . get ( "code" ) ) {
2241 return new Response ( getIndexPageHtml ( ) , {
2342 headers : { "Content-Type" : "text/html" } ,
2443 } )
2544 }
2645
27- if ( url . pathname === "/start_runframe" && url . searchParams . get ( "code" ) ) {
28- const userCode = url . searchParams . get ( "code" )
29- if ( ! userCode ) {
30- return new Response (
31- JSON . stringify ( { ok : false , error : "No code parameter provided" } ) ,
32- { status : 400 } ,
33- )
34- }
35- const worker = new CircuitRunner ( )
36-
37- const [ , executeError ] = await unwrapPromise (
38- worker . executeWithFsMap ( {
39- fsMap : {
40- "entrypoint.tsx" : `
46+ const rawCode = url . searchParams . get ( "code" )
47+ if ( ! rawCode ) {
48+ return new Response (
49+ JSON . stringify ( { ok : false , error : "No code parameter provided" } ) ,
50+ { status : 400 } ,
51+ )
52+ }
53+
54+ const gzipPrefix = "data:application/gzip;base64,"
55+
56+ const normalizedCode = rawCode . startsWith ( gzipPrefix )
57+ ? rawCode
58+ : gzipPrefix + rawCode
59+
60+ const [ userCode , userCodeErr ] = unwrapSyncError ( ( ) =>
61+ getUncompressedSnippetString ( normalizedCode ) ,
62+ )
63+ if ( userCodeErr ) {
64+ return errorResponse ( userCodeErr )
65+ }
66+
67+ const worker = new CircuitRunner ( )
68+
69+ const [ , executeError ] = await unwrapPromise (
70+ worker . executeWithFsMap ( {
71+ fsMap : {
72+ "entrypoint.tsx" : `
4173 import * as UserComponents from "./UserCode.tsx";
4274
4375 const hasBoard = ${ userCode . includes ( "<board" ) . toString ( ) } ;
@@ -55,26 +87,25 @@ export default async (req: Request) => {
5587 )
5688 );
5789 ` ,
58- "UserCode.tsx" : userCode ,
59- } ,
60- entrypoint : "entrypoint.tsx" ,
61- } ) ,
62- )
90+ "UserCode.tsx" : userCode ,
91+ } ,
92+ entrypoint : "entrypoint.tsx" ,
93+ } ) ,
94+ )
6395
64- if ( executeError ) return errorResponse ( executeError )
96+ if ( executeError ) return errorResponse ( executeError )
6597
66- const [ , renderError ] = await unwrapPromise ( worker . renderUntilSettled ( ) )
67- if ( renderError ) return errorResponse ( renderError )
98+ const [ , renderError ] = await unwrapPromise ( worker . renderUntilSettled ( ) )
99+ if ( renderError ) return errorResponse ( renderError )
68100
69- const [ circuitJson , jsonError ] = await unwrapPromise ( worker . getCircuitJson ( ) )
70- if ( jsonError ) return errorResponse ( jsonError )
101+ const [ circuitJson , jsonError ] = await unwrapPromise ( worker . getCircuitJson ( ) )
102+ if ( jsonError ) return errorResponse ( jsonError )
71103
72- if ( circuitJson ) {
73- const html = await getHtmlForGeneratedUrlPage ( circuitJson as any )
74- return new Response ( html , {
75- headers : { "Content-Type" : "text/html" } ,
76- } )
77- }
104+ if ( circuitJson ) {
105+ const html = await circuitJsonPreviewHtml ( circuitJson as any )
106+ return new Response ( html , {
107+ headers : { "Content-Type" : "text/html" } ,
108+ } )
78109 }
79110}
80111
0 commit comments