@visulima/storage 1.0.0-alpha.1 → 1.0.0-alpha.3

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 (224) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/README.md +33 -14
  3. package/dist/adapter/nuxt/module.d.ts +39 -0
  4. package/dist/adapter/nuxt/module.js +93 -0
  5. package/dist/adapter/nuxt/package.json +20 -0
  6. package/dist/handler/base/base-handler-core.d.ts +91 -0
  7. package/dist/handler/base/base-handler-fetch.d.ts +76 -0
  8. package/dist/handler/base/base-handler-node.d.ts +137 -0
  9. package/dist/handler/http/fetch/index.d.ts +4 -0
  10. package/dist/handler/http/fetch/index.js +4 -0
  11. package/dist/handler/http/hono/index.d.ts +67 -0
  12. package/dist/handler/http/hono/index.js +65 -0
  13. package/dist/handler/http/nextjs/index.d.ts +61 -0
  14. package/dist/handler/http/nextjs/index.js +37 -0
  15. package/dist/handler/http/node/index.d.ts +4 -0
  16. package/dist/handler/http/node/index.js +4 -0
  17. package/dist/handler/http/solid-start/index.d.ts +60 -0
  18. package/dist/handler/http/solid-start/index.js +44 -0
  19. package/dist/handler/multipart/multipart-base.d.ts +84 -0
  20. package/dist/handler/multipart/multipart-fetch.d.ts +64 -0
  21. package/dist/handler/multipart/multipart.d.ts +49 -0
  22. package/dist/handler/rest/rest-base.d.ts +106 -0
  23. package/dist/handler/rest/rest-fetch.d.ts +88 -0
  24. package/dist/handler/rest/rest.d.ts +93 -0
  25. package/dist/handler/tus/tus-base.d.ts +152 -0
  26. package/dist/handler/tus/tus-fetch.d.ts +72 -0
  27. package/dist/handler/tus/tus.d.ts +78 -0
  28. package/dist/handler/types.d.ts +53 -0
  29. package/dist/handler/utils/request-parser.d.ts +72 -0
  30. package/dist/handler/utils/response-builder.d.ts +83 -0
  31. package/dist/handler/utils/storage-utils.d.ts +10 -0
  32. package/dist/handler/utils/stream-utils.d.ts +29 -0
  33. package/dist/handler/utils/upload-handlers.d.ts +76 -0
  34. package/dist/index.d.ts +17 -0
  35. package/dist/index.js +13 -0
  36. package/dist/metrics/index.d.ts +2 -0
  37. package/dist/metrics/no-op-metrics.d.ts +15 -0
  38. package/dist/metrics/opentelemetry-metrics.d.ts +55 -0
  39. package/dist/openapi/index.d.ts +5 -0
  40. package/dist/openapi/index.js +5 -0
  41. package/dist/openapi/rest.d.ts +7 -0
  42. package/dist/openapi/shared.d.ts +13 -0
  43. package/dist/openapi/transform.d.ts +3 -0
  44. package/dist/openapi/tus.d.ts +7 -0
  45. package/dist/openapi/xhr.d.ts +7 -0
  46. package/dist/packem_shared/AwsLightFile-tTneXZgG.js +11 -0
  47. package/dist/packem_shared/AwsLightMetaStorage-BWSOtVaN.js +4 -0
  48. package/dist/packem_shared/AwsLightStorage-Blv-GP4v.js +133 -0
  49. package/dist/packem_shared/AzureFile-CesgFzps.js +8 -0
  50. package/dist/packem_shared/AzureSMetaStorage-CFs-OYJT.js +88 -0
  51. package/dist/packem_shared/AzureStorage-B0v048hG.js +357 -0
  52. package/dist/packem_shared/BaseTransformer-C2gLib6v.js +82 -0
  53. package/dist/packem_shared/DiskStorage-BlltBst6.js +12 -0
  54. package/dist/packem_shared/DiskStorageWithChecksum-BJTmMklc.js +238 -0
  55. package/dist/packem_shared/ERRORS-D0apMqnc.js +97 -0
  56. package/dist/packem_shared/File-Bb3P23dr.js +69 -0
  57. package/dist/packem_shared/GCSConfig-vPP22kN6.js +11 -0
  58. package/dist/packem_shared/GCSFile-BIEunhAN.js +8 -0
  59. package/dist/packem_shared/GCSMetaStorage-BMFEdzJ7.js +7 -0
  60. package/dist/packem_shared/GCStorage-DnhiwaS9.js +406 -0
  61. package/dist/packem_shared/LocalMetaStorage-CZHhKkMd.js +7 -0
  62. package/dist/packem_shared/MediaTransformer-D6658DL1.js +1197 -0
  63. package/dist/packem_shared/MetaStorage-pECeFOad.js +46 -0
  64. package/dist/packem_shared/Metadata-DRLXeJ0F.js +89 -0
  65. package/dist/packem_shared/Multipart-CNFK_Okz.js +163 -0
  66. package/dist/packem_shared/Multipart-CtUL7BQw.js +128 -0
  67. package/dist/packem_shared/NetlifyBlobFile-CXzyjqrD.js +14 -0
  68. package/dist/packem_shared/NetlifyBlobMetaStorage-DxZ5aDvD.js +9 -0
  69. package/dist/packem_shared/NetlifyBlobStorage-CLPtjHqd.js +376 -0
  70. package/dist/packem_shared/NoOpMetrics-DhAk5rXc.js +10 -0
  71. package/dist/packem_shared/OpenTelemetryMetrics-BnxhqIaH.js +67 -0
  72. package/dist/packem_shared/Rest-CAOAEkOj.js +267 -0
  73. package/dist/packem_shared/Rest-DNuLwBrK.js +228 -0
  74. package/dist/packem_shared/S3File-DZiyk9Qt.js +11 -0
  75. package/dist/packem_shared/S3MetaStorage-Dz9aDabA.js +76 -0
  76. package/dist/packem_shared/S3Storage-DHmTk-hU.js +316 -0
  77. package/dist/packem_shared/TUS_RESUMABLE-GJzZ9R-f.js +434 -0
  78. package/dist/packem_shared/Tus-B8PmlMgR.js +200 -0
  79. package/dist/packem_shared/Tus-C4F1aYOl.js +195 -0
  80. package/dist/packem_shared/ValidationError-BfF1aE4h.js +26 -0
  81. package/dist/packem_shared/VercelBlobFile-BCg4aTEq.js +18 -0
  82. package/dist/packem_shared/VercelBlobMetaStorage-Bd9F-VFm.js +9 -0
  83. package/dist/packem_shared/VercelBlobStorage-DD9LMOS6.js +261 -0
  84. package/dist/packem_shared/_commonjsHelpers-B85MJLTf.js +5 -0
  85. package/dist/packem_shared/aws-light-meta-storage-DKJBgbR6.js +446 -0
  86. package/dist/packem_shared/backblaze-BlMnIcBC.js +20 -0
  87. package/dist/packem_shared/base-handler-core-BKuf4YLT.js +303 -0
  88. package/dist/packem_shared/base-handler-fetch-Cr0hLiqg.js +291 -0
  89. package/dist/packem_shared/base-handler-node-gk5aN9Cx.js +730 -0
  90. package/dist/packem_shared/cache-B88MXQ_2.js +18 -0
  91. package/dist/packem_shared/cloudflare-Bi1q8wXE.js +21 -0
  92. package/dist/packem_shared/defaultCloudStorageFileNameValidation-DUuacjWv.js +11 -0
  93. package/dist/packem_shared/digitalOcean-CWQRJM3L.js +21 -0
  94. package/dist/packem_shared/disk-storage-Bh2wy3YW.js +810 -0
  95. package/dist/packem_shared/gcs-meta-storage-0tgMNnPO.js +158 -0
  96. package/dist/packem_shared/has-content-CY66ehMK.js +3 -0
  97. package/dist/packem_shared/headers-DoS5nwM-.js +3220 -0
  98. package/dist/packem_shared/is-expired-CTThU1q5.js +8 -0
  99. package/dist/packem_shared/isRetryableError-Dycp7127.js +82 -0
  100. package/dist/packem_shared/isValidMediaType-BeDgiObq.js +35 -0
  101. package/dist/packem_shared/local-meta-storage-By-3SNBI.js +649 -0
  102. package/dist/packem_shared/minio-C2YBZQOw.js +22 -0
  103. package/dist/packem_shared/multipart-base-B3i67SSX.js +102 -0
  104. package/dist/packem_shared/part-match-BMNqHDYD.js +96 -0
  105. package/dist/packem_shared/path-CR6YkPXX-7R1-9CMk.js +161 -0
  106. package/dist/packem_shared/response-builder-BtnRiBUI.js +45 -0
  107. package/dist/packem_shared/rest-base-GRCcnan7.js +457 -0
  108. package/dist/packem_shared/restOpenApiSpec-CwcHHY2C.js +800 -0
  109. package/dist/packem_shared/s3-base-storage-C1DIHTYY.js +510 -0
  110. package/dist/packem_shared/sharedGet-Cpo7QUyu.js +1019 -0
  111. package/dist/packem_shared/storage-C6NSXb8h.js +1175 -0
  112. package/dist/packem_shared/tigris-Hac8TKnX.js +21 -0
  113. package/dist/packem_shared/transformOpenApiSpec-DWM5WtnN.js +975 -0
  114. package/dist/packem_shared/tusOpenApiSpec-6ohiToBy.js +740 -0
  115. package/dist/packem_shared/update-size-CCGm6i1J.js +8 -0
  116. package/dist/packem_shared/validator-BeX_lJet.js +78 -0
  117. package/dist/packem_shared/waitForStorage-Cscw85sx.js +18 -0
  118. package/dist/packem_shared/wasabi-DIyflHSd.js +21 -0
  119. package/dist/packem_shared/xhrOpenApiSpec-DS17brnx.js +264 -0
  120. package/dist/storage/aws/clients/backblaze.d.ts +12 -0
  121. package/dist/storage/aws/clients/cloudflare.d.ts +13 -0
  122. package/dist/storage/aws/clients/digital-ocean.d.ts +12 -0
  123. package/dist/storage/aws/clients/index.d.ts +6 -0
  124. package/dist/storage/aws/clients/index.js +6 -0
  125. package/dist/storage/aws/clients/minio.d.ts +13 -0
  126. package/dist/storage/aws/clients/tigris.d.ts +12 -0
  127. package/dist/storage/aws/clients/types.d.ts +95 -0
  128. package/dist/storage/aws/clients/wasabi.d.ts +12 -0
  129. package/dist/storage/aws/index.d.ts +4 -0
  130. package/dist/storage/aws/index.js +3 -0
  131. package/dist/storage/aws/s3-base-storage.d.ts +248 -0
  132. package/dist/storage/aws/s3-client-adapter.d.ts +110 -0
  133. package/dist/storage/aws/s3-file.d.ts +10 -0
  134. package/dist/storage/aws/s3-meta-storage.d.ts +15 -0
  135. package/dist/storage/aws/s3-storage.d.ts +70 -0
  136. package/dist/storage/aws/types.d.ts +119 -0
  137. package/dist/storage/aws-light/aws-light-api-adapter.d.ts +130 -0
  138. package/dist/storage/aws-light/aws-light-file.d.ts +10 -0
  139. package/dist/storage/aws-light/aws-light-meta-storage.d.ts +19 -0
  140. package/dist/storage/aws-light/aws-light-storage.d.ts +65 -0
  141. package/dist/storage/aws-light/index.d.ts +4 -0
  142. package/dist/storage/aws-light/index.js +3 -0
  143. package/dist/storage/aws-light/types.d.ts +37 -0
  144. package/dist/storage/azure/azure-file.d.ts +6 -0
  145. package/dist/storage/azure/azure-meta-storage.d.ts +15 -0
  146. package/dist/storage/azure/azure-storage.d.ts +69 -0
  147. package/dist/storage/azure/index.d.ts +4 -0
  148. package/dist/storage/azure/index.js +3 -0
  149. package/dist/storage/azure/types.d.ts +62 -0
  150. package/dist/storage/gcs/fetch-error.d.ts +11 -0
  151. package/dist/storage/gcs/gcs-config.d.ts +2 -0
  152. package/dist/storage/gcs/gcs-file.d.ts +6 -0
  153. package/dist/storage/gcs/gcs-meta-storage.d.ts +27 -0
  154. package/dist/storage/gcs/gcs-storage.d.ts +91 -0
  155. package/dist/storage/gcs/index.d.ts +5 -0
  156. package/dist/storage/gcs/index.js +4 -0
  157. package/dist/storage/gcs/types.d.ts +55 -0
  158. package/dist/storage/gcs/utils.d.ts +7 -0
  159. package/dist/storage/local/disk-storage-with-checksum.d.ts +15 -0
  160. package/dist/storage/local/disk-storage.d.ts +135 -0
  161. package/dist/storage/local/index.d.ts +4 -0
  162. package/dist/storage/local/index.js +3 -0
  163. package/dist/storage/local/local-meta-storage.d.ts +32 -0
  164. package/dist/storage/meta-storage.d.ts +30 -0
  165. package/dist/storage/netlify-blob/index.d.ts +4 -0
  166. package/dist/storage/netlify-blob/index.js +3 -0
  167. package/dist/storage/netlify-blob/netlify-blob-file.d.ts +12 -0
  168. package/dist/storage/netlify-blob/netlify-blob-meta-storage.d.ts +7 -0
  169. package/dist/storage/netlify-blob/netlify-blob-storage.d.ts +93 -0
  170. package/dist/storage/netlify-blob/types.d.ts +30 -0
  171. package/dist/storage/storage.d.ts +353 -0
  172. package/dist/storage/types.d.ts +221 -0
  173. package/dist/storage/utils/file/file.d.ts +25 -0
  174. package/dist/storage/utils/file/get-file-status.d.ts +9 -0
  175. package/dist/storage/utils/file/has-content.d.ts +9 -0
  176. package/dist/storage/utils/file/index.d.ts +10 -0
  177. package/dist/storage/utils/file/is-expired.d.ts +8 -0
  178. package/dist/storage/utils/file/metadata.d.ts +18 -0
  179. package/dist/storage/utils/file/part-match.d.ts +11 -0
  180. package/dist/storage/utils/file/types.d.ts +37 -0
  181. package/dist/storage/utils/file/update-metadata.d.ts +10 -0
  182. package/dist/storage/utils/file/update-size.d.ts +10 -0
  183. package/dist/storage/vercel-blob/index.d.ts +4 -0
  184. package/dist/storage/vercel-blob/index.js +3 -0
  185. package/dist/storage/vercel-blob/types.d.ts +32 -0
  186. package/dist/storage/vercel-blob/vercel-blob-file.d.ts +16 -0
  187. package/dist/storage/vercel-blob/vercel-blob-meta-storage.d.ts +7 -0
  188. package/dist/storage/vercel-blob/vercel-blob-storage.d.ts +76 -0
  189. package/dist/transformer/audio-transformer.d.ts +128 -0
  190. package/dist/transformer/audio-transformer.js +278 -0
  191. package/dist/transformer/base-transformer.d.ts +63 -0
  192. package/dist/transformer/image-transformer.d.ts +571 -0
  193. package/dist/transformer/image-transformer.js +1100 -0
  194. package/dist/transformer/index.d.ts +5 -0
  195. package/dist/transformer/index.js +4 -0
  196. package/dist/transformer/media-transformer.d.ts +332 -0
  197. package/dist/transformer/types.d.ts +652 -0
  198. package/dist/transformer/utils.d.ts +33 -0
  199. package/dist/transformer/validation-error.d.ts +23 -0
  200. package/dist/transformer/video-transformer.d.ts +145 -0
  201. package/dist/transformer/video-transformer.js +310 -0
  202. package/dist/utils/cache.d.ts +32 -0
  203. package/dist/utils/chunked-upload.d.ts +65 -0
  204. package/dist/utils/detect-file-type.d.ts +28 -0
  205. package/dist/utils/errors.d.ts +74 -0
  206. package/dist/utils/file-path-url-matcher.d.ts +18 -0
  207. package/dist/utils/headers.d.ts +111 -0
  208. package/dist/utils/http.d.ts +92 -0
  209. package/dist/utils/locker.d.ts +26 -0
  210. package/dist/utils/pipes/stream-checksum.d.ts +47 -0
  211. package/dist/utils/pipes/stream-length.d.ts +22 -0
  212. package/dist/utils/primitives/get-last-one.d.ts +3 -0
  213. package/dist/utils/primitives/is-record.d.ts +2 -0
  214. package/dist/utils/primitives/map-values.d.ts +9 -0
  215. package/dist/utils/primitives/pick.d.ts +2 -0
  216. package/dist/utils/primitives/to-milliseconds.d.ts +12 -0
  217. package/dist/utils/primitives/to-seconds.d.ts +12 -0
  218. package/dist/utils/range-checksum.d.ts +33 -0
  219. package/dist/utils/range-hasher.d.ts +46 -0
  220. package/dist/utils/retry.d.ts +64 -0
  221. package/dist/utils/types.d.ts +108 -0
  222. package/dist/utils/validation-error.d.ts +22 -0
  223. package/dist/utils/validator.d.ts +36 -0
  224. package/package.json +5 -5
