blossom-client-sdk 4.0.0 → 4.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/lib/actions/delete.d.ts +2 -2
- package/lib/actions/delete.js +16 -4
- package/lib/actions/download.d.ts +2 -2
- package/lib/actions/download.js +16 -4
- package/lib/actions/list.d.ts +2 -2
- package/lib/actions/list.js +15 -3
- package/lib/actions/media.js +16 -4
- package/lib/actions/mirror.d.ts +2 -2
- package/lib/actions/mirror.js +16 -4
- package/lib/actions/multi-server.js +14 -3
- package/lib/actions/upload.d.ts +2 -2
- package/lib/actions/upload.js +16 -4
- package/lib/auth.d.ts +3 -1
- package/lib/auth.js +5 -3
- package/package.json +1 -1
- package/src/actions/delete.ts +16 -5
- package/src/actions/download.ts +16 -5
- package/src/actions/list.ts +16 -4
- package/src/actions/media.ts +14 -3
- package/src/actions/mirror.ts +16 -5
- package/src/actions/multi-server.ts +14 -3
- package/src/actions/upload.ts +16 -5
- package/src/auth.ts +11 -3
package/lib/actions/delete.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
|
3
3
|
export type DeleteOptions<S extends ServerType> = {
|
|
4
4
|
/** AbortSignal to cancel the action */
|
|
5
5
|
signal?: AbortSignal;
|
|
6
|
-
/** Override
|
|
7
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
8
8
|
/** Request timeout */
|
|
9
9
|
timeout?: number;
|
|
10
10
|
/**
|
package/lib/actions/delete.js
CHANGED
|
@@ -5,9 +5,17 @@ import { fetchWithTimeout } from "../helpers/fetch.js";
|
|
|
5
5
|
export async function deleteBlob(server, hash, opts) {
|
|
6
6
|
const url = new URL("/" + hash, server);
|
|
7
7
|
const headers = {};
|
|
8
|
-
// attach the
|
|
9
|
-
if (opts?.auth)
|
|
10
|
-
|
|
8
|
+
// attach the authorization if its already set
|
|
9
|
+
if (opts?.auth) {
|
|
10
|
+
if (typeof opts.auth === "boolean") {
|
|
11
|
+
if (!opts.onAuth)
|
|
12
|
+
throw new Error("Missing onAuth handler");
|
|
13
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, hash));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
11
19
|
let res = await fetchWithTimeout(url, {
|
|
12
20
|
method: "DELETE",
|
|
13
21
|
headers,
|
|
@@ -17,7 +25,11 @@ export async function deleteBlob(server, hash, opts) {
|
|
|
17
25
|
// handle auth and payment
|
|
18
26
|
switch (res.status) {
|
|
19
27
|
case 401: {
|
|
20
|
-
|
|
28
|
+
// throw an error if authorization is required but disabled
|
|
29
|
+
if (opts?.auth === false)
|
|
30
|
+
throw new Error("Authorization disabled");
|
|
31
|
+
// request authorization for this request
|
|
32
|
+
const auth = await opts?.onAuth?.(server, hash);
|
|
21
33
|
if (!auth)
|
|
22
34
|
throw new Error("Missing auth handler");
|
|
23
35
|
// Try delete with auth
|
|
@@ -3,8 +3,8 @@ import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
|
3
3
|
export type DownloadOptions<S extends ServerType> = {
|
|
4
4
|
/** AbortSignal to cancel the action */
|
|
5
5
|
signal?: AbortSignal;
|
|
6
|
-
/** Override
|
|
7
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
8
8
|
/** Request timeout */
|
|
9
9
|
timeout?: number;
|
|
10
10
|
/**
|
package/lib/actions/download.js
CHANGED
|
@@ -5,9 +5,17 @@ import { fetchWithTimeout } from "../helpers/index.js";
|
|
|
5
5
|
export async function downloadBlob(server, hash, opts) {
|
|
6
6
|
const url = new URL("/" + hash, server);
|
|
7
7
|
const headers = {};
|
|
8
|
-
// attach the
|
|
9
|
-
if (opts?.auth)
|
|
10
|
-
|
|
8
|
+
// attach the authorization if its already set
|
|
9
|
+
if (opts?.auth) {
|
|
10
|
+
if (typeof opts.auth === "boolean") {
|
|
11
|
+
if (!opts.onAuth)
|
|
12
|
+
throw new Error("Missing onAuth handler");
|
|
13
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, hash));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
11
19
|
let download = await fetchWithTimeout(url, {
|
|
12
20
|
headers,
|
|
13
21
|
signal: opts?.signal,
|
|
@@ -16,7 +24,11 @@ export async function downloadBlob(server, hash, opts) {
|
|
|
16
24
|
// handle auth and payment
|
|
17
25
|
switch (download.status) {
|
|
18
26
|
case 401: {
|
|
19
|
-
|
|
27
|
+
// throw an error if auth is requested and disabled
|
|
28
|
+
if (opts?.auth === false)
|
|
29
|
+
throw new Error("Authorization disabled");
|
|
30
|
+
// Request authorization for this request
|
|
31
|
+
const auth = await opts?.onAuth?.(server, hash);
|
|
20
32
|
if (!auth)
|
|
21
33
|
throw new Error("Missing auth handler");
|
|
22
34
|
// Try download with auth
|
package/lib/actions/list.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../ty
|
|
|
3
3
|
export type ListOptions<S extends ServerType> = {
|
|
4
4
|
/** AbortSignal to cancel the action */
|
|
5
5
|
signal?: AbortSignal;
|
|
6
|
-
/** Override
|
|
7
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
8
8
|
/** Request timeout */
|
|
9
9
|
timeout?: number;
|
|
10
10
|
since?: number;
|
package/lib/actions/list.js
CHANGED
|
@@ -10,13 +10,25 @@ export async function listBlobs(server, pubkey, opts) {
|
|
|
10
10
|
url.searchParams.append("until", String(opts.until));
|
|
11
11
|
// attach the auth if its already set
|
|
12
12
|
const headers = {};
|
|
13
|
-
if
|
|
14
|
-
|
|
13
|
+
// attach the authorization if its already set
|
|
14
|
+
if (opts?.auth) {
|
|
15
|
+
if (typeof opts.auth === "boolean") {
|
|
16
|
+
if (!opts.onAuth)
|
|
17
|
+
throw new Error("Missing onAuth handler");
|
|
18
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
15
24
|
let list = await fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
|
|
16
25
|
// handle auth and payments
|
|
17
26
|
switch (list.status) {
|
|
18
27
|
case 401: {
|
|
19
|
-
|
|
28
|
+
// throw an error if auth is requested and disabled
|
|
29
|
+
if (opts?.auth === false)
|
|
30
|
+
throw new Error("Authorization disabled");
|
|
31
|
+
const auth = await opts?.onAuth?.(server);
|
|
20
32
|
if (!auth)
|
|
21
33
|
throw new Error("Missing auth handler");
|
|
22
34
|
// Try list with auth
|
package/lib/actions/media.js
CHANGED
|
@@ -11,9 +11,17 @@ export async function uploadMedia(server, blob, opts) {
|
|
|
11
11
|
const headers = {
|
|
12
12
|
"X-SHA-256": sha256,
|
|
13
13
|
};
|
|
14
|
-
// attach the
|
|
15
|
-
if (opts?.auth)
|
|
16
|
-
|
|
14
|
+
// attach the authorization if its already set
|
|
15
|
+
if (opts?.auth) {
|
|
16
|
+
if (typeof opts.auth === "boolean") {
|
|
17
|
+
if (!opts.onAuth)
|
|
18
|
+
throw new Error("Missing onAuth handler");
|
|
19
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, sha256, "media", blob));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
17
25
|
// build check headers
|
|
18
26
|
const checkHeaders = {
|
|
19
27
|
...headers,
|
|
@@ -36,7 +44,11 @@ export async function uploadMedia(server, blob, opts) {
|
|
|
36
44
|
// handle auth and payment
|
|
37
45
|
switch (firstTry.status) {
|
|
38
46
|
case 401: {
|
|
39
|
-
|
|
47
|
+
// throw an error if auth is requested and disabled
|
|
48
|
+
if (opts?.auth === false)
|
|
49
|
+
throw new Error("Authorization disabled");
|
|
50
|
+
// Request authorization event for this upload
|
|
51
|
+
const auth = await opts?.onAuth?.(server, sha256, "media", blob);
|
|
40
52
|
if (!auth)
|
|
41
53
|
throw new Error("Missing auth handler");
|
|
42
54
|
// Try upload with auth
|
package/lib/actions/mirror.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../ty
|
|
|
3
3
|
export type MirrorOptions<S extends ServerType> = {
|
|
4
4
|
/** AbortSignal to cancel the action */
|
|
5
5
|
signal?: AbortSignal;
|
|
6
|
-
/** Override
|
|
7
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
8
8
|
/** Request timeout */
|
|
9
9
|
timeout?: number;
|
|
10
10
|
/**
|
package/lib/actions/mirror.js
CHANGED
|
@@ -11,9 +11,17 @@ export async function mirrorBlob(server, blob, opts) {
|
|
|
11
11
|
};
|
|
12
12
|
if (blob.type)
|
|
13
13
|
headers["X-Content-Type"] = blob.type;
|
|
14
|
-
// attach the
|
|
15
|
-
if (opts?.auth)
|
|
16
|
-
|
|
14
|
+
// attach the authorization if its already set
|
|
15
|
+
if (opts?.auth) {
|
|
16
|
+
if (typeof opts.auth === "boolean") {
|
|
17
|
+
if (!opts.onAuth)
|
|
18
|
+
throw new Error("Missing onAuth handler");
|
|
19
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, blob.sha256, blob));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
17
25
|
const body = JSON.stringify({ url: blob.url });
|
|
18
26
|
let mirror = await fetchWithTimeout(url, {
|
|
19
27
|
method: "PUT",
|
|
@@ -24,7 +32,11 @@ export async function mirrorBlob(server, blob, opts) {
|
|
|
24
32
|
});
|
|
25
33
|
switch (mirror.status) {
|
|
26
34
|
case 401: {
|
|
27
|
-
|
|
35
|
+
// throw an error if auth is requested and disabled
|
|
36
|
+
if (opts?.auth === false)
|
|
37
|
+
throw new Error("Authorization disabled");
|
|
38
|
+
// Request authorization event for this mirror
|
|
39
|
+
const auth = await opts?.onAuth?.(server, blob.sha256, blob);
|
|
28
40
|
if (!auth)
|
|
29
41
|
throw new Error("Missing auth handler");
|
|
30
42
|
// Try mirror with auth
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { doseAuthMatchBlob } from "../auth.js";
|
|
2
2
|
import { getBlobSha256 } from "../helpers/index.js";
|
|
3
3
|
import { MediaEndpointMissingError, uploadMedia } from "./media.js";
|
|
4
4
|
import { mirrorBlob } from "./mirror.js";
|
|
@@ -41,11 +41,11 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
41
41
|
let initialUpload;
|
|
42
42
|
const results = new Map();
|
|
43
43
|
// reuse auth events
|
|
44
|
-
let authEvents = options.auth ? [options.auth] : [];
|
|
44
|
+
let authEvents = typeof options.auth === "object" ? [options.auth] : [];
|
|
45
45
|
const handleAuthRequest = async (server, sha256, type) => {
|
|
46
46
|
// check if any existing auth events match
|
|
47
47
|
for (const auth of authEvents) {
|
|
48
|
-
if (await
|
|
48
|
+
if (await doseAuthMatchBlob(auth, server, sha256, type))
|
|
49
49
|
return auth;
|
|
50
50
|
}
|
|
51
51
|
// create a new auth event
|
|
@@ -74,6 +74,10 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
74
74
|
options.onStart?.(server, sha256, blob);
|
|
75
75
|
// attempt to upload media
|
|
76
76
|
initialUpload = await uploadMedia(server, blob, {
|
|
77
|
+
// pass through abort signal
|
|
78
|
+
signal: options.signal,
|
|
79
|
+
// pass through authorization flag
|
|
80
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
77
81
|
onAuth: handleAuthRequest,
|
|
78
82
|
onPayment: handlePaymentRequest,
|
|
79
83
|
});
|
|
@@ -113,7 +117,10 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
113
117
|
try {
|
|
114
118
|
options.onStart?.(server, initialUpload.sha256, blob);
|
|
115
119
|
metadata = await mirrorBlob(server, initialUpload, {
|
|
120
|
+
// pass through abort signal
|
|
116
121
|
signal: options.signal,
|
|
122
|
+
// pass through authorization flag
|
|
123
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
117
124
|
onAuth: (server, sha256) => handleAuthRequest(server, sha256, "upload"),
|
|
118
125
|
onPayment: handlePaymentRequest,
|
|
119
126
|
timeout: options.mirrorTimeout,
|
|
@@ -130,6 +137,10 @@ export async function multiServerUpload(servers, blob, opts) {
|
|
|
130
137
|
const sha256 = await getBlobSha256(blob);
|
|
131
138
|
options.onStart?.(server, sha256, blob);
|
|
132
139
|
metadata = await uploadBlob(server, blob, {
|
|
140
|
+
// pass through abort signal
|
|
141
|
+
signal: options.signal,
|
|
142
|
+
// pass through authorization flag
|
|
143
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
133
144
|
onAuth: handleAuthRequest,
|
|
134
145
|
onPayment: handlePaymentRequest,
|
|
135
146
|
timeout: options.mirrorTimeout,
|
package/lib/actions/upload.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../ty
|
|
|
3
3
|
export type UploadOptions<S extends ServerType, B extends UploadType> = {
|
|
4
4
|
/** AbortSignal to cancel the action */
|
|
5
5
|
signal?: AbortSignal;
|
|
6
|
-
/** Override
|
|
7
|
-
auth?: SignedEvent;
|
|
6
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
7
|
+
auth?: SignedEvent | boolean;
|
|
8
8
|
/** Request timeout */
|
|
9
9
|
timeout?: number;
|
|
10
10
|
/**
|
package/lib/actions/upload.js
CHANGED
|
@@ -8,9 +8,17 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
8
8
|
const headers = {
|
|
9
9
|
"X-SHA-256": sha256,
|
|
10
10
|
};
|
|
11
|
-
// attach the
|
|
12
|
-
if (opts?.auth)
|
|
13
|
-
|
|
11
|
+
// attach the authorization if its already set
|
|
12
|
+
if (opts?.auth) {
|
|
13
|
+
if (typeof opts.auth === "boolean") {
|
|
14
|
+
if (!opts.onAuth)
|
|
15
|
+
throw new Error("Missing onAuth handler");
|
|
16
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, sha256, "upload", blob));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
14
22
|
// build check headers
|
|
15
23
|
const checkHeaders = {
|
|
16
24
|
...headers,
|
|
@@ -39,7 +47,11 @@ export async function uploadBlob(server, blob, opts) {
|
|
|
39
47
|
// handle auth and payment
|
|
40
48
|
switch (firstTry.status) {
|
|
41
49
|
case 401: {
|
|
42
|
-
|
|
50
|
+
// throw an error if auth is requested and disabled
|
|
51
|
+
if (opts?.auth === false)
|
|
52
|
+
throw new Error("Authorization disabled");
|
|
53
|
+
// Request authorization event for this upload
|
|
54
|
+
const auth = await opts?.onAuth?.(server, sha256, "upload", blob);
|
|
43
55
|
if (!auth)
|
|
44
56
|
throw new Error("Missing auth handler");
|
|
45
57
|
// Try upload with auth
|
package/lib/auth.d.ts
CHANGED
|
@@ -5,7 +5,9 @@ export declare const oneHour: () => number;
|
|
|
5
5
|
/** Encodes an auth event into a nostr authorization header */
|
|
6
6
|
export declare function encodeAuthorizationHeader(event: SignedEvent): string;
|
|
7
7
|
/** Checks if an auth event matches a server / blob upload */
|
|
8
|
-
export declare function
|
|
8
|
+
export declare function doseAuthMatchBlob(auth: SignedEvent, server: ServerType, blob: string | UploadType, type?: string): Promise<boolean>;
|
|
9
|
+
/** @deprecated Use `doseAuthMatchBlob` instead */
|
|
10
|
+
export declare const doseAuthMatchUpload: typeof doseAuthMatchBlob;
|
|
9
11
|
export type AuthType = "upload" | "list" | "delete" | "get" | "media";
|
|
10
12
|
export type AuthEventOptions = {
|
|
11
13
|
blobs?: string | string[] | UploadType | UploadType[];
|
package/lib/auth.js
CHANGED
|
@@ -8,9 +8,9 @@ export function encodeAuthorizationHeader(event) {
|
|
|
8
8
|
return "Nostr " + btoa(JSON.stringify(event));
|
|
9
9
|
}
|
|
10
10
|
/** Checks if an auth event matches a server / blob upload */
|
|
11
|
-
export async function
|
|
12
|
-
const
|
|
13
|
-
if (
|
|
11
|
+
export async function doseAuthMatchBlob(auth, server, blob, type = "upload") {
|
|
12
|
+
const authType = auth.tags.find((t) => t[0] === "t")?.[1];
|
|
13
|
+
if (authType !== type)
|
|
14
14
|
return false;
|
|
15
15
|
const sha256 = typeof blob === "string" ? blob : await getBlobSha256(blob);
|
|
16
16
|
for (const tag of auth.tags) {
|
|
@@ -27,6 +27,8 @@ export async function doseAuthMatchUpload(auth, server, blob) {
|
|
|
27
27
|
}
|
|
28
28
|
return false;
|
|
29
29
|
}
|
|
30
|
+
/** @deprecated Use `doseAuthMatchBlob` instead */
|
|
31
|
+
export const doseAuthMatchUpload = doseAuthMatchBlob;
|
|
30
32
|
async function normalizeToHash(blob) {
|
|
31
33
|
return typeof blob === "string" ? blob : getBlobSha256(blob);
|
|
32
34
|
}
|
package/package.json
CHANGED
package/src/actions/delete.ts
CHANGED
|
@@ -7,8 +7,8 @@ import { PaymentRequest, PaymentToken, SignedEvent } from "../types.js";
|
|
|
7
7
|
export type DeleteOptions<S extends ServerType> = {
|
|
8
8
|
/** AbortSignal to cancel the action */
|
|
9
9
|
signal?: AbortSignal;
|
|
10
|
-
/** Override
|
|
11
|
-
auth?: SignedEvent;
|
|
10
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
11
|
+
auth?: SignedEvent | boolean;
|
|
12
12
|
/** Request timeout */
|
|
13
13
|
timeout?: number;
|
|
14
14
|
/**
|
|
@@ -32,8 +32,15 @@ export async function deleteBlob<S extends ServerType>(server: S, hash: string,
|
|
|
32
32
|
|
|
33
33
|
const headers: HeadersInit = {};
|
|
34
34
|
|
|
35
|
-
// attach the
|
|
36
|
-
if (opts?.auth)
|
|
35
|
+
// attach the authorization if its already set
|
|
36
|
+
if (opts?.auth) {
|
|
37
|
+
if (typeof opts.auth === "boolean") {
|
|
38
|
+
if (!opts.onAuth) throw new Error("Missing onAuth handler");
|
|
39
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, hash));
|
|
40
|
+
} else {
|
|
41
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
37
44
|
|
|
38
45
|
let res = await fetchWithTimeout(url, {
|
|
39
46
|
method: "DELETE",
|
|
@@ -45,7 +52,11 @@ export async function deleteBlob<S extends ServerType>(server: S, hash: string,
|
|
|
45
52
|
// handle auth and payment
|
|
46
53
|
switch (res.status) {
|
|
47
54
|
case 401: {
|
|
48
|
-
|
|
55
|
+
// throw an error if authorization is required but disabled
|
|
56
|
+
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
57
|
+
|
|
58
|
+
// request authorization for this request
|
|
59
|
+
const auth = await opts?.onAuth?.(server, hash);
|
|
49
60
|
if (!auth) throw new Error("Missing auth handler");
|
|
50
61
|
|
|
51
62
|
// Try delete with auth
|
package/src/actions/download.ts
CHANGED
|
@@ -7,8 +7,8 @@ import { fetchWithTimeout } from "../helpers/index.js";
|
|
|
7
7
|
export type DownloadOptions<S extends ServerType> = {
|
|
8
8
|
/** AbortSignal to cancel the action */
|
|
9
9
|
signal?: AbortSignal;
|
|
10
|
-
/** Override
|
|
11
|
-
auth?: SignedEvent;
|
|
10
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
11
|
+
auth?: SignedEvent | boolean;
|
|
12
12
|
/** Request timeout */
|
|
13
13
|
timeout?: number;
|
|
14
14
|
/**
|
|
@@ -32,8 +32,15 @@ export async function downloadBlob<S extends ServerType>(server: S, hash: string
|
|
|
32
32
|
|
|
33
33
|
const headers: HeadersInit = {};
|
|
34
34
|
|
|
35
|
-
// attach the
|
|
36
|
-
if (opts?.auth)
|
|
35
|
+
// attach the authorization if its already set
|
|
36
|
+
if (opts?.auth) {
|
|
37
|
+
if (typeof opts.auth === "boolean") {
|
|
38
|
+
if (!opts.onAuth) throw new Error("Missing onAuth handler");
|
|
39
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, hash));
|
|
40
|
+
} else {
|
|
41
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
37
44
|
|
|
38
45
|
let download = await fetchWithTimeout(url, {
|
|
39
46
|
headers,
|
|
@@ -44,7 +51,11 @@ export async function downloadBlob<S extends ServerType>(server: S, hash: string
|
|
|
44
51
|
// handle auth and payment
|
|
45
52
|
switch (download.status) {
|
|
46
53
|
case 401: {
|
|
47
|
-
|
|
54
|
+
// throw an error if auth is requested and disabled
|
|
55
|
+
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
56
|
+
|
|
57
|
+
// Request authorization for this request
|
|
58
|
+
const auth = await opts?.onAuth?.(server, hash);
|
|
48
59
|
if (!auth) throw new Error("Missing auth handler");
|
|
49
60
|
|
|
50
61
|
// Try download with auth
|
package/src/actions/list.ts
CHANGED
|
@@ -7,8 +7,8 @@ import { fetchWithTimeout } from "../helpers/index.js";
|
|
|
7
7
|
export type ListOptions<S extends ServerType> = {
|
|
8
8
|
/** AbortSignal to cancel the action */
|
|
9
9
|
signal?: AbortSignal;
|
|
10
|
-
/** Override
|
|
11
|
-
auth?: SignedEvent;
|
|
10
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
11
|
+
auth?: SignedEvent | boolean;
|
|
12
12
|
/** Request timeout */
|
|
13
13
|
timeout?: number;
|
|
14
14
|
since?: number;
|
|
@@ -38,14 +38,26 @@ export async function listBlobs<S extends ServerType>(
|
|
|
38
38
|
|
|
39
39
|
// attach the auth if its already set
|
|
40
40
|
const headers: HeadersInit = {};
|
|
41
|
-
|
|
41
|
+
|
|
42
|
+
// attach the authorization if its already set
|
|
43
|
+
if (opts?.auth) {
|
|
44
|
+
if (typeof opts.auth === "boolean") {
|
|
45
|
+
if (!opts.onAuth) throw new Error("Missing onAuth handler");
|
|
46
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server));
|
|
47
|
+
} else {
|
|
48
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
42
51
|
|
|
43
52
|
let list = await fetchWithTimeout(url, { headers, signal: opts?.signal, timeout: opts?.timeout });
|
|
44
53
|
|
|
45
54
|
// handle auth and payments
|
|
46
55
|
switch (list.status) {
|
|
47
56
|
case 401: {
|
|
48
|
-
|
|
57
|
+
// throw an error if auth is requested and disabled
|
|
58
|
+
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
59
|
+
|
|
60
|
+
const auth = await opts?.onAuth?.(server);
|
|
49
61
|
if (!auth) throw new Error("Missing auth handler");
|
|
50
62
|
|
|
51
63
|
// Try list with auth
|
package/src/actions/media.ts
CHANGED
|
@@ -23,8 +23,15 @@ export async function uploadMedia<S extends ServerType, B extends UploadType>(
|
|
|
23
23
|
"X-SHA-256": sha256,
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
// attach the
|
|
27
|
-
if (opts?.auth)
|
|
26
|
+
// attach the authorization if its already set
|
|
27
|
+
if (opts?.auth) {
|
|
28
|
+
if (typeof opts.auth === "boolean") {
|
|
29
|
+
if (!opts.onAuth) throw new Error("Missing onAuth handler");
|
|
30
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, sha256, "media", blob));
|
|
31
|
+
} else {
|
|
32
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
28
35
|
|
|
29
36
|
// build check headers
|
|
30
37
|
const checkHeaders: Record<string, string> = {
|
|
@@ -51,7 +58,11 @@ export async function uploadMedia<S extends ServerType, B extends UploadType>(
|
|
|
51
58
|
// handle auth and payment
|
|
52
59
|
switch (firstTry.status) {
|
|
53
60
|
case 401: {
|
|
54
|
-
|
|
61
|
+
// throw an error if auth is requested and disabled
|
|
62
|
+
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
63
|
+
|
|
64
|
+
// Request authorization event for this upload
|
|
65
|
+
const auth = await opts?.onAuth?.(server, sha256, "media", blob);
|
|
55
66
|
if (!auth) throw new Error("Missing auth handler");
|
|
56
67
|
|
|
57
68
|
// Try upload with auth
|
package/src/actions/mirror.ts
CHANGED
|
@@ -7,8 +7,8 @@ import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../ty
|
|
|
7
7
|
export type MirrorOptions<S extends ServerType> = {
|
|
8
8
|
/** AbortSignal to cancel the action */
|
|
9
9
|
signal?: AbortSignal;
|
|
10
|
-
/** Override
|
|
11
|
-
auth?: SignedEvent;
|
|
10
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
11
|
+
auth?: SignedEvent | boolean;
|
|
12
12
|
/** Request timeout */
|
|
13
13
|
timeout?: number;
|
|
14
14
|
/**
|
|
@@ -43,8 +43,15 @@ export async function mirrorBlob<S extends ServerType>(
|
|
|
43
43
|
};
|
|
44
44
|
if (blob.type) headers["X-Content-Type"] = blob.type;
|
|
45
45
|
|
|
46
|
-
// attach the
|
|
47
|
-
if (opts?.auth)
|
|
46
|
+
// attach the authorization if its already set
|
|
47
|
+
if (opts?.auth) {
|
|
48
|
+
if (typeof opts.auth === "boolean") {
|
|
49
|
+
if (!opts.onAuth) throw new Error("Missing onAuth handler");
|
|
50
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, blob.sha256, blob));
|
|
51
|
+
} else {
|
|
52
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
48
55
|
|
|
49
56
|
const body = JSON.stringify({ url: blob.url });
|
|
50
57
|
let mirror = await fetchWithTimeout(url, {
|
|
@@ -57,7 +64,11 @@ export async function mirrorBlob<S extends ServerType>(
|
|
|
57
64
|
|
|
58
65
|
switch (mirror.status) {
|
|
59
66
|
case 401: {
|
|
60
|
-
|
|
67
|
+
// throw an error if auth is requested and disabled
|
|
68
|
+
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
69
|
+
|
|
70
|
+
// Request authorization event for this mirror
|
|
71
|
+
const auth = await opts?.onAuth?.(server, blob.sha256, blob);
|
|
61
72
|
if (!auth) throw new Error("Missing auth handler");
|
|
62
73
|
|
|
63
74
|
// Try mirror with auth
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { doseAuthMatchBlob } from "../auth.js";
|
|
2
2
|
import { ServerType, UploadType } from "../client.js";
|
|
3
3
|
import { getBlobSha256 } from "../helpers/index.js";
|
|
4
4
|
import { BlobDescriptor, PaymentRequest } from "../types.js";
|
|
@@ -86,11 +86,11 @@ export async function multiServerUpload<S extends ServerType, B extends UploadTy
|
|
|
86
86
|
const results = new Map<S, BlobDescriptor>();
|
|
87
87
|
|
|
88
88
|
// reuse auth events
|
|
89
|
-
let authEvents = options.auth ? [options.auth] : [];
|
|
89
|
+
let authEvents = typeof options.auth === "object" ? [options.auth] : [];
|
|
90
90
|
const handleAuthRequest = async (server: S, sha256: string, type: "upload" | "media") => {
|
|
91
91
|
// check if any existing auth events match
|
|
92
92
|
for (const auth of authEvents) {
|
|
93
|
-
if (await
|
|
93
|
+
if (await doseAuthMatchBlob(auth, server, sha256, type)) return auth;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
// create a new auth event
|
|
@@ -122,6 +122,10 @@ export async function multiServerUpload<S extends ServerType, B extends UploadTy
|
|
|
122
122
|
|
|
123
123
|
// attempt to upload media
|
|
124
124
|
initialUpload = await uploadMedia(server, blob, {
|
|
125
|
+
// pass through abort signal
|
|
126
|
+
signal: options.signal,
|
|
127
|
+
// pass through authorization flag
|
|
128
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
125
129
|
onAuth: handleAuthRequest,
|
|
126
130
|
onPayment: handlePaymentRequest,
|
|
127
131
|
});
|
|
@@ -166,7 +170,10 @@ export async function multiServerUpload<S extends ServerType, B extends UploadTy
|
|
|
166
170
|
options.onStart?.(server, initialUpload.sha256, blob);
|
|
167
171
|
|
|
168
172
|
metadata = await mirrorBlob(server, initialUpload, {
|
|
173
|
+
// pass through abort signal
|
|
169
174
|
signal: options.signal,
|
|
175
|
+
// pass through authorization flag
|
|
176
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
170
177
|
onAuth: (server, sha256) => handleAuthRequest(server, sha256, "upload"),
|
|
171
178
|
onPayment: handlePaymentRequest,
|
|
172
179
|
timeout: options.mirrorTimeout,
|
|
@@ -183,6 +190,10 @@ export async function multiServerUpload<S extends ServerType, B extends UploadTy
|
|
|
183
190
|
options.onStart?.(server, sha256, blob);
|
|
184
191
|
|
|
185
192
|
metadata = await uploadBlob(server, blob, {
|
|
193
|
+
// pass through abort signal
|
|
194
|
+
signal: options.signal,
|
|
195
|
+
// pass through authorization flag
|
|
196
|
+
auth: typeof options.auth === "boolean" ? options.auth : undefined,
|
|
186
197
|
onAuth: handleAuthRequest,
|
|
187
198
|
onPayment: handlePaymentRequest,
|
|
188
199
|
timeout: options.mirrorTimeout,
|
package/src/actions/upload.ts
CHANGED
|
@@ -7,8 +7,8 @@ import { BlobDescriptor, PaymentRequest, PaymentToken, SignedEvent } from "../ty
|
|
|
7
7
|
export type UploadOptions<S extends ServerType, B extends UploadType> = {
|
|
8
8
|
/** AbortSignal to cancel the action */
|
|
9
9
|
signal?: AbortSignal;
|
|
10
|
-
/** Override
|
|
11
|
-
auth?: SignedEvent;
|
|
10
|
+
/** Override authorization event, or true to always use authorization, false to disable authorization */
|
|
11
|
+
auth?: SignedEvent | boolean;
|
|
12
12
|
/** Request timeout */
|
|
13
13
|
timeout?: number;
|
|
14
14
|
/**
|
|
@@ -41,8 +41,15 @@ export async function uploadBlob<S extends ServerType, B extends UploadType>(
|
|
|
41
41
|
"X-SHA-256": sha256,
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
// attach the
|
|
45
|
-
if (opts?.auth)
|
|
44
|
+
// attach the authorization if its already set
|
|
45
|
+
if (opts?.auth) {
|
|
46
|
+
if (typeof opts.auth === "boolean") {
|
|
47
|
+
if (!opts.onAuth) throw new Error("Missing onAuth handler");
|
|
48
|
+
headers["Authorization"] = encodeAuthorizationHeader(await opts.onAuth(server, sha256, "upload", blob));
|
|
49
|
+
} else {
|
|
50
|
+
headers["Authorization"] = encodeAuthorizationHeader(opts.auth);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
46
53
|
|
|
47
54
|
// build check headers
|
|
48
55
|
const checkHeaders: Record<string, string> = {
|
|
@@ -75,7 +82,11 @@ export async function uploadBlob<S extends ServerType, B extends UploadType>(
|
|
|
75
82
|
// handle auth and payment
|
|
76
83
|
switch (firstTry.status) {
|
|
77
84
|
case 401: {
|
|
78
|
-
|
|
85
|
+
// throw an error if auth is requested and disabled
|
|
86
|
+
if (opts?.auth === false) throw new Error("Authorization disabled");
|
|
87
|
+
|
|
88
|
+
// Request authorization event for this upload
|
|
89
|
+
const auth = await opts?.onAuth?.(server, sha256, "upload", blob);
|
|
79
90
|
if (!auth) throw new Error("Missing auth handler");
|
|
80
91
|
|
|
81
92
|
// Try upload with auth
|
package/src/auth.ts
CHANGED
|
@@ -13,9 +13,14 @@ export function encodeAuthorizationHeader(event: SignedEvent) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/** Checks if an auth event matches a server / blob upload */
|
|
16
|
-
export async function
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
export async function doseAuthMatchBlob(
|
|
17
|
+
auth: SignedEvent,
|
|
18
|
+
server: ServerType,
|
|
19
|
+
blob: string | UploadType,
|
|
20
|
+
type: string = "upload",
|
|
21
|
+
) {
|
|
22
|
+
const authType = auth.tags.find((t) => t[0] === "t")?.[1];
|
|
23
|
+
if (authType !== type) return false;
|
|
19
24
|
|
|
20
25
|
const sha256 = typeof blob === "string" ? blob : await getBlobSha256(blob);
|
|
21
26
|
|
|
@@ -33,6 +38,9 @@ export async function doseAuthMatchUpload(auth: SignedEvent, server: ServerType,
|
|
|
33
38
|
return false;
|
|
34
39
|
}
|
|
35
40
|
|
|
41
|
+
/** @deprecated Use `doseAuthMatchBlob` instead */
|
|
42
|
+
export const doseAuthMatchUpload = doseAuthMatchBlob;
|
|
43
|
+
|
|
36
44
|
async function normalizeToHash(blob: string | UploadType) {
|
|
37
45
|
return typeof blob === "string" ? blob : getBlobSha256(blob);
|
|
38
46
|
}
|