Skip to content

Commit e535ea9

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

5 files changed

Lines changed: 15 additions & 18 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@
3636
"coverage": "yarn node --experimental-vm-modules ../../node_modules/jest/bin/jest.js --coverage",
3737
"lint": "eslint --ext .ts . && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -c",
3838
"lint:fix": "set TIMING=1 && eslint --ext .ts . --fix --quiet && prettier . --config ../../.prettierrc --ignore-path ../../.prettierignore -w --log-level error",
39-
"check:dependencies": "depcheck .",
39+
"check:dependencies": "depcheck . --ignores=ohash",
4040
"readme": "ts-node ../../scripts/replace-common-readme.ts"
4141
},
4242
"dependencies": {
4343
"@sap-cloud-sdk/connectivity": "^4.6.0",
4444
"@sap-cloud-sdk/resilience": "^4.6.0",
4545
"@sap-cloud-sdk/util": "^4.6.0",
46-
"axios": "^1.15.0"
46+
"axios": "^1.15.0",
47+
"ohash": "^1.1.6"
4748
},
4849
"devDependencies": {
4950
"https-proxy-agent": "^7.0.6",

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)