2024-10-10 11:45:50 +03:00
|
|
|
import FetchWorkerTool from './FetchWorkerTool';
|
|
|
|
import {FetchTool} from './FetchTool';
|
2019-01-23 17:00:06 -05:00
|
|
|
|
2019-04-19 13:11:39 -04:00
|
|
|
/**
|
|
|
|
* @typedef {object} Request
|
|
|
|
* @property {string} url
|
|
|
|
* @property {*} body
|
|
|
|
* @property {string} method
|
|
|
|
* @property {boolean} withCredentials
|
|
|
|
*/
|
|
|
|
|
2019-01-23 17:00:06 -05:00
|
|
|
/**
|
|
|
|
* Get and send assets with other tools in sequence.
|
|
|
|
*/
|
2024-10-10 11:45:50 +03:00
|
|
|
export default class ProxyTool {
|
|
|
|
// TODO: Typing
|
|
|
|
public tools: any[];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constant values that filter the set of tools in a ProxyTool instance.
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
public static TOOL_FILTER = {
|
|
|
|
/**
|
|
|
|
* Use all tools.
|
|
|
|
*/
|
|
|
|
ALL: 'all',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use tools that are ready right now.
|
|
|
|
*/
|
|
|
|
READY: 'ready'
|
|
|
|
};
|
|
|
|
|
2019-01-23 17:00:06 -05:00
|
|
|
constructor (filter = ProxyTool.TOOL_FILTER.ALL) {
|
|
|
|
let tools;
|
|
|
|
if (filter === ProxyTool.TOOL_FILTER.READY) {
|
2019-11-11 17:51:35 -05:00
|
|
|
tools = [new FetchTool()];
|
2019-01-23 17:00:06 -05:00
|
|
|
} else {
|
2019-11-11 17:51:35 -05:00
|
|
|
tools = [new FetchWorkerTool(), new FetchTool()];
|
2019-01-23 17:00:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sequence of tools to proxy.
|
|
|
|
* @type {Array.<Tool>}
|
|
|
|
*/
|
|
|
|
this.tools = tools;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is get supported? false if all proxied tool return false.
|
|
|
|
* @returns {boolean} Is get supported?
|
|
|
|
*/
|
2019-03-26 18:03:24 -04:00
|
|
|
get isGetSupported () {
|
|
|
|
return this.tools.some(tool => tool.isGetSupported);
|
2019-01-23 17:00:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request data from with one of the proxied tools.
|
2019-04-19 13:11:39 -04:00
|
|
|
* @param {Request} reqConfig - Request configuration for data to get.
|
2019-01-23 17:00:06 -05:00
|
|
|
* @returns {Promise.<Buffer>} Resolve to Buffer of data from server.
|
|
|
|
*/
|
2019-04-19 13:11:39 -04:00
|
|
|
get (reqConfig) {
|
2019-01-23 17:00:06 -05:00
|
|
|
let toolIndex = 0;
|
2024-10-10 11:45:50 +03:00
|
|
|
const nextTool = (err?) => {
|
2019-01-23 17:00:06 -05:00
|
|
|
const tool = this.tools[toolIndex++];
|
|
|
|
if (!tool) {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-03-26 18:03:24 -04:00
|
|
|
if (!tool.isGetSupported) {
|
|
|
|
return nextTool(err);
|
|
|
|
}
|
2019-04-19 13:11:39 -04:00
|
|
|
return tool.get(reqConfig).catch(nextTool);
|
2019-01-23 17:00:06 -05:00
|
|
|
};
|
|
|
|
return nextTool();
|
|
|
|
}
|
2019-01-23 17:00:50 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is sending supported? false if all proxied tool return false.
|
|
|
|
* @returns {boolean} Is sending supported?
|
|
|
|
*/
|
2019-03-26 18:03:24 -04:00
|
|
|
get isSendSupported () {
|
|
|
|
return this.tools.some(tool => tool.isSendSupported);
|
2019-01-23 17:00:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send data to a server with one of the proxied tools.
|
2019-04-19 13:11:39 -04:00
|
|
|
* @param {Request} reqConfig - Request configuration for data to send.
|
2019-01-23 17:00:50 -05:00
|
|
|
* @returns {Promise.<Buffer|string|object>} Server returned metadata.
|
|
|
|
*/
|
2019-04-19 13:11:39 -04:00
|
|
|
send (reqConfig) {
|
2019-01-23 17:00:50 -05:00
|
|
|
let toolIndex = 0;
|
2024-10-10 11:45:50 +03:00
|
|
|
const nextTool = (err?) => {
|
2019-01-23 17:00:50 -05:00
|
|
|
const tool = this.tools[toolIndex++];
|
|
|
|
if (!tool) {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-03-26 18:03:24 -04:00
|
|
|
if (!tool.isSendSupported) {
|
|
|
|
return nextTool(err);
|
|
|
|
}
|
2019-04-19 13:11:39 -04:00
|
|
|
return tool.send(reqConfig).catch(nextTool);
|
2019-01-23 17:00:50 -05:00
|
|
|
};
|
|
|
|
return nextTool();
|
|
|
|
}
|
2019-01-23 17:00:06 -05:00
|
|
|
}
|