Skip to content

ReferenceError: File is not defined when uploading file using CLI #1

Description

@raa-solana

Bug Description

When trying to upload a file using the CLI tool, it throws a ReferenceError: File is not defined error. This is because the code tries to use the browser's File API which is not available in Node.js environment.

Steps to Reproduce

  1. Install the CLI tool
  2. Try to upload a file using the command:
shdw-drive upload \
  --keypair ~/.config/solana/id_v2.json \
  --bucket [bucket-id] \
  --file path/to/file.gif

Error Message

Error uploading file: ReferenceError: File is not defined
    at Command.<anonymous> (/Users/srm/dev/shdwdrive-v2-cli/dist/cli.js:41:22)
    ...

Current Implementation

In cli.ts, the code attempts to create a File object:

const fileBuffer = readFileSync(options.file);
const fileName = options.file.split('/').pop()!;
const file = new File([fileBuffer], fileName);

Suggested Fix

Consider using one of these approaches:

  1. Using form-data:
import FormData from 'form-data';

const fileBuffer = readFileSync(options.file);
const fileName = options.file.split('/').pop()!;
const formData = new FormData();
formData.append('file', fileBuffer, fileName);
  1. Or using Blob (Node.js v18.0.0+):
const fileBuffer = readFileSync(options.file);
const fileName = options.file.split('/').pop()!;
const blob = new Blob([fileBuffer], { type: 'application/octet-stream' });

Environment

  • Node.js version: v18.17.0
  • OS: macOS
  • CLI version: latest from main branch

Would appreciate any guidance on which approach would be most suitable for the SDK's requirements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions