blossom-client-sdk 2.0.0 → 2.1.1
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/lib/actions/delete.js +12 -12
- package/lib/actions/download.js +11 -11
- package/lib/actions/list.js +11 -11
- package/lib/actions/upload.js +8 -6
- package/lib/types.d.ts +1 -1
- package/package.json +3 -2
package/lib/actions/delete.js
CHANGED
|
@@ -7,6 +7,18 @@ export async function deleteBlob(server, hash, opts) {
|
|
|
7
7
|
let res = await fetch(url, { signal: opts?.signal, method: "DELETE" });
|
|
8
8
|
// handle auth and payment
|
|
9
9
|
switch (res.status) {
|
|
10
|
+
case 401: {
|
|
11
|
+
const auth = opts?.auth || (await opts?.onAuth?.(server, hash));
|
|
12
|
+
if (!auth)
|
|
13
|
+
throw new Error("Missing auth handler");
|
|
14
|
+
// Try delete with auth
|
|
15
|
+
res = await fetch(url, {
|
|
16
|
+
signal: opts?.signal,
|
|
17
|
+
method: "DELETE",
|
|
18
|
+
headers: { Authorization: encodeAuthorizationHeader(auth) },
|
|
19
|
+
});
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
10
22
|
case 402: {
|
|
11
23
|
if (!opts?.onPayment)
|
|
12
24
|
throw new Error("Missing payment handler");
|
|
@@ -22,18 +34,6 @@ export async function deleteBlob(server, hash, opts) {
|
|
|
22
34
|
});
|
|
23
35
|
break;
|
|
24
36
|
}
|
|
25
|
-
case 403: {
|
|
26
|
-
const auth = opts?.auth || (await opts?.onAuth?.(server, hash));
|
|
27
|
-
if (!auth)
|
|
28
|
-
throw new Error("Missing auth handler");
|
|
29
|
-
// Try delete with auth
|
|
30
|
-
res = await fetch(url, {
|
|
31
|
-
signal: opts?.signal,
|
|
32
|
-
method: "DELETE",
|
|
33
|
-
headers: { Authorization: encodeAuthorizationHeader(auth) },
|
|
34
|
-
});
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
37
|
}
|
|
38
38
|
// handle errors
|
|
39
39
|
await HTTPError.handleErrorResponse(res);
|
package/lib/actions/download.js
CHANGED
|
@@ -7,6 +7,17 @@ export async function downloadBlob(server, hash, opts) {
|
|
|
7
7
|
let download = await fetch(url, { signal: opts?.signal });
|
|
8
8
|
// handle auth and payment
|
|
9
9
|
switch (download.status) {
|
|
10
|
+
case 401: {
|
|
11
|
+
const auth = opts?.auth || (await opts?.onAuth?.(server, hash));
|
|
12
|
+
if (!auth)
|
|
13
|
+
throw new Error("Missing auth handler");
|
|
14
|
+
// Try download with auth
|
|
15
|
+
download = await fetch(url, {
|
|
16
|
+
signal: opts?.signal,
|
|
17
|
+
headers: { Authorization: encodeAuthorizationHeader(auth) },
|
|
18
|
+
});
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
10
21
|
case 402: {
|
|
11
22
|
if (!opts?.onPayment)
|
|
12
23
|
throw new Error("Missing payment handler");
|
|
@@ -21,17 +32,6 @@ export async function downloadBlob(server, hash, opts) {
|
|
|
21
32
|
});
|
|
22
33
|
break;
|
|
23
34
|
}
|
|
24
|
-
case 403: {
|
|
25
|
-
const auth = opts?.auth || (await opts?.onAuth?.(server, hash));
|
|
26
|
-
if (!auth)
|
|
27
|
-
throw new Error("Missing auth handler");
|
|
28
|
-
// Try download with auth
|
|
29
|
-
download = await fetch(url, {
|
|
30
|
-
signal: opts?.signal,
|
|
31
|
-
headers: { Authorization: encodeAuthorizationHeader(auth) },
|
|
32
|
-
});
|
|
33
|
-
break;
|
|
34
|
-
}
|
|
35
35
|
}
|
|
36
36
|
// check download errors
|
|
37
37
|
await HTTPError.handleErrorResponse(download);
|
package/lib/actions/list.js
CHANGED
|
@@ -11,6 +11,17 @@ export async function listBlobs(server, pubkey, opts) {
|
|
|
11
11
|
let list = await fetch(url, { signal: opts?.signal });
|
|
12
12
|
// handle auth and payments
|
|
13
13
|
switch (list.status) {
|
|
14
|
+
case 401: {
|
|
15
|
+
const auth = opts?.auth || (await opts?.onAuth?.(server));
|
|
16
|
+
if (!auth)
|
|
17
|
+
throw new Error("Missing auth handler");
|
|
18
|
+
// Try list with auth
|
|
19
|
+
list = await fetch(url, {
|
|
20
|
+
signal: opts?.signal,
|
|
21
|
+
headers: { Authorization: encodeAuthorizationHeader(auth) },
|
|
22
|
+
});
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
14
25
|
case 402: {
|
|
15
26
|
if (!opts?.onPayment)
|
|
16
27
|
throw new Error("Missing payment handler");
|
|
@@ -25,17 +36,6 @@ export async function listBlobs(server, pubkey, opts) {
|
|
|
25
36
|
});
|
|
26
37
|
break;
|
|
27
38
|
}
|
|
28
|
-
case 403: {
|
|
29
|
-
const auth = opts?.auth || (await opts?.onAuth?.(server));
|
|
30
|
-
if (!auth)
|
|
31
|
-
throw new Error("Missing auth handler");
|
|
32
|
-
// Try list with auth
|
|
33
|
-
list = await fetch(url, {
|
|
34
|
-
signal: opts?.signal,
|
|
35
|
-
headers: { Authorization: encodeAuthorizationHeader(auth) },
|
|
36
|
-
});
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
39
|
}
|
|
40
40
|
// handle errors
|
|
41
41
|
await HTTPError.handleErrorResponse(list);
|
package/lib/actions/upload.js
CHANGED
|
@@ -18,13 +18,17 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
18
18
|
if (type)
|
|
19
19
|
checkHeaders["X-Content-Type"] = type;
|
|
20
20
|
// check upload with HEAD /upload
|
|
21
|
-
let
|
|
21
|
+
let firstTry = await fetch(url, {
|
|
22
22
|
method: "HEAD",
|
|
23
23
|
signal: opts?.signal,
|
|
24
24
|
headers: checkHeaders,
|
|
25
25
|
});
|
|
26
26
|
let upload = undefined;
|
|
27
|
-
|
|
27
|
+
if (firstTry.status === 404) {
|
|
28
|
+
// BUD-06 HEAD endpoint is not supported. attempt to upload
|
|
29
|
+
upload = firstTry = await fetch(url, { body: blob, method: "PUT", signal: opts?.signal });
|
|
30
|
+
}
|
|
31
|
+
switch (firstTry.status) {
|
|
28
32
|
case 401: {
|
|
29
33
|
const auth = opts?.auth || (await opts?.onAuth?.(server, blob));
|
|
30
34
|
if (!auth)
|
|
@@ -42,7 +46,7 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
42
46
|
if (!opts?.onPayment)
|
|
43
47
|
throw new Error("Missing payment handler");
|
|
44
48
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
45
|
-
const request = getPaymentRequestFromHeaders(
|
|
49
|
+
const request = getPaymentRequestFromHeaders(firstTry.headers);
|
|
46
50
|
const token = await opts.onPayment(server, blob, request);
|
|
47
51
|
const payment = getEncodedToken(token);
|
|
48
52
|
// Try upload with payment
|
|
@@ -55,7 +59,7 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
55
59
|
break;
|
|
56
60
|
}
|
|
57
61
|
}
|
|
58
|
-
if (
|
|
62
|
+
if (firstTry.status >= 500)
|
|
59
63
|
throw new Error("Server error");
|
|
60
64
|
// check passed, upload
|
|
61
65
|
if (!upload)
|
|
@@ -67,8 +71,6 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
67
71
|
});
|
|
68
72
|
// handle errors
|
|
69
73
|
await HTTPError.handleErrorResponse(upload);
|
|
70
|
-
// check upload errors
|
|
71
|
-
await HTTPError.handleErrorResponse(upload);
|
|
72
74
|
// return blob descriptor
|
|
73
75
|
return upload.json();
|
|
74
76
|
}
|
package/lib/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blossom-client-sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "A simple client for manage blobs on blossom servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
|
+
"module": "./lib/index.js",
|
|
7
8
|
"typings": "./lib/index.d.ts",
|
|
8
9
|
"author": "hzrd149",
|
|
9
10
|
"license": "MIT",
|
|
@@ -48,7 +49,7 @@
|
|
|
48
49
|
"node": ">=18"
|
|
49
50
|
},
|
|
50
51
|
"dependencies": {
|
|
51
|
-
"@cashu/cashu-ts": "2.
|
|
52
|
+
"@cashu/cashu-ts": "^2.1.0",
|
|
52
53
|
"@noble/hashes": "^1.4.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|