Skip to content

Commit 84d9e89

Browse files
committed
feat(nginx): add http map directive
1 parent 4333196 commit 84d9e89

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

.changeset/early-items-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@anymodel/nginx": minor
3+
---
4+
5+
Add HTTP map directive

packages/nginx/src/Directive.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HttpGzipDirective } from "./directives/HttpGzipDirective";
66
import { HttpHeadersDirective } from "./directives/HttpHeadersDirective";
77
import { HttpIndexDirective } from "./directives/HttpIndexDirective";
88
import { HttpLogDirective } from "./directives/HttpLogDirective";
9+
import { HttpMapDirective } from "./directives/HttpMapDirective";
910
import { HttpProxyDirective } from "./directives/HttpProxyDirective";
1011
import { HttpRewriteDirective } from "./directives/HttpRewriteDirective";
1112
import { HttpSslDirective } from "./directives/HttpSslDirective";
@@ -25,6 +26,7 @@ export const directiveMap = {
2526
[HttpRewriteDirective.type]: HttpRewriteDirective,
2627
[HttpSslDirective.type]: HttpSslDirective,
2728
[HttpUpstreamDirective.type]: HttpUpstreamDirective,
29+
[HttpMapDirective.type]: HttpMapDirective,
2830
};
2931

3032
export type DirectiveMap = {
@@ -40,6 +42,7 @@ export type DirectiveMap = {
4042
[HttpRewriteDirective.type]: HttpRewriteDirective;
4143
[HttpSslDirective.type]: HttpSslDirective;
4244
[HttpUpstreamDirective.type]: HttpUpstreamDirective;
45+
[HttpMapDirective.type]: HttpMapDirective;
4346
};
4447

4548
export type ContextDirectiveConfig = {
@@ -72,4 +75,5 @@ export type PickAllDirectiveKeys<T extends ContextDirectiveConfig> =
7275
PickDirectiveKeys<T, "httpProxy"> &
7376
PickDirectiveKeys<T, "httpRewrite"> &
7477
PickDirectiveKeys<T, "httpSsl"> &
75-
PickDirectiveKeys<T, "httpUpstream">;
78+
PickDirectiveKeys<T, "httpUpstream"> &
79+
PickDirectiveKeys<T, "httpMap">;

packages/nginx/src/contexts/AbstractContext.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ export class AbstractContext<TSpec, TConfig = void> {
5252
contextType as keyof typeof selfDirectiveConfig
5353
] as string[] | undefined;
5454
if (directiveKeys?.includes(directiveKey)) {
55-
return directiveMap[contextType as keyof typeof selfDirectiveConfig][
56-
"config"
57-
] as Config<any>;
55+
return (directiveMap as any)[contextType]["config"] as Config<any>;
5856
}
5957
}
6058
}

packages/nginx/src/contexts/HttpContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const directiveConfig = makeContextDirectiveConfig({
1313
"server",
1414
"keepalive_timeout",
1515
"root",
16-
"etag"
16+
"etag",
1717
],
1818
httpAccess: ["allow", "deny"],
1919
httpFastcgi: ["fastcgi_index", "fastcgi_param", "fastcgi_read_timeout"],
@@ -46,6 +46,7 @@ export const directiveConfig = makeContextDirectiveConfig({
4646
"ssl_session_tickets",
4747
"ssl_session_timeout",
4848
],
49+
httpMap: ["map", "map_hash_bucket_size", "map_hash_max_size"],
4950
});
5051

5152
export type HttpContextDirectiveSpec = PickAllDirectiveKeys<
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { AbstractDirective, Config } from "./AbstractDirective";
2+
3+
export type HttpMapDirectiveSpec = {
4+
/**
5+
* @link https://nginx.org/en/docs/http/ngx_http_map_module.html#map
6+
*/
7+
map?: {
8+
source: string;
9+
variable: string;
10+
values: Record<string, number | string | undefined>;
11+
}[];
12+
/**
13+
* @link https://nginx.org/en/docs/http/ngx_http_map_module.html#map_hash_bucket_size
14+
*/
15+
map_hash_bucket_size?: number;
16+
/**
17+
* @link https://nginx.org/en/docs/http/ngx_http_map_module.html#map_hash_max_size
18+
*/
19+
map_hash_max_size?: number;
20+
};
21+
22+
/**
23+
* @link https://nginx.org/en/docs/http/ngx_http_access_module.html#directives
24+
*/
25+
export class HttpMapDirective extends AbstractDirective<HttpMapDirectiveSpec> {
26+
static type = "httpMap" as const;
27+
static config: Config<HttpMapDirectiveSpec> = {
28+
map: (dir, level) =>
29+
dir.map((item) => {
30+
const pad = " ".repeat(level + 1);
31+
const values = Object.entries(item.values)
32+
.reduce((result, [key, value]) => {
33+
result.push(`${pad} ${key} ${value};`);
34+
return result;
35+
}, [] as string[])
36+
.join("\n");
37+
return `${item.source} ${item.variable} {\n${values}\n${pad}}`;
38+
}),
39+
map_hash_bucket_size: null,
40+
map_hash_max_size: null,
41+
};
42+
}

0 commit comments

Comments
 (0)