blossom-client-sdk 5.0.0 → 5.2.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.
- package/README.md +65 -8
- package/lib/actions/delete.d.ts +3 -0
- package/lib/actions/delete.js +17 -38
- package/lib/actions/download.d.ts +3 -0
- package/lib/actions/download.js +17 -35
- package/lib/actions/has.d.ts +1 -1
- package/lib/actions/has.js +6 -2
- package/lib/actions/list.d.ts +3 -0
- package/lib/actions/list.js +18 -31
- package/lib/actions/media.js +28 -45
- package/lib/actions/mirror.d.ts +3 -0
- package/lib/actions/mirror.js +17 -40
- package/lib/actions/multi-server.js +27 -0
- package/lib/actions/resolve.js +5 -0
- package/lib/actions/upload.d.ts +3 -0
- package/lib/actions/upload.js +30 -47
- package/lib/helpers/blob.js +4 -3
- package/lib/helpers/cashu.d.ts +3 -1
- package/lib/helpers/cashu.js +185 -4
- package/lib/helpers/headers.d.ts +1 -0
- package/lib/helpers/headers.js +5 -0
- package/lib/helpers/index.d.ts +1 -1
- package/lib/helpers/index.js +1 -1
- package/lib/helpers/signal.d.ts +1 -0
- package/lib/helpers/signal.js +4 -3
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/internal/bytes.d.ts +2 -0
- package/lib/internal/bytes.js +6 -0
- package/lib/internal/challenges.d.ts +6 -0
- package/lib/internal/challenges.js +15 -0
- package/package.json +21 -21
- package/src/actions/delete.ts +20 -42
- package/src/actions/download.ts +20 -41
- package/src/actions/has.ts +5 -2
- package/src/actions/list.ts +21 -35
- package/src/actions/media.ts +26 -47
- package/src/actions/mirror.ts +20 -44
- package/src/actions/multi-server.ts +25 -0
- package/src/actions/resolve.ts +5 -0
- package/src/actions/upload.ts +31 -48
- package/src/helpers/blob.ts +5 -4
- package/src/helpers/cashu.ts +184 -7
- package/src/helpers/headers.ts +5 -0
- package/src/helpers/index.ts +1 -1
- package/src/helpers/signal.ts +6 -4
- package/src/index.ts +1 -0
- package/src/internal/bytes.ts +6 -0
- package/src/internal/challenges.ts +25 -0
- package/src/types.ts +1 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blossom-client-sdk",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "Client SDK for talking to blossom servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"types": "./lib/auth.d.ts"
|
|
43
43
|
},
|
|
44
44
|
"./actions": {
|
|
45
|
-
"import": "./lib/actions.js",
|
|
46
|
-
"types": "./lib/actions.d.ts"
|
|
45
|
+
"import": "./lib/actions/index.js",
|
|
46
|
+
"types": "./lib/actions/index.d.ts"
|
|
47
47
|
},
|
|
48
48
|
"./actions/*": {
|
|
49
49
|
"import": "./lib/actions/*.js",
|
|
@@ -51,44 +51,44 @@
|
|
|
51
51
|
}
|
|
52
52
|
},
|
|
53
53
|
"engines": {
|
|
54
|
-
"node": ">=
|
|
54
|
+
"node": ">=20.19.0"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@noble/hashes": "^
|
|
57
|
+
"@noble/hashes": "^2.4.0"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@cashu/cashu-ts": "^2.4.3",
|
|
61
60
|
"hls.js": "^1.6.16"
|
|
62
61
|
},
|
|
63
62
|
"peerDependenciesMeta": {
|
|
64
|
-
"@cashu/cashu-ts": {
|
|
65
|
-
"optional": true
|
|
66
|
-
},
|
|
67
63
|
"hls.js": {
|
|
68
64
|
"optional": true
|
|
69
65
|
}
|
|
70
66
|
},
|
|
71
67
|
"devDependencies": {
|
|
72
|
-
"@cashu/cashu-ts": "^2.
|
|
73
|
-
"@
|
|
74
|
-
"@
|
|
75
|
-
"@
|
|
76
|
-
"@vitest/
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"prettier": "^3.5.3",
|
|
68
|
+
"@cashu/cashu-ts": "^2.9.0",
|
|
69
|
+
"@testing-library/dom": "^10.4.2",
|
|
70
|
+
"@types/node": "^22.20.5",
|
|
71
|
+
"@vitest/browser": "^3.2.7",
|
|
72
|
+
"@vitest/coverage-v8": "^3.2.7",
|
|
73
|
+
"hls.js": "^1.7.4",
|
|
74
|
+
"nostr-tools": "^2.26.1",
|
|
75
|
+
"playwright": "^1.65.1",
|
|
76
|
+
"prettier": "^3.10.1",
|
|
82
77
|
"typedoc": "^0.25.13",
|
|
83
78
|
"typedoc-plugin-mermaid": "^1.12.0",
|
|
84
|
-
"typescript": "^5.
|
|
85
|
-
"vitest": "^3.
|
|
79
|
+
"typescript": "^5.9.3",
|
|
80
|
+
"vitest": "^3.2.7",
|
|
86
81
|
"vitest-fetch-mock": "^0.4.5"
|
|
87
82
|
},
|
|
88
83
|
"scripts": {
|
|
89
84
|
"build": "tsc",
|
|
90
85
|
"prepublish": "npm run build",
|
|
91
86
|
"test": "vitest run",
|
|
87
|
+
"test:browser": "vitest run --browser --browser.headless",
|
|
88
|
+
"test:node": "pnpm build && node node_modules/vitest/vitest.mjs run && node --test tests/package-exports.test.mjs tests/runtime/*.test.mjs",
|
|
89
|
+
"test:deno": "pnpm build && deno run -A --no-config --no-lock --node-modules-dir=manual node_modules/vitest/vitest.mjs run && deno test -A --no-config --no-lock --no-check --node-modules-dir=manual tests/package-exports.test.mjs tests/runtime/",
|
|
90
|
+
"test:bun": "pnpm build && bun run --bun node_modules/vitest/vitest.mjs run && bun test tests/package-exports.test.mjs tests/runtime/",
|
|
91
|
+
"test:all": "pnpm test:node && pnpm test:deno && pnpm test:bun && pnpm test:browser",
|
|
92
92
|
"coverage": "vitest run --coverage",
|
|
93
93
|
"format": "prettier -w .",
|
|
94
94
|
"docs": "typedoc src/index.ts"
|
package/src/actions/delete.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from
|
|
|
2
2
|
import { ServerType } from "../types.js";
|
|
3
3
|
import HTTPError from "../error.js";
|
|
4
4
|
import { fetchWithTimeout } from "../helpers/fetch.js";
|
|
5
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
5
6
|
import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
6
7
|
|
|
7
8
|
export type DeleteOptions<S extends ServerType> = {
|
|
@@ -13,12 +14,15 @@ export type DeleteOptions<S extends ServerType> = {
|
|
|
13
14
|
authEvents?: Set<SignedEvent>;
|
|
14
15
|
/** Request timeout */
|
|
15
16
|
timeout?: number;
|
|
17
|
+
/** Return headers to retry a request rejected with HTTP 402. */
|
|
18
|
+
onPaymentRequired?: (server: S, sha256: string, headers: Headers) => Promise<HeadersInit>;
|
|
16
19
|
/**
|
|
17
20
|
* A method used to request payment when deleting
|
|
18
21
|
* @param server the server requiring payment
|
|
19
22
|
* @param sha256 the sha256 of the blob being uploaded or mirrored
|
|
20
23
|
* @param request the payment request
|
|
21
24
|
*/
|
|
25
|
+
/** @deprecated Use onPaymentRequired to handle payment schemes generically. */
|
|
22
26
|
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<PaymentToken>;
|
|
23
27
|
/**
|
|
24
28
|
* A method used to request a signed auth event for a server and sha256
|
|
@@ -55,50 +59,24 @@ export async function deleteBlob<S extends ServerType>(server: S, hash: string,
|
|
|
55
59
|
}
|
|
56
60
|
}
|
|
57
61
|
|
|
58
|
-
|
|
59
|
-
method: "DELETE",
|
|
60
|
-
|
|
62
|
+
const request = (headers: HeadersInit) =>
|
|
63
|
+
fetchWithTimeout(url, { method: "DELETE", headers, signal: opts?.signal, timeout: opts?.timeout });
|
|
64
|
+
const res = await handleChallenges(await request(headers), headers, request, {
|
|
61
65
|
signal: opts?.signal,
|
|
62
|
-
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// handle auth and payment
|
|
66
|
-
switch (res.status) {
|
|
67
|
-
case 401: {
|
|
68
|
-
// throw an error if authorization is required but disabled
|
|
66
|
+
auth: async () => {
|
|
69
67
|
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
case 402: {
|
|
84
|
-
if (!opts?.onPayment) throw new Error("Missing payment handler");
|
|
85
|
-
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
86
|
-
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
87
|
-
const request = await getPaymentRequestFromHeaders(res.headers);
|
|
88
|
-
|
|
89
|
-
const token = await opts.onPayment(server, hash, request);
|
|
90
|
-
const payment = getEncodedToken(token);
|
|
91
|
-
|
|
92
|
-
// Try delete with payment
|
|
93
|
-
res = await fetchWithTimeout(url, {
|
|
94
|
-
signal: opts?.signal,
|
|
95
|
-
method: "DELETE",
|
|
96
|
-
headers: { ...headers, "X-Cashu": payment },
|
|
97
|
-
timeout: opts?.timeout,
|
|
98
|
-
});
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
68
|
+
return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
|
|
69
|
+
},
|
|
70
|
+
payment: async (headers) => {
|
|
71
|
+
if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, hash, headers);
|
|
72
|
+
if (opts?.onPayment) {
|
|
73
|
+
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
74
|
+
const request = await getPaymentRequestFromHeaders(headers);
|
|
75
|
+
return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, hash, request)) };
|
|
76
|
+
}
|
|
77
|
+
throw new Error("Missing payment handler");
|
|
78
|
+
},
|
|
79
|
+
});
|
|
102
80
|
|
|
103
81
|
// handle errors
|
|
104
82
|
await HTTPError.handleErrorResponse(res);
|
package/src/actions/download.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
|
3
3
|
import HTTPError from "../error.js";
|
|
4
4
|
import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from "../auth.js";
|
|
5
5
|
import { fetchWithTimeout } from "../helpers/index.js";
|
|
6
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
6
7
|
|
|
7
8
|
export type DownloadOptions<S extends ServerType> = {
|
|
8
9
|
/** AbortSignal to cancel the action */
|
|
@@ -13,12 +14,15 @@ export type DownloadOptions<S extends ServerType> = {
|
|
|
13
14
|
authEvents?: Set<SignedEvent>;
|
|
14
15
|
/** Request timeout */
|
|
15
16
|
timeout?: number;
|
|
17
|
+
/** Return headers to retry a request rejected with HTTP 402. */
|
|
18
|
+
onPaymentRequired?: (server: S, sha256: string, headers: Headers) => Promise<HeadersInit>;
|
|
16
19
|
/**
|
|
17
20
|
* A method used to request payment when downloading
|
|
18
21
|
* @param server the server requiring payment
|
|
19
22
|
* @param sha256 the sha256 of the blob being uploaded or mirrored
|
|
20
23
|
* @param request the payment request
|
|
21
24
|
*/
|
|
25
|
+
/** @deprecated Use onPaymentRequired to handle payment schemes generically. */
|
|
22
26
|
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<PaymentToken>;
|
|
23
27
|
/**
|
|
24
28
|
* A method used to request a signed auth event for a server and sha256
|
|
@@ -55,49 +59,24 @@ export async function downloadBlob<S extends ServerType>(server: S, hash: string
|
|
|
55
59
|
}
|
|
56
60
|
}
|
|
57
61
|
|
|
58
|
-
|
|
59
|
-
headers,
|
|
62
|
+
const request = (headers: HeadersInit) =>
|
|
63
|
+
fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
|
|
64
|
+
const download = await handleChallenges(await request(headers), headers, request, {
|
|
60
65
|
signal: opts?.signal,
|
|
61
|
-
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// handle auth and payment
|
|
65
|
-
switch (download.status) {
|
|
66
|
-
case 401: {
|
|
67
|
-
// throw an error if auth is requested and disabled
|
|
66
|
+
auth: async () => {
|
|
68
67
|
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
case 402: {
|
|
83
|
-
if (!opts?.onPayment) throw new Error("Missing payment handler");
|
|
84
|
-
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
85
|
-
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
86
|
-
const request = await getPaymentRequestFromHeaders(download.headers);
|
|
87
|
-
|
|
88
|
-
const token = await opts.onPayment(server, hash, request);
|
|
89
|
-
const payment = getEncodedToken(token);
|
|
90
|
-
|
|
91
|
-
// Try download with payment
|
|
92
|
-
download = await fetchWithTimeout(url, {
|
|
93
|
-
headers: { ...headers, "X-Cashu": payment },
|
|
94
|
-
signal: opts?.signal,
|
|
95
|
-
timeout: opts?.timeout,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
68
|
+
return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
|
|
69
|
+
},
|
|
70
|
+
payment: async (headers) => {
|
|
71
|
+
if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, hash, headers);
|
|
72
|
+
if (opts?.onPayment) {
|
|
73
|
+
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
74
|
+
const request = await getPaymentRequestFromHeaders(headers);
|
|
75
|
+
return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, hash, request)) };
|
|
76
|
+
}
|
|
77
|
+
throw new Error("Missing payment handler");
|
|
78
|
+
},
|
|
79
|
+
});
|
|
101
80
|
|
|
102
81
|
// check download errors
|
|
103
82
|
await HTTPError.handleErrorResponse(download);
|
package/src/actions/has.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ServerType } from "../types.js";
|
|
2
2
|
import { fetchWithTimeout } from "../helpers/index.js";
|
|
3
|
+
import HTTPError from "../error.js";
|
|
3
4
|
|
|
4
5
|
export type HasBlobOptions = {
|
|
5
6
|
/** AbortSignal to cancel the action */
|
|
@@ -8,12 +9,14 @@ export type HasBlobOptions = {
|
|
|
8
9
|
timeout?: number;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
|
-
/**
|
|
12
|
+
/** Returns false for a missing blob (404), true on success, and throws on other HTTP errors. */
|
|
12
13
|
export async function hasBlob<S extends ServerType>(server: S, hash: string, opts?: HasBlobOptions): Promise<boolean> {
|
|
13
14
|
const res = await fetchWithTimeout(new URL("/" + hash, server), {
|
|
14
15
|
method: "HEAD",
|
|
15
16
|
signal: opts?.signal,
|
|
16
17
|
timeout: opts?.timeout,
|
|
17
18
|
});
|
|
18
|
-
|
|
19
|
+
if (res.status === 404) return false;
|
|
20
|
+
await HTTPError.handleErrorResponse(res);
|
|
21
|
+
return true;
|
|
19
22
|
}
|
package/src/actions/list.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { ServerType } from "../types.js";
|
|
|
3
3
|
import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
4
|
import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from "../auth.js";
|
|
5
5
|
import { fetchWithTimeout } from "../helpers/index.js";
|
|
6
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
6
7
|
|
|
7
8
|
export type ListOptions<S extends ServerType> = {
|
|
8
9
|
/** AbortSignal to cancel the action */
|
|
@@ -17,11 +18,14 @@ export type ListOptions<S extends ServerType> = {
|
|
|
17
18
|
limit?: number;
|
|
18
19
|
since?: number;
|
|
19
20
|
until?: number;
|
|
21
|
+
/** Return headers to retry a request rejected with HTTP 402. */
|
|
22
|
+
onPaymentRequired?: (server: S, headers: Headers) => Promise<HeadersInit>;
|
|
20
23
|
/**
|
|
21
24
|
* A method used to request payment
|
|
22
25
|
* @param server the server requiring payment
|
|
23
26
|
* @param request the payment request
|
|
24
27
|
*/
|
|
28
|
+
/** @deprecated Use onPaymentRequired to handle payment schemes generically. */
|
|
25
29
|
onPayment?: (server: S, request: PaymentRequest) => Promise<PaymentToken>;
|
|
26
30
|
/**
|
|
27
31
|
* A method used to request a signed auth event for a server
|
|
@@ -64,42 +68,24 @@ export async function listBlobs<S extends ServerType>(
|
|
|
64
68
|
}
|
|
65
69
|
}
|
|
66
70
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// throw an error if auth is requested and disabled
|
|
71
|
+
const request = (headers: HeadersInit) =>
|
|
72
|
+
fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
|
|
73
|
+
const list = await handleChallenges(await request(headers), headers, request, {
|
|
74
|
+
signal: opts?.signal,
|
|
75
|
+
auth: async () => {
|
|
73
76
|
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (!opts?.onPayment) throw new Error("Missing payment handler");
|
|
87
|
-
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
88
|
-
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
89
|
-
const request = await getPaymentRequestFromHeaders(list.headers);
|
|
90
|
-
|
|
91
|
-
const token = await opts.onPayment(server, request);
|
|
92
|
-
const payment = getEncodedToken(token);
|
|
93
|
-
|
|
94
|
-
// Try list with payment
|
|
95
|
-
list = await fetchWithTimeout(url, {
|
|
96
|
-
headers: { ...headers, "X-Cashu": payment },
|
|
97
|
-
signal: opts?.signal,
|
|
98
|
-
timeout: opts?.timeout,
|
|
99
|
-
});
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
77
|
+
return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
|
|
78
|
+
},
|
|
79
|
+
payment: async (headers) => {
|
|
80
|
+
if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, headers);
|
|
81
|
+
if (opts?.onPayment) {
|
|
82
|
+
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
83
|
+
const request = await getPaymentRequestFromHeaders(headers);
|
|
84
|
+
return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, request)) };
|
|
85
|
+
}
|
|
86
|
+
throw new Error("Missing payment handler");
|
|
87
|
+
},
|
|
88
|
+
});
|
|
103
89
|
|
|
104
90
|
// handle errors
|
|
105
91
|
await HTTPError.handleErrorResponse(list);
|
package/src/actions/media.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { ServerType, UploadType } from "../types.js";
|
|
2
2
|
import { BlobDescriptor } from "../types.js";
|
|
3
3
|
import { getBlobSha256, getBlobSize, getBlobType } from "../helpers/index.js";
|
|
4
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
4
5
|
import HTTPError from "../error.js";
|
|
5
6
|
import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from "../auth.js";
|
|
6
7
|
import { SignedEvent } from "../types.js";
|
|
7
8
|
import { UploadOptions } from "./upload.js";
|
|
9
|
+
import { toWebBytes } from "../internal/bytes.js";
|
|
8
10
|
|
|
9
11
|
/** Error thrown when /media endpoint is not present on a server */
|
|
10
12
|
export class MediaEndpointMissingError extends Error {}
|
|
@@ -19,6 +21,7 @@ export async function uploadMedia<S extends ServerType, B extends UploadType>(
|
|
|
19
21
|
): Promise<BlobDescriptor> {
|
|
20
22
|
const url = new URL("/media", server);
|
|
21
23
|
const sha256 = await getBlobSha256(blob);
|
|
24
|
+
const body = blob instanceof Blob ? blob : toWebBytes(blob);
|
|
22
25
|
const authEvents = opts?.authEvents as Set<SignedEvent> | undefined;
|
|
23
26
|
const resolveAuth = async (preset: boolean = false) => {
|
|
24
27
|
const reused = authEvents
|
|
@@ -55,68 +58,44 @@ export async function uploadMedia<S extends ServerType, B extends UploadType>(
|
|
|
55
58
|
if (type) checkHeaders["X-Content-Type"] = type;
|
|
56
59
|
|
|
57
60
|
// check upload with HEAD /media
|
|
58
|
-
|
|
61
|
+
const firstTry = await fetch(url, {
|
|
59
62
|
method: "HEAD",
|
|
60
63
|
signal: opts?.signal,
|
|
61
64
|
headers: checkHeaders,
|
|
62
65
|
});
|
|
63
66
|
|
|
64
|
-
let upload: Response | undefined = undefined;
|
|
65
|
-
|
|
66
67
|
if (firstTry.status === 404) {
|
|
67
68
|
// HEAD endpoint is not supported. abort
|
|
68
69
|
throw new MediaEndpointMissingError("/media endpoint not supported");
|
|
69
70
|
}
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
switch (firstTry.status) {
|
|
73
|
-
case 401: {
|
|
74
|
-
// throw an error if auth is requested and disabled
|
|
75
|
-
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
76
|
-
|
|
77
|
-
// Request authorization event for this upload
|
|
78
|
-
const auth = await resolveAuth();
|
|
79
|
-
|
|
80
|
-
// Try upload with auth
|
|
81
|
-
upload = await fetch(url, {
|
|
82
|
-
signal: opts?.signal,
|
|
83
|
-
method: "PUT",
|
|
84
|
-
body: blob,
|
|
85
|
-
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
86
|
-
});
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
case 402: {
|
|
90
|
-
if (!opts?.onPayment) throw new Error("Missing payment handler");
|
|
91
|
-
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
92
|
-
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
93
|
-
const request = await getPaymentRequestFromHeaders(firstTry.headers);
|
|
94
|
-
|
|
95
|
-
const token = await opts.onPayment(server, sha256, blob, request);
|
|
96
|
-
const payment = getEncodedToken(token);
|
|
97
|
-
|
|
98
|
-
// Try upload with payment
|
|
99
|
-
upload = await fetch(url, {
|
|
100
|
-
signal: opts?.signal,
|
|
101
|
-
method: "PUT",
|
|
102
|
-
body: blob,
|
|
103
|
-
headers: { ...headers, "X-Cashu": payment },
|
|
104
|
-
});
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (firstTry.status >= 500) throw new Error("Server error");
|
|
72
|
+
if (![401, 402].includes(firstTry.status)) await HTTPError.handleErrorResponse(firstTry);
|
|
110
73
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
upload = await fetch(url, {
|
|
74
|
+
const request = (headers: HeadersInit) =>
|
|
75
|
+
fetch(url, {
|
|
114
76
|
signal: opts?.signal,
|
|
115
77
|
method: "PUT",
|
|
116
|
-
body
|
|
117
|
-
headers
|
|
78
|
+
body,
|
|
79
|
+
headers,
|
|
118
80
|
});
|
|
119
81
|
|
|
82
|
+
const upload = await handleChallenges(firstTry.ok ? await request(headers) : firstTry, headers, request, {
|
|
83
|
+
signal: opts?.signal,
|
|
84
|
+
auth: async () => {
|
|
85
|
+
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
86
|
+
return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
|
|
87
|
+
},
|
|
88
|
+
payment: async (headers) => {
|
|
89
|
+
if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, sha256, blob, headers);
|
|
90
|
+
if (opts?.onPayment) {
|
|
91
|
+
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
92
|
+
const request = await getPaymentRequestFromHeaders(headers);
|
|
93
|
+
return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, sha256, blob, request)) };
|
|
94
|
+
}
|
|
95
|
+
throw new Error("Missing payment handler");
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
120
99
|
// handle errors
|
|
121
100
|
await HTTPError.handleErrorResponse(upload);
|
|
122
101
|
|
package/src/actions/mirror.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from
|
|
|
2
2
|
import { ServerType } from "../types.js";
|
|
3
3
|
import HTTPError from "../error.js";
|
|
4
4
|
import { fetchWithTimeout } from "../helpers/index.js";
|
|
5
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
5
6
|
import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
6
7
|
|
|
7
8
|
export type MirrorOptions<S extends ServerType> = {
|
|
@@ -13,6 +14,8 @@ export type MirrorOptions<S extends ServerType> = {
|
|
|
13
14
|
authEvents?: Set<SignedEvent>;
|
|
14
15
|
/** Request timeout */
|
|
15
16
|
timeout?: number;
|
|
17
|
+
/** Return headers to retry a request rejected with HTTP 402. */
|
|
18
|
+
onPaymentRequired?: (server: S, sha256: string, blob: BlobDescriptor, headers: Headers) => Promise<HeadersInit>;
|
|
16
19
|
/**
|
|
17
20
|
* A method used to request payment when uploading or mirroring a blob
|
|
18
21
|
* @param server the server requiring payment
|
|
@@ -20,6 +23,7 @@ export type MirrorOptions<S extends ServerType> = {
|
|
|
20
23
|
* @param blob the original blob
|
|
21
24
|
* @param request the payment request
|
|
22
25
|
*/
|
|
26
|
+
/** @deprecated Use onPaymentRequired to handle payment schemes generically. */
|
|
23
27
|
onPayment?: (server: S, sha256: string, blob: BlobDescriptor, request: PaymentRequest) => Promise<PaymentToken>;
|
|
24
28
|
/**
|
|
25
29
|
* A method used to request a signed auth event for a server and sha256
|
|
@@ -67,52 +71,24 @@ export async function mirrorBlob<S extends ServerType>(
|
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
const body = JSON.stringify({ url: blob.url });
|
|
70
|
-
|
|
71
|
-
method: "PUT",
|
|
74
|
+
const request = (headers: HeadersInit) =>
|
|
75
|
+
fetchWithTimeout(url, { method: "PUT", body, headers, signal: opts?.signal, timeout: opts?.timeout });
|
|
76
|
+
const mirror = await handleChallenges(await request(headers), headers, request, {
|
|
72
77
|
signal: opts?.signal,
|
|
73
|
-
|
|
74
|
-
body,
|
|
75
|
-
timeout: opts?.timeout,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
switch (mirror.status) {
|
|
79
|
-
case 401: {
|
|
80
|
-
// throw an error if auth is requested and disabled
|
|
78
|
+
auth: async () => {
|
|
81
79
|
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
case 402: {
|
|
97
|
-
if (!opts?.onPayment) throw new Error("Missing payment handler");
|
|
98
|
-
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
99
|
-
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
100
|
-
const request = await getPaymentRequestFromHeaders(mirror.headers);
|
|
101
|
-
|
|
102
|
-
const token = await opts.onPayment(server, blob.sha256, blob, request);
|
|
103
|
-
const payment = getEncodedToken(token);
|
|
104
|
-
|
|
105
|
-
// Try mirror with payment
|
|
106
|
-
mirror = await fetchWithTimeout(url, {
|
|
107
|
-
signal: opts?.signal,
|
|
108
|
-
method: "PUT",
|
|
109
|
-
body,
|
|
110
|
-
headers: { ...headers, "X-Cashu": payment },
|
|
111
|
-
timeout: opts?.timeout,
|
|
112
|
-
});
|
|
113
|
-
break;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
80
|
+
return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
|
|
81
|
+
},
|
|
82
|
+
payment: async (headers) => {
|
|
83
|
+
if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, blob.sha256, blob, headers);
|
|
84
|
+
if (opts?.onPayment) {
|
|
85
|
+
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
86
|
+
const request = await getPaymentRequestFromHeaders(headers);
|
|
87
|
+
return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, blob.sha256, blob, request)) };
|
|
88
|
+
}
|
|
89
|
+
throw new Error("Missing payment handler");
|
|
90
|
+
},
|
|
91
|
+
});
|
|
116
92
|
|
|
117
93
|
// handle errors
|
|
118
94
|
await HTTPError.handleErrorResponse(mirror);
|