1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-30 13:58:08 +00:00
This commit is contained in:
Nebel 2024-04-20 01:21:32 +09:00
parent 07d437da3d
commit acabdaec8c
Signed by: nebel
GPG key ID: 79807D08C6EF6460
11 changed files with 2812 additions and 0 deletions

72
kita/README.md Normal file
View file

@ -0,0 +1,72 @@
# kita
## Installing the project
Run the following command to install the project:
```bash
npm install
```
## Running in development mode
Run the following command to start the server in development mode:
```bash
npm run dev
```
You can now open your browser and navigate to [`http://localhost:1227`](http://localhost:1227).
## Building for production
Run the following command to build the project:
```bash
npm run build
```
Run the following command to start the server in production mode:
```bash
npm start
```
You can now open your browser and navigate to [`http://localhost:1227`](http://localhost:1227).
## Running tests
You can run the tests using the following command:
```bash
npm test
```
## Environment variables
Environment variables are loaded from a [`.env`](./.env) file in the root of the project. These are the available
variables:
```bash
# Port and host for the server
PORT=1227
HOST=0.0.0.0
```
## Linting and Formatting
You can run the following commands to lint and format your code:
```bash
# Formats your code
npm run format
# Lints your code
npm run lint
# Lints and fixes your code
npm run lint:fix
# Lints your code in CI mode
npm run lint:ci
```

25
kita/biome.json Normal file
View file

@ -0,0 +1,25 @@
{
"$schema": "https://biomejs.dev/schemas/1.6.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"lineWidth": 100,
"indentStyle": "space"
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"trailingComma": "none"
}
},
"files": {
"ignore": ["node_modules", "dist", "coverage"]
}
}

44
kita/package.json Normal file
View file

@ -0,0 +1,44 @@
{
"name": "kita",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "kita build && tsc -p tsconfig.build.json",
"dev": "concurrently --raw --restart-tries 0 \"npm:dev:*\"",
"dev:kita": "kita watch",
"dev:server": "node --env-file=.env --enable-source-maps -r @swc-node/register --watch src/index.ts",
"dev:tsc": "tsc -p tsconfig.build.json --watch --preserveWatchOutput",
"format": "biome format --write .",
"lint": "biome check .",
"lint:ci": "biome ci .",
"lint:fix": "biome check --apply-unsafe .",
"start": "node --env-file=.env --enable-source-maps dist/index.js",
"test": "kita build && node --env-file=.env --enable-source-maps -r @swc-node/register --test test/**/*.test.ts",
"test:types": "kita build --dry-run && tsc --noEmit"
},
"dependencies": {
"@fastify/helmet": "^11.1.1",
"@fastify/multipart": "^8.2.0",
"@fastify/sensible": "^5.5.0",
"@fastify/under-pressure": "^8.3.0",
"@kitajs/runtime": "^1.1.17",
"close-with-grace": "^1.3.0",
"fastify": "^4.26.2",
"fastify-plugin": "^4.5.1",
"pino": "^8.19.0",
"tslib": "^2.6.2"
},
"devDependencies": {
"@biomejs/biome": "1.6.3",
"@kitajs/cli": "1.1.34",
"@swc-node/register": "^1.9.0",
"@swc/helpers": "^0.5.8",
"@types/node": "^20.12.2",
"concurrently": "^8.2.2",
"pino-pretty": "^11.0.0",
"typescript": "^5.4.3"
},
"engines": {
"node": ">=20"
}
}

2427
kita/pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load diff

31
kita/src/index.ts Normal file
View file

@ -0,0 +1,31 @@
import { ajvFilePlugin } from '@fastify/multipart';
import fastify from 'fastify';
import { isMainThread } from 'node:worker_threads';
import backendPlugin from './plugin';
// Ensures this file is not executed in test context
if (process.env.NODE_TEST_CONTEXT) {
throw new Error('This file should not be executed in test context');
}
// Ensures this file is not executed in worker context
if (!isMainThread) {
throw new Error('This file should not be executed in worker context');
}
// Ensures PORT are set
if (!process.env.PORT) {
throw new Error('PORT must be set');
}
fastify({
logger: { transport: { target: 'pino-pretty' } },
ajv: { plugins: [ajvFilePlugin] }
})
// Registers our backend
.register(backendPlugin)
// Starts the server
.listen({
port: +process.env.PORT,
host: process.env.HOST || ''
});

43
kita/src/plugin.ts Normal file
View file

