INA-2: каркас Node/TS, переводы ru/kk/en, рендер страницы и сервер, проверки

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peppa 2026-11-21 14:01:29 +00:00
parent 63b5a4ecd8
commit b75d8e5e67
14 changed files with 729 additions and 1 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@ node_modules/
.env
.env.local
.DS_Store
dist/
.pnpm-store/

View File

@ -1 +1,19 @@
{"slug":"inat","key":"INA","name":"inat","peppaUrl":"https://peppa.telecom.quest"}
{
"slug": "inat",
"key": "INA",
"name": "inat",
"peppaUrl": "https://peppa.telecom.quest",
"checks": {
"install": "corepack enable >/dev/null 2>&1 || true; pnpm install --frozen-lockfile",
"lint": "pnpm lint",
"typecheck": "pnpm typecheck",
"test": "pnpm test",
"build": "pnpm build"
},
"deploy": {
"compose": "deploy/compose.yml",
"service": "web",
"port": 3000,
"health": "/healthz"
}
}

7
biome.json Normal file
View File

@ -0,0 +1,7 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"vcs": { "enabled": true, "clientKind": "git", "useIgnoreFile": true },
"files": { "includes": ["src/**", "test/**", "locales/**", "*.json"] },
"formatter": { "indentStyle": "space", "indentWidth": 2, "lineWidth": 100 },
"linter": { "enabled": true, "rules": { "preset": "recommended" } }
}

25
locales/en.json Normal file
View File

@ -0,0 +1,25 @@
{
"meta.title": "[PLACEHOLDER] Company name — business card website",
"meta.description": "[PLACEHOLDER] Short company description for search engines and links",
"site.name": "[PLACEHOLDER] Company name",
"site.tagline": "[PLACEHOLDER] Short company slogan",
"about.heading": "About us",
"about.text": "[PLACEHOLDER] This is where you will find our story: who we are, how long we have been working and why you can trust us.",
"services.heading": "What we do",
"services.text": "[PLACEHOLDER] This is where you will find a description of what the company does.",
"services.item1": "[PLACEHOLDER] First line of work",
"services.item2": "[PLACEHOLDER] Second line of work",
"services.item3": "[PLACEHOLDER] Third line of work",
"contacts.heading": "Contacts",
"contacts.text": "[PLACEHOLDER] This is where you will find ways to contact us.",
"contacts.phoneLabel": "Phone",
"contacts.phone": "[PLACEHOLDER] phone",
"contacts.emailLabel": "Email",
"contacts.email": "[PLACEHOLDER] email",
"contacts.addressLabel": "Address",
"contacts.address": "[PLACEHOLDER] address",
"footer.text": "[PLACEHOLDER] Company name",
"notFound.title": "Page not found",
"notFound.text": "This page does not exist. Go to the home page.",
"notFound.link": "Home page"
}

25
locales/kk.json Normal file
View File

@ -0,0 +1,25 @@
{
"meta.title": "[ҮЛГІ] Компания атауы — визитка-сайт",
"meta.description": "[ҮЛГІ] Іздеу жүйелері мен сілтемелерге арналған компанияның қысқаша сипаттамасы",
"site.name": "[ҮЛГІ] Компания атауы",
"site.tagline": "[ҮЛГІ] Компанияның қысқа ұраны",
"about.heading": "Біз туралы",
"about.text": "[ҮЛГІ] Мұнда компания туралы мәлімет болады: біз кімбіз, қанша жыл жұмыс істейміз және бізге неге сенуге болады.",
"services.heading": "Немен айналысамыз",
"services.text": "[ҮЛГІ] Мұнда компанияның қызметі сипатталады.",
"services.item1": "[ҮЛГІ] Бірінші жұмыс бағыты",
"services.item2": "[ҮЛГІ] Екінші жұмыс бағыты",
"services.item3": "[ҮЛГІ] Үшінші жұмыс бағыты",
"contacts.heading": "Байланыс",
"contacts.text": "[ҮЛГІ] Мұнда бізбен байланысу жолдары болады.",
"contacts.phoneLabel": "Телефон",
"contacts.phone": "[ҮЛГІ] телефон",
"contacts.emailLabel": "Пошта",
"contacts.email": "[ҮЛГІ] пошта",
"contacts.addressLabel": "Мекенжай",
"contacts.address": "[ҮЛГІ] мекенжай",
"footer.text": "[ҮЛГІ] Компания атауы",
"notFound.title": "Бет табылмады",
"notFound.text": "Мұндай бет жоқ. Басты бетке өтіңіз.",
"notFound.link": "Басты бетке"
}

