forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streaming.js
135 lines (120 loc) · 4 KB
/
streaming.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var errors = require('./errors');
var EventSource = require('./eventsource');
var dataKind = require('./versioned_data_kind');
function StreamProcessor(sdkKey, config, requestor) {
var processor = {},
featureStore = config.featureStore,
es;
function getKeyFromPath(kind, path) {
return path.startsWith(kind.streamApiPath) ? path.substring(kind.streamApiPath.length) : null;
}
processor.start = function(fn) {
var cb = fn || function(){};
es = new EventSource(config.streamUri + "/all",
{
agent: config.proxyAgent,
headers: {'Authorization': sdkKey,'User-Agent': config.userAgent}
});
es.onerror = function(err) {
cb(new errors.LDStreamingError(err.message, err.code));
};
es.addEventListener('put', function(e) {
config.logger.debug('Received put event');
if (e && e.data) {
var all = JSON.parse(e.data);
var initData = {};
initData[dataKind.features.namespace] = all.data.flags;
initData[dataKind.segments.namespace] = all.data.segments;
featureStore.init(initData, function() {
cb();
});
} else {
cb(new errors.LDStreamingError('Unexpected payload from event stream'));
}
});
es.addEventListener('patch', function(e) {
config.logger.debug('Received patch event');
if (e && e.data) {
var patch = JSON.parse(e.data);
for (var k in dataKind) {
var kind = dataKind[k];
var key = getKeyFromPath(kind, patch.path);
if (key != null) {
config.logger.debug('Updating ' + key + ' in ' + kind.namespace);
featureStore.upsert(kind, patch.data);
break;
}
}
} else {
cb(new errors.LDStreamingError('Unexpected payload from event stream'));
}
});
es.addEventListener('delete', function(e) {
config.logger.debug('Received delete event');
if (e && e.data) {
var data = JSON.parse(e.data),
version = data.version;
for (var k in dataKind) {
var kind = dataKind[k];
var key = getKeyFromPath(kind, data.path);
if (key != null) {
config.logger.debug('Deleting ' + key + ' in ' + kind.namespace);
featureStore.delete(kind, key, version);
break;
}
}
} else {
cb(new errors.LDStreamingError('Unexpected payload from event stream'));
}
});
es.addEventListener('indirect/put', function(e) {
config.logger.debug('Received indirect put event')
requestor.requestAllFlags(function (err, resp) {
if (err) {
cb(err);
} else {
var all = JSON.parse(resp);
var initData = {};
initData[dataKind.features.namespace] = all.flags;
initData[dataKind.segments.namespace] = all.segments;
featureStore.init(initData, function() {
cb();
});
}
})
});
es.addEventListener('indirect/patch', function(e) {
config.logger.debug('Received indirect patch event')
if (e && e.data) {
var path = e.data;
for (var k in dataKind) {
var kind = dataKind[k];
var key = getKeyFromPath(kind, path);
if (key != null) {
requestor.requestObject(kind, key, function(err, resp) {
if (err) {
cb(new errors.LDStreamingError('Unexpected error requesting ' + key + ' in ' + kind.namespace));
} else {
config.logger.debug('Updating ' + key + ' in ' + kind.namespace);
featureStore.upsert(kind, JSON.parse(resp));
}
});
break;
}
}
} else {
cb(new errors.LDStreamingError('Unexpected payload from event stream'));
}
});
}
processor.stop = function() {
if (es) {
es.close();
}
}
processor.close = function() {
this.stop();
}
return processor;
}
module.exports = StreamProcessor;