blossom-client-sdk 0.6.0 → 0.8.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/CHANGELOG.md +16 -0
- package/lib/client.d.ts +13 -9
- package/lib/client.js +26 -7
- package/lib/helpers.d.ts +1 -0
- package/lib/helpers.js +15 -0
- package/lib/image.d.ts +12 -2
- package/lib/image.js +66 -11
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# blossom-client-sdk
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Add `handleBrokenImages` method for repairing any broken images in an app
|
|
8
|
+
|
|
9
|
+
## 0.7.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- Include "x" tag with sha256 in upload auth
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Allow URL class for server
|
|
18
|
+
|
|
3
19
|
## 0.7.0
|
|
4
20
|
|
|
5
21
|
### Minor Changes
|
package/lib/client.d.ts
CHANGED
|
@@ -32,24 +32,27 @@ export declare class HTTPError extends Error {
|
|
|
32
32
|
} | string);
|
|
33
33
|
static handleErrorResponse(res: Response): Promise<void>;
|
|
34
34
|
}
|
|
35
|
+
type ServerType = string | URL;
|
|
36
|
+
type UploadType = Blob | File;
|
|
35
37
|
export declare class BlossomClient {
|
|
36
|
-
server:
|
|
38
|
+
server: URL;
|
|
37
39
|
signer?: Signer;
|
|
38
|
-
constructor(server: string, signer?: Signer);
|
|
40
|
+
constructor(server: string | URL, signer?: Signer);
|
|
41
|
+
static getFileSha256(file: UploadType): Promise<string>;
|
|
39
42
|
static getGetAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
40
|
-
static getUploadAuth(file:
|
|
43
|
+
static getUploadAuth(file: UploadType, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
41
44
|
static getDeleteAuth(hash: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
42
45
|
static getListAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
43
46
|
getGetAuth(message?: string, expiration?: number): Promise<SignedEvent>;
|
|
44
|
-
getUploadAuth(file:
|
|
47
|
+
getUploadAuth(file: UploadType, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
45
48
|
getDeleteAuth(hash: string, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
46
49
|
getListAuth(message?: string, expiration?: number): Promise<SignedEvent>;
|
|
47
50
|
static encodeAuthorizationHeader(event: SignedEvent): string;
|
|
48
|
-
static getBlob(server:
|
|
51
|
+
static getBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<Blob>;
|
|
49
52
|
getBlob(hash: string, auth?: SignedEvent | boolean): Promise<Blob>;
|
|
50
|
-
static hasBlob(server:
|
|
53
|
+
static hasBlob(server: ServerType, hash: string): Promise<boolean>;
|
|
51
54
|
hasBlob(hash: string): Promise<boolean>;
|
|
52
|
-
static listBlobs(server:
|
|
55
|
+
static listBlobs(server: ServerType, pubkey: string, opts?: {
|
|
53
56
|
since?: number;
|
|
54
57
|
until?: number;
|
|
55
58
|
}, auth?: SignedEvent): Promise<BlobDescriptor[]>;
|
|
@@ -57,8 +60,9 @@ export declare class BlossomClient {
|
|
|
57
60
|
since?: number;
|
|
58
61
|
until?: number;
|
|
59
62
|
}, auth?: SignedEvent | boolean): Promise<BlobDescriptor[]>;
|
|
60
|
-
static deleteBlob(server:
|
|
63
|
+
static deleteBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<string>;
|
|
61
64
|
deleteBlob(hash: string, auth?: SignedEvent | boolean): Promise<string>;
|
|
62
|
-
static uploadBlob(server:
|
|
65
|
+
static uploadBlob(server: ServerType, file: UploadType, auth?: SignedEvent): Promise<BlobDescriptor>;
|
|
63
66
|
uploadBlob(file: File, auth?: SignedEvent | boolean): Promise<BlobDescriptor>;
|
|
64
67
|
}
|
|
68
|
+
export {};
|
package/lib/client.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import fetch from "cross-fetch";
|
|
2
|
+
import { bytesToHex } from "@noble/hashes/utils";
|
|
3
|
+
import { readFileAsArrayBuffer as readBlobAsArrayBuffer } from "./helpers.js";
|
|
2
4
|
const now = () => Math.floor(new Date().valueOf() / 1000);
|
|
3
5
|
const oneHour = () => now() + 60 * 60;
|
|
4
6
|
export const AUTH_EVENT_KIND = 24242;
|
|
@@ -24,9 +26,22 @@ export class HTTPError extends Error {
|
|
|
24
26
|
}
|
|
25
27
|
export class BlossomClient {
|
|
26
28
|
constructor(server, signer) {
|
|
27
|
-
this.server = new URL("/", server)
|
|
29
|
+
this.server = new URL("/", server);
|
|
28
30
|
this.signer = signer;
|
|
29
31
|
}
|
|
32
|
+
static async getFileSha256(file) {
|
|
33
|
+
const buffer = file instanceof File ? await file.arrayBuffer() : await readBlobAsArrayBuffer(file);
|
|
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
|
+
}
|
|
30
45
|
static async getGetAuth(signer, message = "Get Blobs", expiration = oneHour()) {
|
|
31
46
|
return await signer({
|
|
32
47
|
created_at: now(),
|
|
@@ -39,16 +54,20 @@ export class BlossomClient {
|
|
|
39
54
|
});
|
|
40
55
|
}
|
|
41
56
|
static async getUploadAuth(file, signer, message = "Upload Blob", expiration = oneHour()) {
|
|
57
|
+
const sha256 = await BlossomClient.getFileSha256(file);
|
|
58
|
+
// add tags
|
|
59
|
+
const tags = [];
|
|
60
|
+
tags.push(["t", "upload"]);
|
|
61
|
+
if (file instanceof File)
|
|
62
|
+
tags.push(["name", file.name]);
|
|
63
|
+
tags.push(["size", String(file.size)]);
|
|
64
|
+
tags.push(["x", sha256]);
|
|
65
|
+
tags.push(["expiration", String(expiration)]);
|
|
42
66
|
return await signer({
|
|
43
67
|
created_at: now(),
|
|
44
68
|
kind: AUTH_EVENT_KIND,
|
|
45
69
|
content: message,
|
|
46
|
-
tags
|
|
47
|
-
["t", "upload"],
|
|
48
|
-
["name", file.name],
|
|
49
|
-
["size", String(file.size)],
|
|
50
|
-
["expiration", String(expiration)],
|
|
51
|
-
],
|
|
70
|
+
tags,
|
|
52
71
|
});
|
|
53
72
|
}
|
|
54
73
|
static async getDeleteAuth(hash, signer, message = "Delete Blob", expiration = oneHour()) {
|
package/lib/helpers.d.ts
CHANGED
package/lib/helpers.js
CHANGED
|
@@ -7,3 +7,18 @@ export function getHashFromURL(url) {
|
|
|
7
7
|
return hashes[hashes.length - 1][0];
|
|
8
8
|
return null;
|
|
9
9
|
}
|
|
10
|
+
export function readFileAsArrayBuffer(file) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const reader = new FileReader();
|
|
13
|
+
reader.onload = (e) => {
|
|
14
|
+
const result = e.target?.result;
|
|
15
|
+
if (result == undefined || typeof result !== "object") {
|
|
16
|
+
reject();
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
resolve(result);
|
|
20
|
+
};
|
|
21
|
+
reader.onerror = (error) => reject(error);
|
|
22
|
+
reader.readAsArrayBuffer(file);
|
|
23
|
+
});
|
|
24
|
+
}
|
package/lib/image.d.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
+
type GetServersMethod = (pubkey?: string) => Promise<(string | URL)[] | undefined> | undefined;
|
|
1
2
|
/**
|
|
2
3
|
* attaches an "error" event listener to a HTMLImageElement element to handle server fallbacks
|
|
3
4
|
* @param image The image element
|
|
4
5
|
* @param getServers A async method to get an ordered list of servers for a pubkey
|
|
5
|
-
* @param
|
|
6
|
+
* @param overridePubkey An optional pubkey to set the `data-pubkey` attr on the image
|
|
7
|
+
* @returns returns a method to remove the "error" event listener
|
|
6
8
|
*/
|
|
7
|
-
export declare function handleImageFallbacks(image: HTMLImageElement, getServers:
|
|
9
|
+
export declare function handleImageFallbacks(image: HTMLImageElement, getServers: GetServersMethod, overridePubkey?: string): () => void;
|
|
10
|
+
/**
|
|
11
|
+
* Watch for any broken <img> elements in the tree and attempt to fix them
|
|
12
|
+
* @param root The root element to observe
|
|
13
|
+
* @param getServers A method used to get a list of servers for a specific pubkey
|
|
14
|
+
* @returns A MutationObserver that is observing the root element
|
|
15
|
+
*/
|
|
16
|
+
export declare function handleBrokenImages(root: HTMLElement, getServers: GetServersMethod): MutationObserver;
|
|
17
|
+
export {};
|
package/lib/image.js
CHANGED
|
@@ -3,22 +3,34 @@ import { getHashFromURL } from "./helpers.js";
|
|
|
3
3
|
* attaches an "error" event listener to a HTMLImageElement element to handle server fallbacks
|
|
4
4
|
* @param image The image element
|
|
5
5
|
* @param getServers A async method to get an ordered list of servers for a pubkey
|
|
6
|
-
* @param
|
|
6
|
+
* @param overridePubkey An optional pubkey to set the `data-pubkey` attr on the image
|
|
7
|
+
* @returns returns a method to remove the "error" event listener
|
|
7
8
|
*/
|
|
8
|
-
export function handleImageFallbacks(image, getServers,
|
|
9
|
-
|
|
10
|
-
const hash = getHashFromURL(original);
|
|
11
|
-
if (!hash)
|
|
12
|
-
return;
|
|
13
|
-
if (pubkey)
|
|
14
|
-
image.dataset.pubkey = pubkey;
|
|
15
|
-
let tried = [original.hostname];
|
|
9
|
+
export function handleImageFallbacks(image, getServers, overridePubkey) {
|
|
10
|
+
let tried = [];
|
|
16
11
|
let servers = undefined;
|
|
17
12
|
const onError = async () => {
|
|
13
|
+
const hash = getHashFromURL(image.src);
|
|
14
|
+
if (!hash)
|
|
15
|
+
return;
|
|
18
16
|
const url = new URL(image.src);
|
|
19
17
|
const ext = url.pathname.match(/\.\w+$/i);
|
|
18
|
+
tried.push(url.hostname);
|
|
19
|
+
let pubkey = overridePubkey;
|
|
20
|
+
// walk up the tree looking for pubkey
|
|
21
|
+
let el = image;
|
|
22
|
+
while (!pubkey && el.parentElement) {
|
|
23
|
+
if (el.dataset.pubkey)
|
|
24
|
+
pubkey = el.dataset.pubkey;
|
|
25
|
+
else
|
|
26
|
+
el = el.parentElement;
|
|
27
|
+
}
|
|
28
|
+
if (!pubkey) {
|
|
29
|
+
console.warn("Failed to find pubkey for broken image", image);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
20
32
|
if (!servers)
|
|
21
|
-
servers = (await getServers(
|
|
33
|
+
servers = (await getServers(pubkey))?.map((s) => (s instanceof URL ? s : new URL(s)));
|
|
22
34
|
if (servers) {
|
|
23
35
|
const server = servers.find((s) => !tried.includes(s.hostname));
|
|
24
36
|
if (server) {
|
|
@@ -26,7 +38,6 @@ export function handleImageFallbacks(image, getServers, pubkey) {
|
|
|
26
38
|
url.pathname = "/" + hash + ext;
|
|
27
39
|
url.protocol = server.protocol;
|
|
28
40
|
image.src = url.toString();
|
|
29
|
-
tried.push(url.hostname);
|
|
30
41
|
}
|
|
31
42
|
else {
|
|
32
43
|
// ran out of servers, stop listening for errors
|
|
@@ -35,4 +46,48 @@ export function handleImageFallbacks(image, getServers, pubkey) {
|
|
|
35
46
|
}
|
|
36
47
|
};
|
|
37
48
|
image.addEventListener("error", onError);
|
|
49
|
+
return () => image.removeEventListener("error", onError);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Watch for any broken <img> elements in the tree and attempt to fix them
|
|
53
|
+
* @param root The root element to observe
|
|
54
|
+
* @param getServers A method used to get a list of servers for a specific pubkey
|
|
55
|
+
* @returns A MutationObserver that is observing the root element
|
|
56
|
+
*/
|
|
57
|
+
export function handleBrokenImages(root, getServers) {
|
|
58
|
+
const listeners = new WeakMap();
|
|
59
|
+
const observer = new MutationObserver((changes) => {
|
|
60
|
+
for (const change of changes) {
|
|
61
|
+
if (change.type === "childList") {
|
|
62
|
+
// add "error" event handles to any new <img/> elements
|
|
63
|
+
change.addedNodes.forEach((el) => {
|
|
64
|
+
if (el instanceof HTMLImageElement && !listeners.has(el)) {
|
|
65
|
+
const listener = handleImageFallbacks(el, getServers);
|
|
66
|
+
listeners.set(el, listener);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
// cleanup removed nodes
|
|
70
|
+
change.removedNodes.forEach((el) => {
|
|
71
|
+
if (el instanceof HTMLImageElement) {
|
|
72
|
+
const listener = listeners.get(el);
|
|
73
|
+
if (listener) {
|
|
74
|
+
listener();
|
|
75
|
+
listeners.delete(el);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
// start watching root
|
|
83
|
+
observer.observe(root, { subtree: true, childList: true });
|
|
84
|
+
// find all existing images and attach error handler
|
|
85
|
+
const images = root.querySelectorAll("img[src]");
|
|
86
|
+
images.forEach((image) => {
|
|
87
|
+
if (image instanceof HTMLImageElement) {
|
|
88
|
+
const listener = handleImageFallbacks(image, getServers);
|
|
89
|
+
listeners.set(image, listener);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
return observer;
|
|
38
93
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blossom-client-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A simple client for manage blobs on blossom servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
+
"@noble/hashes": "^1.4.0",
|
|
42
43
|
"cross-fetch": "^4.0.0"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|