UNPKG

9.47 kBTypeScriptView Raw
1// Type definitions for tough-cookie 4.0
2// Project: https://github.com/salesforce/tough-cookie
3// Definitions by: Leonard Thieu <https://github.com/leonard-thieu>
4// LiJinyao <https://github.com/LiJinyao>
5// Michael Wei <https://github.com/no2chem>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.2
8
9export const version: string;
10
11export const PrefixSecurityEnum: Readonly<{
12 DISABLED: string;
13 SILENT: string;
14 STRICT: string;
15}>;
16
17/**
18 * Parse a cookie date string into a Date.
19 * Parses according to RFC6265 Section 5.1.1, not Date.parse().
20 */
21export function parseDate(string: string): Date;
22
23/**
24 * Format a Date into a RFC1123 string (the RFC6265-recommended format).
25 */
26export function formatDate(date: Date): string;
27
28/**
29 * Transforms a domain-name into a canonical domain-name.
30 * The canonical domain-name is a trimmed, lowercased, stripped-of-leading-dot
31 * and optionally punycode-encoded domain-name (Section 5.1.2 of RFC6265).
32 * For the most part, this function is idempotent (can be run again on its output without ill effects).
33 */
34export function canonicalDomain(str: string): string;
35
36/**
37 * Answers "does this real domain match the domain in a cookie?".
38 * The str is the "current" domain-name and the domStr is the "cookie" domain-name.
39 * Matches according to RFC6265 Section 5.1.3, but it helps to think of it as a "suffix match".
40 *
41 * The canonicalize parameter will run the other two parameters through canonicalDomain or not.
42 */
43export function domainMatch(str: string, domStr: string, canonicalize?: boolean): boolean;
44
45/**
46 * Given a current request/response path, gives the Path apropriate for storing in a cookie.
47 * This is basically the "directory" of a "file" in the path, but is specified by Section 5.1.4 of the RFC.
48 *
49 * The path parameter MUST be only the pathname part of a URI (i.e. excludes the hostname, query, fragment, etc.).
50 * This is the .pathname property of node's uri.parse() output.
51 */
52export function defaultPath(path: string): string;
53
54/**
55 * Answers "does the request-path path-match a given cookie-path?" as per RFC6265 Section 5.1.4.
56 * Returns a boolean.
57 *
58 * This is essentially a prefix-match where cookiePath is a prefix of reqPath.
59 */
60export function pathMatch(reqPath: string, cookiePath: string): boolean;
61
62/**
63 * alias for Cookie.parse(cookieString[, options])
64 */
65export function parse(cookieString: string, options?: Cookie.ParseOptions): Cookie | undefined;
66
67/**
68 * alias for Cookie.fromJSON(string)
69 */
70export function fromJSON(string: string): Cookie;
71
72export function getPublicSuffix(hostname: string): string | null;
73
74export function cookieCompare(a: Cookie, b: Cookie): number;
75
76export function permuteDomain(domain: string, allowSpecialUseDomain?: boolean): string[];
77
78export function permutePath(path: string): string[];
79
80export class Cookie {
81 static parse(cookieString: string, options?: Cookie.ParseOptions): Cookie | undefined;
82
83 static fromJSON(strOrObj: string | object): Cookie | null;
84
85 constructor(properties?: Cookie.Properties);
86
87 key: string;
88 value: string;
89 expires: Date | 'Infinity';
90 maxAge: number | 'Infinity' | '-Infinity';
91 domain: string | null;
92 path: string | null;
93 secure: boolean;
94 httpOnly: boolean;
95 extensions: string[] | null;
96 creation: Date | null;
97 creationIndex: number;
98
99 hostOnly: boolean | null;
100 pathIsDefault: boolean | null;
101 lastAccessed: Date | null;
102 sameSite: string;
103
104 toString(): string;
105
106 cookieString(): string;
107
108 setExpires(exp: Date | string): void;
109
110 setMaxAge(number: number): void;
111
112 expiryTime(now?: number): number;
113
114 expiryDate(now?: number): Date;
115
116 TTL(now?: Date): number | typeof Infinity;
117
118 isPersistent(): boolean;
119
120 canonicalizedDomain(): string | null;
121
122 cdomain(): string | null;
123
124 inspect(): string;
125
126 toJSON(): { [key: string]: any; };
127
128 clone(): Cookie;
129
130 validate(): boolean | string;
131}
132
133export namespace Cookie {
134 interface ParseOptions {
135 loose?: boolean | undefined;
136 }
137
138 interface Properties {
139 key?: string | undefined;
140 value?: string | undefined;
141 expires?: Date | undefined;
142 maxAge?: number | 'Infinity' | '-Infinity' | undefined;
143 domain?: string | undefined;
144 path?: string | undefined;
145 secure?: boolean | undefined;
146 httpOnly?: boolean | undefined;
147 extensions?: string[] | undefined;
148 creation?: Date | undefined;
149 creationIndex?: number | undefined;
150
151 hostOnly?: boolean | undefined;
152 pathIsDefault?: boolean | undefined;
153 lastAccessed?: Date | undefined;
154 sameSite?: string | undefined;
155 }
156
157 interface Serialized {
158 [key: string]: any;
159 }
160}
161
162export class CookieJar {
163 static deserialize(serialized: CookieJar.Serialized | string, store?: Store): Promise<CookieJar>;
164 static deserialize(serialized: CookieJar.Serialized | string, store: Store, cb: (err: Error | null, object: CookieJar) => void): void;
165 static deserialize(serialized: CookieJar.Serialized | string, cb: (err: Error | null, object: CookieJar) => void): void;
166
167 static deserializeSync(serialized: CookieJar.Serialized | string, store?: Store): CookieJar;
168
169 static fromJSON(string: string): CookieJar;
170
171 constructor(store?: Store, options?: CookieJar.Options);
172
173 setCookie(cookieOrString: Cookie | string, currentUrl: string, options?: CookieJar.SetCookieOptions): Promise<Cookie>;
174 setCookie(cookieOrString: Cookie | string, currentUrl: string, options: CookieJar.SetCookieOptions, cb: (err: Error | null, cookie: Cookie) => void): void;
175 setCookie(cookieOrString: Cookie | string, currentUrl: string, cb: (err: Error | null, cookie: Cookie) => void): void;
176
177 setCookieSync(cookieOrString: Cookie | string, currentUrl: string, options?: CookieJar.SetCookieOptions): Cookie;
178
179 getCookies(currentUrl: string, options?: CookieJar.GetCookiesOptions): Promise<Cookie[]>;
180 getCookies(currentUrl: string, options: CookieJar.GetCookiesOptions, cb: (err: Error | null, cookies: Cookie[]) => void): void;
181 getCookies(currentUrl: string, cb: (err: Error | null, cookies: Cookie[]) => void): void;
182
183 getCookiesSync(currentUrl: string, options?: CookieJar.GetCookiesOptions): Cookie[];
184
185 getCookieString(currentUrl: string, options?: CookieJar.GetCookiesOptions): Promise<string>;
186 getCookieString(currentUrl: string, options: CookieJar.GetCookiesOptions, cb: (err: Error | null, cookies: string) => void): void;
187 getCookieString(currentUrl: string, cb: (err: Error | null, cookies: string) => void): void;
188
189 getCookieStringSync(currentUrl: string, options?: CookieJar.GetCookiesOptions): string;
190
191 getSetCookieStrings(currentUrl: string, options?: CookieJar.GetCookiesOptions): Promise<string[]>;
192 getSetCookieStrings(currentUrl: string, options: CookieJar.GetCookiesOptions, cb: (err: Error | null, cookies: string[]) => void): void;
193 getSetCookieStrings(currentUrl: string, cb: (err: Error | null, cookies: string[]) => void): void;
194
195 getSetCookieStringsSync(currentUrl: string, options?: CookieJar.GetCookiesOptions): string[];
196
197 serialize(): Promise<CookieJar.Serialized>;
198 serialize(cb: (err: Error | null, serializedObject: CookieJar.Serialized) => void): void;
199
200 serializeSync(): CookieJar.Serialized;
201
202 toJSON(): CookieJar.Serialized;
203
204 clone(store?: Store): Promise<CookieJar>;
205 clone(store: Store, cb: (err: Error | null, newJar: CookieJar) => void): void;
206 clone(cb: (err: Error | null, newJar: CookieJar) => void): void;
207
208 cloneSync(store?: Store): CookieJar;
209
210 removeAllCookies(): Promise<void>;
211 removeAllCookies(cb: (err: Error | null) => void): void;
212
213 removeAllCookiesSync(): void;
214}
215
216export namespace CookieJar {
217 interface Options {
218 allowSpecialUseDomain?: boolean | undefined;
219 looseMode?: boolean | undefined;
220 rejectPublicSuffixes?: boolean | undefined;
221 prefixSecurity?: string | undefined;
222 }
223
224 interface SetCookieOptions {
225 http?: boolean | undefined;
226 secure?: boolean | undefined;
227 now?: Date | undefined;
228 ignoreError?: boolean | undefined;
229 }
230
231 interface GetCookiesOptions {
232 http?: boolean | undefined;
233 secure?: boolean | undefined;
234 now?: Date | undefined;
235 expire?: boolean | undefined;
236 allPaths?: boolean | undefined;
237 }
238
239 interface Serialized {
240 version: string;
241 storeType: string;
242 rejectPublicSuffixes: boolean;
243 cookies: Cookie.Serialized[];
244 }
245}
246
247export abstract class Store {
248 synchronous: boolean;
249
250 findCookie(domain: string, path: string, key: string, cb: (err: Error | null, cookie: Cookie | null) => void): void;
251
252 findCookies(domain: string, path: string, allowSpecialUseDomain: boolean, cb: (err: Error | null, cookie: Cookie[]) => void): void;
253
254 putCookie(cookie: Cookie, cb: (err: Error | null) => void): void;
255
256 updateCookie(oldCookie: Cookie, newCookie: Cookie, cb: (err: Error | null) => void): void;
257
258 removeCookie(domain: string, path: string, key: string, cb: (err: Error | null) => void): void;
259
260 removeCookies(domain: string, path: string, cb: (err: Error | null) => void): void;
261
262 getAllCookies(cb: (err: Error | null, cookie: Cookie[]) => void): void;
263}
264
265export class MemoryCookieStore extends Store { }