Skip to content

Commit 65aabb6

Browse files
TsuyoshiUshioTsuyoshi Ushio
andauthored
Fix HttpResponseBodyInit to support Node.js Readable streams (#410)
* Fix HttpResponseBodyInit to include Node.js Readable streams Fixes #409 - adds Readable from stream module to HttpResponseBodyInit type to support HTTP streaming scenarios with Node.js streams. * Add tests for Node.js Readable stream body support * Fix lint error and bump version to 4.11.2 * Update version in src/constants.ts to 4.11.2 --------- Co-authored-by: Tsuyoshi Ushio <tsushi@microsoft.com>
1 parent 06427e7 commit 65aabb6

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azure/functions",
3-
"version": "4.11.1",
3+
"version": "4.11.2",
44
"description": "Microsoft Azure Functions NodeJS Framework",
55
"keywords": [
66
"azure",

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
export const version = '4.11.1';
4+
export const version = '4.11.2';
55

66
export const returnBindingKey = '$return';

test/http/HttpResponse.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Blob } from 'buffer';
66
import * as chai from 'chai';
77
import { expect } from 'chai';
88
import * as chaiAsPromised from 'chai-as-promised';
9+
import { Readable } from 'stream';
910
import { ReadableStream } from 'stream/web';
1011
import { HttpResponse } from '../../src/http/HttpResponse';
1112

@@ -366,6 +367,39 @@ describe('HttpResponse', () => {
366367
});
367368
expect(await res.text()).to.equal('Stream content');
368369
});
370+
371+
it('Node.js Readable stream body', async () => {
372+
// Create a Node.js Readable stream (common for HTTP streaming scenarios)
373+
const readable = new Readable({
374+
read() {
375+
this.push('Readable ');
376+
this.push('stream ');
377+
this.push('content');
378+
this.push(null); // Signal end of stream
379+
},
380+
});
381+
382+
const res = new HttpResponse({
383+
body: readable,
384+
});
385+
expect(await res.text()).to.equal('Readable stream content');
386+
});
387+
388+
it('Node.js Readable stream from async generator', async () => {
389+
// Common pattern for HTTP streaming (e.g., AI chat responses)
390+
function* generateChunks() {
391+
yield 'Hello ';
392+
yield 'from ';
393+
yield 'stream';
394+
}
395+
396+
const readable = Readable.from(generateChunks());
397+
398+
const res = new HttpResponse({
399+
body: readable,
400+
});
401+
expect(await res.text()).to.equal('Hello from stream');
402+
});
369403
});
370404

371405
describe('HttpHeadersInit types', () => {

types/http.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
import { Blob } from 'buffer';
5+
import { Readable } from 'stream';
56
import { ReadableStream } from 'stream/web';
67
import { URLSearchParams } from 'url';
78
import { FunctionOptions, FunctionOutput, FunctionResult, FunctionTrigger } from './index';
@@ -11,9 +12,11 @@ import { InvocationContext } from './InvocationContext';
1112
* Represents the body types that can be used in an HTTP response.
1213
* This is a local definition to avoid dependency on lib.dom.
1314
* Compatible with Node.js native Fetch API body types.
15+
* Includes Node.js Readable streams for HTTP streaming scenarios.
1416
*/
1517
export type HttpResponseBodyInit =
1618
| ReadableStream
19+
| Readable
1720
| Blob
1821
| ArrayBufferView
1922
| ArrayBuffer

0 commit comments

Comments
 (0)