Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/15utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/test157.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/test157.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"a": 1}, {"a": 2}]
[{"a": 1}, {"a": 2}, {"c": "😂"}]
1 change: 1 addition & 0 deletions test/test2112.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
28 changes: 28 additions & 0 deletions test/test2112.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
Loading