建议按以下顺序阅读代码,以理解项目结构和核心功能:
- 核心类型与接口:
src/types.ts- 定义项目中使用到的主要类型和接口。
- 核心存储逻辑:
src/storage.ts- 实现核心的Storage类,包含挂载驱动、基本 K/V 操作等。
- 通用工具函数:
src/utils.ts- 提供各种辅助函数,如序列化、路径处理等。src/_utils.ts- 内部使用的工具函数。
- 入口与导出:
src/index.ts- 项目的主入口文件,导出公共 API。
- 驱动实现:
src/drivers/utils/index.ts- 驱动相关的工具函数。src/_drivers.ts- 驱动相关的基础或辅助代码。src/drivers/memory.ts- 了解一个简单的内存驱动实现。src/drivers/fs.ts- 文件系统驱动,了解如何与外部系统交互。src/drivers/overlay.ts- 叠加驱动,理解驱动组合。- (浏览
src/drivers/目录下的其他驱动以了解特定实现)*
- HTTP 服务器:
src/server.ts- HTTP 存储服务器的实现。
- 命令行工具:
src/cli.ts- 命令行接口实现。
- 构建与配置:
package.json- 项目依赖和脚本。build.config.ts- 构建配置文件。tsconfig.json- TypeScript 配置文件。vite.config.mjs- Vite 构建配置。eslint.config.mjs- ESLint 配置。
- 测试:
test/storage.test.ts- 核心存储接口测试。test/drivers/utils.ts- 测试工具函数。- 驱动测试 (Drivers Tests):
test/drivers/azure-app-configuration.test.tstest/drivers/azure-cosmos.test.tstest/drivers/azure-key-vault.test.tstest/drivers/azure-storage-blob.test.tstest/drivers/azure-storage-table.test.tstest/drivers/capacitor-preferences.test.tstest/drivers/cloudflare-kv-binding.test.tstest/drivers/cloudflare-kv-http.test.tstest/drivers/cloudflare-r2-binding.test.tstest/drivers/db0.test.tstest/drivers/deno-kv-node.test.tstest/drivers/deno-kv.test.tstest/drivers/fs-lite.test.tstest/drivers/fs.test.tstest/drivers/github.test.tstest/drivers/http.test.tstest/drivers/indexedb.test.tstest/drivers/localstorage.test.tstest/drivers/lru-cache.test.tstest/drivers/memory.test.tstest/drivers/mongodb.test.tstest/drivers/netlify-blobs.test.tstest/drivers/null.test.tstest/drivers/overlay.test.tstest/drivers/redis.test.tstest/drivers/s3.test.tstest/drivers/session-storage.test.tstest/drivers/uploadthing.test.tstest/drivers/upstash.test.tstest/drivers/vercel-blob.test.tstest/drivers/vercel-kv.test.ts
Unstorage provides an async Key-Value storage API with conventional features like multi driver mounting, watching and working with metadata, dozens of built-in drivers and a tiny core.
- Designed for all environments: Browser, NodeJS, and Workers
- Lots of Built-in drivers
- Asynchronous API
- Unix-style driver mounting to combine storages
- Default in-memory storage
- Tree-shakable utils and tiny core
- Auto JSON value serialization and deserialization
- Binary and raw value support
- State snapshots and hydration
- Storage watcher
- HTTP Storage with built-in server
Install unstorage npm package:
# yarn
yarn add unstorage
# npm
npm install unstorage
# pnpm
pnpm add unstorageimport { createStorage } from "unstorage";
const storage = createStorage(/* opts */);
await storage.getItem("foo:bar"); // or storage.getItem('/foo/bar')👉 Check out the the documentation for usage information.
You can use the nightly release channel to try the latest changes in the main branch via unstorage-nightly.
If directly using unstorage in your project:
{
"devDependencies": {
"unstorage": "npm:unstorage-nightly"
}
}If using unstorage via another tool in your project:
{
"resolutions": {
"unstorage": "npm:unstorage-nightly"
}
}- Clone repository
- Install dependencies with
pnpm install - Use
pnpm devto start jest watcher verifying changes - Use
pnpm testbefore pushing to ensure all tests and lint checks passing