@valkey/valkey-glide 2.2.7 → 2.3.0-rc6

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.
@@ -10,7 +10,7 @@ const _1 = require(".");
10
10
  * If you need to change configuration, restart the process with new settings.
11
11
  * ### OpenTelemetry
12
12
  *
13
- * - **openTelemetryConfig**: Use this object to configure OpenTelemetry exporters and options.
13
+ * - **openTelemetryConfig**: Use {@link GlideOpenTelemetryConfig} to configure OpenTelemetry exporters and options.
14
14
  * - **traces**: (optional) Configure trace exporting.
15
15
  * - **endpoint**: The collector endpoint for traces. Supported protocols:
16
16
  * - `http://` or `https://` for HTTP/HTTPS
@@ -22,6 +22,7 @@ const _1 = require(".");
22
22
  * - **metrics**: (optional) Configure metrics exporting.
23
23
  * - **endpoint**: The collector endpoint for metrics. Same protocol rules as above.
24
24
  * - **flushIntervalMs**: (optional) Interval in milliseconds for flushing data to the collector. Must be a positive integer. Defaults to 5000ms if not specified.
25
+ * - **parentSpanContextProvider**: (optional) Callback returning the active parent span context. See {@link GlideOpenTelemetryConfig.parentSpanContextProvider}.
25
26
  *
26
27
  * #### File Exporter Details
27
28
  * - For `file://` endpoints:
@@ -40,6 +41,9 @@ const _1 = require(".");
40
41
  class OpenTelemetry {
41
42
  static _instance = null;
42
43
  static openTelemetryConfig = null;
44
+ static spanContextFn = null;
45
+ static TRACE_ID_REGEX = /^[0-9a-f]{32}$/;
46
+ static SPAN_ID_REGEX = /^[0-9a-f]{16}$/;
43
47
  /**
44
48
  * Singleton class for managing OpenTelemetry configuration and operations.
45
49
  * This class provides a centralized way to initialize OpenTelemetry and control
@@ -47,23 +51,26 @@ class OpenTelemetry {
47
51
  *
48
52
  * Example usage:
49
53
  * ```typescript
50
- * import { OpenTelemetry, OpenTelemetryConfig, OpenTelemetryTracesConfig, OpenTelemetryMetricsConfig } from "@valkey/valkey-glide";
54
+ * import { OpenTelemetry, GlideOpenTelemetryConfig } from "@valkey/valkey-glide";
55
+ * import { trace } from "@opentelemetry/api";
51
56
  *
52
- * let tracesConfig: OpenTelemetryTracesConfig = {
53
- * endpoint: "http://localhost:4318/v1/traces",
54
- * samplePercentage: 10, // Optional, defaults to 1. Can also be changed at runtime via setSamplePercentage().
55
- * };
56
- * let metricsConfig: OpenTelemetryMetricsConfig = {
57
- * endpoint: "http://localhost:4318/v1/metrics",
58
- * };
59
-
60
- * let config : OpenTelemetryConfig = {
61
- * traces: tracesConfig,
62
- * metrics: metricsConfig,
63
- * flushIntervalMs: 1000, // Optional, defaults to 5000
57
+ * const config: GlideOpenTelemetryConfig = {
58
+ * traces: {
59
+ * endpoint: "http://localhost:4318/v1/traces",
60
+ * samplePercentage: 10,
61
+ * },
62
+ * metrics: {
63
+ * endpoint: "http://localhost:4318/v1/metrics",
64
+ * },
65
+ * flushIntervalMs: 1000,
66
+ * parentSpanContextProvider: () => {
67
+ * const span = trace.getActiveSpan();
68
+ * if (!span) return undefined;
69
+ * const ctx = span.spanContext();
70
+ * return { traceId: ctx.traceId, spanId: ctx.spanId, traceFlags: ctx.traceFlags };
71
+ * },
64
72
  * };
65
73
  * OpenTelemetry.init(config);
66
- *
67
74
  * ```
68
75
  *
69
76
  * @remarks
@@ -76,16 +83,23 @@ class OpenTelemetry {
76
83
  */
77
84
  static init(openTelemetryConfig) {
78
85
  if (!this._instance) {
79
- this.internalInit(openTelemetryConfig);
86
+ const { parentSpanContextProvider, ...nativeConfig } = openTelemetryConfig;
87
+ this.internalInit(nativeConfig, parentSpanContextProvider);
80
88
  _1.Logger.log("info", "GlideOpenTelemetry", "OpenTelemetry initialized with config: " +
81
- JSON.stringify(openTelemetryConfig));
89
+ JSON.stringify(nativeConfig) +
90
+ (parentSpanContextProvider
91
+ ? " (parentSpanContextProvider: set)"
92
+ : ""));
82
93
  return;
83
94
  }
84
95
  _1.Logger.log("warn", "GlideOpenTelemetry", "OpenTelemetry already initialized - ignoring new configuration");
85
96
  }
