-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtio.js
82 lines (63 loc) · 2.74 KB
/
tio.js
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
var fieldSeparator = "\xff";
var startOfExtraFields = "\xfe";
var TIO = {
run:
async function run(code, input, lang) {
const encoder = new TextEncoder("utf-8");
var length = encoder.encode(code).length;
var iLength = encoder.encode(input).length;
// Vlang\u00001\u0000{language}\u0000F.code.tio\u0000{# of bytes in code}\u0000{code}F.input.tio\u0000{length of input}\u0000{input}Vargs\u0000{number of ARGV}{ARGV}\u0000R
var rBody = "Vlang\x001\x00" + lang + "\x00F.code.tio\x00" + length + "\x00" + code + "F.input.tio\x00" + iLength + "\x00" + input + "Vargs\x000\x00R";
rBody = encode(rBody);
var fetched = await fetch("https://tio.run/cgi-bin/run/api/", {
method: "POST",
headers: {
"Content-Type": "text/plain;charset=utf-8"
},
body: rBody
});
var read = (await fetched.body.getReader().read()).value;
var text = new TextDecoder('utf-8').decode(read);
return text.slice(16).split(text.slice(0, 16));
},
makeLink:
function makeLink(languageId, header = "", code = "", footer = "", input = "", args = [], options = [], fullLink = true) {
var stateString = languageId;
var saveTextArea = function(textArea) {
stateString += fieldSeparator + textToByteString(textArea);
};
[header, code, footer, input, ...args].forEach(saveTextArea);
if (options.length) {
stateString += startOfExtraFields + "options";
options.forEach(saveTextArea);
}
// TODO: This default arg isn't working for some reason
return (fullLink? "https://tio.run/##": "") +
//byteStringToBase64(byteArrayToByteString(deflate(byteStringToByteArray(stateString))));
arrToB64(deflate(byteStringToByteArray(stateString)));
},
parseLink:
function parseLink(link) {
if (link.slice(0,18) === "https://tio.run/##") {
link = link.slice(18);
}
var stateString = byteArrayToByteString(inflate(b64ToArr(link)));
var fields = stateString.split(startOfExtraFields);
var fields = fields.map(n=>n.split(fieldSeparator));
var [languageId, header, code, footer, input, ...args] = fields[0];
[header, code, footer, input, ...args] = [header, code, footer, input, ...args].map(n=>byteStringToText(n));
var options = [];
if (fields.length > 1) {
options = fields[1].slice(1);
}
return {
"languageId": languageId,
"header": header,
"code": code,
"footer": footer,
"input": input,
"args": args,
"options": options
}
},
};