Skip to content

Commit

Permalink
Merge pull request #43 from martin-krcmar/issue-250/custom-component-…
Browse files Browse the repository at this point in the history
…install

Stream lib
  • Loading branch information
martin-krcmar authored Feb 25, 2019
2 parents b027fc1 + db7134e commit 175d95c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
9 changes: 5 additions & 4 deletions appmixer-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ module.exports = {
},
util: {
array: require('./util/array'),
singletons: require('./util/singletons'),
HttpError: require('./util/http-error'),
object: require('./util/object'),
commons: require('./util/commons'),
component: require('./util/component'),
flow: require('./util/flow'),
HttpError: require('./util/http-error'),
object: require('./util/object'),
PagingAggregator: require('./util/paging-aggregator'),
promise: require('./util/promise'),
commons: require('./util/commons')
singletons: require('./util/singletons'),
Stream: require('./util/Stream')
}
};
1 change: 1 addition & 0 deletions lock/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Method {
while (this.callbacks.length > 0) {
this.callbacks.pop().resolve(result);
}
return result;
} catch (err) {
this.inProgress = false;
while (this.callbacks.length > 0) {
Expand Down
72 changes: 72 additions & 0 deletions util/Stream.js
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;
4 changes: 2 additions & 2 deletions util/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports.mapKeys = async (object, callback) => {
check.assert.function(callback, 'Invalid callback function');

let results = [];
return Promise.map(Object.keys(object), async key => {
return Promise.map(Object.keys(object), key => {
return Promise.resolve(callback(key)).reflect();
}).each(inspection => {
if (!inspection.isFulfilled()) {
Expand All @@ -82,7 +82,7 @@ module.exports.mapProperties = async (object, callback) => {
check.assert.function(callback, 'Invalid callback function');

let results = [];
return Promise.map(Object.keys(object), async key => {
return Promise.map(Object.keys(object), key => {
return Promise.resolve(callback(object[key], key)).reflect();
}).each(inspection => {
if (!inspection.isFulfilled()) {
Expand Down

0 comments on commit 175d95c

Please sign in to comment.