1- import {
2- DEFAULT_SCONE_VERSION ,
3- SCONIFY_API_HTTP_URL ,
4- SCONIFY_API_WS_URL ,
5- } from '../config/config.js' ;
1+ import { DEFAULT_SCONE_VERSION , SCONIFY_API_WS_URL } from '../config/config.js' ;
62import { getAuthToken } from './dockerhub.js' ;
73import { sleep } from './sleep.js' ;
84import {
@@ -11,7 +7,6 @@ import {
117 serializeData ,
128} from './websocket.js' ;
139import { debug } from './debug.js' ;
14- import { useWsApi } from './featureFlags.js' ;
1510
1611const INITIAL_RETRY_PERIOD = 20 * 1000 ; // 20s
1712
@@ -56,133 +51,86 @@ export async function sconify({
5651
5752 const pushToken = await getPushToken ( ) ;
5853
59- let sconifyResult : {
54+ const sconifyResult : {
6055 dockerImage ?: string ;
6156 dockerImageDigest ?: string ;
6257 entrypoint ?: string ;
6358 fingerprint ?: string ;
6459 sconeVersion ?: string ;
65- } ;
66-
67- if ( useWsApi ) {
68- // experimental ws connection
69- sconifyResult = await new Promise ( ( resolve , reject ) => {
70- createReconnectingWs ( SCONIFY_API_WS_URL , {
71- headers : {
72- 'x-wallet' : walletAddress ,
73- } ,
74- connectCallback : ( ws ) => {
75- const handleError = ( e : unknown ) => {
76- ws . close ( 1000 ) ; // normal ws close
77- reject ( e ) ;
78- } ;
60+ } = await new Promise ( ( resolve , reject ) => {
61+ createReconnectingWs ( SCONIFY_API_WS_URL , {
62+ headers : {
63+ 'x-wallet' : walletAddress ,
64+ } ,
65+ connectCallback : ( ws ) => {
66+ const handleError = ( e : unknown ) => {
67+ ws . close ( 1000 ) ; // normal ws close
68+ reject ( e ) ;
69+ } ;
7970
80- ws . on ( 'message' , ( data ) => {
81- let message ;
82- // handle communication errors
83- try {
84- message = deserializeData ( data ) ;
85- debug ( `ws message: ${ JSON . stringify ( message , undefined , 2 ) } ` ) ;
86- } catch ( e ) {
87- handleError ( e ) ;
88- }
71+ ws . on ( 'message' , ( data ) => {
72+ let message ;
73+ // handle communication errors
74+ try {
75+ message = deserializeData ( data ) ;
76+ debug ( `ws message: ${ JSON . stringify ( message , undefined , 2 ) } ` ) ;
77+ } catch ( e ) {
78+ handleError ( e ) ;
79+ }
8980
90- // handle server responses
91- if ( message ?. type === 'RESPONSE' ) {
92- if ( message ?. target === 'SCONIFY_BUILD' ) {
93- ws . close ( 1000 ) ; // normal ws close
94- if ( message ?. success === true && message . result ) {
95- resolve ( message . result ) ;
96- } else {
97- reject ( Error ( message . error || 'Server unknown error' ) ) ;
98- }
81+ // handle server responses
82+ if ( message ?. type === 'RESPONSE' ) {
83+ if ( message ?. target === 'SCONIFY_BUILD' ) {
84+ ws . close ( 1000 ) ; // normal ws close
85+ if ( message ?. success === true && message . result ) {
86+ resolve ( message . result ) ;
87+ } else {
88+ reject ( Error ( message . error || 'Server unknown error' ) ) ;
9989 }
10090 }
91+ }
10192
102- // handle server requests
103- if ( message ?. type === 'REQUEST' ) {
104- if ( message ?. target === 'RENEW_PUSH_TOKEN' ) {
105- getPushToken ( )
106- . then ( ( renewedPushToken ) => {
107- ws . send (
108- serializeData ( {
109- type : 'RESPONSE' ,
110- target : 'RENEW_PUSH_TOKEN' ,
111- result : {
112- dockerhubPushToken : renewedPushToken ,
113- } ,
114- } )
115- ) ;
116- } )
117- . catch ( handleError ) ;
118- }
93+ // handle server requests
94+ if ( message ?. type === 'REQUEST' ) {
95+ if ( message ?. target === 'RENEW_PUSH_TOKEN' ) {
96+ getPushToken ( )
97+ . then ( ( renewedPushToken ) => {
98+ ws . send (
99+ serializeData ( {
100+ type : 'RESPONSE' ,
101+ target : 'RENEW_PUSH_TOKEN' ,
102+ result : {
103+ dockerhubPushToken : renewedPushToken ,
104+ } ,
105+ } )
106+ ) ;
107+ } )
108+ . catch ( handleError ) ;
119109 }
110+ }
120111
121- // handle server info
122- if ( message ?. type === 'INFO' ) {
123- // TODO server feedback
124- }
125- } ) ;
126- } ,
127- initCallback : ( ws ) => {
128- ws . send (
129- serializeData ( {
130- type : 'REQUEST' ,
131- target : 'SCONIFY_BUILD' , // call sconify handler
132- template,
133- dockerhubImageToSconify : iAppNameToSconify ,
134- dockerhubPushToken : pushToken ,
135- yourWalletPublicAddress : walletAddress ,
136- sconeVersion : DEFAULT_SCONE_VERSION ,
137- } )
138- ) ;
139- } ,
140- errorCallback : reject ,
141- } ) ;
142- } ) ;
143- } else {
144- // standard http call
145- sconifyResult = await fetch ( `${ SCONIFY_API_HTTP_URL } /sconify/build` , {
146- method : 'POST' ,
147- headers : {
148- 'Content-Type' : 'application/json' ,
149- 'x-wallet' : walletAddress ,
112+ // handle server info
113+ if ( message ?. type === 'INFO' ) {
114+ // TODO server feedback
115+ }
116+ } ) ;
150117 } ,
151- body : JSON . stringify ( {
152- template,
153- dockerhubImageToSconify : iAppNameToSconify ,
154- dockerhubPushToken : pushToken , // used for pushing sconified image on user repo
155- yourWalletPublicAddress : walletAddress ,
156- sconeVersion : DEFAULT_SCONE_VERSION ,
157- } ) ,
158- } )
159- . catch ( ( ) => {
160- throw Error ( "Can't reach TEE transformation server!" ) ;
161- } )
162- . then ( ( res ) => {
163- if ( res . ok ) {
164- return res . json ( ) . catch ( ( ) => {
165- // failed to parse body
166- throw Error ( 'Unexpected server response' ) ;
167- } ) ;
168- }
169- if ( res . status === 429 ) {
170- throw new TooManyRequestsError (
171- 'TEE transformation server is busy, retry later'
172- ) ;
173- }
174- // try getting error message from json body
175- return res
176- . json ( )
177- . catch ( ( ) => {
178- // failed to parse body
179- throw Error ( 'Unknown server error' ) ;
118+ initCallback : ( ws ) => {
119+ ws . send (
120+ serializeData ( {
121+ type : 'REQUEST' ,
122+ target : 'SCONIFY_BUILD' , // call sconify handler
123+ template,
124+ dockerhubImageToSconify : iAppNameToSconify ,
125+ dockerhubPushToken : pushToken ,
126+ yourWalletPublicAddress : walletAddress ,
127+ sconeVersion : DEFAULT_SCONE_VERSION ,
180128 } )
181- . then ( ( { error } ) => {
182- throw Error ( error || 'Unknown server error' ) ;
183- } ) ;
184- } ) ;
185- }
129+ ) ;
130+ } ,
131+ errorCallback : reject ,
132+ } ) ;
133+ } ) ;
186134
187135 // Extract necessary information
188136 if ( ! sconifyResult . dockerImage ) {
0 commit comments