@casfa/storage-memory 0.2.0 → 0.3.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/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # @casfa/storage-memory
2
+
3
+ In-memory storage provider for CAS.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @casfa/storage-memory
9
+ ```
10
+
11
+ ## Overview
12
+
13
+ An in-memory storage provider for CAS (Content-Addressable Storage). Ideal for testing, development, and temporary storage needs.
14
+
15
+ > ⚠️ **Warning**: Data is not persisted and will be lost when the process exits. Use only for testing and development.
16
+
17
+ ## Usage
18
+
19
+ ### Basic Usage
20
+
21
+ ```typescript
22
+ import { createMemoryStorage } from '@casfa/storage-memory';
23
+
24
+ const storage = createMemoryStorage();
25
+
26
+ // Store data
27
+ await storage.put('node:abcd1234...', data);
28
+
29
+ // Retrieve data
30
+ const data = await storage.get('node:abcd1234...');
31
+ ```
32
+
33
+ ### With Inspection (Testing)
34
+
35
+ ```typescript
36
+ import { createMemoryStorageWithInspection } from '@casfa/storage-memory';
37
+
38
+ const { storage, inspect } = createMemoryStorageWithInspection();
39
+
40
+ // Use storage normally
41
+ await storage.put('node:abcd1234...', data);
42
+
43
+ // Inspect internal state (useful for tests)
44
+ console.log(inspect.size()); // Number of items
45
+ console.log(inspect.keys()); // All stored keys
46
+ console.log(inspect.totalBytes()); // Total bytes stored
47
+ inspect.clear(); // Clear all data
48
+ ```
49
+
50
+ ### With Size Limit
51
+
52
+ ```typescript
53
+ import { createMemoryStorage } from '@casfa/storage-memory';
54
+
55
+ const storage = createMemoryStorage({
56
+ maxSize: 100 * 1024 * 1024, // 100MB limit
57
+ });
58
+ ```
59
+
60
+ ## Configuration
61
+
62
+ ```typescript
63
+ interface MemoryStorageConfig {
64
+ // Optional: Maximum storage size in bytes
65
+ // When exceeded, oldest items may be evicted
66
+ maxSize?: number;
67
+
68
+ // Optional: Maximum number of items
69
+ maxItems?: number;
70
+ }
71
+ ```
72
+
73
+ ## API Reference
74
+
75
+ ### Functions
76
+
77
+ - `createMemoryStorage(config?)` - Create basic memory storage
78
+ - `createMemoryStorageWithInspection(config?)` - Create storage with inspection utilities
79
+
80
+ ### StorageProvider Interface
81
+
82
+ ```typescript
83
+ interface StorageProvider {
84
+ get(key: string): Promise<Uint8Array | null>;
85
+ put(key: string, data: Uint8Array): Promise<void>;
86
+ }
87
+ ```
88
+
89
+ ### Inspection Interface
90
+
91
+ ```typescript
92
+ interface StorageInspection {
93
+ size(): number; // Number of items
94
+ keys(): string[]; // All keys
95
+ totalBytes(): number; // Total storage used
96
+ clear(): void; // Clear all data
97
+ }
98
+ ```
99
+
100
+ ## Use Cases
101
+
102
+ - **Unit Testing**: Fast, isolated storage for tests
103
+ - **Integration Testing**: Predictable storage behavior
104
+ - **Development**: Quick iteration without file system overhead
105
+ - **Caching Layer**: Combine with persistent storage
106
+
107
+ ## Example: Testing
108
+
109
+ ```typescript
110
+ import { describe, it, expect, beforeEach } from 'bun:test';
111
+ import { createMemoryStorageWithInspection } from '@casfa/storage-memory';
112
+
113
+ describe('MyService', () => {
114
+ let storage: StorageProvider;
115
+ let inspect: StorageInspection;
116
+
117
+ beforeEach(() => {
118
+ const result = createMemoryStorageWithInspection();
119
+ storage = result.storage;
120
+ inspect = result.inspect;
121
+ });
122
+
123
+ it('should store data', async () => {
124
+ const service = new MyService(storage);
125
+ await service.saveData('test');
126
+
127
+ expect(inspect.size()).toBe(1);
128
+ });
129
+ });
130
+ ```
131
+
132
+ ## Related Packages
133
+
134
+ - `@casfa/storage-core` - Core types and utilities
135
+ - `@casfa/storage-fs` - File system storage (for production)
136
+ - `@casfa/storage-s3` - S3 storage (for cloud deployment)
137
+
138
+ ## License
139
+
140
+ MIT
@@ -0,0 +1,140 @@
1
+ # @casfa/storage-memory
2
+
3
+ 基于内存的 CAS 存储提供者。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ bun add @casfa/storage-memory
9
+ ```
10
+
11
+ ## 概述
12
+
13
+ 基于内存的 CAS(内容寻址存储)存储提供者。适用于测试、开发和临时存储场景。
14
+
15
+ > ⚠️ **警告**:数据不会持久化,进程退出后将丢失。仅用于测试和开发。
16
+
17
+ ## 使用方法
18
+
19
+ ### 基本用法
20
+
21
+ ```typescript
22
+ import { createMemoryStorage } from '@casfa/storage-memory';
23
+
24
+ const storage = createMemoryStorage();
25
+
26
+ // 存储数据
27
+ await storage.put('node:abcd1234...', data);
28
+
29
+ // 检索数据
30
+ const data = await storage.get('node:abcd1234...');
31
+ ```
32
+
33
+ ### 带检查功能(测试用)
34
+
35
+ ```typescript
36
+ import { createMemoryStorageWithInspection } from '@casfa/storage-memory';
37
+
38
+ const { storage, inspect } = createMemoryStorageWithInspection();
39
+
40
+ // 正常使用存储
41
+ await storage.put('node:abcd1234...', data);
42
+
43
+ // 检查内部状态(测试时很有用)
44
+ console.log(inspect.size()); // 条目数量
45
+ console.log(inspect.keys()); // 所有已存储的键
46
+ console.log(inspect.totalBytes()); // 已存储的总字节数
47
+ inspect.clear(); // 清除所有数据
48
+ ```
49
+
50
+ ### 设置大小限制
51
+
52
+ ```typescript
53
+ import { createMemoryStorage } from '@casfa/storage-memory';
54
+
55
+ const storage = createMemoryStorage({
56
+ maxSize: 100 * 1024 * 1024, // 100MB 限制
57
+ });
58
+ ```
59
+
60
+ ## 配置
61
+
62
+ ```typescript
63
+ interface MemoryStorageConfig {
64
+ // 可选:最大存储字节数
65
+ // 超出时可能淘汰最旧的条目
66
+ maxSize?: number;
67
+
68
+ // 可选:最大条目数
69
+ maxItems?: number;
70
+ }
71
+ ```
72
+
73
+ ## API 参考
74
+
75
+ ### 函数
76
+
77
+ - `createMemoryStorage(config?)` - 创建基本内存存储
78
+ - `createMemoryStorageWithInspection(config?)` - 创建带检查工具的存储
79
+
80
+ ### StorageProvider 接口
81
+
82
+ ```typescript
83
+ interface StorageProvider {
84
+ get(key: string): Promise<Uint8Array | null>;
85
+ put(key: string, data: Uint8Array): Promise<void>;
86
+ }
87
+ ```
88
+
89
+ ### Inspection 接口
90
+
91
+ ```typescript
92
+ interface StorageInspection {
93
+ size(): number; // 条目数量
94
+ keys(): string[]; // 所有键
95
+ totalBytes(): number; // 已使用的总存储量
96
+ clear(): void; // 清除所有数据
97
+ }
98
+ ```
99
+
100
+ ## 使用场景
101
+
102
+ - **单元测试**:快速、隔离的测试存储
103
+ - **集成测试**:可预测的存储行为
104
+ - **开发环境**:无需文件系统即可快速迭代
105
+ - **缓存层**:与持久化存储组合使用
106
+
107
+ ## 示例:测试
108
+
109
+ ```typescript
110
+ import { describe, it, expect, beforeEach } from 'bun:test';
111
+ import { createMemoryStorageWithInspection } from '@casfa/storage-memory';
112
+
113
+ describe('MyService', () => {
114
+ let storage: StorageProvider;
115
+ let inspect: StorageInspection;
116
+
117
+ beforeEach(() => {
118
+ const result = createMemoryStorageWithInspection();
119
+ storage = result.storage;
120
+ inspect = result.inspect;
121
+ });
122
+
123
+ it('should store data', async () => {
124
+ const service = new MyService(storage);
125
+ await service.saveData('test');
126
+
127
+ expect(inspect.size()).toBe(1);
128
+ });
129
+ });
130
+ ```
131
+
132
+ ## 相关包
133
+
134
+ - `@casfa/storage-core` - 核心类型与工具
135
+ - `@casfa/storage-fs` - 文件系统存储(用于生产环境)
136
+ - `@casfa/storage-s3` - S3 存储(用于云端部署)
137
+
138
+ ## 许可证
139
+
140
+ MIT
package/dist/index.d.ts CHANGED
@@ -1,39 +1,7 @@
1
- import { StorageProvider } from '@casfa/storage-core';
2
-
3
1
  /**
4
- * In-Memory Storage Provider for CAS
2
+ * CAS Storage Memory
5
3
  *
6
- * Useful for testing and local development.
4
+ * In-memory storage provider for CAS (testing and development).
7
5
  */
