@uploadista/expo 0.0.3

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.
@@ -0,0 +1,70 @@
1
+ import * as Crypto from "expo-crypto";
2
+
3
+ /**
4
+ * Compute SHA-256 checksum using Web Crypto API
5
+ * Compatible with React Native and Expo environments
6
+ *
7
+ * @param data - Uint8Array to hash
8
+ * @returns Promise that resolves to hex-encoded SHA-256 checksum
9
+ */
10
+ export async function computeUint8ArraySha256(
11
+ data: Uint8Array,
12
+ ): Promise<string> {
13
+ try {
14
+ // Compute SHA-256 hash using Web Crypto API
15
+ const hashBuffer = await Crypto.digest(
16
+ Crypto.CryptoDigestAlgorithm.SHA256,
17
+ data,
18
+ );
19
+
20
+ // Convert hash to hex string
21
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
22
+ const hashHex = hashArray
23
+ .map((byte) => byte.toString(16).padStart(2, "0"))
24
+ .join("");
25
+
26
+ return hashHex;
27
+ } catch (error) {
28
+ throw new Error(
29
+ `Failed to compute checksum: ${error instanceof Error ? error.message : "Unknown error"}`,
30
+ );
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Compute SHA-256 checksum of a Blob using Web Crypto API
36
+ * Compatible with React Native and Expo Blob objects
37
+ *
38
+ * @param blob - Blob to hash
39
+ * @returns Promise that resolves to hex-encoded SHA-256 checksum
40
+ */
41
+ export async function computeblobSha256(blob: Blob): Promise<string> {
42
+ try {
43
+ // Convert Blob to Uint8Array using FileReader for compatibility
44
+ const uint8Array = await blobToUint8Array(blob);
45
+ return computeUint8ArraySha256(uint8Array);
46
+ } catch (error) {
47
+ throw new Error(
48
+ `Failed to compute file checksum: ${error instanceof Error ? error.message : "Unknown error"}`,
49
+ );
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Convert Blob to Uint8Array using FileReader
55
+ * Works in React Native and Expo environments
56
+ */
57
+ async function blobToUint8Array(blob: Blob): Promise<Uint8Array> {
58
+ return new Promise((resolve, reject) => {
59
+ const reader = new FileReader();
60
+ reader.onload = () => {
61
+ if (reader.result instanceof ArrayBuffer) {
62
+ resolve(new Uint8Array(reader.result));
63
+ } else {
64
+ reject(new Error("FileReader result is not an ArrayBuffer"));
65
+ }
66
+ };
67
+ reader.onerror = () => reject(reader.error);
68
+ reader.readAsArrayBuffer(blob);
69
+ });
70
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "extends": "@uploadista/typescript-config/base.json",
3
+ "compilerOptions": {
4
+ "jsx": "react-jsx",
5
+ "lib": ["ES2020", "ES2020.Promise"],
6
+ "baseUrl": "./",
7
+ "paths": {
8
+ "@/*": ["./src/*"]
9
+ },
10
+ "typeRoots": ["../../node_modules/@types", "./src"],
11
+ "types": [],
12
+ "outDir": "./dist",
13
+ "rootDir": "./src"
14
+ },
15
+ "include": [
16
+ "src/**/*.ts",
17
+ "src/**/*.d.ts",
18
+ ".expo/types/**/*.ts",
19
+ "expo-env.d.ts"
20
+ ],
21
+ "exclude": [
22
+ "node_modules",
23
+ "dist",
24
+ "**/__tests__/**",
25
+ "**/*.test.ts",
26
+ "**/*.spec.ts"
27
+ ]
28
+ }