@launchdarkly/js-sdk-common 2.20.0 → 2.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  All notable changes to `@launchdarkly/js-sdk-common` will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org).
4
4
 
5
+ ## [2.22.0](https://github.com/launchdarkly/js-core/compare/js-sdk-common-v2.21.0...js-sdk-common-v2.22.0) (2026-02-25)
6
+
7
+
8
+ ### Features
9
+
10
+ * Add flag eval model for FDv2. ([#1124](https://github.com/launchdarkly/js-core/issues/1124)) ([028e63f](https://github.com/launchdarkly/js-core/commit/028e63f34eb0f11c5c0d8d078baf0ec378b9e8e0))
11
+
12
+ ## [2.21.0](https://github.com/launchdarkly/js-core/compare/js-sdk-common-v2.20.0...js-sdk-common-v2.21.0) (2026-02-25)
13
+
14
+
15
+ ### Features
16
+
17
+ * Refactor FDV2 protocol handling. ([4570089](https://github.com/launchdarkly/js-core/commit/4570089cd478cc5811a9a1c207231a96fdb5b39a))
18
+
5
19
  ## [2.20.0](https://github.com/launchdarkly/js-core/compare/js-sdk-common-v2.19.0...js-sdk-common-v2.20.0) (2025-12-05)
6
20
 
7
21
 
@@ -3018,135 +3018,200 @@ function fdv1PayloadAdaptor(processor) {
3018
3018
  };
3019
3019
  }
3020
3020
 
3021
- /**
3022
- * A FDv2 PayloadProcessor can be used to parse payloads from a stream of FDv2 events. It will send payloads
3023
- * to the PayloadListeners as the payloads are received. Invalid series of events may be dropped silently,
3024
- * but the payload processor will continue to operate.
3025
- */
3026
- class PayloadProcessor {
3027
- /**
3028
- * Creates a PayloadProcessor
3029
- *
3030
- * @param _objProcessors defines object processors for each object kind.
3031
- * @param _errorHandler that will be called with parsing errors as they are encountered
3032
- * @param _logger for logging
3033
- */
3034
- constructor(_objProcessors, _errorHandler, _logger) {
3035
- this._objProcessors = _objProcessors;
3036
- this._errorHandler = _errorHandler;
3037
- this._logger = _logger;
3038
- this._listeners = [];
3039
- this._tempId = undefined;
3040
- this._tempBasis = false;
3041
- this._tempUpdates = [];
3042
- this._processServerIntent = (data) => {
3043
- // clear state in prep for handling data
3044
- this._resetAll();
3045
- // if there's no payloads, return
3046
- if (!data.payloads.length) {
3047
- return;
3048
- }
3049
- // at the time of writing this, it was agreed upon that SDKs could assume exactly 1 element in this list. In the future, a negotiation of protocol version will be required to remove this assumption.
3050
- const payload = data.payloads[0];
3051
- switch (payload?.intentCode) {
3052
- case 'xfer-full':
3053
- this._tempBasis = true;
3054
- break;
3055
- case 'xfer-changes':
3056
- this._tempBasis = false;
3057
- break;
3058
- case 'none':
3059
- this._tempBasis = false;
3060
- this._processIntentNone(payload);
3061
- break;
3062
- default:
3063
- // unrecognized intent code, return
3064
- this._logger?.warn(`Unable to process intent code '${payload?.intentCode}'.`);
3065
- return;
3066
- }
3067
- this._tempId = payload?.id;
3068
- };
3069
- this._processPutObject = (data) => {
3070
- // if the following properties haven't been provided by now, we should ignore the event
3071
- if (!this._tempId || // server intent hasn't been received yet.
3072
- !data.kind ||
3073
- !data.key ||
3074
- !data.version ||
3075
- !data.object) {
3076
- return;
3077
- }
3078
- const obj = this._processObj(data.kind, data.object);
3079
- if (!obj) {
3080
- this._logger?.warn(`Unable to process object for kind: '${data.kind}'`);
3081
- // ignore unrecognized kinds
3082
- return;
3083
- }
3084
- this._tempUpdates.push({
3085
- kind: data.kind,
3086
- key: data.key,
3087
- version: data.version,
3088
- object: obj,
3089
- // intentionally omit deleted for this put
3090
- });
3091
- };
3092
- this._processDeleteObject = (data) => {
3093
- // if the following properties haven't been provided by now, we should ignore the event
3094
- if (!this._tempId || !data.kind || !data.key || !data.version) {
3095
- return;
3096
- }
3097
- this._tempUpdates.push({
3098
- kind: data.kind,
3099
- key: data.key,
3100
- version: data.version,
3101
- // intentionally omit object for this delete
3102
- deleted: true,
3103
- });
3104
- };
3105
- this._processIntentNone = (intent) => {
3106
- // if the following properties aren't present ignore the event
3107
- if (!intent.id || !intent.target) {
3108
- return;
3109
- }
3110
- const payload = {
3021
+ const ACTION_NONE = { type: 'none' };
3022
+ function createProtocolHandler(objProcessors, logger) {
3023
+ let protocolState = 'inactive';
3024
+ let tempId;
3025
+ let tempType = 'partial';
3026
+ let tempUpdates = [];
3027
+ function processObj(kind, jsonObj) {
3028
+ return objProcessors[kind]?.(jsonObj);
3029
+ }
3030
+ function resetAll() {
3031
+ protocolState = 'inactive';
3032
+ tempId = undefined;
3033
+ tempType = 'partial';
3034
+ tempUpdates = [];
3035
+ }
3036
+ function resetAfterEmission() {
3037
+ protocolState = 'changes';
3038
+ tempType = 'partial';
3039
+ tempUpdates = [];
3040
+ }
3041
+ function resetAfterError() {
3042
+ tempUpdates = [];
3043
+ }
3044
+ function processIntentNone(intent) {
3045
+ if (!intent.id || !intent.target) {
3046
+ return ACTION_NONE;
3047
+ }
3048
+ return {
3049
+ type: 'payload',
3050
+ payload: {
3111
3051
  id: intent.id,
3112
3052
  version: intent.target,
3113
- basis: false,
3114
- updates: [], // payload with no updates to hide the intent none concept from the consumer
3115
- // note: state is absent here as that only appears in payload transferred events
3116
- };
3117
- this._listeners.forEach((it) => it(payload));
3118
- this._resetAfterEmission();
3053
+ type: 'none',
3054
+ updates: [],
3055
+ },
3119
3056
  };
3120
- this._processPayloadTransferred = (data) => {
3121
- // if the following properties haven't been provided by now, we should reset
3122
- if (
3123
- // server intent hasn't been received yet.
3124
- !this._tempId ||
3125
- // selector can be an empty string if we are using a file data initilizer
3126
- data.state === null ||
3127
- data.state === undefined ||
3128
- !data.version) {
3129
- this._resetAll(); // a reset is best defensive action since payload transferred terminates a payload
3130
- return;
3131
- }
3132
- const payload = {
3133
- id: this._tempId,
3057
+ }
3058
+ function processServerIntent(data) {
3059
+ if (!data.payloads?.length) {
3060
+ return {
3061
+ type: 'error',
3062
+ kind: 'MISSING_PAYLOAD',
3063
+ message: 'No payload present in server-intent',
3064
+ };
3065
+ }
3066
+ // Per spec 3.4.2: SDK uses only the first payload.
3067
+ const payload = data.payloads[0];
3068
+ switch (payload?.intentCode) {
3069
+ case 'xfer-full':
3070
+ protocolState = 'full';
3071
+ tempUpdates = [];
3072
+ tempType = 'full';
3073
+ tempId = payload.id;
3074
+ return ACTION_NONE;
3075
+ case 'xfer-changes':
3076
+ protocolState = 'changes';
3077
+ tempUpdates = [];
3078
+ tempType = 'partial';
3079
+ tempId = payload.id;
3080
+ return ACTION_NONE;
3081
+ case 'none':
3082
+ protocolState = 'changes';
3083
+ tempUpdates = [];
3084
+ tempType = 'partial';
3085
+ tempId = payload.id;
3086
+ return processIntentNone(payload);
3087
+ default:
3088
+ logger?.warn(`Unable to process intent code '${payload?.intentCode}'.`);
3089
+ return ACTION_NONE;
3090
+ }
3091
+ }
3092
+ function processPutObject(data) {
3093
+ if (protocolState === 'inactive' ||
3094
+ !tempId ||
3095
+ !data.kind ||
3096
+ !data.key ||
3097
+ !data.version ||
3098
+ !data.object) {
3099
+ return ACTION_NONE;
3100
+ }
3101
+ const obj = processObj(data.kind, data.object);
3102
+ if (!obj) {
3103
+ logger?.warn(`Unable to process object for kind: '${data.kind}'`);
3104
+ return ACTION_NONE;
3105
+ }
3106
+ tempUpdates.push({
3107
+ kind: data.kind,
3108
+ key: data.key,
3109
+ version: data.version,
3110
+ object: obj,
3111
+ });
3112
+ return ACTION_NONE;
3113
+ }
3114
+ function processDeleteObject(data) {
3115
+ if (protocolState === 'inactive' || !tempId || !data.kind || !data.key || !data.version) {
3116
+ return ACTION_NONE;
3117
+ }
3118
+ tempUpdates.push({
3119
+ kind: data.kind,
3120
+ key: data.key,
3121
+ version: data.version,
3122
+ deleted: true,
3123
+ });
3124
+ return ACTION_NONE;
3125
+ }
3126
+ function processPayloadTransferred(data) {
3127
+ if (protocolState === 'inactive') {
3128
+ return {
3129
+ type: 'error',
3130
+ kind: 'PROTOCOL_ERROR',
3131
+ message: 'A payload transferred has been received without an intent having been established.',
3132
+ };
3133
+ }
3134
+ if (!tempId || data.state === null || data.state === undefined || !data.version) {
3135
+ resetAll();
3136
+ return ACTION_NONE;
3137
+ }
3138
+ const result = {
3139
+ type: 'payload',
3140
+ payload: {
3141
+ id: tempId,
3134
3142
  version: data.version,
3135
3143
  state: data.state,
3136
- basis: this._tempBasis,
3137
- updates: this._tempUpdates,
3138
- };
3139
- this._listeners.forEach((it) => it(payload));
3140
- this._resetAfterEmission();
3141
- };
3142
- this._processGoodbye = (data) => {
3143
- this._logger?.info(`Goodbye was received from the LaunchDarkly connection with reason: ${data.reason}.`);
3144
- this._resetAll();
3145
- };
3146
- this._processError = (data) => {
3147
- this._logger?.info(`An issue was encountered receiving updates for payload ${this._tempId} with reason: ${data.reason}.`);
3148
- this._resetAfterError();
3144
+ type: tempType,
3145
+ updates: tempUpdates,
3146
+ },
3149
3147
  };
3148
+ resetAfterEmission();
3149
+ return result;
3150
+ }
3151
+ function processGoodbye(data) {
3152
+ logger?.info(`Goodbye was received from the LaunchDarkly connection with reason: ${data.reason}.`);
3153
+ resetAll();
3154
+ return { type: 'goodbye', reason: data.reason };
3155
+ }
3156
+ function processError(data) {
3157
+ logger?.info(`An issue was encountered receiving updates for payload ${tempId} with reason: ${data.reason}.`);
3158
+ resetAfterError();
3159
+ return { type: 'serverError', id: data.payload_id, reason: data.reason };
3160
+ }
3161
+ return {
3162
+ get state() {
3163
+ return protocolState;
3164
+ },
3165
+ processEvent(event) {
3166
+ switch (event.event) {
3167
+ case 'server-intent':
3168
+ return processServerIntent(event.data);
3169
+ case 'put-object':
3170
+ return processPutObject(event.data);
3171
+ case 'delete-object':
3172
+ return processDeleteObject(event.data);
3173
+ case 'payload-transferred':
3174
+ return processPayloadTransferred(event.data);
3175
+ case 'goodbye':
3176
+ return processGoodbye(event.data);
3177
+ case 'error':
3178
+ return processError(event.data);
3179
+ case 'heart-beat':
3180
+ return ACTION_NONE;
3181
+ default:
3182
+ return {
3183
+ type: 'error',
3184
+ kind: 'UNKNOWN_EVENT',
3185
+ message: `Received an unknown event of type '${event.event}'`,
3186
+ };
3187
+ }
3188
+ },
3189
+ reset() {
3190
+ resetAll();
3191
+ },
3192
+ };
3193
+ }
3194
+
3195
+ /**
3196
+ * Errors that indicate a problem with the data or protocol flow and should
3197
+ * be reported to the error handler. Informational errors like UNKNOWN_EVENT
3198
+ * are intentionally excluded to preserve forward compatibility — older SDKs
3199
+ * should silently ignore new event types added to the protocol.
3200
+ */
3201
+ function isActionableError(kind) {
3202
+ return kind === 'MISSING_PAYLOAD' || kind === 'PROTOCOL_ERROR';
3203
+ }
3204
+ /**
3205
+ * Parses payloads from a stream of FDv2 events by delegating to a protocol handler.
3206
+ * Sends completed payloads to registered listeners.
3207
+ * Invalid event sequences may be dropped silently, but the processor will continue to operate.
3208
+ */
3209
+ class PayloadProcessor {
3210
+ constructor(objProcessors, _errorHandler, _logger) {
3211
+ this._errorHandler = _errorHandler;
3212
+ this._logger = _logger;
3213
+ this._listeners = [];
3214
+ this._handler = createProtocolHandler(objProcessors, _logger);
3150
3215
  }
3151
3216
  addPayloadListener(listener) {
3152
3217
  this._listeners.push(listener);
@@ -3157,56 +3222,24 @@ class PayloadProcessor {
3157
3222
  this._listeners.splice(index, 1);
3158
3223
  }
3159
3224
  }
3160
- /**
3161
- * Gives the {@link PayloadProcessor} a series of events that it will statefully, incrementally process.
3162
- * This may lead to listeners being invoked as necessary.
3163
- * @param events to be processed (can be a single element)
3164
- */
3165
3225
  processEvents(events) {
3166
3226
  events.forEach((event) => {
3167
- switch (event.event) {
3168
- case 'server-intent': {
3169
- this._processServerIntent(event.data);
3170
- break;
3171
- }
3172
- case 'put-object': {
3173
- this._processPutObject(event.data);
3227
+ const action = this._handler.processEvent(event);
3228
+ switch (action.type) {
3229
+ case 'payload':
3230
+ this._listeners.forEach((it) => it(action.payload));
3174
3231
  break;
3175
- }
3176
- case 'delete-object': {
3177
- this._processDeleteObject(event.data);
3178
- break;
3179
- }
3180
- case 'payload-transferred': {
3181
- this._processPayloadTransferred(event.data);
3182
- break;
3183
- }
3184
- case 'goodbye': {
3185
- this._processGoodbye(event.data);
3186
- break;
3187
- }
3188
- case 'error': {
3189
- this._processError(event.data);
3232
+ case 'error':
3233
+ if (isActionableError(action.kind)) {
3234
+ this._errorHandler?.(exports.DataSourceErrorKind.InvalidData, action.message);
3235
+ }
3236
+ else {
3237
+ this._logger?.warn(action.message);
3238
+ }
3190
3239
  break;
3191
- }
3192
3240
  }
3193
3241
  });
3194
3242
  }
3195
- _processObj(kind, jsonObj) {
3196
- return this._objProcessors[kind]?.(jsonObj);
3197
- }
3198
- _resetAfterEmission() {
3199
- this._tempBasis = false;
3200
- this._tempUpdates = [];
3201
- }
3202
- _resetAfterError() {
3203
- this._tempUpdates = [];
3204
- }
3205
- _resetAll() {
3206
- this._tempId = undefined;
3207
- this._tempBasis = false;
3208
- this._tempUpdates = [];
3209
- }
3210
3243
  }
3211
3244
 
3212
3245
  /**
@@ -3335,6 +3368,7 @@ var index = /*#__PURE__*/Object.freeze({
3335
3368
  PayloadProcessor: PayloadProcessor,
3336
3369
  PayloadStreamReader: PayloadStreamReader,
3337
3370
  canonicalize: canonicalize,
3371
+ createProtocolHandler: createProtocolHandler,
3338
3372
  initMetadataFromHeaders: initMetadataFromHeaders,
3339
3373
  isLegacyUser: isLegacyUser,
3340
3374
  isMultiKind: isMultiKind,