@durable-streams/client
Version:
TypeScript client for the Durable Streams protocol
134 lines (95 loc) • 3.72 kB
Markdown
A `StreamResponse<TJson>` provides three families of consumption methods.
Choose one per response — calling a second method throws `ALREADY_CONSUMED`.
Use for catch-up reads (`live: false`) or when you want all data at once.
### `.json(): Promise<TJson[]>`
Returns all items as a parsed JSON array. JSON-mode streams only.
```typescript
const res = await stream<{ id: string }>({ url, offset: "-1", live: false })
const items = await res.json()
// items: Array<{ id: string }>
```
Returns the complete body as a string. Works with any content type.
```typescript
const res = await stream({ url, offset: "-1", live: false })
const text = await res.text()
```
Returns the complete body as raw bytes.
```typescript
const res = await stream({ url, offset: "-1", live: false })
const bytes = await res.body()
```
Use for processing items one at a time as they arrive.
Yields individual JSON items. JSON-mode streams only.
```typescript
const res = await stream<{ event: string }>({ url, offset: "-1", live: true })
for await (const item of res.jsonStream()) {
console.log(item.event)
}
```
Yields text chunks as they arrive.
```typescript
const res = await stream({ url, offset: "-1", live: true })
for await (const chunk of res.textStream()) {
process.stdout.write(chunk)
}
```
Yields raw byte chunks.
```typescript
const res = await stream({ url, offset: "-1", live: true })
for await (const chunk of res.bodyStream()) {
processBytes(chunk)
}
```
Use for live subscriptions where you need the offset for checkpointing.
Calls back with batches of JSON items and offset.
```typescript
const res = await stream<{ event: string }>({ url, offset: "-1", live: true })
res.subscribeJson(async (batch) => {
for (const item of batch.items) {
console.log(item.event)
}
saveCheckpoint(batch.offset)
})
```
Calls back with text data and offset.
```typescript
const res = await stream({ url, offset: "-1", live: true })
res.subscribeText(async (chunk) => {
appendToUI(chunk.text)
saveCheckpoint(chunk.offset)
})
```
Calls back with byte data and offset.
```typescript
const res = await stream({ url, offset: "-1", live: true })
res.subscribeBytes(async (chunk) => {
processBytes(chunk.data)
saveCheckpoint(chunk.offset)
})
```
| Property | Type | Description |
| -------------- | --------------------- | ----------------------------------------- |
| `offset` | `string` | Current offset (updates after each chunk) |
| `contentType` | `string \| undefined` | Content type from stream creation |
| `live` | `LiveMode` | The live mode for this session |
| `startOffset` | `string` | The offset this session started from |
| `upToDate` | `boolean` | Whether caught up with all existing data |
| `streamClosed` | `boolean` | Whether the stream is permanently closed |
| `closed` | `Promise<void>` | Resolves when the stream session ends |
## Methods
| Method | Description |
| ---------- | ------------------------- |
| `cancel()` | Cancel the stream session |