blossom-client-sdk 0.6.0 → 0.7.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 CHANGED
@@ -4,4 +4,14 @@
4
4
 
5
5
  ### Minor Changes
6
6
 
7
+ - Include "x" tag with sha256 in upload auth
8
+
9
+ ### Patch Changes
10
+
11
+ - Allow URL class for server
12
+
13
+ ## 0.7.0
14
+
15
+ ### Minor Changes
16
+
7
17
  - e2e93f5: Add `handleImageFallbacks` method
package/lib/client.d.ts CHANGED
@@ -32,24 +32,27 @@ export declare class HTTPError extends Error {
32
32
  } | string);
33
33
  static handleErrorResponse(res: Response): Promise<void>;
34
34
  }
35
+ type ServerType = string | URL;
36
+ type UploadType = Blob | File;
35
37
  export declare class BlossomClient {
36
- server: string;
38
+ server: URL;
37
39
  signer?: Signer;
38
- constructor(server: string, signer?: Signer);
40
+ constructor(server: string | URL, signer?: Signer);
41
+ static getFileSha256(file: UploadType): Promise<string>;
39
42
  static getGetAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
40
- static getUploadAuth(file: File, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
43
+ static getUploadAuth(file: UploadType, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
41
44
  static getDeleteAuth(hash: string, signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
42
45
  static getListAuth(signer: Signer, message?: string, expiration?: number): Promise<SignedEvent>;
43
46
  getGetAuth(message?: string, expiration?: number): Promise<SignedEvent>;
44
- getUploadAuth(file: File, message?: string, expiration?: number): Promise<SignedEvent>;
47
+ getUploadAuth(file: UploadType, message?: string, expiration?: number): Promise<SignedEvent>;
45
48
  getDeleteAuth(hash: string, message?: string, expiration?: number): Promise<SignedEvent>;
46
49
  getListAuth(message?: string, expiration?: number): Promise<SignedEvent>;
47
50
  static encodeAuthorizationHeader(event: SignedEvent): string;
48
- static getBlob(server: string, hash: string, auth?: SignedEvent): Promise<Blob>;
51
+ static getBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<Blob>;
49
52
  getBlob(hash: string, auth?: SignedEvent | boolean): Promise<Blob>;
50
- static hasBlob(server: string, hash: string): Promise<boolean>;
53
+ static hasBlob(server: ServerType, hash: string): Promise<boolean>;
51
54
  hasBlob(hash: string): Promise<boolean>;
52
- static listBlobs(server: string, pubkey: string, opts?: {
55
+ static listBlobs(server: ServerType, pubkey: string, opts?: {
53
56
  since?: number;
54
57
  until?: number;
55
58
  }, auth?: SignedEvent): Promise<BlobDescriptor[]>;
@@ -57,8 +60,9 @@ export declare class BlossomClient {
57
60
  since?: number;
58
61
  until?: number;
59
62
  }, auth?: SignedEvent | boolean): Promise<BlobDescriptor[]>;
60
- static deleteBlob(server: string, hash: string, auth?: SignedEvent): Promise<string>;
63
+ static deleteBlob(server: ServerType, hash: string, auth?: SignedEvent): Promise<string>;
61
64
  deleteBlob(hash: string, auth?: SignedEvent | boolean): Promise<string>;
62
- static uploadBlob(server: string, file: File, auth?: SignedEvent): Promise<BlobDescriptor>;
65
+ static uploadBlob(server: ServerType, file: UploadType, auth?: SignedEvent): Promise<BlobDescriptor>;
63
66
  uploadBlob(file: File, auth?: SignedEvent | boolean): Promise<BlobDescriptor>;
64
67
  }
68
+ export {};
package/lib/client.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import fetch from "cross-fetch";
2
+ import { bytesToHex } from "@noble/hashes/utils";
3
+ import { readFileAsArrayBuffer as readBlobAsArrayBuffer } from "./helpers.js";
2
4
  const now = () => Math.floor(new Date().valueOf() / 1000);
3
5
  const oneHour = () => now() + 60 * 60;
4
6
  export const AUTH_EVENT_KIND = 24242;
@@ -24,9 +26,22 @@ export class HTTPError extends Error {
24
26
  }
25
27
  export class BlossomClient {
26
28
  constructor(server, signer) {
27
- this.server = new URL("/", server).toString();
29
+ this.server = new URL("/", server);
28
30
  this.signer = signer;
29
31
  }
32
+ static async getFileSha256(file) {
33
+ const buffer = file instanceof File ? await file.arrayBuffer() : await readBlobAsArrayBuffer(file);
34
+ let hash;
35
+ if (crypto.subtle) {
36
+ const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
37
+ hash = new Uint8Array(hashBuffer);
38
+ }
39
+ else {
40
+ const { sha256 } = await import("@noble/hashes/sha256");
41
+ hash = sha256.create().update(new Uint8Array(buffer)).digest();
42
+ }
43
+ return bytesToHex(hash);
44
+ }
30
45
  static async getGetAuth(signer, message = "Get Blobs", expiration = oneHour()) {
31
46
  return await signer({
32
47
  created_at: now(),
@@ -39,16 +54,20 @@ export class BlossomClient {
39
54
  });
40
55
  }
41
56
  static async getUploadAuth(file, signer, message = "Upload Blob", expiration = oneHour()) {
57
+ const sha256 = await BlossomClient.getFileSha256(file);
58
+ // add tags
59
+ const tags = [];
60
+ tags.push(["t", "upload"]);
61
+ if (file instanceof File)
62
+ tags.push(["name", file.name]);
63
+ tags.push(["size", String(file.size)]);
64
+ tags.push(["x", sha256]);
65
+ tags.push(["expiration", String(expiration)]);
42
66
  return await signer({
43
67
  created_at: now(),
44
68
  kind: AUTH_EVENT_KIND,
45
69
  content: message,
46
- tags: [
47
- ["t", "upload"],
48
- ["name", file.name],
49
- ["size", String(file.size)],
50
- ["expiration", String(expiration)],
51
- ],
70
+ tags,
52
71
  });
53
72
  }
54
73
  static async getDeleteAuth(hash, signer, message = "Delete Blob", expiration = oneHour()) {
package/lib/helpers.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  /** returns the last sha256 in a URL */
2
2
  export declare function getHashFromURL(url: string | URL): string | null;
3
+ export declare function readFileAsArrayBuffer(file: Blob | File): Promise<ArrayBuffer>;
package/lib/helpers.js CHANGED
@@ -7,3 +7,18 @@ export function getHashFromURL(url) {
7
7
  return hashes[hashes.length - 1][0];
8
8
  return null;
9
9
  }
10
+ export function readFileAsArrayBuffer(file) {
11
+ return new Promise((resolve, reject) => {
12
+ const reader = new FileReader();
13
+ reader.onload = (e) => {
14
+ const result = e.target?.result;
15
+ if (result == undefined || typeof result !== "object") {
16
+ reject();
17
+ return;
18
+ }
19
+ resolve(result);
20
+ };
21
+ reader.onerror = (error) => reject(error);
22
+ reader.readAsArrayBuffer(file);
23
+ });
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blossom-client-sdk",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "A simple client for manage blobs on blossom servers",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -39,6 +39,7 @@
39
39
  }
40
40
  },
41
41
  "dependencies": {
42
+ "@noble/hashes": "^1.4.0",
42
43
  "cross-fetch": "^4.0.0"
43
44
  },
44
45
  "devDependencies": {