@api-client/core 0.3.1 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/browser.d.ts +1 -1
- package/build/index.d.ts +2 -2
- package/build/index.js.map +1 -1
- package/build/src/models/Backend.d.ts +22 -0
- package/build/src/runtime/store/StoreSdk.d.ts +192 -12
- package/build/src/runtime/store/StoreSdk.js +592 -53
- package/build/src/runtime/store/StoreSdk.js.map +1 -1
- package/package.json +3 -1
- package/src/data/DataReader.ts +11 -0
- package/src/data/DataUtils.ts +108 -0
- package/src/data/JmesparthReader.ts +26 -0
- package/src/data/Json2Xml.ts +190 -0
- package/src/data/JsonReader.ts +41 -0
- package/src/data/PayloadPointer.ts +48 -0
- package/src/data/RequestDataExtractor.ts +133 -0
- package/src/data/UrlEncodedReader.ts +20 -0
- package/src/data/XmlReader.ts +103 -0
- package/src/events/BaseEvents.ts +259 -0
- package/src/events/CustomEvent.ts +27 -0
- package/src/events/EventTypes.ts +19 -0
- package/src/events/Events.ts +19 -0
- package/src/events/authorization/AuthorizationEventTypes.ts +22 -0
- package/src/events/authorization/AuthorizationEvents.ts +61 -0
- package/src/events/cookies/CookieEventTypes.ts +13 -0
- package/src/events/cookies/CookieEvents.ts +157 -0
- package/src/events/encryption/EncryptionEventTypes.ts +4 -0
- package/src/events/encryption/EncryptionEvents.ts +51 -0
- package/src/events/environment/EnvironmentEventTypes.ts +3 -0
- package/src/events/environment/EnvironmentEvents.ts +24 -0
- package/src/events/models/ClientCertificateEvents.ts +87 -0
- package/src/events/models/ModelEventTypes.ts +47 -0
- package/src/events/models/ModelEvents.ts +7 -0
- package/src/events/models/ProjectEvents.ts +331 -0
- package/src/events/process/ProcessEventTypes.ts +5 -0
- package/src/events/process/ProcessEvents.ts +76 -0
- package/src/events/readme.md +22 -0
- package/src/events/reporting/ReportingEventTypes.ts +3 -0
- package/src/events/reporting/ReportingEvents.ts +28 -0
- package/src/events/telemetry/TelemetryEventTypes.ts +10 -0
- package/src/events/telemetry/TelemetryEvents.ts +156 -0
- package/src/lib/cookies/Cookie.ts +312 -0
- package/src/lib/cookies/Cookies.ts +326 -0
- package/src/lib/cookies/Utils.ts +168 -0
- package/src/lib/headers/Headers.ts +219 -0
- package/src/lib/logging/DefaultLogger.ts +19 -0
- package/src/lib/logging/DummyLogger.ts +21 -0
- package/src/lib/logging/Logger.ts +16 -0
- package/src/lib/transformers/PayloadSerializer.ts +332 -0
- package/src/lib/transformers/Utils.ts +18 -0
- package/src/lib/uuid.ts +40 -0
- package/src/mocking/LegacyInterfaces.ts +52 -0
- package/src/mocking/LegacyMock.ts +37 -0
- package/src/mocking/legacy/Authorization.ts +39 -0
- package/src/mocking/legacy/Certificates.ts +145 -0
- package/src/mocking/legacy/Cookies.ts +51 -0
- package/src/mocking/legacy/HostRules.ts +43 -0
- package/src/mocking/legacy/Http.ts +236 -0
- package/src/mocking/legacy/HttpResponse.ts +106 -0
- package/src/mocking/legacy/RestApi.ts +68 -0
- package/src/mocking/legacy/Urls.ts +44 -0
- package/src/mocking/legacy/Variables.ts +53 -0
- package/src/models/ArcResponse.ts +166 -0
- package/src/models/Authorization.ts +481 -0
- package/src/models/AuthorizationData.ts +60 -0
- package/src/models/Backend.ts +107 -0
- package/src/models/ClientCertificate.ts +68 -0
- package/src/models/Environment.ts +279 -0
- package/src/models/ErrorResponse.ts +101 -0
- package/src/models/HistoryIndex.ts +76 -0
- package/src/models/HistoryRequest.ts +28 -0
- package/src/models/HostRule.ts +163 -0
- package/src/models/HttpCookie.ts +285 -0
- package/src/models/HttpProject.ts +1294 -0
- package/src/models/HttpProjectListItem.ts +23 -0
- package/src/models/HttpRequest.ts +124 -0
- package/src/models/HttpResponse.ts +143 -0
- package/src/models/License.ts +113 -0
- package/src/models/ProjectDefinitionProperty.ts +40 -0
- package/src/models/ProjectFolder.ts +439 -0
- package/src/models/ProjectItem.ts +135 -0
- package/src/models/ProjectParent.ts +113 -0
- package/src/models/ProjectRequest.ts +277 -0
- package/src/models/ProjectSchema.ts +202 -0
- package/src/models/Property.ts +423 -0
- package/src/models/Provider.ts +98 -0
- package/src/models/README.md +20 -0
- package/src/models/Request.ts +452 -0
- package/src/models/RequestActions.ts +163 -0
- package/src/models/RequestAuthorization.ts +115 -0
- package/src/models/RequestConfig.ts +317 -0
- package/src/models/RequestLog.ts +159 -0
- package/src/models/RequestTime.ts +108 -0
- package/src/models/RequestUiMeta.ts +258 -0
- package/src/models/RequestsSize.ts +65 -0
- package/src/models/ResponseAuthorization.ts +104 -0
- package/src/models/ResponseRedirect.ts +158 -0
- package/src/models/RevisionInfo.ts +37 -0
- package/src/models/SentRequest.ts +125 -0
- package/src/models/SerializablePayload.ts +68 -0
- package/src/models/Server.ts +153 -0
- package/src/models/Thing.ts +110 -0
- package/src/models/Url.ts +90 -0
- package/src/models/User.ts +120 -0
- package/src/models/WebApi.ts +234 -0
- package/src/models/WebApiIndex.ts +122 -0
- package/src/models/Workspace.ts +182 -0
- package/src/models/actions/Action.ts +213 -0
- package/src/models/actions/ActionView.ts +40 -0
- package/src/models/actions/Condition.ts +207 -0
- package/src/models/actions/ConditionView.ts +42 -0
- package/src/models/actions/Enums.ts +29 -0
- package/src/models/actions/RunnableAction.ts +144 -0
- package/src/models/actions/runnable/DeleteCookieAction.ts +113 -0
- package/src/models/actions/runnable/Runnable.ts +9 -0
- package/src/models/actions/runnable/SetCookieAction.ts +216 -0
- package/src/models/actions/runnable/SetVariableAction.ts +81 -0
- package/src/models/legacy/DataExport.ts +172 -0
- package/src/models/legacy/Normalizer.ts +110 -0
- package/src/models/legacy/actions/Actions.ts +269 -0
- package/src/models/legacy/authorization/Authorization.ts +572 -0
- package/src/models/legacy/models/ApiTypes.ts +202 -0
- package/src/models/legacy/models/ArcLegacyProject.ts +39 -0
- package/src/models/legacy/models/AuthData.ts +17 -0
- package/src/models/legacy/models/ClientCertificate.ts +95 -0
- package/src/models/legacy/models/Cookies.ts +52 -0
- package/src/models/legacy/models/HostRule.ts +35 -0
- package/src/models/legacy/models/RestApi.ts +49 -0
- package/src/models/legacy/models/UrlHistory.ts +37 -0
- package/src/models/legacy/models/Variable.ts +43 -0
- package/src/models/legacy/models/base.d.ts +95 -0
- package/src/models/legacy/request/ArcRequest.ts +405 -0
- package/src/models/legacy/request/ArcResponse.ts +177 -0
- package/src/models/legacy/request/HistoryData.ts +47 -0
- package/src/models/legacy/request/Legacy.ts +45 -0
- package/src/models/legacy/request/RequestBody.ts +87 -0
- package/src/models/transformers/ArcDexieTransformer.ts +323 -0
- package/src/models/transformers/ArcLegacyNormalizer.ts +85 -0
- package/src/models/transformers/ArcLegacyTransformer.ts +200 -0
- package/src/models/transformers/ArcPouchTransformer.ts +184 -0
- package/src/models/transformers/BaseTransformer.ts +116 -0
- package/src/models/transformers/ImportUtils.ts +141 -0
- package/src/models/transformers/LegacyDataExportToApiProject.ts +76 -0
- package/src/models/transformers/LegacyExportProcessor.ts +252 -0
- package/src/models/transformers/PostmanBackupTransformer.ts +306 -0
- package/src/models/transformers/PostmanDataTransformer.ts +50 -0
- package/src/models/transformers/PostmanTransformer.ts +106 -0
- package/src/models/transformers/PostmanV21Transformer.ts +311 -0
- package/src/models/transformers/PostmanV2Transformer.ts +308 -0
- package/src/models/transformers/har.ts +865 -0
- package/src/runtime/actions/ActionRunner.ts +83 -0
- package/src/runtime/actions/ConditionRunner.ts +194 -0
- package/src/runtime/actions/RunnableCondition.ts +57 -0
- package/src/runtime/actions/runnable/ActionRunnable.ts +19 -0
- package/src/runtime/actions/runnable/DeleteCookieRunnable.ts +39 -0
- package/src/runtime/actions/runnable/SetCookieRunnable.ts +92 -0
- package/src/runtime/actions/runnable/SetVariableRunnable.ts +53 -0
- package/src/runtime/http-engine/ArcEngine.ts +1064 -0
- package/src/runtime/http-engine/Errors.ts +13 -0
- package/src/runtime/http-engine/FormData.ts +85 -0
- package/src/runtime/http-engine/HttpEngine.ts +874 -0
- package/src/runtime/http-engine/HttpErrorCodes.ts +270 -0
- package/src/runtime/http-engine/NodeEngine.ts +787 -0
- package/src/runtime/http-engine/NodeEngineDirect.ts +476 -0
- package/src/runtime/http-engine/PayloadSupport.ts +84 -0
- package/src/runtime/http-engine/RequestUtils.ts +164 -0
- package/src/runtime/http-engine/ntlm/Des.ts +345 -0
- package/src/runtime/http-engine/ntlm/MD4.ts +135 -0
- package/src/runtime/http-engine/ntlm/NtlmAuth.ts +186 -0
- package/src/runtime/http-engine/ntlm/NtlmMessage.ts +57 -0
- package/src/runtime/modules/BasicAuthCache.ts +133 -0
- package/src/runtime/modules/ExecutionResponse.ts +4 -0
- package/src/runtime/modules/ModulesRegistry.ts +136 -0
- package/src/runtime/modules/RequestAuthorization.ts +110 -0
- package/src/runtime/modules/RequestCookies.ts +145 -0
- package/src/runtime/node/ProjectRunner.ts +275 -0
- package/src/runtime/node/RequestFactory.ts +422 -0
- package/src/runtime/node/VariablesStore.ts +25 -0
- package/src/runtime/store/StoreSdk.ts +838 -0
- package/src/runtime/variables/Cache.ts +53 -0
- package/src/runtime/variables/EvalFunctions.ts +132 -0
- package/src/runtime/variables/ProjectVariables.ts +6 -0
- package/src/runtime/variables/VariablesProcessor.ts +543 -0
- package/src/runtime/variables/VariablesTokenizer.ts +55 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { Cookie, CookieOptions } from './Cookie.js';
|
|
2
|
+
import {
|
|
3
|
+
getPath,
|
|
4
|
+
matchesDomain,
|
|
5
|
+
matchesPath,
|
|
6
|
+
fillCookieAttributes,
|
|
7
|
+
} from './Utils.js';
|
|
8
|
+
|
|
9
|
+
export const urlSymbol = Symbol('url');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A library to handle Cookie parsing.
|
|
13
|
+
*/
|
|
14
|
+
export class Cookies {
|
|
15
|
+
cookies: Cookie[];
|
|
16
|
+
uri: URL | undefined;
|
|
17
|
+
|
|
18
|
+
[urlSymbol]: string | undefined;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Constructs an object.
|
|
22
|
+
*
|
|
23
|
+
* @param header A HTTP cookie string to parse.
|
|
24
|
+
* @param url A request url for this cookie. If empty some cookie computations (like checking if cookies matches) will be omitted.
|
|
25
|
+
*/
|
|
26
|
+
constructor(header = '', url?: string) {
|
|
27
|
+
/**
|
|
28
|
+
* A base URL for this object.
|
|
29
|
+
*/
|
|
30
|
+
this.url = url;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A list of parsed cookies.
|
|
34
|
+
*/
|
|
35
|
+
this.cookies = Cookies.parse(header);
|
|
36
|
+
if (this.uri && url) {
|
|
37
|
+
fillCookieAttributes(this.uri, url, this.cookies);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Set's the URL and parses it setting `uri` property.
|
|
43
|
+
* @param url Cookie URL
|
|
44
|
+
*/
|
|
45
|
+
set url(url: string | undefined) {
|
|
46
|
+
if (url) {
|
|
47
|
+
this[urlSymbol] = url;
|
|
48
|
+
this.uri = new URL(url);
|
|
49
|
+
} else {
|
|
50
|
+
this[urlSymbol] = undefined;
|
|
51
|
+
this.uri = undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @returns {string} Cookie URL
|
|
57
|
+
*/
|
|
58
|
+
get url(): string | undefined {
|
|
59
|
+
return this[urlSymbol];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Parses a cookie string to a list of Cookie objects.
|
|
64
|
+
*
|
|
65
|
+
* @param cookies A HTTP cookie string
|
|
66
|
+
* @returns List of parsed cookies.
|
|
67
|
+
*/
|
|
68
|
+
static parse(cookies: string): Cookie[] {
|
|
69
|
+
const cookieParts: (keyof Cookie)[] = [
|
|
70
|
+
'path',
|
|
71
|
+
'domain',
|
|
72
|
+
'max-age',
|
|
73
|
+
'expires',
|
|
74
|
+
'secure',
|
|
75
|
+
'httponly',
|
|
76
|
+
'samesite',
|
|
77
|
+
'hostonly',
|
|
78
|
+
];
|
|
79
|
+
const booleanParts: (keyof Cookie)[] = [
|
|
80
|
+
'secure', 'httponly', 'hostonly'
|
|
81
|
+
];
|
|
82
|
+
const list: Cookie[] = [];
|
|
83
|
+
if (!cookies || !cookies.trim()) {
|
|
84
|
+
return list;
|
|
85
|
+
}
|
|
86
|
+
const blocks = cookies.split(';').map(i => i.trim());
|
|
87
|
+
blocks.forEach((part, index) => {
|
|
88
|
+
// Consider the following set-cookie string:
|
|
89
|
+
// c1=v1; Path=/; Expires=Wed, 09 Feb 2022 01:30:04 GMT; HttpOnly,c2=v2; Path=/,c3=v3; Path=/; Secure; SameSite=Strict
|
|
90
|
+
// It is a valid set-cookie header event though it mixes different formatting making it harder to parse cookies.
|
|
91
|
+
// This loop looks for invalid parts and creates a canonical cookie parts array.
|
|
92
|
+
const periodIndex = part.indexOf(',');
|
|
93
|
+
if (periodIndex === -1) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (part.toLowerCase().startsWith('expires=')) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const tmp = part.split(',');
|
|
100
|
+
// remove current
|
|
101
|
+
blocks.splice(index, 1);
|
|
102
|
+
// add the new two
|
|
103
|
+
blocks.splice(index, 0, ...tmp);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
blocks.forEach((cookie) => {
|
|
107
|
+
const parts = cookie.split(/=/, 2);
|
|
108
|
+
if (parts.length === 0) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const name = decodeURIComponent(parts[0].trim());
|
|
112
|
+
if (!name) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const lowerName = name.toLowerCase() as keyof Cookie;
|
|
116
|
+
let value: string | boolean | undefined;
|
|
117
|
+
if (parts.length > 1) {
|
|
118
|
+
try {
|
|
119
|
+
value = decodeURIComponent(parts[1].trim());
|
|
120
|
+
} catch (e) {
|
|
121
|
+
value = parts[1];
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
value = true;
|
|
125
|
+
}
|
|
126
|
+
// if this is an attribute of previous cookie, set it for last
|
|
127
|
+
// added cookie.
|
|
128
|
+
if (cookieParts.includes(lowerName)) {
|
|
129
|
+
if (list.length - 1 >= 0) {
|
|
130
|
+
const attr = lowerName as keyof Cookie;
|
|
131
|
+
const previousCookie = list[list.length - 1];
|
|
132
|
+
previousCookie[attr] = value as never;
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
try {
|
|
136
|
+
list.push(new Cookie(name, value as string));
|
|
137
|
+
} catch (e) {
|
|
138
|
+
// ..
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
return list;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Get a cookie by name.
|
|
147
|
+
*
|
|
148
|
+
* @param name Cookie name
|
|
149
|
+
* @returns A Cookie object or null.
|
|
150
|
+
*/
|
|
151
|
+
get(name: string): Cookie | null {
|
|
152
|
+
const { cookies } = this;
|
|
153
|
+
// eslint-disable-next-line no-plusplus
|
|
154
|
+
for (let i = 0, len = cookies.length; i < len; i++) {
|
|
155
|
+
if (cookies[i].name === name) {
|
|
156
|
+
return cookies[i];
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Adds a cookie to the list of cookies.
|
|
164
|
+
*
|
|
165
|
+
* @param name Name of the cookie.
|
|
166
|
+
* @param value Value of the cookie.
|
|
167
|
+
* @param opts Other cookie options to set.
|
|
168
|
+
*/
|
|
169
|
+
set(name: string, value?: string, opts?: CookieOptions): void {
|
|
170
|
+
const cookie = new Cookie(name, value, opts);
|
|
171
|
+
const cookies = this.cookies.filter((c) => c.name !== name);
|
|
172
|
+
cookies.push(cookie);
|
|
173
|
+
this.cookies = cookies;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Returns a string that can be used in a HTTP header value for Cookie.
|
|
178
|
+
* The structure of the cookie string depends on if you want to send a
|
|
179
|
+
* cookie from the server to client or other way around.
|
|
180
|
+
* When you want to send the `Cookie` header to server set
|
|
181
|
+
* `toServer` argument to true. Then it will produce only `name=value;`
|
|
182
|
+
* string. Otherwise it will be the `Set-Cookie` header value
|
|
183
|
+
* containing all other cookies properties.
|
|
184
|
+
*
|
|
185
|
+
* @param toServer True if produced string is to be used with the `Cookie` header
|
|
186
|
+
* @returns HTTP header string value for all cookies.
|
|
187
|
+
*/
|
|
188
|
+
toString(toServer = false): string {
|
|
189
|
+
const parts: string[] = [];
|
|
190
|
+
this.cookies.forEach((cookie) => {
|
|
191
|
+
parts.push(toServer ? cookie.toString() : cookie.toHeader());
|
|
192
|
+
});
|
|
193
|
+
return parts.join('; ');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Removes cookies from `this.cookies` that has been set for different
|
|
198
|
+
* domain and path.
|
|
199
|
+
* This function has no effect if the URL is not set.
|
|
200
|
+
*
|
|
201
|
+
* This function follows an algorithm defined in https://tools.ietf.org/html/rfc6265 for
|
|
202
|
+
* domain match.
|
|
203
|
+
*
|
|
204
|
+
* @returns A list of removed cookies.
|
|
205
|
+
*/
|
|
206
|
+
filter(): Cookie[] {
|
|
207
|
+
const { uri, url } = this;
|
|
208
|
+
if (!uri || !url) {
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
const domain = uri.hostname.toLowerCase();
|
|
212
|
+
const path = getPath(url);
|
|
213
|
+
const removed: Cookie[] = [];
|
|
214
|
+
this.cookies = this.cookies.filter((cookie) => {
|
|
215
|
+
if (!cookie.path) {
|
|
216
|
+
cookie.path = path;
|
|
217
|
+
}
|
|
218
|
+
const cDomain = cookie.domain;
|
|
219
|
+
if (!cDomain) {
|
|
220
|
+
cookie.domain = domain;
|
|
221
|
+
// point 6. of https://tools.ietf.org/html/rfc6265#section-5.3
|
|
222
|
+
cookie.hostOnly = true;
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
const res =
|
|
226
|
+
matchesDomain(cDomain, uri) && matchesPath(cookie.path, uri, url);
|
|
227
|
+
if (!res) {
|
|
228
|
+
removed.push(cookie);
|
|
229
|
+
}
|
|
230
|
+
return res;
|
|
231
|
+
});
|
|
232
|
+
return removed;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Merges this cookies with another Cookies object.
|
|
237
|
+
* This cookies will be overwritten by passed cookies according to
|
|
238
|
+
* the HTTP spec.
|
|
239
|
+
* This function is useful when you need to override cookies with
|
|
240
|
+
* the response from the server
|
|
241
|
+
* as defined in the https://tools.ietf.org/html/rfc6265.
|
|
242
|
+
*
|
|
243
|
+
* @param cookies An Cookies object with newest cookies.
|
|
244
|
+
* @param copyKeys When set, it copies values for given keys from old object to the new one.
|
|
245
|
+
*/
|
|
246
|
+
merge(cookies: Cookies, copyKeys: (keyof Cookie)[] = []): void {
|
|
247
|
+
if (!cookies || !cookies.cookies || cookies.cookies.length === 0) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!this.cookies || this.cookies.length === 0) {
|
|
251
|
+
this.cookies = cookies.cookies;
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const foreignDomain = cookies.uri ? cookies.uri.hostname : null;
|
|
255
|
+
const foreignPath = cookies.url ? getPath(cookies.url) : null;
|
|
256
|
+
// delete cookies from this.cookies that has the same name as new ones,
|
|
257
|
+
// but are domain/path match
|
|
258
|
+
const newCookies: Cookie[] = cookies.cookies;
|
|
259
|
+
const nLength: number = newCookies.length;
|
|
260
|
+
|
|
261
|
+
const { uri, url } = this;
|
|
262
|
+
if (!uri || !url) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const copyKeysLength = copyKeys.length;
|
|
266
|
+
for (let i = this.cookies.length - 1; i >= 0; i--) {
|
|
267
|
+
const tName = this.cookies[i].name;
|
|
268
|
+
for (let j = 0; j < nLength; j++) {
|
|
269
|
+
const targetCookie = newCookies[j];
|
|
270
|
+
const nName = targetCookie.name;
|
|
271
|
+
if (nName !== tName) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (!foreignDomain || !matchesDomain(foreignDomain, uri)) {
|
|
275
|
+
// This is cookie for a different domain. Don't override.
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (!foreignPath || !matchesPath(foreignPath, uri, url)) {
|
|
279
|
+
// This is cookie for a different path. Don't override.
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
const removed = this.cookies.splice(i, 1);
|
|
283
|
+
targetCookie.created = removed[0].created;
|
|
284
|
+
if (copyKeysLength) {
|
|
285
|
+
for (let k = 0; k < copyKeysLength; k++) {
|
|
286
|
+
const key = copyKeys[k];
|
|
287
|
+
if (key in removed[0]) {
|
|
288
|
+
targetCookie[key] = removed[0][key] as never;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
// Do not re-set cookies that values are not set.
|
|
296
|
+
for (let i = nLength - 1; i >= 0; i--) {
|
|
297
|
+
const nValue = newCookies[i].value;
|
|
298
|
+
if (!nValue || !nValue.trim || !nValue.trim()) {
|
|
299
|
+
newCookies.splice(i, 1);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
this.cookies = this.cookies.concat(newCookies);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Clears cookies from `this.cookies` that already expired.
|
|
307
|
+
*
|
|
308
|
+
* @returns List of removed (expired) cookies.
|
|
309
|
+
*/
|
|
310
|
+
clearExpired(): Cookie[] {
|
|
311
|
+
const now = Date.now();
|
|
312
|
+
const expired: Cookie[] = [];
|
|
313
|
+
const cookies = this.cookies.filter((cookie) => {
|
|
314
|
+
if (!cookie.expires) {
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
if (now >= cookie.expires) {
|
|
318
|
+
expired.push(cookie);
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
return true;
|
|
322
|
+
});
|
|
323
|
+
this.cookies = cookies;
|
|
324
|
+
return expired;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { Cookie } from './Cookie.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Gets the path for a domain as defined in
|
|
5
|
+
* https://tools.ietf.org/html/rfc6265#section-5.1.4
|
|
6
|
+
*
|
|
7
|
+
* @param urlValue A url to extract path from.
|
|
8
|
+
*/
|
|
9
|
+
export function getPath(urlValue: string): string {
|
|
10
|
+
let url = urlValue;
|
|
11
|
+
const defaultValue = '/';
|
|
12
|
+
if (!url) {
|
|
13
|
+
return defaultValue;
|
|
14
|
+
}
|
|
15
|
+
let index = url.indexOf('/', 8); // after `http(s)://` string
|
|
16
|
+
if (index === -1) {
|
|
17
|
+
return defaultValue;
|
|
18
|
+
}
|
|
19
|
+
url = url.substr(index);
|
|
20
|
+
if (!url || url[0] !== '/') {
|
|
21
|
+
return defaultValue;
|
|
22
|
+
}
|
|
23
|
+
// removed query string
|
|
24
|
+
index = url.indexOf('?');
|
|
25
|
+
if (index !== -1) {
|
|
26
|
+
url = url.substr(0, index);
|
|
27
|
+
}
|
|
28
|
+
// removes hash string
|
|
29
|
+
index = url.indexOf('#');
|
|
30
|
+
if (index !== -1) {
|
|
31
|
+
url = url.substr(0, index);
|
|
32
|
+
}
|
|
33
|
+
index = url.indexOf('/', 1);
|
|
34
|
+
if (index === -1) {
|
|
35
|
+
return defaultValue;
|
|
36
|
+
}
|
|
37
|
+
index = url.lastIndexOf('/');
|
|
38
|
+
if (index !== 0) {
|
|
39
|
+
url = url.substr(0, index);
|
|
40
|
+
}
|
|
41
|
+
return url;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Checks if `domain` of the request url (defined as `this.url`)
|
|
46
|
+
* matches domain defined in a cookie.
|
|
47
|
+
* This follows algorithm defined in https://tools.ietf.org/html/rfc6265#section-5.1.3
|
|
48
|
+
*
|
|
49
|
+
* Note: If `cookieDomain` is not set it returns false, while
|
|
50
|
+
* (according to the spec) it should be set to `domain` and pass the test.
|
|
51
|
+
* Because this function only check if domains matches it will not
|
|
52
|
+
* override domain.
|
|
53
|
+
* Cookie domain should be filled before calling this function.
|
|
54
|
+
*
|
|
55
|
+
* Note: This function will return false if the `this.url` was not set.
|
|
56
|
+
*
|
|
57
|
+
* @param cookieDomain A domain received in the cookie.
|
|
58
|
+
* @param uri
|
|
59
|
+
* @return True if domains matches.
|
|
60
|
+
*/
|
|
61
|
+
export function matchesDomain(cookieDomain: string, uri: URL): boolean {
|
|
62
|
+
if (!uri) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
let domain = uri.hostname;
|
|
66
|
+
domain = domain && domain.toLowerCase && domain.toLowerCase();
|
|
67
|
+
// eslint-disable-next-line no-param-reassign
|
|
68
|
+
cookieDomain =
|
|
69
|
+
cookieDomain && cookieDomain.toLowerCase && cookieDomain.toLowerCase();
|
|
70
|
+
if (!cookieDomain) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (domain === cookieDomain) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (cookieDomain[0] === '.') {
|
|
77
|
+
const parts = domain.split('.');
|
|
78
|
+
if (parts.length > 1) {
|
|
79
|
+
parts.shift();
|
|
80
|
+
domain = parts.join('.');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const index = cookieDomain.indexOf(domain);
|
|
84
|
+
if (index === -1) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
if (cookieDomain.substr(index - 1, index) !== '.') {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Checks if paths mach as defined in
|
|
95
|
+
* https://tools.ietf.org/html/rfc6265#section-5.1.4
|
|
96
|
+
*
|
|
97
|
+
* Note: This function will return false if the `this.url` was not set.
|
|
98
|
+
*
|
|
99
|
+
* @param cookiePath Path from the cookie.
|
|
100
|
+
* @param uri
|
|
101
|
+
* @param url
|
|
102
|
+
* @return True when paths matches.
|
|
103
|
+
*/
|
|
104
|
+
export function matchesPath(cookiePath: string, uri: URL, url: string): boolean {
|
|
105
|
+
if (!uri) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
if (!cookiePath) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
const hostPath = getPath(url);
|
|
112
|
+
if (hostPath === cookiePath) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
// const index = cookiePath.indexOf(hostPath);
|
|
116
|
+
const index = hostPath.indexOf(cookiePath);
|
|
117
|
+
if (index === 0 && cookiePath[cookiePath.length - 1] === '/') {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
if (index === 0 && cookiePath.indexOf('/', 1) === -1) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (index === 0) {
|
|
125
|
+
// eslint-disable-next-line no-plusplus
|
|
126
|
+
for (let i = 0, len = hostPath.length; i < len; i++) {
|
|
127
|
+
if (cookiePath.indexOf(hostPath[i]) === -1 && hostPath[i] === '/') {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Clients must fill `path` and `domain` attribute if not set by the
|
|
137
|
+
* server to match current request url.
|
|
138
|
+
*
|
|
139
|
+
* @param uri HTTP request url parsed by the URL class.
|
|
140
|
+
* @param url The HTTP request url.
|
|
141
|
+
* @param cookies Parsed cookies
|
|
142
|
+
*/
|
|
143
|
+
export function fillCookieAttributes(uri: URL, url: string, cookies: Cookie[]): void {
|
|
144
|
+
if (!uri) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
let domain = uri.hostname;
|
|
148
|
+
if (!domain) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
domain = domain.toLowerCase();
|
|
152
|
+
const path = getPath(url);
|
|
153
|
+
cookies.forEach((cookie) => {
|
|
154
|
+
if (!cookie.path) {
|
|
155
|
+
// eslint-disable-next-line no-param-reassign
|
|
156
|
+
cookie.path = path;
|
|
157
|
+
}
|
|
158
|
+
const cDomain = cookie.domain;
|
|
159
|
+
if (!cDomain) {
|
|
160
|
+
// eslint-disable-next-line no-param-reassign
|
|
161
|
+
cookie.domain = domain;
|
|
162
|
+
// point 6. of https://tools.ietf.org/html/rfc6265#section-5.3
|
|
163
|
+
// eslint-disable-next-line no-param-reassign
|
|
164
|
+
cookie.hostOnly = true;
|
|
165
|
+
}
|
|
166
|
+
return cookie;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-unused-vars */
|
|
4
|
+
interface RawValue {
|
|
5
|
+
name: string;
|
|
6
|
+
value: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Normalizes name of a header.
|
|
11
|
+
*
|
|
12
|
+
* @param name
|
|
13
|
+
* @return Normalized name
|
|
14
|
+
*/
|
|
15
|
+
function normalizeName(name: string): string {
|
|
16
|
+
if (typeof name !== 'string') {
|
|
17
|
+
name = String(name);
|
|
18
|
+
}
|
|
19
|
+
return name.toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Normalizes value of a header.
|
|
24
|
+
* @param value
|
|
25
|
+
* @return Normalized name
|
|
26
|
+
*/
|
|
27
|
+
function normalizeValue(value: string): string {
|
|
28
|
+
if (typeof value !== 'string') {
|
|
29
|
+
value = String(value);
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A generator for list of headers from a string.
|
|
36
|
+
*
|
|
37
|
+
* ```javascript
|
|
38
|
+
* for (let [name, value] of headersStringToList('a:b')) {
|
|
39
|
+
* ...
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
* @param string Headers string to parse
|
|
43
|
+
*/
|
|
44
|
+
function* headersStringToList(string: string): Generator<string[]> {
|
|
45
|
+
if (!string || string.trim() === '') {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
const headers = string.split(/\n(?=[^ \t]+)/gim);
|
|
49
|
+
for (let i = 0, len = headers.length; i < len; i++) {
|
|
50
|
+
const line = headers[i].trim();
|
|
51
|
+
if (line === '') {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const sepPosition = line.indexOf(':');
|
|
55
|
+
if (sepPosition === -1) {
|
|
56
|
+
yield [line, ''];
|
|
57
|
+
} else {
|
|
58
|
+
const name = line.substr(0, sepPosition);
|
|
59
|
+
const value = line.substr(sepPosition + 1).trim();
|
|
60
|
+
yield [name, value];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The same interface as Web platform's Headers but without
|
|
67
|
+
* CORS restrictions.
|
|
68
|
+
*/
|
|
69
|
+
export class Headers {
|
|
70
|
+
/**
|
|
71
|
+
* The keys are canonical keys and the values are the input values.
|
|
72
|
+
*/
|
|
73
|
+
map: Record<string, RawValue> = {};
|
|
74
|
+
/**
|
|
75
|
+
* @param headers The headers to parse.
|
|
76
|
+
*/
|
|
77
|
+
constructor(headers?: string | Record<string, string> | Headers | http.IncomingHttpHeaders) {
|
|
78
|
+
if (headers instanceof Headers) {
|
|
79
|
+
headers.forEach((value, name) => this.append(name, value));
|
|
80
|
+
} else if (typeof headers === 'string') {
|
|
81
|
+
const iterator = headersStringToList(headers);
|
|
82
|
+
let result = iterator.next();
|
|
83
|
+
while (!result.done) {
|
|
84
|
+
this.append(result.value[0], result.value[1]);
|
|
85
|
+
result = iterator.next();
|
|
86
|
+
}
|
|
87
|
+
} else if (headers) {
|
|
88
|
+
Object.keys(headers).forEach((name) => this.append(name, headers[name]));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Adds value to existing header or creates new header
|
|
94
|
+
*/
|
|
95
|
+
append(name: string, value: string | string[] | undefined): void {
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
value.forEach(v => this.append(name, v));
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const normalizedName = normalizeName(name);
|
|
101
|
+
value = value ? normalizeValue(value) : '';
|
|
102
|
+
let item = this.map[normalizedName];
|
|
103
|
+
if (item) {
|
|
104
|
+
const oldValue = item.value;
|
|
105
|
+
item.value = oldValue ? `${oldValue},${value}` : value;
|
|
106
|
+
} else {
|
|
107
|
+
item = {
|
|
108
|
+
name,
|
|
109
|
+
value,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
this.map[normalizedName] = item;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Removes a header from the list of headers.
|
|
117
|
+
* @param name The header name
|
|
118
|
+
*/
|
|
119
|
+
delete(name: string): void {
|
|
120
|
+
delete this.map[normalizeName(name)];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns the current value of the header
|
|
125
|
+
* @param name Header name
|
|
126
|
+
*/
|
|
127
|
+
get(name: string): string | undefined {
|
|
128
|
+
name = normalizeName(name);
|
|
129
|
+
return this.has(name) ? this.map[name].value : undefined;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Checks if the header exists.
|
|
134
|
+
*/
|
|
135
|
+
has(name: string): boolean {
|
|
136
|
+
return Object.prototype.hasOwnProperty.call(this.map, normalizeName(name));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Creates a new header. If the header exist it replaces the value.
|
|
141
|
+
*/
|
|
142
|
+
set(name: string, value: string): void {
|
|
143
|
+
const normalizedName = normalizeName(name);
|
|
144
|
+
this.map[normalizedName] = {
|
|
145
|
+
value: normalizeValue(value),
|
|
146
|
+
name,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Iterates over each header.
|
|
152
|
+
*/
|
|
153
|
+
forEach(callback: (value: string, name: string, headers: Headers) => void, thisArg?: unknown): void {
|
|
154
|
+
for (const name in this.map) {
|
|
155
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
156
|
+
callback.call(thisArg, this.map[name].value, this.map[name].name, this);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @return The headers HTTP string
|
|
163
|
+
*/
|
|
164
|
+
toString(): string {
|
|
165
|
+
const result: string[] = [];
|
|
166
|
+
this.forEach((value, name) => {
|
|
167
|
+
let tmp = `${name}: `;
|
|
168
|
+
if (value) {
|
|
169
|
+
tmp += value;
|
|
170
|
+
}
|
|
171
|
+
result.push(tmp);
|
|
172
|
+
});
|
|
173
|
+
return result.join('\n');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Iterates over keys.
|
|
178
|
+
*/
|
|
179
|
+
*keys(): IterableIterator<string> {
|
|
180
|
+
for (const name in this.map) {
|
|
181
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
182
|
+
yield this.map[name].name;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Iterates over values.
|
|
189
|
+
*/
|
|
190
|
+
*values(): IterableIterator<string> {
|
|
191
|
+
for (const name in this.map) {
|
|
192
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
193
|
+
yield this.map[name].value;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Iterates over headers.
|
|
200
|
+
*/
|
|
201
|
+
*entries(): IterableIterator<string[]> {
|
|
202
|
+
for (const name in this.map) {
|
|
203
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
204
|
+
yield [this.map[name].name, this.map[name].value];
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Iterates over headers.
|
|
211
|
+
*/
|
|
212
|
+
*[Symbol.iterator](): IterableIterator<string[]> {
|
|
213
|
+
for (const name in this.map) {
|
|
214
|
+
if (Object.prototype.hasOwnProperty.call(this.map, name)) {
|
|
215
|
+
yield [this.map[name].name, this.map[name].value];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|