cache web assets between CI runs (#2089)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 14 Jun 2017 03:53:10 +0000 (11:53 +0800)
committerGitHub <noreply@github.com>
Wed, 14 Jun 2017 03:53:10 +0000 (11:53 +0800)
- skip `test/jetstream.js` for `node@0.12`

.travis.yml
test/benchmark.js
test/fetch.js [new file with mode: 0644]
test/jetstream.js
test/mocha/release.js

index 968addf..e199df2 100644 (file)
@@ -10,3 +10,5 @@ env:
 matrix:
   fast_finish: true
 sudo: false
+cache:
+  directories: tmp
index e27ed2c..e34f085 100644 (file)
@@ -4,6 +4,7 @@
 "use strict";
 
 var createHash = require("crypto").createHash;
+var fetch = require("./fetch");
 var fork = require("child_process").fork;
 var args = process.argv.slice(2);
 if (!args.length) {
@@ -52,7 +53,8 @@ urls.forEach(function(url) {
         output: 0,
         log: ""
     };
-    require(url.slice(0, url.indexOf(":"))).get(url, function(res) {
+    fetch(url, function(err, res) {
+        if (err) throw err;
         var uglifyjs = fork("bin/uglifyjs", args, { silent: true });
         res.on("data", function(data) {
             results[url].input += data.length;
diff --git a/test/fetch.js b/test/fetch.js
new file mode 100644 (file)
index 0000000..5ca95bb
--- /dev/null
@@ -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);
+    });
+};
index a1b041a..1cc6d4a 100644 (file)
@@ -23,13 +23,19 @@ if (typeof phantom == "undefined") {
     }
     args.push("--timings");
     var child_process = require("child_process");
+    var fetch = require("./fetch");
     var http = require("http");
     var server = http.createServer(function(request, response) {
         request.resume();
         var url = site + request.url;
-        http.get(url, function(res) {
-            response.writeHead(res.statusCode, {
-                "Content-Type": res.headers["content-type"]
+        fetch(url, function(err, res) {
+            if (err) throw err;
+            response.writeHead(200, {
+                "Content-Type": {
+                    css: "text/css",
+                    js: "application/javascript",
+                    png: "image/png"
+                }[url.slice(url.lastIndexOf(".") + 1)] || "text/html; charset=utf-8"
             });
             if (/\.js$/.test(url)) {
                 var stderr = "";
index bd1154a..1bf6e87 100644 (file)
@@ -1,4 +1,5 @@
 var assert = require("assert");
+var semver = require("semver");
 var spawn = require("child_process").spawn;
 
 if (!process.env.UGLIFYJS_TEST_ALL) return;
@@ -32,6 +33,7 @@ describe("test/benchmark.js", function() {
     });
 });
 
+if (semver.satisfies(process.version, "0.12")) return;
 describe("test/jetstream.js", function() {
     this.timeout(20 * 60 * 1000);
     [