blossom-client-sdk 5.0.0-1 → 5.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/README.md +3 -1
- package/lib/media.js +19 -15
- package/package.json +1 -1
- package/src/actions/resolve.ts +1 -4
- package/src/helpers/cashu.ts +1 -4
- package/src/media.ts +21 -15
- package/lib/helpers/cachu.d.ts +0 -5
- package/lib/helpers/cachu.js +0 -14
- package/lib/helpers.d.ts +0 -20
- package/lib/helpers.js +0 -73
- package/lib/upload.d.ts +0 -13
- package/lib/upload.js +0 -37
package/README.md
CHANGED
|
@@ -117,7 +117,9 @@ cleanup();
|
|
|
117
117
|
Media elements can use `blossom:` URIs directly in the `src` attribute. The fallback handler will automatically resolve the URI to HTTP URLs using the server hints:
|
|
118
118
|
|
|
119
119
|
```html
|
|
120
|
-
<img
|
|
120
|
+
<img
|
|
121
|
+
src="blossom:b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553.png?xs=https://cdn1.com&xs=https://cdn2.com"
|
|
122
|
+
/>
|
|
121
123
|
```
|
|
122
124
|
|
|
123
125
|
When the browser can't load the `blossom:` protocol, the error handler parses the URI, resolves server URLs from `xs` and `as` hints, and sets the first working URL.
|
package/lib/media.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { getHashFromURL } from "./helpers/url.js";
|
|
2
|
-
import { parseBlossomURI } from "./helpers/blossom-uri.js";
|
|
3
1
|
import { getBlobUrls } from "./actions/resolve.js";
|
|
2
|
+
import { parseBlossomURI } from "./helpers/blossom-uri.js";
|
|
3
|
+
import { getHashFromURL } from "./helpers/url.js";
|
|
4
4
|
/**
|
|
5
5
|
* Attaches an "error" event listener to a media element (img, video, audio) to handle server fallbacks.
|
|
6
6
|
*
|
|
@@ -22,9 +22,7 @@ export function handleMediaFallbacks(element, getServers, overridePubkey) {
|
|
|
22
22
|
const resolveBlossomUri = async (src) => {
|
|
23
23
|
const parsed = parseBlossomURI(src);
|
|
24
24
|
return getBlobUrls(parsed, {
|
|
25
|
-
getServers: getServers
|
|
26
|
-
? async (pubkey) => (await getServers(pubkey)) ?? undefined
|
|
27
|
-
: undefined,
|
|
25
|
+
getServers: getServers ? async (pubkey) => (await getServers(pubkey)) ?? undefined : undefined,
|
|
28
26
|
});
|
|
29
27
|
};
|
|
30
28
|
/** Resolve an HTTP blob URL into fallback URLs via pubkey server lookup */
|
|
@@ -33,7 +31,6 @@ export function handleMediaFallbacks(element, getServers, overridePubkey) {
|
|
|
33
31
|
if (!hash)
|
|
34
32
|
return undefined;
|
|
35
33
|
const url = new URL(element.src);
|
|
36
|
-
const ext = url.pathname.match(/\.\w+$/i);
|
|
37
34
|
let pubkey = overridePubkey;
|
|
38
35
|
// walk up the tree looking for pubkey
|
|
39
36
|
let el = element;
|
|
@@ -50,15 +47,7 @@ export function handleMediaFallbacks(element, getServers, overridePubkey) {
|
|
|
50
47
|
const servers = (await getServers(pubkey))?.map((s) => (s instanceof URL ? s : new URL(s)));
|
|
51
48
|
if (!servers)
|
|
52
49
|
return undefined;
|
|
53
|
-
return servers
|
|
54
|
-
.filter((s) => s.hostname !== url.hostname)
|
|
55
|
-
.map((s) => {
|
|
56
|
-
const u = new URL(url);
|
|
57
|
-
u.hostname = s.hostname;
|
|
58
|
-
u.protocol = s.protocol;
|
|
59
|
-
u.pathname = "/" + hash + (ext?.[0] ?? "");
|
|
60
|
-
return u.toString();
|
|
61
|
-
});
|
|
50
|
+
return servers.filter((s) => s.hostname !== url.hostname).map((s) => new URL(url.pathname, s).toString());
|
|
62
51
|
};
|
|
63
52
|
const onError = async () => {
|
|
64
53
|
// Already resolved — try the next URL in the list
|
|
@@ -88,8 +77,23 @@ export function handleMediaFallbacks(element, getServers, overridePubkey) {
|
|
|
88
77
|
}
|
|
89
78
|
};
|
|
90
79
|
element.addEventListener("error", onError);
|
|
80
|
+
// Trigger resolution immediately if:
|
|
81
|
+
// - src is a blossom: URI (browser can't load it natively)
|
|
82
|
+
// - element already failed to load before we attached the listener
|
|
83
|
+
const rawSrc = element.getAttribute("src") || "";
|
|
84
|
+
if (rawSrc.startsWith("blossom:") || hasFailedToLoad(element)) {
|
|
85
|
+
onError();
|
|
86
|
+
}
|
|
91
87
|
return () => element.removeEventListener("error", onError);
|
|
92
88
|
}
|
|
89
|
+
/** Returns true if a media element has already finished loading and failed */
|
|
90
|
+
function hasFailedToLoad(element) {
|
|
91
|
+
if (element instanceof HTMLImageElement) {
|
|
92
|
+
return element.complete && element.naturalWidth === 0;
|
|
93
|
+
}
|
|
94
|
+
// video/audio — check if loading resulted in an error
|
|
95
|
+
return element.error !== null;
|
|
96
|
+
}
|
|
93
97
|
const MEDIA_SELECTOR = "img[src], video[src], audio[src]";
|
|
94
98
|
function isMediaElement(node) {
|
|
95
99
|
return node instanceof HTMLImageElement || node instanceof HTMLVideoElement || node instanceof HTMLAudioElement;
|
package/package.json
CHANGED
package/src/actions/resolve.ts
CHANGED
|
@@ -66,10 +66,7 @@ export type GetBlobUrlsOptions = {
|
|
|
66
66
|
*
|
|
67
67
|
* Accepts a blossom URI string, URL, or pre-parsed BlossomURI.
|
|
68
68
|
*/
|
|
69
|
-
export async function getBlobUrls(
|
|
70
|
-
uri: string | URL | BlossomURI,
|
|
71
|
-
opts?: GetBlobUrlsOptions,
|
|
72
|
-
): Promise<string[]> {
|
|
69
|
+
export async function getBlobUrls(uri: string | URL | BlossomURI, opts?: GetBlobUrlsOptions): Promise<string[]> {
|
|
73
70
|
const parsed = parseInput(uri);
|
|
74
71
|
const collector = createServerCollector(buildBlobPath(parsed.sha256, parsed.ext));
|
|
75
72
|
const urls: string[] = [];
|
package/src/helpers/cashu.ts
CHANGED
|
@@ -3,10 +3,7 @@ import { PaymentRequest } from "../types.js";
|
|
|
3
3
|
/** Extracts a cashu-ts PaymentRequest from Headers. Loads @cashu/cashu-ts on demand. */
|
|
4
4
|
export async function getPaymentRequestFromHeaders(headers: Headers): Promise<PaymentRequest>;
|
|
5
5
|
export async function getPaymentRequestFromHeaders(headers: Headers, quite: false): Promise<PaymentRequest>;
|
|
6
|
-
export async function getPaymentRequestFromHeaders(
|
|
7
|
-
headers: Headers,
|
|
8
|
-
quite: true,
|
|
9
|
-
): Promise<PaymentRequest | undefined>;
|
|
6
|
+
export async function getPaymentRequestFromHeaders(headers: Headers, quite: true): Promise<PaymentRequest | undefined>;
|
|
10
7
|
export async function getPaymentRequestFromHeaders(headers: Headers, quite = false) {
|
|
11
8
|
const header = headers.get("X-Cashu");
|
|
12
9
|
if (!header) {
|
package/src/media.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { getHashFromURL } from "./helpers/url.js";
|
|
2
|
-
import { parseBlossomURI } from "./helpers/blossom-uri.js";
|
|
3
1
|
import { getBlobUrls } from "./actions/resolve.js";
|
|
2
|
+
import { parseBlossomURI } from "./helpers/blossom-uri.js";
|
|
3
|
+
import { getHashFromURL } from "./helpers/url.js";
|
|
4
4
|
|
|
5
5
|
type MediaElement = HTMLImageElement | HTMLVideoElement | HTMLAudioElement;
|
|
6
6
|
|
|
@@ -28,9 +28,7 @@ export function handleMediaFallbacks(element: MediaElement, getServers: GetServe
|
|
|
28
28
|
const resolveBlossomUri = async (src: string): Promise<string[]> => {
|
|
29
29
|
const parsed = parseBlossomURI(src);
|
|
30
30
|
return getBlobUrls(parsed, {
|
|
31
|
-
getServers: getServers
|
|
32
|
-
? async (pubkey) => (await getServers(pubkey)) ?? undefined
|
|
33
|
-
: undefined,
|
|
31
|
+
getServers: getServers ? async (pubkey) => (await getServers(pubkey)) ?? undefined : undefined,
|
|
34
32
|
});
|
|
35
33
|
};
|
|
36
34
|
|
|
@@ -40,7 +38,6 @@ export function handleMediaFallbacks(element: MediaElement, getServers: GetServe
|
|
|
40
38
|
if (!hash) return undefined;
|
|
41
39
|
|
|
42
40
|
const url = new URL(element.src);
|
|
43
|
-
const ext = url.pathname.match(/\.\w+$/i);
|
|
44
41
|
|
|
45
42
|
let pubkey = overridePubkey;
|
|
46
43
|
|
|
@@ -59,15 +56,7 @@ export function handleMediaFallbacks(element: MediaElement, getServers: GetServe
|
|
|
59
56
|
const servers = (await getServers(pubkey))?.map((s) => (s instanceof URL ? s : new URL(s)));
|
|
60
57
|
if (!servers) return undefined;
|
|
61
58
|
|
|
62
|
-
return servers
|
|
63
|
-
.filter((s) => s.hostname !== url.hostname)
|
|
64
|
-
.map((s) => {
|
|
65
|
-
const u = new URL(url);
|
|
66
|
-
u.hostname = s.hostname;
|
|
67
|
-
u.protocol = s.protocol;
|
|
68
|
-
u.pathname = "/" + hash + (ext?.[0] ?? "");
|
|
69
|
-
return u.toString();
|
|
70
|
-
});
|
|
59
|
+
return servers.filter((s) => s.hostname !== url.hostname).map((s) => new URL(url.pathname, s).toString());
|
|
71
60
|
};
|
|
72
61
|
|
|
73
62
|
const onError = async () => {
|
|
@@ -100,9 +89,26 @@ export function handleMediaFallbacks(element: MediaElement, getServers: GetServe
|
|
|
100
89
|
|
|
101
90
|
element.addEventListener("error", onError);
|
|
102
91
|
|
|
92
|
+
// Trigger resolution immediately if:
|
|
93
|
+
// - src is a blossom: URI (browser can't load it natively)
|
|
94
|
+
// - element already failed to load before we attached the listener
|
|
95
|
+
const rawSrc = element.getAttribute("src") || "";
|
|
96
|
+
if (rawSrc.startsWith("blossom:") || hasFailedToLoad(element)) {
|
|
97
|
+
onError();
|
|
98
|
+
}
|
|
99
|
+
|
|
103
100
|
return () => element.removeEventListener("error", onError);
|
|
104
101
|
}
|
|
105
102
|
|
|
103
|
+
/** Returns true if a media element has already finished loading and failed */
|
|
104
|
+
function hasFailedToLoad(element: MediaElement): boolean {
|
|
105
|
+
if (element instanceof HTMLImageElement) {
|
|
106
|
+
return element.complete && element.naturalWidth === 0;
|
|
107
|
+
}
|
|
108
|
+
// video/audio — check if loading resulted in an error
|
|
109
|
+
return element.error !== null;
|
|
110
|
+
}
|
|
111
|
+
|
|
106
112
|
const MEDIA_SELECTOR = "img[src], video[src], audio[src]";
|
|
107
113
|
|
|
108
114
|
function isMediaElement(node: Node): node is MediaElement {
|
package/lib/helpers/cachu.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { PaymentRequest } from "../types.js";
|
|
2
|
-
/** Extracts a cashu-ts PaymentRequest from Headers */
|
|
3
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers): PaymentRequest;
|
|
4
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers, quite: false): PaymentRequest;
|
|
5
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers, quite: true): PaymentRequest | undefined;
|
package/lib/helpers/cachu.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { type UploadType } from "./client.js";
|
|
2
|
-
import { PaymentRequest } from "./types.js";
|
|
3
|
-
export declare function isSha256(str: string): RegExpMatchArray | null;
|
|
4
|
-
/** returns the last sha256 in a URL */
|
|
5
|
-
export declare function getHashFromURL(url: string | URL): string | null;
|
|
6
|
-
export declare const BlobHashSymbol: unique symbol;
|
|
7
|
-
/** gets or calculates the sha2456 of a Blob */
|
|
8
|
-
export declare function getBlobSha256(blob: UploadType): string | Promise<string>;
|
|
9
|
-
/** Calculates the sha2456 of a Blob */
|
|
10
|
-
export declare function computeBlobSha256(blob: UploadType): Promise<string>;
|
|
11
|
-
/** Returns the size of the blob in bytes */
|
|
12
|
-
export declare function getBlobSize(blob: UploadType): number;
|
|
13
|
-
/** Returns the mime type of the blob */
|
|
14
|
-
export declare function getBlobType(blob: UploadType): string | undefined;
|
|
15
|
-
/** Check if two servers are the same */
|
|
16
|
-
export declare function areServersEqual(a: string | URL, b: string | URL): boolean;
|
|
17
|
-
/** Extracts a cashu-ts PaymentRequest from Headers */
|
|
18
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers): PaymentRequest;
|
|
19
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers, quite: false): PaymentRequest;
|
|
20
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers, quite: true): PaymentRequest | undefined;
|
package/lib/helpers.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { bytesToHex } from "@noble/hashes/utils";
|
|
2
|
-
export function isSha256(str) {
|
|
3
|
-
return str.match(/^[0-9a-f]{64}$/);
|
|
4
|
-
}
|
|
5
|
-
/** returns the last sha256 in a URL */
|
|
6
|
-
export function getHashFromURL(url) {
|
|
7
|
-
if (typeof url === "string")
|
|
8
|
-
url = new URL(url);
|
|
9
|
-
const hashes = Array.from(url.pathname.matchAll(/[0-9a-f]{64}/gi));
|
|
10
|
-
if (hashes.length > 0)
|
|
11
|
-
return hashes[hashes.length - 1][0];
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
export const BlobHashSymbol = Symbol.for("sha256");
|
|
15
|
-
/** gets or calculates the sha2456 of a Blob */
|
|
16
|
-
export function getBlobSha256(blob) {
|
|
17
|
-
if (Reflect.has(blob, BlobHashSymbol))
|
|
18
|
-
return Reflect.get(blob, BlobHashSymbol);
|
|
19
|
-
return computeBlobSha256(blob).then((hash) => {
|
|
20
|
-
Reflect.set(blob, BlobHashSymbol, hash);
|
|
21
|
-
return hash;
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
/** Calculates the sha2456 of a Blob */
|
|
25
|
-
export async function computeBlobSha256(blob) {
|
|
26
|
-
let buffer;
|
|
27
|
-
if (blob instanceof File || blob instanceof Blob) {
|
|
28
|
-
buffer = await blob.arrayBuffer();
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
// nodejs Buffer
|
|
32
|
-
buffer = blob;
|
|
33
|
-
}
|
|
34
|
-
let hash;
|
|
35
|
-
if (crypto.subtle) {
|
|
36
|
-
const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
|
|
37
|
-
hash = new Uint8Array(hashBuffer);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
const { sha256 } = await import("@noble/hashes/sha256");
|
|
41
|
-
hash = sha256.create().update(new Uint8Array(buffer)).digest();
|
|
42
|
-
}
|
|
43
|
-
return bytesToHex(hash);
|
|
44
|
-
}
|
|
45
|
-
/** Returns the size of the blob in bytes */
|
|
46
|
-
export function getBlobSize(blob) {
|
|
47
|
-
if (blob instanceof File || blob instanceof Blob) {
|
|
48
|
-
return blob.size;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
// nodejs Buffer
|
|
52
|
-
return blob.length;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
/** Returns the mime type of the blob */
|
|
56
|
-
export function getBlobType(blob) {
|
|
57
|
-
if (blob instanceof File || blob instanceof Blob) {
|
|
58
|
-
return blob.type;
|
|
59
|
-
}
|
|
60
|
-
return undefined;
|
|
61
|
-
}
|
|
62
|
-
/** Check if two servers are the same */
|
|
63
|
-
export function areServersEqual(a, b) {
|
|
64
|
-
const hostnameA = a instanceof URL ? a.hostname : new URL(a).hostname;
|
|
65
|
-
const hostnameB = b instanceof URL ? b.hostname : new URL(b).hostname;
|
|
66
|
-
return hostnameA === hostnameB;
|
|
67
|
-
}
|
|
68
|
-
export function getPaymentRequestFromHeaders(headers, quite = false) {
|
|
69
|
-
const header = headers.get("X-Cashu");
|
|
70
|
-
if (!header && !quite)
|
|
71
|
-
throw new Error("Missing cashu header");
|
|
72
|
-
return header ? JSON.parse(atob(header)) : undefined;
|
|
73
|
-
}
|
package/lib/upload.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { BlobDescriptor, ServerType, SignedEvent, Signer, UploadType } from "./client.js";
|
|
2
|
-
/**
|
|
3
|
-
* Creates an AsyncGenerator that can be used to upload a blob to multiple servers
|
|
4
|
-
* @param servers A Set or Array of servers to upload to
|
|
5
|
-
* @param file The blob to be uploaded
|
|
6
|
-
* @param signer An async function used for signing nostr events
|
|
7
|
-
* @returns The BlobDescriptor if successful
|
|
8
|
-
*/
|
|
9
|
-
export declare function multiServerUpload(servers: Iterable<ServerType>, file: UploadType, signer: Signer, auth?: SignedEvent): AsyncGenerator<{
|
|
10
|
-
blob: BlobDescriptor;
|
|
11
|
-
progress: number;
|
|
12
|
-
server: ServerType;
|
|
13
|
-
}, BlobDescriptor | undefined, unknown>;
|
package/lib/upload.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { BlossomClient } from "./client.js";
|
|
2
|
-
/**
|
|
3
|
-
* Creates an AsyncGenerator that can be used to upload a blob to multiple servers
|
|
4
|
-
* @param servers A Set or Array of servers to upload to
|
|
5
|
-
* @param file The blob to be uploaded
|
|
6
|
-
* @param signer An async function used for signing nostr events
|
|
7
|
-
* @returns The BlobDescriptor if successful
|
|
8
|
-
*/
|
|
9
|
-
export async function* multiServerUpload(servers, file, signer, auth) {
|
|
10
|
-
auth = auth || (await BlossomClient.getUploadAuth(file, signer));
|
|
11
|
-
let blob = undefined;
|
|
12
|
-
let total = Array.from(servers).length;
|
|
13
|
-
let uploaded = 0;
|
|
14
|
-
for (const server of servers) {
|
|
15
|
-
if (blob) {
|
|
16
|
-
// mirror blob to server
|
|
17
|
-
try {
|
|
18
|
-
let b = await BlossomClient.mirrorBlob(server, blob.url, auth);
|
|
19
|
-
uploaded++;
|
|
20
|
-
yield { blob: b, progress: uploaded / total, server };
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
// not all servers support mirroring, attempt to upload
|
|
24
|
-
let b = await BlossomClient.uploadBlob(server, file, auth);
|
|
25
|
-
uploaded++;
|
|
26
|
-
yield { blob: b, progress: uploaded / total, server };
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
// attempt initial upload
|
|
31
|
-
blob = await BlossomClient.uploadBlob(server, file, auth);
|
|
32
|
-
uploaded++;
|
|
33
|
-
yield { blob, progress: uploaded / total, server };
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return blob;
|
|
37
|
-
}
|