@common-stack/store-redis 8.2.5-alpha.33 → 8.2.5-alpha.35

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 (41) hide show
  1. package/lib/containers/container.d.ts +2 -0
  2. package/lib/containers/container.js +9 -0
  3. package/lib/containers/index.d.ts +1 -0
  4. package/lib/core/index.d.ts +3 -0
  5. package/lib/core/ioredis.d.ts +15 -0
  6. package/lib/core/ioredis.js +31 -0
  7. package/lib/core/keyBuilder/generate-query-cache-key.d.ts +57 -0
  8. package/lib/core/keyBuilder/generate-query-cache-key.js +88 -0
  9. package/lib/core/keyBuilder/index.d.ts +18 -0
  10. package/lib/core/keyBuilder/index.js +27 -0
  11. package/lib/core/keyBuilder/redis-key-builder.d.ts +152 -0
  12. package/lib/core/keyBuilder/redis-key-builder.js +185 -0
  13. package/lib/core/keyBuilder/sanitize-redis-key.d.ts +118 -0
  14. package/lib/core/keyBuilder/sanitize-redis-key.js +120 -0
  15. package/lib/core/upstash-redis.d.ts +14 -0
  16. package/lib/core/upstash-redis.js +27 -0
  17. package/lib/index.d.ts +2 -1
  18. package/lib/index.js +7 -3
  19. package/lib/interfaces/index.d.ts +1 -6
  20. package/lib/interfaces/redis.d.ts +11 -0
  21. package/lib/module.d.ts +2 -0
  22. package/lib/module.js +9 -0
  23. package/lib/services/RedisCacheManager.d.ts +77 -0
  24. package/lib/services/RedisCacheManager.js +185 -0
  25. package/lib/services/index.d.ts +1 -5
  26. package/lib/templates/constants/SERVER_TYPES.ts.template +0 -1
  27. package/lib/templates/repositories/IRedisKeyBuilder.ts.template +4 -4
  28. package/lib/templates/repositories/redisCommonTypes.ts.template +2 -163
  29. package/lib/templates/{repositories/IRedisCacheManager.ts.template → services/RedisCacheManager.ts.template} +7 -7
  30. package/package.json +8 -7
  31. package/lib/interfaces/cache-manager.d.ts +0 -28
  32. package/lib/interfaces/redis-key-options.d.ts +0 -35
  33. package/lib/interfaces/redis-key-options.js +0 -17
  34. package/lib/interfaces/storage-backend.d.ts +0 -17
  35. package/lib/templates/repositories/IRedisService.ts.template +0 -236
  36. package/lib/templates/repositories/IRedisStorageBackend.ts.template +0 -229
  37. package/lib/utils/index.d.ts +0 -5
  38. package/lib/utils/redis-key-builder.d.ts +0 -32
  39. package/lib/utils/redis-key-builder.js +0 -68
  40. package/lib/utils/redis-key-sanitizer.d.ts +0 -30
  41. package/lib/utils/redis-key-sanitizer.js +0 -54
