-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): adds new class for file stream
The new `StreamableFile` class allows for users to create an easy to consume file to send back to the client side. Both `ExpressAdapter` and `FastifyAdapter` have been updated to accomodate for this new class.
- Loading branch information
Showing
8 changed files
with
55 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Injectable, StreamableFile } from '@nestjs/common'; | ||
import { createReadStream, readFileSync } from 'fs'; | ||
import { Readable } from 'stream'; | ||
import { join } from 'path'; | ||
import { NonFile } from './non-file'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
|
||
getReadStream(): Readable { | ||
return createReadStream(join(process.cwd(), 'Readme.md')); | ||
getReadStream(): StreamableFile { | ||
return new StreamableFile( | ||
createReadStream(join(process.cwd(), 'Readme.md')), | ||
); | ||
} | ||
|
||
getBuffer(): Buffer { | ||
return readFileSync(join(process.cwd(), 'Readme.md')); | ||
getBuffer(): StreamableFile { | ||
return new StreamableFile(readFileSync(join(process.cwd(), 'Readme.md'))); | ||
} | ||
|
||
getNonFile(): NonFile { | ||
return new NonFile('Hello world'); | ||
} | ||
} | ||
} |
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 @@ | ||
export * from './streamable-file'; |
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,24 @@ | ||
import { buffer } from 'rxjs/operators'; | ||
import { Readable } from 'stream'; | ||
|
||
export class StreamableFile { | ||
private stream: Readable; | ||
constructor(buffer: Buffer); | ||
constructor(readble: Readable); | ||
constructor(bufferOrReadStream: Buffer | Readable) { | ||
if (Buffer.isBuffer(bufferOrReadStream)) { | ||
this.stream = new Readable(); | ||
this.stream.push(bufferOrReadStream); | ||
this.stream.push(null); | ||
} else if ( | ||
bufferOrReadStream.pipe && | ||
typeof bufferOrReadStream.pipe === 'function' | ||
) { | ||
this.stream = bufferOrReadStream; | ||
} | ||
} | ||
|
||
getStream() { | ||
return this.stream; | ||
} | ||
} |
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