@launchdarkly/js-sdk-common 2.29.0 → 2.30.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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toRequestInit.d.ts","sourceRoot":"","sources":["../../../src/internal/fetch/toRequestInit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEtD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,GAAG;IAAE,MAAM,CAAC,EAAE,GAAG,CAAA;CAAE,CAE1F"}
@@ -3,6 +3,7 @@ export * from './diagnostics';
3
3
  export * from './evaluation';
4
4
  export * from './events';
5
5
  export * from './fdv2';
6
+ export * from './fetch';
6
7
  export * from './metadata';
7
8
  export * from './plugins';
8
9
  export * from './json';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/internal/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/internal/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,QAAQ,CAAC"}
@@ -38,6 +38,41 @@ export interface Headers {
38
38
  * @param name The name of the header to check.
39
39
  */
40
40
  has(name: string): boolean;
41
+ /**
42
+ * Executes the callback once for each header, with the value first. Optional so existing
43
+ * implementations remain valid. The streaming client reads response headers through this
44
+ * method, or through {@link Headers.entries} when this method is absent.
45
+ *
46
+ * https://developer.mozilla.org/en-US/docs/Web/API/Headers/forEach
47
+ */
48
+ forEach?(callback: (value: string, key: string) => void): void;
49
+ }
50
+ /**
51
+ * A reader over a response body stream.
52
+ */
53
+ export interface BodyReader {
54
+ /**
55
+ * Resolves with the next chunk, or with `done: true` when the server ends the stream.
56
+ * Rejects when the connection terminates before the server ends the stream.
57
+ */
58
+ read(): Promise<{
59
+ done: boolean;
60
+ value?: Uint8Array;
61
+ }>;
62
+ /**
63
+ * Releases the reader and its connection.
64
+ */
65
+ cancel?(): Promise<unknown> | void;
66
+ }
67
+ /**
68
+ * A response body stream.
69
+ */
70
+ export interface ResponseBody {
71
+ getReader(): BodyReader;
72
+ /**
73
+ * Releases the body.
74
+ */
75
+ cancel?(): Promise<unknown> | void;
41
76
  }
42
77
  /**
43
78
  * Interface for fetch responses.
@@ -45,6 +80,20 @@ export interface Headers {
45
80
  export interface Response {
46
81
  headers: Headers;
47
82
  status: number;
83
+ /**
84
+ * The HTTP status message of the response. Optional so existing implementations remain
85
+ * valid.
86
+ *
87
+ * https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText
88
+ */
89
+ statusText?: string;
90
+ /**
91
+ * The response body stream. An implementation provides it for a request made with
92
+ * {@link Options.streaming}. It can also provide it for other requests.
93
+ *
94
+ * https://developer.mozilla.org/en-US/docs/Web/API/Response/body
95
+ */
96
+ body?: ResponseBody | null;
48
97
  /**
49
98
  * Read the response and provide it as a string.
50
99
  */
@@ -54,6 +103,25 @@ export interface Response {
54
103
  */
55
104
  json(): Promise<any>;
56
105
  }
