Skip to content

Commit

Permalink
SiteConfig should always override the defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliV committed Apr 7, 2020
1 parent b941a50 commit 1e3ae0d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const CACHE_SEC = 30;
export const CACHE_CONTROL = `public, no-cache, max-age=0, stale-while-revalidate=${CACHE_SEC}, s-maxage=${CACHE_SEC}, no-transform`;
export const ENABLE_FILE_LISTING = false;
export const ENABLE_DIR_LISTING = false;
export const PROTECTED_FILES = [/\.swp$/];
export const HIDDEN_FILES = [/^\./, ...PROTECTED_FILES];
export const INDEX_PATTERN = /^index\..*/i;
Expand Down
16 changes: 13 additions & 3 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export interface TuristError {
[x: string]: any;
}

const safeSiteConfig: SiteConfig = {
customErrors: [],
dirListing: false,
functions: false,
};

/**
* Send a standardized error to the HTTP client.
* @param req is the incoming request.
Expand All @@ -22,10 +28,14 @@ export async function sendError(
res: ServerResponse,
statusCode: number,
error: TuristError,
siteConfig?: SiteConfig | null
siteConfig?: SiteConfig
): Promise<void> {
let types = ['*/*'];

if (!siteConfig) {
siteConfig = safeSiteConfig;
}

if (!error.code) {
throw new Error('Error "code" is missing');
}
Expand All @@ -44,11 +54,11 @@ export async function sendError(

setVary(res);
if (types.includes('text/html')) {
const customErrors = siteConfig?.customErrors;
const customErrors = siteConfig.customErrors;
const customPage = customErrors && customErrors[statusCode];
if (customPage) {
const host = req.headers.host?.split(':')[0] || '';
return serveUri(req, res, host, customPage, { dirListing: false });
return serveUri(req, res, host, customPage, safeSiteConfig);
}

return send(
Expand Down
43 changes: 29 additions & 14 deletions src/get-site-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import fetch from './fetch';
import getEnv from './get-env';
import promiseCache from './promise-cache';
import { DirectoryListing, File, Folder } from './graph-api-types';
import * as defaultConfig from './config';

const [ROOT] = getEnv('ROOT');

/**
* SiteConfig can be set per each domain.
* Anything set here will override the default configuration;
* If nothing is set here then the defaults will apply.
*/
export type SiteConfig = {
/**
Expand All @@ -21,24 +24,30 @@ export type SiteConfig = {
* }
* ```
*/
customErrors?: {
customErrors: {
[index: number]: string;
};
/**
* Enable directory listings.
*/
dirListing?: boolean;
dirListing: boolean;
/**
* Execute functions.
*/
functions?: boolean;
functions: boolean;
};

const defaultSiteConfig: SiteConfig = {
customErrors: {},
dirListing: defaultConfig.ENABLE_DIR_LISTING,
functions: defaultConfig.ENABLE_FUNCTIONS,
};

const dirCache = new LRU<string, Promise<Array<File | Folder>>>({
max: 1,
maxAge: 60 * 1000,
});
const configCache = new LRU<string, Promise<SiteConfig | null>>({
const configCache = new LRU<string, Promise<SiteConfig>>({
max: 100,
maxAge: 60 * 1000,
});
Expand All @@ -54,17 +63,23 @@ const getDirList = promiseCache<Array<File | Folder>>(dirCache, async () => {
return res.value;
});

const getSiteConfig = promiseCache(configCache, async (host: string) => {
const dir = await getDirList();
const configFile = dir.find((o: any) => o.file && o.name === `${host}.json`) as File | undefined;
const getSiteConfig = promiseCache(
configCache,
async (host: string): Promise<SiteConfig> => {
const dir = await getDirList();
const configFile = dir.find((o: any) => o.file && o.name === `${host}.json`) as File | undefined;

if (!configFile) {
return null;
}
if (!configFile) {
return defaultSiteConfig;
}

const res = await fetch(configFile['@microsoft.graph.downloadUrl']);
const body: SiteConfig = await res.json();
const res = await fetch(configFile['@microsoft.graph.downloadUrl']);
const body = await res.json();

return body;
});
return {
...defaultSiteConfig,
...body,
};
}
);
export default getSiteConfig;
18 changes: 5 additions & 13 deletions src/serve-uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ import getEnv from './get-env';
import promiseCache from './promise-cache';
import sendFile from './send-file';
import sendFileList from './send-file-list';
import {
CACHE_SEC,
ENABLE_FILE_LISTING,
ENABLE_FUNCTIONS,
FUNCTION_PATTERN,
HIDDEN_FILES,
INDEX_PATTERN,
PROTECTED_FILES,
} from './config';
import { CACHE_SEC, FUNCTION_PATTERN, HIDDEN_FILES, INDEX_PATTERN, PROTECTED_FILES } from './config';
import { File, Folder } from './graph-api-types';
import { SiteConfig } from './get-site-config';
import { sendError } from './error';
Expand Down Expand Up @@ -48,16 +40,16 @@ function isIndexFile(name: string) {
return INDEX_PATTERN.test(name) && PROTECTED_FILES.every((re) => !re.test(name.toLowerCase()));
}

function shouldExec(siteConfig: SiteConfig | null, name: string): boolean {
return (ENABLE_FUNCTIONS || !!siteConfig?.functions) && FUNCTION_PATTERN.test(name);
function shouldExec(siteConfig: SiteConfig, name: string): boolean {
return !!siteConfig.functions && FUNCTION_PATTERN.test(name);
}

export default async function serveUri(
req: IncomingMessage,
res: ServerResponse,
host: string,
pathname: string,
siteConfig: SiteConfig | null
siteConfig: SiteConfig
): Promise<void> {
const graphUrl = buildUrl(host, pathname);
if (graphUrl === null) {
Expand Down Expand Up @@ -101,7 +93,7 @@ export default async function serveUri(

return sendFile(req, res, index);
} else {
if (ENABLE_FILE_LISTING || siteConfig?.dirListing) {
if (siteConfig.dirListing) {
return sendError(
req,
res,
Expand Down

0 comments on commit 1e3ae0d

Please sign in to comment.