Repository navigation
Expand file tree
/
Copy pathendpoint.js
More file actions
150 lines (120 loc) · 4.24 KB
/
Copy pathendpoint.js
File metadata and controls
150 lines (120 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import uuidGenerator from 'uuid';
import { StatusAnnouncement } from '../flow_interfaces';
import utils from '../utils';
import Config from './config';
import operationConstants from '../constants/operations';
class PubNubError extends Error {
constructor(message, status) {
super(message);
this.name = this.constructor.name;
this.status = status;
this.message = message;
}
}
function createError(errorPayload: Object, type: string): Object {
errorPayload.type = type;
return errorPayload;
}
function createValidationError(message: string): Object {
return createError({ message }, 'validationError');
}
function decideURL(endpoint, modules, incomingParams) {
if (endpoint.usePost && endpoint.usePost(modules, incomingParams)) {
return endpoint.postURL(modules, incomingParams);
} else {
return endpoint.getURL(modules, incomingParams);
}
}
function generatePNSDK(config: Config): string {
let base = 'PubNub-JS-' + config.sdkFamily;
if (config.partnerId) {
base += '-' + config.partnerId;
}
base += '/' + config.getVersion();
return base;
}
function signRequest(modules, url, outgoingParams) {
let { config, crypto } = modules;
outgoingParams.timestamp = Math.floor(new Date().getTime() / 1000);
let signInput = config.subscribeKey + '\n' + config.publishKey + '\n' + url + '\n';
signInput += utils.signPamFromParams(outgoingParams);
let signature = crypto.HMACSHA256(signInput);
signature = signature.replace(/\+/g, '-');
signature = signature.replace(/\//g, '_');
outgoingParams.signature = signature;
}
export default function (modules, endpoint, ...args) {
let { networking, config } = modules;
let callback = null;
let promiseComponent = null;
let incomingParams = {};
if (endpoint.getOperation() === operationConstants.PNTimeOperation || endpoint.getOperation() === operationConstants.PNChannelGroupsOperation) {
callback = args[0];
} else {
incomingParams = args[0];
callback = args[1];
}
// bridge in Promise support.
if (typeof Promise !== 'undefined' && !callback) {
promiseComponent = utils.createPromise();
}
let validationResult = endpoint.validateParams(modules, incomingParams);
if (validationResult) {
if (callback) {
return callback(createValidationError(validationResult));
} else if (promiseComponent) {
promiseComponent.reject(new PubNubError('Validation failed, check status for details', createValidationError(validationResult)));
return promiseComponent.promise;
}
return;
}
let outgoingParams = endpoint.prepareParams(modules, incomingParams);
let url = decideURL(endpoint, modules, incomingParams);
let callInstance;
let networkingParams = { url,
operation: endpoint.getOperation(),
timeout: endpoint.getRequestTimeout(modules)
};
outgoingParams.uuid = config.UUID;
outgoingParams.pnsdk = generatePNSDK(config);
if (config.useInstanceId) {
outgoingParams.instanceid = config.instanceId;
}
if (config.useRequestId) {
outgoingParams.requestid = uuidGenerator.v4();
}
if (endpoint.isAuthSupported() && config.getAuthKey()) {
outgoingParams.auth = config.getAuthKey();
}
if (config.secretKey) {
signRequest(modules, url, outgoingParams);
}
let onResponse = (status: StatusAnnouncement, payload: Object) => {
if (status.error) {
if (callback) {
callback(status);
} else if (promiseComponent) {
promiseComponent.reject(new PubNubError('PubNub call failed, check status for details', status));
}
return;
}
let parsedPayload = endpoint.handleResponse(modules, payload, incomingParams);
if (callback) {
callback(status, parsedPayload);
} else if (promiseComponent) {
promiseComponent.fulfill(parsedPayload);
}
};
if (endpoint.usePost && endpoint.usePost(modules, incomingParams)) {
let payload = endpoint.postPayload(modules, incomingParams);
callInstance = networking.POST(outgoingParams, payload, networkingParams, onResponse);
} else {
callInstance = networking.GET(outgoingParams, networkingParams, onResponse);
}
if (endpoint.getOperation() === operationConstants.PNSubscribeOperation) {
return callInstance;
}
if (promiseComponent) {
return promiseComponent.promise;
}
}