Skip to content

Commit 25f0302

Browse files
committed
fix: utf8 issue
1 parent 0d5c981 commit 25f0302

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

src/15utility.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,19 @@ async function fetchData(path, success, error, async) {
442442
}
443443

444444
function getData(path, success, error) {
445+
return _fetch(path)
446+
.then(response => response.text())
447+
.then(txt => {
448+
success(txt);
449+
})
450+
.catch(e => {
451+
if (error) return error(e);
452+
console.error(e);
453+
throw e;
454+
});
455+
}
456+
457+
function getBinaryData(path, success, error) {
445458
return _fetch(path)
446459
.then(response => response.arrayBuffer())
447460
.then(buf => {
@@ -481,7 +494,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function (
481494
fs = require('fs');
482495

483496
if (/^[a-z]+:\/\//i.test(path)) {
484-
fetchData(path, success, error, runAsync);
497+
return getBinaryData(path, success, error);
485498
} else {
486499
if (runAsync) {
487500
fs.readFile(path, function (err, data) {

test/test157.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Test 157 - json()', function () {
1010
it('1. Load text data from file async', function (done) {
1111
alasql('select * from json("' + __dirname + '/test157.json")', [], function (res) {
1212
// console.log(13,res);
13-
assert.deepEqual(res, [{a: 1}, {a: 2}]);
13+
assert.deepEqual(res, [{a: 1}, {a: 2}, {c: '😂'}]);
1414
done();
1515
});
1616
});

test/test157.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"a": 1}, {"a": 2}]
1+
[{"a": 1}, {"a": 2}, {"c": "😂"}]

test/test2112.dat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/test2112.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 2112 - load binary file', function () {
7+
const test = '2112'; // insert test file number
8+
9+
it('A) Loads binary file (sync)', function () {
10+
alasql.utils.loadBinaryFile('./test/test' + test + '.dat', false, function (data) {
11+
assert.equal(data, '�');
12+
});
13+
});
14+
15+
it('B) Loads binary file (async)', function (done) {
16+
alasql.utils.loadBinaryFile('./test/test' + test + '.dat', true, function (data) {
17+
assert.equal(data, '�');
18+
done();
19+
});
20+
});
21+
22+
it('C) Loads HTTPS binary file (async)', function (done) {
23+
alasql.utils.loadBinaryFile('https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg', true, function (data) {
24+
assert.equal(data.slice(0, 3), 'ÿØÿ');
25+
done();
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)