@launchdarkly/js-sdk-common 2.19.0 → 2.21.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.
Files changed (30) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/index.cjs +261 -160
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs/internal/fdv2/FDv1PayloadAdaptor.d.ts +43 -0
  5. package/dist/cjs/internal/fdv2/FDv1PayloadAdaptor.d.ts.map +1 -0
  6. package/dist/cjs/internal/fdv2/index.d.ts +7 -2
  7. package/dist/cjs/internal/fdv2/index.d.ts.map +1 -1
  8. package/dist/cjs/internal/fdv2/payloadProcessor.d.ts +7 -56
  9. package/dist/cjs/internal/fdv2/payloadProcessor.d.ts.map +1 -1
  10. package/dist/cjs/internal/fdv2/payloadStreamReader.d.ts +1 -1
  11. package/dist/cjs/internal/fdv2/payloadStreamReader.d.ts.map +1 -1
  12. package/dist/cjs/internal/fdv2/proto.d.ts +29 -6
  13. package/dist/cjs/internal/fdv2/proto.d.ts.map +1 -1
  14. package/dist/cjs/internal/fdv2/protocolHandler.d.ts +76 -0
  15. package/dist/cjs/internal/fdv2/protocolHandler.d.ts.map +1 -0
  16. package/dist/esm/index.mjs +261 -160
  17. package/dist/esm/index.mjs.map +1 -1
  18. package/dist/esm/internal/fdv2/FDv1PayloadAdaptor.d.ts +43 -0
  19. package/dist/esm/internal/fdv2/FDv1PayloadAdaptor.d.ts.map +1 -0
  20. package/dist/esm/internal/fdv2/index.d.ts +7 -2
  21. package/dist/esm/internal/fdv2/index.d.ts.map +1 -1
  22. package/dist/esm/internal/fdv2/payloadProcessor.d.ts +7 -56
  23. package/dist/esm/internal/fdv2/payloadProcessor.d.ts.map +1 -1
  24. package/dist/esm/internal/fdv2/payloadStreamReader.d.ts +1 -1
  25. package/dist/esm/internal/fdv2/payloadStreamReader.d.ts.map +1 -1
  26. package/dist/esm/internal/fdv2/proto.d.ts +29 -6
  27. package/dist/esm/internal/fdv2/proto.d.ts.map +1 -1
  28. package/dist/esm/internal/fdv2/protocolHandler.d.ts +76 -0
  29. package/dist/esm/internal/fdv2/protocolHandler.d.ts.map +1 -0
  30. package/package.json +1 -1
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.21.0](https://github.com/launchdarkly/js-core/compare/js-sdk-common-v2.20.0...js-sdk-common-v2.21.0) (2026-02-25)
6
+
7
+
8
+ ### Features
9
+
10
+ * Refactor FDV2 protocol handling. ([4570089](https://github.com/launchdarkly/js-core/commit/4570089cd478cc5811a9a1c207231a96fdb5b39a))
11
+
12
+ ## [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)
13
+
14
+
15
+ ### Features
16
+
17
+ * add FDv2 FileDataSource initializer ([#1010](https://github.com/launchdarkly/js-core/issues/1010)) ([99931f0](https://github.com/launchdarkly/js-core/commit/99931f0706c0202390e3b68e3701ba0fb8aba124))
18
+
5
19
  ## [2.19.0](https://github.com/launchdarkly/js-core/compare/js-sdk-common-v2.18.0...js-sdk-common-v2.19.0) (2025-07-23)
6
20
 
7
21
 
@@ -2956,131 +2956,262 @@ class EventFactoryBase {
2956
2956
  }
2957
2957
  }
2958
2958
 
2959
- /**
2960
- * A FDv2 PayloadProcessor can be used to parse payloads from a stream of FDv2 events. It will send payloads
2961
- * to the PayloadListeners as the payloads are received. Invalid series of events may be dropped silently,
2962
- * but the payload processor will continue to operate.
2963
- */
2964
- class PayloadProcessor {
2965
- /**
2966
- * Creates a PayloadProcessor
2967
- *
2968
- * @param _objProcessors defines object processors for each object kind.
2969
- * @param _errorHandler that will be called with parsing errors as they are encountered
2970
- * @param _logger for logging
2971
- */
2972
- constructor(_objProcessors, _errorHandler, _logger) {
2973
- this._objProcessors = _objProcessors;
2974
- this._errorHandler = _errorHandler;
2975
- this._logger = _logger;
2976
- this._listeners = [];
2977
- this._tempId = undefined;
2978
- this._tempBasis = false;
2979
- this._tempUpdates = [];
2980
- this._processServerIntent = (data) => {
2981
- // clear state in prep for handling data
2982
- this._resetAll();
2983
- // if there's no payloads, return
2984
- if (!data.payloads.length) {
2985
- return;
2986
- }
2987
- // 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.
2988
- const payload = data.payloads[0];
2989
- switch (payload?.intentCode) {
2990
- case 'xfer-full':
2991
- this._tempBasis = true;
2992
- break;
2993
- case 'xfer-changes':
2994
- this._tempBasis = false;
2995
- break;
2996
- case 'none':
2997
- this._tempBasis = false;
2998
- this._processIntentNone(payload);
2999
- break;
3000
- default:
3001
- // unrecognized intent code, return
3002
- this._logger?.warn(`Unable to process intent code '${payload?.intentCode}'.`);
3003
- return;
3004
- }
3005
- this._tempId = payload?.id;
3006
- };
3007
- this._processPutObject = (data) => {
3008
- // if the following properties haven't been provided by now, we should ignore the event
3009
- if (!this._tempId || // server intent hasn't been received yet.
3010
- !data.kind ||
3011
- !data.key ||
3012
- !data.version ||
3013
- !data.object) {
3014
- return;
3015
- }
3016
- const obj = this._processObj(data.kind, data.object);
3017
- if (!obj) {
3018
- this._logger?.warn(`Unable to process object for kind: '${data.kind}'`);
3019
- // ignore unrecognized kinds
3020
- return;
3021
- }
3022
- this._tempUpdates.push({
3023
- kind: data.kind,
3024
- key: data.key,
3025
- version: data.version,
3026
- object: obj,
3027
- // intentionally omit deleted for this put
2959
+ const PAYLOAD_ID = 'FDv1Fallback';
2960
+ function fdv1PayloadAdaptor(processor) {
2961
+ return {
2962
+ _processor: processor,
2963
+ _selector: '',
2964
+ useSelector(selector) {
2965
+ this._selector = selector;
2966
+ return this;
2967
+ },
2968
+ processFullTransfer(data) {
2969
+ const events = [
2970
+ {
2971
+ event: 'server-intent',
2972
+ data: {
2973
+ payloads: [
2974
+ {
2975
+ id: PAYLOAD_ID,
2976
+ target: 1,
2977
+ intentCode: 'xfer-full',
2978
+ reason: 'payload-missing',
2979
+ },
2980
+ ],
2981
+ },
2982
+ },
2983
+ ];
2984
+ Object.entries(data?.flags || []).forEach(([key, flag]) => {
2985
+ events.push({
2986
+ event: 'put-object',
2987
+ data: {
2988
+ kind: 'flag',
2989
+ key,
2990
+ version: flag.version || 1,
2991
+ object: flag,
2992
+ },
2993
+ });
3028
2994
  });
3029
- };
3030
- this._processDeleteObject = (data) => {
3031
- // if the following properties haven't been provided by now, we should ignore the event
3032
- if (!this._tempId || !data.kind || !data.key || !data.version) {
3033
- return;
3034
- }
3035
- this._tempUpdates.push({
3036
- kind: data.kind,
3037
- key: data.key,
3038
- version: data.version,
3039
- // intentionally omit object for this delete
3040
- deleted: true,
2995
+ Object.entries(data?.segments || []).forEach(([key, segment]) => {
2996
+ events.push({
2997
+ event: 'put-object',
2998
+ data: {
2999
+ kind: 'segment',
3000
+ key,
3001
+ version: segment.version || 1,
3002
+ object: segment,
3003
+ },
3004
+ });
3041
3005
  });
3042
- };
3043
- this._processIntentNone = (intent) => {
3044
- // if the following properties aren't present ignore the event
3045
- if (!intent.id || !intent.target) {
3046
- return;
3047
- }
3048
- const payload = {
3006
+ events.push({
3007
+ event: 'payload-transferred',
3008
+ data: {
3009
+ // IMPORTANT: the selector MUST be empty or "live" data synchronizers
3010
+ // will not work as it would try to resume from a bogus state.
3011
+ state: this._selector,
3012
+ version: 1,
3013
+ id: PAYLOAD_ID,
3014
+ },
3015
+ });
3016
+ this._processor.processEvents(events);
3017
+ },
3018
+ };
3019
+ }
3020
+
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: {
3049
3051
  id: intent.id,
3050
3052
  version: intent.target,
3051
- basis: false,
3052
- updates: [], // payload with no updates to hide the intent none concept from the consumer
3053
- // note: state is absent here as that only appears in payload transferred events
3054
- };
3055
- this._listeners.forEach((it) => it(payload));
3056
- this._resetAfterEmission();
3053
+ type: 'none',
3054
+ updates: [],
3055
+ },
3057
3056
  };
3058
- this._processPayloadTransferred = (data) => {
3059
- // if the following properties haven't been provided by now, we should reset
3060
- if (!this._tempId || // server intent hasn't been received yet.
3061
- !data.state ||
3062
- !data.version) {
3063
- this._resetAll(); // a reset is best defensive action since payload transferred terminates a payload
3064
- return;
3065
- }
3066
- const payload = {
3067
- 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,
3068
3142
  version: data.version,
3069
3143
  state: data.state,
3070
- basis: this._tempBasis,
3071
- updates: this._tempUpdates,
3072
- };
3073
- this._listeners.forEach((it) => it(payload));
3074
- this._resetAfterEmission();
3075
- };
3076
- this._processGoodbye = (data) => {
3077
- this._logger?.info(`Goodbye was received from the LaunchDarkly connection with reason: ${data.reason}.`);
3078
- this._resetAll();
3079
- };
3080
- this._processError = (data) => {
3081
- this._logger?.info(`An issue was encountered receiving updates for payload ${this._tempId} with reason: ${data.reason}.`);
3082
- this._resetAfterError();
3144
+ type: tempType,
3145
+ updates: tempUpdates,
3146
+ },
3083
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);
3084
3215
  }
3085
3216
  addPayloadListener(listener) {
3086
3217
  this._listeners.push(listener);
@@ -3091,56 +3222,24 @@ class PayloadProcessor {
3091
3222
  this._listeners.splice(index, 1);
3092
3223
  }
3093
3224
  }
3094
- /**
3095
- * Gives the {@link PayloadProcessor} a series of events that it will statefully, incrementally process.
3096
- * This may lead to listeners being invoked as necessary.
3097
- * @param events to be processed (can be a single element)
3098
- */
3099
3225
  processEvents(events) {
3100
3226
  events.forEach((event) => {
3101
- switch (event.event) {
3102
- case 'server-intent': {
3103
- this._processServerIntent(event.data);
3104
- break;
3105
- }
3106
- case 'put-object': {
3107
- this._processPutObject(event.data);
3108
- break;
3109
- }
3110
- case 'delete-object': {
3111
- this._processDeleteObject(event.data);
3112
- break;
3113
- }
3114
- case 'payload-transferred': {
3115
- this._processPayloadTransferred(event.data);
3227
+ const action = this._handler.processEvent(event);
3228
+ switch (action.type) {
3229
+ case 'payload':
3230
+ this._listeners.forEach((it) => it(action.payload));
3116
3231
  break;
3117
- }
3118
- case 'goodbye': {
3119
- this._processGoodbye(event.data);
3120
- break;
3121
- }
3122
- case 'error': {
3123
- 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
+ }
3124
3239
  break;
3125
- }
3126
3240
  }
3127
3241
  });
3128
3242
  }
3129
- _processObj(kind, jsonObj) {
3130
- return this._objProcessors[kind]?.(jsonObj);
3131
- }
3132
- _resetAfterEmission() {
3133
- this._tempBasis = false;
3134
- this._tempUpdates = [];
3135
- }
3136
- _resetAfterError() {
3137
- this._tempUpdates = [];
3138
- }
3139
- _resetAll() {
3140
- this._tempId = undefined;
3141
- this._tempBasis = false;
3142
- this._tempUpdates = [];
3143
- }
3144
3243
  }
3145
3244
 
3146
3245
  /**
@@ -3261,6 +3360,7 @@ var index = /*#__PURE__*/Object.freeze({
3261
3360
  ErrorKinds: ErrorKinds$1,
3262
3361
  EventFactoryBase: EventFactoryBase,
3263
3362
  EventProcessor: EventProcessor,
3363
+ FDv1PayloadAdaptor: fdv1PayloadAdaptor,
3264
3364
  InputCustomEvent: InputCustomEvent,
3265
3365
  InputEvalEvent: InputEvalEvent,
3266
3366
  InputIdentifyEvent: InputIdentifyEvent,
@@ -3268,6 +3368,7 @@ var index = /*#__PURE__*/Object.freeze({
3268
3368
  PayloadProcessor: PayloadProcessor,
3269
3369
  PayloadStreamReader: PayloadStreamReader,
3270
3370
  canonicalize: canonicalize,
3371
+ createProtocolHandler: createProtocolHandler,
3271
3372
  initMetadataFromHeaders: initMetadataFromHeaders,
3272
3373
  isLegacyUser: isLegacyUser,
3273
3374
  isMultiKind: isMultiKind,