Skip to content

Commit cdf9b3a

Browse files
committed
Use ohash for cache key generation
1 parent 8ca6638 commit cdf9b3a

5 files changed

Lines changed: 13 additions & 16 deletions

File tree

.github/dependabot.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ updates:
3030
# yargs v18 needs ESM, see https://github.com/yargs/yargs/releases/tag/v18.0.0, we'll stay on v17 until we can use ESM
3131
- dependency-name: 'yargs'
3232
update-types: ['version-update:semver-major']
33+
# ohash v2 needs ESM, https://github.com/unjs/ohash/releases/tag/v2.0.1, we'll stay on v1 until we can use ESM
34+
- dependency-name: 'ohash'
35+
update-types: ['version-update:semver-major']
3336
- package-ecosystem: 'github-actions'
3437
directory: '/'
3538
schedule:

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { readFile } from 'node:fs/promises';
22
import http from 'node:http';
33
import https from 'node:https';
44
import * as jks from 'jks-js';
5+
// @ts-expect-error - ts fails to detect that this package provides CJS exports
6+
import * as ohash from 'ohash';
57
import { createLogger, last } from '@sap-cloud-sdk/util';
68
/* Careful the proxy imports cause circular dependencies if imported from scp directly */
79
// eslint-disable-next-line import/no-internal-modules
810
import { getProtocolOrDefault } from '../scp-cf/get-protocol';
911
// eslint-disable-next-line import/no-internal-modules
10-
import { Cache, hashCacheKey } from '../scp-cf/cache';
12+
import { Cache } from '../scp-cf/cache';
1113
import {
1214
addProxyConfigurationInternet,
1315
getProxyConfig,
@@ -307,8 +309,7 @@ function createAgent(
307309
options: https.AgentOptions
308310
): HttpAgentConfig | HttpsAgentConfig {
309311
const protocol = getProtocolOrDefault(destination);
310-
const cacheKey = hashCacheKey({ protocol, options });
311-
312+
const cacheKey = ohash.hash({ protocol, options });
312313
return agentCreateCache.getOrInsertComputed(cacheKey, () => {
313314
logger.debug(
314315
`Creating new ${protocol.toUpperCase()} agent for destination ${destination.name || '<unknown>'}`

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { createHash } from 'node:crypto';
2-
31
interface CacheInterface<T> {
42
hasKey(key: string): boolean;
53
get(key: string | undefined): T | undefined;
@@ -149,17 +147,6 @@ export class Cache<T> implements CacheInterface<T> {
149147
}
150148
}
151149

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-
163150
function isExpired<T>(item: CacheEntry<T>): boolean {
164151
if (item.expires === undefined) {
165152
return false;

packages/http-client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"devDependencies": {
4949
"https-proxy-agent": "^7.0.6",
5050
"nock": "^14.0.11",
51+
"ohash": "^1.1.6",
5152
"typescript": "~5.9.3",
5253
"jsonwebtoken": "^9.0.3"
5354
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7342,6 +7342,11 @@ object.values@^1.2.1:
73427342
define-properties "^1.2.1"
73437343
es-object-atoms "^1.0.0"
73447344

7345+
ohash@^1.1.6:
7346+
version "1.1.6"
7347+
resolved "https://registry.npmjs.org/ohash/-/ohash-1.1.6.tgz#9ff7b0271d7076290794537d68ec2b40a60d133e"
7348+
integrity sha512-TBu7PtV8YkAZn0tSxobKY2n2aAQva936lhRrj6957aDaCf9IEtqsKbgMzXE/F/sjqYOwmrukeORHNLe5glk7Cg==
7349+
73457350
[email protected], on-finished@^2.4.1, on-finished@~2.4.1:
73467351
version "2.4.1"
73477352
resolved "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz#58c8c44116e54845ad57f14ab10b03533184ac3f"

0 commit comments

Comments
 (0)