@nuvix/storage 0.1.0 → 1.0.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 +125 -68
- package/dist/index.cjs.js +454 -428
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +38 -87
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +454 -429
- package/dist/index.esm.js.map +1 -1
- package/dist/src/device/local.d.ts +3 -3
- package/dist/src/device/local.d.ts.map +1 -1
- package/dist/src/device/minio.d.ts +16 -0
- package/dist/src/device/minio.d.ts.map +1 -0
- package/dist/src/device/s3.d.ts +14 -9
- package/dist/src/device/s3.d.ts.map +1 -1
- package/dist/src/device/wasabi.d.ts.map +1 -1
- package/dist/src/device.d.ts +3 -3
- package/dist/src/device.d.ts.map +1 -1
- package/dist/src/storage.d.ts +2 -1
- package/dist/src/storage.d.ts.map +1 -1
- package/dist/src/validator/file-ext.d.ts +0 -2
- package/dist/src/validator/file-ext.d.ts.map +1 -1
- package/dist/src/validator/file-name.d.ts +1 -17
- package/dist/src/validator/file-name.d.ts.map +1 -1
- package/dist/src/validator/file-size.d.ts +1 -13
- package/dist/src/validator/file-size.d.ts.map +1 -1
- package/dist/src/validator/file-type.d.ts +1 -9
- package/dist/src/validator/file-type.d.ts.map +1 -1
- package/dist/src/validator/file.d.ts +0 -16
- package/dist/src/validator/file.d.ts.map +1 -1
- package/dist/src/validator/upload.d.ts +0 -16
- package/dist/src/validator/upload.d.ts.map +1 -1
- package/dist/src/validator/validator.d.ts.map +1 -1
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# @nuvix/storage
|
|
2
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
|
|
3
|
+
A powerful, TypeScript-first S3-compatible storage library for the Nuvix BaaS platform. Supports multiple storage backends including local filesystem, AWS S3, Wasabi, and MinIO with a unified API.
|
|
4
4
|
|
|
5
5
|
## ✨ Features
|
|
6
6
|
|
|
7
7
|
- 🚀 **S3-Compatible API** - Consistent interface across all storage providers
|
|
8
|
-
- 🔌 **Multiple Storage Backends** - Local, AWS S3, Wasabi, and more
|
|
8
|
+
- 🔌 **Multiple Storage Backends** - Local, AWS S3, Wasabi, MinIO, and more
|
|
9
9
|
- 📝 **TypeScript First** - Full type safety with comprehensive TypeScript definitions
|
|
10
10
|
- ✅ **File Validation** - Built-in validators for file types, sizes, names, and extensions
|
|
11
11
|
- 🔄 **Chunked Uploads** - Support for large file uploads with multipart upload
|
|
@@ -31,179 +31,227 @@ bun add @nuvix/storage
|
|
|
31
31
|
## 🚀 Quick Start
|
|
32
32
|
|
|
33
33
|
### ESM (Modern)
|
|
34
|
+
|
|
34
35
|
```typescript
|
|
35
|
-
import {
|
|
36
|
+
import {
|
|
37
|
+
Storage,
|
|
38
|
+
Local,
|
|
39
|
+
Wasabi,
|
|
40
|
+
MinIO,
|
|
41
|
+
FileExt,
|
|
42
|
+
FileSize,
|
|
43
|
+
} from "@nuvix/storage";
|
|
36
44
|
|
|
37
45
|
// Set up local storage
|
|
38
|
-
const localStorage = new Local(
|
|
46
|
+
const localStorage = new Local("./uploads");
|
|
39
47
|
Storage.setDevice(Storage.DEVICE_LOCAL, localStorage);
|
|
40
48
|
|
|
41
49
|
// Upload a file
|
|
42
50
|
const localDevice = Storage.getDevice(Storage.DEVICE_LOCAL);
|
|
43
|
-
await localDevice.write(
|
|
51
|
+
await localDevice.write("test.txt", "Hello, World!", "text/plain");
|
|
44
52
|
```
|
|
45
53
|
|
|
46
54
|
### CommonJS (Legacy)
|
|
55
|
+
|
|
47
56
|
```javascript
|
|
48
|
-
const { Storage, Local, Wasabi } = require(
|
|
57
|
+
const { Storage, Local, Wasabi, MinIO } = require("@nuvix/storage");
|
|
49
58
|
|
|
50
59
|
// Set up local storage
|
|
51
|
-
const localStorage = new Local(
|
|
60
|
+
const localStorage = new Local("./uploads");
|
|
52
61
|
Storage.setDevice(Storage.DEVICE_LOCAL, localStorage);
|
|
53
62
|
```
|
|
54
63
|
|
|
55
64
|
## 🔧 Storage Devices
|
|
56
65
|
|
|
57
66
|
### Local File System
|
|
67
|
+
|
|
58
68
|
```typescript
|
|
59
|
-
import { Local, Storage } from
|
|
69
|
+
import { Local, Storage } from "@nuvix/storage";
|
|
60
70
|
|
|
61
|
-
const localStorage = new Local(
|
|
62
|
-
Storage.setDevice(
|
|
71
|
+
const localStorage = new Local("./uploads");
|
|
72
|
+
Storage.setDevice("local", localStorage);
|
|
63
73
|
|
|
64
74
|
// Write file
|
|
65
|
-
await localStorage.write(
|
|
75
|
+
await localStorage.write("documents/file.txt", "content", "text/plain");
|
|
66
76
|
|
|
67
77
|
// Read file
|
|
68
|
-
const content = await localStorage.read(
|
|
78
|
+
const content = await localStorage.read("documents/file.txt");
|
|
69
79
|
|
|
70
80
|
// Check if file exists
|
|
71
|
-
const exists = await localStorage.exists(
|
|
81
|
+
const exists = await localStorage.exists("documents/file.txt");
|
|
72
82
|
```
|
|
73
83
|
|
|
74
84
|
### Wasabi Cloud Storage
|
|
85
|
+
|
|
75
86
|
```typescript
|
|
76
|
-
import { Wasabi, Storage } from
|
|
87
|
+
import { Wasabi, Storage } from "@nuvix/storage";
|
|
77
88
|
|
|
78
89
|
const wasabiStorage = new Wasabi(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
Wasabi.US_CENTRAL_1,
|
|
84
|
-
Wasabi.ACL_PRIVATE
|
|
90
|
+
"my-app-uploads", // root path
|
|
91
|
+
"your-access-key", // access key
|
|
92
|
+
"your-secret-key", // secret key
|
|
93
|
+
"your-bucket-name", // bucket name
|
|
94
|
+
Wasabi.US_CENTRAL_1, // region
|
|
95
|
+
Wasabi.ACL_PRIVATE, // ACL (optional)
|
|
85
96
|
);
|
|
86
97
|
|
|
87
|
-
Storage.setDevice(
|
|
98
|
+
Storage.setDevice("wasabi", wasabiStorage);
|
|
88
99
|
|
|
89
100
|
// Upload with metadata
|
|
90
101
|
await wasabiStorage.uploadData(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
1,
|
|
95
|
-
|
|
102
|
+
"Hello, Cloud!",
|
|
103
|
+
"cloud-file.txt",
|
|
104
|
+
"text/plain",
|
|
105
|
+
1,
|
|
106
|
+
1,
|
|
107
|
+
{ author: "user123" },
|
|
96
108
|
);
|
|
97
109
|
```
|
|
98
110
|
|
|
99
111
|
### AWS S3
|
|
112
|
+
|
|
100
113
|
```typescript
|
|
101
|
-
import { S3, Storage } from
|
|
114
|
+
import { S3, Storage } from "@nuvix/storage";
|
|
102
115
|
|
|
103
116
|
const s3Storage = new S3(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
S3.US_EAST_1,
|
|
109
|
-
S3.ACL_PRIVATE
|
|
117
|
+
"my-app-uploads", // root path
|
|
118
|
+
"your-access-key", // access key
|
|
119
|
+
"your-secret-key", // secret key
|
|
120
|
+
"your-bucket-name", // bucket name
|
|
121
|
+
S3.US_EAST_1, // region
|
|
122
|
+
S3.ACL_PRIVATE, // ACL (optional)
|
|
110
123
|
);
|
|
111
124
|
|
|
112
|
-
Storage.setDevice(
|
|
125
|
+
Storage.setDevice("s3", s3Storage);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### MinIO
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { MinIO, Storage } from "@nuvix/storage";
|
|
132
|
+
|
|
133
|
+
const minioStorage = new MinIO(
|
|
134
|
+
"my-app-uploads", // root path
|
|
135
|
+
"minioadmin", // access key (default MinIO credentials)
|
|
136
|
+
"minioadmin", // secret key (default MinIO credentials)
|
|
137
|
+
"your-bucket-name", // bucket name
|
|
138
|
+
"localhost:9000", // MinIO endpoint
|
|
139
|
+
MinIO.ACL_PRIVATE, // ACL (optional)
|
|
140
|
+
false, // useSSL (optional, default: false)
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
Storage.setDevice("minio", minioStorage);
|
|
144
|
+
|
|
145
|
+
// Upload to MinIO
|
|
146
|
+
await minioStorage.write("local-file.txt", "Hello, MinIO!", "text/plain");
|
|
147
|
+
|
|
148
|
+
// Read from MinIO
|
|
149
|
+
const content = await minioStorage.read("local-file.txt");
|
|
150
|
+
|
|
151
|
+
// Check if file exists
|
|
152
|
+
const exists = await minioStorage.exists("local-file.txt");
|
|
153
|
+
|
|
154
|
+
// Get file information
|
|
155
|
+
const size = await minioStorage.getFileSize("local-file.txt");
|
|
156
|
+
const mimeType = await minioStorage.getFileMimeType("local-file.txt");
|
|
113
157
|
```
|
|
114
158
|
|
|
115
159
|
## 🛡️ File Validation
|
|
116
160
|
|
|
117
161
|
### File Extension Validation
|
|
162
|
+
|
|
118
163
|
```typescript
|
|
119
|
-
import { FileExt } from
|
|
164
|
+
import { FileExt } from "@nuvix/storage";
|
|
120
165
|
|
|
121
|
-
const imageValidator = new FileExt([
|
|
166
|
+
const imageValidator = new FileExt(["jpg", "png", "gif", "webp"]);
|
|
122
167
|
|
|
123
|
-
console.log(imageValidator.isValid(
|
|
124
|
-
console.log(imageValidator.isValid(
|
|
168
|
+
console.log(imageValidator.isValid("photo.jpg")); // true
|
|
169
|
+
console.log(imageValidator.isValid("document.pdf")); // false
|
|
125
170
|
```
|
|
126
171
|
|
|
127
172
|
### File Size Validation
|
|
173
|
+
|
|
128
174
|
```typescript
|
|
129
|
-
import { FileSize } from
|
|
175
|
+
import { FileSize } from "@nuvix/storage";
|
|
130
176
|
|
|
131
177
|
const sizeValidator = new FileSize(5 * 1024 * 1024); // 5MB limit
|
|
132
178
|
|
|
133
|
-
console.log(sizeValidator.isValid(1024 * 1024));
|
|
179
|
+
console.log(sizeValidator.isValid(1024 * 1024)); // true (1MB)
|
|
134
180
|
console.log(sizeValidator.isValid(10 * 1024 * 1024)); // false (10MB)
|
|
135
181
|
```
|
|
136
182
|
|
|
137
183
|
### File Name Validation
|
|
184
|
+
|
|
138
185
|
```typescript
|
|
139
|
-
import { FileName } from
|
|
186
|
+
import { FileName } from "@nuvix/storage";
|
|
140
187
|
|
|
141
188
|
const nameValidator = new FileName();
|
|
142
189
|
|
|
143
|
-
console.log(nameValidator.isValid(
|
|
144
|
-
console.log(nameValidator.isValid(
|
|
190
|
+
console.log(nameValidator.isValid("validfile123.txt")); // true
|
|
191
|
+
console.log(nameValidator.isValid("invalid-file.txt")); // false (contains hyphen)
|
|
145
192
|
```
|
|
146
193
|
|
|
147
194
|
### File Type (MIME) Validation
|
|
195
|
+
|
|
148
196
|
```typescript
|
|
149
|
-
import { FileType } from
|
|
197
|
+
import { FileType } from "@nuvix/storage";
|
|
150
198
|
|
|
151
|
-
const typeValidator = new FileType([
|
|
199
|
+
const typeValidator = new FileType(["jpeg", "png", "gif"]);
|
|
152
200
|
|
|
153
201
|
// Validates based on file content, not extension
|
|
154
|
-
console.log(await typeValidator.isValid(
|
|
202
|
+
console.log(await typeValidator.isValid("./image.jpg")); // true if actual JPEG
|
|
155
203
|
```
|
|
156
204
|
|
|
157
205
|
### Upload File Validation
|
|
206
|
+
|
|
158
207
|
```typescript
|
|
159
|
-
import { Upload } from
|
|
208
|
+
import { Upload } from "@nuvix/storage";
|
|
160
209
|
|
|
161
210
|
const uploadValidator = new Upload();
|
|
162
211
|
|
|
163
|
-
console.log(await uploadValidator.isValid(
|
|
164
|
-
console.log(await uploadValidator.isValid(
|
|
212
|
+
console.log(await uploadValidator.isValid("./existing-file.txt")); // true
|
|
213
|
+
console.log(await uploadValidator.isValid("./non-existent.txt")); // false
|
|
165
214
|
```
|
|
166
215
|
|
|
167
216
|
## 🔄 Advanced Features
|
|
168
217
|
|
|
169
218
|
### Chunked Uploads
|
|
219
|
+
|
|
170
220
|
```typescript
|
|
171
221
|
// Upload large files in chunks
|
|
172
|
-
const chunks = [
|
|
222
|
+
const chunks = ["chunk1", "chunk2", "chunk3"];
|
|
173
223
|
const metadata = {};
|
|
174
224
|
|
|
175
225
|
for (let i = 0; i < chunks.length; i++) {
|
|
176
226
|
await device.uploadData(
|
|
177
|
-
chunks[i],
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
i + 1,
|
|
181
|
-
chunks.length,
|
|
182
|
-
metadata
|
|
227
|
+
chunks[i],
|
|
228
|
+
"large-file.txt",
|
|
229
|
+
"text/plain",
|
|
230
|
+
i + 1, // current chunk
|
|
231
|
+
chunks.length, // total chunks
|
|
232
|
+
metadata,
|
|
183
233
|
);
|
|
184
234
|
}
|
|
185
235
|
```
|
|
186
236
|
|
|
187
237
|
### File Transfer Between Devices
|
|
238
|
+
|
|
188
239
|
```typescript
|
|
189
|
-
const sourceDevice = Storage.getDevice(
|
|
190
|
-
const targetDevice = Storage.getDevice(
|
|
240
|
+
const sourceDevice = Storage.getDevice("local");
|
|
241
|
+
const targetDevice = Storage.getDevice("wasabi");
|
|
191
242
|
|
|
192
243
|
// Transfer file from local to cloud
|
|
193
|
-
await sourceDevice.transfer(
|
|
194
|
-
'local-file.txt',
|
|
195
|
-
'cloud-file.txt',
|
|
196
|
-
targetDevice
|
|
197
|
-
);
|
|
244
|
+
await sourceDevice.transfer("local-file.txt", "cloud-file.txt", targetDevice);
|
|
198
245
|
```
|
|
199
246
|
|
|
200
247
|
### Human-Readable File Sizes
|
|
248
|
+
|
|
201
249
|
```typescript
|
|
202
|
-
import { Storage } from
|
|
250
|
+
import { Storage } from "@nuvix/storage";
|
|
203
251
|
|
|
204
|
-
console.log(Storage.human(1024));
|
|
205
|
-
console.log(Storage.human(1048576));
|
|
206
|
-
console.log(Storage.human(1024, 2,
|
|
252
|
+
console.log(Storage.human(1024)); // "1.00kB"
|
|
253
|
+
console.log(Storage.human(1048576)); // "1.00MB"
|
|
254
|
+
console.log(Storage.human(1024, 2, "binary")); // "1.00KiB"
|
|
207
255
|
```
|
|
208
256
|
|
|
209
257
|
## 🏗️ Building
|
|
@@ -255,18 +303,27 @@ export WASABI_BUCKET="your-test-bucket"
|
|
|
255
303
|
export AWS_ACCESS_KEY="your-access-key"
|
|
256
304
|
export AWS_SECRET_KEY="your-secret-key"
|
|
257
305
|
export AWS_BUCKET="your-test-bucket"
|
|
306
|
+
|
|
307
|
+
# For MinIO testing
|
|
308
|
+
export MINIO_ACCESS_KEY="minioadmin"
|
|
309
|
+
export MINIO_SECRET_KEY="minioadmin"
|
|
310
|
+
export MINIO_BUCKET="test-bucket"
|
|
311
|
+
export MINIO_ENDPOINT="localhost:9000"
|
|
258
312
|
```
|
|
259
313
|
|
|
260
314
|
## 📝 API Reference
|
|
261
315
|
|
|
262
316
|
### Storage Class
|
|
317
|
+
|
|
263
318
|
- `Storage.setDevice(name, device)` - Register a storage device
|
|
264
319
|
- `Storage.getDevice(name)` - Get a registered storage device
|
|
265
320
|
- `Storage.exists(name)` - Check if a device is registered
|
|
266
321
|
- `Storage.human(bytes, decimals?, system?)` - Format bytes to human-readable string
|
|
267
322
|
|
|
268
323
|
### Device Interface
|
|
324
|
+
|
|
269
325
|
All storage devices implement the same interface:
|
|
326
|
+
|
|
270
327
|
- `upload(source, path, chunk?, chunks?, metadata?)` - Upload file
|
|
271
328
|
- `uploadData(data, path, contentType, chunk?, chunks?, metadata?)` - Upload data
|
|
272
329
|
- `read(path, offset?, length?)` - Read file content
|
|
@@ -301,4 +358,4 @@ MIT © [Nuvix](https://github.com/nuvix-tech/storage)
|
|
|
301
358
|
- [GitHub Repository](https://github.com/nuvix-tech/storage)
|
|
302
359
|
- [NPM Package](https://www.npmjs.com/package/@nuvix/storage)
|
|
303
360
|
- [Documentation](https://github.com/nuvix-tech/storage#readme)
|
|
304
|
-
- [Issue Tracker](https://github.com/nuvix-tech/storage/issues)
|
|
361
|
+
- [Issue Tracker](https://github.com/nuvix-tech/storage/issues)
|