86
- static internalInit(openTelemetryConfig) {
87
- this.openTelemetryConfig = openTelemetryConfig;
88
- (0, _1.InitOpenTelemetry)(openTelemetryConfig);
97
+ static internalInit(nativeConfig, parentSpanContextProvider) {
98
+ this.openTelemetryConfig = nativeConfig;
99
+ if (parentSpanContextProvider) {
100
+ this.spanContextFn = parentSpanContextProvider;
101
+ }
102
+ (0, _1.InitOpenTelemetry)(nativeConfig);
89
103
  this._instance = new OpenTelemetry();
90
104
  }
91
105
  /**
@@ -130,5 +144,143 @@ class OpenTelemetry {
130
144
  }
131
145
  this.openTelemetryConfig.traces.samplePercentage = percentage;
132
146
  }
147
+ /**
148
+ * Register or replace the callback that returns the active parent span context.
149
+ *
150
+ * This allows changing the provider at runtime (e.g., switching tracing contexts
151
+ * in a multi-tenant application). The initial provider can also be set via
152
+ * {@link GlideOpenTelemetryConfig.parentSpanContextProvider} in `init()`.
153
+ *
154
+ * @param fn - A function returning a `GlideSpanContext` or `undefined`, or `null` to clear.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * import { trace } from "@opentelemetry/api";
159
+ *
160
+ * OpenTelemetry.setParentSpanContextProvider(() => {
161
+ * const span = trace.getActiveSpan();
162
+ * if (!span) return undefined;
163
+ * const ctx = span.spanContext();
164
+ * return {
165
+ * traceId: ctx.traceId,
166
+ * spanId: ctx.spanId,
167
+ * traceFlags: ctx.traceFlags,
168
+ * traceState: ctx.traceState?.toString(),
169
+ * };
170
+ * });
171
+ * ```
172
+ */
173
+ static setParentSpanContextProvider(fn) {
174
+ this.spanContextFn = fn;
175
+ }
176
+ /**
177
+ * Retrieve the current parent span context by invoking the registered callback.
178
+ *
179
+ * @returns The `GlideSpanContext` from the registered callback, or `undefined` if no callback
180
+ * is set or the callback returns `undefined`.
181
+ * @internal
182
+ */
183
+ static getParentSpanContext() {
184
+ let ctx;
185
+ try {
186
+ ctx = this.spanContextFn?.();
187
+ }
188
+ catch (e) {
189
+ _1.Logger.log("warn", "GlideOpenTelemetry", `parentSpanContextProvider threw: ${e}. Falling back to standalone span.`);
190
+ return undefined;
191
+ }
192
+ if (ctx === undefined) {
193
+ return undefined;
194
+ }
195
+ if (!this.TRACE_ID_REGEX.test(ctx.traceId)) {
196
+ _1.Logger.log("warn", "GlideOpenTelemetry", `Invalid traceId "${ctx.traceId}" — expected 32 lowercase hex chars. Falling back to standalone span.`);
197
+ return undefined;
198
+ }
199
+ if (!this.SPAN_ID_REGEX.test(ctx.spanId)) {
200
+ _1.Logger.log("warn", "GlideOpenTelemetry", `Invalid spanId "${ctx.spanId}" — expected 16 lowercase hex chars. Falling back to standalone span.`);
201
+ return undefined;
202
+ }
203
+ if (!Number.isInteger(ctx.traceFlags) ||
204
+ ctx.traceFlags < 0 ||
205
+ ctx.traceFlags > 255) {
206
+ _1.Logger.log("warn", "GlideOpenTelemetry", `Invalid traceFlags "${ctx.traceFlags}" — expected integer 0-255. Falling back to standalone span.`);
207
+ return undefined;
208
+ }
209
+ if (ctx.traceState !== undefined &&
210
+ !OpenTelemetry.validTraceState(ctx.traceState)) {
211
+ _1.Logger.log("warn", "GlideOpenTelemetry", `Invalid traceState "${ctx.traceState}" — expected W3C tracestate format. Falling back to standalone span.`);
212
+ return undefined;
213
+ }
214
+ return ctx;
215
+ }
216
+ /**
217
+ * Validate a W3C tracestate key.
218
+ * See https://www.w3.org/TR/trace-context/#key
219
+ * @internal
220
+ */
221
+ static validTraceStateKey(key) {
222
+ if (key.length === 0 || key.length > 256)
223
+ return false;
224
+ const ALLOWED_SPECIAL = new Set([
225
+ "_".charCodeAt(0),
226
+ "-".charCodeAt(0),
227
+ "*".charCodeAt(0),
228
+ "/".charCodeAt(0),
229
+ ]);
230
+ let vendorStart = null;
231
+ for (let i = 0; i < key.length; i++) {
232
+ const c = key.charCodeAt(i);
233
+ const isLower = c >= 0x61 && c <= 0x7a; // a-z
234
+ const isDigit = c >= 0x30 && c <= 0x39; // 0-9
235
+ const isAt = c === 0x40; // @
236
+ if (!(isLower || isDigit || ALLOWED_SPECIAL.has(c) || isAt)) {
237
+ return false;
238
+ }
239
+ if (i === 0 && !isLower && !isDigit)
240
+ return false;
241
+ if (isAt) {
242
+ if (vendorStart !== null || i + 14 < key.length)
243
+ return false;
244
+ vendorStart = i;
245
+ }
246
+ else if (vendorStart !== null && i === vendorStart + 1) {
247
+ if (!isLower && !isDigit)
248
+ return false;
249
+ }
250
+ }
251
+ return true;
252
+ }
253
+ /**
254
+ * Validate a W3C tracestate value.
255
+ * See https://www.w3.org/TR/trace-context/#value
256
+ * @internal
257
+ */
258
+ static validTraceStateValue(value) {
259
+ if (value.length > 256)
260
+ return false;
261
+ return !value.includes(",") && !value.includes("=");
262
+ }
263
+ /**
264
+ * Validate a W3C tracestate string (comma-separated key=value pairs).
265
+ * Mirrors the validation in opentelemetry-rust TraceState::from_str.
266
+ * @internal
267
+ */
268
+ static validTraceState(traceState) {
269
+ if (traceState === "")
270
+ return true;
271
+ const entries = traceState.split(",");
272
+ for (const entry of entries) {
273
+ const eqIndex = entry.indexOf("=");
274
+ if (eqIndex === -1)
275
+ return false;
276
+ const key = entry.substring(0, eqIndex);
277
+ const value = entry.substring(eqIndex + 1);
278
+ if (!OpenTelemetry.validTraceStateKey(key) ||
279
+ !OpenTelemetry.validTraceStateValue(value)) {
280
+ return false;
281
+ }
282
+ }
283
+ return true;
284
+ }
133
285
  }
