@@ -120,60 +120,44 @@ export function getDevframeConnection(): DevframeConnection | undefined {
120120export async function setupDevframeConnection (
121121 options : SetupDevframeConnectionOptions = { } ,
122122) : Promise < DevframeConnection > {
123- const isolateConnection = options . isolateConnection === true
124- function resolveAuthToken ( authToken : string | undefined ) : string | undefined {
125- if ( isolateConnection )
126- return authToken
127- return readStoredAuthToken ( authToken )
128- }
129- function finishConnectionSetup ( connection : DevframeConnection ) : DevframeConnection {
130- if ( ! isolateConnection )
131- storeConnection ( connection )
132- return connection
133- }
134- if ( options . connection ) {
135- const connection = withAuthToken (
136- options . connection ,
137- resolveAuthToken (
138- options . authToken
139- ?? options . connection . authToken
140- ?? options . connection . connectionMeta . authToken ,
141- ) ,
142- )
143- return finishConnectionSetup ( connection )
144- }
123+ const connection = await resolveDevframeConnection ( options )
124+ const authToken = options . authToken ?? connection . authToken ?? connection . connectionMeta . authToken
125+ const resolvedConnection = withAuthToken (
126+ connection ,
127+ options . isolateConnection ? authToken : readStoredAuthToken ( authToken ) ,
128+ )
129+ if ( options . isolateConnection )
130+ return resolvedConnection
131+
132+ storeConnection ( resolvedConnection )
133+ return resolvedConnection
134+ }
135+
136+ async function resolveDevframeConnection (
137+ options : SetupDevframeConnectionOptions ,
138+ ) : Promise < DevframeConnection > {
139+ if ( options . connection )
140+ return options . connection
145141
146142 const bases = Array . isArray ( options . baseURL )
147143 ? options . baseURL
148144 : [ options . baseURL ?? './' ]
149145
150146 if ( options . connectionMeta ) {
151- const connection : DevframeConnection = {
147+ return {
152148 connectionMeta : options . connectionMeta ,
153149 /**
154150 * Preserve the established connectionMeta behavior: an explicitly
155151 * supplied descriptor resolves from the caller's explicit base.
156152 */
157153 metaBaseUrl : resolveMetaBaseUrl ( bases [ 0 ] ?? './' ) ,
158- authToken : resolveAuthToken (
159- options . authToken ?? options . connectionMeta . authToken ,
160- ) ,
154+ authToken : options . connectionMeta . authToken ,
161155 }
162- return finishConnectionSetup ( connection )
163156 }
164157
165- const existing = isolateConnection ? undefined : getDevframeConnection ( )
166- if ( existing ) {
167- const connection = withAuthToken (
168- existing ,
169- resolveAuthToken (
170- options . authToken
171- ?? existing . authToken
172- ?? existing . connectionMeta . authToken ,
173- ) ,
174- )
175- return finishConnectionSetup ( connection )
176- }
158+ const existing = options . isolateConnection ? undefined : getDevframeConnection ( )
159+ if ( existing )
160+ return existing
177161
178162 const errors : Error [ ] = [ ]
179163 for ( const base of bases ) {
@@ -186,7 +170,7 @@ export async function setupDevframeConnection(
186170
187171 const connectionMeta = await response . json ( ) as ConnectionMeta
188172 const loadedFrom = response . url || metaUrl
189- const connection : DevframeConnection = {
173+ return {
190174 connectionMeta,
191175 /**
192176 * A served `baseUrl` re-points relative resolution (RPC dump shards,
@@ -196,11 +180,8 @@ export async function setupDevframeConnection(
196180 metaBaseUrl : connectionMeta . baseUrl
197181 ? new URL ( connectionMeta . baseUrl , loadedFrom ) . href
198182 : loadedFrom ,
199- authToken : resolveAuthToken (
200- options . authToken ?? connectionMeta . authToken ,
201- ) ,
183+ authToken : connectionMeta . authToken ,
202184 }
203- return finishConnectionSetup ( connection )
204185 }
205186 catch ( error ) {
206187 errors . push ( error as Error )
0 commit comments