UNPKG

tough-cookie-file-store

Version:
269 lines (268 loc) 12.3 kB
import * as tough from 'tough-cookie'; export type CookiesMap = { [key: string]: tough.Cookie; }; export type CookiesDomainData = { [path: string]: CookiesMap; }; export type CookiesData = { [domain: string]: CookiesDomainData; }; export type FileCookieStoreOptions = { async?: boolean; loadAsync?: boolean; onLoad?: (exists: boolean) => void; onLoadError?: (err: Error) => void; }; /** * Class representing a JSON file store. * * @augments Store */ export default class FileCookieStore extends tough.Store { synchronous: boolean; filePath: string; idx: CookiesData; 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 {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<void>); /** @inheritdoc */ findCookie(domain: tough.Nullable<string>, path: tough.Nullable<string>, key: tough.Nullable<string>, cb: tough.Callback<tough.Cookie | undefined>): void; /** @inheritdoc */ findCookie(domain: tough.Nullable<string>, path: tough.Nullable<string>, key: tough.Nullable<string>): Promise<tough.Cookie | undefined>; /** * 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<tough.Cookie>} 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<string>, path: tough.Nullable<string>, allowSpecialUseDomain?: boolean, cb?: tough.Callback<tough.Cookie[]>): void; /** @inheritdoc */ findCookies(domain: tough.Nullable<string>, path: tough.Nullable<string>, allowSpecialUseDomain?: boolean): Promise<tough.Cookie[]>; /** * 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<tough.Cookie[]>} 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<void>; /** * 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<void>; /** @inheritdoc */ removeCookie(domain: string, path: string, key: string, cb: tough.ErrorCallback): void; /** @inheritdoc */ removeCookie(domain: string, path: string, key: string): Promise<void>; /** * 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<string>, cb: tough.ErrorCallback): void; /** @inheritdoc */ removeCookies(domain: string, path: tough.Nullable<string>): Promise<void>; /** * 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<void>; /** * 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<tough.Cookie[]>): void; /** @inheritdoc */ getAllCookies(): Promise<tough.Cookie[]>; /** * 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<tough.Cookie[]>} 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; /** * 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. * @returns {Promise<CookiesData>} 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. * @returns {CookiesData} 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. * @returns {CookiesData} 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 {CookiesData} 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 {CookiesData} data - The cookies to save to the file. */ private _saveToFileSync; }