blossom-client-sdk 3.0.1 → 4.0.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/lib/actions/delete.d.ts +2 -3
- package/lib/actions/delete.js +2 -2
- package/lib/actions/download.d.ts +2 -3
- package/lib/actions/download.js +2 -1
- package/lib/actions/list.d.ts +2 -3
- package/lib/actions/list.js +9 -4
- package/lib/actions/media.js +2 -1
- package/lib/actions/mirror.d.ts +2 -3
- package/lib/actions/mirror.js +3 -2
- package/lib/actions/multi-server.js +1 -1
- package/lib/actions/upload.d.ts +2 -3
- package/lib/actions/upload.js +3 -2
- package/lib/auth.d.ts +1 -0
- package/lib/auth.js +13 -5
- package/lib/helpers/blob.d.ts +2 -0
- package/lib/helpers/blob.js +8 -5
- package/lib/helpers/cashu.js +14 -0
- package/lib/helpers/index.d.ts +1 -1
- package/lib/helpers/index.js +1 -1
- package/lib/helpers/signal.js +1 -1
- package/lib/image.js +25 -11
- package/lib/types.d.ts +20 -4
- package/package.json +18 -17
- package/src/actions/delete.ts +85 -0
- package/src/actions/download.ts +84 -0
- package/src/actions/index.ts +7 -0
- package/src/actions/list.ts +83 -0
- package/src/actions/media.ts +102 -0
- package/src/actions/mirror.ts +99 -0
- package/src/actions/multi-server.ts +207 -0
- package/src/actions/upload.ts +129 -0
- package/src/auth.ts +137 -0
- package/src/client.ts +221 -0
- package/src/const.ts +1 -0
- package/src/error.ts +23 -0
- package/src/helpers/blob.ts +62 -0
- package/src/helpers/cashu.ts +21 -0
- package/src/helpers/fetch.ts +16 -0
- package/src/helpers/index.ts +5 -0
- package/src/helpers/signal.ts +20 -0
- package/src/helpers/url.ts +16 -0
- package/src/image.ts +122 -0
- package/src/index.ts +9 -0
- package/src/nostr.ts +17 -0
- package/src/types.ts +63 -0
- package/lib/helpers/cachu.js +0 -6
- /package/lib/helpers/{cachu.d.ts → cashu.d.ts} +0 -0
package/lib/actions/delete.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType } from "../client.js";
|
|
3
|
-
import { PaymentRequest, SignedEvent } from "../types.js";
|
|
2
|
+
import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type DeleteOptions<S extends ServerType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
@@ -14,7 +13,7 @@ export type DeleteOptions<S extends ServerType> = {
|
|
|
14
13
|
* @param sha256 the sha256 of the blob being uploaded or mirrored
|
|
15
14
|
* @param request the payment request
|
|
16
15
|
*/
|
|
17
|
-
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<
|
|
16
|
+
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<PaymentToken>;
|
|
18
17
|
/**
|
|
19
18
|
* A method used to request a signed auth event for a server and sha256
|
|
20
19
|
* @param server the server requesting the auth
|
package/lib/actions/delete.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import HTTPError from "../error.js";
|
|
2
1
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
3
|
-
import
|
|
2
|
+
import HTTPError from "../error.js";
|
|
4
3
|
import { fetchWithTimeout } from "../helpers/fetch.js";
|
|
5
4
|
/** Deletes a blob to a server */
|
|
6
5
|
export async function deleteBlob(server, hash, opts) {
|
|
@@ -34,6 +33,7 @@ export async function deleteBlob(server, hash, opts) {
|
|
|
34
33
|
if (!opts?.onPayment)
|
|
35
34
|
throw new Error("Missing payment handler");
|
|
36
35
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
36
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
37
37
|
const request = getPaymentRequestFromHeaders(res.headers);
|
|
38
38
|
const token = await opts.onPayment(server, hash, request);
|
|
39
39
|
const payment = getEncodedToken(token);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType } from "../client.js";
|
|
3
|
-
import { PaymentRequest, SignedEvent } from "../types.js";
|
|
2
|
+
import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type DownloadOptions<S extends ServerType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
@@ -14,7 +13,7 @@ export type DownloadOptions<S extends ServerType> = {
|
|
|
14
13
|
* @param sha256 the sha256 of the blob being uploaded or mirrored
|
|
15
14
|
* @param request the payment request
|
|
16
15
|
*/
|
|
17
|
-
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<
|
|
16
|
+
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<PaymentToken>;
|
|
18
17
|
/**
|
|
19
18
|
* A method used to request a signed auth event for a server and sha256
|
|
20
19
|
* @param server the server requesting the auth
|
package/lib/actions/download.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import HTTPError from "../error.js";
|
|
2
2
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
3
|
-
import { fetchWithTimeout
|
|
3
|
+
import { fetchWithTimeout } from "../helpers/index.js";
|
|
4
4
|
/** Downloads a blob from a server and returns the Response */
|
|
5
5
|
export async function downloadBlob(server, hash, opts) {
|
|
6
6
|
const url = new URL("/" + hash, server);
|
|
@@ -31,6 +31,7 @@ export async function downloadBlob(server, hash, opts) {
|
|
|
31
31
|
if (!opts?.onPayment)
|
|
32
32
|
throw new Error("Missing payment handler");
|
|
33
33
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
34
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
34
35
|
const request = getPaymentRequestFromHeaders(download.headers);
|
|
35
36
|
const token = await opts.onPayment(server, hash, request);
|
|
36
37
|
const payment = getEncodedToken(token);
|
package/lib/actions/list.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType } from "../client.js";
|
|
3
|
-
import { BlobDescriptor, PaymentRequest, SignedEvent } from "../types.js";
|
|
2
|
+
import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type ListOptions<S extends ServerType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
@@ -15,7 +14,7 @@ export type ListOptions<S extends ServerType> = {
|
|
|
15
14
|
* @param server the server requiring payment
|
|
16
15
|
* @param request the payment request
|
|
17
16
|
*/
|
|
18
|
-
onPayment?: (server: S, request: PaymentRequest) => Promise<
|
|
17
|
+
onPayment?: (server: S, request: PaymentRequest) => Promise<PaymentToken>;
|
|
19
18
|
/**
|
|
20
19
|
* A method used to request a signed auth event for a server
|
|
21
20
|
* @param server the server requesting the auth
|
package/lib/actions/list.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import HTTPError from "../error.js";
|
|
2
2
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
3
|
-
import { fetchWithTimeout
|
|
3
|
+
import { fetchWithTimeout } from "../helpers/index.js";
|
|
4
4
|
/** Mirrors a blob to a server */
|
|
5
5
|
export async function listBlobs(server, pubkey, opts) {
|
|
6
6
|
const url = new URL(`/list/` + pubkey, server);
|
|
@@ -8,7 +8,11 @@ export async function listBlobs(server, pubkey, opts) {
|
|
|
8
8
|
url.searchParams.append("since", String(opts.since));
|
|
9
9
|
if (opts?.until)
|
|
10
10
|
url.searchParams.append("until", String(opts.until));
|
|
11
|
-
|
|
11
|
+
// attach the auth if its already set
|
|
12
|
+
const headers = {};
|
|
13
|
+
if (opts?.auth)
|
|
14
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
15
|
+
let list = await fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
|
|
12
16
|
// handle auth and payments
|
|
13
17
|
switch (list.status) {
|
|
14
18
|
case 401: {
|
|
@@ -17,7 +21,7 @@ export async function listBlobs(server, pubkey, opts) {
|
|
|
17
21
|
throw new Error("Missing auth handler");
|
|
18
22
|
// Try list with auth
|
|
19
23
|
list = await fetchWithTimeout(url, {
|
|
20
|
-
headers: { Authorization: encodeAuthorizationHeader(auth) },
|
|
24
|
+
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
21
25
|
signal: opts?.signal,
|
|
22
26
|
timeout: opts?.timeout,
|
|
23
27
|
});
|
|
@@ -27,12 +31,13 @@ export async function listBlobs(server, pubkey, opts) {
|
|
|
27
31
|
if (!opts?.onPayment)
|
|
28
32
|
throw new Error("Missing payment handler");
|
|
29
33
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
34
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
30
35
|
const request = getPaymentRequestFromHeaders(list.headers);
|
|
31
36
|
const token = await opts.onPayment(server, request);
|
|
32
37
|
const payment = getEncodedToken(token);
|
|
33
38
|
// Try list with payment
|
|
34
39
|
list = await fetchWithTimeout(url, {
|
|
35
|
-
headers: { "X-Cashu": payment },
|
|
40
|
+
headers: { ...headers, "X-Cashu": payment },
|
|
36
41
|
signal: opts?.signal,
|
|
37
42
|
timeout: opts?.timeout,
|
|
38
43
|
});
|
package/lib/actions/media.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getBlobSha256, getBlobSize, getBlobType
|
|
1
|
+
import { getBlobSha256, getBlobSize, getBlobType } from "../helpers/index.js";
|
|
2
2
|
import HTTPError from "../error.js";
|
|
3
3
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
4
4
|
/** Error thrown when /media endpoint is not present on a server */
|
|
@@ -52,6 +52,7 @@ export async function uploadMedia(server, blob, opts) {
|
|
|
52
52
|
if (!opts?.onPayment)
|
|
53
53
|
throw new Error("Missing payment handler");
|
|
54
54
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
55
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
55
56
|
const request = getPaymentRequestFromHeaders(firstTry.headers);
|
|
56
57
|
const token = await opts.onPayment(server, sha256, blob, request);
|
|
57
58
|
const payment = getEncodedToken(token);
|
package/lib/actions/mirror.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType } from "../client.js";
|
|
3
|
-
import { BlobDescriptor,
|
|
2
|
+
import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type MirrorOptions<S extends ServerType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
@@ -15,7 +14,7 @@ export type MirrorOptions<S extends ServerType> = {
|
|
|
15
14
|
* @param blob the original blob
|
|
16
15
|
* @param request the payment request
|
|
17
16
|
*/
|
|
18
|
-
onPayment?: (server: S, sha256: string, blob: BlobDescriptor, request: PaymentRequest) => Promise<
|
|
17
|
+
onPayment?: (server: S, sha256: string, blob: BlobDescriptor, request: PaymentRequest) => Promise<PaymentToken>;
|
|
19
18
|
/**
|
|
20
19
|
* A method used to request a signed auth event for a server and sha256
|
|
21
20
|
* @param server the server requesting the auth
|
package/lib/actions/mirror.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import HTTPError from "../error.js";
|
|
2
1
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
3
|
-
import
|
|
2
|
+
import HTTPError from "../error.js";
|
|
3
|
+
import { fetchWithTimeout } from "../helpers/index.js";
|
|
4
4
|
/** Mirrors a blob to a server */
|
|
5
5
|
export async function mirrorBlob(server, blob, opts) {
|
|
6
6
|
const url = new URL("/mirror", server);
|
|
@@ -41,6 +41,7 @@ export async function mirrorBlob(server, blob, opts) {
|
|
|
41
41
|
if (!opts?.onPayment)
|
|
42
42
|
throw new Error("Missing payment handler");
|
|
43
43
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
44
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
44
45
|
const request = getPaymentRequestFromHeaders(mirror.headers);
|
|
45
46
|
const token = await opts.onPayment(server, blob.sha256, blob, request);
|
|
46
47
|
const payment = getEncodedToken(token);
|
|
@@ -125,7 +125,7 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
125
125
|
continue;
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
-
// attempt to upload
|
|
128
|
+
// attempt to upload if first upload or mirror failed
|
|
129
129
|
if (!metadata) {
|
|
130
130
|
const sha256 = await getBlobSha256(blob);
|
|
131
131
|
options.onStart?.(server, sha256, blob);
|
package/lib/actions/upload.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { type Token } from "@cashu/cashu-ts";
|
|
2
1
|
import { ServerType, UploadType } from "../client.js";
|
|
3
|
-
import { BlobDescriptor, PaymentRequest, SignedEvent } from "../types.js";
|
|
2
|
+
import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
4
3
|
export type UploadOptions<S extends ServerType, B extends UploadType> = {
|
|
5
4
|
/** AbortSignal to cancel the action */
|
|
6
5
|
signal?: AbortSignal;
|
|
@@ -15,7 +14,7 @@ export type UploadOptions<S extends ServerType, B extends UploadType> = {
|
|
|
15
14
|
* @param blob the original blob
|
|
16
15
|
* @param request the payment request
|
|
17
16
|
*/
|
|
18
|
-
onPayment?: (server: S, sha256: string, blob: B, request: PaymentRequest) => Promise<
|
|
17
|
+
onPayment?: (server: S, sha256: string, blob: B, request: PaymentRequest) => Promise<PaymentToken>;
|
|
19
18
|
/**
|
|
20
19
|
* A method used to request a signed auth event for a server and sha256
|
|
21
20
|
* @param server the server requesting the auth
|
package/lib/actions/upload.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { fetchWithTimeout, getBlobSha256, getBlobSize, getBlobType, getPaymentRequestFromHeaders, } from "../helpers/index.js";
|
|
2
|
-
import HTTPError from "../error.js";
|
|
3
1
|
import { encodeAuthorizationHeader } from "../auth.js";
|
|
2
|
+
import HTTPError from "../error.js";
|
|
3
|
+
import { fetchWithTimeout, getBlobSha256, getBlobSize, getBlobType } from "../helpers/index.js";
|
|
4
4
|
/** Upload a blob to a server, handles payment and auth */
|
|
5
5
|
export async function uploadBlob(server, blob, opts) {
|
|
6
6
|
const url = new URL("/upload", server);
|
|
@@ -56,6 +56,7 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
56
56
|
if (!opts?.onPayment)
|
|
57
57
|
throw new Error("Missing payment handler");
|
|
58
58
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
59
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
59
60
|
const request = getPaymentRequestFromHeaders(firstTry.headers);
|
|
60
61
|
const token = await opts.onPayment(server, sha256, blob, request);
|
|
61
62
|
const payment = getEncodedToken(token);
|
package/lib/auth.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { ServerType, UploadType } from "./client.js";
|
|
|
2
2
|
import { SignedEvent, Signer } from "./types.js";
|
|
3
3
|
export declare const now: () => number;
|
|
4
4
|
export declare const oneHour: () => number;
|
|
5
|
+
/** Encodes an auth event into a nostr authorization header */
|
|
5
6
|
export declare function encodeAuthorizationHeader(event: SignedEvent): string;
|
|
6
7
|
/** Checks if an auth event matches a server / blob upload */
|
|
7
8
|
export declare function doseAuthMatchUpload(auth: SignedEvent, server: ServerType, blob: string | UploadType): Promise<boolean>;
|
package/lib/auth.js
CHANGED
|
@@ -3,6 +3,7 @@ import { getBlobSha256, isSha256 } from "./helpers/blob.js";
|
|
|
3
3
|
import { areServersEqual } from "./helpers/url.js";
|
|
4
4
|
export const now = () => Math.floor(new Date().valueOf() / 1000);
|
|
5
5
|
export const oneHour = () => now() + 60 * 60;
|
|
6
|
+
/** Encodes an auth event into a nostr authorization header */
|
|
6
7
|
export function encodeAuthorizationHeader(event) {
|
|
7
8
|
return "Nostr " + btoa(JSON.stringify(event));
|
|
8
9
|
}
|
|
@@ -43,17 +44,24 @@ export async function createAuthEvent(signer, type, options) {
|
|
|
43
44
|
};
|
|
44
45
|
// add blob tags
|
|
45
46
|
if (options?.blobs) {
|
|
46
|
-
if (Array.isArray(options.blobs))
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
if (Array.isArray(options.blobs)) {
|
|
48
|
+
const seen = new Set();
|
|
49
|
+
for (const blob of options.blobs) {
|
|
50
|
+
const hash = await normalizeToHash(blob);
|
|
51
|
+
if (!seen.has(hash)) {
|
|
52
|
+
draft.tags.push(["x", hash]);
|
|
53
|
+
seen.add(hash);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
49
57
|
else
|
|
50
58
|
draft.tags.push(["x", await normalizeToHash(options.blobs)]);
|
|
51
59
|
}
|
|
52
60
|
// add server tags
|
|
53
61
|
if (options?.servers) {
|
|
54
62
|
if (Array.isArray(options.servers))
|
|
55
|
-
for (const
|
|
56
|
-
draft.tags.push(["server",
|
|
63
|
+
for (const server of new Set(options.servers))
|
|
64
|
+
draft.tags.push(["server", server]);
|
|
57
65
|
else
|
|
58
66
|
draft.tags.push(["server", options.servers]);
|
|
59
67
|
}
|
package/lib/helpers/blob.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { UploadType } from "../client.js";
|
|
2
|
+
/** Checks if a string is a 64 length hex string */
|
|
2
3
|
export declare function isSha256(str: string): RegExpMatchArray | null;
|
|
4
|
+
/** A symbol use to store the sha256 hash of a blob on a Blob instance */
|
|
3
5
|
export declare const BlobHashSymbol: unique symbol;
|
|
4
6
|
/** gets or calculates the sha2456 of a Blob */
|
|
5
7
|
export declare function getBlobSha256(blob: UploadType): string | Promise<string>;
|
package/lib/helpers/blob.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { bytesToHex } from "@noble/hashes/utils";
|
|
2
|
+
/** Checks if a string is a 64 length hex string */
|
|
2
3
|
export function isSha256(str) {
|
|
3
4
|
return str.match(/^[0-9a-f]{64}$/);
|
|
4
5
|
}
|
|
6
|
+
/** A symbol use to store the sha256 hash of a blob on a Blob instance */
|
|
5
7
|
export const BlobHashSymbol = Symbol.for("sha256");
|
|
6
8
|
/** gets or calculates the sha2456 of a Blob */
|
|
7
9
|
export function getBlobSha256(blob) {
|
|
@@ -15,7 +17,8 @@ export function getBlobSha256(blob) {
|
|
|
15
17
|
/** Calculates the sha2456 of a Blob */
|
|
16
18
|
export async function computeBlobSha256(blob) {
|
|
17
19
|
let buffer;
|
|
18
|
-
|
|
20
|
+
// NOTE: use typeof File !== "undefined" to check if File is supported (required to support node 18)
|
|
21
|
+
if ((typeof File !== "undefined" && blob instanceof File) || blob instanceof Blob) {
|
|
19
22
|
buffer = await blob.arrayBuffer();
|
|
20
23
|
}
|
|
21
24
|
else {
|
|
@@ -23,19 +26,19 @@ export async function computeBlobSha256(blob) {
|
|
|
23
26
|
buffer = blob;
|
|
24
27
|
}
|
|
25
28
|
let hash;
|
|
26
|
-
if (crypto.subtle) {
|
|
29
|
+
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
27
30
|
const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
|
|
28
31
|
hash = new Uint8Array(hashBuffer);
|
|
29
32
|
}
|
|
30
33
|
else {
|
|
31
|
-
const { sha256 } = await import("@noble/hashes/
|
|
34
|
+
const { sha256 } = await import("@noble/hashes/sha2");
|
|
32
35
|
hash = sha256.create().update(new Uint8Array(buffer)).digest();
|
|
33
36
|
}
|
|
34
37
|
return bytesToHex(hash);
|
|
35
38
|
}
|
|
36
39
|
/** Returns the size of the blob in bytes */
|
|
37
40
|
export function getBlobSize(blob) {
|
|
38
|
-
if (blob instanceof File || blob instanceof Blob) {
|
|
41
|
+
if ((typeof File !== "undefined" && blob instanceof File) || blob instanceof Blob) {
|
|
39
42
|
return blob.size;
|
|
40
43
|
}
|
|
41
44
|
else {
|
|
@@ -45,7 +48,7 @@ export function getBlobSize(blob) {
|
|
|
45
48
|
}
|
|
46
49
|
/** Returns the mime type of the blob */
|
|
47
50
|
export function getBlobType(blob) {
|
|
48
|
-
if (blob instanceof File || blob instanceof Blob) {
|
|
51
|
+
if ((typeof File !== "undefined" && blob instanceof File) || blob instanceof Blob) {
|
|
49
52
|
return blob.type;
|
|
50
53
|
}
|
|
51
54
|
return undefined;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { decodePaymentRequest } from "@cashu/cashu-ts";
|
|
2
|
+
export function getPaymentRequestFromHeaders(headers, quite = false) {
|
|
3
|
+
const header = headers.get("X-Cashu");
|
|
4
|
+
if (!header) {
|
|
5
|
+
if (!quite)
|
|
6
|
+
throw new Error("Missing cashu header");
|
|
7
|
+
else
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
const request = decodePaymentRequest(header);
|
|
11
|
+
// Clear the transport, since NUT-23 is only in-band payments
|
|
12
|
+
request.transport = [];
|
|
13
|
+
return request;
|
|
14
|
+
}
|
package/lib/helpers/index.d.ts
CHANGED
package/lib/helpers/index.js
CHANGED
package/lib/helpers/signal.js
CHANGED
|
@@ -4,7 +4,7 @@ export class TimeoutError extends Error {
|
|
|
4
4
|
export function wrapSignalWithTimeout(timeout, message, signal) {
|
|
5
5
|
const controller = new AbortController();
|
|
6
6
|
if (signal)
|
|
7
|
-
signal.addEventListener("abort", (err) => controller.abort(err));
|
|
7
|
+
signal.addEventListener("abort", (err) => controller.abort(err), { once: true });
|
|
8
8
|
const t = setTimeout(() => {
|
|
9
9
|
controller.abort(new TimeoutError(message));
|
|
10
10
|
}, timeout);
|
package/lib/image.js
CHANGED
|
@@ -59,20 +59,34 @@ export function handleBrokenImages(root, getServers) {
|
|
|
59
59
|
const observer = new MutationObserver((changes) => {
|
|
60
60
|
for (const change of changes) {
|
|
61
61
|
if (change.type === "childList") {
|
|
62
|
-
//
|
|
63
|
-
change.addedNodes.forEach((
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
// Process added nodes and their children for images
|
|
63
|
+
change.addedNodes.forEach((node) => {
|
|
64
|
+
if (!(node instanceof HTMLElement))
|
|
65
|
+
return;
|
|
66
|
+
// Find all img elements in the added node and its children
|
|
67
|
+
const children = Array.from(node.querySelectorAll("img[src]"));
|
|
68
|
+
const images = node instanceof HTMLImageElement ? [node, ...children] : children;
|
|
69
|
+
for (const image of images) {
|
|
70
|
+
if (image instanceof HTMLImageElement && !listeners.has(image)) {
|
|
71
|
+
const listener = handleImageFallbacks(image, getServers);
|
|
72
|
+
listeners.set(image, listener);
|
|
73
|
+
}
|
|
67
74
|
}
|
|
68
75
|
});
|
|
69
76
|
// cleanup removed nodes
|
|
70
|
-
change.removedNodes.forEach((
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
77
|
+
change.removedNodes.forEach((node) => {
|
|
78
|
+
if (!(node instanceof HTMLElement))
|
|
79
|
+
return;
|
|
80
|
+
// Find all img elements in the removed node and its children
|
|
81
|
+
const children = Array.from(node.querySelectorAll("img[src]"));
|
|
82
|
+
const images = node instanceof HTMLImageElement ? [node, ...children] : children;
|
|
83
|
+
for (const image of images) {
|
|
84
|
+
if (image instanceof HTMLImageElement) {
|
|
85
|
+
const listener = listeners.get(image);
|
|
86
|
+
if (listener) {
|
|
87
|
+
listener();
|
|
88
|
+
listeners.delete(image);
|
|
89
|
+
}
|
|
76
90
|
}
|
|
77
91
|
}
|
|
78
92
|
});
|
package/lib/types.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { ListOptions } from "./actions/list.js";
|
|
|
4
4
|
import { MirrorOptions } from "./actions/mirror.js";
|
|
5
5
|
import { UploadOptions } from "./actions/upload.js";
|
|
6
6
|
import { ServerType, UploadType } from "./client.js";
|
|
7
|
+
import { type PaymentRequest as CashuPaymentRequest } from "@cashu/cashu-ts";
|
|
7
8
|
export type EventTemplate = {
|
|
8
9
|
created_at: number;
|
|
9
10
|
kind: number;
|
|
@@ -32,9 +33,24 @@ export type BlobDescriptor = {
|
|
|
32
33
|
size: number;
|
|
33
34
|
url: string;
|
|
34
35
|
};
|
|
35
|
-
export type PaymentRequest =
|
|
36
|
+
export type PaymentRequest = CashuPaymentRequest;
|
|
37
|
+
type SerializedDLEQ = {
|
|
38
|
+
s: string;
|
|
39
|
+
e: string;
|
|
40
|
+
r?: string;
|
|
41
|
+
};
|
|
42
|
+
type Proof = {
|
|
43
|
+
id: string;
|
|
36
44
|
amount: number;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
secret: string;
|
|
46
|
+
C: string;
|
|
47
|
+
dleq?: SerializedDLEQ;
|
|
48
|
+
};
|
|
49
|
+
/** Copy of the Token type from cashu-ts */
|
|
50
|
+
export type PaymentToken = {
|
|
51
|
+
mint: string;
|
|
52
|
+
proofs: Array<Proof>;
|
|
53
|
+
memo?: string;
|
|
54
|
+
unit?: string;
|
|
40
55
|
};
|
|
56
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blossom-client-sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "Client SDK for talking to blossom servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"module": "./lib/index.js",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"author": "hzrd149",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"files": [
|
|
12
|
-
"lib"
|
|
12
|
+
"lib",
|
|
13
|
+
"src"
|
|
13
14
|
],
|
|
14
15
|
"exports": {
|
|
15
16
|
".": {
|
|
@@ -53,22 +54,23 @@
|
|
|
53
54
|
"node": ">=18"
|
|
54
55
|
},
|
|
55
56
|
"dependencies": {
|
|
56
|
-
"@cashu/cashu-ts": "^2.
|
|
57
|
-
"@noble/hashes": "^1.
|
|
57
|
+
"@cashu/cashu-ts": "^2.4.3",
|
|
58
|
+
"@noble/hashes": "^1.8.0"
|
|
58
59
|
},
|
|
59
60
|
"devDependencies": {
|
|
60
|
-
"@changesets/cli": "^2.
|
|
61
|
+
"@changesets/cli": "^2.29.3",
|
|
61
62
|
"@testing-library/dom": "^10.4.0",
|
|
62
|
-
"@types/node": "^22.
|
|
63
|
-
"@vitest/browser": "^
|
|
64
|
-
"@vitest/coverage-v8": "
|
|
65
|
-
"nostr-tools": "^2.
|
|
66
|
-
"playwright": "^1.
|
|
67
|
-
"prettier": "^3.
|
|
68
|
-
"typedoc": "^0.25.
|
|
63
|
+
"@types/node": "^22.15.14",
|
|
64
|
+
"@vitest/browser": "^3.1.3",
|
|
65
|
+
"@vitest/coverage-v8": "^3.1.3",
|
|
66
|
+
"nostr-tools": "^2.12.0",
|
|
67
|
+
"playwright": "^1.52.0",
|
|
68
|
+
"prettier": "^3.5.3",
|
|
69
|
+
"typedoc": "^0.25.13",
|
|
69
70
|
"typedoc-plugin-mermaid": "^1.12.0",
|
|
70
|
-
"typescript": "^5.
|
|
71
|
-
"vitest": "^
|
|
71
|
+
"typescript": "^5.8.3",
|
|
72
|
+
"vitest": "^3.1.3",
|
|
73
|
+
"vitest-fetch-mock": "^0.4.5"
|
|
72
74
|
},
|
|
73
75
|
"scripts": {
|
|
74
76
|
"build": "tsc",
|
|
@@ -76,7 +78,6 @@
|
|
|
76
78
|
"test": "vitest run",
|
|
77
79
|
"coverage": "vitest run --coverage",
|
|
78
80
|
"format": "prettier -w .",
|
|
79
|
-
"docs": "typedoc src/index.ts"
|
|
80
|
-
"test:browser": "vitest"
|
|
81
|
+
"docs": "typedoc src/index.ts"
|
|
81
82
|
}
|
|
82
83
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { encodeAuthorizationHeader } from "../auth.js";
|
|
2
|
+
import { ServerType } from "../client.js";
|
|
3
|
+
import HTTPError from "../error.js";
|
|
4
|
+
import { fetchWithTimeout } from "../helpers/fetch.js";
|
|
5
|
+
import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
6
|
+
|
|
7
|
+
export type DeleteOptions<S extends ServerType> = {
|
|
8
|
+
/** AbortSignal to cancel the action */
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
/** Override auth event to use */
|
|
11
|
+
auth?: SignedEvent;
|
|
12
|
+
/** Request timeout */
|
|
13
|
+
timeout?: number;
|
|
14
|
+
/**
|
|
15
|
+
* A method used to request payment when deleting
|
|
16
|
+
* @param server the server requiring payment
|
|
17
|
+
* @param sha256 the sha256 of the blob being uploaded or mirrored
|
|
18
|
+
* @param request the payment request
|
|
19
|
+
*/
|
|
20
|
+
onPayment?: (server: S, sha256: string, request: PaymentRequest) => Promise<PaymentToken>;
|
|
21
|
+
/**
|
|
22
|
+
* A method used to request a signed auth event for a server and sha256
|
|
23
|
+
* @param server the server requesting the auth
|
|
24
|
+
* @param sha256 the sha256 of the blob being upload or mirror to the server
|
|
25
|
+
*/
|
|
26
|
+
onAuth?: (server: S, sha256: string) => Promise<SignedEvent>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Deletes a blob to a server */
|
|
30
|
+
export async function deleteBlob<S extends ServerType>(server: S, hash: string, opts?: DeleteOptions<S>) {
|
|
31
|
+
const url = new URL("/" + hash, server);
|
|
32
|
+
|
|
33
|
+
const headers: HeadersInit = {};
|
|
34
|
+
|
|
35
|
+
// attach the auth if its already set
|
|
36
|
+
if (opts?.auth) headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
37
|
+
|
|
38
|
+
let res = await fetchWithTimeout(url, {
|
|
39
|
+
method: "DELETE",
|
|
40
|
+
headers,
|
|
41
|
+
signal: opts?.signal,
|
|
42
|
+
timeout: opts?.timeout,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// handle auth and payment
|
|
46
|
+
switch (res.status) {
|
|
47
|
+
case 401: {
|
|
48
|
+
const auth = opts?.auth || (await opts?.onAuth?.(server, hash));
|
|
49
|
+
if (!auth) throw new Error("Missing auth handler");
|
|
50
|
+
|
|
51
|
+
// Try delete with auth
|
|
52
|
+
res = await fetchWithTimeout(url, {
|
|
53
|
+
signal: opts?.signal,
|
|
54
|
+
method: "DELETE",
|
|
55
|
+
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
56
|
+
timeout: opts?.timeout,
|
|
57
|
+
});
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case 402: {
|
|
61
|
+
if (!opts?.onPayment) throw new Error("Missing payment handler");
|
|
62
|
+
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
63
|
+
const { getPaymentRequestFromHeaders } = await import("../helpers/cashu.js");
|
|
64
|
+
const request = getPaymentRequestFromHeaders(res.headers);
|
|
65
|
+
|
|
66
|
+
const token = await opts.onPayment(server, hash, request);
|
|
67
|
+
const payment = getEncodedToken(token);
|
|
68
|
+
|
|
69
|
+
// Try delete with payment
|
|
70
|
+
res = await fetchWithTimeout(url, {
|
|
71
|
+
signal: opts?.signal,
|
|
72
|
+
method: "DELETE",
|
|
73
|
+
headers: { ...headers, "X-Cashu": payment },
|
|
74
|
+
timeout: opts?.timeout,
|
|
75
|
+
});
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// handle errors
|
|
81
|
+
await HTTPError.handleErrorResponse(res);
|
|
82
|
+
|
|
83
|
+
// return blob descriptor
|
|
84
|
+
return res.ok;
|
|
85
|
+
}
|