Skip to content

Commit

Permalink
feat(nginx): add http map directive
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Nov 27, 2023
1 parent 4333196 commit 84d9e89
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-items-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@anymodel/nginx": minor
---

Add HTTP map directive
6 changes: 5 additions & 1 deletion packages/nginx/src/Directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { HttpGzipDirective } from "./directives/HttpGzipDirective";
import { HttpHeadersDirective } from "./directives/HttpHeadersDirective";
import { HttpIndexDirective } from "./directives/HttpIndexDirective";
import { HttpLogDirective } from "./directives/HttpLogDirective";
import { HttpMapDirective } from "./directives/HttpMapDirective";
import { HttpProxyDirective } from "./directives/HttpProxyDirective";
import { HttpRewriteDirective } from "./directives/HttpRewriteDirective";
import { HttpSslDirective } from "./directives/HttpSslDirective";
Expand All @@ -25,6 +26,7 @@ export const directiveMap = {
[HttpRewriteDirective.type]: HttpRewriteDirective,
[HttpSslDirective.type]: HttpSslDirective,
[HttpUpstreamDirective.type]: HttpUpstreamDirective,
[HttpMapDirective.type]: HttpMapDirective,
};

export type DirectiveMap = {
Expand All @@ -40,6 +42,7 @@ export type DirectiveMap = {
[HttpRewriteDirective.type]: HttpRewriteDirective;
[HttpSslDirective.type]: HttpSslDirective;
[HttpUpstreamDirective.type]: HttpUpstreamDirective;
[HttpMapDirective.type]: HttpMapDirective;
};

export type ContextDirectiveConfig = {
Expand Down Expand Up @@ -72,4 +75,5 @@ export type PickAllDirectiveKeys<T extends ContextDirectiveConfig> =
PickDirectiveKeys<T, "httpProxy"> &
PickDirectiveKeys<T, "httpRewrite"> &
PickDirectiveKeys<T, "httpSsl"> &
PickDirectiveKeys<T, "httpUpstream">;
PickDirectiveKeys<T, "httpUpstream"> &
PickDirectiveKeys<T, "httpMap">;
4 changes: 1 addition & 3 deletions packages/nginx/src/contexts/AbstractContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export class AbstractContext<TSpec, TConfig = void> {
contextType as keyof typeof selfDirectiveConfig
] as string[] | undefined;
if (directiveKeys?.includes(directiveKey)) {
return directiveMap[contextType as keyof typeof selfDirectiveConfig][
"config"
] as Config<any>;
return (directiveMap as any)[contextType]["config"] as Config<any>;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/nginx/src/contexts/HttpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const directiveConfig = makeContextDirectiveConfig({
"server",
"keepalive_timeout",
"root",
"etag"
"etag",
],
httpAccess: ["allow", "deny"],
httpFastcgi: ["fastcgi_index", "fastcgi_param", "fastcgi_read_timeout"],
Expand Down Expand Up @@ -46,6 +46,7 @@ export const directiveConfig = makeContextDirectiveConfig({
"ssl_session_tickets",
"ssl_session_timeout",
],
httpMap: ["map", "map_hash_bucket_size", "map_hash_max_size"],
});

export type HttpContextDirectiveSpec = PickAllDirectiveKeys<
Expand Down
42 changes: 42 additions & 0 deletions packages/nginx/src/directives/HttpMapDirective.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { AbstractDirective, Config } from "./AbstractDirective";

export type HttpMapDirectiveSpec = {
/**
* @link https://nginx.org/en/docs/http/ngx_http_map_module.html#map
*/
map?: {
source: string;
variable: string;
values: Record<string, number | string | undefined>;
}[];
/**
* @link https://nginx.org/en/docs/http/ngx_http_map_module.html#map_hash_bucket_size
*/
map_hash_bucket_size?: number;
/**
* @link https://nginx.org/en/docs/http/ngx_http_map_module.html#map_hash_max_size
*/
map_hash_max_size?: number;
};

/**
* @link https://nginx.org/en/docs/http/ngx_http_access_module.html#directives
*/
export class HttpMapDirective extends AbstractDirective<HttpMapDirectiveSpec> {
static type = "httpMap" as const;
static config: Config<HttpMapDirectiveSpec> = {
map: (dir, level) =>
dir.map((item) => {
const pad = " ".repeat(level + 1);
const values = Object.entries(item.values)
.reduce((result, [key, value]) => {
result.push(`${pad} ${key} ${value};`);
return result;
}, [] as string[])
.join("\n");
return `${item.source} ${item.variable} {\n${values}\n${pad}}`;
}),
map_hash_bucket_size: null,
map_hash_max_size: null,
};
}

0 comments on commit 84d9e89

Please sign in to comment.