@strav/kernel 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/package.json +59 -0
- package/src/cache/cache_manager.ts +60 -0
- package/src/cache/cache_store.ts +31 -0
- package/src/cache/helpers.ts +74 -0
- package/src/cache/index.ts +4 -0
- package/src/cache/memory_store.ts +63 -0
- package/src/config/configuration.ts +105 -0
- package/src/config/index.ts +2 -0
- package/src/config/loaders/base_loader.ts +69 -0
- package/src/config/loaders/env_loader.ts +112 -0
- package/src/config/loaders/typescript_loader.ts +56 -0
- package/src/config/types.ts +8 -0
- package/src/core/application.ts +241 -0
- package/src/core/container.ts +113 -0
- package/src/core/index.ts +4 -0
- package/src/core/inject.ts +39 -0
- package/src/core/service_provider.ts +44 -0
- package/src/encryption/encryption_manager.ts +215 -0
- package/src/encryption/helpers.ts +158 -0
- package/src/encryption/index.ts +3 -0
- package/src/encryption/types.ts +6 -0
- package/src/events/emitter.ts +101 -0
- package/src/events/index.ts +2 -0
- package/src/exceptions/errors.ts +71 -0
- package/src/exceptions/exception_handler.ts +140 -0
- package/src/exceptions/helpers.ts +25 -0
- package/src/exceptions/http_exception.ts +132 -0
- package/src/exceptions/index.ts +23 -0
- package/src/exceptions/strav_error.ts +11 -0
- package/src/helpers/compose.ts +104 -0
- package/src/helpers/crypto.ts +4 -0
- package/src/helpers/env.ts +50 -0
- package/src/helpers/index.ts +6 -0
- package/src/helpers/strings.ts +67 -0
- package/src/helpers/ulid.ts +28 -0
- package/src/i18n/defaults/en/validation.json +20 -0
- package/src/i18n/helpers.ts +76 -0
- package/src/i18n/i18n_manager.ts +157 -0
- package/src/i18n/index.ts +3 -0
- package/src/i18n/translator.ts +96 -0
- package/src/i18n/types.ts +17 -0
- package/src/index.ts +11 -0
- package/src/logger/index.ts +5 -0
- package/src/logger/logger.ts +113 -0
- package/src/logger/sinks/console_sink.ts +24 -0
- package/src/logger/sinks/file_sink.ts +24 -0
- package/src/logger/sinks/sink.ts +36 -0
- package/src/providers/cache_provider.ts +16 -0
- package/src/providers/config_provider.ts +26 -0
- package/src/providers/encryption_provider.ts +16 -0
- package/src/providers/i18n_provider.ts +17 -0
- package/src/providers/index.ts +8 -0
- package/src/providers/logger_provider.ts +16 -0
- package/src/providers/storage_provider.ts +16 -0
- package/src/storage/index.ts +32 -0
- package/src/storage/local_driver.ts +46 -0
- package/src/storage/ostra_client.ts +432 -0
- package/src/storage/ostra_driver.ts +58 -0
- package/src/storage/s3_driver.ts +51 -0
- package/src/storage/storage.ts +43 -0
- package/src/storage/storage_manager.ts +70 -0
- package/src/storage/types.ts +49 -0
- package/src/storage/upload.ts +91 -0
- package/tsconfig.json +5 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import Storage from './storage.ts'
|
|
2
|
+
import { StravError } from '../exceptions/strav_error.ts'
|
|
3
|
+
|
|
4
|
+
export class FileTooLargeError extends StravError {}
|
|
5
|
+
|
|
6
|
+
export class InvalidFileTypeError extends StravError {}
|
|
7
|
+
|
|
8
|
+
export interface UploadResult {
|
|
9
|
+
path: string
|
|
10
|
+
url: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const SIZE_UNITS: Record<string, number> = {
|
|
14
|
+
b: 1,
|
|
15
|
+
kb: 1024,
|
|
16
|
+
mb: 1024 * 1024,
|
|
17
|
+
gb: 1024 * 1024 * 1024,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Fluent file upload builder with validation.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const { path, url } = await Upload.file(avatar)
|
|
25
|
+
* .maxSize('5mb')
|
|
26
|
+
* .types(['image/jpeg', 'image/png'])
|
|
27
|
+
* .store('avatars')
|
|
28
|
+
*/
|
|
29
|
+
export class Upload {
|
|
30
|
+
private _maxSizeBytes?: number
|
|
31
|
+
private _allowedTypes?: string[]
|
|
32
|
+
|
|
33
|
+
private constructor(private _file: File) {}
|
|
34
|
+
|
|
35
|
+
/** Start an upload pipeline. */
|
|
36
|
+
static file(file: File): Upload {
|
|
37
|
+
return new Upload(file)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Set maximum file size (e.g. '5mb', '500kb', '1gb', or bytes as number). */
|
|
41
|
+
maxSize(size: string | number): this {
|
|
42
|
+
this._maxSizeBytes = typeof size === 'number' ? size : parseSize(size)
|
|
43
|
+
return this
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Set allowed MIME types. */
|
|
47
|
+
types(types: string[]): this {
|
|
48
|
+
this._allowedTypes = types
|
|
49
|
+
return this
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Validate and store the file. */
|
|
53
|
+
async store(directory: string, name?: string): Promise<UploadResult> {
|
|
54
|
+
this.validate()
|
|
55
|
+
|
|
56
|
+
const path = name
|
|
57
|
+
? await Storage.putAs(directory, this._file, name)
|
|
58
|
+
: await Storage.put(directory, this._file)
|
|
59
|
+
|
|
60
|
+
const url = Storage.url(path)
|
|
61
|
+
|
|
62
|
+
return { path, url }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private validate(): void {
|
|
66
|
+
if (this._maxSizeBytes !== undefined && this._file.size > this._maxSizeBytes) {
|
|
67
|
+
throw new FileTooLargeError(
|
|
68
|
+
`File size ${formatBytes(this._file.size)} exceeds maximum ${formatBytes(this._maxSizeBytes)}`
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (this._allowedTypes && !this._allowedTypes.includes(this._file.type)) {
|
|
73
|
+
throw new InvalidFileTypeError(
|
|
74
|
+
`File type "${this._file.type}" not allowed. Allowed: ${this._allowedTypes.join(', ')}`
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseSize(size: string): number {
|
|
81
|
+
const match = size.toLowerCase().match(/^(\d+(?:\.\d+)?)\s*(b|kb|mb|gb)$/)
|
|
82
|
+
if (!match) throw new Error(`Invalid size format: "${size}". Use e.g. '5mb', '500kb', '1gb'`)
|
|
83
|
+
return parseFloat(match[1]!) * SIZE_UNITS[match[2]!]!
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function formatBytes(bytes: number): string {
|
|
87
|
+
if (bytes < 1024) return `${bytes}B`
|
|
88
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`
|
|
89
|
+
if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)}MB`
|
|
90
|
+
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)}GB`
|
|
91
|
+
}
|