data-source.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. *
  7. * Adapted from LiteMol
  8. */
  9. import { Task, RuntimeContext } from '../mol-task';
  10. import { unzip, ungzip } from './zip/zip';
  11. import { utf8Read } from '../mol-io/common/utf8';
  12. import { AssetManager, Asset } from './assets';
  13. // polyfill XMLHttpRequest in node.js
  14. const XHR = typeof document === 'undefined' ? require('xhr2') as {
  15. prototype: XMLHttpRequest;
  16. new(): XMLHttpRequest;
  17. readonly DONE: number;
  18. readonly HEADERS_RECEIVED: number;
  19. readonly LOADING: number;
  20. readonly OPENED: number;
  21. readonly UNSENT: number;
  22. } : XMLHttpRequest;
  23. export enum DataCompressionMethod {
  24. None,
  25. Gzip,
  26. Zip,
  27. }
  28. export type DataType = 'json' | 'xml' | 'string' | 'binary' | 'zip'
  29. export type DataValue = 'string' | any | XMLDocument | Uint8Array
  30. export type DataResponse<T extends DataType> =
  31. T extends 'json' ? any :
  32. T extends 'xml' ? XMLDocument :
  33. T extends 'string' ? string :
  34. T extends 'binary' ? Uint8Array :
  35. T extends 'zip' ? { [k: string]: Uint8Array } : never
  36. export interface AjaxGetParams<T extends DataType = 'string'> {
  37. url: string,
  38. type?: T,
  39. title?: string,
  40. headers?: [string, string][],
  41. body?: string
  42. }
  43. export function readStringFromFile(file: File) {
  44. return readFromFileInternal(file, 'string');
  45. }
  46. export function readUint8ArrayFromFile(file: File) {
  47. return readFromFileInternal(file, 'binary');
  48. }
  49. export function readFromFile<T extends DataType>(file: File, type: T) {
  50. return readFromFileInternal(file, type);
  51. }
  52. export function ajaxGet(url: string): Task<DataValue>
  53. export function ajaxGet<T extends DataType>(params: AjaxGetParams<T>): Task<DataResponse<T>>
  54. export function ajaxGet<T extends DataType>(params: AjaxGetParams<T> | string) {
  55. if (typeof params === 'string') return ajaxGetInternal(params, params, 'string');
  56. return ajaxGetInternal(params.title, params.url, params.type || 'string', params.body, params.headers);
  57. }
  58. export type AjaxTask = typeof ajaxGet
  59. function isDone(data: XMLHttpRequest | FileReader) {
  60. if (data instanceof FileReader) {
  61. return data.readyState === FileReader.DONE;
  62. } else if (data instanceof XMLHttpRequest) {
  63. return data.readyState === XMLHttpRequest.DONE;
  64. }
  65. throw new Error('unknown data type');
  66. }
  67. function genericError(isDownload: boolean) {
  68. if (isDownload) return 'Failed to download data. Possible reasons: Resource is not available, or CORS is not allowed on the server.';
  69. return 'Failed to open file.';
  70. }
  71. function readData<T extends XMLHttpRequest | FileReader>(ctx: RuntimeContext, action: string, data: T): Promise<T> {
  72. return new Promise<T>((resolve, reject) => {
  73. // first check if data reading is already done
  74. if (isDone(data)) {
  75. const { error } = data as FileReader;
  76. if (error !== null && error !== undefined) {
  77. reject(error ?? genericError(data instanceof XMLHttpRequest));
  78. } else {
  79. resolve(data);
  80. }
  81. return;
  82. }
  83. let hasError = false;
  84. data.onerror = (e: ProgressEvent) => {
  85. if (hasError) return;
  86. const { error } = e.target as FileReader;
  87. reject(error ?? genericError(data instanceof XMLHttpRequest));
  88. };
  89. data.onprogress = (e: ProgressEvent) => {
  90. if (!ctx.shouldUpdate || hasError) return;
  91. try {
  92. if (e.lengthComputable) {
  93. ctx.update({ message: action, isIndeterminate: false, current: e.loaded, max: e.total });
  94. } else {
  95. ctx.update({ message: `${action} ${(e.loaded / 1024 / 1024).toFixed(2)} MB`, isIndeterminate: true });
  96. }
  97. } catch (e) {
  98. hasError = true;
  99. reject(e);
  100. }
  101. };
  102. data.onload = (e: ProgressEvent) => {
  103. resolve(data);
  104. };
  105. });
  106. }
  107. function getCompression(name: string) {
  108. return /\.gz$/i.test(name) ? DataCompressionMethod.Gzip :
  109. /\.zip$/i.test(name) ? DataCompressionMethod.Zip :
  110. DataCompressionMethod.None;
  111. }
  112. async function decompress(ctx: RuntimeContext, data: Uint8Array, compression: DataCompressionMethod): Promise<Uint8Array> {
  113. switch (compression) {
  114. case DataCompressionMethod.None: return data;
  115. case DataCompressionMethod.Gzip: return ungzip(ctx, data);
  116. case DataCompressionMethod.Zip:
  117. const parsed = await unzip(ctx, data.buffer);
  118. const names = Object.keys(parsed);
  119. if (names.length !== 1) throw new Error('can only decompress zip files with a single entry');
  120. return parsed[names[0]] as Uint8Array;
  121. }
  122. }
  123. async function processFile<T extends DataType>(ctx: RuntimeContext, reader: FileReader, type: T, compression: DataCompressionMethod): Promise<DataResponse<T>> {
  124. const { result } = reader;
  125. let data = result instanceof ArrayBuffer ? new Uint8Array(result) : result;
  126. if (data === null) throw new Error('no data given');
  127. if (compression !== DataCompressionMethod.None) {
  128. if (!(data instanceof Uint8Array)) throw new Error('need Uint8Array for decompression');
  129. const decompressed = await decompress(ctx, data, compression);
  130. if (type === 'string') {
  131. await ctx.update({ message: 'Decoding text...' });
  132. data = utf8Read(decompressed, 0, decompressed.length);
  133. } else {
  134. data = decompressed;
  135. }
  136. }
  137. if (type === 'binary' && data instanceof Uint8Array) {
  138. return data as DataResponse<T>;
  139. } else if (type === 'zip' && data instanceof Uint8Array) {
  140. return await unzip(ctx, data.buffer) as DataResponse<T>;
  141. } else if (type === 'string' && typeof data === 'string') {
  142. return data as DataResponse<T>;
  143. } else if (type === 'xml' && typeof data === 'string') {
  144. const parser = new DOMParser();
  145. return parser.parseFromString(data, 'application/xml') as DataResponse<T>;
  146. } else if (type === 'json' && typeof data === 'string') {
  147. return JSON.parse(data) as DataResponse<T>;
  148. }
  149. throw new Error(`could not get requested response data '${type}'`);
  150. }
  151. function readFromFileInternal<T extends DataType>(file: File, type: T): Task<DataResponse<T>> {
  152. let reader: FileReader | undefined = void 0;
  153. return Task.create('Read File', async ctx => {
  154. try {
  155. reader = new FileReader();
  156. // unzipping for type 'zip' handled explicitly in `processFile`
  157. const compression = type === 'zip' ? DataCompressionMethod.None : getCompression(file.name);
  158. if (type === 'binary' || type === 'zip' || compression !== DataCompressionMethod.None) {
  159. reader.readAsArrayBuffer(file);
  160. } else {
  161. reader.readAsText(file);
  162. }
  163. await ctx.update({ message: 'Opening file...', canAbort: true });
  164. const fileReader = await readData(ctx, 'Reading...', reader);
  165. await ctx.update({ message: 'Processing file...', canAbort: false });
  166. return await processFile(ctx, fileReader, type, compression);
  167. } finally {
  168. reader = void 0;
  169. }
  170. }, () => {
  171. if (reader) reader.abort();
  172. });
  173. }
  174. class RequestPool {
  175. private static pool: XMLHttpRequest[] = [];
  176. private static poolSize = 15;
  177. static get() {
  178. if (this.pool.length) {
  179. return this.pool.pop()!;
  180. }
  181. return new XHR();
  182. }
  183. static emptyFunc() { }
  184. static deposit(req: XMLHttpRequest) {
  185. if (this.pool.length < this.poolSize) {
  186. req.onabort = RequestPool.emptyFunc;
  187. req.onerror = RequestPool.emptyFunc;
  188. req.onload = RequestPool.emptyFunc;
  189. req.onprogress = RequestPool.emptyFunc;
  190. this.pool.push(req);
  191. }
  192. }
  193. }
  194. function processAjax<T extends DataType>(req: XMLHttpRequest, type: T): DataResponse<T> {
  195. if (req.status >= 200 && req.status < 400) {
  196. const { response } = req;
  197. RequestPool.deposit(req);
  198. if ((type === 'binary' || type === 'zip') && response instanceof ArrayBuffer) {
  199. return new Uint8Array(response) as DataResponse<T>;
  200. } else if (type === 'string' && typeof response === 'string') {
  201. return response as DataResponse<T>;
  202. } else if (type === 'xml' && response instanceof XMLDocument) {
  203. return response as DataResponse<T>;
  204. } else if (type === 'json' && typeof response === 'object') {
  205. return response as DataResponse<T>;
  206. }
  207. throw new Error(`could not get requested response data '${type}'`);
  208. } else {
  209. RequestPool.deposit(req);
  210. throw new Error(`Download failed with status code ${req.status}`);
  211. }
  212. }
  213. function getRequestResponseType(type: DataType): XMLHttpRequestResponseType {
  214. switch(type) {
  215. case 'json': return 'json';
  216. case 'xml': return 'document';
  217. case 'string': return 'text';
  218. case 'binary': return 'arraybuffer';
  219. case 'zip': return 'arraybuffer';
  220. }
  221. }
  222. function ajaxGetInternal<T extends DataType>(title: string | undefined, url: string, type: T, body?: string, headers?: [string, string][]): Task<DataResponse<T>> {
  223. let xhttp: XMLHttpRequest | undefined = void 0;
  224. return Task.create(title ? title : 'Download', async ctx => {
  225. xhttp = RequestPool.get();
  226. xhttp.open(body ? 'post' : 'get', url, true);
  227. if (headers) {
  228. for (const [name, value] of headers) {
  229. xhttp.setRequestHeader(name, value);
  230. }
  231. }
  232. xhttp.responseType = getRequestResponseType(type);
  233. xhttp.send(body);
  234. await ctx.update({ message: 'Waiting for server...', canAbort: true });
  235. const req = await readData(ctx, 'Downloading...', xhttp);
  236. xhttp = void 0; // guard against reuse, help garbage collector
  237. await ctx.update({ message: 'Parsing response...', canAbort: false });
  238. const result = processAjax(req, type);
  239. return result;
  240. }, () => {
  241. if (xhttp) {
  242. xhttp.abort();
  243. xhttp = void 0; // guard against reuse, help garbage collector
  244. }
  245. });
  246. }
  247. export type AjaxGetManyEntry = { kind: 'ok', id: string, result: Asset.Wrapper<'string' | 'binary'> } | { kind: 'error', id: string, error: any }
  248. export async function ajaxGetMany(ctx: RuntimeContext, assetManager: AssetManager, sources: { id: string, url: Asset.Url | string, isBinary?: boolean, canFail?: boolean }[], maxConcurrency: number) {
  249. const len = sources.length;
  250. const slots: AjaxGetManyEntry[] = new Array(sources.length);
  251. await ctx.update({ message: 'Downloading...', current: 0, max: len });
  252. let promises: Promise<AjaxGetManyEntry & { index: number }>[] = [], promiseKeys: number[] = [];
  253. let currentSrc = 0;
  254. for (let _i = Math.min(len, maxConcurrency); currentSrc < _i; currentSrc++) {
  255. const current = sources[currentSrc];
  256. promises.push(wrapPromise(currentSrc, current.id,
  257. assetManager.resolve(Asset.getUrlAsset(assetManager, current.url), current.isBinary ? 'binary' : 'string').runAsChild(ctx)));
  258. promiseKeys.push(currentSrc);
  259. }
  260. let done = 0;
  261. while (promises.length > 0) {
  262. const r = await Promise.race(promises);
  263. const src = sources[r.index];
  264. const idx = promiseKeys.indexOf(r.index);
  265. done++;
  266. if (r.kind === 'error' && !src.canFail) {
  267. // TODO: cancel other downloads
  268. throw new Error(`${src.url}: ${r.error}`);
  269. }
  270. if (ctx.shouldUpdate) {
  271. await ctx.update({ message: 'Downloading...', current: done, max: len });
  272. }
  273. slots[r.index] = r;
  274. promises = promises.filter(_filterRemoveIndex, idx);
  275. promiseKeys = promiseKeys.filter(_filterRemoveIndex, idx);
  276. if (currentSrc < len) {
  277. const current = sources[currentSrc];
  278. const asset = assetManager.resolve(Asset.getUrlAsset(assetManager, current.url), current.isBinary ? 'binary' : 'string').runAsChild(ctx);
  279. promises.push(wrapPromise(currentSrc, current.id, asset));
  280. promiseKeys.push(currentSrc);
  281. currentSrc++;
  282. }
  283. }
  284. return slots;
  285. }
  286. function _filterRemoveIndex(this: number, _: any, i: number) {
  287. return this !== i;
  288. }
  289. async function wrapPromise(index: number, id: string, p: Promise<Asset.Wrapper<'string' | 'binary'>>): Promise<AjaxGetManyEntry & { index: number }> {
  290. try {
  291. const result = await p;
  292. return { kind: 'ok', result, index, id };
  293. } catch (error) {
  294. return { kind: 'error', error, index, id };
  295. }
  296. }