0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-13 21:30:30 -05:00
logto/packages/core/src/middleware/koa-serve-static.ts
Gao Sun 2812d6de46
refactor(core): serve static middleware (#1010)
* refactor(core): serve static middleware

* chore(deps): remove unused package

* refactor(core): per review

* fix(core): test

* refactor(core): using endsWith to detect index
2022-06-01 05:12:15 +00:00

38 lines
1 KiB
TypeScript

// Modified from https://github.com/koajs/static/blob/7f0ed88c8902e441da4e30b42f108617d8dff9ec/index.js
import path from 'path';
import buildDebug from 'debug';
import { MiddlewareType } from 'koa';
import send from 'koa-send';
import assertThat from '@/utils/assert-that';
const debug = buildDebug('koa-static');
export default function serve(root: string) {
assertThat(root, new Error('Root directory is required to serve files.'));
const options: send.SendOptions = {
root: path.resolve(root),
index: 'index.html',
};
debug('static "%s"', root);
const serve: MiddlewareType = async (ctx, next) => {
if (ctx.method === 'HEAD' || ctx.method === 'GET') {
await send(ctx, ctx.path, {
...options,
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
...(!['/', `/${options.index || ''}`].some((path) => ctx.path.endsWith(path)) && {
maxage: 604_800_000 /* 7 days */,
}),
});
}
return next();
};
return serve;
}