Skip to content

Commit

Permalink
fix: empty response handler
Browse files Browse the repository at this point in the history
  • Loading branch information
adeyahya committed Aug 31, 2021
1 parent b783362 commit b805696
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ build/
.autoenv.zsh
yarn-error.log
.cache
.DS_Store
/.warlock.yaml
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"lru-cache": "^6.0.0",
"md5": "^2.3.0",
"moment": "^2.29.1",
"ms": "^2.1.3",
"multer": "^1.4.2",
"node-lmdb": "^0.9.4",
"proxy-from-env": "^1.1.0",
Expand Down
15 changes: 6 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,17 @@ const init = () => {

app.use(coreMiddleware(cacheInstance));
app.use(resolverMiddleware);
app.use((_, res) => {
if (
typeof res.locals === 'object' &&
Object.keys(res.locals).length === 0
) {
return res.sendFile(path.join(__dirname, '../www/index.html'));
}

app.use((req, res) => {
if (typeof res.locals === 'object') {
res.set('Content-Type', 'application/json; charset=utf-8');
res.set('Access-Control-Allow-Origin', '*');
}

res.send(res.locals);
if (!req.url.match(/^\/http/)) {
res.sendFile(path.join(__dirname, '../www/index.html'));
} else {
res.send(res.locals);
}
});
};
export const run = ({ port, config }: { port: number; config: Config }) => {
Expand Down
12 changes: 9 additions & 3 deletions src/middleware/rest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RequestHandler } from 'express';
import axios, { Method } from 'axios';
import ms from 'ms';
import * as E from 'fp-ts/Either';
import { ICache } from '../libs/cache';
import { getCacheKey, encode, decode } from '../libs/cacheHttp';
Expand All @@ -8,6 +9,7 @@ import { logger, debugable } from '../libs';

const instance = axios.create({
withCredentials: true,
timeout: ms('5s'),
// all http status code will handled as positive (then) instead of negative (catch)
// so the proxy can pipe all kind of response.
validateStatus: () => true,
Expand All @@ -28,6 +30,8 @@ export const restMiddleware = (cache: ICache) => (url: URL): RequestHandler => (
res.set('x-warlock-cache', 'HIT');
debugable(req, _cachedResponse);
next();
} else {
res.locals = {};
}
};

Expand Down Expand Up @@ -89,9 +93,11 @@ export const restMiddleware = (cache: ICache) => (url: URL): RequestHandler => (
.catch((e) => {
logger.error(JSON.stringify(e));

if (req.method.toLocaleLowerCase() !== 'get') {
if (req.method.toLocaleLowerCase() === 'get') {
res.locals = {};
next();
} else {
responseWithCache(cacheKey);
}
})
.finally(next);
});
};
20 changes: 2 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1333,14 +1333,6 @@
pirates "^4.0.0"
source-map-support "^0.5.16"

"@babel/runtime-corejs2@^7.14.0":
version "7.14.0"
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs2/-/runtime-corejs2-7.14.0.tgz#5519b92ccc819bd3d0a1ecb25ab3cb5a775485f8"
integrity sha512-btR4E8JiGlmmDI5YgirlG9z3T91rBdxnVh2YuEStrHDcekffaaIeK+CE0S4IaYUyYhMa7rFDfF2GEO79XNbLEA==
dependencies:
core-js "^2.6.5"
regenerator-runtime "^0.13.4"

"@babel/runtime-corejs3@^7.10.2":
version "7.13.10"
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.13.10.tgz#14c3f4c85de22ba88e8e86685d13e8861a82fe86"
Expand All @@ -1349,14 +1341,6 @@
core-js-pure "^3.0.0"
regenerator-runtime "^0.13.4"

"@babel/runtime-corejs3@^7.14.0":
version "7.14.0"
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.14.0.tgz#6bf5fbc0b961f8e3202888cb2cd0fb7a0a9a3f66"
integrity sha512-0R0HTZWHLk6G8jIk0FtoX+AatCtKnswS98VhXwGImFc759PJRp4Tru0PQYZofyijTFUr+gT8Mu7sgXVJLQ0ceg==
dependencies:
core-js-pure "^3.0.0"
regenerator-runtime "^0.13.4"

"@babel/[email protected]":
version "7.12.1"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.1.tgz#b4116a6b6711d010b2dad3b7b6e43bf1b9954740"
Expand Down Expand Up @@ -7127,7 +7111,7 @@ core-js-pure@^3.0.0:
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.10.0.tgz#dab9d6b141779b622b40567e7a536d2276646c15"
integrity sha512-CC582enhrFZStO4F8lGI7QL3SYx7/AIRc+IdSi3btrQGrVsTawo5K/crmKbRrQ+MOMhNX4v+PATn0k2NN6wI7A==

core-js@^2.4.0, core-js@^2.6.5:
core-js@^2.4.0:
version "2.6.12"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.12.tgz#d9333dfa7b065e347cc5682219d6f690859cc2ec"
integrity sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==
Expand Down Expand Up @@ -13067,7 +13051,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

[email protected], ms@^2.0.0, ms@^2.1.1, ms@^2.1.2:
[email protected], ms@^2.0.0, ms@^2.1.1, ms@^2.1.2, ms@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
Expand Down

0 comments on commit b805696

Please sign in to comment.