@greenwood/cli
Advanced tools
Comparing version 0.28.3 to 0.28.4
{ | ||
"name": "@greenwood/cli", | ||
"version": "0.28.3", | ||
"version": "0.28.4", | ||
"description": "Greenwood CLI.", | ||
@@ -76,3 +76,3 @@ "type": "module", | ||
}, | ||
"gitHead": "1d08276fc2b05ee4aaaa5f33afb77e7c6cb54828" | ||
"gitHead": "7c4aa0128bea0395c66df48f1a07901c4829dd33" | ||
} |
@@ -135,23 +135,34 @@ import fs from 'fs/promises'; | ||
app.use(async (ctx) => { | ||
const body = ctx.response.body; | ||
const url = new URL(ctx.url); | ||
// don't interfere with external requests or API calls, binary files, or JSON | ||
// don't interfere with external requests or API calls, only files | ||
// and only run in development | ||
if (process.env.__GWD_COMMAND__ === 'develop' && url.protocol === 'file:') { // eslint-disable-line no-underscore-dangle | ||
if (!body || Buffer.isBuffer(body)) { | ||
// console.warn(`no body for => ${ctx.url}`); | ||
} else { | ||
const inm = ctx.headers['if-none-match']; | ||
const etagHash = url.pathname.split('.').pop() === 'json' | ||
? hashString(JSON.stringify(body)) | ||
: hashString(body); | ||
// TODO there's probably a better way to do this with tee-ing streams but this works for now | ||
const response = new Response(ctx.body, { | ||
status: ctx.response.status, | ||
headers: new Headers(ctx.response.header) | ||
}).clone(); | ||
const splitResponse = response.clone(); | ||
const contents = await splitResponse.text(); | ||
const inm = ctx.headers['if-none-match']; | ||
const etagHash = url.pathname.split('.').pop() === 'json' | ||
? hashString(JSON.stringify(contents)) | ||
: hashString(contents); | ||
if (inm && inm === etagHash) { | ||
ctx.status = 304; | ||
ctx.body = null; | ||
ctx.set('Etag', etagHash); | ||
ctx.set('Cache-Control', 'no-cache'); | ||
} else if (!inm || inm !== etagHash) { | ||
ctx.set('Etag', etagHash); | ||
if (inm && inm === etagHash) { | ||
ctx.status = 304; | ||
ctx.body = null; | ||
ctx.set('Etag', etagHash); | ||
ctx.set('Cache-Control', 'no-cache'); | ||
} else if (!inm || inm !== etagHash) { | ||
ctx.body = Readable.from(response.body); | ||
ctx.status = ctx.status; | ||
ctx.set('Content-Type', ctx.response.header['content-type']); | ||
ctx.set('Etag', etagHash); | ||
// TODO automatically loop and apply all custom headers to Koa response, include Content-Type below | ||
// https://github.com/ProjectEvergreen/greenwood/issues/1048 | ||
if (response.headers.has('Content-Length')) { | ||
ctx.set('Content-Length', response.headers.get('Content-Length')); | ||
} | ||
@@ -158,0 +169,0 @@ } |
168476
3952