@beignet/provider-storage-local

中文文档
0.0.65发布于 2026/11/08MITTypeScriptES Module近 30 天下载 1734

Local filesystem storage provider for Beignet

原始使用文档
文档目录10 节
  1. Install
  2. Provider setup
  3. Direct port factory
  4. Private downloads
  5. Files on disk
  6. Devtools
  7. Failure behavior
  8. Local and tests
  9. Deployment notes
  10. License

@beignet/provider-storage-local

Runtime: Beignet requires Node.js 22.12 or newer. Bun is optional.

[!CAUTION] Beignet is experimental alpha software. The 0.0.x package line is for early evaluation, and APIs may change between releases while the framework settles.

Local filesystem storage provider for Beignet.

The provider installs the app-facing ctx.ports.storage port. Use it for local development, tests that need durable files, and small deployments where a filesystem-backed object store is enough.

createLocalStorageProvider(...) returns the stable LocalStorageProvider type. LocalStorageConfig describes its validated config; the Zod schema remains internal.

Install

bun add @beignet/provider-storage-local @beignet/core

Provider setup

import { createLocalStorageProvider } from "@beignet/provider-storage-local";
import { createServer } from "@beignet/core/server";

const server = await createServer({
  ports: basePorts,
  providers: [createLocalStorageProvider()],
  context: ({ ports }) => ({ ports }),
  routes,
});

Environment variables:

VariableDescription
STORAGE_ROOTDirectory where objects are written. Defaults to storage/app.
STORAGE_PUBLIC_BASE_URLOptional base URL returned by publicUrl(...) for public objects.

beignet doctor --strict checks that installed local storage providers are registered in server/providers.ts.

STORAGE_PUBLIC_BASE_URL may be an absolute URL such as https://assets.example.com or an app-relative path such as /storage. The provider only returns URLs. If you use an app-relative path in a Next.js app, serve public objects with createStorageRoute:

// app/storage/[...key]/route.ts
import { createStorageRoute } from "@beignet/next";
import { getServer } from "@/server";

export const { GET, HEAD } = createStorageRoute(
  async () => (await getServer()).ports.storage,
  {
    basePath: "/storage",
  },
);

Direct port factory

import { createLocalStorage } from "@beignet/provider-storage-local";

const storage = createLocalStorage({
  root: "storage/app",
  publicBaseUrl: "/storage",
});

The provider contributes ctx.ports.storage, the standard Beignet StoragePort. It has no provider-specific escape hatch; use the port for reads, writes, public URLs, and metadata.

The same StoragePort works with local files, memory tests, and cloud object stores:

await ctx.ports.storage.put("avatars/user_123.png", avatarBytes, {
  contentType: "image/png",
  visibility: "public",
});

const object = await ctx.ports.storage.get("avatars/user_123.png");
const url = await ctx.ports.storage.publicUrl("avatars/user_123.png");

Private downloads

Store private files with visibility: "private". In an authenticated app route, resolve and authorize the file record, then call createStorageDownloadResponse({ storage, key, fileName }) from @beignet/web or @beignet/next. It streams a non-cacheable attachment and returns null when the object is missing, so the route can use its app error catalog.

storage.temporaryUrl(key, { expiresInSeconds, fileName? }) validates its inputs and returns { status: "unsupported" } without checking existence. The local provider does not issue expiring links or make private files public. Keep the storage root outside any static-file directory; the public storage route only serves objects explicitly marked public.

Files on disk

Objects are written below root using their storage key. Object metadata is stored in a .beignet-storage-meta sidecar directory below the same root.

Storage keys use the shared @beignet/core/ports contract: no empty strings, control characters, empty path segments, leading or trailing /, backslashes, or . / .. path segments. The .beignet-storage-meta path segment is an additional local-provider restriction because it stores sidecar metadata.

Devtools

When ctx.ports.devtools is installed, the provider records storage operations under the storage watcher. Events include operation name, key, duration, object size, visibility, and whether a lookup hit. Object bodies are never recorded.

Failure behavior

The provider creates directories as needed and throws for invalid object keys, metadata errors, filesystem permission failures, and failed reads or writes. Path traversal is rejected before touching the filesystem. Mutation commits targeting the same key and root are serialized in invocation order within one Node.js process, including mutations issued through separate local-storage instances. put(...) stages its input stream at the front of that key's mutation queue, outside the short commit lock, so the stream may read the previous version of the same key without deadlocking and queued bodies do not all consume temporary disk space at once. get(...) returns a one-shot snapshot: its metadata and body remain on the same version even when another operation overwrites the key before consumption. Consume the body or call await object.cancel() to close an unread snapshot deterministically. Use stat(...) for metadata-only reads.

Local and tests

Use this provider for local development or integration tests that need durable files. Use an app-owned fake or memory StoragePort for use-case tests that only assert storage intent.

Deployment notes

Only use local storage in deployments where the filesystem is durable and shared with every process that needs the objects. Serverless and horizontally scaled apps should usually use @beignet/provider-storage-s3 or another object store. The provider does not coordinate same-key writes across processes or hosts.

License

MIT

版本与发布记录

@beignet/provider-storage-local 共发布 65 个版本,首次发布于 2026/05/27;过去 12 个月发布了 65 个版本。

版本发布日期距今
0.0.65latest2026/11/081 天前
0.0.642026/11/036 天前
0.0.632026/11/028 天前
0.0.622026/11/018 天前
0.0.612026/11/018 天前
0.0.602026/11/018 天前

关于 @beignet/provider-storage-local 的常见问题

@beignet/provider-storage-local 是什么?

Local filesystem storage provider for Beignet

如何安装 @beignet/provider-storage-local?

在项目目录运行 npm install @beignet/provider-storage-local,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@beignet/core(>=0.0.3 <1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@beignet/provider-storage-local 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@beignet/provider-storage-local 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@beignet/provider-storage-local 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@beignet/provider-storage-local 可以商用吗?

@beignet/provider-storage-local 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@beignet/provider-storage-local 的下载量有多少?

根据 npm 官方下载统计,@beignet/provider-storage-local 在 2026/10/08 – 2026/11/06 这 30 天内被下载 1,734 次,日均约 58 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@beignet/provider-storage-local 还在维护吗?

最新版本 0.0.65 发布于 2026/11/08(1 天前);至今共发布 65 个版本,其中过去 12 个月发布了 65 个;首次发布于 2026/05/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。