Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 6x 3x 25x 7x 3x 7x 7x 1x 6x 6x 5x 1x 1x 4x 4x 2x 2x 2x 3x 6x 6x 5x 5x 1x 4x 1x 1x 3x 2x 2x 1x 1x 3x 4x 4x 3x 3x 3x 3x 2x 2x 2x 1x 1x 3x 3x 3x 2x 2x 3x 3x 2x 2x 2x 1x 1x | /*
Copyright (C) 2023 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { v4 as uuidv4 } from 'uuid';
import {
TAxios,
TCacheData,
TLastCachedResult,
TCacheMethod,
KEYS,
TManifestProps
} from '@front/storage/indexedDB/types/idb.types';
import idb, { TCache } from '@front/storage/indexedDB';
import env from './env';
let Cache: TCache | null;
const useIdbKey = (key: string | object): string => JSON.stringify(key);
const initCache = async(): Promise<void> => {
if (!Cache) {
Cache = (await idb)?.cache;
}
};
export const requestCacheInterceptor = async(
params: TAxios<AxiosRequestConfig>
): Promise<void> => {
const method = (params.method || 'GET').toUpperCase();
// This system is not designed to cache POST/PUT/etc. requests with body data.
if (method !== 'GET' && method !== 'HEAD' && !params.reRequest) {
return;
}
await initCache();
if (Cache) {
if (params.reRequest) {
params.method = 'GET';
params.reRequest = false;
} else {
const lastCachedResult: TCacheData = await Cache.getData(useIdbKey(params.url));
if (lastCachedResult && lastCachedResult.eTag) {
params.method = env.cache as TCacheMethod;
params.lastCachedResult = lastCachedResult;
params.headers = {
...params.headers,
'If-None-Match': lastCachedResult.eTag
};
}
}
}
};
export const responseCacheInterceptor = async(
response: TAxios<AxiosResponse>
): Promise<AxiosRequestConfig | void> => {
await initCache();
if (Cache) {
const method = (response.config.method || 'GET').toUpperCase();
if (method !== 'GET' && method !== 'HEAD') {
return;
}
if (
method === 'HEAD' &&
response.status === 200
) {
(response.config as TLastCachedResult).reRequest = true;
return response.config;
}
if (response.headers.etag) {
const newCachedData: TCacheData = {
id: useIdbKey(response.config.url),
eTag: response.headers.etag,
data: response.data
};
if ((response.config as TLastCachedResult).lastCachedResult) {
await Cache.putData(newCachedData);
} else {
await Cache.setData(newCachedData);
}
}
}
};
export const plantUmlCache: {
set(umlUrl: string, response: AxiosResponse): Promise<void>;
get(umlUri: string): Promise<TCacheData>
} = {
async get(umlUri: string): Promise<TCacheData | null> {
await initCache();
if (Cache) {
const cachedData: TCacheData = await Cache.getData(umlUri);
return cachedData?.id === umlUri ? cachedData : null;
}
},
async set(umlUri: string, response: AxiosResponse): Promise<void> {
await initCache();
if (Cache) {
const cachedData: TCacheData = await Cache.getData(umlUri);
const newCachedData: TCacheData = {
id: umlUri,
eTag: uuidv4(),
data: response.data
};
if (cachedData) {
await Cache.putData(newCachedData);
} else {
await Cache.setData(newCachedData);
}
}
}
};
export const manifestCache: {
set(manifest: TManifestProps): Promise<void>;
get(): Promise<TManifestProps | null>
} = {
async get(): Promise<TManifestProps | null> {
await initCache();
if (Cache) {
const cachedData: TCacheData = await Cache.getData(KEYS.Manifest);
return cachedData?.data ?? null;
}
},
async set(manifest: TManifestProps): Promise<void> {
await initCache();
if (Cache) {
const cachedData: TCacheData = await Cache.getData(KEYS.Manifest);
const newCachedData: TCacheData = {
id: KEYS.Manifest,
eTag: uuidv4(),
data: manifest
};
if (cachedData) {
await Cache.putData(newCachedData);
} else {
await Cache.setData(newCachedData);
}
}
}
};
|