@heilgar/file-storage-adapter-core 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.
@@ -0,0 +1,40 @@
1
+ import type { DownloadOptions, FileMetadata, FileObject, FileStorageAdapter, FileStorageAdapterConfig, ListOptions, ListResult, SignedUrlOptions, SignedUrlUploadResult, UploadOptions } from './types';
2
+ export declare abstract class BaseAdapter implements FileStorageAdapter {
3
+ protected config: FileStorageAdapterConfig;
4
+ constructor(config?: FileStorageAdapterConfig);
5
+ /**
6
+ * Prepends the base path to the given key if a base path is configured.
7
+ *
8
+ * @param key - The original file key
9
+ *
10
+ * @returns The full file key with base path prepended if applicable
11
+ */
12
+ protected getFullKey(key: string): string;
13
+ /**
14
+ * Removes the base path from the given full key if a base path is configured.
15
+ *
16
+ * @param fullKey - The full file fullKey
17
+ *
18
+ * @returns The file key without the base path if applicable
19
+ */
20
+ protected stripBasePath(fullKey: string): string;
21
+ /**
22
+ * Converts various file input types to a Buffer.
23
+ *
24
+ * @param file - The file input (Buffer, Stream, or File)
25
+ *
26
+ * @returns A Promise that resolves to a Buffer
27
+ */
28
+ protected toBuffer(file: Buffer | NodeJS.ReadableStream | File): Promise<Buffer>;
29
+ abstract upload(key: string, file: Buffer | NodeJS.ReadableStream | File, options?: UploadOptions): Promise<FileMetadata>;
30
+ abstract download(key: string, options?: DownloadOptions): Promise<FileObject>;
31
+ abstract getMetadata(key: string): Promise<FileMetadata | null>;
32
+ abstract delete(key: string): Promise<boolean>;
33
+ abstract exists(key: string): Promise<boolean>;
34
+ abstract list(options?: ListOptions): Promise<ListResult>;
35
+ abstract getSignedUrl(key: string, options: SignedUrlOptions): Promise<string>;
36
+ abstract getSignedUrlUpload(key: string, options: SignedUrlOptions): Promise<SignedUrlUploadResult>;
37
+ abstract copy(sourceKey: string, destinationKey: string): Promise<FileMetadata>;
38
+ abstract move(sourceKey: string, destinationKey: string): Promise<FileMetadata>;
39
+ }
40
+ //# sour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O,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IACzD,QAAQ,CAAC,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9E,QAAQ,CAAC,kBAAkB,CACzB,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,qBAAqB,CAAC;IACjC,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAC/E,QAAQ,CAAC,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CAChF"}
@@ -0,0 +1,54 @@
1
+ import { join } from 'node:path';
2
+ export class BaseAdapter {
3
+ constructor(config = {}) {
4
+ this.config = config;
5
+ }
6
+ /**
7
+ * Prepends the base path to the given key if a base path is configured.
8
+ *
9
+ * @param key - The original file key
10
+ *
11
+ * @returns The full file key with base path prepended if applicable
12
+ */
13
+ getFullKey(key) {
14
+ if (this.config.basePath) {
15
+ return join(this.config.basePath, key);
16
+ }
17
+ return key;
18
+ }
19
+ /**
20
+ * Removes the base path from the given full key if a base path is configured.
21
+ *
22
+ * @param fullKey - The full file fullKey
23
+ *
24
+ * @returns The file key without the base path if applicable
25
+ */
26
+ stripBasePath(fullKey) {
27
+ if (this.config.basePath && fullKey.startsWith(this.config.basePath)) {
28
+ return fullKey.slice(this.config.basePath.length + 1);
29
+ }
30
+ return fullKey;
31
+ }
32
+ /**
33
+ * Converts various file input types to a Buffer.
34
+ *
35
+ * @param file - The file input (Buffer, Stream, or File)
36
+ *
37
+ * @returns A Promise that resolves to a Buffer
38
+ */
39
+ async toBuffer(file) {
40
+ if (Buffer.isBuffer(file)) {
41
+ return file;
42
+ }
43
+ if ('stream' in file && typeof file.stream === 'function') {
44
+ const arrayBuffer = await file.arrayBuffer();
45
+ return Buffer.from(arrayBuffer);
46
+ }
47
+ const chunks = [];
48
+ for await (const chunk of file) {
49
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
50
+ }
51
+ return Buffer.concat(chunks);
52
+ }
53
+ }
54
+ //# sourceMappingURL=base-adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-adapter.js","sourceRoot":"","sources":["../src/base-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAcjC,MAAM,OAAgB,WAAW;IAC/B,YAAsB,SAAmC,EAAE;QAArC,WAAM,GAAN,MAAM,CAA+B;IAAG,CAAC;IAE/D;;;;;;OAMG;IACO,UAAU,CAAC,GAAW;QAC9B,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;YACzB,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QACzC,CAAC;QAED,OAAO,GAAG,CAAC;IACb,CAAC;IAED;;;;;;OAMG;IACO,aAAa,CAAC,OAAe;QACrC,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;YACrE,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACxD,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;IACO,KAAK,CAAC,QAAQ,CAAC,IAA2C;QAClE,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAI,QAAQ,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;YAC1D,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;YAC7C,OAAO,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAClC,CAAC;QAED,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,IAA6B,EAAE,CAAC;YACxD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QACnE,CAAC;QAED,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;CAmBF"}
@@ -0,0 +1,3 @@
1
+ export * from './types';
2
+ export * from './base-adapter';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './types';
2
+ export * from './base-adapter';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,133 @@
1
+ export interface FileMetadata {
2
+ name: string;
3
+ mimeType: string;
4
+ size: number;
5
+ uploadedAt: Date;
6
+ metadata?: Record<string, any>;
7
+ }
8
+ export interface FileObject extends FileMetadata {
9
+ content: Buffer;
10
+ }
11
+ export interface UploadOptions {
12
+ contentType?: string;
13
+ cacheControl?: string;
14
+ path?: string;
15
+ metadata?: Record<string, any>;
16
+ }
17
+ export interface ListOptions {
18
+ prefix?: string;
19
+ limit?: number;
20
+ cursor?: string;
21
+ }
22
+ export interface ListResult {
23
+ files: FileMetadata[];
24
+ nextCursor?: string;
25
+ hasMore: boolean;
26
+ }
27
+ export interface DownloadOptions {
28
+ range?: {
29
+ start: number;
30
+ end: number;
31
+ };
32
+ }
33
+ export interface SignedUrlOptions {
34
+ expiresIn: number;
35
+ contentType?: string;
36
+ }
37
+ export interface SignedUrlUploadResult {
38
+ url: string;
39
+ headers?: Record<string, string>;
40
+ }
41
+ export interface FileStorageAdapterConfig {
42
+ basePath?: string;
43
+ }
44
+ export interface FileStorageAdapter {
45
+ /**
46
+ * Uploads a file to the storage system.
47
+ *
48
+ * @param key - unique identifier for the file
49
+ * @param file - File content (Buffer, Stream, or File)
50
+ * @param options - Upload options
51
+ *
52
+ * @returns FileMetadata
53
+ */
54
+ upload(key: string, file: Buffer | NodeJS.ReadableStream | File, options?: UploadOptions): Promise<FileMetadata>;
55
+ /**
56
+ * Downloads a file from the storage system.
57
+ *
58
+ * @param key - File identifier
59
+ * @param options - Download options
60
+ *
61
+ * @returns FileObject
62
+ */
63
+ download(key: string, options?: DownloadOptions): Promise<FileObject>;
64
+ /**
65
+ * Retrieves metadata for a file.
66
+ *
67
+ * @param key - File identifier
68
+ *
69
+ * @returns void
70
+ */
71
+ getMetadata(key: string): Promise<FileMetadata | null>;
72
+ /**
73
+ * Deletes a file from the storage system.
74
+ *
75
+ * @param key - File identifier
76
+ *
77
+ * @returns boolean
78
+ */
79
+ delete(key: string): Promise<boolean>;
80
+ /**
81
+ * Checks if a file exists in the storage system.
82
+ *
83
+ * @param key - File identifier
84
+ *
85
+ * @returns boolean
86
+ */
87
+ exists(key: string): Promise<boolean>;
88
+ /**
89
+ * Lists files in the storage system.
90
+ *
91
+ * @param options - List options
92
+ *
93
+ * @returns ListResult
94
+ */
95
+ list(options?: ListOptions): Promise<ListResult>;
96
+ /**
97
+ * Generates a signed URL for accessing a file.
98
+ *
99
+ * @param key - File identifier
100
+ * @param options - Signed URL options
101
+ *
102
+ * @returns string - Signed URL
103
+ */
104
+ getSignedUrl(key: string, options: SignedUrlOptions): Promise<string>;
105
+ /**
106
+ * Generates a signed URL for uploading a file.
107
+ *
108
+ * @param key - File identifier
109
+ * @param options - Signed URL options
110
+ *
111
+ * @returns object - Signed URL and optional headers
112
+ */
113
+ getSignedUrlUpload(key: string, options: SignedUrlOptions): Promise<SignedUrlUploadResult>;
114
+ /**
115
+ * Copies a file within the storage system.
116
+ *
117
+ * @param sourceKey - Source file identifier
118
+ * @param destinationKey - Destination file identifier
119
+ *
120
+ * @returns FileMetadata
121
+ */
122
+ copy(sourceKey: string, destinationKey: string): Promise<FileMetadata>;
123
+ /**
124
+ * Moves a file within the storage system.
125
+ *
126
+ * @param sourceKey - Source file identifier
127
+ * @param destinationKey - Destination file identifier
128
+ *
129
+ * @returns FileMetadata
130
+ */
131
+ move(sourceKey: string, destinationKey: string): Promise<FileMetadata>;
132
+ }
133
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,IAAI,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,UAAW,SAAQ,YAAY;IAC9C,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,aAAa;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC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C,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAEjD;;;;;;;OAOG;IACH,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEtE;;;;;;;OAOG;IACH,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAE3F;;;;;;;OAOG;IACH,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAEvE;;;;;;;OAOG;IACH,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CACxE"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@heilgar/file-storage-adapter-core",
3
+ "version": "1.0.0",
4
+ "description": "Core types and interfaces for storage adapters",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc --build",
20
+ "test": "vitest run --config ../../vitest.config.js",
21
+ "test:watch": "vitest --config ../../vitest.config.js"
22
+ },
23
+ "keywords": [
24
+ "storage",
25
+ "adapter"
26
+ ],
27
+ "author": "Oleh Hrebeniuk",
28
+ "license": "MIT",
29
+ "devDependencies": {
30
+ "@types/node": "^25.0.3",
31
+ "typescript": "^5.9.3",
32
+ "vitest": "^4.0.16"
33
+ }
34
+ }