-
-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to fetch for url requests (#1165)
* Change load from url request to use fetch * Remove phin dependency * Fix linting for using after in tests * Fix http server being created for browser tests in core request
- Loading branch information
1 parent
22f2535
commit b3b6438
Showing
6 changed files
with
57 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ module.exports = { | |
it: true, | ||
describe: true, | ||
before: true, | ||
after: true, | ||
test: true, | ||
}, | ||
rules: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,18 @@ | ||
/* global XMLHttpRequest */ | ||
import "isomorphic-fetch"; | ||
|
||
if ( | ||
process.browser || | ||
process.env.ENVIRONMENT === "BROWSER" || | ||
(typeof process.versions.electron !== "undefined" && | ||
process.type === "renderer" && | ||
typeof XMLHttpRequest === "function") | ||
) { | ||
// If we run into a browser or the electron renderer process, | ||
// use XHR method instead of Request node module. | ||
|
||
module.exports = function (options, cb) { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open("GET", options.url, true); | ||
xhr.responseType = "arraybuffer"; | ||
xhr.addEventListener("load", function () { | ||
if (xhr.status < 400) { | ||
try { | ||
const data = Buffer.from(this.response); | ||
cb(null, xhr, data); | ||
} catch (error) { | ||
return cb( | ||
new Error( | ||
"Response is not a buffer for url " + | ||
options.url + | ||
". Error: " + | ||
error.message | ||
) | ||
export default ({ url, ...options }, cb) => { | ||
fetch(url, options) | ||
.then((response) => { | ||
if (response.ok) { | ||
return response.arrayBuffer().catch((error) => { | ||
throw new Error( | ||
`Response is not a buffer for url ${url}. Error: ${error.message}` | ||
); | ||
} | ||
} else { | ||
cb(new Error("HTTP Status " + xhr.status + " for url " + options.url)); | ||
}); | ||
} | ||
}); | ||
xhr.addEventListener("error", (e) => { | ||
cb(e); | ||
}); | ||
xhr.send(); | ||
}; | ||
} else { | ||
module.exports = function ({ ...options }, cb) { | ||
const p = require("phin"); | ||
const allOptions = { compression: true, ...options }; | ||
|
||
try { | ||
p(allOptions, (err, res) => { | ||
if (err) { | ||
cb(err); | ||
} else { | ||
cb(null, res, res.body); | ||
} | ||
}); | ||
} catch (error) { | ||
cb(error); | ||
} | ||
}; | ||
} | ||
throw new Error(`HTTP Status ${response.status} for url ${url}`); | ||
}) | ||
.then((data) => cb(null, data)) | ||
.catch((error) => cb(error)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters