index.d.ts 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. /**
  2. * Cookie prefixes are a way to indicate that a given cookie was set with a set of attributes simply by inspecting the
  3. * first few characters of the cookie's name. These are defined in {@link https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.3 | RFC6265bis - Section 4.1.3}.
  4. *
  5. * The following values can be used to configure how a {@link CookieJar} enforces attribute restrictions for Cookie prefixes:
  6. *
  7. * - `silent` - Enable cookie prefix checking but silently ignores the cookie if conditions are not met. This is the default configuration for a {@link CookieJar}.
  8. *
  9. * - `strict` - Enables cookie prefix checking and will raise an error if conditions are not met.
  10. *
  11. * - `unsafe-disabled` - Disables cookie prefix checking.
  12. * @public
  13. */
  14. declare const PrefixSecurityEnum: {
  15. readonly SILENT: "silent";
  16. readonly STRICT: "strict";
  17. readonly DISABLED: "unsafe-disabled";
  18. };
  19. /**
  20. * A JSON representation of a {@link CookieJar}.
  21. * @public
  22. */
  23. interface SerializedCookieJar {
  24. /**
  25. * The version of `tough-cookie` used during serialization.
  26. */
  27. version: string;
  28. /**
  29. * The name of the store used during serialization.
  30. */
  31. storeType: string | null;
  32. /**
  33. * The value of {@link CreateCookieJarOptions.rejectPublicSuffixes} configured on the {@link CookieJar}.
  34. */
  35. rejectPublicSuffixes: boolean;
  36. /**
  37. * Other configuration settings on the {@link CookieJar}.
  38. */
  39. [key: string]: unknown;
  40. /**
  41. * The list of {@link Cookie} values serialized as JSON objects.
  42. */
  43. cookies: SerializedCookie[];
  44. }
  45. /**
  46. * A JSON object that is created when {@link Cookie.toJSON} is called. This object will contain the properties defined in {@link Cookie.serializableProperties}.
  47. * @public
  48. */
  49. type SerializedCookie = {
  50. key?: string;
  51. value?: string;
  52. [key: string]: unknown;
  53. };
  54. /**
  55. * Optional configuration to be used when parsing cookies.
  56. * @public
  57. */
  58. interface ParseCookieOptions {
  59. /**
  60. * If `true` then keyless cookies like `=abc` and `=` which are not RFC-compliant will be parsed.
  61. */
  62. loose?: boolean | undefined;
  63. }
  64. /**
  65. * Configurable values that can be set when creating a {@link Cookie}.
  66. * @public
  67. */
  68. interface CreateCookieOptions {
  69. /** {@inheritDoc Cookie.key} */
  70. key?: string;
  71. /** {@inheritDoc Cookie.value} */
  72. value?: string;
  73. /** {@inheritDoc Cookie.expires} */
  74. expires?: Date | 'Infinity' | null;
  75. /** {@inheritDoc Cookie.maxAge} */
  76. maxAge?: number | 'Infinity' | '-Infinity' | null;
  77. /** {@inheritDoc Cookie.domain} */
  78. domain?: string | null;
  79. /** {@inheritDoc Cookie.path} */
  80. path?: string | null;
  81. /** {@inheritDoc Cookie.secure} */
  82. secure?: boolean;
  83. /** {@inheritDoc Cookie.httpOnly} */
  84. httpOnly?: boolean;
  85. /** {@inheritDoc Cookie.extensions} */
  86. extensions?: string[] | null;
  87. /** {@inheritDoc Cookie.creation} */
  88. creation?: Date | 'Infinity' | null;
  89. /** {@inheritDoc Cookie.hostOnly} */
  90. hostOnly?: boolean | null;
  91. /** {@inheritDoc Cookie.pathIsDefault} */
  92. pathIsDefault?: boolean | null;
  93. /** {@inheritDoc Cookie.lastAccessed} */
  94. lastAccessed?: Date | 'Infinity' | null;
  95. /** {@inheritDoc Cookie.sameSite} */
  96. sameSite?: string | undefined;
  97. }
  98. /**
  99. * An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to a user's web browser.
  100. * It is defined in {@link https://www.rfc-editor.org/rfc/rfc6265.html | RFC6265}.
  101. * @public
  102. */
  103. declare class Cookie {
  104. /**
  105. * The name or key of the cookie
  106. */
  107. key: string;
  108. /**
  109. * The value of the cookie
  110. */
  111. value: string;
  112. /**
  113. * The 'Expires' attribute of the cookie
  114. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.1 | RFC6265 Section 5.2.1}).
  115. */
  116. expires: Date | 'Infinity' | null;
  117. /**
  118. * The 'Max-Age' attribute of the cookie
  119. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.2 | RFC6265 Section 5.2.2}).
  120. */
  121. maxAge: number | 'Infinity' | '-Infinity' | null;
  122. /**
  123. * The 'Domain' attribute of the cookie represents the domain the cookie belongs to
  124. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.3 | RFC6265 Section 5.2.3}).
  125. */
  126. domain: string | null;
  127. /**
  128. * The 'Path' attribute of the cookie represents the path of the cookie
  129. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.4 | RFC6265 Section 5.2.4}).
  130. */
  131. path: string | null;
  132. /**
  133. * The 'Secure' flag of the cookie indicates if the scope of the cookie is
  134. * limited to secure channels (e.g.; HTTPS) or not
  135. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.5 | RFC6265 Section 5.2.5}).
  136. */
  137. secure: boolean;
  138. /**
  139. * The 'HttpOnly' flag of the cookie indicates if the cookie is inaccessible to
  140. * client scripts or not
  141. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.6 | RFC6265 Section 5.2.6}).
  142. */
  143. httpOnly: boolean;
  144. /**
  145. * Contains attributes which are not part of the defined spec but match the `extension-av` syntax
  146. * defined in Section 4.1.1 of RFC6265
  147. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-4.1.1 | RFC6265 Section 4.1.1}).
  148. */
  149. extensions: string[] | null;
  150. /**
  151. * Set to the date and time when a Cookie is initially stored or a matching cookie is
  152. * received that replaces an existing cookie
  153. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3 | RFC6265 Section 5.3}).
  154. *
  155. * Also used to maintain ordering among cookies. Among cookies that have equal-length path fields,
  156. * cookies with earlier creation-times are listed before cookies with later creation-times
  157. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.4 | RFC6265 Section 5.4}).
  158. */
  159. creation: Date | 'Infinity' | null;
  160. /**
  161. * A global counter used to break ordering ties between two cookies that have equal-length path fields
  162. * and the same creation-time.
  163. */
  164. creationIndex: number;
  165. /**
  166. * A boolean flag indicating if a cookie is a host-only cookie (i.e.; when the request's host exactly
  167. * matches the domain of the cookie) or not
  168. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3 | RFC6265 Section 5.3}).
  169. */
  170. hostOnly: boolean | null;
  171. /**
  172. * A boolean flag indicating if a cookie had no 'Path' attribute and the default path
  173. * was used
  174. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.2.4 | RFC6265 Section 5.2.4}).
  175. */
  176. pathIsDefault: boolean | null;
  177. /**
  178. * Set to the date and time when a cookie was initially stored ({@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3 | RFC6265 Section 5.3}) and updated whenever
  179. * the cookie is retrieved from the {@link CookieJar} ({@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.4 | RFC6265 Section 5.4}).
  180. */
  181. lastAccessed: Date | 'Infinity' | null;
  182. /**
  183. * The 'SameSite' attribute of a cookie as defined in RFC6265bis
  184. * (See {@link https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-13.html#section-5.2 | RFC6265bis (v13) Section 5.2 }).
  185. */
  186. sameSite: string | undefined;
  187. /**
  188. * Create a new Cookie instance.
  189. * @public
  190. * @param options - The attributes to set on the cookie
  191. */
  192. constructor(options?: CreateCookieOptions);
  193. /**
  194. * For convenience in using `JSON.stringify(cookie)`. Returns a plain-old Object that can be JSON-serialized.
  195. *
  196. * @remarks
  197. * - Any `Date` properties (such as {@link Cookie.expires}, {@link Cookie.creation}, and {@link Cookie.lastAccessed}) are exported in ISO format (`Date.toISOString()`).
  198. *
  199. * - Custom Cookie properties are discarded. In tough-cookie 1.x, since there was no {@link Cookie.toJSON} method explicitly defined, all enumerable properties were captured.
  200. * If you want a property to be serialized, add the property name to {@link Cookie.serializableProperties}.
  201. */
  202. toJSON(): SerializedCookie;
  203. /**
  204. * Does a deep clone of this cookie, implemented exactly as `Cookie.fromJSON(cookie.toJSON())`.
  205. * @public
  206. */
  207. clone(): Cookie | undefined;
  208. /**
  209. * Validates cookie attributes for semantic correctness. Useful for "lint" checking any `Set-Cookie` headers you generate.
  210. * For now, it returns a boolean, but eventually could return a reason string.
  211. *
  212. * @remarks
  213. * Works for a few things, but is by no means comprehensive.
  214. *
  215. * @beta
  216. */
  217. validate(): boolean;
  218. /**
  219. * Sets the 'Expires' attribute on a cookie.
  220. *
  221. * @remarks
  222. * When given a `string` value it will be parsed with {@link parseDate}. If the value can't be parsed as a cookie date
  223. * then the 'Expires' attribute will be set to `"Infinity"`.
  224. *
  225. * @param exp - the new value for the 'Expires' attribute of the cookie.
  226. */
  227. setExpires(exp: string | Date): void;
  228. /**
  229. * Sets the 'Max-Age' attribute (in seconds) on a cookie.
  230. *
  231. * @remarks
  232. * Coerces `-Infinity` to `"-Infinity"` and `Infinity` to `"Infinity"` so it can be serialized to JSON.
  233. *
  234. * @param age - the new value for the 'Max-Age' attribute (in seconds).
  235. */
  236. setMaxAge(age: number): void;
  237. /**
  238. * Encodes to a `Cookie` header value (specifically, the {@link Cookie.key} and {@link Cookie.value} properties joined with "=").
  239. * @public
  240. */
  241. cookieString(): string;
  242. /**
  243. * Encodes to a `Set-Cookie header` value.
  244. * @public
  245. */
  246. toString(): string;
  247. /**
  248. * Computes the TTL relative to now (milliseconds).
  249. *
  250. * @remarks
  251. * - `Infinity` is returned for cookies without an explicit expiry
  252. *
  253. * - `0` is returned if the cookie is expired.
  254. *
  255. * - Otherwise a time-to-live in milliseconds is returned.
  256. *
  257. * @param now - passing an explicit value is mostly used for testing purposes since this defaults to the `Date.now()`
  258. * @public
  259. */
  260. TTL(now?: number): number;
  261. /**
  262. * Computes the absolute unix-epoch milliseconds that this cookie expires.
  263. *
  264. * The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The {@link Cookie.lastAccessed} attribute
  265. * (or the `now` parameter if given) is used to offset the {@link Cookie.maxAge} attribute.
  266. *
  267. * If Expires ({@link Cookie.expires}) is set, that's returned.
  268. *
  269. * @param now - can be used to provide a time offset (instead of {@link Cookie.lastAccessed}) to use when calculating the "Max-Age" value
  270. */
  271. expiryTime(now?: Date): number | undefined;
  272. /**
  273. * Similar to {@link Cookie.expiryTime}, computes the absolute unix-epoch milliseconds that this cookie expires and returns it as a Date.
  274. *
  275. * The "Max-Age" attribute takes precedence over "Expires" (as per the RFC). The {@link Cookie.lastAccessed} attribute
  276. * (or the `now` parameter if given) is used to offset the {@link Cookie.maxAge} attribute.
  277. *
  278. * If Expires ({@link Cookie.expires}) is set, that's returned.
  279. *
  280. * @param now - can be used to provide a time offset (instead of {@link Cookie.lastAccessed}) to use when calculating the "Max-Age" value
  281. */
  282. expiryDate(now?: Date): Date | undefined;
  283. /**
  284. * Indicates if the cookie has been persisted to a store or not.
  285. * @public
  286. */
  287. isPersistent(): boolean;
  288. /**
  289. * Calls {@link canonicalDomain} with the {@link Cookie.domain} property.
  290. * @public
  291. */
  292. canonicalizedDomain(): string | undefined;
  293. /**
  294. * Alias for {@link Cookie.canonicalizedDomain}
  295. * @public
  296. */
  297. cdomain(): string | undefined;
  298. /**
  299. * Parses a string into a Cookie object.
  300. *
  301. * @remarks
  302. * Note: when parsing a `Cookie` header it must be split by ';' before each Cookie string can be parsed.
  303. *
  304. * @example
  305. * ```
  306. * // parse a `Set-Cookie` header
  307. * const setCookieHeader = 'a=bcd; Expires=Tue, 18 Oct 2011 07:05:03 GMT'
  308. * const cookie = Cookie.parse(setCookieHeader)
  309. * cookie.key === 'a'
  310. * cookie.value === 'bcd'
  311. * cookie.expires === new Date(Date.parse('Tue, 18 Oct 2011 07:05:03 GMT'))
  312. * ```
  313. *
  314. * @example
  315. * ```
  316. * // parse a `Cookie` header
  317. * const cookieHeader = 'name=value; name2=value2; name3=value3'
  318. * const cookies = cookieHeader.split(';').map(Cookie.parse)
  319. * cookies[0].name === 'name'
  320. * cookies[0].value === 'value'
  321. * cookies[1].name === 'name2'
  322. * cookies[1].value === 'value2'
  323. * cookies[2].name === 'name3'
  324. * cookies[2].value === 'value3'
  325. * ```
  326. *
  327. * @param str - The `Set-Cookie` header or a Cookie string to parse.
  328. * @param options - Configures `strict` or `loose` mode for cookie parsing
  329. */
  330. static parse(str: string, options?: ParseCookieOptions): Cookie | undefined;
  331. /**
  332. * Does the reverse of {@link Cookie.toJSON}.
  333. *
  334. * @remarks
  335. * Any Date properties (such as .expires, .creation, and .lastAccessed) are parsed via Date.parse, not tough-cookie's parseDate, since ISO timestamps are being handled at this layer.
  336. *
  337. * @example
  338. * ```
  339. * const json = JSON.stringify({
  340. * key: 'alpha',
  341. * value: 'beta',
  342. * domain: 'example.com',
  343. * path: '/foo',
  344. * expires: '2038-01-19T03:14:07.000Z',
  345. * })
  346. * const cookie = Cookie.fromJSON(json)
  347. * cookie.key === 'alpha'
  348. * cookie.value === 'beta'
  349. * cookie.domain === 'example.com'
  350. * cookie.path === '/foo'
  351. * cookie.expires === new Date(Date.parse('2038-01-19T03:14:07.000Z'))
  352. * ```
  353. *
  354. * @param str - An unparsed JSON string or a value that has already been parsed as JSON
  355. */
  356. static fromJSON(str: unknown): Cookie | undefined;
  357. private static cookiesCreated;
  358. /**
  359. * @internal
  360. */
  361. static sameSiteLevel: {
  362. readonly strict: 3;
  363. readonly lax: 2;
  364. readonly none: 1;
  365. };
  366. /**
  367. * @internal
  368. */
  369. static sameSiteCanonical: {
  370. readonly strict: "Strict";
  371. readonly lax: "Lax";
  372. };
  373. /**
  374. * Cookie properties that will be serialized when using {@link Cookie.fromJSON} and {@link Cookie.toJSON}.
  375. * @public
  376. */
  377. static serializableProperties: readonly ["key", "value", "expires", "maxAge", "domain", "path", "secure", "httpOnly", "extensions", "hostOnly", "pathIsDefault", "creation", "lastAccessed", "sameSite"];
  378. }
  379. /**
  380. * A callback function that accepts an error or a result.
  381. * @public
  382. */
  383. interface Callback<T> {
  384. (error: Error, result?: never): void;
  385. (error: null, result: T): void;
  386. }
  387. /**
  388. * A callback function that only accepts an error.
  389. * @public
  390. */
  391. interface ErrorCallback {
  392. (error: Error | null): void;
  393. }
  394. /**
  395. * The inverse of NonNullable<T>.
  396. * @public
  397. */
  398. type Nullable<T> = T | null | undefined;
  399. /**
  400. * Base class for {@link CookieJar} stores.
  401. *
  402. * The storage model for each {@link CookieJar} instance can be replaced with a custom implementation. The default is
  403. * {@link MemoryCookieStore}.
  404. *
  405. * @remarks
  406. * - Stores should inherit from the base Store class, which is available as a top-level export.
  407. *
  408. * - Stores are asynchronous by default, but if {@link Store.synchronous} is set to true, then the `*Sync` methods
  409. * of the containing {@link CookieJar} can be used.
  410. *
  411. * @public
  412. */
  413. declare class Store {
  414. /**
  415. * Store implementations that support synchronous methods must return `true`.
  416. */
  417. synchronous: boolean;
  418. constructor();
  419. /**
  420. * Retrieve a {@link Cookie} with the given `domain`, `path`, and `key` (`name`). The RFC maintains that exactly
  421. * one of these cookies should exist in a store. If the store is using versioning, this means that the latest or
  422. * newest such cookie should be returned.
  423. *
  424. * Callback takes an error and the resulting Cookie object. If no cookie is found then null MUST be passed instead (that is, not an error).
  425. * @param domain - The cookie domain to match against.
  426. * @param path - The cookie path to match against.
  427. * @param key - The cookie name to match against.
  428. */
  429. findCookie(domain: Nullable<string>, path: Nullable<string>, key: Nullable<string>): Promise<Cookie | undefined>;
  430. /**
  431. * Retrieve a {@link Cookie} with the given `domain`, `path`, and `key` (`name`). The RFC maintains that exactly
  432. * one of these cookies should exist in a store. If the store is using versioning, this means that the latest or
  433. * newest such cookie should be returned.
  434. *
  435. * Callback takes an error and the resulting Cookie object. If no cookie is found then null MUST be passed instead (that is, not an error).
  436. * @param domain - The cookie domain to match against.
  437. * @param path - The cookie path to match against.
  438. * @param key - The cookie name to match against.
  439. * @param callback - A function to call with either the found cookie or an error.
  440. */
  441. findCookie(domain: Nullable<string>, path: Nullable<string>, key: Nullable<string>, callback: Callback<Cookie | undefined>): void;
  442. /**
  443. * Locates all {@link Cookie} values matching the given `domain` and `path`.
  444. *
  445. * The resulting list is checked for applicability to the current request according to the RFC (`domain-match`, `path-match`,
  446. * `http-only-flag`, `secure-flag`, `expiry`, and so on), so it's OK to use an optimistic search algorithm when implementing
  447. * this method. However, the search algorithm used SHOULD try to find cookies that {@link domainMatch} the `domain` and
  448. * {@link pathMatch} the `path` in order to limit the amount of checking that needs to be done.
  449. *
  450. * @remarks
  451. * - As of version `0.9.12`, the `allPaths` option to cookiejar.getCookies() above causes the path here to be `null`.
  452. *
  453. * - If the `path` is `null`, `path-matching` MUST NOT be performed (that is, `domain-matching` only).
  454. *
  455. * @param domain - The cookie domain to match against.
  456. * @param path - The cookie path to match against.
  457. * @param allowSpecialUseDomain - If `true` then special-use domain suffixes, will be allowed in matches. Defaults to `false`.
  458. */
  459. findCookies(domain: Nullable<string>, path: Nullable<string>, allowSpecialUseDomain?: boolean): Promise<Cookie[]>;
  460. /**
  461. * Locates all {@link Cookie} values matching the given `domain` and `path`.
  462. *
  463. * The resulting list is checked for applicability to the current request according to the RFC (`domain-match`, `path-match`,
  464. * `http-only-flag`, `secure-flag`, `expiry`, and so on), so it's OK to use an optimistic search algorithm when implementing
  465. * this method. However, the search algorithm used SHOULD try to find cookies that {@link domainMatch} the `domain` and
  466. * {@link pathMatch} the `path` in order to limit the amount of checking that needs to be done.
  467. *
  468. * @remarks
  469. * - As of version `0.9.12`, the `allPaths` option to cookiejar.getCookies() above causes the path here to be `null`.
  470. *
  471. * - If the `path` is `null`, `path-matching` MUST NOT be performed (that is, `domain-matching` only).
  472. *
  473. * @param domain - The cookie domain to match against.
  474. * @param path - The cookie path to match against.
  475. * @param allowSpecialUseDomain - If `true` then special-use domain suffixes, will be allowed in matches. Defaults to `false`.
  476. * @param callback - A function to call with either the found cookies or an error.
  477. */
  478. findCookies(domain: Nullable<string>, path: Nullable<string>, allowSpecialUseDomain?: boolean, callback?: Callback<Cookie[]>): void;
  479. /**
  480. * Adds a new {@link Cookie} to the store. The implementation SHOULD replace any existing cookie with the same `domain`,
  481. * `path`, and `key` properties.
  482. *
  483. * @remarks
  484. * - Depending on the nature of the implementation, it's possible that between the call to `fetchCookie` and `putCookie`
  485. * that a duplicate `putCookie` can occur.
  486. *
  487. * - The {@link Cookie} object MUST NOT be modified; as the caller has already updated the `creation` and `lastAccessed` properties.
  488. *
  489. * @param cookie - The cookie to store.
  490. */
  491. putCookie(cookie: Cookie): Promise<void>;
  492. /**
  493. * Adds a new {@link Cookie} to the store. The implementation SHOULD replace any existing cookie with the same `domain`,
  494. * `path`, and `key` properties.
  495. *
  496. * @remarks
  497. * - Depending on the nature of the implementation, it's possible that between the call to `fetchCookie` and `putCookie`
  498. * that a duplicate `putCookie` can occur.
  499. *
  500. * - The {@link Cookie} object MUST NOT be modified; as the caller has already updated the `creation` and `lastAccessed` properties.
  501. *
  502. * @param cookie - The cookie to store.
  503. * @param callback - A function to call when the cookie has been stored or an error has occurred.
  504. */
  505. putCookie(cookie: Cookie, callback: ErrorCallback): void;
  506. /**
  507. * Update an existing {@link Cookie}. The implementation MUST update the `value` for a cookie with the same `domain`,
  508. * `path`, and `key`. The implementation SHOULD check that the old value in the store is equivalent to oldCookie -
  509. * how the conflict is resolved is up to the store.
  510. *
  511. * @remarks
  512. * - The `lastAccessed` property is always different between the two objects (to the precision possible via JavaScript's clock).
  513. *
  514. * - Both `creation` and `creationIndex` are guaranteed to be the same.
  515. *
  516. * - Stores MAY ignore or defer the `lastAccessed` change at the cost of affecting how cookies are selected for automatic deletion.
  517. *
  518. * - Stores may wish to optimize changing the `value` of the cookie in the store versus storing a new cookie.
  519. *
  520. * - The `newCookie` and `oldCookie` objects MUST NOT be modified.
  521. *
  522. * @param oldCookie - the cookie that is already present in the store.
  523. * @param newCookie - the cookie to replace the one already present in the store.
  524. */
  525. updateCookie(oldCookie: Cookie, newCookie: Cookie): Promise<void>;
  526. /**
  527. * Update an existing {@link Cookie}. The implementation MUST update the `value` for a cookie with the same `domain`,
  528. * `path`, and `key`. The implementation SHOULD check that the old value in the store is equivalent to oldCookie -
  529. * how the conflict is resolved is up to the store.
  530. *
  531. * @remarks
  532. * - The `lastAccessed` property is always different between the two objects (to the precision possible via JavaScript's clock).
  533. *
  534. * - Both `creation` and `creationIndex` are guaranteed to be the same.
  535. *
  536. * - Stores MAY ignore or defer the `lastAccessed` change at the cost of affecting how cookies are selected for automatic deletion.
  537. *
  538. * - Stores may wish to optimize changing the `value` of the cookie in the store versus storing a new cookie.
  539. *
  540. * - The `newCookie` and `oldCookie` objects MUST NOT be modified.
  541. *
  542. * @param oldCookie - the cookie that is already present in the store.
  543. * @param newCookie - the cookie to replace the one already present in the store.
  544. * @param callback - A function to call when the cookie has been updated or an error has occurred.
  545. */
  546. updateCookie(oldCookie: Cookie, newCookie: Cookie, callback: ErrorCallback): void;
  547. /**
  548. * Remove a cookie from the store (see notes on `findCookie` about the uniqueness constraint).
  549. *
  550. * @param domain - The cookie domain to match against.
  551. * @param path - The cookie path to match against.
  552. * @param key - The cookie name to match against.
  553. */
  554. removeCookie(domain: Nullable<string>, path: Nullable<string>, key: Nullable<string>): Promise<void>;
  555. /**
  556. * Remove a cookie from the store (see notes on `findCookie` about the uniqueness constraint).
  557. *
  558. * @param domain - The cookie domain to match against.
  559. * @param path - The cookie path to match against.
  560. * @param key - The cookie name to match against.
  561. * @param callback - A function to call when the cookie has been removed or an error occurs.
  562. */
  563. removeCookie(domain: Nullable<string>, path: Nullable<string>, key: Nullable<string>, callback: ErrorCallback): void;
  564. /**
  565. * Removes matching cookies from the store. The `path` parameter is optional and if missing,
  566. * means all paths in a domain should be removed.
  567. *
  568. * @param domain - The cookie domain to match against.
  569. * @param path - The cookie path to match against.
  570. */
  571. removeCookies(domain: string, path: Nullable<string>): Promise<void>;
  572. /**
  573. * Removes matching cookies from the store. The `path` parameter is optional and if missing,
  574. * means all paths in a domain should be removed.
  575. *
  576. * @param domain - The cookie domain to match against.
  577. * @param path - The cookie path to match against.
  578. * @param callback - A function to call when the cookies have been removed or an error occurs.
  579. */
  580. removeCookies(domain: string, path: Nullable<string>, callback: ErrorCallback): void;
  581. /**
  582. * Removes all cookies from the store.
  583. */
  584. removeAllCookies(): Promise<void>;
  585. /**
  586. * Removes all cookies from the store.
  587. *
  588. * @param callback - A function to call when all the cookies have been removed or an error occurs.
  589. */
  590. removeAllCookies(callback: ErrorCallback): void;
  591. /**
  592. * Gets all the cookies in the store.
  593. *
  594. * @remarks
  595. * - Cookies SHOULD be returned in creation order to preserve sorting via {@link cookieCompare}.
  596. */
  597. getAllCookies(): Promise<Cookie[]>;
  598. /**
  599. * Gets all the cookies in the store.
  600. *
  601. * @remarks
  602. * - Cookies SHOULD be returned in creation order to preserve sorting via {@link cookieCompare}.
  603. *
  604. * @param callback - A function to call when all the cookies have been retrieved or an error occurs.
  605. */
  606. getAllCookies(callback: Callback<Cookie[]>): void;
  607. }
  608. /**
  609. * The internal structure used in {@link MemoryCookieStore}.
  610. * @internal
  611. */
  612. type MemoryCookieStoreIndex = {
  613. [domain: string]: {
  614. [path: string]: {
  615. [key: string]: Cookie;
  616. };
  617. };
  618. };
  619. /**
  620. * An in-memory {@link Store} implementation for {@link CookieJar}. This is the default implementation used by
  621. * {@link CookieJar} and supports both async and sync operations. Also supports serialization, getAllCookies, and removeAllCookies.
  622. * @public
  623. */
  624. declare class MemoryCookieStore extends Store {
  625. /**
  626. * This value is `true` since {@link MemoryCookieStore} implements synchronous functionality.
  627. */
  628. synchronous: boolean;
  629. /**
  630. * @internal
  631. */
  632. idx: MemoryCookieStoreIndex;
  633. /**
  634. * Create a new {@link MemoryCookieStore}.
  635. */
  636. constructor();
  637. /**
  638. * Retrieve a {@link Cookie} with the given `domain`, `path`, and `key` (`name`). The RFC maintains that exactly
  639. * one of these cookies should exist in a store. If the store is using versioning, this means that the latest or
  640. * newest such cookie should be returned.
  641. *
  642. * @param domain - The cookie domain to match against.
  643. * @param path - The cookie path to match against.
  644. * @param key - The cookie name to match against.
  645. */
  646. findCookie(domain: Nullable<string>, path: Nullable<string>, key: Nullable<string>): Promise<Cookie | undefined>;
  647. /**
  648. * Retrieve a {@link Cookie} with the given `domain`, `path`, and `key` (`name`). The RFC maintains that exactly
  649. * one of these cookies should exist in a store. If the store is using versioning, this means that the latest or
  650. * newest such cookie should be returned.
  651. *
  652. * Callback takes an error and the resulting Cookie object. If no cookie is found then null MUST be passed instead (that is, not an error).
  653. * @param domain - The cookie domain to match against.
  654. * @param path - The cookie path to match against.
  655. * @param key - The cookie name to match against.
  656. * @param callback - A function to call with either the found cookie or an error.
  657. */
  658. findCookie(domain: Nullable<string>, path: Nullable<string>, key: Nullable<string>, callback: Callback<Cookie | undefined>): void;
  659. /**
  660. * Locates all {@link Cookie} values matching the given `domain` and `path`.
  661. *
  662. * The resulting list is checked for applicability to the current request according to the RFC (`domain-match`, `path-match`,
  663. * `http-only-flag`, `secure-flag`, `expiry`, and so on), so it's OK to use an optimistic search algorithm when implementing
  664. * this method. However, the search algorithm used SHOULD try to find cookies that {@link domainMatch} the `domain` and
  665. * {@link pathMatch} the `path` in order to limit the amount of checking that needs to be done.
  666. *
  667. * @remarks
  668. * - As of version `0.9.12`, the `allPaths` option to cookiejar.getCookies() above causes the path here to be `null`.
  669. *
  670. * - If the `path` is `null`, `path-matching` MUST NOT be performed (that is, `domain-matching` only).
  671. *
  672. * @param domain - The cookie domain to match against.
  673. * @param path - The cookie path to match against.
  674. * @param allowSpecialUseDomain - If `true` then special-use domain suffixes, will be allowed in matches. Defaults to `false`.
  675. */
  676. findCookies(domain: string, path: string, allowSpecialUseDomain?: boolean): Promise<Cookie[]>;
  677. /**
  678. * Locates all {@link Cookie} values matching the given `domain` and `path`.
  679. *
  680. * The resulting list is checked for applicability to the current request according to the RFC (`domain-match`, `path-match`,
  681. * `http-only-flag`, `secure-flag`, `expiry`, and so on), so it's OK to use an optimistic search algorithm when implementing
  682. * this method. However, the search algorithm used SHOULD try to find cookies that {@link domainMatch} the `domain` and
  683. * {@link pathMatch} the `path` in order to limit the amount of checking that needs to be done.
  684. *
  685. * @remarks
  686. * - As of version `0.9.12`, the `allPaths` option to cookiejar.getCookies() above causes the path here to be `null`.
  687. *
  688. * - If the `path` is `null`, `path-matching` MUST NOT be performed (that is, `domain-matching` only).
  689. *
  690. * @param domain - The cookie domain to match against.
  691. * @param path - The cookie path to match against.
  692. * @param allowSpecialUseDomain - If `true` then special-use domain suffixes, will be allowed in matches. Defaults to `false`.
  693. * @param callback - A function to call with either the found cookies or an error.
  694. */
  695. findCookies(domain: string, path: string, allowSpecialUseDomain?: boolean, callback?: Callback<Cookie[]>): void;
  696. /**
  697. * Adds a new {@link Cookie} to the store. The implementation SHOULD replace any existing cookie with the same `domain`,
  698. * `path`, and `key` properties.
  699. *
  700. * @remarks
  701. * - Depending on the nature of the implementation, it's possible that between the call to `fetchCookie` and `putCookie`
  702. * that a duplicate `putCookie` can occur.
  703. *
  704. * - The {@link Cookie} object MUST NOT be modified; as the caller has already updated the `creation` and `lastAccessed` properties.
  705. *
  706. * @param cookie - The cookie to store.
  707. */
  708. putCookie(cookie: Cookie): Promise<void>;
  709. /**
  710. * Adds a new {@link Cookie} to the store. The implementation SHOULD replace any existing cookie with the same `domain`,
  711. * `path`, and `key` properties.
  712. *
  713. * @remarks
  714. * - Depending on the nature of the implementation, it's possible that between the call to `fetchCookie` and `putCookie`
  715. * that a duplicate `putCookie` can occur.
  716. *
  717. * - The {@link Cookie} object MUST NOT be modified; as the caller has already updated the `creation` and `lastAccessed` properties.
  718. *
  719. * @param cookie - The cookie to store.
  720. * @param callback - A function to call when the cookie has been stored or an error has occurred.
  721. */
  722. putCookie(cookie: Cookie, callback: ErrorCallback): void;
  723. /**
  724. * Update an existing {@link Cookie}. The implementation MUST update the `value` for a cookie with the same `domain`,
  725. * `path`, and `key`. The implementation SHOULD check that the old value in the store is equivalent to oldCookie -
  726. * how the conflict is resolved is up to the store.
  727. *
  728. * @remarks
  729. * - The `lastAccessed` property is always different between the two objects (to the precision possible via JavaScript's clock).
  730. *
  731. * - Both `creation` and `creationIndex` are guaranteed to be the same.
  732. *
  733. * - Stores MAY ignore or defer the `lastAccessed` change at the cost of affecting how cookies are selected for automatic deletion.
  734. *
  735. * - Stores may wish to optimize changing the `value` of the cookie in the store versus storing a new cookie.
  736. *
  737. * - The `newCookie` and `oldCookie` objects MUST NOT be modified.
  738. *
  739. * @param oldCookie - the cookie that is already present in the store.
  740. * @param newCookie - the cookie to replace the one already present in the store.
  741. */
  742. updateCookie(oldCookie: Cookie, newCookie: Cookie): Promise<void>;
  743. /**
  744. * Update an existing {@link Cookie}. The implementation MUST update the `value` for a cookie with the same `domain`,
  745. * `path`, and `key`. The implementation SHOULD check that the old value in the store is equivalent to oldCookie -
  746. * how the conflict is resolved is up to the store.
  747. *
  748. * @remarks
  749. * - The `lastAccessed` property is always different between the two objects (to the precision possible via JavaScript's clock).
  750. *
  751. * - Both `creation` and `creationIndex` are guaranteed to be the same.
  752. *
  753. * - Stores MAY ignore or defer the `lastAccessed` change at the cost of affecting how cookies are selected for automatic deletion.
  754. *
  755. * - Stores may wish to optimize changing the `value` of the cookie in the store versus storing a new cookie.
  756. *
  757. * - The `newCookie` and `oldCookie` objects MUST NOT be modified.
  758. *
  759. * @param oldCookie - the cookie that is already present in the store.
  760. * @param newCookie - the cookie to replace the one already present in the store.
  761. * @param callback - A function to call when the cookie has been updated or an error has occurred.
  762. */
  763. updateCookie(oldCookie: Cookie, newCookie: Cookie, callback: ErrorCallback): void;
  764. /**
  765. * Remove a cookie from the store (see notes on `findCookie` about the uniqueness constraint).
  766. *
  767. * @param domain - The cookie domain to match against.
  768. * @param path - The cookie path to match against.
  769. * @param key - The cookie name to match against.
  770. */
  771. removeCookie(domain: string, path: string, key: string): Promise<void>;
  772. /**
  773. * Remove a cookie from the store (see notes on `findCookie` about the uniqueness constraint).
  774. *
  775. * @param domain - The cookie domain to match against.
  776. * @param path - The cookie path to match against.
  777. * @param key - The cookie name to match against.
  778. * @param callback - A function to call when the cookie has been removed or an error occurs.
  779. */
  780. removeCookie(domain: string, path: string, key: string, callback: ErrorCallback): void;
  781. /**
  782. * Removes matching cookies from the store. The `path` parameter is optional and if missing,
  783. * means all paths in a domain should be removed.
  784. *
  785. * @param domain - The cookie domain to match against.
  786. * @param path - The cookie path to match against.
  787. */
  788. removeCookies(domain: string, path: string): Promise<void>;
  789. /**
  790. * Removes matching cookies from the store. The `path` parameter is optional and if missing,
  791. * means all paths in a domain should be removed.
  792. *
  793. * @param domain - The cookie domain to match against.
  794. * @param path - The cookie path to match against.
  795. * @param callback - A function to call when the cookies have been removed or an error occurs.
  796. */
  797. removeCookies(domain: string, path: string, callback: ErrorCallback): void;
  798. /**
  799. * Removes all cookies from the store.
  800. */
  801. removeAllCookies(): Promise<void>;
  802. /**
  803. * Removes all cookies from the store.
  804. *
  805. * @param callback - A function to call when all the cookies have been removed or an error occurs.
  806. */
  807. removeAllCookies(callback: ErrorCallback): void;
  808. /**
  809. * Gets all the cookies in the store.
  810. *
  811. * @remarks
  812. * - Cookies SHOULD be returned in creation order to preserve sorting via {@link cookieCompare}.
  813. */
  814. getAllCookies(): Promise<Cookie[]>;
  815. /**
  816. * Gets all the cookies in the store.
  817. *
  818. * @remarks
  819. * - Cookies SHOULD be returned in creation order to preserve sorting via {@link cookieCompare}.
  820. *
  821. * @param callback - A function to call when all the cookies have been retrieved or an error occurs.
  822. */
  823. getAllCookies(callback: Callback<Cookie[]>): void;
  824. }
  825. /**
  826. * Answers "does the request-path path-match a given cookie-path?" as per {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.1.4 | RFC6265 Section 5.1.4}.
  827. * This is essentially a prefix-match where cookiePath is a prefix of reqPath.
  828. *
  829. * @remarks
  830. * A request-path path-matches a given cookie-path if at least one of
  831. * the following conditions holds:
  832. *
  833. * - The cookie-path and the request-path are identical.
  834. * - The cookie-path is a prefix of the request-path, and the last character of the cookie-path is %x2F ("/").
  835. * - The cookie-path is a prefix of the request-path, and the first character of the request-path that is not included in the cookie-path is a %x2F ("/") character.
  836. *
  837. * @param reqPath - the path of the request
  838. * @param cookiePath - the path of the cookie
  839. * @public
  840. */
  841. declare function pathMatch(reqPath: string, cookiePath: string): boolean;
  842. /**
  843. * Generates the permutation of all possible values that {@link domainMatch} the given `domain` parameter. The
  844. * array is in shortest-to-longest order. Useful when building custom {@link Store} implementations.
  845. *
  846. * @example
  847. * ```
  848. * permuteDomain('foo.bar.example.com')
  849. * // ['example.com', 'bar.example.com', 'foo.bar.example.com']
  850. * ```
  851. *
  852. * @public
  853. * @param domain - the domain to generate permutations for
  854. * @param allowSpecialUseDomain - flag to control if {@link https://www.rfc-editor.org/rfc/rfc6761.html | Special Use Domains} such as `localhost` should be allowed
  855. */
  856. declare function permuteDomain(domain: string, allowSpecialUseDomain?: boolean): string[] | undefined;
  857. /**
  858. * Options for configuring how {@link getPublicSuffix} behaves.
  859. * @public
  860. */
  861. interface GetPublicSuffixOptions {
  862. /**
  863. * If set to `true` then the following {@link https://www.rfc-editor.org/rfc/rfc6761.html | Special Use Domains} will
  864. * be treated as if they were valid public suffixes ('local', 'example', 'invalid', 'localhost', 'test').
  865. *
  866. * @remarks
  867. * In testing scenarios it's common to configure the cookie store with so that `http://localhost` can be used as a domain:
  868. * ```json
  869. * {
  870. * allowSpecialUseDomain: true,
  871. * rejectPublicSuffixes: false
  872. * }
  873. * ```
  874. *
  875. * @defaultValue false
  876. */
  877. allowSpecialUseDomain?: boolean | undefined;
  878. /**
  879. * If set to `true` then any errors that occur while executing {@link getPublicSuffix} will be silently ignored.
  880. *
  881. * @defaultValue false
  882. */
  883. ignoreError?: boolean | undefined;
  884. }
  885. /**
  886. * Returns the public suffix of this hostname. The public suffix is the shortest domain
  887. * name upon which a cookie can be set.
  888. *
  889. * @remarks
  890. * A "public suffix" is a domain that is controlled by a
  891. * public registry, such as "com", "co.uk", and "pvt.k12.wy.us".
  892. * This step is essential for preventing attacker.com from
  893. * disrupting the integrity of example.com by setting a cookie
  894. * with a Domain attribute of "com". Unfortunately, the set of
  895. * public suffixes (also known as "registry controlled domains")
  896. * changes over time. If feasible, user agents SHOULD use an
  897. * up-to-date public suffix list, such as the one maintained by
  898. * the Mozilla project at http://publicsuffix.org/.
  899. * (See {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3 | RFC6265 - Section 5.3})
  900. *
  901. * @example
  902. * ```
  903. * getPublicSuffix('www.example.com') === 'example.com'
  904. * getPublicSuffix('www.subdomain.example.com') === 'example.com'
  905. * ```
  906. *
  907. * @param domain - the domain attribute of a cookie
  908. * @param options - optional configuration for controlling how the public suffix is determined
  909. * @public
  910. */
  911. declare function getPublicSuffix(domain: string, options?: GetPublicSuffixOptions): string | undefined;
  912. /**
  913. * Represents a validation error.
  914. * @public
  915. */
  916. declare class ParameterError extends Error {
  917. }
  918. /**
  919. * The version of `tough-cookie`
  920. * @public
  921. */
  922. declare const version = "6.0.0";
  923. /**
  924. * Transforms a domain name into a canonical domain name. The canonical domain name is a domain name
  925. * that has been trimmed, lowercased, stripped of leading dot, and optionally punycode-encoded
  926. * ({@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.1.2 | Section 5.1.2 of RFC 6265}). For
  927. * the most part, this function is idempotent (calling the function with the output from a previous call
  928. * returns the same output).
  929. *
  930. * @remarks
  931. * A canonicalized host name is the string generated by the following
  932. * algorithm:
  933. *
  934. * 1. Convert the host name to a sequence of individual domain name
  935. * labels.
  936. *
  937. * 2. Convert each label that is not a Non-Reserved LDH (NR-LDH) label,
  938. * to an A-label (see Section 2.3.2.1 of [RFC5890] for the former
  939. * and latter), or to a "punycode label" (a label resulting from the
  940. * "ToASCII" conversion in Section 4 of [RFC3490]), as appropriate
  941. * (see Section 6.3 of this specification).
  942. *
  943. * 3. Concatenate the resulting labels, separated by a %x2E (".")
  944. * character.
  945. *
  946. * @example
  947. * ```
  948. * canonicalDomain('.EXAMPLE.com') === 'example.com'
  949. * ```
  950. *
  951. * @param domainName - the domain name to generate the canonical domain from
  952. * @public
  953. */
  954. declare function canonicalDomain(domainName: Nullable<string>): string | undefined;
  955. /**
  956. * A comparison function that can be used with {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | Array.sort()},
  957. * which orders a list of cookies into the recommended order given in Step 2 of {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.4 | RFC6265 - Section 5.4}.
  958. *
  959. * The sort algorithm is, in order of precedence:
  960. *
  961. * - Longest {@link Cookie.path}
  962. *
  963. * - Oldest {@link Cookie.creation} (which has a 1-ms precision, same as Date)
  964. *
  965. * - Lowest {@link Cookie.creationIndex} (to get beyond the 1-ms precision)
  966. *
  967. * @remarks
  968. * ### RFC6265 - Section 5.4 - Step 2
  969. *
  970. * The user agent SHOULD sort the cookie-list in the following order:
  971. *
  972. * - Cookies with longer paths are listed before cookies with shorter paths.
  973. *
  974. * - Among cookies that have equal-length path fields, cookies with
  975. * earlier creation-times are listed before cookies with later
  976. * creation-times.
  977. *
  978. * NOTE: Not all user agents sort the cookie-list in this order, but
  979. * this order reflects common practice when this document was
  980. * written, and, historically, there have been servers that
  981. * (erroneously) depended on this order.
  982. *
  983. * ### Custom Store Implementors
  984. *
  985. * Since the JavaScript Date is limited to a 1-ms precision, cookies within the same millisecond are entirely possible.
  986. * This is especially true when using the `now` option to `CookieJar.setCookie(...)`. The {@link Cookie.creationIndex}
  987. * property is a per-process global counter, assigned during construction with `new Cookie()`, which preserves the spirit
  988. * of the RFC sorting: older cookies go first. This works great for {@link MemoryCookieStore} since `Set-Cookie` headers
  989. * are parsed in order, but is not so great for distributed systems.
  990. *
  991. * Sophisticated Stores may wish to set this to some other
  992. * logical clock so that if cookies `A` and `B` are created in the same millisecond, but cookie `A` is created before
  993. * cookie `B`, then `A.creationIndex < B.creationIndex`.
  994. *
  995. * @example
  996. * ```
  997. * const cookies = [
  998. * new Cookie({ key: 'a', value: '' }),
  999. * new Cookie({ key: 'b', value: '' }),
  1000. * new Cookie({ key: 'c', value: '', path: '/path' }),
  1001. * new Cookie({ key: 'd', value: '', path: '/path' }),
  1002. * ]
  1003. * cookies.sort(cookieCompare)
  1004. * // cookie sort order would be ['c', 'd', 'a', 'b']
  1005. * ```
  1006. *
  1007. * @param a - the first Cookie for comparison
  1008. * @param b - the second Cookie for comparison
  1009. * @public
  1010. */
  1011. declare function cookieCompare(a: Cookie, b: Cookie): number;
  1012. /**
  1013. * Configuration options used when calling `CookieJar.setCookie(...)`
  1014. * @public
  1015. */
  1016. interface SetCookieOptions {
  1017. /**
  1018. * Controls if a cookie string should be parsed using `loose` mode or not.
  1019. * See {@link Cookie.parse} and {@link ParseCookieOptions} for more details.
  1020. *
  1021. * Defaults to `false` if not provided.
  1022. */
  1023. loose?: boolean | undefined;
  1024. /**
  1025. * Set this to 'none', 'lax', or 'strict' to enforce SameSite cookies upon storage.
  1026. *
  1027. * - `'strict'` - If the request is on the same "site for cookies" (see the RFC draft
  1028. * for more information), pass this option to add a layer of defense against CSRF.
  1029. *
  1030. * - `'lax'` - If the request is from another site, but is directly because of navigation
  1031. * by the user, such as, `<link type=prefetch>` or `<a href="...">`, then use `lax`.
  1032. *
  1033. * - `'none'` - This indicates a cross-origin request.
  1034. *
  1035. * - `undefined` - SameSite is not enforced! This can be a valid use-case for when
  1036. * CSRF isn't in the threat model of the system being built.
  1037. *
  1038. * Defaults to `undefined` if not provided.
  1039. *
  1040. * @remarks
  1041. * - It is highly recommended that you read {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02##section-8.8 | RFC6265bis - Section 8.8}
  1042. * which discusses security considerations and defence on SameSite cookies in depth.
  1043. */
  1044. sameSiteContext?: 'strict' | 'lax' | 'none' | undefined;
  1045. /**
  1046. * Silently ignore things like parse errors and invalid domains. Store errors aren't ignored by this option.
  1047. *
  1048. * Defaults to `false` if not provided.
  1049. */
  1050. ignoreError?: boolean | undefined;
  1051. /**
  1052. * Indicates if this is an HTTP or non-HTTP API. Affects HttpOnly cookies.
  1053. *
  1054. * Defaults to `true` if not provided.
  1055. */
  1056. http?: boolean | undefined;
  1057. /**
  1058. * Forces the cookie creation and access time of cookies to this value when stored.
  1059. *
  1060. * Defaults to `Date.now()` if not provided.
  1061. */
  1062. now?: Date | undefined;
  1063. }
  1064. /**
  1065. * Configuration options used when calling `CookieJar.getCookies(...)`.
  1066. * @public
  1067. */
  1068. interface GetCookiesOptions {
  1069. /**
  1070. * Indicates if this is an HTTP or non-HTTP API. Affects HttpOnly cookies.
  1071. *
  1072. * Defaults to `true` if not provided.
  1073. */
  1074. http?: boolean | undefined;
  1075. /**
  1076. * Perform `expiry-time` checking of cookies and asynchronously remove expired
  1077. * cookies from the store.
  1078. *
  1079. * @remarks
  1080. * - Using `false` returns expired cookies and does not remove them from the
  1081. * store, which is potentially useful for replaying `Set-Cookie` headers.
  1082. *
  1083. * Defaults to `true` if not provided.
  1084. */
  1085. expire?: boolean | undefined;
  1086. /**
  1087. * If `true`, do not scope cookies by path. If `false`, then RFC-compliant path scoping will be used.
  1088. *
  1089. * @remarks
  1090. * - May not be supported by the underlying store (the default {@link MemoryCookieStore} supports it).
  1091. *
  1092. * Defaults to `false` if not provided.
  1093. */
  1094. allPaths?: boolean | undefined;
  1095. /**
  1096. * Set this to 'none', 'lax', or 'strict' to enforce SameSite cookies upon retrieval.
  1097. *
  1098. * - `'strict'` - If the request is on the same "site for cookies" (see the RFC draft
  1099. * for more information), pass this option to add a layer of defense against CSRF.
  1100. *
  1101. * - `'lax'` - If the request is from another site, but is directly because of navigation
  1102. * by the user, such as, `<link type=prefetch>` or `<a href="...">`, then use `lax`.
  1103. *
  1104. * - `'none'` - This indicates a cross-origin request.
  1105. *
  1106. * - `undefined` - SameSite is not enforced! This can be a valid use-case for when
  1107. * CSRF isn't in the threat model of the system being built.
  1108. *
  1109. * Defaults to `undefined` if not provided.
  1110. *
  1111. * @remarks
  1112. * - It is highly recommended that you read {@link https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02##section-8.8 | RFC6265bis - Section 8.8}
  1113. * which discusses security considerations and defence on SameSite cookies in depth.
  1114. */
  1115. sameSiteContext?: 'none' | 'lax' | 'strict' | undefined;
  1116. /**
  1117. * Flag to indicate if the returned cookies should be sorted or not.
  1118. *
  1119. * Defaults to `undefined` if not provided.
  1120. */
  1121. sort?: boolean | undefined;
  1122. }
  1123. /**
  1124. * Configuration settings to be used with a {@link CookieJar}.
  1125. * @public
  1126. */
  1127. interface CreateCookieJarOptions {
  1128. /**
  1129. * Reject cookies that match those defined in the {@link https://publicsuffix.org/ | Public Suffix List} (e.g.; domains like "com" and "co.uk").
  1130. *
  1131. * Defaults to `true` if not specified.
  1132. */
  1133. rejectPublicSuffixes?: boolean | undefined;
  1134. /**
  1135. * Accept malformed cookies like `bar` and `=bar`, which have an implied empty name but are not RFC-compliant.
  1136. *
  1137. * Defaults to `false` if not specified.
  1138. */
  1139. looseMode?: boolean | undefined;
  1140. /**
  1141. * Controls how cookie prefixes are handled. See {@link PrefixSecurityEnum}.
  1142. *
  1143. * Defaults to `silent` if not specified.
  1144. */
  1145. prefixSecurity?: 'strict' | 'silent' | 'unsafe-disabled' | undefined;
  1146. /**
  1147. * Accepts {@link https://datatracker.ietf.org/doc/html/rfc6761 | special-use domains } such as `local`.
  1148. * This is not in the standard, but is used sometimes on the web and is accepted by most browsers. It is
  1149. * also useful for testing purposes.
  1150. *
  1151. * Defaults to `true` if not specified.
  1152. */
  1153. allowSpecialUseDomain?: boolean | undefined;
  1154. /**
  1155. * Flag to indicate if localhost and loopback addresses with an unsecure scheme should store and retrieve `Secure` cookies.
  1156. *
  1157. * If `true`, localhost, loopback addresses or similarly local addresses are treated as secure contexts
  1158. * and thus will store and retrieve `Secure` cookies even with an unsecure scheme.
  1159. *
  1160. * If `false`, only secure schemes (`https` and `wss`) will store and retrieve `Secure` cookies.
  1161. *
  1162. * @remarks
  1163. * When set to `true`, the {@link https://w3c.github.io/webappsec-secure-contexts/#potentially-trustworthy-origin | potentially trustworthy}
  1164. * algorithm is followed to determine if a URL is considered a secure context.
  1165. */
  1166. allowSecureOnLocal?: boolean | undefined;
  1167. }
  1168. /**
  1169. * A CookieJar is for storage and retrieval of {@link Cookie} objects as defined in
  1170. * {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.3 | RFC6265 - Section 5.3}.
  1171. *
  1172. * It also supports a pluggable persistence layer via {@link Store}.
  1173. * @public
  1174. */
  1175. declare class CookieJar {
  1176. private readonly rejectPublicSuffixes;
  1177. private readonly enableLooseMode;
  1178. private readonly allowSpecialUseDomain;
  1179. private readonly allowSecureOnLocal;
  1180. /**
  1181. * The configured {@link Store} for the {@link CookieJar}.
  1182. */
  1183. readonly store: Store;
  1184. /**
  1185. * The configured {@link PrefixSecurityEnum} value for the {@link CookieJar}.
  1186. */
  1187. readonly prefixSecurity: string;
  1188. /**
  1189. * Creates a new `CookieJar` instance.
  1190. *
  1191. * @remarks
  1192. * - If a custom store is not passed to the constructor, an in-memory store ({@link MemoryCookieStore} will be created and used.
  1193. * - If a boolean value is passed as the `options` parameter, this is equivalent to passing `{ rejectPublicSuffixes: <value> }`
  1194. *
  1195. * @param store - a custom {@link Store} implementation (defaults to {@link MemoryCookieStore})
  1196. * @param options - configures how cookies are processed by the cookie jar
  1197. */
  1198. constructor(store?: Nullable<Store>, options?: CreateCookieJarOptions | boolean);
  1199. private callSync;
  1200. /**
  1201. * Attempt to set the {@link Cookie} in the {@link CookieJar}.
  1202. *
  1203. * @remarks
  1204. * - If successfully persisted, the {@link Cookie} will have updated
  1205. * {@link Cookie.creation}, {@link Cookie.lastAccessed} and {@link Cookie.hostOnly}
  1206. * properties.
  1207. *
  1208. * - As per the RFC, the {@link Cookie.hostOnly} flag is set if there was no `Domain={value}`
  1209. * attribute on the cookie string. The {@link Cookie.domain} property is set to the
  1210. * fully-qualified hostname of `currentUrl` in this case. Matching this cookie requires an
  1211. * exact hostname match (not a {@link domainMatch} as per usual)
  1212. *
  1213. * @param cookie - The cookie object or cookie string to store. A string value will be parsed into a cookie using {@link Cookie.parse}.
  1214. * @param url - The domain to store the cookie with.
  1215. * @param callback - A function to call after a cookie has been successfully stored.
  1216. * @public
  1217. */
  1218. setCookie(cookie: string | Cookie, url: string | URL, callback: Callback<Cookie | undefined>): void;
  1219. /**
  1220. * Attempt to set the {@link Cookie} in the {@link CookieJar}.
  1221. *
  1222. * @remarks
  1223. * - If successfully persisted, the {@link Cookie} will have updated
  1224. * {@link Cookie.creation}, {@link Cookie.lastAccessed} and {@link Cookie.hostOnly}
  1225. * properties.
  1226. *
  1227. * - As per the RFC, the {@link Cookie.hostOnly} flag is set if there was no `Domain={value}`
  1228. * attribute on the cookie string. The {@link Cookie.domain} property is set to the
  1229. * fully-qualified hostname of `currentUrl` in this case. Matching this cookie requires an
  1230. * exact hostname match (not a {@link domainMatch} as per usual)
  1231. *
  1232. * @param cookie - The cookie object or cookie string to store. A string value will be parsed into a cookie using {@link Cookie.parse}.
  1233. * @param url - The domain to store the cookie with.
  1234. * @param options - Configuration settings to use when storing the cookie.
  1235. * @param callback - A function to call after a cookie has been successfully stored.
  1236. * @public
  1237. */
  1238. setCookie(cookie: string | Cookie, url: string | URL, options: SetCookieOptions, callback: Callback<Cookie | undefined>): void;
  1239. /**
  1240. * Attempt to set the {@link Cookie} in the {@link CookieJar}.
  1241. *
  1242. * @remarks
  1243. * - If successfully persisted, the {@link Cookie} will have updated
  1244. * {@link Cookie.creation}, {@link Cookie.lastAccessed} and {@link Cookie.hostOnly}
  1245. * properties.
  1246. *
  1247. * - As per the RFC, the {@link Cookie.hostOnly} flag is set if there was no `Domain={value}`
  1248. * attribute on the cookie string. The {@link Cookie.domain} property is set to the
  1249. * fully-qualified hostname of `currentUrl` in this case. Matching this cookie requires an
  1250. * exact hostname match (not a {@link domainMatch} as per usual)
  1251. *
  1252. * @param cookie - The cookie object or cookie string to store. A string value will be parsed into a cookie using {@link Cookie.parse}.
  1253. * @param url - The domain to store the cookie with.
  1254. * @param options - Configuration settings to use when storing the cookie.
  1255. * @public
  1256. */
  1257. setCookie(cookie: string | Cookie, url: string | URL, options?: SetCookieOptions): Promise<Cookie | undefined>;
  1258. /**
  1259. * @internal No doc because this is an overload that supports the implementation
  1260. */
  1261. setCookie(cookie: string | Cookie, url: string | URL, options: SetCookieOptions | Callback<Cookie | undefined>, callback?: Callback<Cookie | undefined>): unknown;
  1262. /**
  1263. * Synchronously attempt to set the {@link Cookie} in the {@link CookieJar}.
  1264. *
  1265. * <strong>Note:</strong> Only works if the configured {@link Store} is also synchronous.
  1266. *
  1267. * @remarks
  1268. * - If successfully persisted, the {@link Cookie} will have updated
  1269. * {@link Cookie.creation}, {@link Cookie.lastAccessed} and {@link Cookie.hostOnly}
  1270. * properties.
  1271. *
  1272. * - As per the RFC, the {@link Cookie.hostOnly} flag is set if there was no `Domain={value}`
  1273. * attribute on the cookie string. The {@link Cookie.domain} property is set to the
  1274. * fully-qualified hostname of `currentUrl` in this case. Matching this cookie requires an
  1275. * exact hostname match (not a {@link domainMatch} as per usual)
  1276. *
  1277. * @param cookie - The cookie object or cookie string to store. A string value will be parsed into a cookie using {@link Cookie.parse}.
  1278. * @param url - The domain to store the cookie with.
  1279. * @param options - Configuration settings to use when storing the cookie.
  1280. * @public
  1281. */
  1282. setCookieSync(cookie: string | Cookie, url: string, options?: SetCookieOptions): Cookie | undefined;
  1283. /**
  1284. * Retrieve the list of cookies that can be sent in a Cookie header for the
  1285. * current URL.
  1286. *
  1287. * @remarks
  1288. * - The array of cookies returned will be sorted according to {@link cookieCompare}.
  1289. *
  1290. * - The {@link Cookie.lastAccessed} property will be updated on all returned cookies.
  1291. *
  1292. * @param url - The domain to store the cookie with.
  1293. */
  1294. getCookies(url: string): Promise<Cookie[]>;
  1295. /**
  1296. * Retrieve the list of cookies that can be sent in a Cookie header for the
  1297. * current URL.
  1298. *
  1299. * @remarks
  1300. * - The array of cookies returned will be sorted according to {@link cookieCompare}.
  1301. *
  1302. * - The {@link Cookie.lastAccessed} property will be updated on all returned cookies.
  1303. *
  1304. * @param url - The domain to store the cookie with.
  1305. * @param callback - A function to call after a cookie has been successfully retrieved.
  1306. */
  1307. getCookies(url: string, callback: Callback<Cookie[]>): void;
  1308. /**
  1309. * Retrieve the list of cookies that can be sent in a Cookie header for the
  1310. * current URL.
  1311. *
  1312. * @remarks
  1313. * - The array of cookies returned will be sorted according to {@link cookieCompare}.
  1314. *
  1315. * - The {@link Cookie.lastAccessed} property will be updated on all returned cookies.
  1316. *
  1317. * @param url - The domain to store the cookie with.
  1318. * @param options - Configuration settings to use when retrieving the cookies.
  1319. * @param callback - A function to call after a cookie has been successfully retrieved.
  1320. */
  1321. getCookies(url: string | URL, options: GetCookiesOptions | undefined, callback: Callback<Cookie[]>): void;
  1322. /**
  1323. * Retrieve the list of cookies that can be sent in a Cookie header for the
  1324. * current URL.
  1325. *
  1326. * @remarks
  1327. * - The array of cookies returned will be sorted according to {@link cookieCompare}.
  1328. *
  1329. * - The {@link Cookie.lastAccessed} property will be updated on all returned cookies.
  1330. *
  1331. * @param url - The domain to store the cookie with.
  1332. * @param options - Configuration settings to use when retrieving the cookies.
  1333. */
  1334. getCookies(url: string | URL, options?: GetCookiesOptions): Promise<Cookie[]>;
  1335. /**
  1336. * @internal No doc because this is an overload that supports the implementation
  1337. */
  1338. getCookies(url: string | URL, options: GetCookiesOptions | undefined | Callback<Cookie[]>, callback?: Callback<Cookie[]>): unknown;
  1339. /**
  1340. * Synchronously retrieve the list of cookies that can be sent in a Cookie header for the
  1341. * current URL.
  1342. *
  1343. * <strong>Note</strong>: Only works if the configured Store is also synchronous.
  1344. *
  1345. * @remarks
  1346. * - The array of cookies returned will be sorted according to {@link cookieCompare}.
  1347. *
  1348. * - The {@link Cookie.lastAccessed} property will be updated on all returned cookies.
  1349. *
  1350. * @param url - The domain to store the cookie with.
  1351. * @param options - Configuration settings to use when retrieving the cookies.
  1352. */
  1353. getCookiesSync(url: string, options?: GetCookiesOptions): Cookie[];
  1354. /**
  1355. * Accepts the same options as `.getCookies()` but returns a string suitable for a
  1356. * `Cookie` header rather than an Array.
  1357. *
  1358. * @param url - The domain to store the cookie with.
  1359. * @param options - Configuration settings to use when retrieving the cookies.
  1360. * @param callback - A function to call after the `Cookie` header string has been created.
  1361. */
  1362. getCookieString(url: string, options: GetCookiesOptions, callback: Callback<string | undefined>): void;
  1363. /**
  1364. * Accepts the same options as `.getCookies()` but returns a string suitable for a
  1365. * `Cookie` header rather than an Array.
  1366. *
  1367. * @param url - The domain to store the cookie with.
  1368. * @param callback - A function to call after the `Cookie` header string has been created.
  1369. */
  1370. getCookieString(url: string, callback: Callback<string | undefined>): void;
  1371. /**
  1372. * Accepts the same options as `.getCookies()` but returns a string suitable for a
  1373. * `Cookie` header rather than an Array.
  1374. *
  1375. * @param url - The domain to store the cookie with.
  1376. * @param options - Configuration settings to use when retrieving the cookies.
  1377. */
  1378. getCookieString(url: string, options?: GetCookiesOptions): Promise<string>;
  1379. /**
  1380. * @internal No doc because this is an overload that supports the implementation
  1381. */
  1382. getCookieString(url: string, options: GetCookiesOptions | Callback<string | undefined>, callback?: Callback<string | undefined>): unknown;
  1383. /**
  1384. * Synchronous version of `.getCookieString()`. Accepts the same options as `.getCookies()` but returns a string suitable for a
  1385. * `Cookie` header rather than an Array.
  1386. *
  1387. * <strong>Note</strong>: Only works if the configured Store is also synchronous.
  1388. *
  1389. * @param url - The domain to store the cookie with.
  1390. * @param options - Configuration settings to use when retrieving the cookies.
  1391. */
  1392. getCookieStringSync(url: string, options?: GetCookiesOptions): string;
  1393. /**
  1394. * Returns an array of strings suitable for `Set-Cookie` headers. Accepts the same options
  1395. * as `.getCookies()`.
  1396. *
  1397. * @param url - The domain to store the cookie with.
  1398. * @param callback - A function to call after the `Set-Cookie` header strings have been created.
  1399. */
  1400. getSetCookieStrings(url: string, callback: Callback<string[] | undefined>): void;
  1401. /**
  1402. * Returns an array of strings suitable for `Set-Cookie` headers. Accepts the same options
  1403. * as `.getCookies()`.
  1404. *
  1405. * @param url - The domain to store the cookie with.
  1406. * @param options - Configuration settings to use when retrieving the cookies.
  1407. * @param callback - A function to call after the `Set-Cookie` header strings have been created.
  1408. */
  1409. getSetCookieStrings(url: string, options: GetCookiesOptions, callback: Callback<string[] | undefined>): void;
  1410. /**
  1411. * Returns an array of strings suitable for `Set-Cookie` headers. Accepts the same options
  1412. * as `.getCookies()`.
  1413. *
  1414. * @param url - The domain to store the cookie with.
  1415. * @param options - Configuration settings to use when retrieving the cookies.
  1416. */
  1417. getSetCookieStrings(url: string, options?: GetCookiesOptions): Promise<string[] | undefined>;
  1418. /**
  1419. * @internal No doc because this is an overload that supports the implementation
  1420. */
  1421. getSetCookieStrings(url: string, options: GetCookiesOptions, callback?: Callback<string[] | undefined>): unknown;
  1422. /**
  1423. * Synchronous version of `.getSetCookieStrings()`. Returns an array of strings suitable for `Set-Cookie` headers.
  1424. * Accepts the same options as `.getCookies()`.
  1425. *
  1426. * <strong>Note</strong>: Only works if the configured Store is also synchronous.
  1427. *
  1428. * @param url - The domain to store the cookie with.
  1429. * @param options - Configuration settings to use when retrieving the cookies.
  1430. */
  1431. getSetCookieStringsSync(url: string, options?: GetCookiesOptions): string[];
  1432. /**
  1433. * Serialize the CookieJar if the underlying store supports `.getAllCookies`.
  1434. * @param callback - A function to call after the CookieJar has been serialized
  1435. */
  1436. serialize(callback: Callback<SerializedCookieJar>): void;
  1437. /**
  1438. * Serialize the CookieJar if the underlying store supports `.getAllCookies`.
  1439. */
  1440. serialize(): Promise<SerializedCookieJar>;
  1441. /**
  1442. * Serialize the CookieJar if the underlying store supports `.getAllCookies`.
  1443. *
  1444. * <strong>Note</strong>: Only works if the configured Store is also synchronous.
  1445. */
  1446. serializeSync(): SerializedCookieJar | undefined;
  1447. /**
  1448. * Alias of {@link CookieJar.serializeSync}. Allows the cookie to be serialized
  1449. * with `JSON.stringify(cookieJar)`.
  1450. */
  1451. toJSON(): SerializedCookieJar | undefined;
  1452. /**
  1453. * Use the class method CookieJar.deserialize instead of calling this directly
  1454. * @internal
  1455. */
  1456. _importCookies(serialized: unknown, callback: Callback<CookieJar>): void;
  1457. /**
  1458. * @internal
  1459. */
  1460. _importCookiesSync(serialized: unknown): void;
  1461. /**
  1462. * Produces a deep clone of this CookieJar. Modifications to the original do
  1463. * not affect the clone, and vice versa.
  1464. *
  1465. * @remarks
  1466. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1467. *
  1468. * - Transferring between store types is supported so long as the source
  1469. * implements `.getAllCookies()` and the destination implements `.putCookie()`.
  1470. *
  1471. * @param callback - A function to call when the CookieJar is cloned.
  1472. */
  1473. clone(callback: Callback<CookieJar>): void;
  1474. /**
  1475. * Produces a deep clone of this CookieJar. Modifications to the original do
  1476. * not affect the clone, and vice versa.
  1477. *
  1478. * @remarks
  1479. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1480. *
  1481. * - Transferring between store types is supported so long as the source
  1482. * implements `.getAllCookies()` and the destination implements `.putCookie()`.
  1483. *
  1484. * @param newStore - The target {@link Store} to clone cookies into.
  1485. * @param callback - A function to call when the CookieJar is cloned.
  1486. */
  1487. clone(newStore: Store, callback: Callback<CookieJar>): void;
  1488. /**
  1489. * Produces a deep clone of this CookieJar. Modifications to the original do
  1490. * not affect the clone, and vice versa.
  1491. *
  1492. * @remarks
  1493. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1494. *
  1495. * - Transferring between store types is supported so long as the source
  1496. * implements `.getAllCookies()` and the destination implements `.putCookie()`.
  1497. *
  1498. * @param newStore - The target {@link Store} to clone cookies into.
  1499. */
  1500. clone(newStore?: Store): Promise<CookieJar>;
  1501. /**
  1502. * @internal
  1503. */
  1504. _cloneSync(newStore?: Store): CookieJar | undefined;
  1505. /**
  1506. * Produces a deep clone of this CookieJar. Modifications to the original do
  1507. * not affect the clone, and vice versa.
  1508. *
  1509. * <strong>Note</strong>: Only works if both the configured Store and destination
  1510. * Store are synchronous.
  1511. *
  1512. * @remarks
  1513. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1514. *
  1515. * - Transferring between store types is supported so long as the source
  1516. * implements `.getAllCookies()` and the destination implements `.putCookie()`.
  1517. *
  1518. * @param newStore - The target {@link Store} to clone cookies into.
  1519. */
  1520. cloneSync(newStore?: Store): CookieJar | undefined;
  1521. /**
  1522. * Removes all cookies from the CookieJar.
  1523. *
  1524. * @remarks
  1525. * - This is a new backwards-compatible feature of tough-cookie version 2.5,
  1526. * so not all Stores will implement it efficiently. For Stores that do not
  1527. * implement `removeAllCookies`, the fallback is to call `removeCookie` after
  1528. * `getAllCookies`.
  1529. *
  1530. * - If `getAllCookies` fails or isn't implemented in the Store, an error is returned.
  1531. *
  1532. * - If one or more of the `removeCookie` calls fail, only the first error is returned.
  1533. *
  1534. * @param callback - A function to call when all the cookies have been removed.
  1535. */
  1536. removeAllCookies(callback: ErrorCallback): void;
  1537. /**
  1538. * Removes all cookies from the CookieJar.
  1539. *
  1540. * @remarks
  1541. * - This is a new backwards-compatible feature of tough-cookie version 2.5,
  1542. * so not all Stores will implement it efficiently. For Stores that do not
  1543. * implement `removeAllCookies`, the fallback is to call `removeCookie` after
  1544. * `getAllCookies`.
  1545. *
  1546. * - If `getAllCookies` fails or isn't implemented in the Store, an error is returned.
  1547. *
  1548. * - If one or more of the `removeCookie` calls fail, only the first error is returned.
  1549. */
  1550. removeAllCookies(): Promise<void>;
  1551. /**
  1552. * Removes all cookies from the CookieJar.
  1553. *
  1554. * <strong>Note</strong>: Only works if the configured Store is also synchronous.
  1555. *
  1556. * @remarks
  1557. * - This is a new backwards-compatible feature of tough-cookie version 2.5,
  1558. * so not all Stores will implement it efficiently. For Stores that do not
  1559. * implement `removeAllCookies`, the fallback is to call `removeCookie` after
  1560. * `getAllCookies`.
  1561. *
  1562. * - If `getAllCookies` fails or isn't implemented in the Store, an error is returned.
  1563. *
  1564. * - If one or more of the `removeCookie` calls fail, only the first error is returned.
  1565. */
  1566. removeAllCookiesSync(): void;
  1567. /**
  1568. * A new CookieJar is created and the serialized {@link Cookie} values are added to
  1569. * the underlying store. Each {@link Cookie} is added via `store.putCookie(...)` in
  1570. * the order in which they appear in the serialization.
  1571. *
  1572. * @remarks
  1573. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1574. *
  1575. * - As a convenience, if `strOrObj` is a string, it is passed through `JSON.parse` first.
  1576. *
  1577. * @param strOrObj - A JSON string or object representing the deserialized cookies.
  1578. * @param callback - A function to call after the {@link CookieJar} has been deserialized.
  1579. */
  1580. static deserialize(strOrObj: string | object, callback: Callback<CookieJar>): void;
  1581. /**
  1582. * A new CookieJar is created and the serialized {@link Cookie} values are added to
  1583. * the underlying store. Each {@link Cookie} is added via `store.putCookie(...)` in
  1584. * the order in which they appear in the serialization.
  1585. *
  1586. * @remarks
  1587. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1588. *
  1589. * - As a convenience, if `strOrObj` is a string, it is passed through `JSON.parse` first.
  1590. *
  1591. * @param strOrObj - A JSON string or object representing the deserialized cookies.
  1592. * @param store - The underlying store to persist the deserialized cookies into.
  1593. * @param callback - A function to call after the {@link CookieJar} has been deserialized.
  1594. */
  1595. static deserialize(strOrObj: string | object, store: Store, callback: Callback<CookieJar>): void;
  1596. /**
  1597. * A new CookieJar is created and the serialized {@link Cookie} values are added to
  1598. * the underlying store. Each {@link Cookie} is added via `store.putCookie(...)` in
  1599. * the order in which they appear in the serialization.
  1600. *
  1601. * @remarks
  1602. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1603. *
  1604. * - As a convenience, if `strOrObj` is a string, it is passed through `JSON.parse` first.
  1605. *
  1606. * @param strOrObj - A JSON string or object representing the deserialized cookies.
  1607. * @param store - The underlying store to persist the deserialized cookies into.
  1608. */
  1609. static deserialize(strOrObj: string | object, store?: Store): Promise<CookieJar>;
  1610. /**
  1611. * @internal No doc because this is an overload that supports the implementation
  1612. */
  1613. static deserialize(strOrObj: string | object, store?: Store | Callback<CookieJar>, callback?: Callback<CookieJar>): unknown;
  1614. /**
  1615. * A new CookieJar is created and the serialized {@link Cookie} values are added to
  1616. * the underlying store. Each {@link Cookie} is added via `store.putCookie(...)` in
  1617. * the order in which they appear in the serialization.
  1618. *
  1619. * <strong>Note</strong>: Only works if the configured Store is also synchronous.
  1620. *
  1621. * @remarks
  1622. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1623. *
  1624. * - As a convenience, if `strOrObj` is a string, it is passed through `JSON.parse` first.
  1625. *
  1626. * @param strOrObj - A JSON string or object representing the deserialized cookies.
  1627. * @param store - The underlying store to persist the deserialized cookies into.
  1628. */
  1629. static deserializeSync(strOrObj: string | SerializedCookieJar, store?: Store): CookieJar;
  1630. /**
  1631. * Alias of {@link CookieJar.deserializeSync}.
  1632. *
  1633. * @remarks
  1634. * - When no {@link Store} is provided, a new {@link MemoryCookieStore} will be used.
  1635. *
  1636. * - As a convenience, if `strOrObj` is a string, it is passed through `JSON.parse` first.
  1637. *
  1638. * @param jsonString - A JSON string or object representing the deserialized cookies.
  1639. * @param store - The underlying store to persist the deserialized cookies into.
  1640. */
  1641. static fromJSON(jsonString: string | SerializedCookieJar, store?: Store): CookieJar;
  1642. }
  1643. /**
  1644. * Given a current request/response path, gives the path appropriate for storing
  1645. * in a cookie. This is basically the "directory" of a "file" in the path, but
  1646. * is specified by {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.1.4 | RFC6265 - Section 5.1.4}.
  1647. *
  1648. * @remarks
  1649. * ### RFC6265 - Section 5.1.4
  1650. *
  1651. * The user agent MUST use an algorithm equivalent to the following algorithm to compute the default-path of a cookie:
  1652. *
  1653. * 1. Let uri-path be the path portion of the request-uri if such a
  1654. * portion exists (and empty otherwise). For example, if the
  1655. * request-uri contains just a path (and optional query string),
  1656. * then the uri-path is that path (without the %x3F ("?") character
  1657. * or query string), and if the request-uri contains a full
  1658. * absoluteURI, the uri-path is the path component of that URI.
  1659. *
  1660. * 2. If the uri-path is empty or if the first character of the uri-
  1661. * path is not a %x2F ("/") character, output %x2F ("/") and skip
  1662. * the remaining steps.
  1663. *
  1664. * 3. If the uri-path contains no more than one %x2F ("/") character,
  1665. * output %x2F ("/") and skip the remaining step.
  1666. *
  1667. * 4. Output the characters of the uri-path from the first character up
  1668. * to, but not including, the right-most %x2F ("/").
  1669. *
  1670. * @example
  1671. * ```
  1672. * defaultPath('') === '/'
  1673. * defaultPath('/some-path') === '/'
  1674. * defaultPath('/some-parent-path/some-path') === '/some-parent-path'
  1675. * defaultPath('relative-path') === '/'
  1676. * ```
  1677. *
  1678. * @param path - the path portion of the request-uri (excluding the hostname, query, fragment, and so on)
  1679. * @public
  1680. */
  1681. declare function defaultPath(path?: Nullable<string>): string;
  1682. /**
  1683. * Answers "does this real domain match the domain in a cookie?". The `domain` is the "current" domain name and the
  1684. * `cookieDomain` is the "cookie" domain name. Matches according to {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.1.3 | RFC6265 - Section 5.1.3},
  1685. * but it helps to think of it as a "suffix match".
  1686. *
  1687. * @remarks
  1688. * ### 5.1.3. Domain Matching
  1689. *
  1690. * A string domain-matches a given domain string if at least one of the
  1691. * following conditions hold:
  1692. *
  1693. * - The domain string and the string are identical. (Note that both
  1694. * the domain string and the string will have been canonicalized to
  1695. * lower case at this point.)
  1696. *
  1697. * - All of the following conditions hold:
  1698. *
  1699. * - The domain string is a suffix of the string.
  1700. *
  1701. * - The last character of the string that is not included in the
  1702. * domain string is a %x2E (".") character.
  1703. *
  1704. * - The string is a host name (i.e., not an IP address).
  1705. *
  1706. * @example
  1707. * ```
  1708. * domainMatch('example.com', 'example.com') === true
  1709. * domainMatch('eXaMpLe.cOm', 'ExAmPlE.CoM') === true
  1710. * domainMatch('no.ca', 'yes.ca') === false
  1711. * ```
  1712. *
  1713. * @param domain - The domain string to test
  1714. * @param cookieDomain - The cookie domain string to match against
  1715. * @param canonicalize - The canonicalize parameter toggles whether the domain parameters get normalized with canonicalDomain or not
  1716. * @public
  1717. */
  1718. declare function domainMatch(domain?: Nullable<string>, cookieDomain?: Nullable<string>, canonicalize?: boolean): boolean | undefined;
  1719. /**
  1720. * Format a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date | Date} into
  1721. * the {@link https://www.rfc-editor.org/rfc/rfc2616#section-3.3.1 | preferred Internet standard format}
  1722. * defined in {@link https://www.rfc-editor.org/rfc/rfc822#section-5 | RFC822} and
  1723. * updated in {@link https://www.rfc-editor.org/rfc/rfc1123#page-55 | RFC1123}.
  1724. *
  1725. * @example
  1726. * ```
  1727. * formatDate(new Date(0)) === 'Thu, 01 Jan 1970 00:00:00 GMT`
  1728. * ```
  1729. *
  1730. * @param date - the date value to format
  1731. * @public
  1732. */
  1733. declare function formatDate(date: Date): string;
  1734. /**
  1735. * Parse a cookie date string into a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date | Date}. Parses according to
  1736. * {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-5.1.1 | RFC6265 - Section 5.1.1}, not
  1737. * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse | Date.parse()}.
  1738. *
  1739. * @remarks
  1740. *
  1741. * ### RFC6265 - 5.1.1. Dates
  1742. *
  1743. * The user agent MUST use an algorithm equivalent to the following
  1744. * algorithm to parse a cookie-date. Note that the various boolean
  1745. * flags defined as a part of the algorithm (i.e., found-time, found-
  1746. * day-of-month, found-month, found-year) are initially "not set".
  1747. *
  1748. * 1. Using the grammar below, divide the cookie-date into date-tokens.
  1749. *
  1750. * ```
  1751. * cookie-date = *delimiter date-token-list *delimiter
  1752. * date-token-list = date-token *( 1*delimiter date-token )
  1753. * date-token = 1*non-delimiter
  1754. *
  1755. * delimiter = %x09 / %x20-2F / %x3B-40 / %x5B-60 / %x7B-7E
  1756. * non-delimiter = %x00-08 / %x0A-1F / DIGIT / ":" / ALPHA / %x7F-FF
  1757. * non-digit = %x00-2F / %x3A-FF
  1758. *
  1759. * day-of-month = 1*2DIGIT ( non-digit *OCTET )
  1760. * month = ( "jan" / "feb" / "mar" / "apr" /
  1761. * "may" / "jun" / "jul" / "aug" /
  1762. * "sep" / "oct" / "nov" / "dec" ) *OCTET
  1763. * year = 2*4DIGIT ( non-digit *OCTET )
  1764. * time = hms-time ( non-digit *OCTET )
  1765. * hms-time = time-field ":" time-field ":" time-field
  1766. * time-field = 1*2DIGIT
  1767. * ```
  1768. *
  1769. * 2. Process each date-token sequentially in the order the date-tokens
  1770. * appear in the cookie-date:
  1771. *
  1772. * 1. If the found-time flag is not set and the token matches the
  1773. * time production, set the found-time flag and set the hour-
  1774. * value, minute-value, and second-value to the numbers denoted
  1775. * by the digits in the date-token, respectively. Skip the
  1776. * remaining sub-steps and continue to the next date-token.
  1777. *
  1778. * 2. If the found-day-of-month flag is not set and the date-token
  1779. * matches the day-of-month production, set the found-day-of-
  1780. * month flag and set the day-of-month-value to the number
  1781. * denoted by the date-token. Skip the remaining sub-steps and
  1782. * continue to the next date-token.
  1783. *
  1784. * 3. If the found-month flag is not set and the date-token matches
  1785. * the month production, set the found-month flag and set the
  1786. * month-value to the month denoted by the date-token. Skip the
  1787. * remaining sub-steps and continue to the next date-token.
  1788. *
  1789. * 4. If the found-year flag is not set and the date-token matches
  1790. * the year production, set the found-year flag and set the
  1791. * year-value to the number denoted by the date-token. Skip the
  1792. * remaining sub-steps and continue to the next date-token.
  1793. *
  1794. * 3. If the year-value is greater than or equal to 70 and less than or
  1795. * equal to 99, increment the year-value by 1900.
  1796. *
  1797. * 4. If the year-value is greater than or equal to 0 and less than or
  1798. * equal to 69, increment the year-value by 2000.
  1799. *
  1800. * 1. NOTE: Some existing user agents interpret two-digit years differently.
  1801. *
  1802. * 5. Abort these steps and fail to parse the cookie-date if:
  1803. *
  1804. * - at least one of the found-day-of-month, found-month, found-
  1805. * year, or found-time flags is not set,
  1806. *
  1807. * - the day-of-month-value is less than 1 or greater than 31,
  1808. *
  1809. * - the year-value is less than 1601,
  1810. *
  1811. * - the hour-value is greater than 23,
  1812. *
  1813. * - the minute-value is greater than 59, or
  1814. *
  1815. * - the second-value is greater than 59.
  1816. *
  1817. * (Note that leap seconds cannot be represented in this syntax.)
  1818. *
  1819. * 6. Let the parsed-cookie-date be the date whose day-of-month, month,
  1820. * year, hour, minute, and second (in UTC) are the day-of-month-
  1821. * value, the month-value, the year-value, the hour-value, the
  1822. * minute-value, and the second-value, respectively. If no such
  1823. * date exists, abort these steps and fail to parse the cookie-date.
  1824. *
  1825. * 7. Return the parsed-cookie-date as the result of this algorithm.
  1826. *
  1827. * @example
  1828. * ```
  1829. * parseDate('Wed, 09 Jun 2021 10:18:14 GMT')
  1830. * ```
  1831. *
  1832. * @param cookieDate - the cookie date string
  1833. * @public
  1834. */
  1835. declare function parseDate(cookieDate: Nullable<string>): Date | undefined;
  1836. /**
  1837. * Generates the permutation of all possible values that {@link pathMatch} the `path` parameter.
  1838. * The array is in longest-to-shortest order. Useful when building custom {@link Store} implementations.
  1839. *
  1840. * @example
  1841. * ```
  1842. * permutePath('/foo/bar/')
  1843. * // ['/foo/bar/', '/foo/bar', '/foo', '/']
  1844. * ```
  1845. *
  1846. * @param path - the path to generate permutations for
  1847. * @public
  1848. */
  1849. declare function permutePath(path: string): string[];
  1850. /**
  1851. * {@inheritDoc Cookie.parse}
  1852. * @public
  1853. */
  1854. declare function parse(str: string, options?: ParseCookieOptions): Cookie | undefined;
  1855. /**
  1856. * {@inheritDoc Cookie.fromJSON}
  1857. * @public
  1858. */
  1859. declare function fromJSON(str: unknown): Cookie | undefined;
  1860. export { type Callback, Cookie, CookieJar, type CreateCookieJarOptions, type CreateCookieOptions, type ErrorCallback, type GetCookiesOptions, type GetPublicSuffixOptions, MemoryCookieStore, type MemoryCookieStoreIndex, type Nullable, ParameterError, type ParseCookieOptions, PrefixSecurityEnum, type SerializedCookie, type SerializedCookieJar, type SetCookieOptions, Store, canonicalDomain, cookieCompare, defaultPath, domainMatch, formatDate, fromJSON, getPublicSuffix, parse, parseDate, pathMatch, permuteDomain, permutePath, version };