@openfeature/core
Advanced tools
Comparing version 0.0.23 to 0.0.24
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __export = (target, all) => { | ||
@@ -36,22 +19,2 @@ for (var name in all) | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
@@ -90,4 +53,4 @@ // src/index.ts | ||
var OpenFeatureError = class extends Error { | ||
constructor(message) { | ||
super(message); | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, OpenFeatureError.prototype); | ||
@@ -148,4 +111,5 @@ this.name = "OpenFeatureError"; | ||
var GeneralError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, GeneralError.prototype); | ||
@@ -159,4 +123,5 @@ this.name = "GeneralError"; | ||
var FlagNotFoundError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, FlagNotFoundError.prototype); | ||
@@ -170,4 +135,5 @@ this.name = "FlagNotFoundError"; | ||
var ParseError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, ParseError.prototype); | ||
@@ -181,4 +147,5 @@ this.name = "ParseError"; | ||
var TypeMismatchError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, TypeMismatchError.prototype); | ||
@@ -192,4 +159,5 @@ this.name = "TypeMismatchError"; | ||
var TargetingKeyMissingError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, TargetingKeyMissingError.prototype); | ||
@@ -203,4 +171,5 @@ this.name = "TargetingKeyMissingError"; | ||
var InvalidContextError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, InvalidContextError.prototype); | ||
@@ -214,4 +183,5 @@ this.name = "InvalidContextError"; | ||
var ProviderNotReadyError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, ProviderNotReadyError.prototype); | ||
@@ -277,4 +247,5 @@ this.name = "ProviderNotReadyError"; | ||
var SafeLogger = class { | ||
logger; | ||
fallbackLogger = new DefaultLogger(); | ||
constructor(logger) { | ||
this.fallbackLogger = new DefaultLogger(); | ||
try { | ||
@@ -318,5 +289,11 @@ for (const level of LOG_LEVELS) { | ||
this.globalLogger = globalLogger; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._handlers = /* @__PURE__ */ new WeakMap(); | ||
} | ||
_handlers = { | ||
["PROVIDER_CONFIGURATION_CHANGED" /* ConfigurationChanged */]: /* @__PURE__ */ new WeakMap(), | ||
["PROVIDER_CONTEXT_CHANGED" /* ContextChanged */]: /* @__PURE__ */ new WeakMap(), | ||
["PROVIDER_READY" /* Ready */]: /* @__PURE__ */ new WeakMap(), | ||
["PROVIDER_ERROR" /* Error */]: /* @__PURE__ */ new WeakMap(), | ||
["PROVIDER_STALE" /* Stale */]: /* @__PURE__ */ new WeakMap() | ||
}; | ||
_eventLogger; | ||
// here we use E, to restrict the events a provider can manually emit (PROVIDER_CONTEXT_CHANGED is emitted by the SDK) | ||
@@ -327,14 +304,17 @@ emit(eventType, context) { | ||
addHandler(eventType, handler) { | ||
const asyncHandler = (details) => __async(this, null, function* () { | ||
yield handler(details); | ||
}); | ||
this._handlers.set(handler, asyncHandler); | ||
const asyncHandler = async (details) => { | ||
await handler(details); | ||
}; | ||
const existingAsyncHandlers = this._handlers[eventType].get(handler); | ||
this._handlers[eventType].set(handler, [...existingAsyncHandlers || [], asyncHandler]); | ||
this.eventEmitter.on(eventType, asyncHandler); | ||
} | ||
removeHandler(eventType, handler) { | ||
const asyncHandler = this._handlers.get(handler); | ||
if (!asyncHandler) { | ||
return; | ||
const existingAsyncHandlers = this._handlers[eventType].get(handler); | ||
if (existingAsyncHandlers) { | ||
const removedAsyncHandler = existingAsyncHandlers.pop(); | ||
if (removedAsyncHandler) { | ||
this.eventEmitter.removeListener(eventType, removedAsyncHandler); | ||
} | ||
} | ||
this.eventEmitter.removeListener(eventType, asyncHandler); | ||
} | ||
@@ -356,4 +336,3 @@ removeAllHandlers(eventType) { | ||
get _logger() { | ||
var _a, _b; | ||
return (_b = this._eventLogger) != null ? _b : (_a = this.globalLogger) == null ? void 0 : _a.call(this); | ||
return this._eventLogger ?? this.globalLogger?.(); | ||
} | ||
@@ -383,9 +362,10 @@ }; | ||
var OpenFeatureCommonAPI = class { | ||
_hooks = []; | ||
_context = {}; | ||
_logger = new DefaultLogger(); | ||
_clientEventHandlers = /* @__PURE__ */ new Map(); | ||
_clientProviders = /* @__PURE__ */ new Map(); | ||
_clientEvents = /* @__PURE__ */ new Map(); | ||
_runsOn; | ||
constructor(category) { | ||
this._hooks = []; | ||
this._context = {}; | ||
this._logger = new DefaultLogger(); | ||
this._clientEventHandlers = /* @__PURE__ */ new Map(); | ||
this._clientProviders = /* @__PURE__ */ new Map(); | ||
this._clientEvents = /* @__PURE__ */ new Map(); | ||
this._runsOn = category; | ||
@@ -433,3 +413,2 @@ } | ||
[.../* @__PURE__ */ new Map([[void 0, this._defaultProvider]]), ...this._clientProviders].forEach((keyProviderTuple) => { | ||
var _a; | ||
const clientName = keyProviderTuple[0]; | ||
@@ -442,3 +421,3 @@ const provider = keyProviderTuple[1]; | ||
} catch (err) { | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
this._logger?.error("Error running event handler:", err); | ||
} | ||
@@ -471,6 +450,4 @@ } | ||
} | ||
setProviderAndWait(clientOrProvider, providerOrUndefined) { | ||
return __async(this, null, function* () { | ||
yield this.setAwaitableProvider(clientOrProvider, providerOrUndefined); | ||
}); | ||
async setProviderAndWait(clientOrProvider, providerOrUndefined) { | ||
await this.setAwaitableProvider(clientOrProvider, providerOrUndefined); | ||
} | ||
@@ -486,5 +463,4 @@ setProvider(clientOrProvider, providerOrUndefined) { | ||
setAwaitableProvider(clientOrProvider, providerOrUndefined) { | ||
var _a, _b, _c, _d, _e, _f; | ||
const clientName = stringOrUndefined(clientOrProvider); | ||
const provider = (_a = objectOrUndefined(clientOrProvider)) != null ? _a : objectOrUndefined(providerOrUndefined); | ||
const provider = objectOrUndefined(clientOrProvider) ?? objectOrUndefined(providerOrUndefined); | ||
if (!provider) { | ||
@@ -513,15 +489,13 @@ this._logger.debug("No provider defined, ignoring setProvider call"); | ||
let initializationPromise = void 0; | ||
if ((provider == null ? void 0 : provider.status) === "NOT_READY" /* NOT_READY */ && typeof provider.initialize === "function") { | ||
initializationPromise = (_d = (_c = (_b = provider.initialize) == null ? void 0 : _b.call(provider, this._context)) == null ? void 0 : _c.then(() => { | ||
var _a2; | ||
if (provider?.status === "NOT_READY" /* NOT_READY */ && typeof provider.initialize === "function") { | ||
initializationPromise = provider.initialize?.(this._context)?.then(() => { | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
emitter?.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
}); | ||
(_a2 = this._events) == null ? void 0 : _a2.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
})) == null ? void 0 : _d.catch((error) => { | ||
var _a2; | ||
this._events?.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
})?.catch((error) => { | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error == null ? void 0 : error.message }); | ||
emitter?.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error?.message }); | ||
}); | ||
(_a2 = this._events) == null ? void 0 : _a2.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error == null ? void 0 : error.message }); | ||
this._events?.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error?.message }); | ||
throw error; | ||
@@ -531,5 +505,5 @@ }); | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
emitter?.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
}); | ||
(_e = this._events) == null ? void 0 : _e.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
this._events?.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
} | ||
@@ -543,3 +517,3 @@ if (clientName) { | ||
if (![...this._clientProviders.values(), this._defaultProvider].includes(oldProvider)) { | ||
(_f = oldProvider == null ? void 0 : oldProvider.onClose) == null ? void 0 : _f.call(oldProvider); | ||
oldProvider?.onClose?.(); | ||
} | ||
@@ -549,7 +523,6 @@ return initializationPromise; | ||
getProviderForClient(name) { | ||
var _a; | ||
if (!name) { | ||
return this._defaultProvider; | ||
} | ||
return (_a = this._clientProviders.get(name)) != null ? _a : this._defaultProvider; | ||
return this._clientProviders.get(name) ?? this._defaultProvider; | ||
} | ||
@@ -565,8 +538,5 @@ buildAndCacheEventEmitterForClient(name) { | ||
Object.values(ClientProviderEvents).forEach( | ||
(eventType) => { | ||
var _a; | ||
return (_a = clientProvider.events) == null ? void 0 : _a.addHandler(eventType, (details) => __async(this, null, function* () { | ||
newEmitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName: name, providerName: clientProvider.metadata.name })); | ||
})); | ||
} | ||
(eventType) => clientProvider.events?.addHandler(eventType, async (details) => { | ||
newEmitter.emit(eventType, { ...details, clientName: name, providerName: clientProvider.metadata.name }); | ||
}) | ||
); | ||
@@ -590,60 +560,47 @@ return newEmitter; | ||
transferListeners(oldProvider, newProvider, clientName, emitters) { | ||
var _a; | ||
(_a = this._clientEventHandlers.get(clientName)) == null ? void 0 : _a.forEach((eventHandler) => { | ||
var _a2; | ||
return (_a2 = oldProvider.events) == null ? void 0 : _a2.removeHandler(...eventHandler); | ||
}); | ||
this._clientEventHandlers.get(clientName)?.forEach((eventHandler) => oldProvider.events?.removeHandler(...eventHandler)); | ||
const newClientHandlers = Object.values(ClientProviderEvents).map((eventType) => { | ||
const handler = (details) => __async(this, null, function* () { | ||
const handler = async (details) => { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName, providerName: newProvider.metadata.name })); | ||
emitter?.emit(eventType, { ...details, clientName, providerName: newProvider.metadata.name }); | ||
}); | ||
this._events.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName, providerName: newProvider.metadata.name })); | ||
}); | ||
this._events.emit(eventType, { ...details, clientName, providerName: newProvider.metadata.name }); | ||
}; | ||
return [eventType, handler]; | ||
}); | ||
this._clientEventHandlers.set(clientName, newClientHandlers); | ||
newClientHandlers.forEach((eventHandler) => { | ||
var _a2; | ||
return (_a2 = newProvider.events) == null ? void 0 : _a2.addHandler(...eventHandler); | ||
}); | ||
newClientHandlers.forEach((eventHandler) => newProvider.events?.addHandler(...eventHandler)); | ||
} | ||
close() { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
try { | ||
yield (_b = (_a = this == null ? void 0 : this._defaultProvider) == null ? void 0 : _a.onClose) == null ? void 0 : _b.call(_a); | ||
} catch (err) { | ||
this.handleShutdownError(this._defaultProvider, err); | ||
} | ||
const providers = Array.from(this._clientProviders); | ||
yield Promise.all( | ||
providers.map((_0) => __async(this, [_0], function* ([, provider]) { | ||
var _a2; | ||
try { | ||
yield (_a2 = provider.onClose) == null ? void 0 : _a2.call(provider); | ||
} catch (err) { | ||
this.handleShutdownError(provider, err); | ||
} | ||
})) | ||
); | ||
}); | ||
async close() { | ||
try { | ||
await this?._defaultProvider?.onClose?.(); | ||
} catch (err) { | ||
this.handleShutdownError(this._defaultProvider, err); | ||
} | ||
const providers = Array.from(this._clientProviders); | ||
await Promise.all( | ||
providers.map(async ([, provider]) => { | ||
try { | ||
await provider.onClose?.(); | ||
} catch (err) { | ||
this.handleShutdownError(provider, err); | ||
} | ||
}) | ||
); | ||
} | ||
clearProvidersAndSetDefault(defaultProvider) { | ||
return __async(this, null, function* () { | ||
try { | ||
yield this.close(); | ||
} catch (err) { | ||
this._logger.error("Unable to cleanly close providers. Resetting to the default configuration."); | ||
} finally { | ||
this._clientProviders.clear(); | ||
this._defaultProvider = defaultProvider; | ||
} | ||
}); | ||
async clearProvidersAndSetDefault(defaultProvider) { | ||
try { | ||
await this.close(); | ||
} catch (err) { | ||
this._logger.error("Unable to cleanly close providers. Resetting to the default configuration."); | ||
} finally { | ||
this._clientProviders.clear(); | ||
this._defaultProvider = defaultProvider; | ||
} | ||
} | ||
handleShutdownError(provider, err) { | ||
this._logger.error(`Error during shutdown of provider ${provider.metadata.name}: ${err}`); | ||
this._logger.error(err == null ? void 0 : err.stack); | ||
this._logger.error(err?.stack); | ||
} | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,45 +0,5 @@ | ||
var __defProp = Object.defineProperty; | ||
var __defProps = Object.defineProperties; | ||
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __async = (__this, __arguments, generator) => { | ||
return new Promise((resolve, reject) => { | ||
var fulfilled = (value) => { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var rejected = (value) => { | ||
try { | ||
step(generator.throw(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}; | ||
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); | ||
step((generator = generator.apply(__this, __arguments)).next()); | ||
}); | ||
}; | ||
// src/errors/open-feature-error-abstract.ts | ||
var OpenFeatureError = class extends Error { | ||
constructor(message) { | ||
super(message); | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, OpenFeatureError.prototype); | ||
@@ -100,4 +60,5 @@ this.name = "OpenFeatureError"; | ||
var GeneralError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, GeneralError.prototype); | ||
@@ -111,4 +72,5 @@ this.name = "GeneralError"; | ||
var FlagNotFoundError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, FlagNotFoundError.prototype); | ||
@@ -122,4 +84,5 @@ this.name = "FlagNotFoundError"; | ||
var ParseError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, ParseError.prototype); | ||
@@ -133,4 +96,5 @@ this.name = "ParseError"; | ||
var TypeMismatchError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, TypeMismatchError.prototype); | ||
@@ -144,4 +108,5 @@ this.name = "TypeMismatchError"; | ||
var TargetingKeyMissingError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, TargetingKeyMissingError.prototype); | ||
@@ -155,4 +120,5 @@ this.name = "TargetingKeyMissingError"; | ||
var InvalidContextError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, InvalidContextError.prototype); | ||
@@ -166,4 +132,5 @@ this.name = "InvalidContextError"; | ||
var ProviderNotReadyError = class extends OpenFeatureError { | ||
constructor(message) { | ||
super(message); | ||
code; | ||
constructor(message, options) { | ||
super(message, options); | ||
Object.setPrototypeOf(this, ProviderNotReadyError.prototype); | ||
@@ -229,4 +196,5 @@ this.name = "ProviderNotReadyError"; | ||
var SafeLogger = class { | ||
logger; | ||
fallbackLogger = new DefaultLogger(); | ||
constructor(logger) { | ||
this.fallbackLogger = new DefaultLogger(); | ||
try { | ||
@@ -270,5 +238,11 @@ for (const level of LOG_LEVELS) { | ||
this.globalLogger = globalLogger; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
this._handlers = /* @__PURE__ */ new WeakMap(); | ||
} | ||
_handlers = { | ||
["PROVIDER_CONFIGURATION_CHANGED" /* ConfigurationChanged */]: /* @__PURE__ */ new WeakMap(), | ||
["PROVIDER_CONTEXT_CHANGED" /* ContextChanged */]: /* @__PURE__ */ new WeakMap(), | ||
["PROVIDER_READY" /* Ready */]: /* @__PURE__ */ new WeakMap(), | ||
["PROVIDER_ERROR" /* Error */]: /* @__PURE__ */ new WeakMap(), | ||
["PROVIDER_STALE" /* Stale */]: /* @__PURE__ */ new WeakMap() | ||
}; | ||
_eventLogger; | ||
// here we use E, to restrict the events a provider can manually emit (PROVIDER_CONTEXT_CHANGED is emitted by the SDK) | ||
@@ -279,14 +253,17 @@ emit(eventType, context) { | ||
addHandler(eventType, handler) { | ||
const asyncHandler = (details) => __async(this, null, function* () { | ||
yield handler(details); | ||
}); | ||
this._handlers.set(handler, asyncHandler); | ||
const asyncHandler = async (details) => { | ||
await handler(details); | ||
}; | ||
const existingAsyncHandlers = this._handlers[eventType].get(handler); | ||
this._handlers[eventType].set(handler, [...existingAsyncHandlers || [], asyncHandler]); | ||
this.eventEmitter.on(eventType, asyncHandler); | ||
} | ||
removeHandler(eventType, handler) { | ||
const asyncHandler = this._handlers.get(handler); | ||
if (!asyncHandler) { | ||
return; | ||
const existingAsyncHandlers = this._handlers[eventType].get(handler); | ||
if (existingAsyncHandlers) { | ||
const removedAsyncHandler = existingAsyncHandlers.pop(); | ||
if (removedAsyncHandler) { | ||
this.eventEmitter.removeListener(eventType, removedAsyncHandler); | ||
} | ||
} | ||
this.eventEmitter.removeListener(eventType, asyncHandler); | ||
} | ||
@@ -308,4 +285,3 @@ removeAllHandlers(eventType) { | ||
get _logger() { | ||
var _a, _b; | ||
return (_b = this._eventLogger) != null ? _b : (_a = this.globalLogger) == null ? void 0 : _a.call(this); | ||
return this._eventLogger ?? this.globalLogger?.(); | ||
} | ||
@@ -335,9 +311,10 @@ }; | ||
var OpenFeatureCommonAPI = class { | ||
_hooks = []; | ||
_context = {}; | ||
_logger = new DefaultLogger(); | ||
_clientEventHandlers = /* @__PURE__ */ new Map(); | ||
_clientProviders = /* @__PURE__ */ new Map(); | ||
_clientEvents = /* @__PURE__ */ new Map(); | ||
_runsOn; | ||
constructor(category) { | ||
this._hooks = []; | ||
this._context = {}; | ||
this._logger = new DefaultLogger(); | ||
this._clientEventHandlers = /* @__PURE__ */ new Map(); | ||
this._clientProviders = /* @__PURE__ */ new Map(); | ||
this._clientEvents = /* @__PURE__ */ new Map(); | ||
this._runsOn = category; | ||
@@ -385,3 +362,2 @@ } | ||
[.../* @__PURE__ */ new Map([[void 0, this._defaultProvider]]), ...this._clientProviders].forEach((keyProviderTuple) => { | ||
var _a; | ||
const clientName = keyProviderTuple[0]; | ||
@@ -394,3 +370,3 @@ const provider = keyProviderTuple[1]; | ||
} catch (err) { | ||
(_a = this._logger) == null ? void 0 : _a.error("Error running event handler:", err); | ||
this._logger?.error("Error running event handler:", err); | ||
} | ||
@@ -423,6 +399,4 @@ } | ||
} | ||
setProviderAndWait(clientOrProvider, providerOrUndefined) { | ||
return __async(this, null, function* () { | ||
yield this.setAwaitableProvider(clientOrProvider, providerOrUndefined); | ||
}); | ||
async setProviderAndWait(clientOrProvider, providerOrUndefined) { | ||
await this.setAwaitableProvider(clientOrProvider, providerOrUndefined); | ||
} | ||
@@ -438,5 +412,4 @@ setProvider(clientOrProvider, providerOrUndefined) { | ||
setAwaitableProvider(clientOrProvider, providerOrUndefined) { | ||
var _a, _b, _c, _d, _e, _f; | ||
const clientName = stringOrUndefined(clientOrProvider); | ||
const provider = (_a = objectOrUndefined(clientOrProvider)) != null ? _a : objectOrUndefined(providerOrUndefined); | ||
const provider = objectOrUndefined(clientOrProvider) ?? objectOrUndefined(providerOrUndefined); | ||
if (!provider) { | ||
@@ -465,15 +438,13 @@ this._logger.debug("No provider defined, ignoring setProvider call"); | ||
let initializationPromise = void 0; | ||
if ((provider == null ? void 0 : provider.status) === "NOT_READY" /* NOT_READY */ && typeof provider.initialize === "function") { | ||
initializationPromise = (_d = (_c = (_b = provider.initialize) == null ? void 0 : _b.call(provider, this._context)) == null ? void 0 : _c.then(() => { | ||
var _a2; | ||
if (provider?.status === "NOT_READY" /* NOT_READY */ && typeof provider.initialize === "function") { | ||
initializationPromise = provider.initialize?.(this._context)?.then(() => { | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
emitter?.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
}); | ||
(_a2 = this._events) == null ? void 0 : _a2.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
})) == null ? void 0 : _d.catch((error) => { | ||
var _a2; | ||
this._events?.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
})?.catch((error) => { | ||
this.getAssociatedEventEmitters(clientName).forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error == null ? void 0 : error.message }); | ||
emitter?.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error?.message }); | ||
}); | ||
(_a2 = this._events) == null ? void 0 : _a2.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error == null ? void 0 : error.message }); | ||
this._events?.emit("PROVIDER_ERROR" /* Error */, { clientName, providerName, message: error?.message }); | ||
throw error; | ||
@@ -483,5 +454,5 @@ }); | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
emitter?.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
}); | ||
(_e = this._events) == null ? void 0 : _e.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
this._events?.emit("PROVIDER_READY" /* Ready */, { clientName, providerName }); | ||
} | ||
@@ -495,3 +466,3 @@ if (clientName) { | ||
if (![...this._clientProviders.values(), this._defaultProvider].includes(oldProvider)) { | ||
(_f = oldProvider == null ? void 0 : oldProvider.onClose) == null ? void 0 : _f.call(oldProvider); | ||
oldProvider?.onClose?.(); | ||
} | ||
@@ -501,7 +472,6 @@ return initializationPromise; | ||
getProviderForClient(name) { | ||
var _a; | ||
if (!name) { | ||
return this._defaultProvider; | ||
} | ||
return (_a = this._clientProviders.get(name)) != null ? _a : this._defaultProvider; | ||
return this._clientProviders.get(name) ?? this._defaultProvider; | ||
} | ||
@@ -517,8 +487,5 @@ buildAndCacheEventEmitterForClient(name) { | ||
Object.values(ClientProviderEvents).forEach( | ||
(eventType) => { | ||
var _a; | ||
return (_a = clientProvider.events) == null ? void 0 : _a.addHandler(eventType, (details) => __async(this, null, function* () { | ||
newEmitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName: name, providerName: clientProvider.metadata.name })); | ||
})); | ||
} | ||
(eventType) => clientProvider.events?.addHandler(eventType, async (details) => { | ||
newEmitter.emit(eventType, { ...details, clientName: name, providerName: clientProvider.metadata.name }); | ||
}) | ||
); | ||
@@ -542,58 +509,45 @@ return newEmitter; | ||
transferListeners(oldProvider, newProvider, clientName, emitters) { | ||
var _a; | ||
(_a = this._clientEventHandlers.get(clientName)) == null ? void 0 : _a.forEach((eventHandler) => { | ||
var _a2; | ||
return (_a2 = oldProvider.events) == null ? void 0 : _a2.removeHandler(...eventHandler); | ||
}); | ||
this._clientEventHandlers.get(clientName)?.forEach((eventHandler) => oldProvider.events?.removeHandler(...eventHandler)); | ||
const newClientHandlers = Object.values(ClientProviderEvents).map((eventType) => { | ||
const handler = (details) => __async(this, null, function* () { | ||
const handler = async (details) => { | ||
emitters.forEach((emitter) => { | ||
emitter == null ? void 0 : emitter.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName, providerName: newProvider.metadata.name })); | ||
emitter?.emit(eventType, { ...details, clientName, providerName: newProvider.metadata.name }); | ||
}); | ||
this._events.emit(eventType, __spreadProps(__spreadValues({}, details), { clientName, providerName: newProvider.metadata.name })); | ||
}); | ||
this._events.emit(eventType, { ...details, clientName, providerName: newProvider.metadata.name }); | ||
}; | ||
return [eventType, handler]; | ||
}); | ||
this._clientEventHandlers.set(clientName, newClientHandlers); | ||
newClientHandlers.forEach((eventHandler) => { | ||
var _a2; | ||
return (_a2 = newProvider.events) == null ? void 0 : _a2.addHandler(...eventHandler); | ||
}); | ||
newClientHandlers.forEach((eventHandler) => newProvider.events?.addHandler(...eventHandler)); | ||
} | ||
close() { | ||
return __async(this, null, function* () { | ||
var _a, _b; | ||
try { | ||
yield (_b = (_a = this == null ? void 0 : this._defaultProvider) == null ? void 0 : _a.onClose) == null ? void 0 : _b.call(_a); | ||
} catch (err) { | ||
this.handleShutdownError(this._defaultProvider, err); | ||
} | ||
const providers = Array.from(this._clientProviders); | ||
yield Promise.all( | ||
providers.map((_0) => __async(this, [_0], function* ([, provider]) { | ||
var _a2; | ||
try { | ||
yield (_a2 = provider.onClose) == null ? void 0 : _a2.call(provider); | ||
} catch (err) { | ||
this.handleShutdownError(provider, err); | ||
} | ||
})) | ||
); | ||
}); | ||
async close() { | ||
try { | ||
await this?._defaultProvider?.onClose?.(); | ||
} catch (err) { | ||
this.handleShutdownError(this._defaultProvider, err); | ||
} | ||
const providers = Array.from(this._clientProviders); | ||
await Promise.all( | ||
providers.map(async ([, provider]) => { | ||
try { | ||
await provider.onClose?.(); | ||
} catch (err) { | ||
this.handleShutdownError(provider, err); | ||
} | ||
}) | ||
); | ||
} | ||
clearProvidersAndSetDefault(defaultProvider) { | ||
return __async(this, null, function* () { | ||
try { | ||
yield this.close(); | ||
} catch (err) { | ||
this._logger.error("Unable to cleanly close providers. Resetting to the default configuration."); | ||
} finally { | ||
this._clientProviders.clear(); | ||
this._defaultProvider = defaultProvider; | ||
} | ||
}); | ||
async clearProvidersAndSetDefault(defaultProvider) { | ||
try { | ||
await this.close(); | ||
} catch (err) { | ||
this._logger.error("Unable to cleanly close providers. Resetting to the default configuration."); | ||
} finally { | ||
this._clientProviders.clear(); | ||
this._defaultProvider = defaultProvider; | ||
} | ||
} | ||
handleShutdownError(provider, err) { | ||
this._logger.error(`Error during shutdown of provider ${provider.metadata.name}: ${err}`); | ||
this._logger.error(err == null ? void 0 : err.stack); | ||
this._logger.error(err?.stack); | ||
} | ||
@@ -600,0 +554,0 @@ }; |
@@ -472,3 +472,3 @@ type FlagValueType = 'boolean' | 'string' | 'number' | 'object'; | ||
abstract code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -478,3 +478,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -484,3 +484,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -490,3 +490,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -496,3 +496,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -502,3 +502,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -508,3 +508,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -514,3 +514,3 @@ | ||
code: ErrorCode; | ||
constructor(message?: string); | ||
constructor(message?: string, options?: ErrorOptions); | ||
} | ||
@@ -517,0 +517,0 @@ |
{ | ||
"name": "@openfeature/core", | ||
"version": "0.0.23", | ||
"version": "0.0.24", | ||
"description": "Shared OpenFeature JS components (server and web)", | ||
@@ -22,4 +22,4 @@ "main": "./dist/cjs/index.js", | ||
"type": "tsc --project ./tsconfig.json --declaration --emitDeclarationOnly", | ||
"build:esm": "esbuild src/index.ts --bundle --external:events --sourcemap --target=es2016 --format=esm --outfile=./dist/esm/index.js --analyze", | ||
"build:cjs": "esbuild src/index.ts --bundle --external:events --sourcemap --target=es2016 --format=cjs --outfile=./dist/cjs/index.js --analyze", | ||
"build:esm": "esbuild src/index.ts --bundle --external:events --sourcemap --target=es2022 --format=esm --outfile=./dist/esm/index.js --analyze", | ||
"build:cjs": "esbuild src/index.ts --bundle --external:events --sourcemap --target=es2022 --format=cjs --outfile=./dist/cjs/index.js --analyze", | ||
"build:rollup-types": "rollup -c ../../rollup.config.mjs", | ||
@@ -26,0 +26,0 @@ "build": "npm run clean && npm run build:esm && npm run build:cjs && npm run build:rollup-types", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
171196
1718