@@ -0,0 +1,93 @@
1
+ import type { IncomingMessage, ServerResponse } from "node:http";
2
+ import type { UploadFile } from "../../storage/utils/index.d.ts";
3
+ import BaseHandlerNode from "../base/base-handler-node.d.ts";
4
+ import type { Handlers, ResponseFile, ResponseList, UploadOptions } from "../types.d.ts";
5
+ /**
6
+ * REST API handler for direct binary file uploads (Node.js version).
7
+ *
8
+ * This handler provides a clean REST interface for file operations:
9
+ * - POST: Create a new file with raw binary data or initialize chunked upload
10
+ * - PUT: Create or update a file (requires ID in URL)
11
+ * - PATCH: Upload chunks for chunked uploads (requires ID in URL)
12
+ * - GET: Retrieve a file or list files
13
+ * - DELETE: Delete a file (single) or multiple files (via ?ids=id1,id2 or JSON body)
14
+ * - HEAD: Get file metadata and upload progress
15
+ * - OPTIONS: CORS preflight
16
+ * @example
17
+ * ```ts
18
+ * const rest = new Rest({
19
+ * storage,
20
+ * });
21
+ *
22
+ * app.use('/files', rest.handle);
23
+ * ```
24
+ */
25
+ declare class Rest<TFile extends UploadFile, NodeRequest extends IncomingMessage = IncomingMessage, NodeResponse extends ServerResponse = ServerResponse> extends BaseHandlerNode<TFile, NodeRequest, NodeResponse> {
26
+ /**
27
+ * Limiting enabled http method handler
28
+ */
29
+ static readonly methods: Handlers[];
30
+ private readonly restBase;
31
+ constructor(options: UploadOptions<TFile>);
32
+ /**
33
+ * Compose and register HTTP method handlers.
34
+ */
35
+ protected compose(): void;
36
+ /**
37
+ * Build file URL from request and file data.
38
+ * @param requestUrl Request URL string
39
+ * @param file File object containing ID and content type
40
+ * @returns Constructed file URL with extension based on content type
41
+ */
42
+ protected buildFileUrlForRest(requestUrl: string, file: TFile): string;
43
+ /**
44
+ * Creates a new file via POST request with raw binary data.
45
+ * Supports both full file uploads and chunked upload initialization.
46
+ * @param request Node.js IncomingMessage with file data.
47
+ * @returns Promise resolving to ResponseFile with upload result.
48
+ */
49
+ post(request: NodeRequest): Promise<ResponseFile<TFile>>;
50
+ /**
51
+ * Create or update a file via PUT request.
52
+ * Requires file ID in the URL path.
53
+ * @param request Node.js IncomingMessage with file ID and data
54
+ * @returns Promise resolving to ResponseFile with upload result
55
+ */
56
+ put(request: NodeRequest): Promise<ResponseFile<TFile>>;
57
+ /**
58
+ * Delete an uploaded file or multiple files.
59
+ * Supports single file (ID in URL) or batch delete (via ?ids=id1,id2 or JSON body).
60
+ * @param request Node.js IncomingMessage with file ID(s)
61
+ * @returns Promise resolving to ResponseFile (single) or ResponseList (batch) with deletion result
62
+ */
63
+ delete(request: NodeRequest): Promise<ResponseFile<TFile> | ResponseList<TFile>>;
64
+ /**
65
+ * Uploads a chunk via PATCH request for chunked uploads.
66
+ * Headers required: X-Chunk-Offset (byte offset), Content-Length (chunk size).
67
+ * Optional: X-Chunk-Checksum (SHA256 checksum for validation).
68
+ * @param request Node.js IncomingMessage with chunk data.
69
+ * @returns Promise resolving to ResponseFile with upload progress.
70
+ */
71
+ patch(request: NodeRequest): Promise<ResponseFile<TFile>>;
72
+ /**
73
+ * Get file metadata via HEAD request.
74
+ * For chunked uploads, also returns upload progress information.
75
+ * @param request Node.js IncomingMessage with file ID
76
+ * @returns Promise resolving to ResponseFile with metadata headers
77
+ */
78
+ head(request: NodeRequest): Promise<ResponseFile<TFile>>;
79
+ /**
80
+ * Handle OPTIONS requests with REST API capabilities.
81
+ * @returns Promise resolving to ResponseFile with CORS headers
82
+ */
83
+ options(): Promise<ResponseFile<TFile>>;
84
+ /**
85
+ * Retrieves a file or list of files based on the request path.
86
+ * Delegates to BaseHandlerNode.get() method.
87
+ * @param request Node.js IncomingMessage with optional originalUrl.
88
+ * @param response Node.js ServerResponse.
89
+ * @returns Promise resolving to a single file, paginated list, or array of files.
90
+ */
91
+ get(request: NodeRequest, response: NodeResponse): Promise<ResponseFile<TFile> | ResponseList<TFile>>;
92
+ }
93
+ export default Rest;
@@ -0,0 +1,152 @@
1
+ import type { Checksum, FileInit, UploadFile } from "../../storage/utils/index.d.ts";
2
+ import { Metadata } from "../../storage/utils/index.d.ts";
3
+ import type { Headers } from "../../utils/types.d.ts";
4
+ import type { ResponseFile } from "../types.d.ts";
5
+ /**
6
+ * Parse TUS protocol metadata string into object.
7
+ * @param encoded Base64-encoded metadata string (optional, defaults to empty string)
8
+ * @returns Parsed metadata object with decoded values
9
+ */
10
+ export declare const parseMetadata: (encoded?: string) => Metadata;
11
+ /**
12
+ * Serialize metadata object to TUS protocol format.
13
+ * @param object Metadata object to serialize
14
+ * @returns Base64-encoded metadata string in TUS format
15
+ */
16
+ export declare const serializeMetadata: (object: Metadata | Record<string, unknown> | undefined) => string;
17
+ /**
18
+ * Base class containing shared TUS protocol business logic.
19
+ * Platform-agnostic - contains no Node.js or Web API specific code.
20
+ * @template TFile The file type used by this handler.
21
+ */
22
+ export declare abstract class TusBase<TFile extends UploadFile> {
23
+ /**
24
+ * Storage instance for file operations.
25
+ */
26
+ protected get storage(): {
27
+ checkIfExpired: (file: TFile) => Promise<void>;
28
+ checksumTypes: string[];
29
+ config: {
30
+ useRelativeLocation?: boolean;
31
+ };
32
+ create: (config: FileInit) => Promise<TFile>;
33
+ delete: (options: {
34
+ id: string;
35
+ }) => Promise<TFile>;
36
+ getMeta: (id: string) => Promise<TFile>;
37
+ getStream?: (options: {
38
+ id: string;
39
+ }) => Promise<{
40
+ size?: number;
41
+ stream: unknown;
42
+ }>;
43
+ maxUploadSize: number;
44
+ tusExtension: string[];
45
+ update: (options: {
46
+ id: string;
47
+ }, updates: {
48
+ id?: string;
49
+ metadata?: Record<string, unknown>;
50
+ size?: number;
51
+ }) => Promise<TFile>;
52
+ write: (options: {
53
+ body: unknown;
54
+ checksum?: string;
55
+ checksumAlgorithm?: string;
56
+ contentLength: number;
57
+ id: string;
58
+ start: number;
59
+ }) => Promise<TFile>;
60
+ };
61
+ /**
62
+ * Whether to disable termination for finished uploads.
63
+ * Must be implemented by subclasses via getter.
64
+ */
65
+ protected abstract get disableTerminationForFinishedUploads(): boolean;
66
+ /**
67
+ * Build file URL from request URL and file data.
68
+ * @param _requestUrl Request URL string
69
+ * @param _file File object containing ID
70
+ * @returns Constructed file URL for TUS protocol
71
+ */
72
+ protected buildFileUrl(_requestUrl: string, _file: TFile): string;
73
+ /**
74
+ * Handle OPTIONS request with TUS protocol capabilities.
75
+ * @param methods Array of supported HTTP methods
76
+ * @returns ResponseFile with TUS headers
77
+ */
78
+ handleOptions(methods: string[]): ResponseFile<TFile>;
79
+ /**
80
+ * Handle TUS POST (create upload).
81
+ * @param uploadLength Upload length header value
82
+ * @param uploadDeferLength Upload defer length header value
83
+ * @param uploadConcat Upload concat header value
84
+ * @param metadataHeader Upload metadata header value
85
+ * @param requestUrl Request URL for Location header
86
+ * @param bodyStream Request body stream (for creation-with-upload)
87
+ * @param contentLength Content length (for creation-with-upload)
88
+ * @param contentType Content type (for creation-with-upload)
89
+ * @returns Promise resolving to ResponseFile with upload result
90
+ */
91
+ handlePost(uploadLength: string | undefined, uploadDeferLength: string | undefined, uploadConcat: string | undefined, metadataHeader: string | undefined, requestUrl: string, bodyStream: unknown, contentLength: number, contentType: string): Promise<ResponseFile<TFile>>;
92
+ /**
93
+ * Handle TUS PATCH (write chunk).
94
+ * @param id File ID from URL
95
+ * @param uploadOffset Upload offset header value
96
+ * @param uploadLength Optional upload length header value (for defer-length)
97
+ * @param metadataHeader Optional upload metadata header value
98
+ * @param checksum Optional checksum
99
+ * @param checksumAlgorithm Optional checksum algorithm
100
+ * @param requestUrl Request URL for Location header
101
+ * @param bodyStream Request body stream
102
+ * @param contentLength Content length
103
+ * @returns Promise resolving to ResponseFile with upload progress
104
+ */
105
+ handlePatch(id: string, uploadOffset: number, uploadLength: string | undefined, metadataHeader: string | undefined, checksum: string | undefined, checksumAlgorithm: string | undefined, _requestUrl: string, bodyStream: unknown, contentLength: number): Promise<ResponseFile<TFile>>;
106
+ /**
107
+ * Handle TUS HEAD (get upload status).
108
+ * @param id File ID from URL
109
+ * @returns Promise resolving to ResponseFile with upload status headers
110
+ */
111
+ handleHead(id: string): Promise<ResponseFile<TFile>>;
112
+ /**
113
+ * Handle TUS GET (get upload metadata).
114
+ * @param id File ID from URL
115
+ * @returns Promise resolving to ResponseFile with file metadata as JSON
116
+ */
117
+ handleGet(id: string): Promise<ResponseFile<TFile>>;
118
+ /**
119
+ * Handle TUS DELETE (terminate upload).
120
+ * @param id File ID from URL
121
+ * @returns Promise resolving to ResponseFile with deletion confirmation
122
+ */
123
+ handleDelete(id: string): Promise<ResponseFile<TFile>>;
124
+ /**
125
+ * Build TUS protocol headers including required Tus-Resumable and optional Upload-Expires.
126
+ * @param file Upload file object with metadata
127
+ * @param headers Additional headers to include
128
+ * @returns Headers object with TUS protocol headers
129
+ */
130
+ protected buildHeaders(file: UploadFile, headers?: Headers): Headers;
131
+ /**
132
+ * Extract checksum algorithm and value from Upload-Checksum header.
133
+ * @param checksumHeader Upload-Checksum header value
134
+ * @returns Object containing checksum algorithm and value
135
+ */
136
+ extractChecksum(checksumHeader: string | undefined): Checksum;
137
+ /**
138
+ * Validate Tus-Resumable header value.
139
+ * @param tusResumable Tus-Resumable header value
140
+ * @throws {Error} 412 if version doesn't match or header is missing
141
+ */
142
+ validateTusResumableHeader(tusResumable: string | undefined): void;
143
+ /**
144
+ * Concatenate partial uploads into a final upload.
145
+ * @param finalFile Final file that will contain concatenated content
146
+ * @param partialFiles Array of partial upload files to concatenate
147
+ * @returns Promise resolving when concatenation is complete
148
+ */
149
+ protected concatenateFiles(finalFile: TFile, partialFiles: TFile[]): Promise<void>;
150
+ }
151
+ export declare const TUS_RESUMABLE: string;
152
+ export declare const TUS_VERSION: string;
@@ -0,0 +1,72 @@
1
+ import type { UploadFile } from "../../storage/utils/index.d.ts";
2
+ import BaseHandlerFetch from "../base/base-handler-fetch.d.ts";
3
+ import type { Handlers, ResponseFile, UploadOptions } from "../types.d.ts";
4
+ export { TUS_RESUMABLE, TUS_VERSION } from "./tus-base.d.ts";
5
+ /**
6
+ * TUS resumable upload protocol handler (Web API Fetch version).
7
+ *
8
+ * [tus resumable upload protocol](https://github.com/tus/tus-resumable-upload-protocol/blob/master/protocol.md)
9
+ * @example
10
+ * ```ts
11
+ * const tus = new TusFetch({storage});
12
+ *
13
+ * // Use with Hono, Cloudflare Workers, etc.
14
+ * app.all('/files/*', async (c) => {
15
+ * return tus.fetch(c.req.raw);
16
+ * });
17
+ * ```
18
+ */
19
+ export declare class Tus<TFile extends UploadFile> extends BaseHandlerFetch<TFile> {
20
+ /**
21
+ * Limiting enabled http method handler
22
+ */
23
+ static readonly methods: Handlers[];
24
+ private readonly tusBase;
25
+ constructor(options: UploadOptions<TFile>);
26
+ /**
27
+ * Handle OPTIONS requests with TUS protocol capabilities.
28
+ * @returns Promise resolving to ResponseFile with TUS headers
29
+ */
30
+ options(): Promise<ResponseFile<TFile>>;
31
+ /**
32
+ * Creates a new TUS upload and optionally starts uploading data.
33
+ * @param request Web API Request with TUS headers.
34
+ * @returns Promise resolving to ResponseFile with upload location and offset.
35
+ */
36
+ post(request: Request): Promise<ResponseFile<TFile>>;
37
+ /**
38
+ * Write a chunk of data to an existing TUS upload.
39
+ * @param request Web API Request with chunk data and TUS headers
40
+ * @returns Promise resolving to ResponseFile with updated offset
41
+ */
42
+ patch(request: Request): Promise<ResponseFile<TFile>>;
43
+ /**
44
+ * Get current upload offset and metadata for TUS resumable uploads.
45
+ * @param request Web API Request with upload ID
46
+ * @returns Promise resolving to ResponseFile with upload-offset and metadata headers
47
+ */
48
+ head(request: Request): Promise<ResponseFile<TFile>>;
49
+ /**
50
+ * Get TUS upload metadata and current status.
51
+ * @param request Web API Request with upload ID
52
+ * @returns Promise resolving to ResponseFile with file metadata as JSON
53
+ */
54
+ get(request: Request): Promise<ResponseFile<TFile>>;
55
+ /**
56
+ * Delete a TUS upload and its associated data.
57
+ * @param request Web API Request with upload ID
58
+ * @returns Promise resolving to ResponseFile with deletion confirmation
59
+ */
60
+ delete(request: Request): Promise<ResponseFile<TFile>>;
61
+ /**
62
+ * Compose and register HTTP method handlers.
63
+ */
64
+ protected compose(): void;
65
+ /**
66
+ * Build file URL for TUS uploads (without file extension).
67
+ * @param requestUrl Request URL string
68
+ * @param file File object containing ID
69
+ * @returns Constructed file URL for TUS protocol
70
+ */
71
+ protected buildFileUrlForTus(requestUrl: string, file: TFile): string;
72
+ }
@@ -0,0 +1,78 @@
1
+ import type { IncomingMessage, ServerResponse } from "node:http";
2
+ import type { UploadFile } from "../../storage/utils/index.d.ts";
3
+ import type { UploadResponse } from "../../utils/types.d.ts";
4
+ import BaseHandlerNode from "../base/base-handler-node.d.ts";
5
+ import type { Handlers, ResponseFile, UploadOptions } from "../types.d.ts";
6
+ export { TUS_RESUMABLE, TUS_VERSION } from "./tus-base.d.ts";
7
+ /**
8
+ * TUS resumable upload protocol handler (Node.js version).
9
+ *
10
+ * [tus resumable upload protocol](https://github.com/tus/tus-resumable-upload-protocol/blob/master/protocol.md)
11
+ * @example
12
+ * ```ts
13
+ * const tus = new Tus({storage});
14
+ *
15
+ * app.all('/files', tus.handle);
16
+ * ```
17
+ */
18
+ export declare class Tus<TFile extends UploadFile, NodeRequest extends IncomingMessage = IncomingMessage, NodeResponse extends ServerResponse = ServerResponse> extends BaseHandlerNode<TFile, NodeRequest, NodeResponse> {
19
+ /**
20
+ * Limiting enabled http method handler
21
+ */
22
+ static readonly methods: Handlers[];
23
+ disableTerminationForFinishedUploads: boolean;
24
+ private readonly tusBase;
25
+ constructor(options: UploadOptions<TFile>);
26
+ /**
27
+ * Handle OPTIONS requests with TUS protocol capabilities.
28
+ * @returns Promise resolving to ResponseFile with TUS headers
29
+ */
30
+ options(): Promise<ResponseFile<TFile>>;
31
+ /**
32
+ * Creates a new TUS upload and optionally starts uploading data.
33
+ * @param request Node.js IncomingMessage with TUS headers.
34
+ * @returns Promise resolving to ResponseFile with upload location and offset.
35
+ */
36
+ post(request: NodeRequest): Promise<ResponseFile<TFile>>;
37
+ /**
38
+ * Write a chunk of data to an existing TUS upload.
39
+ * @param request Node.js IncomingMessage with chunk data and TUS headers
40
+ * @returns Promise resolving to ResponseFile with updated offset
41
+ */
42
+ patch(request: NodeRequest): Promise<ResponseFile<TFile>>;
43
+ /**
44
+ * Get current upload offset and metadata for TUS resumable uploads.
45
+ * @param request Node.js IncomingMessage with upload ID
46
+ * @returns Promise resolving to ResponseFile with upload-offset and metadata headers
47
+ */
48
+ head(request: NodeRequest): Promise<ResponseFile<TFile>>;
49
+ /**
50
+ * Get TUS upload metadata and current status.
51
+ * @param request Node.js IncomingMessage with upload ID
52
+ * @returns Promise resolving to ResponseFile with file metadata as JSON
53
+ */
54
+ get(request: NodeRequest): Promise<ResponseFile<TFile>>;
55
+ /**
56
+ * Delete a TUS upload and its associated data.
57
+ * @param request Node.js IncomingMessage with upload ID
58
+ * @returns Promise resolving to ResponseFile with deletion confirmation
59
+ */
60
+ delete(request: NodeRequest): Promise<ResponseFile<TFile>>;
61
+ /**
62
+ * Send TUS protocol response with required headers.
63
+ * @param response Node.js ServerResponse to send response to
64
+ * @param uploadResponse Response data with body, headers, and status code
65
+ */
66
+ send(response: NodeResponse, { body, headers, statusCode }: UploadResponse): void;
67
+ /**
68
+ * Compose and register HTTP method handlers.
69
+ */
70
+ protected compose(): void;
71
+ /**
72
+ * Build file URL for TUS uploads (without file extension).
73
+ * @param requestUrl Request URL string
74
+ * @param file File object containing ID
75
+ * @returns Constructed file URL for TUS protocol
76
+ */
77
+ protected buildFileUrlForTus(requestUrl: string, file: TFile): string;
78
+ }
@@ -0,0 +1,53 @@
1
+ import type { EventEmitter } from "node:events";
2
+ import type { IncomingMessage } from "node:http";
3
+ import type { Readable } from "node:stream";
4
+ import type { PaginationResult } from "@visulima/pagination";
5
+ import type { BaseStorage } from "../storage/storage.d.ts";
6
+ import type { UploadEventType, UploadFile } from "../storage/utils/index.d.ts";
7
+ import type MediaTransformer from "../transformer/media-transformer.d.ts";
8
+ import type { UploadError } from "../utils/errors.d.ts";
9
+ type WebRequest = globalThis.Request;
10
+ type WebResponse = globalThis.Response;
11
+ interface BaseResponse {
12
+ headers: Record<string, number | string>;
13
+ statusCode: number;
14
+ }
15
+ export interface StreamingResponse extends BaseResponse {
16
+ /** Total size of the stream (for Content-Length header) */
17
+ size?: number;
18
+ /** Stream of data to send */
19
+ stream: Readable;
20
+ }
21
+ export interface RequestEvent {
22
+ request: Pick<IncomingMessage, "headers" | "method" | "url">;
23
+ }
24
+ export type AsyncHandler<Request, Response> = (request: Request, response: Response) => Promise<void | ResponseFile<UploadFile> | ResponseList<UploadFile> | StreamingResponse>;
25
+ export type Handlers = "delete" | "download" | "get" | "head" | "options" | "patch" | "post" | "put";
26
+ export type MethodHandler<Request, Response> = {
27
+ [h in Handlers]?: AsyncHandler<Request, Response>;
28
+ };
29
+ export type UploadEvent<TFile extends UploadFile> = RequestEvent & TFile;
30
+ export type UploadErrorEvent = RequestEvent & UploadError;
31
+ export type ResponseFile<TFile extends UploadFile> = BaseResponse & TFile & {
32
+ /** Optional stream for streaming responses instead of content buffer */
33
+ stream?: Readable;
34
+ };
35
+ export type ResponseList<TFile extends UploadFile> = BaseResponse & {
36
+ data: PaginationResult<TFile> | TFile[];
37
+ };
38
+ export interface BaseHandler<TFile extends UploadFile> extends EventEmitter {
39
+ emit: ((event: "error", error: UploadErrorEvent) => boolean) & ((event: UploadEventType, payload: UploadEvent<TFile>) => boolean);
40
+ fetch: (request: WebRequest) => Promise<WebResponse>;
41
+ off: ((event: "error", listener: (error: UploadErrorEvent) => void) => this) & ((event: UploadEventType, listener: (payload: UploadEvent<TFile>) => void) => this);
42
+ on: ((event: "error", listener: (error: UploadErrorEvent) => void) => this) & ((event: UploadEventType, listener: (payload: UploadEvent<TFile>) => void) => this);
43
+ }
44
+ export interface UploadOptions<TFile extends UploadFile> {
45
+ disableTerminationForFinishedUploads?: boolean;
46
+ /** Maximum file size for multipart parser (default: min(storage.maxUploadSize, 1GB)) */
47
+ maxFileSize?: number;
48
+ /** Maximum header size for multipart parser (default: 64KB) */
49
+ maxHeaderSize?: number;
50
+ mediaTransformer?: MediaTransformer;
51
+ storage: BaseStorage<TFile>;
52
+ }
53
+ export {};
@@ -0,0 +1,72 @@
1
+ import type { IncomingMessage } from "node:http";
2
+ import type { FileInit } from "../../storage/utils/index.d.ts";
3
+ /**
4
+ * Parses metadata from X-File-Metadata header.
5
+ * @param request The HTTP request
6
+ * @param existingMetadata Existing metadata to merge with (optional)
7
+ * @returns Parsed metadata object
8
+ */
9
+ export declare const parseMetadata: (request: IncomingMessage, existingMetadata?: Record<string, unknown>) => Record<string, unknown>;
10
+ /**
11
+ * Parses filename from Content-Disposition header value string.
12
+ * Uses a safer parsing approach to avoid ReDoS vulnerabilities.
13
+ * @param contentDisposition The Content-Disposition header value
14
+ * @returns Filename if found, undefined otherwise
15
+ */
16
+ export declare const parseContentDispositionValue: (contentDisposition: string | null | undefined) => string | undefined;
17
+ /**
18
+ * Parses filename from Content-Disposition header.
19
+ * Uses a safer parsing approach to avoid ReDoS vulnerabilities.
20
+ * @param request The HTTP request
21
+ * @returns Filename if found, undefined otherwise
22
+ */
23
+ export declare const parseContentDisposition: (request: IncomingMessage) => string | undefined;
24
+ /**
25
+ * Parses chunked upload headers (X-Chunk-Offset, X-Total-Size, etc.).
26
+ * @param request The HTTP request
27
+ * @returns Object with chunk offset, total size, and chunked upload flag
28
+ */
29
+ export declare const parseChunkHeaders: (request: IncomingMessage) => {
30
+ chunkOffset?: number;
31
+ isChunkedUpload: boolean;
32
+ totalSize?: number;
33
+ };
34
+ /**
35
+ * Validates that a request has a body.
36
+ * @param request The HTTP request
37
+ * @param allowEmptyForChunked Whether to allow empty body for chunked uploads
38
+ * @throws {HttpError} If body is required but missing
39
+ */
40
+ export declare const validateRequestBody: (request: IncomingMessage, allowEmptyForChunked?: boolean) => void;
41
+ /**
42
+ * Validates Content-Length header.
43
+ * @param request The HTTP request
44
+ * @param allowZeroForChunked Whether to allow zero length for chunked uploads
45
+ * @param maxSize Maximum allowed size
46
+ * @returns Parsed content length
47
+ * @throws {HttpError} If Content-Length is invalid or exceeds max size
48
+ */
49
+ export declare const validateContentLength: (request: IncomingMessage, allowZeroForChunked?: boolean, maxSize?: number) => number;
50
+ /**
51
+ * Extracts file initialization config from request headers.
52
+ * @param request The HTTP request
53
+ * @param contentLength The content length (already validated)
54
+ * @param contentType The content type (default: application/octet-stream)
55
+ * @returns FileInit configuration object
56
+ */
57
+ export declare const extractFileInit: (request: IncomingMessage, contentLength: number, contentType?: string) => FileInit;
58
+ /**
59
+ * Validates chunk offset and size for chunked uploads.
60
+ * @param chunkOffset The chunk offset
61
+ * @param contentLength The chunk content length
62
+ * @param totalSize The total file size
63
+ * @throws {HttpError} If validation fails
64
+ */
65
+ export declare const validateChunk: (chunkOffset: number, contentLength: number, totalSize: number) => void;
66
+ /**
67
+ * Gets content type from request headers with fallback.
68
+ * @param request The HTTP request
69
+ * @param fallback Default content type if not found (default: application/octet-stream)
70
+ * @returns Content type string
71
+ */
72
+ export declare const getContentType: (request: IncomingMessage, fallback?: string) => string;
@@ -0,0 +1,83 @@
1
+ import type { UploadFile } from "../../storage/utils/index.d.ts";
2
+ import type { Header } from "../../utils/types.d.ts";
3
+ import type { ResponseFile } from "../types.d.ts";
4
+ /**
5
+ * Builds a standard ResponseFile from a file object and headers.
6
+ * @param file The file object to convert to a response
7
+ * @param headers Additional headers to include
8
+ * @param statusCode HTTP status code (default: 200)
9
+ * @returns ResponseFile with file data and headers
10
+ */
11
+ export declare const buildResponseFile: <TFile extends UploadFile>(file: TFile, headers?: Record<string, string | number>, statusCode?: number) => ResponseFile<TFile>;
12
+ /**
13
+ * Builds standard file headers including Location, expiration, and ETag.
14
+ * @param file The file object
15
+ * @param locationUrl The Location header URL
16
+ * @param additionalHeaders Additional headers to merge in
17
+ * @returns Headers object with standard file headers
18
+ */
19
+ export declare const buildFileHeaders: <TFile extends UploadFile>(file: TFile, locationUrl: string, additionalHeaders?: Record<string, string | number>) => Record<string, string | number>;
20
+ /**
21
+ * Builds chunked upload headers for progress tracking.
22
+ * @param file The file object with chunked upload metadata
23
+ * @param isComplete Whether the upload is complete
24
+ * @returns Headers object with chunked upload progress headers
25
+ */
26
+ export declare const buildChunkedUploadHeaders: <TFile extends UploadFile>(file: TFile, isComplete: boolean) => Record<string, string | number>;
27
+ /**
28
+ * Builds a Location header URL for a file.
29
+ * @param requestUrl The request URL or originalUrl
30
+ * @param fileId The file ID
31
+ * @param contentType The file content type (for extension)
32
+ * @param useRelativeLocation Whether to use relative URLs
33
+ * @param baseUrl The base URL (for absolute URLs)
34
+ * @returns Location URL string
35
+ */
36
+ export declare const buildLocationHeader: (requestUrl: string | undefined, fileId: string, contentType: string, useRelativeLocation: boolean, baseUrl?: string) => string;
37
+ /**
38
+ * Builds standard file metadata headers for HEAD requests.
39
+ * @param file The file object
40
+ * @returns Headers object with file metadata
41
+ */
42
+ export declare const buildFileMetadataHeaders: <TFile extends UploadFile>(file: TFile) => Record<string, string | number>;
43
+ /**
44
+ * Converts headers from Record format to string format, handling arrays.
45
+ * @param headers Headers object that may contain arrays
46
+ * @returns Headers object with all values as strings
47
+ */
48
+ export declare const convertHeadersToString: (headers: Record<string, Header>) => Record<string, string>;
49
+ /**
50
+ * Builds an error response body in standard format.
51
+ * @param error The error object
52
+ * @returns Error response body object
53
+ */
54
+ export declare const buildErrorResponseBody: (error: {
55
+ code?: string;
56
+ message?: string;
57
+ name?: string;
58
+ }) => {
59
+ error: {
60
+ code: string;
61
+ message: string;
62
+ name: string;
63
+ };
64
+ };
65
+ /**
66
+ * Prepares response body and headers for sending, handling different body types.
67
+ * @param body The response body (string, Buffer, or object)
68
+ * @param headers Existing headers
69
+ * @returns Object with prepared data and headers
70
+ */
71
+ export declare const prepareResponseBody: (body: string | Buffer | object | undefined, headers?: Record<string, string | number>) => {
72
+ data: Buffer | string;
73
+ headers: Record<string, string | number>;
74
+ };
75
+ /**
76
+ * Cleans file data for serialization by removing non-serializable properties.
77
+ * @param fileData File data object
78
+ * @returns Cleaned file data object
79
+ */
80
+ export declare const cleanFileData: <TFile extends UploadFile>(fileData: TFile & {
81
+ content?: unknown;
82
+ stream?: unknown;
83
+ }) => Omit<TFile, "content" | "stream">;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Wait for storage to be ready before handling requests.
3
+ * This ensures storage initialization (e.g., AWS S3, GCS) completes before processing uploads.
4
+ * @param storage Storage instance with isReady property
5
+ * @param timeoutMs Maximum time to wait in milliseconds (default: 5000)
6
+ * @throws Error if storage doesn't become ready within timeout
7
+ */
8
+ export declare const waitForStorage: (storage: {
9
+ isReady: boolean;
10
+ }, timeoutMs?: number) => Promise<void>;
@@ -0,0 +1,29 @@
1
+ import type { ServerResponse } from "node:http";
2
+ import type { Readable } from "node:stream";
3
+ /**
4
+ * Creates a range-limited stream that properly handles backpressure.
5
+ * @param sourceStream Source readable stream to limit
6
+ * @param start Start byte position (inclusive)
7
+ * @param end End byte position (inclusive)
8
+ * @returns New readable stream limited to the specified byte range
9
+ */
10
+ export declare const createRangeLimitedStream: (sourceStream: Readable, start: number, end: number) => Readable;
11
+ /**
12
+ * Pipes streams with proper backpressure handling and error management.
13
+ * @param source Source readable stream to pipe from
14
+ * @param destination Destination response stream to pipe to
15
+ * @param sendError Function to send error responses
16
+ */
17
+ export declare const pipeWithBackpressure: <TResponse extends ServerResponse>(source: Readable, destination: TResponse, sendError: (response: TResponse, error: Error) => Promise<void>) => void;
18
+ /**
19
+ * Creates a stream response configuration object.
20
+ * @param stream The readable stream
21
+ * @param size Optional total size of the stream
22
+ * @param headers Optional headers to include
23
+ * @returns Stream response configuration
24
+ */
25
+ export declare const createStreamResponse: (stream: Readable, size?: number, headers?: Record<string, string | number>) => {
26
+ headers: Record<string, string | number>;
27
+ size?: number;
28
+ stream: Readable;
29
+ };