106
+ /**
107
+ * The platform's own `AbortSignal` type when the platform declares one as a global
108
+ * variable. An implementation that passes {@link Options} to a native `fetch` then
109
+ * type-checks against its platform's `RequestInit`. A platform that declares
110
+ * `AbortSignal` as a lexical class resolves to the fallback instead. The fallback is
111
+ * the minimal structural subset an implementation consumes. It keeps these declarations
112
+ * valid on a platform without the global.
113
+ */
114
+ export type AbortSignalLike = typeof globalThis extends {
115
+ AbortSignal: {
116
+ prototype: infer T;
117
+ };
118
+ } ? T : {
119
+ readonly aborted: boolean;
120
+ addEventListener(type: 'abort', listener: () => void, options?: {
121
+ once?: boolean;
122
+ }): void;
123
+ removeEventListener(type: 'abort', listener: () => void): void;
124
+ };
57
125
  export interface Options {
58
126
  headers?: Record<string, string>;
59
127
  method?: string;
@@ -69,6 +137,23 @@ export interface Options {
69
137
  * https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#keepalive
70
138
  */
71
139
  keepalive?: boolean;
140
+ /**
141
+ * An abort signal for the request. When the signal aborts, the implementation stops the
142
+ * request and rejects any pending read. Platform support for this field is best effort.
143
+ *
144
+ * https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#signal
145
+ */
146
+ signal?: AbortSignalLike;
147
+ /**
148
+ * True for a streaming request. The implementation does not buffer the response body,
149
+ * it does not request compressed content, and it ignores {@link Options.timeout}. The
150
+ * response exposes its body through
151
+ * {@link Response.body}. An SDK must use a fetch as a streaming transport only when the
152
+ * implementation honors this field. A streaming implementation must provide at least one
153
+ * release path. It honors {@link Options.signal}, or it provides the body cancel. It
154
+ * preferably provides both.
155
+ */
156
+ streaming?: boolean;
72
157
  }
73
158
  export interface EventSourceCapabilities {
74
159
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"Requests.d.ts","sourceRoot":"","sources":["../../../src/api/platform/Requests.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAOtE;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB;;;;;;OAMG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAEjC;;;;;;OAMG;IACH,IAAI,IAAI,QAAQ,CAAC,MAAM,CAA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C;IAEzB;;;;;;;;OAQG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,uBAAuB;IACtC;;;;;;;;;OASG;IACH,WAAW,EAAE,OAAO,CAAC;IAErB;;;OAGG;IACH,YAAY,EAAE,OAAO,CAAC;IAEtB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,MAAM,WAAW,QAAQ;IACvB,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAEzD,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,mBAAmB,EAAE,mBAAmB,GAAG,WAAW,CAAC;IAEtF,0BAA0B,IAAI,uBAAuB,CAAC;IAEtD;;OAEG;IACH,UAAU,CAAC,IAAI,OAAO,CAAC;IAEvB;;OAEG;IACH,cAAc,CAAC,IAAI,OAAO,CAAC;CAC5B;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC"}
@@ -3659,6 +3659,136 @@ class PayloadStreamReader {
3659
3659
  }
3660
3660
  }
3661
3661
 
3662
+ /**
3663
+ * Wraps the raw headers in the fetch `Headers` shape. Lookups are case-insensitive.
3664
+ * Multi-value headers are joined with ", ", names that differ only by case merge into one
3665
+ * joined value, and names without a value are skipped.
3666
+ */
3667
+ function wrapRawHeaders(headers) {
3668
+ const normalized = new Map();
3669
+ Object.keys(headers).forEach((key) => {
3670
+ const val = headers[key];
3671
+ if (val !== undefined && val !== null) {
3672
+ const name = key.toLowerCase();
3673
+ const joined = Array.isArray(val) ? val.join(', ') : val;
3674
+ const existing = normalized.get(name);
3675
+ normalized.set(name, existing === undefined ? joined : `${existing}, ${joined}`);
3676
+ }
3677
+ });
3678
+ return {
3679
+ get: (name) => normalized.get(name.toLowerCase()) ?? null,
3680
+ keys: () => normalized.keys(),
3681
+ values: () => normalized.values(),
3682
+ entries: () => normalized.entries(),
3683
+ forEach: (callback) => {
3684
+ normalized.forEach((value, key) => callback(value, key));
3685
+ },
3686
+ has: (name) => normalized.has(name.toLowerCase()),
3687
+ };
3688
+ }
3689
+ /**
3690
+ * Creates a response for a streaming request. The response does not buffer the body. Each
3691
+ * read hands out the next chunk from the source iterator. The body does not enforce a
3692
+ * consumer lock: every `getReader`, `text`, and `json` call reads from the same iterator,
3693
+ * so the caller uses one consumer per response. The `cancel` methods on the body and the
3694
+ * reader call every release function the source provides: they destroy the source when it
3695
+ * has a `destroy` method, and they also call the iterator's `return` method when one
3696
+ * exists. The body passes through as-is, so the caller decodes any content encoding first.
3697
+ *
3698
+ * @param source The raw platform response, for example a Node `IncomingMessage`. A missing
3699
+ * status code maps to 0, and a missing status message maps to an empty string.
3700
+ */
3701
+ function createStreamingResponse(source) {
3702
+ // A consumer compiled without `Symbol.asyncIterator` in its lib can pass a source that
3703
+ // lacks the iterator without a compile error. The check turns that mistake into a named
3704
+ // failure instead of a bare member-access TypeError.
3705
+ if (typeof source[Symbol.asyncIterator] !== 'function') {
3706
+ throw new TypeError('the streaming response source does not provide an async iterator');
3707
+ }
3708
+ const iterator = source[Symbol.asyncIterator]();
3709
+ const cancel = async () => {
3710
+ // Destroy the stream first. That rejects a pending read and unblocks a queued return
3711
+ // call. A Node readable iterator queues the return call behind a pending read, and on
3712
+ // a stream that was never read it completes without a destroy, so the return call
3713
+ // alone cannot release the connection. Do not await it for the same reason. Each
3714
+ // release call is guarded on its own, so a release function that throws, or a return
3715
+ // that resolves synchronously, does not skip the other release path or reject cancel.
3716
+ try {
3717
+ source.destroy?.();
3718
+ }
3719
+ catch {
3720
+ // A throwing destroy has nothing left to release.
3721
+ }
3722
+ try {
3723
+ Promise.resolve(iterator.return?.()).catch(() => { });
3724
+ }
3725
+ catch {
3726
+ // A throwing return has nothing left to release.
3727
+ }
3728
+ };
3729
+ async function text() {
3730
+ const decoder = new TextDecoder();
3731
+ let result = '';
3732
+ try {
3733
+ let next = await iterator.next();
3734
+ while (!next.done) {
3735
+ if (next.value !== undefined) {
3736
+ try {
3737
+ result += decoder.decode(next.value, { stream: true });
3738
+ }
3739
+ catch {
3740
+ // decode() rejects a chunk that is not a BufferSource. The fixed message keeps
3741
+ // chunk contents out of error messages.
3742
+ throw new TypeError('the source supplied a chunk that is not a BufferSource');
3743
+ }
3744
+ }
3745
+ // The stream provides the chunks in sequence, so the reads cannot run in parallel.
3746
+ // oxlint-disable-next-line no-await-in-loop
3747
+ next = await iterator.next();
3748
+ }
3749
+ }
3750
+ catch (err) {
3751
+ // text() owns the body, so any failed read must release the source itself. This
3752
+ // covers a rejecting next() as well as a bad chunk.
3753
+ cancel();
3754
+ throw err;
3755
+ }
3756
+ // The final decode flushes a trailing partial multi-byte sequence.
3757
+ return result + decoder.decode();
3758
+ }
3759
+ return {
3760
+ headers: wrapRawHeaders(source.headers),
3761
+ status: source.statusCode ?? 0,
3762
+ statusText: source.statusMessage ?? '',
3763
+ body: {
3764
+ getReader: () => ({
3765
+ read: async () => {
3766
+ const next = await iterator.next();
3767
+ if (next.done) {
3768
+ return { done: true };
3769
+ }
3770
+ return { done: false, value: next.value };
3771
+ },
3772
+ cancel,
3773
+ }),
3774
+ cancel,
3775
+ },
3776
+ text,
3777
+ json: async () => JSON.parse(await text()),
3778
+ };
3779
+ }
3780
+
3781
+ /**
3782
+ * Converts the request options into the init shape a platform's native `fetch` accepts. A
3783
+ * platform can declare its `AbortSignal` as a lexical class, which `AbortSignalLike`
3784
+ * cannot resolve to, so the `signal` member widens here. The caller must pass a signal
3785
+ * created by the running platform. The widening then only restores the type the runtime
3786
+ * already has.
3787
+ */
3788
+ function toRequestInit(options) {
3789
+ return options;
3790
+ }
3791
+
3662
3792
  /**
3663
3793
  * Creates an InitMetadata object from initialization headers.
3664
3794
  *
@@ -3734,6 +3864,7 @@ var index = /*#__PURE__*/Object.freeze({
3734
3864
  PayloadStreamReader: PayloadStreamReader,
3735
3865
  canonicalize: canonicalize,
3736
3866
  createProtocolHandler: createProtocolHandler,
3867
+ createStreamingResponse: createStreamingResponse,
3737
3868
  initMetadataFromHeaders: initMetadataFromHeaders,
3738
3869
  isLegacyUser: isLegacyUser,
3739
3870
  isMultiKind: isMultiKind,
@@ -3743,7 +3874,8 @@ var index = /*#__PURE__*/Object.freeze({
3743
3874
  safeGetHooks: safeGetHooks,
3744
3875
  safeGetName: safeGetName,
3745
3876
  safeRegisterPlugins: safeRegisterPlugins,
3746
- shouldSample: shouldSample
3877
+ shouldSample: shouldSample,
3878
+ toRequestInit: toRequestInit
3747
3879
  });
3748
3880
 
3749
3881
  export { ApplicationTags, AttributeReference, AutoEnvAttributes, BasicLogger, ClientContext, CompositeDataSource, Context, ContextFilter, DataSourceErrorKind, DateValidator, DefaultBackoff, FactoryOrInstance, Function, KindValidator, LDClientError, LDFileDataSourceError, LDFlagDeliveryFallbackError, LDPollingError, LDStreamingError, LDTimeoutError, LDUnexpectedResponseError, NullableBoolean, NumberWithMinimum, OneOf, OptionMessages, SafeLogger, ServiceEndpoints, StringMatchingRegex, Type, TypeArray, TypeValidators, base64UrlEncode, cancelableTimedPromise, classifyHttpStatus, classifyTransportFailure, clone, createAfterConsecutiveSuccesses, createAfterHealthyFor, createRetryState, createSafeLogger, debounce, deepCompact, defaultHeaders, fastDeepEqual, forPolling, forStreaming, getEventsUri, getPollingUri, getStreamingUri, httpErrorMessage, index as internal, isHttpLocallyRecoverable, isHttpRecoverable, isNullish, monotonicNow, noop, secondsToMillis, shouldRetry, sleep, index$1 as subsystem, timedPromise };