8
-
9
- /**
10
- * Memory Storage configuration
11
- */
12
- type MemoryStorageConfig = {
13
- /** Optional initial data */
14
- initialData?: Map<string, Uint8Array>;
15
- };
16
- /**
17
- * Create an in-memory storage provider
18
- */
19
- declare const createMemoryStorage: (config?: MemoryStorageConfig) => StorageProvider;
20
- /**
21
- * Create memory storage with inspection methods (for testing)
22
- */
23
- declare const createMemoryStorageWithInspection: (config?: MemoryStorageConfig) => {
24
- /** Clear all stored data */
25
- clear: () => void;
26
- /** Get number of stored items */
27
- size: () => number;
28
- /** Get all stored keys */
29
- keys: () => string[];
30
- /** Delete a specific key */
31
- delete: (key: string) => boolean;
32
- /** Get raw data map (for inspection) */
33
- getData: () => Map<string, Uint8Array<ArrayBufferLike>>;
34
- has: (key: string) => Promise<boolean>;
35
- get: (key: string) => Promise<Uint8Array | null>;
36
- put: (key: string, value: Uint8Array) => Promise<void>;
37
- };
38
-
39
- export { type MemoryStorageConfig, createMemoryStorage, createMemoryStorageWithInspection };
6
+ export { createMemoryStorage, createMemoryStorageWithInspection, type MemoryStorageConfig, } from "./memory-storage.ts";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,mBAAmB,EACnB,iCAAiC,EACjC,KAAK,mBAAmB,GACzB,MAAM,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -1,8 +1,7 @@
1
1
  // src/memory-storage.ts
