|
| 1 | +/* @flow */ |
| 2 | +/* global window */ |
| 3 | + |
| 4 | +import Config from '../core/components/config'; |
| 5 | +import categoryConstants from '../core/constants/categories'; |
| 6 | + |
| 7 | +import { EndpointDefinition, NetworkingModules } from '../core/flow_interfaces'; |
| 8 | + |
| 9 | +export default class { |
| 10 | + _modules: NetworkingModules; |
| 11 | + _config: Config; |
| 12 | + |
| 13 | + _maxSubDomain: number; |
| 14 | + _currentSubDomain: number; |
| 15 | + |
| 16 | + _standardOrigin: string; |
| 17 | + _subscribeOrigin: string; |
| 18 | + |
| 19 | + _providedFQDN: string; |
| 20 | + |
| 21 | + _requestTimeout: number; |
| 22 | + |
| 23 | + _coreParams: Object; /* items that must be passed with each request. */ |
| 24 | + |
| 25 | + constructor(modules: NetworkingModules) { |
| 26 | + this._modules = {}; |
| 27 | + |
| 28 | + Object.keys(modules).forEach((key) => { |
| 29 | + this._modules[key] = modules[key].bind(this); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + init(config: Config) { |
| 34 | + this._config = config; |
| 35 | + |
| 36 | + this._maxSubDomain = 20; |
| 37 | + this._currentSubDomain = Math.floor(Math.random() * this._maxSubDomain); |
| 38 | + this._providedFQDN = (this._config.secure ? 'https://' : 'http://') + this._config.origin; |
| 39 | + this._coreParams = {}; |
| 40 | + |
| 41 | + // create initial origins |
| 42 | + this.shiftStandardOrigin(); |
| 43 | + } |
| 44 | + |
| 45 | + nextOrigin(): string { |
| 46 | + // if a custom origin is supplied, use do not bother with shuffling subdomains |
| 47 | + if (this._providedFQDN.indexOf('pubsub.') === -1) { |
| 48 | + return this._providedFQDN; |
| 49 | + } |
| 50 | + |
| 51 | + let newSubDomain: string; |
| 52 | + |
| 53 | + this._currentSubDomain = this._currentSubDomain + 1; |
| 54 | + |
| 55 | + if (this._currentSubDomain >= this._maxSubDomain) { |
| 56 | + this._currentSubDomain = 1; |
| 57 | + } |
| 58 | + |
| 59 | + newSubDomain = this._currentSubDomain.toString(); |
| 60 | + |
| 61 | + return this._providedFQDN.replace('pubsub', `ps${newSubDomain}`); |
| 62 | + } |
| 63 | + |
| 64 | + // origin operations |
| 65 | + shiftStandardOrigin(failover: boolean = false): string { |
| 66 | + this._standardOrigin = this.nextOrigin(failover); |
| 67 | + |
| 68 | + return this._standardOrigin; |
| 69 | + } |
| 70 | + |
| 71 | + getStandardOrigin(): string { |
| 72 | + return this._standardOrigin; |
| 73 | + } |
| 74 | + |
| 75 | + POST(params: Object, body: string, endpoint: EndpointDefinition, callback: Function) { |
| 76 | + return this._modules.post(params, body, endpoint, callback); |
| 77 | + } |
| 78 | + |
| 79 | + GET(params: Object, endpoint: EndpointDefinition, callback: Function) { |
| 80 | + return this._modules.get(params, endpoint, callback); |
| 81 | + } |
| 82 | + |
| 83 | + _detectErrorCategory(err: Object): string { |
| 84 | + if (err.code === 'ENOTFOUND') return categoryConstants.PNNetworkIssuesCategory; |
| 85 | + if (err.code === 'ECONNREFUSED') return categoryConstants.PNNetworkIssuesCategory; |
| 86 | + if (err.code === 'ECONNRESET') return categoryConstants.PNNetworkIssuesCategory; |
| 87 | + if (err.code === 'EAI_AGAIN') return categoryConstants.PNNetworkIssuesCategory; |
| 88 | + |
| 89 | + if (err.status === 0 || (err.hasOwnProperty('status') && typeof err.status === 'undefined')) return categoryConstants.PNNetworkIssuesCategory; |
| 90 | + if (err.timeout) return categoryConstants.PNTimeoutCategory; |
| 91 | + |
| 92 | + if (err.response) { |
| 93 | + if (err.response.badRequest) return categoryConstants.PNBadRequestCategory; |
| 94 | + if (err.response.forbidden) return categoryConstants.PNAccessDeniedCategory; |
| 95 | + } |
| 96 | + |
| 97 | + return categoryConstants.PNUnknownCategory; |
| 98 | + } |
| 99 | +} |
0 commit comments