1- import ld from 'node-server-sdk' ;
1+ import got from 'got' ;
2+ import ld , {
3+ createMigration ,
4+ LDConcurrentExecution ,
5+ LDExecutionOrdering ,
6+ LDMigrationError ,
7+ LDMigrationSuccess ,
8+ LDSerialExecution ,
9+ } from 'node-server-sdk' ;
210
311import BigSegmentTestStore from './BigSegmentTestStore.js' ;
412import { Log , sdkLogger } from './log.js' ;
@@ -9,7 +17,7 @@ export { badCommandError };
917export function makeSdkConfig ( options , tag ) {
1018 const cf = {
1119 logger : sdkLogger ( tag ) ,
12- diagnosticOptOut : true
20+ diagnosticOptOut : true ,
1321 } ;
1422 const maybeTime = ( seconds ) =>
1523 seconds === undefined || seconds === null ? undefined : seconds / 1000 ;
@@ -55,6 +63,30 @@ export function makeSdkConfig(options, tag) {
5563 return cf ;
5664}
5765
66+ function getExecution ( order ) {
67+ switch ( order ) {
68+ case 'serial' : {
69+ return new LDSerialExecution ( LDExecutionOrdering . Fixed ) ;
70+ }
71+ case 'random' : {
72+ return new LDSerialExecution ( LDExecutionOrdering . Random ) ;
73+ }
74+ case 'concurrent' : {
75+ return new LDConcurrentExecution ( ) ;
76+ }
77+ default : {
78+ throw new Error ( 'Unsupported execution order.' ) ;
79+ }
80+ }
81+ }
82+
83+ function makeMigrationPostOptions ( payload ) {
84+ if ( payload ) {
85+ return { body : payload } ;
86+ }
87+ return { } ;
88+ }
89+
5890export async function newSdkClientEntity ( options ) {
5991 const c = { } ;
6092 const log = Log ( options . tag ) ;
@@ -93,10 +125,65 @@ export async function newSdkClientEntity(options) {
93125 case 'evaluate' : {
94126 const pe = params . evaluate ;
95127 if ( pe . detail ) {
96- return await client . variationDetail ( pe . flagKey , pe . context || pe . user , pe . defaultValue ) ;
128+ switch ( pe . valueType ) {
129+ case 'bool' :
130+ return await client . boolVariationDetail (
131+ pe . flagKey ,
132+ pe . context || pe . user ,
133+ pe . defaultValue ,
134+ ) ;
135+ case 'int' : // Intentional fallthrough.
136+ case 'double' :
137+ return await client . numberVariationDetail (
138+ pe . flagKey ,
139+ pe . context || pe . user ,
140+ pe . defaultValue ,
141+ ) ;
142+ case 'string' :
143+ return await client . stringVariationDetail (
144+ pe . flagKey ,
145+ pe . context || pe . user ,
146+ pe . defaultValue ,
147+ ) ;
148+ default :
149+ return await client . variationDetail (
150+ pe . flagKey ,
151+ pe . context || pe . user ,
152+ pe . defaultValue ,
153+ ) ;
154+ }
97155 } else {
98- const value = await client . variation ( pe . flagKey , pe . context || pe . user , pe . defaultValue ) ;
99- return { value } ;
156+ switch ( pe . valueType ) {
157+ case 'bool' :
158+ return {
159+ value : await client . boolVariation (
160+ pe . flagKey ,
161+ pe . context || pe . user ,
162+ pe . defaultValue ,
163+ ) ,
164+ } ;
165+ case 'int' : // Intentional fallthrough.
166+ case 'double' :
167+ return {
168+ value : await client . numberVariation (
169+ pe . flagKey ,
170+ pe . context || pe . user ,
171+ pe . defaultValue ,
172+ ) ,
173+ } ;
174+ case 'string' :
175+ return {
176+ value : await client . stringVariation (
177+ pe . flagKey ,
178+ pe . context || pe . user ,
179+ pe . defaultValue ,
180+ ) ,
181+ } ;
182+ default :
183+ return {
184+ value : await client . variation ( pe . flagKey , pe . context || pe . user , pe . defaultValue ) ,
185+ } ;
186+ }
100187 }
101188 }
102189
@@ -127,6 +214,101 @@ export async function newSdkClientEntity(options) {
127214 case 'getBigSegmentStoreStatus' :
128215 return await client . bigSegmentStoreStatusProvider . requireStatus ( ) ;
129216
217+ case 'migrationVariation' :
218+ const migrationVariation = params . migrationVariation ;
219+ const res = await client . migrationVariation (
220+ migrationVariation . key ,
221+ migrationVariation . context ,
222+ migrationVariation . defaultStage ,
223+ ) ;
224+ return { result : res . value } ;
225+
226+ case 'migrationOperation' :
227+ const migrationOperation = params . migrationOperation ;
228+ const readExecutionOrder = migrationOperation . readExecutionOrder ;
229+
230+ const migration = createMigration ( client , {
231+ execution : getExecution ( readExecutionOrder ) ,
232+ latencyTracking : migrationOperation . trackLatency ,
233+ errorTracking : migrationOperation . trackErrors ,
234+ check : migrationOperation . trackConsistency ? ( a , b ) => a === b : undefined ,
235+ readNew : async ( payload ) => {
236+ try {
237+ const res = await got . post (
238+ migrationOperation . newEndpoint ,
239+ makeMigrationPostOptions ( payload ) ,
240+ ) ;
241+ return LDMigrationSuccess ( res . body ) ;
242+ } catch ( err ) {
243+ return LDMigrationError ( err . message ) ;
244+ }
245+ } ,
246+ writeNew : async ( payload ) => {
247+ try {
248+ const res = await got . post (
249+ migrationOperation . newEndpoint ,
250+ makeMigrationPostOptions ( payload ) ,
251+ ) ;
252+ return LDMigrationSuccess ( res . body ) ;
253+ } catch ( err ) {
254+ return LDMigrationError ( err . message ) ;
255+ }
256+ } ,
257+ readOld : async ( payload ) => {
258+ try {
259+ const res = await got . post (
260+ migrationOperation . oldEndpoint ,
261+ makeMigrationPostOptions ( payload ) ,
262+ ) ;
263+ return LDMigrationSuccess ( res . body ) ;
264+ } catch ( err ) {
265+ return LDMigrationError ( err . message ) ;
266+ }
267+ } ,
268+ writeOld : async ( payload ) => {
269+ try {
270+ const res = await got . post (
271+ migrationOperation . oldEndpoint ,
272+ makeMigrationPostOptions ( payload ) ,
273+ ) ;
274+ return LDMigrationSuccess ( res . body ) ;
275+ } catch ( err ) {
276+ return LDMigrationError ( err . message ) ;
277+ }
278+ } ,
279+ } ) ;
280+
281+ switch ( migrationOperation . operation ) {
282+ case 'read' : {
283+ const res = await migration . read (
284+ migrationOperation . key ,
285+ migrationOperation . context ,
286+ migrationOperation . defaultStage ,
287+ migrationOperation . payload ,
288+ ) ;
289+ if ( res . success ) {
290+ return { result : res . result } ;
291+ } else {
292+ return { result : res . error } ;
293+ }
294+ }
295+ case 'write' : {
296+ const res = await migration . write (
297+ migrationOperation . key ,
298+ migrationOperation . context ,
299+ migrationOperation . defaultStage ,
300+ migrationOperation . payload ,
301+ ) ;
302+
303+ if ( res . authoritative . success ) {
304+ return { result : res . authoritative . result } ;
305+ } else {
306+ return { result : res . authoritative . error } ;
307+ }
308+ }
309+ }
310+ return undefined ;
311+
130312 default :
131313 throw badCommandError ;
132314 }
0 commit comments