@unikvs/memory 0.0.12 → 0.0.14
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 +1 -1
- package/dist/index.d.ts +117 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +178 -0
- package/dist/index.js.map +1 -0
- package/package.json +20 -23
- package/src/errors.ts +19 -47
- package/src/index.ts +2 -2
- package/src/memory.ts +88 -64
- package/dist/src/errors.d.ts +0 -54
- package/dist/src/errors.d.ts.map +0 -1
- package/dist/src/errors.js +0 -39
- package/dist/src/index.d.ts +0 -5
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -2
- package/dist/src/memory.d.ts +0 -64
- package/dist/src/memory.d.ts.map +0 -1
- package/dist/src/memory.js +0 -110
package/README.md
CHANGED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ErrorBase, ErrorOptions, IStorage, KeyNotFoundError, KeyNotFoundErrorArgs, KeyNotFoundErrorMeta } from "@unikvs/core";
|
|
2
|
+
//#region ../src/errors.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
5
|
+
*/
|
|
6
|
+
type InvalidChunkTypeErrorMeta = {
|
|
7
|
+
/**
|
|
8
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-key)
|
|
9
|
+
*/
|
|
10
|
+
readonly key: string;
|
|
11
|
+
/**
|
|
12
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-chunk)
|
|
13
|
+
*/
|
|
14
|
+
readonly chunk: unknown;
|
|
15
|
+
/**
|
|
16
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-chunk-type)
|
|
17
|
+
*/
|
|
18
|
+
readonly chunkType: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
22
|
+
*/
|
|
23
|
+
type InvalidChunkTypeErrorArgs = ErrorOptions & Omit<InvalidChunkTypeErrorMeta, "chunkType">;
|
|
24
|
+
/**
|
|
25
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
26
|
+
*/
|
|
27
|
+
export declare class InvalidChunkTypeError extends ErrorBase<InvalidChunkTypeErrorMeta> {
|
|
28
|
+
/**
|
|
29
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
30
|
+
*/
|
|
31
|
+
constructor(args: InvalidChunkTypeErrorArgs);
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region ../src/memory.d.ts
|
|
35
|
+
/**
|
|
36
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
37
|
+
*/
|
|
38
|
+
type MemoryOptions = {
|
|
39
|
+
/**
|
|
40
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#clone)
|
|
41
|
+
*/
|
|
42
|
+
readonly clone?: (<T>(value: T) => T) | undefined;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
46
|
+
*/
|
|
47
|
+
declare class Memory implements IStorage {
|
|
48
|
+
#private;
|
|
49
|
+
/**
|
|
50
|
+
* ストレージ名です。常に `Memory` を返します。
|
|
51
|
+
*/
|
|
52
|
+
readonly name: string;
|
|
53
|
+
/**
|
|
54
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
55
|
+
*/
|
|
56
|
+
constructor(options?: MemoryOptions);
|
|
57
|
+
/**
|
|
58
|
+
* ストレージが利用可能かどうかを返します。Memory は常に利用可能です。
|
|
59
|
+
*
|
|
60
|
+
* @returns 常に true を返します。
|
|
61
|
+
*/
|
|
62
|
+
get isOpen(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* 指定したキーに値を保存します。保存前に値を複製します。
|
|
65
|
+
*
|
|
66
|
+
* @param c 書き込み対象のキーを持つコンテキストです。
|
|
67
|
+
* @param data 保存する値です。
|
|
68
|
+
*/
|
|
69
|
+
write(c: IStorage.WriteContext, data: any): void;
|
|
70
|
+
/**
|
|
71
|
+
* 指定したキーの値を読み出します。保存値を複製して返します。
|
|
72
|
+
*
|
|
73
|
+
* @param c 読み出すキーを持つコンテキストです。
|
|
74
|
+
*
|
|
75
|
+
* @returns 複製した保存値です。
|
|
76
|
+
*/
|
|
77
|
+
read(c: IStorage.ReadContext): any;
|
|
78
|
+
/**
|
|
79
|
+
* 指定したキーが存在するかどうかを返します。
|
|
80
|
+
*
|
|
81
|
+
* @param c 確認するキーを持つコンテキストです。
|
|
82
|
+
*
|
|
83
|
+
* @returns キーが存在すれば true を返します。
|
|
84
|
+
*/
|
|
85
|
+
exists(c: IStorage.ExistsContext): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* 指定したキーを削除します。
|
|
88
|
+
*
|
|
89
|
+
* @param c 削除するキーを持つコンテキストです。
|
|
90
|
+
*/
|
|
91
|
+
delete(c: IStorage.DeleteContext): void;
|
|
92
|
+
/**
|
|
93
|
+
* すべてのキーを削除します。
|
|
94
|
+
*
|
|
95
|
+
* @param _c 使用しないクリア用コンテキストです。
|
|
96
|
+
*/
|
|
97
|
+
clear(_c: IStorage.ClearContext): void;
|
|
98
|
+
/**
|
|
99
|
+
* Uint8Array を書き込むためのストリームを作成します。close 時にチャンクを結合して保存します。
|
|
100
|
+
*
|
|
101
|
+
* @param c 書き込み対象のキーを持つコンテキストです。
|
|
102
|
+
*
|
|
103
|
+
* @returns Uint8Array を受け付ける書き込みストリームです。
|
|
104
|
+
*/
|
|
105
|
+
getWritable(c: IStorage.GetWritableContext): WritableStream<Uint8Array<ArrayBuffer>>;
|
|
106
|
+
/**
|
|
107
|
+
* 保存済みの値を単一チャンクとして送出するストリームを作成します。
|
|
108
|
+
*
|
|
109
|
+
* @param c 読み出すキーを持つコンテキストです。
|
|
110
|
+
*
|
|
111
|
+
* @returns Uint8Array を送出する読み取りストリームです。
|
|
112
|
+
*/
|
|
113
|
+
getReadable(c: IStorage.GetReadableContext): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
export { type InvalidChunkTypeErrorArgs, type InvalidChunkTypeErrorMeta, KeyNotFoundError, type KeyNotFoundErrorArgs, type KeyNotFoundErrorMeta, Memory, type MemoryOptions };
|
|
117
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/errors.ts","../src/memory.ts"],"mappings":";;;;;KASY;;;;WAID;;;;WAKA;;;;WAKA;;;;;KAMC,4BAA4B,eAAe,KAAK;;;;qBAK/C,8BAA8B,UAAU;;;;EAQnD,YAAmB,MAAM;;;;;;;KCnCf;;;;WAID,UAAU,GAAG,OAAO,MAAM;;;;;cAMhB,kBAAkB;;;;;WAIrB;;;;EAehB,YAAmB,UAAS;;;;;;MAajB;;;;;;;EAUX,MAAa,GAAG,SAAS,cAAc;;;;;;;;EAWvC,KAAY,GAAG,SAAS;;;;;;;;EAiBxB,OAAc,GAAG,SAAS;;;;;;EAW1B,OAAc,GAAG,SAAS;;;;;;EAa1B,MAAa,IAAI,SAAS;;;;;;;;EAW1B,YAAmB,GAAG,SAAS,qBAAqB,eAAe,WAAW;;;;;;;;EAuC9E,YAAmB,GAAG,SAAS,qBAAqB,eAAe,WAAW"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { ErrorBase, KeyNotFoundError, KeyNotFoundError as KeyNotFoundError$1, setErrorMessage } from "@unikvs/core";
|
|
2
|
+
import getTypeName from "type-name";
|
|
3
|
+
//#region ../src/errors.ts
|
|
4
|
+
var _InvalidChunkTypeError;
|
|
5
|
+
/**
|
|
6
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
7
|
+
*/
|
|
8
|
+
var InvalidChunkTypeError = class extends ErrorBase {
|
|
9
|
+
/**
|
|
10
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
11
|
+
*/
|
|
12
|
+
constructor(args) {
|
|
13
|
+
const { key, chunk, ...options } = args;
|
|
14
|
+
const meta = {
|
|
15
|
+
key,
|
|
16
|
+
chunk,
|
|
17
|
+
chunkType: getTypeName(chunk)
|
|
18
|
+
};
|
|
19
|
+
super(meta, ({ key: messageKey, chunkType }) => `Expected chunk for key ${JSON.stringify(messageKey)} is Uint8Array<ArrayBuffer>, but got ${chunkType}`, options);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
_InvalidChunkTypeError = InvalidChunkTypeError;
|
|
23
|
+
_InvalidChunkTypeError.prototype.name = "MemoryInvalidChunkTypeError";
|
|
24
|
+
setErrorMessage(InvalidChunkTypeError, ({ key: messageKey, chunkType }) => `キー ${JSON.stringify(messageKey)} のチャンク型に Uint8Array<ArrayBuffer> を期待しましたが、${chunkType} を得ました`, "ja");
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/checkPrivateRedeclaration.js
|
|
27
|
+
function _checkPrivateRedeclaration(e, t) {
|
|
28
|
+
if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/classPrivateFieldInitSpec.js
|
|
32
|
+
function _classPrivateFieldInitSpec(e, t, a) {
|
|
33
|
+
_checkPrivateRedeclaration(e, t), t.set(e, a);
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/assertClassBrand.js
|
|
37
|
+
function _assertClassBrand(e, t, n) {
|
|
38
|
+
if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
|
|
39
|
+
throw new TypeError("Private element is not present on this object");
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/classPrivateFieldSet2.js
|
|
43
|
+
function _classPrivateFieldSet2(s, a, r) {
|
|
44
|
+
return s.set(_assertClassBrand(s, a), r), r;
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region \0@oxc-project+runtime@0.154.0/helpers/esm/classPrivateFieldGet2.js
|
|
48
|
+
function _classPrivateFieldGet2(s, a) {
|
|
49
|
+
return s.get(_assertClassBrand(s, a));
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region ../src/memory.ts
|
|
53
|
+
var _map = /* @__PURE__ */ new WeakMap();
|
|
54
|
+
var _clone = /* @__PURE__ */ new WeakMap();
|
|
55
|
+
/**
|
|
56
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
57
|
+
*/
|
|
58
|
+
var Memory = class {
|
|
59
|
+
/**
|
|
60
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
61
|
+
*/
|
|
62
|
+
constructor(options = {}) {
|
|
63
|
+
_classPrivateFieldInitSpec(this, _map, void 0);
|
|
64
|
+
_classPrivateFieldInitSpec(this, _clone, void 0);
|
|
65
|
+
const { clone = (value) => structuredClone(value) } = options;
|
|
66
|
+
this.name = "Memory";
|
|
67
|
+
_classPrivateFieldSet2(_map, this, /* @__PURE__ */ new Map());
|
|
68
|
+
_classPrivateFieldSet2(_clone, this, clone);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* ストレージが利用可能かどうかを返します。Memory は常に利用可能です。
|
|
72
|
+
*
|
|
73
|
+
* @returns 常に true を返します。
|
|
74
|
+
*/
|
|
75
|
+
get isOpen() {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 指定したキーに値を保存します。保存前に値を複製します。
|
|
80
|
+
*
|
|
81
|
+
* @param c 書き込み対象のキーを持つコンテキストです。
|
|
82
|
+
* @param data 保存する値です。
|
|
83
|
+
*/
|
|
84
|
+
write(c, data) {
|
|
85
|
+
_classPrivateFieldGet2(_map, this).set(c.key, _classPrivateFieldGet2(_clone, this).call(this, data));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 指定したキーの値を読み出します。保存値を複製して返します。
|
|
89
|
+
*
|
|
90
|
+
* @param c 読み出すキーを持つコンテキストです。
|
|
91
|
+
*
|
|
92
|
+
* @returns 複製した保存値です。
|
|
93
|
+
*/
|
|
94
|
+
read(c) {
|
|
95
|
+
if (!_classPrivateFieldGet2(_map, this).has(c.key)) throw new KeyNotFoundError$1({ key: c.key });
|
|
96
|
+
const value = _classPrivateFieldGet2(_map, this).get(c.key);
|
|
97
|
+
return _classPrivateFieldGet2(_clone, this).call(this, value);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 指定したキーが存在するかどうかを返します。
|
|
101
|
+
*
|
|
102
|
+
* @param c 確認するキーを持つコンテキストです。
|
|
103
|
+
*
|
|
104
|
+
* @returns キーが存在すれば true を返します。
|
|
105
|
+
*/
|
|
106
|
+
exists(c) {
|
|
107
|
+
return _classPrivateFieldGet2(_map, this).has(c.key);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 指定したキーを削除します。
|
|
111
|
+
*
|
|
112
|
+
* @param c 削除するキーを持つコンテキストです。
|
|
113
|
+
*/
|
|
114
|
+
delete(c) {
|
|
115
|
+
if (!_classPrivateFieldGet2(_map, this).has(c.key)) throw new KeyNotFoundError$1({ key: c.key });
|
|
116
|
+
_classPrivateFieldGet2(_map, this).delete(c.key);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* すべてのキーを削除します。
|
|
120
|
+
*
|
|
121
|
+
* @param _c 使用しないクリア用コンテキストです。
|
|
122
|
+
*/
|
|
123
|
+
clear(_c) {
|
|
124
|
+
_classPrivateFieldGet2(_map, this).clear();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Uint8Array を書き込むためのストリームを作成します。close 時にチャンクを結合して保存します。
|
|
128
|
+
*
|
|
129
|
+
* @param c 書き込み対象のキーを持つコンテキストです。
|
|
130
|
+
*
|
|
131
|
+
* @returns Uint8Array を受け付ける書き込みストリームです。
|
|
132
|
+
*/
|
|
133
|
+
getWritable(c) {
|
|
134
|
+
const chunks = [];
|
|
135
|
+
return new WritableStream({
|
|
136
|
+
close: () => {
|
|
137
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
138
|
+
const merged = new Uint8Array(totalLength);
|
|
139
|
+
let offset = 0;
|
|
140
|
+
for (const chunk of chunks) {
|
|
141
|
+
merged.set(chunk, offset);
|
|
142
|
+
offset += chunk.byteLength;
|
|
143
|
+
}
|
|
144
|
+
_classPrivateFieldGet2(_map, this).set(c.key, merged);
|
|
145
|
+
},
|
|
146
|
+
write: (chunk) => {
|
|
147
|
+
if (!(chunk instanceof Uint8Array)) throw new InvalidChunkTypeError({
|
|
148
|
+
key: c.key,
|
|
149
|
+
chunk
|
|
150
|
+
});
|
|
151
|
+
chunks.push(_classPrivateFieldGet2(_clone, this).call(this, chunk));
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 保存済みの値を単一チャンクとして送出するストリームを作成します。
|
|
157
|
+
*
|
|
158
|
+
* @param c 読み出すキーを持つコンテキストです。
|
|
159
|
+
*
|
|
160
|
+
* @returns Uint8Array を送出する読み取りストリームです。
|
|
161
|
+
*/
|
|
162
|
+
getReadable(c) {
|
|
163
|
+
if (!_classPrivateFieldGet2(_map, this).has(c.key)) throw new KeyNotFoundError$1({ key: c.key });
|
|
164
|
+
const value = _classPrivateFieldGet2(_map, this).get(c.key);
|
|
165
|
+
return new ReadableStream({ pull: (controller) => {
|
|
166
|
+
if (!(value instanceof Uint8Array)) throw new InvalidChunkTypeError({
|
|
167
|
+
key: c.key,
|
|
168
|
+
chunk: value
|
|
169
|
+
});
|
|
170
|
+
controller.enqueue(_classPrivateFieldGet2(_clone, this).call(this, value));
|
|
171
|
+
controller.close();
|
|
172
|
+
} });
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
//#endregion
|
|
176
|
+
export { InvalidChunkTypeError, KeyNotFoundError, Memory };
|
|
177
|
+
|
|
178
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["KeyNotFoundError"],"sources":["../src/errors.ts","../src/memory.ts"],"sourcesContent":["import { type ErrorOptions, ErrorBase, setErrorMessage } from \"@unikvs/core\";\nimport getTypeName from \"type-name\";\n\nexport type { KeyNotFoundErrorArgs, KeyNotFoundErrorMeta } from \"@unikvs/core\";\nexport { KeyNotFoundError } from \"@unikvs/core\";\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)\n */\nexport type InvalidChunkTypeErrorMeta = {\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-key)\n */\n readonly key: string;\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-chunk)\n */\n readonly chunk: unknown;\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-chunk-type)\n */\n readonly chunkType: string;\n};\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)\n */\nexport type InvalidChunkTypeErrorArgs = ErrorOptions & Omit<InvalidChunkTypeErrorMeta, \"chunkType\">;\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)\n */\nexport class InvalidChunkTypeError extends ErrorBase<InvalidChunkTypeErrorMeta> {\n static {\n this.prototype.name = \"MemoryInvalidChunkTypeError\";\n }\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)\n */\n public constructor(args: InvalidChunkTypeErrorArgs) {\n const { key, chunk, ...options } = args;\n const meta: InvalidChunkTypeErrorMeta = {\n key,\n chunk,\n chunkType: getTypeName(chunk),\n };\n super(\n meta,\n ({ key: messageKey, chunkType }) =>\n `Expected chunk for key ${JSON.stringify(messageKey)} is Uint8Array<ArrayBuffer>, but got ${chunkType}`,\n options,\n );\n }\n}\n\nsetErrorMessage(\n InvalidChunkTypeError,\n ({ key: messageKey, chunkType }) =>\n `キー ${JSON.stringify(messageKey)} のチャンク型に Uint8Array<ArrayBuffer> を期待しましたが、${chunkType} を得ました`,\n \"ja\",\n);\n","import { type IStorage, KeyNotFoundError } from \"@unikvs/core\";\n\nimport { InvalidChunkTypeError } from \"./errors.js\";\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)\n */\nexport type MemoryOptions = {\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#clone)\n */\n readonly clone?: (<T>(value: T) => T) | undefined;\n};\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)\n */\nexport default class Memory implements IStorage {\n /**\n * ストレージ名です。常に `Memory` を返します。\n */\n public readonly name: string;\n\n /**\n * キーと値のペアを保持する内部マップです。\n */\n readonly #map: Map<string, any>;\n\n /**\n * データの複製に使用する関数です。\n */\n readonly #clone: <T>(value: T) => T;\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)\n */\n public constructor(options: MemoryOptions = {}) {\n const { clone = <T>(value: T): T => structuredClone(value) } = options;\n\n this.name = \"Memory\";\n this.#map = new Map();\n this.#clone = clone;\n }\n\n /**\n * ストレージが利用可能かどうかを返します。Memory は常に利用可能です。\n *\n * @returns 常に true を返します。\n */\n public get isOpen(): boolean {\n return true;\n }\n\n /**\n * 指定したキーに値を保存します。保存前に値を複製します。\n *\n * @param c 書き込み対象のキーを持つコンテキストです。\n * @param data 保存する値です。\n */\n public write(c: IStorage.WriteContext, data: any): void {\n this.#map.set(c.key, this.#clone(data));\n }\n\n /**\n * 指定したキーの値を読み出します。保存値を複製して返します。\n *\n * @param c 読み出すキーを持つコンテキストです。\n *\n * @returns 複製した保存値です。\n */\n public read(c: IStorage.ReadContext): any {\n if (!this.#map.has(c.key)) {\n throw new KeyNotFoundError({ key: c.key });\n }\n\n const value = this.#map.get(c.key);\n\n return this.#clone(value);\n }\n\n /**\n * 指定したキーが存在するかどうかを返します。\n *\n * @param c 確認するキーを持つコンテキストです。\n *\n * @returns キーが存在すれば true を返します。\n */\n public exists(c: IStorage.ExistsContext): boolean {\n const exists = this.#map.has(c.key);\n\n return exists;\n }\n\n /**\n * 指定したキーを削除します。\n *\n * @param c 削除するキーを持つコンテキストです。\n */\n public delete(c: IStorage.DeleteContext): void {\n if (!this.#map.has(c.key)) {\n throw new KeyNotFoundError({ key: c.key });\n }\n\n this.#map.delete(c.key);\n }\n\n /**\n * すべてのキーを削除します。\n *\n * @param _c 使用しないクリア用コンテキストです。\n */\n public clear(_c: IStorage.ClearContext): void {\n this.#map.clear();\n }\n\n /**\n * Uint8Array を書き込むためのストリームを作成します。close 時にチャンクを結合して保存します。\n *\n * @param c 書き込み対象のキーを持つコンテキストです。\n *\n * @returns Uint8Array を受け付ける書き込みストリームです。\n */\n public getWritable(c: IStorage.GetWritableContext): WritableStream<Uint8Array<ArrayBuffer>> {\n // メモリーストレージにはネイティブなストリームがないため、書き込まれたチャンクを配列に保持し、クローズ時に結合して保存します。\n const chunks: Uint8Array[] = [];\n const stream = new WritableStream<Uint8Array<ArrayBuffer>>({\n close: (): void => {\n const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);\n const merged = new Uint8Array(totalLength);\n\n let offset = 0;\n\n for (const chunk of chunks) {\n merged.set(chunk, offset);\n offset += chunk.byteLength;\n }\n\n this.#map.set(c.key, merged);\n },\n write: (chunk): void => {\n if (!(chunk instanceof Uint8Array)) {\n throw new InvalidChunkTypeError({\n key: c.key,\n chunk,\n });\n }\n\n chunks.push(this.#clone(chunk));\n },\n });\n\n return stream;\n }\n\n /**\n * 保存済みの値を単一チャンクとして送出するストリームを作成します。\n *\n * @param c 読み出すキーを持つコンテキストです。\n *\n * @returns Uint8Array を送出する読み取りストリームです。\n */\n public getReadable(c: IStorage.GetReadableContext): ReadableStream<Uint8Array<ArrayBuffer>> {\n if (!this.#map.has(c.key)) {\n throw new KeyNotFoundError({ key: c.key });\n }\n\n // メモリーストレージにはネイティブなストリームがないため、既存の値を単一チャンクとしてストリームで送出します。\n const value = this.#map.get(c.key);\n const stream = new ReadableStream<Uint8Array<ArrayBuffer>>({\n pull: (controller): void => {\n if (!(value instanceof Uint8Array)) {\n throw new InvalidChunkTypeError({\n key: c.key,\n chunk: value,\n });\n }\n\n controller.enqueue(this.#clone(value) as Uint8Array<ArrayBuffer>);\n controller.close();\n },\n });\n\n return stream;\n }\n}\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,50 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unikvs/memory",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "Storage plugin that stores arbitrary data in memory.",
|
|
5
|
-
"homepage": "https://
|
|
5
|
+
"homepage": "https://tai-kun.github.io/unikvs/",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"author": "tai-kun",
|
|
8
7
|
"repository": {
|
|
9
|
-
"url": "https://github.com/tai-kun/unikvs"
|
|
8
|
+
"url": "https://github.com/tai-kun/unikvs",
|
|
9
|
+
"directory": "packages/memory"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"dist",
|
|
13
13
|
"src",
|
|
14
14
|
"package.json",
|
|
15
|
-
"
|
|
16
|
-
"
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.md"
|
|
17
17
|
],
|
|
18
18
|
"type": "module",
|
|
19
19
|
"sideEffects": false,
|
|
20
|
-
"main": "./dist/src/index.js",
|
|
21
|
-
"types": "./dist/src/index.d.ts",
|
|
22
20
|
"exports": {
|
|
23
21
|
".": {
|
|
24
|
-
"types": "./dist/
|
|
25
|
-
"default": "./dist/
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
26
24
|
}
|
|
27
25
|
},
|
|
28
26
|
"dependencies": {
|
|
29
|
-
"@unikvs/core": "0.0.
|
|
30
|
-
"type-name": "2.0.2"
|
|
27
|
+
"@unikvs/core": "0.0.14",
|
|
28
|
+
"type-name": "^2.0.2"
|
|
31
29
|
},
|
|
32
30
|
"devDependencies": {
|
|
33
|
-
"@tsconfig/node24": "24.0.5",
|
|
34
|
-
"@tsconfig/strictest": "2.0.8",
|
|
35
|
-
"@types/node": "24.13.6",
|
|
36
31
|
"@types/type-name": "1.0.32",
|
|
37
|
-
"@
|
|
38
|
-
"@vitest/browser-playwright": "
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
32
|
+
"@unikvs/testing": "0.0.0",
|
|
33
|
+
"@vitest/browser-playwright": "5.0.5",
|
|
34
|
+
"@vitest/coverage-istanbul": "5.0.5",
|
|
35
|
+
"@vitest/coverage-v8": "5.0.5",
|
|
36
|
+
"playwright": "1.64.1",
|
|
37
|
+
"valibot": "1.5.0",
|
|
38
|
+
"vite-plus": "1.3.0"
|
|
43
39
|
},
|
|
44
40
|
"peerDependencies": {
|
|
45
|
-
"@logtape/logtape": "2.0.0"
|
|
41
|
+
"@logtape/logtape": "^2.0.0"
|
|
46
42
|
},
|
|
43
|
+
"readme": "README.md",
|
|
47
44
|
"scripts": {
|
|
48
|
-
"build": "
|
|
45
|
+
"build": "vp -C .config pack"
|
|
49
46
|
}
|
|
50
47
|
}
|
package/src/errors.ts
CHANGED
|
@@ -1,68 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type ErrorOptions, ErrorBase, setErrorMessage } from "@unikvs/core";
|
|
2
2
|
import getTypeName from "type-name";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*/
|
|
7
|
-
export type KeyNotFoundErrorMeta = {
|
|
8
|
-
/**
|
|
9
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
10
|
-
*/
|
|
11
|
-
readonly key: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
16
|
-
*/
|
|
17
|
-
export type KeyNotFoundErrorArgs = ErrorOptions & KeyNotFoundErrorMeta;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
21
|
-
*/
|
|
22
|
-
export class KeyNotFoundError extends ErrorBase<KeyNotFoundErrorMeta> {
|
|
23
|
-
static {
|
|
24
|
-
this.prototype.name = "MemoryKeyNotFoundError";
|
|
25
|
-
}
|
|
26
|
-
|
|
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);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
setErrorMessage(KeyNotFoundError, ({ key }) => `キー ${key} が見つかりません`, "ja");
|
|
4
|
+
export type { KeyNotFoundErrorArgs, KeyNotFoundErrorMeta } from "@unikvs/core";
|
|
5
|
+
export { KeyNotFoundError } from "@unikvs/core";
|
|
38
6
|
|
|
39
7
|
/**
|
|
40
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#
|
|
8
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
41
9
|
*/
|
|
42
10
|
export type InvalidChunkTypeErrorMeta = {
|
|
43
11
|
/**
|
|
44
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#
|
|
12
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-key)
|
|
45
13
|
*/
|
|
46
14
|
readonly key: string;
|
|
47
15
|
|
|
48
16
|
/**
|
|
49
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#
|
|
17
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-chunk)
|
|
50
18
|
*/
|
|
51
19
|
readonly chunk: unknown;
|
|
52
20
|
|
|
53
21
|
/**
|
|
54
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#
|
|
22
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-chunk-type)
|
|
55
23
|
*/
|
|
56
24
|
readonly chunkType: string;
|
|
57
25
|
};
|
|
58
26
|
|
|
59
27
|
/**
|
|
60
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#
|
|
28
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
61
29
|
*/
|
|
62
30
|
export type InvalidChunkTypeErrorArgs = ErrorOptions & Omit<InvalidChunkTypeErrorMeta, "chunkType">;
|
|
63
31
|
|
|
64
32
|
/**
|
|
65
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#
|
|
33
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
66
34
|
*/
|
|
67
35
|
export class InvalidChunkTypeError extends ErrorBase<InvalidChunkTypeErrorMeta> {
|
|
68
36
|
static {
|
|
@@ -70,15 +38,19 @@ export class InvalidChunkTypeError extends ErrorBase<InvalidChunkTypeErrorMeta>
|
|
|
70
38
|
}
|
|
71
39
|
|
|
72
40
|
/**
|
|
73
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#
|
|
41
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#invalid-chunk-type-error)
|
|
74
42
|
*/
|
|
75
43
|
public constructor(args: InvalidChunkTypeErrorArgs) {
|
|
76
44
|
const { key, chunk, ...options } = args;
|
|
77
|
-
const meta: InvalidChunkTypeErrorMeta = {
|
|
45
|
+
const meta: InvalidChunkTypeErrorMeta = {
|
|
46
|
+
key,
|
|
47
|
+
chunk,
|
|
48
|
+
chunkType: getTypeName(chunk),
|
|
49
|
+
};
|
|
78
50
|
super(
|
|
79
51
|
meta,
|
|
80
|
-
({ key, chunkType }) =>
|
|
81
|
-
`Expected chunk for key ${JSON.stringify(
|
|
52
|
+
({ key: messageKey, chunkType }) =>
|
|
53
|
+
`Expected chunk for key ${JSON.stringify(messageKey)} is Uint8Array<ArrayBuffer>, but got ${chunkType}`,
|
|
82
54
|
options,
|
|
83
55
|
);
|
|
84
56
|
}
|
|
@@ -86,7 +58,7 @@ export class InvalidChunkTypeError extends ErrorBase<InvalidChunkTypeErrorMeta>
|
|
|
86
58
|
|
|
87
59
|
setErrorMessage(
|
|
88
60
|
InvalidChunkTypeError,
|
|
89
|
-
({ key, chunkType }) =>
|
|
90
|
-
`キー ${JSON.stringify(
|
|
61
|
+
({ key: messageKey, chunkType }) =>
|
|
62
|
+
`キー ${JSON.stringify(messageKey)} のチャンク型に Uint8Array<ArrayBuffer> を期待しましたが、${chunkType} を得ました`,
|
|
91
63
|
"ja",
|
|
92
64
|
);
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export type {
|
|
2
|
-
KeyNotFoundErrorMeta,
|
|
3
2
|
KeyNotFoundErrorArgs,
|
|
4
|
-
|
|
3
|
+
KeyNotFoundErrorMeta,
|
|
5
4
|
InvalidChunkTypeErrorArgs,
|
|
5
|
+
InvalidChunkTypeErrorMeta,
|
|
6
6
|
} from "./errors.js";
|
|
7
7
|
export { KeyNotFoundError, InvalidChunkTypeError } from "./errors.js";
|
|
8
8
|
|
package/src/memory.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type IStorage, KeyNotFoundError } from "@unikvs/core";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { InvalidChunkTypeError } from "./errors.js";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
7
7
|
*/
|
|
8
8
|
export type MemoryOptions = {
|
|
9
9
|
/**
|
|
10
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#
|
|
10
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#clone)
|
|
11
11
|
*/
|
|
12
12
|
readonly clone?: (<T>(value: T) => T) | undefined;
|
|
13
13
|
};
|
|
@@ -17,115 +17,135 @@ export type MemoryOptions = {
|
|
|
17
17
|
*/
|
|
18
18
|
export default class Memory implements IStorage {
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* ストレージ名です。常に `Memory` を返します。
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
public readonly name: string;
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
25
|
+
* キーと値のペアを保持する内部マップです。
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
readonly #map: Map<string, any>;
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* データの複製に使用する関数です。
|
|
31
31
|
*/
|
|
32
|
-
|
|
32
|
+
readonly #clone: <T>(value: T) => T;
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
36
36
|
*/
|
|
37
37
|
public constructor(options: MemoryOptions = {}) {
|
|
38
|
+
const { clone = <T>(value: T): T => structuredClone(value) } = options;
|
|
39
|
+
|
|
38
40
|
this.name = "Memory";
|
|
39
|
-
this
|
|
40
|
-
this
|
|
41
|
+
this.#map = new Map();
|
|
42
|
+
this.#clone = clone;
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
/**
|
|
44
|
-
*
|
|
46
|
+
* ストレージが利用可能かどうかを返します。Memory は常に利用可能です。
|
|
47
|
+
*
|
|
48
|
+
* @returns 常に true を返します。
|
|
45
49
|
*/
|
|
46
50
|
public get isOpen(): boolean {
|
|
47
51
|
return true;
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
/**
|
|
51
|
-
*
|
|
55
|
+
* 指定したキーに値を保存します。保存前に値を複製します。
|
|
56
|
+
*
|
|
57
|
+
* @param c 書き込み対象のキーを持つコンテキストです。
|
|
58
|
+
* @param data 保存する値です。
|
|
52
59
|
*/
|
|
53
|
-
public write(
|
|
54
|
-
|
|
55
|
-
this.map.set(key, this.clone(data));
|
|
60
|
+
public write(c: IStorage.WriteContext, data: any): void {
|
|
61
|
+
this.#map.set(c.key, this.#clone(data));
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
/**
|
|
59
|
-
*
|
|
65
|
+
* 指定したキーの値を読み出します。保存値を複製して返します。
|
|
66
|
+
*
|
|
67
|
+
* @param c 読み出すキーを持つコンテキストです。
|
|
68
|
+
*
|
|
69
|
+
* @returns 複製した保存値です。
|
|
60
70
|
*/
|
|
61
|
-
public read(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
throw new KeyNotFoundError({ key });
|
|
71
|
+
public read(c: IStorage.ReadContext): any {
|
|
72
|
+
if (!this.#map.has(c.key)) {
|
|
73
|
+
throw new KeyNotFoundError({ key: c.key });
|
|
65
74
|
}
|
|
66
75
|
|
|
67
|
-
const value = this
|
|
76
|
+
const value = this.#map.get(c.key);
|
|
68
77
|
|
|
69
|
-
return this
|
|
78
|
+
return this.#clone(value);
|
|
70
79
|
}
|
|
71
80
|
|
|
72
81
|
/**
|
|
73
|
-
*
|
|
82
|
+
* 指定したキーが存在するかどうかを返します。
|
|
83
|
+
*
|
|
84
|
+
* @param c 確認するキーを持つコンテキストです。
|
|
85
|
+
*
|
|
86
|
+
* @returns キーが存在すれば true を返します。
|
|
74
87
|
*/
|
|
75
|
-
public exists(
|
|
76
|
-
const
|
|
77
|
-
const exists = this.map.has(key);
|
|
88
|
+
public exists(c: IStorage.ExistsContext): boolean {
|
|
89
|
+
const exists = this.#map.has(c.key);
|
|
78
90
|
|
|
79
91
|
return exists;
|
|
80
92
|
}
|
|
81
93
|
|
|
82
94
|
/**
|
|
83
|
-
*
|
|
95
|
+
* 指定したキーを削除します。
|
|
96
|
+
*
|
|
97
|
+
* @param c 削除するキーを持つコンテキストです。
|
|
84
98
|
*/
|
|
85
|
-
public delete(
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
throw new KeyNotFoundError({ key });
|
|
99
|
+
public delete(c: IStorage.DeleteContext): void {
|
|
100
|
+
if (!this.#map.has(c.key)) {
|
|
101
|
+
throw new KeyNotFoundError({ key: c.key });
|
|
89
102
|
}
|
|
90
103
|
|
|
91
|
-
this
|
|
104
|
+
this.#map.delete(c.key);
|
|
92
105
|
}
|
|
93
106
|
|
|
94
107
|
/**
|
|
95
|
-
*
|
|
108
|
+
* すべてのキーを削除します。
|
|
109
|
+
*
|
|
110
|
+
* @param _c 使用しないクリア用コンテキストです。
|
|
96
111
|
*/
|
|
97
|
-
public clear(): void {
|
|
98
|
-
this
|
|
112
|
+
public clear(_c: IStorage.ClearContext): void {
|
|
113
|
+
this.#map.clear();
|
|
99
114
|
}
|
|
100
115
|
|
|
101
116
|
/**
|
|
102
|
-
*
|
|
117
|
+
* Uint8Array を書き込むためのストリームを作成します。close 時にチャンクを結合して保存します。
|
|
118
|
+
*
|
|
119
|
+
* @param c 書き込み対象のキーを持つコンテキストです。
|
|
120
|
+
*
|
|
121
|
+
* @returns Uint8Array を受け付ける書き込みストリームです。
|
|
103
122
|
*/
|
|
104
|
-
public getWritable(
|
|
105
|
-
args: Pick<IStorage.GetWritableArgs, "key">,
|
|
106
|
-
): WritableStream<Uint8Array<ArrayBuffer>> {
|
|
107
|
-
const { key } = args;
|
|
123
|
+
public getWritable(c: IStorage.GetWritableContext): WritableStream<Uint8Array<ArrayBuffer>> {
|
|
108
124
|
// メモリーストレージにはネイティブなストリームがないため、書き込まれたチャンクを配列に保持し、クローズ時に結合して保存します。
|
|
109
125
|
const chunks: Uint8Array[] = [];
|
|
110
126
|
const stream = new WritableStream<Uint8Array<ArrayBuffer>>({
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
throw new InvalidChunkTypeError({ key, chunk });
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
chunks.push(this.clone(chunk));
|
|
117
|
-
},
|
|
118
|
-
close: () => {
|
|
119
|
-
const totalLength = chunks.reduce((sum, c) => sum + c.byteLength, 0);
|
|
127
|
+
close: (): void => {
|
|
128
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
120
129
|
const merged = new Uint8Array(totalLength);
|
|
121
130
|
|
|
122
131
|
let offset = 0;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
offset
|
|
132
|
+
|
|
133
|
+
for (const chunk of chunks) {
|
|
134
|
+
merged.set(chunk, offset);
|
|
135
|
+
offset += chunk.byteLength;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this.#map.set(c.key, merged);
|
|
139
|
+
},
|
|
140
|
+
write: (chunk): void => {
|
|
141
|
+
if (!(chunk instanceof Uint8Array)) {
|
|
142
|
+
throw new InvalidChunkTypeError({
|
|
143
|
+
key: c.key,
|
|
144
|
+
chunk,
|
|
145
|
+
});
|
|
126
146
|
}
|
|
127
147
|
|
|
128
|
-
|
|
148
|
+
chunks.push(this.#clone(chunk));
|
|
129
149
|
},
|
|
130
150
|
});
|
|
131
151
|
|
|
@@ -133,25 +153,29 @@ export default class Memory implements IStorage {
|
|
|
133
153
|
}
|
|
134
154
|
|
|
135
155
|
/**
|
|
136
|
-
*
|
|
156
|
+
* 保存済みの値を単一チャンクとして送出するストリームを作成します。
|
|
157
|
+
*
|
|
158
|
+
* @param c 読み出すキーを持つコンテキストです。
|
|
159
|
+
*
|
|
160
|
+
* @returns Uint8Array を送出する読み取りストリームです。
|
|
137
161
|
*/
|
|
138
|
-
public getReadable(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const { key } = args;
|
|
142
|
-
if (!this.map.has(key)) {
|
|
143
|
-
throw new KeyNotFoundError({ key });
|
|
162
|
+
public getReadable(c: IStorage.GetReadableContext): ReadableStream<Uint8Array<ArrayBuffer>> {
|
|
163
|
+
if (!this.#map.has(c.key)) {
|
|
164
|
+
throw new KeyNotFoundError({ key: c.key });
|
|
144
165
|
}
|
|
145
166
|
|
|
146
167
|
// メモリーストレージにはネイティブなストリームがないため、既存の値を単一チャンクとしてストリームで送出します。
|
|
147
|
-
const value = this
|
|
168
|
+
const value = this.#map.get(c.key);
|
|
148
169
|
const stream = new ReadableStream<Uint8Array<ArrayBuffer>>({
|
|
149
|
-
pull: (controller) => {
|
|
170
|
+
pull: (controller): void => {
|
|
150
171
|
if (!(value instanceof Uint8Array)) {
|
|
151
|
-
throw new InvalidChunkTypeError({
|
|
172
|
+
throw new InvalidChunkTypeError({
|
|
173
|
+
key: c.key,
|
|
174
|
+
chunk: value,
|
|
175
|
+
});
|
|
152
176
|
}
|
|
153
177
|
|
|
154
|
-
controller.enqueue(this
|
|
178
|
+
controller.enqueue(this.#clone(value) as Uint8Array<ArrayBuffer>);
|
|
155
179
|
controller.close();
|
|
156
180
|
},
|
|
157
181
|
});
|
package/dist/src/errors.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { ErrorBase, type ErrorOptions } from "@unikvs/core";
|
|
2
|
-
/**
|
|
3
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
4
|
-
*/
|
|
5
|
-
export type KeyNotFoundErrorMeta = {
|
|
6
|
-
/**
|
|
7
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
8
|
-
*/
|
|
9
|
-
readonly key: string;
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
13
|
-
*/
|
|
14
|
-
export type KeyNotFoundErrorArgs = ErrorOptions & KeyNotFoundErrorMeta;
|
|
15
|
-
/**
|
|
16
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
17
|
-
*/
|
|
18
|
-
export declare class KeyNotFoundError extends ErrorBase<KeyNotFoundErrorMeta> {
|
|
19
|
-
/**
|
|
20
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
21
|
-
*/
|
|
22
|
-
constructor(args: KeyNotFoundErrorArgs);
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
26
|
-
*/
|
|
27
|
-
export type InvalidChunkTypeErrorMeta = {
|
|
28
|
-
/**
|
|
29
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
30
|
-
*/
|
|
31
|
-
readonly key: string;
|
|
32
|
-
/**
|
|
33
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
34
|
-
*/
|
|
35
|
-
readonly chunk: unknown;
|
|
36
|
-
/**
|
|
37
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
38
|
-
*/
|
|
39
|
-
readonly chunkType: string;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
43
|
-
*/
|
|
44
|
-
export type InvalidChunkTypeErrorArgs = ErrorOptions & Omit<InvalidChunkTypeErrorMeta, "chunkType">;
|
|
45
|
-
/**
|
|
46
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
47
|
-
*/
|
|
48
|
-
export declare class InvalidChunkTypeError extends ErrorBase<InvalidChunkTypeErrorMeta> {
|
|
49
|
-
/**
|
|
50
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
51
|
-
*/
|
|
52
|
-
constructor(args: InvalidChunkTypeErrorArgs);
|
|
53
|
-
}
|
|
54
|
-
//# sourceMappingURL=errors.d.ts.map
|
package/dist/src/errors.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
var _a, _b;
|
|
2
|
-
import { ErrorBase, setErrorMessage } from "@unikvs/core";
|
|
3
|
-
import getTypeName from "type-name";
|
|
4
|
-
/**
|
|
5
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
6
|
-
*/
|
|
7
|
-
export class KeyNotFoundError extends ErrorBase {
|
|
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);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
_a = KeyNotFoundError;
|
|
18
|
-
(() => {
|
|
19
|
-
_a.prototype.name = "MemoryKeyNotFoundError";
|
|
20
|
-
})();
|
|
21
|
-
setErrorMessage(KeyNotFoundError, ({ key }) => `キー ${key} が見つかりません`, "ja");
|
|
22
|
-
/**
|
|
23
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#errors)
|
|
24
|
-
*/
|
|
25
|
-
export class InvalidChunkTypeError extends ErrorBase {
|
|
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);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
_b = InvalidChunkTypeError;
|
|
36
|
-
(() => {
|
|
37
|
-
_b.prototype.name = "MemoryInvalidChunkTypeError";
|
|
38
|
-
})();
|
|
39
|
-
setErrorMessage(InvalidChunkTypeError, ({ key, chunkType }) => `キー ${JSON.stringify(key)} のチャンク型に Uint8Array<ArrayBuffer> を期待しましたが、${chunkType} を得ました`, "ja");
|
package/dist/src/index.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export type { KeyNotFoundErrorMeta, KeyNotFoundErrorArgs, InvalidChunkTypeErrorMeta, InvalidChunkTypeErrorArgs, } from "./errors.js";
|
|
2
|
-
export { KeyNotFoundError, InvalidChunkTypeError } from "./errors.js";
|
|
3
|
-
export type * from "./memory.js";
|
|
4
|
-
export { default as Memory } from "./memory.js";
|
|
5
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,oBAAoB,EACpB,oBAAoB,EACpB,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEtE,mBAAmB,aAAa,CAAC;AACjC,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/src/index.js
DELETED
package/dist/src/memory.d.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type { IStorage } from "@unikvs/core";
|
|
2
|
-
/**
|
|
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)
|
|
13
|
-
*/
|
|
14
|
-
export default class Memory implements IStorage {
|
|
15
|
-
/**
|
|
16
|
-
* キーと値のペアを保持する内部マップです。
|
|
17
|
-
*/
|
|
18
|
-
private readonly map;
|
|
19
|
-
/**
|
|
20
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
21
|
-
*/
|
|
22
|
-
readonly name: string;
|
|
23
|
-
/**
|
|
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)
|
|
33
|
-
*/
|
|
34
|
-
get isOpen(): boolean;
|
|
35
|
-
/**
|
|
36
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
37
|
-
*/
|
|
38
|
-
write(args: Pick<IStorage.WriteArgs<any>, "key" | "data">): void;
|
|
39
|
-
/**
|
|
40
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
41
|
-
*/
|
|
42
|
-
read(args: Pick<IStorage.ReadArgs, "key">): any;
|
|
43
|
-
/**
|
|
44
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
45
|
-
*/
|
|
46
|
-
exists(args: Pick<IStorage.ExistsArgs, "key">): boolean;
|
|
47
|
-
/**
|
|
48
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
49
|
-
*/
|
|
50
|
-
delete(args: Pick<IStorage.DeleteArgs, "key">): void;
|
|
51
|
-
/**
|
|
52
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
53
|
-
*/
|
|
54
|
-
clear(): void;
|
|
55
|
-
/**
|
|
56
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
57
|
-
*/
|
|
58
|
-
getWritable(args: Pick<IStorage.GetWritableArgs, "key">): WritableStream<Uint8Array<ArrayBuffer>>;
|
|
59
|
-
/**
|
|
60
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
61
|
-
*/
|
|
62
|
-
getReadable(args: Pick<IStorage.GetReadableArgs, "key">): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
63
|
-
}
|
|
64
|
-
//# sourceMappingURL=memory.d.ts.map
|
package/dist/src/memory.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { KeyNotFoundError, InvalidChunkTypeError } from "./errors.js";
|
|
2
|
-
/**
|
|
3
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
4
|
-
*/
|
|
5
|
-
export default class Memory {
|
|
6
|
-
/**
|
|
7
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
8
|
-
*/
|
|
9
|
-
constructor(options = {}) {
|
|
10
|
-
this.name = "Memory";
|
|
11
|
-
this.map = new Map();
|
|
12
|
-
this.clone = options.clone ?? ((v) => structuredClone(v));
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
16
|
-
*/
|
|
17
|
-
get isOpen() {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
22
|
-
*/
|
|
23
|
-
write(args) {
|
|
24
|
-
const { key, data } = args;
|
|
25
|
-
this.map.set(key, this.clone(data));
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
29
|
-
*/
|
|
30
|
-
read(args) {
|
|
31
|
-
const { key } = args;
|
|
32
|
-
if (!this.map.has(key)) {
|
|
33
|
-
throw new KeyNotFoundError({ key });
|
|
34
|
-
}
|
|
35
|
-
const value = this.map.get(key);
|
|
36
|
-
return this.clone(value);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
40
|
-
*/
|
|
41
|
-
exists(args) {
|
|
42
|
-
const { key } = args;
|
|
43
|
-
const exists = this.map.has(key);
|
|
44
|
-
return exists;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
48
|
-
*/
|
|
49
|
-
delete(args) {
|
|
50
|
-
const { key } = args;
|
|
51
|
-
if (!this.map.has(key)) {
|
|
52
|
-
throw new KeyNotFoundError({ key });
|
|
53
|
-
}
|
|
54
|
-
this.map.delete(key);
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
58
|
-
*/
|
|
59
|
-
clear() {
|
|
60
|
-
this.map.clear();
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
64
|
-
*/
|
|
65
|
-
getWritable(args) {
|
|
66
|
-
const { key } = args;
|
|
67
|
-
// メモリーストレージにはネイティブなストリームがないため、書き込まれたチャンクを配列に保持し、クローズ時に結合して保存します。
|
|
68
|
-
const chunks = [];
|
|
69
|
-
const stream = new WritableStream({
|
|
70
|
-
write: (chunk) => {
|
|
71
|
-
if (!(chunk instanceof Uint8Array)) {
|
|
72
|
-
throw new InvalidChunkTypeError({ key, chunk });
|
|
73
|
-
}
|
|
74
|
-
chunks.push(this.clone(chunk));
|
|
75
|
-
},
|
|
76
|
-
close: () => {
|
|
77
|
-
const totalLength = chunks.reduce((sum, c) => sum + c.byteLength, 0);
|
|
78
|
-
const merged = new Uint8Array(totalLength);
|
|
79
|
-
let offset = 0;
|
|
80
|
-
for (const c of chunks) {
|
|
81
|
-
merged.set(c, offset);
|
|
82
|
-
offset += c.byteLength;
|
|
83
|
-
}
|
|
84
|
-
this.map.set(key, merged);
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
return stream;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
91
|
-
*/
|
|
92
|
-
getReadable(args) {
|
|
93
|
-
const { key } = args;
|
|
94
|
-
if (!this.map.has(key)) {
|
|
95
|
-
throw new KeyNotFoundError({ key });
|
|
96
|
-
}
|
|
97
|
-
// メモリーストレージにはネイティブなストリームがないため、既存の値を単一チャンクとしてストリームで送出します。
|
|
98
|
-
const value = this.map.get(key);
|
|
99
|
-
const stream = new ReadableStream({
|
|
100
|
-
pull: (controller) => {
|
|
101
|
-
if (!(value instanceof Uint8Array)) {
|
|
102
|
-
throw new InvalidChunkTypeError({ key, chunk: value });
|
|
103
|
-
}
|
|
104
|
-
controller.enqueue(this.clone(value));
|
|
105
|
-
controller.close();
|
|
106
|
-
},
|
|
107
|
-
});
|
|
108
|
-
return stream;
|
|
109
|
-
}
|
|
110
|
-
}
|