From c64387668de1fd633a1759ab1bda5a914848f3da Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 17 Nov 2021 21:02:53 -0800 Subject: [PATCH] Merge fix/path-parsing --- flow-typed/npm/koa-sendfile_vx.x.x.js | 33 ------------------ package.json | 2 +- server/routes/index.js | 48 ++++++++++++++++----------- yarn.lock | 48 ++++++++++----------------- 4 files changed, 46 insertions(+), 85 deletions(-) delete mode 100644 flow-typed/npm/koa-sendfile_vx.x.x.js diff --git a/flow-typed/npm/koa-sendfile_vx.x.x.js b/flow-typed/npm/koa-sendfile_vx.x.x.js deleted file mode 100644 index 29310ee0..00000000 --- a/flow-typed/npm/koa-sendfile_vx.x.x.js +++ /dev/null @@ -1,33 +0,0 @@ -// flow-typed signature: 6b7b08ea7721b475bf21c23e1d7cd2f9 -// flow-typed version: <>/koa-sendfile_v2.0.0/flow_v0.104.0 - -/** - * This is an autogenerated libdef stub for: - * - * 'koa-sendfile' - * - * Fill this stub out by replacing all the `any` types. - * - * Once filled out, we encourage you to share your work with the - * community by sending a pull request to: - * https://github.com/flowtype/flow-typed - */ - -declare module 'koa-sendfile' { - declare module.exports: any; -} - -/** - * We include stubs for each file inside this npm package in case you need to - * require those files directly. Feel free to delete any files that aren't - * needed. - */ - - -// Filename aliases -declare module 'koa-sendfile/index' { - declare module.exports: $Exports<'koa-sendfile'>; -} -declare module 'koa-sendfile/index.js' { - declare module.exports: $Exports<'koa-sendfile'>; -} diff --git a/package.json b/package.json index e95d4540..435bcb36 100644 --- a/package.json +++ b/package.json @@ -108,7 +108,7 @@ "koa-mount": "^3.0.0", "koa-onerror": "^4.1.0", "koa-router": "7.0.1", - "koa-sendfile": "2.0.0", + "koa-send": "5.0.1", "koa-sslify": "2.1.2", "koa-static": "^4.0.1", "lodash": "^4.17.21", diff --git a/server/routes/index.js b/server/routes/index.js index 8d827893..6a29b34e 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -4,7 +4,7 @@ import path from "path"; import util from "util"; import Koa from "koa"; import Router from "koa-router"; -import sendfile from "koa-sendfile"; +import send from "koa-send"; import serve from "koa-static"; import isUUID from "validator/lib/isUUID"; import { languages } from "../../shared/i18n"; @@ -95,16 +95,26 @@ koa.use( if (process.env.NODE_ENV === "production") { router.get("/static/*", async (ctx) => { - ctx.set({ - "Service-Worker-Allowed": "/", - "Access-Control-Allow-Origin": "*", - "Cache-Control": `max-age=${356 * 24 * 60 * 60}`, - }); + try { + await send(ctx, ctx.path.substring(8), { + root: path.join(__dirname, "../../app/"), + setHeaders: (res, path, stat) => { + res.setHeader("Service-Worker-Allowed", "/"); + res.setHeader("Access-Control-Allow-Origin", "*"); + res.setHeader("Cache-Control", `max-age=${365 * 24 * 60 * 60}`); + }, + }); + } catch (err) { + if (err.status === 404) { + // Serve a bad request instead of not found if the file doesn't exist + // This prevents CDN's from caching the response, allowing them to continue + // serving old file versions + ctx.status = 400; + return; + } - await sendfile( - ctx, - path.join(__dirname, "../../app/", ctx.path.substring(8)) - ); + throw err; + } }); } @@ -116,16 +126,14 @@ router.get("/locales/:lng.json", async (ctx) => { return; } - if (process.env.NODE_ENV === "production") { - ctx.set({ - "Cache-Control": `max-age=${7 * 24 * 60 * 60}`, - }); - } - - await sendfile( - ctx, - path.join(__dirname, "../../shared/i18n/locales", lng, "translation.json") - ); + await send(ctx, path.join(lng, "translation.json"), { + setHeaders: (res, path, stat) => { + if (process.env.NODE_ENV === "production") { + res.setHeader("Cache-Control", `max-age=${7 * 24 * 60 * 60}`); + } + }, + root: path.join(__dirname, "../../shared/i18n/locales"), + }); }); router.get("/robots.txt", (ctx) => { diff --git a/yarn.lock b/yarn.lock index fbfa3be2..5d315d16 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5382,13 +5382,6 @@ de-indent@^1.0.2: resolved "https://registry.yarnpkg.com/de-indent/-/de-indent-1.0.2.tgz#b2038e846dc33baa5796128d0804b455b8c1e21d" integrity sha1-sgOOhG3DO6pXlhKNCAS0VbjB4h0= -debug@*, debug@4, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1: - version "4.3.1" - resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee" - integrity sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ== - dependencies: - ms "2.1.2" - debug@3.1.0, debug@~3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" @@ -5396,6 +5389,13 @@ debug@3.1.0, debug@~3.1.0: dependencies: ms "2.0.0" +debug@4, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1: + version "4.3.1" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee" + integrity sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ== + dependencies: + ms "2.1.2" + debug@^2.1.3, debug@^2.2.0, debug@^2.3.3, debug@^2.6.1, debug@^2.6.3, debug@^2.6.8, debug@^2.6.9: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" @@ -6425,11 +6425,6 @@ esutils@^2.0.2: resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64" integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g== -etag@^1.5.1: - version "1.8.1" - resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" - integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc= - event-emitter@^0.3.5: version "0.3.5" resolved "https://registry.yarnpkg.com/event-emitter/-/event-emitter-0.3.5.tgz#df8c69eef1647923c7157b9ce83840610b02cc39" @@ -9400,6 +9395,15 @@ koa-router@^10.0.0: methods "^1.1.2" path-to-regexp "^6.1.0" +koa-send@5.0.1, koa-send@^5.0.0: + version "5.0.1" + resolved "https://registry.yarnpkg.com/koa-send/-/koa-send-5.0.1.tgz#39dceebfafb395d0d60beaffba3a70b4f543fe79" + integrity sha512-tmcyQ/wXXuxpDxyNXv5yNNkdAMdFRqwtegBXUaowiQzUKqJehttS0x2j0eOZDQAyloAth5w6wwBImnFzkUz3pQ== + dependencies: + debug "^4.1.1" + http-errors "^1.7.3" + resolve-path "^1.4.0" + koa-send@^4.1.3: version "4.1.3" resolved "https://registry.yarnpkg.com/koa-send/-/koa-send-4.1.3.tgz#0822207bbf5253a414c8f1765ebc29fa41353cb6" @@ -9410,24 +9414,6 @@ koa-send@^4.1.3: mz "^2.6.0" resolve-path "^1.4.0" -koa-send@^5.0.0: - version "5.0.1" - resolved "https://registry.yarnpkg.com/koa-send/-/koa-send-5.0.1.tgz#39dceebfafb395d0d60beaffba3a70b4f543fe79" - integrity sha512-tmcyQ/wXXuxpDxyNXv5yNNkdAMdFRqwtegBXUaowiQzUKqJehttS0x2j0eOZDQAyloAth5w6wwBImnFzkUz3pQ== - dependencies: - debug "^4.1.1" - http-errors "^1.7.3" - resolve-path "^1.4.0" - -koa-sendfile@2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/koa-sendfile/-/koa-sendfile-2.0.0.tgz#446e2a35ba7074ed03afda2c667c2359406e1082" - integrity sha1-RG4qNbpwdO0Dr9osZnwjWUBuEII= - dependencies: - debug "*" - etag "^1.5.1" - mz "2" - koa-sslify@2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/koa-sslify/-/koa-sslify-2.1.2.tgz#8947fd53949d69d539607814097863c1ecf38f30" @@ -10337,7 +10323,7 @@ multer@^1.4.2: type-is "^1.6.4" xtend "^4.0.0" -mz@2, mz@^2.4.0, mz@^2.6.0: +mz@^2.4.0, mz@^2.6.0: version "2.7.0" resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32" integrity sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==