BREAKING CHANGE(client/streaming): Unify streaming APIs: remove raw()/streamNode() and standardize on web ReadableStream across runtimes
This commit is contained in:
@@ -1,27 +1,68 @@
|
||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||
import { SmartRequest } from '../ts/index.js';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
tap.test('should have streamNode() method available', async () => {
|
||||
tap.test('should have stream() method that returns web ReadableStream', async () => {
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://httpbin.org/get')
|
||||
.get();
|
||||
|
||||
// Verify streamNode() method exists
|
||||
expect(response.streamNode).toBeDefined();
|
||||
expect(typeof response.streamNode).toEqual('function');
|
||||
|
||||
// In Node.js, it should return a stream
|
||||
const nodeStream = response.streamNode();
|
||||
// Verify stream() method exists
|
||||
expect(response.stream).toBeDefined();
|
||||
expect(typeof response.stream).toEqual('function');
|
||||
|
||||
// Get web stream
|
||||
const webStream = response.stream();
|
||||
expect(webStream).toBeDefined();
|
||||
|
||||
// Verify it's a web ReadableStream
|
||||
expect(typeof webStream.getReader).toEqual('function');
|
||||
expect(typeof webStream.cancel).toEqual('function');
|
||||
|
||||
// Convert to Node.js stream using Readable.fromWeb()
|
||||
// Known TypeScript limitation: @types/node ReadableStream differs from web ReadableStream
|
||||
const nodeStream = Readable.fromWeb(webStream as any);
|
||||
expect(nodeStream).toBeDefined();
|
||||
|
||||
|
||||
// Verify it's a Node.js readable stream
|
||||
expect(typeof nodeStream.pipe).toEqual('function');
|
||||
expect(typeof nodeStream.on).toEqual('function');
|
||||
|
||||
|
||||
// Consume the stream to avoid hanging
|
||||
nodeStream.resume();
|
||||
});
|
||||
|
||||
tap.test('should convert web stream to Node.js stream correctly', async () => {
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://httpbin.org/get')
|
||||
.get();
|
||||
|
||||
const webStream = response.stream();
|
||||
const nodeStream = Readable.fromWeb(webStream as any);
|
||||
|
||||
export default tap.start();
|
||||
// Collect data from stream
|
||||
const chunks: Buffer[] = [];
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
nodeStream.on('data', (chunk) => {
|
||||
chunks.push(chunk);
|
||||
});
|
||||
|
||||
nodeStream.on('end', () => {
|
||||
resolve();
|
||||
});
|
||||
|
||||
nodeStream.on('error', reject);
|
||||
});
|
||||
|
||||
// Verify we received data
|
||||
const data = Buffer.concat(chunks);
|
||||
expect(data.length).toBeGreaterThan(0);
|
||||
|
||||
// Verify it's valid JSON
|
||||
const json = JSON.parse(data.toString('utf-8'));
|
||||
expect(json).toBeDefined();
|
||||
expect(json.url).toEqual('https://httpbin.org/get');
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
|
||||
Reference in New Issue
Block a user