|
| 1 | +import { Response } from 'express'; |
| 2 | + |
| 3 | +import { LDClient } from '@launchdarkly/js-server-sdk-common'; |
| 4 | +import { init } from '@launchdarkly/shopify-oxygen-sdk'; |
| 5 | + |
| 6 | +/* eslint-disable no-console */ |
| 7 | + |
| 8 | +// NOTE: Currently, this is a very simple client pool that only really handles the |
| 9 | +// very limited Oxygen specific use cases... we should be expand this to be more |
| 10 | +// general purpose in the future and maybe even come up with some shared ts interface |
| 11 | +// to facilitate future contract testing. |
| 12 | + |
| 13 | +/** |
| 14 | + * ClientPool is a singleton that manages a pool of LDClient instances. Currently there is |
| 15 | + * no separation between a managed client and this pool. Which means all of the client specs |
| 16 | + * will be implemented in this class. |
| 17 | + * |
| 18 | + * @see https://github.com/launchdarkly/sdk-test-harness/blob/v2/docs/service_spec.md |
| 19 | + */ |
| 20 | +export default class ClientPool { |
| 21 | + private _clients: Record<string, LDClient> = {}; |
| 22 | + private _clientCounter = 0; |
| 23 | + |
| 24 | + constructor() { |
| 25 | + this._clients = {}; |
| 26 | + this._clientCounter = 0; |
| 27 | + } |
| 28 | + |
| 29 | + private _makeId(): string { |
| 30 | + this._clientCounter += 1; |
| 31 | + return `client-${this._clientCounter}`; |
| 32 | + } |
| 33 | + |
| 34 | + public async runCommand(id: string, body: any, res: Response): Promise<void> { |
| 35 | + const client = this._clients[id]; |
| 36 | + // TODO: handle the 'itCanFailCase' |
| 37 | + if (client) { |
| 38 | + try { |
| 39 | + const { command, ...rest } = body; |
| 40 | + switch (command) { |
| 41 | + case 'evaluate': { |
| 42 | + const { flagKey, context, defaultValue, detail } = rest.evaluate; |
| 43 | + const evaluation = detail |
| 44 | + ? await client.variationDetail(flagKey, context, defaultValue) |
| 45 | + : await client.variation(flagKey, context, defaultValue); |
| 46 | + res.status(200); |
| 47 | + res.json({ value: evaluation }); |
| 48 | + break; |
| 49 | + } |
| 50 | + default: { |
| 51 | + res.status(400); |
| 52 | + res.json({ error: `Unknown command: ${command}` }); |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + } catch (err) { |
| 57 | + console.error(`Error running command: ${err}`); |
| 58 | + res.status(500); |
| 59 | + } |
| 60 | + } else { |
| 61 | + res.status(404); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public async deleteClient(id: string, res: Response): Promise<void> { |
| 66 | + const client = this._clients[id]; |
| 67 | + if (client) { |
| 68 | + client.close(); |
| 69 | + delete this._clients[id]; |
| 70 | + res.status(204); |
| 71 | + } else { |
| 72 | + res.status(404); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public async createClient(options: any, res: Response): Promise<void> { |
| 77 | + try { |
| 78 | + const id = this._makeId(); |
| 79 | + const { |
| 80 | + configuration: { credential = 'unknown-sdk-key', polling }, |
| 81 | + } = options; |
| 82 | + |
| 83 | + if (!polling) { |
| 84 | + // We do not support non-polling clients yet |
| 85 | + res.status(400); |
| 86 | + return; |
| 87 | + } |
| 88 | + const client = await init(credential, { |
| 89 | + ...(polling && { |
| 90 | + baseUri: polling.baseUri, |
| 91 | + }), |
| 92 | + }); |
| 93 | + |
| 94 | + await client.waitForInitialization({ timeout: 10 }); |
| 95 | + this._clients[id] = client; |
| 96 | + res.status(201); |
| 97 | + res.set('Location', `/clients/${id}`); |
| 98 | + if (!client.initialized()) { |
| 99 | + res.status(500); |
| 100 | + client.close(); |
| 101 | + } |
| 102 | + this._clients[id] = client; |
| 103 | + console.debug(`Creating client with configuration: ${JSON.stringify(options.configuration)}`); |
| 104 | + } catch (err) { |
| 105 | + console.error(`Error creating client: ${err}`); |
| 106 | + res.status(500); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments