2018-07-15 00:19:08 +03:00
|
|
|
'use strict';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
import { XMLHttpRequest } from 'xmlhttprequest';
|
2018-07-15 00:19:08 +03:00
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
import { encode as base64Encode } from './base64';
|
2018-07-15 00:19:08 +03:00
|
|
|
import { toUtf8Bytes } from './utf8';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-09-24 22:55:17 +03:00
|
|
|
import * as errors from '../errors';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
|
|
|
|
// Exported Types
|
|
|
|
export type ConnectionInfo = {
|
|
|
|
url: string,
|
|
|
|
user?: string,
|
|
|
|
password?: string,
|
2018-09-06 23:34:45 +03:00
|
|
|
allowInsecure?: boolean,
|
|
|
|
timeout?: number,
|
|
|
|
headers?: { [key: string]: string | number }
|
2018-07-31 01:59:52 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export interface OnceBlockable {
|
|
|
|
once(eventName: "block", handler: () => void): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PollOptions = {
|
|
|
|
timeout?: number,
|
|
|
|
floor?: number,
|
|
|
|
ceiling?: number,
|
|
|
|
interval?: number,
|
|
|
|
onceBlock?: OnceBlockable
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
type Header = { key: string, value: string };
|
|
|
|
|
2018-07-15 00:19:08 +03:00
|
|
|
export function fetchJson(connection: string | ConnectionInfo, json: string, processFunc: (value: any) => any): Promise<any> {
|
2018-09-06 23:34:45 +03:00
|
|
|
let headers: { [key: string]: Header } = { };
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
let url: string = null;
|
|
|
|
|
2018-09-06 23:34:45 +03:00
|
|
|
let timeout = 2 * 60 * 1000;
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
if (typeof(connection) === 'string') {
|
|
|
|
url = connection;
|
|
|
|
|
|
|
|
} else if (typeof(connection) === 'object') {
|
|
|
|
if (connection.url == null) {
|
2018-06-13 22:39:39 +03:00
|
|
|
errors.throwError('missing URL', errors.MISSING_ARGUMENT, { arg: 'url' });
|
|
|
|
}
|
2018-06-23 03:30:50 +03:00
|
|
|
|
|
|
|
url = connection.url;
|
|
|
|
|
2018-09-06 23:34:45 +03:00
|
|
|
if (typeof(connection.timeout) === 'number' && connection.timeout > 0) {
|
|
|
|
timeout = connection.timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection.headers) {
|
|
|
|
for (let key in connection.headers) {
|
|
|
|
headers[key.toLowerCase()] = { key: key, value: String(connection.headers[key]) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
if (connection.user != null && connection.password != null) {
|
|
|
|
if (url.substring(0, 6) !== 'https:' && connection.allowInsecure !== true) {
|
2018-06-13 22:39:39 +03:00
|
|
|
errors.throwError(
|
2018-09-06 23:34:45 +03:00
|
|
|
'basic authentication requires a secure https url',
|
2018-06-13 22:39:39 +03:00
|
|
|
errors.INVALID_ARGUMENT,
|
2018-06-23 03:30:50 +03:00
|
|
|
{ arg: 'url', url: url, user: connection.user, password: '[REDACTED]' }
|
2018-06-13 22:39:39 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:34:45 +03:00
|
|
|
let authorization = connection.user + ':' + connection.password;
|
|
|
|
headers['authorization'] = {
|
2018-06-13 22:39:39 +03:00
|
|
|
key: 'Authorization',
|
|
|
|
value: 'Basic ' + base64Encode(toUtf8Bytes(authorization))
|
2018-09-06 23:34:45 +03:00
|
|
|
};
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
2018-09-06 23:34:45 +03:00
|
|
|
let request = new XMLHttpRequest();
|
|
|
|
|
|
|
|
let timer: any = null;
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
if (timer == null) { return; }
|
|
|
|
timer = null;
|
|
|
|
|
|
|
|
reject(new Error('timeout'));
|
|
|
|
setTimeout(() => {
|
|
|
|
request.abort();
|
|
|
|
}, 0);
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
let cancelTimeout = () => {
|
|
|
|
if (timer == null) { return; }
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
}
|
2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
if (json) {
|
|
|
|
request.open('POST', url, true);
|
2018-09-06 23:34:45 +03:00
|
|
|
headers['content-type'] = { key: 'Content-Type', value: 'application/json' };
|
2018-06-13 22:39:39 +03:00
|
|
|
} else {
|
|
|
|
request.open('GET', url, true);
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:34:45 +03:00
|
|
|
Object.keys(headers).forEach((key) => {
|
|
|
|
let header = headers[key];
|
2018-06-13 22:39:39 +03:00
|
|
|
request.setRequestHeader(header.key, header.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
request.onreadystatechange = function() {
|
|
|
|
if (request.readyState !== 4) { return; }
|
|
|
|
|
2018-09-20 19:44:46 +03:00
|
|
|
if (request.status != 200) {
|
|
|
|
cancelTimeout();
|
|
|
|
// @TODO: not any!
|
|
|
|
let error: any = new Error('invalid response - ' + request.status);
|
|
|
|
error.statusCode = request.status;
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:34:45 +03:00
|
|
|
let result: any = null;
|
2018-06-13 22:39:39 +03:00
|
|
|
try {
|
2018-09-06 23:34:45 +03:00
|
|
|
result = JSON.parse(request.responseText);
|
2018-06-13 22:39:39 +03:00
|
|
|
} catch (error) {
|
2018-09-06 23:34:45 +03:00
|
|
|
cancelTimeout();
|
2018-06-13 22:39:39 +03:00
|
|
|
// @TODO: not any!
|
2018-09-06 23:34:45 +03:00
|
|
|
let jsonError: any = new Error('invalid json response');
|
2018-06-13 22:39:39 +03:00
|
|
|
jsonError.orginialError = error;
|
|
|
|
jsonError.responseText = request.responseText;
|
|
|
|
jsonError.url = url;
|
|
|
|
reject(jsonError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (processFunc) {
|
|
|
|
try {
|
|
|
|
result = processFunc(result);
|
|
|
|
} catch (error) {
|
2018-09-06 23:34:45 +03:00
|
|
|
cancelTimeout();
|
2018-06-13 22:39:39 +03:00
|
|
|
error.url = url;
|
|
|
|
error.body = json;
|
|
|
|
error.responseText = request.responseText;
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:34:45 +03:00
|
|
|
cancelTimeout();
|
2018-06-13 22:39:39 +03:00
|
|
|
resolve(result);
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onerror = function(error) {
|
2018-09-06 23:34:45 +03:00
|
|
|
cancelTimeout();
|
2018-06-13 22:39:39 +03:00
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (json) {
|
|
|
|
request.send(json);
|
|
|
|
} else {
|
|
|
|
request.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
2018-09-06 23:34:45 +03:00
|
|
|
cancelTimeout();
|
2018-06-13 22:39:39 +03:00
|
|
|
// @TODO: not any!
|
2018-09-06 23:34:45 +03:00
|
|
|
let connectionError: any = new Error('connection error');
|
2018-06-13 22:39:39 +03:00
|
|
|
connectionError.error = error;
|
|
|
|
reject(connectionError);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-01 06:05:22 +03:00
|
|
|
export function poll(func: () => Promise<any>, options?: PollOptions): Promise<any> {
|
|
|
|
if (!options) { options = {}; }
|
|
|
|
if (options.floor == null) { options.floor = 0; }
|
|
|
|
if (options.ceiling == null) { options.ceiling = 10000; }
|
|
|
|
if (options.interval == null) { options.interval = 250; }
|
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
|
|
|
|
let timer: any = null;
|
|
|
|
let done: boolean = false;
|
|
|
|
|
|
|
|
// Returns true if cancel was successful. Unsuccessful cancel means we're already done.
|
|
|
|
let cancel = (): boolean => {
|
|
|
|
if (done) { return false; }
|
|
|
|
done = true;
|
|
|
|
if (timer) { clearTimeout(timer); }
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.timeout) {
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
if (cancel()) { reject(new Error('timeout')); }
|
|
|
|
}, options.timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
let attempt = 0;
|
|
|
|
function check() {
|
2018-07-03 23:44:05 +03:00
|
|
|
return func().then(function(result) {
|
2018-07-01 06:05:22 +03:00
|
|
|
|
|
|
|
// If we have a result, or are allowed null then we're done
|
|
|
|
if (result !== undefined) {
|
|
|
|
if (cancel()) { resolve(result); }
|
|
|
|
|
2018-07-03 22:48:37 +03:00
|
|
|
} else if (options.onceBlock) {
|
|
|
|
options.onceBlock.once('block', check);
|
|
|
|
|
2018-07-01 06:05:22 +03:00
|
|
|
// Otherwise, exponential back-off (up to 10s) our next request
|
|
|
|
} else if (!done) {
|
|
|
|
attempt++;
|
|
|
|
|
|
|
|
let timeout = options.interval * parseInt(String(Math.random() * Math.pow(2, attempt)));
|
|
|
|
if (timeout < options.floor) { timeout = options.floor; }
|
|
|
|
if (timeout > options.ceiling) { timeout = options.ceiling; }
|
|
|
|
|
|
|
|
setTimeout(check, timeout);
|
|
|
|
}
|
2018-07-03 23:44:05 +03:00
|
|
|
|
|
|
|
return null;
|
2018-07-01 06:05:22 +03:00
|
|
|
}, function(error) {
|
|
|
|
if (cancel()) { reject(error); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
check();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|