@settlemint/dalp-sdk 2.1.7-main.25732471874 → 2.1.7-main.25733254752
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.
- package/dist/attempt-header.d.ts +4 -0
- package/dist/attempt-header.d.ts.map +1 -0
- package/dist/attempt-header.js +9 -0
- package/dist/chunk-213v5nej.js +54 -0
- package/dist/client.d.ts +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/index.js +989 -879
- package/package.json +8 -1
- package/src/attempt-header.ts +82 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function fetchWithAttemptHeader(request: RequestInfo | URL, init: RequestInit | undefined, innerFetch: (req: RequestInfo | URL, init?: RequestInit) => Promise<Response>): Promise<Response>;
|
|
2
|
+
/** Test-only: reset the in-memory attempt tracker between cases. */
|
|
3
|
+
export declare function __resetAttemptTrackerForTests(): void;
|
|
4
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXR0ZW1wdC1oZWFkZXIuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9hdHRlbXB0LWhlYWRlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFxREEsd0JBQXNCLHNCQUFzQixDQUMxQyxPQUFPLEVBQUUsV0FBVyxHQUFHLEdBQUcsRUFDMUIsSUFBSSxFQUFFLFdBQVcsR0FBRyxTQUFTLEVBQzdCLFVBQVUsRUFBRSxDQUFDLEdBQUcsRUFBRSxXQUFXLEdBQUcsR0FBRyxFQUFFLElBQUksQ0FBQyxFQUFFLFdBQVcsS0FBSyxPQUFPLENBQUMsUUFBUSxDQUFDLEdBQzVFLE9BQU8sQ0FBQyxRQUFRLENBQUMsQ0FtQm5CO0FBRUQsb0VBQW9FO0FBQ3BFLHdCQUFnQiw2QkFBNkIsSUFBSSxJQUFJLENBRXBEIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attempt-header.d.ts","sourceRoot":"","sources":["../src/attempt-header.ts"],"names":[],"mappings":"AAqDA,wBAAsB,sBAAsB,CAC1C,OAAO,EAAE,WAAW,GAAG,GAAG,EAC1B,IAAI,EAAE,WAAW,GAAG,SAAS,EAC7B,UAAU,EAAE,CAAC,GAAG,EAAE,WAAW,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,GAC5E,OAAO,CAAC,QAAQ,CAAC,CAmBnB;AAED,oEAAoE;AACpE,wBAAgB,6BAA6B,IAAI,IAAI,CAEpD"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/attempt-header.ts
|
|
2
|
+
var ATTEMPT_TTL_MS = 60000;
|
|
3
|
+
var attemptByKey = new Map;
|
|
4
|
+
function buildAttemptKey(url, method, bodyKey) {
|
|
5
|
+
return `${method} ${url}|${bodyKey}`;
|
|
6
|
+
}
|
|
7
|
+
function bodyInitKey(body) {
|
|
8
|
+
return typeof body === "string" ? body.slice(0, 256) : "";
|
|
9
|
+
}
|
|
10
|
+
async function requestBodyKey(request) {
|
|
11
|
+
if (request.body === null) {
|
|
12
|
+
return "";
|
|
13
|
+
}
|
|
14
|
+
const body = await request.clone().text();
|
|
15
|
+
return body.slice(0, 256);
|
|
16
|
+
}
|
|
17
|
+
function nextAttempt(key, now = Date.now()) {
|
|
18
|
+
for (const [k, v] of attemptByKey) {
|
|
19
|
+
if (v.expiresAt < now) {
|
|
20
|
+
attemptByKey.delete(k);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const existing = attemptByKey.get(key);
|
|
24
|
+
const attempt = (existing?.attempt ?? 0) + 1;
|
|
25
|
+
attemptByKey.set(key, { attempt, expiresAt: now + ATTEMPT_TTL_MS });
|
|
26
|
+
return attempt;
|
|
27
|
+
}
|
|
28
|
+
function clearAttempt(key) {
|
|
29
|
+
attemptByKey.delete(key);
|
|
30
|
+
}
|
|
31
|
+
function appendAttempt(source, attempt) {
|
|
32
|
+
const headers = new Headers(source);
|
|
33
|
+
headers.set("X-Dalp-Attempt", String(attempt));
|
|
34
|
+
return headers;
|
|
35
|
+
}
|
|
36
|
+
async function fetchWithAttemptHeader(request, init, innerFetch) {
|
|
37
|
+
const url = request instanceof Request ? request.url : String(request);
|
|
38
|
+
const method = (request instanceof Request ? request.method : init?.method) ?? "GET";
|
|
39
|
+
const bodyKey = request instanceof Request ? await requestBodyKey(request) : bodyInitKey(init?.body ?? null);
|
|
40
|
+
const key = buildAttemptKey(url, method, bodyKey);
|
|
41
|
+
const attempt = nextAttempt(key);
|
|
42
|
+
const augmentedRequest = request instanceof Request ? new Request(request, { headers: appendAttempt(request.headers, attempt) }) : request;
|
|
43
|
+
const augmentedInit = request instanceof Request ? init : { ...init, headers: appendAttempt(init?.headers, attempt) };
|
|
44
|
+
const response = await innerFetch(augmentedRequest, augmentedInit);
|
|
45
|
+
if (response.ok) {
|
|
46
|
+
clearAttempt(key);
|
|
47
|
+
}
|
|
48
|
+
return response;
|
|
49
|
+
}
|
|
50
|
+
function __resetAttemptTrackerForTests() {
|
|
51
|
+
attemptByKey.clear();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { fetchWithAttemptHeader, __resetAttemptTrackerForTests };
|
package/dist/client.d.ts
CHANGED
|
@@ -16,4 +16,4 @@ import type { DalpClient, DalpClientConfig, PublicDalpClientConfig } from "./typ
|
|
|
16
16
|
* ```
|
|
17
17
|
*/
|
|
18
18
|
export declare function createDalpClient(config: DalpClientConfig | PublicDalpClientConfig): DalpClient;
|
|
19
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
19
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xpZW50LmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvY2xpZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQW1CQSxPQUFPLEtBQUssRUFBRSxVQUFVLEVBQUUsZ0JBQWdCLEVBQUUsc0JBQXNCLEVBQUUsTUFBTSxZQUFZLENBQUM7QUFJdkY7Ozs7Ozs7Ozs7Ozs7OztHQWVHO0FBQ0gsd0JBQWdCLGdCQUFnQixDQUFDLE1BQU0sRUFBRSxnQkFBZ0IsR0FBRyxzQkFBc0IsR0FBRyxVQUFVLENBb0Y5RiJ9
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAIvF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,GAAG,sBAAsB,GAAG,UAAU,CAoF9F"}
|