@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.
@@ -0,0 +1,519 @@
1
+ interface DeviceMetadata {
2
+ [key: string]: any;
3
+ }
4
+ declare abstract class Device {
5
+ /**
6
+ * Max chunk size while transferring file from one device to another
7
+ */
8
+ protected transferChunkSize: number;
9
+ /**
10
+ * Sets the maximum number of keys returned to the response. By default, the action returns up to 1,000 key names.
11
+ */
12
+ protected static readonly MAX_PAGE_SIZE: number;
13
+ /**
14
+ * Set Transfer Chunk Size
15
+ */
16
+ setTransferChunkSize(chunkSize: number): void;
17
+ /**
18
+ * Get Transfer Chunk Size
19
+ */
20
+ getTransferChunkSize(): number;
21
+ /**
22
+ * Get Name.
23
+ *
24
+ * Get storage device name
25
+ */
26
+ abstract getName(): string;
27
+ /**
28
+ * Get Type.
29
+ *
30
+ * Get storage device type
31
+ */
32
+ abstract getType(): string;
33
+ /**
34
+ * Get Description.
35
+ *
36
+ * Get storage device description and purpose.
37
+ */
38
+ abstract getDescription(): string;
39
+ /**
40
+ * Get Root.
41
+ *
42
+ * Get storage device root path
43
+ */
44
+ abstract getRoot(): string;
45
+ /**
46
+ * Get Path.
47
+ *
48
+ * Each device hold a complex directory structure that is being build in this method.
49
+ */
50
+ abstract getPath(filename: string, prefix?: string): string;
51
+ /**
52
+ * Upload.
53
+ *
54
+ * Upload a file to desired destination in the selected disk
55
+ * return number of chunks uploaded or 0 if it fails.
56
+ */
57
+ abstract upload(source: string, path: string, chunk?: number, chunks?: number, metadata?: DeviceMetadata): Promise<number>;
58
+ /**
59
+ * Upload Data.
60
+ *
61
+ * Upload file contents to desired destination in the selected disk.
62
+ * return number of chunks uploaded or 0 if it fails.
63
+ */
64
+ abstract uploadData(data: string, path: string, contentType: string, chunk?: number, chunks?: number, metadata?: DeviceMetadata): Promise<number>;
65
+ /**
66
+ * Abort Chunked Upload
67
+ */
68
+ abstract abort(path: string, extra?: string): Promise<boolean>;
69
+ /**
70
+ * Read file by given path.
71
+ */
72
+ abstract read(path: string, offset?: number, length?: number): Promise<string>;
73
+ /**
74
+ * Transfer
75
+ * Transfer a file from current device to destination device.
76
+ */
77
+ abstract transfer(path: string, destination: string, device: Device): Promise<boolean>;
78
+ /**
79
+ * Write file by given path.
80
+ */
81
+ abstract write(path: string, data: string, contentType: string): Promise<boolean>;
82
+ /**
83
+ * Move file from given source to given path, return true on success and false on failure.
84
+ */
85
+ move(source: string, target: string): Promise<boolean>;
86
+ /**
87
+ * Delete file in given path return true on success and false on failure.
88
+ */
89
+ abstract delete(path: string, recursive?: boolean): Promise<boolean>;
90
+ /**
91
+ * Delete files in given path, path must be a directory. return true on success and false on failure.
92
+ */
93
+ abstract deletePath(path: string): Promise<boolean>;
94
+ /**
95
+ * Check if file exists
96
+ */
97
+ abstract exists(path: string): Promise<boolean>;
98
+ /**
99
+ * Returns given file path its size.
100
+ */
101
+ abstract getFileSize(path: string): Promise<number>;
102
+ /**
103
+ * Returns given file path its mime type.
104
+ */
105
+ abstract getFileMimeType(path: string): Promise<string>;
106
+ /**
107
+ * Returns given file path its MD5 hash value.
108
+ */
109
+ abstract getFileHash(path: string): Promise<string>;
110
+ /**
111
+ * Create a directory at the specified path.
112
+ *
113
+ * Returns true on success or if the directory already exists and false on error
114
+ */
115
+ abstract createDirectory(path: string): Promise<boolean>;
116
+ /**
117
+ * Get directory size in bytes.
118
+ *
119
+ * Return -1 on error
120
+ */
121
+ abstract getDirectorySize(path: string): Promise<number>;
122
+ /**
123
+ * Get Partition Free Space.
124
+ *
125
+ * Returns available space on filesystem or disk partition
126
+ */
127
+ abstract getPartitionFreeSpace(): Promise<number>;
128
+ /**
129
+ * Get Partition Total Space.
130
+ *
131
+ * Returns the total size of a filesystem or disk partition
132
+ */
133
+ abstract getPartitionTotalSpace(): Promise<number>;
134
+ /**
135
+ * Get all files and directories inside a directory.
136
+ */
137
+ abstract getFiles(dir: string, max?: number, continuationToken?: string): Promise<any[]>;
138
+ /**
139
+ * Get the absolute path by resolving strings like ../, .., //, /\ and so on.
140
+ *
141
+ * Works like the realpath function but works on files that does not exist
142
+ */
143
+ getAbsolutePath(path: string): string;
144
+ protected getMimeType(filePath: string): Promise<string>;
145
+ }
146
+
147
+ declare class Storage {
148
+ /**
149
+ * Supported devices
150
+ */
151
+ static readonly DEVICE_LOCAL = "local";
152
+ static readonly DEVICE_S3 = "s3";
153
+ static readonly DEVICE_WASABI = "wasabi";
154
+ /**
155
+ * Devices.
156
+ *
157
+ * List of all available storage devices
158
+ */
159
+ private static devices;
160
+ /**
161
+ * Set Device.
162
+ *
163
+ * Add device by name
164
+ *
165
+ * @param name
166
+ * @param device
167
+ * @throws Error
168
+ */
169
+ static setDevice(name: string, device: Device): void;
170
+ /**
171
+ * Get Device.
172
+ *
173
+ * Get device by name
174
+ *
175
+ * @param name
176
+ * @returns Device
177
+ * @throws Error
178
+ */
179
+ static getDevice(name: string): Device;
180
+ /**
181
+ * Exists.
182
+ *
183
+ * Checks if given storage name is registered or not
184
+ *
185
+ * @param name
186
+ * @returns boolean
187
+ */
188
+ static exists(name: string): boolean;
189
+ /**
190
+ * Human readable data size format from bytes input.
191
+ *
192
+ * Based on: https://stackoverflow.com/a/38659168/2299554
193
+ *
194
+ * @param bytes
195
+ * @param decimals
196
+ * @param system
197
+ * @returns string
198
+ */
199
+ static human(bytes: number, decimals?: number, system?: 'binary' | 'metric'): string;
200
+ }
201
+
202
+ declare abstract class Validator {
203
+ static TYPE_STRING: string;
204
+ static TYPE_ARRAY: string;
205
+ static TYPE_INTEGER: string;
206
+ static TYPE_BOOLEAN: string;
207
+ abstract isValid(value: any): boolean | Promise<boolean>;
208
+ }
209
+
210
+ declare class File extends Validator {
211
+ getDescription(): string;
212
+ /**
213
+ * NOT MUCH RIGHT NOW.
214
+ *
215
+ * TODO think what to do here, currently only used for parameter to be present in SDKs
216
+ *
217
+ * @param name
218
+ * @return boolean
219
+ */
220
+ isValid(name: any): boolean;
221
+ /**
222
+ * Is array
223
+ *
224
+ * Function will return true if object is array.
225
+ *
226
+ * @return boolean
227
+ */
228
+ isArray(): boolean;
229
+ /**
230
+ * Get Type
231
+ *
232
+ * Returns validator type.
233
+ *
234
+ * @return string
235
+ */
236
+ getType(): string;
237
+ }
238
+
239
+ declare class FileExt extends Validator {
240
+ static readonly TYPE_JPEG = "jpeg";
241
+ static readonly TYPE_JPG = "jpg";
242
+ static readonly TYPE_GIF = "gif";
243
+ static readonly TYPE_PNG = "png";
244
+ static readonly TYPE_GZIP = "gz";
245
+ static readonly TYPE_ZIP = "zip";
246
+ private allowed;
247
+ constructor(allowed: string[]);
248
+ getDescription(): string;
249
+ isValid(filename: string): boolean;
250
+ isArray(): boolean;
251
+ getType(): string;
252
+ private getFileExtension;
253
+ }
254
+
255
+ declare class FileName extends Validator {
256
+ /**
257
+ * Get Description
258
+ */
259
+ getDescription(): string;
260
+ /**
261
+ * The file name can only contain "a-z", "A-Z", "0-9" and "." and not empty.
262
+ *
263
+ * @param name - The filename to validate
264
+ * @returns boolean indicating if the filename is valid
265
+ */
266
+ isValid(name: any): boolean;
267
+ /**
268
+ * Is array
269
+ *
270
+ * Function will return true if object is array.
271
+ *
272
+ * @returns boolean
273
+ */
274
+ isArray(): boolean;
275
+ /**
276
+ * Get Type
277
+ *
278
+ * Returns validator type.
279
+ *
280
+ * @returns string
281
+ */
282
+ getType(): string;
283
+ }
284
+
285
+ declare class FileSize extends Validator {
286
+ private max;
287
+ /**
288
+ * Max size in bytes
289
+ */
290
+ constructor(max: number);
291
+ /**
292
+ * Get Description
293
+ */
294
+ getDescription(): string;
295
+ /**
296
+ * Finds whether a file size is smaller than required limit.
297
+ */
298
+ isValid(fileSize: any): boolean;
299
+ /**
300
+ * Is array
301
+ *
302
+ * Function will return true if object is array.
303
+ */
304
+ isArray(): boolean;
305
+ /**
306
+ * Get Type
307
+ *
308
+ * Returns validator type.
309
+ */
310
+ getType(): string;
311
+ }
312
+
313
+ declare class FileType extends Validator {
314
+ /**
315
+ * File Types Constants.
316
+ */
317
+ static readonly FILE_TYPE_JPEG = "jpeg";
318
+ static readonly FILE_TYPE_GIF = "gif";
319
+ static readonly FILE_TYPE_PNG = "png";
320
+ static readonly FILE_TYPE_GZIP = "gz";
321
+ /**
322
+ * File Type Binaries.
323
+ */
324
+ private readonly types;
325
+ private readonly allowed;
326
+ constructor(allowed: string[]);
327
+ /**
328
+ * Get Description
329
+ */
330
+ getDescription(): string;
331
+ /**
332
+ * Is Valid.
333
+ *
334
+ * Binary check to finds whether a file is of valid type
335
+ */
336
+ isValid(path: string): Promise<boolean>;
337
+ /**
338
+ * Is array
339
+ */
340
+ isArray(): boolean;
341
+ /**
342
+ * Get Type
343
+ */
344
+ getType(): string;
345
+ }
346
+
347
+ declare class Upload extends Validator {
348
+ /**
349
+ * Get Description
350
+ */
351
+ getDescription(): string;
352
+ /**
353
+ * Check if a file is a valid upload file
354
+ *
355
+ * @param path - The file path to validate
356
+ * @returns Promise that resolves to true if valid upload file, false otherwise
357
+ */
358
+ isValid(path: unknown): Promise<boolean>;
359
+ /**
360
+ * Is array
361
+ *
362
+ * Function will return true if object is array.
363
+ *
364
+ * @returns false - this validator doesn't handle arrays
365
+ */
366
+ isArray(): boolean;
367
+ /**
368
+ * Get Type
369
+ *
370
+ * Returns validator type.
371
+ *
372
+ * @returns The validator type
373
+ */
374
+ getType(): string;
375
+ }
376
+
377
+ declare class Local extends Device {
378
+ protected root: string;
379
+ protected readonly MAX_PAGE_SIZE = 1000;
380
+ constructor(root?: string);
381
+ getName(): string;
382
+ getType(): string;
383
+ getDescription(): string;
384
+ getRoot(): string;
385
+ getPath(filename: string, prefix?: string): string;
386
+ upload(source: string, filePath: string, chunk?: number, chunks?: number, metadata?: Record<string, any>): Promise<number>;
387
+ uploadData(data: string, filePath: string, contentType: string, chunk?: number, chunks?: number, metadata?: Record<string, any>): Promise<number>;
388
+ private joinChunks;
389
+ transfer(filePath: string, destination: string, device: Device): Promise<boolean>;
390
+ abort(filePath: string, extra?: string): Promise<boolean>;
391
+ read(filePath: string, offset?: number, length?: number): Promise<string>;
392
+ write(filePath: string, data: string, contentType?: string): Promise<boolean>;
393
+ move(source: string, target: string): Promise<boolean>;
394
+ delete(filePath: string, recursive?: boolean): Promise<boolean>;
395
+ deletePath(filePath: string): Promise<boolean>;
396
+ exists(filePath: string): Promise<boolean>;
397
+ getFileSize(filePath: string): Promise<number>;
398
+ getFileMimeType(filePath: string): Promise<string>;
399
+ getFileHash(filePath: string): Promise<string>;
400
+ createDirectory(dirPath: string): Promise<boolean>;
401
+ getDirectorySize(dirPath: string): Promise<number>;
402
+ getPartitionFreeSpace(): Promise<number>;
403
+ getPartitionTotalSpace(): Promise<number>;
404
+ getFiles(dir: string, max?: number, continuationToken?: string): Promise<string[]>;
405
+ }
406
+
407
+ declare class S3 extends Device {
408
+ static readonly METHOD_GET = "GET";
409
+ static readonly METHOD_POST = "POST";
410
+ static readonly METHOD_PUT = "PUT";
411
+ static readonly METHOD_PATCH = "PATCH";
412
+ static readonly METHOD_DELETE = "DELETE";
413
+ static readonly METHOD_HEAD = "HEAD";
414
+ static readonly METHOD_OPTIONS = "OPTIONS";
415
+ static readonly METHOD_CONNECT = "CONNECT";
416
+ static readonly METHOD_TRACE = "TRACE";
417
+ static readonly US_EAST_1 = "us-east-1";
418
+ static readonly US_EAST_2 = "us-east-2";
419
+ static readonly US_WEST_1 = "us-west-1";
420
+ static readonly US_WEST_2 = "us-west-2";
421
+ static readonly AF_SOUTH_1 = "af-south-1";
422
+ static readonly AP_EAST_1 = "ap-east-1";
423
+ static readonly AP_SOUTH_1 = "ap-south-1";
424
+ static readonly AP_NORTHEAST_3 = "ap-northeast-3";
425
+ static readonly AP_NORTHEAST_2 = "ap-northeast-2";
426
+ static readonly AP_NORTHEAST_1 = "ap-northeast-1";
427
+ static readonly AP_SOUTHEAST_1 = "ap-southeast-1";
428
+ static readonly AP_SOUTHEAST_2 = "ap-southeast-2";
429
+ static readonly CA_CENTRAL_1 = "ca-central-1";
430
+ static readonly EU_CENTRAL_1 = "eu-central-1";
431
+ static readonly EU_WEST_1 = "eu-west-1";
432
+ static readonly EU_SOUTH_1 = "eu-south-1";
433
+ static readonly EU_WEST_2 = "eu-west-2";
434
+ static readonly EU_WEST_3 = "eu-west-3";
435
+ static readonly EU_NORTH_1 = "eu-north-1";
436
+ static readonly SA_EAST_1 = "eu-north-1";
437
+ static readonly CN_NORTH_1 = "cn-north-1";
438
+ static readonly CN_NORTH_4 = "cn-north-4";
439
+ static readonly CN_NORTHWEST_1 = "cn-northwest-1";
440
+ static readonly ME_SOUTH_1 = "me-south-1";
441
+ static readonly US_GOV_EAST_1 = "us-gov-east-1";
442
+ static readonly US_GOV_WEST_1 = "us-gov-west-1";
443
+ static readonly ACL_PRIVATE = "private";
444
+ static readonly ACL_PUBLIC_READ = "public-read";
445
+ static readonly ACL_PUBLIC_READ_WRITE = "public-read-write";
446
+ static readonly ACL_AUTHENTICATED_READ = "authenticated-read";
447
+ protected static readonly MAX_PAGE_SIZE = 1000;
448
+ protected static retryAttempts: number;
449
+ protected static retryDelay: number;
450
+ protected accessKey: string;
451
+ protected secretKey: string;
452
+ protected bucket: string;
453
+ protected region: string;
454
+ protected acl: string;
455
+ protected root: string;
456
+ protected headers: Record<string, string>;
457
+ protected amzHeaders: Record<string, string>;
458
+ constructor(root: string, accessKey: string, secretKey: string, bucket: string, region?: string, acl?: string, endpointUrl?: string);
459
+ getName(): string;
460
+ getType(): string;
461
+ getDescription(): string;
462
+ getRoot(): string;
463
+ getPath(filename: string, prefix?: string): string;
464
+ getPartitionTotalSpace(): Promise<number>;
465
+ static setRetryAttempts(attempts: number): void;
466
+ static setRetryDelay(delay: number): void;
467
+ upload(source: string | Buffer, path: string, chunk?: number, chunks?: number, metadata?: Record<string, any>): Promise<number>;
468
+ uploadData(data: string, path: string, contentType: string, chunk?: number, chunks?: number, metadata?: Record<string, any>): Promise<number>;
469
+ transfer(path: string, destination: string, device: Device): Promise<boolean>;
470
+ protected createMultipartUpload(path: string, contentType: string): Promise<string>;
471
+ protected uploadPart(data: string, path: string, contentType: string, chunk: number, uploadId: string): Promise<string>;
472
+ protected completeMultipartUpload(path: string, uploadId: string, parts: Record<number, string>): Promise<boolean>;
473
+ abort(path: string, extra?: string): Promise<boolean>;
474
+ read(path: string, offset?: number, length?: number): Promise<string>;
475
+ write(path: string, data: string, contentType?: string): Promise<boolean>;
476
+ delete(path: string, recursive?: boolean): Promise<boolean>;
477
+ protected listObjects(prefix?: string, maxKeys?: number, continuationToken?: string): Promise<any>;
478
+ deletePath(path: string): Promise<boolean>;
479
+ exists(path: string): Promise<boolean>;
480
+ getFileSize(path: string): Promise<number>;
481
+ getFileMimeType(path: string): Promise<string>;
482
+ getFileHash(path: string): Promise<string>;
483
+ createDirectory(path: string): Promise<boolean>;
484
+ getDirectorySize(path: string): Promise<number>;
485
+ getPartitionFreeSpace(): Promise<number>;
486
+ getFiles(dir: string, max?: number, continuationToken?: string): Promise<any>;
487
+ private getInfo;
488
+ private getSignatureV4;
489
+ protected call(method: string, uri: string, data?: string, parameters?: Record<string, string>, decode?: boolean): Promise<any>;
490
+ private md5;
491
+ private sha256;
492
+ private hmacSha256;
493
+ }
494
+
495
+ declare class Wasabi extends S3 {
496
+ /**
497
+ * Regions constants
498
+ */
499
+ static readonly US_WEST_1 = "us-west-1";
500
+ static readonly AP_NORTHEAST_1 = "ap-northeast-1";
501
+ static readonly AP_NORTHEAST_2 = "ap-northeast-2";
502
+ static readonly EU_CENTRAL_1 = "eu-central-1";
503
+ static readonly EU_CENTRAL_2 = "eu-central-2";
504
+ static readonly EU_WEST_1 = "eu-west-1";
505
+ static readonly EU_WEST_2 = "eu-west-2";
506
+ static readonly US_CENTRAL_1 = "us-central-1";
507
+ static readonly US_EAST_1 = "us-east-1";
508
+ static readonly US_EAST_2 = "us-east-2";
509
+ /**
510
+ * Wasabi Constructor
511
+ */
512
+ constructor(root: string, accessKey: string, secretKey: string, bucket: string, region?: string, acl?: string);
513
+ getName(): string;
514
+ getDescription(): string;
515
+ getType(): string;
516
+ }
517
+
518
+ export { Device, File, FileExt, FileName, FileSize, FileType, Local, S3, Storage, Upload, Wasabi };
519
+ export type { DeviceMetadata };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}