From 41beae4dd71920d2173bb2bc1efeb1231373c2a8 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 14 Jun 2017 11:53:10 +0800 Subject: cache web assets between CI runs (#2089) - skip `test/jetstream.js` for `node@0.12` --- test/fetch.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 test/fetch.js (limited to 'test/fetch.js') 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); + }); +}; -- cgit v1.2.3