blossom-client-sdk 2.1.1 → 3.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/README.md +36 -4
- package/lib/actions/delete.d.ts +17 -2
- package/lib/actions/delete.js +18 -6
- package/lib/actions/download.d.ts +17 -2
- package/lib/actions/download.js +16 -6
- package/lib/actions/index.d.ts +2 -0
- package/lib/actions/index.js +2 -0
- package/lib/actions/list.d.ts +13 -0
- package/lib/actions/list.js +8 -6
- package/lib/actions/media.d.ts +9 -0
- package/lib/actions/media.js +82 -0
- package/lib/actions/mirror.d.ts +19 -2
- package/lib/actions/mirror.js +15 -8
- package/lib/actions/multi-server.d.ts +66 -0
- package/lib/actions/multi-server.js +153 -0
- package/lib/actions/upload.d.ts +19 -15
- package/lib/actions/upload.js +27 -86
- package/lib/auth.d.ts +27 -21
- package/lib/auth.js +53 -77
- package/lib/client.d.ts +14 -7
- package/lib/client.js +42 -23
- package/lib/helpers/blob.d.ts +11 -0
- package/lib/{helpers.js → helpers/blob.js} +3 -21
- package/lib/helpers/cachu.d.ts +5 -0
- package/lib/helpers/cachu.js +6 -0
- package/lib/helpers/fetch.d.ts +6 -0
- package/lib/helpers/fetch.js +10 -0
- package/lib/helpers/index.d.ts +5 -0
- package/lib/helpers/index.js +5 -0
- package/lib/helpers/signal.d.ts +7 -0
- package/lib/helpers/signal.js +13 -0
- package/lib/helpers/url.d.ts +4 -0
- package/lib/helpers/url.js +15 -0
- package/lib/image.js +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/package.json +19 -5
- package/lib/helpers.d.ts +0 -19
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { doseAuthMatchUpload } from "../auth.js";
|
|
2
|
+
import { getBlobSha256 } from "../helpers/index.js";
|
|
3
|
+
import { MediaEndpointMissingError, uploadMedia } from "./media.js";
|
|
4
|
+
import { mirrorBlob } from "./mirror.js";
|
|
5
|
+
import { uploadBlob } from "./upload.js";
|
|
6
|
+
const defaultMultiServerOptions = {
|
|
7
|
+
isMedia: false,
|
|
8
|
+
mediaUploadBehavior: "first",
|
|
9
|
+
mediaUploadFallback: false,
|
|
10
|
+
mirrorTimeout: 5000,
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Creates an AsyncGenerator that can be used to upload a blob to multiple servers
|
|
14
|
+
* @param servers A Set or Array of servers to upload to
|
|
15
|
+
* @param blob The blob to be uploaded
|
|
16
|
+
* @param signer An async function used for signing nostr events
|
|
17
|
+
* @returns The BlobDescriptor if successful
|
|
18
|
+
*
|
|
19
|
+
* ```mermaid
|
|
20
|
+
* flowchart LR
|
|
21
|
+
* isMedia{"isMedia"} -- yes --> media["/media"]
|
|
22
|
+
* isMedia -- no --> upload["/upload"]
|
|
23
|
+
* media --> ifmedia{"Successful"}
|
|
24
|
+
* ifmedia -- yes --> next["Next server"]
|
|
25
|
+
* ifmedia -- no --> iffallback{"fallback"}
|
|
26
|
+
* iffallback -- yes --> upload
|
|
27
|
+
* iffallback -- no --> abort
|
|
28
|
+
* upload -- complete --> next
|
|
29
|
+
* next --> uploaded{"Uploaded"}
|
|
30
|
+
* uploaded -- yes --> mirror["/mirror"]
|
|
31
|
+
* uploaded -- no --> upload
|
|
32
|
+
* mirror --> isMirror{"Successful"}
|
|
33
|
+
* isMirror -- yes --> next
|
|
34
|
+
* isMirror -- no --> ifMediaAbort{"isMedia"}
|
|
35
|
+
* ifMediaAbort -- no --> upload
|
|
36
|
+
* ifMediaAbort -- yes --> next
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export async function multiServerUpload(servers, blob, opts) {
|
|
40
|
+
const options = { ...defaultMultiServerOptions, ...opts };
|
|
41
|
+
let initialUpload;
|
|
42
|
+
const results = new Map();
|
|
43
|
+
// reuse auth events
|
|
44
|
+
let authEvents = options.auth ? [options.auth] : [];
|
|
45
|
+
const handleAuthRequest = async (server, sha256, type) => {
|
|
46
|
+
// check if any existing auth events match
|
|
47
|
+
for (const auth of authEvents) {
|
|
48
|
+
if (await doseAuthMatchUpload(auth, server, sha256))
|
|
49
|
+
return auth;
|
|
50
|
+
}
|
|
51
|
+
// create a new auth event
|
|
52
|
+
if (options.onAuth) {
|
|
53
|
+
const auth = await options.onAuth(server, sha256, type, blob);
|
|
54
|
+
authEvents.push(auth);
|
|
55
|
+
return auth;
|
|
56
|
+
}
|
|
57
|
+
else
|
|
58
|
+
throw new Error("Missing onAuth handler");
|
|
59
|
+
};
|
|
60
|
+
// handle payment requests for servers
|
|
61
|
+
const handlePaymentRequest = async (server, sha256, _blog, request) => {
|
|
62
|
+
if (!options.onPayment)
|
|
63
|
+
throw new Error("Missing payment handler");
|
|
64
|
+
return options.onPayment(server, sha256, blob, request);
|
|
65
|
+
};
|
|
66
|
+
// servers to upload the blob to
|
|
67
|
+
const uploadServers = Array.from(servers);
|
|
68
|
+
// start media upload
|
|
69
|
+
if (options.isMedia) {
|
|
70
|
+
const mediaServers = options.mediaUploadBehavior === "any" ? Array.from(servers) : [Array.from(servers)[0]];
|
|
71
|
+
for (const server of mediaServers) {
|
|
72
|
+
try {
|
|
73
|
+
const sha256 = await getBlobSha256(blob);
|
|
74
|
+
options.onStart?.(server, sha256, blob);
|
|
75
|
+
// attempt to upload media
|
|
76
|
+
initialUpload = await uploadMedia(server, blob, {
|
|
77
|
+
onAuth: handleAuthRequest,
|
|
78
|
+
onPayment: handlePaymentRequest,
|
|
79
|
+
});
|
|
80
|
+
// save result
|
|
81
|
+
results.set(server, initialUpload);
|
|
82
|
+
// finished upload to server
|
|
83
|
+
options.onUpload?.(server, initialUpload.sha256, blob);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (error instanceof MediaEndpointMissingError) {
|
|
87
|
+
// ignore error
|
|
88
|
+
}
|
|
89
|
+
else if (error instanceof Error) {
|
|
90
|
+
const sha256 = await getBlobSha256(blob);
|
|
91
|
+
options.onError?.(server, sha256, blob, error);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (initialUpload) {
|
|
95
|
+
// remove server from upload array
|
|
96
|
+
if (uploadServers.includes(server))
|
|
97
|
+
uploadServers.splice(uploadServers.indexOf(server), 1);
|
|
98
|
+
// exit on first successful media upload
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!options.mediaUploadFallback && !initialUpload) {
|
|
103
|
+
// failed to find a /media endpoint, abort
|
|
104
|
+
throw new Error("Failed to find media processing endpoint");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// start server uploads
|
|
108
|
+
for (const server of uploadServers) {
|
|
109
|
+
try {
|
|
110
|
+
let metadata = undefined;
|
|
111
|
+
// attempt to mirror the initial upload
|
|
112
|
+
if (initialUpload) {
|
|
113
|
+
try {
|
|
114
|
+
options.onStart?.(server, initialUpload.sha256, blob);
|
|
115
|
+
metadata = await mirrorBlob(server, initialUpload, {
|
|
116
|
+
signal: options.signal,
|
|
117
|
+
onAuth: (server, sha256) => handleAuthRequest(server, sha256, "upload"),
|
|
118
|
+
onPayment: handlePaymentRequest,
|
|
119
|
+
timeout: options.mirrorTimeout,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
// mirror failed, if isMedia skip server since we don't want to upload two versions of the blob
|
|
124
|
+
if (options.isMedia)
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// attempt to upload
|
|
129
|
+
if (!metadata) {
|
|
130
|
+
const sha256 = await getBlobSha256(blob);
|
|
131
|
+
options.onStart?.(server, sha256, blob);
|
|
132
|
+
metadata = await uploadBlob(server, blob, {
|
|
133
|
+
onAuth: handleAuthRequest,
|
|
134
|
+
onPayment: handlePaymentRequest,
|
|
135
|
+
timeout: options.mirrorTimeout,
|
|
136
|
+
});
|
|
137
|
+
// save first upload
|
|
138
|
+
if (!initialUpload)
|
|
139
|
+
initialUpload = metadata;
|
|
140
|
+
}
|
|
141
|
+
// save result
|
|
142
|
+
results.set(server, metadata);
|
|
143
|
+
// finished upload to server
|
|
144
|
+
options.onUpload?.(server, metadata.sha256, blob);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
// failed to upload to server
|
|
148
|
+
if (error instanceof Error)
|
|
149
|
+
options.onError?.(server, await getBlobSha256(blob), blob, error);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return results;
|
|
153
|
+
}
|
package/lib/actions/upload.d.ts
CHANGED
|
@@ -2,23 +2,27 @@ import { type Token } from "@cashu/cashu-ts";
|
|
|
2
2
|
import { ServerType, UploadType } from "../client.js";
|
|
3
3
|
import { BlobDescriptor, PaymentRequest, SignedEvent } from "../types.js";
|
|
4
4
|
export type UploadOptions<S extends ServerType, B extends UploadType> = {
|
|
5
|
+
/** AbortSignal to cancel the action */
|
|
5
6
|
signal?: AbortSignal;
|
|
7
|
+
/** Override auth event to use */
|
|
6
8
|
auth?: SignedEvent;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
/** Request timeout */
|
|
10
|
+
timeout?: number;
|
|
11
|
+
/**
|
|
12
|
+
* A method used to request payment when uploading or mirroring a blob
|
|
13
|
+
* @param server the server requiring payment
|
|
14
|
+
* @param sha256 the sha256 of the blob being uploaded or mirrored
|
|
15
|
+
* @param blob the original blob
|
|
16
|
+
* @param request the payment request
|
|
17
|
+
*/
|
|
18
|
+
onPayment?: (server: S, sha256: string, blob: B, request: PaymentRequest) => Promise<Token>;
|
|
19
|
+
/**
|
|
20
|
+
* A method used to request a signed auth event for a server and sha256
|
|
21
|
+
* @param server the server requesting the auth
|
|
22
|
+
* @param sha256 the sha256 of the blob being upload or mirror to the server
|
|
23
|
+
* @param blob the original blob passed to the method
|
|
24
|
+
*/
|
|
25
|
+
onAuth?: (server: S, sha256: string, authType: "upload" | "media", blob: B) => Promise<SignedEvent>;
|
|
9
26
|
};
|
|
10
27
|
/** Upload a blob to a server, handles payment and auth */
|
|
11
28
|
export declare function uploadBlob<S extends ServerType, B extends UploadType>(server: S, blob: B, opts?: UploadOptions<S, B>): Promise<BlobDescriptor>;
|
|
12
|
-
export type MultiServerUploadOptions<S extends ServerType, B extends UploadType> = UploadOptions<S, B> & {
|
|
13
|
-
onStart?: (server: S, blob: B) => void;
|
|
14
|
-
onUpload?: (server: S, blob: B) => void;
|
|
15
|
-
onError?: (server: S, blob: B, error: Error) => void;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Creates an AsyncGenerator that can be used to upload a blob to multiple servers
|
|
19
|
-
* @param servers A Set or Array of servers to upload to
|
|
20
|
-
* @param blob The blob to be uploaded
|
|
21
|
-
* @param signer An async function used for signing nostr events
|
|
22
|
-
* @returns The BlobDescriptor if successful
|
|
23
|
-
*/
|
|
24
|
-
export declare function multiServerUpload<S extends ServerType, B extends UploadType>(servers: Iterable<S>, blob: B, opts?: MultiServerUploadOptions<S, B>): Promise<Map<S, BlobDescriptor>>;
|
package/lib/actions/upload.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import { getBlobSha256, getBlobSize, getBlobType, getPaymentRequestFromHeaders } from "../helpers.js";
|
|
1
|
+
import { fetchWithTimeout, getBlobSha256, getBlobSize, getBlobType, getPaymentRequestFromHeaders, } from "../helpers/index.js";
|
|
2
2
|
import HTTPError from "../error.js";
|
|
3
|
-
import {
|
|
4
|
-
import { mirrorBlob } from "./mirror.js";
|
|
3
|
+
import { encodeAuthorizationHeader } from "../auth.js";
|
|
5
4
|
/** Upload a blob to a server, handles payment and auth */
|
|
6
5
|
export async function uploadBlob(server, blob, opts) {
|
|
7
6
|
const url = new URL("/upload", server);
|
|
8
|
-
const
|
|
7
|
+
const sha256 = await getBlobSha256(blob);
|
|
9
8
|
const headers = {
|
|
10
|
-
"X-SHA-256":
|
|
9
|
+
"X-SHA-256": sha256,
|
|
11
10
|
};
|
|
11
|
+
// attach the auth if its already set
|
|
12
|
+
if (opts?.auth)
|
|
13
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
12
14
|
// build check headers
|
|
13
15
|
const checkHeaders = {
|
|
14
16
|
...headers,
|
|
@@ -18,27 +20,35 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
18
20
|
if (type)
|
|
19
21
|
checkHeaders["X-Content-Type"] = type;
|
|
20
22
|
// check upload with HEAD /upload
|
|
21
|
-
let firstTry = await
|
|
23
|
+
let firstTry = await fetchWithTimeout(url, {
|
|
22
24
|
method: "HEAD",
|
|
23
25
|
signal: opts?.signal,
|
|
24
26
|
headers: checkHeaders,
|
|
27
|
+
timeout: opts?.timeout,
|
|
25
28
|
});
|
|
26
29
|
let upload = undefined;
|
|
27
30
|
if (firstTry.status === 404) {
|
|
28
31
|
// BUD-06 HEAD endpoint is not supported. attempt to upload
|
|
29
|
-
upload = firstTry = await
|
|
32
|
+
upload = firstTry = await fetchWithTimeout(url, {
|
|
33
|
+
body: blob,
|
|
34
|
+
method: "PUT",
|
|
35
|
+
signal: opts?.signal,
|
|
36
|
+
timeout: opts?.timeout,
|
|
37
|
+
});
|
|
30
38
|
}
|
|
39
|
+
// handle auth and payment
|
|
31
40
|
switch (firstTry.status) {
|
|
32
41
|
case 401: {
|
|
33
|
-
const auth = opts?.auth || (await opts?.onAuth?.(server, blob));
|
|
42
|
+
const auth = opts?.auth || (await opts?.onAuth?.(server, sha256, "upload", blob));
|
|
34
43
|
if (!auth)
|
|
35
44
|
throw new Error("Missing auth handler");
|
|
36
45
|
// Try upload with auth
|
|
37
|
-
upload = await
|
|
38
|
-
signal: opts?.signal,
|
|
46
|
+
upload = await fetchWithTimeout(url, {
|
|
39
47
|
method: "PUT",
|
|
40
48
|
body: blob,
|
|
41
49
|
headers: { ...headers, Authorization: encodeAuthorizationHeader(auth) },
|
|
50
|
+
signal: opts?.signal,
|
|
51
|
+
timeout: opts?.timeout,
|
|
42
52
|
});
|
|
43
53
|
break;
|
|
44
54
|
}
|
|
@@ -47,14 +57,15 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
47
57
|
throw new Error("Missing payment handler");
|
|
48
58
|
const { getEncodedToken } = await import("@cashu/cashu-ts");
|
|
49
59
|
const request = getPaymentRequestFromHeaders(firstTry.headers);
|
|
50
|
-
const token = await opts.onPayment(server, blob, request);
|
|
60
|
+
const token = await opts.onPayment(server, sha256, blob, request);
|
|
51
61
|
const payment = getEncodedToken(token);
|
|
52
62
|
// Try upload with payment
|
|
53
|
-
upload = await
|
|
54
|
-
signal: opts?.signal,
|
|
63
|
+
upload = await fetchWithTimeout(url, {
|
|
55
64
|
method: "PUT",
|
|
56
65
|
body: blob,
|
|
57
66
|
headers: { ...headers, "X-Cashu": payment },
|
|
67
|
+
signal: opts?.signal,
|
|
68
|
+
timeout: opts?.timeout,
|
|
58
69
|
});
|
|
59
70
|
break;
|
|
60
71
|
}
|
|
@@ -63,85 +74,15 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
63
74
|
throw new Error("Server error");
|
|
64
75
|
// check passed, upload
|
|
65
76
|
if (!upload)
|
|
66
|
-
upload = await
|
|
67
|
-
signal: opts?.signal,
|
|
77
|
+
upload = await fetchWithTimeout(url, {
|
|
68
78
|
method: "PUT",
|
|
69
79
|
body: blob,
|
|
70
80
|
headers: { ...headers },
|
|
81
|
+
signal: opts?.signal,
|
|
82
|
+
timeout: opts?.timeout,
|
|
71
83
|
});
|
|
72
84
|
// handle errors
|
|
73
85
|
await HTTPError.handleErrorResponse(upload);
|
|
74
86
|
// return blob descriptor
|
|
75
87
|
return upload.json();
|
|
76
88
|
}
|
|
77
|
-
/**
|
|
78
|
-
* Creates an AsyncGenerator that can be used to upload a blob to multiple servers
|
|
79
|
-
* @param servers A Set or Array of servers to upload to
|
|
80
|
-
* @param blob The blob to be uploaded
|
|
81
|
-
* @param signer An async function used for signing nostr events
|
|
82
|
-
* @returns The BlobDescriptor if successful
|
|
83
|
-
*/
|
|
84
|
-
export async function multiServerUpload(servers, blob, opts) {
|
|
85
|
-
let initialUpload;
|
|
86
|
-
const results = new Map();
|
|
87
|
-
// reuse auth events
|
|
88
|
-
let authEvents = opts?.auth ? [opts?.auth] : [];
|
|
89
|
-
const handleAuthRequest = async (server) => {
|
|
90
|
-
// check if any existing auth events match
|
|
91
|
-
for (const auth of authEvents) {
|
|
92
|
-
if (await doseAuthMatchUpload(auth, server, blob))
|
|
93
|
-
return auth;
|
|
94
|
-
}
|
|
95
|
-
// create a new auth event
|
|
96
|
-
if (opts?.onAuth) {
|
|
97
|
-
const auth = await opts.onAuth(server, blob);
|
|
98
|
-
authEvents.push(auth);
|
|
99
|
-
return auth;
|
|
100
|
-
}
|
|
101
|
-
else
|
|
102
|
-
throw new Error("Missing onAuth handler");
|
|
103
|
-
};
|
|
104
|
-
// handle payment requests for servers
|
|
105
|
-
const handlePaymentRequest = async (server, _b, request) => {
|
|
106
|
-
if (!opts?.onPayment)
|
|
107
|
-
throw new Error("Missing payment handler");
|
|
108
|
-
return opts.onPayment(server, blob, request);
|
|
109
|
-
};
|
|
110
|
-
// start server uploads
|
|
111
|
-
for (const server of servers) {
|
|
112
|
-
try {
|
|
113
|
-
let metadata = undefined;
|
|
114
|
-
opts?.onStart?.(server, blob);
|
|
115
|
-
// attempt to mirror the blob
|
|
116
|
-
if (initialUpload) {
|
|
117
|
-
try {
|
|
118
|
-
metadata = await mirrorBlob(server, initialUpload, {
|
|
119
|
-
signal: opts?.signal,
|
|
120
|
-
onAuth: handleAuthRequest,
|
|
121
|
-
onPayment: handlePaymentRequest,
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
catch (error) { }
|
|
125
|
-
}
|
|
126
|
-
if (!metadata) {
|
|
127
|
-
metadata = await uploadBlob(server, blob, {
|
|
128
|
-
onAuth: handleAuthRequest,
|
|
129
|
-
onPayment: handlePaymentRequest,
|
|
130
|
-
});
|
|
131
|
-
// save first upload
|
|
132
|
-
if (!initialUpload)
|
|
133
|
-
initialUpload = metadata;
|
|
134
|
-
}
|
|
135
|
-
// save result
|
|
136
|
-
results.set(server, metadata);
|
|
137
|
-
// finished upload to server
|
|
138
|
-
opts?.onUpload?.(server, blob);
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
// failed to upload to server
|
|
142
|
-
if (error instanceof Error)
|
|
143
|
-
opts?.onError?.(server, blob, error);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return results;
|
|
147
|
-
}
|
package/lib/auth.d.ts
CHANGED
|
@@ -4,24 +4,30 @@ export declare const now: () => number;
|
|
|
4
4
|
export declare const oneHour: () => number;
|
|
5
5
|
export declare function encodeAuthorizationHeader(event: SignedEvent): string;
|
|
6
6
|
/** Checks if an auth event matches a server / blob upload */
|
|
7
|
-
export declare function doseAuthMatchUpload(auth: SignedEvent, server: ServerType, blob: UploadType): Promise<boolean>;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
*/
|
|
16
|
-
export declare function
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export
|
|
26
|
-
|
|
27
|
-
export declare function
|
|
7
|
+
export declare function doseAuthMatchUpload(auth: SignedEvent, server: ServerType, blob: string | UploadType): Promise<boolean>;
|
|
8
|
+
export type AuthType = "upload" | "list" | "delete" | "get" | "media";
|
|
9
|
+
export type AuthEventOptions = {
|
|
10
|
+
blobs?: string | string[] | UploadType | UploadType[];
|
|
11
|
+
servers?: string | string[];
|
|
12
|
+
message?: string;
|
|
13
|
+
expiration?: number;
|
|
14
|
+
};
|
|
15
|
+
/** Generic auth event builder */
|
|
16
|
+
export declare function createAuthEvent(signer: Signer, type: AuthType, options?: AuthEventOptions): Promise<SignedEvent>;
|
|
17
|
+
export type DownloadAuthOptions = Omit<AuthEventOptions, "blobs" | "servers">;
|
|
18
|
+
/** Creates a GET auth event */
|
|
19
|
+
export declare function createDownloadAuth(signer: Signer, serverOrHash: string | string[] | UploadType | UploadType[], options?: DownloadAuthOptions): Promise<SignedEvent>;
|
|
20
|
+
export type UploadAuthOptions = Omit<AuthEventOptions, "blobs"> & {
|
|
21
|
+
type?: "upload" | "media";
|
|
22
|
+
};
|
|
23
|
+
/** Creates an upload or media upload auth event */
|
|
24
|
+
export declare function createUploadAuth(signer: Signer, blobs: string | string[] | UploadType | UploadType[], options?: UploadAuthOptions): Promise<SignedEvent>;
|
|
25
|
+
export type MirrorAuthOptions = Omit<AuthEventOptions, "blobs">;
|
|
26
|
+
/** Creates an upload or media upload auth event */
|
|
27
|
+
export declare function createMirrorAuth(signer: Signer, blobs: string | string[] | UploadType | UploadType[], options?: MirrorAuthOptions): Promise<SignedEvent>;
|
|
28
|
+
export type ListAuthOptions = Omit<AuthEventOptions, "blobs">;
|
|
29
|
+
/** Creates a list auth event */
|
|
30
|
+
export declare function createListAuth(signer: Signer, options?: ListAuthOptions): Promise<SignedEvent>;
|
|
31
|
+
export type DeleteAuthOptions = Omit<AuthEventOptions, "blobs">;
|
|
32
|
+
/** Creates a "delete" auth event for a specific hash */
|
|
33
|
+
export declare function createDeleteAuth(signer: Signer, blobs: string | string[] | UploadType | UploadType[], options?: DeleteAuthOptions): Promise<SignedEvent>;
|
package/lib/auth.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AUTH_EVENT_KIND } from "./const.js";
|
|
2
|
-
import {
|
|
2
|
+
import { getBlobSha256, isSha256 } from "./helpers/blob.js";
|
|
3
|
+
import { areServersEqual } from "./helpers/url.js";
|
|
3
4
|
export const now = () => Math.floor(new Date().valueOf() / 1000);
|
|
4
5
|
export const oneHour = () => now() + 60 * 60;
|
|
5
6
|
export function encodeAuthorizationHeader(event) {
|
|
@@ -8,12 +9,13 @@ export function encodeAuthorizationHeader(event) {
|
|
|
8
9
|
/** Checks if an auth event matches a server / blob upload */
|
|
9
10
|
export async function doseAuthMatchUpload(auth, server, blob) {
|
|
10
11
|
const type = auth.tags.find((t) => t[0] === "t")?.[1];
|
|
11
|
-
if (type !== "upload")
|
|
12
|
+
if (type !== "upload" && type !== "media")
|
|
12
13
|
return false;
|
|
14
|
+
const sha256 = typeof blob === "string" ? blob : await getBlobSha256(blob);
|
|
13
15
|
for (const tag of auth.tags) {
|
|
14
16
|
switch (tag[0]) {
|
|
15
17
|
case "x":
|
|
16
|
-
if (tag[1] ===
|
|
18
|
+
if (tag[1] === sha256)
|
|
17
19
|
return true;
|
|
18
20
|
break;
|
|
19
21
|
case "server":
|
|
@@ -24,90 +26,64 @@ export async function doseAuthMatchUpload(auth, server, blob) {
|
|
|
24
26
|
}
|
|
25
27
|
return false;
|
|
26
28
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
* @param signer the signer to use for signing the event
|
|
30
|
-
* @param message A human readable explanation of what the auth token will be used for
|
|
31
|
-
* @param serverOrHash A server URL or one or many blob hashes
|
|
32
|
-
* @param expiration The expiration time in seconds
|
|
33
|
-
* @returns {Promise<SignedEvent>}
|
|
34
|
-
*/
|
|
35
|
-
export async function createDownloadAuth(signer, serverOrHash, message, expiration = oneHour()) {
|
|
36
|
-
const draft = {
|
|
37
|
-
created_at: now(),
|
|
38
|
-
kind: AUTH_EVENT_KIND,
|
|
39
|
-
content: message,
|
|
40
|
-
tags: [
|
|
41
|
-
["t", "get"],
|
|
42
|
-
["expiration", String(expiration)],
|
|
43
|
-
],
|
|
44
|
-
};
|
|
45
|
-
if (Array.isArray(serverOrHash)) {
|
|
46
|
-
for (const sha256 of serverOrHash)
|
|
47
|
-
draft.tags.push(["x", sha256]);
|
|
48
|
-
}
|
|
49
|
-
else if (serverOrHash.match(/^[0-9a-f]{64}$/)) {
|
|
50
|
-
draft.tags.push(["x", serverOrHash]);
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
draft.tags.push(["server", new URL("/", serverOrHash).toString()]);
|
|
54
|
-
}
|
|
55
|
-
return await signer(draft);
|
|
29
|
+
async function normalizeToHash(blob) {
|
|
30
|
+
return typeof blob === "string" ? blob : getBlobSha256(blob);
|
|
56
31
|
}
|
|
57
|
-
/**
|
|
58
|
-
|
|
59
|
-
* @param blobsOrHashes one or an array of sha256 hashes
|
|
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 expiration The expiration time in seconds
|
|
63
|
-
* @returns {Promise<SignedEvent>}
|
|
64
|
-
*/
|
|
65
|
-
export async function createUploadAuth(signer, blobsOrHashes, message = "Upload Blob", expiration = oneHour()) {
|
|
32
|
+
/** Generic auth event builder */
|
|
33
|
+
export async function createAuthEvent(signer, type, options) {
|
|
66
34
|
const draft = {
|
|
67
|
-
kind: AUTH_EVENT_KIND,
|
|
68
|
-
content: message,
|
|
69
35
|
created_at: now(),
|
|
36
|
+
kind: AUTH_EVENT_KIND,
|
|
37
|
+
content: options?.message ?? "",
|
|
70
38
|
tags: [
|
|
71
|
-
["t",
|
|
72
|
-
|
|
39
|
+
["t", type],
|
|
40
|
+
// attach NIP-40 expiration
|
|
41
|
+
["expiration", String(options?.expiration ?? oneHour())],
|
|
73
42
|
],
|
|
74
43
|
};
|
|
75
|
-
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
-
|
|
44
|
+
// add blob tags
|
|
45
|
+
if (options?.blobs) {
|
|
46
|
+
if (Array.isArray(options.blobs))
|
|
47
|
+
for (const blob of options.blobs)
|
|
48
|
+
draft.tags.push(["x", await normalizeToHash(blob)]);
|
|
49
|
+
else
|
|
50
|
+
draft.tags.push(["x", await normalizeToHash(options.blobs)]);
|
|
79
51
|
}
|
|
80
|
-
|
|
81
|
-
|
|
52
|
+
// add server tags
|
|
53
|
+
if (options?.servers) {
|
|
54
|
+
if (Array.isArray(options.servers))
|
|
55
|
+
for (const blob of options.servers)
|
|
56
|
+
draft.tags.push(["server", blob]);
|
|
57
|
+
else
|
|
58
|
+
draft.tags.push(["server", options.servers]);
|
|
82
59
|
}
|
|
83
60
|
return await signer(draft);
|
|
84
61
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
62
|
+
/** Creates a GET auth event */
|
|
63
|
+
export async function createDownloadAuth(signer, serverOrHash, options) {
|
|
64
|
+
if (!Array.isArray(serverOrHash))
|
|
65
|
+
serverOrHash = [serverOrHash];
|
|
66
|
+
return await createAuthEvent(signer, "get", {
|
|
67
|
+
message: "Download Blob",
|
|
68
|
+
...options,
|
|
69
|
+
blobs: serverOrHash.filter((s) => (typeof s === "string" ? isSha256(s) : true)),
|
|
70
|
+
servers: serverOrHash.filter((s) => typeof s === "string" && !isSha256(s) && URL.canParse(s)),
|
|
94
71
|
});
|
|
95
72
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return await signer(draft);
|
|
73
|
+
/** Creates an upload or media upload auth event */
|
|
74
|
+
export async function createUploadAuth(signer, blobs, options) {
|
|
75
|
+
return await createAuthEvent(signer, options?.type ?? "upload", { message: "Upload Blob", ...options, blobs });
|
|
76
|
+
}
|
|
77
|
+
/** Creates an upload or media upload auth event */
|
|
78
|
+
export async function createMirrorAuth(signer, blobs, options) {
|
|
79
|
+
// The /mirror endpoint uses "upload" type
|
|
80
|
+
return await createAuthEvent(signer, "upload", { message: "Mirror Blob", ...options, blobs });
|
|
81
|
+
}
|
|
82
|
+
/** Creates a list auth event */
|
|
83
|
+
export async function createListAuth(signer, options) {
|
|
84
|
+
return await createAuthEvent(signer, "list", { message: "List Blobs", ...options });
|
|
85
|
+
}
|
|
86
|
+
/** Creates a "delete" auth event for a specific hash */
|
|
87
|
+
export async function createDeleteAuth(signer, blobs, options) {
|
|
88
|
+
return await createAuthEvent(signer, "delete", { message: "Delete Blob", ...options, blobs });
|
|
113
89
|
}
|
package/lib/client.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { getBlobSha256 } from "./helpers.js";
|
|
2
|
-
import { createDeleteAuth, createDownloadAuth, createListAuth, createUploadAuth, encodeAuthorizationHeader } from "./auth.js";
|
|
1
|
+
import { getBlobSha256 } from "./helpers/blob.js";
|
|
2
|
+
import { createDeleteAuth, createDownloadAuth, createListAuth, createUploadAuth, DeleteAuthOptions, DownloadAuthOptions, encodeAuthorizationHeader, ListAuthOptions, MirrorAuthOptions, UploadAuthOptions } from "./auth.js";
|
|
3
3
|
import { BlobDescriptor, PaymentHandlers as PaymentHandler, SignedEvent, Signer } from "./types.js";
|
|
4
4
|
import { mirrorBlob, MirrorOptions } from "./actions/mirror.js";
|
|
5
5
|
import { uploadBlob, UploadOptions } from "./actions/upload.js";
|
|
6
6
|
import { listBlobs, ListOptions } from "./actions/list.js";
|
|
7
7
|
import { downloadBlob, DownloadOptions } from "./actions/download.js";
|
|
8
8
|
import { deleteBlob, DeleteOptions } from "./actions/delete.js";
|
|
9
|
+
import { uploadMedia, UploadMediaOptions } from "./actions/media.js";
|
|
9
10
|
export type ServerType = string | URL;
|
|
10
11
|
export type UploadType = Blob | File | Buffer;
|
|
11
12
|
export declare class BlossomClient {
|
|
@@ -24,29 +25,35 @@ export declare class BlossomClient {
|
|
|
24
25
|
static listBlobs: typeof listBlobs;
|
|
25
26
|
static downloadBlob: typeof downloadBlob;
|
|
26
27
|
static deleteBlob: typeof deleteBlob;
|
|
27
|
-
|
|
28
|
+
static uploadMedia: typeof uploadMedia;
|
|
29
|
+
createDownloadAuth(hash: string, options?: DownloadAuthOptions): Promise<SignedEvent>;
|
|
28
30
|
downloadBlob(hash: string, opts?: Omit<DownloadOptions<URL>, "onAuth" | "onPayment" | "auth"> & {
|
|
29
31
|
auth?: SignedEvent | boolean;
|
|
30
32
|
payment?: boolean;
|
|
31
33
|
}): Promise<Blob>;
|
|
32
|
-
createUploadAuth(blob:
|
|
34
|
+
createUploadAuth(blob: string | UploadType, options?: UploadAuthOptions): Promise<SignedEvent>;
|
|
33
35
|
uploadBlob<B extends UploadType>(blob: B, opts?: Omit<UploadOptions<URL, B>, "onAuth" | "onPayment" | "auth"> & {
|
|
34
36
|
auth?: SignedEvent | boolean;
|
|
35
37
|
payment?: boolean;
|
|
36
38
|
}): Promise<BlobDescriptor>;
|
|
37
|
-
createMirrorAuth(blob: string | BlobDescriptor,
|
|
39
|
+
createMirrorAuth(blob: string | BlobDescriptor, options?: MirrorAuthOptions): Promise<SignedEvent>;
|
|
38
40
|
mirrorBlob(blob: BlobDescriptor, opts?: Omit<MirrorOptions<URL>, "onAuth" | "onPayment" | "auth"> & {
|
|
39
41
|
auth?: SignedEvent | boolean;
|
|
40
42
|
payment?: boolean;
|
|
41
43
|
}): Promise<BlobDescriptor>;
|
|
44
|
+
createMediaAuth(blob: string | UploadType, options?: Omit<UploadAuthOptions, "type">): Promise<SignedEvent>;
|
|
45
|
+
uploadMedia<B extends UploadType>(blob: B, opts?: Omit<UploadMediaOptions<URL, B>, "onAuth" | "onPayment" | "auth"> & {
|
|
46
|
+
auth?: SignedEvent | boolean;
|
|
47
|
+
payment?: boolean;
|
|
48
|
+
}): Promise<BlobDescriptor>;
|
|
42
49
|
static hasBlob(server: ServerType, hash: string): Promise<boolean>;
|
|
43
50
|
hasBlob(hash: string): Promise<boolean>;
|
|
44
|
-
createListAuth(
|
|
51
|
+
createListAuth(options?: ListAuthOptions): Promise<SignedEvent>;
|
|
45
52
|
listBlobs(pubkey: string, opts?: Omit<ListOptions<URL>, "onAuth" | "onPayment" | "auth"> & {
|
|
46
53
|
auth?: SignedEvent | boolean;
|
|
47
54
|
payment?: boolean;
|
|
48
55
|
}): Promise<BlobDescriptor[]>;
|
|
49
|
-
createDeleteAuth(
|
|
56
|
+
createDeleteAuth(blob: string | UploadType, options?: DeleteAuthOptions): Promise<SignedEvent>;
|
|
50
57
|
deleteBlob(hash: string, opts?: Omit<DeleteOptions<URL>, "onAuth" | "onPayment" | "auth"> & {
|
|
51
58
|
auth?: SignedEvent | boolean;
|
|
52
59
|
payment?: boolean;
|