From 7f2660554e306e05856908658200d320754a7bae Mon Sep 17 00:00:00 2001 From: Matchu Date: Mon, 21 Sep 2020 07:07:47 -0700 Subject: [PATCH] oops, assetProxy more things for animate-test.html --- api/assetProxy.js | 18 +++++++++++++----- public/animate-test.html | 7 ++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/api/assetProxy.js b/api/assetProxy.js index 27002c1..96aa788 100644 --- a/api/assetProxy.js +++ b/api/assetProxy.js @@ -6,7 +6,7 @@ const streamPipeline = util.promisify(stream.pipeline); const VALID_URL_PATTERNS = [ /^http:\/\/images\.neopets\.com\/items\/[a-zA-Z0-9_ -]+\.gif$/, - /^http:\/\/images\.neopets\.com\/cp\/(bio|items)\/data\/[0-9]{3}\/[0-9]{3}\/[0-9]{3}\/[a-f0-9_]+\/[a-zA-Z0-9_\/]+\.(svg|png)$/, + /^http:\/\/images\.neopets\.com\/cp\/(bio|items)\/data\/[0-9]{3}\/[0-9]{3}\/[0-9]{3}\/[a-f0-9_]+\/[a-zA-Z0-9_\-\/]+\.(svg|png|js)$/, /^http:\/\/images\.neopets\.com\/cp\/(bio|items)\/swf\/[0-9]{3}\/[0-9]{3}\/[0-9]{3}\/[a-f0-9_]+\.swf$/, ]; @@ -36,12 +36,20 @@ export default async (req, res) => { res.status(proxyRes.status); - res.setHeader("Content-Length", proxyRes.headers.get("Content-Length")); - res.setHeader("Content-Type", proxyRes.headers.get("Content-Type")); - res.setHeader("Cache-Control", proxyRes.headers.get("Cache-Control")); res.setHeader("ETag", proxyRes.headers.get("ETag")); res.setHeader("Last-Modified", proxyRes.headers.get("Last-Modified")); - streamPipeline(proxyRes.body, res); + if (!proxyRes.headers.get("Content-Encoding")) { + // If the content is not encoded (I think their images generally aren't?), + // stream the body directly, to speed things up a bit. + res.setHeader("Content-Length", proxyRes.headers.get("Content-Length")); + res.setHeader("Content-Type", proxyRes.headers.get("Content-Type")); + await streamPipeline(proxyRes.body, res); + } else { + // Otherwise, I don't immediately know how to stream encoded content, so, + // let's just await the full body and send it the normal way. + const buffer = await proxyRes.buffer(); + res.send(buffer); + } }; diff --git a/public/animate-test.html b/public/animate-test.html index aad2a67..4a1f14d 100644 --- a/public/animate-test.html +++ b/public/animate-test.html @@ -1,9 +1,10 @@ - - - + + + +