Skip to content

Commit

Permalink
fix: move bundlephobia call in the backend (#92)
Browse files Browse the repository at this point in the history
* feat: add new routes and handle error

* fix: pass httpie error to polka

* feat: replace request

* fix: delete forgotten console.log

* revert: port context
  • Loading branch information
tony-go authored Dec 4, 2021
1 parent 9ddcd7e commit b05a006
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion public/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export async function getBundlephobiaSize(name, version) {
try {
const {
gzip, size, dependencySizes
} = await getJSON(`https://bundlephobia.com/api/size?package=${name}@${version}`);
} = await getJSON(`/bundle/${name}/${version}`);

This comment has been minimized.

Copy link
@targos

targos Dec 5, 2021

Collaborator

what about scoped packages (which contain a slash in the name) ?

const fullSize = dependencySizes.reduce((prev, curr) => prev + curr.approximateSize, 0);

document.querySelector(".size-gzip").textContent = prettyBytes(gzip);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { buildServer } from "../http-server/index.js";

export async function start(json = "nsecure-result.json", options = {}) {
const port = Number(options.port);

const dataFilePath = path.join(process.cwd(), json);

const httpServer = buildServer(dataFilePath, {
port: Number.isNaN(port) ? 0 : port
});
Expand Down
23 changes: 23 additions & 0 deletions src/http-server/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { get as getRequest } from "@myunisoft/httpie";
import send from "@polka/send-type";

const kBaseBundlePhobiaUrl = "https://bundlephobia.com/api";

export async function get(req, res) {
const { pkgName, version } = req.params;

const pkgTemplate = version ? `${pkgName}@${version}` : pkgName;
try {
const { data } = await getRequest(`${kBaseBundlePhobiaUrl}/size?package=${pkgTemplate}`);
const { gzip, size, dependencySizes } = data;

return send(res, 200, {
gzip,
size,
dependencySizes
});
}
catch (error) {
return send(res, error.statusCode, { error: error.statusMessage });
}
}
6 changes: 5 additions & 1 deletion src/http-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as i18n from "@nodesecure/i18n";
import * as root from "./root.js";
import * as data from "./data.js";
import * as flags from "./flags.js";
import * as bundle from "./bundle.js";
import * as middleware from "./middleware.js";

export function buildServer(dataFilePath, options = {}) {
Expand All @@ -29,9 +30,12 @@ export function buildServer(dataFilePath, options = {}) {
httpServer.get("/data", data.get);
httpServer.get("/flags", flags.getAll);
httpServer.get("/flags/description/:title", flags.get);
httpServer.get("/bundle/:pkgName", bundle.get);
httpServer.get("/bundle/:pkgName/:version", bundle.get);

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

if (openLink) {
Expand Down
56 changes: 56 additions & 0 deletions test/httpServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,63 @@ test("'/data' should return the fixture payload we expect", async(tape) => {
tape.end();
});

test("'/bundle/:name/:version' should return the bundle size", async(tape) => {
const result = await get(new URL("/bundle/flatstr/1.0.12", HTTP_URL));

tape.equal(result.statusCode, 200);
tape.equal(result.headers["content-type"], "application/json;charset=utf-8");
checkBundleResponse(tape, result.data);
});

test("'/bundle/:name/:version' should return an error if it fails", async(tape) => {
tape.plan(2);
const wrongVersion = undefined;

try {
await get(new URL(`/bundle/rimraf/${wrongVersion}`, HTTP_URL));
}
catch (error) {
tape.equal(error.statusCode, 404);
tape.equal(error.data.error, "Not Found");
}

tape.end();
});

test("'/bundle/:name' should return the bundle size of the last version", async(tape) => {
const result = await get(new URL("/bundle/flatstr", HTTP_URL));

tape.equal(result.statusCode, 200);
tape.equal(result.headers["content-type"], "application/json;charset=utf-8");
checkBundleResponse(tape, result.data);
});

test("'/bundle/:name' should return an error if it fails", async(tape) => {
tape.plan(2);
const wrongPackageName = "br-br-br-brah";

try {
await get(new URL(`/bundle/${wrongPackageName}`, HTTP_URL));
}
catch (error) {
tape.equal(error.statusCode, 404);
tape.equal(error.data.error, "Not Found");
}

tape.end();
});

test("teardown", (tape) => {
httpServer.server.close();
tape.end();
});

/**
* HELPERS
*/

function checkBundleResponse(tapeInstance, payload) {
tapeInstance.ok(payload.gzip);
tapeInstance.ok(payload.size);
tapeInstance.ok(payload.dependencySizes);
}

0 comments on commit b05a006

Please sign in to comment.