This repository was archived by the owner on Apr 2, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathstreaming.js
More file actions
169 lines (154 loc) · 5.12 KB
/
Copy pathstreaming.js
File metadata and controls
169 lines (154 loc) · 5.12 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const errors = require('./errors');
const messages = require('./messages');
const EventSource = require('./eventsource');
const dataKind = require('./versioned_data_kind');
function StreamProcessor(sdkKey, config, requestor, eventSourceFactory) {
const processor = {},
featureStore = config.featureStore;
let es;
eventSourceFactory = eventSourceFactory || EventSource;
function getKeyFromPath(kind, path) {
return path.startsWith(kind.streamApiPath) ? path.substring(kind.streamApiPath.length) : null;
}
processor.start = fn => {
const cb = fn || function(){};
es = new eventSourceFactory(config.streamUri + '/all',
{
agent: config.proxyAgent,
headers: {'Authorization': sdkKey,'User-Agent': config.userAgent},
tlsParams: config.tlsParams
});
es.onerror = err => {
if (err.status && !errors.isHttpErrorRecoverable(err.status)) {
const message = messages.httpErrorMessage(err.status, 'streaming request');
config.logger.error(message);
es && es.close();
cb(new errors.LDStreamingError(err.message, err.status));
} else {
const message = messages.httpErrorMessage(err.status, 'streaming request', 'will retry');
config.logger.warn(message);
}
};
function reportJsonError(type, data) {
config.logger.error('Stream received invalid data in "' + type + '" message');
config.logger.debug('Invalid JSON follows: ' + data);
cb(new errors.LDStreamingError('Malformed JSON data in event stream'));
}
es.addEventListener('put', e => {
config.logger.debug('Received put event');
if (e && e.data) {
let all;
try {
all = JSON.parse(e.data);
} catch (err) {
reportJsonError('put', e.data);
return;
}
const initData = {};
initData[dataKind.features.namespace] = all.data.flags;
initData[dataKind.segments.namespace] = all.data.segments;
featureStore.init(initData, () => {
cb();
});
} else {
cb(new errors.LDStreamingError('Unexpected payload from event stream'));
}
});
es.addEventListener('patch', e => {
config.logger.debug('Received patch event');
if (e && e.data) {
let patch;
try {
patch = JSON.parse(e.data);
} catch (err) {
reportJsonError('patch', e.data);
return;
}
for (let k in dataKind) {
const kind = dataKind[k];
const 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', e => {
config.logger.debug('Received delete event');
if (e && e.data) {
let data, version;
try {
data = JSON.parse(e.data);
} catch (err) {
reportJsonError('delete', e.data);
return;
}
version = data.version;
for (let k in dataKind) {
const kind = dataKind[k];
const 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', () => {
config.logger.debug('Received indirect put event');
requestor.requestAllData((err, resp) => {
if (err) {
cb(err);
} else {
const all = JSON.parse(resp);
const initData = {};
initData[dataKind.features.namespace] = all.flags;
initData[dataKind.segments.namespace] = all.segments;
featureStore.init(initData, () => {
cb();
});
}
});
});
es.addEventListener('indirect/patch', e => {
config.logger.debug('Received indirect patch event');
if (e && e.data) {
const path = e.data;
for (let k in dataKind) {
const kind = dataKind[k];
const key = getKeyFromPath(kind, path);
if (key != null) {
requestor.requestObject(kind, key, (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 = () => {
if (es) {
es.close();
}
};
processor.close = () => {
processor.stop();
};
return processor;
}
module.exports = StreamProcessor;