Skip to content

Commit

Permalink
Remove XMLHttpRequest to fix #1596
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Dec 29, 2022
1 parent f3ca60c commit 5c7375b
Show file tree
Hide file tree
Showing 9 changed files with 6,756 additions and 6,713 deletions.
24 changes: 14 additions & 10 deletions .github/CONTRIBUTING.md
Expand Up @@ -9,17 +9,21 @@ Inputs to improvement? [Open an issue](https://github.com/agershun/alasql/issues

**All contributions are much welcome and greatly appreciated(!)**

- Fork the repo here on Github
- Clone your forked repo and install dependencies `git clone https://github.com/MYUSERNAME/alasql/ && cd alasql && npm install`
- Please work with the code from the develop branch `git checkout develop`
- Add a test for the issue: Copy `test/test000.js` and replace `000` with a new number.
- Impelement a test that reflects the issue.
- Run `npm test` to verify only the new test fails
- Make sure you have git, Node and yarn installed (`npm install -g yarn`)
- Fork the repo here on Github (button top right)
- Clone your forked repo and install dependencies `git clone https://github.com/MYUSERNAME/alasql/ --depth 1 && cd alasql && yarn`
- Make sure you work with the develop branch `git checkout develop`
- Run tests to verify all is good `yarn test`
- Implement a test that reflects the issue.
- Add a new test file for the issue: Copy `test/test000.js` and replace `000` with a new number. Preferably the number of the issue you are solving.
- Run `yarn test` to verify only the new test fails
- Implement your contributions in `src/`
- Run `npm test` and verify all tests are OK
- Run `yarn test` and verify all tests are OK
- Format the souce with `yarn format`
- Commit changes to git and push to your forked repo
- Click "Create Pull-request" when looking at your forked repo on Github

_Please note that `npm test` will compile from `src/` before running tests_


Please note that
- `npm test` will compile from `src/` and overwrite `dist/` before running all tests
- If you would like to change the alasql.org website please make a PR to https://github.com/agershun/alasql-org
- To help debug a problem you can see some advice on https://github.com/AlaSQL/alasql/issues/1415#issuecomment-1293335079
@@ -1,4 +1,4 @@
name: "Build and test node & browser"
name: "CICD pipeline"

on:
push:
Expand Down Expand Up @@ -27,7 +27,7 @@ jobs:
name: node_modules
path: node_modules.tar

Test-node:
"Test Node":
needs: Build
runs-on: ubuntu-latest
strategy:
Expand All @@ -50,7 +50,7 @@ jobs:
- run: tar -xvf node_modules.tar
- run: yarn test-only

Test-browser:
"Test browser":
needs: Build
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 0 additions & 2 deletions .jshintrc
Expand Up @@ -11,7 +11,6 @@
"wsh" : true, // Windows Scripting Host.
"jquery" : true,
"mocha" : true,
"node" : true,
"worker" : true,

"predef" : [ // Custom globals.
Expand Down Expand Up @@ -68,7 +67,6 @@
"curly" : true, // Require {} for every new block or scope.
"eqeqeq" : true, // Require triple equals i.e. `===`.
"eqnull" : false, // Tolerate use of `== null`.
"evil" : true, // Tolerate use of `eval`.
"expr" : false, // Tolerate `ExpressionStatement` as Programs.
"forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`.
"immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -46,6 +46,7 @@
"postversion": "npm publish && git push && git push --tags && echo \"Successfully released version $npm_package_version\""
},
"dependencies": {
"cross-fetch": "^3.1.5",
"dom-storage": "2.1.0",
"es6-promise": "4.2.8",
"node-fetch": "2",
Expand Down
71 changes: 30 additions & 41 deletions src/15utility.js
Expand Up @@ -319,19 +319,7 @@ var loadFile = (utils.loadFile = function (path, asy, success, error) {
});
} else {
if (/^[a-z]+:\/\//i.test(path)) {
var fetch = require('node-fetch');
fetch(path)
.then((response) => response.arrayBuffer())
.then((buf) => {
var a = new Uint8Array(buf);
var b = [...a].map((e) => String.fromCharCode(e)).join('');
success(cutbom(b));
})
.catch((e) => {
if (error) return error(e);
console.error(e);
throw e;
});
fetchData(path, (x) => success(cutbom(x)), error, asy);
} else {
//If async callthen call async
if (asy) {
Expand Down Expand Up @@ -416,21 +404,7 @@ var loadFile = (utils.loadFile = function (path, asy, success, error) {
Simply read file from HTTP request, like:
SELECT * FROM TXT('http://alasql.org/README.md');
*/
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
if (success) {
success(cutbom(xhr.responseText));
}
} else if (error) {
return error(xhr);
}
// Todo: else...?
}
};
xhr.open('GET', path, asy); // Async
xhr.send();
fetchData(path, (x) => success(cutbom(x)), error, asy);
}
} else if (path instanceof Event) {
/*
Expand All @@ -457,6 +431,33 @@ var loadFile = (utils.loadFile = function (path, asy, success, error) {
}
});

let _fetch = typeof fetch !== 'undefined' ? fetch : null;
//*not-for-browser/*
_fetch = typeof fetch !== 'undefined' ? fetch : require('cross-fetch');
//*/

async function fetchData(path, success, error, async) {
if (async) {
return getData(path, success, error);
}
return await getData(path, success, error);
}

function getData(path, success, error) {
return _fetch(path)
.then((response) => response.arrayBuffer())
.then((buf) => {
var a = new Uint8Array(buf);
var b = [...a].map((e) => String.fromCharCode(e)).join('');
success(b);
})
.catch((e) => {
if (error) return error(e);
console.error(e);
throw e;
});
}

/**
@function Load binary file from anywhere
@param {string} path File path
Expand All @@ -482,19 +483,7 @@ var loadBinaryFile = (utils.loadBinaryFile = function (
fs = require('fs');

if (/^[a-z]+:\/\//i.test(path)) {
var fetch = require('node-fetch');
fetch(path)
.then((response) => response.arrayBuffer())
.then((buf) => {
var a = new Uint8Array(buf);
var b = [...a].map((e) => String.fromCharCode(e)).join('');
success(b);
})
.catch((e) => {
if (error) return error(e);
console.error(e);
throw e;
});
fetchData(path, success, error, runAsync);
} else {
if (runAsync) {
fs.readFile(path, function (err, data) {
Expand Down

0 comments on commit 5c7375b

Please sign in to comment.