25
locales/ru.json Normal file
View File

@ -0,0 +1,25 @@
{
"meta.title": "[ЗАГЛУШКА] Название компании — сайт-визитка",
"meta.description": "[ЗАГЛУШКА] Краткое описание компании для поисковиков и ссылок",
"site.name": "[ЗАГЛУШКА] Название компании",
"site.tagline": "[ЗАГЛУШКА] Короткий слоган компании",
"about.heading": "Кто мы",
"about.text": "[ЗАГЛУШКА] Здесь будет рассказ о компании: кто мы, сколько лет работаем и почему нам можно доверять.",
"services.heading": "Чем занимаемся",
"services.text": "[ЗАГЛУШКА] Здесь будет описание того, чем занимается компания.",
"services.item1": "[ЗАГЛУШКА] Первое направление работы",
"services.item2": "[ЗАГЛУШКА] Второе направление работы",
"services.item3": "[ЗАГЛУШКА] Третье направление работы",
"contacts.heading": "Контакты",
"contacts.text": "[ЗАГЛУШКА] Здесь будут способы связаться с нами.",
"contacts.phoneLabel": "Телефон",
"contacts.phone": "[ЗАГЛУШКА] телефон",
"contacts.emailLabel": "Почта",
"contacts.email": "[ЗАГЛУШКА] почта",
"contacts.addressLabel": "Адрес",
"contacts.address": "[ЗАГЛУШКА] адрес",
"footer.text": "[ЗАГЛУШКА] Название компании",
"notFound.title": "Страница не найдена",
"notFound.text": "Такой страницы нет. Перейдите на главную страницу.",
"notFound.link": "На главную"
}

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "inat",
"version": "0.1.0",
"private": true,
"type": "module",
"engines": {
"node": ">=22.18"
},
"packageManager": "pnpm@10.34.9",
"scripts": {
"dev": "node src/index.ts",
"start": "node dist/index.js",
"lint": "biome check .",
"typecheck": "tsc --noEmit",
"test": "node --test \"test/**/*.test.ts\"",
"build": "tsc -p tsconfig.build.json"
},
"devDependencies": {
"@biomejs/biome": "^2.6.8",
"@types/node": "^22.20.5",
"typescript": "^7.0.2"
}
}

355
pnpm-lock.yaml generated Normal file
View File

