data-source.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. const reFilterPath = /^(__MACOSX|.DS_Store)/;
  113. async function decompress(ctx: RuntimeContext, data: Uint8Array, compression: DataCompressionMethod): Promise<Uint8Array> {
  114. switch (compression) {
  115. case DataCompressionMethod.None: return data;
  116. case DataCompressionMethod.Gzip: return ungzip(ctx, data);
  117. case DataCompressionMethod.Zip:
  118. const parsed = await unzip(ctx, data.buffer);
  119. const names = Object.keys(parsed).filter(n => !reFilterPath.test(n));
  120. if (names.length !== 1) throw new Error('can only decompress zip files with a single entry');
  121. return parsed[names[0]] as Uint8Array;
  122. }
  123. }
  124. async function processFile<T extends DataType>(ctx: RuntimeContext, reader: FileReader, type: T, compression: DataCompressionMethod): Promise<DataResponse<T>> {
  125. const { result } = reader;
  126. let data = result instanceof ArrayBuffer ? new Uint8Array(result) : result;
  127. if (data === null) throw new Error('no data given');
  128. if (compression !== DataCompressionMethod.None) {
  129. if (!(data instanceof Uint8Array)) throw new Error('need Uint8Array for decompression');
  130. const decompressed = await decompress(ctx, data, compression);
  131. if (type === 'string') {
  132. await ctx.update({ message: 'Decoding text...' });
  133. data = utf8Read(decompressed, 0, decompressed.length);
  134. } else {
  135. data = decompressed;
  136. }
  137. }
  138. if (type === 'binary' && data instanceof Uint8Array) {
  139. return data as DataResponse<T>;
  140. } else if (type === 'zip' && data instanceof Uint8Array) {
  141. return await unzip(ctx, data.buffer) as DataResponse<T>;
  142. } else if (type === 'string' && typeof data === 'string') {
  143. return data as DataResponse<T>;
  144. } else if (type === 'xml' && typeof data === 'string') {
  145. const parser = new DOMParser();
  146. return parser.parseFromString(data, 'application/xml') as DataResponse<T>;
  147. } else if (type === 'json' && typeof data === 'string') {
  148. return JSON.parse(data) as DataResponse<T>;
  149. }
  150. throw new Error(`could not get requested response data '${type}'`);
  151. }
  152. function readFromFileInternal<T extends DataType>(file: File, type: T): Task<DataResponse<T>> {
  153. let reader: FileReader | undefined = void 0;
  154. return Task.create('Read File', async ctx => {
  155. try {
  156. reader = new FileReader();
  157. // unzipping for type 'zip' handled explicitly in `processFile`
  158. const compression = type === 'zip' ? DataCompressionMethod.None : getCompression(file.name);
  159. if (type === 'binary' || type === 'zip' || compression !== DataCompressionMethod.None) {
  160. reader.readAsArrayBuffer(file);
  161. } else {
  162. reader.readAsText(file);
  163. }
  164. await ctx.update({ message: 'Opening file...', canAbort: true });
  165. const fileReader = await readData(ctx, 'Reading...', reader);
  166. await ctx.update({ message: 'Processing file...', canAbort: false });
  167. return await processFile(ctx, fileReader, type, compression);
  168. } finally {
  169. reader = void 0;
  170. }
  171. }, () => {
  172. if (reader) reader.abort();
  173. });
  174. }
  175. class RequestPool {
  176. private static pool: XMLHttpRequest[] = [];
  177. private static poolSize = 15;
  178. static get() {
  179. if (this.pool.length) {
  180. return this.pool.pop()!;
  181. }
  182. return new XHR();
  183. }
  184. static emptyFunc() { }
  185. static deposit(req: XMLHttpRequest) {
  186. if (this.pool.length < this.poolSize) {
  187. req.onabort = RequestPool.emptyFunc;
  188. req.onerror = RequestPool.emptyFunc;
  189. req.onload = RequestPool.emptyFunc;
  190. req.onprogress = RequestPool.emptyFunc;
  191. this.pool.push(req);
  192. }
  193. }
  194. }
  195. function processAjax<T extends DataType>(req: XMLHttpRequest, type: T): DataResponse<T> {
  196. if (req.status >= 200 && req.status < 400) {
  197. const { response } = req;
  198. RequestPool.deposit(req);
  199. if ((type === 'binary' || type === 'zip') && response instanceof ArrayBuffer) {
  200. return new Uint8Array(response) as DataResponse<T>;
  201. } else if (type === 'string' && typeof response === 'string') {
  202. return response as DataResponse<T>;
  203. } else if (type === 'xml' && response instanceof XMLDocument) {
  204. return response as DataResponse<T>;
  205. } else if (type === 'json' && typeof response === 'object') {
  206. return response as DataResponse<T>;
  207. }
  208. throw new Error(`could not get requested response data '${type}'`);
  209. } else {
  210. RequestPool.deposit(req);
  211. throw new Error(`Download failed with status code ${req.status}`);
  212. }
  213. }
  214. function getRequestResponseType(type: DataType): XMLHttpRequestResponseType {
  215. switch (type) {
  216. case 'json': return 'json';
  217. case 'xml': return 'document';
  218. case 'string': return 'text';
  219. case 'binary': return 'arraybuffer';
  220. case 'zip': return 'arraybuffer';
  221. }
  222. }
  223. function ajaxGetInternal<T extends DataType>(title: string | undefined, url: string, type: T, body?: string, headers?: [string, string][]): Task<DataResponse<T>> {
  224. let xhttp: XMLHttpRequest | undefined = void 0;
  225. return Task.create(title ? title : 'Download', async ctx => {
  226. xhttp = RequestPool.get();
  227. xhttp.open(body ? 'post' : 'get', url, true);
  228. if (headers) {
  229. for (const [name, value] of headers) {
  230. xhttp.setRequestHeader(name, value);
  231. }
  232. }
  233. xhttp.responseType = getRequestResponseType(type);
  234. xhttp.send(body);
  235. await ctx.update({ message: 'Waiting for server...', canAbort: true });
  236. const req = await readData(ctx, 'Downloading...', xhttp);
  237. xhttp = void 0; // guard against reuse, help garbage collector
  238. await ctx.update({ message: 'Parsing response...', canAbort: false });
  239. const result = processAjax(req, type);
  240. return result;
  241. }, () => {
  242. if (xhttp) {
  243. xhttp.abort();
  244. xhttp = void 0; // guard against reuse, help garbage collector
  245. }
  246. });
  247. }
  248. export type AjaxGetManyEntry = { kind: 'ok', id: string, result: Asset.Wrapper<'string' | 'binary'> } | { kind: 'error', id: string, error: any }
  249. export async function ajaxGetMany(ctx: RuntimeContext, assetManager: AssetManager, sources: { id: string, url: Asset.Url | string, isBinary?: boolean, canFail?: boolean }[], maxConcurrency: number) {
  250. const len = sources.length;
  251. const slots: AjaxGetManyEntry[] = new Array(sources.length);
  252. await ctx.update({ message: 'Downloading...', current: 0, max: len });
  253. let promises: Promise<AjaxGetManyEntry & { index: number }>[] = [], promiseKeys: number[] = [];
  254. let currentSrc = 0;
  255. for (let _i = Math.min(len, maxConcurrency); currentSrc < _i; currentSrc++) {
  256. const current = sources[currentSrc];
  257. promises.push(wrapPromise(currentSrc, current.id,
  258. assetManager.resolve(Asset.getUrlAsset(assetManager, current.url), current.isBinary ? 'binary' : 'string').runAsChild(ctx)));
  259. promiseKeys.push(currentSrc);
  260. }
  261. let done = 0;
  262. while (promises.length > 0) {
  263. const r = await Promise.race(promises);
  264. const src = sources[r.index];
  265. const idx = promiseKeys.indexOf(r.index);
  266. done++;
  267. if (r.kind === 'error' && !src.canFail) {
  268. // TODO: cancel other downloads
  269. throw new Error(`${src.url}: ${r.error}`);
  270. }
  271. if (ctx.shouldUpdate) {
  272. await ctx.update({ message: 'Downloading...', current: done, max: len });
  273. }
  274. slots[r.index] = r;
  275. promises = promises.filter(_filterRemoveIndex, idx);
  276. promiseKeys = promiseKeys.filter(_filterRemoveIndex, idx);
  277. if (currentSrc < len) {
  278. const current = sources[currentSrc];
  279. const asset = assetManager.resolve(Asset.getUrlAsset(assetManager, current.url), current.isBinary ? 'binary' : 'string').runAsChild(ctx);
  280. promises.push(wrapPromise(currentSrc, current.id, asset));
  281. promiseKeys.push(currentSrc);
  282. currentSrc++;
  283. }
  284. }
  285. return slots;
  286. }
  287. function _filterRemoveIndex(this: number, _: any, i: number) {
  288. return this !== i;
  289. }
  290. async function wrapPromise(index: number, id: string, p: Promise<Asset.Wrapper<'string' | 'binary'>>): Promise<AjaxGetManyEntry & { index: number }> {
  291. try {
  292. const result = await p;
  293. return { kind: 'ok', result, index, id };
  294. } catch (error) {
  295. return { kind: 'error', error, index, id };
  296. }
  297. }