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.
Files changed (47) hide show
  1. package/lib/actions/delete.d.ts +2 -3
  2. package/lib/actions/delete.js +2 -2
  3. package/lib/actions/download.d.ts +2 -3
  4. package/lib/actions/download.js +2 -1
  5. package/lib/actions/list.d.ts +2 -3
  6. package/lib/actions/list.js +9 -4
  7. package/lib/actions/media.js +2 -1
  8. package/lib/actions/mirror.d.ts +2 -3
  9. package/lib/actions/mirror.js +3 -2
  10. package/lib/actions/multi-server.js +1 -1
  11. package/lib/actions/upload.d.ts +2 -3
  12. package/lib/actions/upload.js +3 -2
  13. package/lib/auth.d.ts +1 -0
  14. package/lib/auth.js +13 -5
  15. package/lib/helpers/blob.d.ts +2 -0
  16. package/lib/helpers/blob.js +8 -5
  17. package/lib/helpers/cashu.js +14 -0
  18. package/lib/helpers/index.d.ts +1 -1
  19. package/lib/helpers/index.js +1 -1
  20. package/lib/helpers/signal.js +1 -1
  21. package/lib/image.js +25 -11
  22. package/lib/types.d.ts +20 -4
  23. package/package.json +18 -17
  24. package/src/actions/delete.ts +85 -0
  25. package/src/actions/download.ts +84 -0
  26. package/src/actions/index.ts +7 -0
  27. package/src/actions/list.ts +83 -0
  28. package/src/actions/media.ts +102 -0
  29. package/src/actions/mirror.ts +99 -0
  30. package/src/actions/multi-server.ts +207 -0
  31. package/src/actions/upload.ts +129 -0
  32. package/src/auth.ts +137 -0
  33. package/src/client.ts +221 -0
  34. package/src/const.ts +1 -0
  35. package/src/error.ts +23 -0
  36. package/src/helpers/blob.ts +62 -0
  37. package/src/helpers/cashu.ts +21 -0
  38. package/src/helpers/fetch.ts +16 -0
  39. package/src/helpers/index.ts +5 -0
  40. package/src/helpers/signal.ts +20 -0
  41. package/src/helpers/url.ts +16 -0
  42. package/src/image.ts +122 -0
  43. package/src/index.ts +9 -0
  44. package/src/nostr.ts +17 -0
  45. package/src/types.ts +63 -0
  46. package/lib/helpers/cachu.js +0 -6
  47. /package/lib/helpers/{cachu.d.ts → cashu.d.ts} +0 -0
