blossom-client-sdk 0.4.0 → 0.5.1

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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 hzrd149
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,8 +1,85 @@
1
- # 🌸 Blossom Client
1
+ # 🌸 blossom-client-sdk
2
2
 
3
- A javascript client for manage blobs on blossom servers
3
+ A client for manage blobs on blossom servers
4
4
 
5
- ## Example
5
+ [Documentation](https://hzrd149.github.io/blossom-client-sdk/classes/BlossomClient)
6
+
7
+ ## Using the static methods
8
+
9
+ ```js
10
+ import { BlossomClient } from "blossom-client-sdk";
11
+
12
+ async function signer(event) {
13
+ return await window.nostr.signEvent(event);
14
+ }
15
+
16
+ // create an upload auth event
17
+ const uploadAuth = await BlossomClient.getUploadAuth(file, server, "Upload bitcoin.pdf");
18
+
19
+ // encode it using base64
20
+ const encodedAuthHeader = BlossomClient.encodeAuthorizationHeader(auth);
21
+
22
+ // manually make the request
23
+ const res = await fetch(new URL("/upload", server), {
24
+ method: "PUT",
25
+ body: file,
26
+ headers: { authorization: encodedAuthHeader },
27
+ });
28
+
29
+ // or use the static method
30
+ const res = await BlossomClient.uploadBlob(server, file, uploadAuth);
31
+
32
+ // check if successful
33
+ if (res.ok) {
34
+ console.log("Blob uploaded!");
35
+ }
36
+ ```
37
+
38
+ ## Using the class
39
+
40
+ The `BlossomClient` class can be used to talk to a single server
41
+
42
+ ```js
43
+ import { BlossomClient } from "blossom-client-sdk";
44
+
45
+ async function signer(event) {
46
+ return await window.nostr.signEvent(event);
47
+ }
48
+
49
+ const client = new BlossomClient("https://cdn.example.com", signer);
50
+
51
+ const pubkey = "266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5";
52
+ const blobs = await client.listBlobs(pubkey, undefined, true);
53
+ // passing true as the last argument will make it send an auth event with the list request
54
+ ```
55
+
56
+ ## Examples
57
+
58
+ ### List all blobs on a server
59
+
60
+ ```js
61
+ import { BlossomClient } from "blossom-client-sdk";
62
+
63
+ async function signer(event) {
64
+ return await window.nostr.signEvent(event);
65
+ }
66
+
67
+ const pubkey = "266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5";
68
+ const server = "https://cdn.example.com";
69
+
70
+ async function listBlobs() {
71
+ try {
72
+ return BlossomClient.listBlobs(server, pubkey);
73
+ } catch (e) {
74
+ if (e.status === 401) {
75
+ const auth = await BlossomClient.getListAuth(signer, "List Blobs from " + server);
76
+ return BlossomClient.listBlobs(server, pubkey, undefined, auth);
77
+ }
78
+ }
79
+ }
80
+ ```
81
+
82
+ ### Upload a single blob
6
83
 
7
84
  ```js
8
85
  import { BlossomClient } from "blossom-client-sdk";
@@ -17,3 +94,22 @@ const blobs = await client.listBlobs();
17
94
 
18
95
  await client.uploadBlob(new File(["testing"], "test.txt"));
19
96
  ```
97
+
98
+ ### Upload a single blob to multiple servers
99
+
100
+ ```js
101
+ import { BlossomClient } from "blossom-client-sdk";
102
+
103
+ async function signer(event) {
104
+ return await window.nostr.signEvent(event);
105
+ }
106
+
107
+ const servers = ["https://cdn.example.com", "https://cdn.other.com"];
108
+ const file = new File(["testing"], "test.txt");
109
+
110
+ const auth = await BlossomClient.getUploadAuth(file, signer, "Upload test.txt");
111
+
112
+ for (let server of servers) {
113
+ await BlossomClient.uploadBlob(server, file, auth);
114
+ }
115
+ ```
package/lib/index.d.ts CHANGED
@@ -9,9 +9,13 @@ export type SignedEvent = EventTemplate & {
9
9
  sig: string;
10
10
  pubkey: string;
11
11
  };
12
+ /** An async method used to sign nostr events */
12
13
  export type Signer = (draft: EventTemplate) => Promise<SignedEvent>;
14
+ export declare const AUTH_EVENT_KIND = 24242;
13
15
  export type BlobDescriptor = {
14
- created: number;
16
+ /** @deprecated use uploaded instead */
17
+ created?: number;
18
+ uploaded: number;
15
19
  type?: string;
16
20
  sha256: string;
17
21
  size: number;
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import fetch from "cross-fetch";
2
2
  const now = () => Math.floor(new Date().valueOf() / 1000);
3
3
  const oneHour = () => now() + 60 * 60;
4
+ export const AUTH_EVENT_KIND = 24242;
4
5
  export class HTTPError extends Error {
5
6
  constructor(response, body) {
6
7
  super(typeof body === "string" ? body : body.message);
@@ -29,7 +30,7 @@ export class BlossomClient {
29
30
  static async getGetAuth(signer, message = "Get Blobs", expiration = oneHour()) {
30
31
  return await signer({
31
32
  created_at: now(),
32
- kind: 24242,
33
+ kind: AUTH_EVENT_KIND,
33
34
  content: message,
34
35
  tags: [
35
36
  ["t", "get"],
@@ -40,7 +41,7 @@ export class BlossomClient {
40
41
  static async getUploadAuth(file, signer, message = "Upload Blob", expiration = oneHour()) {
41
42
  return await signer({
42
43
  created_at: now(),
43
- kind: 24242,
44
+ kind: AUTH_EVENT_KIND,
44
45
  content: message,
45
46
  tags: [
46
47
  ["t", "upload"],
@@ -53,7 +54,7 @@ export class BlossomClient {
53
54
  static async getDeleteAuth(hash, signer, message = "Delete Blob", expiration = oneHour()) {
54
55
  return await signer({
55
56
  created_at: now(),
56
- kind: 24242,
57
+ kind: AUTH_EVENT_KIND,
57
58
  content: message,
58
59
  tags: [
59
60
  ["t", "delete"],
@@ -65,7 +66,7 @@ export class BlossomClient {
65
66
  static async getListAuth(signer, message = "List Blobs", expiration = oneHour()) {
66
67
  return await signer({
67
68
  created_at: now(),
68
- kind: 24242,
69
+ kind: AUTH_EVENT_KIND,
69
70
  content: message,
70
71
  tags: [
71
72
  ["t", "list"],
@@ -98,9 +99,7 @@ export class BlossomClient {
98
99
  }
99
100
  static async getBlob(server, hash, auth) {
100
101
  const res = await fetch(new URL(hash, server), {
101
- headers: auth
102
- ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) }
103
- : {},
102
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
104
103
  });
105
104
  await HTTPError.handleErrorResponse(res);
106
105
  return await res.blob();
@@ -125,9 +124,7 @@ export class BlossomClient {
125
124
  if (opts?.until)
126
125
  url.searchParams.append("until", String(opts.until));
127
126
  const res = await fetch(url, {
128
- headers: auth
129
- ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) }
130
- : {},
127
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
131
128
  });
132
129
  await HTTPError.handleErrorResponse(res);
133
130
  return (await res.json());
@@ -140,9 +137,7 @@ export class BlossomClient {
140
137
  static async deleteBlob(server, hash, auth) {
141
138
  const res = await fetch(new URL("/" + hash, server), {
142
139
  method: "DELETE",
143
- headers: auth
144
- ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) }
145
- : {},
140
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
146
141
  });
147
142
  await HTTPError.handleErrorResponse(res);
148
143
  return await res.text();
@@ -156,9 +151,7 @@ export class BlossomClient {
156
151
  const res = await fetch(new URL("/upload", server), {
157
152
  method: "PUT",
158
153
  body: file,
159
- headers: auth
160
- ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) }
161
- : {},
154
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
162
155
  });
163
156
  await HTTPError.handleErrorResponse(res);
164
157
  return (await res.json());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blossom-client-sdk",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "A simple client for manage blobs on blossom servers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -19,7 +19,8 @@
19
19
  "cross-fetch": "^4.0.0"
20
20
  },
21
21
  "devDependencies": {
22
- "typescript": "^5.3.3",
23
- "prettier": "^3.2.5"
22
+ "prettier": "^3.2.5",
23
+ "typedoc": "^0.25.12",
24
+ "typescript": "^5.3.3"
24
25
  }
25
26
  }