@devbro/neko-storage 0.1.2 → 0.1.4
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/dist/Storage.d.mts +13 -13
- package/dist/Storage.mjs +30 -8
- package/dist/Storage.mjs.map +1 -1
- package/dist/StorageProviderFactory.d.mts +14 -0
- package/dist/StorageProviderFactory.mjs +19 -0
- package/dist/StorageProviderFactory.mjs.map +1 -0
- package/dist/StorageProviderInterface.d.mts +17 -0
- package/dist/StorageProviderInterface.mjs +1 -0
- package/dist/StorageProviderInterface.mjs.map +1 -0
- package/dist/index.d.mts +5 -3
- package/dist/index.js +181 -229
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -3
- package/dist/index.mjs.map +1 -1
- package/dist/{AWSS3Storage.d.mts → providers/AWSS3StorageProvider.d.mts} +4 -5
- package/dist/providers/AWSS3StorageProvider.mjs +108 -0
- package/dist/providers/AWSS3StorageProvider.mjs.map +1 -0
- package/dist/{LocalStorage.d.mts → providers/LocalStorageProvider.d.mts} +6 -6
- package/dist/providers/LocalStorageProvider.mjs +84 -0
- package/dist/providers/LocalStorageProvider.mjs.map +1 -0
- package/package.json +3 -4
- package/dist/AWSS3Storage.mjs +0 -154
- package/dist/AWSS3Storage.mjs.map +0 -1
- package/dist/LocalStorage.mjs +0 -127
- package/dist/LocalStorage.mjs.map +0 -1
- package/dist/StorageFactory.d.mts +0 -13
- package/dist/StorageFactory.mjs +0 -24
- package/dist/StorageFactory.mjs.map +0 -1
package/dist/Storage.d.mts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { ReadStream } from 'fs';
|
|
2
2
|
import { Stream } from 'stream';
|
|
3
|
-
import {
|
|
3
|
+
import { Metadata } from './types.mjs';
|
|
4
|
+
import { StorageProviderInterface } from './StorageProviderInterface.mjs';
|
|
4
5
|
import '@aws-sdk/client-s3';
|
|
5
6
|
|
|
6
|
-
declare
|
|
7
|
-
protected
|
|
8
|
-
constructor(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
abstract metadata(path: string): Promise<Metadata>;
|
|
7
|
+
declare class Storage {
|
|
8
|
+
protected provider: StorageProviderInterface;
|
|
9
|
+
constructor(provider: StorageProviderInterface);
|
|
10
|
+
exists(path: string): Promise<boolean>;
|
|
11
|
+
put(path: string, content: string | object | Stream | Buffer): Promise<boolean>;
|
|
12
|
+
getJson(path: string): Promise<object>;
|
|
13
|
+
getString(path: string): Promise<string>;
|
|
14
|
+
getBuffer(path: string): Promise<Buffer>;
|
|
15
|
+
getStream(path: string): Promise<ReadStream>;
|
|
16
|
+
delete(path: string): Promise<boolean>;
|
|
17
|
+
metadata(path: string): Promise<Metadata>;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export { Storage };
|
package/dist/Storage.mjs
CHANGED
|
@@ -1,15 +1,37 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
|
-
constructor(
|
|
5
|
-
this.
|
|
3
|
+
class Storage {
|
|
4
|
+
constructor(provider) {
|
|
5
|
+
this.provider = provider;
|
|
6
6
|
}
|
|
7
|
-
static
|
|
8
|
-
|
|
7
|
+
static {
|
|
8
|
+
__name(this, "Storage");
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
exists(path) {
|
|
11
|
+
return this.provider.exists(path);
|
|
12
|
+
}
|
|
13
|
+
put(path, content) {
|
|
14
|
+
return this.provider.put(path, content);
|
|
15
|
+
}
|
|
16
|
+
getJson(path) {
|
|
17
|
+
return this.provider.getJson(path);
|
|
18
|
+
}
|
|
19
|
+
getString(path) {
|
|
20
|
+
return this.provider.getString(path);
|
|
21
|
+
}
|
|
22
|
+
getBuffer(path) {
|
|
23
|
+
return this.provider.getBuffer(path);
|
|
24
|
+
}
|
|
25
|
+
getStream(path) {
|
|
26
|
+
return this.provider.getStream(path);
|
|
27
|
+
}
|
|
28
|
+
delete(path) {
|
|
29
|
+
return this.provider.delete(path);
|
|
30
|
+
}
|
|
31
|
+
metadata(path) {
|
|
32
|
+
return this.provider.metadata(path);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
13
35
|
export {
|
|
14
36
|
Storage
|
|
15
37
|
};
|
package/dist/Storage.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Storage.mts"],"sourcesContent":["import { ReadStream } from 'fs';\nimport { Stream } from 'stream';\nimport { Metadata } from './types.mjs';\nimport { StorageConfig } from './types.mjs';\n\nexport
|
|
1
|
+
{"version":3,"sources":["../src/Storage.mts"],"sourcesContent":["import { ReadStream } from 'fs';\nimport { Stream } from 'stream';\nimport { Metadata } from './types.mjs';\nimport { StorageConfig } from './types.mjs';\nimport { StorageProviderInterface } from './StorageProviderInterface.mjs';\n\nexport class Storage {\n constructor(protected provider: StorageProviderInterface) {}\n\n exists(path: string): Promise<boolean> {\n return this.provider.exists(path);\n }\n \n put(path: string, content: string | object | Stream | Buffer): Promise<boolean> {\n return this.provider.put(path, content);\n }\n \n getJson(path: string): Promise<object> {\n return this.provider.getJson(path);\n }\n \n getString(path: string): Promise<string> {\n return this.provider.getString(path);\n }\n \n getBuffer(path: string): Promise<Buffer> {\n return this.provider.getBuffer(path);\n }\n \n getStream(path: string): Promise<ReadStream> {\n return this.provider.getStream(path);\n }\n \n delete(path: string): Promise<boolean> {\n return this.provider.delete(path);\n }\n \n metadata(path: string): Promise<Metadata> {\n return this.provider.metadata(path);\n }\n}\n"],"mappings":";;AAMO,MAAM,QAAQ;AAAA,EACnB,YAAsB,UAAoC;AAApC;AAAA,EAAqC;AAAA,EAP7D,OAMqB;AAAA;AAAA;AAAA,EAGnB,OAAO,MAAgC;AACrC,WAAO,KAAK,SAAS,OAAO,IAAI;AAAA,EAClC;AAAA,EAEA,IAAI,MAAc,SAA8D;AAC9E,WAAO,KAAK,SAAS,IAAI,MAAM,OAAO;AAAA,EACxC;AAAA,EAEA,QAAQ,MAA+B;AACrC,WAAO,KAAK,SAAS,QAAQ,IAAI;AAAA,EACnC;AAAA,EAEA,UAAU,MAA+B;AACvC,WAAO,KAAK,SAAS,UAAU,IAAI;AAAA,EACrC;AAAA,EAEA,UAAU,MAA+B;AACvC,WAAO,KAAK,SAAS,UAAU,IAAI;AAAA,EACrC;AAAA,EAEA,UAAU,MAAmC;AAC3C,WAAO,KAAK,SAAS,UAAU,IAAI;AAAA,EACrC;AAAA,EAEA,OAAO,MAAgC;AACrC,WAAO,KAAK,SAAS,OAAO,IAAI;AAAA,EAClC;AAAA,EAEA,SAAS,MAAiC;AACxC,WAAO,KAAK,SAAS,SAAS,IAAI;AAAA,EACpC;AACF;","names":[]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FlexibleFactory } from '@devbro/neko-helper';
|
|
2
|
+
import { StorageProviderInterface } from './StorageProviderInterface.mjs';
|
|
3
|
+
import 'fs';
|
|
4
|
+
import 'stream';
|
|
5
|
+
import './types.mjs';
|
|
6
|
+
import '@aws-sdk/client-s3';
|
|
7
|
+
|
|
8
|
+
declare class StorageProviderFactory {
|
|
9
|
+
static instance: FlexibleFactory<StorageProviderInterface>;
|
|
10
|
+
static register(key: string, factory: (...args: any[]) => StorageProviderInterface): void;
|
|
11
|
+
static create<T>(key: string, ...args: any[]): StorageProviderInterface;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { StorageProviderFactory };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import { FlexibleFactory } from "@devbro/neko-helper";
|
|
4
|
+
class StorageProviderFactory {
|
|
5
|
+
static {
|
|
6
|
+
__name(this, "StorageProviderFactory");
|
|
7
|
+
}
|
|
8
|
+
static instance = new FlexibleFactory();
|
|
9
|
+
static register(key, factory) {
|
|
10
|
+
StorageProviderFactory.instance.register(key, factory);
|
|
11
|
+
}
|
|
12
|
+
static create(key, ...args) {
|
|
13
|
+
return StorageProviderFactory.instance.create(key, ...args);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
StorageProviderFactory
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=StorageProviderFactory.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/StorageProviderFactory.mts"],"sourcesContent":["import { Storage } from './Storage.mjs';\nimport { FlexibleFactory } from '@devbro/neko-helper';\nimport { StorageProviderInterface } from './StorageProviderInterface.mjs';\n\nexport class StorageProviderFactory {\n static instance: FlexibleFactory<StorageProviderInterface> = new FlexibleFactory<StorageProviderInterface>();\n\n static register(key: string, factory: (...args: any[]) => StorageProviderInterface): void {\n StorageProviderFactory.instance.register(key, factory);\n }\n\n static create<T>(key: string, ...args: any[]): StorageProviderInterface {\n return StorageProviderFactory.instance.create(key, ...args);\n }\n}\n"],"mappings":";;AACA,SAAS,uBAAuB;AAGzB,MAAM,uBAAuB;AAAA,EAJpC,OAIoC;AAAA;AAAA;AAAA,EAClC,OAAO,WAAsD,IAAI,gBAA0C;AAAA,EAE3G,OAAO,SAAS,KAAa,SAA6D;AACxF,2BAAuB,SAAS,SAAS,KAAK,OAAO;AAAA,EACvD;AAAA,EAEA,OAAO,OAAU,QAAgB,MAAuC;AACtE,WAAO,uBAAuB,SAAS,OAAO,KAAK,GAAG,IAAI;AAAA,EAC5D;AACF;","names":[]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ReadStream } from 'fs';
|
|
2
|
+
import { Stream } from 'stream';
|
|
3
|
+
import { Metadata } from './types.mjs';
|
|
4
|
+
import '@aws-sdk/client-s3';
|
|
5
|
+
|
|
6
|
+
interface StorageProviderInterface {
|
|
7
|
+
exists(path: string): Promise<boolean>;
|
|
8
|
+
put(path: string, content: string | object | Stream | Buffer): Promise<boolean>;
|
|
9
|
+
getJson(path: string): Promise<object>;
|
|
10
|
+
getString(path: string): Promise<string>;
|
|
11
|
+
getBuffer(path: string): Promise<Buffer>;
|
|
12
|
+
getStream(path: string): Promise<ReadStream>;
|
|
13
|
+
delete(path: string): Promise<boolean>;
|
|
14
|
+
metadata(path: string): Promise<Metadata>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type { StorageProviderInterface };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=StorageProviderInterface.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export { Metadata, StorageConfig } from './types.mjs';
|
|
2
2
|
export { Storage } from './Storage.mjs';
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
3
|
+
export { AWSS3StorageProvider } from './providers/AWSS3StorageProvider.mjs';
|
|
4
|
+
export { LocalStorageProvider } from './providers/LocalStorageProvider.mjs';
|
|
5
|
+
export { StorageProviderFactory } from './StorageProviderFactory.mjs';
|
|
6
|
+
export { StorageProviderInterface } from './StorageProviderInterface.mjs';
|
|
6
7
|
import '@aws-sdk/client-s3';
|
|
7
8
|
import 'fs';
|
|
8
9
|
import 'stream';
|
|
10
|
+
import '@devbro/neko-helper';
|
package/dist/index.js
CHANGED
|
@@ -27,296 +27,248 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
27
|
mod
|
|
28
28
|
));
|
|
29
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
var __async = (__this, __arguments, generator) => {
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
var fulfilled = (value) => {
|
|
33
|
-
try {
|
|
34
|
-
step(generator.next(value));
|
|
35
|
-
} catch (e) {
|
|
36
|
-
reject(e);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
var rejected = (value) => {
|
|
40
|
-
try {
|
|
41
|
-
step(generator.throw(value));
|
|
42
|
-
} catch (e) {
|
|
43
|
-
reject(e);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
47
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
30
|
|
|
51
31
|
// src/index.ts
|
|
52
32
|
var index_exports = {};
|
|
53
33
|
__export(index_exports, {
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
AWSS3StorageProvider: () => AWSS3StorageProvider,
|
|
35
|
+
LocalStorageProvider: () => LocalStorageProvider,
|
|
56
36
|
Storage: () => Storage,
|
|
57
|
-
|
|
37
|
+
StorageProviderFactory: () => StorageProviderFactory
|
|
58
38
|
});
|
|
59
39
|
module.exports = __toCommonJS(index_exports);
|
|
60
40
|
|
|
61
41
|
// src/Storage.mts
|
|
62
|
-
var
|
|
63
|
-
constructor(
|
|
64
|
-
this.
|
|
42
|
+
var Storage = class {
|
|
43
|
+
constructor(provider) {
|
|
44
|
+
this.provider = provider;
|
|
45
|
+
}
|
|
46
|
+
static {
|
|
47
|
+
__name(this, "Storage");
|
|
48
|
+
}
|
|
49
|
+
exists(path2) {
|
|
50
|
+
return this.provider.exists(path2);
|
|
51
|
+
}
|
|
52
|
+
put(path2, content) {
|
|
53
|
+
return this.provider.put(path2, content);
|
|
54
|
+
}
|
|
55
|
+
getJson(path2) {
|
|
56
|
+
return this.provider.getJson(path2);
|
|
65
57
|
}
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
getString(path2) {
|
|
59
|
+
return this.provider.getString(path2);
|
|
60
|
+
}
|
|
61
|
+
getBuffer(path2) {
|
|
62
|
+
return this.provider.getBuffer(path2);
|
|
63
|
+
}
|
|
64
|
+
getStream(path2) {
|
|
65
|
+
return this.provider.getStream(path2);
|
|
66
|
+
}
|
|
67
|
+
delete(path2) {
|
|
68
|
+
return this.provider.delete(path2);
|
|
69
|
+
}
|
|
70
|
+
metadata(path2) {
|
|
71
|
+
return this.provider.metadata(path2);
|
|
68
72
|
}
|
|
69
73
|
};
|
|
70
|
-
__name(_Storage, "Storage");
|
|
71
|
-
var Storage = _Storage;
|
|
72
74
|
|
|
73
|
-
// src/
|
|
75
|
+
// src/providers/AWSS3StorageProvider.mts
|
|
74
76
|
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
75
77
|
var import_stream = __toESM(require("stream"), 1);
|
|
76
|
-
var
|
|
78
|
+
var AWSS3StorageProvider = class {
|
|
77
79
|
constructor(config) {
|
|
78
|
-
var _a;
|
|
79
|
-
super(config);
|
|
80
80
|
this.config = config;
|
|
81
|
-
|
|
82
|
-
throw new Error(`storage engine cannot handle this config.`);
|
|
83
|
-
}
|
|
84
|
-
this.s3 = new import_client_s3.S3Client(((_a = this.config) == null ? void 0 : _a.s3Config) || {});
|
|
81
|
+
this.s3 = new import_client_s3.S3Client(this.config?.s3Config || {});
|
|
85
82
|
}
|
|
86
|
-
static
|
|
87
|
-
|
|
83
|
+
static {
|
|
84
|
+
__name(this, "AWSS3StorageProvider");
|
|
88
85
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
throw error;
|
|
86
|
+
s3;
|
|
87
|
+
async exists(path2) {
|
|
88
|
+
try {
|
|
89
|
+
await this.s3.send(new import_client_s3.HeadObjectCommand({ Bucket: this.config?.bucket, Key: path2 }));
|
|
90
|
+
return true;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
if (error.name === "NotFound") {
|
|
93
|
+
return false;
|
|
100
94
|
}
|
|
101
|
-
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
102
97
|
}
|
|
103
|
-
put(path2, content) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return true;
|
|
123
|
-
});
|
|
98
|
+
async put(path2, content) {
|
|
99
|
+
let body;
|
|
100
|
+
if (typeof content === "string" || content instanceof Buffer) {
|
|
101
|
+
body = content;
|
|
102
|
+
} else if (typeof content === "object" && !(content instanceof import_stream.default)) {
|
|
103
|
+
body = JSON.stringify(content);
|
|
104
|
+
} else if (content instanceof import_stream.default) {
|
|
105
|
+
body = content;
|
|
106
|
+
} else {
|
|
107
|
+
throw new Error("Unsupported content type");
|
|
108
|
+
}
|
|
109
|
+
await this.s3.send(
|
|
110
|
+
new import_client_s3.PutObjectCommand({
|
|
111
|
+
Bucket: this.config.bucket,
|
|
112
|
+
Key: path2,
|
|
113
|
+
Body: body
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
return true;
|
|
124
117
|
}
|
|
125
|
-
getJson(path2) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return JSON.parse(body);
|
|
132
|
-
});
|
|
118
|
+
async getJson(path2) {
|
|
119
|
+
const data = await this.s3.send(
|
|
120
|
+
new import_client_s3.GetObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
121
|
+
);
|
|
122
|
+
const body = await this.streamToString(data.Body);
|
|
123
|
+
return JSON.parse(body);
|
|
133
124
|
}
|
|
134
|
-
getString(path2) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return yield this.streamToString(data.Body);
|
|
140
|
-
});
|
|
125
|
+
async getString(path2) {
|
|
126
|
+
const data = await this.s3.send(
|
|
127
|
+
new import_client_s3.GetObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
128
|
+
);
|
|
129
|
+
return await this.streamToString(data.Body);
|
|
141
130
|
}
|
|
142
|
-
delete(path2) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return true;
|
|
146
|
-
});
|
|
131
|
+
async delete(path2) {
|
|
132
|
+
await this.s3.send(new import_client_s3.DeleteObjectCommand({ Bucket: this.config.bucket, Key: path2 }));
|
|
133
|
+
return true;
|
|
147
134
|
}
|
|
148
|
-
streamToString(stream) {
|
|
149
|
-
return
|
|
150
|
-
return new Promise((resolve, reject) => {
|
|
151
|
-
const chunks = [];
|
|
152
|
-
stream.on("data", (chunk) => chunks.push(chunk));
|
|
153
|
-
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
154
|
-
stream.on("error", reject);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
getBuffer(path2) {
|
|
159
|
-
return __async(this, null, function* () {
|
|
160
|
-
const data = yield this.s3.send(
|
|
161
|
-
new import_client_s3.GetObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
162
|
-
);
|
|
135
|
+
async streamToString(stream) {
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
163
137
|
const chunks = [];
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
168
|
-
stream.on("error", reject);
|
|
169
|
-
});
|
|
138
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
139
|
+
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf-8")));
|
|
140
|
+
stream.on("error", reject);
|
|
170
141
|
});
|
|
171
142
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
143
|
+
async getBuffer(path2) {
|
|
144
|
+
const data = await this.s3.send(
|
|
145
|
+
new import_client_s3.GetObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
146
|
+
);
|
|
147
|
+
const chunks = [];
|
|
148
|
+
const stream = data.Body;
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
151
|
+
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
152
|
+
stream.on("error", reject);
|
|
178
153
|
});
|
|
179
154
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
155
|
+
async getStream(path2) {
|
|
156
|
+
const data = await this.s3.send(
|
|
157
|
+
new import_client_s3.GetObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
158
|
+
);
|
|
159
|
+
return data.Body;
|
|
160
|
+
}
|
|
161
|
+
async metadata(path2) {
|
|
162
|
+
const metadata = await this.s3.send(
|
|
163
|
+
new import_client_s3.HeadObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
164
|
+
);
|
|
165
|
+
return {
|
|
166
|
+
size: metadata.ContentLength || 0,
|
|
167
|
+
mimeType: metadata.ContentType || "unknown",
|
|
168
|
+
lastModifiedDate: (metadata.LastModified || /* @__PURE__ */ new Date(0)).toISOString()
|
|
169
|
+
};
|
|
191
170
|
}
|
|
192
171
|
};
|
|
193
|
-
__name(_AWSS3Storage, "AWSS3Storage");
|
|
194
|
-
var AWSS3Storage = _AWSS3Storage;
|
|
195
172
|
|
|
196
|
-
// src/
|
|
173
|
+
// src/providers/LocalStorageProvider.mts
|
|
197
174
|
var import_stream2 = __toESM(require("stream"), 1);
|
|
198
175
|
var fs = __toESM(require("fs/promises"), 1);
|
|
199
176
|
var import_fs = require("fs");
|
|
200
177
|
var path = __toESM(require("path"), 1);
|
|
201
178
|
var mime = __toESM(require("mime-types"), 1);
|
|
202
|
-
var
|
|
179
|
+
var LocalStorageProvider = class {
|
|
203
180
|
constructor(config) {
|
|
204
|
-
|
|
205
|
-
if (!_LocalStorage.canHandle(config)) {
|
|
206
|
-
throw new Error(`storage engine cannot handle this config.`);
|
|
207
|
-
}
|
|
181
|
+
this.config = config;
|
|
208
182
|
fs.mkdir(this.config.basePath, { recursive: true }).catch((error) => {
|
|
209
183
|
throw error;
|
|
210
184
|
});
|
|
211
185
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const fullPath = this.getFullPath(path2);
|
|
215
|
-
const stats = yield fs.stat(fullPath);
|
|
216
|
-
return {
|
|
217
|
-
size: stats.size,
|
|
218
|
-
mimeType: mime.lookup(fullPath) || "unknown",
|
|
219
|
-
lastModifiedDate: stats.mtime.toISOString()
|
|
220
|
-
};
|
|
221
|
-
});
|
|
186
|
+
static {
|
|
187
|
+
__name(this, "LocalStorageProvider");
|
|
222
188
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
189
|
+
async metadata(path2) {
|
|
190
|
+
const fullPath = this.getFullPath(path2);
|
|
191
|
+
const stats = await fs.stat(fullPath);
|
|
192
|
+
return {
|
|
193
|
+
size: stats.size,
|
|
194
|
+
mimeType: mime.lookup(fullPath) || "unknown",
|
|
195
|
+
lastModifiedDate: stats.mtime.toISOString()
|
|
196
|
+
};
|
|
228
197
|
}
|
|
229
198
|
getFullPath(filePath) {
|
|
230
199
|
return path.join(this.config.basePath, filePath);
|
|
231
200
|
}
|
|
232
|
-
exists(path2) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
yield fs.access(this.getFullPath(path2));
|
|
236
|
-
return true;
|
|
237
|
-
} catch (e) {
|
|
238
|
-
return false;
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
put(path2, content) {
|
|
243
|
-
return __async(this, null, function* () {
|
|
244
|
-
const fullPath = this.getFullPath(path2);
|
|
245
|
-
if (typeof content === "string" || content instanceof Buffer) {
|
|
246
|
-
yield fs.writeFile(fullPath, content);
|
|
247
|
-
} else if (typeof content === "object" && !(content instanceof import_stream2.default)) {
|
|
248
|
-
yield fs.writeFile(fullPath, JSON.stringify(content, null, 2));
|
|
249
|
-
} else if (typeof content === "object" && content instanceof import_stream2.default) {
|
|
250
|
-
const writeStream = (0, import_fs.createWriteStream)(fullPath);
|
|
251
|
-
yield new Promise((resolve, reject) => {
|
|
252
|
-
content.pipe(writeStream);
|
|
253
|
-
content.on("end", resolve);
|
|
254
|
-
content.on("error", reject);
|
|
255
|
-
});
|
|
256
|
-
} else {
|
|
257
|
-
throw new Error("Unsupported content type");
|
|
258
|
-
}
|
|
201
|
+
async exists(path2) {
|
|
202
|
+
try {
|
|
203
|
+
await fs.access(this.getFullPath(path2));
|
|
259
204
|
return true;
|
|
260
|
-
}
|
|
205
|
+
} catch {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
261
208
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
209
|
+
async put(filepath, content) {
|
|
210
|
+
const fullPath = this.getFullPath(filepath);
|
|
211
|
+
const dir = path.dirname(fullPath);
|
|
212
|
+
await fs.mkdir(dir, { recursive: true });
|
|
213
|
+
if (typeof content === "string" || content instanceof Buffer) {
|
|
214
|
+
await fs.writeFile(fullPath, content);
|
|
215
|
+
} else if (typeof content === "object" && !(content instanceof import_stream2.default)) {
|
|
216
|
+
await fs.writeFile(fullPath, JSON.stringify(content, null, 2));
|
|
217
|
+
} else if (typeof content === "object" && content instanceof import_stream2.default) {
|
|
218
|
+
const writeStream = (0, import_fs.createWriteStream)(fullPath);
|
|
219
|
+
await new Promise((resolve, reject) => {
|
|
220
|
+
content.pipe(writeStream);
|
|
221
|
+
content.on("end", resolve);
|
|
222
|
+
content.on("error", reject);
|
|
223
|
+
});
|
|
224
|
+
} else {
|
|
225
|
+
throw new Error("Unsupported content type");
|
|
226
|
+
}
|
|
227
|
+
return true;
|
|
268
228
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
});
|
|
229
|
+
async getJson(path2) {
|
|
230
|
+
const fullPath = this.getFullPath(path2);
|
|
231
|
+
const content = await fs.readFile(fullPath, "utf-8");
|
|
232
|
+
return JSON.parse(content);
|
|
274
233
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
return yield fs.readFile(fullPath);
|
|
279
|
-
});
|
|
234
|
+
async getString(path2, encoding = "utf-8") {
|
|
235
|
+
const fullPath = this.getFullPath(path2);
|
|
236
|
+
return await fs.readFile(fullPath, encoding);
|
|
280
237
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
return (0, import_fs.createReadStream)(fullPath);
|
|
285
|
-
});
|
|
238
|
+
async getBuffer(path2) {
|
|
239
|
+
const fullPath = this.getFullPath(path2);
|
|
240
|
+
return await fs.readFile(fullPath);
|
|
286
241
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
242
|
+
async getStream(path2) {
|
|
243
|
+
const fullPath = this.getFullPath(path2);
|
|
244
|
+
return (0, import_fs.createReadStream)(fullPath);
|
|
245
|
+
}
|
|
246
|
+
async delete(path2) {
|
|
247
|
+
const fullPath = this.getFullPath(path2);
|
|
248
|
+
await fs.unlink(fullPath);
|
|
249
|
+
return true;
|
|
293
250
|
}
|
|
294
251
|
};
|
|
295
|
-
__name(_LocalStorage, "LocalStorage");
|
|
296
|
-
var LocalStorage = _LocalStorage;
|
|
297
252
|
|
|
298
|
-
// src/
|
|
299
|
-
var
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
253
|
+
// src/StorageProviderFactory.mts
|
|
254
|
+
var import_neko_helper = require("@devbro/neko-helper");
|
|
255
|
+
var StorageProviderFactory = class _StorageProviderFactory {
|
|
256
|
+
static {
|
|
257
|
+
__name(this, "StorageProviderFactory");
|
|
258
|
+
}
|
|
259
|
+
static instance = new import_neko_helper.FlexibleFactory();
|
|
260
|
+
static register(key, factory) {
|
|
261
|
+
_StorageProviderFactory.instance.register(key, factory);
|
|
262
|
+
}
|
|
263
|
+
static create(key, ...args) {
|
|
264
|
+
return _StorageProviderFactory.instance.create(key, ...args);
|
|
310
265
|
}
|
|
311
266
|
};
|
|
312
|
-
__name(_StorageFactory, "StorageFactory");
|
|
313
|
-
_StorageFactory.storageEngines = [LocalStorage, AWSS3Storage];
|
|
314
|
-
var StorageFactory = _StorageFactory;
|
|
315
267
|
// Annotate the CommonJS export names for ESM import in node:
|
|
316
268
|
0 && (module.exports = {
|
|
317
|
-
|
|
318
|
-
|
|
269
|
+
AWSS3StorageProvider,
|
|
270
|
+
LocalStorageProvider,
|
|
319
271
|
Storage,
|
|
320
|
-
|
|
272
|
+
StorageProviderFactory
|
|
321
273
|
});
|
|
322
274
|
//# sourceMappingURL=index.js.map
|