@ -0,0 +1,355 @@
lockfileVersion: '9.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
importers:
.:
devDependencies:
'@biomejs/biome':
specifier: ^2.6.8
version: 2.6.8
'@types/node':
specifier: ^22.20.5
version: 22.20.5
typescript:
specifier: ^7.0.2
version: 7.0.2
packages:
'@biomejs/biome@2.6.8':
resolution: {integrity: sha512-kqZUFvX6/aYQOmzfRN5ANcfNS9wLH4aHLWWP/dQKyhRZVoA6rdWszA7DGugFcxPbMiznUyVYyukVl+ExZN4PQA==}
engines: {node: '>=14.21.3'}
hasBin: true
'@biomejs/cli-android-arm64@2.6.8':
resolution: {integrity: sha512-X4JKPLK85kfUTwuuaORFplnBfGjKkp62PsFhhcL7F/Tpltc7pyUofjlkkMnlZZLWsifFl7LNB8ngVrwJg1uo1Q==}
engines: {node: '>=14.21.3'}
cpu: [arm64]
os: [android]
'@biomejs/cli-android-x64@2.6.8':
resolution: {integrity: sha512-C3Hk485vl+deEj53K08+SYYelPIPkdSZL3qHuD31Xi+qydVa4q2ze+aCYo1k3iJGWtvWtjHaGkDh7S2sX6wk8w==}
engines: {node: '>=14.21.3'}
cpu: [x64]
os: [android]
'@biomejs/cli-darwin-arm64@2.6.8':
resolution: {integrity: sha512-DHyj+JnRHcn4YdvpiqgTFZMYKSPELMCfFpNNlEuu1L8DKTGXhzW7raIwdsbo2Ie9nCljQLAQkVWQk/mwM0ueiA==}
engines: {node: '>=14.21.3'}
cpu: [arm64]
os: [darwin]
'@biomejs/cli-darwin-x64@2.6.8':
resolution: {integrity: sha512-QWWUPjlA0RnkkgYnFLUHZu+RrDc0FdK0A+wEPA+dENQJwsuSHMH89lspnAw2QUWWc9VHyfluGx1Xmp6Y2GZi8Q==}
engines: {node: '>=14.21.3'}
cpu: [x64]
os: [darwin]
'@biomejs/cli-linux-arm64-musl@2.6.8':
resolution: {integrity: sha512-WB9PhOitX+bRc0G3+GNJGlvbKoWn3JoB9z4E04JQizIC9aPCQyIvwvWG7QZHxw6k20Fi+dHLmZ2fSEmBa9HqiA==}
engines: {node: '>=14.21.3'}
cpu: [arm64]
os: [linux]
libc: [musl]
'@biomejs/cli-linux-arm64@2.6.8':
resolution: {integrity: sha512-0Bjim8lKwnrvXUHoI4k3Tb51BjoiLEwDnovCI8nSzkk6wj3dShiuAjrx6+X0Hjf4ejR1sRZgTQiSDHlpy9vB7w==}
engines: {node: '>=14.21.3'}
cpu: [arm64]
os: [linux]
libc: [glibc]
'@biomejs/cli-linux-x64-musl@2.6.8':
resolution: {integrity: sha512-64Yzo+J4fEk7Kf9SMnVdVxC+Y2A2e/L86QTP9JjGQ57lsddyELLpbBcNMxSJggre2T1AvISwwSrOApkBAHDqNA==}
engines: {node: '>=14.21.3'}
cpu: [x64]
os: [linux]
libc: [musl]
'@biomejs/cli-linux-x64@2.6.8':
resolution: {integrity: sha512-XIk3pLQT56I6Tti+u0DF8FtSNXjCMTPbxVTzfJ9cS2t1ImVKvrqV5b52GuEaJg6/wuiXvz0sjfb5saB4TPRjzA==}
engines: {node: '>=14.21.3'}
cpu: [x64]
os: [linux]
libc: [glibc]
'@biomejs/cli-win32-arm64@2.6.8':
resolution: {integrity: sha512-ezjCEZzHIWE2pdh83a7zLVcAAcDaERcTcvfA7uWlxqz/1CuDxgtfO19pDg9B6YY4wKYIDvYBw2QCMnqIshSwmw==}
engines: {node: '>=14.21.3'}
cpu: [arm64]
os: [win32]
'@biomejs/cli-win32-x64@2.6.8':
resolution: {integrity: sha512-cD6HvGczZUiAYsY8s2TxxXj2AHFzEdMomvVJfEjzOjBM45nGqIDfBpbtvwGQzDCN8X2yKKk9u1jnDZisT2nxIg==}
engines: {node: '>=14.21.3'}
cpu: [x64]
os: [win32]
'@types/node@22.20.5':
resolution: {integrity: sha512-U2+DNr+wSjpsTS/wZGYHq7GcwfuSmKiKvoPvK22zwTlRhU91yOniN4qRR5KhIjvif7ysw/dz/hKmfDH0Ris4aA==}
'@typescript/typescript-aix-ppc64@7.0.2':
resolution: {integrity: sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==}
engines: {node: '>=16.20.0'}
cpu: [ppc64]
os: [aix]
'@typescript/typescript-darwin-arm64@7.0.2':
resolution: {integrity: sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==}
engines: {node: '>=16.20.0'}
cpu: [arm64]
os: [darwin]
'@typescript/typescript-darwin-x64@7.0.2':
resolution: {integrity: sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==}
engines: {node: '>=16.20.0'}
cpu: [x64]
os: [darwin]
'@typescript/typescript-freebsd-arm64@7.0.2':
resolution: {integrity: sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==}
engines: {node: '>=16.20.0'}
cpu: [arm64]
os: [freebsd]
'@typescript/typescript-freebsd-x64@7.0.2':
resolution: {integrity: sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==}
engines: {node: '>=16.20.0'}
cpu: [x64]
os: [freebsd]
'@typescript/typescript-linux-arm64@7.0.2':
resolution: {integrity: sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==}
engines: {node: '>=16.20.0'}
cpu: [arm64]
os: [linux]
'@typescript/typescript-linux-arm@7.0.2':
resolution: {integrity: sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==}
engines: {node: '>=16.20.0'}
cpu: [arm]
os: [linux]
'@typescript/typescript-linux-loong64@7.0.2':
resolution: {integrity: sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==}
engines: {node: '>=16.20.0'}
cpu: [loong64]
os: [linux]
'@typescript/typescript-linux-mips64el@7.0.2':
resolution: {integrity: sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==}
engines: {node: '>=16.20.0'}
cpu: [mips64el]
os: [linux]
'@typescript/typescript-linux-ppc64@7.0.2':
resolution: {integrity: sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==}
engines: {node: '>=16.20.0'}
cpu: [ppc64]
os: [linux]
'@typescript/typescript-linux-riscv64@7.0.2':
resolution: {integrity: sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==}
engines: {node: '>=16.20.0'}
cpu: [riscv64]
os: [linux]
'@typescript/typescript-linux-s390x@7.0.2':
resolution: {integrity: sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==}
engines: {node: '>=16.20.0'}
cpu: [s390x]
os: [linux]
'@typescript/typescript-linux-x64@7.0.2':
resolution: {integrity: sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==}
engines: {node: '>=16.20.0'}
cpu: [x64]
os: [linux]
'@typescript/typescript-netbsd-arm64@7.0.2':
resolution: {integrity: sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==}
engines: {node: '>=16.20.0'}
cpu: [arm64]
os: [netbsd]
'@typescript/typescript-netbsd-x64@7.0.2':
resolution: {integrity: sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==}
engines: {node: '>=16.20.0'}
cpu: [x64]
os: [netbsd]
'@typescript/typescript-openbsd-arm64@7.0.2':
resolution: {integrity: sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==}
engines: {node: '>=16.20.0'}
cpu: [arm64]
os: [openbsd]
'@typescript/typescript-openbsd-x64@7.0.2':
resolution: {integrity: sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==}
engines: {node: '>=16.20.0'}
cpu: [x64]
os: [openbsd]
'@typescript/typescript-sunos-x64@7.0.2':
resolution: {integrity: sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==}
engines: {node: '>=16.20.0'}
cpu: [x64]
os: [sunos]
'@typescript/typescript-win32-arm64@7.0.2':
resolution: {integrity: sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==}
engines: {node: '>=16.20.0'}
cpu: [arm64]
os: [win32]
'@typescript/typescript-win32-x64@7.0.2':
resolution: {integrity: sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==}
engines: {node: '>=16.20.0'}
cpu: [x64]
os: [win32]
typescript@7.0.2:
resolution: {integrity: sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==}
engines: {node: '>=16.20.0'}
hasBin: true
undici-types@6.21.0:
resolution: {integrity: sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==}
snapshots:
'@biomejs/biome@2.6.8':
optionalDependencies:
'@biomejs/cli-android-arm64': 2.6.8
'@biomejs/cli-android-x64': 2.6.8
'@biomejs/cli-darwin-arm64': 2.6.8
'@biomejs/cli-darwin-x64': 2.6.8
'@biomejs/cli-linux-arm64': 2.6.8
'@biomejs/cli-linux-arm64-musl': 2.6.8
'@biomejs/cli-linux-x64': 2.6.8
'@biomejs/cli-linux-x64-musl': 2.6.8
'@biomejs/cli-win32-arm64': 2.6.8
'@biomejs/cli-win32-x64': 2.6.8
'@biomejs/cli-android-arm64@2.6.8':
optional: true
'@biomejs/cli-android-x64@2.6.8':
optional: true
'@biomejs/cli-darwin-arm64@2.6.8':
optional: true
'@biomejs/cli-darwin-x64@2.6.8':
optional: true
'@biomejs/cli-linux-arm64-musl@2.6.8':
optional: true
'@biomejs/cli-linux-arm64@2.6.8':
optional: true
'@biomejs/cli-linux-x64-musl@2.6.8':
optional: true
'@biomejs/cli-linux-x64@2.6.8':
optional: true
'@biomejs/cli-win32-arm64@2.6.8':
optional: true
'@biomejs/cli-win32-x64@2.6.8':
optional: true
'@types/node@22.20.5':
dependencies:
undici-types: 6.21.0
'@typescript/typescript-aix-ppc64@7.0.2':
optional: true
'@typescript/typescript-darwin-arm64@7.0.2':
optional: true
'@typescript/typescript-darwin-x64@7.0.2':
optional: true
'@typescript/typescript-freebsd-arm64@7.0.2':
optional: true
'@typescript/typescript-freebsd-x64@7.0.2':
optional: true
'@typescript/typescript-linux-arm64@7.0.2':
optional: true
'@typescript/typescript-linux-arm@7.0.2':
optional: true
'@typescript/typescript-linux-loong64@7.0.2':
optional: true
'@typescript/typescript-linux-mips64el@7.0.2':
optional: true
'@typescript/typescript-linux-ppc64@7.0.2':
optional: true
'@typescript/typescript-linux-riscv64@7.0.2':
optional: true
'@typescript/typescript-linux-s390x@7.0.2':
optional: true
'@typescript/typescript-linux-x64@7.0.2':
optional: true
'@typescript/typescript-netbsd-arm64@7.0.2':
optional: true
'@typescript/typescript-netbsd-x64@7.0.2':
optional: true
'@typescript/typescript-openbsd-arm64@7.0.2':
optional: true
'@typescript/typescript-openbsd-x64@7.0.2':
optional: true
'@typescript/typescript-sunos-x64@7.0.2':
optional: true
'@typescript/typescript-win32-arm64@7.0.2':
optional: true
'@typescript/typescript-win32-x64@7.0.2':
optional: true
typescript@7.0.2:
optionalDependencies:
'@typescript/typescript-aix-ppc64': 7.0.2
'@typescript/typescript-darwin-arm64': 7.0.2
'@typescript/typescript-darwin-x64': 7.0.2
'@typescript/typescript-freebsd-arm64': 7.0.2
'@typescript/typescript-freebsd-x64': 7.0.2
'@typescript/typescript-linux-arm': 7.0.2
'@typescript/typescript-linux-arm64': 7.0.2
'@typescript/typescript-linux-loong64': 7.0.2
'@typescript/typescript-linux-mips64el': 7.0.2
'@typescript/typescript-linux-ppc64': 7.0.2
'@typescript/typescript-linux-riscv64': 7.0.2
'@typescript/typescript-linux-s390x': 7.0.2
'@typescript/typescript-linux-x64': 7.0.2
'@typescript/typescript-netbsd-arm64': 7.0.2
'@typescript/typescript-netbsd-x64': 7.0.2
'@typescript/typescript-openbsd-arm64': 7.0.2
'@typescript/typescript-openbsd-x64': 7.0.2
'@typescript/typescript-sunos-x64': 7.0.2
'@typescript/typescript-win32-arm64': 7.0.2
'@typescript/typescript-win32-x64': 7.0.2
undici-types@6.21.0: {}

