Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import axios, { AxiosAdapter, AxiosResponse } from "axios";
- import { createHash } from "crypto";
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
- function str2Sha512(data: string) {
- const hash = createHash('sha512')
- hash.update(data)
- return hash.digest('hex');
- }
- export const mfockAdapter: AxiosAdapter = async (config) => {
- const sha512 = str2Sha512(`${config.url} ${config.method}`);
- const dir = `./.cache/`;
- mkdirSync(dir, {
- recursive: true
- })
- const cacheFilePath = `${dir}/${sha512}.json`;
- if (existsSync(cacheFilePath)) {
- const cachedResponse: AxiosResponse = JSON.parse(readFileSync(cacheFilePath).toString('UTF-8'));
- return Promise.resolve(cachedResponse);
- } else {
- config.adapter = undefined; // disabled for calling own infinity
- const response = await axios.request(config)
- writeFileSync(cacheFilePath, JSON.stringify({
- status: response.status,
- statusText: response.statusText,
- config: response.config,
- data: response.data,
- headers: response.headers,
- request: undefined//response.request
- }))
- return response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement