This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmds.js
More file actions
819 lines (650 loc) · 18.5 KB
/
mds.js
File metadata and controls
819 lines (650 loc) · 18.5 KB
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/**
* MDS JS lib for MiniDAPPs..
*
* @spartacusrex
*/
/**
* The MAIN Minima Callback function
*/
var MDS_MAIN_CALLBACK = null;
/**
* API call id array
*/
var API_CALLS = [];
/**
* Main MINIMA Object for all interaction
*/
var MDS = {
//Main File host
filehost : "",
//RPC Host for Minima
mainhost : "",
//The MiniDAPP UID
minidappuid : "",
//Is logging RPC enabled
logging : false,
//When debuggin you can hard set the Host and port
DEBUG_HOST : null,
DEBUG_PORT : -1,
//An allowed TEST Minidapp ID for SQL - can be overridden
DEBUG_MINIDAPPID : "0x00",
/**
* Minima Startup - with the callback function used for all Minima messages
*/
init : function(callback){
//Log a little..
MDS.log("Initialising MDS..");
//Is logging enabled.. via the URL
if(MDS.form.getParams("MDS_LOGGING") != null){
MDS.logging = true;
}
//Get the host and port..
var host = window.location.hostname;
var port = Math.floor(window.location.port);
//Get ther MiniDAPP UID
MDS.minidappuid = MDS.form.getParams("uid");
//HARD SET if debug mode - running from a file
if(MDS.DEBUG_HOST != null){
MDS.log("DEBUG Settings Found..");
host=MDS.DEBUG_HOST;
port=MDS.DEBUG_PORT;
}
if(MDS.minidappuid == null){
MDS.minidappuid = MDS.DEBUG_MINIDAPPID;
}
//Is one specified..
if(MDS.minidappuid == "0x00"){
MDS.log("No MiniDAPP UID specified.. using test value");
}
MDS.filehost = "https://"+host+":"+port+"/";
MDS.mainhost = "https://"+host+":"+port+"/mdscommand_/";
MDS.log("MDS HOST : "+MDS.filehost);
//Store this for poll messages
MDS_MAIN_CALLBACK = callback;
//Start the Long Poll listener
PollListener();
//And Post a message
MDSPostMessage({ "event": "inited" });
},
/**
* Log some data with a timestamp in a consistent manner to the console
*/
log : function(output){
console.log("Minima @ "+new Date().toLocaleString()+" : "+output);
},
/**
* Notify the User - on Phone it pops up in status bar. On desktop appears in Logs
*/
notify : function(output){
//Send via POST
httpPostAsync("notify", output);
},
/**
* Cancel this MiniDAPPs notification
*/
notifycancel : function(){
//Send via POST
httpPostAsync("notifycancel", "*");
},
/**
* Runs a function on the Minima Command Line - same format as MInima
*/
cmd : function(command, callback){
//Send via POST
httpPostAsync("cmd", command, callback);
},
/**
* Runs a SQL command on this MiniDAPPs SQL Database
*/
sql : function(command, callback){
//Send via POST
httpPostAsync("sql", command, callback);
},
/**
* Get a link to a different Dapp. READ dapps can only get READ DAPPS. WRITE can get all dapps.
*/
dapplink : function(dappname, callback){
//Send via POST
httpPostAsync("dapplink", dappname, function(result){
var linkdata = {};
linkdata.status = result.status;
//Create the link..
if(result.status){
linkdata.uid = result.response.uid;
linkdata.sessionid = result.response.sessionid;
linkdata.base = MDS.filehost+linkdata.uid+"/index.html?uid="+result.response.sessionid;
}else{
//Not found..
linkdata.error = result.error;
}
callback(linkdata);
});
},
/**
* API call to a different minidapp
*/
api : {
call : function(dappname, data, callback){
//Construct a unique API request
var rand = ""+Math.random()*1000000000;
//Construct a callback list object
var callitem = {};
callitem.id = rand;
callitem.callback = callback;
//Add to the api calls..
API_CALLS.push(callitem);
//Create the single line
var commsline = dappname+"&request&"+rand+"&"+data;
//Send via POST
httpPostAsync("api", commsline);
},
reply : function(dappname, id, data, callback){
//Create the single line
var commsline = dappname+"&response&"+id+"&"+data;
//Send via POST
httpPostAsync("api", commsline, callback);
}
},
/**
* Network Commands
*/
net : {
/**
* Make a GET request
*/
GET : function(url, callback){
//Send via POST
httpPostAsync("net", url, callback);
},
/**
* Make a POST request
*/
POST : function(url, data, callback){
//Create the sinlg eline version..
var postline = url+"&"+data;
//Send via POST
httpPostAsync("netpost", postline, callback);
}
},
/**
* Simple GET and SET key value pairs that are saved persistently
*/
keypair : {
/**
* GET a value
*/
get : function(key, callback){
//Create the single line
var commsline = "get&"+key;
//Send via POST
httpPostAsync("keypair", commsline, callback);
},
/**
* SET a value
*/
set : function(key, value, callback){
//Create the single line
var commsline = "set&"+key+"&"+value;
//Send via POST
httpPostAsync("keypair", commsline, callback);
}
},
/**
* COMMS - send a message to ALL minidapps or JUST your own service.js
*/
comms : {
/**
* PUBLIC message broadcast to ALL (callback is optional)
*/
broadcast : function(msg, callback){
//Create the single line
var commsline = "public&"+msg;
//Send via POST
httpPostAsync("comms", commsline, callback);
},
/**
* PRIVATE message send just to this MiniDAPP (callback is optional)
*/
solo : function(msg, callback){
//Create the single line
var commsline = "private&"+msg;
//Send via POST
httpPostAsync("comms", commsline, callback);
}
},
/**
* File access
*/
file : {
/**
* List file in a folder .. start at /
*/
list : function(folder, callback){
//Create the single line
var commsline = "list&"+folder;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Save text - can be text, a JSON in string format or hex encoded data
*/
save : function(filename, text, callback){
//Create the single line
var commsline = "save&"+filename+"&"+text;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Save Binary Data - supply as a HEX string
*/
savebinary : function(filename, hexdata, callback){
//Create the single line
var commsline = "savebinary&"+filename+"&"+hexdata;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Load text - can be text, a JSON in string format or hex encoded data
*/
load : function(filename, callback){
//Create the single line
var commsline = "load&"+filename;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Load Binary data - returns the HEX data
*/
loadbinary : function(filename, callback){
//Create the single line
var commsline = "loadbinary&"+filename;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Delete a file
*/
delete : function(filename, callback){
//Create the single line
var commsline = "delete&"+filename;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Get the full path - if you want to run a command on the file / import a txn / unsigned txn etc
*/
getpath : function(filename, callback){
//Create the single line
var commsline = "getpath&"+filename;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Make a directory
*/
makedir : function(filename, callback){
//Create the single line
var commsline = "makedir&"+filename;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Copy a file
*/
copy : function(filename, newfilename, callback){
//Create the single line
var commsline = "copy&"+filename+"&"+newfilename;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Move a file
*/
move : function(filename, newfilename, callback){
//Create the single line
var commsline = "move&"+filename+"&"+newfilename;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Download a File from the InterWeb - Will be put in Downloads folder
*/
download : function(url, callback){
//Create the single line
var commsline = "download&"+url;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Upload a file in chunks to the /fileupload folder
*/
upload : function(file, callback){
//Start the file recursion..
_recurseUploadMDS(file,0,callback);
},
/**
* List file in a folder .. start at /
*/
listweb : function(folder, callback){
//Create the single line
var commsline = "listweb&"+folder;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Copy a file to your web folder
*/
copytoweb : function(file, webfile, callback){
//Create the single line
var commsline = "copytoweb&"+file+"&"+webfile;
//Send via POST
httpPostAsync("file", commsline, callback);
},
/**
* Delete a file or folder from web folder
*/
deletefromweb : function(file, callback){
//Create the single line
var commsline = "deletefromweb&"+file;
//Send via POST
httpPostAsync("file", commsline, callback);
}
},
/**
* Function for GET parameters..
*/
form : {
//Return the GET parameter by scraping the location..
getParams : function(parameterName){
var result = null,
tmp = [];
var items = window.location.search.substr(1).split("&");
for (var index = 0; index < items.length; index++) {
tmp = items[index].split("=");
//console.log("TMP:"+tmp);
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
}
return result;
}
},
/**
* UTILITY functions.. very useful
*/
util : {
//Convert HEX to Base 64 - removes the 0x if necessary
hexToBase64(hexstring) {
//Check if starts with 0x
var thex = hexstring;
if(hexstring.startsWith("0x")){
thex = hexstring.substring(2);
}
return btoa(thex.match(/\w{2}/g).map(function(a) {
return String.fromCharCode(parseInt(a, 16));
}).join(""));
},
//Convert Base64 to HEX
base64ToHex(str) {
const raw = atob(str);
let result = '';
for (let i = 0; i < raw.length; i++) {
const hex = raw.charCodeAt(i).toString(16);
result += (hex.length === 2 ? hex : '0' + hex);
}
return result.toUpperCase();
},
//Convert Base64 to a Uint8Array - useful for Blobs
base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
},
//Return a state variable given the coin
getStateVariable(coin,port){
//Get the state vars
var statvars = coin.state;
var len = statvars.length;
for (var i = 0; i < len; i++) {
var state = statvars[i];
if(state.port == port){
return state.data;
}
}
return undefined;
}
}
};
/**
* Post a message to the Minima Event Listeners
*/
function MDSPostMessage(json){
//And dispatch
if(MDS_MAIN_CALLBACK){
//Is this an API response call..
if(json.event == "MDSAPI"){
//Check if it is a response..
if(!json.data.request){
//Find the API CALL Object
var found = "";
var len = API_CALLS.length;
for(var i=0;i<len;i++){
if(API_CALLS[i].id == json.data.id){
//found it..!
found = json.data.id;
//Construct a reply..
var reply = {};
reply.status = json.data.status;
reply.data = json.data.message;
API_CALLS[i].callback(reply);
}
}
//Remove it..
if(found != ""){
API_CALLS = API_CALLS.filter(function(apic){
return apic.id != found;
});
}else{
//MDS.log("API CALL NOT FOUND!"+JSON.stringify(json));
}
//Response messages not forwarded - only via API call
return;
}
}
//Call the main function
MDS_MAIN_CALLBACK(json);
}
}
var PollCounter = 0;
var PollSeries = 0;
function PollListener(){
//The POLL host
var pollhost = MDS.mainhost+"poll?"+"uid="+MDS.minidappuid;
var polldata = "series="+PollSeries+"&counter="+PollCounter;
httpPostAsyncPoll(pollhost,polldata,function(msg){
//Are we on the right Series..
if(PollSeries != msg.series){
//Reset to the right series..
PollSeries = msg.series;
PollCounter = msg.counter;
}else{
//Is there a message ?
if(msg.status == true){
//Get the current counter..
PollCounter = msg.response.counter+1;
//And Post the message..
MDSPostMessage(msg.response.message);
}
}
//And around we go again..
PollListener();
});
}
function postMDSFail(command, params, status){
//Some error..
if(MDS.logging){
MDS.log("** An error occurred during an MDS command!");
}
//Create the message
var errormsg = {};
errormsg.event = "MDSFAIL";
errormsg.data = {};
errormsg.data.command = command;
errormsg.data.params = params;
errormsg.data.error = status;
//Post it to the stack
MDSPostMessage(errormsg);
}
/**
* Utility function for GET request
*
* @param theUrl
* @param callback
* @param params
* @returns
*/
function httpPostAsync(theUrl, params, callback){
//Add the MiniDAPP UID..
var finalurl = MDS.mainhost+theUrl+"?uid="+MDS.minidappuid;
//Do we log it..
if(MDS.logging){
MDS.log("POST_RPC:"+finalurl+" PARAMS:"+params);
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
var status = xmlHttp.status;
if (xmlHttp.readyState == XMLHttpRequest.DONE){
if (status === 0 || (status >= 200 && status < 400)) {
//Do we log it..
if(MDS.logging){
MDS.log("RESPONSE:"+xmlHttp.responseText);
}
//Send it to the callback function..
if(callback){
callback(JSON.parse(xmlHttp.responseText));
}
}else{
//Some error..
postMDSFail(finalurl,params,xmlHttp.status);
}
}
}
xmlHttp.open("POST", finalurl, true); // true for asynchronous
xmlHttp.overrideMimeType('text/plain; charset=UTF-8');
xmlHttp.send(encodeURIComponent(params));
//xmlHttp.onerror = function () {
// console.log("** An error occurred during the transaction");
//};
}
/**
* POLLING Call
*/
function httpPostAsyncPoll(theUrl, params, callback){
//Do we log it..
if(MDS.logging){
MDS.log("POST_POLL_RPC:"+theUrl+" PARAMS:"+params);
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
var status = xmlHttp.status;
if (xmlHttp.readyState == XMLHttpRequest.DONE){
if (status === 0 || (status >= 200 && status < 400)) {
//Do we log it..
if(MDS.logging){
MDS.log("RESPONSE:"+xmlHttp.responseText);
}
//Send it to the callback function..
if(callback){
callback(JSON.parse(xmlHttp.responseText));
}
}else{
//Some error..
postMDSFail(theUrl,params,xmlHttp.status);
}
}
}
xmlHttp.addEventListener('error', function(ev){
MDS.log("Error Polling - reconnect in 10s");
setTimeout(function(){PollListener();},10000);
});
xmlHttp.open("POST", theUrl, true); // true for asynchronous
xmlHttp.overrideMimeType('text/plain; charset=UTF-8');
xmlHttp.send(encodeURIComponent(params));
}
/**
* Internal recursive function for file upload
*/
function _recurseUploadMDS(thefullfile, chunk, callback){
//Get some details
var filename = thefullfile.name;
var filesize = thefullfile.size;
//1MB MAX Chunk size..
var chunk_size = 1024 * 1024;
var allchunks = Math.ceil(filesize / chunk_size);
//Have we finished..
if(chunk>allchunks-1){
return;
}
var startbyte = chunk_size * chunk;
var endbyte = startbyte + chunk_size;
if(endbyte>filesize){
endbyte = filesize;
}
//Get a piece of the file
var filepiece = thefullfile.slice(startbyte,endbyte);
//Create a form..
var formdata = new FormData();
formdata.append("uid", MDS.minidappuid);
//Filedata handled a little differently
formdata.append("filename", filename);
formdata.append("filesize", filesize);
formdata.append("allchunks", allchunks);
formdata.append("chunknum", chunk);
formdata.append("fileupload", filepiece);
var request = new XMLHttpRequest();
request.open("POST", "/fileuploadchunk.html");
request.onreadystatechange = function() {
var status = request.status;
if (request.readyState == XMLHttpRequest.DONE){
if (status === 0 || (status >= 200 && status < 400)) {
//Send it to the callback function..
if(callback){
var resp = {};
resp.status = true;
resp.filename = filename;
resp.size = filesize;
resp.allchunks = allchunks;
resp.chunk = chunk+1;
resp.start = startbyte;
resp.end = endbyte;
callback(resp);
}
//And now continue uploading..
if(callback){
_recurseUploadMDS(thefullfile,chunk+1, callback);
}else{
_recurseUploadMDS(thefullfile,chunk+1);
}
}else{
if(callback){
var resp = {};
resp.status = false;
resp.error = request.responseText;
resp.filename = filename;
resp.size = filesize;
resp.allchunks = allchunks;
resp.chunk = chunk;
resp.start = startbyte;
resp.end = endbyte;
callback(resp);
}
//Some error..
MDS.log("MDS FILEUPLOAD CHUNK ERROR: "+request.responseText);
}
}
}
//And finally send the POST request
request.send(formdata);
}