@sap-cloud-sdk/connectivity 4.6.0 → 4.6.1-20260414124519.0
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.
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { Cache } from '../scp-cf/cache';
|
|
3
|
+
import type { HttpDestination } from '../scp-cf/destination';
|
|
4
|
+
import type { BasicProxyConfiguration } from '../scp-cf/connectivity-service-types';
|
|
3
5
|
import type { HttpAgentConfig, HttpsAgentConfig } from './agent-config';
|
|
4
6
|
/**
|
|
5
7
|
* Returns a promise of the http or https-agent config depending on the destination URL.
|
|
@@ -23,6 +25,12 @@ export interface MtlsOptions {
|
|
|
23
25
|
*/
|
|
24
26
|
key: string;
|
|
25
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Cache for http(s) agents.
|
|
30
|
+
* Exported for testing purposes only.
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export declare const agentCreateCache: Cache<HttpAgentConfig | HttpsAgentConfig>;
|
|
26
34
|
/**
|
|
27
35
|
* Builds part of the request config containing the URL and if needed proxy agents or normal http agents.
|
|
28
36
|
* Considers the `no_proxy` environment variable together with the `targetUri`.
|
|
@@ -36,16 +36,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.agentCreateCache = void 0;
|
|
39
40
|
exports.getAgentConfig = getAgentConfig;
|
|
40
41
|
exports.urlAndAgent = urlAndAgent;
|
|
41
|
-
const promises_1 = require("fs/promises");
|
|
42
|
-
const
|
|
43
|
-
const
|
|
42
|
+
const promises_1 = require("node:fs/promises");
|
|
43
|
+
const node_http_1 = __importDefault(require("node:http"));
|
|
44
|
+
const node_https_1 = __importDefault(require("node:https"));
|
|
44
45
|
const jks = __importStar(require("jks-js"));
|
|
45
46
|
const util_1 = require("@sap-cloud-sdk/util");
|
|
46
47
|
/* Careful the proxy imports cause circular dependencies if imported from scp directly */
|
|
47
48
|
// eslint-disable-next-line import/no-internal-modules
|
|
48
49
|
const get_protocol_1 = require("../scp-cf/get-protocol");
|
|
50
|
+
// eslint-disable-next-line import/no-internal-modules
|
|
51
|
+
const cache_1 = require("../scp-cf/cache");
|
|
49
52
|
const http_proxy_util_1 = require("../scp-cf/destination/http-proxy-util");
|
|
50
53
|
// eslint-disable-next-line import/no-internal-modules
|
|
51
54
|
const register_destination_cache_1 = require("../scp-cf/destination/register-destination-cache");
|
|
@@ -219,14 +222,30 @@ function validateFormat(certificate) {
|
|
|
219
222
|
throw Error(`The format of the provided certificate '${certificate.name}' is not supported. Supported formats are: ${supportedCertificateFormats.join(', ')}.`);
|
|
220
223
|
}
|
|
221
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Cache for http(s) agents.
|
|
227
|
+
* Exported for testing purposes only.
|
|
228
|
+
* @internal
|
|
229
|
+
*/
|
|
230
|
+
exports.agentCreateCache = new cache_1.Cache(3600000, // 1 hour
|
|
231
|
+
100 // max 100 LRU-cached agents
|
|
232
|
+
);
|
|
222
233
|
/**
|
|
223
234
|
* @internal
|
|
235
|
+
* Agents are cache for up to one hour, but can be evicted earlier if more than 100 agents are created.
|
|
224
236
|
* See https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener for details on the possible options
|
|
225
237
|
*/
|
|
226
238
|
function createAgent(destination, options) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
239
|
+
const protocol = (0, get_protocol_1.getProtocolOrDefault)(destination);
|
|
240
|
+
const cacheKey = (0, cache_1.hashCacheKey)({ protocol, options });
|
|
241
|
+
return exports.agentCreateCache.getOrInsertComputed(cacheKey, () => {
|
|
242
|
+
logger.debug(`Creating new ${protocol.toUpperCase()} agent for destination ${destination.name || ''} with options: ${JSON.stringify(options)}`);
|
|
243
|
+
const optionsWithDefaults = { keepAlive: true, ...options };
|
|
244
|
+
const entry = protocol === 'https'
|
|
245
|
+
? { httpsAgent: new node_https_1.default.Agent(optionsWithDefaults) }
|
|
246
|
+
: { httpAgent: new node_http_1.default.Agent(optionsWithDefaults) };
|
|
247
|
+
return { entry };
|
|
248
|
+
});
|
|
230
249
|
}
|
|
231
250
|
/**
|
|
232
251
|
* Builds part of the request config containing the URL and if needed proxy agents or normal http agents.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-agent.js","sourceRoot":"","sources":["../../src/http-agent/http-agent.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"http-agent.js","sourceRoot":"","sources":["../../src/http-agent/http-agent.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,wCASC;AA2RD,kCAeC;AA3VD,+CAA4C;AAC5C,0DAA6B;AAC7B,4DAA+B;AAC/B,4CAA8B;AAC9B,8CAAyD;AACzD,yFAAyF;AACzF,sDAAsD;AACtD,yDAA8D;AAC9D,sDAAsD;AACtD,2CAAsD;AACtD,2EAK+C;AAC/C,sDAAsD;AACtD,iGAA4F;AAW5F,MAAM,MAAM,GAAG,IAAA,mBAAY,EAAC;IAC1B,OAAO,EAAE,cAAc;IACvB,cAAc,EAAE,YAAY;CAC7B,CAAC,CAAC;AAEH;;;;;;GAMG;AACI,KAAK,UAAU,cAAc,CAClC,WAA4B;IAE5B,MAAM,kBAAkB,GAAG;QACzB,GAAG,oBAAoB,CAAC,WAAW,CAAC;QACpC,GAAG,kBAAkB,CAAC,WAAW,CAAC;QAClC,GAAG,CAAC,MAAM,cAAc,CAAC,WAAW,CAAC,CAAC;KACvC,CAAC;IACF,OAAO,WAAW,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;AACtD,CAAC;AAED;;;;;;GAMG;AACH,SAAS,oBAAoB,CAAC,WAA4B;IAIxD,mCAAmC;IACn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C;QAEzD,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,WAAW,CAAC,SAAiB;IAMjD,IAAI,WAAW,GAAoB,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;IAC7E,IAAI,IAAA,+BAAa,EAAC,WAAW,CAAC,KAAK,UAAU,EAAE,CAAC;QAC9C,WAAW,GAAG,IAAA,+CAA6B,EAAC,WAAW,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO;QACL,OAAO,EAAE,WAAW,CAAC,GAAG;QACxB,GAAG,CAAC,MAAM,cAAc,CAAC,WAAW,CAAC,CAAC;QACtC,KAAK,EAAE,IAAA,gCAAc,EAAC,WAAW,CAAC;KACnC,CAAC;AACJ,CAAC"}
|
package/dist/scp-cf/cache.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ interface CacheInterface<T> {
|
|
|
3
3
|
get(key: string | undefined): T | undefined;
|
|
4
4
|
set(key: string | undefined, item: CacheEntry<T>): void;
|
|
5
5
|
clear(): void;
|
|
6
|
+
getOrInsertComputed(key: string | undefined, computeFn: () => CacheEntry<T>): T;
|
|
6
7
|
}
|
|
7
8
|
/**
|
|
8
9
|
* Representation of a cached object.
|
|
@@ -34,6 +35,7 @@ export interface CachingOptions {
|
|
|
34
35
|
*/
|
|
35
36
|
export declare class Cache<T> implements CacheInterface<T> {
|
|
36
37
|
private defaultValidityTime;
|
|
38
|
+
private maxSize;
|
|
37
39
|
/**
|
|
38
40
|
* Object that stores all cached entries.
|
|
39
41
|
*/
|
|
@@ -41,8 +43,9 @@ export declare class Cache<T> implements CacheInterface<T> {
|
|
|
41
43
|
/**
|
|
42
44
|
* Creates an instance of Cache.
|
|
43
45
|
* @param defaultValidityTime - The default validity time in milliseconds. Use 0 for unlimited cache duration.
|
|
46
|
+
* @param maxSize - The maximum number of entries in the cache. Use Infinity for unlimited size. Items are evicted based on a least recently used (LRU) strategy.
|
|
44
47
|
*/
|
|
45
|
-
constructor(defaultValidityTime: number);
|
|
48
|
+
constructor(defaultValidityTime: number, maxSize?: number);
|
|
46
49
|
/**
|
|
47
50
|
* Clear all cached items.
|
|
48
51
|
*/
|
|
@@ -65,6 +68,14 @@ export declare class Cache<T> implements CacheInterface<T> {
|
|
|
65
68
|
* @param item - The entry to cache.
|
|
66
69
|
*/
|
|
67
70
|
set(key: string | undefined, item: CacheEntry<T>): void;
|
|
71
|
+
getOrInsertComputed(key: string | undefined, computeFn: () => CacheEntry<T>): T;
|
|
68
72
|
private inferDefaultExpirationTime;
|
|
69
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Hashes the given value to create a cache key.
|
|
76
|
+
* @internal
|
|
77
|
+
* @param value - The value to hash.
|
|
78
|
+
* @returns A hash of the given value using a cryptographic hash function.
|
|
79
|
+
*/
|
|
80
|
+
export declare function hashCacheKey(value: Record<string, unknown>): string;
|
|
70
81
|
export {};
|
package/dist/scp-cf/cache.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Cache = void 0;
|
|
4
|
+
exports.hashCacheKey = hashCacheKey;
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
4
6
|
/**
|
|
5
7
|
* Representation of a cache to transiently store objects locally for faster access.
|
|
6
8
|
* @typeParam T - Type of the cache entries.
|
|
@@ -10,16 +12,18 @@ class Cache {
|
|
|
10
12
|
/**
|
|
11
13
|
* Creates an instance of Cache.
|
|
12
14
|
* @param defaultValidityTime - The default validity time in milliseconds. Use 0 for unlimited cache duration.
|
|
15
|
+
* @param maxSize - The maximum number of entries in the cache. Use Infinity for unlimited size. Items are evicted based on a least recently used (LRU) strategy.
|
|
13
16
|
*/
|
|
14
|
-
constructor(defaultValidityTime) {
|
|
17
|
+
constructor(defaultValidityTime, maxSize = Infinity) {
|
|
15
18
|
this.defaultValidityTime = defaultValidityTime;
|
|
16
|
-
this.
|
|
19
|
+
this.maxSize = maxSize;
|
|
20
|
+
this.cache = new Map();
|
|
17
21
|
}
|
|
18
22
|
/**
|
|
19
23
|
* Clear all cached items.
|
|
20
24
|
*/
|
|
21
25
|
clear() {
|
|
22
|
-
this.cache
|
|
26
|
+
this.cache.clear();
|
|
23
27
|
}
|
|
24
28
|
/**
|
|
25
29
|
* Specifies whether an entry with a given key is defined in cache.
|
|
@@ -27,7 +31,7 @@ class Cache {
|
|
|
27
31
|
* @returns A boolean value that indicates whether the entry exists in cache.
|
|
28
32
|
*/
|
|
29
33
|
hasKey(key) {
|
|
30
|
-
return this.cache.
|
|
34
|
+
return this.cache.has(key);
|
|
31
35
|
}
|
|
32
36
|
/**
|
|
33
37
|
* Getter of cached entries.
|
|
@@ -35,9 +39,19 @@ class Cache {
|
|
|
35
39
|
* @returns The corresponding entry to the provided key if it is still valid, returns `undefined` otherwise.
|
|
36
40
|
*/
|
|
37
41
|
get(key) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
42
|
+
const entry = key ? this.cache.get(key) : undefined;
|
|
43
|
+
if (entry) {
|
|
44
|
+
if (isExpired(entry)) {
|
|
45
|
+
this.cache.delete(key);
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
// LRU cache: Move accessed entry to the end of the Map to mark it as recently used
|
|
49
|
+
if (this.maxSize !== Infinity) {
|
|
50
|
+
this.cache.delete(key);
|
|
51
|
+
this.cache.set(key, entry);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return entry?.entry;
|
|
41
55
|
}
|
|
42
56
|
/**
|
|
43
57
|
* Setter of entries in cache.
|
|
@@ -45,10 +59,34 @@ class Cache {
|
|
|
45
59
|
* @param item - The entry to cache.
|
|
46
60
|
*/
|
|
47
61
|
set(key, item) {
|
|
48
|
-
if (key) {
|
|
49
|
-
|
|
50
|
-
|
|
62
|
+
if (!key) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (this.cache.size >= this.maxSize &&
|
|
66
|
+
this.cache.size > 0 &&
|
|
67
|
+
!this.cache.has(key)) {
|
|
68
|
+
// Evict the least recently used (LRU) entry
|
|
69
|
+
const lruKey = this.cache.keys().next().value;
|
|
70
|
+
this.cache.delete(lruKey); // SAFETY: size > 0
|
|
51
71
|
}
|
|
72
|
+
if (this.maxSize !== Infinity && this.cache.has(key)) {
|
|
73
|
+
// If the key already exists, delete it to update its position in the LRU order
|
|
74
|
+
this.cache.delete(key);
|
|
75
|
+
}
|
|
76
|
+
const expires = item.expires ?? this.inferDefaultExpirationTime();
|
|
77
|
+
this.cache.set(key, { entry: item.entry, expires });
|
|
78
|
+
}
|
|
79
|
+
getOrInsertComputed(key, computeFn) {
|
|
80
|
+
if (!key) {
|
|
81
|
+
return computeFn().entry;
|
|
82
|
+
}
|
|
83
|
+
const cachedEntry = this.get(key);
|
|
84
|
+
if (cachedEntry !== undefined) {
|
|
85
|
+
return cachedEntry;
|
|
86
|
+
}
|
|
87
|
+
const newEntry = computeFn();
|
|
88
|
+
this.set(key, newEntry);
|
|
89
|
+
return newEntry.entry;
|
|
52
90
|
}
|
|
53
91
|
inferDefaultExpirationTime() {
|
|
54
92
|
const now = new Date();
|
|
@@ -60,6 +98,16 @@ class Cache {
|
|
|
60
98
|
}
|
|
61
99
|
}
|
|
62
100
|
exports.Cache = Cache;
|
|
101
|
+
/**
|
|
102
|
+
* Hashes the given value to create a cache key.
|
|
103
|
+
* @internal
|
|
104
|
+
* @param value - The value to hash.
|
|
105
|
+
* @returns A hash of the given value using a cryptographic hash function.
|
|
106
|
+
*/
|
|
107
|
+
function hashCacheKey(value) {
|
|
108
|
+
const serialized = JSON.stringify(value);
|
|
109
|
+
return (0, node_crypto_1.createHash)('blake2s256').update(serialized).digest('hex');
|
|
110
|
+
}
|
|
63
111
|
function isExpired(item) {
|
|
64
112
|
if (item.expires === undefined) {
|
|
65
113
|
return false;
|
package/dist/scp-cf/cache.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../src/scp-cf/cache.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../../src/scp-cf/cache.ts"],"names":[],"mappings":";;;AA6JA,oCAGC;AAhKD,6CAAyC;AAsCzC;;;;GAIG;AACH,MAAa,KAAK;IAMhB;;;;OAIG;IACH,YACU,mBAA2B,EAC3B,UAAU,QAAQ;QADlB,wBAAmB,GAAnB,mBAAmB,CAAQ;QAC3B,YAAO,GAAP,OAAO,CAAW;QAE1B,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAyB,CAAC;IAChD,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,GAAW;QAChB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,GAAuB;QACzB,MAAM,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACpD,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;gBACrB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAI,CAAC,CAAC;gBACxB,OAAO,SAAS,CAAC;YACnB,CAAC;YACD,mFAAmF;YACnF,IAAI,IAAI,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;gBAC9B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAI,CAAC,CAAC;gBACxB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAI,EAAE,KAAK,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;QACD,OAAO,KA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AG,SAAS,EAAE,CAAC;QAC7B,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;QACxB,OAAO,QAAQ,CAAC,KAAK,CAAC;IACxB,CAAC;IAEO,0BAA0B;QAChC,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,OAAO,IAAI,CAAC,mBAAmB;YAC7B,CAAC,CAAC,GAAG;iBACA,eAAe,CAAC,GAAG,CAAC,eAAe,EAAE,GAAG,IAAI,CAAC,mBAAmB,CAAC;iBACjE,OAAO,EAAE;YACd,CAAC,CAAC,SAAS,CAAC;IAChB,CAAC;CACF;AA1GD,sBA0GC;AAED;;;;;GAKG;AACH,SAAgB,YAAY,CAAC,KAA8B;IACzD,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACzC,OAAO,IAAA,wBAAU,EAAC,YAAY,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACnE,CAAC;AAED,SAAS,SAAS,CAAI,IAAmB;IACvC,IAAI,IAAI,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;AACnC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sap-cloud-sdk/connectivity",
|
|
3
|
-
"version": "4.6.0",
|
|
3
|
+
"version": "4.6.1-20260414124519.0",
|
|
4
4
|
"description": "SAP Cloud SDK for JavaScript connectivity",
|
|
5
5
|
"homepage": "https://sap.github.io/cloud-sdk/docs/js/overview",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"readme": "ts-node ../../scripts/replace-common-readme.ts"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@sap-cloud-sdk/resilience": "^4.6.0",
|
|
44
|
-
"@sap-cloud-sdk/util": "^4.6.0",
|
|
43
|
+
"@sap-cloud-sdk/resilience": "^4.6.1-20260414124519.0",
|
|
44
|
+
"@sap-cloud-sdk/util": "^4.6.1-20260414124519.0",
|
|
45
45
|
"@sap/xsenv": "^6.1.0",
|
|
46
46
|
"@sap/xssec": "^4.13.0",
|
|
47
47
|
"async-retry": "^1.3.3",
|