blossom-client-sdk 5.1.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.js +14 -39
- package/lib/actions/download.js +14 -36
- package/lib/actions/has.d.ts +1 -1
- package/lib/actions/has.js +6 -2
- package/lib/actions/list.js +15 -32
- package/lib/actions/media.js +25 -46
- package/lib/actions/mirror.js +14 -41
- package/lib/actions/multi-server.js +13 -0
- package/lib/actions/resolve.js +5 -0
- package/lib/actions/upload.js +27 -48
- package/lib/helpers/blob.js +4 -3
- 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 -17
- package/src/actions/delete.ts +16 -42
- package/src/actions/download.ts +16 -41
- package/src/actions/has.ts +5 -2
- package/src/actions/list.ts +17 -35
- package/src/actions/media.ts +26 -49
- package/src/actions/mirror.ts +16 -45
- package/src/actions/multi-server.ts +13 -0
- package/src/actions/resolve.ts +5 -0
- package/src/actions/upload.ts +28 -50
- package/src/helpers/blob.ts +5 -4
- 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/lib/actions/resolve.js
CHANGED
|
@@ -66,11 +66,13 @@ export async function getBlobUrls(uri, opts) {
|
|
|
66
66
|
}
|
|
67
67
|
/** Resolves a blossom URI to a downloaded blob by trying servers from hints sequentially */
|
|
68
68
|
export async function resolveBlob(uri, opts) {
|
|
69
|
+
opts?.signal?.throwIfAborted();
|
|
69
70
|
const parsed = parseInput(uri);
|
|
70
71
|
const collector = createServerCollector(buildBlobPath(parsed.sha256, parsed.ext));
|
|
71
72
|
/** Try GET on each URL, return the first successful response */
|
|
72
73
|
const tryUrls = async (urls) => {
|
|
73
74
|
for (const url of urls) {
|
|
75
|
+
opts?.signal?.throwIfAborted();
|
|
74
76
|
try {
|
|
75
77
|
const res = await fetchWithTimeout(new URL(url), {
|
|
76
78
|
signal: opts?.signal,
|
|
@@ -80,6 +82,7 @@ export async function resolveBlob(uri, opts) {
|
|
|
80
82
|
return res;
|
|
81
83
|
}
|
|
82
84
|
catch {
|
|
85
|
+
opts?.signal?.throwIfAborted();
|
|
83
86
|
// server unreachable or error — try next
|
|
84
87
|
continue;
|
|
85
88
|
}
|
|
@@ -96,7 +99,9 @@ export async function resolveBlob(uri, opts) {
|
|
|
96
99
|
// 2. resolve and try author hints (deferred — requires relay queries)
|
|
97
100
|
if (opts?.getServers) {
|
|
98
101
|
for (const pubkey of parsed.authors) {
|
|
102
|
+
opts?.signal?.throwIfAborted();
|
|
99
103
|
const authorServers = await opts.getServers(pubkey);
|
|
104
|
+
opts?.signal?.throwIfAborted();
|
|
100
105
|
if (authorServers) {
|
|
101
106
|
const urls = collector.add(authorServers);
|
|
102
107
|
if (urls.length > 0) {
|
package/lib/actions/upload.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { encodeAuthorizationHeader, getReusableAuthEvent, storeAuthEvent } from "../auth.js";
|
|
2
2
|
import HTTPError from "../error.js";
|
|
3
3
|
import { fetchWithTimeout, getBlobSha256, getBlobSize, getBlobType } from "../helpers/index.js";
|
|
4
|
-
import {
|
|
4
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
5
|
+
import { toWebBytes } from "../internal/bytes.js";
|
|
5
6
|
/** Upload a blob to a server, handles payment and auth */
|
|
6
7
|
export async function uploadBlob(server, blob, opts) {
|
|
7
8
|
const url = new URL("/upload", server);
|
|
8
9
|
const sha256 = await getBlobSha256(blob);
|
|
10
|
+
const body = blob instanceof Blob ? blob : toWebBytes(blob);
|
|
9
11
|
const resolveAuth = async (preset = false) => {
|
|
10
12
|
const reused = opts?.authEvents
|
|
11
13
|
? await getReusableAuthEvent(opts.authEvents, { server, type: "upload", blob: sha256 })
|
|
@@ -47,66 +49,43 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
47
49
|
timeout: opts?.timeout,
|
|
48
50
|
});
|
|
49
51
|
let upload = undefined;
|
|
52
|
+
if (![401, 402, 404].includes(firstTry.status))
|
|
53
|
+
await HTTPError.handleErrorResponse(firstTry);
|
|
50
54
|
if (firstTry.status === 404) {
|
|
51
55
|
// BUD-06 HEAD endpoint is not supported. attempt to upload
|
|
52
56
|
upload = firstTry = await fetchWithTimeout(url, {
|
|
53
|
-
body
|
|
57
|
+
body,
|
|
54
58
|
method: "PUT",
|
|
59
|
+
headers,
|
|
55
60
|
signal: opts?.signal,
|
|
56
61
|
timeout: opts?.timeout,
|
|
57
62
|
});
|
|
58
63
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
const request = (headers) => fetchWithTimeout(url, {
|
|
65
|
+
method: "PUT",
|
|
66
|
+
body,
|
|
67
|
+
headers,
|
|
68
|
+
signal: opts?.signal,
|
|
69
|
+
timeout: opts?.timeout,
|
|
70
|
+
});
|
|
71
|
+
upload = await handleChallenges(upload ?? (firstTry.ok ? await request(headers) : firstTry), headers, request, {
|
|
72
|
+
signal: opts?.signal,
|
|
73
|
+
auth: async () => {
|
|
63
74
|
if (opts?.auth === false)
|
|
64
75
|
throw new Error("Authorization disabled");
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
upload = await fetchWithTimeout(url, {
|
|
69
|
-
method: "PUT",
|
|
70
|
-
body: blob,
|
|
71
|
-
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
72
|
-
signal: opts?.signal,
|
|
73
|
-
timeout: opts?.timeout,
|
|
74
|
-
});
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
case 402: {
|
|
78
|
-
let paymentHeaders;
|
|
76
|
+
return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
|
|
77
|
+
},
|
|
78
|
+
payment: async (headers) => {
|
|
79
79
|
if (opts?.onPaymentRequired)
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
return opts.onPaymentRequired(server, sha256, blob, headers);
|
|
81
|
+
if (opts?.onPayment) {
|
|
82
82
|
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
83
|
-
const request = await getPaymentRequestFromHeaders(
|
|
84
|
-
|
|
83
|
+
const request = await getPaymentRequestFromHeaders(headers);
|
|
84
|
+
return { "X-Cashu": encodePaymentToken(await opts.onPayment(server, sha256, blob, request)) };
|
|
85
85
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
upload = await fetchWithTimeout(url, {
|
|
90
|
-
method: "PUT",
|
|
91
|
-
body: blob,
|
|
92
|
-
headers: mergeHeaders(headers, paymentHeaders),
|
|
93
|
-
signal: opts?.signal,
|
|
94
|
-
timeout: opts?.timeout,
|
|
95
|
-
});
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
if (firstTry.status >= 500)
|
|
100
|
-
throw new Error("Server error");
|
|
101
|
-
// check passed, upload
|
|
102
|
-
if (!upload)
|
|
103
|
-
upload = await fetchWithTimeout(url, {
|
|
104
|
-
method: "PUT",
|
|
105
|
-
body: blob,
|
|
106
|
-
headers: { ...headers },
|
|
107
|
-
signal: opts?.signal,
|
|
108
|
-
timeout: opts?.timeout,
|
|
109
|
-
});
|
|
86
|
+
throw new Error("Missing payment handler");
|
|
87
|
+
},
|
|
88
|
+
});
|
|
110
89
|
// handle errors
|
|
111
90
|
await HTTPError.handleErrorResponse(upload);
|
|
112
91
|
// return blob descriptor
|
package/lib/helpers/blob.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { bytesToHex } from "@noble/hashes/utils";
|
|
1
|
+
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
2
|
+
import { toWebBytes } from "../internal/bytes.js";
|
|
2
3
|
/** Checks if a string is a 64 length hex string */
|
|
3
4
|
export function isSha256(str) {
|
|
4
5
|
return str.match(/^[0-9a-f]{64}$/);
|
|
@@ -23,7 +24,7 @@ export async function computeBlobSha256(blob) {
|
|
|
23
24
|
}
|
|
24
25
|
else {
|
|
25
26
|
// nodejs Buffer
|
|
26
|
-
buffer = blob;
|
|
27
|
+
buffer = toWebBytes(blob);
|
|
27
28
|
}
|
|
28
29
|
let hash;
|
|
29
30
|
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
@@ -31,7 +32,7 @@ export async function computeBlobSha256(blob) {
|
|
|
31
32
|
hash = new Uint8Array(hashBuffer);
|
|
32
33
|
}
|
|
33
34
|
else {
|
|
34
|
-
const { sha256 } = await import("@noble/hashes/sha2");
|
|
35
|
+
const { sha256 } = await import("@noble/hashes/sha2.js");
|
|
35
36
|
hash = sha256.create().update(new Uint8Array(buffer)).digest();
|
|
36
37
|
}
|
|
37
38
|
return bytesToHex(hash);
|
package/lib/helpers/signal.d.ts
CHANGED
package/lib/helpers/signal.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
export class TimeoutError extends Error {
|
|
2
|
+
name = "TimeoutError";
|
|
2
3
|
}
|
|
3
4
|
/** Creates and optionally wraps an AbortSignal with a timeout */
|
|
4
5
|
export function wrapSignalWithTimeout(timeout, message, signal) {
|
|
5
6
|
const controller = new AbortController();
|
|
6
|
-
if (signal)
|
|
7
|
-
signal.addEventListener("abort", (err) => controller.abort(err), { once: true });
|
|
8
7
|
const t = setTimeout(() => {
|
|
9
8
|
controller.abort(new TimeoutError(message));
|
|
10
9
|
}, timeout);
|
|
11
10
|
const cancel = () => clearTimeout(t);
|
|
12
|
-
|
|
11
|
+
// Native composition preserves existing abort reasons and avoids retaining listeners
|
|
12
|
+
// on a caller's long-lived signal. It also keeps cancellation working after headers arrive.
|
|
13
|
+
return { cancel, signal: signal ? AbortSignal.any([signal, controller.signal]) : controller.signal };
|
|
13
14
|
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Handle each challenge at most once, including challenges from a preflight response. */
|
|
2
|
+
export declare function handleChallenges(response: Response, headers: HeadersInit, request: (headers: HeadersInit) => Promise<Response>, handlers: {
|
|
3
|
+
auth: () => Promise<HeadersInit>;
|
|
4
|
+
payment: (headers: Headers) => Promise<HeadersInit>;
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
}): Promise<Response>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { mergeHeaders } from "../helpers/headers.js";
|
|
2
|
+
/** Handle each challenge at most once, including challenges from a preflight response. */
|
|
3
|
+
export async function handleChallenges(response, headers, request, handlers) {
|
|
4
|
+
const handled = new Set();
|
|
5
|
+
while ((response.status === 401 || response.status === 402) && !handled.has(response.status)) {
|
|
6
|
+
handlers.signal?.throwIfAborted();
|
|
7
|
+
handled.add(response.status);
|
|
8
|
+
const nextHeaders = response.status === 401 ? await handlers.auth() : await handlers.payment(response.headers);
|
|
9
|
+
handlers.signal?.throwIfAborted();
|
|
10
|
+
headers = mergeHeaders(headers, nextHeaders);
|
|
11
|
+
await response.body?.cancel();
|
|
12
|
+
response = await request(headers);
|
|
13
|
+
}
|
|
14
|
+
return response;
|
|
15
|
+
}
|
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,10 +51,10 @@
|
|
|
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
60
|
"hls.js": "^1.6.16"
|
|
@@ -65,26 +65,30 @@
|
|
|
65
65
|
}
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
|
-
"@cashu/cashu-ts": "^2.
|
|
69
|
-
"@
|
|
70
|
-
"@
|
|
71
|
-
"@
|
|
72
|
-
"@vitest/
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"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",
|
|
78
77
|
"typedoc": "^0.25.13",
|
|
79
78
|
"typedoc-plugin-mermaid": "^1.12.0",
|
|
80
|
-
"typescript": "^5.
|
|
81
|
-
"vitest": "^3.
|
|
79
|
+
"typescript": "^5.9.3",
|
|
80
|
+
"vitest": "^3.2.7",
|
|
82
81
|
"vitest-fetch-mock": "^0.4.5"
|
|
83
82
|
},
|
|
84
83
|
"scripts": {
|
|
85
84
|
"build": "tsc",
|
|
86
85
|
"prepublish": "npm run build",
|
|
87
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",
|
|
88
92
|
"coverage": "vitest run --coverage",
|
|
89
93
|
"format": "prettier -w .",
|
|
90
94
|
"docs": "typedoc src/index.ts"
|
package/src/actions/delete.ts
CHANGED
|
@@ -2,7 +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 {
|
|
5
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
6
6
|
import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
7
7
|
|
|
8
8
|
export type DeleteOptions<S extends ServerType> = {
|
|
@@ -59,50 +59,24 @@ export async function deleteBlob<S extends ServerType>(server: S, hash: string,
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
method: "DELETE",
|
|
64
|
-
|
|
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, {
|
|
65
65
|
signal: opts?.signal,
|
|
66
|
-
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
// handle auth and payment
|
|
70
|
-
switch (res.status) {
|
|
71
|
-
case 401: {
|
|
72
|
-
// throw an error if authorization is required but disabled
|
|
66
|
+
auth: async () => {
|
|
73
67
|
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
res = await fetchWithTimeout(url, {
|
|
80
|
-
signal: opts?.signal,
|
|
81
|
-
method: "DELETE",
|
|
82
|
-
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
83
|
-
timeout: opts?.timeout,
|
|
84
|
-
});
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case 402: {
|
|
88
|
-
let paymentHeaders: HeadersInit;
|
|
89
|
-
if (opts?.onPaymentRequired) paymentHeaders = await opts.onPaymentRequired(server, hash, res.headers);
|
|
90
|
-
else if (opts?.onPayment) {
|
|
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) {
|
|
91
73
|
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
92
|
-
const request = await getPaymentRequestFromHeaders(
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
signal: opts?.signal,
|
|
99
|
-
method: "DELETE",
|
|
100
|
-
headers: mergeHeaders(headers, paymentHeaders),
|
|
101
|
-
timeout: opts?.timeout,
|
|
102
|
-
});
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
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
|
+
});
|
|
106
80
|
|
|
107
81
|
// handle errors
|
|
108
82
|
await HTTPError.handleErrorResponse(res);
|
package/src/actions/download.ts
CHANGED
|
@@ -3,7 +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 {
|
|
6
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
7
7
|
|
|
8
8
|
export type DownloadOptions<S extends ServerType> = {
|
|
9
9
|
/** AbortSignal to cancel the action */
|
|
@@ -59,49 +59,24 @@ export async function downloadBlob<S extends ServerType>(server: S, hash: string
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
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, {
|
|
64
65
|
signal: opts?.signal,
|
|
65
|
-
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// handle auth and payment
|
|
69
|
-
switch (download.status) {
|
|
70
|
-
case 401: {
|
|
71
|
-
// throw an error if auth is requested and disabled
|
|
66
|
+
auth: async () => {
|
|
72
67
|
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
download = await fetchWithTimeout(url, {
|
|
79
|
-
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
80
|
-
signal: opts?.signal,
|
|
81
|
-
timeout: opts?.timeout,
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
case 402: {
|
|
87
|
-
let paymentHeaders: HeadersInit;
|
|
88
|
-
if (opts?.onPaymentRequired) paymentHeaders = await opts.onPaymentRequired(server, hash, download.headers);
|
|
89
|
-
else if (opts?.onPayment) {
|
|
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) {
|
|
90
73
|
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
91
|
-
const request = await getPaymentRequestFromHeaders(
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
headers: mergeHeaders(headers, paymentHeaders),
|
|
98
|
-
signal: opts?.signal,
|
|
99
|
-
timeout: opts?.timeout,
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
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
|
+
});
|
|
105
80
|
|
|
106
81
|
// check download errors
|
|
107
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,7 +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 {
|
|
6
|
+
import { handleChallenges } from "../internal/challenges.js";
|
|
7
7
|
|
|
8
8
|
export type ListOptions<S extends ServerType> = {
|
|
9
9
|
/** AbortSignal to cancel the action */
|
|
@@ -68,42 +68,24 @@ export async function listBlobs<S extends ServerType>(
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
// 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 () => {
|
|
77
76
|
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
84
|
-
signal: opts?.signal,
|
|
85
|
-
timeout: opts?.timeout,
|
|
86
|
-
});
|
|
87
|
-
break;
|
|
88
|
-
}
|
|
89
|
-
case 402: {
|
|
90
|
-
let paymentHeaders: HeadersInit;
|
|
91
|
-
if (opts?.onPaymentRequired) paymentHeaders = await opts.onPaymentRequired(server, list.headers);
|
|
92
|
-
else if (opts?.onPayment) {
|
|
77
|
+
return { Authorization: encodeAuthorizationHeader(await resolveAuth()) };
|
|
78
|
+
},
|
|
79
|
+
payment: async (headers) => {
|
|
80
|
+
if (opts?.onPaymentRequired) return opts.onPaymentRequired(server, headers);
|
|
81
|
+
if (opts?.onPayment) {
|
|
93
82
|
const { encodePaymentToken, getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
94
|
-
const request = await getPaymentRequestFromHeaders(
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
headers: mergeHeaders(headers, paymentHeaders),
|
|
101
|
-
signal: opts?.signal,
|
|
102
|
-
timeout: opts?.timeout,
|
|
103
|
-
});
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
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
|
+
});
|
|
107
89
|
|
|
108
90
|
// handle errors
|
|
109
91
|
await HTTPError.handleErrorResponse(list);
|