Skip to content

Commit c5f2a3b

Browse files
Merge pull request #75 from pubnub/networkingModules
move networking functions to modules
2 parents 8c9fbf9 + 9417293 commit c5f2a3b

File tree

10 files changed

+236
-241
lines changed

10 files changed

+236
-241
lines changed

src/core/flow_interfaces.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ export type KeepAliveStruct = {
2929
}
3030

3131
export type NetworkingModules = {
32-
agentKeepAliveModule: ?Function,
33-
sendBeaconModule: ?Function
32+
keepAlive: ?Function,
33+
sendBeacon: ?Function,
34+
get: Function,
35+
post: Function
3436
}
3537

3638
export type InternalSetupStruct = {

src/core/pubnub-common.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import uuidGenerator from 'uuid';
44

55
import Config from './components/config';
66
import Crypto from './components/cryptography/index';
7-
import Networking from '../networking/base';
87
import SubscriptionManager from './components/subscription_manager';
98
import ListenerManager from './components/listener_manager';
109

@@ -107,10 +106,6 @@ export default class {
107106
const config = this._config = new Config({ setup, db });
108107
const crypto = new Crypto({ config });
109108

110-
if (!networking) {
111-
networking = new Networking({});
112-
}
113-
114109
networking.init(config);
115110

116111
let modules = { config, networking, crypto };

src/networking/base.js

Lines changed: 0 additions & 169 deletions
This file was deleted.

src/networking/index.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)