1
+ import { getUncompressedSnippetString } from "@tscircuit/create-snippet-url"
1
2
import { 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"
4
4
import { getHtmlForGeneratedUrlPage } from "./get-html-for-generated-url-page"
5
+ import { getIndexPageHtml } from "./get-index-page-html"
6
+ import { getErrorSvg } from "./getErrorSvg"
5
7
6
8
type Result < T , E = Error > = [ T , null ] | [ null , E ]
7
9
@@ -11,33 +13,63 @@ async function unwrapPromise<T>(promise: Promise<T>): Promise<Result<T>> {
11
13
. catch < [ null , Error ] > ( ( err ) => [ null , err ] )
12
14
}
13
15
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
+
14
25
export default async ( req : Request ) => {
15
26
const url = new URL ( req . url . replace ( "/api" , "/" ) )
27
+ const host = `${ url . protocol } //${ url . host } `
16
28
17
29
if ( url . pathname === "/health" ) {
18
30
return new Response ( JSON . stringify ( { ok : true } ) )
19
31
}
20
32
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
+
21
40
if ( url . pathname === "/" && ! url . searchParams . get ( "code" ) ) {
22
41
return new Response ( getIndexPageHtml ( ) , {
23
42
headers : { "Content-Type" : "text/html" } ,
24
43
} )
25
44
}
26
45
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" : `
41
73
import * as UserComponents from "./UserCode.tsx";
42
74
43
75
const hasBoard = ${ userCode . includes ( "<board" ) . toString ( ) } ;
@@ -55,26 +87,25 @@ export default async (req: Request) => {
55
87
)
56
88
);
57
89
` ,
58
- "UserCode.tsx" : userCode ,
59
- } ,
60
- entrypoint : "entrypoint.tsx" ,
61
- } ) ,
62
- )
90
+ "UserCode.tsx" : userCode ,
91
+ } ,
92
+ entrypoint : "entrypoint.tsx" ,
93
+ } ) ,
94
+ )
63
95
64
- if ( executeError ) return errorResponse ( executeError )
96
+ if ( executeError ) return errorResponse ( executeError )
65
97
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 )
68
100
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 )
71
103
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
+ } )
78
109
}
79
110
}
80
111
0 commit comments