@nuvix/storage 0.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/README.md ADDED
@@ -0,0 +1,304 @@
1
+ # @nuvix/storage
2
+
3
+ A powerful, TypeScript-first S3-compatible storage library for the Nuvix BaaS platform. Supports multiple storage backends including local filesystem, AWS S3, and Wasabi with a unified API.
4
+
5
+ ## โœจ Features
6
+
7
+ - ๐Ÿš€ **S3-Compatible API** - Consistent interface across all storage providers
8
+ - ๐Ÿ”Œ **Multiple Storage Backends** - Local, AWS S3, Wasabi, and more
9
+ - ๐Ÿ“ **TypeScript First** - Full type safety with comprehensive TypeScript definitions
10
+ - โœ… **File Validation** - Built-in validators for file types, sizes, names, and extensions
11
+ - ๐Ÿ”„ **Chunked Uploads** - Support for large file uploads with multipart upload
12
+ - ๐ŸŒ **Modern Builds** - ESM and CommonJS support for maximum compatibility
13
+ - ๐Ÿงช **Well Tested** - Comprehensive test suite with real implementation testing
14
+
15
+ ## ๐Ÿ“ฆ Installation
16
+
17
+ ```bash
18
+ # Using npm
19
+ npm install @nuvix/storage
20
+
21
+ # Using yarn
22
+ yarn add @nuvix/storage
23
+
24
+ # Using pnpm
25
+ pnpm add @nuvix/storage
26
+
27
+ # Using bun
28
+ bun add @nuvix/storage
29
+ ```
30
+
31
+ ## ๐Ÿš€ Quick Start
32
+
33
+ ### ESM (Modern)
34
+ ```typescript
35
+ import { Storage, Local, Wasabi, FileExt, FileSize } from '@nuvix/storage';
36
+
37
+ // Set up local storage
38
+ const localStorage = new Local('./uploads');
39
+ Storage.setDevice(Storage.DEVICE_LOCAL, localStorage);
40
+
41
+ // Upload a file
42
+ const localDevice = Storage.getDevice(Storage.DEVICE_LOCAL);
43
+ await localDevice.write('test.txt', 'Hello, World!', 'text/plain');
44
+ ```
45
+
46
+ ### CommonJS (Legacy)
47
+ ```javascript
48
+ const { Storage, Local, Wasabi } = require('@nuvix/storage');
49
+
50
+ // Set up local storage
51
+ const localStorage = new Local('./uploads');
52
+ Storage.setDevice(Storage.DEVICE_LOCAL, localStorage);
53
+ ```
54
+
55
+ ## ๐Ÿ”ง Storage Devices
56
+
57
+ ### Local File System
58
+ ```typescript
59
+ import { Local, Storage } from '@nuvix/storage';
60
+
61
+ const localStorage = new Local('./uploads');
62
+ Storage.setDevice('local', localStorage);
63
+
64
+ // Write file
65
+ await localStorage.write('documents/file.txt', 'content', 'text/plain');
66
+
67
+ // Read file
68
+ const content = await localStorage.read('documents/file.txt');
69
+
70
+ // Check if file exists
71
+ const exists = await localStorage.exists('documents/file.txt');
72
+ ```
73
+
74
+ ### Wasabi Cloud Storage
75
+ ```typescript
76
+ import { Wasabi, Storage } from '@nuvix/storage';
77
+
78
+ const wasabiStorage = new Wasabi(
79
+ 'my-app-uploads', // root path
80
+ 'your-access-key', // access key
81
+ 'your-secret-key', // secret key
82
+ 'your-bucket-name', // bucket name
83
+ Wasabi.US_CENTRAL_1, // region
84
+ Wasabi.ACL_PRIVATE // ACL (optional)
85
+ );
86
+
87
+ Storage.setDevice('wasabi', wasabiStorage);
88
+
89
+ // Upload with metadata
90
+ await wasabiStorage.uploadData(
91
+ 'Hello, Cloud!',
92
+ 'cloud-file.txt',
93
+ 'text/plain',
94
+ 1, 1,
95
+ { author: 'user123' }
96
+ );
97
+ ```
98
+
99
+ ### AWS S3
100
+ ```typescript
101
+ import { S3, Storage } from '@nuvix/storage';
102
+
103
+ const s3Storage = new S3(
104
+ 'my-app-uploads', // root path
105
+ 'your-access-key', // access key
106
+ 'your-secret-key', // secret key
107
+ 'your-bucket-name', // bucket name
108
+ S3.US_EAST_1, // region
109
+ S3.ACL_PRIVATE // ACL (optional)
110
+ );
111
+
112
+ Storage.setDevice('s3', s3Storage);
113
+ ```
114
+
115
+ ## ๐Ÿ›ก๏ธ File Validation
116
+
117
+ ### File Extension Validation
118
+ ```typescript
119
+ import { FileExt } from '@nuvix/storage';
120
+
121
+ const imageValidator = new FileExt(['jpg', 'png', 'gif', 'webp']);
122
+
123
+ console.log(imageValidator.isValid('photo.jpg')); // true
124
+ console.log(imageValidator.isValid('document.pdf')); // false
125
+ ```
126
+
127
+ ### File Size Validation
128
+ ```typescript
129
+ import { FileSize } from '@nuvix/storage';
130
+
131
+ const sizeValidator = new FileSize(5 * 1024 * 1024); // 5MB limit
132
+
133
+ console.log(sizeValidator.isValid(1024 * 1024)); // true (1MB)
134
+ console.log(sizeValidator.isValid(10 * 1024 * 1024)); // false (10MB)
135
+ ```
136
+
137
+ ### File Name Validation
138
+ ```typescript
139
+ import { FileName } from '@nuvix/storage';
140
+
141
+ const nameValidator = new FileName();
142
+
143
+ console.log(nameValidator.isValid('validfile123.txt')); // true
144
+ console.log(nameValidator.isValid('invalid-file.txt')); // false (contains hyphen)
145
+ ```
146
+
147
+ ### File Type (MIME) Validation
148
+ ```typescript
149
+ import { FileType } from '@nuvix/storage';
150
+
151
+ const typeValidator = new FileType(['jpeg', 'png', 'gif']);
152
+
153
+ // Validates based on file content, not extension
154
+ console.log(await typeValidator.isValid('./image.jpg')); // true if actual JPEG
155
+ ```
156
+
157
+ ### Upload File Validation
158
+ ```typescript
159
+ import { Upload } from '@nuvix/storage';
160
+
161
+ const uploadValidator = new Upload();
162
+
163
+ console.log(await uploadValidator.isValid('./existing-file.txt')); // true
164
+ console.log(await uploadValidator.isValid('./non-existent.txt')); // false
165
+ ```
166
+
167
+ ## ๐Ÿ”„ Advanced Features
168
+
169
+ ### Chunked Uploads
170
+ ```typescript
171
+ // Upload large files in chunks
172
+ const chunks = ['chunk1', 'chunk2', 'chunk3'];
173
+ const metadata = {};
174
+
175
+ for (let i = 0; i < chunks.length; i++) {
176
+ await device.uploadData(
177
+ chunks[i],
178
+ 'large-file.txt',
179
+ 'text/plain',
180
+ i + 1, // current chunk
181
+ chunks.length, // total chunks
182
+ metadata
183
+ );
184
+ }
185
+ ```
186
+
187
+ ### File Transfer Between Devices
188
+ ```typescript
189
+ const sourceDevice = Storage.getDevice('local');
190
+ const targetDevice = Storage.getDevice('wasabi');
191
+
192
+ // Transfer file from local to cloud
193
+ await sourceDevice.transfer(
194
+ 'local-file.txt',
195
+ 'cloud-file.txt',
196
+ targetDevice
197
+ );
198
+ ```
199
+
200
+ ### Human-Readable File Sizes
201
+ ```typescript
202
+ import { Storage } from '@nuvix/storage';
203
+
204
+ console.log(Storage.human(1024)); // "1.00kB"
205
+ console.log(Storage.human(1048576)); // "1.00MB"
206
+ console.log(Storage.human(1024, 2, 'binary')); // "1.00KiB"
207
+ ```
208
+
209
+ ## ๐Ÿ—๏ธ Building
210
+
211
+ The library is built using Rollup and provides both ESM and CommonJS outputs:
212
+
213
+ ```bash
214
+ # Build for production
215
+ bun run build
216
+
217
+ # Build and watch for changes
218
+ bun run build:watch
219
+
220
+ # Clean build directory
221
+ bun run clean
222
+ ```
223
+
224
+ ## ๐Ÿงช Testing
225
+
226
+ Comprehensive test suite with real implementation testing:
227
+
228
+ ```bash
229
+ # Run all tests
230
+ bun test
231
+
232
+ # Run tests in watch mode
233
+ bun run test:watch
234
+
235
+ # Run tests with coverage
236
+ bun run test:coverage
237
+
238
+ # Run specific test files
239
+ bun test storage.test.ts
240
+ bun test device/
241
+ bun test validator/
242
+ ```
243
+
244
+ ### Testing with Real Cloud Storage
245
+
246
+ Set environment variables to test against real storage services:
247
+
248
+ ```bash
249
+ # For Wasabi testing
250
+ export WASABI_ACCESS_KEY="your-access-key"
251
+ export WASABI_SECRET_KEY="your-secret-key"
252
+ export WASABI_BUCKET="your-test-bucket"
253
+
254
+ # For AWS S3 testing
255
+ export AWS_ACCESS_KEY="your-access-key"
256
+ export AWS_SECRET_KEY="your-secret-key"
257
+ export AWS_BUCKET="your-test-bucket"
258
+ ```
259
+
260
+ ## ๐Ÿ“ API Reference
261
+
262
+ ### Storage Class
263
+ - `Storage.setDevice(name, device)` - Register a storage device
264
+ - `Storage.getDevice(name)` - Get a registered storage device
265
+ - `Storage.exists(name)` - Check if a device is registered
266
+ - `Storage.human(bytes, decimals?, system?)` - Format bytes to human-readable string
267
+
268
+ ### Device Interface
269
+ All storage devices implement the same interface:
270
+ - `upload(source, path, chunk?, chunks?, metadata?)` - Upload file
271
+ - `uploadData(data, path, contentType, chunk?, chunks?, metadata?)` - Upload data
272
+ - `read(path, offset?, length?)` - Read file content
273
+ - `write(path, data, contentType)` - Write file
274
+ - `delete(path, recursive?)` - Delete file/directory
275
+ - `exists(path)` - Check if file exists
276
+ - `getFileSize(path)` - Get file size
277
+ - `getFileMimeType(path)` - Get file MIME type
278
+ - `getFileHash(path)` - Get file MD5 hash
279
+ - `transfer(source, destination, targetDevice)` - Transfer file
280
+
281
+ ## ๐ŸŒ Browser Support
282
+
283
+ This library is designed for Node.js environments. For browser usage, you'll need to provide polyfills for Node.js modules or use a bundler that can handle Node.js dependencies.
284
+
285
+ ## ๐Ÿค Contributing
286
+
287
+ Contributions are welcome! Please read our contributing guidelines and ensure all tests pass:
288
+
289
+ ```bash
290
+ bun run lint # Check code style
291
+ bun run lint:fix # Fix code style issues
292
+ bun test # Run test suite
293
+ ```
294
+
295
+ ## ๐Ÿ“„ License
296
+
297
+ MIT ยฉ [Nuvix](https://github.com/nuvix-tech/storage)
298
+
299
+ ## ๐Ÿ”— Links
300
+
301
+ - [GitHub Repository](https://github.com/nuvix-tech/storage)
302
+ - [NPM Package](https://www.npmjs.com/package/@nuvix/storage)
303
+ - [Documentation](https://github.com/nuvix-tech/storage#readme)
304
+ - [Issue Tracker](https://github.com/nuvix-tech/storage/issues)