Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
336 lines (286 loc) · 10.7 KB

File metadata and controls

336 lines (286 loc) · 10.7 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { map as eventStreamMap } from 'event-stream';
import FormData from 'form-data';
import { Param, PostDataCommon, Request as NpmHarRequest } from 'har-format';
import { stringify as queryStringify } from 'querystring';
import { format as urlFormat, parse as urlParse, UrlWithParsedQuery } from 'url';
import { formDataIterator, isBlob } from './helpers/form-data';
import { validateHarRequest } from './helpers/har-validator';
import { getHeaderName } from './helpers/headers';
import { ReducedHelperObject, reducer } from './helpers/reducer';
import { ClientId, TargetId, targets } from './targets/targets';
export { availableTargets, extname } from './helpers/utils';
export { addTarget, addTargetClient } from './targets/targets';
const DEBUG_MODE = false;
const debug = {
// eslint-disable-next-line @typescript-eslint/no-empty-function -- intentional noop
info: DEBUG_MODE ? console.info : () => {},
};
/** is this wrong? yes. according to the spec (http://www.softwareishard.com/blog/har-12-spec/#postData) it's technically wrong since `params` and `text` are (by the spec) mutually exclusive. However, in practice, this is not what is often the case.
*
* In general, this library takes a _descriptive_ rather than _perscriptive_ approach (see https://amyrey.web.unc.edu/classes/ling-101-online/tutorials/understanding-prescriptive-vs-descriptive-grammar/).
*
* Then, in addition to that, it really adds to complexity with TypeScript (TypeScript takes this constraint very very seriously) in a way that's not actually super useful. So, we treat this object as though it could have both or either of `params` and/or `text`.
*/
type PostDataBase = PostDataCommon & {
text?: string;
params?: Param[];
};
export type HarRequest = Omit<NpmHarRequest, 'postData'> & { postData: PostDataBase };
export interface RequestExtras {
postData: PostDataBase & {
jsonObj?: ReducedHelperObject;
paramsObj?: ReducedHelperObject;
boundary?: string;
};
fullUrl: string;
queryObj: ReducedHelperObject;
headersObj: ReducedHelperObject;
uriObj: UrlWithParsedQuery;
cookiesObj: ReducedHelperObject;
allHeaders: ReducedHelperObject;
}
export type Request = HarRequest & RequestExtras;
interface Entry {
request: Partial<HarRequest>;
}
interface HarEntry {
log: {
version: string;
creator: {
name: string;
version: string;
};
entries: Entry[];
};
}
const isHarEntry = (value: any): value is HarEntry =>
typeof value === 'object' &&
'log' in value &&
typeof value.log === 'object' &&
'entries' in value.log &&
Array.isArray(value.log.entries);
export class HTTPSnippet {
requests: Request[] = [];
constructor(input: HarEntry | HarRequest) {
let entries: Entry[] = [];
// prep the main container
this.requests = [];
// is it har?
if (isHarEntry(input)) {
entries = input.log.entries;
} else {
entries = [
{
request: input,
},
];
}
entries.forEach(({ request }) => {
// add optional properties to make validation successful
const req = {
bodySize: 0,
headersSize: 0,
headers: [],
cookies: [],
httpVersion: 'HTTP/1.1',
queryString: [],
postData: {
mimeType: request.postData?.mimeType || 'application/octet-stream',
},
...request,
};
if (validateHarRequest(req)) {
this.requests.push(this.prepare(req));
}
});
}
prepare = (harRequest: HarRequest) => {
const request: Request = {
...harRequest,
fullUrl: '',
uriObj: {} as UrlWithParsedQuery,
queryObj: {},
headersObj: {},
cookiesObj: {},
allHeaders: {},
};
// construct query objects
if (request.queryString && request.queryString.length) {
debug.info('queryString found, constructing queryString pair map');
request.queryObj = request.queryString.reduce(reducer, {});
}
// construct headers objects
if (request.headers && request.headers.length) {
const http2VersionRegex = /^HTTP\/2/;
request.headersObj = request.headers.reduce((accumulator, { name, value }) => {
const headerName = http2VersionRegex.exec(request.httpVersion)
? name.toLocaleLowerCase()
: name;
return {
...accumulator,
[headerName]: value,
};
}, {});
}
// construct headers objects
if (request.cookies && request.cookies.length) {
request.cookiesObj = request.cookies.reduceRight(
(accumulator, { name, value }) => ({
...accumulator,
[name]: value,
}),
{},
);
}
// construct Cookie header
const cookies = request.cookies?.map(
({ name, value }) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`,
);
if (cookies?.length) {
request.allHeaders.cookie = cookies.join('; ');
}
switch (request.postData.mimeType) {
case 'multipart/mixed':
case 'multipart/related':
case 'multipart/form-data':
case 'multipart/alternative':
// reset values
request.postData.text = '';
request.postData.mimeType = 'multipart/form-data';
if (request.postData?.params) {
const form = new FormData();
// The `form-data` module returns one of two things: a native FormData object, or its own polyfill
// Since the polyfill does not support the full API of the native FormData object, when this library is running in a browser environment it'll fail on two things:
//
// 1. The API for `form.append()` has three arguments and the third should only be present when the second is a
// Blob or USVString.
// 1. `FormData.pipe()` isn't a function.
//
// Since the native FormData object is iterable, we easily detect what version of `form-data` we're working with here to allow `multipart/form-data` requests to be compiled under both browser and Node environments.
//
// This hack is pretty awful but it's the only way we can use this library in the browser as if we code this against just the native FormData object, we can't polyfill that back into Node because Blob and File objects, which something like `formdata-polyfill` requires, don't exist there.
// @ts-expect-error TODO
const isNativeFormData = typeof form[Symbol.iterator] === 'function';
// TODO: THIS ABSOLUTELY MUST BE REMOVED.
// IT BREAKS SOME USE-CASES FOR MULTIPART FORMS THAT DEPEND ON BEING ABLE TO SET THE BOUNDARY.
// easter egg
const boundary = '---011000010111000001101001'; // this is binary for "api". yep.
if (!isNativeFormData) {
// @ts-expect-error THIS IS WRONG. VERY WRONG.
form._boundary = boundary;
}
request.postData?.params.forEach(param => {
const name = param.name;
const value = param.value || '';
const filename = param.fileName || null;
if (isNativeFormData) {
if (isBlob(value)) {
// @ts-expect-error TODO
form.append(name, value, filename);
} else {
form.append(name, value);
}
} else {
form.append(name, value, {
// @ts-expect-error TODO
filename,
// @ts-expect-error TODO
contentType: param.contentType || null,
});
}
});
if (isNativeFormData) {
for (const data of formDataIterator(form, boundary)) {
request.postData.text += data;
}
} else {
form.pipe(
// @ts-expect-error TODO
eventStreamMap(data => {
request.postData.text += data;
}),
);
}
request.postData.boundary = boundary;
// Since headers are case-sensitive we need to see if there's an existing `Content-Type` header that we can override.
const contentTypeHeader =
getHeaderName(request.headersObj, 'content-type') || 'content-type';
request.headersObj[contentTypeHeader] = `multipart/form-data; boundary=${boundary}`;
}
break;
case 'application/x-www-form-urlencoded':
if (!request.postData.params) {
request.postData.text = '';
} else {
// @ts-expect-error the `har-format` types make this challenging
request.postData.paramsObj = request.postData.params.reduce(reducer, {});
// always overwrite
request.postData.text = queryStringify(request.postData.paramsObj);
}
break;
case 'text/json':
case 'text/x-json':
case 'application/json':
case 'application/x-json':
request.postData.mimeType = 'application/json';
if (request.postData.text) {
try {
request.postData.jsonObj = JSON.parse(request.postData.text);
} catch (e) {
debug.info(e);
// force back to `text/plain` if headers have proper content-type value, then this should also work
request.postData.mimeType = 'text/plain';
}
}
break;
}
// create allHeaders object
const allHeaders = {
...request.allHeaders,
...request.headersObj,
};
const urlWithParsedQuery = urlParse(request.url, true, true); //?
// query string key/value pairs in with literal querystrings containd within the url
request.queryObj = {
...request.queryObj,
...(urlWithParsedQuery.query as ReducedHelperObject),
}; //?
// reset uriObj values for a clean url
const search = queryStringify(request.queryObj);
const uriObj = {
...urlWithParsedQuery,
query: request.queryObj,
search,
path: search ? `${urlWithParsedQuery.pathname}?${search}` : urlWithParsedQuery.pathname,
};
// keep the base url clean of queryString
const url = urlFormat({
...urlWithParsedQuery,
query: null,
search: null,
}); //?
const fullUrl = urlFormat({
...urlWithParsedQuery,
...uriObj,
}); //?
return {
...request,
allHeaders,
fullUrl,
url,
uriObj,
};
};
convert = (targetId: TargetId, clientId?: ClientId, options?: any) => {
if (!options && clientId) {
options = clientId;
}
const target = targets[targetId];
if (!target) {
return false;
}
const { convert } = target.clientsById[clientId || target.info.default];
const results = this.requests.map(request => convert(request, options));
return results.length === 1 ? results[0] : results;
};
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.