-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from martin-krcmar/issue-250/custom-component-…
…install Stream lib
- Loading branch information
Showing
4 changed files
with
80 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
const check = require('check-types'); | ||
const Readable = require('stream').Readable; | ||
const Promise = require('bluebird'); | ||
|
||
/** | ||
* Simple wrapper around node streams. | ||
*/ | ||
class Stream { | ||
|
||
/** | ||
* @param {string} string | ||
* @return {Stream} | ||
* @throws Error | ||
*/ | ||
static createReadStreamFromString(string) { | ||
|
||
check.assert.string(string, 'Missing input string.'); | ||
let s = new Readable(); | ||
s.push(string); | ||
s.push(null); | ||
return s; | ||
} | ||
|
||
/** | ||
* @param buffer | ||
* @return {Stream} | ||
*/ | ||
static createReadStreamFromBuffer(buffer) { | ||
|
||
let s = new Readable(); | ||
s.push(buffer); | ||
s.push(null); | ||
return s; | ||
} | ||
|
||
/** | ||
* @param {*} what | ||
* @return {boolean} | ||
*/ | ||
static isStream(what) { | ||
|
||
return what instanceof Readable; | ||
} | ||
|
||
/** | ||
* This method reads stream into string, this method affects stream param - once read | ||
* it won't return anything when trying to read again. | ||
* @param {Stream} stream | ||
* @return {Promise<string>} | ||
* @throws Error | ||
*/ | ||
static async readStreamToString(stream) { | ||
|
||
check.assert.instance(stream, Readable, 'Invalid file stream.'); | ||
|
||
let fileContent = ''; | ||
return new Promise((resolve, reject) => { | ||
stream.on('data', data => { | ||
fileContent += data.toString(); | ||
}); | ||
stream.on('end', () => { | ||
resolve(fileContent); | ||
}); | ||
stream.on('error', err => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = 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