smartrequest/ts/smartrequest.binaryrest.ts

30 lines
864 B
TypeScript
Raw Normal View History

2018-06-18 05:39:42 +00:00
// this file implements methods to get and post binary data.
import * as interfaces from './smartrequest.interfaces';
import { request } from './smartrequest.request';
import * as plugins from './smartrequest.plugins';
export const getBinary = async (
domainArg: string,
optionsArg: interfaces.ISmartRequestOptions = {}
) => {
const done = plugins.smartpromise.defer();
2018-06-18 05:39:42 +00:00
const response = await request(domainArg, optionsArg, true);
const data = [];
response
.on('data', function(chunk) {
2018-06-18 05:39:42 +00:00
data.push(chunk);
})
.on('end', function() {
2018-06-18 05:39:42 +00:00
//at this point data is an array of Buffers
//so Buffer.concat() can make us a new Buffer
//of all of them together
const buffer = Buffer.concat(data);
response.body = buffer.toString('binary');
done.resolve();
});
2018-06-18 05:39:42 +00:00
await done.promise;
return response;
};