@file-cabinet/web 0.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.
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Converts bytes into a raw base64 string.
3
+ * Doesn't include a data URL prefix.
4
+ *
5
+ * @param bytes - The data to convert.
6
+ * @returns Raw base64 data string.
7
+ **/
8
+ export declare function bytesToBase64(bytes: Uint8Array): string;
9
+ /**
10
+ * Converts raw base64 string into an array of bytes.
11
+ *
12
+ * @param data - The data to convert.
13
+ * @returns Array of bytes.
14
+ **/
15
+ export declare function base64ToBytes(data: string): Uint8Array;
16
+ /**
17
+ * Converts a raw base64 string or data URL into a File object.
18
+ * If no MIME type is found, defaults to 'application/octet-stream'.
19
+ *
20
+ * @param data - The base64 data to convert.
21
+ * @param fileName - Name for the file (default = 'file')
22
+ * @returns File object.
23
+ **/
24
+ export declare function base64ToFile(data: string, fileName?: string): Promise<File>;
25
+ /**
26
+ * Converts a buffer into a File object using MIME type detection.
27
+ * Infers file extension from binary content.
28
+ *
29
+ * @param buff - The data to convert.
30
+ * @param fileName - Name for the file (default = 'file')
31
+ * @returns File object.
32
+ * @throws If MIME type can't be determined.
33
+ **/
34
+ export declare function bufferToFile(buff: ArrayBuffer, fileName?: string): Promise<File>;
35
+ /**
36
+ * Converts a File object to a base64 data URL.
37
+ *
38
+ * @param file - The File to convert.
39
+ * @returns Base64 data URL.
40
+ **/
41
+ export declare function fileToBase64(file: File): Promise<string | ArrayBuffer | null>;
@@ -0,0 +1,87 @@
1
+ import { fileTypeFromBuffer } from "file-type";
2
+ /**
3
+ * Converts bytes into a raw base64 string.
4
+ * Doesn't include a data URL prefix.
5
+ *
6
+ * @param bytes - The data to convert.
7
+ * @returns Raw base64 data string.
8
+ **/
9
+ export function bytesToBase64(bytes) {
10
+ const len = bytes.byteLength;
11
+ let binary = "";
12
+ for (let i = 0; i < len; i++)
13
+ binary += String.fromCharCode(bytes[i]);
14
+ return btoa(binary);
15
+ }
16
+ /**
17
+ * Converts raw base64 string into an array of bytes.
18
+ *
19
+ * @param data - The data to convert.
20
+ * @returns Array of bytes.
21
+ **/
22
+ export function base64ToBytes(data) {
23
+ const binary = atob(data);
24
+ const len = binary.length;
25
+ const bytes = new Uint8Array(len);
26
+ for (let i = 0; i < len; i++)
27
+ bytes[i] = binary.charCodeAt(i);
28
+ return bytes;
29
+ }
30
+ /**
31
+ * Converts a raw base64 string or data URL into a File object.
32
+ * If no MIME type is found, defaults to 'application/octet-stream'.
33
+ *
34
+ * @param data - The base64 data to convert.
35
+ * @param fileName - Name for the file (default = 'file')
36
+ * @returns File object.
37
+ **/
38
+ export async function base64ToFile(data, fileName = "file") {
39
+ const matches = data.match(/^data:(.*?);base64,(.*)$/);
40
+ let mime;
41
+ let base64Data;
42
+ if (matches) {
43
+ mime = matches[1];
44
+ base64Data = matches[2];
45
+ }
46
+ else {
47
+ base64Data = data;
48
+ }
49
+ const byteArray = Uint8Array.from(atob(base64Data), c => c.charCodeAt(0));
50
+ return new File([byteArray], fileName, {
51
+ type: mime || "application/octet-stream",
52
+ lastModified: Date.now()
53
+ });
54
+ }
55
+ /**
56
+ * Converts a buffer into a File object using MIME type detection.
57
+ * Infers file extension from binary content.
58
+ *
59
+ * @param buff - The data to convert.
60
+ * @param fileName - Name for the file (default = 'file')
61
+ * @returns File object.
62
+ * @throws If MIME type can't be determined.
63
+ **/
64
+ export async function bufferToFile(buff, fileName = "file") {
65
+ const type = await fileTypeFromBuffer(buff);
66
+ if (!type)
67
+ throw new Error("Couldn't get file type from buffer.");
68
+ return new File([buff], `${fileName}.${type.ext}`, {
69
+ type: type.mime,
70
+ lastModified: Date.now()
71
+ });
72
+ }
73
+ /**
74
+ * Converts a File object to a base64 data URL.
75
+ *
76
+ * @param file - The File to convert.
77
+ * @returns Base64 data URL.
78
+ **/
79
+ export function fileToBase64(file) {
80
+ return new Promise((resolve, reject) => {
81
+ const reader = new FileReader();
82
+ reader.onload = () => resolve(reader.result);
83
+ reader.onerror = () => reject(reader.error);
84
+ reader.readAsDataURL(file);
85
+ });
86
+ }
87
+ //
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Downloads a remote asset as a file in the browser.
3
+ * Attempts to infer file name from `Content-Disposition` header.
4
+ *
5
+ * @param url - Remote file URL to download.
6
+ * @returns Raw base64 data string.
7
+ **/
8
+ export declare function downloadAsset(url: string): Promise<void>;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Downloads a remote asset as a file in the browser.
3
+ * Attempts to infer file name from `Content-Disposition` header.
4
+ *
5
+ * @param url - Remote file URL to download.
6
+ * @returns Raw base64 data string.
7
+ **/
8
+ export async function downloadAsset(url) {
9
+ const res = await fetch(url);
10
+ if (!res.ok)
11
+ throw new Error(`Failed to fetch asset: ${res.status} ${res.statusText}`);
12
+ const blob = await res.blob();
13
+ const downloadUrl = URL.createObjectURL(blob);
14
+ const cd = res.headers.get("content-disposition");
15
+ const match = cd?.match(/filename\*?=(?:UTF-8'')?["']?([^"';\n]+)["']?/i);
16
+ const fileName = match?.[1]
17
+ ? decodeURIComponent(match[1])
18
+ : "file";
19
+ const link = document.createElement("a");
20
+ link.href = downloadUrl;
21
+ link.download = fileName;
22
+ document.body.appendChild(link);
23
+ link.click();
24
+ document.body.removeChild(link);
25
+ URL.revokeObjectURL(downloadUrl);
26
+ }
27
+ //
@@ -0,0 +1,8 @@
1
+ /**
2
+ * File-based utilities for browser separated into:
3
+ *
4
+ * - Data conversion.
5
+ * - Remote downloading.
6
+ **/
7
+ export * from "./convert.js";
8
+ export * from "./download.js";
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * File-based utilities for browser separated into:
3
+ *
4
+ * - Data conversion.
5
+ * - Remote downloading.
6
+ **/
7
+ export * from "./convert.js";
8
+ export * from "./download.js";
9
+ //
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+
3
+ "name": "@file-cabinet/web",
4
+
5
+ "version": "0.0.1",
6
+
7
+ "main": "./dist/index.js",
8
+
9
+ "types": "./dist/index.d.ts",
10
+
11
+ "type": "module",
12
+
13
+ "license": "MIT",
14
+
15
+ "files": [
16
+
17
+ "dist"
18
+
19
+ ],
20
+
21
+ "publishConfig": {
22
+
23
+ "access": "public"
24
+
25
+ },
26
+
27
+ "author": "Bouk",
28
+
29
+ "description": "Helper package for browser.",
30
+
31
+ "scripts": {
32
+
33
+ "build": "tsc"
34
+
35
+ },
36
+
37
+ "dependencies": {
38
+
39
+ "@bouko/ts": "^0.4.9",
40
+
41
+ "clsx": "^2.1.1",
42
+
43
+ "file-type": "^22.0.1",
44
+
45
+ "tailwind-merge": "^3.6.0"
46
+
47
+ },
48
+
49
+ "devDependencies": {
50
+
51
+ "typescript": "^5.9.2"
52
+
53
+ }
54
+
55
+ }
56
+