slimfits
Version:
Package for loading data stored in FITS data format
54 lines • 2.76 kB
JavaScript
import { Header, AcceptRangeHeader } from '../utils/Header';
import { PromiseUtils } from '../utils/PromiseUtils';
import { ArrayUtils } from '../utils/ArrayUtils';
var MultipleRequestFile = /** @class */ (function () {
function MultipleRequestFile(url) {
this.url = url;
this.size = 0;
}
MultipleRequestFile.parseHeaders = function (headerStr) {
return !headerStr ? [] : headerStr.split('\u000d\u000a').filter(function (x) { return x !== '' || x.trim() !== ''; }).map(function (pair) {
var index = pair.indexOf('\u003a\u0020');
if (index > 0) {
return new Header(pair.substring(0, index), pair.substring(index + 2));
}
throw new Error('keyword not found');
});
};
MultipleRequestFile.prototype.initialize = function () {
var _this = this;
return PromiseUtils.getRequestAsync(this.url, 'HEAD', 'text').then(function (xhr) {
var headers = MultipleRequestFile.parseHeaders(xhr.getAllResponseHeaders());
if (headers.some(function (h) { return (h.name === 'Accept-Ranges') && (h.value === 'bytes'); })) {
var s = headers.filter(function (h) { return h.name === 'Content-Length'; });
_this.size = parseInt(s[0].value, 10);
return _this.getStringAsync(0, 6);
}
else {
throw new Error('File does not support Ranges request keyword');
}
}).then(function (value) { return (value === 'SIMPLE') && (_this.getByteLength() % 2880 === 0); });
};
MultipleRequestFile.prototype.getByteLength = function () {
return this.size;
};
MultipleRequestFile.prototype.getStringAsync = function (start, byteLength) {
var headers = [new AcceptRangeHeader(start, byteLength)];
return PromiseUtils.getRequestAsync(this.url, 'GET', 'text', headers)
.then(function (xhr) { return xhr.responseText; });
};
MultipleRequestFile.prototype.getDataAsync = function (start, length, bitPix, changeEndian) {
if (changeEndian === void 0) { changeEndian = true; }
var typedArray = ArrayUtils.generateTypedArray(bitPix, length);
var byteLength = typedArray.BYTES_PER_ELEMENT * length;
var headers = [new AcceptRangeHeader(start, byteLength)];
return PromiseUtils.getRequestAsync(this.url, 'GET', 'arraybuffer', headers).then(function (xhr) {
var source = xhr.response;
ArrayUtils.copy(source, typedArray.buffer, 0, length, bitPix, changeEndian);
return typedArray;
});
};
return MultipleRequestFile;
}());
export { MultipleRequestFile };
//# sourceMappingURL=MultipleRequestFile.js.map