Skip to content
Snippets Groups Projects
Commit 445977d9 authored by dsehnal's avatar dsehnal
Browse files

Fix data-source node workaround

parent a748b158
No related branches found
No related tags found
No related merge requests found
...@@ -11,16 +11,10 @@ ...@@ -11,16 +11,10 @@
import { utf8Read } from '../mol-io/common/utf8'; import { utf8Read } from '../mol-io/common/utf8';
import { RuntimeContext, Task } from '../mol-task'; import { RuntimeContext, Task } from '../mol-task';
import { Asset, AssetManager } from './assets'; import { Asset, AssetManager } from './assets';
import { LazyImports } from './lazy-imports';
import { File_ as File, RUNNING_IN_NODEJS, XMLHttpRequest_ as XMLHttpRequest } from './nodejs-shims'; import { File_ as File, RUNNING_IN_NODEJS, XMLHttpRequest_ as XMLHttpRequest } from './nodejs-shims';
import { ungzip, unzip } from './zip/zip'; import { ungzip, unzip } from './zip/zip';
const lazyImports = LazyImports.create('fs') as {
'fs': typeof import ('fs'),
};
export enum DataCompressionMethod { export enum DataCompressionMethod {
None, None,
Gzip, Gzip,
...@@ -306,12 +300,21 @@ function ajaxGetInternal<T extends DataType>(title: string | undefined, url: str ...@@ -306,12 +300,21 @@ function ajaxGetInternal<T extends DataType>(title: string | undefined, url: str
}); });
} }
// NOTE: lazy imports cannot be used here because WebPack complains since this
// is part of the "browser" build.
let _fs: (typeof import ('fs')) | undefined = undefined;
function getFS() {
if (_fs) return _fs!;
_fs = require('fs');
return _fs!;
}
/** Alternative implementation of ajaxGetInternal (because xhr2 does not support file:// protocol) */ /** Alternative implementation of ajaxGetInternal (because xhr2 does not support file:// protocol) */
function ajaxGetInternal_file_NodeJS<T extends DataType>(title: string | undefined, url: string, type: T, body?: string, headers?: [string, string][]): Task<DataResponse<T>> { function ajaxGetInternal_file_NodeJS<T extends DataType>(title: string | undefined, url: string, type: T, body?: string, headers?: [string, string][]): Task<DataResponse<T>> {
if (!RUNNING_IN_NODEJS) throw new Error('This function should only be used when running in Node.js'); if (!RUNNING_IN_NODEJS) throw new Error('This function should only be used when running in Node.js');
if (!url.startsWith('file://')) throw new Error('This function is only for URLs with protocol file://'); if (!url.startsWith('file://')) throw new Error('This function is only for URLs with protocol file://');
const filename = url.substring('file://'.length); const filename = url.substring('file://'.length);
const data = lazyImports.fs.readFileSync(filename); const data = getFS().readFileSync(filename);
const file = new File([data], 'raw-data'); const file = new File([data], 'raw-data');
return readFromFile(file, type); return readFromFile(file, type);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment