Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 863cffa

Browse files
Compression
1 parent da25c2e commit 863cffa

File tree

4 files changed

+87
-18
lines changed

4 files changed

+87
-18
lines changed

cache.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import express from 'express';
2+
3+
const app = express()
4+
const port = 3000
5+
var cache = {}
6+
var midWare = (req, res, next) => {
7+
const key = req.url
8+
if (cache[key]) {
9+
res.send('from cache')
10+
} else {
11+
res.sendResponse = res.send
12+
res.send = (body) => {
13+
cache[key] = body
14+
res.sendResponse(body)
15+
}
16+
next()
17+
}
18+
}
19+
app.get('/', midWare, (req, res) => {
20+
res.send('message from route /')
21+
})
22+
23+
app.listen(port, () => {
24+
console.log(`Example app listening on port ${port}`)
25+
})

index.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { hostname } from 'node:os';
77
import cluster from 'cluster';
88
import os from 'os';
99
import chalk from 'chalk';
10+
import compression from 'compression'
1011
//@ts-ignore
1112
import { handler as ssrHandler } from './dist/server/entry.mjs';
1213
const __dirname = path.resolve();
@@ -40,6 +41,7 @@ dotenv.config();
4041
const numCPUs = process.env.CPUS || os.cpus().length;
4142
let key = process.env.KEY || 'unlock';
4243
let uri = process.env.URL || 'rubynetwork.tech';
44+
let start = false;
4345
if (uri.includes('http')) {
4446
uri = uri.replace('http://', '');
4547
}
@@ -62,22 +64,24 @@ fs.readFile(join(__dirname, 'blocklists/ADS.txt'), (err, data) => {
6264
for (let i in lines) blacklisted.push(lines[i]);
6365
});
6466
if (numCPUs > 0 && cluster.isPrimary) {
65-
console.log(`Primary ${process.pid} is running`);
67+
console.log(chalk.cyan(chalk.bold('Starting Server...')))
68+
console.log(chalk.yellow(`Primary ${process.pid} is running`));
6669
for (let i = 0; i < numCPUs; i++) {
6770
cluster.fork().on('online', () => {
68-
console.log(`Worker ${i + 1} is online`);
71+
console.log(chalk.magenta(`Worker ${i + 1} is online`));
6972
});
7073
}
7174
cluster.on('exit', (worker, code, signal) => {
72-
console.log(
75+
console.log(chalk.red(chalk.bold(
7376
`Worker ${worker.process.pid} died with code: ${code} and signal: ${signal}`
74-
);
75-
console.log(`Starting new worker in it's place`);
77+
)));
78+
console.log(chalk.yellow(`Starting new worker in it's place`));
7679
cluster.fork();
7780
});
7881
} else {
7982
const bare = createBareServer('/bare/');
8083
const app = express();
84+
app.use(compression());
8185
app.use(express.static(join(__dirname, 'dist/client')));
8286
//Server side render middleware for astro
8387
app.use(ssrHandler);
@@ -246,6 +250,7 @@ if (numCPUs > 0 && cluster.isPrimary) {
246250
res.end();
247251
return;
248252
});
253+
249254
//!CUSTOM ENDPOINTS END
250255
//RAMMERHEAD FUNCTIONS
251256
//@ts-ignore
@@ -276,12 +281,12 @@ if (numCPUs > 0 && cluster.isPrimary) {
276281
// we just need to list a few
277282
// LIST PID
278283
console.log(chalk.green(`Process id: ${process.pid}`));
279-
console.log('Listening on:');
284+
console.log(chalk.blue('Listening on:'));
280285
//@ts-ignore
281-
console.log(`\thttp://localhost:${address.port}`);
286+
console.log(chalk.blue(`\thttp://localhost:${address.port}`));
282287
//@ts-ignore
283-
console.log(`\thttp://${hostname()}:${address.port}`);
284-
console.log(
288+
console.log(chalk.blue(`\thttp://${hostname()}:${address.port}`));
289+
console.log(chalk.blue(
285290
`\thttp://${
286291
//@ts-ignore
287292
address.family === 'IPv6'
@@ -291,7 +296,7 @@ if (numCPUs > 0 && cluster.isPrimary) {
291296
address.address
292297
//@ts-ignore
293298
}:${address.port}`
294-
);
299+
));
295300
});
296301

