diff --git a/package.json b/package.json index c028cc6d..c79636f1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/request.coffee b/src/lib/request.coffee deleted file mode 100644 index beb86623..00000000 --- a/src/lib/request.coffee +++ /dev/null @@ -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) diff --git a/src/lib/request.ts b/src/lib/request.ts new file mode 100644 index 00000000..167ed802 --- /dev/null +++ b/src/lib/request.ts @@ -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); diff --git a/typings/resumable-request.d.ts b/typings/resumable-request.d.ts new file mode 100644 index 00000000..bfaceb61 --- /dev/null +++ b/typings/resumable-request.d.ts @@ -0,0 +1,5 @@ +declare module 'resumable-request' { + import * as request from 'request'; + // Not technically correct, but they do share an interface + export = request; +}