Skip to content

Commit

Permalink
chore: lint code
Browse files Browse the repository at this point in the history
  • Loading branch information
chizukicn committed Jan 31, 2024
1 parent 761cbaf commit 2e2480d
Show file tree
Hide file tree
Showing 123 changed files with 3,712 additions and 4,294 deletions.
6 changes: 0 additions & 6 deletions .eslintrc

This file was deleted.

3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ node_modules
.temp
.tmp
.cache
.vscode

# Yarn
**/.yarn/cache
Expand Down Expand Up @@ -38,8 +37,6 @@ coverage
*.lcov
.nyc_output

# VSCode
.vscode

# Intellij idea
*.iml
Expand Down
36 changes: 36 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
// Enable the ESlint flat config support
"eslint.experimental.useFlatConfig": true,

// Disable the default formatter, use eslint instead
"prettier.enable": false,
"editor.formatOnSave": false,

// Auto fix
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},

// Enable eslint for all supported languages
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"vue",
"html",
"markdown",
"json",
"jsonc",
"yaml",
"toml"
],

"pair-diff.patterns": [
{
"source": "./fixtures/output/**/*.*",
"target": "./fixtures/input/<base>"
}
]
}
21 changes: 4 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<h1 align="center">Fourze</h1>
<p align="center"> Api route framework for the browser and node.js.</p>


`pnpm add @fourze/core`

create a router file `src/mock/example.ts`

```ts
import { defineRouter } from "@fourze/core";
export default defineRouter(router => {
export default defineRouter((router) => {
router.get("/hello", () => {
return "hello,world";
});
});

```

configure vite config
Expand All @@ -29,15 +27,13 @@ export default defineConfig({
})
],
});

```

then you can fetch `/api/hello` to get response.

# Features
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fchizukicn%2Ffourze.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fchizukicn%2Ffourze?ref=badge_shield)


- Simple api route register

- Mock XHR/fetch/http.request response
Expand All @@ -46,7 +42,6 @@ then you can fetch `/api/hello` to get response.

- Node.js and browser support


# Development
```shell
pnpm install
Expand All @@ -67,7 +62,6 @@ export default defineNuxtConfig({
base: "/api"
}
});

```

# Node.js Server
Expand All @@ -81,7 +75,7 @@ import { createServer } from "@fourze/server";
const server = createServer({
base: "/api"
});
server.use(defineRouter(router => {
server.use(defineRouter((router) => {
router.get("/hello", (_, res) => {
res.send("hello,world");
});
Expand All @@ -100,7 +94,6 @@ const middleware = createServer({
const app = express();
app.use(middleware);
app.listen(7609);

```

# Register Router
Expand All @@ -109,7 +102,7 @@ src/mock/example.ts

```ts
import { defineRouter } from "@fourze/core";
export default defineRouter(router => {
export default defineRouter((router) => {
// base = '/api'
router.post("/user/{id}", (req) => {
return {
Expand All @@ -118,7 +111,6 @@ export default defineRouter(router => {
};
});
});

```

Set `base` to `/api` in vite/nuxt config, then you can fetch `/api/user/1` to get response.
Expand All @@ -131,7 +123,6 @@ Response

`{"id":"1","name":"test"}`


# Thanks
This project is heavily inspired by the following awesome projects.

Expand All @@ -151,9 +142,5 @@ This project is heavily inspired by the following awesome projects.

- [jiti](https://github.com/unjs/jiti.git)





## License
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fchizukicn%2Ffourze.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fchizukicn%2Ffourze?ref=badge_large)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fchizukicn%2Ffourze.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fchizukicn%2Ffourze?ref=badge_large)
1 change: 0 additions & 1 deletion alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ export const alias: Record<string, string> = Object.keys(paths).reduce(
},
{}
);

