1- import {
2- createClient ,
3- InitializerEntry ,
4- LDOptions ,
5- ModeDefinition ,
6- SynchronizerEntry ,
7- } from '@launchdarkly/js-client-sdk' ;
1+ import { createClient , LDContext , LDOptions } from '@launchdarkly/js-client-sdk' ;
82import {
93 ClientEntity ,
104 CreateInstanceParams ,
115 IClientEntity ,
12- makeDefaultInitialContext ,
13- makeSdkConfig ,
14- SDKConfigDataInitializer ,
15- SDKConfigDataSynchronizer ,
16- SDKConfigModeDefinition ,
17- SDKConfigParams ,
6+ parseClientOptions ,
187} from '@launchdarkly/js-contract-test-utils/client' ;
198
20- function translateInitializer ( init : SDKConfigDataInitializer ) : InitializerEntry | undefined {
21- if ( init . polling ) {
22- return {
23- type : 'polling' ,
24- ...( init . polling . pollIntervalMs !== undefined && {
25- pollInterval : init . polling . pollIntervalMs / 1000 ,
26- } ) ,
27- ...( init . polling . baseUri && {
28- endpoints : { pollingBaseUri : init . polling . baseUri } ,
29- } ) ,
30- } ;
31- }
32- return undefined ;
33- }
34-
35- function translateSynchronizer ( sync : SDKConfigDataSynchronizer ) : SynchronizerEntry | undefined {
36- if ( sync . streaming ) {
37- return {
38- type : 'streaming' ,
39- ...( sync . streaming . initialRetryDelayMs !== undefined && {
40- initialReconnectDelay : sync . streaming . initialRetryDelayMs / 1000 ,
41- } ) ,
42- ...( sync . streaming . baseUri && {
43- endpoints : { streamingBaseUri : sync . streaming . baseUri } ,
44- } ) ,
45- } ;
46- }
47- if ( sync . polling ) {
48- return {
49- type : 'polling' ,
50- ...( sync . polling . pollIntervalMs !== undefined && {
51- pollInterval : sync . polling . pollIntervalMs / 1000 ,
52- } ) ,
53- ...( sync . polling . baseUri && {
54- endpoints : { pollingBaseUri : sync . polling . baseUri } ,
55- } ) ,
56- } ;
57- }
58- return undefined ;
59- }
60-
61- function translateModeDefinition ( modeDef : SDKConfigModeDefinition ) : ModeDefinition {
62- const initializers : InitializerEntry [ ] = ( modeDef . initializers ?? [ ] )
63- . map ( translateInitializer )
64- . filter ( ( x ) : x is InitializerEntry => x !== undefined ) ;
65-
66- const synchronizers : SynchronizerEntry [ ] = ( modeDef . synchronizers ?? [ ] )
67- . map ( translateSynchronizer )
68- . filter ( ( x ) : x is SynchronizerEntry => x !== undefined ) ;
69-
70- return { initializers, synchronizers } ;
71- }
72-
73- /**
74- * Browser-specific makeSdkConfig that wraps the shared base config with
75- * FDv2 data system translation and browser-specific options.
76- */
77- function makeBrowserSdkConfig ( options : SDKConfigParams , tag : string ) : LDOptions {
78- const isSet = ( x ?: unknown ) => x !== null && x !== undefined ;
79- const maybeTime = ( seconds ?: number ) => ( isSet ( seconds ) ? seconds ! / 1000 : undefined ) ;
80-
81- const cf = { ...makeSdkConfig ( options , tag ) , fetchGoals : false } as LDOptions ;
82-
83- if ( options . dataSystem ?. payloadFilter ) {
84- cf . payloadFilterKey = options . dataSystem . payloadFilter ;
85- }
86-
87- if ( options . dataSystem ) {
88- const dataSystem : any = { } ;
89-
90- if ( options . dataSystem . connectionModeConfig ) {
91- const connMode = options . dataSystem . connectionModeConfig ;
92- dataSystem . automaticModeSwitching = connMode . initialConnectionMode
93- ? { type : 'manual' , initialConnectionMode : connMode . initialConnectionMode }
94- : false ;
95-
96- if ( connMode . customConnectionModes ) {
97- const connectionModes : Record < string , any > = { } ;
98- Object . entries ( connMode . customConnectionModes ) . forEach ( ( [ modeName , modeDef ] ) => {
99- connectionModes [ modeName ] = translateModeDefinition ( modeDef ) ;
100-
101- // Per-entry endpoint overrides also set global URIs for ServiceEndpoints
102- // compatibility. These override the serviceEndpoints values above.
103- ( modeDef . synchronizers ?? [ ] ) . forEach ( ( sync ) => {
104- if ( sync . streaming ?. baseUri ) {
105- cf . streamUri = sync . streaming . baseUri ;
106- cf . streamInitialReconnectDelay = maybeTime ( sync . streaming . initialRetryDelayMs ) ;
107- }
108- if ( sync . polling ?. baseUri ) {
109- cf . baseUri = sync . polling . baseUri ;
110- }
111- } ) ;
112- ( modeDef . initializers ?? [ ] ) . forEach ( ( init ) => {
113- if ( init . polling ?. baseUri ) {
114- cf . baseUri = init . polling . baseUri ;
115- }
116- } ) ;
117- } ) ;
118- dataSystem . connectionModes = connectionModes ;
119- }
120- }
121-
122- ( cf as any ) . dataSystem = dataSystem ;
123- }
124-
125- return cf ;
126- }
127-
1289export async function newSdkClientEntity (
12910 _id : string ,
13011 options : CreateInstanceParams ,
13112) : Promise < IClientEntity > {
132- const timeout =
133- options . configuration . startWaitTimeMs !== null &&
134- options . configuration . startWaitTimeMs !== undefined
135- ? options . configuration . startWaitTimeMs
136- : 5000 ;
137-
138- const sdkConfig = makeBrowserSdkConfig ( options . configuration , options . tag ) ;
139- const initialContext =
140- options . configuration . clientSide ?. initialUser ||
141- options . configuration . clientSide ?. initialContext ||
142- makeDefaultInitialContext ( ) ;
13+ const { timeout, sdkConfig, initialContext, credential, initCanFail } =
14+ parseClientOptions ( options ) ;
14315
14416 const client = createClient (
145- options . configuration . credential || 'unknown-env-id' ,
146- initialContext ,
147- sdkConfig ,
17+ credential ,
18+ initialContext as LDContext ,
19+ { ... sdkConfig , fetchGoals : false } as LDOptions ,
14820 ) ;
14921
15022 let failed = false ;
@@ -158,7 +30,7 @@ export async function newSdkClientEntity(
15830 } catch ( _ ) {
15931 failed = true ;
16032 }
161- if ( failed && ! options . configuration . initCanFail ) {
33+ if ( failed && ! initCanFail ) {
16234 client . close ( ) ;
16335 throw new Error ( 'client initialization failed' ) ;
16436 }
0 commit comments