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';
|
|
|
|
|
2018-07-15 21:21:07 +00:00
|
|
|
export const getBinary = async (
|
|
|
|
domainArg: string,
|
|
|
|
optionsArg: interfaces.ISmartRequestOptions = {}
|
|
|
|
) => {
|
2020-01-13 08:00:39 +00:00
|
|
|
optionsArg = {
|
|
|
|
...optionsArg,
|
2020-08-24 12:01:38 +00:00
|
|
|
autoJsonParse: false,
|
2020-01-13 08:00:39 +00:00
|
|
|
};
|
2018-07-15 21:21:07 +00:00
|
|
|
const done = plugins.smartpromise.defer();
|
2018-06-18 05:39:42 +00:00
|
|
|
const response = await request(domainArg, optionsArg, true);
|
2022-02-15 17:53:02 +00:00
|
|
|
const data: Array<Buffer> = [];
|
2018-06-18 05:39:42 +00:00
|
|
|
|
2018-07-15 21:21:07 +00:00
|
|
|
response
|
2022-02-15 17:53:02 +00:00
|
|
|
.on('data', function (chunk: Buffer) {
|
2018-06-18 05:39:42 +00:00
|
|
|
data.push(chunk);
|
2018-07-15 21:21:07 +00:00
|
|
|
})
|
2020-08-24 12:01:38 +00:00
|
|
|
.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);
|
2020-01-13 07:58:54 +00:00
|
|
|
response.body = buffer;
|
2018-06-18 05:39:42 +00:00
|
|
|
done.resolve();
|
2018-07-15 21:21:07 +00:00
|
|
|
});
|
2018-06-18 05:39:42 +00:00
|
|
|
await done.promise;
|
|
|
|
return response;
|
2018-07-15 21:21:07 +00:00
|
|
|
};
|