67
src/i18n.ts Normal file
View File

@ -0,0 +1,67 @@
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
export const LOCALES = ["ru", "kk", "en"] as const;
export type Locale = (typeof LOCALES)[number];
export const DEFAULT_LOCALE: Locale = "ru";
/** Keys every locale file must contain. */
export const REQUIRED_KEYS = [
"meta.title",
"meta.description",
"site.name",
"site.tagline",
"about.heading",
"about.text",
"services.heading",
"services.text",
"services.item1",
"services.item2",
"services.item3",
"contacts.heading",
"contacts.text",
"contacts.phoneLabel",
"contacts.phone",
"contacts.emailLabel",
"contacts.email",
"contacts.addressLabel",
"contacts.address",
"footer.text",
"notFound.title",
"notFound.text",
"notFound.link",
] as const;
export type MessageKey = (typeof REQUIRED_KEYS)[number];
export type Messages = Record<MessageKey, string>;
/** Locale files live in <project>/locales both from src/ and from dist/. */
export const LOCALES_DIR = fileURLToPath(new URL("../locales/", import.meta.url));
/** Returns problems with a locale dictionary: missing, empty or unknown keys. */
export function validateMessages(data: unknown): string[] {
if (typeof data !== "object" || data === null || Array.isArray(data)) {
return ["file must contain a JSON object"];
}
const record = data as Record<string, unknown>;
const problems: string[] = [];
for (const key of REQUIRED_KEYS) {
const value = record[key];
if (value === undefined) problems.push(`missing key ${key}`);
else if (typeof value !== "string" || value.trim() === "") problems.push(`empty key ${key}`);
}
const known = new Set<string>(REQUIRED_KEYS);
for (const key of Object.keys(record)) {
if (!known.has(key)) problems.push(`unknown key ${key}`);
}
return problems;
}
export function loadMessages(locale: Locale, dir: string = LOCALES_DIR): Messages {
const file = `${dir}/${locale}.json`;
const data: unknown = JSON.parse(readFileSync(file, "utf8"));
const problems = validateMessages(data);
if (problems.length > 0) {
throw new Error(`Invalid locale file ${file}: ${problems.join(", ")}`);
}
return data as Messages;
}

