@unikvs/memory 0.0.12 → 0.0.13
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 +65 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +129 -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 +52 -80
- 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,65 @@
|
|
|
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
|
+
readonly name: string;
|
|
50
|
+
/**
|
|
51
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
52
|
+
*/
|
|
53
|
+
constructor(options?: MemoryOptions);
|
|
54
|
+
get isOpen(): boolean;
|
|
55
|
+
write(c: IStorage.WriteContext, data: any): void;
|
|
56
|
+
read(c: IStorage.ReadContext): any;
|
|
57
|
+
exists(c: IStorage.ExistsContext): boolean;
|
|
58
|
+
delete(c: IStorage.DeleteContext): void;
|
|
59
|
+
clear(_c: IStorage.ClearContext): void;
|
|
60
|
+
getWritable(c: IStorage.GetWritableContext): WritableStream<Uint8Array<ArrayBuffer>>;
|
|
61
|
+
getReadable(c: IStorage.GetReadableContext): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
export { type InvalidChunkTypeErrorArgs, type InvalidChunkTypeErrorMeta, KeyNotFoundError, type KeyNotFoundErrorArgs, type KeyNotFoundErrorMeta, Memory, type MemoryOptions };
|
|
65
|
+
//# 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;;WAMrB;;;;EAUhB,YAAmB,UAAS;MAQjB;EAIX,MAAa,GAAG,SAAS,cAAc;EAIvC,KAAY,GAAG,SAAS;EAUxB,OAAc,GAAG,SAAS;EAM1B,OAAc,GAAG,SAAS;EAQ1B,MAAa,IAAI,SAAS;EAI1B,YAAmB,GAAG,SAAS,qBAAqB,eAAe,WAAW;EAgC9E,YAAmB,GAAG,SAAS,qBAAqB,eAAe,WAAW"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
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
|
+
get isOpen() {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
write(c, data) {
|
|
74
|
+
_classPrivateFieldGet2(_map, this).set(c.key, _classPrivateFieldGet2(_clone, this).call(this, data));
|
|
75
|
+
}
|
|
76
|
+
read(c) {
|
|
77
|
+
if (!_classPrivateFieldGet2(_map, this).has(c.key)) throw new KeyNotFoundError$1({ key: c.key });
|
|
78
|
+
const value = _classPrivateFieldGet2(_map, this).get(c.key);
|
|
79
|
+
return _classPrivateFieldGet2(_clone, this).call(this, value);
|
|
80
|
+
}
|
|
81
|
+
exists(c) {
|
|
82
|
+
return _classPrivateFieldGet2(_map, this).has(c.key);
|
|
83
|
+
}
|
|
84
|
+
delete(c) {
|
|
85
|
+
if (!_classPrivateFieldGet2(_map, this).has(c.key)) throw new KeyNotFoundError$1({ key: c.key });
|
|
86
|
+
_classPrivateFieldGet2(_map, this).delete(c.key);
|
|
87
|
+
}
|
|
88
|
+
clear(_c) {
|
|
89
|
+
_classPrivateFieldGet2(_map, this).clear();
|
|
90
|
+
}
|
|
91
|
+
getWritable(c) {
|
|
92
|
+
const chunks = [];
|
|
93
|
+
return new WritableStream({
|
|
94
|
+
close: () => {
|
|
95
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
96
|
+
const merged = new Uint8Array(totalLength);
|
|
97
|
+
let offset = 0;
|
|
98
|
+
for (const chunk of chunks) {
|
|
99
|
+
merged.set(chunk, offset);
|
|
100
|
+
offset += chunk.byteLength;
|
|
101
|
+
}
|
|
102
|
+
_classPrivateFieldGet2(_map, this).set(c.key, merged);
|
|
103
|
+
},
|
|
104
|
+
write: (chunk) => {
|
|
105
|
+
if (!(chunk instanceof Uint8Array)) throw new InvalidChunkTypeError({
|
|
106
|
+
key: c.key,
|
|
107
|
+
chunk
|
|
108
|
+
});
|
|
109
|
+
chunks.push(_classPrivateFieldGet2(_clone, this).call(this, chunk));
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
getReadable(c) {
|
|
114
|
+
if (!_classPrivateFieldGet2(_map, this).has(c.key)) throw new KeyNotFoundError$1({ key: c.key });
|
|
115
|
+
const value = _classPrivateFieldGet2(_map, this).get(c.key);
|
|
116
|
+
return new ReadableStream({ pull: (controller) => {
|
|
117
|
+
if (!(value instanceof Uint8Array)) throw new InvalidChunkTypeError({
|
|
118
|
+
key: c.key,
|
|
119
|
+
chunk: value
|
|
120
|
+
});
|
|
121
|
+
controller.enqueue(_classPrivateFieldGet2(_clone, this).call(this, value));
|
|
122
|
+
controller.close();
|
|
123
|
+
} });
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
//#endregion
|
|
127
|
+
export { InvalidChunkTypeError, KeyNotFoundError, Memory };
|
|
128
|
+
|
|
129
|
+
//# 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 * キーと値のペアを保持する内部マップです。\n */\n readonly #map: Map<string, any>;\n\n public readonly name: string;\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 public get isOpen(): boolean {\n return true;\n }\n\n public write(c: IStorage.WriteContext, data: any): void {\n this.#map.set(c.key, this.#clone(data));\n }\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 public exists(c: IStorage.ExistsContext): boolean {\n const exists = this.#map.has(c.key);\n\n return exists;\n }\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 public clear(_c: IStorage.ClearContext): void {\n this.#map.clear();\n }\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 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":";;;;;;;AAkCA,IAAa,wBAAb,cAA2C,UAAqC;;;;CAQ9E,YAAmB,MAAiC;EAClD,MAAM,EAAE,KAAK,OAAO,GAAG,YAAY;EACnC,MAAM,OAAkC;GACtC;GACA;GACA,WAAW,YAAY,KAAK;EAC9B;EACA,MACE,OACC,EAAE,KAAK,YAAY,gBAClB,0BAA0B,KAAK,UAAU,UAAU,EAAE,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ary 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.13",
|
|
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
|
};
|
|
@@ -19,139 +19,111 @@ export default class Memory implements IStorage {
|
|
|
19
19
|
/**
|
|
20
20
|
* キーと値のペアを保持する内部マップです。
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
readonly #map: Map<string, any>;
|
|
23
23
|
|
|
24
|
-
/**
|
|
25
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
26
|
-
*/
|
|
27
24
|
public readonly name: string;
|
|
28
25
|
|
|
29
26
|
/**
|
|
30
27
|
* データの複製に使用する関数です。
|
|
31
28
|
*/
|
|
32
|
-
|
|
29
|
+
readonly #clone: <T>(value: T) => T;
|
|
33
30
|
|
|
34
31
|
/**
|
|
35
32
|
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
36
33
|
*/
|
|
37
34
|
public constructor(options: MemoryOptions = {}) {
|
|
35
|
+
const { clone = <T>(value: T): T => structuredClone(value) } = options;
|
|
36
|
+
|
|
38
37
|
this.name = "Memory";
|
|
39
|
-
this
|
|
40
|
-
this
|
|
38
|
+
this.#map = new Map();
|
|
39
|
+
this.#clone = clone;
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
/**
|
|
44
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#usage)
|
|
45
|
-
*/
|
|
46
42
|
public get isOpen(): boolean {
|
|
47
43
|
return true;
|
|
48
44
|
}
|
|
49
45
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*/
|
|
53
|
-
public write(args: Pick<IStorage.WriteArgs<any>, "key" | "data">): void {
|
|
54
|
-
const { key, data } = args;
|
|
55
|
-
this.map.set(key, this.clone(data));
|
|
46
|
+
public write(c: IStorage.WriteContext, data: any): void {
|
|
47
|
+
this.#map.set(c.key, this.#clone(data));
|
|
56
48
|
}
|
|
57
49
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
public read(args: Pick<IStorage.ReadArgs, "key">): any {
|
|
62
|
-
const { key } = args;
|
|
63
|
-
if (!this.map.has(key)) {
|
|
64
|
-
throw new KeyNotFoundError({ key });
|
|
50
|
+
public read(c: IStorage.ReadContext): any {
|
|
51
|
+
if (!this.#map.has(c.key)) {
|
|
52
|
+
throw new KeyNotFoundError({ key: c.key });
|
|
65
53
|
}
|
|
66
54
|
|
|
67
|
-
const value = this
|
|
55
|
+
const value = this.#map.get(c.key);
|
|
68
56
|
|
|
69
|
-
return this
|
|
57
|
+
return this.#clone(value);
|
|
70
58
|
}
|
|
71
59
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
*/
|
|
75
|
-
public exists(args: Pick<IStorage.ExistsArgs, "key">): boolean {
|
|
76
|
-
const { key } = args;
|
|
77
|
-
const exists = this.map.has(key);
|
|
60
|
+
public exists(c: IStorage.ExistsContext): boolean {
|
|
61
|
+
const exists = this.#map.has(c.key);
|
|
78
62
|
|
|
79
63
|
return exists;
|
|
80
64
|
}
|
|
81
65
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
public delete(args: Pick<IStorage.DeleteArgs, "key">): void {
|
|
86
|
-
const { key } = args;
|
|
87
|
-
if (!this.map.has(key)) {
|
|
88
|
-
throw new KeyNotFoundError({ key });
|
|
66
|
+
public delete(c: IStorage.DeleteContext): void {
|
|
67
|
+
if (!this.#map.has(c.key)) {
|
|
68
|
+
throw new KeyNotFoundError({ key: c.key });
|
|
89
69
|
}
|
|
90
70
|
|
|
91
|
-
this
|
|
71
|
+
this.#map.delete(c.key);
|
|
92
72
|
}
|
|
93
73
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
*/
|
|
97
|
-
public clear(): void {
|
|
98
|
-
this.map.clear();
|
|
74
|
+
public clear(_c: IStorage.ClearContext): void {
|
|
75
|
+
this.#map.clear();
|
|
99
76
|
}
|
|
100
77
|
|
|
101
|
-
|
|
102
|
-
* [API Reference](https://tai-kun.github.io/unikvs/packages/memory#data)
|
|
103
|
-
*/
|
|
104
|
-
public getWritable(
|
|
105
|
-
args: Pick<IStorage.GetWritableArgs, "key">,
|
|
106
|
-
): WritableStream<Uint8Array<ArrayBuffer>> {
|
|
107
|
-
const { key } = args;
|
|
78
|
+
public getWritable(c: IStorage.GetWritableContext): WritableStream<Uint8Array<ArrayBuffer>> {
|
|
108
79
|
// メモリーストレージにはネイティブなストリームがないため、書き込まれたチャンクを配列に保持し、クローズ時に結合して保存します。
|
|
109
80
|
const chunks: Uint8Array[] = [];
|
|
110
81
|
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);
|
|
82
|
+
close: (): void => {
|
|
83
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
120
84
|
const merged = new Uint8Array(totalLength);
|
|
121
85
|
|
|
122
86
|
let offset = 0;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
offset
|
|
87
|
+
|
|
88
|
+
for (const chunk of chunks) {
|
|
89
|
+
merged.set(chunk, offset);
|
|
90
|
+
offset += chunk.byteLength;
|
|
126
91
|
}
|
|
127
92
|
|
|
128
|
-
this
|
|
93
|
+
this.#map.set(c.key, merged);
|
|
94
|
+
},
|
|
95
|
+
write: (chunk): void => {
|
|
96
|
+
if (!(chunk instanceof Uint8Array)) {
|
|
97
|
+
throw new InvalidChunkTypeError({
|
|
98
|
+
key: c.key,
|
|
99
|
+
chunk,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
chunks.push(this.#clone(chunk));
|
|
129
104
|
},
|
|
130
105
|
});
|
|
131
106
|
|
|
132
107
|
return stream;
|
|
133
108
|
}
|
|
134
109
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
public getReadable(
|
|
139
|
-
args: Pick<IStorage.GetReadableArgs, "key">,
|
|
140
|
-
): ReadableStream<Uint8Array<ArrayBuffer>> {
|
|
141
|
-
const { key } = args;
|
|
142
|
-
if (!this.map.has(key)) {
|
|
143
|
-
throw new KeyNotFoundError({ key });
|
|
110
|
+
public getReadable(c: IStorage.GetReadableContext): ReadableStream<Uint8Array<ArrayBuffer>> {
|
|
111
|
+
if (!this.#map.has(c.key)) {
|
|
112
|
+
throw new KeyNotFoundError({ key: c.key });
|
|
144
113
|
}
|
|
145
114
|
|
|
146
115
|
// メモリーストレージにはネイティブなストリームがないため、既存の値を単一チャンクとしてストリームで送出します。
|
|
147
|
-
const value = this
|
|
116
|
+
const value = this.#map.get(c.key);
|
|
148
117
|
const stream = new ReadableStream<Uint8Array<ArrayBuffer>>({
|
|
149
|
-
pull: (controller) => {
|
|
118
|
+
pull: (controller): void => {
|
|
150
119
|
if (!(value instanceof Uint8Array)) {
|
|
151
|
-
throw new InvalidChunkTypeError({
|
|
120
|
+
throw new InvalidChunkTypeError({
|
|
121
|
+
key: c.key,
|
|
122
|
+
chunk: value,
|
|
123
|
+
});
|
|
152
124
|
}
|
|
153
125
|
|
|
154
|
-
controller.enqueue(this
|
|
126
|
+
controller.enqueue(this.#clone(value) as Uint8Array<ArrayBuffer>);
|
|
155
127
|
controller.close();
|
|
156
128
|
},
|
|
157
129
|
});
|
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,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AI,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
|
-
}
|