You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
980 B
JavaScript
46 lines
980 B
JavaScript
3 years ago
|
"use strict";
|
||
|
|
||
|
const Promise = require("bluebird");
|
||
|
const pEvent = require("p-event");
|
||
|
const pStream = require("p-stream");
|
||
|
|
||
|
module.exports = function fetch(client, args, options = {}) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
let attributeResults = [];
|
||
|
let bodyInfoResults = [];
|
||
|
let bodyPromises = [];
|
||
|
|
||
|
let fetchOp = client.seq.fetch(... args);
|
||
|
|
||
|
fetchOp.on("end", () => {
|
||
|
resolve(Promise.try(() => {
|
||
|
if (options.onEnd != null) {
|
||
|
return options.onEnd();
|
||
|
}
|
||
|
}).then(() => {
|
||
|
return Promise.all(bodyPromises);
|
||
|
}).then((bodies) => {
|
||
|
return {
|
||
|
attributeResults,
|
||
|
bodyInfoResults,
|
||
|
bodies
|
||
|
};
|
||
|
}));
|
||
|
});
|
||
|
|
||
|
fetchOp.on("message", (message) => {
|
||
|
message.on("attributes", (attributes) => {
|
||
|
attributeResults.push(attributes);
|
||
|
});
|
||
|
|
||
|
message.on("body", (bodyStream, info) => {
|
||
|
bodyInfoResults.push(info);
|
||
|
|
||
|
bodyPromises.push(pStream(bodyStream));
|
||
|
});
|
||
|
});
|
||
|
|
||
|
pEvent(fetchOp, "error").then(reject);
|
||
|
});
|
||
|
};
|