package/src/image.ts ADDED
@@ -0,0 +1,122 @@
1
+ import { getHashFromURL } from "./helpers/url.js";
2
+
3
+ type GetServersMethod = (pubkey?: string) => Promise<(string | URL)[] | undefined> | undefined;
4
+
5
+ /**
6
+ * attaches an "error" event listener to a HTMLImageElement element to handle server fallbacks
7
+ * @param image The image element
8
+ * @param getServers A async method to get an ordered list of servers for a pubkey
9
+ * @param overridePubkey An optional pubkey to set the `data-pubkey` attr on the image
10
+ * @returns returns a method to remove the "error" event listener
11
+ */
12
+ export function handleImageFallbacks(image: HTMLImageElement, getServers: GetServersMethod, overridePubkey?: string) {
13
+ let tried: string[] = [];
14
+ let servers: URL[] | undefined = undefined;
15
+
16
+ const onError = async () => {
17
+ const hash = getHashFromURL(image.src);
18
+ if (!hash) return;
19
+
20
+ const url = new URL(image.src);
21
+ const ext = url.pathname.match(/\.\w+$/i);
22
+ tried.push(url.hostname);
23
+
24
+ let pubkey = overridePubkey;
25
+
26
+ // walk up the tree looking for pubkey
27
+ let el: HTMLElement | null = image;
28
+ while (!pubkey && el.parentElement) {
29
+ if (el.dataset.pubkey) pubkey = el.dataset.pubkey;
30
+ else el = el.parentElement;
31
+ }
32
+
33
+ if (!pubkey) {
34
+ console.warn("Failed to find pubkey for broken image", image);
35
+ return;
36
+ }
37
+
38
+ if (!servers) servers = (await getServers(pubkey))?.map((s) => (s instanceof URL ? s : new URL(s)));
39
+ if (servers) {
40
+ const server = servers.find((s) => !tried.includes(s.hostname));
41
+
42
+ if (server) {
43
+ url.hostname = server.hostname;
44
+ url.pathname = "/" + hash + ext;
45
+ url.protocol = server.protocol;
46
+
47
+ image.src = url.toString();
48
+ } else {
49
+ // ran out of servers, stop listening for errors
50
+ image.removeEventListener("error", onError);
51
+ }
52
+ }
53
+ };
54
+
55
+ image.addEventListener("error", onError);
56
+
57
+ return () => image.removeEventListener("error", onError);
58
+ }
59
+
60
+ /**
61
+ * Watch for any broken <img> elements in the tree and attempt to fix them
62
+ * @param root The root element to observe
63
+ * @param getServers A method used to get a list of servers for a specific pubkey
64
+ * @returns A MutationObserver that is observing the root element
65
+ */
66
+ export function handleBrokenImages(root: HTMLElement, getServers: GetServersMethod) {
67
+ const listeners = new WeakMap<HTMLImageElement, () => void>();
68
+
69
+ const observer = new MutationObserver((changes) => {
70
+ for (const change of changes) {
71
+ if (change.type === "childList") {
72
+ // Process added nodes and their children for images
73
+ change.addedNodes.forEach((node) => {
74
+ if (!(node instanceof HTMLElement)) return;
75
+
76
+ // Find all img elements in the added node and its children
77
+ const children = Array.from(node.querySelectorAll("img[src]"));
78
+ const images = node instanceof HTMLImageElement ? [node, ...children] : children;
79
+
80
+ for (const image of images) {
81
+ if (image instanceof HTMLImageElement && !listeners.has(image)) {
82
+ const listener = handleImageFallbacks(image, getServers);
83
+ listeners.set(image, listener);
84
+ }
85
+ }
86
+ });
87
+
88
+ // cleanup removed nodes
89
+ change.removedNodes.forEach((node) => {
90
+ if (!(node instanceof HTMLElement)) return;
91
+
92
+ // Find all img elements in the removed node and its children
93
+ const children = Array.from(node.querySelectorAll("img[src]"));
94
+ const images = node instanceof HTMLImageElement ? [node, ...children] : children;
95
+ for (const image of images) {
96
+ if (image instanceof HTMLImageElement) {
97
+ const listener = listeners.get(image);
98
+ if (listener) {
99
+ listener();
100
+ listeners.delete(image);
101
+ }
102
+ }
103
+ }
104
+ });
105
+ }
106
+ }
107
+ });
108
+
109
+ // start watching root
110
+ observer.observe(root, { subtree: true, childList: true });
111
+
112
+ // find all existing images and attach error handler
113
+ const images = root.querySelectorAll("img[src]");
114
+ images.forEach((image) => {
115
+ if (image instanceof HTMLImageElement) {
116
+ const listener = handleImageFallbacks(image, getServers);
117
+ listeners.set(image, listener);
118
+ }
119
+ });
120
+
121
+ return observer;
122
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from "./const.js";
2
+ export * from "./auth.js";
3
+ export * from "./client.js";
4
+ export * from "./helpers/index.js";
5
+ export * from "./image.js";
6
+ export * from "./nostr.js";
7
+ export * from "./error.js";
8
+ export * from "./types.js";
9
+ export * as Actions from "./actions/index.js";
package/src/nostr.ts ADDED
@@ -0,0 +1,17 @@
1
+ export const USER_BLOSSOM_SERVER_LIST_KIND = 10063;
2
+
3
+ /** Returns an ordered array of servers found in a server list event (10063) */
4
+ export function getServersFromServerListEvent(event: { tags: string[][] }) {
5
+ const servers: URL[] = [];
6
+ for (const tag of event.tags) {
7
+ if (tag[0] === "server" && tag[1]) {
8
+ try {
9
+ const url = new URL(tag[1]);
10
+ url.pathname = "/";
11
+
12
+ servers.push(url);
13
+ } catch (e) {}
14
+ }
15
+ }
16
+ return servers;
17
+ }
package/src/types.ts ADDED
@@ -0,0 +1,63 @@
1
+ import { DeleteOptions } from "./actions/delete.js";
2
+ import { DownloadOptions } from "./actions/download.js";
3
+ import { ListOptions } from "./actions/list.js";
4
+ import { MirrorOptions } from "./actions/mirror.js";
5
+ import { UploadOptions } from "./actions/upload.js";
6
+ import { ServerType, UploadType } from "./client.js";
7
+ import { type PaymentRequest as CashuPaymentRequest } from "@cashu/cashu-ts";
8
+
9
+ export type EventTemplate = {
10
+ created_at: number;
11
+ kind: number;
12
+ content: string;
13
+ tags: string[][];
14
+ };
15
+ export type SignedEvent = EventTemplate & {
16
+ id: string;
17
+ sig: string;
18
+ pubkey: string;
19
+ };
20
+
21
+ /** An async method used to sign nostr events */
22
+ export type Signer = (draft: EventTemplate) => Promise<SignedEvent>;
23
+
24
+ /** interface for handling payment requests */
25
+ export interface PaymentHandlers<S extends ServerType = ServerType> {
26
+ upload?: UploadOptions<S, UploadType>["onPayment"];
27
+ download?: DownloadOptions<S>["onPayment"];
28
+ list?: ListOptions<S>["onPayment"];
29
+ mirror?: MirrorOptions<S>["onPayment"];
30
+ delete?: DeleteOptions<S>["onPayment"];
31
+ }
32
+
33
+ export type BlobDescriptor = {
34
+ uploaded: number;
35
+ type?: string;
36
+ sha256: string;
37
+ size: number;
38
+ url: string;
39
+ };
40
+
41
+ export type PaymentRequest = CashuPaymentRequest;
42
+
43
+ // NOTE: hack for Token type not being exported from cashu-ts
44
+ type SerializedDLEQ = {
45
+ s: string;
46
+ e: string;
47
+ r?: string;
48
+ };
49
+ type Proof = {
50
+ id: string;
51
+ amount: number;
52
+ secret: string;
53
+ C: string;
54
+ dleq?: SerializedDLEQ;
55
+ };
56
+
57
+ /** Copy of the Token type from cashu-ts */
58
+ export type PaymentToken = {
59
+ mint: string;
60
+ proofs: Array<Proof>;
61
+ memo?: string;
62
+ unit?: string;
63
+ };
@@ -1,6 +0,0 @@
1
- export function getPaymentRequestFromHeaders(headers, quite = false) {
2
- const header = headers.get("X-Cashu");
3
- if (!header && !quite)
4
- throw new Error("Missing cashu header");
5
- return header ? JSON.parse(atob(header)) : undefined;
6
- }
File without changes