@nice-code/util 0.6.3 → 0.8.0
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/build/index.js +33 -1
- package/build/types/index.d.ts +2 -1
- package/build/types/storage_adapter/StorageAdapter.d.ts +2 -2
- package/build/types/storage_adapter/specific/{durable_object → cloudflare/durable_object}/durable_object_storage.d.ts +3 -3
- package/build/types/storage_adapter/specific/cloudflare/kv/kv_storage.d.ts +18 -0
- package/build/types/storage_adapter/specific/cloudflare/kv/kv_storage.types.d.ts +22 -0
- package/package.json +7 -4
- /package/build/types/storage_adapter/specific/{durable_object → cloudflare/durable_object}/durable_object_storage.types.d.ts +0 -0
package/build/index.js
CHANGED
|
@@ -918,11 +918,13 @@ class StorageAdapter {
|
|
|
918
918
|
const currentVal = await this.getJson(rawKey);
|
|
919
919
|
const newVal = updater(currentVal);
|
|
920
920
|
await this.setJson(rawKey, newVal);
|
|
921
|
+
return newVal;
|
|
921
922
|
}
|
|
922
923
|
async updateJsonOrDef(rawKey, defVal, updater) {
|
|
923
924
|
const currentVal = await this.getJsonOrDef(rawKey, defVal);
|
|
924
925
|
const newVal = updater(currentVal);
|
|
925
926
|
await this.setJson(rawKey, newVal);
|
|
927
|
+
return newVal;
|
|
926
928
|
}
|
|
927
929
|
createJsonGetterSetter(rawKey) {
|
|
928
930
|
return {
|
|
@@ -984,7 +986,7 @@ function createTypedWebSessionStorage(options) {
|
|
|
984
986
|
storageAdapter: createWebSessionStorageAdapter(options)
|
|
985
987
|
});
|
|
986
988
|
}
|
|
987
|
-
// src/storage_adapter/specific/durable_object/durable_object_storage.ts
|
|
989
|
+
// src/storage_adapter/specific/cloudflare/durable_object/durable_object_storage.ts
|
|
988
990
|
function createDurableObjectStorageMethods(durableObjectStorage) {
|
|
989
991
|
return {
|
|
990
992
|
type: "json" /* json */,
|
|
@@ -1009,6 +1011,33 @@ function createDurableObjectTypedStorage(options) {
|
|
|
1009
1011
|
storageAdapter: createDurableObjectStorageAdapter(options)
|
|
1010
1012
|
});
|
|
1011
1013
|
}
|
|
1014
|
+
// src/storage_adapter/specific/cloudflare/kv/kv_storage.ts
|
|
1015
|
+
function createKVStorageMethods({
|
|
1016
|
+
kvNamespace,
|
|
1017
|
+
defaultPutOptions
|
|
1018
|
+
}) {
|
|
1019
|
+
return {
|
|
1020
|
+
type: "string" /* string */,
|
|
1021
|
+
getItem: (key) => kvNamespace.get(key),
|
|
1022
|
+
setItem: (key, value) => kvNamespace.put(key, value, defaultPutOptions),
|
|
1023
|
+
removeItem: (key) => kvNamespace.delete(key)
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
var createKVStorageAdapter = ({
|
|
1027
|
+
kvNamespace,
|
|
1028
|
+
defaultPutOptions,
|
|
1029
|
+
...options
|
|
1030
|
+
}) => {
|
|
1031
|
+
return new StorageAdapter({
|
|
1032
|
+
methods: createKVStorageMethods({ kvNamespace, defaultPutOptions }),
|
|
1033
|
+
...options
|
|
1034
|
+
});
|
|
1035
|
+
};
|
|
1036
|
+
function createKVTypedStorage(options) {
|
|
1037
|
+
return createTypedStorage({
|
|
1038
|
+
storageAdapter: createKVStorageAdapter(options)
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1012
1041
|
// src/storage_adapter/specific/memory/memory_storage.ts
|
|
1013
1042
|
function createMemoryStorageMethods_string(memoryStorageMap = new Map) {
|
|
1014
1043
|
return {
|
|
@@ -1095,6 +1124,9 @@ export {
|
|
|
1095
1124
|
createMemoryStorageMethods_json,
|
|
1096
1125
|
createMemoryStorageAdapter_string,
|
|
1097
1126
|
createMemoryStorageAdapter_json,
|
|
1127
|
+
createKVTypedStorage,
|
|
1128
|
+
createKVStorageMethods,
|
|
1129
|
+
createKVStorageAdapter,
|
|
1098
1130
|
createDurableObjectTypedStorage,
|
|
1099
1131
|
createDurableObjectStorageMethods,
|
|
1100
1132
|
createDurableObjectStorageAdapter,
|
package/build/types/index.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ export type { TTypeAndId } from "./core/core_valibot_schemas";
|
|
|
2
2
|
export * from "./crypto";
|
|
3
3
|
export * from "./storage_adapter/StorageAdapter";
|
|
4
4
|
export * from "./storage_adapter/specific/browser/browser_storage";
|
|
5
|
-
export * from "./storage_adapter/specific/durable_object/durable_object_storage";
|
|
5
|
+
export * from "./storage_adapter/specific/cloudflare/durable_object/durable_object_storage";
|
|
6
|
+
export * from "./storage_adapter/specific/cloudflare/kv/kv_storage";
|
|
6
7
|
export * from "./storage_adapter/specific/memory/memory_storage";
|
|
7
8
|
export * from "./storage_adapter/storage_adapter.types";
|
|
8
9
|
export * from "./storage_adapter/typed_storage/createTypedStorage";
|
|
@@ -17,7 +17,7 @@ export declare class StorageAdapter {
|
|
|
17
17
|
setJson(rawKey: string, value: any): Promise<void>;
|
|
18
18
|
getJson<T>(rawKey: string): Promise<T | undefined>;
|
|
19
19
|
getJsonOrDef<T>(rawKey: string, defVal: T): Promise<T>;
|
|
20
|
-
updateJson<T>(rawKey: string, updater: (currentVal: T | undefined) => T): Promise<
|
|
21
|
-
updateJsonOrDef<T>(rawKey: string, defVal: T, updater: (currentVal: T) => T): Promise<
|
|
20
|
+
updateJson<T>(rawKey: string, updater: (currentVal: T | undefined) => T): Promise<T>;
|
|
21
|
+
updateJsonOrDef<T>(rawKey: string, defVal: T, updater: (currentVal: T) => T): Promise<T>;
|
|
22
22
|
createJsonGetterSetter<T>(rawKey: string): IStorageKeyGetterAndSetter<T>;
|
|
23
23
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type IStorageAdapterConstructor, StorageAdapter } from "
|
|
2
|
-
import { type IStorageAdapterMethods_Json } from "
|
|
3
|
-
import { type ITypedStorage } from "
|
|
1
|
+
import { type IStorageAdapterConstructor, StorageAdapter } from "../../../StorageAdapter";
|
|
2
|
+
import { type IStorageAdapterMethods_Json } from "../../../storage_adapter.types";
|
|
3
|
+
import { type ITypedStorage } from "../../../typed_storage/createTypedStorage";
|
|
4
4
|
import type { IDurableObjectStorage } from "./durable_object_storage.types";
|
|
5
5
|
export declare function createDurableObjectStorageMethods(durableObjectStorage: IDurableObjectStorage): IStorageAdapterMethods_Json;
|
|
6
6
|
export type TCreateDurableObjectStorageOptions = Omit<IStorageAdapterConstructor, "methods"> & {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type IStorageAdapterConstructor, StorageAdapter } from "../../../StorageAdapter";
|
|
2
|
+
import { type IStorageAdapterMethods_String } from "../../../storage_adapter.types";
|
|
3
|
+
import { type ITypedStorage } from "../../../typed_storage/createTypedStorage";
|
|
4
|
+
import type { IKVNamespace, IKVNamespacePutOptions } from "./kv_storage.types";
|
|
5
|
+
export interface ICreateKVStorageMethodsOptions {
|
|
6
|
+
/** The `KVNamespace` binding, e.g. accessed off `env.MY_KV` inside a worker. */
|
|
7
|
+
kvNamespace: IKVNamespace;
|
|
8
|
+
/** Default options applied to every `put`, e.g. `{ expirationTtl }` for TTL-based eviction. */
|
|
9
|
+
defaultPutOptions?: IKVNamespacePutOptions;
|
|
10
|
+
}
|
|
11
|
+
export declare function createKVStorageMethods({ kvNamespace, defaultPutOptions, }: ICreateKVStorageMethodsOptions): IStorageAdapterMethods_String;
|
|
12
|
+
export type TCreateKVStorageOptions = Omit<IStorageAdapterConstructor, "methods"> & ICreateKVStorageMethodsOptions;
|
|
13
|
+
/**
|
|
14
|
+
* Wraps a Cloudflare KV namespace binding in the generic StorageAdapter interface, e.g. for handing
|
|
15
|
+
* to a ClientCryptoKeyLink so it can persist its keys inside KV.
|
|
16
|
+
*/
|
|
17
|
+
export declare const createKVStorageAdapter: ({ kvNamespace, defaultPutOptions, ...options }: TCreateKVStorageOptions) => StorageAdapter;
|
|
18
|
+
export declare function createKVTypedStorage<T extends Record<string, any>>(options: TCreateKVStorageOptions): ITypedStorage<T>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface IKVNamespaceGetOptions {
|
|
2
|
+
cacheTtl?: number;
|
|
3
|
+
}
|
|
4
|
+
interface IKVNamespacePutOptions {
|
|
5
|
+
/** Absolute time (seconds since epoch) at which the key should expire. */
|
|
6
|
+
expiration?: number;
|
|
7
|
+
/** Relative TTL (in seconds) from now, after which the key should expire. */
|
|
8
|
+
expirationTtl?: number;
|
|
9
|
+
metadata?: unknown;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Minimal subset of Cloudflare's `KVNamespace` binding that our storage adapter relies on. Values
|
|
13
|
+
* are always written/read as strings (KV's `put` only accepts string/stream/buffer values), so the
|
|
14
|
+
* adapter is a {@link EStorageAdapterType.string} adapter and JSON (de)serialization happens in the
|
|
15
|
+
* generic `StorageAdapter`.
|
|
16
|
+
*/
|
|
17
|
+
export interface IKVNamespace {
|
|
18
|
+
get(key: string, options?: IKVNamespaceGetOptions): Promise<string | null>;
|
|
19
|
+
put(key: string, value: string, options?: IKVNamespacePutOptions): Promise<void>;
|
|
20
|
+
delete(key: string): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export type { IKVNamespaceGetOptions, IKVNamespacePutOptions };
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nice-code/util",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
|
-
"
|
|
9
|
-
"import": "./build/index.js"
|
|
8
|
+
"source": "./src/index.ts",
|
|
9
|
+
"import": "./build/index.js",
|
|
10
|
+
"types": "./build/types/index.d.ts"
|
|
10
11
|
}
|
|
11
12
|
},
|
|
12
13
|
"files": [
|
|
@@ -30,7 +31,9 @@
|
|
|
30
31
|
"dependencies": {
|
|
31
32
|
"nanoid": "^5.1.9"
|
|
32
33
|
},
|
|
33
|
-
"devDependencies": {
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@scure/base": "^2.2.0"
|
|
36
|
+
},
|
|
34
37
|
"peerDependencies": {
|
|
35
38
|
"@tanstack/react-query": "^5.100.3",
|
|
36
39
|
"react": ">=19",
|