blossom-client-sdk 2.1.1 → 3.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 +36 -4
- package/lib/actions/delete.d.ts +17 -2
- package/lib/actions/delete.js +18 -6
- package/lib/actions/download.d.ts +17 -2
- package/lib/actions/download.js +16 -6
- package/lib/actions/index.d.ts +2 -0
- package/lib/actions/index.js +2 -0
- package/lib/actions/list.d.ts +13 -0
- package/lib/actions/list.js +8 -6
- package/lib/actions/media.d.ts +9 -0
- package/lib/actions/media.js +82 -0
- package/lib/actions/mirror.d.ts +19 -2
- package/lib/actions/mirror.js +12 -6
- package/lib/actions/multi-server.d.ts +66 -0
- package/lib/actions/multi-server.js +153 -0
- package/lib/actions/upload.d.ts +19 -15
- package/lib/actions/upload.js +27 -86
- package/lib/auth.d.ts +27 -21
- package/lib/auth.js +53 -77
- package/lib/client.d.ts +14 -7
- package/lib/client.js +42 -23
- package/lib/helpers/blob.d.ts +11 -0
- package/lib/{helpers.js → helpers/blob.js} +3 -21
- package/lib/helpers/cachu.d.ts +5 -0
- package/lib/helpers/cachu.js +6 -0
- package/lib/helpers/fetch.d.ts +6 -0
- package/lib/helpers/fetch.js +10 -0
- package/lib/helpers/index.d.ts +5 -0
- package/lib/helpers/index.js +5 -0
- package/lib/helpers/signal.d.ts +7 -0
- package/lib/helpers/signal.js +13 -0
- package/lib/helpers/url.d.ts +4 -0
- package/lib/helpers/url.js +15 -0
- package/lib/image.js +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/package.json +19 -5
- package/lib/helpers.d.ts +0 -19
package/lib/client.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { getBlobSha256 } from "./helpers.js";
|
|
2
|
-
import { createDeleteAuth, createDownloadAuth, createListAuth, createUploadAuth, encodeAuthorizationHeader, } from "./auth.js";
|
|
1
|
+
import { getBlobSha256 } from "./helpers/blob.js";
|
|
2
|
+
import { createDeleteAuth, createDownloadAuth, createListAuth, createMirrorAuth, createUploadAuth, encodeAuthorizationHeader, } from "./auth.js";
|
|
3
3
|
import { mirrorBlob } from "./actions/mirror.js";
|
|
4
4
|
import { uploadBlob } from "./actions/upload.js";
|
|
5
5
|
import { listBlobs } from "./actions/list.js";
|
|
6
6
|
import { downloadBlob } from "./actions/download.js";
|
|
7
7
|
import { deleteBlob } from "./actions/delete.js";
|
|
8
|
+
import { uploadMedia } from "./actions/media.js";
|
|
8
9
|
export class BlossomClient {
|
|
9
10
|
server;
|
|
10
11
|
signer;
|
|
@@ -28,20 +29,21 @@ export class BlossomClient {
|
|
|
28
29
|
static listBlobs = listBlobs;
|
|
29
30
|
static downloadBlob = downloadBlob;
|
|
30
31
|
static deleteBlob = deleteBlob;
|
|
32
|
+
static uploadMedia = uploadMedia;
|
|
31
33
|
// download blob
|
|
32
|
-
async createDownloadAuth(
|
|
34
|
+
async createDownloadAuth(hash, options) {
|
|
33
35
|
if (!this.signer)
|
|
34
36
|
throw new Error("Missing signer");
|
|
35
|
-
return await createDownloadAuth(this.signer, hash,
|
|
37
|
+
return await createDownloadAuth(this.signer, hash, options);
|
|
36
38
|
}
|
|
37
39
|
async downloadBlob(hash, opts) {
|
|
38
40
|
const options = { signal: opts?.signal };
|
|
39
41
|
// attach auth
|
|
40
42
|
if (opts?.auth !== false) {
|
|
43
|
+
if (this.signer)
|
|
44
|
+
options.onAuth = (_server, sha256) => this.createDownloadAuth(sha256, { message: `Download ${sha256}` });
|
|
41
45
|
if (typeof opts?.auth === "object")
|
|
42
46
|
options.auth = opts.auth;
|
|
43
|
-
if (this.signer)
|
|
44
|
-
options.auth = await this.createDownloadAuth(`Download ${hash}`, hash);
|
|
45
47
|
}
|
|
46
48
|
// attach payment
|
|
47
49
|
if (opts?.payment !== false && this.payment)
|
|
@@ -50,20 +52,19 @@ export class BlossomClient {
|
|
|
50
52
|
return download.blob();
|
|
51
53
|
}
|
|
52
54
|
// upload blob
|
|
53
|
-
async createUploadAuth(blob,
|
|
55
|
+
async createUploadAuth(blob, options) {
|
|
54
56
|
if (!this.signer)
|
|
55
57
|
throw new Error("Missing signer");
|
|
56
|
-
|
|
57
|
-
return await createUploadAuth(this.signer, hash, message, expiration);
|
|
58
|
+
return await createUploadAuth(this.signer, blob, options);
|
|
58
59
|
}
|
|
59
60
|
async uploadBlob(blob, opts) {
|
|
60
61
|
const options = { signal: opts?.signal };
|
|
61
62
|
// attach auth
|
|
62
63
|
if (opts?.auth !== false) {
|
|
64
|
+
if (this.signer)
|
|
65
|
+
options.onAuth = (_server, sha256, type) => this.createUploadAuth(sha256, { type });
|
|
63
66
|
if (typeof opts?.auth === "object")
|
|
64
67
|
options.auth = opts.auth;
|
|
65
|
-
if (this.signer)
|
|
66
|
-
options.auth = await this.createUploadAuth(blob);
|
|
67
68
|
}
|
|
68
69
|
// attach payment
|
|
69
70
|
if (opts?.payment !== false && this.payment)
|
|
@@ -71,25 +72,43 @@ export class BlossomClient {
|
|
|
71
72
|
return uploadBlob(this.server, blob, options);
|
|
72
73
|
}
|
|
73
74
|
// mirror blob
|
|
74
|
-
async createMirrorAuth(blob,
|
|
75
|
+
async createMirrorAuth(blob, options) {
|
|
75
76
|
if (!this.signer)
|
|
76
77
|
throw new Error("Missing signer");
|
|
77
|
-
return await
|
|
78
|
+
return await createMirrorAuth(this.signer, typeof blob === "string" ? blob : blob.sha256, options);
|
|
78
79
|
}
|
|
79
80
|
async mirrorBlob(blob, opts) {
|
|
80
81
|
const options = { signal: opts?.signal };
|
|
81
82
|
// attach auth
|
|
82
83
|
if (opts?.auth !== false) {
|
|
84
|
+
if (this.signer)
|
|
85
|
+
options.onAuth = (_server, sha256) => this.createMirrorAuth(sha256);
|
|
83
86
|
if (typeof opts?.auth === "object")
|
|
84
87
|
options.auth = opts.auth;
|
|
85
|
-
if (this.signer)
|
|
86
|
-
options.auth = await this.createMirrorAuth(blob);
|
|
87
88
|
}
|
|
88
89
|
// attach payment
|
|
89
90
|
if (opts?.payment !== false && this.payment)
|
|
90
91
|
options.onPayment = this.payment.mirror;
|
|
91
92
|
return mirrorBlob(this.server, blob, options);
|
|
92
93
|
}
|
|
94
|
+
// upload media
|
|
95
|
+
async createMediaAuth(blob, options) {
|
|
96
|
+
return await this.createUploadAuth(blob, { ...options, type: "media" });
|
|
97
|
+
}
|
|
98
|
+
async uploadMedia(blob, opts) {
|
|
99
|
+
const options = { signal: opts?.signal };
|
|
100
|
+
// attach auth
|
|
101
|
+
if (opts?.auth !== false) {
|
|
102
|
+
if (this.signer)
|
|
103
|
+
options.onAuth = (_server, sha256, type) => this.createUploadAuth(sha256, { type });
|
|
104
|
+
if (typeof opts?.auth === "object")
|
|
105
|
+
options.auth = opts.auth;
|
|
106
|
+
}
|
|
107
|
+
// attach payment
|
|
108
|
+
if (opts?.payment !== false && this.payment)
|
|
109
|
+
options.onPayment = this.payment.upload;
|
|
110
|
+
return uploadMedia(this.server, blob, options);
|
|
111
|
+
}
|
|
93
112
|
// has blob
|
|
94
113
|
static async hasBlob(server, hash) {
|
|
95
114
|
const res = await fetch(new URL(`/` + hash, server), { method: "HEAD" });
|
|
@@ -99,19 +118,19 @@ export class BlossomClient {
|
|
|
99
118
|
return BlossomClient.hasBlob(this.server, hash);
|
|
100
119
|
}
|
|
101
120
|
// list blobs
|
|
102
|
-
async createListAuth(
|
|
121
|
+
async createListAuth(options) {
|
|
103
122
|
if (!this.signer)
|
|
104
123
|
throw new Error("Missing signer");
|
|
105
|
-
return await createListAuth(this.signer,
|
|
124
|
+
return await createListAuth(this.signer, options);
|
|
106
125
|
}
|
|
107
126
|
async listBlobs(pubkey, opts) {
|
|
108
127
|
const options = { signal: opts?.signal };
|
|
109
128
|
// attach auth
|
|
110
129
|
if (opts?.auth !== false) {
|
|
130
|
+
if (this.signer)
|
|
131
|
+
options.onAuth = (_server) => this.createListAuth();
|
|
111
132
|
if (typeof opts?.auth === "object")
|
|
112
133
|
options.auth = opts.auth;
|
|
113
|
-
if (this.signer)
|
|
114
|
-
options.auth = await this.createListAuth();
|
|
115
134
|
}
|
|
116
135
|
// attach payment
|
|
117
136
|
if (opts?.payment !== false && this.payment)
|
|
@@ -119,19 +138,19 @@ export class BlossomClient {
|
|
|
119
138
|
return listBlobs(this.server, pubkey, options);
|
|
120
139
|
}
|
|
121
140
|
// delete blob
|
|
122
|
-
async createDeleteAuth(
|
|
141
|
+
async createDeleteAuth(blob, options) {
|
|
123
142
|
if (!this.signer)
|
|
124
143
|
throw new Error("Missing signer");
|
|
125
|
-
return await createDeleteAuth(this.signer,
|
|
144
|
+
return await createDeleteAuth(this.signer, blob, options);
|
|
126
145
|
}
|
|
127
146
|
async deleteBlob(hash, opts) {
|
|
128
147
|
const options = { signal: opts?.signal };
|
|
129
148
|
// attach auth
|
|
130
149
|
if (opts?.auth !== false) {
|
|
150
|
+
if (this.signer)
|
|
151
|
+
options.onAuth = (_server, sha256) => this.createDeleteAuth(sha256);
|
|
131
152
|
if (typeof opts?.auth === "object")
|
|
132
153
|
options.auth = opts.auth;
|
|
133
|
-
if (this.signer)
|
|
134
|
-
options.auth = await this.createListAuth();
|
|
135
154
|
}
|
|
136
155
|
// attach payment
|
|
137
156
|
if (opts?.payment !== false && this.payment)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UploadType } from "../client.js";
|
|
2
|
+
export declare function isSha256(str: string): RegExpMatchArray | null;
|
|
3
|
+
export declare const BlobHashSymbol: unique symbol;
|
|
4
|
+
/** gets or calculates the sha2456 of a Blob */
|
|
5
|
+
export declare function getBlobSha256(blob: UploadType): string | Promise<string>;
|
|
6
|
+
/** Calculates the sha2456 of a Blob */
|
|
7
|
+
export declare function computeBlobSha256(blob: UploadType): Promise<string>;
|
|
8
|
+
/** Returns the size of the blob in bytes */
|
|
9
|
+
export declare function getBlobSize(blob: UploadType): number;
|
|
10
|
+
/** Returns the mime type of the blob */
|
|
11
|
+
export declare function getBlobType(blob: UploadType): string | undefined;
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { bytesToHex } from "@noble/hashes/utils";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (typeof url === "string")
|
|
5
|
-
url = new URL(url);
|
|
6
|
-
const hashes = Array.from(url.pathname.matchAll(/[0-9a-f]{64}/gi));
|
|
7
|
-
if (hashes.length > 0)
|
|
8
|
-
return hashes[hashes.length - 1][0];
|
|
9
|
-
return null;
|
|
2
|
+
export function isSha256(str) {
|
|
3
|
+
return str.match(/^[0-9a-f]{64}$/);
|
|
10
4
|
}
|
|
11
5
|
export const BlobHashSymbol = Symbol.for("sha256");
|
|
12
6
|
/** gets or calculates the sha2456 of a Blob */
|
|
@@ -49,22 +43,10 @@ export function getBlobSize(blob) {
|
|
|
49
43
|
return blob.length;
|
|
50
44
|
}
|
|
51
45
|
}
|
|
52
|
-
/** Returns the
|
|
46
|
+
/** Returns the mime type of the blob */
|
|
53
47
|
export function getBlobType(blob) {
|
|
54
48
|
if (blob instanceof File || blob instanceof Blob) {
|
|
55
49
|
return blob.type;
|
|
56
50
|
}
|
|
57
51
|
return undefined;
|
|
58
52
|
}
|
|
59
|
-
/** Check if two servers are the same */
|
|
60
|
-
export function areServersEqual(a, b) {
|
|
61
|
-
const hostnameA = a instanceof URL ? a.hostname : new URL(a).hostname;
|
|
62
|
-
const hostnameB = b instanceof URL ? b.hostname : new URL(b).hostname;
|
|
63
|
-
return hostnameA === hostnameB;
|
|
64
|
-
}
|
|
65
|
-
export function getPaymentRequestFromHeaders(headers, quite = false) {
|
|
66
|
-
const header = headers.get("X-Cashu");
|
|
67
|
-
if (!header && !quite)
|
|
68
|
-
throw new Error("Missing cashu header");
|
|
69
|
-
return header ? JSON.parse(atob(header)) : undefined;
|
|
70
|
-
}
|
|
@@ -0,0 +1,5 @@
|
|
|
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;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { wrapSignalWithTimeout } from "./signal.js";
|
|
2
|
+
/** Make a fetch request with a timeout */
|
|
3
|
+
export function fetchWithTimeout(url, init) {
|
|
4
|
+
if (init?.timeout) {
|
|
5
|
+
const { cancel, signal } = wrapSignalWithTimeout(init.timeout, init.timeoutMessage ?? "Timeout", init.signal ?? undefined);
|
|
6
|
+
return fetch(url, { ...init, signal }).finally(cancel);
|
|
7
|
+
}
|
|
8
|
+
else
|
|
9
|
+
return fetch(url, init);
|
|
10
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class TimeoutError extends Error {
|
|
2
|
+
}
|
|
3
|
+
/** Creates and optionally wraps an AbortSignal with a timeout */
|
|
4
|
+
export declare function wrapSignalWithTimeout(timeout: number, message: string, signal: AbortSignal | undefined): {
|
|
5
|
+
cancel: () => void;
|
|
6
|
+
signal: AbortSignal;
|
|
7
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class TimeoutError extends Error {
|
|
2
|
+
}
|
|
3
|
+
/** Creates and optionally wraps an AbortSignal with a timeout */
|
|
4
|
+
export function wrapSignalWithTimeout(timeout, message, signal) {
|
|
5
|
+
const controller = new AbortController();
|
|
6
|
+
if (signal)
|
|
7
|
+
signal.addEventListener("abort", (err) => controller.abort(err));
|
|
8
|
+
const t = setTimeout(() => {
|
|
9
|
+
controller.abort(new TimeoutError(message));
|
|
10
|
+
}, timeout);
|
|
11
|
+
const cancel = () => clearTimeout(t);
|
|
12
|
+
return { cancel, signal: controller.signal };
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Check if two servers are the same */
|
|
2
|
+
export function areServersEqual(a, b) {
|
|
3
|
+
const hostnameA = a instanceof URL ? a.hostname : new URL(a).hostname;
|
|
4
|
+
const hostnameB = b instanceof URL ? b.hostname : new URL(b).hostname;
|
|
5
|
+
return hostnameA === hostnameB;
|
|
6
|
+
}
|
|
7
|
+
/** returns the last sha256 in a URL */
|
|
8
|
+
export function getHashFromURL(url) {
|
|
9
|
+
if (typeof url === "string")
|
|
10
|
+
url = new URL(url);
|
|
11
|
+
const hashes = Array.from(url.pathname.matchAll(/[0-9a-f]{64}/gi));
|
|
12
|
+
if (hashes.length > 0)
|
|
13
|
+
return hashes[hashes.length - 1][0];
|
|
14
|
+
return null;
|
|
15
|
+
}
|
package/lib/image.js
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export * from "./const.js";
|
|
2
2
|
export * from "./auth.js";
|
|
3
3
|
export * from "./client.js";
|
|
4
|
-
export * from "./helpers.js";
|
|
4
|
+
export * from "./helpers/index.js";
|
|
5
5
|
export * from "./image.js";
|
|
6
6
|
export * from "./nostr.js";
|
|
7
7
|
export * from "./error.js";
|
|
8
8
|
export * from "./types.js";
|
|
9
|
+
export * as Actions from "./actions/index.js";
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export * from "./const.js";
|
|
2
2
|
export * from "./auth.js";
|
|
3
3
|
export * from "./client.js";
|
|
4
|
-
export * from "./helpers.js";
|
|
4
|
+
export * from "./helpers/index.js";
|
|
5
5
|
export * from "./image.js";
|
|
6
6
|
export * from "./nostr.js";
|
|
7
7
|
export * from "./error.js";
|
|
8
8
|
export * from "./types.js";
|
|
9
|
+
export * as Actions from "./actions/index.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blossom-client-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "A simple client for manage blobs on blossom servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -25,8 +25,12 @@
|
|
|
25
25
|
"types": "./lib/image.d.ts"
|
|
26
26
|
},
|
|
27
27
|
"./helpers": {
|
|
28
|
-
"import": "./lib/helpers.js",
|
|
29
|
-
"types": "./lib/helpers.d.ts"
|
|
28
|
+
"import": "./lib/helpers/index.js",
|
|
29
|
+
"types": "./lib/helpers/index.d.ts"
|
|
30
|
+
},
|
|
31
|
+
"./helpers/*": {
|
|
32
|
+
"import": "./lib/helpers/*.js",
|
|
33
|
+
"types": "./lib/helpers/*.d.ts"
|
|
30
34
|
},
|
|
31
35
|
"./nostr": {
|
|
32
36
|
"import": "./lib/nostr.js",
|
|
@@ -54,15 +58,25 @@
|
|
|
54
58
|
},
|
|
55
59
|
"devDependencies": {
|
|
56
60
|
"@changesets/cli": "^2.27.1",
|
|
61
|
+
"@testing-library/dom": "^10.4.0",
|
|
57
62
|
"@types/node": "^22.5.5",
|
|
63
|
+
"@vitest/browser": "^2.1.8",
|
|
64
|
+
"@vitest/coverage-v8": "2.1.8",
|
|
65
|
+
"nostr-tools": "^2.10.4",
|
|
66
|
+
"playwright": "^1.49.1",
|
|
58
67
|
"prettier": "^3.3.3",
|
|
59
68
|
"typedoc": "^0.25.12",
|
|
60
|
-
"
|
|
69
|
+
"typedoc-plugin-mermaid": "^1.12.0",
|
|
70
|
+
"typescript": "^5.3.3",
|
|
71
|
+
"vitest": "^2.1.8"
|
|
61
72
|
},
|
|
62
73
|
"scripts": {
|
|
63
74
|
"build": "tsc",
|
|
64
75
|
"prepublish": "npm run build",
|
|
76
|
+
"test": "vitest run",
|
|
77
|
+
"coverage": "vitest run --coverage",
|
|
65
78
|
"format": "prettier -w .",
|
|
66
|
-
"docs": "typedoc src/index.ts"
|
|
79
|
+
"docs": "typedoc src/index.ts",
|
|
80
|
+
"test:browser": "vitest"
|
|
67
81
|
}
|
|
68
82
|
}
|
package/lib/helpers.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { type UploadType } from "./client.js";
|
|
2
|
-
import { PaymentRequest } from "./types.js";
|
|
3
|
-
/** returns the last sha256 in a URL */
|
|
4
|
-
export declare function getHashFromURL(url: string | URL): string | null;
|
|
5
|
-
export declare const BlobHashSymbol: unique symbol;
|
|
6
|
-
/** gets or calculates the sha2456 of a Blob */
|
|
7
|
-
export declare function getBlobSha256(blob: UploadType): string | Promise<string>;
|
|
8
|
-
/** Calculates the sha2456 of a Blob */
|
|
9
|
-
export declare function computeBlobSha256(blob: UploadType): Promise<string>;
|
|
10
|
-
/** Returns the size of the blob in bytes */
|
|
11
|
-
export declare function getBlobSize(blob: UploadType): number;
|
|
12
|
-
/** Returns the mimt type of the blob */
|
|
13
|
-
export declare function getBlobType(blob: UploadType): string | undefined;
|
|
14
|
-
/** Check if two servers are the same */
|
|
15
|
-
export declare function areServersEqual(a: string | URL, b: string | URL): boolean;
|
|
16
|
-
/** Extracts a cashu-ts PaymentRequest from Headers */
|
|
17
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers): PaymentRequest;
|
|
18
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers, quite: false): PaymentRequest;
|
|
19
|
-
export declare function getPaymentRequestFromHeaders(headers: Headers, quite: true): PaymentRequest | undefined;
|