refactor: Convert request module to typescript

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2018-12-12 15:26:43 +00:00
parent 2ea657c95d
commit e00954babd
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
4 changed files with 54 additions and 40 deletions

View File

@ -38,6 +38,7 @@
"@types/memoizee": "^0.4.2",
"@types/mz": "0.0.32",
"@types/node": "^10.3.1",
"@types/request": "^2.48.1",
"@types/rwlock": "^5.0.2",
"@types/shell-quote": "^1.6.0",
"blinking": "~0.0.2",

View File

@ -1,40 +0,0 @@
Promise = require 'bluebird'
request = require 'request'
resumable = require 'resumable-request'
constants = require './constants'
osRelease = require './os-release'
osVersion = osRelease.getOSVersionSync(constants.hostOSVersionPath)
osVariant = osRelease.getOSVariantSync(constants.hostOSVersionPath)
supervisorVersion = require('./supervisor-version')
userAgent = "Supervisor/#{supervisorVersion}"
if osVersion?
if osVariant?
userAgent += " (Linux; #{osVersion}; #{osVariant})"
else
userAgent += " (Linux; #{osVersion})"
# With these settings, the device must be unable to receive a single byte
# from the network for a continuous period of 20 minutes before we give up.
# (reqTimeout + retryInterval) * retryCount / 1000ms / 60sec ~> minutes
DEFAULT_REQUEST_TIMEOUT = 30000 # ms
DEFAULT_REQUEST_RETRY_INTERVAL = 10000 # ms
DEFAULT_REQUEST_RETRY_COUNT = 30
exports.requestOpts =
gzip: true
timeout: DEFAULT_REQUEST_TIMEOUT
headers:
'User-Agent': userAgent
resumableOpts =
timeout: DEFAULT_REQUEST_TIMEOUT
maxRetries: DEFAULT_REQUEST_RETRY_COUNT
retryInterval: DEFAULT_REQUEST_RETRY_INTERVAL
request = request.defaults(exports.requestOpts)
exports.request = Promise.promisifyAll(request, multiArgs: true)
exports.resumable = resumable.defaults(resumableOpts)

48
src/lib/request.ts Normal file
View File

@ -0,0 +1,48 @@
import * as Bluebird from 'bluebird';
import * as requestLib from 'request';
import * as resumableRequestLib from 'resumable-request';
import * as constants from './constants';
import * as osRelease from './os-release';
import supervisorVersion = require('./supervisor-version');
const osVersion = osRelease.getOSVersionSync(constants.hostOSVersionPath);
const osVariant = osRelease.getOSVariantSync(constants.hostOSVersionPath);
let userAgent = `Supervisor/${supervisorVersion}`;
if (osVersion != null) {
if (osVariant != null) {
userAgent += ` (Linux; ${osVersion}; ${osVariant})`;
} else {
userAgent += ` (Linux; ${osVersion})`;
}
}
// With these settings, the device must be unable to receive a single byte
// from the network for a continuous period of 20 minutes before we give up.
// (reqTimeout + retryInterval) * retryCount / 1000ms / 60sec ~> minutes
const DEFAULT_REQUEST_TIMEOUT = 30000; // ms
const DEFAULT_REQUEST_RETRY_INTERVAL = 10000; // ms
const DEFAULT_REQUEST_RETRY_COUNT = 30;
export const requestOpts = {
gzip: true,
timeout: DEFAULT_REQUEST_TIMEOUT,
headers: {
'User-Agent': userAgent,
},
};
const resumableOpts = {
timeout: DEFAULT_REQUEST_TIMEOUT,
maxRetries: DEFAULT_REQUEST_RETRY_COUNT,
retryInterval: DEFAULT_REQUEST_RETRY_INTERVAL,
};
const requestHandle = requestLib.defaults(exports.requestOpts);
export const request = Bluebird.promisifyAll(requestHandle, {
multiArgs: true,
});
export const resumable = resumableRequestLib.defaults(resumableOpts);

5
typings/resumable-request.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
declare module 'resumable-request' {
import * as request from 'request';
// Not technically correct, but they do share an interface
export = request;
}