5 changes: 3 additions & 2 deletions bench/compare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { arch, cpus, platform, totalmem } from "node:os";
import { join } from "node:path";
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
import process from "node:process";
import { program } from "commander";
import Table from "cli-table";
import chalk from "chalk";
Expand Down Expand Up @@ -31,9 +32,9 @@ if (!getAvailableResults().length) {

function getAvailableResults() {
return readdirSync(resultsPath)
.filter((file) => file.match(/(.+)\.json$/))
.filter(file => file.match(/(.+)\.json$/))
.sort()
.map((choice) => choice.replace(".json", ""));
.map(choice => choice.replace(".json", ""));
}

function formatHasRouter(hasRouter: boolean | string) {
Expand Down
5 changes: 3 additions & 2 deletions bench/lib/autocannon.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { access, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import process from "node:process";
import autocannon from "autocannon";

const resultsDirectory = join(process.cwd(), "results");

const run = (opts: autocannon.Options = { url: "http://localhost:3000" }) => {
function run(opts: autocannon.Options = { url: "http://localhost:3000" }) {
return new Promise<any>((resolve, reject) => {
opts.url = "http://localhost:3000";
autocannon(opts, (err, result) => {
Expand All @@ -15,7 +16,7 @@ const run = (opts: autocannon.Options = { url: "http://localhost:3000" }) => {
}
});
});
};
}

export async function fire(
opts: autocannon.Options,
Expand Down
8 changes: 4 additions & 4 deletions bench/lib/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fire } from "./autocannon";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

const doBench = async (opts: any, handler: string) => {
async function doBench(opts: any, handler: string) {
const spinner = ora(`Started ${handler}`).start();
const forked = fork(join(__dirname, "../modules", `${handler}.js`));
try {
Expand All @@ -30,10 +30,10 @@ const doBench = async (opts: any, handler: string) => {
} catch (error) {
return console.error(error);
}
};
}

let index = 0;
const start = async (opts: any, module: string[] | string): Promise<any> => {
async function start(opts: any, module: string[] | string): Promise<any> {
if (typeof module === "string") {
module = [module];
}
Expand All @@ -49,6 +49,6 @@ const start = async (opts: any, module: string[] | string): Promise<any> => {
} catch (error) {
console.error(error);
}
};
}

export default start;
13 changes: 7 additions & 6 deletions bench/lib/packages.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { createRequire } from "node:module";
import path from "node:path";
import process from "node:process";
import pkgJson from "../package.json";

const packages: Record<string, {
checked?: boolean
hasRouter?: boolean
package?: string
version?: string
extra?: boolean
checked?: boolean;
hasRouter?: boolean;
package?: string;
version?: string;
extra?: boolean;
}> = {
"fourze": { checked: true, hasRouter: true, package: "@fourze/core" },
"fourze-router": { checked: true, hasRouter: true, package: "@fourze/core" },
Expand Down Expand Up @@ -46,7 +47,7 @@ export function list(extra = false) {
: null;
return null;
})
.filter((c) => c);
.filter(c => c);
}
export function info(module: string) {
return packages[module];
Expand Down
7 changes: 3 additions & 4 deletions bench/lib/suite.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import consola from "consola";

export const suite = async (name: string, frequency: number, fn: () => void | Promise<void>) => {
export async function suite(name: string, frequency: number, fn: () => void | Promise<void>) {
const start = performance.now();
const list = new Array(frequency).fill(0);
const list = Array.from({ length: frequency }).fill(0);
for (let i = 0; i < frequency; i++) {
list[i] = fn();
}
await Promise.all(list);
const end = performance.now();
consola.log(`suit-${name}`, `time: ${(end - start).toFixed(3)}ms`);
};

}
1 change: 0 additions & 1 deletion bench/modules/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ app.get("/", (req, res) => {
});
});
app.listen(3000);

1 change: 1 addition & 0 deletions bench/modules/fastify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fastify from "fastify";

const app = fastify();

const schema = {
Expand Down
3 changes: 2 additions & 1 deletion bench/modules/fourze-router.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import process from "node:process";
import { defineRouter } from "@fourze/core";

import { createServer } from "@fourze/server";

const router = defineRouter(router => {
const router = defineRouter((router) => {
router.get("/", () => {
return {
hello: "world"
Expand Down
1 change: 1 addition & 0 deletions bench/modules/fourze.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createServer } from "node:http";
import process from "node:process";
import { connect } from "@fourze/server";
import { createApp } from "@fourze/core";

Expand Down
4 changes: 3 additions & 1 deletion bench/modules/koa.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import process from "node:process";
import Koa from "koa";

const app = new Koa();

app.use(ctx => {
app.use((ctx) => {
ctx.body = { hello: "world" };
});

Expand Down
1 change: 1 addition & 0 deletions bench/modules/nodehttp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import http from "node:http";
import process from "node:process";

const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "application/json" });
Expand Down
2 changes: 1 addition & 1 deletion bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"connect": "^3.7.0",
"consola": "^3.2.3",
"express": "^4.18.2",
"fastify": "^4.25.2",
"fastify": "^4.26.0",
"h3": "^1.10.1",
"inquirer": "^9.2.13",
"koa": "^2.15.0"
Expand Down
9 changes: 5 additions & 4 deletions bench/run.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import process from "node:process";
import inquirer from "inquirer";
import { program } from "commander";
import { choices, list } from "./lib/packages";
import bench from "./lib/bench";

let argv: string[] = [];

const run = async () => {
async function run() {
const options = await getBenchmarkOptions();
const modules = options.all ? choices : await select();
return bench(options, modules);
};
}

argv = program
.option("-t --target <module>", "module to benchmark")
Expand All @@ -27,15 +28,15 @@ argv = program
}
}).parse(process.argv).args;

const parseArgv = async () => {
async function parseArgv() {
const [all, connections, pipelining, duration] = argv;
return {
all: all === "y",
connections: +connections,
pipelining: +pipelining,
duration: +duration
};
};
}

async function getBenchmarkOptions() {
if (argv.length) {
Expand Down
Loading

0 comments on commit 2e2480d

Please sign in to comment.