Skip to content

Commit 9bb5312

Browse files
#15 added http data loading to file utils
1 parent 36a6fc2 commit 9bb5312

File tree

1 file changed

+10
-38
lines changed

1 file changed

+10
-38
lines changed

src/utils/file.utils.ts

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as util from 'util';
3-
import * as request from 'superagent';
3+
import * as superagent from 'superagent';
44
import * as path from 'path';
55
import * as config from '../config';
66
import {Logger, LogLevel} from '../logger';
@@ -24,10 +24,7 @@ export async function readDataFile(dataFilePath: string, encoding:string = null)
2424
window.showErrorMessage(`${dataFilePath} is not a supported data file for Data Preview!`);
2525
}
2626
else if (dataFilePath.startsWith('http://') || dataFilePath.startsWith('https://')) {
27-
window.showInformationMessage('Remote data loading coming soon!');
28-
// TODO: finish this part with remote data read async
29-
data = readRemoteData(dataFilePath, encoding);
30-
// logger.debug('readDataFile(): data:\n', data);
27+
data = await readRemoteData(dataFilePath, encoding);
3128
}
3229
else if (fs.existsSync(fileUri.fsPath)) {
3330
// read local data file via fs.readFile() api
@@ -117,39 +114,14 @@ async function readLocalData(dataFilePath: string, encoding: string = null): Pro
117114
* TODO: change this to read data async later
118115
* TODO: rework this to using streaming api for large data files support later
119116
*/
120-
function readRemoteData(dataUrl: string, encoding: string = null): any {
121-
let data: any = '';
117+
async function readRemoteData(dataUrl: string, encoding: string = null): Promise<string | Buffer> {
122118
logger.debug('readRemoteData(): url:', dataUrl);
123-
spawn(function *() {
124-
try {
125-
const response: any = yield Promise.resolve(request.get(dataUrl));
126-
data = response.text;
127-
// logger.debug('readRemoteData(): data:\n', data);
128-
}
129-
catch (error) {
130-
data = '';
131-
console.error(error);
132-
}
133-
});
134-
return data;
135-
}
136-
137-
function spawn(generatorFunc) {
138-
function continuer(verb, arg) {
139-
var result;
140-
try {
141-
result = generator[verb](arg);
142-
} catch (err) {
143-
return Promise.reject(err);
144-
}
145-
if (result.done) {
146-
return result.value;
147-
} else {
148-
return Promise.resolve(result.value).then(onFulfilled, onRejected);
149-
}
119+
if (encoding) { // must be text request
120+
return await superagent.get(dataUrl).then(response => response.text);
121+
}
122+
else { // binary data request
123+
return await superagent.get(dataUrl)
124+
.buffer(true).parse(superagent.parse.image)
125+
.then(response => response.body);
150126
}
151-
var generator = generatorFunc();
152-
var onFulfilled = continuer.bind(continuer, "next");
153-
var onRejected = continuer.bind(continuer, "throw");
154-
return onFulfilled();
155127
}

0 commit comments

Comments
 (0)