Skip to content

Commit

Permalink
fix(forex): reduce distribution size
Browse files Browse the repository at this point in the history
For the browser, use XMLHttpRequest, instead of node https shim, to get JSON data.  Reduces the minified version by 80 KB.
  • Loading branch information
richardschneider committed Nov 20, 2016
1 parent 91ecc71 commit d6a6c46
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions browser/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"esversion": 6,
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"unused": true,
"boss": true,
"eqnull": true,
"browser": true,
"predef": ['Money', 'XMLHttpRequest']
}
21 changes: 21 additions & 0 deletions browser/secure-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

function getSecureJSON(url) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function () {
if (this.status < 200 || this.status > 299) {
reject(new Error('Failed to get data: ' + this.status));
} else {
resolve(JSON.parse(xhr.response));
}
};
xhr.onerror = function () {
reject(new Error(xhr.statusText));
};
xhr.send();
});
}

module.exports = getSecureJSON;
2 changes: 2 additions & 0 deletions lib/secure-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Thanks Tomas Dvorak for the initial idea (https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/)

// NOTE: the dist copy (for the browser) uses '../browser/secure-json.js' not this file!!!

'use strict';

const https = require('https');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"main": "index.js",
"browser": {
"index.js": "browser.js",
"./lib/secure-json.js": "./browser/secure-json.js",
"intl": false
},
"browserify": {
Expand Down

0 comments on commit d6a6c46

Please sign in to comment.