15
src/index.ts Normal file
View File

@ -0,0 +1,15 @@
import { createApp } from "./server.ts";
const port = Number(process.env.PORT ?? 3000);
const server = createApp();
server.listen(port, () => {
console.log(`inat: listening on port ${port}`);
});
for (const signal of ["SIGTERM", "SIGINT"] as const) {
process.on(signal, () => {
server.close(() => process.exit(0));
setTimeout(() => process.exit(0), 5000).unref();
});
}

101
src/render.ts Normal file
View File

@ -0,0 +1,101 @@
import type { Locale, Messages } from "./i18n.ts";
const HTML_ESCAPES: Record<string, string> = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39;",
};
export function escapeHtml(value: string): string {
return value.replace(/[&<>"']/g, (ch) => HTML_ESCAPES[ch] ?? ch);
}
const STYLES = `
*,*::before,*::after{box-sizing:border-box}
html{-webkit-text-size-adjust:100%;text-size-adjust:100%}
body{margin:0;font-family:system-ui,-apple-system,"Segoe UI",Roboto,Arial,sans-serif;
font-size:1.0625rem;line-height:1.6;color:#1f2328;background:#fff;
overflow-wrap:anywhere;word-break:normal}
.container{width:100%;max-width:46rem;margin:0 auto;padding:0 1rem}
header{padding:2.5rem 0 1.5rem;border-bottom:1px solid #d0d7de}
h1{margin:0 0 .5rem;font-size:clamp(1.5rem,5vw,2.25rem);line-height:1.2}
h2{margin:0 0 .75rem;font-size:clamp(1.25rem,4vw,1.6rem);line-height:1.25}
.tagline{margin:0;color:#57606a}
section{padding:1.75rem 0;border-bottom:1px solid #eaeef2}
p{margin:0 0 .75rem}
ul{margin:0;padding-left:1.25rem}
dl{margin:0;display:grid;grid-template-columns:minmax(0,max-content) minmax(0,1fr);gap:.25rem 1rem}
dt{font-weight:600}
dd{margin:0}
footer{padding:1.5rem 0 2.5rem;color:#57606a;font-size:.9375rem}
@media (max-width:30rem){dl{grid-template-columns:minmax(0,1fr)}dd{margin-bottom:.5rem}}
`.trim();
function layout(locale: Locale, title: string, description: string, body: string): string {
return `<!doctype html>
<html lang="${locale}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${escapeHtml(title)}</title>
<meta name="description" content="${escapeHtml(description)}">
<style>${STYLES}</style>
</head>
<body>
${body}
</body>
</html>
`;
}
export function renderHomePage(locale: Locale, m: Messages): string {
const e = escapeHtml;
const body = `<header>
<div class="container">
<h1>${e(m["site.name"])}</h1>
<p class="tagline">${e(m["site.tagline"])}</p>
</div>
</header>
<main class="container">
<section id="about" aria-labelledby="about-heading">
<h2 id="about-heading">${e(m["about.heading"])}</h2>
<p>${e(m["about.text"])}</p>
</section>
<section id="services" aria-labelledby="services-heading">
<h2 id="services-heading">${e(m["services.heading"])}</h2>
<p>${e(m["services.text"])}</p>
<ul>
<li>${e(m["services.item1"])}</li>
<li>${e(m["services.item2"])}</li>
<li>${e(m["services.item3"])}</li>
</ul>
</section>
<section id="contacts" aria-labelledby="contacts-heading">
<h2 id="contacts-heading">${e(m["contacts.heading"])}</h2>
<p>${e(m["contacts.text"])}</p>
<dl>
<dt>${e(m["contacts.phoneLabel"])}</dt><dd>${e(m["contacts.phone"])}</dd>
<dt>${e(m["contacts.emailLabel"])}</dt><dd>${e(m["contacts.email"])}</dd>
<dt>${e(m["contacts.addressLabel"])}</dt><dd>${e(m["contacts.address"])}</dd>
</dl>
</section>
</main>
<footer>
<div class="container">${e(m["footer.text"])}</div>
</footer>`;
return layout(locale, m["meta.title"], m["meta.description"], body);
}
export function renderNotFoundPage(locale: Locale, m: Messages): string {
const e = escapeHtml;
const body = `<main class="container">
<section>
<h1>${e(m["notFound.title"])}</h1>
<p>${e(m["notFound.text"])}</p>
<p><a href="/">${e(m["notFound.link"])}</a></p>
</section>
</main>`;
return layout(locale, m["notFound.title"], m["meta.description"], body);
}

38
src/server.ts Normal file
View File

@ -0,0 +1,38 @@
import { createServer, type Server } from "node:http";
import { DEFAULT_LOCALE, type Locale, loadMessages } from "./i18n.ts";
import { renderHomePage, renderNotFoundPage } from "./render.ts";
const HTML = "text/html; charset=utf-8";
export function createApp(locale: Locale = DEFAULT_LOCALE): Server {
const messages = loadMessages(locale);
const home = renderHomePage(locale, messages);
const notFound = renderNotFoundPage(locale, messages);
return createServer((req, res) => {
const path = new URL(req.url ?? "/", "http://localhost").pathname;
const method = req.method ?? "GET";
const headOnly = method === "HEAD";
if (method !== "GET" && method !== "HEAD") {
res.writeHead(405, { Allow: "GET, HEAD", "Content-Type": "text/plain; charset=utf-8" });
res.end("Method Not Allowed");
return;
}
if (path === "/healthz") {
res.writeHead(200, {
"Content-Type": "text/plain; charset=utf-8",
"Cache-Control": "no-store",
});
res.end(headOnly ? undefined : "ok");
return;
}
if (path === "/" || path === "/index.html") {
res.writeHead(200, { "Content-Type": HTML, "Cache-Control": "no-cache" });
res.end(headOnly ? undefined : home);
return;
}
res.writeHead(404, { "Content-Type": HTML });
res.end(headOnly ? undefined : notFound);
});
}

9
tsconfig.build.json Normal file
View File

@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}

18
tsconfig.json Normal file
View File

@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "es2023",
"module": "nodenext",
"moduleResolution": "nodenext",
"strict": true,
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"verbatimModuleSyntax": true,
"erasableSyntaxOnly": true,
"allowImportingTsExtensions": true,
"rewriteRelativeImportExtensions": true,
"types": ["node"],
"skipLibCheck": true,
"noEmit": true
},
"include": ["src", "test"]
}