134
286
  exports.OpenTelemetry = OpenTelemetry;
@@ -504,6 +504,13 @@ export namespace command_request {
504
504
  Subscribe = 911,
505
505
  SUnsubscribe = 912,
506
506
  Unsubscribe = 913,
507
+ SubscribeBlocking = 914,
508
+ UnsubscribeBlocking = 915,
509
+ PSubscribeBlocking = 916,
510
+ PUnsubscribeBlocking = 917,
511
+ SSubscribeBlocking = 918,
512
+ SUnsubscribeBlocking = 919,
513
+ GetSubscriptions = 920,
507
514
  Eval = 1001,
508
515
  EvalReadOnly = 1002,
509
516
  EvalSha = 1003,
@@ -533,7 +540,7 @@ export namespace command_request {
533
540
  AclLoad = 1107,
534
541
  AclLog = 1108,
535
542
  AclSave = 1109,
536
- AclSetSser = 1110,
543
+ AclSetUser = 1110,
537
544
  AclUsers = 1111,
538
545
  AclWhoami = 1112,
539
546
  BgRewriteAof = 1113,
@@ -1911,6 +1918,99 @@ export namespace connection_request {
1911
1918
  Sharded = 2
1912
1919
  }
1913
1920
 
1921
+ /** CompressionBackend enum. */
1922
+ enum CompressionBackend {
1923
+ ZSTD = 0,
1924
+ LZ4 = 1
1925
+ }
1926
+
1927
+ /** Properties of a CompressionConfig. */
1928
+ interface ICompressionConfig {
1929
+
1930
+ /** CompressionConfig enabled */
1931
+ enabled?: (boolean|null);
1932
+
1933
+ /** CompressionConfig backend */
1934
+ backend?: (connection_request.CompressionBackend|null);
1935
+
1936
+ /** CompressionConfig compressionLevel */
1937
+ compressionLevel?: (number|null);
1938
+
1939
+ /** CompressionConfig minCompressionSize */
1940
+ minCompressionSize?: (number|null);
1941
+ }
1942
+
1943
+ /** Represents a CompressionConfig. */
1944
+ class CompressionConfig implements ICompressionConfig {
1945
+
1946
+ /**
1947
+ * Constructs a new CompressionConfig.
1948
+ * @param [properties] Properties to set
1949
+ */
1950
+ constructor(properties?: connection_request.ICompressionConfig);
1951
+
1952
+ /** CompressionConfig enabled. */
1953
+ public enabled: boolean;
1954
+
1955
+ /** CompressionConfig backend. */
1956
+ public backend: connection_request.CompressionBackend;
1957
+
1958
+ /** CompressionConfig compressionLevel. */
1959
+ public compressionLevel?: (number|null);
1960
+
1961
+ /** CompressionConfig minCompressionSize. */
1962
+ public minCompressionSize: number;
1963
+
1964
+ /**
1965
+ * Creates a new CompressionConfig instance using the specified properties.
1966
+ * @param [properties] Properties to set
1967
+ * @returns CompressionConfig instance
1968
+ */
1969
+ public static create(properties?: connection_request.ICompressionConfig): connection_request.CompressionConfig;
1970
+
1971
+ /**
1972
+ * Encodes the specified CompressionConfig message. Does not implicitly {@link connection_request.CompressionConfig.verify|verify} messages.
1973
+ * @param message CompressionConfig message or plain object to encode
1974
+ * @param [writer] Writer to encode to
1975
+ * @returns Writer
1976
+ */
1977
+ public static encode(message: connection_request.ICompressionConfig, writer?: $protobuf.Writer): $protobuf.Writer;
1978
+
1979
+ /**
1980
+ * Encodes the specified CompressionConfig message, length delimited. Does not implicitly {@link connection_request.CompressionConfig.verify|verify} messages.
1981
+ * @param message CompressionConfig message or plain object to encode
1982
+ * @param [writer] Writer to encode to
1983
+ * @returns Writer
1984
+ */
1985
+ public static encodeDelimited(message: connection_request.ICompressionConfig, writer?: $protobuf.Writer): $protobuf.Writer;
1986
+
1987
+ /**
1988
+ * Decodes a CompressionConfig message from the specified reader or buffer.
1989
+ * @param reader Reader or buffer to decode from
1990
+ * @param [length] Message length if known beforehand
1991
+ * @returns CompressionConfig
1992
+ * @throws {Error} If the payload is not a reader or valid buffer
1993
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
1994
+ */
1995
+ public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): connection_request.CompressionConfig;
1996
+
1997
+ /**
1998
+ * Decodes a CompressionConfig message from the specified reader or buffer, length delimited.
1999
+ * @param reader Reader or buffer to decode from
2000
+ * @returns CompressionConfig
2001
+ * @throws {Error} If the payload is not a reader or valid buffer
2002
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
2003
+ */
2004
+ public static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): connection_request.CompressionConfig;
2005
+
2006
+ /**
2007
+ * Gets the default type url for CompressionConfig
2008
+ * @param [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
2009
+ * @returns The default type url
2010
+ */
2011
+ public static getTypeUrl(typeUrlPrefix?: string): string;
2012
+ }
2013
+
1914
2014
  /** Properties of a PubSubChannelsOrPatterns. */
