featurehub-javascript-node-sdk
Version:
FeatureHub NodeJS SDK
134 lines • 5.25 kB
JavaScript
import { FeatureHubPollingClient, FeatureUpdater, GoogleAnalyticsCollector, PollingBase, FeatureHubEventSourceClient, EdgeFeatureHubConfig, FHLog, fhLog } from 'featurehub-javascript-client-sdk';
import { URL } from 'url';
const ES = require('eventsource');
export * from 'featurehub-javascript-client-sdk';
FeatureHubEventSourceClient.eventSourceProvider = (url, dict) => {
return new ES(url, dict);
};
export class NodejsPollingService extends PollingBase {
constructor(options, url, frequency, _callback) {
super(url, frequency, _callback);
this._options = options;
this.uri = new URL(this.url);
}
poll() {
if (this._busy) {
return new Promise((resolve, reject) => {
this._outstandingPromises.push({ resolve: resolve, reject: reject });
});
}
if (this._stopped) {
return new Promise((resolve) => resolve());
}
this._busy = true;
return new Promise(((resolve, reject) => {
const http = this.uri.protocol === 'http:' ? require('http') : require('https');
let data = '';
const headers = this._header === undefined ? {} : {
'x-featurehub': this._header
};
if (this._etag) {
headers['if-none-match'] = this._etag;
}
const reqOptions = {
protocol: this.uri.protocol,
host: this.uri.host,
hostname: this.uri.hostname,
port: this.uri.port,
method: 'GET',
path: this.uri.pathname + this.uri.search + `&contextSha=${this._shaHeader}`,
headers: headers,
timeout: this._options.timeout || 8000
};
if (this.modifyRequestFunction) {
this.modifyRequestFunction(reqOptions);
}
const req = http.request(reqOptions, (res) => {
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
this.parseCacheControl(res.headers['cache-control']);
if (res.statusCode === 200 || res.statusCode === 236) {
this._etag = res.headers.etag;
this._callback(JSON.parse(data));
this._stopped = (res.statusCode === 236);
this._busy = false;
this.resolveOutstanding();
resolve();
}
else if (res.statusCode == 304) {
this._busy = false;
this.resolveOutstanding();
resolve();
}
else {
this._busy = false;
this.rejectOutstanding(req.status);
reject(res.statusCode);
}
});
});
req.end();
}));
}
}
FeatureHubPollingClient.pollingClientProvider = (opt, url, freq, callback) => new NodejsPollingService(opt, url, freq, callback);
export class NodejsFeaturePostUpdater {
post(url, update) {
const loc = new URL(url);
const cra = {
protocol: loc.protocol,
path: loc.pathname,
host: loc.hostname,
method: 'PUT',
port: loc.port,
timeout: 3000,
headers: {
'content-type': 'application/json'
}
};
if (this.modifyRequestFunction) {
this.modifyRequestFunction(cra);
}
const http = cra.protocol === 'http:' ? require('http') : require('https');
return new Promise((resolve) => {
try {
const req = http.request(cra, (res) => {
fhLog.trace('update result -> ', res.statusCode);
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(true);
}
else {
resolve(false);
}
});
req.on('error', () => {
fhLog.trace('update result -> error');
resolve(false);
});
FHLog.fhLog.trace('FeatureUpdater', cra, update);
req.write(JSON.stringify(update));
req.end();
}
catch (e) {
resolve(false);
}
});
}
}
FeatureUpdater.featureUpdaterProvider = () => new NodejsFeaturePostUpdater();
class NodejsGoogleAnalyticsApiClient {
cid(other) {
return other.get('cid') || process.env.GA_CID || '';
}
postBatchUpdate(batchData) {
const req = require('https').request({
host: 'www.google-analytics.com',
path: 'batch'
});
req.write(batchData);
req.end();
}
}
GoogleAnalyticsCollector.googleAnalyticsClientProvider = () => new NodejsGoogleAnalyticsApiClient();
EdgeFeatureHubConfig.defaultEdgeServiceSupplier = (repository, config) => new FeatureHubEventSourceClient(config, repository);
//# sourceMappingURL=index.js.map