@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/LocalStorage.mjs
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
var __async = (__this, __arguments, generator) => {
|
|
4
|
-
return new Promise((resolve, reject) => {
|
|
5
|
-
var fulfilled = (value) => {
|
|
6
|
-
try {
|
|
7
|
-
step(generator.next(value));
|
|
8
|
-
} catch (e) {
|
|
9
|
-
reject(e);
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
var rejected = (value) => {
|
|
13
|
-
try {
|
|
14
|
-
step(generator.throw(value));
|
|
15
|
-
} catch (e) {
|
|
16
|
-
reject(e);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
20
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
import Stream from "stream";
|
|
24
|
-
import * as fs from "fs/promises";
|
|
25
|
-
import { createWriteStream, createReadStream } from "fs";
|
|
26
|
-
import * as path from "path";
|
|
27
|
-
import * as mime from "mime-types";
|
|
28
|
-
import { Storage } from "./Storage.mjs";
|
|
29
|
-
const _LocalStorage = class _LocalStorage extends Storage {
|
|
30
|
-
constructor(config) {
|
|
31
|
-
super(config);
|
|
32
|
-
if (!_LocalStorage.canHandle(config)) {
|
|
33
|
-
throw new Error(`storage engine cannot handle this config.`);
|
|
34
|
-
}
|
|
35
|
-
fs.mkdir(this.config.basePath, { recursive: true }).catch((error) => {
|
|
36
|
-
throw error;
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
metadata(path2) {
|
|
40
|
-
return __async(this, null, function* () {
|
|
41
|
-
const fullPath = this.getFullPath(path2);
|
|
42
|
-
const stats = yield fs.stat(fullPath);
|
|
43
|
-
return {
|
|
44
|
-
size: stats.size,
|
|
45
|
-
mimeType: mime.lookup(fullPath) || "unknown",
|
|
46
|
-
lastModifiedDate: stats.mtime.toISOString()
|
|
47
|
-
};
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
static canHandle(config) {
|
|
51
|
-
if (config.engine === "local") {
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
getFullPath(filePath) {
|
|
57
|
-
return path.join(this.config.basePath, filePath);
|
|
58
|
-
}
|
|
59
|
-
exists(path2) {
|
|
60
|
-
return __async(this, null, function* () {
|
|
61
|
-
try {
|
|
62
|
-
yield fs.access(this.getFullPath(path2));
|
|
63
|
-
return true;
|
|
64
|
-
} catch (e) {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
put(path2, content) {
|
|
70
|
-
return __async(this, null, function* () {
|
|
71
|
-
const fullPath = this.getFullPath(path2);
|
|
72
|
-
if (typeof content === "string" || content instanceof Buffer) {
|
|
73
|
-
yield fs.writeFile(fullPath, content);
|
|
74
|
-
} else if (typeof content === "object" && !(content instanceof Stream)) {
|
|
75
|
-
yield fs.writeFile(fullPath, JSON.stringify(content, null, 2));
|
|
76
|
-
} else if (typeof content === "object" && content instanceof Stream) {
|
|
77
|
-
const writeStream = createWriteStream(fullPath);
|
|
78
|
-
yield new Promise((resolve, reject) => {
|
|
79
|
-
content.pipe(writeStream);
|
|
80
|
-
content.on("end", resolve);
|
|
81
|
-
content.on("error", reject);
|
|
82
|
-
});
|
|
83
|
-
} else {
|
|
84
|
-
throw new Error("Unsupported content type");
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
getJson(path2) {
|
|
90
|
-
return __async(this, null, function* () {
|
|
91
|
-
const fullPath = this.getFullPath(path2);
|
|
92
|
-
const content = yield fs.readFile(fullPath, "utf-8");
|
|
93
|
-
return JSON.parse(content);
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
getString(path2, encoding = "utf-8") {
|
|
97
|
-
return __async(this, null, function* () {
|
|
98
|
-
const fullPath = this.getFullPath(path2);
|
|
99
|
-
return yield fs.readFile(fullPath, encoding);
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
getBuffer(path2) {
|
|
103
|
-
return __async(this, null, function* () {
|
|
104
|
-
const fullPath = this.getFullPath(path2);
|
|
105
|
-
return yield fs.readFile(fullPath);
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
getStream(path2) {
|
|
109
|
-
return __async(this, null, function* () {
|
|
110
|
-
const fullPath = this.getFullPath(path2);
|
|
111
|
-
return createReadStream(fullPath);
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
delete(path2) {
|
|
115
|
-
return __async(this, null, function* () {
|
|
116
|
-
const fullPath = this.getFullPath(path2);
|
|
117
|
-
yield fs.unlink(fullPath);
|
|
118
|
-
return true;
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
__name(_LocalStorage, "LocalStorage");
|
|
123
|
-
let LocalStorage = _LocalStorage;
|
|
124
|
-
export {
|
|
125
|
-
LocalStorage
|
|
126
|
-
};
|
|
127
|
-
//# sourceMappingURL=LocalStorage.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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(path: string, content: string | object | Stream | Buffer): Promise<boolean> {\n const fullPath = this.getFullPath(path);\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,EAC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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;AA1F0C;AAAnC,IAAM,eAAN;","names":["path"]}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Storage } from './Storage.mjs';
|
|
2
|
-
import { StorageConfig } from './types.mjs';
|
|
3
|
-
import 'fs';
|
|
4
|
-
import 'stream';
|
|
5
|
-
import '@aws-sdk/client-s3';
|
|
6
|
-
|
|
7
|
-
declare class StorageFactory {
|
|
8
|
-
static storageEngines: (typeof Storage)[];
|
|
9
|
-
registerStorageEngine(engine: typeof Storage): void;
|
|
10
|
-
static create(config: StorageConfig): Storage;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export { StorageFactory };
|
package/dist/StorageFactory.mjs
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
import { AWSS3Storage } from "./AWSS3Storage.mjs";
|
|
4
|
-
import { LocalStorage } from "./LocalStorage.mjs";
|
|
5
|
-
const _StorageFactory = class _StorageFactory {
|
|
6
|
-
registerStorageEngine(engine) {
|
|
7
|
-
_StorageFactory.storageEngines.push(engine);
|
|
8
|
-
}
|
|
9
|
-
static create(config) {
|
|
10
|
-
for (const engine of _StorageFactory.storageEngines) {
|
|
11
|
-
if (engine.canHandle(config)) {
|
|
12
|
-
return new engine(config);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
throw new Error("No matchin storage engine found");
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
__name(_StorageFactory, "StorageFactory");
|
|
19
|
-
_StorageFactory.storageEngines = [LocalStorage, AWSS3Storage];
|
|
20
|
-
let StorageFactory = _StorageFactory;
|
|
21
|
-
export {
|
|
22
|
-
StorageFactory
|
|
23
|
-
};
|
|
24
|
-
//# sourceMappingURL=StorageFactory.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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":[]}
|