feat(client/smartrequest): Add streaming and raw buffer support to SmartRequest (buffer, stream, raw); update docs and tests
This commit is contained in:
94
readme.md
94
readme.md
@@ -25,7 +25,7 @@ yarn add @push.rocks/smartrequest
|
||||
- ⚡ **Keep-Alive Connections** - Efficient connection pooling in Node.js
|
||||
- 🛡️ **TypeScript First** - Full type safety and IntelliSense support
|
||||
- 🎯 **Zero Magic Defaults** - Explicit configuration following fetch API principles
|
||||
- 📡 **Streaming Support** - Handle large files and real-time data
|
||||
- 📡 **Streaming Support** - Stream buffers, files, and custom data without loading into memory
|
||||
- 🔧 **Highly Configurable** - Timeouts, retries, headers, and more
|
||||
|
||||
## Architecture
|
||||
@@ -303,6 +303,98 @@ async function uploadMultipleFiles(
|
||||
}
|
||||
```
|
||||
|
||||
### Streaming Request Bodies
|
||||
|
||||
SmartRequest provides multiple ways to stream data in requests, making it easy to upload large files or send real-time data without loading everything into memory:
|
||||
|
||||
```typescript
|
||||
import { SmartRequest } from '@push.rocks/smartrequest';
|
||||
import * as fs from 'fs';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
// Stream a Buffer directly
|
||||
async function uploadBuffer() {
|
||||
const buffer = Buffer.from('Hello, World!');
|
||||
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://api.example.com/upload')
|
||||
.buffer(buffer, 'text/plain')
|
||||
.post();
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
// Stream a file using Node.js streams
|
||||
async function uploadLargeFile(filePath: string) {
|
||||
const fileStream = fs.createReadStream(filePath);
|
||||
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://api.example.com/upload')
|
||||
.stream(fileStream, 'application/octet-stream')
|
||||
.post();
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
// Stream data from any readable source
|
||||
async function streamData(dataSource: Readable) {
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://api.example.com/stream')
|
||||
.stream(dataSource)
|
||||
.post();
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
// Advanced: Full control over request streaming (Node.js only)
|
||||
async function customStreaming() {
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://api.example.com/stream')
|
||||
.raw((request) => {
|
||||
// Custom streaming logic - you have full control
|
||||
request.write('chunk1');
|
||||
request.write('chunk2');
|
||||
|
||||
// Stream from another source
|
||||
someReadableStream.pipe(request);
|
||||
})
|
||||
.post();
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
// Send Uint8Array (works in both Node.js and browser)
|
||||
async function uploadBinaryData() {
|
||||
const data = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
|
||||
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://api.example.com/binary')
|
||||
.buffer(data, 'application/octet-stream')
|
||||
.post();
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
```
|
||||
|
||||
#### Streaming Methods
|
||||
|
||||
- **`.buffer(data, contentType?)`** - Stream a Buffer or Uint8Array directly
|
||||
- `data`: Buffer or Uint8Array to send
|
||||
- `contentType`: Optional content type (defaults to 'application/octet-stream')
|
||||
|
||||
- **`.stream(stream, contentType?)`** - Stream from Node.js ReadableStream or web ReadableStream
|
||||
- `stream`: The stream to pipe to the request
|
||||
- `contentType`: Optional content type
|
||||
|
||||
- **`.raw(streamFunc)`** - Advanced control over request streaming (Node.js only)
|
||||
- `streamFunc`: Function that receives the raw request object for custom streaming
|
||||
|
||||
These methods are particularly useful for:
|
||||
- Uploading large files without loading them into memory
|
||||
- Streaming real-time data to servers
|
||||
- Proxying data between services
|
||||
- Implementing chunked transfer encoding
|
||||
|
||||
### Unix Socket Support (Node.js only)
|
||||
|
||||
```typescript
|
||||
|
Reference in New Issue
Block a user