mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-24 07:46:41 +00:00
refactor: Convert request module to typescript
Change-type: patch Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
parent
2ea657c95d
commit
e00954babd
@ -38,6 +38,7 @@
|
|||||||
"@types/memoizee": "^0.4.2",
|
"@types/memoizee": "^0.4.2",
|
||||||
"@types/mz": "0.0.32",
|
"@types/mz": "0.0.32",
|
||||||
"@types/node": "^10.3.1",
|
"@types/node": "^10.3.1",
|
||||||
|
"@types/request": "^2.48.1",
|
||||||
"@types/rwlock": "^5.0.2",
|
"@types/rwlock": "^5.0.2",
|
||||||
"@types/shell-quote": "^1.6.0",
|
"@types/shell-quote": "^1.6.0",
|
||||||
"blinking": "~0.0.2",
|
"blinking": "~0.0.2",
|
||||||
|
@ -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
48
src/lib/request.ts
Normal 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
5
typings/resumable-request.d.ts
vendored
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user