This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portaltest.html
736 lines (641 loc) · 26 KB
/
portaltest.html
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
<!DOCTYPE html>
<html lang="en">
<head>
<title>Portal Test</title>
<meta charset="utf-8"/>
<style type="text/css">
/* Form */
form { display: table; }
p { display: table-row; }
label { display: table-cell; padding-right: 1em; }
input { display: table-cell; }
image { max-width: 500px; }
</style>
</head>
<body>
<h1>Portal Test</h1>
<form>
<p>
<label for="portal"><b>Portal:</b></label>
<input type="text" name="portal" id="portal" value="https://siasky.net" />
</p>
<br>
<p>
<label for="uploadEndpoint">Upload Endpoint:</label>
<input type="text" name="uploadEndpoint" id="uploadEndpoint" value="/skynet/skyfile" />
</p>
<br>
<p>
<label for="downloadEndpoint">Download Endpoint:</label>
<input type="text" name="downloadEndpoint" id="downloadEndpoint" value="/" />
</p>
<br>
<p>
<label for="statsEndpoint">Stats Endpoint:</label>
<input type="text" name="statsEndpoint" id="statsEndpoint" value="/skynet/stats" />
</p>
<br>
<p>
<label for="blacklistEndpoint">Blacklist Endpoint:</label>
<input type="text" name="blacklistEndpoint" id="blacklistEndpoint" value="/skynet/blacklist" />
</p>
<br>
<p>
<label for="portalsEndpoint">Portals Endpoint:</label>
<input type="text" name="portalsEndpoint" id="portalsEndpoint" value="/skynet/portals" />
</p>
<br>
<p>
<label for="createSkykeyEndpoint">Create Skykey Endpoint:</label>
<input type="text" name="createSkykeyEndpoint" id="createSkykeyEndpoint" value="/skynet/createskykey" />
</p>
<br>
<p>
<label for="addSkykeyEndpoint">Add Skykey Endpoint:</label>
<input type="text" name="addSkykeyEndpoint" id="addSkykeyEndpoint" value="/skynet/addskykey" />
</p>
<br>
<p>
<label for="getSkykeysEndpoint">Get Skykeys Endpoint:</label>
<input type="text" name="getSkykeysEndpoint" id="getSkykeysEndpoint" value="/skynet/skykeys" />
</p>
<br>
<p>
<label for="getSkykeyEndpoint">Get Skykey Endpoint:</label>
<input type="text" name="getSkykeyEndpoint" id="getSkykeyEndpoint" value="/skynet/skykey" />
</p>
<br>
<p>
<label for="getSkykeyIdEndpoint">Get Skykey ID Endpoint:</label>
<input type="text" name="getSkykeyIdEndpoint" id="getSkykeyIdEndpoint" value="/skynet/skykeyid" />
</p>
<br>
<br>
<h2>Endpoint Test</h2>
<p>
<label for="uploadAmt">Upload Amount (KiBs):</label>
<input type="number" name="uploadAmt" id="uploadAmt" value="4000" />
</p>
<br>
<p>
<input type="button" value="Run Test" id="submit"/>
</p>
</form>
<br>
<label id="upload1"></label>
<br>
<label id="upload2"></label>
<br>
<label id="metadata"></label>
<br>
<label id="download"></label>
<br>
<label id="stats"></label>
<br>
<label id="blacklist1"></label>
<br>
<label id="blacklist2"></label>
<br>
<label id="portals1"></label>
<br>
<label id="portals2"></label>
<br>
<label id="createSkykey"></label>
<br>
<label id="addSkykey"></label>
<br>
<label id="getSkykeys"></label>
<br>
<label id="getSkykey"></label>
<br>
<label id="getSkykeyId"></label>
<h2>Directory Test</h2>
<form method="post" enctype="multipart/form-data">
<p>
<input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" />
</p>
<br>
<p>
<label for="directoryPath">Path:</label>
<input type="text" name="directoryPath" id="directoryPath" value="" />
</p>
<br>
<br>
<p>
<input type="button" value="Run Test" id="submit-directory"/>
</p>
</form>
<br>
<label id="directory1"></label>
<br>
<label id="directory2"></label>
<br>
<label id="directory3"></label>
<label id="end" />
<script type="text/javascript">
const uploadfile = "test";
// const skylink = "_AzaXbaPt7jRhHTDwZs5ScY_hjCXMUdmYYEDUE89vsj5lg";
const keyname = "testkey";
function randomString(length) {
return "a".repeat(1000).repeat(length);
}
async function testUpload(portal, endpoint, uploadAmt) {
document.getElementById("upload1").innerHTML = "Running upload...";
var data = randomString(uploadAmt);
var blob = new Blob([data], { type: "text/plain" });
var formData = new FormData();
formData.append("file", blob);
const startTime = new Date();
var timeDiff;
var skylink = await fetch(portal + endpoint + "?filename=" + uploadfile, {
method: "POST",
body: formData,
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime; // in ms
return response.json();
})
.then((result) => {
var skylink = result.skylink;
var output1 = "Skylink: " + portal + "/" + skylink;
var output2 =
endpoint +
" Upload test took " +
timeDiff +
"ms to complete";
document.getElementById("upload1").innerHTML = output1;
document.getElementById("upload2").innerHTML = output2;
return skylink;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("upload1").innerHTML = endpoint + " " + error;
});
return skylink;
}
async function testMetadata(portal, endpoint, skylink) {
document.getElementById("metadata").innerHTML = "Getting file metadata...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint + skylink, {
method: "HEAD",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
var metadata = response.headers.get("skynet-file-metadata");
var filename = JSON.parse(metadata).filename;
if (filename != uploadfile) {
throw "expected filename "+uploadfile+", got "+filename;
}
var output =
endpoint +
" Metadata test took " +
timeDiff +
"ms to complete";
document.getElementById("metadata").innerHTML = output;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("metadata").innerHTML = endpoint + " " + error;
});
}
async function testDownload(portal, endpoint, skylink) {
document.getElementById("download").innerHTML = "Downloading file...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint + skylink, {
method: "GET",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
var metadata = response.headers.get("skynet-file-metadata");
var filename = JSON.parse(metadata).filename;
if (filename != uploadfile) {
throw "expected filename "+uploadfile+", got "+filename;
}
var output =
endpoint +
" Download test took " +
timeDiff +
"ms to complete";
document.getElementById("download").innerHTML = output;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("download").innerHTML = endpoint + " " + error;
});
}
async function testStats(portal, endpoint) {
document.getElementById("stats").innerHTML = "Getting stats...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint, {
method: "GET",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
var version = result.version;
var output =
endpoint +
" Stats test took " +
timeDiff +
"ms to complete. Version: " +
version;
document.getElementById("stats").innerHTML = output;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("stats").innerHTML = endpoint + " " + error;
});
}
// Test that GET on the blacklist endpoint succeeds.
async function testBlacklistGet(portal, endpoint) {
document.getElementById("blacklist1").innerHTML = "Getting blacklist...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint, {
method: "GET",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
var blacklist = result.blacklist;
var output =
endpoint +
" Blacklist GET test took " +
timeDiff +
"ms to complete. Blacklist length: " +
blacklist.length;
document.getElementById("blacklist1").innerHTML = output;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("blacklist1").innerHTML = endpoint + " " + error;
});
}
// Test that POST on the blacklist endpoint fails.
async function testBlacklistPost(portal, endpoint) {
document.getElementById("blacklist2").innerHTML = "Updating blacklist (should fail)...";
const startTime = new Date();
var timeDiff;
const body = '{"add" : ["GAC38Gan6YHVpLl-bfefa7aY85fn4C0EEOt5KJ6SPmEy4g"]}'
fetch(portal + endpoint, {
method: "POST",
body: body,
})
.then((result) => {
error = endpoint + " error: expected blacklist POST to fail"
console.error(error);
document.getElementById("blacklist2").innerHTML = error
})
.catch((error) => {
var output =
endpoint +
" Blacklist POST did not succeed (expected)";
document.getElementById("blacklist2").innerHTML = output;
});
}
// Test that GET on the portals endpoint fails.
async function testPortalsGet(portal, endpoint) {
document.getElementById("portals1").innerHTML = "Getting portals list...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint, {
method: "GET",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
var portals = result.portals;
var output =
endpoint +
" Portals GET test took " +
timeDiff +
"ms to complete. Portals list length: " +
portals.length;
document.getElementById("portals1").innerHTML = output;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("portals1").innerHTML = endpoint + " " + error;
});
}
// Test that POST on the portals endpoint fails.
async function testPortalsPost(portal, endpoint) {
document.getElementById("portals2").innerHTML = "Updating portals list (should fail)...";
const startTime = new Date();
var timeDiff;
const body = '{"add" : ["GAC38Gan6YHVpLl-bfefa7aY85fn4C0EEOt5KJ6SPmEy4g"]}'
fetch(portal + endpoint, {
method: "POST",
body: body,
})
.then((result) => {
error = endpoint + " error: expected portals POST to fail"
console.error(error);
document.getElementById("portals2").innerHTML = error
})
.catch((error) => {
var output =
endpoint +
" Portals POST did not succeed (expected)";
document.getElementById("portals2").innerHTML = output;
});
}
async function testCreateSkykey(portal, endpoint) {
document.getElementById("createSkykey").innerHTML = "Creating skykey...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint, {
method: "POST",
body: keyname,
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
var skykey = result.portals;
var output =
endpoint +
" Create skykey test took " +
timeDiff +
"ms to complete. Skykey: " +
skykey;
document.getElementById("createSkykey").innerHTML = output;
return skykey;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("createSkykey").innerHTML = endpoint + " " + error;
});
}
async function testAddSkykey(portal, endpoint, skykey) {
document.getElementById("addSkykey").innerHTML = "Adding skykey...";
skykey = skykey.substring(skykey.indexOf(":")+1, skykey.indexOf("?"));
const startTime = new Date();
var timeDiff;
var skykey = await fetch(portal + endpoint + "?skykey=" + skykey, {
method: "POST",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
var skykey = result.portals;
var output =
endpoint + "?skykey=" + skykey +
" Add skykey test took " +
timeDiff +
"ms to complete.";
document.getElementById("addSkykey").innerHTML = output;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("addSkykey").innerHTML = endpoint + " " + error;
});
return skykey;
}
async function testGetSkykeys(portal, endpoint, skykey) {
document.getElementById("getSkykeys").innerHTML = "Getting skykeys...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint, {
method: "GET",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
var skykeys = result.skykeys;
var output =
endpoint +
" Get skykeys test took " +
timeDiff +
"ms to complete. Skykeys length: " +
skykeys.length;
document.getElementById("getSkykeys").innerHTML = output;
return skykey;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("getSkykeys").innerHTML = endpoint + " " + error;
});
}
async function testGetSkykey(portal, endpoint, skykey) {
document.getElementById("getSkykey").innerHTML = "Getting skykey " + skykey + "...";
const startTime = new Date();
var timeDiff;
var id = await fetch(portal + endpoint + "?name=" + keyname, {
method: "GET",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
if (result.skykey != skykey) {
throw "expected skykey "+skykey+", got "+result.skykey;
}
var output =
endpoint +
" Get skykey test took " +
timeDiff +
"ms to complete. Skykey ID: " +
result.id;
document.getElementById("getSkykey").innerHTML = output;
return result.id;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("getSkykey").innerHTML = endpoint + " " + error;
});
return id;
}
async function testGetSkykeyId(portal, endpoint, skykeyid) {
document.getElementById("getSkykeyId").innerHTML = "Getting skykey ID " + skykey + "...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint + "?name=" + keyname, {
method: "GET",
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
if (result.skykeyid != skykeyid) {
throw "expected skykey ID "+skykeyid+", got "+result.skykeyid;
}
var output =
endpoint +
" Get skykey ID test took " +
timeDiff +
"ms to complete. Skykey ID: " +
result.skykeyid;
document.getElementById("getSkykeyId").innerHTML = output;
return result.id;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("getSkykeyId").innerHTML = endpoint + " " + error;
});
}
async function testPortalEndpoints() {
// Get inputs.
const portal = document.getElementById("portal").value;
const uploadEndpoint = document.getElementById("uploadEndpoint").value;
const downloadEndpoint = document.getElementById("downloadEndpoint").value;
const statsEndpoint = document.getElementById("statsEndpoint").value;
const blacklistEndpoint = document.getElementById("blacklistEndpoint").value;
const portalsEndpoint = document.getElementById("portalsEndpoint").value;
const createSkykeyEndpoint = document.getElementById("createSkykeyEndpoint").value;
const addSkykeyEndpoint = document.getElementById("addSkykeyEndpoint").value;
const getSkykeysEndpoint = document.getElementById("getSkykeysEndpoint").value;
const getSkykeyEndpoint = document.getElementById("getSkykeyEndpoint").value;
const getSkykeyIdEndpoint = document.getElementById("getSkykeyIdEndpoint").value;
const uploadAmt = document.getElementById("uploadAmt").value;
// Run tests.
var skylink = await testUpload(portal, uploadEndpoint, uploadAmt);
skylink = skylink.replace(portal, "");
await testMetadata(portal, downloadEndpoint, skylink);
await testDownload(portal, downloadEndpoint, skylink);
await testStats(portal, statsEndpoint);
await testBlacklistGet(portal, blacklistEndpoint);
await testBlacklistPost(portal, blacklistEndpoint);
await testPortalsGet(portal, portalsEndpoint);
await testPortalsPost(portal, portalsEndpoint);
var skykey = await testCreateSkykey(portal, createSkykeyEndpoint);
await testAddSkykey(portal, addSkykeyEndpoint, skykey);
await testGetSkykeys(portal, getSkykeysEndpoint, skykey);
var skykeyid = await testGetSkykey(portal, getSkykeyEndpoint, skykey);
await testGetSkykeyId(portal, getSkykeyIdEndpoint, skykeyid);
}
async function testDirectoryUpload(portal, endpoint, input, path) {
const numFiles = input.files.length;
var files = new Array(numFiles);
document.getElementById("directory1").innerHTML = "Uploading "+numFiles+" files...";
var formData = new FormData();
for (var i = 0; i < numFiles; i++) {
const name = input.files[i].name;
formData.append(
"files[]",
input.files[i],
name,
);
files[i] = name;
}
const startTime = new Date();
var timeDiff;
var skylink = await fetch(portal + endpoint + "/" + path + "?filename=" + uploadfile, {
method: "POST",
body: formData,
})
.then((response) => {
var endTime = new Date();
timeDiff = endTime - startTime;
return response.json();
})
.then((result) => {
var skylink = result.skylink;
var output =
endpoint +
" Directory upload test took " +
timeDiff +
"ms to complete. Skylink: " +
skylink;
document.getElementById("directory1").innerHTML = output;
return skylink;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("directory1").innerHTML = endpoint + " " + error;
});
return { skylink, files };
}
// Test downloading an entire directory at once.
async function testDirectoryDownload(portal, endpoint, skylink) {
document.getElementById("directory2").innerHTML = "Downloading directory...";
const startTime = new Date();
var timeDiff;
fetch(portal + endpoint + skylink + "?format=concat", {
method: "GET",
})
.then(() => {
var endTime = new Date();
timeDiff = endTime - startTime;
var output =
endpoint +
" Directory download test took " +
timeDiff +
"ms to complete";
document.getElementById("directory2").innerHTML = output;
})
.catch((error) => {
console.error(endpoint + " error:", error);
document.getElementById("directory2").innerHTML = endpoint + " " + error;
});
}
// Test downloading individual files from a directory.
async function testDirectoryDownloadFiles(portal, endpoint, skylink, files) {
document.getElementById("directory3").innerHTML = "Downloading individual directory files...";
const startTime = new Date();
var timeDiff;
var gets = new Array(files.length);
for (var i = 0; i < files.length; i++) {
// Create new img element.
var img = document.createElement("img");
img.src = portal + endpoint + skylink + "/" + files[i];
// Insert img at the bottom of the page.
const end = document.getElementById("end");
end.parentNode.insertBefore(img, end);
}
}
async function testPortalDirectory() {
// Get inputs.
const portal = document.getElementById("portal").value;
const uploadEndpoint = document.getElementById("uploadEndpoint").value;
const downloadEndpoint = document.getElementById("downloadEndpoint").value;
const input = document.getElementById('filesToUpload');
const path = document.getElementById('directoryPath').value;
// Run tests.
const result = await testDirectoryUpload(portal, uploadEndpoint, input, path);
const skylink = result.skylink.replace(portal, "");
const files = result.files;
await testDirectoryDownload(portal, downloadEndpoint, skylink);
await testDirectoryDownloadFiles(portal, downloadEndpoint, skylink, files);
}
var url = new URL(window.location.href);
document.getElementById("portal").value = url.href.substring(
0,
url.href.indexOf(url.pathname)
);
document.getElementById("submit").addEventListener("click", function () {
testPortalEndpoints();
});
document.getElementById("submit-directory").addEventListener("click", function () {
testPortalDirectory();
});
</script>
</body>
</html>