297302
server.listen({

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"astro": "^2.1.9",
3434
"astro-robots-txt": "^0.4.1",
3535
"chalk": "^5.2.0",
36+
"compression": "^1.7.4",
3637
"dotenv": "^16.0.3",
3738
"express": "^4.18.2",
3839
"framer-motion": "^10.9.1",
@@ -49,6 +50,7 @@
4950
"ts-node": "^10.9.1"
5051
},
5152
"devDependencies": {
53+
"@types/compression": "^1.7.2",
5254
"@types/express": "^4.17.13",
5355
"@types/http-auth": "^4.1.1",
5456
"@types/node": "^18.15.10",

yarn.lock

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,13 @@
775775
"@types/connect" "*"
776776
"@types/node" "*"
777777

778+
"@types/compression@^1.7.2":
779+
version "1.7.2"
780+
resolved "https://registry.yarnpkg.com/@types/compression/-/compression-1.7.2.tgz#7cc1cdb01b4730eea284615a68fc70a2cdfd5e71"
781+
integrity sha512-lwEL4M/uAGWngWFLSG87ZDr2kLrbuR8p7X+QZB1OQlT+qkHsCPDVFnHPyXf4Vyl4yDDorNY+mAhosxkCvppatg==
782+
dependencies:
783+
"@types/express" "*"
784+
778785
"@types/connect@*":
779786
version "3.4.35"
780787
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.35.tgz#5fcf6ae445e4021d1fc2219a4873cc73a3bb2ad1"
@@ -808,7 +815,7 @@
808815
"@types/qs" "*"
809816
"@types/range-parser" "*"
810817

811-
"@types/express@^4.17.13":
818+
"@types/express@*", "@types/express@^4.17.13":
812819
version "4.17.17"
813820
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.17.tgz#01d5437f6ef9cfa8668e616e13c2f2ac9a491ae4"
814821
integrity sha512-Q4FmmuLGBG58btUnfS1c1r/NQdlp3DMfGDGig8WhfpA2YRUtEkxAjkZb0yvplJGYdF1fsQ81iMDcH24sSCNC/Q==
@@ -964,7 +971,7 @@ abbrev@1:
964971
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
965972
integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==
966973

967-
accepts@~1.3.8:
974+
accepts@~1.3.5, accepts@~1.3.8:
968975
version "1.3.8"
969976
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e"
970977
integrity sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==
@@ -1381,6 +1388,11 @@ busboy@^1.6.0:
13811388
dependencies:
13821389
streamsearch "^1.1.0"
13831390

1391+
bytes@3.0.0:
1392+
version "3.0.0"
1393+
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
1394+
integrity sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw==
1395+
13841396
bytes@3.1.2:
13851397
version "3.1.2"
13861398
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5"
@@ -1591,6 +1603,26 @@ common-ancestor-path@^1.0.1:
15911603
resolved "https://registry.yarnpkg.com/common-ancestor-path/-/common-ancestor-path-1.0.1.tgz#4f7d2d1394d91b7abdf51871c62f71eadb0182a7"
15921604
integrity sha512-L3sHRo1pXXEqX8VU28kfgUY+YGsk09hPqZiZmLacNib6XNTCM8ubYeT7ryXQw8asB1sKgcU5lkB7ONug08aB8w==
15931605

1606+
compressible@~2.0.16:
1607+
version "2.0.18"
1608+
resolved "https://registry.yarnpkg.com/compressible/-/compressible-2.0.18.tgz#af53cca6b070d4c3c0750fbd77286a6d7cc46fba"
1609+
integrity sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg==
1610+
dependencies:
1611+
mime-db ">= 1.43.0 < 2"
1612+
1613+
compression@^1.7.4:
1614+
version "1.7.4"
1615+
resolved "https://registry.yarnpkg.com/compression/-/compression-1.7.4.tgz#95523eff170ca57c29a0ca41e6fe131f41e5bb8f"
1616+
integrity sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ==
1617+
dependencies:
1618+
accepts "~1.3.5"
1619+
bytes "3.0.0"
1620+
compressible "~2.0.16"
1621+
debug "2.6.9"
1622+
on-headers "~1.0.2"
1623+
safe-buffer "5.1.2"
1624+
vary "~1.1.2"
1625+
15941626
concat-map@0.0.1:
15951627
version "0.0.1"
15961628
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
@@ -3426,7 +3458,7 @@ micromatch@^4.0.2, micromatch@^4.0.4, micromatch@^4.0.5:
34263458
braces "^3.0.2"
34273459
picomatch "^2.3.1"
34283460

3429-
mime-db@1.52.0, mime-db@^1.51.0:
3461+
mime-db@1.52.0, "mime-db@>= 1.43.0 < 2", mime-db@^1.51.0:
34303462
version "1.52.0"
34313463
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
34323464
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==
@@ -3707,6 +3739,11 @@ on-finished@2.4.1:
37073739
dependencies:
37083740
ee-first "1.1.1"
37093741

3742+
on-headers@~1.0.2:
3743+
version "1.0.2"
3744+
resolved "https://registry.yarnpkg.com/on-headers/-/on-headers-1.0.2.tgz#772b0ae6aaa525c399e489adfad90c403eb3c28f"
3745+
integrity sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==
3746+
37103747
once@^1.3.0:
37113748
version "1.4.0"
37123749
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
@@ -4371,16 +4408,16 @@ sade@^1.7.3:
43714408
dependencies:
43724409
mri "^1.1.0"
43734410

4411+
safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
4412+
version "5.1.2"
4413+
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
4414+
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
4415+
43744416
safe-buffer@5.2.1, safe-buffer@^5.0.1, safe-buffer@~5.2.0:
43754417
version "5.2.1"
43764418
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
43774419
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
43784420

4379-
safe-buffer@~5.1.0, safe-buffer@~5.1.1:
4380-
version "5.1.2"
4381-
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
4382-
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
4383-
43844421
"safer-buffer@>= 2.1.2 < 3", "safer-buffer@>= 2.1.2 < 3.0.0":
43854422
version "2.1.2"
43864423
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"

0 commit comments

Comments
 (0)