@unikvs/memory 0.0.9 → 0.0.12
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/README.md +3 -0
- package/dist/src/errors.d.ts +28 -12
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +16 -9
- package/dist/src/memory.d.ts +43 -9
- package/dist/src/memory.d.ts.map +1 -1
- package/dist/src/memory.js +32 -12
- package/package.json +16 -15
- package/src/errors.ts +36 -17
- package/src/memory.ts +50 -13
package/README.md
ADDED
package/dist/src/errors.d.ts
CHANGED
|
@@ -1,38 +1,54 @@
|
|
|
1
1
|
import { ErrorBase, type ErrorOptions } from "@unikvs/core";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
4
4
|
*/
|
|
5
5
|
export type KeyNotFoundErrorMeta = {
|
|
6
|
+
/**
|
|
7
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
8
|
+
*/
|
|
6
9
|
readonly key: string;
|
|
7
10
|
};
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
12
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
10
13
|
*/
|
|
11
|
-
export type KeyNotFoundErrorArgs = KeyNotFoundErrorMeta;
|
|
14
|
+
export type KeyNotFoundErrorArgs = ErrorOptions & KeyNotFoundErrorMeta;
|
|
12
15
|
/**
|
|
13
|
-
*
|
|
16
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
14
17
|
*/
|
|
15
18
|
export declare class KeyNotFoundError extends ErrorBase<KeyNotFoundErrorMeta> {
|
|
16
|
-
|
|
19
|
+
/**
|
|
20
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
21
|
+
*/
|
|
22
|
+
constructor(args: KeyNotFoundErrorArgs);
|
|
17
23
|
}
|
|
18
24
|
/**
|
|
19
|
-
*
|
|
25
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
20
26
|
*/
|
|
21
27
|
export type InvalidChunkTypeErrorMeta = {
|
|
28
|
+
/**
|
|
29
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
30
|
+
*/
|
|
22
31
|
readonly key: string;
|
|
32
|
+
/**
|
|
33
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
34
|
+
*/
|
|
23
35
|
readonly chunk: unknown;
|
|
36
|
+
/**
|
|
37
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
38
|
+
*/
|
|
24
39
|
readonly chunkType: string;
|
|
25
40
|
};
|
|
26
41
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* `chunkType` はコンストラクター内部で自動的に設定されるため、引数からは除外されています。
|
|
42
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
30
43
|
*/
|
|
31
|
-
export type InvalidChunkTypeErrorArgs = Omit<InvalidChunkTypeErrorMeta, "chunkType">;
|
|
44
|
+
export type InvalidChunkTypeErrorArgs = ErrorOptions & Omit<InvalidChunkTypeErrorMeta, "chunkType">;
|
|
32
45
|
/**
|
|
33
|
-
*
|
|
46
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
34
47
|
*/
|
|
35
48
|
export declare class InvalidChunkTypeError extends ErrorBase<InvalidChunkTypeErrorMeta> {
|
|
36
|
-
|
|
49
|
+
/**
|
|
50
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
51
|
+
*/
|
|
52
|
+
constructor(args: InvalidChunkTypeErrorArgs);
|
|
37
53
|
}
|
|
38
54
|
//# sourceMappingURL=errors.d.ts.map
|
package/dist/src/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,YAAY,EAAmB,MAAM,cAAc,CAAC;AAG7E;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,YAAY,EAAmB,MAAM,cAAc,CAAC;AAG7E;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,YAAY,GAAG,oBAAoB,CAAC;AAEvE;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,SAAS,CAAC,oBAAoB,CAAC;IAKnE;;OAEG;IACH,YAAmB,IAAI,EAAE,oBAAoB,EAI5C;CACF;AAID;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IAExB;;OAEG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG,YAAY,GAAG,IAAI,CAAC,yBAAyB,EAAE,WAAW,CAAC,CAAC;AAEpG;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,SAAS,CAAC,yBAAyB,CAAC;IAK7E;;OAEG;IACH,YAAmB,IAAI,EAAE,yBAAyB,EASjD;CACF"}
|
package/dist/src/errors.js
CHANGED
|
@@ -2,11 +2,16 @@ var _a, _b;
|
|
|
2
2
|
import { ErrorBase, setErrorMessage } from "@unikvs/core";
|
|
3
3
|
import getTypeName from "type-name";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
6
6
|
*/
|
|
7
7
|
export class KeyNotFoundError extends ErrorBase {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
10
|
+
*/
|
|
11
|
+
constructor(args) {
|
|
12
|
+
const { key, ...options } = args;
|
|
13
|
+
const meta = { key };
|
|
14
|
+
super(meta, ({ key }) => `Key not found: ${key}`, options);
|
|
10
15
|
}
|
|
11
16
|
}
|
|
12
17
|
_a = KeyNotFoundError;
|
|
@@ -15,14 +20,16 @@ _a = KeyNotFoundError;
|
|
|
15
20
|
})();
|
|
16
21
|
setErrorMessage(KeyNotFoundError, ({ key }) => `キー ${key} が見つかりません`, "ja");
|
|
17
22
|
/**
|
|
18
|
-
*
|
|
23
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
19
24
|
*/
|
|
20
25
|
export class InvalidChunkTypeError extends ErrorBase {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
/**
|
|
27
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
28
|
+
*/
|
|
29
|
+
constructor(args) {
|
|
30
|
+
const { key, chunk, ...options } = args;
|
|
31
|
+
const meta = { key, chunk, chunkType: getTypeName(chunk) };
|
|
32
|
+
super(meta, ({ key, chunkType }) => `Expected chunk for key ${JSON.stringify(key)} is Uint8Array<ArrayBuffer>, but got ${chunkType}`, options);
|
|
26
33
|
}
|
|
27
34
|
}
|
|
28
35
|
_b = InvalidChunkTypeError;
|
package/dist/src/memory.d.ts
CHANGED
|
@@ -1,30 +1,64 @@
|
|
|
1
1
|
import type { IStorage } from "@unikvs/core";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
4
|
+
*/
|
|
5
|
+
export type MemoryOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
8
|
+
*/
|
|
9
|
+
readonly clone?: (<T>(value: T) => T) | undefined;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
7
13
|
*/
|
|
8
14
|
export default class Memory implements IStorage {
|
|
9
15
|
/**
|
|
10
16
|
* キーと値のペアを保持する内部マップです。
|
|
11
17
|
*/
|
|
12
18
|
private readonly map;
|
|
13
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
21
|
+
*/
|
|
14
22
|
readonly name: string;
|
|
15
23
|
/**
|
|
16
|
-
*
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
* データの複製に使用する関数です。
|
|
25
|
+
*/
|
|
26
|
+
private readonly clone;
|
|
27
|
+
/**
|
|
28
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
29
|
+
*/
|
|
30
|
+
constructor(options?: MemoryOptions);
|
|
31
|
+
/**
|
|
32
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
19
33
|
*/
|
|
20
|
-
constructor();
|
|
21
34
|
get isOpen(): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
37
|
+
*/
|
|
22
38
|
write(args: Pick<IStorage.WriteArgs<any>, "key" | "data">): void;
|
|
39
|
+
/**
|
|
40
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
41
|
+
*/
|
|
23
42
|
read(args: Pick<IStorage.ReadArgs, "key">): any;
|
|
43
|
+
/**
|
|
44
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
45
|
+
*/
|
|
24
46
|
exists(args: Pick<IStorage.ExistsArgs, "key">): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
49
|
+
*/
|
|
25
50
|
delete(args: Pick<IStorage.DeleteArgs, "key">): void;
|
|
51
|
+
/**
|
|
52
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
53
|
+
*/
|
|
26
54
|
clear(): void;
|
|
55
|
+
/**
|
|
56
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
57
|
+
*/
|
|
27
58
|
getWritable(args: Pick<IStorage.GetWritableArgs, "key">): WritableStream<Uint8Array<ArrayBuffer>>;
|
|
59
|
+
/**
|
|
60
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
61
|
+
*/
|
|
28
62
|
getReadable(args: Pick<IStorage.GetReadableArgs, "key">): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
29
63
|
}
|
|
30
64
|
//# sourceMappingURL=memory.d.ts.map
|
package/dist/src/memory.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/memory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAI7C
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/memory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAI7C;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B;;OAEG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,SAAS,CAAC;CACnD,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,MAAO,YAAW,QAAQ;IAC7C;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAmB;IAEvC;;OAEG;IACH,SAAgB,IAAI,EAAE,MAAM,CAAC;IAE7B;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAqB;IAE3C;;OAEG;IACH,YAAmB,OAAO,GAAE,aAAkB,EAI7C;IAED;;OAEG;IACH,IAAW,MAAM,IAAI,OAAO,CAE3B;IAED;;OAEG;IACI,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,GAAG,IAAI,CAGtE;IAED;;OAEG;IACI,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC,GAAG,GAAG,CASrD;IAED;;OAEG;IACI,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,KAAK,CAAC,GAAG,OAAO,CAK7D;IAED;;OAEG;IACI,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,KAAK,CAAC,GAAG,IAAI,CAO1D;IAED;;OAEG;IACI,KAAK,IAAI,IAAI,CAEnB;IAED;;OAEG;IACI,WAAW,CAChB,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,eAAe,EAAE,KAAK,CAAC,GAC1C,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CA2BzC;IAED;;OAEG;IACI,WAAW,CAChB,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,eAAe,EAAE,KAAK,CAAC,GAC1C,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAoBzC;CACF"}
|
package/dist/src/memory.js
CHANGED
|
@@ -1,40 +1,51 @@
|
|
|
1
1
|
import { KeyNotFoundError, InvalidChunkTypeError } from "./errors.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* アプリケーションの実行中のみデータを保持し、プロセス終了時に破棄されます。
|
|
6
|
-
* 全ての操作が同期で完了するため、非同期処理のオーバーヘッドがありません。
|
|
3
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
7
4
|
*/
|
|
8
5
|
export default class Memory {
|
|
9
6
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* 内部のマップを初期化し、常にオープン状態として動作します。
|
|
7
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
13
8
|
*/
|
|
14
|
-
constructor() {
|
|
9
|
+
constructor(options = {}) {
|
|
15
10
|
this.name = "Memory";
|
|
16
11
|
this.map = new Map();
|
|
12
|
+
this.clone = options.clone ?? ((v) => structuredClone(v));
|
|
17
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
16
|
+
*/
|
|
18
17
|
get isOpen() {
|
|
19
18
|
return true;
|
|
20
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
22
|
+
*/
|
|
21
23
|
write(args) {
|
|
22
24
|
const { key, data } = args;
|
|
23
|
-
this.map.set(key, data);
|
|
25
|
+
this.map.set(key, this.clone(data));
|
|
24
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
29
|
+
*/
|
|
25
30
|
read(args) {
|
|
26
31
|
const { key } = args;
|
|
27
32
|
if (!this.map.has(key)) {
|
|
28
33
|
throw new KeyNotFoundError({ key });
|
|
29
34
|
}
|
|
30
35
|
const value = this.map.get(key);
|
|
31
|
-
return value;
|
|
36
|
+
return this.clone(value);
|
|
32
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
40
|
+
*/
|
|
33
41
|
exists(args) {
|
|
34
42
|
const { key } = args;
|
|
35
43
|
const exists = this.map.has(key);
|
|
36
44
|
return exists;
|
|
37
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
48
|
+
*/
|
|
38
49
|
delete(args) {
|
|
39
50
|
const { key } = args;
|
|
40
51
|
if (!this.map.has(key)) {
|
|
@@ -42,9 +53,15 @@ export default class Memory {
|
|
|
42
53
|
}
|
|
43
54
|
this.map.delete(key);
|
|
44
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
58
|
+
*/
|
|
45
59
|
clear() {
|
|
46
60
|
this.map.clear();
|
|
47
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
64
|
+
*/
|
|
48
65
|
getWritable(args) {
|
|
49
66
|
const { key } = args;
|
|
50
67
|
// メモリーストレージにはネイティブなストリームがないため、書き込まれたチャンクを配列に保持し、クローズ時に結合して保存します。
|
|
@@ -54,7 +71,7 @@ export default class Memory {
|
|
|
54
71
|
if (!(chunk instanceof Uint8Array)) {
|
|
55
72
|
throw new InvalidChunkTypeError({ key, chunk });
|
|
56
73
|
}
|
|
57
|
-
chunks.push(chunk);
|
|
74
|
+
chunks.push(this.clone(chunk));
|
|
58
75
|
},
|
|
59
76
|
close: () => {
|
|
60
77
|
const totalLength = chunks.reduce((sum, c) => sum + c.byteLength, 0);
|
|
@@ -69,6 +86,9 @@ export default class Memory {
|
|
|
69
86
|
});
|
|
70
87
|
return stream;
|
|
71
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
91
|
+
*/
|
|
72
92
|
getReadable(args) {
|
|
73
93
|
const { key } = args;
|
|
74
94
|
if (!this.map.has(key)) {
|
|
@@ -81,7 +101,7 @@ export default class Memory {
|
|
|
81
101
|
if (!(value instanceof Uint8Array)) {
|
|
82
102
|
throw new InvalidChunkTypeError({ key, chunk: value });
|
|
83
103
|
}
|
|
84
|
-
controller.enqueue(value);
|
|
104
|
+
controller.enqueue(this.clone(value));
|
|
85
105
|
controller.close();
|
|
86
106
|
},
|
|
87
107
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unikvs/memory",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.0.12",
|
|
4
|
+
"description": "Storage plugin that stores arbitrary data in memory.",
|
|
5
5
|
"homepage": "https://github.com/tai-kun/unikvs",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "tai-kun",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"dist",
|
|
13
13
|
"src",
|
|
14
14
|
"package.json",
|
|
15
|
+
"README.md",
|
|
15
16
|
"LICENSE"
|
|
16
17
|
],
|
|
17
18
|
"type": "module",
|
|
@@ -25,23 +26,23 @@
|
|
|
25
26
|
}
|
|
26
27
|
},
|
|
27
28
|
"dependencies": {
|
|
28
|
-
"@unikvs/core": "0.0.
|
|
29
|
-
"type-name": "
|
|
29
|
+
"@unikvs/core": "0.0.12",
|
|
30
|
+
"type-name": "2.0.2"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"@tsconfig/node24": "
|
|
33
|
-
"@tsconfig/strictest": "
|
|
34
|
-
"@types/node": "
|
|
35
|
-
"@types/type-name": "
|
|
36
|
-
"@vitest/browser": "
|
|
37
|
-
"@vitest/browser-playwright": "
|
|
38
|
-
"npm-check-updates": "
|
|
39
|
-
"playwright": "
|
|
40
|
-
"vite": "
|
|
41
|
-
"vitest": "
|
|
33
|
+
"@tsconfig/node24": "24.0.5",
|
|
34
|
+
"@tsconfig/strictest": "2.0.8",
|
|
35
|
+
"@types/node": "24.13.6",
|
|
36
|
+
"@types/type-name": "1.0.32",
|
|
37
|
+
"@vitest/browser": "4.1.11",
|
|
38
|
+
"@vitest/browser-playwright": "4.1.11",
|
|
39
|
+
"npm-check-updates": "20.0.2",
|
|
40
|
+
"playwright": "1.63.0",
|
|
41
|
+
"vite": "8.3.0",
|
|
42
|
+
"vitest": "4.1.11"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
|
44
|
-
"@logtape/logtape": "
|
|
45
|
+
"@logtape/logtape": "2.0.0"
|
|
45
46
|
},
|
|
46
47
|
"scripts": {
|
|
47
48
|
"build": "tsc --project ./.config/tsconfig.build.json"
|
package/src/errors.ts
CHANGED
|
@@ -2,62 +2,81 @@ import { ErrorBase, type ErrorOptions, setErrorMessage } from "@unikvs/core";
|
|
|
2
2
|
import getTypeName from "type-name";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
6
6
|
*/
|
|
7
7
|
export type KeyNotFoundErrorMeta = {
|
|
8
|
+
/**
|
|
9
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
10
|
+
*/
|
|
8
11
|
readonly key: string;
|
|
9
12
|
};
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
|
-
*
|
|
15
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
13
16
|
*/
|
|
14
|
-
export type KeyNotFoundErrorArgs = KeyNotFoundErrorMeta;
|
|
17
|
+
export type KeyNotFoundErrorArgs = ErrorOptions & KeyNotFoundErrorMeta;
|
|
15
18
|
|
|
16
19
|
/**
|
|
17
|
-
*
|
|
20
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
18
21
|
*/
|
|
19
22
|
export class KeyNotFoundError extends ErrorBase<KeyNotFoundErrorMeta> {
|
|
20
23
|
static {
|
|
21
24
|
this.prototype.name = "MemoryKeyNotFoundError";
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
/**
|
|
28
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
29
|
+
*/
|
|
30
|
+
public constructor(args: KeyNotFoundErrorArgs) {
|
|
31
|
+
const { key, ...options } = args;
|
|
32
|
+
const meta: KeyNotFoundErrorMeta = { key };
|
|
33
|
+
super(meta, ({ key }) => `Key not found: ${key}`, options);
|
|
26
34
|
}
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
setErrorMessage(KeyNotFoundError, ({ key }) => `キー ${key} が見つかりません`, "ja");
|
|
30
38
|
|
|
31
39
|
/**
|
|
32
|
-
*
|
|
40
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
33
41
|
*/
|
|
34
42
|
export type InvalidChunkTypeErrorMeta = {
|
|
43
|
+
/**
|
|
44
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
45
|
+
*/
|
|
35
46
|
readonly key: string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
50
|
+
*/
|
|
36
51
|
readonly chunk: unknown;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
55
|
+
*/
|
|
37
56
|
readonly chunkType: string;
|
|
38
57
|
};
|
|
39
58
|
|
|
40
59
|
/**
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* `chunkType` はコンストラクター内部で自動的に設定されるため、引数からは除外されています。
|
|
60
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
44
61
|
*/
|
|
45
|
-
export type InvalidChunkTypeErrorArgs = Omit<InvalidChunkTypeErrorMeta, "chunkType">;
|
|
62
|
+
export type InvalidChunkTypeErrorArgs = ErrorOptions & Omit<InvalidChunkTypeErrorMeta, "chunkType">;
|
|
46
63
|
|
|
47
64
|
/**
|
|
48
|
-
*
|
|
65
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
49
66
|
*/
|
|
50
67
|
export class InvalidChunkTypeError extends ErrorBase<InvalidChunkTypeErrorMeta> {
|
|
51
68
|
static {
|
|
52
69
|
this.prototype.name = "MemoryInvalidChunkTypeError";
|
|
53
70
|
}
|
|
54
71
|
|
|
55
|
-
|
|
72
|
+
/**
|
|
73
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
74
|
+
*/
|
|
75
|
+
public constructor(args: InvalidChunkTypeErrorArgs) {
|
|
76
|
+
const { key, chunk, ...options } = args;
|
|
77
|
+
const meta: InvalidChunkTypeErrorMeta = { key, chunk, chunkType: getTypeName(chunk) };
|
|
56
78
|
super(
|
|
57
|
-
|
|
58
|
-
...args,
|
|
59
|
-
chunkType: getTypeName(args.chunk),
|
|
60
|
-
},
|
|
79
|
+
meta,
|
|
61
80
|
({ key, chunkType }) =>
|
|
62
81
|
`Expected chunk for key ${JSON.stringify(key)} is Uint8Array<ArrayBuffer>, but got ${chunkType}`,
|
|
63
82
|
options,
|
package/src/memory.ts
CHANGED
|
@@ -3,10 +3,17 @@ import type { IStorage } from "@unikvs/core";
|
|
|
3
3
|
import { KeyNotFoundError, InvalidChunkTypeError } from "./errors.js";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
7
|
+
*/
|
|
8
|
+
export type MemoryOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
11
|
+
*/
|
|
12
|
+
readonly clone?: (<T>(value: T) => T) | undefined;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
10
17
|
*/
|
|
11
18
|
export default class Memory implements IStorage {
|
|
12
19
|
/**
|
|
@@ -14,28 +21,43 @@ export default class Memory implements IStorage {
|
|
|
14
21
|
*/
|
|
15
22
|
private readonly map: Map<string, any>;
|
|
16
23
|
|
|
17
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
26
|
+
*/
|
|
18
27
|
public readonly name: string;
|
|
19
28
|
|
|
20
29
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* 内部のマップを初期化し、常にオープン状態として動作します。
|
|
30
|
+
* データの複製に使用する関数です。
|
|
24
31
|
*/
|
|
25
|
-
|
|
32
|
+
private readonly clone: <T>(value: T) => T;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
36
|
+
*/
|
|
37
|
+
public constructor(options: MemoryOptions = {}) {
|
|
26
38
|
this.name = "Memory";
|
|
27
39
|
this.map = new Map();
|
|
40
|
+
this.clone = options.clone ?? ((v) => structuredClone(v));
|
|
28
41
|
}
|
|
29
42
|
|
|
43
|
+
/**
|
|
44
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
45
|
+
*/
|
|
30
46
|
public get isOpen(): boolean {
|
|
31
47
|
return true;
|
|
32
48
|
}
|
|
33
49
|
|
|
50
|
+
/**
|
|
51
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
52
|
+
*/
|
|
34
53
|
public write(args: Pick<IStorage.WriteArgs<any>, "key" | "data">): void {
|
|
35
54
|
const { key, data } = args;
|
|
36
|
-
this.map.set(key, data);
|
|
55
|
+
this.map.set(key, this.clone(data));
|
|
37
56
|
}
|
|
38
57
|
|
|
58
|
+
/**
|
|
59
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
60
|
+
*/
|
|
39
61
|
public read(args: Pick<IStorage.ReadArgs, "key">): any {
|
|
40
62
|
const { key } = args;
|
|
41
63
|
if (!this.map.has(key)) {
|
|
@@ -44,9 +66,12 @@ export default class Memory implements IStorage {
|
|
|
44
66
|
|
|
45
67
|
const value = this.map.get(key);
|
|
46
68
|
|
|
47
|
-
return value;
|
|
69
|
+
return this.clone(value);
|
|
48
70
|
}
|
|
49
71
|
|
|
72
|
+
/**
|
|
73
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
74
|
+
*/
|
|
50
75
|
public exists(args: Pick<IStorage.ExistsArgs, "key">): boolean {
|
|
51
76
|
const { key } = args;
|
|
52
77
|
const exists = this.map.has(key);
|
|
@@ -54,6 +79,9 @@ export default class Memory implements IStorage {
|
|
|
54
79
|
return exists;
|
|
55
80
|
}
|
|
56
81
|
|
|
82
|
+
/**
|
|
83
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
84
|
+
*/
|
|
57
85
|
public delete(args: Pick<IStorage.DeleteArgs, "key">): void {
|
|
58
86
|
const { key } = args;
|
|
59
87
|
if (!this.map.has(key)) {
|
|
@@ -63,10 +91,16 @@ export default class Memory implements IStorage {
|
|
|
63
91
|
this.map.delete(key);
|
|
64
92
|
}
|
|
65
93
|
|
|
94
|
+
/**
|
|
95
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
96
|
+
*/
|
|
66
97
|
public clear(): void {
|
|
67
98
|
this.map.clear();
|
|
68
99
|
}
|
|
69
100
|
|
|
101
|
+
/**
|
|
102
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
103
|
+
*/
|
|
70
104
|
public getWritable(
|
|
71
105
|
args: Pick<IStorage.GetWritableArgs, "key">,
|
|
72
106
|
): WritableStream<Uint8Array<ArrayBuffer>> {
|
|
@@ -79,7 +113,7 @@ export default class Memory implements IStorage {
|
|
|
79
113
|
throw new InvalidChunkTypeError({ key, chunk });
|
|
80
114
|
}
|
|
81
115
|
|
|
82
|
-
chunks.push(chunk);
|
|
116
|
+
chunks.push(this.clone(chunk));
|
|
83
117
|
},
|
|
84
118
|
close: () => {
|
|
85
119
|
const totalLength = chunks.reduce((sum, c) => sum + c.byteLength, 0);
|
|
@@ -98,6 +132,9 @@ export default class Memory implements IStorage {
|
|
|
98
132
|
return stream;
|
|
99
133
|
}
|
|
100
134
|
|
|
135
|
+
/**
|
|
136
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
137
|
+
*/
|
|
101
138
|
public getReadable(
|
|
102
139
|
args: Pick<IStorage.GetReadableArgs, "key">,
|
|
103
140
|
): ReadableStream<Uint8Array<ArrayBuffer>> {
|
|
@@ -114,7 +151,7 @@ export default class Memory implements IStorage {
|
|
|
114
151
|
throw new InvalidChunkTypeError({ key, chunk: value });
|
|
115
152
|
}
|
|
116
153
|
|
|
117
|
-
controller.enqueue(value as Uint8Array<ArrayBuffer>);
|
|
154
|
+
controller.enqueue(this.clone(value) as Uint8Array<ArrayBuffer>);
|
|
118
155
|
controller.close();
|
|
119
156
|
},
|
|
120
157
|
});
|