@vultro/storage-r2 0.0.0-stage → 0.0.2

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.
@@ -0,0 +1 @@
1
+ export { createR2BlobStore, createR2Storage, r2Storage, type CreateR2StorageOptions, type R2BucketLike, type R2ObjectBodyLike, type R2StorageOptions, } from "./r2.js";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { createR2BlobStore, createR2Storage, r2Storage } from "./r2.js";
package/dist/r2.d.ts ADDED
@@ -0,0 +1,72 @@
1
+ import { type BlobStore, type StorageBackend, type StorageFactory } from "@vultro/core/storage";
2
+ /**
3
+ * ============================================================================
4
+ * Cloudflare R2 存储后端(`@vultro/storage-r2`)
5
+ * ============================================================================
6
+ *
7
+ * 只负责文件**字节**:把 R2 bucket 包装成 `BlobStore`,再交给
8
+ * `createStorageFromContext` 与 DO SQLite / D1 的元数据组合。
9
+ *
10
+ * 配置里可以写:
11
+ *
12
+ * ```ts
13
+ * import { r2Storage } from "@vultro/storage-r2";
14
+ * export default defineConfig({ cloudflare: { storage: r2Storage() } });
15
+ * ```
16
+ *
17
+ * 运行时绑定名默认 `VULTRO_R2`,前缀默认读 `VULTRO_R2_PREFIX`;bucket 不存在
18
+ * 时回退到元数据表里的 base64 副本(本地开发零配置)。
19
+ *
20
+ * 约定:**key 不含前缀**。`StorageService` 只产出 `objects/<id>` 这样的逻辑
21
+ * key,前缀由本适配器统一加。
22
+ */
23
+ /**
24
+ * R2 bucket 的最小结构化类型。
25
+ *
26
+ * 只依赖对象存储真正需要的 put/get/delete/head,不引入 workers-types,
27
+ * 这样测试可以用内存 fake,生产直接传 `env.VULTRO_R2`。
28
+ */
29
+ export interface R2ObjectBodyLike {
30
+ arrayBuffer(): Promise<ArrayBuffer>;
31
+ httpMetadata?: {
32
+ contentType?: string;
33
+ };
34
+ size?: number;
35
+ }
36
+ export interface R2BucketLike {
37
+ put(key: string, value: ArrayBuffer | Uint8Array | Blob | string, options?: {
38
+ httpMetadata?: {
39
+ contentType?: string;
40
+ };
41
+ }): Promise<unknown>;
42
+ get(key: string): Promise<R2ObjectBodyLike | null>;
43
+ delete(key: string): Promise<void>;
44
+ head?(key: string): Promise<unknown>;
45
+ }
46
+ export interface R2StorageOptions {
47
+ bucket: R2BucketLike;
48
+ /** 对象 key 前缀,便于同一 bucket 放多个 deployment。 */
49
+ prefix?: string;
50
+ }
51
+ /** 创建 R2 Storage 工厂的选项。 */
52
+ export interface CreateR2StorageOptions {
53
+ /** 直接传入 bucket;生成入口 / 运行时默认值从 env 读取时不需要。 */
54
+ bucket?: R2BucketLike;
55
+ /** Worker binding 名;默认 `VULTRO_R2`。 */
56
+ binding?: string;
57
+ /** 对象 key 前缀;不传时读 `VULTRO_R2_PREFIX`。 */
58
+ prefix?: string;
59
+ /** 读取前缀的环境变量名;默认 `VULTRO_R2_PREFIX`。 */
60
+ prefixEnv?: string;
61
+ }
62
+ /**
63
+ * 创建 R2 Storage 工厂。
64
+ *
65
+ * `ctx.env` 里没有 bucket 时不报错,而是回退到 SQLite 的 base64 副本,
66
+ * 这样本地 `vite dev` 不配置 R2 也能上传 / 下载文件。
67
+ */
68
+ export declare function createR2Storage(options?: CreateR2StorageOptions): StorageFactory;
69
+ /** 配置层选择器:`cloudflare.storage: r2Storage()`。 */
70
+ export declare function r2Storage(options?: CreateR2StorageOptions): StorageBackend;
71
+ /** 把 R2 bucket 包装成 `BlobStore`;前缀在这里统一加上。 */
72
+ export declare function createR2BlobStore(options: R2StorageOptions): BlobStore;
package/dist/r2.js ADDED
@@ -0,0 +1,72 @@
1
+ import { createStorageFromContext } from "@vultro/core/storage";
2
+ // src/r2.ts
3
+ /**
4
+ * 创建 R2 Storage 工厂。
5
+ *
6
+ * `ctx.env` 里没有 bucket 时不报错,而是回退到 SQLite 的 base64 副本,
7
+ * 这样本地 `vite dev` 不配置 R2 也能上传 / 下载文件。
8
+ */
9
+ function createR2Storage(options = {}) {
10
+ const binding = options.binding ?? "VULTRO_R2";
11
+ const prefixEnv = options.prefixEnv ?? "VULTRO_R2_PREFIX";
12
+ return (context) => {
13
+ const bucket = options.bucket ?? context.env[binding];
14
+ const envPrefix = context.env[prefixEnv];
15
+ const prefix = options.prefix ?? (typeof envPrefix === "string" ? envPrefix : void 0);
16
+ const blobs = bucket && typeof bucket === "object" ? createR2BlobStore({
17
+ bucket,
18
+ ...prefix ? { prefix } : {}
19
+ }) : void 0;
20
+ return createStorageFromContext(context, blobs);
21
+ };
22
+ }
23
+ /** 配置层选择器:`cloudflare.storage: r2Storage()`。 */
24
+ function r2Storage(options = {}) {
25
+ return {
26
+ name: "r2",
27
+ runtimes: ["cloudflare", "cloud"],
28
+ module: "@vultro/storage-r2",
29
+ exportName: "createR2Storage",
30
+ options: {
31
+ ...options.binding ? { binding: options.binding } : {},
32
+ ...options.prefix ? { prefix: options.prefix } : {},
33
+ ...options.prefixEnv ? { prefixEnv: options.prefixEnv } : {}
34
+ },
35
+ create: (runtime) => {
36
+ if (runtime !== "cloudflare" && runtime !== "cloud") throw new Error(`r2Storage only supports cloudflare/cloud, got `);
37
+ return createR2Storage(options);
38
+ }
39
+ };
40
+ }
41
+ /** 把 R2 bucket 包装成 `BlobStore`;前缀在这里统一加上。 */
42
+ function createR2BlobStore(options) {
43
+ const prefix = options.prefix?.replace(/^\/+|\/+$/g, "");
44
+ const keyOf = (key) => prefix ? `${prefix}/${key}` : key;
45
+ return {
46
+ kind: "r2",
47
+ async put(key, bytes, putOptions) {
48
+ await options.bucket.put(keyOf(key), bytes, { httpMetadata: { contentType: putOptions?.contentType ?? "application/octet-stream" } });
49
+ },
50
+ async get(key) {
51
+ const object = await options.bucket.get(keyOf(key));
52
+ if (!object) return null;
53
+ return {
54
+ bytes: new Uint8Array(await object.arrayBuffer()),
55
+ ...object.httpMetadata?.contentType ? { contentType: object.httpMetadata.contentType } : {}
56
+ };
57
+ },
58
+ async delete(key) {
59
+ await options.bucket.delete(keyOf(key));
60
+ },
61
+ async head(key) {
62
+ if (!options.bucket.head) return null;
63
+ const result = await options.bucket.head(keyOf(key));
64
+ if (!result) return null;
65
+ return {
66
+ size: Number(result.size ?? 0),
67
+ ...result.httpMetadata?.contentType ? { contentType: result.httpMetadata.contentType } : {}
68
+ };
69
+ }
70
+ };
71
+ }
72
+ export { createR2BlobStore, createR2Storage, r2Storage };
package/package.json CHANGED
@@ -1,6 +1,20 @@
1
1
  {
2
2
  "name": "@vultro/storage-r2",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.2",
4
+ "type": "module",
5
+ "description": "Cloudflare R2 file storage backend for Vultro",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "dependencies": {
14
+ "@vultro/core": "0.0.2"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "types": "./dist/index.d.ts"
20
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.