<commit_before>'use strict';

import { SerializedHttpRequest } from './models/httpRequest';
import { EnvironmentPickItem } from './models/environmentPickItem';
import * as Constants from './constants';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';

export class PersistUtility {
    public static readonly historyFilePath: string = path.join(os.homedir(), Constants.ExtensionFolderName, Constants.HistoryFileName);
    public static readonly cookieFilePath: string = path.join(os.homedir(), Constants.ExtensionFolderName, Constants.CookieFileName);
    public static readonly environmentFilePath: string = path.join(os.homedir(), Constants.ExtensionFolderName, Constants.EnvironmentFileName);
    private static emptyHttpRequestItems: SerializedHttpRequest[] = [];

    public static async saveRequest(httpRequest: SerializedHttpRequest) {
        try {
            let requests = await PersistUtility.deserializeFromHistoryFile();
            requests.unshift(httpRequest);
            requests = requests.slice(0, Constants.HistoryItemsMaxCount);
            await PersistUtility.serializeToHistoryFile(requests);
        } catch (error) {
        }
    }

    public static loadRequests(): Promise<SerializedHttpRequest[]> {
        return PersistUtility.deserializeFromHistoryFile();
    }

    public static async saveEnvironment(environment: EnvironmentPickItem) {
        await PersistUtility.createFileIfNotExistsAsync(PersistUtility.environmentFilePath);
        await PersistUtility.serializeToEnvironmentFile(environment);
    }

    public static loadEnvironment(): Promise<EnvironmentPickItem> {
        return PersistUtility.deserializeFromEnvironmentFile();
    }

    public static createFileIfNotExists(path: string) {
        try {
            fs.statSync(path);
        } catch (error) {
            PersistUtility.ensureDirectoryExistence(path);
            fs.writeFileSync(path, '');
        }
    }

    public static async createFileIfNotExistsAsync(path: string) {
        return new Promise<void>((resolve, reject) => {
            fs.stat(path, err => {
                if(err === null) {
                    resolve();
                }

                new Promise<string>((resolve, reject) => {
                    PersistUtility.ensureDirectoryExistence(path);
                    fs.writeFile(path, '', err => err === null ? resolve(path) : reject(err))
                }).then(_ => resolve());
            });
        });
    }

    public static async serializeToEnvironmentFile(environment: EnvironmentPickItem) {
        return new Promise<void>((resolve, reject) => {
            fs.writeFile(PersistUtility.environmentFilePath, JSON.stringify(environment), error => {
                if (error) {
                    reject(error);
                    return;
                }

                resolve();
            })
        });
    }

    private static async deserializeFromEnvironmentFile(): Promise<EnvironmentPickItem> {
        return new Promise<EnvironmentPickItem>((resolve, reject) => {
            fs.readFile(PersistUtility.environmentFilePath, (error, data) => {
                if (error) {
                    PersistUtility.createFileIfNotExists(PersistUtility.environmentFilePath);
                } else {
                    let fileContent = data.toString();
                    if (fileContent) {
                        resolve(JSON.parse(fileContent));
                        return;
                    }
                }
                resolve(null);
            })
        });
    }

    public static async serializeToHistoryFile(requests: SerializedHttpRequest[]) {
        return new Promise<void>((resolve, reject) => {
            fs.writeFile(PersistUtility.historyFilePath, JSON.stringify(requests), error => {
                if (error) {
                    reject(error);
                    return;
                }

                resolve();
            })
        });
    }

    private static async deserializeFromHistoryFile(): Promise<SerializedHttpRequest[]> {
        return new Promise<SerializedHttpRequest[]>((resolve, reject) => {
            fs.readFile(PersistUtility.historyFilePath, (error, data) => {
                if (error) {
                    PersistUtility.createFileIfNotExists(PersistUtility.historyFilePath);
                } else {
                    let fileContent = data.toString();
                    if (fileContent) {
                        resolve(JSON.parse(fileContent));
                        return;<commit_msg>allow deserialize corrupted file<commit_after>'use strict';

import { SerializedHttpRequest } from './models/httpRequest';
import { EnvironmentPickItem } from './models/environmentPickItem';
import * as Constants from './constants';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';

export class PersistUtility {
    public static readonly historyFilePath: string = path.join(os.homedir(), Constants.ExtensionFolderName, Constants.HistoryFileName);
    public static readonly cookieFilePath: string = path.join(os.homedir(), Constants.ExtensionFolderName, Constants.CookieFileName);
    public static readonly environmentFilePath: string = path.join(os.homedir(), Constants.ExtensionFolderName, Constants.EnvironmentFileName);
    private static emptyHttpRequestItems: SerializedHttpRequest[] = [];

    public static async saveRequest(httpRequest: SerializedHttpRequest) {
        try {
            let requests = await PersistUtility.deserializeFromHistoryFile();
            requests.unshift(httpRequest);
            requests = requests.slice(0, Constants.HistoryItemsMaxCount);
            await PersistUtility.serializeToHistoryFile(requests);
        } catch (error) {
        }
    }

    public static loadRequests(): Promise<SerializedHttpRequest[]> {
        return PersistUtility.deserializeFromHistoryFile();
    }

    public static async saveEnvironment(environment: EnvironmentPickItem) {
        await PersistUtility.createFileIfNotExistsAsync(PersistUtility.environmentFilePath);
        await PersistUtility.serializeToEnvironmentFile(environment);
    }

    public static loadEnvironment(): Promise<EnvironmentPickItem> {
        return PersistUtility.deserializeFromEnvironmentFile();
    }

    public static createFileIfNotExists(path: string) {
        try {
            fs.statSync(path);
        } catch (error) {
            PersistUtility.ensureDirectoryExistence(path);
            fs.writeFileSync(path, '');
        }
    }

    public static async createFileIfNotExistsAsync(path: string) {
        return new Promise<void>((resolve, reject) => {
            fs.stat(path, err => {
                if(err === null) {
                    resolve();
                }

                new Promise<string>((resolve, reject) => {
                    PersistUtility.ensureDirectoryExistence(path);
                    fs.writeFile(path, '', err => err === null ? resolve(path) : reject(err))
                }).then(_ => resolve());
            });
        });
    }

    public static async serializeToEnvironmentFile(environment: EnvironmentPickItem) {
        return new Promise<void>((resolve, reject) => {
            fs.writeFile(PersistUtility.environmentFilePath, JSON.stringify(environment), error => {
                if (error) {
                    reject(error);
                    return;
                }

                resolve();
            })
        });
    }

    private static async deserializeFromEnvironmentFile(): Promise<EnvironmentPickItem> {
        return new Promise<EnvironmentPickItem>((resolve, reject) => {
            fs.readFile(PersistUtility.environmentFilePath, (error, data) => {
                if (error) {
                    PersistUtility.createFileIfNotExists(PersistUtility.environmentFilePath);
                } else {
                    let fileContent = data.toString();
                    if (fileContent) {
                        resolve(JSON.parse(fileContent));
                        return;
                    }
                }
                resolve(null);
            })
        });
    }

    public static async serializeToHistoryFile(requests: SerializedHttpRequest[]) {
        return new Promise<void>((resolve, reject) => {
            fs.writeFile(PersistUtility.historyFilePath, JSON.stringify(requests), error => {
                if (error) {
                    reject(error);
                    return;
                }

                resolve();
            })
        });
    }

    private static async deserializeFromHistoryFile(): Promise<SerializedHttpRequest[]> {
        return new Promise<SerializedHttpRequest[]>((resolve, reject) => {
            fs.readFile(PersistUtility.historyFilePath, (error, data) => {
                if (error) {
                    PersistUtility.createFileIfNotExists(PersistUtility.historyFilePath);
                } else {
                    let fileContent = data.toString();
                    if (fileContent) {
                        try {
                            resolve(JSON.parse(fileContent));
                            return;
                        } catch (error) {
                        }
{'fraction_non_alphanumeric': 0.09938557723401961, 'fraction_numerical': 0.00021558693543171284, 'mean_word_length': 2.0957624290957626, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '315865', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2164, 'n_words': 439}