Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module.exports = (function() {
privateAttributeNames: [],
userKeysCapacity: 1000,
userKeysFlushInterval: 300,
featureStore: InMemoryFeatureStore()
featureStore: InMemoryFeatureStore(),
inMemoryDevFlags: false,
};
};

Expand Down Expand Up @@ -132,7 +133,7 @@ module.exports = (function() {

function validate(options) {
let config = Object.assign({}, options || {});

config.userAgent = 'NodeJSClient/' + package_json.version;
config.logger = (config.logger ||
new winston.Logger({
Expand All @@ -146,7 +147,7 @@ module.exports = (function() {
]
})
);

checkDeprecatedOptions(config);

const defaultConfig = defaults();
Expand Down
45 changes: 45 additions & 0 deletions in_memory_data_source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
DevDataSource provides a way to pass features in to dev without connecting to LaunchDarkly's live service.
This would typically be used in a local development environment.
*/

function InMemoryDataSource(features) {
if (!features) {
return;
}

const flags = {};
Object.keys(features).forEach(key => {
flags[key] = {
key,
on: features[key],
};
});
const ld_features = {
flags,
segments: {},
};

return (config) => {
let inited = false;
const featureStore = config.featureStore;

const dev_ds = {
start: fn => {
featureStore.init(ld_features, () => {
inited = true;
});
const cb = fn || (() => {});
cb();
},
stop: () => {},
initialized: () => inited,
close: () => {
dev_ds.stop();
},
};
return dev_ds;
};
}

module.exports = InMemoryDataSource;
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const EventProcessor = require('./event_processor');
const PollingProcessor = require('./polling');
const StreamingProcessor = require('./streaming');
const FlagsStateBuilder = require('./flags_state');
const InMemoryDataSource = require('./in_memory_data_source');
const configuration = require('./configuration');
const evaluate = require('./evaluate_flag');
const messages = require('./messages');
Expand Down Expand Up @@ -62,7 +63,7 @@ const newClient = function(sdkKey, originalConfig) {
eventFactoryWithReasons;

const config = configuration.validate(originalConfig);

// Initialize global tunnel if proxy options are set
if (config.proxyHost && config.proxyPort ) {
config.proxyAgent = createProxyAgent(config);
Expand Down Expand Up @@ -90,11 +91,15 @@ const newClient = function(sdkKey, originalConfig) {
}

const createDefaultUpdateProcessor = config => {
if (config.inMemoryDevFlags) {
config.logger.info('Creating in-memory flags for offline usage');
return InMemoryDataSource(config.inMemoryDevFlags);
}
if (config.useLdd || config.offline) {
return NullUpdateProcessor();
} else {
requestor = Requestor(sdkKey, config);

if (config.stream) {
config.logger.info('Initializing stream processor to receive feature flag updates');
return StreamingProcessor(sdkKey, config, requestor);
Expand Down
29 changes: 27 additions & 2 deletions test/event_processor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ describe('EventProcessor', () => {
warn: jest.fn()
}
};
const developmentConfig = {
eventsUri: eventsUri,
capacity: 100,
flushInterval: 30,
userKeysCapacity: 1000,
userKeysFlushInterval: 300,
inMemoryDevFlags: { 'development-feature': true },
logger: {
debug: jest.fn(),
warn: jest.fn()
}
};
const user = { key: 'userKey', name: 'Red' };
const filteredUser = { key: 'userKey', privateAttrs: [ 'name' ] };
const numericUser = { key: 1, secondary: 2, ip: 3, country: 4, email: 5, firstName: 6, lastName: 7,
Expand Down Expand Up @@ -212,6 +224,19 @@ describe('EventProcessor', () => {
});
}));

it('processes offline events when defined', eventsServerTest(async s => {
const config = Object.assign({}, developmentConfig, { allAttributesPrivate: true });
await withEventProcessor(config, s, async ep => {
const e = developmentConfig.inMemoryDevFlags['development-feature']
ep.sendEvent(e);
await ep.flush();

const output = await getJsonRequest(s);
expect(output.length).toEqual(1);
expect(output[0]).toEqual(e)
});
}));

it('stringifies user attributes in feature event', eventsServerTest(async s => {
const config = Object.assign({}, defaultConfig, { inlineUsersInEvents: true });
await withEventProcessor(config, s, async ep => {
Expand Down Expand Up @@ -297,7 +322,7 @@ describe('EventProcessor', () => {
const serverTime = new Date().getTime() - 20000;
s.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(200, headersWithDate(serverTime)));

// Send and flush an event we don't care about, just to set the last server time
// Send and flush an event we don't care about, just to set the last server time
ep.sendEvent({ kind: 'identify', user: { key: 'otherUser' } });
await ep.flush();
await s.nextRequest();
Expand Down Expand Up @@ -536,7 +561,7 @@ describe('EventProcessor', () => {
it('swallows errors from failed background flush', eventsServerTest(async s => {
// This test verifies that when a background flush fails, we don't emit an unhandled
// promise rejection. Jest will fail the test if we do that.

const config = Object.assign({}, defaultConfig, { flushInterval: 0.25 });
await withEventProcessor(config, s, async ep => {
s.forMethodAndPath('post', '/bulk', TestHttpHandlers.respond(500));
Expand Down