2020-12-16 09:48:50 +00:00
|
|
|
import { PendingQueryItem } from '@cyberalien/redundancy';
|
2020-04-28 09:47:35 +00:00
|
|
|
import {
|
|
|
|
APIQueryParams,
|
|
|
|
IconifyAPIPrepareQuery,
|
|
|
|
IconifyAPISendQuery,
|
|
|
|
} from '@iconify/core/lib/api/modules';
|
|
|
|
import { IconifyJSON } from '@iconify/types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fake data entry
|
|
|
|
*/
|
|
|
|
export interface FakeData {
|
|
|
|
icons: string[];
|
|
|
|
host?: string; // host to respond to
|
|
|
|
delay?: number; // 0 = instant reply
|
|
|
|
data: IconifyJSON; // data to send
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fake data storage
|
|
|
|
*/
|
2020-05-29 19:08:45 +00:00
|
|
|
const fakeData: Record<string, Record<string, FakeData[]>> = Object.create(
|
|
|
|
null
|
|
|
|
);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2020-05-29 19:08:45 +00:00
|
|
|
export function setFakeData(
|
|
|
|
provider: string,
|
|
|
|
prefix: string,
|
|
|
|
item: FakeData
|
|
|
|
): void {
|
|
|
|
if (fakeData[provider] === void 0) {
|
|
|
|
fakeData[provider] = Object.create(null);
|
|
|
|
}
|
|
|
|
const providerFakeData = fakeData[provider];
|
|
|
|
if (providerFakeData[prefix] === void 0) {
|
|
|
|
providerFakeData[prefix] = [];
|
2020-04-28 09:47:35 +00:00
|
|
|
}
|
2020-05-29 19:08:45 +00:00
|
|
|
providerFakeData[prefix].push(item);
|
2020-04-28 09:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface FakeAPIQueryParams extends APIQueryParams {
|
|
|
|
data: FakeData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare params
|
|
|
|
*/
|
|
|
|
export const prepareQuery: IconifyAPIPrepareQuery = (
|
2020-05-29 19:08:45 +00:00
|
|
|
provider: string,
|
2020-04-28 09:47:35 +00:00
|
|
|
prefix: string,
|
|
|
|
icons: string[]
|
|
|
|
): APIQueryParams[] => {
|
|
|
|
// Find items that have query
|
|
|
|
const items: APIQueryParams[] = [];
|
|
|
|
let missing = icons.slice(0);
|
|
|
|
|
2020-05-29 19:08:45 +00:00
|
|
|
if (fakeData[provider] === void 0) {
|
|
|
|
fakeData[provider] = Object.create(null);
|
|
|
|
}
|
|
|
|
const providerFakeData = fakeData[provider];
|
|
|
|
|
|
|
|
if (providerFakeData[prefix] !== void 0) {
|
|
|
|
providerFakeData[prefix].forEach((item) => {
|
2020-04-28 09:47:35 +00:00
|
|
|
const matches = item.icons.filter(
|
2020-05-29 19:08:45 +00:00
|
|
|
(icon) => missing.indexOf(icon) !== -1
|
2020-04-28 09:47:35 +00:00
|
|
|
);
|
|
|
|
if (!matches.length) {
|
|
|
|
// No match
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains at least one matching icon
|
2020-05-29 19:08:45 +00:00
|
|
|
missing = missing.filter((icon) => matches.indexOf(icon) === -1);
|
2020-04-28 09:47:35 +00:00
|
|
|
const query: FakeAPIQueryParams = {
|
2020-05-29 19:08:45 +00:00
|
|
|
provider,
|
2020-04-28 09:47:35 +00:00
|
|
|
prefix,
|
|
|
|
icons: matches,
|
|
|
|
data: item,
|
|
|
|
};
|
|
|
|
items.push(query);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load icons
|
|
|
|
*/
|
|
|
|
export const sendQuery: IconifyAPISendQuery = (
|
|
|
|
host: string,
|
|
|
|
params: APIQueryParams,
|
2021-01-12 12:04:19 +00:00
|
|
|
status: PendingQueryItem
|
2020-04-28 09:47:35 +00:00
|
|
|
): void => {
|
2020-05-29 19:08:45 +00:00
|
|
|
const provider = params.provider;
|
2020-04-28 09:47:35 +00:00
|
|
|
const prefix = params.prefix;
|
|
|
|
const icons = params.icons;
|
|
|
|
|
|
|
|
const data = (params as FakeAPIQueryParams).data;
|
|
|
|
if (!data) {
|
|
|
|
throw new Error('Fake data is missing in query params');
|
|
|
|
}
|
|
|
|
if (typeof data.host === 'string' && data.host !== host) {
|
2020-12-16 09:48:50 +00:00
|
|
|
// Host mismatch - send error (first parameter = undefined)
|
2021-01-12 12:04:19 +00:00
|
|
|
status.done(void 0, 404);
|
2020-04-28 09:47:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sendResponse = () => {
|
2020-05-29 19:08:45 +00:00
|
|
|
console.log(
|
|
|
|
'Sending data for prefix "' +
|
|
|
|
(provider === '' ? '' : '@' + provider + ':') +
|
|
|
|
prefix +
|
|
|
|
'", icons:',
|
|
|
|
icons
|
|
|
|
);
|
2021-01-12 12:04:19 +00:00
|
|
|
status.done(data.data);
|
2020-04-28 09:47:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!data.delay) {
|
|
|
|
sendResponse();
|
|
|
|
} else {
|
|
|
|
setTimeout(sendResponse, data.delay);
|
|
|
|
}
|
|
|
|
};
|