@@ -0,0 +1,2 @@
1
+ import { interfaces } from 'inversify';
2
+ export declare const infraContainer: (settings: any) => interfaces.ContainerModule;
@@ -0,0 +1,9 @@
1
+ import { ContainerModule } from 'inversify';
2
+ import { SERVER_TYPES } from 'common/server';
3
+ import { RedisCacheManager } from '../services/RedisCacheManager.js';
4
+
5
+ const infraContainer = (settings) => new ContainerModule((bind) => {
6
+ bind(SERVER_TYPES.RedisCacheManager).to(RedisCacheManager).inSingletonScope();
7
+ });
8
+
9
+ export { infraContainer };
@@ -0,0 +1 @@
1
+ export * from './container';
@@ -0,0 +1,3 @@
1
+ export * from './ioredis';
2
+ export * from './upstash-redis';
3
+ export * from './keyBuilder';
@@ -0,0 +1,15 @@
1
+ import Redis from 'ioredis';
2
+ import { IRedisClient } from 'common/server';
3
+ export declare class IORedisClient implements IRedisClient {
4
+ private redis;
5
+ constructor(config: {
6
+ url: string;
7
+ });
8
+ get(key: string): Promise<any | null>;
9
+ set(key: string, value: string, options?: {
10
+ ex?: number;
11
+ }): Promise<string>;
12
+ delMulti(keys: string[]): Promise<any>;
13
+ del(key: string): Promise<number>;
14
+ on(event: string, cb: (...args: any[]) => void): Redis;
15
+ }
@@ -0,0 +1,31 @@
1
+ import Redis from 'ioredis';
2
+
3
+ class IORedisClient {
4
+ redis;
5
+ constructor(config) {
6
+ this.redis = new Redis(config.url);
7
+ }
8
+ async get(key) {
9
+ const data = await this.redis.get(key);
10
+ return data;
11
+ }
12
+ async set(key, value, options) {
13
+ if (options?.ex) {
14
+ return await this.redis.set(key, value, 'EX', options?.ex);
15
+ }
16
+ else {
17
+ return await this.redis.set(key, value);
18
+ }
19
+ }
20
+ async delMulti(keys) {
21
+ return await this.redis.del(keys);
22
+ }
23
+ async del(key) {
24
+ return await this.redis.del(key);
25
+ }
26
+ on(event, cb) {
27
+ return this.redis.on(event, cb);
28
+ }
29
+ }
30
+
31
+ export { IORedisClient };
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @file generate-query-cache-key.ts
3
+ * @description Generates consistent cache keys for GraphQL queries
4
+ *
5
+ * This utility creates deterministic cache keys by hashing GraphQL queries and variables,
6
+ * with proper sanitization for Redis key compatibility (e.g., Auth0 userIds with pipes).
7
+ *
8
+ * Key format: queryName:queryHash[:variablesHash]
9
+ * - Query is hashed using SHA-256 for consistent, compact keys
10
+ * - Variables are optionally hashed if provided
11
+ * - All components are sanitized for Redis key safety
12
+ */
13
+ import { DocumentNode } from 'graphql/language/index.js';
14
+ export type GenerateQueryCacheKeyOptions = {
15
+ query: string | DocumentNode;
16
+ variables: Record<string, unknown>;
17
+ userId?: string;
18
+ tenantId?: string;
19
+ appName?: string;
20
+ logger?: any;
21
+ };
22
+ /**
23
+ * Generates a cache key for GraphQL queries using standardized Redis key builder
24
+ *
25
+ * Key format: `[appName]:[tenantId]:[userId]:queryName:queryHash[:variablesHash]`
26
+ * or legacy format: `[tenantId]:[userId]:queryName:queryHash[:variablesHash]`
27
+ * - Query and variables are hashed for consistent, compact keys
28
+ * - Uses buildRedisKey for proper sanitization and validation
29
+ * - Optional components are omitted if not provided
30
+ *
31
+ * @param options - Cache key generation options
32
+ * @returns Generated cache key string
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * // With appName (recommended)
37
+ * generateQueryCacheKey({
38
+ * query: GetUserDocument,
39
+ * variables: { userId: '123' },
40
+ * userId: 'auth0|123',
41
+ * tenantId: 'default',
42
+ * appName: 'MY_APP',
43
+ * logger: myLogger
44
+ * })
45
+ * // Returns: 'MY_APP:default:auth0-123:user:hash1:hash2'
46
+ *
47
+ * // Legacy format without appName (for backward compatibility)
48
+ * generateQueryCacheKey({
49
+ * query: GetUserDocument,
50
+ * variables: { userId: '123' },
51
+ * userId: 'auth0|123',
52
+ * tenantId: 'default',
53
+ * })
54
+ * // Returns: 'default:auth0-123:user:hash1:hash2'
55
+ * ```
56
+ */
57
+ export declare const generateQueryCacheKey: ({ query, variables, logger, userId, tenantId, appName, }: GenerateQueryCacheKeyOptions) => string;
@@ -0,0 +1,88 @@
1
+ import { createHash } from 'crypto';
2
+ import { print } from 'graphql/language/index.js';
3
+ import { buildRedisKey } from './sanitize-redis-key.js';
4
+
5
+ /**
6
+ * @file generate-query-cache-key.ts
7
+ * @description Generates consistent cache keys for GraphQL queries
8
+ *
9
+ * This utility creates deterministic cache keys by hashing GraphQL queries and variables,
10
+ * with proper sanitization for Redis key compatibility (e.g., Auth0 userIds with pipes).
11
+ *
12
+ * Key format: queryName:queryHash[:variablesHash]
13
+ * - Query is hashed using SHA-256 for consistent, compact keys
14
+ * - Variables are optionally hashed if provided
15
+ * - All components are sanitized for Redis key safety
16
+ */
17
+ /**
18
+ * Generates a cache key for GraphQL queries using standardized Redis key builder
19
+ *
20
+ * Key format: `[appName]:[tenantId]:[userId]:queryName:queryHash[:variablesHash]`
21
+ * or legacy format: `[tenantId]:[userId]:queryName:queryHash[:variablesHash]`
22
+ * - Query and variables are hashed for consistent, compact keys
23
+ * - Uses buildRedisKey for proper sanitization and validation
24
+ * - Optional components are omitted if not provided
25
+ *
26
+ * @param options - Cache key generation options
27
+ * @returns Generated cache key string
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * // With appName (recommended)
32
+ * generateQueryCacheKey({
33
+ * query: GetUserDocument,
34
+ * variables: { userId: '123' },
35
+ * userId: 'auth0|123',
36
+ * tenantId: 'default',
37
+ * appName: 'MY_APP',
38
+ * logger: myLogger
39
+ * })
40
+ * // Returns: 'MY_APP:default:auth0-123:user:hash1:hash2'
41
+ *
42
+ * // Legacy format without appName (for backward compatibility)
43
+ * generateQueryCacheKey({
44
+ * query: GetUserDocument,
45
+ * variables: { userId: '123' },
46
+ * userId: 'auth0|123',
47
+ * tenantId: 'default',
48
+ * })
49
+ * // Returns: 'default:auth0-123:user:hash1:hash2'
50
+ * ```
51
+ */
52
+ const generateQueryCacheKey = ({ query, variables, logger, userId, tenantId, appName, }) => {
53
+ const normalizedQuery = (typeof query === 'string' ? query : print(query)).trim();
54
+ const [, queryName] = normalizedQuery?.match(/{\s*(\w+)/) ?? [];
55
+ // Log the data right before hashing
56
+ if (logger && typeof logger.trace === 'function') {
57
+ logger.trace('About to hash normalized query: [%s]', normalizedQuery);
58
+ logger.trace('About to hash variables: [%j]', variables);
59
+ }
60
+ const queryHash = createHash('sha256').update(normalizedQuery).digest('hex');
61
+ const variablesHash = variables ? createHash('sha256').update(JSON.stringify(variables)).digest('hex') : null;
62
+ // Build segments: queryName, queryHash, and optional variablesHash
63
+ const segments = [queryName, queryHash];
64
+ if (variablesHash) {
65
+ segments.push(variablesHash);
66
+ }
67
+ // If appName is provided, use the full standardized format
68
+ if (appName) {
69
+ return buildRedisKey({
70
+ appName,
71
+ tenantId,
72
+ userId,
73
+ segments,
74
+ });
75
+ }
76
+ // Legacy format for backward compatibility (no appName)
77
+ // Still uses buildRedisKey for sanitization, but strips the appName prefix
78
+ const fullKey = buildRedisKey({
79
+ appName: '__temp__',
80
+ tenantId,
81
+ userId,
82
+ segments,
83
+ });
84
+ // Remove the temporary appName prefix: __temp__:
85
+ return fullKey.substring('__temp__:'.length);
86
+ };
87
+
88
+ export { generateQueryCacheKey };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Redis Utilities
3
+ *
4
+ * Utility functions for Redis key building, sanitization, and cache key generation.
5
+ * These utilities provide consistent Redis key formatting across applications.
6
+ */
7
+ export * from './sanitize-redis-key';
8
+ export * from './redis-key-builder';
9
+ export * from './generate-query-cache-key';
10
+ import { type RedisKeyBuilderOptions } from './redis-key-builder';
11
+ /**
12
+ * Simplified buildRedisKey that uses APP_NAME from environment
13
+ */
14
+ export declare function buildRedisKey(options: Omit<RedisKeyBuilderOptions, 'appName'>): string;
15
+ /**
16
+ * Simplified buildRedisKeyPattern that uses APP_NAME from environment
17
+ */
18
+ export declare function buildRedisKeyPattern(options: Omit<RedisKeyBuilderOptions, 'appName'>): string;
@@ -0,0 +1,27 @@
1
+ import { buildRedisKeyWithNamespace, buildRedisKeyPatternWithNamespace } from './redis-key-builder.js';
2
+ export { RedisNamespace, extractNamespaceFromRedisKey, extractTenantIdFromRedisKey, isValidRedisKeyWithNamespace, parseRedisKey } from './redis-key-builder.js';
3
+ import 'crypto';
4
+ import 'graphql/language/index.js';
5
+
6
+ /**
7
+ * Redis Utilities
8
+ *
9
+ * Utility functions for Redis key building, sanitization, and cache key generation.
10
+ * These utilities provide consistent Redis key formatting across applications.
11
+ */
12
+ /**
13
+ * Simplified buildRedisKey that uses APP_NAME from environment
14
+ */
15
+ function buildRedisKey(options) {
16
+ const appName = process.env.APP_NAME || 'APP';
17
+ return buildRedisKeyWithNamespace({ ...options, appName });
18
+ }
19
+ /**
20
+ * Simplified buildRedisKeyPattern that uses APP_NAME from environment
21
+ */
22
+ function buildRedisKeyPattern(options) {
23
+ const appName = process.env.APP_NAME || 'APP';
24
+ return buildRedisKeyPatternWithNamespace({ ...options, appName });
25
+ }
26
+
27
+ export { buildRedisKey, buildRedisKeyPattern, buildRedisKeyPatternWithNamespace, buildRedisKeyWithNamespace };
@@ -0,0 +1,152 @@
1
+ /**
2
+ * Centralized Redis Key Builder
3
+ *
4
+ * Enforces consistent Redis key formatting across the entire application.
5
+ * Standard format: <APP_NAME>:<tenantId>:<namespace>:<...segments>
6
+ *
7
+ * Key Design Principles:
8
+ * 1. All keys MUST start with APP_NAME for application isolation
9
+ * 2. All keys SHOULD include tenantId for multi-tenancy (use 'default' if no tenant context)
10
+ * 3. Keys are sanitized to remove invalid Redis characters
11
+ * 4. Use namespaces to organize keys by domain/feature
12
+ */
13
+ /**
14
+ * Options for building Redis keys with namespace support
15
+ */
16
+ export interface RedisKeyBuilderOptions {
17
+ /** Application name for key prefix (e.g., 'CDMBASE_TEST') */
18
+ appName: string;
19
+ /** Tenant ID for multi-tenancy. Use 'default' for global/system keys */
20
+ tenantId?: string;
21
+ /** Optional user ID for user-specific keys */
22
+ userId?: string;
23
+ /** Namespace for organizing keys (e.g., 'cache', 'session', 'tenant', 'config') */
24
+ namespace: string;
25
+ /** Additional key segments (e.g., ['user', userId] or ['query', queryName]) */
26
+ segments: string[];
27
+ }
28
+ /**
29
+ * Builds a standardized Redis key with namespace and consistent formatting
30
+ *
31
+ * Format: <APP_NAME>:<tenantId>:<namespace>:<segment1>:<segment2>:...
32
+ *
33
+ * @param options - Key building options
34
+ * @returns Formatted and sanitized Redis key
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * // Cache key with tenant
39
+ * buildRedisKeyWithNamespace({
40
+ * appName: 'CDMBASE_TEST',
41
+ * tenantId: 'tenant-123',
42
+ * namespace: 'cache',
43
+ * segments: ['user', userId, 'profile']
44
+ * });
45
+ * // => "CDMBASE_TEST:tenant-123:cache:user:auth0-123:profile"
46
+ *
47
+ * // Global configuration key
48
+ * buildRedisKeyWithNamespace({
49
+ * appName: 'CDMBASE_TEST',
50
+ * tenantId: 'default',
51
+ * namespace: 'config',
52
+ * segments: ['extension', 'permissions']
53
+ * });
54
+ * // => "CDMBASE_TEST:default:config:extension:permissions"
55
+ *
56
+ * // Session key without tenant
57
+ * buildRedisKeyWithNamespace({
58
+ * appName: 'CDMBASE_TEST',
59
+ * namespace: 'session',
60
+ * segments: ['abc123']
61
+ * });
62
+ * // => "CDMBASE_TEST:default:session:abc123"
63
+ * ```
64
+ */
65
+ export declare function buildRedisKeyWithNamespace(options: RedisKeyBuilderOptions): string;
66
+ /**
67
+ * Builds a wildcard pattern for Redis KEYS command with namespace support
68
+ *
69
+ * @param options - Key building options (segments can include '*' wildcards)
70
+ * @returns Redis key pattern for matching multiple keys
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * // Match all cache keys for a tenant
75
+ * buildRedisKeyPatternWithNamespace({
76
+ * appName: 'CDMBASE_TEST',
77
+ * tenantId: 'tenant-123',
78
+ * namespace: 'cache',
79
+ * segments: ['*']
80
+ * });
81
+ * // => "CDMBASE_TEST:tenant-123:cache:*"
82
+ *
83
+ * // Match all user cache keys across all tenants
84
+ * buildRedisKeyPatternWithNamespace({
85
+ * appName: 'CDMBASE_TEST',
86
+ * tenantId: '*',
87
+ * namespace: 'cache',
88
+ * segments: ['user', '*']
89
+ * });
90
+ * // => "CDMBASE_TEST:*:cache:user:*"
91
+ * ```
92
+ */
93
+ export declare function buildRedisKeyPatternWithNamespace(options: RedisKeyBuilderOptions): string;
94
+ /**
95
+ * Common namespaces used across the application
96
+ * These provide a standardized vocabulary for organizing Redis keys
97
+ */
98
+ export declare const RedisNamespace: {
99
+ /** GraphQL/API response caching */
100
+ readonly CACHE: "cache";
101
+ /** User session data */
102
+ readonly SESSION: "session";
103
+ /** Tenant information cache */
104
+ readonly TENANT: "tenant";
105
+ /** Configuration and settings */
106
+ readonly CONFIG: "config";
107
+ /** Extension/plugin data */
108
+ readonly EXTENSION: "extension";
109
+ /** Contribution points */
110
+ readonly CONTRIBUTION: "contribution";
111
+ /** Request-scoped storage */
112
+ readonly STORAGE: "storage";
113
+ /** Permission and access control */
114
+ readonly PERMISSION: "permission";
115
+ /** Temporary/TTL based data */
116
+ readonly TEMP: "temp";
117
+ };
118
+ export type RedisNamespaceType = (typeof RedisNamespace)[keyof typeof RedisNamespace];
119
+ /**
120
+ * Extracts tenant ID from a Redis key if present
121
+ *
122
+ * @param key - Redis key to parse
123
+ * @returns Tenant ID or null if not found/parseable
124
+ */
125
+ export declare function extractTenantIdFromRedisKey(key: string): string | null;
126
+ /**
127
+ * Extracts namespace from a Redis key if present
128
+ *
129
+ * @param key - Redis key to parse
130
+ * @returns Namespace or null if not found/parseable
131
+ */
132
+ export declare function extractNamespaceFromRedisKey(key: string): string | null;
133
+ /**
134
+ * Validates if a key follows the standard format with namespace
135
+ *
136
+ * @param key - Redis key to validate
137
+ * @returns True if key follows standard format
138
+ */
139
+ export declare function isValidRedisKeyWithNamespace(key: string): boolean;
140
+ /**
141
+ * Parses a Redis key into its components
142
+ *
143
+ * @param key - Redis key to parse
144
+ * @returns Parsed key components or null if invalid
145
+ */
146
+ export interface ParsedRedisKey {
147
+ appName: string;
148
+ tenantId: string;
149
+ namespace: string;
150
+ segments: string[];
151
+ }
152
+ export declare function parseRedisKey(key: string): ParsedRedisKey | null;
@@ -0,0 +1,185 @@
1
+ import { sanitizeRedisKeyComponent } from './sanitize-redis-key.js';
2
+
3
+ /**
4
+ * Centralized Redis Key Builder
5
+ *
6
+ * Enforces consistent Redis key formatting across the entire application.
7
+ * Standard format: <APP_NAME>:<tenantId>:<namespace>:<...segments>
8
+ *
9
+ * Key Design Principles:
10
+ * 1. All keys MUST start with APP_NAME for application isolation
11
+ * 2. All keys SHOULD include tenantId for multi-tenancy (use 'default' if no tenant context)
12
+ * 3. Keys are sanitized to remove invalid Redis characters
13
+ * 4. Use namespaces to organize keys by domain/feature
14
+ */
15
+ /**
16
+ * Builds a standardized Redis key with namespace and consistent formatting
17
+ *
18
+ * Format: <APP_NAME>:<tenantId>:<namespace>:<segment1>:<segment2>:...
19
+ *
20
+ * @param options - Key building options
21
+ * @returns Formatted and sanitized Redis key
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * // Cache key with tenant
26
+ * buildRedisKeyWithNamespace({
27
+ * appName: 'CDMBASE_TEST',
28
+ * tenantId: 'tenant-123',
29
+ * namespace: 'cache',
30
+ * segments: ['user', userId, 'profile']
31
+ * });
32
+ * // => "CDMBASE_TEST:tenant-123:cache:user:auth0-123:profile"
33
+ *
34
+ * // Global configuration key
35
+ * buildRedisKeyWithNamespace({
36
+ * appName: 'CDMBASE_TEST',
37
+ * tenantId: 'default',
38
+ * namespace: 'config',
39
+ * segments: ['extension', 'permissions']
40
+ * });
41
+ * // => "CDMBASE_TEST:default:config:extension:permissions"
42
+ *
43
+ * // Session key without tenant
44
+ * buildRedisKeyWithNamespace({
45
+ * appName: 'CDMBASE_TEST',
46
+ * namespace: 'session',
47
+ * segments: ['abc123']
48
+ * });
49
+ * // => "CDMBASE_TEST:default:session:abc123"
50
+ * ```
51
+ */
52
+ function buildRedisKeyWithNamespace(options) {
53
+ const { appName, tenantId = 'default', userId, namespace, segments } = options;
54
+ // Sanitize all components
55
+ const sanitizedAppName = sanitizeRedisKeyComponent(appName);
56
+ const sanitizedTenantId = sanitizeRedisKeyComponent(tenantId);
57
+ const sanitizedNamespace = sanitizeRedisKeyComponent(namespace);
58
+ const sanitizedSegments = segments.map(sanitizeRedisKeyComponent);
59
+ // Build key with consistent format, including userId if provided
60
+ const parts = [sanitizedAppName, sanitizedTenantId];
61
+ if (userId) {
62
+ parts.push(sanitizeRedisKeyComponent(userId));
63
+ }
64
+ parts.push(sanitizedNamespace, ...sanitizedSegments);
65
+ return parts.join(':');
66
+ }
67
+ /**
68
+ * Builds a wildcard pattern for Redis KEYS command with namespace support
69
+ *
70
+ * @param options - Key building options (segments can include '*' wildcards)
71
+ * @returns Redis key pattern for matching multiple keys
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * // Match all cache keys for a tenant
76
+ * buildRedisKeyPatternWithNamespace({
77
+ * appName: 'CDMBASE_TEST',
78
+ * tenantId: 'tenant-123',
79
+ * namespace: 'cache',
80
+ * segments: ['*']
81
+ * });
82
+ * // => "CDMBASE_TEST:tenant-123:cache:*"
83
+ *
84
+ * // Match all user cache keys across all tenants
85
+ * buildRedisKeyPatternWithNamespace({
86
+ * appName: 'CDMBASE_TEST',
87
+ * tenantId: '*',
88
+ * namespace: 'cache',
89
+ * segments: ['user', '*']
90
+ * });
91
+ * // => "CDMBASE_TEST:*:cache:user:*"
92
+ * ```
93
+ */
94
+ function buildRedisKeyPatternWithNamespace(options) {
95
+ // For patterns, allow '*' to pass through without sanitization
96
+ const { appName, tenantId = '*', userId, namespace, segments } = options;
97
+ const sanitizedAppName = sanitizeRedisKeyComponent(appName);
98
+ const sanitizedTenantId = tenantId === '*' ? '*' : sanitizeRedisKeyComponent(tenantId);
99
+ const sanitizedNamespace = sanitizeRedisKeyComponent(namespace);
100
+ const sanitizedSegments = segments.map((seg) => (seg === '*' ? '*' : sanitizeRedisKeyComponent(seg)));
101
+ // Build pattern with userId if provided
102
+ const parts = [sanitizedAppName, sanitizedTenantId];
103
+ if (userId !== undefined) {
104
+ parts.push(userId === '*' ? '*' : sanitizeRedisKeyComponent(userId));
105
+ }
106
+ parts.push(sanitizedNamespace, ...sanitizedSegments);
107
+ return parts.join(':');
108
+ }
109
+ /**
110
+ * Common namespaces used across the application
111
+ * These provide a standardized vocabulary for organizing Redis keys
112
+ */
113
+ const RedisNamespace = {
114
+ /** GraphQL/API response caching */
115
+ CACHE: 'cache',
116
+ /** User session data */
117
+ SESSION: 'session',
118
+ /** Tenant information cache */
119
+ TENANT: 'tenant',
120
+ /** Configuration and settings */
121
+ CONFIG: 'config',
122
+ /** Extension/plugin data */
123
+ EXTENSION: 'extension',
124
+ /** Contribution points */
125
+ CONTRIBUTION: 'contribution',
126
+ /** Request-scoped storage */
127
+ STORAGE: 'storage',
128
+ /** Permission and access control */
129
+ PERMISSION: 'permission',
130
+ /** Temporary/TTL based data */
131
+ TEMP: 'temp',
132
+ };
133
+ /**
134
+ * Extracts tenant ID from a Redis key if present
135
+ *
136
+ * @param key - Redis key to parse
137
+ * @returns Tenant ID or null if not found/parseable
138
+ */
139
+ function extractTenantIdFromRedisKey(key) {
140
+ const parts = key.split(':');
141
+ // Format: APP_NAME:tenantId:namespace:...
142
+ if (parts.length >= 3) {
143
+ return parts[1];
144
+ }
145
+ return null;
146
+ }
147
+ /**
148
+ * Extracts namespace from a Redis key if present
149
+ *
150
+ * @param key - Redis key to parse
151
+ * @returns Namespace or null if not found/parseable
152
+ */
153
+ function extractNamespaceFromRedisKey(key) {
154
+ const parts = key.split(':');
155
+ // Format: APP_NAME:tenantId:namespace:...
156
+ if (parts.length >= 3) {
157
+ return parts[2];
158
+ }
159
+ return null;
160
+ }
161
+ /**
162
+ * Validates if a key follows the standard format with namespace
163
+ *
164
+ * @param key - Redis key to validate
165
+ * @returns True if key follows standard format
166
+ */
167
+ function isValidRedisKeyWithNamespace(key) {
168
+ const parts = key.split(':');
169
+ // Minimum format: APP_NAME:tenantId:namespace
170
+ return parts.length >= 3 && parts[0].length > 0 && parts[1].length > 0 && parts[2].length > 0;
171
+ }
172
+ function parseRedisKey(key) {
173
+ const parts = key.split(':');
174
+ if (parts.length < 3) {
175
+ return null;
176
+ }
177
+ return {
178
+ appName: parts[0],
179
+ tenantId: parts[1],
180
+ namespace: parts[2],
181
+ segments: parts.slice(3),
182
+ };
183
+ }
184
+
185
+ export { RedisNamespace, buildRedisKeyPatternWithNamespace, buildRedisKeyWithNamespace, extractNamespaceFromRedisKey, extractTenantIdFromRedisKey, isValidRedisKeyWithNamespace, parseRedisKey };