duna-oss / flystorage

Flystorage; File storage abstraction for Node / TypeScript
https://flystorage.dev/
195 stars 14 forks source link

Make it work on Cloudflare Workers #82

Open svelterust opened 3 days ago

svelterust commented 3 days ago

I'm getting this issue when using the library. I dynamically import S3 in production because Cloudflare Workers don't support local filesystem:

import { env } from "$env/dynamic/private";
import { FileStorage } from "@flystorage/file-storage";

async function createAdapter() {
  if (import.meta.env.DEV) {
    const { LocalStorageAdapter } = await import("@flystorage/local-fs");

    // While developing, use local filesystem
    const location = `${process.cwd()}/files`;
    return new LocalStorageAdapter(location, {
      publicUrlOptions: {
        baseUrl: "/files",
      },
    });
  } else {
    const { S3Client } = await import("@aws-sdk/client-s3");
    const { AwsS3StorageAdapter } = await import("@flystorage/aws-s3");

    // In production, use S3
    const client = new S3Client({
      region: env.S3_REGION ?? "auto",
      endpoint: env.S3_URL,
      credentials: {
        accessKeyId: env.S3_ID,
        secretAccessKey: env.S3_KEY,
      },
    });
    return new AwsS3StorageAdapter(client, {
      bucket: env.S3_BUCKET,
    });
  }
}

// Create storage with adapter
const adapter = await createAdapter();
export const storage = new FileStorage(adapter);
(error) Error: Dynamic require of "node:path" is not supported
svelterust commented 3 days ago

I have node_compat_v2 enabled as well, which should support node:path: https://developers.cloudflare.com/workers/runtime-apis/nodejs/path/