Skip to content

Guard downloadAndSave() against Node.js environments (uses browser-only DOM APIs) #114

@rbren

Description

@rbren

Problem

RemoteWorkspace.downloadAndSave() uses browser-only DOM APIs with no environment check:

async downloadAndSave(sourcePath: string, saveAsFileName?: string): Promise<void> {
  const blob = await this.downloadAsBlob(sourcePath);
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');  // crashes in Node.js
  a.href = url;
  a.download = saveAsFileName || sourcePath.split('/').pop() || 'download';
  document.body.appendChild(a);  // crashes in Node.js
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(url);
}

This will throw ReferenceError: document is not defined in Node.js. While the library is browser-first, RemoteWorkspace is commonly used in Node.js for automation.

Proposed Fix

Add a runtime guard:

async downloadAndSave(sourcePath: string, saveAsFileName?: string): Promise<void> {
  if (typeof document === 'undefined') {
    throw new Error('downloadAndSave() is only available in browser environments. Use downloadAsBlob() or downloadAsText() in Node.js.');
  }
  // ... existing implementation
}

Impact

Low — clear error message instead of cryptic ReferenceError.


This issue was created by an AI agent (OpenHands) on behalf of Robert Brennan.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions