blossom-client-sdk 1.0.1 → 1.1.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/lib/client.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  export type EventTemplate = {
3
2
  created_at: number;
4
3
  kind: number;
@@ -41,9 +40,26 @@ export declare class BlossomClient {
41
40
  constructor(server: string | URL, signer?: Signer);
42
41
  static getFileSha256(file: UploadType): Promise<string>;
43
42
  static encodeAuthorizationHeader(event: SignedEvent): string;
44
- static getGetAuth(signer: Signer, message: string, serverOrHash: string, expiration?: number): Promise<SignedEvent>;
43
+ /**
44
+ * Creates a get auth event
45
+ * @param signer the signer to use for signing the event
46
+ * @param message A human readable explanation of what the auth token will be used for
47
+ * @param serverOrHash A server URL or one or many blob hashes
48
+ * @param expiration The expiration time in seconds
49
+ * @returns {Promise<SignedEvent>}
50
+ */
51
+ static getGetAuth(signer: Signer, message: string, serverOrHash: string | string[], expiration?: number): Promise<SignedEvent>;
45
52
  static getBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<Blob>;
46
- static createUploadAuth(sha256: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
53
+ /**
54
+ * Creates an upload auth event
55
+ * @param sha256 one or an array of sha256 hashes
56
+ * @param signer the signer to use for signing the event
57
+ * @param message A human readable explanation of what the auth token will be used for
58
+ * @param expiration The expiration time in seconds
59
+ * @returns {Promise<SignedEvent>}
60
+ */
61
+ static createUploadAuth(sha256: string | string[], signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
62
+ /** Creates a one-off upload auth event for a file */
47
63
  static getUploadAuth(file: UploadType, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
48
64
  static uploadBlob(server: ServerType, file: UploadType, auth?: SignedEvent): Promise<BlobDescriptor>;
49
65
  static mirrorBlob(server: ServerType, url: string | URL, auth?: SignedEvent): Promise<BlobDescriptor>;
@@ -52,7 +68,7 @@ export declare class BlossomClient {
52
68
  since?: number;
53
69
  until?: number;
54
70
  }, auth?: SignedEvent): Promise<BlobDescriptor[]>;
55
- static getDeleteAuth(hash: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
71
+ static getDeleteAuth(hash: string | string[], signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
56
72
  static deleteBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<string>;
57
73
  getGetAuth(message: string, serverOrHash: string, expiration?: number): Promise<SignedEvent>;
58
74
  getBlob(hash: string, auth?: SignedEvent | boolean): Promise<Blob>;
package/lib/client.js CHANGED
@@ -39,7 +39,7 @@ export class BlossomClient {
39
39
  }
40
40
  else {
41
41
  // nodejs Buffer
42
- buffer = file.buffer;
42
+ buffer = file;
43
43
  }
44
44
  let hash;
45
45
  if (crypto.subtle) {
@@ -55,20 +55,35 @@ export class BlossomClient {
55
55
  static encodeAuthorizationHeader(event) {
56
56
  return "Nostr " + btoa(JSON.stringify(event));
57
57
  }
58
- // static get blob
58
+ /**
59
+ * Creates a get auth event
60
+ * @param signer the signer to use for signing the event
61
+ * @param message A human readable explanation of what the auth token will be used for
62
+ * @param serverOrHash A server URL or one or many blob hashes
63
+ * @param expiration The expiration time in seconds
64
+ * @returns {Promise<SignedEvent>}
65
+ */
59
66
  static async getGetAuth(signer, message, serverOrHash, expiration = oneHour()) {
60
- let scopeTag;
61
- if (serverOrHash.match(/^[0-9a-f]{64}$/)) {
62
- scopeTag = ["x", serverOrHash];
63
- }
64
- else
65
- scopeTag = ["server", new URL("/", serverOrHash).toString()];
66
- return await signer({
67
+ const draft = {
67
68
  created_at: now(),
68
69
  kind: AUTH_EVENT_KIND,
69
70
  content: message,
70
- tags: [["t", "get"], ["expiration", String(expiration)], scopeTag],
71
- });
71
+ tags: [
72
+ ["t", "get"],
73
+ ["expiration", String(expiration)],
74
+ ],
75
+ };
76
+ if (Array.isArray(serverOrHash)) {
77
+ for (const sha256 of serverOrHash)
78
+ draft.tags.push(["x", sha256]);
79
+ }
80
+ else if (serverOrHash.match(/^[0-9a-f]{64}$/)) {
81
+ draft.tags.push(["x", serverOrHash]);
82
+ }
83
+ else {
84
+ draft.tags.push(["server", new URL("/", serverOrHash).toString()]);
85
+ }
86
+ return await signer(draft);
72
87
  }
73
88
  static async getBlob(server, hash, auth) {
74
89
  const res = await fetch(new URL(hash, server), {
@@ -77,19 +92,33 @@ export class BlossomClient {
77
92
  await HTTPError.handleErrorResponse(res);
78
93
  return await res.blob();
79
94
  }
80
- // static upload blob
95
+ /**
96
+ * Creates an upload auth event
97
+ * @param sha256 one or an array of sha256 hashes
98
+ * @param signer the signer to use for signing the event
99
+ * @param message A human readable explanation of what the auth token will be used for
100
+ * @param expiration The expiration time in seconds
101
+ * @returns {Promise<SignedEvent>}
102
+ */
81
103
  static async createUploadAuth(sha256, signer, message = "Upload Blob", expiration = oneHour()) {
82
- const tags = [];
83
- tags.push(["t", "upload"]);
84
- tags.push(["x", sha256]);
85
- tags.push(["expiration", String(expiration)]);
86
- return await signer({
87
- created_at: now(),
104
+ const draft = {
88
105
  kind: AUTH_EVENT_KIND,
89
106
  content: message,
90
- tags,
91
- });
107
+ created_at: now(),
108
+ tags: [
109
+ ["t", "upload"],
110
+ ["expiration", String(expiration)],
111
+ ],
112
+ };
113
+ if (Array.isArray(sha256)) {
114
+ for (const hash of sha256)
115
+ draft.tags.push(["x", hash]);
116
+ }
117
+ else
118
+ draft.tags.push(["x", sha256]);
119
+ return await signer(draft);
92
120
  }
121
+ /** Creates a one-off upload auth event for a file */
93
122
  static async getUploadAuth(file, signer, message = "Upload Blob", expiration = oneHour()) {
94
123
  const sha256 = await BlossomClient.getFileSha256(file);
95
124
  return await BlossomClient.createUploadAuth(sha256, signer, message, expiration);
@@ -139,16 +168,22 @@ export class BlossomClient {
139
168
  }
140
169
  // static delete blob
141
170
  static async getDeleteAuth(hash, signer, message = "Delete Blob", expiration = oneHour()) {
142
- return await signer({
171
+ const draft = {
143
172
  created_at: now(),
144
173
  kind: AUTH_EVENT_KIND,
145
174
  content: message,
146
175
  tags: [
147
176
  ["t", "delete"],
148
- ["x", hash],
149
177
  ["expiration", String(expiration)],
150
178
  ],
151
- });
179
+ };
180
+ if (Array.isArray(hash)) {
181
+ for (const x of hash)
182
+ draft.tags.push(["x", x]);
183
+ }
184
+ else
185
+ draft.tags.push(["x", hash]);
186
+ return await signer(draft);
152
187
  }
153
188
  static async deleteBlob(server, hash, auth) {
154
189
  const res = await fetch(new URL("/" + hash, server), {
package/lib/upload.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { BlobDescriptor, ServerType, Signer, UploadType } from "./client.js";
1
+ import { BlobDescriptor, ServerType, SignedEvent, Signer, UploadType } from "./client.js";
2
2
  /**
3
3
  * Creates an AsyncGenerator that can be used to upload a blob to multiple servers
4
4
  * @param servers A Set or Array of servers to upload to
@@ -6,7 +6,7 @@ import { BlobDescriptor, ServerType, Signer, UploadType } from "./client.js";
6
6
  * @param signer An async function used for signing nostr events
7
7
  * @returns The BlobDescriptor if successful
8
8
  */
9
- export declare function multiServerUpload(servers: Iterable<ServerType>, file: UploadType, signer: Signer): AsyncGenerator<{
9
+ export declare function multiServerUpload(servers: Iterable<ServerType>, file: UploadType, signer: Signer, auth?: SignedEvent): AsyncGenerator<{
10
10
  blob: BlobDescriptor;
11
11
  progress: number;
12
12
  server: ServerType;
package/lib/upload.js CHANGED
@@ -6,8 +6,8 @@ import { BlossomClient } from "./client.js";
6
6
  * @param signer An async function used for signing nostr events
7
7
  * @returns The BlobDescriptor if successful
8
8
  */
9
- export async function* multiServerUpload(servers, file, signer) {
10
- const auth = await BlossomClient.getUploadAuth(file, signer);
9
+ export async function* multiServerUpload(servers, file, signer, auth) {
10
+ auth = auth || (await BlossomClient.getUploadAuth(file, signer));
11
11
  let blob = undefined;
12
12
  let total = Array.from(servers).length;
13
13
  let uploaded = 0;
package/package.json CHANGED
@@ -1,18 +1,12 @@
1
1
  {
2
2
  "name": "blossom-client-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "A simple client for manage blobs on blossom servers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
7
7
  "typings": "./lib/index.d.ts",
8
8
  "author": "hzrd149",
9
9
  "license": "MIT",
10
- "scripts": {
11
- "build": "tsc",
12
- "prepublish": "npm run build",
13
- "format": "prettier -w .",
14
- "docs": "typedoc src/index.ts"
15
- },
16
10
  "files": [
17
11
  "lib"
18
12
  ],
@@ -50,8 +44,15 @@
50
44
  },
51
45
  "devDependencies": {
52
46
  "@changesets/cli": "^2.27.1",
47
+ "@types/node": "^22.5.5",
53
48
  "prettier": "^3.3.3",
54
49
  "typedoc": "^0.25.12",
55
50
  "typescript": "^5.3.3"
51
+ },
52
+ "scripts": {
53
+ "build": "tsc",
54
+ "prepublish": "npm run build",
55
+ "format": "prettier -w .",
56
+ "docs": "typedoc src/index.ts"
56
57
  }
57
- }
58
+ }
package/CHANGELOG.md DELETED
@@ -1,52 +0,0 @@
1
- # blossom-client-sdk
2
-
3
- ## 1.0.1
4
-
5
- ### Patch Changes
6
-
7
- - Add upload exports to package.json
8
-
9
- ## 1.0.0
10
-
11
- ### Major Changes
12
-
13
- - 8350d95: Remove dependency on cross-fetch and require NodeJS >= 18 for native `fetch` API
14
-
15
- ### Minor Changes
16
-
17
- - 383a13b: Add `multiServerUpload` method
18
-
19
- ## 0.9.1
20
-
21
- ### Patch Changes
22
-
23
- - Add support for nodejs Buffer type
24
-
25
- ## 0.9.0
26
-
27
- ### Minor Changes
28
-
29
- - 1a5342b: Add mirror blob methods
30
- - 24f4e57: add "x" or "server" tag on get auth
31
-
32
- ## 0.8.0
33
-
34
- ### Minor Changes
35
-
36
- - Add `handleBrokenImages` method for repairing any broken images in an app
37
-
38
- ## 0.7.0
39
-
40
- ### Minor Changes
41
-
42
- - Include "x" tag with sha256 in upload auth
43
-
44
- ### Patch Changes
45
-
46
- - Allow URL class for server
47
-
48
- ## 0.7.0
49
-
50
- ### Minor Changes
51
-
52
- - e2e93f5: Add `handleImageFallbacks` method