@magaya/hyperion-algorithms
Version:
Algorithms meant to be used with hyperion.
50 lines (41 loc) • 1.39 kB
JavaScript
/*
Copyright 2018 Magaya Corporation
*/
class StreamAsync {
constructor(retriever, resource) {
this.retriever = retriever;
this.resource = resource;
}
stream(writeStream) {
function writeChunk(resolve, writeStream, data, start) {
const size = 4 * 1024;
if (!data || start == data.length ) {
resolve();
return;
}
const end = Math.min(start + size, data.length);
const buffer = data.slice(start, end);
writeStream.write(buffer);
process.nextTick(writeChunk, resolve, writeStream, data, end);
}
return new Promise((resolve, reject) => {
this.retriever(
this.resource,
(data) => {
writeChunk(resolve, writeStream, data, 0);
});
});
}
}
class StreamAttachmentAsync extends StreamAsync {
constructor(asyncApi, attachment) {
super(asyncApi.getAttachmentContentAsync, attachment);
}
}
class StreamDocumentAsync extends StreamAsync {
constructor(asyncApi, document) {
super(asyncApi.getDocumentContentAsync, document);
}
}
module.exports.StreamDocument = StreamDocumentAsync;
module.exports.StreamAttachment = StreamAttachmentAsync;