blossom-client-sdk 0.4.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/README.md +19 -0
- package/lib/index.d.ts +60 -0
- package/lib/index.js +171 -0
- package/package.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# 🌸 Blossom Client
|
|
2
|
+
|
|
3
|
+
A javascript client for manage blobs on blossom servers
|
|
4
|
+
|
|
5
|
+
## Example
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { BlossomClient } from "blossom-client-sdk";
|
|
9
|
+
|
|
10
|
+
async function signer(event) {
|
|
11
|
+
return await window.nostr.signEvent(event);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const client = new BlossomClient("https://cdn.example.com", signer);
|
|
15
|
+
|
|
16
|
+
const blobs = await client.listBlobs();
|
|
17
|
+
|
|
18
|
+
await client.uploadBlob(new File(["testing"], "test.txt"));
|
|
19
|
+
```
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export type EventTemplate = {
|
|
2
|
+
created_at: number;
|
|
3
|
+
kind: number;
|
|
4
|
+
content: string;
|
|
5
|
+
tags: string[][];
|
|
6
|
+
};
|
|
7
|
+
export type SignedEvent = EventTemplate & {
|
|
8
|
+
id: string;
|
|
9
|
+
sig: string;
|
|
10
|
+
pubkey: string;
|
|
11
|
+
};
|
|
12
|
+
export type Signer = (draft: EventTemplate) => Promise<SignedEvent>;
|
|
13
|
+
export type BlobDescriptor = {
|
|
14
|
+
created: number;
|
|
15
|
+
type?: string;
|
|
16
|
+
sha256: string;
|
|
17
|
+
size: number;
|
|
18
|
+
url: string;
|
|
19
|
+
};
|
|
20
|
+
export declare class HTTPError extends Error {
|
|
21
|
+
response: Response;
|
|
22
|
+
status: number;
|
|
23
|
+
body?: {
|
|
24
|
+
message: string;
|
|
25
|
+
};
|
|
26
|
+
constructor(response: Response, body: {
|
|
27
|
+
message: string;
|
|
28
|
+
} | string);
|
|
29
|
+
static handleErrorResponse(res: Response): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export declare class BlossomClient {
|
|
32
|
+
server: string;
|
|
33
|
+
signer?: Signer;
|
|
34
|
+
constructor(server: string, signer?: Signer);
|
|
35
|
+
static getGetAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
36
|
+
static getUploadAuth(file: File, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
37
|
+
static getDeleteAuth(hash: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
38
|
+
static getListAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
39
|
+
getGetAuth(message?: string, expiration?: number): Promise<SignedEvent>;
|
|
40
|
+
getUploadAuth(file: File, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
41
|
+
getDeleteAuth(hash: string, message?: string, expiration?: number): Promise<SignedEvent>;
|
|
42
|
+
getListAuth(message?: string, expiration?: number): Promise<SignedEvent>;
|
|
43
|
+
static encodeAuthorizationHeader(event: SignedEvent): string;
|
|
44
|
+
static getBlob(server: string, hash: string, auth?: SignedEvent): Promise<Blob>;
|
|
45
|
+
getBlob(hash: string, auth?: SignedEvent | boolean): Promise<Blob>;
|
|
46
|
+
static hasBlob(server: string, hash: string): Promise<boolean>;
|
|
47
|
+
hasBlob(hash: string): Promise<boolean>;
|
|
48
|
+
static listBlobs(server: string, pubkey: string, opts?: {
|
|
49
|
+
since?: number;
|
|
50
|
+
until?: number;
|
|
51
|
+
}, auth?: SignedEvent): Promise<BlobDescriptor[]>;
|
|
52
|
+
listBlobs(pubkey: string, opts?: {
|
|
53
|
+
since?: number;
|
|
54
|
+
until?: number;
|
|
55
|
+
}, auth?: SignedEvent | boolean): Promise<BlobDescriptor[]>;
|
|
56
|
+
static deleteBlob(server: string, hash: string, auth?: SignedEvent): Promise<string>;
|
|
57
|
+
deleteBlob(hash: string, auth?: SignedEvent | boolean): Promise<string>;
|
|
58
|
+
static uploadBlob(server: string, file: File, auth?: SignedEvent): Promise<BlobDescriptor>;
|
|
59
|
+
uploadBlob(file: File, auth?: SignedEvent | boolean): Promise<BlobDescriptor>;
|
|
60
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import fetch from "cross-fetch";
|
|
2
|
+
const now = () => Math.floor(new Date().valueOf() / 1000);
|
|
3
|
+
const oneHour = () => now() + 60 * 60;
|
|
4
|
+
export class HTTPError extends Error {
|
|
5
|
+
constructor(response, body) {
|
|
6
|
+
super(typeof body === "string" ? body : body.message);
|
|
7
|
+
this.response = response;
|
|
8
|
+
this.status = response.status;
|
|
9
|
+
if (typeof body == "object")
|
|
10
|
+
this.body = body;
|
|
11
|
+
}
|
|
12
|
+
static async handleErrorResponse(res) {
|
|
13
|
+
if (!res.ok) {
|
|
14
|
+
try {
|
|
15
|
+
throw new HTTPError(res, await res.json());
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
if (e instanceof Error)
|
|
19
|
+
throw new HTTPError(res, e.message);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class BlossomClient {
|
|
25
|
+
constructor(server, signer) {
|
|
26
|
+
this.server = new URL("/", server).toString();
|
|
27
|
+
this.signer = signer;
|
|
28
|
+
}
|
|
29
|
+
static async getGetAuth(signer, message = "Get Blobs", expiration = oneHour()) {
|
|
30
|
+
return await signer({
|
|
31
|
+
created_at: now(),
|
|
32
|
+
kind: 24242,
|
|
33
|
+
content: message,
|
|
34
|
+
tags: [
|
|
35
|
+
["t", "get"],
|
|
36
|
+
["expiration", String(expiration)],
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
static async getUploadAuth(file, signer, message = "Upload Blob", expiration = oneHour()) {
|
|
41
|
+
return await signer({
|
|
42
|
+
created_at: now(),
|
|
43
|
+
kind: 24242,
|
|
44
|
+
content: message,
|
|
45
|
+
tags: [
|
|
46
|
+
["t", "upload"],
|
|
47
|
+
["name", file.name],
|
|
48
|
+
["size", String(file.size)],
|
|
49
|
+
["expiration", String(expiration)],
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
static async getDeleteAuth(hash, signer, message = "Delete Blob", expiration = oneHour()) {
|
|
54
|
+
return await signer({
|
|
55
|
+
created_at: now(),
|
|
56
|
+
kind: 24242,
|
|
57
|
+
content: message,
|
|
58
|
+
tags: [
|
|
59
|
+
["t", "delete"],
|
|
60
|
+
["x", hash],
|
|
61
|
+
["expiration", String(expiration)],
|
|
62
|
+
],
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
static async getListAuth(signer, message = "List Blobs", expiration = oneHour()) {
|
|
66
|
+
return await signer({
|
|
67
|
+
created_at: now(),
|
|
68
|
+
kind: 24242,
|
|
69
|
+
content: message,
|
|
70
|
+
tags: [
|
|
71
|
+
["t", "list"],
|
|
72
|
+
["expiration", String(expiration)],
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
async getGetAuth(message, expiration) {
|
|
77
|
+
if (!this.signer)
|
|
78
|
+
throw new Error("Missing signer");
|
|
79
|
+
return await BlossomClient.getGetAuth(this.signer, message, expiration);
|
|
80
|
+
}
|
|
81
|
+
async getUploadAuth(file, message, expiration) {
|
|
82
|
+
if (!this.signer)
|
|
83
|
+
throw new Error("Missing signer");
|
|
84
|
+
return await BlossomClient.getUploadAuth(file, this.signer, message, expiration);
|
|
85
|
+
}
|
|
86
|
+
async getDeleteAuth(hash, message, expiration) {
|
|
87
|
+
if (!this.signer)
|
|
88
|
+
throw new Error("Missing signer");
|
|
89
|
+
return await BlossomClient.getDeleteAuth(hash, this.signer, message, expiration);
|
|
90
|
+
}
|
|
91
|
+
async getListAuth(message, expiration) {
|
|
92
|
+
if (!this.signer)
|
|
93
|
+
throw new Error("Missing signer");
|
|
94
|
+
return await BlossomClient.getListAuth(this.signer, message, expiration);
|
|
95
|
+
}
|
|
96
|
+
static encodeAuthorizationHeader(event) {
|
|
97
|
+
return "Nostr " + btoa(JSON.stringify(event));
|
|
98
|
+
}
|
|
99
|
+
static async getBlob(server, hash, auth) {
|
|
100
|
+
const res = await fetch(new URL(hash, server), {
|
|
101
|
+
headers: auth
|
|
102
|
+
? { authorization: BlossomClient.encodeAuthorizationHeader(auth) }
|
|
103
|
+
: {},
|
|
104
|
+
});
|
|
105
|
+
await HTTPError.handleErrorResponse(res);
|
|
106
|
+
return await res.blob();
|
|
107
|
+
}
|
|
108
|
+
async getBlob(hash, auth = false) {
|
|
109
|
+
if (typeof auth === "boolean" && auth)
|
|
110
|
+
auth = await this.getGetAuth();
|
|
111
|
+
return BlossomClient.getBlob(this.server, hash, auth ? auth : undefined);
|
|
112
|
+
}
|
|
113
|
+
static async hasBlob(server, hash) {
|
|
114
|
+
const res = await fetch(new URL(`/` + hash, server), { method: "HEAD" });
|
|
115
|
+
await HTTPError.handleErrorResponse(res);
|
|
116
|
+
return res.ok;
|
|
117
|
+
}
|
|
118
|
+
async hasBlob(hash) {
|
|
119
|
+
return BlossomClient.hasBlob(this.server, hash);
|
|
120
|
+
}
|
|
121
|
+
static async listBlobs(server, pubkey, opts, auth) {
|
|
122
|
+
const url = new URL(`/list/` + pubkey, server);
|
|
123
|
+
if (opts?.since)
|
|
124
|
+
url.searchParams.append("since", String(opts.since));
|
|
125
|
+
if (opts?.until)
|
|
126
|
+
url.searchParams.append("until", String(opts.until));
|
|
127
|
+
const res = await fetch(url, {
|
|
128
|
+
headers: auth
|
|
129
|
+
? { authorization: BlossomClient.encodeAuthorizationHeader(auth) }
|
|
130
|
+
: {},
|
|
131
|
+
});
|
|
132
|
+
await HTTPError.handleErrorResponse(res);
|
|
133
|
+
return (await res.json());
|
|
134
|
+
}
|
|
135
|
+
async listBlobs(pubkey, opts, auth = false) {
|
|
136
|
+
if (typeof auth === "boolean" && auth)
|
|
137
|
+
auth = await this.getListAuth();
|
|
138
|
+
return BlossomClient.listBlobs(this.server, pubkey, opts, auth ? auth : undefined);
|
|
139
|
+
}
|
|
140
|
+
static async deleteBlob(server, hash, auth) {
|
|
141
|
+
const res = await fetch(new URL("/" + hash, server), {
|
|
142
|
+
method: "DELETE",
|
|
143
|
+
headers: auth
|
|
144
|
+
? { authorization: BlossomClient.encodeAuthorizationHeader(auth) }
|
|
145
|
+
: {},
|
|
146
|
+
});
|
|
147
|
+
await HTTPError.handleErrorResponse(res);
|
|
148
|
+
return await res.text();
|
|
149
|
+
}
|
|
150
|
+
async deleteBlob(hash, auth = true) {
|
|
151
|
+
if (typeof auth === "boolean" && auth)
|
|
152
|
+
auth = await this.getDeleteAuth(hash);
|
|
153
|
+
return BlossomClient.deleteBlob(this.server, hash, auth ? auth : undefined);
|
|
154
|
+
}
|
|
155
|
+
static async uploadBlob(server, file, auth) {
|
|
156
|
+
const res = await fetch(new URL("/upload", server), {
|
|
157
|
+
method: "PUT",
|
|
158
|
+
body: file,
|
|
159
|
+
headers: auth
|
|
160
|
+
? { authorization: BlossomClient.encodeAuthorizationHeader(auth) }
|
|
161
|
+
: {},
|
|
162
|
+
});
|
|
163
|
+
await HTTPError.handleErrorResponse(res);
|
|
164
|
+
return (await res.json());
|
|
165
|
+
}
|
|
166
|
+
async uploadBlob(file, auth = true) {
|
|
167
|
+
if (typeof auth === "boolean" && auth)
|
|
168
|
+
auth = await this.getUploadAuth(file);
|
|
169
|
+
return BlossomClient.uploadBlob(this.server, file, auth ? auth : undefined);
|
|
170
|
+
}
|
|
171
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "blossom-client-sdk",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "A simple client for manage blobs on blossom servers",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"typings": "./lib/index.d.ts",
|
|
8
|
+
"author": "hzrd149",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"prepublish": "npm run build",
|
|
13
|
+
"format": "prettier -w ."
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"lib"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"cross-fetch": "^4.0.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^5.3.3",
|
|
23
|
+
"prettier": "^3.2.5"
|
|
24
|
+
}
|
|
25
|
+
}
|