Skip to content

Use a single type for attachment data instead of Blob | Buffer #4

@cuibonobo

Description

@cuibonobo

getAttachment() and putAttachment() currently use Blob | Buffer in the StackAdapter interface:

putAttachment(data: Blob | Buffer, mimeType: string): Promise<FileId>;
getAttachment(fileId: FileId): Promise<Blob | Buffer>;

This union is awkward for callers — Blob and Buffer have incompletely overlapping APIs, and code that handles both needs branching logic (as seen in the SQLite adapter tests). In practice the SQLite adapter always returns a Buffer, while a browser-targeted adapter would return a Blob.

Options:

  • Uint8Array — works in both Node and browsers, universally understood, no branching needed. Would require callers to wrap in Buffer.from() or new Blob([...]) if they need those specifically.
  • ArrayBuffer — similar story, slightly less ergonomic.
  • Keep the union but provide a helper — e.g. toUint8Array(data: Blob | Buffer) in @haverstack/core. Avoids a breaking change to the interface but doesn't eliminate the awkwardness.

Uint8Array is probably the right call — it's the standard cross-runtime binary type and both Buffer and Blob are constructible from it.

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