2
2
  var createMemoryStorage = (config = {}) => {
3
- const data = config.initialData ?? /* @__PURE__ */ new Map();
3
+ const data = config.initialData ?? new Map;
4
4
  return {
5
- has: async (key) => data.has(key),
6
5
  get: async (key) => data.get(key) ?? null,
7
6
  put: async (key, value) => {
8
7
  data.set(key, value);
@@ -10,9 +9,8 @@ var createMemoryStorage = (config = {}) => {
10
9
  };
11
10
  };
12
11
  var createMemoryStorageWithInspection = (config = {}) => {
13
- const data = config.initialData ?? /* @__PURE__ */ new Map();
12
+ const data = config.initialData ?? new Map;
14
13
  const storage = {
15
- has: async (key) => data.has(key),
16
14
  get: async (key) => data.get(key) ?? null,
17
15
  put: async (key, value) => {
18
16
  data.set(key, value);
@@ -20,20 +18,16 @@ var createMemoryStorageWithInspection = (config = {}) => {
20
18
  };
21
19
  return {
22
20
  ...storage,
23
- /** Clear all stored data */
24
21
  clear: () => data.clear(),
25
- /** Get number of stored items */
26
22
  size: () => data.size,
27
- /** Get all stored keys */
28
23
  keys: () => Array.from(data.keys()),
29
- /** Delete a specific key */
30
24
  delete: (key) => data.delete(key),
31
- /** Get raw data map (for inspection) */
32
25
  getData: () => data
33
26
  };
34
27
  };
35
28
  export {
36
- createMemoryStorage,
37
- createMemoryStorageWithInspection
29
+ createMemoryStorageWithInspection,
30
+ createMemoryStorage
38
31
  };
39
- //# sourceMappingURL=index.js.map
32
+
33
+ //# debugId=64ED1D2374BB756B64756E2164756E21
package/dist/index.js.map CHANGED
@@ -1 +1,10 @@
1
- {"version":3,"sources":["../src/memory-storage.ts"],"sourcesContent":["/**\n * In-Memory Storage Provider for CAS\n *\n * Useful for testing and local development.\n */\n\nimport type { StorageProvider } from \"@casfa/storage-core\";\n\n/**\n * Memory Storage configuration\n */\nexport type MemoryStorageConfig = {\n /** Optional initial data */\n initialData?: Map<string, Uint8Array>;\n};\n\n/**\n * Create an in-memory storage provider\n */\nexport const createMemoryStorage = (config: MemoryStorageConfig = {}): StorageProvider => {\n const data = config.initialData ?? new Map<string, Uint8Array>();\n\n return {\n has: async (key) => data.has(key),\n get: async (key) => data.get(key) ?? null,\n put: async (key, value) => {\n data.set(key, value);\n },\n };\n};\n\n/**\n * Create memory storage with inspection methods (for testing)\n */\nexport const createMemoryStorageWithInspection = (config: MemoryStorageConfig = {}) => {\n const data = config.initialData ?? new Map<string, Uint8Array>();\n\n const storage: StorageProvider = {\n has: async (key) => data.has(key),\n get: async (key) => data.get(key) ?? null,\n put: async (key, value) => {\n data.set(key, value);\n },\n };\n\n return {\n ...storage,\n /** Clear all stored data */\n clear: () => data.clear(),\n /** Get number of stored items */\n size: () => data.size,\n /** Get all stored keys */\n keys: () => Array.from(data.keys()),\n /** Delete a specific key */\n delete: (key: string) => data.delete(key),\n /** Get raw data map (for inspection) */\n getData: () => data,\n };\n};\n"],"mappings":";AAmBO,IAAM,sBAAsB,CAAC,SAA8B,CAAC,MAAuB;AACxF,QAAM,OAAO,OAAO,eAAe,oBAAI,IAAwB;AAE/D,SAAO;AAAA,IACL,KAAK,OAAO,QAAQ,KAAK,IAAI,GAAG;AAAA,IAChC,KAAK,OAAO,QAAQ,KAAK,IAAI,GAAG,KAAK;AAAA,IACrC,KAAK,OAAO,KAAK,UAAU;AACzB,WAAK,IAAI,KAAK,KAAK;AAAA,IACrB;AAAA,EACF;AACF;AAKO,IAAM,oCAAoC,CAAC,SAA8B,CAAC,MAAM;AACrF,QAAM,OAAO,OAAO,eAAe,oBAAI,IAAwB;AAE/D,QAAM,UAA2B;AAAA,IAC/B,KAAK,OAAO,QAAQ,KAAK,IAAI,GAAG;AAAA,IAChC,KAAK,OAAO,QAAQ,KAAK,IAAI,GAAG,KAAK;AAAA,IACrC,KAAK,OAAO,KAAK,UAAU;AACzB,WAAK,IAAI,KAAK,KAAK;AAAA,IACrB;AAAA,EACF;AAEA,SAAO;AAAA,IACL,GAAG;AAAA;AAAA,IAEH,OAAO,MAAM,KAAK,MAAM;AAAA;AAAA,IAExB,MAAM,MAAM,KAAK;AAAA;AAAA,IAEjB,MAAM,MAAM,MAAM,KAAK,KAAK,KAAK,CAAC;AAAA;AAAA,IAElC,QAAQ,CAAC,QAAgB,KAAK,OAAO,GAAG;AAAA;AAAA,IAExC,SAAS,MAAM;AAAA,EACjB;AACF;","names":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/memory-storage.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * In-Memory Storage Provider for CAS\n *\n * Useful for testing and local development.\n */\n\nimport type { StorageProvider } from \"@casfa/storage-core\";\n\n/**\n * Memory Storage configuration\n */\nexport type MemoryStorageConfig = {\n /** Optional initial data */\n initialData?: Map<string, Uint8Array>;\n};\n\n/**\n * Create an in-memory storage provider\n */\nexport const createMemoryStorage = (config: MemoryStorageConfig = {}): StorageProvider => {\n const data = config.initialData ?? new Map<string, Uint8Array>();\n\n return {\n get: async (key) => data.get(key) ?? null,\n put: async (key, value) => {\n data.set(key, value);\n },\n };\n};\n\n/**\n * Create memory storage with inspection methods (for testing)\n */\nexport const createMemoryStorageWithInspection = (config: MemoryStorageConfig = {}) => {\n const data = config.initialData ?? new Map<string, Uint8Array>();\n\n const storage: StorageProvider = {\n get: async (key) => data.get(key) ?? null,\n put: async (key, value) => {\n data.set(key, value);\n },\n };\n\n return {\n ...storage,\n /** Clear all stored data */\n clear: () => data.clear(),\n /** Get number of stored items */\n size: () => data.size,\n /** Get all stored keys */\n keys: () => Array.from(data.keys()),\n /** Delete a specific key */\n delete: (key: string) => data.delete(key),\n /** Get raw data map (for inspection) */\n getData: () => data,\n };\n};\n"
6
+ ],
7
+ "mappings": ";AAmBO,IAAM,sBAAsB,CAAC,SAA8B,CAAC,MAAuB;AAAA,EACxF,MAAM,OAAO,OAAO,eAAe,IAAI;AAAA,EAEvC,OAAO;AAAA,IACL,KAAK,OAAO,QAAQ,KAAK,IAAI,GAAG,KAAK;AAAA,IACrC,KAAK,OAAO,KAAK,UAAU;AAAA,MACzB,KAAK,IAAI,KAAK,KAAK;AAAA;AAAA,EAEvB;AAAA;AAMK,IAAM,oCAAoC,CAAC,SAA8B,CAAC,MAAM;AAAA,EACrF,MAAM,OAAO,OAAO,eAAe,IAAI;AAAA,EAEvC,MAAM,UAA2B;AAAA,IAC/B,KAAK,OAAO,QAAQ,KAAK,IAAI,GAAG,KAAK;AAAA,IACrC,KAAK,OAAO,KAAK,UAAU;AAAA,MACzB,KAAK,IAAI,KAAK,KAAK;AAAA;AAAA,EAEvB;AAAA,EAEA,OAAO;AAAA,OACF;AAAA,IAEH,OAAO,MAAM,KAAK,MAAM;AAAA,IAExB,MAAM,MAAM,KAAK;AAAA,IAEjB,MAAM,MAAM,MAAM,KAAK,KAAK,KAAK,CAAC;AAAA,IAElC,QAAQ,CAAC,QAAgB,KAAK,OAAO,GAAG;AAAA,IAExC,SAAS,MAAM;AAAA,EACjB;AAAA;",
8
+ "debugId": "64ED1D2374BB756B64756E2164756E21",
9
+ "names": []
10
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * In-Memory Storage Provider for CAS
3
+ *
4
+ * Useful for testing and local development.
5
+ */
6
+ import type { StorageProvider } from "@casfa/storage-core";
7
+ /**
8
+ * Memory Storage configuration
9
+ */
10
+ export type MemoryStorageConfig = {
11
+ /** Optional initial data */
12
+ initialData?: Map<string, Uint8Array>;
13
+ };
14
+ /**
15
+ * Create an in-memory storage provider
16
+ */
17
+ export declare const createMemoryStorage: (config?: MemoryStorageConfig) => StorageProvider;
18
+ /**
19
+ * Create memory storage with inspection methods (for testing)
20
+ */
21
+ export declare const createMemoryStorageWithInspection: (config?: MemoryStorageConfig) => {
22
+ /** Clear all stored data */
23
+ clear: () => void;
24
+ /** Get number of stored items */
25
+ size: () => number;
26
+ /** Get all stored keys */
27
+ keys: () => string[];
28
+ /** Delete a specific key */
29
+ delete: (key: string) => boolean;
30
+ /** Get raw data map (for inspection) */
31
+ getData: () => Map<string, Uint8Array<ArrayBufferLike>>;
32
+ get: (key: string) => Promise<Uint8Array | null>;
33
+ put: (key: string, value: Uint8Array) => Promise<void>;
34
+ };
35
+ //# sourceMappingURL=memory-storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory-storage.d.ts","sourceRoot":"","sources":["../src/memory-storage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3D;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,4BAA4B;IAC5B,WAAW,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACvC,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,SAAQ,mBAAwB,KAAG,eAStE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iCAAiC,GAAI,SAAQ,mBAAwB;IAY9E,4BAA4B;;IAE5B,iCAAiC;;IAEjC,0BAA0B;;IAE1B,4BAA4B;kBACd,MAAM;IACpB,wCAAwC;;;;CAG3C,CAAC"}
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@casfa/storage-memory",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "In-memory storage provider for CAS (testing)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
+ "bun": "./src/index.ts",
10
11
  "types": "./dist/index.d.ts",
11
12
  "import": "./dist/index.js"
12
13
  }
13
14
  },
14
15
  "scripts": {
15
- "build": "tsup",
16
- "dev": "tsup --watch",
16
+ "build": "bun ../../scripts/build-pkg.ts",
17
17
  "typecheck": "tsc --noEmit",
18
18
  "lint": "biome check .",
19
19
  "lint:fix": "biome check --write .",