Skip to content

Commit 344df6a

Browse files
committed
refactor(server): remove polka and use find-my-way + native HTTP
Update workspaces/server/src/endpoints/report.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Update workspaces/server/src/endpoints/util/send.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> chore: fix review
1 parent 393b4f6 commit 344df6a

File tree

25 files changed

+435
-277
lines changed

25 files changed

+435
-277
lines changed

src/commands/http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export async function start(
6464
});
6565

6666
httpServer.listen(httpPort, async() => {
67-
const link = `http://localhost:${httpServer.server.address().port}`;
67+
const link = `http://localhost:${httpServer.address().port}`;
6868
console.log(kleur.magenta().bold(await i18n.getToken("cli.http_server_started")), kleur.cyan().bold(link));
6969

7070
open(link);
@@ -77,7 +77,7 @@ export async function start(
7777

7878
for (const eventName of ["SIGINT", "SIGTERM"]) {
7979
process.on(eventName, () => {
80-
httpServer.server.close();
80+
httpServer.close();
8181

8282
console.log(kleur.red().bold(`${eventName} signal received.`));
8383
process.exit(0);

test/helpers/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export function getExpectedScorecardLines(pkgName, body) {
1+
export function getExpectedScorecardLines(packageName, body) {
22
const { date, score: scorePkg, checks } = body;
33

44
const expectedLines = [
55
"",
66
" OSSF Scorecard",
77
"",
8-
mockScorecardCliLine("Repository", pkgName),
8+
mockScorecardCliLine("Repository", packageName),
99
mockScorecardCliLine("Scan at", date),
1010
mockScorecardCliLine("Score", scorePkg),
1111
"--------------------------------------------------------------------------------"

workspaces/server/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,31 @@ Get the HTML description for a specific flag.
126126
**Params**:
127127
- `title`: The flag name.
128128
129-
- `GET /bundle/:pkgName`
129+
- `GET /bundle/:packageName`
130130
Get bundle size information for a package from Bundlephobia.
131131
132132
**Params**:
133-
- `pkgName`: The npm package name.
133+
- `packageName`: The npm package name.
134134
135-
- `GET /bundle/:pkgName/:version`
135+
- `GET /bundle/:packageName/:version`
136136
Get bundle size information for a specific version of a package from Bundlephobia.
137137
138138
**Params**:
139-
- `pkgName`: The npm package name.
139+
- `packageName`: The npm package name.
140140
- `version`: The package version.
141141
142-
- `GET /downloads/:pkgName`
142+
- `GET /downloads/:packageName`
143143
Get npm download statistics for the last week for a package.
144144
145145
**Params**:
146-
- `pkgName`: The npm package name.
146+
- `packageName`: The npm package name.
147147
148-
- `GET /scorecard/:org/:pkgName`
148+
- `GET /scorecard/:org/:packageName`
149149
Get OSSF Scorecard results for a package repository.
150150
151151
**Params**:
152152
- `org`: The organization or user.
153-
- `pkgName`: The repository name.
153+
- `packageName`: The repository name.
154154
155155
**Query**:
156156
`platform` (*optional*): The platform (default: `github.com`).

workspaces/server/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@
1818
"author": "GENTILHOMME Thomas <gentilhomme.thomas@gmail.com>",
1919
"license": "MIT",
2020
"devDependencies": {
21-
"@polka/send-type": "0.5.2",
22-
"@types/polka": "^0.5.7",
2321
"@types/server-destroy": "^1.0.4",
24-
"@types/ws": "^8.18.1"
22+
"@types/ws": "^8.18.1",
23+
"server-destroy": "1.0.1"
2524
},
2625
"dependencies": {
2726
"@nodesecure/cache": "1.0.0",
2827
"cacache": "20.0.3",
2928
"chokidar": "5.0.0",
29+
"find-my-way": "9.3.0",
3030
"glob": "13.0.0",
3131
"pino": "10.1.0",
3232
"pino-pretty": "13.1.2",
33-
"polka": "0.5.2",
34-
"server-destroy": "1.0.1",
3533
"sirv": "3.0.2",
3634
"ts-pattern": "5.9.0",
3735
"ws": "8.18.3",

workspaces/server/src/ALS.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
import { AsyncLocalStorage } from "node:async_hooks";
33

44
// Import Internal Dependencies
5-
import type { AyncStoreContext } from "./middlewares/context.ts";
5+
import type { ViewBuilder } from "./ViewBuilder.class.ts";
66

7-
export const context = new AsyncLocalStorage<AyncStoreContext>();
7+
export type NestedStringRecord = {
8+
[key: string]: string | NestedStringRecord;
9+
};
10+
11+
export interface AsyncStoreContext {
12+
dataFilePath?: string;
13+
i18n: {
14+
english: NestedStringRecord;
15+
french: NestedStringRecord;
16+
};
17+
viewBuilder: ViewBuilder;
18+
}
19+
20+
export const context = new AsyncLocalStorage<AsyncStoreContext>();
Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
// Import Node.js Dependencies
2+
import type {
3+
IncomingMessage,
4+
ServerResponse
5+
} from "node:http";
6+
17
// Import Third-party Dependencies
28
import * as httpie from "@openally/httpie";
3-
import send from "@polka/send-type";
4-
import type { Request, Response } from "express-serve-static-core";
9+
10+
// Import Internal Dependencies
11+
import { send } from "./util/send.ts";
512

613
// CONSTANTS
714
const kBaseBundlePhobiaUrl = "https://bundlephobia.com/api";
@@ -15,21 +22,34 @@ interface BundlePhobiaResponse {
1522
}[];
1623
}
1724

18-
export async function get(req: Request, res: Response) {
19-
const { pkgName, version } = req.params;
25+
export async function get(
26+
_: IncomingMessage,
27+
res: ServerResponse,
28+
params: Record<string, string | undefined>
29+
) {
30+
const { packageName, version } = params;
31+
if (!packageName) {
32+
return send(res, {
33+
error: "Package name is missing."
34+
}, { code: 400 });
35+
}
2036

21-
const pkgTemplate = version ? `${pkgName.replaceAll("%2F", "/")}@${version}` : pkgName;
37+
const pkgTemplate = version ?
38+
`${packageName.replaceAll("%2F", "/")}@${version}` :
39+
packageName;
2240
try {
2341
const { data } = await httpie.get<BundlePhobiaResponse>(`${kBaseBundlePhobiaUrl}/size?package=${pkgTemplate}`);
2442
const { gzip, size, dependencySizes } = data;
2543

26-
return send(res, 200, {
44+
return send(res, {
2745
gzip,
2846
size,
2947
dependencySizes
3048
});
3149
}
3250
catch (error: any) {
33-
return send(res, error.statusCode, { error: error.statusMessage });
51+
return send(res, { error: error.statusMessage }, {
52+
code: error.statusCode ?? 500
53+
});
3454
}
3555
}
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1+
// Import Node.js Dependencies
2+
import type {
3+
IncomingMessage,
4+
ServerResponse
5+
} from "node:http";
6+
17
// Import Third-party Dependencies
2-
import send from "@polka/send-type";
3-
import type { Request, Response } from "express-serve-static-core";
8+
import type { AppConfig } from "@nodesecure/cache";
49

510
// Import Internal Dependencies
611
import * as config from "../config.ts";
7-
import { bodyParser } from "../middlewares/bodyParser.ts";
12+
import { bodyParser } from "./util/bodyParser.ts";
13+
import { send } from "./util/send.ts";
814

9-
export async function get(_req: Request, res: Response) {
15+
export async function get(
16+
_req: IncomingMessage,
17+
res: ServerResponse
18+
) {
1019
const result = await config.get();
1120

12-
send(res, 200, result);
21+
send(res, result);
1322
}
1423

15-
export async function save(req: Request, res: Response) {
16-
const data = await bodyParser(req);
24+
export async function save(
25+
req: IncomingMessage,
26+
res: ServerResponse
27+
) {
28+
const data = await bodyParser<AppConfig>(req);
1729
await config.set(data);
1830

19-
send(res, 204);
31+
res.statusCode = 204;
32+
res.end();
2033
}

workspaces/server/src/endpoints/data.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
// Import Node.js Dependencies
22
import fs from "node:fs";
33
import path from "node:path";
4-
5-
// Import Third-party Dependencies
6-
import send from "@polka/send-type";
7-
import type { Request, Response } from "express-serve-static-core";
4+
import type {
5+
IncomingMessage,
6+
ServerResponse
7+
} from "node:http";
88

99
// Import Internal Dependencies
1010
import { context } from "../ALS.ts";
1111
import { logger } from "../logger.ts";
1212
import { cache } from "../cache.ts";
13+
import { send } from "./util/send.ts";
1314

1415
// CONSTANTS
1516
const kDefaultPayloadPath = path.join(process.cwd(), "nsecure-result.json");
1617

17-
export async function get(_req: Request, res: Response) {
18+
export async function get(
19+
_req: IncomingMessage,
20+
res: ServerResponse
21+
) {
1822
if (cache.startFromZero) {
1923
logger.info("[data|get](no content)");
20-
send(res, 204);
24+
res.statusCode = 204;
25+
res.end();
2126

2227
return;
2328
}
@@ -27,7 +32,7 @@ export async function get(_req: Request, res: Response) {
2732
logger.info(`[data|get](current: ${current})`);
2833
logger.debug(`[data|get](lru: ${mru})`);
2934

30-
send(res, 200, cache.getPayload(current));
35+
send(res, cache.getPayload(current));
3136
}
3237
catch {
3338
logger.error("[data|get](No cache yet. Creating one...)");
@@ -55,6 +60,6 @@ export async function get(_req: Request, res: Response) {
5560
cache.updatePayload(formatted, payload);
5661
logger.info(`[data|get](cache: created|payloadsList: ${payloadsList.lru})`);
5762

58-
send(res, 200, payload);
63+
send(res, payload);
5964
}
6065
}

workspaces/server/src/endpoints/flags.ts

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,58 @@
11
// Import Node.js Dependencies
22
import stream from "node:stream";
3+
import type {
4+
IncomingMessage,
5+
ServerResponse
6+
} from "node:http";
37

48
// Import Third-party Dependencies
5-
import send from "@polka/send-type";
6-
import { getManifest, lazyFetchFlagFile, getFlags } from "@nodesecure/flags";
7-
import type { Request, Response } from "express-serve-static-core";
9+
import {
10+
getManifest,
11+
lazyFetchFlagFile,
12+
getFlags
13+
} from "@nodesecure/flags";
14+
15+
// Import Internal Dependencies
16+
import { send } from "./util/send.ts";
817

918
// CONSTANTS
1019
const kNodeSecureFlags = getFlags();
1120

12-
export function getAll(_req, res) {
13-
send(res, 200, getManifest());
21+
export function getAll(
22+
_req: IncomingMessage,
23+
res: ServerResponse
24+
) {
25+
send(res, getManifest());
1426
}
1527

16-
export function get(req: Request, res: Response) {
17-
if (req.params.title !== "hasDuplicate" && !kNodeSecureFlags.has(req.params.title)) {
18-
return send(res, 404, { error: "Not Found" });
28+
export function get(
29+
_: IncomingMessage,
30+
res: ServerResponse,
31+
params: Record<string, string | undefined>
32+
) {
33+
const { title } = params;
34+
if (!title) {
35+
return send(
36+
res,
37+
{ error: "Title is missing." },
38+
{ code: 400 }
39+
);
40+
}
41+
42+
if (
43+
title !== "hasDuplicate" &&
44+
!kNodeSecureFlags.has(title)
45+
) {
46+
return send(
47+
res,
48+
{ error: "Not Found" },
49+
{ code: 404 }
50+
);
1951
}
2052

2153
res.writeHead(200, { "Content-Type": "text/html" });
2254

23-
return stream.pipeline(lazyFetchFlagFile(req.params.title), res, (err) => {
55+
return stream.pipeline(lazyFetchFlagFile(title), res, (err) => {
2456
if (err) {
2557
console.error(err);
2658
}

workspaces/server/src/endpoints/i18n.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
// Import Third-party Dependencies
2-
import send from "@polka/send-type";
1+
// Import Node.js Dependencies
2+
import type {
3+
IncomingMessage,
4+
ServerResponse
5+
} from "node:http";
36

47
// Import Internal Dependencies
58
import { context } from "../ALS.ts";
9+
import { send } from "./util/send.ts";
610

7-
export async function get(_req, res) {
11+
export async function get(
12+
_req: IncomingMessage,
13+
res: ServerResponse
14+
) {
815
const { i18n } = context.getStore()!;
916

1017
send(
1118
res,
12-
200,
1319
{
1420
english: i18n.english.ui,
1521
french: i18n.french.ui

0 commit comments

Comments
 (0)