Skip to content

Commit 175d95c

Browse files
Merge pull request #43 from martin-krcmar/issue-250/custom-component-install
Stream lib
2 parents b027fc1 + db7134e commit 175d95c

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

appmixer-lib.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ module.exports = {
1010
},
1111
util: {
1212
array: require('./util/array'),
13-
singletons: require('./util/singletons'),
14-
HttpError: require('./util/http-error'),
15-
object: require('./util/object'),
13+
commons: require('./util/commons'),
1614
component: require('./util/component'),
1715
flow: require('./util/flow'),
16+
HttpError: require('./util/http-error'),
17+
object: require('./util/object'),
1818
PagingAggregator: require('./util/paging-aggregator'),
1919
promise: require('./util/promise'),
20-
commons: require('./util/commons')
20+
singletons: require('./util/singletons'),
21+
Stream: require('./util/Stream')
2122
}
2223
};

lock/method.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Method {
3636
while (this.callbacks.length > 0) {
3737
this.callbacks.pop().resolve(result);
3838
}
39+
return result;
3940
} catch (err) {
4041
this.inProgress = false;
4142
while (this.callbacks.length > 0) {

util/Stream.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
const check = require('check-types');
3+
const Readable = require('stream').Readable;
4+
const Promise = require('bluebird');
5+
6+
/**
7+
* Simple wrapper around node streams.
8+
*/
9+
class Stream {
10+
11+
/**
12+
* @param {string} string
13+
* @return {Stream}
14+
* @throws Error
15+
*/
16+
static createReadStreamFromString(string) {
17+
18+
check.assert.string(string, 'Missing input string.');
19+
let s = new Readable();
20+
s.push(string);
21+
s.push(null);
22+
return s;
23+
}
24+
25+
/**
26+
* @param buffer
27+
* @return {Stream}
28+
*/
29+
static createReadStreamFromBuffer(buffer) {
30+
31+
let s = new Readable();
32+
s.push(buffer);
33+
s.push(null);
34+
return s;
35+
}
36+
37+
/**
38+
* @param {*} what
39+
* @return {boolean}
40+
*/
41+
static isStream(what) {
42+
43+
return what instanceof Readable;
44+
}
45+
46+
/**
47+
* This method reads stream into string, this method affects stream param - once read
48+
* it won't return anything when trying to read again.
49+
* @param {Stream} stream
50+
* @return {Promise<string>}
51+
* @throws Error
52+
*/
53+
static async readStreamToString(stream) {
54+
55+
check.assert.instance(stream, Readable, 'Invalid file stream.');
56+
57+
let fileContent = '';
58+
return new Promise((resolve, reject) => {
59+
stream.on('data', data => {
60+
fileContent += data.toString();
61+
});
62+
stream.on('end', () => {
63+
resolve(fileContent);
64+
});
65+
stream.on('error', err => {
66+
reject(err);
67+
});
68+
});
69+
}
70+
}
71+
72+
module.exports = Stream;

util/promise.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports.mapKeys = async (object, callback) => {
5656
check.assert.function(callback, 'Invalid callback function');
5757

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

8484
let results = [];
85-
return Promise.map(Object.keys(object), async key => {
85+
return Promise.map(Object.keys(object), key => {
8686
return Promise.resolve(callback(object[key], key)).reflect();
8787
}).each(inspection => {
8888
if (!inspection.isFulfilled()) {

0 commit comments

Comments
 (0)