venus-pit 1.1.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,68 +0,0 @@
1
- import { tar } from "./tar.js";
2
- import express from "express";
3
- import { randomWord } from "./words/randomWord.js";
4
-
5
- function rand() {
6
- return (Math.sqrt(Math.random() * 10) / 2) * 1000;
7
- }
8
-
9
- const tarpitRouter = express.Router();
10
- const routeHandlers = new Map();
11
- let inited = false;
12
-
13
- function makeRoute() {
14
- let length = Math.floor(Math.sqrt(Math.random()) * 10);
15
- length = Math.max(length, 1)
16
- let words = "";
17
- for (let i = 0; i < length; i++) {
18
- let word = randomWord();
19
- words = words + word + "-";
20
- }
21
- if (words.endsWith("-")) words = words.slice(0, -1);
22
- return words;
23
- }
24
-
25
- function selfDestruct(route) {
26
- if (routeHandlers.has(route)) {
27
- routeHandlers.delete(route);
28
-
29
- // recreate the router w/o the deleted route
30
- tarpitRouter.stack = tarpitRouter.stack.filter((layer) => {
31
- return !(layer.route && layer.route.path === route);
32
- });
33
- }
34
- }
35
-
36
- function pit(app, instanceRoot, req) {
37
- let newRoute = `${instanceRoot.path}${makeRoute()}/`;
38
- console.log(`Creating route ${newRoute} for UA ${req.headers["user-agent"]}`);
39
-
40
- const handler = (req, res) => {
41
- // use a promise to avoid a stack overflow
42
- Promise.resolve().then(() => {
43
- // reasonable server response time, should waste cpu cycles
44
- setTimeout(() => {
45
- res.send(tar(pit(app, instanceRoot, req)));
46
- }, rand());
47
-
48
- // prevent memory leak by cleaning up old routes : add delay on garbage collection
49
- setTimeout(() => {
50
- selfDestruct(newRoute);
51
- }, rand() * rand());
52
- });
53
- };
54
-
55
- routeHandlers.set(newRoute, handler);
56
- tarpitRouter.get(newRoute, handler);
57
- // FOR TESTIng PURPOSES IF THIS MAKES IT TO PROD JUTS SHOOT ME
58
- //fetch(`http://localhost:8080${newRoute}`)
59
-
60
- // just so it doesnt get attached multiple times
61
- if (!inited) {
62
- app.use(tarpitRouter);
63
- inited = true;
64
- }
65
- return newRoute;
66
- }
67
-
68
- export { pit };
@@ -1,20 +0,0 @@
1
- import { testRandomWord } from "./randomWord.test.js";
2
- import { testTar } from "./tar.test.js";
3
- import { testVenusRoot } from "./venusRoot.test.js";
4
-
5
- const tests = [
6
- { name: "randomWord", fn: testRandomWord },
7
- { name: "tar", fn: testTar },
8
- { name: "venusRoot", fn: testVenusRoot },
9
- ];
10
- let allPassed = true;
11
-
12
- for (const { name, fn } of tests) {
13
- const result = fn();
14
- console.log(`${name}: ${result ? "PASS" : "FAIL"}`);
15
- if (!result) {allPassed = false}
16
- }
17
-
18
- if (allPassed) {
19
- console.log("all unit tests pass")
20
- }
@@ -1,10 +0,0 @@
1
- import { randomWord } from "../src/lib/tarpit/words/randomWord.js";
2
-
3
- function testRandomWord() {
4
- if (/^[a-z]+$/.test(randomWord().toLowerCase())) {
5
- return true
6
- } else {
7
- return false
8
- }
9
- }
10
- export {testRandomWord}
@@ -1,24 +0,0 @@
1
- import { parse } from "node-html-parser";
2
- import { tar } from "../src/lib/tarpit/tar.js";
3
-
4
- function testTar() {
5
- const str = tar("/SAMPLE/ROUTE/");
6
- let worked = false;
7
-
8
- try {
9
- const root = parse(str);
10
- const head = root.querySelector("head");
11
- const body = root.querySelector("body");
12
- const title = root.querySelector("title");
13
- const link = root.querySelector("a[href='/SAMPLE/ROUTE/']");
14
-
15
- worked = !!head && !!body && !!title && !!link;
16
-
17
- } catch (e) {
18
- worked = false;
19
- }
20
-
21
- return worked;
22
- }
23
-
24
- export { testTar };
@@ -1,13 +0,0 @@
1
- import { venusRoot } from "../src/lib/venusRoot.js";
2
-
3
- function testVenusRoot() {
4
- let vRoot = new venusRoot()
5
- let custom = new venusRoot("ello")
6
- // 32 chars long + the two slashes
7
- if (vRoot.path.length == 34 && custom.path == "/ello/") {
8
- return true
9
- }
10
- return false
11
- }
12
-
13
- export {testVenusRoot}