aboutsummaryrefslogtreecommitdiff
path: root/test/fetch.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-06-14 11:53:10 +0800
committerGitHub <noreply@github.com>2017-06-14 11:53:10 +0800
commit41beae4dd71920d2173bb2bc1efeb1231373c2a8 (patch)
tree645bc76c29b560bb113af5a4ea66f085dcf3a3d4 /test/fetch.js
parent82db9188ac2f0a6ffa4c7ab4c7d4c0ade3d93de9 (diff)
downloadtracifyjs-41beae4dd71920d2173bb2bc1efeb1231373c2a8.tar.gz
tracifyjs-41beae4dd71920d2173bb2bc1efeb1231373c2a8.zip
cache web assets between CI runs (#2089)
- skip `test/jetstream.js` for `node@0.12`
Diffstat (limited to 'test/fetch.js')
-rw-r--r--test/fetch.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/test/fetch.js b/test/fetch.js
new file mode 100644
index 00000000..5ca95bbb
--- /dev/null
+++ b/test/fetch.js
@@ -0,0 +1,31 @@
+var fs = require("fs");
+var path = require("path");
+
+try {
+ fs.mkdirSync("./tmp");
+} catch (e) {
+ if (e.code != "EEXIST") throw e;
+}
+
+function local(url) {
+ return path.join("./tmp", encodeURIComponent(url));
+}
+
+function read(url) {
+ return fs.createReadStream(local(url));
+}
+
+module.exports = function(url, callback) {
+ var result = read(url);
+ result.on("error", function(e) {
+ if (e.code != "ENOENT") return callback(e);
+ require(url.slice(0, url.indexOf(":"))).get(url, function(res) {
+ if (res.statusCode !== 200) return callback(res);
+ res.pipe(fs.createWriteStream(local(url)).on("close", function() {
+ callback(null, read(url));
+ }));
+ });
+ }).on("open", function() {
+ callback(null, result);
+ });
+};