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