blossom-client-sdk 0.8.0 → 0.9.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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # blossom-client-sdk
2
2
 
3
+ ## 0.9.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Add support for nodejs Buffer type
8
+
9
+ ## 0.9.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 1a5342b: Add mirror blob methods
14
+ - 24f4e57: add "x" or "server" tag on get auth
15
+
3
16
  ## 0.8.0
4
17
 
5
18
  ### Minor Changes
package/README.md CHANGED
@@ -194,3 +194,28 @@ for (let server of servers) {
194
194
  await BlossomClient.uploadBlob(server, file, auth);
195
195
  }
196
196
  ```
197
+
198
+ ### Upload and Mirror
199
+
200
+ ```js
201
+ import { BlossomClient } from "blossom-client-sdk";
202
+
203
+ async function signer(event) {
204
+ return await window.nostr.signEvent(event);
205
+ }
206
+
207
+ const mainServer = "https://cdn.server-a.com";
208
+ const mirrorServers = ["https://cdn.example.com", "https://cdn.other.com"];
209
+ const file = new File(["testing"], "test.txt");
210
+
211
+ const auth = await BlossomClient.getUploadAuth(file, signer, "Upload test.txt");
212
+
213
+ // first upload blob to main server
214
+ const blob = await BlossomClient.uploadBlob(mainServer, file, auth);
215
+
216
+ // then tell mirror servers to download it
217
+ for (let server of mirrorServers) {
218
+ // reuse the same auth for mirroring
219
+ await BlossomClient.mirrorBlob(server, blob.url, auth);
220
+ }
221
+ ```
package/lib/client.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" resolution-mode="require"/>
1
2
  export type EventTemplate = {
2
3
  created_at: number;
3
4
  kind: number;
@@ -33,36 +34,40 @@ export declare class HTTPError extends Error {
33
34
  static handleErrorResponse(res: Response): Promise<void>;
34
35
  }
35
36
  type ServerType = string | URL;
36
- type UploadType = Blob | File;
37
+ type UploadType = Blob | File | Buffer;
37
38
  export declare class BlossomClient {
38
39
  server: URL;
39
40
  signer?: Signer;
40
41
  constructor(server: string | URL, signer?: Signer);
41
42
  static getFileSha256(file: UploadType): Promise<string>;
42
- static getGetAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
43
- static getUploadAuth(file: UploadType, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
44
- static getDeleteAuth(hash: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
45
- static getListAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
46
- getGetAuth(message?: string, expiration?: number): Promise<SignedEvent>;
47
- getUploadAuth(file: UploadType, message?: string, expiration?: number): Promise<SignedEvent>;
48
- getDeleteAuth(hash: string, message?: string, expiration?: number): Promise<SignedEvent>;
49
- getListAuth(message?: string, expiration?: number): Promise<SignedEvent>;
50
43
  static encodeAuthorizationHeader(event: SignedEvent): string;
44
+ static getGetAuth(signer: Signer, message: string, serverOrHash: string, expiration?: number): Promise<SignedEvent>;
51
45
  static getBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<Blob>;
52
- getBlob(hash: string, auth?: SignedEvent | boolean): Promise<Blob>;
53
- static hasBlob(server: ServerType, hash: string): Promise<boolean>;
54
- hasBlob(hash: string): Promise<boolean>;
46
+ static createUploadAuth(sha256: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
47
+ static getUploadAuth(file: UploadType, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
48
+ static uploadBlob(server: ServerType, file: UploadType, auth?: SignedEvent): Promise<BlobDescriptor>;
49
+ static mirrorBlob(server: ServerType, url: string | URL, auth?: SignedEvent): Promise<BlobDescriptor>;
50
+ static getListAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
55
51
  static listBlobs(server: ServerType, pubkey: string, opts?: {
56
52
  since?: number;
57
53
  until?: number;
58
54
  }, auth?: SignedEvent): Promise<BlobDescriptor[]>;
55
+ static getDeleteAuth(hash: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
56
+ static deleteBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<string>;
57
+ getGetAuth(message: string, serverOrHash: string, expiration?: number): Promise<SignedEvent>;
58
+ getBlob(hash: string, auth?: SignedEvent | boolean): Promise<Blob>;
59
+ getUploadAuth(file: UploadType, message?: string, expiration?: number): Promise<SignedEvent>;
60
+ uploadBlob(file: UploadType, auth?: SignedEvent | boolean): Promise<BlobDescriptor>;
61
+ getMirrorAuth(sha256: string, message?: string, expiration?: number): Promise<SignedEvent>;
62
+ mirrorBlob(sha256: string, url: string | URL, auth?: SignedEvent | boolean): Promise<BlobDescriptor>;
63
+ static hasBlob(server: ServerType, hash: string): Promise<boolean>;
64
+ hasBlob(hash: string): Promise<boolean>;
65
+ getListAuth(message?: string, expiration?: number): Promise<SignedEvent>;
59
66
  listBlobs(pubkey: string, opts?: {
60
67
  since?: number;
61
68
  until?: number;
62
69
  }, auth?: SignedEvent | boolean): Promise<BlobDescriptor[]>;
63
- static deleteBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<string>;
70
+ getDeleteAuth(hash: string, message?: string, expiration?: number): Promise<SignedEvent>;
64
71
  deleteBlob(hash: string, auth?: SignedEvent | boolean): Promise<string>;
65
- static uploadBlob(server: ServerType, file: UploadType, auth?: SignedEvent): Promise<BlobDescriptor>;
66
- uploadBlob(file: File, auth?: SignedEvent | boolean): Promise<BlobDescriptor>;
67
72
  }
68
73
  export {};
package/lib/client.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import fetch from "cross-fetch";
2
2
  import { bytesToHex } from "@noble/hashes/utils";
3
- import { readFileAsArrayBuffer as readBlobAsArrayBuffer } from "./helpers.js";
4
3
  const now = () => Math.floor(new Date().valueOf() / 1000);
5
4
  const oneHour = () => now() + 60 * 60;
6
5
  export const AUTH_EVENT_KIND = 24242;
@@ -30,7 +29,14 @@ export class BlossomClient {
30
29
  this.signer = signer;
31
30
  }
32
31
  static async getFileSha256(file) {
33
- const buffer = file instanceof File ? await file.arrayBuffer() : await readBlobAsArrayBuffer(file);
32
+ let buffer;
33
+ if (file instanceof File || file instanceof Blob) {
34
+ buffer = await file.arrayBuffer();
35
+ }
36
+ else {
37
+ // nodejs Buffer
38
+ buffer = file.buffer;
39
+ }
34
40
  let hash;
35
41
  if (crypto.subtle) {
36
42
  const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
@@ -42,25 +48,35 @@ export class BlossomClient {
42
48
  }
43
49
  return bytesToHex(hash);
44
50
  }
45
- static async getGetAuth(signer, message = "Get Blobs", expiration = oneHour()) {
51
+ static encodeAuthorizationHeader(event) {
52
+ return "Nostr " + btoa(JSON.stringify(event));
53
+ }
54
+ // static get blob
55
+ static async getGetAuth(signer, message, serverOrHash, expiration = oneHour()) {
56
+ let scopeTag;
57
+ if (serverOrHash.match(/^[0-9a-f]{64}$/)) {
58
+ scopeTag = ["x", serverOrHash];
59
+ }
60
+ else
61
+ scopeTag = ["server", new URL("/", serverOrHash).toString()];
46
62
  return await signer({
47
63
  created_at: now(),
48
64
  kind: AUTH_EVENT_KIND,
49
65
  content: message,
50
- tags: [
51
- ["t", "get"],
52
- ["expiration", String(expiration)],
53
- ],
66
+ tags: [["t", "get"], ["expiration", String(expiration)], scopeTag],
54
67
  });
55
68
  }
56
- static async getUploadAuth(file, signer, message = "Upload Blob", expiration = oneHour()) {
57
- const sha256 = await BlossomClient.getFileSha256(file);
58
- // add tags
69
+ static async getBlob(server, hash, auth) {
70
+ const res = await fetch(new URL(hash, server), {
71
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
72
+ });
73
+ await HTTPError.handleErrorResponse(res);
74
+ return await res.blob();
75
+ }
76
+ // static upload blob
77
+ static async createUploadAuth(sha256, signer, message = "Upload Blob", expiration = oneHour()) {
59
78
  const tags = [];
60
79
  tags.push(["t", "upload"]);
61
- if (file instanceof File)
62
- tags.push(["name", file.name]);
63
- tags.push(["size", String(file.size)]);
64
80
  tags.push(["x", sha256]);
65
81
  tags.push(["expiration", String(expiration)]);
66
82
  return await signer({
@@ -70,64 +86,108 @@ export class BlossomClient {
70
86
  tags,
71
87
  });
72
88
  }
73
- static async getDeleteAuth(hash, signer, message = "Delete Blob", expiration = oneHour()) {
89
+ static async getUploadAuth(file, signer, message = "Upload Blob", expiration = oneHour()) {
90
+ const sha256 = await BlossomClient.getFileSha256(file);
91
+ return await BlossomClient.createUploadAuth(sha256, signer, message, expiration);
92
+ }
93
+ static async uploadBlob(server, file, auth) {
94
+ const res = await fetch(new URL("/upload", server), {
95
+ method: "PUT",
96
+ body: file,
97
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
98
+ });
99
+ await HTTPError.handleErrorResponse(res);
100
+ return (await res.json());
101
+ }
102
+ // static mirror blob
103
+ static async mirrorBlob(server, url, auth) {
104
+ const res = await fetch(new URL("/mirror", server), {
105
+ method: "PUT",
106
+ body: JSON.stringify({ url: url.toString() }),
107
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
108
+ });
109
+ await HTTPError.handleErrorResponse(res);
110
+ return (await res.json());
111
+ }
112
+ // static list blobs
113
+ static async getListAuth(signer, message = "List Blobs", expiration = oneHour()) {
74
114
  return await signer({
75
115
  created_at: now(),
76
116
  kind: AUTH_EVENT_KIND,
77
117
  content: message,
78
118
  tags: [
79
- ["t", "delete"],
80
- ["x", hash],
119
+ ["t", "list"],
81
120
  ["expiration", String(expiration)],
82
121
  ],
83
122
  });
84
123
  }
85
- static async getListAuth(signer, message = "List Blobs", expiration = oneHour()) {
124
+ static async listBlobs(server, pubkey, opts, auth) {
125
+ const url = new URL(`/list/` + pubkey, server);
126
+ if (opts?.since)
127
+ url.searchParams.append("since", String(opts.since));
128
+ if (opts?.until)
129
+ url.searchParams.append("until", String(opts.until));
130
+ const res = await fetch(url, {
131
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
132
+ });
133
+ await HTTPError.handleErrorResponse(res);
134
+ return (await res.json());
135
+ }
136
+ // static delete blob
137
+ static async getDeleteAuth(hash, signer, message = "Delete Blob", expiration = oneHour()) {
86
138
  return await signer({
87
139
  created_at: now(),
88
140
  kind: AUTH_EVENT_KIND,
89
141
  content: message,
90
142
  tags: [
91
- ["t", "list"],
143
+ ["t", "delete"],
144
+ ["x", hash],
92
145
  ["expiration", String(expiration)],
93
146
  ],
94
147
  });
95
148
  }
96
- async getGetAuth(message, expiration) {
149
+ static async deleteBlob(server, hash, auth) {
150
+ const res = await fetch(new URL("/" + hash, server), {
151
+ method: "DELETE",
152
+ headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
153
+ });
154
+ await HTTPError.handleErrorResponse(res);
155
+ return await res.text();
156
+ }
157
+ // get blob
158
+ async getGetAuth(message, serverOrHash, expiration) {
97
159
  if (!this.signer)
98
160
  throw new Error("Missing signer");
99
- return await BlossomClient.getGetAuth(this.signer, message, expiration);
161
+ return await BlossomClient.getGetAuth(this.signer, message, serverOrHash, expiration);
100
162
  }
163
+ async getBlob(hash, auth = false) {
164
+ if (typeof auth === "boolean" && auth)
165
+ auth = await this.getGetAuth("Get Blob", hash);
166
+ return BlossomClient.getBlob(this.server, hash, auth ? auth : undefined);
167
+ }
168
+ // upload blob
101
169
  async getUploadAuth(file, message, expiration) {
102
170
  if (!this.signer)
103
171
  throw new Error("Missing signer");
104
172
  return await BlossomClient.getUploadAuth(file, this.signer, message, expiration);
105
173
  }
106
- async getDeleteAuth(hash, message, expiration) {
107
- if (!this.signer)
108
- throw new Error("Missing signer");
109
- return await BlossomClient.getDeleteAuth(hash, this.signer, message, expiration);
174
+ async uploadBlob(file, auth = true) {
175
+ if (typeof auth === "boolean" && auth)
176
+ auth = await this.getUploadAuth(file);
177
+ return BlossomClient.uploadBlob(this.server, file, auth ? auth : undefined);
110
178
  }
111
- async getListAuth(message, expiration) {
179
+ // mirror blob
180
+ async getMirrorAuth(sha256, message, expiration) {
112
181
  if (!this.signer)
113
182
  throw new Error("Missing signer");
114
- return await BlossomClient.getListAuth(this.signer, message, expiration);
115
- }
116
- static encodeAuthorizationHeader(event) {
117
- return "Nostr " + btoa(JSON.stringify(event));
183
+ return await BlossomClient.createUploadAuth(sha256, this.signer, message, expiration);
118
184
  }
119
- static async getBlob(server, hash, auth) {
120
- const res = await fetch(new URL(hash, server), {
121
- headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
122
- });
123
- await HTTPError.handleErrorResponse(res);
124
- return await res.blob();
125
- }
126
- async getBlob(hash, auth = false) {
185
+ async mirrorBlob(sha256, url, auth = true) {
127
186
  if (typeof auth === "boolean" && auth)
128
- auth = await this.getGetAuth();
129
- return BlossomClient.getBlob(this.server, hash, auth ? auth : undefined);
187
+ auth = await this.getMirrorAuth(sha256);
188
+ return BlossomClient.mirrorBlob(this.server, url, auth ? auth : undefined);
130
189
  }
190
+ // has blob
131
191
  static async hasBlob(server, hash) {
132
192
  const res = await fetch(new URL(`/` + hash, server), { method: "HEAD" });
133
193
  await HTTPError.handleErrorResponse(res);
@@ -136,48 +196,26 @@ export class BlossomClient {
136
196
  async hasBlob(hash) {
137
197
  return BlossomClient.hasBlob(this.server, hash);
138
198
  }
139
- static async listBlobs(server, pubkey, opts, auth) {
140
- const url = new URL(`/list/` + pubkey, server);
141
- if (opts?.since)
142
- url.searchParams.append("since", String(opts.since));
143
- if (opts?.until)
144
- url.searchParams.append("until", String(opts.until));
145
- const res = await fetch(url, {
146
- headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
147
- });
148
- await HTTPError.handleErrorResponse(res);
149
- return (await res.json());
199
+ // list blobs
200
+ async getListAuth(message, expiration) {
201
+ if (!this.signer)
202
+ throw new Error("Missing signer");
203
+ return await BlossomClient.getListAuth(this.signer, message, expiration);
150
204
  }
151
205
  async listBlobs(pubkey, opts, auth = false) {
152
206
  if (typeof auth === "boolean" && auth)
153
207
  auth = await this.getListAuth();
154
208
  return BlossomClient.listBlobs(this.server, pubkey, opts, auth ? auth : undefined);
155
209
  }
156
- static async deleteBlob(server, hash, auth) {
157
- const res = await fetch(new URL("/" + hash, server), {
158
- method: "DELETE",
159
- headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
160
- });
161
- await HTTPError.handleErrorResponse(res);
162
- return await res.text();
210
+ // delete blob
211
+ async getDeleteAuth(hash, message, expiration) {
212
+ if (!this.signer)
213
+ throw new Error("Missing signer");
214
+ return await BlossomClient.getDeleteAuth(hash, this.signer, message, expiration);
163
215
  }
164
216
  async deleteBlob(hash, auth = true) {
165
217
  if (typeof auth === "boolean" && auth)
166
218
  auth = await this.getDeleteAuth(hash);
167
219
  return BlossomClient.deleteBlob(this.server, hash, auth ? auth : undefined);
168
220
  }
169
- static async uploadBlob(server, file, auth) {
170
- const res = await fetch(new URL("/upload", server), {
171
- method: "PUT",
172
- body: file,
173
- headers: auth ? { authorization: BlossomClient.encodeAuthorizationHeader(auth) } : {},
174
- });
175
- await HTTPError.handleErrorResponse(res);
176
- return (await res.json());
177
- }
178
- async uploadBlob(file, auth = true) {
179
- if (typeof auth === "boolean" && auth)
180
- auth = await this.getUploadAuth(file);
181
- return BlossomClient.uploadBlob(this.server, file, auth ? auth : undefined);
182
- }
183
221
  }
package/lib/helpers.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  /** returns the last sha256 in a URL */
2
2
  export declare function getHashFromURL(url: string | URL): string | null;
3
- export declare function readFileAsArrayBuffer(file: Blob | File): Promise<ArrayBuffer>;
package/lib/helpers.js CHANGED
@@ -7,18 +7,3 @@ 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blossom-client-sdk",
3
- "version": "0.8.0",
3
+ "version": "0.9.1",
4
4
  "description": "A simple client for manage blobs on blossom servers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "devDependencies": {
46
46
  "@changesets/cli": "^2.27.1",
47
- "prettier": "^3.2.5",
47
+ "prettier": "^3.3.3",
48
48
  "typedoc": "^0.25.12",
49
49
  "typescript": "^5.3.3"
50
50
  }