Skip to content

Add getArrayBuffer(), getBlob(), and getBuffer() to SpeakRestClient for TTS response #457

@pranavkafle

Description

@pranavkafle

Summary
SpeakRestClient only exposes getStream() and getHeaders(). To get the audio as bytes (e.g. to save to file), users must manually consume the stream and buffer chunks. The Python SDK exposes convenience methods (response.stream.getvalue(), .save()) for this. Adding similar methods to the JS SDK would improve parity and reduce boilerplate.

Current usage (from examples/node-speak/index.js):

const stream = await response.getStream();
if (stream) {
  const buffer = await new Promise((resolve, reject) => {
    const chunks = [];
    stream.on("data", (chunk) => chunks.push(chunk));
    stream.on("end", () => resolve(Buffer.concat(chunks)));
    stream.on("error", reject);
  });
  writeFileSync(filePath, buffer);
}

Proposed API
Add three methods on SpeakRestClient that delegate to the stored Response:

  • getArrayBuffer(): Promise<ArrayBuffer>this.result.arrayBuffer()
  • getBlob(): Promise<Blob>this.result.blob()
  • getBuffer(): Promise<Buffer> – Node.js convenience: Buffer.from(await this.getArrayBuffer())

Same precondition as existing accessors: throw if no request has been made yet (e.g. "Tried to get buffer before making request").

Why

  • Matches the convenience the Python SDK provides.
  • Reduces repeated stream-to-buffer code.
  • Uses standard Response APIs; small, low-risk change.

Scope

  • Implementation in SpeakRestClient.ts
  • Tests in the existing speak E2E test file (success path + "no request" error)
  • Optional: simplify examples/node-speak/index.js to use getBuffer()

Happy to implement this once maintainers are okay with the direction.

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