blossom-client-sdk 5.0.0-1 → 5.0.0-2

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 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 src="blossom:b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553.png?xs=https://cdn1.com&xs=https://cdn2.com" />
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blossom-client-sdk",
3
- "version": "5.0.0-1",
3
+ "version": "5.0.0-2",
4
4
  "description": "Client SDK for talking to blossom servers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -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[] = [];
@@ -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 {