blossom-client-sdk 1.0.0 → 1.1.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/CHANGELOG.md +16 -0
- package/lib/client.d.ts +20 -3
- package/lib/client.js +57 -22
- package/lib/upload.d.ts +2 -2
- package/lib/upload.js +2 -2
- package/package.json +5 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# blossom-client-sdk
|
|
2
2
|
|
|
3
|
+
## 1.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Add support for multiple hashes when creating auth events
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Allow auth to be passed into multiServerUpload
|
|
12
|
+
|
|
13
|
+
## 1.0.1
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Add upload exports to package.json
|
|
18
|
+
|
|
3
19
|
## 1.0.0
|
|
4
20
|
|
|
5
21
|
### Major Changes
|
package/lib/client.d.ts
CHANGED
|
@@ -41,9 +41,26 @@ export declare class BlossomClient {
|
|
|
41
41
|
constructor(server: string | URL, signer?: Signer);
|
|
42
42
|
static getFileSha256(file: UploadType): Promise<string>;
|
|
43
43
|
static encodeAuthorizationHeader(event: SignedEvent): string;
|
|
44
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Creates a get auth event
|
|
46
|
+
* @param signer the signer to use for signing the event
|
|
47
|
+
* @param message A human readable explanation of what the auth token will be used for
|
|
48
|
+
* @param serverOrHash A server URL or one or many blob hashes
|
|
49
|
+
* @param expiration The expiration time in seconds
|
|
50
|
+
* @returns {Promise<SignedEvent>}
|
|
51
|
+
*/
|
|
52
|
+
static getGetAuth(signer: Signer, message: string, serverOrHash: string | string[], expiration?: number): Promise<SignedEvent>;
|
|
45
53
|
static getBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<Blob>;
|
|
46
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Creates an upload auth event
|
|
56
|
+
* @param sha256 one or an array of sha256 hashes
|
|
57
|
+
* @param signer the signer to use for signing the event
|
|
58
|
+
* @param message A human readable explanation of what the auth token will be used for
|
|
59
|
+
* @param expiration The expiration time in seconds
|
|
60
|
+
* @returns {Promise<SignedEvent>}
|
|
61
|
+
*/
|
|
62
|
+
static createUploadAuth(sha256: string | string[], signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
63
|
+
/** Creates a one-off upload auth event for a file */
|
|
47
64
|
static getUploadAuth(file: UploadType, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
48
65
|
static uploadBlob(server: ServerType, file: UploadType, auth?: SignedEvent): Promise<BlobDescriptor>;
|
|
49
66
|
static mirrorBlob(server: ServerType, url: string | URL, auth?: SignedEvent): Promise<BlobDescriptor>;
|
|
@@ -52,7 +69,7 @@ export declare class BlossomClient {
|
|
|
52
69
|
since?: number;
|
|
53
70
|
until?: number;
|
|
54
71
|
}, auth?: SignedEvent): Promise<BlobDescriptor[]>;
|
|
55
|
-
static getDeleteAuth(hash: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
72
|
+
static getDeleteAuth(hash: string | string[], signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
56
73
|
static deleteBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<string>;
|
|
57
74
|
getGetAuth(message: string, serverOrHash: string, expiration?: number): Promise<SignedEvent>;
|
|
58
75
|
getBlob(hash: string, auth?: SignedEvent | boolean): Promise<Blob>;
|
package/lib/client.js
CHANGED
|
@@ -55,20 +55,35 @@ export class BlossomClient {
|
|
|
55
55
|
static encodeAuthorizationHeader(event) {
|
|
56
56
|
return "Nostr " + btoa(JSON.stringify(event));
|
|
57
57
|
}
|
|
58
|
-
|
|
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
|
-
|
|
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: [
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "blossom-client-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A simple client for manage blobs on blossom servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -36,6 +36,10 @@
|
|
|
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
|
},
|
|
41
45
|
"engines": {
|