-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adequate support for stream responses (#326)
* Implementation of StreamResponse * Comment type fix * Fixed type in `resolveMiddleware` method name * Updated README.md to include example usage of StreamResponse * ESLint fix * Changed import name for Node streams to be compatible with v12 * merge main --------- Co-authored-by: Podaru Dragos <[email protected]> Co-authored-by: James Monger <[email protected]>
- Loading branch information
1 parent
ebb986f
commit c3570bc
Showing
9 changed files
with
130 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Readable } from 'stream'; | ||
import { HttpContent } from './httpContent'; | ||
|
||
export class StreamContent extends HttpContent { | ||
constructor(private readonly content: Readable, private mediaType: string) { | ||
super(); | ||
|
||
this.headers['content-type'] = mediaType; | ||
} | ||
readAsync(): Promise<Readable> { | ||
return Promise.resolve(this.content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Readable } from 'stream'; | ||
import { IHttpActionResult } from '../interfaces'; | ||
import { HttpResponseMessage } from '../httpResponseMessage'; | ||
import { StreamContent } from '../content/streamContent'; | ||
|
||
|
||
export class StreamResult implements IHttpActionResult { | ||
constructor( | ||
public readableStream: Readable, | ||
public contentType: string, | ||
public readonly statusCode: number, | ||
) { | ||
} | ||
|
||
public async executeAsync(): Promise<HttpResponseMessage> { | ||
const response = new HttpResponseMessage(this.statusCode); | ||
response.content = new StreamContent( | ||
this.readableStream, | ||
this.contentType, | ||
); | ||
return Promise.resolve(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Readable, Writable } from 'stream'; | ||
import { StreamContent } from '../../src/content/streamContent'; | ||
|
||
describe('StreamContent', () => { | ||
it('should have text/plain as the set media type', () => { | ||
const stream = new Readable(); | ||
|
||
const content = new StreamContent(stream, 'text/plain'); | ||
|
||
expect(content.headers['content-type']).toEqual('text/plain'); | ||
}); | ||
|
||
it('should be able to pipe stream which was given to it', done => { | ||
const stream = new Readable({ | ||
read() { | ||
this.push(Buffer.from('test')); | ||
this.push(null); | ||
}, | ||
}); | ||
|
||
const content = new StreamContent(stream, 'text/plain'); | ||
|
||
void content.readAsync().then((readable: Readable) => { | ||
const chunks: Array<Buffer> = []; | ||
|
||
let buffer: Buffer | null = null; | ||
|
||
readable.on('end', () => { | ||
buffer = Buffer.concat(chunks); | ||
|
||
expect(buffer.toString()).toEqual('test'); | ||
|
||
done(); | ||
}); | ||
|
||
const writableStream = new Writable({ | ||
write(chunk) { | ||
chunks.push(chunk as Buffer); | ||
}, | ||
}); | ||
|
||
readable.pipe(writableStream); | ||
}); | ||
}); | ||
}); |