...
 
Commits (2)
function APIRequest(method, url, data)
{
this.method = method;
this.url = url;
this.data = data;
this.headers = {};
this.mime = null;
this.responseType = '';
this.rq = null;
this.rate_limit_remaining = null;
this.rate_limit_reset = null;
}
APIRequest.prototype.overrideMimeType = function(mime)
{
if (this.rq !== null)
return this;
this.mime = mime;
return this;
};
APIRequest.prototype.setHeader = function(k, v)
{
if (this.rq !== null)
return this;
this.headers[k] = v;
return this;
};
APIRequest.prototype.setResponseType = function(type)
{
if (this.rq !== null)
return this;
this.responseType = type;
return this;
};
APIRequest.prototype.send = function()
{
if (this.rq !== null)
return Promise.reject("Already sending.");
this.rq = new XMLHttpRequest();
return new Promise((resolve, reject) => {
this.rq.onreadystatechange = (evt) => {
if (this.rq.readyState === XMLHttpRequest.HEADERS_RECEIVED)
{
this.rate_limit_remaining = this.rq.getResponseHeader('X-RateLimit-Remaining');
this.rate_limit_reset = this.rq.getResponseHeader('X-RateLimit-Reset');
}
if (this.rq.readyState !== XMLHttpRequest.DONE)
return;
if (this.rate_limit_remaining !== null && this.rate_limit_reset !== null
&& this.rate_limit_remaining <= 0
&& Date.now() / 1000 <= this.rate_limit_reset)
{
window.setTimeout(() => {
this.rq = null;
resolve(this.send());
}, this.rate_limit_reset * 1000 - Date.now());
this.rate_limit_remaining = null;
this.rate_limit_reset = null;
return;
}
if (((this.rq.status / 100) | 0) !== 2)
{
reject(this.rq);
this.rq = null;
return;
}
resolve(this.rq);
this.rq = null;
};
this.rq.onerror = (evt) => {
reject(this.rq);
this.rq = null;
};
this.rq.open(this.method, this.url, true);
for (let k in this.headers)
{
if (!this.headers.hasOwnProperty(k))
continue;
this.rq.setRequestHeader(k, this.headers[k]);
}
if (this.mime !== null)
this.rq.overrideMimeType(this.mime);
this.rq.responseType = this.responseType;
this.rq.send(this.data);
});
};
......@@ -7,30 +7,40 @@ function TarFile()
TarFile.prototype.appendFile = function(filename, data)
{
let header = new Uint8Array(512);
let d = new Uint8Array(Math.ceil(data.length / 512) * 512);
let fname = this.enc.encode(filename);
let ftype = '0';
if (fname.length > 100)
this.appendFile('././@LongLink', fname);
if (filename === '././@LongLink')
ftype = 'L';
if (typeof data === 'string')
data = this.enc.encode(data);
let num_encode = (num, pad) => this.enc.encode(num.toString(8).padStart(pad, '0'));
header.set(this.enc.encode(filename).subarray(0, 100), 0);
header.set(fname.subarray(0, 100), 0);
header.set(num_encode(0644, 7), 100);
header.set(num_encode(0, 7), 108);
header.set(num_encode(0, 7), 116);
header.set(num_encode(data.length, 11), 124);
header.set(num_encode(0, 11), 136);
header.set(this.enc.encode(' '), 148);
header.set(num_encode(0, 1), 156);
header.set(this.enc.encode(ftype), 156);
header.set(this.enc.encode('ustar'), 257);
header.set(num_encode(0, 2), 263);
header.set(this.enc.encode('root'), 265);
header.set(this.enc.encode('root'), 297);
header.set(num_encode(0, 8), 329);
header.set(num_encode(0, 8), 337);
header.set(this.enc.encode('/'), 345);
let checksum = header.reduce((old, cur) => old + cur);
header.set(num_encode(checksum, 6), 148);
header.set(0, 154);
let d = new Uint8Array(Math.ceil(data.length / 512) * 512);
d.set(data, 0);
this.parts.push(header);
......
......@@ -3,6 +3,7 @@
<html>
<head>
<meta charset="UTF-8">
<script src="APIRequest.js"></script>
<script src="TarFile.js"></script>
<script src="main.js"></script>
</head>
......
This diff is collapsed.