Skip to content

Commit b05a006

Browse files
authored
fix: move bundlephobia call in the backend (#92)
* feat: add new routes and handle error * fix: pass httpie error to polka * feat: replace request * fix: delete forgotten console.log * revert: port context
1 parent 9ddcd7e commit b05a006

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

public/js/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export async function getBundlephobiaSize(name, version) {
214214
try {
215215
const {
216216
gzip, size, dependencySizes
217-
} = await getJSON(`https://bundlephobia.com/api/size?package=${name}@${version}`);
217+
} = await getJSON(`/bundle/${name}/${version}`);
218218
const fullSize = dependencySizes.reduce((prev, curr) => prev + curr.approximateSize, 0);
219219

220220
document.querySelector(".size-gzip").textContent = prettyBytes(gzip);

src/commands/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { buildServer } from "../http-server/index.js";
66

77
export async function start(json = "nsecure-result.json", options = {}) {
88
const port = Number(options.port);
9-
109
const dataFilePath = path.join(process.cwd(), json);
10+
1111
const httpServer = buildServer(dataFilePath, {
1212
port: Number.isNaN(port) ? 0 : port
1313
});

src/http-server/bundle.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { get as getRequest } from "@myunisoft/httpie";
2+
import send from "@polka/send-type";
3+
4+
const kBaseBundlePhobiaUrl = "https://bundlephobia.com/api";
5+
6+
export async function get(req, res) {
7+
const { pkgName, version } = req.params;
8+
9+
const pkgTemplate = version ? `${pkgName}@${version}` : pkgName;
10+
try {
11+
const { data } = await getRequest(`${kBaseBundlePhobiaUrl}/size?package=${pkgTemplate}`);
12+
const { gzip, size, dependencySizes } = data;
13+
14+
return send(res, 200, {
15+
gzip,
16+
size,
17+
dependencySizes
18+
});
19+
}
20+
catch (error) {
21+
return send(res, error.statusCode, { error: error.statusMessage });
22+
}
23+
}

src/http-server/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as i18n from "@nodesecure/i18n";
1313
import * as root from "./root.js";
1414
import * as data from "./data.js";
1515
import * as flags from "./flags.js";
16+
import * as bundle from "./bundle.js";
1617
import * as middleware from "./middleware.js";
1718

1819
export function buildServer(dataFilePath, options = {}) {
@@ -29,9 +30,12 @@ export function buildServer(dataFilePath, options = {}) {
2930
httpServer.get("/data", data.get);
3031
httpServer.get("/flags", flags.getAll);
3132
httpServer.get("/flags/description/:title", flags.get);
33+
httpServer.get("/bundle/:pkgName", bundle.get);
34+
httpServer.get("/bundle/:pkgName/:version", bundle.get);
3235

3336
httpServer.listen(httpConfigPort, () => {
34-
const link = `http://localhost:${httpServer.server.address().port}`;
37+
const port = httpServer.server.address().port;
38+
const link = `http://localhost:${port}`;
3539
console.log(kleur.magenta().bold(i18n.getToken("cli.http_server_started")), kleur.cyan().bold(link));
3640

3741
if (openLink) {

test/httpServer.spec.js

+56
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,63 @@ test("'/data' should return the fixture payload we expect", async(tape) => {
8686
tape.end();
8787
});
8888

89+
test("'/bundle/:name/:version' should return the bundle size", async(tape) => {
90+
const result = await get(new URL("/bundle/flatstr/1.0.12", HTTP_URL));
91+
92+
tape.equal(result.statusCode, 200);
93+
tape.equal(result.headers["content-type"], "application/json;charset=utf-8");
94+
checkBundleResponse(tape, result.data);
95+
});
96+
97+
test("'/bundle/:name/:version' should return an error if it fails", async(tape) => {
98+
tape.plan(2);
99+
const wrongVersion = undefined;
100+
101+
try {
102+
await get(new URL(`/bundle/rimraf/${wrongVersion}`, HTTP_URL));
103+
}
104+
catch (error) {
105+
tape.equal(error.statusCode, 404);
106+
tape.equal(error.data.error, "Not Found");
107+
}
108+
109+
tape.end();
110+
});
111+
112+
test("'/bundle/:name' should return the bundle size of the last version", async(tape) => {
113+
const result = await get(new URL("/bundle/flatstr", HTTP_URL));
114+
115+
tape.equal(result.statusCode, 200);
116+
tape.equal(result.headers["content-type"], "application/json;charset=utf-8");
117+
checkBundleResponse(tape, result.data);
118+
});
119+
120+
test("'/bundle/:name' should return an error if it fails", async(tape) => {
121+
tape.plan(2);
122+
const wrongPackageName = "br-br-br-brah";
123+
124+
try {
125+
await get(new URL(`/bundle/${wrongPackageName}`, HTTP_URL));
126+
}
127+
catch (error) {
128+
tape.equal(error.statusCode, 404);
129+
tape.equal(error.data.error, "Not Found");
130+
}
131+
132+
tape.end();
133+
});
134+
89135
test("teardown", (tape) => {
90136
httpServer.server.close();
91137
tape.end();
92138
});
139+
140+
/**
141+
* HELPERS
142+
*/
143+
144+
function checkBundleResponse(tapeInstance, payload) {
145+
tapeInstance.ok(payload.gzip);
146+
tapeInstance.ok(payload.size);
147+
tapeInstance.ok(payload.dependencySizes);
148+
}

0 commit comments

Comments
 (0)