featurehub-repository
Version:
Core package of API that exposes FeatureHub feature flags, values and configuration to client applications written in Typescript or Javascript.
195 lines • 6.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.featurehubMiddleware = void 0;
const models_1 = require("./models");
class BaggageHolder {
constructor(existing, value) {
this.existing = existing;
this.value = value;
}
isEnabled() {
return this.getBoolean() === true;
}
withContext(param) {
return new BaggageHolder(this.existing.withContext(param), this.value);
}
addListener(listener) {
}
getBoolean() {
if (this.existing.isLocked()) {
return this.existing.getBoolean();
}
return this.existing.getType() === models_1.FeatureValueType.Boolean ? ('true' === this.value) : undefined;
}
getFlag() {
return this.getBoolean();
}
getKey() {
return this.existing.getKey();
}
getNumber() {
if (this.existing.isLocked()) {
return this.existing.getNumber();
}
if (this.existing.getType() === models_1.FeatureValueType.Number && this.value !== undefined) {
if (this.value.includes('.')) {
return parseFloat(this.value);
}
else {
return parseInt(this.value);
}
}
return undefined;
}
getRawJson() {
return undefined;
}
getString() {
if (this.existing.isLocked()) {
return this.existing.getString();
}
if (this.existing.getType() === models_1.FeatureValueType.String) {
return this.value;
}
return undefined;
}
getType() {
return this.existing.getType();
}
getVersion() {
return this.existing.getVersion();
}
isLocked() {
return this.existing.isLocked();
}
isSet() {
return this.value != null;
}
triggerListeners(feature) {
this.existing.triggerListeners(feature);
}
}
class BaggageRepository {
constructor(repo, baggage) {
this.mappedBaggage = new Map();
this.repo = repo;
this.baggage = baggage;
}
apply(strategies, key, featureValueId, context) {
return this.repo.apply(strategies, key, featureValueId, context);
}
getFlag(key) {
return this.feature(key).getFlag();
}
getString(key) {
return this.feature(key).getString();
}
getJson(key) {
return this.feature(key).getRawJson();
}
getNumber(key) {
return this.feature(key).getNumber();
}
isSet(key) {
return this.feature(key).isSet();
}
get readyness() {
return this.repo.readyness;
}
hasFeature(key) {
return this.feature(key);
}
feature(key) {
const realFeature = this.repo.hasFeature(key);
if (realFeature !== undefined && realFeature.getType() !== undefined) {
if (this.baggage.has(key)) {
let fh = this.mappedBaggage.get(key);
if (fh === undefined && realFeature.getType() !== models_1.FeatureValueType.Json) {
fh = new BaggageHolder(realFeature, this.baggage.get(key));
this.mappedBaggage.set(key, fh);
}
if (fh !== undefined) {
return fh;
}
}
}
return realFeature;
}
logAnalyticsEvent(action, other) {
const otherCopy = other ? other : new Map();
const baggageCopy = new Map([...this.baggage.entries(), ...otherCopy.entries()]);
this.repo.logAnalyticsEvent(action, baggageCopy);
}
simpleFeatures() {
const features = this.repo.simpleFeatures();
this.baggage.forEach((value, key) => features.set(key, value));
return features;
}
notReady() {
this.repo.notReady();
}
notify(state, data) {
this.repo.notify(state, data);
}
addValueInterceptor(interceptor) {
this.repo.addValueInterceptor(interceptor);
}
valueInterceptorMatched(key) {
return this.repo.valueInterceptorMatched(key);
}
addReadynessListener(listener) {
this.repo.addReadynessListener(listener);
}
addAnalyticCollector(collector) {
this.repo.addAnalyticCollector(collector);
}
get catchAndReleaseMode() {
return this.repo.catchAndReleaseMode;
}
set catchAndReleaseMode(value) {
this.repo.catchAndReleaseMode = value;
}
addPostLoadNewFeatureStateAvailableListener(listener) {
this.repo.addPostLoadNewFeatureStateAvailableListener(listener);
}
getFeatureState(key) {
return this.feature(key);
}
release(disableCatchAndRelease) {
return this.repo.release(disableCatchAndRelease);
}
}
function featurehubMiddleware(repo) {
return (req, res, next) => {
let reqRepo = repo;
if (process.env.FEATUREHUB_ACCEPT_BAGGAGE !== undefined) {
const baggage = req.header('baggage');
if (baggage != null) {
const baggageMap = new Map();
baggage.split(',')
.map(b => b.trim())
.filter(b => b.startsWith('fhub='))
.forEach(b => {
const fhub = decodeURIComponent(b.substring(5));
fhub.split(',')
.forEach(feature => {
const parts = feature.split('=');
if (parts.length === 2) {
baggageMap.set(parts[0], decodeURIComponent(parts[1]));
}
else if (parts.length === 1) {
baggageMap.set(parts[0], undefined);
}
});
});
if (baggageMap.size > 0) {
reqRepo = new BaggageRepository(repo, baggageMap);
}
}
}
req.featureHub = reqRepo;
next();
};
}
exports.featurehubMiddleware = featurehubMiddleware;
//# sourceMappingURL=middleware.js.map