Skip to content

Commit fdbad2c

Browse files
committed
chore: Hash cache-key to shorten it
1 parent 68fd5d8 commit fdbad2c

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

packages/connectivity/src/http-agent/http-agent.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { readFile } from 'fs/promises';
2-
import http from 'http';
3-
import https from 'https';
1+
import { readFile } from 'node:fs/promises';
2+
import http from 'node:http';
3+
import https from 'node:https';
44
import * as jks from 'jks-js';
55
import { createLogger, last } from '@sap-cloud-sdk/util';
66
/* Careful the proxy imports cause circular dependencies if imported from scp directly */
77
// eslint-disable-next-line import/no-internal-modules
88
import { getProtocolOrDefault } from '../scp-cf/get-protocol';
99
// eslint-disable-next-line import/no-internal-modules
10-
import { Cache } from '../scp-cf/cache';
10+
import { Cache, hashCacheKey } from '../scp-cf/cache';
1111
import {
1212
addProxyConfigurationInternet,
1313
getProxyConfig,
@@ -307,7 +307,7 @@ function createAgent(
307307
options: https.AgentOptions
308308
): HttpAgentConfig | HttpsAgentConfig {
309309
const protocol = getProtocolOrDefault(destination);
310-
const cacheKey = JSON.stringify({ protocol, options });
310+
const cacheKey = hashCacheKey({ protocol, options });
311311

312312
return agentCreateCache.getOrInsertComputed(cacheKey, () => {
313313
logger.debug(

packages/connectivity/src/scp-cf/cache.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createHash } from 'node:crypto';
2+
13
interface CacheInterface<T> {
24
hasKey(key: string): boolean;
35
get(key: string | undefined): T | undefined;
@@ -147,9 +149,20 @@ export class Cache<T> implements CacheInterface<T> {
147149
}
148150
}
149151

152+
/**
153+
* Hashes the given value to create a cache key.
154+
* @internal
155+
* @param value - The value to hash.
156+
* @returns A hash of the given value using a cryptographic hash function.
157+
*/
158+
export function hashCacheKey(value: Record<string, unknown>): string {
159+
const serialized = JSON.stringify(value);
160+
return createHash('blake2s256').update(serialized).digest('hex');
161+
}
162+
150163
function isExpired<T>(item: CacheEntry<T>): boolean {
151164
if (item.expires === undefined) {
152165
return false;
153166
}
154167
return item.expires < Date.now();
155-
}
168+
}

0 commit comments

Comments
 (0)