@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.
Files changed (64) hide show
  1. package/package.json +59 -0
  2. package/src/cache/cache_manager.ts +60 -0
  3. package/src/cache/cache_store.ts +31 -0
  4. package/src/cache/helpers.ts +74 -0
  5. package/src/cache/index.ts +4 -0
  6. package/src/cache/memory_store.ts +63 -0
  7. package/src/config/configuration.ts +105 -0
  8. package/src/config/index.ts +2 -0
  9. package/src/config/loaders/base_loader.ts +69 -0
  10. package/src/config/loaders/env_loader.ts +112 -0
  11. package/src/config/loaders/typescript_loader.ts +56 -0
  12. package/src/config/types.ts +8 -0
  13. package/src/core/application.ts +241 -0
  14. package/src/core/container.ts +113 -0
  15. package/src/core/index.ts +4 -0
  16. package/src/core/inject.ts +39 -0
  17. package/src/core/service_provider.ts +44 -0
  18. package/src/encryption/encryption_manager.ts +215 -0
  19. package/src/encryption/helpers.ts +158 -0
  20. package/src/encryption/index.ts +3 -0
  21. package/src/encryption/types.ts +6 -0
  22. package/src/events/emitter.ts +101 -0
  23. package/src/events/index.ts +2 -0
  24. package/src/exceptions/errors.ts +71 -0
  25. package/src/exceptions/exception_handler.ts +140 -0
  26. package/src/exceptions/helpers.ts +25 -0
  27. package/src/exceptions/http_exception.ts +132 -0
  28. package/src/exceptions/index.ts +23 -0
  29. package/src/exceptions/strav_error.ts +11 -0
  30. package/src/helpers/compose.ts +104 -0
  31. package/src/helpers/crypto.ts +4 -0
  32. package/src/helpers/env.ts +50 -0
  33. package/src/helpers/index.ts +6 -0
  34. package/src/helpers/strings.ts +67 -0
  35. package/src/helpers/ulid.ts +28 -0
  36. package/src/i18n/defaults/en/validation.json +20 -0
  37. package/src/i18n/helpers.ts +76 -0
  38. package/src/i18n/i18n_manager.ts +157 -0
  39. package/src/i18n/index.ts +3 -0
  40. package/src/i18n/translator.ts +96 -0
  41. package/src/i18n/types.ts +17 -0
  42. package/src/index.ts +11 -0
  43. package/src/logger/index.ts +5 -0
  44. package/src/logger/logger.ts +113 -0
  45. package/src/logger/sinks/console_sink.ts +24 -0
  46. package/src/logger/sinks/file_sink.ts +24 -0
  47. package/src/logger/sinks/sink.ts +36 -0
  48. package/src/providers/cache_provider.ts +16 -0
  49. package/src/providers/config_provider.ts +26 -0
  50. package/src/providers/encryption_provider.ts +16 -0
  51. package/src/providers/i18n_provider.ts +17 -0
  52. package/src/providers/index.ts +8 -0
  53. package/src/providers/logger_provider.ts +16 -0
  54. package/src/providers/storage_provider.ts +16 -0
  55. package/src/storage/index.ts +32 -0
  56. package/src/storage/local_driver.ts +46 -0
  57. package/src/storage/ostra_client.ts +432 -0
  58. package/src/storage/ostra_driver.ts +58 -0
  59. package/src/storage/s3_driver.ts +51 -0
  60. package/src/storage/storage.ts +43 -0
  61. package/src/storage/storage_manager.ts +70 -0
  62. package/src/storage/types.ts +49 -0
  63. package/src/storage/upload.ts +91 -0
  64. 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
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "include": ["src/**/*.ts"],
4
+ "exclude": ["node_modules", "tests"]
5
+ }