JA
ベータ版の翻訳

@core / storage

1.0.1 ▾
認証済みMIT
GitHub

S3 互換バケット(R2、S3、MinIO)とローカルディスクのファイル保存、署名付き直接アップロード対応

コード11 ファイルコンテキスト約 697 トークンスキャン合格

.genpmignore 適用後に組み込まれる正確なツリーです。固定先:

src/lib/storage/provider.ts読み取り専用 · 5b6a9cc
// Interfaz común de los drivers (S3 compatible, local, memoria).

export type PutBody = Uint8Array | ArrayBuffer | string | Blob;

export type PutOptions = { contentType: string; cacheControl?: string };

export type StoredObject = { key: string; size: number; contentType: string; body: ReadableStream<Uint8Array> };

export type ObjectInfo = { key: string; size: number; contentType: string };

/** Subida directa desde el navegador: `fetch(url, { method, headers, body: file })`. */
export type PresignedUpload = { url: string; method: 'PUT'; headers: Record<string, string>; expiresAt: Date };

export type PresignUploadOptions = {
  contentType: string;
  /** Tamaño exacto declarado por el cliente; va firmado, así que no puede subir otra cosa. */
  size: number;
  /** Límite superior; si `size` lo supera se rechaza antes de firmar. */
  maxBytes: number;
  expiresIn?: number;
};

export interface StorageProvider {
  readonly driver: 's3' | 'local' | 'memory';
  put(key: string, body: PutBody, opts: PutOptions): Promise<ObjectInfo>;
  get(key: string): Promise<StoredObject | null>;
  head(key: string): Promise<ObjectInfo | null>;
  delete(key: string): Promise<void>;
  presignUpload(key: string, opts: PresignUploadOptions): Promise<PresignedUpload>;
  /** URL temporal de descarga para objetos privados. */
  presignDownload(key: string, expiresIn?: number): Promise<string>;
  /** URL pública estable; solo para claves bajo `public/`. */
  publicUrl(key: string): string;
}

export const toBytes = async (body: PutBody): Promise<Uint8Array> =>
  typeof body === 'string'
    ? new TextEncoder().encode(body)
    : body instanceof Uint8Array
      ? body
      : body instanceof ArrayBuffer
        ? new Uint8Array(body)
        : new Uint8Array(await body.arrayBuffer());

export const streamOf = (bytes: Uint8Array): ReadableStream<Uint8Array> =>
  new ReadableStream({
    start(c) {
      c.enqueue(bytes);
      c.close();
    },
  });

@core/storage を報告

パッケージを報告するには GitHub でログインしてください。

GitHub で続ける