forked from microsoft/edge-devtools-network-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-application-host.ts
392 lines (365 loc) · 13.4 KB
/
web-application-host.ts
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// This file is intended as a rapid development debugging aid and is not a
// supported application host for any scenario.
import {
IHttpHeader,
IHttpRequest,
INetConsoleAuthorization,
INetConsoleParameter,
INetConsoleResponse,
binFromB64,
INetConsoleRequest,
} from 'network-console-shared';
import { INetConsoleHost, ISaveResult } from './interfaces';
import { globalDispatch } from 'store';
import { setHostCapabilitiesAction, setHostOptionsAction } from 'actions/host-capabilities';
import { binToB64 } from 'utility/b64';
import { deserializeFromHost } from './vscode-protocol-host';
import { loadRequestAction } from 'actions/common';
import { DEFAULT_NET_CONSOLE_REQUEST } from 'reducers/request';
import { synthesizeHttpRequest } from 'utility/http-compose';
import { recalculateAndApplyTheme } from 'themes/vscode-theme';
import { INetConsoleRequestInternal } from 'model/NetConsoleRequest';
import { initializeLocaleDictionary } from 'actions/locale-action';
import { makeSetCollectionTreeAction } from 'actions/collections';
import { makeEditAuthorizationInModalAction, makeEditEnvironmentAction } from 'actions/modal';
export default class WebApplicationHost implements INetConsoleHost {
constructor() {
const showEnvironmentModalAtStartup = false;
const showAuthorizationModalAtStartup = false;
setTimeout(() => {
globalDispatch(setHostCapabilitiesAction(
/* hasNativeTabs: */ false,
/* canSave: */ true,
/* canEditCORS: */ true,
/* transparentAuthorization: */ true,
));
globalDispatch(setHostOptionsAction(true));
globalDispatch(loadRequestAction('coll1/coll2/0', DEFAULT_NET_CONSOLE_REQUEST, /* requiresSaveAs: */ true));
recalculateAndApplyTheme('', 'light');
loadPseudoloc();
}, 1000);
setTimeout(() => {
if (showEnvironmentModalAtStartup) {
globalDispatch(makeEditEnvironmentAction('env1', 'env1.nc.json', 'Environment Container 1', 'Environment 1', [
{ key: 'foo', value: 'bar', description: '', isActive: true },
{ key: 'bar', value: 'baz', description: '', isActive: true },
{ key: 'frob', value: 'florbo', description: '', isActive: true },
{ key: 'widget', value: 'contoso', description: '', isActive: true },
]));
}
else if (showAuthorizationModalAtStartup) {
globalDispatch(makeEditAuthorizationInModalAction('coll1', [], { type: 'none' }));
}
}, 5000);
(window as any).__debug_WAH = this;
}
async makeRequest(request: INetConsoleRequestInternal, environmentalAuthorization: INetConsoleAuthorization | null, environmentVariables: INetConsoleParameter[]): Promise<INetConsoleResponse> {
const start = Date.now();
let mergedRequest = await synthesizeHttpRequest(request, environmentalAuthorization, environmentVariables);
const toFetch = constructRequest(mergedRequest);
// await new Promise(r => setTimeout(r, 10000));
const response = await fetch(toFetch);
const stop = Date.now();
const body = await bodyFromResponse(response);
return {
duration: stop - start,
status: 'COMPLETE',
response: {
headers: parseHeaders(response.headers),
statusCode: response.status,
statusText: response.statusText,
size: body.byteLength,
body: {
content: binToB64(body),
},
}
};
}
async saveRequest(request: INetConsoleRequestInternal): Promise<ISaveResult> {
// TODO: Investigate supporting save in web application host
return {
resultRequest: formatRequestForSave(request),
resultRequestId: 'DEFAULT_REQUEST',
};
}
public _debug_manualLoad(requestBody: string) {
const deserialized = deserializeFromHost('DEFAULT_REQUEST', JSON.parse(requestBody));
globalDispatch(loadRequestAction('DEFAULT_REQUEST', deserialized, /* requiresSaveAs: */ true));
}
async saveCollectionAuthorization(_collectionId: string, _authorization: INetConsoleAuthorization): Promise<void> {
// TODO: Investigate supporting save in web application host
}
async saveEnvironment(_envVariables: INetConsoleParameter[], _envId: string): Promise<void> {
// TODO: Investigate supporting save in WAH
await timeout(2000);
}
public mustAskToOpenLink = () => false;
public markDirtyState(requestId: string, isDirty: boolean) {
// Not supported in WAH
}
public openUnattachedRequest(_requestId: string) {
// Not supported in WAH
}
public log(message: object) {
console.log({
type: 'LOG',
...message,
});
}
public reassociateFocus() { }
public async requestCreateNewCollection() {
await timeout(1500);
globalDispatch(makeSetCollectionTreeAction([
{
id: 'coll1',
name: 'Collection 1',
children: [{
id: 'coll1/coll2',
name: 'Child 1',
children: [],
authorization: {
type: 'basic',
basic: {
username: 'test',
password: 'test',
showPassword: false,
}
},
}],
authorization: undefined,
},
{
id: 'coll2',
name: 'Collection 2',
children: [{
id: 'coll2/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll3',
name: 'Collection 3',
children: [{
id: 'coll3/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll4',
name: 'Collection 4',
children: [{
id: 'coll4/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll5',
name: 'Collection 5',
children: [{
id: 'coll5/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll6',
name: 'Collection 6',
children: [{
id: 'coll6/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll7',
name: 'Collection 7',
children: [{
id: 'coll7/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll8',
name: 'Collection 8',
children: [{
id: 'coll8/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll9',
name: 'Collection 9',
children: [{
id: 'coll9/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll10',
name: 'Collection 10',
children: [{
id: 'coll10/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll11',
name: 'Collection 11',
children: [{
id: 'coll11/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll12',
name: 'Collection 12',
children: [{
id: 'coll12/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll13',
name: 'Collection 13',
children: [{
id: 'coll13/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll14',
name: 'Collection 14',
children: [{
id: 'coll14/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll15',
name: 'Collection 15',
children: [{
id: 'coll15/coll2',
name: 'Child 1',
children: [],
authorization: undefined,
}],
authorization: undefined,
},
{
id: 'coll16',
name: 'Collection 16',
children: [],
authorization: undefined,
},
]));
}
}
async function loadPseudoloc() {
const messages = await fetch('./_locales/ps-PS/messages.json');
globalDispatch(initializeLocaleDictionary(await messages.json(), 'ps'));
}
function constructHeaders(request: IHttpRequest): Headers {
const result = new Headers();
request.headers.forEach(header => {
result.append(header.key, header.value);
});
return result;
}
function constructRequest(request: IHttpRequest): Request {
const result = new Request(request.url, {
headers: constructHeaders(request),
method: request.verb,
mode: request.fetchParams?.corsMode || 'cors',
cache: request.fetchParams?.cacheMode || 'no-cache',
redirect: request.fetchParams?.redirectMode || 'follow',
referrer: '',
credentials: request.fetchParams?.credentialsMode || 'same-origin',
body: request.body.content ? binFromB64(request.body.content) : undefined,
});
return result;
}
function parseHeaders(headers: Headers): IHttpHeader[] {
const result: IHttpHeader[] = [];
headers.forEach((value, key) => {
result.push({
key,
value,
});
});
// Because this is a sample and Web Application host can't read cookies,
// make some fake ones up here
result.push({ key: 'set-cookie', value: '_EDGE_V=1; path=/; httponly; expires=Tue, 09-Feb-2021 02:38:24 GMT; domain=bing.com' });
result.push({ key: 'set-cookie', value: `SampleCookie=this thing; path=/; domain=localhost; HttpOnly; expires=${new Date().toUTCString()}` });
return result;
}
async function bodyFromResponse(response: Response): Promise<ArrayBuffer> {
const result = await response.arrayBuffer();
return result;
}
function timeout(ms: number): Promise<void> {
return new Promise<void>(r => setTimeout(r, ms));
}
function formatRequestForSave(request: INetConsoleRequestInternal): INetConsoleRequest {
return {
authorization: request.authorization,
body: {
content: '',
},
bodyComponents: {
bodySelection: request.bodyComponents.bodySelection,
formData: request.bodyComponents.formData.valueSeq().toArray(),
rawTextBody: {
contentType: request.bodyComponents.rawTextBody.contentType || '',
text: request.bodyComponents.rawTextBody.text,
},
xWwwFormUrlencoded: request.bodyComponents.xWwwFormUrlencoded.valueSeq().toArray(),
},
description: request.description,
headers: request.headers.valueSeq().toArray(),
name: request.name,
queryParameters: request.queryParameters.valueSeq().toArray(),
routeParameters: request.routeParameters.valueSeq().toArray(),
url: request.url,
verb: request.verb,
fetchParams: request.fetchParams,
};
}