scratch-storage/src/ProxyTool.ts

106 lines
2.7 KiB
TypeScript
Raw Normal View History

import FetchWorkerTool from './FetchWorkerTool';
import {FetchTool} from './FetchTool';
/**
* @typedef {object} Request
* @property {string} url
* @property {*} body
* @property {string} method
* @property {boolean} withCredentials
*/
/**
* Get and send assets with other tools in sequence.
*/
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'
};
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()];
} else {
2019-11-11 17:51:35 -05:00
tools = [new FetchWorkerTool(), new FetchTool()];
}
/**
* 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?
*/
get isGetSupported () {
return this.tools.some(tool => tool.isGetSupported);
}
/**
* Request data from with one of the proxied tools.
* @param {Request} reqConfig - Request configuration for data to get.
* @returns {Promise.<Buffer>} Resolve to Buffer of data from server.
*/
get (reqConfig) {
let toolIndex = 0;
const nextTool = (err?) => {
const tool = this.tools[toolIndex++];
if (!tool) {
throw err;
}
if (!tool.isGetSupported) {
return nextTool(err);
}
return tool.get(reqConfig).catch(nextTool);
};
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?
*/
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.
* @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.
*/
send (reqConfig) {
2019-01-23 17:00:50 -05:00
let toolIndex = 0;
const nextTool = (err?) => {
2019-01-23 17:00:50 -05:00
const tool = this.tools[toolIndex++];
if (!tool) {
throw err;
}
if (!tool.isSendSupported) {
return nextTool(err);
}
return tool.send(reqConfig).catch(nextTool);
2019-01-23 17:00:50 -05:00
};
return nextTool();
}
}