@devbro/neko-storage 0.1.1 → 0.1.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.
- package/dist/AWSS3Storage.mjs +32 -39
- package/dist/AWSS3Storage.mjs.map +1 -1
- package/dist/LocalStorage.d.mts +1 -1
- package/dist/LocalStorage.mjs +6 -6
- package/dist/LocalStorage.mjs.map +1 -1
- package/dist/Storage.mjs +0 -3
- package/dist/Storage.mjs.map +1 -1
- package/dist/StorageFactory.mjs +3 -8
- package/dist/StorageFactory.mjs.map +1 -1
- package/dist/index.js +304 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -2
- package/dist/AWSS3Storage.d.ts +0 -23
- package/dist/AWSS3Storage.js +0 -194
- package/dist/AWSS3Storage.js.map +0 -1
- package/dist/LocalStorage.d.ts +0 -21
- package/dist/LocalStorage.js +0 -162
- package/dist/LocalStorage.js.map +0 -1
- package/dist/Storage.d.ts +0 -20
- package/dist/Storage.js +0 -42
- package/dist/Storage.js.map +0 -1
- package/dist/StorageFactory.d.ts +0 -13
- package/dist/StorageFactory.js +0 -52
- package/dist/StorageFactory.js.map +0 -1
- package/dist/index.d.ts +0 -8
- package/dist/types.d.ts +0 -15
- package/dist/types.js +0 -17
- package/dist/types.js.map +0 -1
package/dist/AWSS3Storage.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
3
|
var __async = (__this, __arguments, generator) => {
|
|
6
4
|
return new Promise((resolve, reject) => {
|
|
7
5
|
var fulfilled = (value) => {
|
|
@@ -22,15 +20,19 @@ var __async = (__this, __arguments, generator) => {
|
|
|
22
20
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
23
21
|
});
|
|
24
22
|
};
|
|
25
|
-
import { Storage } from "./Storage";
|
|
26
|
-
import {
|
|
23
|
+
import { Storage } from "./Storage.mjs";
|
|
24
|
+
import {
|
|
25
|
+
S3Client,
|
|
26
|
+
HeadObjectCommand,
|
|
27
|
+
PutObjectCommand,
|
|
28
|
+
GetObjectCommand,
|
|
29
|
+
DeleteObjectCommand
|
|
30
|
+
} from "@aws-sdk/client-s3";
|
|
27
31
|
import Stream from "stream";
|
|
28
32
|
const _AWSS3Storage = class _AWSS3Storage extends Storage {
|
|
29
33
|
constructor(config) {
|
|
30
34
|
var _a;
|
|
31
35
|
super(config);
|
|
32
|
-
__publicField(this, "config");
|
|
33
|
-
__publicField(this, "s3");
|
|
34
36
|
this.config = config;
|
|
35
37
|
if (!_AWSS3Storage.canHandle(config)) {
|
|
36
38
|
throw new Error(`storage engine cannot handle this config.`);
|
|
@@ -44,10 +46,7 @@ const _AWSS3Storage = class _AWSS3Storage extends Storage {
|
|
|
44
46
|
return __async(this, null, function* () {
|
|
45
47
|
var _a;
|
|
46
48
|
try {
|
|
47
|
-
yield this.s3.send(new HeadObjectCommand({
|
|
48
|
-
Bucket: (_a = this.config) == null ? void 0 : _a.bucket,
|
|
49
|
-
Key: path
|
|
50
|
-
}));
|
|
49
|
+
yield this.s3.send(new HeadObjectCommand({ Bucket: (_a = this.config) == null ? void 0 : _a.bucket, Key: path }));
|
|
51
50
|
return true;
|
|
52
51
|
} catch (error) {
|
|
53
52
|
if (error.name === "NotFound") {
|
|
@@ -69,39 +68,36 @@ const _AWSS3Storage = class _AWSS3Storage extends Storage {
|
|
|
69
68
|
} else {
|
|
70
69
|
throw new Error("Unsupported content type");
|
|
71
70
|
}
|
|
72
|
-
yield this.s3.send(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
71
|
+
yield this.s3.send(
|
|
72
|
+
new PutObjectCommand({
|
|
73
|
+
Bucket: this.config.bucket,
|
|
74
|
+
Key: path,
|
|
75
|
+
Body: body
|
|
76
|
+
})
|
|
77
|
+
);
|
|
77
78
|
return true;
|
|
78
79
|
});
|
|
79
80
|
}
|
|
80
81
|
getJson(path) {
|
|
81
82
|
return __async(this, null, function* () {
|
|
82
|
-
const data = yield this.s3.send(
|
|
83
|
-
Bucket: this.config.bucket,
|
|
84
|
-
|
|
85
|
-
}));
|
|
83
|
+
const data = yield this.s3.send(
|
|
84
|
+
new GetObjectCommand({ Bucket: this.config.bucket, Key: path })
|
|
85
|
+
);
|
|
86
86
|
const body = yield this.streamToString(data.Body);
|
|
87
87
|
return JSON.parse(body);
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
90
|
getString(path) {
|
|
91
91
|
return __async(this, null, function* () {
|
|
92
|
-
const data = yield this.s3.send(
|
|
93
|
-
Bucket: this.config.bucket,
|
|
94
|
-
|
|
95
|
-
}));
|
|
92
|
+
const data = yield this.s3.send(
|
|
93
|
+
new GetObjectCommand({ Bucket: this.config.bucket, Key: path })
|
|
94
|
+
);
|
|
96
95
|
return yield this.streamToString(data.Body);
|
|
97
96
|
});
|
|
98
97
|
}
|
|
99
98
|
delete(path) {
|
|
100
99
|
return __async(this, null, function* () {
|
|
101
|
-
yield this.s3.send(new DeleteObjectCommand({
|
|
102
|
-
Bucket: this.config.bucket,
|
|
103
|
-
Key: path
|
|
104
|
-
}));
|
|
100
|
+
yield this.s3.send(new DeleteObjectCommand({ Bucket: this.config.bucket, Key: path }));
|
|
105
101
|
return true;
|
|
106
102
|
});
|
|
107
103
|
}
|
|
@@ -117,10 +113,9 @@ const _AWSS3Storage = class _AWSS3Storage extends Storage {
|
|
|
117
113
|
}
|
|
118
114
|
getBuffer(path) {
|
|
119
115
|
return __async(this, null, function* () {
|
|
120
|
-
const data = yield this.s3.send(
|
|
121
|
-
Bucket: this.config.bucket,
|
|
122
|
-
|
|
123
|
-
}));
|
|
116
|
+
const data = yield this.s3.send(
|
|
117
|
+
new GetObjectCommand({ Bucket: this.config.bucket, Key: path })
|
|
118
|
+
);
|
|
124
119
|
const chunks = [];
|
|
125
120
|
const stream = data.Body;
|
|
126
121
|
return new Promise((resolve, reject) => {
|
|
@@ -132,19 +127,17 @@ const _AWSS3Storage = class _AWSS3Storage extends Storage {
|
|
|
132
127
|
}
|
|
133
128
|
getStream(path) {
|
|
134
129
|
return __async(this, null, function* () {
|
|
135
|
-
const data = yield this.s3.send(
|
|
136
|
-
Bucket: this.config.bucket,
|
|
137
|
-
|
|
138
|
-
}));
|
|
130
|
+
const data = yield this.s3.send(
|
|
131
|
+
new GetObjectCommand({ Bucket: this.config.bucket, Key: path })
|
|
132
|
+
);
|
|
139
133
|
return data.Body;
|
|
140
134
|
});
|
|
141
135
|
}
|
|
142
136
|
metadata(path) {
|
|
143
137
|
return __async(this, null, function* () {
|
|
144
|
-
const metadata = yield this.s3.send(
|
|
145
|
-
Bucket: this.config.bucket,
|
|
146
|
-
|
|
147
|
-
}));
|
|
138
|
+
const metadata = yield this.s3.send(
|
|
139
|
+
new HeadObjectCommand({ Bucket: this.config.bucket, Key: path })
|
|
140
|
+
);
|
|
148
141
|
return {
|
|
149
142
|
size: metadata.ContentLength || 0,
|
|
150
143
|
mimeType: metadata.ContentType || "unknown",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/AWSS3Storage.
|
|
1
|
+
{"version":3,"sources":["../src/AWSS3Storage.mts"],"sourcesContent":["import { Metadata, StorageConfig } from './types.mjs';\nimport { Storage } from './Storage.mjs';\nimport {\n S3Client,\n HeadObjectCommand,\n PutObjectCommand,\n GetObjectCommand,\n DeleteObjectCommand,\n HeadObjectOutput,\n} from '@aws-sdk/client-s3';\nimport { ReadStream } from 'fs';\nimport Stream, { Readable } from 'stream';\n\nexport class AWSS3Storage extends Storage {\n private s3: S3Client;\n\n constructor(protected config: StorageConfig) {\n super(config);\n\n if (!AWSS3Storage.canHandle(config)) {\n throw new Error(`storage engine cannot handle this config.`);\n }\n\n this.s3 = new S3Client(this.config?.s3Config || {});\n }\n\n static canHandle(config: StorageConfig): boolean {\n return config.engine === 's3';\n }\n\n async exists(path: string): Promise<boolean> {\n try {\n await this.s3.send(new HeadObjectCommand({ Bucket: this.config?.bucket, Key: path }));\n return true;\n } catch (error: any) {\n if (error.name === 'NotFound') {\n return false;\n }\n throw error;\n }\n }\n\n async put(path: string, content: string | object | Stream | Buffer): Promise<boolean> {\n let body: any;\n if (typeof content === 'string' || content instanceof Buffer) {\n body = content;\n } else if (typeof content === 'object' && !(content instanceof Stream)) {\n body = JSON.stringify(content);\n } else if (content instanceof Stream) {\n body = content;\n } else {\n throw new Error('Unsupported content type');\n }\n\n await this.s3.send(\n new PutObjectCommand({\n Bucket: this.config.bucket,\n Key: path,\n Body: body,\n })\n );\n\n return true;\n }\n\n async getJson(path: string): Promise<object> {\n const data = await this.s3.send(\n new GetObjectCommand({ Bucket: this.config.bucket, Key: path })\n );\n const body = await this.streamToString(data.Body as Stream);\n return JSON.parse(body);\n }\n\n async getString(path: string): Promise<string> {\n const data = await this.s3.send(\n new GetObjectCommand({ Bucket: this.config.bucket, Key: path })\n );\n return await this.streamToString(data.Body as Stream);\n }\n\n async delete(path: string): Promise<boolean> {\n await this.s3.send(new DeleteObjectCommand({ Bucket: this.config.bucket, Key: path }));\n return true;\n }\n\n private async streamToString(stream: Stream): Promise<string> {\n return new Promise((resolve, reject) => {\n const chunks: Uint8Array[] = [];\n stream.on('data', (chunk) => chunks.push(chunk));\n stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));\n stream.on('error', reject);\n });\n }\n\n async getBuffer(path: string): Promise<Buffer> {\n const data = await this.s3.send(\n new GetObjectCommand({ Bucket: this.config.bucket, Key: path })\n );\n const chunks: Uint8Array[] = [];\n const stream = data.Body as Readable;\n\n return new Promise((resolve, reject) => {\n stream.on('data', (chunk) => chunks.push(chunk));\n stream.on('end', () => resolve(Buffer.concat(chunks)));\n stream.on('error', reject);\n });\n }\n\n async getStream(path: string): Promise<ReadStream> {\n const data = await this.s3.send(\n new GetObjectCommand({ Bucket: this.config.bucket, Key: path })\n );\n return data.Body as unknown as ReadStream;\n }\n\n async metadata(path: string): Promise<Metadata> {\n const metadata = await this.s3.send(\n new HeadObjectCommand({ Bucket: this.config.bucket, Key: path })\n );\n return {\n size: metadata.ContentLength || 0,\n mimeType: metadata.ContentType || 'unknown',\n lastModifiedDate: (metadata.LastModified || new Date(0)).toISOString(),\n };\n }\n}\n"],"mappings":"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","names":[]}
|
package/dist/LocalStorage.d.mts
CHANGED
|
@@ -10,7 +10,7 @@ declare class LocalStorage extends Storage {
|
|
|
10
10
|
static canHandle(config: StorageConfig): boolean;
|
|
11
11
|
getFullPath(filePath: string): string;
|
|
12
12
|
exists(path: string): Promise<boolean>;
|
|
13
|
-
put(
|
|
13
|
+
put(filepath: string, content: string | object | Stream | Buffer): Promise<boolean>;
|
|
14
14
|
getJson(path: string): Promise<object>;
|
|
15
15
|
getString(path: string, encoding?: BufferEncoding): Promise<string>;
|
|
16
16
|
getBuffer(path: string): Promise<Buffer>;
|
package/dist/LocalStorage.mjs
CHANGED
|
@@ -25,16 +25,14 @@ import * as fs from "fs/promises";
|
|
|
25
25
|
import { createWriteStream, createReadStream } from "fs";
|
|
26
26
|
import * as path from "path";
|
|
27
27
|
import * as mime from "mime-types";
|
|
28
|
-
import { Storage } from "./Storage";
|
|
28
|
+
import { Storage } from "./Storage.mjs";
|
|
29
29
|
const _LocalStorage = class _LocalStorage extends Storage {
|
|
30
30
|
constructor(config) {
|
|
31
31
|
super(config);
|
|
32
32
|
if (!_LocalStorage.canHandle(config)) {
|
|
33
33
|
throw new Error(`storage engine cannot handle this config.`);
|
|
34
34
|
}
|
|
35
|
-
fs.mkdir(this.config.basePath, {
|
|
36
|
-
recursive: true
|
|
37
|
-
}).catch((error) => {
|
|
35
|
+
fs.mkdir(this.config.basePath, { recursive: true }).catch((error) => {
|
|
38
36
|
throw error;
|
|
39
37
|
});
|
|
40
38
|
}
|
|
@@ -68,9 +66,11 @@ const _LocalStorage = class _LocalStorage extends Storage {
|
|
|
68
66
|
}
|
|
69
67
|
});
|
|
70
68
|
}
|
|
71
|
-
put(
|
|
69
|
+
put(filepath, content) {
|
|
72
70
|
return __async(this, null, function* () {
|
|
73
|
-
const fullPath = this.getFullPath(
|
|
71
|
+
const fullPath = this.getFullPath(filepath);
|
|
72
|
+
const dir = path.dirname(fullPath);
|
|
73
|
+
yield fs.mkdir(dir, { recursive: true });
|
|
74
74
|
if (typeof content === "string" || content instanceof Buffer) {
|
|
75
75
|
yield fs.writeFile(fullPath, content);
|
|
76
76
|
} else if (typeof content === "object" && !(content instanceof Stream)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/LocalStorage.
|
|
1
|
+
{"version":3,"sources":["../src/LocalStorage.mts"],"sourcesContent":["import Stream from 'stream';\nimport * as fs from 'fs/promises';\nimport { createWriteStream, createReadStream, ReadStream } from 'fs';\nimport * as path from 'path';\nimport * as mime from 'mime-types';\nimport { Metadata, StorageConfig } from './types.mjs';\nimport { Storage } from './Storage.mjs';\n\nexport class LocalStorage extends Storage {\n constructor(config: StorageConfig) {\n super(config);\n\n if (!LocalStorage.canHandle(config)) {\n throw new Error(`storage engine cannot handle this config.`);\n }\n // Ensure the base folder exists\n fs.mkdir(this.config.basePath, { recursive: true }).catch((error) => {\n throw error;\n });\n }\n\n async metadata(path: string): Promise<Metadata> {\n const fullPath = this.getFullPath(path);\n const stats = await fs.stat(fullPath);\n return {\n size: stats.size,\n mimeType: mime.lookup(fullPath) || 'unknown',\n lastModifiedDate: stats.mtime.toISOString(),\n };\n }\n\n static canHandle(config: StorageConfig) {\n if (config.engine === 'local') {\n return true;\n }\n return false;\n }\n\n getFullPath(filePath: string) {\n return path.join(this.config.basePath, filePath);\n }\n\n async exists(path: string): Promise<boolean> {\n try {\n await fs.access(this.getFullPath(path));\n return true;\n } catch {\n return false;\n }\n }\n\n async put(filepath: string, content: string | object | Stream | Buffer): Promise<boolean> {\n const fullPath = this.getFullPath(filepath);\n\n const dir = path.dirname(fullPath);\n await fs.mkdir(dir, { recursive: true });\n\n if (typeof content === 'string' || content instanceof Buffer) {\n await fs.writeFile(fullPath, content);\n } else if (typeof content === 'object' && !(content instanceof Stream)) {\n await fs.writeFile(fullPath, JSON.stringify(content, null, 2));\n } else if (typeof content === 'object' && content instanceof Stream) {\n const writeStream = createWriteStream(fullPath);\n await new Promise((resolve, reject) => {\n (content as Stream).pipe(writeStream);\n (content as Stream).on('end', resolve);\n (content as Stream).on('error', reject);\n });\n } else {\n throw new Error('Unsupported content type');\n }\n\n return true;\n }\n\n async getJson(path: string): Promise<object> {\n const fullPath = this.getFullPath(path);\n const content = await fs.readFile(fullPath, 'utf-8');\n return JSON.parse(content);\n }\n\n async getString(path: string, encoding: BufferEncoding = 'utf-8'): Promise<string> {\n const fullPath = this.getFullPath(path);\n return await fs.readFile(fullPath, encoding);\n }\n\n async getBuffer(path: string): Promise<Buffer> {\n const fullPath = this.getFullPath(path);\n return await fs.readFile(fullPath);\n }\n\n async getStream(path: string): Promise<ReadStream> {\n const fullPath = this.getFullPath(path);\n return createReadStream(fullPath);\n }\n\n async delete(path: string): Promise<boolean> {\n const fullPath = this.getFullPath(path);\n await fs.unlink(fullPath);\n return true;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,YAAY;AACnB,YAAY,QAAQ;AACpB,SAAS,mBAAmB,wBAAoC;AAChE,YAAY,UAAU;AACtB,YAAY,UAAU;AAEtB,SAAS,eAAe;AAEjB,MAAM,gBAAN,MAAM,sBAAqB,QAAQ;AAAA,EACxC,YAAY,QAAuB;AACjC,UAAM,MAAM;AAEZ,QAAI,CAAC,cAAa,UAAU,MAAM,GAAG;AACnC,YAAM,IAAI,MAAM,2CAA2C;AAAA,IAC7D;AAEA,OAAG,MAAM,KAAK,OAAO,UAAU,EAAE,WAAW,KAAK,CAAC,EAAE,MAAM,CAAC,UAAU;AACnE,YAAM;AAAA,IACR,CAAC;AAAA,EACH;AAAA,EAEM,SAASA,OAAiC;AAAA;AAC9C,YAAM,WAAW,KAAK,YAAYA,KAAI;AACtC,YAAM,QAAQ,MAAM,GAAG,KAAK,QAAQ;AACpC,aAAO;AAAA,QACL,MAAM,MAAM;AAAA,QACZ,UAAU,KAAK,OAAO,QAAQ,KAAK;AAAA,QACnC,kBAAkB,MAAM,MAAM,YAAY;AAAA,MAC5C;AAAA,IACF;AAAA;AAAA,EAEA,OAAO,UAAU,QAAuB;AACtC,QAAI,OAAO,WAAW,SAAS;AAC7B,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,UAAkB;AAC5B,WAAO,KAAK,KAAK,KAAK,OAAO,UAAU,QAAQ;AAAA,EACjD;AAAA,EAEM,OAAOA,OAAgC;AAAA;AAC3C,UAAI;AACF,cAAM,GAAG,OAAO,KAAK,YAAYA,KAAI,CAAC;AACtC,eAAO;AAAA,MACT,SAAQ;AACN,eAAO;AAAA,MACT;AAAA,IACF;AAAA;AAAA,EAEM,IAAI,UAAkB,SAA8D;AAAA;AACxF,YAAM,WAAW,KAAK,YAAY,QAAQ;AAE1C,YAAM,MAAM,KAAK,QAAQ,QAAQ;AACjC,YAAM,GAAG,MAAM,KAAK,EAAE,WAAW,KAAK,CAAC;AAEvC,UAAI,OAAO,YAAY,YAAY,mBAAmB,QAAQ;AAC5D,cAAM,GAAG,UAAU,UAAU,OAAO;AAAA,MACtC,WAAW,OAAO,YAAY,YAAY,EAAE,mBAAmB,SAAS;AACtE,cAAM,GAAG,UAAU,UAAU,KAAK,UAAU,SAAS,MAAM,CAAC,CAAC;AAAA,MAC/D,WAAW,OAAO,YAAY,YAAY,mBAAmB,QAAQ;AACnE,cAAM,cAAc,kBAAkB,QAAQ;AAC9C,cAAM,IAAI,QAAQ,CAAC,SAAS,WAAW;AACrC,UAAC,QAAmB,KAAK,WAAW;AACpC,UAAC,QAAmB,GAAG,OAAO,OAAO;AACrC,UAAC,QAAmB,GAAG,SAAS,MAAM;AAAA,QACxC,CAAC;AAAA,MACH,OAAO;AACL,cAAM,IAAI,MAAM,0BAA0B;AAAA,MAC5C;AAEA,aAAO;AAAA,IACT;AAAA;AAAA,EAEM,QAAQA,OAA+B;AAAA;AAC3C,YAAM,WAAW,KAAK,YAAYA,KAAI;AACtC,YAAM,UAAU,MAAM,GAAG,SAAS,UAAU,OAAO;AACnD,aAAO,KAAK,MAAM,OAAO;AAAA,IAC3B;AAAA;AAAA,EAEM,UAAUA,OAAc,WAA2B,SAA0B;AAAA;AACjF,YAAM,WAAW,KAAK,YAAYA,KAAI;AACtC,aAAO,MAAM,GAAG,SAAS,UAAU,QAAQ;AAAA,IAC7C;AAAA;AAAA,EAEM,UAAUA,OAA+B;AAAA;AAC7C,YAAM,WAAW,KAAK,YAAYA,KAAI;AACtC,aAAO,MAAM,GAAG,SAAS,QAAQ;AAAA,IACnC;AAAA;AAAA,EAEM,UAAUA,OAAmC;AAAA;AACjD,YAAM,WAAW,KAAK,YAAYA,KAAI;AACtC,aAAO,iBAAiB,QAAQ;AAAA,IAClC;AAAA;AAAA,EAEM,OAAOA,OAAgC;AAAA;AAC3C,YAAM,WAAW,KAAK,YAAYA,KAAI;AACtC,YAAM,GAAG,OAAO,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA;AACF;AA7F0C;AAAnC,IAAM,eAAN;","names":["path"]}
|
package/dist/Storage.mjs
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
3
|
const _Storage = class _Storage {
|
|
6
4
|
constructor(config) {
|
|
7
|
-
__publicField(this, "config");
|
|
8
5
|
this.config = config;
|
|
9
6
|
}
|
|
10
7
|
static canHandle(config) {
|
package/dist/Storage.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Storage.
|
|
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 abstract class Storage {\n constructor(protected config: StorageConfig) {}\n\n static canHandle(config: StorageConfig): boolean {\n throw new Error('Method not implemented.');\n }\n\n abstract exists(path: string): Promise<boolean>;\n abstract put(path: string, content: string | object | Stream | Buffer): Promise<boolean>;\n abstract getJson(path: string): Promise<object>;\n abstract getString(path: string): Promise<string>;\n abstract getBuffer(path: string): Promise<Buffer>;\n abstract getStream(path: string): Promise<ReadStream>;\n abstract delete(path: string): Promise<boolean>;\n abstract metadata(path: string): Promise<Metadata>;\n}\n"],"mappings":";;AAKO,MAAe,WAAf,MAAe,SAAQ;AAAA,EAC5B,YAAsB,QAAuB;AAAvB;AAAA,EAAwB;AAAA,EAE9C,OAAO,UAAU,QAAgC;AAC/C,UAAM,IAAI,MAAM,yBAAyB;AAAA,EAC3C;AAUF;AAf8B;AAAvB,IAAe,UAAf;","names":[]}
|
package/dist/StorageFactory.mjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import { LocalStorage } from "./LocalStorage";
|
|
3
|
+
import { AWSS3Storage } from "./AWSS3Storage.mjs";
|
|
4
|
+
import { LocalStorage } from "./LocalStorage.mjs";
|
|
7
5
|
const _StorageFactory = class _StorageFactory {
|
|
8
6
|
registerStorageEngine(engine) {
|
|
9
7
|
_StorageFactory.storageEngines.push(engine);
|
|
@@ -18,10 +16,7 @@ const _StorageFactory = class _StorageFactory {
|
|
|
18
16
|
}
|
|
19
17
|
};
|
|
20
18
|
__name(_StorageFactory, "StorageFactory");
|
|
21
|
-
|
|
22
|
-
LocalStorage,
|
|
23
|
-
AWSS3Storage
|
|
24
|
-
]);
|
|
19
|
+
_StorageFactory.storageEngines = [LocalStorage, AWSS3Storage];
|
|
25
20
|
let StorageFactory = _StorageFactory;
|
|
26
21
|
export {
|
|
27
22
|
StorageFactory
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/StorageFactory.
|
|
1
|
+
{"version":3,"sources":["../src/StorageFactory.mts"],"sourcesContent":["import { AWSS3Storage } from './AWSS3Storage.mjs';\nimport { LocalStorage } from './LocalStorage.mjs';\nimport { Storage } from './Storage.mjs';\nimport { StorageConfig } from './types.mjs';\n\nexport class StorageFactory {\n public static storageEngines: (typeof Storage)[] = [LocalStorage, AWSS3Storage];\n\n registerStorageEngine(engine: typeof Storage) {\n StorageFactory.storageEngines.push(engine);\n }\n\n public static create(config: StorageConfig): Storage {\n for (const engine of StorageFactory.storageEngines) {\n if (engine.canHandle(config)) {\n // @ts-ignore\n return new engine(config);\n }\n }\n throw new Error('No matchin storage engine found');\n }\n}\n"],"mappings":";;AAAA,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAItB,MAAM,kBAAN,MAAM,gBAAe;AAAA,EAG1B,sBAAsB,QAAwB;AAC5C,oBAAe,eAAe,KAAK,MAAM;AAAA,EAC3C;AAAA,EAEA,OAAc,OAAO,QAAgC;AACnD,eAAW,UAAU,gBAAe,gBAAgB;AAClD,UAAI,OAAO,UAAU,MAAM,GAAG;AAE5B,eAAO,IAAI,OAAO,MAAM;AAAA,MAC1B;AAAA,IACF;AACA,UAAM,IAAI,MAAM,iCAAiC;AAAA,EACnD;AACF;AAhB4B;AAAf,gBACG,iBAAqC,CAAC,cAAc,YAAY;AADzE,IAAM,iBAAN;","names":[]}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
6
13
|
var __copyProps = (to, from, except, desc) => {
|
|
7
14
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
15
|
for (let key of __getOwnPropNames(from))
|
|
@@ -11,21 +18,307 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
11
18
|
}
|
|
12
19
|
return to;
|
|
13
20
|
};
|
|
14
|
-
var
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
15
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
|
+
|
|
51
|
+
// src/index.ts
|
|
16
52
|
var index_exports = {};
|
|
53
|
+
__export(index_exports, {
|
|
54
|
+
AWSS3Storage: () => AWSS3Storage,
|
|
55
|
+
LocalStorage: () => LocalStorage,
|
|
56
|
+
Storage: () => Storage,
|
|
57
|
+
StorageFactory: () => StorageFactory
|
|
58
|
+
});
|
|
17
59
|
module.exports = __toCommonJS(index_exports);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
60
|
+
|
|
61
|
+
// src/Storage.mts
|
|
62
|
+
var _Storage = class _Storage {
|
|
63
|
+
constructor(config) {
|
|
64
|
+
this.config = config;
|
|
65
|
+
}
|
|
66
|
+
static canHandle(config) {
|
|
67
|
+
throw new Error("Method not implemented.");
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
__name(_Storage, "Storage");
|
|
71
|
+
var Storage = _Storage;
|
|
72
|
+
|
|
73
|
+
// src/AWSS3Storage.mts
|
|
74
|
+
var import_client_s3 = require("@aws-sdk/client-s3");
|
|
75
|
+
var import_stream = __toESM(require("stream"), 1);
|
|
76
|
+
var _AWSS3Storage = class _AWSS3Storage extends Storage {
|
|
77
|
+
constructor(config) {
|
|
78
|
+
var _a;
|
|
79
|
+
super(config);
|
|
80
|
+
this.config = config;
|
|
81
|
+
if (!_AWSS3Storage.canHandle(config)) {
|
|
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) || {});
|
|
85
|
+
}
|
|
86
|
+
static canHandle(config) {
|
|
87
|
+
return config.engine === "s3";
|
|
88
|
+
}
|
|
89
|
+
exists(path2) {
|
|
90
|
+
return __async(this, null, function* () {
|
|
91
|
+
var _a;
|
|
92
|
+
try {
|
|
93
|
+
yield this.s3.send(new import_client_s3.HeadObjectCommand({ Bucket: (_a = this.config) == null ? void 0 : _a.bucket, Key: path2 }));
|
|
94
|
+
return true;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (error.name === "NotFound") {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
put(path2, content) {
|
|
104
|
+
return __async(this, null, function* () {
|
|
105
|
+
let body;
|
|
106
|
+
if (typeof content === "string" || content instanceof Buffer) {
|
|
107
|
+
body = content;
|
|
108
|
+
} else if (typeof content === "object" && !(content instanceof import_stream.default)) {
|
|
109
|
+
body = JSON.stringify(content);
|
|
110
|
+
} else if (content instanceof import_stream.default) {
|
|
111
|
+
body = content;
|
|
112
|
+
} else {
|
|
113
|
+
throw new Error("Unsupported content type");
|
|
114
|
+
}
|
|
115
|
+
yield this.s3.send(
|
|
116
|
+
new import_client_s3.PutObjectCommand({
|
|
117
|
+
Bucket: this.config.bucket,
|
|
118
|
+
Key: path2,
|
|
119
|
+
Body: body
|
|
120
|
+
})
|
|
121
|
+
);
|
|
122
|
+
return true;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
getJson(path2) {
|
|
126
|
+
return __async(this, null, function* () {
|
|
127
|
+
const data = yield this.s3.send(
|
|
128
|
+
new import_client_s3.GetObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
129
|
+
);
|
|
130
|
+
const body = yield this.streamToString(data.Body);
|
|
131
|
+
return JSON.parse(body);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
getString(path2) {
|
|
135
|
+
return __async(this, null, function* () {
|
|
136
|
+
const data = yield this.s3.send(
|
|
137
|
+
new import_client_s3.GetObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
138
|
+
);
|
|
139
|
+
return yield this.streamToString(data.Body);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
delete(path2) {
|
|
143
|
+
return __async(this, null, function* () {
|
|
144
|
+
yield this.s3.send(new import_client_s3.DeleteObjectCommand({ Bucket: this.config.bucket, Key: path2 }));
|
|
145
|
+
return true;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
streamToString(stream) {
|
|
149
|
+
return __async(this, null, function* () {
|
|
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
|
+
);
|
|
163
|
+
const chunks = [];
|
|
164
|
+
const stream = data.Body;
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
167
|
+
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
168
|
+
stream.on("error", reject);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
getStream(path2) {
|
|
173
|
+
return __async(this, null, function* () {
|
|
174
|
+
const data = yield this.s3.send(
|
|
175
|
+
new import_client_s3.GetObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
176
|
+
);
|
|
177
|
+
return data.Body;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
metadata(path2) {
|
|
181
|
+
return __async(this, null, function* () {
|
|
182
|
+
const metadata = yield this.s3.send(
|
|
183
|
+
new import_client_s3.HeadObjectCommand({ Bucket: this.config.bucket, Key: path2 })
|
|
184
|
+
);
|
|
185
|
+
return {
|
|
186
|
+
size: metadata.ContentLength || 0,
|
|
187
|
+
mimeType: metadata.ContentType || "unknown",
|
|
188
|
+
lastModifiedDate: (metadata.LastModified || /* @__PURE__ */ new Date(0)).toISOString()
|
|
189
|
+
};
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
__name(_AWSS3Storage, "AWSS3Storage");
|
|
194
|
+
var AWSS3Storage = _AWSS3Storage;
|
|
195
|
+
|
|
196
|
+
// src/LocalStorage.mts
|
|
197
|
+
var import_stream2 = __toESM(require("stream"), 1);
|
|
198
|
+
var fs = __toESM(require("fs/promises"), 1);
|
|
199
|
+
var import_fs = require("fs");
|
|
200
|
+
var path = __toESM(require("path"), 1);
|
|
201
|
+
var mime = __toESM(require("mime-types"), 1);
|
|
202
|
+
var _LocalStorage = class _LocalStorage extends Storage {
|
|
203
|
+
constructor(config) {
|
|
204
|
+
super(config);
|
|
205
|
+
if (!_LocalStorage.canHandle(config)) {
|
|
206
|
+
throw new Error(`storage engine cannot handle this config.`);
|
|
207
|
+
}
|
|
208
|
+
fs.mkdir(this.config.basePath, { recursive: true }).catch((error) => {
|
|
209
|
+
throw error;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
metadata(path2) {
|
|
213
|
+
return __async(this, null, function* () {
|
|
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
|
+
});
|
|
222
|
+
}
|
|
223
|
+
static canHandle(config) {
|
|
224
|
+
if (config.engine === "local") {
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
getFullPath(filePath) {
|
|
230
|
+
return path.join(this.config.basePath, filePath);
|
|
231
|
+
}
|
|
232
|
+
exists(path2) {
|
|
233
|
+
return __async(this, null, function* () {
|
|
234
|
+
try {
|
|
235
|
+
yield fs.access(this.getFullPath(path2));
|
|
236
|
+
return true;
|
|
237
|
+
} catch (e) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
put(filepath, content) {
|
|
243
|
+
return __async(this, null, function* () {
|
|
244
|
+
const fullPath = this.getFullPath(filepath);
|
|
245
|
+
const dir = path.dirname(fullPath);
|
|
246
|
+
yield fs.mkdir(dir, { recursive: true });
|
|
247
|
+
if (typeof content === "string" || content instanceof Buffer) {
|
|
248
|
+
yield fs.writeFile(fullPath, content);
|
|
249
|
+
} else if (typeof content === "object" && !(content instanceof import_stream2.default)) {
|
|
250
|
+
yield fs.writeFile(fullPath, JSON.stringify(content, null, 2));
|
|
251
|
+
} else if (typeof content === "object" && content instanceof import_stream2.default) {
|
|
252
|
+
const writeStream = (0, import_fs.createWriteStream)(fullPath);
|
|
253
|
+
yield new Promise((resolve, reject) => {
|
|
254
|
+
content.pipe(writeStream);
|
|
255
|
+
content.on("end", resolve);
|
|
256
|
+
content.on("error", reject);
|
|
257
|
+
});
|
|
258
|
+
} else {
|
|
259
|
+
throw new Error("Unsupported content type");
|
|
260
|
+
}
|
|
261
|
+
return true;
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
getJson(path2) {
|
|
265
|
+
return __async(this, null, function* () {
|
|
266
|
+
const fullPath = this.getFullPath(path2);
|
|
267
|
+
const content = yield fs.readFile(fullPath, "utf-8");
|
|
268
|
+
return JSON.parse(content);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
getString(path2, encoding = "utf-8") {
|
|
272
|
+
return __async(this, null, function* () {
|
|
273
|
+
const fullPath = this.getFullPath(path2);
|
|
274
|
+
return yield fs.readFile(fullPath, encoding);
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
getBuffer(path2) {
|
|
278
|
+
return __async(this, null, function* () {
|
|
279
|
+
const fullPath = this.getFullPath(path2);
|
|
280
|
+
return yield fs.readFile(fullPath);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
getStream(path2) {
|
|
284
|
+
return __async(this, null, function* () {
|
|
285
|
+
const fullPath = this.getFullPath(path2);
|
|
286
|
+
return (0, import_fs.createReadStream)(fullPath);
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
delete(path2) {
|
|
290
|
+
return __async(this, null, function* () {
|
|
291
|
+
const fullPath = this.getFullPath(path2);
|
|
292
|
+
yield fs.unlink(fullPath);
|
|
293
|
+
return true;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
__name(_LocalStorage, "LocalStorage");
|
|
298
|
+
var LocalStorage = _LocalStorage;
|
|
299
|
+
|
|
300
|
+
// src/StorageFactory.mts
|
|
301
|
+
var _StorageFactory = class _StorageFactory {
|
|
302
|
+
registerStorageEngine(engine) {
|
|
303
|
+
_StorageFactory.storageEngines.push(engine);
|
|
304
|
+
}
|
|
305
|
+
static create(config) {
|
|
306
|
+
for (const engine of _StorageFactory.storageEngines) {
|
|
307
|
+
if (engine.canHandle(config)) {
|
|
308
|
+
return new engine(config);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
throw new Error("No matchin storage engine found");
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
__name(_StorageFactory, "StorageFactory");
|
|
315
|
+
_StorageFactory.storageEngines = [LocalStorage, AWSS3Storage];
|
|
316
|
+
var StorageFactory = _StorageFactory;
|
|
23
317
|
// Annotate the CommonJS export names for ESM import in node:
|
|
24
318
|
0 && (module.exports = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
...require("./StorageFactory")
|
|
319
|
+
AWSS3Storage,
|
|
320
|
+
LocalStorage,
|
|
321
|
+
Storage,
|
|
322
|
+
StorageFactory
|
|
30
323
|
});
|
|
31
324
|
//# sourceMappingURL=index.js.map
|