Skip to content

Commit d57376e

Browse files
authored
feat: public plugin for static assets (#36)
1 parent b30d133 commit d57376e

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

packages/dobs/src/_types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export type LooseObject<KnownKeys extends string, ValueType> = Partial<
66
export type LooseKey<K extends string> = K | (string & {});
77

88
export type Promisable<T> = T | Promise<T>;
9-
export type Maybe<T> = T | null | undefined;
9+
export type Maybe<T> = T | null | undefined | void;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './cache';
2+
export * from './public';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { statSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
import { Plugin } from '~/dobs/plugin';
4+
5+
function _dynamic(prefix: string, directory: string): Plugin {
6+
return {
7+
name: 'dobs/experimental/public-plugin:dynamic',
8+
server(server) {
9+
server.use((req, res, next) => {
10+
if (!req.pathname.startsWith(prefix)) {
11+
return next();
12+
}
13+
14+
const filePath = join(directory, req.pathname.slice(prefix.length));
15+
16+
try {
17+
const stat = statSync(filePath);
18+
19+
if (!stat.isDirectory()) {
20+
return res.sendFile(filePath);
21+
}
22+
23+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
24+
} catch (e) {
25+
// res.status(404).send('File not found');
26+
}
27+
28+
next();
29+
});
30+
},
31+
};
32+
}
33+
export function publicPlugin(prefix: string, directory: string): Plugin {
34+
return _dynamic(prefix, directory);
35+
}

packages/dobs/src/plugin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { BuildOptions } from 'rolldown';
2+
import type { BaseServer } from '@dobsjs/http';
23

34
import type { ResolvedServerConfig, ServerConfig } from './config';
45
import type { Promisable, Maybe } from './_types';
@@ -19,6 +20,8 @@ export interface Plugin {
1920
*/
2021
resolveBuildOptions?(buildOptions: BuildOptions): Maybe<BuildOptions>;
2122

23+
server?(server: BaseServer): Promisable<Maybe<BaseServer>>;
24+
2225
generateRoute?(route: Routes): Promisable<Maybe<Routes>>;
2326
}
2427

packages/dobs/src/server/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export async function createDobsServer<T extends ServerConfig>(
2222
const resolvedConfig = resolveConfig(config);
2323
const server = httpServer(resolvedConfig.createServer);
2424

25+
// [plugin] execute plugin.server
26+
await runner.execute('server', server);
27+
2528
// user middleware
2629
server.middlewares.push(...resolvedConfig.middlewares);
2730
// router middleware

0 commit comments

Comments
 (0)