Skip to content

Commit 1e3ae0d

Browse files
committed
SiteConfig should always override the defaults
1 parent b941a50 commit 1e3ae0d

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const CACHE_SEC = 30;
22
export const CACHE_CONTROL = `public, no-cache, max-age=0, stale-while-revalidate=${CACHE_SEC}, s-maxage=${CACHE_SEC}, no-transform`;
3-
export const ENABLE_FILE_LISTING = false;
3+
export const ENABLE_DIR_LISTING = false;
44
export const PROTECTED_FILES = [/\.swp$/];
55
export const HIDDEN_FILES = [/^\./, ...PROTECTED_FILES];
66
export const INDEX_PATTERN = /^index\..*/i;

src/error.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export interface TuristError {
1111
[x: string]: any;
1212
}
1313

14+
const safeSiteConfig: SiteConfig = {
15+
customErrors: [],
16+
dirListing: false,
17+
functions: false,
18+
};
19+
1420
/**
1521
* Send a standardized error to the HTTP client.
1622
* @param req is the incoming request.
@@ -22,10 +28,14 @@ export async function sendError(
2228
res: ServerResponse,
2329
statusCode: number,
2430
error: TuristError,
25-
siteConfig?: SiteConfig | null
31+
siteConfig?: SiteConfig
2632
): Promise<void> {
2733
let types = ['*/*'];
2834

35+
if (!siteConfig) {
36+
siteConfig = safeSiteConfig;
37+
}
38+
2939
if (!error.code) {
3040
throw new Error('Error "code" is missing');
3141
}
@@ -44,11 +54,11 @@ export async function sendError(
4454

4555
setVary(res);
4656
if (types.includes('text/html')) {
47-
const customErrors = siteConfig?.customErrors;
57+
const customErrors = siteConfig.customErrors;
4858
const customPage = customErrors && customErrors[statusCode];
4959
if (customPage) {
5060
const host = req.headers.host?.split(':')[0] || '';
51-
return serveUri(req, res, host, customPage, { dirListing: false });
61+
return serveUri(req, res, host, customPage, safeSiteConfig);
5262
}
5363

5464
return send(

src/get-site-config.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import fetch from './fetch';
44
import getEnv from './get-env';
55
import promiseCache from './promise-cache';
66
import { DirectoryListing, File, Folder } from './graph-api-types';
7+
import * as defaultConfig from './config';
78

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

1011
/**
1112
* SiteConfig can be set per each domain.
13+
* Anything set here will override the default configuration;
14+
* If nothing is set here then the defaults will apply.
1215
*/
1316
export type SiteConfig = {
1417
/**
@@ -21,24 +24,30 @@ export type SiteConfig = {
2124
* }
2225
* ```
2326
*/
24-
customErrors?: {
27+
customErrors: {
2528
[index: number]: string;
2629
};
2730
/**
2831
* Enable directory listings.
2932
*/
30-
dirListing?: boolean;
33+
dirListing: boolean;
3134
/**
3235
* Execute functions.
3336
*/
34-
functions?: boolean;
37+
functions: boolean;
38+
};
39+
40+
const defaultSiteConfig: SiteConfig = {
41+
customErrors: {},
42+
dirListing: defaultConfig.ENABLE_DIR_LISTING,
43+
functions: defaultConfig.ENABLE_FUNCTIONS,
3544
};
3645

3746
const dirCache = new LRU<string, Promise<Array<File | Folder>>>({
3847
max: 1,
3948
maxAge: 60 * 1000,
4049
});
41-
const configCache = new LRU<string, Promise<SiteConfig | null>>({
50+
const configCache = new LRU<string, Promise<SiteConfig>>({
4251
max: 100,
4352
maxAge: 60 * 1000,
4453
});
@@ -54,17 +63,23 @@ const getDirList = promiseCache<Array<File | Folder>>(dirCache, async () => {
5463
return res.value;
5564
});
5665

57-
const getSiteConfig = promiseCache(configCache, async (host: string) => {
58-
const dir = await getDirList();
59-
const configFile = dir.find((o: any) => o.file && o.name === `${host}.json`) as File | undefined;
66+
const getSiteConfig = promiseCache(
67+
configCache,
68+
async (host: string): Promise<SiteConfig> => {
69+
const dir = await getDirList();
70+
const configFile = dir.find((o: any) => o.file && o.name === `${host}.json`) as File | undefined;
6071

61-
if (!configFile) {
62-
return null;
63-
}
72+
if (!configFile) {
73+
return defaultSiteConfig;
74+
}
6475

65-
const res = await fetch(configFile['@microsoft.graph.downloadUrl']);
66-
const body: SiteConfig = await res.json();
76+
const res = await fetch(configFile['@microsoft.graph.downloadUrl']);
77+
const body = await res.json();
6778

68-
return body;
69-
});
79+
return {
80+
...defaultSiteConfig,
81+
...body,
82+
};
83+
}
84+
);
7085
export default getSiteConfig;

src/serve-uri.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@ import getEnv from './get-env';
77
import promiseCache from './promise-cache';
88
import sendFile from './send-file';
99
import sendFileList from './send-file-list';
10-
import {
11-
CACHE_SEC,
12-
ENABLE_FILE_LISTING,
13-
ENABLE_FUNCTIONS,
14-
FUNCTION_PATTERN,
15-
HIDDEN_FILES,
16-
INDEX_PATTERN,
17-
PROTECTED_FILES,
18-
} from './config';
10+
import { CACHE_SEC, FUNCTION_PATTERN, HIDDEN_FILES, INDEX_PATTERN, PROTECTED_FILES } from './config';
1911
import { File, Folder } from './graph-api-types';
2012
import { SiteConfig } from './get-site-config';
2113
import { sendError } from './error';
@@ -48,16 +40,16 @@ function isIndexFile(name: string) {
4840
return INDEX_PATTERN.test(name) && PROTECTED_FILES.every((re) => !re.test(name.toLowerCase()));
4941
}
5042

51-
function shouldExec(siteConfig: SiteConfig | null, name: string): boolean {
52-
return (ENABLE_FUNCTIONS || !!siteConfig?.functions) && FUNCTION_PATTERN.test(name);
43+
function shouldExec(siteConfig: SiteConfig, name: string): boolean {
44+
return !!siteConfig.functions && FUNCTION_PATTERN.test(name);
5345
}
5446

5547
export default async function serveUri(
5648
req: IncomingMessage,
5749
res: ServerResponse,
5850
host: string,
5951
pathname: string,
60-
siteConfig: SiteConfig | null
52+
siteConfig: SiteConfig
6153
): Promise<void> {
6254
const graphUrl = buildUrl(host, pathname);
6355
if (graphUrl === null) {
@@ -101,7 +93,7 @@ export default async function serveUri(
10193

10294
return sendFile(req, res, index);
10395
} else {
104-
if (ENABLE_FILE_LISTING || siteConfig?.dirListing) {
96+
if (siteConfig.dirListing) {
10597
return sendError(
10698
req,
10799
res,

0 commit comments

Comments
 (0)