@unikvs/readonly 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +2 -2
- package/dist/index.d.ts +92 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +97 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -3
- package/src/errors.ts +111 -0
- package/src/index.ts +14 -0
- package/src/readonly.ts +107 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-present, tai-kun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unikvs/readonly
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Document](https://tai-kun.github.io/unikvs/)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { ErrorOptions, IMiddleware, InvalidUsageErrorBase } from "@unikvs/core";
|
|
2
|
+
//#region ../src/errors.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
5
|
+
*/
|
|
6
|
+
type ReadOnlySetNotAllowedErrorMeta = {
|
|
7
|
+
/**
|
|
8
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
9
|
+
*/
|
|
10
|
+
readonly key: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
14
|
+
*/
|
|
15
|
+
type ReadOnlySetNotAllowedErrorArgs = ErrorOptions & ReadOnlySetNotAllowedErrorMeta;
|
|
16
|
+
/**
|
|
17
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
18
|
+
*/
|
|
19
|
+
export declare class ReadOnlySetNotAllowedError extends InvalidUsageErrorBase<ReadOnlySetNotAllowedErrorMeta> {
|
|
20
|
+
/**
|
|
21
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
22
|
+
*/
|
|
23
|
+
constructor(args: ReadOnlySetNotAllowedErrorArgs);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
27
|
+
*/
|
|
28
|
+
type ReadOnlyDeleteNotAllowedErrorMeta = {
|
|
29
|
+
/**
|
|
30
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
31
|
+
*/
|
|
32
|
+
readonly key: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
36
|
+
*/
|
|
37
|
+
type ReadOnlyDeleteNotAllowedErrorArgs = ErrorOptions & ReadOnlyDeleteNotAllowedErrorMeta;
|
|
38
|
+
/**
|
|
39
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
40
|
+
*/
|
|
41
|
+
export declare class ReadOnlyDeleteNotAllowedError extends InvalidUsageErrorBase<ReadOnlyDeleteNotAllowedErrorMeta> {
|
|
42
|
+
/**
|
|
43
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
44
|
+
*/
|
|
45
|
+
constructor(args: ReadOnlyDeleteNotAllowedErrorArgs);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-clear-not-allowed)
|
|
49
|
+
*/
|
|
50
|
+
export declare class ReadOnlyClearNotAllowedError extends InvalidUsageErrorBase<undefined> {
|
|
51
|
+
/**
|
|
52
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-clear-not-allowed)
|
|
53
|
+
*/
|
|
54
|
+
constructor(options?: ErrorOptions);
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region ../src/readonly.d.ts
|
|
58
|
+
/**
|
|
59
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
60
|
+
*/
|
|
61
|
+
type ReadOnlyOptions = {
|
|
62
|
+
/**
|
|
63
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly-allow-delete)
|
|
64
|
+
*/
|
|
65
|
+
readonly allowDelete?: boolean | undefined;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
69
|
+
*/
|
|
70
|
+
declare class ReadOnly implements IMiddleware {
|
|
71
|
+
/**
|
|
72
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
73
|
+
*/
|
|
74
|
+
readonly name: string;
|
|
75
|
+
/**
|
|
76
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
77
|
+
*/
|
|
78
|
+
readonly allowDelete: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
81
|
+
*/
|
|
82
|
+
constructor(options?: ReadOnlyOptions);
|
|
83
|
+
get isOpen(): boolean;
|
|
84
|
+
encode(c: IMiddleware.EncodeContext, _next: IMiddleware.INext): Promise<void>;
|
|
85
|
+
decode(c: IMiddleware.DecodeContext, next: IMiddleware.INext): Promise<void>;
|
|
86
|
+
encodeStream(c: IMiddleware.EncodeStreamContext, _next: IMiddleware.INext): Promise<void>;
|
|
87
|
+
decodeStream(c: IMiddleware.DecodeStreamContext, next: IMiddleware.INext): Promise<void>;
|
|
88
|
+
use(c: IMiddleware.UseContext, next: IMiddleware.INext): Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
export { ReadOnly, type ReadOnlyDeleteNotAllowedErrorArgs, type ReadOnlyDeleteNotAllowedErrorMeta, type ReadOnlyOptions, type ReadOnlySetNotAllowedErrorArgs, type ReadOnlySetNotAllowedErrorMeta };
|
|
92
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/errors.ts","../src/readonly.ts"],"mappings":";;;;;KAKY;;;;WAID;;;;;KAMC,iCAAiC,eAAe;;;;qBAK/C,mCAAmC,sBAAsB;;;;EAQpE,YAAmB,MAAM;;;;;KAoBf;;;;WAID;;;;;KAMC,oCAAoC,eAAe;;;;qBAKlD,sCAAsC,sBAAsB;;;;EAQvE,YAAmB,MAAM;;;;;qBAsBd,qCAAqC;;;;EAQhD,YAAmB,UAAU;;;;;;;KC1FnB;;;;WAID;;;;;cAMU,oBAAoB;;;;WAIvB;;;;WAKA;;;;EAKhB,YAAmB,UAAU;MAIlB;EAIX,OAAoB,GAAG,YAAY,eAAe,OAAO,YAAY,QAAQ;EAI7E,OAAoB,GAAG,YAAY,eAAe,MAAM,YAAY,QAAQ;EAM5E,aACE,GAAG,YAAY,qBACf,OAAO,YAAY,QAClB;EAIH,aACE,GAAG,YAAY,qBACf,MAAM,YAAY,QACjB;EAMH,IAAiB,GAAG,YAAY,YAAY,MAAM,YAAY,QAAQ"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { InvalidUsageErrorBase, setErrorMessage } from "@unikvs/core";
|
|
2
|
+
//#region ../src/errors.ts
|
|
3
|
+
var _ReadOnlySetNotAllowedError;
|
|
4
|
+
var _ReadOnlyDeleteNotAllowedError;
|
|
5
|
+
var _ReadOnlyClearNotAllowedError;
|
|
6
|
+
/**
|
|
7
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
8
|
+
*/
|
|
9
|
+
var ReadOnlySetNotAllowedError = class extends InvalidUsageErrorBase {
|
|
10
|
+
/**
|
|
11
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
12
|
+
*/
|
|
13
|
+
constructor(args) {
|
|
14
|
+
const { key, ...options } = args;
|
|
15
|
+
super({ key }, ({ key: messageKey }) => `ReadOnly.set cannot write key ${JSON.stringify(messageKey)}.`, options);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
_ReadOnlySetNotAllowedError = ReadOnlySetNotAllowedError;
|
|
19
|
+
_ReadOnlySetNotAllowedError.prototype.name = "ReadOnlySetNotAllowedError";
|
|
20
|
+
setErrorMessage(ReadOnlySetNotAllowedError, ({ key }) => `ReadOnly.set は読み取り専用のためキー ${JSON.stringify(key)} に書き込めません。`, "ja");
|
|
21
|
+
/**
|
|
22
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
23
|
+
*/
|
|
24
|
+
var ReadOnlyDeleteNotAllowedError = class extends InvalidUsageErrorBase {
|
|
25
|
+
/**
|
|
26
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
27
|
+
*/
|
|
28
|
+
constructor(args) {
|
|
29
|
+
const { key, ...options } = args;
|
|
30
|
+
super({ key }, ({ key: messageKey }) => `ReadOnly.delete with allowDelete false cannot delete key ${JSON.stringify(messageKey)}.`, options);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
_ReadOnlyDeleteNotAllowedError = ReadOnlyDeleteNotAllowedError;
|
|
34
|
+
_ReadOnlyDeleteNotAllowedError.prototype.name = "ReadOnlyDeleteNotAllowedError";
|
|
35
|
+
setErrorMessage(ReadOnlyDeleteNotAllowedError, ({ key }) => `ReadOnly.delete は allowDelete が false のためキー ${JSON.stringify(key)} を削除できません。`, "ja");
|
|
36
|
+
/**
|
|
37
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-clear-not-allowed)
|
|
38
|
+
*/
|
|
39
|
+
var ReadOnlyClearNotAllowedError = class extends InvalidUsageErrorBase {
|
|
40
|
+
/**
|
|
41
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-clear-not-allowed)
|
|
42
|
+
*/
|
|
43
|
+
constructor(options) {
|
|
44
|
+
super("ReadOnly.clear with allowDelete false cannot delete all data.", options);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
_ReadOnlyClearNotAllowedError = ReadOnlyClearNotAllowedError;
|
|
48
|
+
_ReadOnlyClearNotAllowedError.prototype.name = "ReadOnlyClearNotAllowedError";
|
|
49
|
+
setErrorMessage(ReadOnlyClearNotAllowedError, "ReadOnly.clear は allowDelete が false のため全件を削除できません。", "ja");
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region ../src/readonly.ts
|
|
52
|
+
/**
|
|
53
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
54
|
+
*/
|
|
55
|
+
var ReadOnly = class {
|
|
56
|
+
/**
|
|
57
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
58
|
+
*/
|
|
59
|
+
constructor(options) {
|
|
60
|
+
this.name = "ReadOnly";
|
|
61
|
+
this.allowDelete = Boolean(options?.allowDelete);
|
|
62
|
+
}
|
|
63
|
+
get isOpen() {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
async encode(c, _next) {
|
|
67
|
+
throw new ReadOnlySetNotAllowedError({ key: c.key });
|
|
68
|
+
}
|
|
69
|
+
async decode(c, next) {
|
|
70
|
+
c.destination = c.source;
|
|
71
|
+
await next();
|
|
72
|
+
}
|
|
73
|
+
async encodeStream(c, _next) {
|
|
74
|
+
throw new ReadOnlySetNotAllowedError({ key: c.key });
|
|
75
|
+
}
|
|
76
|
+
async decodeStream(c, next) {
|
|
77
|
+
c.destination = c.source;
|
|
78
|
+
await next();
|
|
79
|
+
}
|
|
80
|
+
async use(c, next) {
|
|
81
|
+
switch (c.action) {
|
|
82
|
+
case "set": throw new ReadOnlySetNotAllowedError({ key: c.key });
|
|
83
|
+
case "get":
|
|
84
|
+
case "has":
|
|
85
|
+
case "stream": break;
|
|
86
|
+
case "delete":
|
|
87
|
+
if (!this.allowDelete) throw new ReadOnlyDeleteNotAllowedError({ key: c.key });
|
|
88
|
+
break;
|
|
89
|
+
case "clear": if (!this.allowDelete) throw new ReadOnlyClearNotAllowedError();
|
|
90
|
+
}
|
|
91
|
+
await next();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
//#endregion
|
|
95
|
+
export { ReadOnly, ReadOnlyClearNotAllowedError, ReadOnlyDeleteNotAllowedError, ReadOnlySetNotAllowedError };
|
|
96
|
+
|
|
97
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/errors.ts","../src/readonly.ts"],"sourcesContent":["import { type ErrorOptions, setErrorMessage, InvalidUsageErrorBase } from \"@unikvs/core\";\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)\n */\nexport type ReadOnlySetNotAllowedErrorMeta = {\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)\n */\n readonly key: string;\n};\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)\n */\nexport type ReadOnlySetNotAllowedErrorArgs = ErrorOptions & ReadOnlySetNotAllowedErrorMeta;\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)\n */\nexport class ReadOnlySetNotAllowedError extends InvalidUsageErrorBase<ReadOnlySetNotAllowedErrorMeta> {\n static {\n this.prototype.name = \"ReadOnlySetNotAllowedError\";\n }\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)\n */\n public constructor(args: ReadOnlySetNotAllowedErrorArgs) {\n const { key, ...options } = args;\n const meta: ReadOnlySetNotAllowedErrorMeta = { key };\n super(\n meta,\n ({ key: messageKey }) => `ReadOnly.set cannot write key ${JSON.stringify(messageKey)}.`,\n options,\n );\n }\n}\n\nsetErrorMessage(\n ReadOnlySetNotAllowedError,\n ({ key }) => `ReadOnly.set は読み取り専用のためキー ${JSON.stringify(key)} に書き込めません。`,\n \"ja\",\n);\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)\n */\nexport type ReadOnlyDeleteNotAllowedErrorMeta = {\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)\n */\n readonly key: string;\n};\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)\n */\nexport type ReadOnlyDeleteNotAllowedErrorArgs = ErrorOptions & ReadOnlyDeleteNotAllowedErrorMeta;\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)\n */\nexport class ReadOnlyDeleteNotAllowedError extends InvalidUsageErrorBase<ReadOnlyDeleteNotAllowedErrorMeta> {\n static {\n this.prototype.name = \"ReadOnlyDeleteNotAllowedError\";\n }\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)\n */\n public constructor(args: ReadOnlyDeleteNotAllowedErrorArgs) {\n const { key, ...options } = args;\n const meta: ReadOnlyDeleteNotAllowedErrorMeta = { key };\n super(\n meta,\n ({ key: messageKey }) =>\n `ReadOnly.delete with allowDelete false cannot delete key ${JSON.stringify(messageKey)}.`,\n options,\n );\n }\n}\n\nsetErrorMessage(\n ReadOnlyDeleteNotAllowedError,\n ({ key }) =>\n `ReadOnly.delete は allowDelete が false のためキー ${JSON.stringify(key)} を削除できません。`,\n \"ja\",\n);\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-clear-not-allowed)\n */\nexport class ReadOnlyClearNotAllowedError extends InvalidUsageErrorBase<undefined> {\n static {\n this.prototype.name = \"ReadOnlyClearNotAllowedError\";\n }\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-clear-not-allowed)\n */\n public constructor(options?: ErrorOptions) {\n super(\"ReadOnly.clear with allowDelete false cannot delete all data.\", options);\n }\n}\n\nsetErrorMessage(\n ReadOnlyClearNotAllowedError,\n \"ReadOnly.clear は allowDelete が false のため全件を削除できません。\",\n \"ja\",\n);\n","import { type IMiddleware } from \"@unikvs/core\";\n\nimport {\n ReadOnlySetNotAllowedError,\n ReadOnlyClearNotAllowedError,\n ReadOnlyDeleteNotAllowedError,\n} from \"./errors.js\";\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)\n */\nexport type ReadOnlyOptions = {\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly-allow-delete)\n */\n readonly allowDelete?: boolean | undefined;\n};\n\n/**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)\n */\nexport default class ReadOnly implements IMiddleware {\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)\n */\n public readonly name: string = \"ReadOnly\";\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)\n */\n public readonly allowDelete: boolean;\n\n /**\n * [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)\n */\n public constructor(options?: ReadOnlyOptions) {\n this.allowDelete = Boolean(options?.allowDelete);\n }\n\n public get isOpen(): boolean {\n return true;\n }\n\n public async encode(c: IMiddleware.EncodeContext, _next: IMiddleware.INext): Promise<void> {\n throw new ReadOnlySetNotAllowedError({ key: c.key });\n }\n\n public async decode(c: IMiddleware.DecodeContext, next: IMiddleware.INext): Promise<void> {\n c.destination = c.source;\n\n await next();\n }\n\n public async encodeStream(\n c: IMiddleware.EncodeStreamContext,\n _next: IMiddleware.INext,\n ): Promise<void> {\n throw new ReadOnlySetNotAllowedError({ key: c.key });\n }\n\n public async decodeStream(\n c: IMiddleware.DecodeStreamContext,\n next: IMiddleware.INext,\n ): Promise<void> {\n c.destination = c.source;\n\n await next();\n }\n\n public async use(c: IMiddleware.UseContext, next: IMiddleware.INext): Promise<void> {\n switch (c.action) {\n case \"set\": {\n throw new ReadOnlySetNotAllowedError({ key: c.key });\n }\n\n case \"get\":\n case \"has\":\n case \"stream\": {\n break;\n }\n\n case \"delete\": {\n // 削除が許可されていない場合は delete を拒否します。\n if (!this.allowDelete) {\n throw new ReadOnlyDeleteNotAllowedError({ key: c.key });\n }\n\n break;\n }\n\n case \"clear\": {\n // 削除が許可されていない場合は clear を拒否します。\n if (!this.allowDelete) {\n throw new ReadOnlyClearNotAllowedError();\n }\n\n break;\n }\n\n default: {\n break;\n }\n }\n\n await next();\n }\n}\n"],"mappings":";;;;;;;;AAoBA,IAAa,6BAAb,cAAgD,sBAAsD;;;;CAQpG,YAAmB,MAAsC;EACvD,MAAM,EAAE,KAAK,GAAG,YAAY;EAE5B,MACE,EAF6C,IAE1C,IACF,EAAE,KAAK,iBAAiB,iCAAiC,KAAK,UAAU,UAAU,EAAE,IACrF,OACF;CACF;AACF;;AAfI,4BAAK,UAAU,OAAO;AAiB1B,gBACE,6BACC,EAAE,UAAU,6BAA6B,KAAK,UAAU,GAAG,EAAE,aAC9D,IACF;;;;AAoBA,IAAa,gCAAb,cAAmD,sBAAyD;;;;CAQ1G,YAAmB,MAAyC;EAC1D,MAAM,EAAE,KAAK,GAAG,YAAY;EAE5B,MACE,EAFgD,IAE7C,IACF,EAAE,KAAK,iBACN,4DAA4D,KAAK,UAAU,UAAU,EAAE,IACzF,OACF;CACF;AACF;;AAhBI,+BAAK,UAAU,OAAO;AAkB1B,gBACE,gCACC,EAAE,UACD,+CAA+C,KAAK,UAAU,GAAG,EAAE,aACrE,IACF;;;;AAKA,IAAa,+BAAb,cAAkD,sBAAiC;;;;CAQjF,YAAmB,SAAwB;EACzC,MAAM,iEAAiE,OAAO;CAChF;AACF;;AATI,8BAAK,UAAU,OAAO;AAW1B,gBACE,8BACA,uDACA,IACF;;;;;;ACzFA,IAAqB,WAArB,MAAqD;;;;CAcnD,YAAmB,SAA2B;EAV9C,KAAgB,OAAe;EAW7B,KAAK,cAAc,QAAQ,SAAS,WAAW;CACjD;CAEA,IAAW,SAAkB;EAC3B,OAAO;CACT;CAEA,MAAa,OAAO,GAA8B,OAAyC;EACzF,MAAM,IAAI,2BAA2B,EAAE,KAAK,EAAE,IAAI,CAAC;CACrD;CAEA,MAAa,OAAO,GAA8B,MAAwC;EACxF,EAAE,cAAc,EAAE;EAElB,MAAM,KAAK;CACb;CAEA,MAAa,aACX,GACA,OACe;EACf,MAAM,IAAI,2BAA2B,EAAE,KAAK,EAAE,IAAI,CAAC;CACrD;CAEA,MAAa,aACX,GACA,MACe;EACf,EAAE,cAAc,EAAE;EAElB,MAAM,KAAK;CACb;CAEA,MAAa,IAAI,GAA2B,MAAwC;EAClF,QAAQ,EAAE,QAAV;GACE,KAAK,OACH,MAAM,IAAI,2BAA2B,EAAE,KAAK,EAAE,IAAI,CAAC;GAGrD,KAAK;GACL,KAAK;GACL,KAAK,UACH;GAGF,KAAK;IAEH,IAAI,CAAC,KAAK,aACR,MAAM,IAAI,8BAA8B,EAAE,KAAK,EAAE,IAAI,CAAC;IAGxD;GAGF,KAAK,SAEH,IAAI,CAAC,KAAK,aACR,MAAM,IAAI,6BAA6B;EAS7C;EAEA,MAAM,KAAK;CACb;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unikvs/readonly",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.13",
|
|
4
|
+
"description": "Transformer plugin that rejects writes and allows reads.",
|
|
5
|
+
"homepage": "https://tai-kun.github.io/unikvs/",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"url": "https://github.com/tai-kun/unikvs",
|
|
9
|
+
"directory": "packages/readonly"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"src",
|
|
14
|
+
"package.json",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@unikvs/core": "0.0.13"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@unikvs/memory": "0.0.13",
|
|
31
|
+
"@unikvs/testing": "0.0.0",
|
|
32
|
+
"@vitest/browser-playwright": "5.0.5",
|
|
33
|
+
"@vitest/coverage-istanbul": "5.0.5",
|
|
34
|
+
"@vitest/coverage-v8": "5.0.5",
|
|
35
|
+
"playwright": "1.64.1",
|
|
36
|
+
"unikvs": "0.0.13",
|
|
37
|
+
"valibot": "1.5.0",
|
|
38
|
+
"vite-plus": "1.3.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@logtape/logtape": "^2.0.0"
|
|
42
|
+
},
|
|
43
|
+
"readme": "README.md",
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "vp -C .config pack"
|
|
46
|
+
}
|
|
6
47
|
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type ErrorOptions, setErrorMessage, InvalidUsageErrorBase } from "@unikvs/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
5
|
+
*/
|
|
6
|
+
export type ReadOnlySetNotAllowedErrorMeta = {
|
|
7
|
+
/**
|
|
8
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
9
|
+
*/
|
|
10
|
+
readonly key: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
15
|
+
*/
|
|
16
|
+
export type ReadOnlySetNotAllowedErrorArgs = ErrorOptions & ReadOnlySetNotAllowedErrorMeta;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
20
|
+
*/
|
|
21
|
+
export class ReadOnlySetNotAllowedError extends InvalidUsageErrorBase<ReadOnlySetNotAllowedErrorMeta> {
|
|
22
|
+
static {
|
|
23
|
+
this.prototype.name = "ReadOnlySetNotAllowedError";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-set-not-allowed)
|
|
28
|
+
*/
|
|
29
|
+
public constructor(args: ReadOnlySetNotAllowedErrorArgs) {
|
|
30
|
+
const { key, ...options } = args;
|
|
31
|
+
const meta: ReadOnlySetNotAllowedErrorMeta = { key };
|
|
32
|
+
super(
|
|
33
|
+
meta,
|
|
34
|
+
({ key: messageKey }) => `ReadOnly.set cannot write key ${JSON.stringify(messageKey)}.`,
|
|
35
|
+
options,
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
setErrorMessage(
|
|
41
|
+
ReadOnlySetNotAllowedError,
|
|
42
|
+
({ key }) => `ReadOnly.set は読み取り専用のためキー ${JSON.stringify(key)} に書き込めません。`,
|
|
43
|
+
"ja",
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
48
|
+
*/
|
|
49
|
+
export type ReadOnlyDeleteNotAllowedErrorMeta = {
|
|
50
|
+
/**
|
|
51
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
52
|
+
*/
|
|
53
|
+
readonly key: string;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
58
|
+
*/
|
|
59
|
+
export type ReadOnlyDeleteNotAllowedErrorArgs = ErrorOptions & ReadOnlyDeleteNotAllowedErrorMeta;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
63
|
+
*/
|
|
64
|
+
export class ReadOnlyDeleteNotAllowedError extends InvalidUsageErrorBase<ReadOnlyDeleteNotAllowedErrorMeta> {
|
|
65
|
+
static {
|
|
66
|
+
this.prototype.name = "ReadOnlyDeleteNotAllowedError";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-delete-not-allowed)
|
|
71
|
+
*/
|
|
72
|
+
public constructor(args: ReadOnlyDeleteNotAllowedErrorArgs) {
|
|
73
|
+
const { key, ...options } = args;
|
|
74
|
+
const meta: ReadOnlyDeleteNotAllowedErrorMeta = { key };
|
|
75
|
+
super(
|
|
76
|
+
meta,
|
|
77
|
+
({ key: messageKey }) =>
|
|
78
|
+
`ReadOnly.delete with allowDelete false cannot delete key ${JSON.stringify(messageKey)}.`,
|
|
79
|
+
options,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
setErrorMessage(
|
|
85
|
+
ReadOnlyDeleteNotAllowedError,
|
|
86
|
+
({ key }) =>
|
|
87
|
+
`ReadOnly.delete は allowDelete が false のためキー ${JSON.stringify(key)} を削除できません。`,
|
|
88
|
+
"ja",
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-clear-not-allowed)
|
|
93
|
+
*/
|
|
94
|
+
export class ReadOnlyClearNotAllowedError extends InvalidUsageErrorBase<undefined> {
|
|
95
|
+
static {
|
|
96
|
+
this.prototype.name = "ReadOnlyClearNotAllowedError";
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#error-read-only-clear-not-allowed)
|
|
101
|
+
*/
|
|
102
|
+
public constructor(options?: ErrorOptions) {
|
|
103
|
+
super("ReadOnly.clear with allowDelete false cannot delete all data.", options);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
setErrorMessage(
|
|
108
|
+
ReadOnlyClearNotAllowedError,
|
|
109
|
+
"ReadOnly.clear は allowDelete が false のため全件を削除できません。",
|
|
110
|
+
"ja",
|
|
111
|
+
);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ReadOnlySetNotAllowedErrorArgs,
|
|
3
|
+
ReadOnlySetNotAllowedErrorMeta,
|
|
4
|
+
ReadOnlyDeleteNotAllowedErrorArgs,
|
|
5
|
+
ReadOnlyDeleteNotAllowedErrorMeta,
|
|
6
|
+
} from "./errors.js";
|
|
7
|
+
export {
|
|
8
|
+
ReadOnlySetNotAllowedError,
|
|
9
|
+
ReadOnlyClearNotAllowedError,
|
|
10
|
+
ReadOnlyDeleteNotAllowedError,
|
|
11
|
+
} from "./errors.js";
|
|
12
|
+
|
|
13
|
+
export type * from "./readonly.js";
|
|
14
|
+
export { default as ReadOnly } from "./readonly.js";
|
package/src/readonly.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { type IMiddleware } from "@unikvs/core";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ReadOnlySetNotAllowedError,
|
|
5
|
+
ReadOnlyClearNotAllowedError,
|
|
6
|
+
ReadOnlyDeleteNotAllowedError,
|
|
7
|
+
} from "./errors.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
11
|
+
*/
|
|
12
|
+
export type ReadOnlyOptions = {
|
|
13
|
+
/**
|
|
14
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly-allow-delete)
|
|
15
|
+
*/
|
|
16
|
+
readonly allowDelete?: boolean | undefined;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
21
|
+
*/
|
|
22
|
+
export default class ReadOnly implements IMiddleware {
|
|
23
|
+
/**
|
|
24
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
25
|
+
*/
|
|
26
|
+
public readonly name: string = "ReadOnly";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
30
|
+
*/
|
|
31
|
+
public readonly allowDelete: boolean;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/readonly#readonly)
|
|
35
|
+
*/
|
|
36
|
+
public constructor(options?: ReadOnlyOptions) {
|
|
37
|
+
this.allowDelete = Boolean(options?.allowDelete);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public get isOpen(): boolean {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public async encode(c: IMiddleware.EncodeContext, _next: IMiddleware.INext): Promise<void> {
|
|
45
|
+
throw new ReadOnlySetNotAllowedError({ key: c.key });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async decode(c: IMiddleware.DecodeContext, next: IMiddleware.INext): Promise<void> {
|
|
49
|
+
c.destination = c.source;
|
|
50
|
+
|
|
51
|
+
await next();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async encodeStream(
|
|
55
|
+
c: IMiddleware.EncodeStreamContext,
|
|
56
|
+
_next: IMiddleware.INext,
|
|
57
|
+
): Promise<void> {
|
|
58
|
+
throw new ReadOnlySetNotAllowedError({ key: c.key });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public async decodeStream(
|
|
62
|
+
c: IMiddleware.DecodeStreamContext,
|
|
63
|
+
next: IMiddleware.INext,
|
|
64
|
+
): Promise<void> {
|
|
65
|
+
c.destination = c.source;
|
|
66
|
+
|
|
67
|
+
await next();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public async use(c: IMiddleware.UseContext, next: IMiddleware.INext): Promise<void> {
|
|
71
|
+
switch (c.action) {
|
|
72
|
+
case "set": {
|
|
73
|
+
throw new ReadOnlySetNotAllowedError({ key: c.key });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
case "get":
|
|
77
|
+
case "has":
|
|
78
|
+
case "stream": {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
case "delete": {
|
|
83
|
+
// 削除が許可されていない場合は delete を拒否します。
|
|
84
|
+
if (!this.allowDelete) {
|
|
85
|
+
throw new ReadOnlyDeleteNotAllowedError({ key: c.key });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
case "clear": {
|
|
92
|
+
// 削除が許可されていない場合は clear を拒否します。
|
|
93
|
+
if (!this.allowDelete) {
|
|
94
|
+
throw new ReadOnlyClearNotAllowedError();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
default: {
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
await next();
|
|
106
|
+
}
|
|
107
|
+
}
|