Compare commits

..

No commits in common. "257f894087aad0c80c8b533246d8990b3daab76e" and "5e3eca8edc57a0e38d449c8859bd5bc600ebbfeb" have entirely different histories.

2 changed files with 3 additions and 70 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@char/ngx",
"version": "0.2.2",
"version": "0.2.1",
"license": "WTFPL",
"exports": "./ngx.ts"
}

71
ngx.ts
View file

@ -1,4 +1,4 @@
const NGX_VERSION = "0.2.2";
const NGX_VERSION = "0.2.1";
type ConfigNode = ConfigStatement | ConfigBlock | ConfigBreak | ConfigFile;
@ -84,23 +84,6 @@ function conform(looseNode: LooseConfigNode): ConfigNode[] {
return [looseNode];
}
/**
* create nginx config nodes. behavior depends on arguments:
*
* - no args - ConfigBreak
* - value only - ConfigStatement
* - value + children - ConfigBlock
* - children only - ConfigFile
*
* accept strings, ConfigNodes, or nested arrays that flatten automatically.
*
* ```ts
* ngx() // ConfigBreak
* ngx("worker_processes auto") // ConfigStatement
* ngx("location /", ["proxy_pass http://backend"]) // ConfigBlock
* ngx(["server { ... }", "server { ... }"]) // ConfigFile
* ```
*/
export function ngx(value?: string, children?: LooseConfigNode[]): ConfigNode {
const hasValue = value !== undefined && value !== "";
const hasChildren = children !== undefined;
@ -118,16 +101,6 @@ export function ngx(value?: string, children?: LooseConfigNode[]): ConfigNode {
throw new Error("unreachable");
}
/**
* create ssl listen directives for ipv4/ipv6 with http/2 enabled.
*
* extras modify the listen directives.
*
* ```ts
* listen() // listen 443 ssl, listen [::]:443 ssl, http2 on
* listen("default_server") // includes "default_server"
* ```
*/
export const listen = (...extras: string[]) =>
conform([
`listen 443 ${["ssl", ...extras].join(" ")}`,
@ -135,39 +108,9 @@ export const listen = (...extras: string[]) =>
`http2 on`,
]);
/**
* create http/3 (quic) listen directives (+ supporting alt-svc header).
*/
export const http3 = (
opts: {
/** max age for alt-svc header */ ma?: number;
} = {},
) =>
conform([
"listen 443 quic reuseport",
"listen [::]:443 quic reuseport",
`add_header Alt-Svc 'h3=":443"; ma=${opts.ma ?? 86400}'`,
]);
/**
* create a server_name nginx directive.
*
* ```ts
* serverName("example.com") // "server_name example.com;"
* ```
*/
export const serverName = (name: string) =>
new ConfigStatement(`server_name ${name}`);
/**
* create ssl certificate directives for let's encrypt certificates.
*
* ```ts
* letsEncrypt("example.com")
* // ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem
* // ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem
* ```
*/
export const letsEncrypt = (
domain: string,
liveDir = "/etc/letsencrypt/live",
@ -178,17 +121,7 @@ export const letsEncrypt = (
]);
// the default export is both the ngx function and a namespace:
/**
* use as a function to create config nodes, or access helpers.
*
* ```ts
* import ngx from './ngx';
* const config = ngx("location /", ["proxy_pass http://backend"]);
* console.log(ngx.NGX_VERSION);
* const ssl = ngx.letsEncrypt("example.com");
* ```
*/
export default Object.assign(
(value?: string, children?: LooseConfigNode[]) => ngx(value, children),
{ NGX_VERSION, listen, letsEncrypt, serverName, http3 },
{ NGX_VERSION, listen, letsEncrypt, serverName },
);