Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
chore: Prefer axios usage over the deprecated request lib (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed May 3, 2020
1 parent 7749ff7 commit 92b20fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions lib/mjpeg.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import _ from 'lodash';
import request from 'request';
import log from './logger';
import http from 'http';
import B from 'bluebird';
import { getJimpImage, MIME_PNG } from './image-util';
import mJpegServer from 'mjpeg-server';
import { Writable } from 'stream';
import { requirePackage } from './node';
import axios from 'axios';


// lazy load this, as it might not be available
Expand Down Expand Up @@ -98,7 +98,7 @@ class MJpegStream extends Writable {
clear () {
this.registerStartSuccess = null;
this.registerStartFailure = null;
this.request = null;
this.responseStream = null;
this.consumer = null;
this.lastChunk = null;
this.updateCount = 0;
Expand Down Expand Up @@ -127,16 +127,25 @@ class MJpegStream extends Writable {
`Waited ${serverTimeout}ms but the MJPEG server never sent any images`);

const onErr = (err) => {
log.error(`Error getting MJpeg screenshot chunk: ${err}`);
log.error(`Error getting MJpeg screenshot chunk: ${err.message}`);
this.errorHandler(err);
if (this.registerStartFailure) {
this.registerStartFailure(err);
}
};

this.request = request(this.url);
try {
this.responseStream = (await axios({
url: this.url,
method: 'GET',
responseType: 'stream',
timeout: serverTimeout,
})).data;
} catch (e) {
return onErr(e);
}

this.request
this.responseStream
.on('error', onErr) // ensure we do something with errors
.pipe(this.consumer) // allow chunking and transforming of jpeg data
.pipe(this); // send the actual jpegs to ourself
Expand All @@ -153,8 +162,9 @@ class MJpegStream extends Writable {
return;
}

this.responseStream.unpipe();
this.consumer.unpipe();
this.request.end();
this.responseStream.destroy();
this.clear();
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@babel/runtime": "^7.0.0",
"archiver": "^4.0.1",
"axios": "^0.19.2",
"base64-stream": "^1.0.0",
"bluebird": "^3.5.1",
"bplist-creator": "^0",
Expand All @@ -51,7 +52,6 @@
"plist": "^3.0.1",
"pluralize": "^8.0.0",
"pngjs": "^5.0.0",
"request": "^2.83.0",
"request-promise": "^4.2.2",
"rimraf": "^3.0.0",
"sanitize-filename": "^1.6.1",
Expand Down

0 comments on commit 92b20fb

Please sign in to comment.