blossom-client-sdk 0.9.1 → 1.0.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 +16 -0
- package/README.md +50 -1
- package/lib/client.d.ts +2 -3
- package/lib/client.js +5 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/upload.d.ts +13 -0
- package/lib/upload.js +37 -0
- package/package.json +9 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# blossom-client-sdk
|
|
2
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
|
+
|
|
3
19
|
## 0.9.1
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -195,7 +195,56 @@ for (let server of servers) {
|
|
|
195
195
|
}
|
|
196
196
|
```
|
|
197
197
|
|
|
198
|
-
###
|
|
198
|
+
### Uploading and mirroring to multiple servers
|
|
199
|
+
|
|
200
|
+
The `multiServerUpload` method can be used to upload a single blob to multiple servers
|
|
201
|
+
|
|
202
|
+
Example of uploading to each server one at time
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
import { multiServerUpload } from "blossom-server-sdk";
|
|
206
|
+
|
|
207
|
+
async function signer(event: any) {
|
|
208
|
+
// @ts-expect-error
|
|
209
|
+
return await window.nostr.signEvent(event);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const servers = ["https://cdn.server-a.com", "https://cdn.example.com", "https://cdn.other.com"];
|
|
213
|
+
const file = new File(["testing"], "test.txt");
|
|
214
|
+
|
|
215
|
+
// create async generator for upload
|
|
216
|
+
const upload = multiServerUpload(servers, file, signer);
|
|
217
|
+
|
|
218
|
+
let successful = 0;
|
|
219
|
+
|
|
220
|
+
while (true) {
|
|
221
|
+
let result: Awaited<ReturnType<typeof upload.next>> | undefined = undefined;
|
|
222
|
+
|
|
223
|
+
// attempt to upload
|
|
224
|
+
try {
|
|
225
|
+
result = await upload.next();
|
|
226
|
+
successful++;
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (error instanceof Error) console.log(`Failed to upload ${error.message}`);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (result) {
|
|
232
|
+
// if upload was successful log progress
|
|
233
|
+
if (!result.done) {
|
|
234
|
+
console.log(`Uploaded to ${result.value.server} ${result.value.progress * 100}%`);
|
|
235
|
+
} else if (result.done && result.value) {
|
|
236
|
+
console.log(`Successfully uploaded ${result.value.sha256} to ${servers} servers`);
|
|
237
|
+
|
|
238
|
+
// exit while loop (important)
|
|
239
|
+
break;
|
|
240
|
+
} else {
|
|
241
|
+
throw Error("Failed to upload blob to any servers");
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Upload and Mirror manually
|
|
199
248
|
|
|
200
249
|
```js
|
|
201
250
|
import { BlossomClient } from "blossom-client-sdk";
|
package/lib/client.d.ts
CHANGED
|
@@ -33,8 +33,8 @@ export declare class HTTPError extends Error {
|
|
|
33
33
|
} | string);
|
|
34
34
|
static handleErrorResponse(res: Response): Promise<void>;
|
|
35
35
|
}
|
|
36
|
-
type ServerType = string | URL;
|
|
37
|
-
type UploadType = Blob | File | Buffer;
|
|
36
|
+
export type ServerType = string | URL;
|
|
37
|
+
export type UploadType = Blob | File | Buffer;
|
|
38
38
|
export declare class BlossomClient {
|
|
39
39
|
server: URL;
|
|
40
40
|
signer?: Signer;
|
|
@@ -70,4 +70,3 @@ export declare class BlossomClient {
|
|
|
70
70
|
getDeleteAuth(hash: string, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
71
71
|
deleteBlob(hash: string, auth?: SignedEvent | boolean): Promise<string>;
|
|
72
72
|
}
|
|
73
|
-
export {};
|
package/lib/client.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import fetch from "cross-fetch";
|
|
2
1
|
import { bytesToHex } from "@noble/hashes/utils";
|
|
3
2
|
const now = () => Math.floor(new Date().valueOf() / 1000);
|
|
4
3
|
const oneHour = () => now() + 60 * 60;
|
|
5
4
|
export const AUTH_EVENT_KIND = 24242;
|
|
6
5
|
export class HTTPError extends Error {
|
|
6
|
+
response;
|
|
7
|
+
status;
|
|
8
|
+
body;
|
|
7
9
|
constructor(response, body) {
|
|
8
10
|
super(typeof body === "string" ? body : body.message);
|
|
9
11
|
this.response = response;
|
|
@@ -24,6 +26,8 @@ export class HTTPError extends Error {
|
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
export class BlossomClient {
|
|
29
|
+
server;
|
|
30
|
+
signer;
|
|
27
31
|
constructor(server, signer) {
|
|
28
32
|
this.server = new URL("/", server);
|
|
29
33
|
this.signer = signer;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/lib/upload.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BlobDescriptor, ServerType, Signer, UploadType } from "./client.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates an AsyncGenerator that can be used to upload a blob to multiple servers
|
|
4
|
+
* @param servers A Set or Array of servers to upload to
|
|
5
|
+
* @param file The blob to be uploaded
|
|
6
|
+
* @param signer An async function used for signing nostr events
|
|
7
|
+
* @returns The BlobDescriptor if successful
|
|
8
|
+
*/
|
|
9
|
+
export declare function multiServerUpload(servers: Iterable<ServerType>, file: UploadType, signer: Signer): AsyncGenerator<{
|
|
10
|
+
blob: BlobDescriptor;
|
|
11
|
+
progress: number;
|
|
12
|
+
server: ServerType;
|
|
13
|
+
}, BlobDescriptor | undefined, unknown>;
|
package/lib/upload.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { BlossomClient } from "./client.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates an AsyncGenerator that can be used to upload a blob to multiple servers
|
|
4
|
+
* @param servers A Set or Array of servers to upload to
|
|
5
|
+
* @param file The blob to be uploaded
|
|
6
|
+
* @param signer An async function used for signing nostr events
|
|
7
|
+
* @returns The BlobDescriptor if successful
|
|
8
|
+
*/
|
|
9
|
+
export async function* multiServerUpload(servers, file, signer) {
|
|
10
|
+
const auth = await BlossomClient.getUploadAuth(file, signer);
|
|
11
|
+
let blob = undefined;
|
|
12
|
+
let total = Array.from(servers).length;
|
|
13
|
+
let uploaded = 0;
|
|
14
|
+
for (const server of servers) {
|
|
15
|
+
if (blob) {
|
|
16
|
+
// mirror blob to server
|
|
17
|
+
try {
|
|
18
|
+
let b = await BlossomClient.mirrorBlob(server, blob.url, auth);
|
|
19
|
+
uploaded++;
|
|
20
|
+
yield { blob: b, progress: uploaded / total, server };
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
// not all servers support mirroring, attempt to upload
|
|
24
|
+
let b = await BlossomClient.uploadBlob(server, file, auth);
|
|
25
|
+
uploaded++;
|
|
26
|
+
yield { blob: b, progress: uploaded / total, server };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// attempt initial upload
|
|
31
|
+
blob = await BlossomClient.uploadBlob(server, file, auth);
|
|
32
|
+
uploaded++;
|
|
33
|
+
yield { blob, progress: uploaded / total, server };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return blob;
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blossom-client-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A simple client for manage blobs on blossom servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -36,11 +36,17 @@
|
|
|
36
36
|
"./nostr": {
|
|
37
37
|
"import": "./lib/nostr.js",
|
|
38
38
|
"types": "./lib/nostr.d.ts"
|
|
39
|
+
},
|
|
40
|
+
"./upload": {
|
|
41
|
+
"import": "./lib/upload.js",
|
|
42
|
+
"types": "./lib/upload.d.ts"
|
|
39
43
|
}
|
|
40
44
|
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18"
|
|
47
|
+
},
|
|
41
48
|
"dependencies": {
|
|
42
|
-
"@noble/hashes": "^1.4.0"
|
|
43
|
-
"cross-fetch": "^4.0.0"
|
|
49
|
+
"@noble/hashes": "^1.4.0"
|
|
44
50
|
},
|
|
45
51
|
"devDependencies": {
|
|
46
52
|
"@changesets/cli": "^2.27.1",
|