Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Stream-Api
- return new Promise(async (resolve, reject) => {
- const response = await this.gotInstanse(url, gotOptions);
- const converterStream = iconv.decodeStream('win1251');
- const xmlStream = await this.gotInstanse.stream(url, gotOptions);
- xmlStream.pipe(converterStream);
- let xmlString = '';
- xmlStream
- .on('error', error => this.logger.error(error))
- .on('data', data => {
- xmlString += data;
- })
- .on('end', () => {
- if (parser.validate(xmlString) === true) {
- //optional (it'll return an object in case it's not valid)
- const jsonObj = parser.parse(xmlString, options);
- resolve(jsonObj);
- } else {
- reject({ error: 'format is not valid' });
- }
- });
- });
- Buffer-Api
- const { url, auth, login, password } = params;
- const gotOptions = auth ? { username: login, password } : {};
- const responsePromise = this.gotInstanse(url, gotOptions);
- const bufferPromise = responsePromise.buffer();
- try {
- const [response, buffer] = await Promise.all([responsePromise, bufferPromise]);
- let parseString = response.body;
- if (
- response.headers['content-type'].includes('windows-1251') ||
- response.headers['content-type'].includes('cp1251')
- ) {
- parseString = iconv.decode(buffer, 'win1251');
- }
- return parser.parse(parseString, options);
- } catch (err) {
- throw new Error(err);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement