import * as tough from 'tough-cookie'; import * as util from 'util'; import type { CookiesIndex } from './cookies-index.js'; export declare enum FileFormat { json = "json", txt = "txt" } export declare const DefaultFileFormat = FileFormat.json; export type FileCookieStoreOptions = { async?: boolean; loadAsync?: boolean; fileFormat?: FileFormat; forceParse?: boolean; httpOnlyExtension?: boolean; onLoad?: (exists: boolean) => void; onLoadLineError?: (line: string, lineNumber: number) => void; onLoadError?: (err: Error) => void; }; /** * Class representing a JSON file store. * * @augments Store */ export default class FileCookieStore extends tough.Store { synchronous: boolean; filePath: string; fileFormat?: FileFormat; idx: CookiesIndex; httpOnlyExtension: boolean; private _readPromise; private _writePromise; private _nextWritePromise; /** * Creates a new JSON file store in the specified file. * * @param {string} filePath - The file in which the store will be created. * @param {object} options - Options for initializing the store. * @param {boolean} options.async - Whether to write the file asynchronously. * @param {boolean} options.loadAsync - Whether to read the file asynchronously. * @param {FileFormat} options.fileFormat - The format of the file. If not specified, the format will be auto-detected. If not specified and the file doesn't exist, the format will default to txt. * @param {boolean} options.forceParse - Continue parse file and don't throw exception if a bad line was found. This only applies to the txt format. * @param {boolean} options.httpOnlyExtension - Detect the #HttpOnly_ prefix for http only cookies. Defaults to true. * @param {Function} options.onLoad - Called when the file successfuly loads asynchronously. Unused if `loadAsync` is false. * @param {Function} options.onLoadError - Optional callback for any async file-load error. Unused if `loadAsync` is false. */ constructor(filePath: string, options?: FileCookieStoreOptions); /** * Waits for the initial load to finish if unfinished, and then performs the given synchronous read action. * Afterwards, the callback will be called with an error or a result. If no callback is passed, a promise * will be returned instead. * @param {Function} action - The synchronous read action to execute * @param {Function} cb - The callback to call with the error or result * @returns {Promise} a promise if no callback was passed. */ private _doSyncReadAsAsync; /** * Waits for the initial load to finish if unfinished, and then performs the given synchronous write action. * Afterwards, if the store has changed, then changes to the store will be saved to its file, and then * the callback will be called with an error if any, or `null` if no error. If no callback is passed, a * promise will be returned instead. * @param {Function} action - The synchronous write action to execute. This should return a boolean indicating whether the store has changed. * @param {Function} cb - The callback to call with the error or result * @returns {Promise} a promise if no callback was passed. */ _doSyncWriteAsAsync(action: () => boolean, cb: (((error: Error | null) => void) | undefined)): (void | Promise); /** @inheritdoc */ findCookie(domain: tough.Nullable, path: tough.Nullable, key: tough.Nullable, cb: tough.Callback): void; /** @inheritdoc */ findCookie(domain: tough.Nullable, path: tough.Nullable, key: tough.Nullable): Promise; /** * Searches for a cookie after waiting for the initial read to finish. * @see _doSyncReadAsAsync * @param {string} domain - The cookie domain. * @param {string} path - The cookie path. * @param {string} key - The cookie key. * @param {Function} cb - The callback that will be called with the result. * @returns {Promise} a promise if no callback was passed. */ private _findCookieAsync; /** * Searches for a cookie and returns it or null. * @param {string} domain - The cookie domain. * @param {string} path - The cookie path. * @param {string} key - The cookie key. * @returns {Cookie} the matching cookie if found. */ private _findCookieSync; /** @inheritdoc */ findCookies(domain: tough.Nullable, path: tough.Nullable, allowSpecialUseDomain?: boolean, cb?: tough.Callback): void; /** @inheritdoc */ findCookies(domain: tough.Nullable, path: tough.Nullable, allowSpecialUseDomain?: boolean): Promise; /** * Searches for cookies after waiting for the initial read to finish * @see _doSyncReadAsAsync * @param {string} domain - The cookies domain. * @param {string} path - The cookies path. * @param {boolean} allowSpecialUseDomain - If `true` then special-use domain suffixes will be allowed in matches. Defaults to `false`. * @param {Function} cb - The callback that will be called with the result. * @returns {Promise} a promise if no callback was passed. */ private _findCookiesAsync; /** * Searches for matching cookies and returns them. * @param {string} domain - The cookies domain. * @param {string} path - The cookies path. * @param {boolean} allowSpecialUseDomain - If `true` then special-use domain suffixes will be allowed in matches. Defaults to `false`. * @returns {Cookie[]} the matching cookies if any were found. */ private _findCookiesSync; /** @inheritdoc */ putCookie(cookie: tough.Cookie, cb: tough.ErrorCallback): void; /** @inheritdoc */ putCookie(cookie: tough.Cookie): Promise; /** * Puts a cookie in the store after waiting for the initial read to finish, then saves the store to its file. * @see _doSyncReadAsAsync * @param {Cookie} cookie - The cookie to add to the store. * @param {Function} cb - The callback to be called when finished. * @returns {Promise} a promise if no callback was passed. */ private _putCookieAsync; /** * Puts a cookie in the store without saving to a file. * @param {Cookie} cookie - The cookie to add to the store. * @returns {boolean} true if the store was changed, or false if the store was not changed. */ private _putCookieSyncInternal; /** * Puts a cookie in the store, then saves synchronously. * @param {Cookie} cookie - The cookie to add to the store. */ private _putCookieSync; /** @inheritdoc */ updateCookie(oldCookie: tough.Cookie, newCookie: tough.Cookie, cb: tough.ErrorCallback): void; /** @inheritdoc */ updateCookie(oldCookie: tough.Cookie, newCookie: tough.Cookie): Promise; /** @inheritdoc */ removeCookie(domain: string, path: string, key: string, cb: tough.ErrorCallback): void; /** @inheritdoc */ removeCookie(domain: string, path: string, key: string): Promise; /** * Removes a cookie from the store after waiting for the initial read to finish, then saves the store to its file if removed. * @see _doSyncReadAsAsync * @param {string} domain - The domain of the cookie to remove. * @param {string} path - The path of the cookie to remove. * @param {string} key - The key of the cookie to remove. * @param {Function} cb - The callback to be called when finished. * @returns {Promise} a promise if no callback was passed. */ private _removeCookieAsync; /** * Removes a cookie from the store without saving to a file. * @param {string} domain - The domain of the cookie to remove. * @param {string} path - The path of the cookie to remove. * @param {string} key - The key of the cookie to remove. * @returns {boolean} true if a cookie was removed, or false if no change occured. */ private _removeCookieSyncInternal; /** * Removes a cookie from the store, then saves synchronously if removed. * @param {string} domain - The domain of the cookie to remove. * @param {string} path - The path of the cookie to remove. * @param {string} key - The key of the cookie to remove. */ private _removeCookieSync; /** @inheritdoc */ removeCookies(domain: string, path: tough.Nullable, cb: tough.ErrorCallback): void; /** @inheritdoc */ removeCookies(domain: string, path: tough.Nullable): Promise; /** * Removes cookies from the store after waiting for the initial read to finish, then saves the store to its file if any were removed. * @see _doSyncReadAsAsync * @param {string} domain - The domain of the cookies to remove. * @param {string} path - The path of the cookies to remove. * @param {Function} cb - The callback to be called when finished. * @returns {Promise} a promise if no callback was passed. */ private _removeCookiesAsync; /** * Removes cookies from the store without saving to a file. * @param {string} domain - The domain of the cookies to remove. * @param {string} path - The path of the cookies to remove. * @returns {boolean} true if any cookies were removed, or false if no change occured */ private _removeCookiesSyncInternal; /** * Removes cookies from the store, then saves synchronously if any were removed. * @param {string} domain - The domain of the cookies to remove. * @param {string} path - The path of the cookies to remove. */ private _removeCookiesSync; /** @inheritdoc */ removeAllCookies(cb: tough.ErrorCallback): void; /** @inheritdoc */ removeAllCookies(): Promise; /** * Removes all cookies after waiting for the initial read to finish, then saves the store to its file if any were removed. * @param {Function} cb - The callback to be called when finished. * @returns {Promise} a promise if no callback was passed. */ private _removeAllCookiesAsync; /** * Removes all cookies from the store without saving to a file. * @returns {boolean} true if any cookies were removed, or false if no change occured */ private _removeAllCookiesSyncInternal; /** * Removes all cookies from the store, then saves synchronously if any were removed. */ private _removeAllCookiesSync; /** @inheritdoc */ getAllCookies(cb: tough.Callback): void; /** @inheritdoc */ getAllCookies(): Promise; /** * Gets all the cookies after waiting for the initial read to finish. * @param {Function} cb - The callback to be called with the results. * @returns {Promise} a promise if no callback was passed. */ private _getAllCookiesAsync; /** * Gets all the cookies in the store and returns them. * @returns {Cookie[]} an array of all the cookies in the store. */ private _getAllCookiesSync; [util.inspect.custom]: () => string; /** * Returns a string representation of the store object for debugging purposes. * * @returns {string} - The string representation of the store. */ private _inspect; /** * Load the store from a file asynchronously. * * @param {string} filePath - The file to load the store from. * @param {object} options - Options for loading the file * @returns {Promise} a promise that resolves with the parsed data from the file. */ private _loadFromFileAsync; /** * Load the store from a file synchronously. * * @param {string} filePath - The file to load the store from. * @param {object} options - Options for loading the file * @returns {CookiesIndex} the parsed data from the file */ private _loadFromFileSync; /** * Loads the store from a json string. * @param {string} data - The string data that was loaded from a file. * @param {string} filePath - The path of the file that the string data was loaded from. * @param {object} options - Options for loading the file. * @returns {CookiesIndex} the parsed data */ private _loadFromStringSync; /** * Saves the store to its file asynchronously. * @param {Function} cb - The callback to be called when finished. * @returns {Promise} a promise if no callback was passed. */ private _saveAsync; /** * Saves the store to its file synchronously. */ private _saveSync; /** * Saves the store to a file asynchronously. * @param {string} filePath - The file path to save the store to. * @param {CookiesIndex} data - The cookies to save to the file. * @returns {Promise} a promise for the write task */ private _saveToFileAsync; /** * Saves the store to a file synchronously. * @param {string} filePath - The file path to save the store to. * @param {CookiesIndex} data - The cookies to save to the file. */ private _saveToFileSync; /** * Serializes the cookies data to a string * @param {CookiesIndex} data - The cookies data to serialize * @returns {string} the serialized cookies data */ private _saveToStringSync; }