v0.2: new http2 syntax, drop write(..), add serverName(..)

This commit is contained in:
Charlotte Som 2024-11-27 05:34:18 +02:00
parent 881649cf49
commit 7ea3f8e0c1
2 changed files with 14 additions and 16 deletions

View file

@ -1,5 +1,5 @@
{ {
"name": "@char/ngx", "name": "@char/ngx",
"version": "0.1.1", "version": "0.2.0",
"exports": "./ngx.ts" "exports": "./ngx.ts"
} }

28
ngx.ts
View file

@ -1,4 +1,4 @@
const NGX_VERSION = "0.1.1"; const NGX_VERSION = "0.2.0";
type ConfigNode = ConfigStatement | ConfigBlock | ConfigBreak | ConfigFile; type ConfigNode = ConfigStatement | ConfigBlock | ConfigBreak | ConfigFile;
@ -6,11 +6,6 @@ class ConfigBuildable {
build(): string { build(): string {
throw new Error("build(..) not implemented!"); throw new Error("build(..) not implemented!");
} }
write(path: string) {
const contents = this.build();
Deno.writeTextFileSync(path, contents);
}
} }
class ConfigBlock extends ConfigBuildable { class ConfigBlock extends ConfigBuildable {
@ -24,7 +19,7 @@ class ConfigBlock extends ConfigBuildable {
this.children = children; this.children = children;
} }
build(): string { override build(): string {
let output = this.value; let output = this.value;
output += " {\n "; output += " {\n ";
output += this.children output += this.children
@ -43,13 +38,13 @@ class ConfigStatement extends ConfigBuildable {
this.value = value; this.value = value;
} }
build(): string { override build(): string {
return this.value + ";"; return this.value + ";";
} }
} }
class ConfigBreak extends ConfigBuildable { class ConfigBreak extends ConfigBuildable {
build(): string { override build(): string {
return ""; return "";
} }
} }
@ -62,7 +57,7 @@ class ConfigFile extends ConfigBuildable {
this.nodes = nodes; this.nodes = nodes;
} }
build(): string { override build(): string {
return this.nodes.map((n) => n.build()).join("\n"); return this.nodes.map((n) => n.build()).join("\n");
} }
} }
@ -82,7 +77,7 @@ function conform(looseNode: LooseConfigNode): ConfigNode[] {
return looseNode return looseNode
.map((n) => conform(n)) .map((n) => conform(n))
.reduceRight((b, a) => .reduceRight((b, a) =>
a.length === 1 ? [...a, ...b] : [...a, new ConfigBreak(), ...b] a.length === 1 ? [...a, ...b] : [...a, new ConfigBreak(), ...b],
); );
} }
} }
@ -108,13 +103,16 @@ export function ngx(value?: string, children?: LooseConfigNode[]): ConfigNode {
export const listen = (...extras: string[]) => export const listen = (...extras: string[]) =>
conform([ conform([
`listen 443 ${["ssl", "http2", ...extras].join(" ")}`, `listen 443 ${["ssl", ...extras].join(" ")}`,
`listen [::]:443 ${["ssl", "http2", ...extras].join(" ")}`, `listen [::]:443 ${["ssl", ...extras].join(" ")}`,
`http2 on`,
]); ]);
export const serverName = (name: string) => conform([`server_name ${name}`]);
export const letsEncrypt = ( export const letsEncrypt = (
domain: string, domain: string,
liveDir = "/etc/letsencrypt/live" liveDir = "/etc/letsencrypt/live",
) => ) =>
conform([ conform([
`ssl_certificate ${liveDir}/${domain}/fullchain.pem`, `ssl_certificate ${liveDir}/${domain}/fullchain.pem`,
@ -124,5 +122,5 @@ export const letsEncrypt = (
// the default export is both the ngx function and a namespace: // the default export is both the ngx function and a namespace:
export default Object.assign( export default Object.assign(
(value?: string, children?: LooseConfigNode[]) => ngx(value, children), (value?: string, children?: LooseConfigNode[]) => ngx(value, children),
{ NGX_VERSION, listen, letsEncrypt } { NGX_VERSION, listen, letsEncrypt, serverName },
); );