@casfa/storage-memory 0.1.0 → 0.2.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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/README.md +0 -148
  3. package/README.zh-CN.md +0 -148
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@casfa/storage-memory",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "In-memory storage provider for CAS (testing)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/README.md DELETED
@@ -1,148 +0,0 @@
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
- // Check existence
33
- const exists = await storage.has('node:abcd1234...');
34
-
35
- // Delete
36
- const deleted = await storage.delete('node:abcd1234...');
37
- ```
38
-
39
- ### With Inspection (Testing)
40
-
41
- ```typescript
42
- import { createMemoryStorageWithInspection } from '@casfa/storage-memory';
43
-
44
- const { storage, inspect } = createMemoryStorageWithInspection();
45
-
46
- // Use storage normally
47
- await storage.put('node:abcd1234...', data);
48
-
49
- // Inspect internal state (useful for tests)
50
- console.log(inspect.size()); // Number of items
51
- console.log(inspect.keys()); // All stored keys
52
- console.log(inspect.totalBytes()); // Total bytes stored
53
- inspect.clear(); // Clear all data
54
- ```
55
-
56
- ### With Size Limit
57
-
58
- ```typescript
59
- import { createMemoryStorage } from '@casfa/storage-memory';
60
-
61
- const storage = createMemoryStorage({
62
- maxSize: 100 * 1024 * 1024, // 100MB limit
63
- });
64
- ```
65
-
66
- ## Configuration
67
-
68
- ```typescript
69
- interface MemoryStorageConfig {
70
- // Optional: Maximum storage size in bytes
71
- // When exceeded, oldest items may be evicted
72
- maxSize?: number;
73
-
74
- // Optional: Maximum number of items
75
- maxItems?: number;
76
- }
77
- ```
78
-
79
- ## API Reference
80
-
81
- ### Functions
82
-
83
- - `createMemoryStorage(config?)` - Create basic memory storage
84
- - `createMemoryStorageWithInspection(config?)` - Create storage with inspection utilities
85
-
86
- ### StorageProvider Interface
87
-
88
- ```typescript
89
- interface StorageProvider {
90
- get(key: string): Promise<Uint8Array | null>;
91
- put(key: string, data: Uint8Array): Promise<void>;
92
- has(key: string): Promise<boolean>;
93
- delete(key: string): Promise<boolean>;
94
- }
95
- ```
96
-
97
- ### Inspection Interface
98
-
99
- ```typescript
100
- interface StorageInspection {
101
- size(): number; // Number of items
102
- keys(): string[]; // All keys
103
- totalBytes(): number; // Total storage used
104
- clear(): void; // Clear all data
105
- }
106
- ```
107
-
108
- ## Use Cases
109
-
110
- - **Unit Testing**: Fast, isolated storage for tests
111
- - **Integration Testing**: Predictable storage behavior
112
- - **Development**: Quick iteration without file system overhead
113
- - **Caching Layer**: Combine with persistent storage
114
-
115
- ## Example: Testing
116
-
117
- ```typescript
118
- import { describe, it, expect, beforeEach } from 'bun:test';
119
- import { createMemoryStorageWithInspection } from '@casfa/storage-memory';
120
-
121
- describe('MyService', () => {
122
- let storage: StorageProvider;
123
- let inspect: StorageInspection;
124
-
125
- beforeEach(() => {
126
- const result = createMemoryStorageWithInspection();
127
- storage = result.storage;
128
- inspect = result.inspect;
129
- });
130
-
131
- it('should store data', async () => {
132
- const service = new MyService(storage);
133
- await service.saveData('test');
134
-
135
- expect(inspect.size()).toBe(1);
136
- });
137
- });
138
- ```
139
-
140
- ## Related Packages
141
-
142
- - `@casfa/storage-core` - Core types and utilities
143
- - `@casfa/storage-fs` - File system storage (for production)
144
- - `@casfa/storage-s3` - S3 storage (for cloud deployment)
145
-
146
- ## License
147
-
148
- MIT
package/README.zh-CN.md DELETED
@@ -1,148 +0,0 @@
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
- const exists = await storage.has('node:abcd1234...');
34
-
35
- // 删除
36
- const deleted = await storage.delete('node:abcd1234...');
37
- ```
38
-
39
- ### 带检查功能(测试用)
40
-
41
- ```typescript
42
- import { createMemoryStorageWithInspection } from '@casfa/storage-memory';
43
-
44
- const { storage, inspect } = createMemoryStorageWithInspection();
45
-
46
- // 正常使用存储
47
- await storage.put('node:abcd1234...', data);
48
-
49
- // 检查内部状态(测试时很有用)
50
- console.log(inspect.size()); // 条目数量
51
- console.log(inspect.keys()); // 所有已存储的键
52
- console.log(inspect.totalBytes()); // 已存储的总字节数
53
- inspect.clear(); // 清除所有数据
54
- ```
55
-
56
- ### 设置大小限制
57
-
58
- ```typescript
59
- import { createMemoryStorage } from '@casfa/storage-memory';
60
-
61
- const storage = createMemoryStorage({
62
- maxSize: 100 * 1024 * 1024, // 100MB 限制
63
- });
64
- ```
65
-
66
- ## 配置
67
-
68
- ```typescript
69
- interface MemoryStorageConfig {
70
- // 可选:最大存储字节数
71
- // 超出时可能淘汰最旧的条目
72
- maxSize?: number;
73
-
74
- // 可选:最大条目数
75
- maxItems?: number;
76
- }
77
- ```
78
-
79
- ## API 参考
80
-
81
- ### 函数
82
-
83
- - `createMemoryStorage(config?)` - 创建基本内存存储
84
- - `createMemoryStorageWithInspection(config?)` - 创建带检查工具的存储
85
-
86
- ### StorageProvider 接口
87
-
88
- ```typescript
89
- interface StorageProvider {
90
- get(key: string): Promise<Uint8Array | null>;
91
- put(key: string, data: Uint8Array): Promise<void>;
92
- has(key: string): Promise<boolean>;
93
- delete(key: string): Promise<boolean>;
94
- }
95
- ```
96
-
97
- ### Inspection 接口
98
-
99
- ```typescript
100
- interface StorageInspection {
101
- size(): number; // 条目数量
102
- keys(): string[]; // 所有键
103
- totalBytes(): number; // 已使用的总存储量
104
- clear(): void; // 清除所有数据
105
- }
106
- ```
107
-
108
- ## 使用场景
109
-
110
- - **单元测试**:快速、隔离的测试存储
111
- - **集成测试**:可预测的存储行为
112
- - **开发环境**:无需文件系统即可快速迭代
113
- - **缓存层**:与持久化存储组合使用
114
-
115
- ## 示例:测试
116
-
117
- ```typescript
118
- import { describe, it, expect, beforeEach } from 'bun:test';
119
- import { createMemoryStorageWithInspection } from '@casfa/storage-memory';
120
-
121
- describe('MyService', () => {
122
- let storage: StorageProvider;
123
- let inspect: StorageInspection;
124
-
125
- beforeEach(() => {
126
- const result = createMemoryStorageWithInspection();
127
- storage = result.storage;
128
- inspect = result.inspect;
129
- });
130
-
131
- it('should store data', async () => {
132
- const service = new MyService(storage);
133
- await service.saveData('test');
134
-
135
- expect(inspect.size()).toBe(1);
136
- });
137
- });
138
- ```
139
-
140
- ## 相关包
141
-
142
- - `@casfa/storage-core` - 核心类型与工具
143
- - `@casfa/storage-fs` - 文件系统存储(用于生产环境)
144
- - `@casfa/storage-s3` - S3 存储(用于云端部署)
145
-
146
- ## 许可证
147
-
148
- MIT