@uploadista/client-browser 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.
- package/.turbo/turbo-build.log +5 -0
- package/.turbo/turbo-check.log +130 -0
- package/AUTO_CAPABILITIES.md +98 -0
- package/FRAMEWORK_INTEGRATION.md +407 -0
- package/LICENSE +21 -0
- package/README.md +795 -0
- package/SMART_CHUNKING.md +140 -0
- package/dist/client/create-uploadista-client.d.ts +182 -0
- package/dist/client/create-uploadista-client.d.ts.map +1 -0
- package/dist/client/create-uploadista-client.js +76 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +1 -0
- package/dist/framework-utils.d.ts +201 -0
- package/dist/framework-utils.d.ts.map +1 -0
- package/dist/framework-utils.js +282 -0
- package/dist/http-client.d.ts +44 -0
- package/dist/http-client.d.ts.map +1 -0
- package/dist/http-client.js +489 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/services/abort-controller-factory.d.ts +30 -0
- package/dist/services/abort-controller-factory.d.ts.map +1 -0
- package/dist/services/abort-controller-factory.js +98 -0
- package/dist/services/checksum-service.d.ts +30 -0
- package/dist/services/checksum-service.d.ts.map +1 -0
- package/dist/services/checksum-service.js +44 -0
- package/dist/services/create-browser-services.d.ts +36 -0
- package/dist/services/create-browser-services.d.ts.map +1 -0
- package/dist/services/create-browser-services.js +56 -0
- package/dist/services/file-reader.d.ts +91 -0
- package/dist/services/file-reader.d.ts.map +1 -0
- package/dist/services/file-reader.js +251 -0
- package/dist/services/fingerprint-service.d.ts +41 -0
- package/dist/services/fingerprint-service.d.ts.map +1 -0
- package/dist/services/fingerprint-service.js +64 -0
- package/dist/services/id-generation/id-generation.d.ts +40 -0
- package/dist/services/id-generation/id-generation.d.ts.map +1 -0
- package/dist/services/id-generation/id-generation.js +58 -0
- package/dist/services/platform-service.d.ts +38 -0
- package/dist/services/platform-service.d.ts.map +1 -0
- package/dist/services/platform-service.js +221 -0
- package/dist/services/storage/local-storage-service.d.ts +55 -0
- package/dist/services/storage/local-storage-service.d.ts.map +1 -0
- package/dist/services/storage/local-storage-service.js +178 -0
- package/dist/services/storage/session-storage-service.d.ts +55 -0
- package/dist/services/storage/session-storage-service.d.ts.map +1 -0
- package/dist/services/storage/session-storage-service.js +179 -0
- package/dist/services/websocket-factory.d.ts +46 -0
- package/dist/services/websocket-factory.d.ts.map +1 -0
- package/dist/services/websocket-factory.js +196 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/upload-input.d.ts +26 -0
- package/dist/types/upload-input.d.ts.map +1 -0
- package/dist/types/upload-input.js +1 -0
- package/dist/utils/hash-util.d.ts +60 -0
- package/dist/utils/hash-util.d.ts.map +1 -0
- package/dist/utils/hash-util.js +75 -0
- package/package.json +32 -0
- package/src/client/create-uploadista-client.ts +150 -0
- package/src/client/index.ts +1 -0
- package/src/framework-utils.ts +446 -0
- package/src/http-client.ts +546 -0
- package/src/index.ts +8 -0
- package/src/services/abort-controller-factory.ts +108 -0
- package/src/services/checksum-service.ts +46 -0
- package/src/services/create-browser-services.ts +81 -0
- package/src/services/file-reader.ts +344 -0
- package/src/services/fingerprint-service.ts +67 -0
- package/src/services/id-generation/id-generation.ts +60 -0
- package/src/services/platform-service.ts +231 -0
- package/src/services/storage/local-storage-service.ts +187 -0
- package/src/services/storage/session-storage-service.ts +188 -0
- package/src/services/websocket-factory.ts +212 -0
- package/src/types/index.ts +1 -0
- package/src/types/upload-input.ts +25 -0
- package/src/utils/hash-util.ts +79 -0
- package/tsconfig.json +22 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/vitest.config.ts +15 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a browser-specific ID generation service using the Web Crypto API.
|
|
3
|
+
*
|
|
4
|
+
* This service generates cryptographically secure random UUIDs (v4) using the
|
|
5
|
+
* browser's native `crypto.randomUUID()` method. These UUIDs are used throughout
|
|
6
|
+
* the Uploadista client for:
|
|
7
|
+
* - Upload session identifiers
|
|
8
|
+
* - Chunk identifiers
|
|
9
|
+
* - Request correlation IDs
|
|
10
|
+
* - Internal tracking
|
|
11
|
+
*
|
|
12
|
+
* The generated UUIDs conform to RFC 4122 version 4 (random) and provide
|
|
13
|
+
* strong uniqueness guarantees suitable for distributed systems.
|
|
14
|
+
*
|
|
15
|
+
* Browser compatibility: Requires support for `crypto.randomUUID()` (available
|
|
16
|
+
* in modern browsers). If you need to support older browsers, consider using a
|
|
17
|
+
* polyfill.
|
|
18
|
+
*
|
|
19
|
+
* @returns An IdGenerationService that generates cryptographically secure UUIDs
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { createBrowserIdGenerationService } from '@uploadista/client-browser';
|
|
24
|
+
*
|
|
25
|
+
* const idService = createBrowserIdGenerationService();
|
|
26
|
+
*
|
|
27
|
+
* // Generate a unique ID
|
|
28
|
+
* const id = idService.generate();
|
|
29
|
+
* console.log('Generated ID:', id);
|
|
30
|
+
* // Output: "550e8400-e29b-41d4-a716-446655440000" (example UUID v4)
|
|
31
|
+
*
|
|
32
|
+
* // Each call generates a new unique ID
|
|
33
|
+
* const id2 = idService.generate();
|
|
34
|
+
* console.log('Another ID:', id2);
|
|
35
|
+
* // Output: "7c9e6679-7425-40de-944b-e07fc1f90ae7" (different UUID)
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function createBrowserIdGenerationService() {
|
|
39
|
+
return {
|
|
40
|
+
/**
|
|
41
|
+
* Generates a cryptographically secure random UUID (v4).
|
|
42
|
+
*
|
|
43
|
+
* Uses the Web Crypto API's `crypto.randomUUID()` method to generate
|
|
44
|
+
* a UUID conforming to RFC 4122 version 4. Each UUID is statistically
|
|
45
|
+
* unique and suitable for use as an identifier in distributed systems.
|
|
46
|
+
*
|
|
47
|
+
* @returns A UUID v4 string in the format "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* const service = createBrowserIdGenerationService();
|
|
52
|
+
* const uploadId = service.generate();
|
|
53
|
+
* console.log(uploadId); // "f47ac10b-58cc-4372-a567-0e02b2c3d479"
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
generate: () => crypto.randomUUID(),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { PlatformService } from "@uploadista/client-core";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a browser-specific platform service that provides environment capabilities.
|
|
4
|
+
*
|
|
5
|
+
* This service abstracts platform-specific functionality and provides a consistent
|
|
6
|
+
* interface for the Uploadista client to interact with browser APIs. It handles:
|
|
7
|
+
* - Timer management (setTimeout/clearTimeout)
|
|
8
|
+
* - Environment detection (browser vs. Node.js)
|
|
9
|
+
* - Network connectivity status
|
|
10
|
+
* - File object detection and metadata extraction
|
|
11
|
+
*
|
|
12
|
+
* This abstraction allows the core upload logic to remain platform-agnostic while
|
|
13
|
+
* still accessing browser-specific features when needed.
|
|
14
|
+
*
|
|
15
|
+
* @returns A PlatformService configured for browser environments
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { createBrowserPlatformService } from '@uploadista/client-browser';
|
|
20
|
+
*
|
|
21
|
+
* const platform = createBrowserPlatformService();
|
|
22
|
+
*
|
|
23
|
+
* // Check if running in browser
|
|
24
|
+
* console.log('Is browser:', platform.isBrowser()); // true
|
|
25
|
+
*
|
|
26
|
+
* // Check network status
|
|
27
|
+
* console.log('Is online:', platform.isOnline()); // true or false
|
|
28
|
+
*
|
|
29
|
+
* // Extract file metadata
|
|
30
|
+
* const fileInput = document.querySelector('input[type="file"]');
|
|
31
|
+
* const file = fileInput.files[0];
|
|
32
|
+
* console.log('File name:', platform.getFileName(file));
|
|
33
|
+
* console.log('File type:', platform.getFileType(file));
|
|
34
|
+
* console.log('File size:', platform.getFileSize(file));
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare function createBrowserPlatformService(): PlatformService;
|
|
38
|
+
//# sourceMappingURL=platform-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platform-service.d.ts","sourceRoot":"","sources":["../../src/services/platform-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAW,MAAM,yBAAyB,CAAC;AAExE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,4BAA4B,IAAI,eAAe,CAiM9D"}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a browser-specific platform service that provides environment capabilities.
|
|
3
|
+
*
|
|
4
|
+
* This service abstracts platform-specific functionality and provides a consistent
|
|
5
|
+
* interface for the Uploadista client to interact with browser APIs. It handles:
|
|
6
|
+
* - Timer management (setTimeout/clearTimeout)
|
|
7
|
+
* - Environment detection (browser vs. Node.js)
|
|
8
|
+
* - Network connectivity status
|
|
9
|
+
* - File object detection and metadata extraction
|
|
10
|
+
*
|
|
11
|
+
* This abstraction allows the core upload logic to remain platform-agnostic while
|
|
12
|
+
* still accessing browser-specific features when needed.
|
|
13
|
+
*
|
|
14
|
+
* @returns A PlatformService configured for browser environments
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { createBrowserPlatformService } from '@uploadista/client-browser';
|
|
19
|
+
*
|
|
20
|
+
* const platform = createBrowserPlatformService();
|
|
21
|
+
*
|
|
22
|
+
* // Check if running in browser
|
|
23
|
+
* console.log('Is browser:', platform.isBrowser()); // true
|
|
24
|
+
*
|
|
25
|
+
* // Check network status
|
|
26
|
+
* console.log('Is online:', platform.isOnline()); // true or false
|
|
27
|
+
*
|
|
28
|
+
* // Extract file metadata
|
|
29
|
+
* const fileInput = document.querySelector('input[type="file"]');
|
|
30
|
+
* const file = fileInput.files[0];
|
|
31
|
+
* console.log('File name:', platform.getFileName(file));
|
|
32
|
+
* console.log('File type:', platform.getFileType(file));
|
|
33
|
+
* console.log('File size:', platform.getFileSize(file));
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function createBrowserPlatformService() {
|
|
37
|
+
return {
|
|
38
|
+
/**
|
|
39
|
+
* Schedules a function to be executed after a specified delay.
|
|
40
|
+
*
|
|
41
|
+
* Wraps the browser's native `setTimeout` function.
|
|
42
|
+
*
|
|
43
|
+
* @param callback - Function to execute after the delay
|
|
44
|
+
* @param ms - Delay in milliseconds before executing the callback
|
|
45
|
+
* @returns A timeout ID that can be passed to clearTimeout
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const timeoutId = platform.setTimeout(() => {
|
|
50
|
+
* console.log('Executed after 1 second');
|
|
51
|
+
* }, 1000);
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
setTimeout: (callback, ms) => {
|
|
55
|
+
return globalThis.setTimeout(callback, ms);
|
|
56
|
+
},
|
|
57
|
+
/**
|
|
58
|
+
* Cancels a timeout previously scheduled with setTimeout.
|
|
59
|
+
*
|
|
60
|
+
* Wraps the browser's native `clearTimeout` function.
|
|
61
|
+
*
|
|
62
|
+
* @param id - The timeout ID returned by setTimeout
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const timeoutId = platform.setTimeout(() => { }, 1000);
|
|
67
|
+
* platform.clearTimeout(timeoutId); // Cancel the timeout
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
clearTimeout: (id) => {
|
|
71
|
+
globalThis.clearTimeout(id);
|
|
72
|
+
},
|
|
73
|
+
/**
|
|
74
|
+
* Checks if the code is running in a browser environment.
|
|
75
|
+
*
|
|
76
|
+
* Detects browser by checking for the existence of the `window` object.
|
|
77
|
+
* This is useful for conditional logic that should only run in browsers.
|
|
78
|
+
*
|
|
79
|
+
* @returns `true` if running in a browser, `false` otherwise
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* if (platform.isBrowser()) {
|
|
84
|
+
* // Browser-specific code
|
|
85
|
+
* console.log('Running in browser');
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
isBrowser: () => {
|
|
90
|
+
return typeof window !== "undefined";
|
|
91
|
+
},
|
|
92
|
+
/**
|
|
93
|
+
* Checks if the browser is currently online.
|
|
94
|
+
*
|
|
95
|
+
* Uses the Navigator Online Status API (`navigator.onLine`) to determine
|
|
96
|
+
* network connectivity. Note that this only indicates if the device has
|
|
97
|
+
* a network connection, not if it can reach the internet.
|
|
98
|
+
*
|
|
99
|
+
* @returns `true` if online, `false` if offline, defaults to `true` if not in browser
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```typescript
|
|
103
|
+
* if (platform.isOnline()) {
|
|
104
|
+
* // Proceed with upload
|
|
105
|
+
* await client.upload(file);
|
|
106
|
+
* } else {
|
|
107
|
+
* console.log('Waiting for network connection...');
|
|
108
|
+
* }
|
|
109
|
+
*
|
|
110
|
+
* // Listen for online/offline events
|
|
111
|
+
* window.addEventListener('online', () => {
|
|
112
|
+
* console.log('Back online, resuming upload');
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
isOnline: () => {
|
|
117
|
+
if (typeof navigator !== "undefined") {
|
|
118
|
+
return navigator.onLine;
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
},
|
|
122
|
+
/**
|
|
123
|
+
* Checks if a value is a File object.
|
|
124
|
+
*
|
|
125
|
+
* Type guard to determine if an unknown value is a browser File object.
|
|
126
|
+
* Useful for validating upload inputs and conditional file handling.
|
|
127
|
+
*
|
|
128
|
+
* @param value - The value to check
|
|
129
|
+
* @returns `true` if the value is a File instance, `false` otherwise
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const input = getUploadInput(); // unknown type
|
|
134
|
+
*
|
|
135
|
+
* if (platform.isFileLike(input)) {
|
|
136
|
+
* // TypeScript knows input is a File here
|
|
137
|
+
* console.log('Uploading file:', input.name);
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
isFileLike: (value) => {
|
|
142
|
+
return value instanceof File;
|
|
143
|
+
},
|
|
144
|
+
/**
|
|
145
|
+
* Extracts the file name from a File object.
|
|
146
|
+
*
|
|
147
|
+
* @param file - The file to extract the name from
|
|
148
|
+
* @returns The file name string, or `undefined` if not a File
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const file = new File(['content'], 'document.pdf');
|
|
153
|
+
* const name = platform.getFileName(file);
|
|
154
|
+
* console.log(name); // "document.pdf"
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
getFileName: (file) => {
|
|
158
|
+
if (file instanceof File) {
|
|
159
|
+
return file.name;
|
|
160
|
+
}
|
|
161
|
+
return undefined;
|
|
162
|
+
},
|
|
163
|
+
/**
|
|
164
|
+
* Extracts the MIME type from a File object.
|
|
165
|
+
*
|
|
166
|
+
* @param file - The file to extract the type from
|
|
167
|
+
* @returns The MIME type string, or `undefined` if not a File
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* const file = new File(['content'], 'image.png', { type: 'image/png' });
|
|
172
|
+
* const type = platform.getFileType(file);
|
|
173
|
+
* console.log(type); // "image/png"
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
getFileType: (file) => {
|
|
177
|
+
if (file instanceof File) {
|
|
178
|
+
return file.type;
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
},
|
|
182
|
+
/**
|
|
183
|
+
* Extracts the file size in bytes from a File object.
|
|
184
|
+
*
|
|
185
|
+
* @param file - The file to extract the size from
|
|
186
|
+
* @returns The file size in bytes, or `undefined` if not a File
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const file = new File(['Hello'], 'greeting.txt');
|
|
191
|
+
* const size = platform.getFileSize(file);
|
|
192
|
+
* console.log(size); // 5 (bytes)
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
getFileSize: (file) => {
|
|
196
|
+
if (file instanceof File) {
|
|
197
|
+
return file.size;
|
|
198
|
+
}
|
|
199
|
+
return undefined;
|
|
200
|
+
},
|
|
201
|
+
/**
|
|
202
|
+
* Extracts the last modified timestamp from a File object.
|
|
203
|
+
*
|
|
204
|
+
* @param file - The file to extract the timestamp from
|
|
205
|
+
* @returns The last modified timestamp in milliseconds since epoch, or `undefined` if not a File
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* const file = new File(['content'], 'data.txt');
|
|
210
|
+
* const lastModified = platform.getFileLastModified(file);
|
|
211
|
+
* console.log(new Date(lastModified)); // Date object of when file was last modified
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
getFileLastModified: (file) => {
|
|
215
|
+
if (file instanceof File) {
|
|
216
|
+
return file.lastModified;
|
|
217
|
+
}
|
|
218
|
+
return undefined;
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { StorageService } from "@uploadista/client-core";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a browser storage service using localStorage for persistent data storage.
|
|
4
|
+
*
|
|
5
|
+
* This service provides a key-value storage interface backed by the browser's
|
|
6
|
+
* localStorage API. Data stored with this service persists across browser sessions
|
|
7
|
+
* and page reloads until explicitly deleted or cleared by the user.
|
|
8
|
+
*
|
|
9
|
+
* Use cases include:
|
|
10
|
+
* - Persisting upload state for resumable uploads
|
|
11
|
+
* - Caching file metadata and fingerprints
|
|
12
|
+
* - Storing user preferences and settings
|
|
13
|
+
* - Maintaining upload history
|
|
14
|
+
*
|
|
15
|
+
* **Important notes:**
|
|
16
|
+
* - localStorage has a typical limit of 5-10MB per origin
|
|
17
|
+
* - Data is stored as strings (objects are JSON-serialized)
|
|
18
|
+
* - localStorage is synchronous but this service wraps it in Promises for consistency
|
|
19
|
+
* - Data is scoped to the origin (protocol + domain + port)
|
|
20
|
+
* - Users can clear localStorage through browser settings
|
|
21
|
+
*
|
|
22
|
+
* @returns A StorageService backed by browser localStorage
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { createLocalStorageService } from '@uploadista/client-browser';
|
|
27
|
+
*
|
|
28
|
+
* const storage = createLocalStorageService();
|
|
29
|
+
*
|
|
30
|
+
* // Store upload state
|
|
31
|
+
* await storage.setItem('upload:123', JSON.stringify({
|
|
32
|
+
* fileId: '123',
|
|
33
|
+
* progress: 75,
|
|
34
|
+
* uploadedChunks: [0, 1, 2]
|
|
35
|
+
* }));
|
|
36
|
+
*
|
|
37
|
+
* // Retrieve upload state
|
|
38
|
+
* const data = await storage.getItem('upload:123');
|
|
39
|
+
* if (data) {
|
|
40
|
+
* const state = JSON.parse(data);
|
|
41
|
+
* console.log('Resuming upload at', state.progress, '%');
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* // Find all uploads
|
|
45
|
+
* const uploads = await storage.find('upload:');
|
|
46
|
+
* console.log('Found', Object.keys(uploads).length, 'uploads');
|
|
47
|
+
*
|
|
48
|
+
* // Clean up completed upload
|
|
49
|
+
* await storage.removeItem('upload:123');
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @see {@link createSessionStorageService} for session-only storage
|
|
53
|
+
*/
|
|
54
|
+
export declare function createLocalStorageService(): StorageService;
|
|
55
|
+
//# sourceMappingURL=local-storage-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-storage-service.d.ts","sourceRoot":"","sources":["../../../src/services/storage/local-storage-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,wBAAgB,yBAAyB,IAAI,cAAc,CAoI1D"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a browser storage service using localStorage for persistent data storage.
|
|
3
|
+
*
|
|
4
|
+
* This service provides a key-value storage interface backed by the browser's
|
|
5
|
+
* localStorage API. Data stored with this service persists across browser sessions
|
|
6
|
+
* and page reloads until explicitly deleted or cleared by the user.
|
|
7
|
+
*
|
|
8
|
+
* Use cases include:
|
|
9
|
+
* - Persisting upload state for resumable uploads
|
|
10
|
+
* - Caching file metadata and fingerprints
|
|
11
|
+
* - Storing user preferences and settings
|
|
12
|
+
* - Maintaining upload history
|
|
13
|
+
*
|
|
14
|
+
* **Important notes:**
|
|
15
|
+
* - localStorage has a typical limit of 5-10MB per origin
|
|
16
|
+
* - Data is stored as strings (objects are JSON-serialized)
|
|
17
|
+
* - localStorage is synchronous but this service wraps it in Promises for consistency
|
|
18
|
+
* - Data is scoped to the origin (protocol + domain + port)
|
|
19
|
+
* - Users can clear localStorage through browser settings
|
|
20
|
+
*
|
|
21
|
+
* @returns A StorageService backed by browser localStorage
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import { createLocalStorageService } from '@uploadista/client-browser';
|
|
26
|
+
*
|
|
27
|
+
* const storage = createLocalStorageService();
|
|
28
|
+
*
|
|
29
|
+
* // Store upload state
|
|
30
|
+
* await storage.setItem('upload:123', JSON.stringify({
|
|
31
|
+
* fileId: '123',
|
|
32
|
+
* progress: 75,
|
|
33
|
+
* uploadedChunks: [0, 1, 2]
|
|
34
|
+
* }));
|
|
35
|
+
*
|
|
36
|
+
* // Retrieve upload state
|
|
37
|
+
* const data = await storage.getItem('upload:123');
|
|
38
|
+
* if (data) {
|
|
39
|
+
* const state = JSON.parse(data);
|
|
40
|
+
* console.log('Resuming upload at', state.progress, '%');
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* // Find all uploads
|
|
44
|
+
* const uploads = await storage.find('upload:');
|
|
45
|
+
* console.log('Found', Object.keys(uploads).length, 'uploads');
|
|
46
|
+
*
|
|
47
|
+
* // Clean up completed upload
|
|
48
|
+
* await storage.removeItem('upload:123');
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @see {@link createSessionStorageService} for session-only storage
|
|
52
|
+
*/
|
|
53
|
+
export function createLocalStorageService() {
|
|
54
|
+
/**
|
|
55
|
+
* Internal helper to find entries matching a prefix.
|
|
56
|
+
*
|
|
57
|
+
* Iterates through all localStorage keys and returns those that start
|
|
58
|
+
* with the specified prefix along with their values.
|
|
59
|
+
*
|
|
60
|
+
* @param prefix - Key prefix to filter by
|
|
61
|
+
* @returns Object mapping matching keys to their values
|
|
62
|
+
* @private
|
|
63
|
+
*/
|
|
64
|
+
const findEntries = (prefix) => {
|
|
65
|
+
const results = {};
|
|
66
|
+
for (const key in localStorage) {
|
|
67
|
+
if (key.startsWith(prefix)) {
|
|
68
|
+
const item = localStorage.getItem(key);
|
|
69
|
+
if (item) {
|
|
70
|
+
results[key] = item;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return results;
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
/**
|
|
78
|
+
* Retrieves a value from localStorage by key.
|
|
79
|
+
*
|
|
80
|
+
* @param key - The key to retrieve
|
|
81
|
+
* @returns Promise resolving to the value, or null if the key doesn't exist
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const value = await storage.getItem('user:preferences');
|
|
86
|
+
* if (value) {
|
|
87
|
+
* const prefs = JSON.parse(value);
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
async getItem(key) {
|
|
92
|
+
return localStorage.getItem(key);
|
|
93
|
+
},
|
|
94
|
+
/**
|
|
95
|
+
* Stores a value in localStorage.
|
|
96
|
+
*
|
|
97
|
+
* If the key already exists, its value will be overwritten.
|
|
98
|
+
* Values must be strings; use JSON.stringify() for objects.
|
|
99
|
+
*
|
|
100
|
+
* @param key - The key to store under
|
|
101
|
+
* @param value - The string value to store
|
|
102
|
+
*
|
|
103
|
+
* @throws {QuotaExceededError} If localStorage quota is exceeded
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* // Store string
|
|
108
|
+
* await storage.setItem('upload:status', 'completed');
|
|
109
|
+
*
|
|
110
|
+
* // Store object
|
|
111
|
+
* await storage.setItem('upload:metadata', JSON.stringify({
|
|
112
|
+
* name: 'file.txt',
|
|
113
|
+
* size: 1024
|
|
114
|
+
* }));
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
async setItem(key, value) {
|
|
118
|
+
localStorage.setItem(key, value);
|
|
119
|
+
},
|
|
120
|
+
/**
|
|
121
|
+
* Removes a value from localStorage by key.
|
|
122
|
+
*
|
|
123
|
+
* If the key doesn't exist, this is a no-op (no error is thrown).
|
|
124
|
+
*
|
|
125
|
+
* @param key - The key to remove
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* // Clean up completed upload
|
|
130
|
+
* await storage.removeItem('upload:123');
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
async removeItem(key) {
|
|
134
|
+
localStorage.removeItem(key);
|
|
135
|
+
},
|
|
136
|
+
/**
|
|
137
|
+
* Retrieves all entries from localStorage.
|
|
138
|
+
*
|
|
139
|
+
* Returns an object mapping every key in localStorage to its value.
|
|
140
|
+
* Use with caution as this can return a large amount of data.
|
|
141
|
+
*
|
|
142
|
+
* @returns Promise resolving to object with all key-value pairs
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const all = await storage.findAll();
|
|
147
|
+
* console.log('Total items in storage:', Object.keys(all).length);
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
async findAll() {
|
|
151
|
+
return findEntries("");
|
|
152
|
+
},
|
|
153
|
+
/**
|
|
154
|
+
* Finds all entries with keys starting with a given prefix.
|
|
155
|
+
*
|
|
156
|
+
* Useful for querying related data or implementing namespacing.
|
|
157
|
+
* For example, use "upload:" prefix to find all upload-related entries.
|
|
158
|
+
*
|
|
159
|
+
* @param prefix - The key prefix to search for
|
|
160
|
+
* @returns Promise resolving to object with matching key-value pairs
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* // Find all uploads
|
|
165
|
+
* const uploads = await storage.find('upload:');
|
|
166
|
+
* for (const [key, value] of Object.entries(uploads)) {
|
|
167
|
+
* console.log('Upload:', key, JSON.parse(value));
|
|
168
|
+
* }
|
|
169
|
+
*
|
|
170
|
+
* // Find user preferences
|
|
171
|
+
* const prefs = await storage.find('pref:');
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
async find(prefix) {
|
|
175
|
+
return findEntries(prefix);
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { StorageService } from "@uploadista/client-core";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a browser storage service using sessionStorage for temporary data storage.
|
|
4
|
+
*
|
|
5
|
+
* This service provides a key-value storage interface backed by the browser's
|
|
6
|
+
* sessionStorage API. Unlike localStorage, data stored with sessionStorage is
|
|
7
|
+
* only available for the duration of the page session and is cleared when the
|
|
8
|
+
* browser tab or window is closed.
|
|
9
|
+
*
|
|
10
|
+
* Use cases include:
|
|
11
|
+
* - Temporary upload state during a session
|
|
12
|
+
* - One-time authentication tokens
|
|
13
|
+
* - Transient UI state
|
|
14
|
+
* - Session-specific preferences
|
|
15
|
+
*
|
|
16
|
+
* **Key differences from localStorage:**
|
|
17
|
+
* - Data persists only for the current browser tab/window session
|
|
18
|
+
* - Each tab has its own isolated sessionStorage
|
|
19
|
+
* - Data is cleared when the tab/window is closed
|
|
20
|
+
* - Page reloads preserve sessionStorage (unlike in-memory storage)
|
|
21
|
+
* - Same storage quota limits as localStorage (5-10MB)
|
|
22
|
+
*
|
|
23
|
+
* @returns A StorageService backed by browser sessionStorage
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { createSessionStorageService } from '@uploadista/client-browser';
|
|
28
|
+
*
|
|
29
|
+
* const storage = createSessionStorageService();
|
|
30
|
+
*
|
|
31
|
+
* // Store temporary upload state
|
|
32
|
+
* await storage.setItem('temp-upload:abc', JSON.stringify({
|
|
33
|
+
* fileId: 'abc',
|
|
34
|
+
* started: Date.now()
|
|
35
|
+
* }));
|
|
36
|
+
*
|
|
37
|
+
* // Retrieve within same session
|
|
38
|
+
* const data = await storage.getItem('temp-upload:abc');
|
|
39
|
+
* if (data) {
|
|
40
|
+
* const state = JSON.parse(data);
|
|
41
|
+
* console.log('Upload started at', new Date(state.started));
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* // Find all temporary uploads
|
|
45
|
+
* const tempUploads = await storage.find('temp-upload:');
|
|
46
|
+
*
|
|
47
|
+
* // Clean up
|
|
48
|
+
* await storage.removeItem('temp-upload:abc');
|
|
49
|
+
* // OR: Close tab/window to clear all sessionStorage
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @see {@link createLocalStorageService} for persistent storage
|
|
53
|
+
*/
|
|
54
|
+
export declare function createSessionStorageService(): StorageService;
|
|
55
|
+
//# sourceMappingURL=session-storage-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-storage-service.d.ts","sourceRoot":"","sources":["../../../src/services/storage/session-storage-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,wBAAgB,2BAA2B,IAAI,cAAc,CAqI5D"}
|