@ -0,0 +1,43 @@
import './prelude';
import fastifyHelmet from '@fastify/helmet';
import fastifyUnderPressure from '@fastify/under-pressure';
import { Kita } from '@kitajs/runtime';
import closeWithGrace from 'close-with-grace';
import fp from 'fastify-plugin';
export default fp(async (app) => {
// Registers the generated kita plugin
app.register(Kita);
// Measures process load with automatic handling of "Service Unavailable"
app.register(fastifyUnderPressure, {
maxEventLoopDelay: 1000,
maxHeapUsedBytes: 1000000000,
maxRssBytes: 1000000000,
maxEventLoopUtilization: 0.98
});
// Important security headers for Fastify
app.register(fastifyHelmet, {
global: true
});
// Add your custom stuff here
// app.register(myPlugin)
// ...
// Delay is the number of milliseconds for the graceful close to finish
const closeListeners = closeWithGrace({ delay: 500 }, async ({ err }) => {
if (err) {
app.log.error(err);
}
await app.close();
});
// Cancelling the close listeners
app.addHook('onClose', async () => {
closeListeners.uninstall();
});
});

2
kita/src/prelude.ts Normal file
View file

@ -0,0 +1,2 @@
// This tells kita where to find the root of your project
globalThis.KITA_PROJECT_ROOT ??= __dirname;

13
kita/src/routes/index.ts Normal file
View file

@ -0,0 +1,13 @@
import type { Query } from '@kitajs/runtime';
/**
* @tag Hello
* @operationId getHello
* @summary Get a hello message with date
*/
export function get(name: Query = 'World') {
return {
name,
message: `Hello ${name}!`
};
}

39
kita/test/index.test.ts Normal file
View file

@ -0,0 +1,39 @@
import fastify from 'fastify';
import assert from 'node:assert';
import test, { describe } from 'node:test';
import backendPlugin from '../src/plugin';
describe('Creates route', () => {
test('GET /', async () => {
await using app = fastify();
app.register(backendPlugin);
const response = await app.inject({
method: 'GET',
url: '/'
});
assert.strictEqual(response.statusCode, 200);
assert.deepStrictEqual(response.json(), {
name: 'World',
message: 'Hello World!'
});
});
test('GET /?name=Kita', async () => {
await using app = fastify();
app.register(backendPlugin);
const response = await app.inject({
method: 'GET',
url: '/',
query: { name: 'Kita' }
});
assert.strictEqual(response.statusCode, 200);
assert.deepStrictEqual(response.json(), {
name: 'Kita',
message: 'Hello Kita!'
});
});
});

4
kita/tsconfig.build.json Normal file
View file

@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["src"]
}

112
kita/tsconfig.json Normal file
View file

@ -0,0 +1,112 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
"incremental": true,
// "composite": true,
// "tsBuildInfoFile": "./.tsbuildinfo",
// "disableSourceOfProjectReferenceRedirect": true,
// "disableSolutionSearching": true,
// "disableReferencedProjectLoad": true,
/* Language and Environment */
"target": "ESNext",
// "lib": [],
// "jsx": "preserve",
// "experimentalDecorators": true,
// "emitDecoratorMetadata": true,
// "jsxFactory": "",
// "jsxFragmentFactory": "",
// "jsxImportSource": "",
// "reactNamespace": "",
// "noLib": true,
// "useDefineForClassFields": true,
// "moduleDetection": "auto", ,
"preserveWatchOutput": true,
/* Modules */
"module": "CommonJS",
// "rootDir": "./",
"moduleResolution": "Node10",
// "baseUrl": "./",
// "paths": {},
// "rootDirs": [],
// "typeRoots": [],
// "types": [],
// "allowUmdGlobalAccess": true,
// "moduleSuffixes": [],
// "allowImportingTsExtensions": true,
// "resolvePackageJsonExports": true,
// "resolvePackageJsonImports": true,
// "customConditions": [],
// "resolveJsonModule": true,
// "allowArbitraryExtensions": true,
// "noResolve": true,
/* JavaScript Support */
// "allowJs": true,
// "checkJs": true,
// "maxNodeModuleJsDepth": 1,
/* Emit */
// "declaration": true,
// "declarationMap": true,
// "emitDeclarationOnly": true,
"sourceMap": true,
// "inlineSourceMap": true,
// "outFile": "./",
"outDir": "./dist",
// "removeComments": true,
// "noEmit": true,
"importHelpers": true,
// "importsNotUsedAsValues": "remove",
// "downlevelIteration": true,
// "sourceRoot": "",
// "mapRoot": "",
// "inlineSources": true,
// "emitBOM": true,
// "newLine": "crlf",
// "stripInternal": true,
// "noEmitHelpers": true,
// "noEmitOnError": true,
// "preserveConstEnums": true,
// "declarationDir": "./",
// "preserveValueImports": true,
/* Interop Constraints */
"isolatedModules": true,
"verbatimModuleSyntax": false,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"preserveSymlinks": true,
"forceConsistentCasingInFileNames": true,
/* Type Checking */
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"useUnknownInCatchVariables": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"exactOptionalPropertyTypes": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
// "noPropertyAccessFromIndexSignature": true,
// "allowUnusedLabels": true,
// "allowUnreachableCode": true,
/* Completeness */
"skipDefaultLibCheck": true,
"skipLibCheck": true
},
"include": ["src", "test"]
}