1915
2015
  interface IPubSubChannelsOrPatterns {
1916
2016
 
@@ -2112,8 +2212,23 @@ export namespace connection_request {
2112
2212
  /** ConnectionRequest rootCerts */
2113
2213
  rootCerts?: (Uint8Array[]|null);
2114
2214
 
2215
+ /** ConnectionRequest compressionConfig */
2216
+ compressionConfig?: (connection_request.ICompressionConfig|null);
2217
+
2218
+ /** ConnectionRequest clientCert */
2219
+ clientCert?: (Uint8Array|null);
2220
+
2221
+ /** ConnectionRequest clientKey */
2222
+ clientKey?: (Uint8Array|null);
2223
+
2115
2224
  /** ConnectionRequest tcpNodelay */
2116
2225
  tcpNodelay?: (boolean|null);
2226
+
2227
+ /** ConnectionRequest pubsubReconciliationIntervalMs */
2228
+ pubsubReconciliationIntervalMs?: (number|null);
2229
+
2230
+ /** ConnectionRequest readOnly */
2231
+ readOnly?: (boolean|null);
2117
2232
  }
2118
2233
 
2119
2234
  /** Represents a ConnectionRequest. */
@@ -2185,9 +2300,24 @@ export namespace connection_request {
2185
2300
  /** ConnectionRequest rootCerts. */
2186
2301
  public rootCerts: Uint8Array[];
2187
2302
 
2303
+ /** ConnectionRequest compressionConfig. */
2304
+ public compressionConfig?: (connection_request.ICompressionConfig|null);
2305
+
2306
+ /** ConnectionRequest clientCert. */
2307
+ public clientCert: Uint8Array;
2308
+
2309
+ /** ConnectionRequest clientKey. */
2310
+ public clientKey: Uint8Array;
2311
+
2188
2312
  /** ConnectionRequest tcpNodelay. */
2189
2313
  public tcpNodelay?: (boolean|null);
2190
2314
 
2315
+ /** ConnectionRequest pubsubReconciliationIntervalMs. */
2316
+ public pubsubReconciliationIntervalMs?: (number|null);
2317
+
2318
+ /** ConnectionRequest readOnly. */
2319
+ public readOnly?: (boolean|null);
2320
+
2191
2321
  /** ConnectionRequest periodicChecks. */
2192
2322
  public periodicChecks?: ("periodicChecksManualInterval"|"periodicChecksDisabled");
2193
2323