@fengsoft/storage-core-adapter-memory 0.1.0 → 0.1.1
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/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +100 -0
- package/dist/index.js.map +1 -0
- package/package.json +2 -2
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type CreatePresignedUploadInput, type DeleteObjectInput, type GetObjectResult, type HeadObjectResult, type ListObjectsInput, type ListObjectsResult, type PresignedUpload, type PutObjectInput, type StorageDriver } from "@fengsoft/storage-core";
|
|
2
|
+
export type MemoryStorageDriverOptions = {
|
|
3
|
+
publicBaseUrl?: string;
|
|
4
|
+
baseUrl?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare class MemoryStorageDriver implements StorageDriver {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(options?: MemoryStorageDriverOptions);
|
|
9
|
+
putObject(input: PutObjectInput): Promise<HeadObjectResult>;
|
|
10
|
+
deleteObject(input: DeleteObjectInput): Promise<void>;
|
|
11
|
+
headObject(input: DeleteObjectInput): Promise<HeadObjectResult>;
|
|
12
|
+
getObject(input: DeleteObjectInput): Promise<GetObjectResult>;
|
|
13
|
+
listObjects(input?: ListObjectsInput): Promise<ListObjectsResult>;
|
|
14
|
+
createPresignedUpload(input: CreatePresignedUploadInput): Promise<PresignedUpload>;
|
|
15
|
+
resolvePublicUrl(key: string): string | null;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,0BAA0B,EAC/B,KAAK,iBAAiB,EAEtB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,aAAa,EAKlB,MAAM,wBAAwB,CAAC;AAWhC,MAAM,MAAM,0BAA0B,GAAG;IACxC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,qBAAa,mBAAoB,YAAW,aAAa;;gBAK5C,OAAO,GAAE,0BAA+B;IAK9C,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAe3D,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrD,UAAU,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAK/D,SAAS,CAAC,KAAK,EAAE,iBAAiB,GAAG,OAAO,CAAC,eAAe,CAAC;IAQ7D,WAAW,CAAC,KAAK,GAAE,gBAAqB,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAqBrE,qBAAqB,CAC1B,KAAK,EAAE,0BAA0B,GAC/B,OAAO,CAAC,eAAe,CAAC;IAqB3B,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;CA0B5C"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { buildPublicUrl, decodeCursor, StorageObjectNotFoundError, toBuffer, toCursor, } from "@fengsoft/storage-core";
|
|
2
|
+
export class MemoryStorageDriver {
|
|
3
|
+
#objects = new Map();
|
|
4
|
+
#publicBaseUrl;
|
|
5
|
+
#baseUrl;
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.#publicBaseUrl = options.publicBaseUrl ?? null;
|
|
8
|
+
this.#baseUrl = options.baseUrl ?? "memory://storage";
|
|
9
|
+
}
|
|
10
|
+
async putObject(input) {
|
|
11
|
+
const body = toBuffer(input.body);
|
|
12
|
+
const lastModified = new Date();
|
|
13
|
+
const stored = {
|
|
14
|
+
body,
|
|
15
|
+
contentType: input.contentType ?? null,
|
|
16
|
+
cacheControl: input.cacheControl ?? null,
|
|
17
|
+
metadata: { ...(input.metadata ?? {}) },
|
|
18
|
+
etag: `memory-${input.key}-${body.length}-${lastModified.getTime()}`,
|
|
19
|
+
lastModified,
|
|
20
|
+
};
|
|
21
|
+
this.#objects.set(input.key, stored);
|
|
22
|
+
return this.#toHeadObjectResult(input.key, stored);
|
|
23
|
+
}
|
|
24
|
+
async deleteObject(input) {
|
|
25
|
+
this.#objects.delete(input.key);
|
|
26
|
+
}
|
|
27
|
+
async headObject(input) {
|
|
28
|
+
const stored = this.#requireObject(input.key);
|
|
29
|
+
return this.#toHeadObjectResult(input.key, stored);
|
|
30
|
+
}
|
|
31
|
+
async getObject(input) {
|
|
32
|
+
const stored = this.#requireObject(input.key);
|
|
33
|
+
return {
|
|
34
|
+
...this.#toHeadObjectResult(input.key, stored),
|
|
35
|
+
body: Buffer.from(stored.body),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
async listObjects(input = {}) {
|
|
39
|
+
const prefix = input.prefix ?? "";
|
|
40
|
+
const limit = input.limit ?? 1000;
|
|
41
|
+
const offset = decodeCursor(input.cursor);
|
|
42
|
+
const filtered = [...this.#objects.entries()]
|
|
43
|
+
.filter(([key]) => key.startsWith(prefix))
|
|
44
|
+
.sort(([left], [right]) => left.localeCompare(right));
|
|
45
|
+
const items = filtered
|
|
46
|
+
.slice(offset, offset + limit)
|
|
47
|
+
.map(([key, stored]) => ({
|
|
48
|
+
key,
|
|
49
|
+
contentLength: stored.body.length,
|
|
50
|
+
lastModified: stored.lastModified,
|
|
51
|
+
etag: stored.etag,
|
|
52
|
+
}));
|
|
53
|
+
return {
|
|
54
|
+
items,
|
|
55
|
+
nextCursor: toCursor(offset + items.length, filtered.length),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async createPresignedUpload(input) {
|
|
59
|
+
const expiresInSeconds = input.expiresInSeconds ?? 900;
|
|
60
|
+
const headers = {};
|
|
61
|
+
if (input.contentType) {
|
|
62
|
+
headers["content-type"] = input.contentType;
|
|
63
|
+
}
|
|
64
|
+
if (input.cacheControl) {
|
|
65
|
+
headers["cache-control"] = input.cacheControl;
|
|
66
|
+
}
|
|
67
|
+
for (const [key, value] of Object.entries(input.metadata ?? {})) {
|
|
68
|
+
headers[`x-storage-meta-${key}`] = value;
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
key: input.key,
|
|
72
|
+
method: "PUT",
|
|
73
|
+
url: `${this.#baseUrl}/${input.key}`,
|
|
74
|
+
headers,
|
|
75
|
+
expiresAt: new Date(Date.now() + expiresInSeconds * 1000),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
resolvePublicUrl(key) {
|
|
79
|
+
return buildPublicUrl(this.#publicBaseUrl, key);
|
|
80
|
+
}
|
|
81
|
+
#requireObject(key) {
|
|
82
|
+
const stored = this.#objects.get(key);
|
|
83
|
+
if (!stored) {
|
|
84
|
+
throw new StorageObjectNotFoundError(key);
|
|
85
|
+
}
|
|
86
|
+
return stored;
|
|
87
|
+
}
|
|
88
|
+
#toHeadObjectResult(key, stored) {
|
|
89
|
+
return {
|
|
90
|
+
key,
|
|
91
|
+
contentLength: stored.body.length,
|
|
92
|
+
contentType: stored.contentType,
|
|
93
|
+
cacheControl: stored.cacheControl,
|
|
94
|
+
etag: stored.etag,
|
|
95
|
+
lastModified: stored.lastModified,
|
|
96
|
+
metadata: { ...stored.metadata },
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EAGd,YAAY,EASZ,0BAA0B,EAC1B,QAAQ,EACR,QAAQ,GACR,MAAM,wBAAwB,CAAC;AAgBhC,MAAM,OAAO,mBAAmB;IACtB,QAAQ,GAAG,IAAI,GAAG,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IACf,CAAC;IAED,mBAAmB,CAClB,GAAW,EACX,MAA0B;QAE1B,OAAO;YACN,GAAG;YACH,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM;YACjC,WAAW,EAAE,MAAM,CAAC,WAAW;YAC/B,YAAY,EAAE,MAAM,CAAC,YAAY;YACjC,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,YAAY,EAAE,MAAM,CAAC,YAAY;YACjC,QAAQ,EAAE,EAAE,GAAG,MAAM,CAAC,QAAQ,EAAE;SAChC,CAAC;IACH,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fengsoft/storage-core-adapter-memory",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"testing"
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@fengsoft/storage-core": "0.1.
|
|
34
|
+
"@fengsoft/storage-core": "0.1.1"
|
|
35
35
|
},
|
|
36
36
|
"publishConfig": {
|
|
37
37
|
"access": "public"
|