diff --git a/src/15utility.js b/src/15utility.js index 843d49ea2..ef61d7ec2 100755 --- a/src/15utility.js +++ b/src/15utility.js @@ -442,6 +442,19 @@ async function fetchData(path, success, error, async) { } function getData(path, success, error) { + return _fetch(path) + .then(response => response.text()) + .then(txt => { + success(txt); + }) + .catch(e => { + if (error) return error(e); + console.error(e); + throw e; + }); +} + +function getBinaryData(path, success, error) { return _fetch(path) .then(response => response.arrayBuffer()) .then(buf => { @@ -481,7 +494,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function ( fs = require('fs'); if (/^[a-z]+:\/\//i.test(path)) { - fetchData(path, success, error, runAsync); + return getBinaryData(path, success, error); } else { if (runAsync) { fs.readFile(path, function (err, data) { diff --git a/test/test157.js b/test/test157.js index 3b827d8b3..488faff97 100644 --- a/test/test157.js +++ b/test/test157.js @@ -10,7 +10,7 @@ describe('Test 157 - json()', function () { it('1. Load text data from file async', function (done) { alasql('select * from json("' + __dirname + '/test157.json")', [], function (res) { // console.log(13,res); - assert.deepEqual(res, [{a: 1}, {a: 2}]); + assert.deepEqual(res, [{a: 1}, {a: 2}, {c: '😂'}]); done(); }); }); diff --git a/test/test157.json b/test/test157.json index 964de21c0..2a4feb25a 100644 --- a/test/test157.json +++ b/test/test157.json @@ -1 +1 @@ -[{"a": 1}, {"a": 2}] +[{"a": 1}, {"a": 2}, {"c": "😂"}] diff --git a/test/test2112.dat b/test/test2112.dat new file mode 100644 index 000000000..f488f8491 --- /dev/null +++ b/test/test2112.dat @@ -0,0 +1 @@ +� \ No newline at end of file diff --git a/test/test2112.js b/test/test2112.js new file mode 100644 index 000000000..98c9f2cae --- /dev/null +++ b/test/test2112.js @@ -0,0 +1,28 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +describe('Test 2112 - load binary file', function () { + const test = '2112'; // insert test file number + + it('A) Loads binary file (sync)', function () { + alasql.utils.loadBinaryFile('./test/test' + test + '.dat', false, function (data) { + assert.equal(data, '�'); + }); + }); + + it('B) Loads binary file (async)', function (done) { + alasql.utils.loadBinaryFile('./test/test' + test + '.dat', true, function (data) { + assert.equal(data, '�'); + done(); + }); + }); + + it('C) Loads HTTPS binary file (async)', function (done) { + alasql.utils.loadBinaryFile('https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg', true, function (data) { + assert.equal(data.slice(0, 3), 'ÿØÿ'); + done(); + }); + }); +});