|
| 1 | +const ld = require('launchdarkly-node-server-sdk'); |
| 2 | + |
| 3 | +const { Log, sdkLogger } = require('./log'); |
| 4 | + |
| 5 | +const badCommandError = new Error('unsupported command'); |
| 6 | + |
| 7 | +function makeSdkConfig(options, tag) { |
| 8 | + const cf = { |
| 9 | + logger: sdkLogger(tag), |
| 10 | + }; |
| 11 | + const maybeTime = seconds => (seconds === undefined || seconds === null ? undefined : seconds / 1000); |
| 12 | + if (options.streaming) { |
| 13 | + cf.streamUri = options.streaming.baseUri; |
| 14 | + cf.streamInitialReconnectDelay = maybeTime(options.streaming.initialRetryDelayMs); |
| 15 | + } |
| 16 | + if (options.events) { |
| 17 | + cf.allAttributesPrivate = options.events.allAttributesPrivate; |
| 18 | + cf.eventsUri = options.events.baseUri; |
| 19 | + cf.capacity = options.events.capacity; |
| 20 | + cf.diagnosticOptOut = !options.events.enableDiagnostics; |
| 21 | + cf.flushInterval = maybeTime(options.events.flushIntervalMs); |
| 22 | + cf.inlineUsersInEvents = options.events.inlineUsers; |
| 23 | + cf.privateAttributeNames = options.events.globalPrivateAttributes; |
| 24 | + } |
| 25 | + return cf; |
| 26 | +} |
| 27 | + |
| 28 | +async function newSdkClientEntity(options) { |
| 29 | + const c = {}; |
| 30 | + const log = Log(options.tag); |
| 31 | + |
| 32 | + log.info('Creating client with configuration: ' + JSON.stringify(options.configuration)); |
| 33 | + const timeout = |
| 34 | + options.configuration.startWaitTimeMs !== null && options.configuration.startWaitTimeMs !== undefined |
| 35 | + ? options.configuration.startWaitTimeMs |
| 36 | + : 5000; |
| 37 | + const client = ld.init( |
| 38 | + options.configuration.credential || 'unknown-sdk-key', |
| 39 | + makeSdkConfig(options.configuration, options.tag) |
| 40 | + ); |
| 41 | + try { |
| 42 | + await Promise.race([client.waitForInitialization(), new Promise(resolve => setTimeout(resolve, timeout))]); |
| 43 | + } catch (_) { |
| 44 | + // if waitForInitialization() rejects, the client failed to initialize, see next line |
| 45 | + } |
| 46 | + if (!client.initialized() && !options.configuration.initCanFail) { |
| 47 | + client.close(); |
| 48 | + throw new Error('client initialization failed'); |
| 49 | + } |
| 50 | + |
| 51 | + c.close = () => { |
| 52 | + client.close(); |
| 53 | + log.info('Test ended'); |
| 54 | + }; |
| 55 | + |
| 56 | + c.doCommand = async params => { |
| 57 | + log.info('Received command: ' + params.command); |
| 58 | + switch (params.command) { |
| 59 | + case 'evaluate': { |
| 60 | + const pe = params.evaluate; |
| 61 | + if (pe.detail) { |
| 62 | + return await client.variationDetail(pe.flagKey, pe.user, pe.defaultValue); |
| 63 | + } else { |
| 64 | + const value = await client.variation(pe.flagKey, pe.user, pe.defaultValue); |
| 65 | + return { value }; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + case 'evaluateAll': { |
| 70 | + const pea = params.evaluateAll; |
| 71 | + const eao = { |
| 72 | + clientSideOnly: pea.clientSideOnly, |
| 73 | + detailsOnlyForTrackedFlags: pea.detailsOnlyForTrackedFlags, |
| 74 | + withReasons: pea.withReasons, |
| 75 | + }; |
| 76 | + return { state: await client.allFlagsState(pea.user, eao) }; |
| 77 | + } |
| 78 | + |
| 79 | + case 'identifyEvent': |
| 80 | + client.identify(params.identifyEvent.user); |
| 81 | + return undefined; |
| 82 | + |
| 83 | + case 'customEvent': { |
| 84 | + const pce = params.customEvent; |
| 85 | + client.track(pce.eventKey, pce.user, pce.data, pce.metricValue); |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + |
| 89 | + case 'aliasEvent': |
| 90 | + client.alias(params.aliasEvent.user, params.aliasEvent.previousUser); |
| 91 | + return undefined; |
| 92 | + |
| 93 | + case 'flushEvents': |
| 94 | + client.flush(); |
| 95 | + return undefined; |
| 96 | + |
| 97 | + case 'getBigSegmentStoreStatus': |
| 98 | + return undefined; |
| 99 | + |
| 100 | + default: |
| 101 | + throw badCommandError; |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + return c; |
| 106 | +} |
| 107 | + |
| 108 | +module.exports.newSdkClientEntity = newSdkClientEntity; |
| 109 | +module.exports.badCommandError = badCommandError; |
0 commit comments