Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 17eec65

Browse files
committed
Cleanup socket injection
1 parent 33095b8 commit 17eec65

17 files changed

Lines changed: 1033 additions & 757 deletions

File tree

.env

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
VITE_DM_SOCKET_URL=http://localhost:3000
2-
VITE_EDGECREATOR_SOCKET_URL=http://localhost:3003
2+
VITE_EDGECREATOR_SOCKET_URL=http://localhost:3001
33
VITE_DM_URL=http://localhost:8001
44
VITE_EDGES_URL=http://localhost:8001/edges
55
VITE_FONT_SEARCH_URL=https://www.myfonts.com/WhatTheFont/
6-
7-
CLOUDINARY_URL=cloudinary://955445841324817:fqXP5YDdv8n1vwNjzgDRlFHDVto@dl7hskxab
8-
FONT_BASE_URL=https://www.myfonts.com/fonts/
9-
FONT_IMAGE_GEN_URL=https://render.myfonts.net/fonts/font_rend.php
10-
FONT_PRODUCT_BASE_URL=https://www.myfonts.com/products/
11-
12-
# Used only in the backend, so with the api directory as basis
13-
EDGES_PATH=../DucksManager/edges
14-
15-
TOKEN_SECRET=3543c30fe79047b4f73cfb61aa1eb52cb3173de4b3941e0fc4ec1b127bbeed6019695a1a453a81c33c2eea964ccc577e69c7df994124bd2751e262a311ea23a1

api/.env

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CLOUDINARY_URL=cloudinary://955445841324817:fqXP5YDdv8n1vwNjzgDRlFHDVto@dl7hskxab
2+
FONT_BASE_URL=https://www.myfonts.com/fonts/
3+
FONT_IMAGE_GEN_URL=https://render.myfonts.net/fonts/font_rend.php
4+
FONT_PRODUCT_BASE_URL=https://www.myfonts.com/products/
5+
6+
# Used only in the backend, so with the api directory as basis
7+
EDGES_PATH=../DucksManager/edges
8+
9+
TOKEN_SECRET=3543c30fe79047b4f73cfb61aa1eb52cb3173de4b3941e0fc4ec1b127bbeed6019695a1a453a81c33c2eea964ccc577e69c7df994124bd2751e262a311ea23a1
10+
11+
DM_SOCKET_URL=http://localhost:3000

api/assets/default.svg

Lines changed: 18 additions & 0 deletions
Loading

api/generateDefaultEdge.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
import { exec } from "child_process";
12
import type { Request, Response } from "express";
23
import { readFileSync } from "fs";
3-
import sharp from "sharp";
4-
5-
// eslint-disable-next-line max-len
6-
const REGEX_EDGE_URL =
7-
/^edges\/(?<countryCode>[^/]+)\/gen\/_?(?<magazineCode>[^.]+)\.(?<issueNumber>[^.]+)\.(?<extension>[^?]+)?(?:\?.+)?$/;
4+
import { pipeline } from "stream";
85

96
const corsHeaders = {
107
"Access-Control-Allow-Origin": "*",
@@ -15,35 +12,36 @@ const corsHeaders = {
1512
"If-Modified-Since,Cache-Control,Content-Type,x-dm-user,x-dm-pass",
1613
};
1714
export const get = (req: Request, res: Response) => {
18-
const input = req.url.replace(/^\//, "");
19-
let text;
20-
const match = input.match(REGEX_EDGE_URL);
21-
if (match) {
22-
const { countryCode, magazineCode, issueNumber, extension } = match.groups!;
15+
const { countrycode, magazinecode, issuenumber, extension } = req.params!;
2316

24-
if (countryCode && extension !== "png") {
25-
res.writeHead(404, corsHeaders);
26-
res.end("");
27-
return;
28-
}
29-
text = `${countryCode}/${magazineCode} ${issueNumber}`;
30-
} else {
31-
text = input;
17+
if (countrycode && extension !== "png") {
18+
res.writeHead(404, corsHeaders);
19+
res.end("");
20+
return;
3221
}
22+
const text = `${countrycode}/${magazinecode} ${issuenumber}`;
23+
24+
const convert = exec("convert svg:- png:-", {
25+
encoding: "buffer",
26+
});
3327

34-
const content = Buffer.from(
35-
readFileSync("assets/default.svg")
36-
.toString()
37-
.replace("My text", decodeURIComponent(text)),
38-
"utf8",
28+
convert.stdin!.write(
29+
Buffer.from(
30+
readFileSync("assets/default.svg")
31+
.toString()
32+
.replace("My text", decodeURIComponent(text)),
33+
"utf8"
34+
)
3935
);
40-
sharp(content).toBuffer((error, buffer) => {
41-
if (error) {
42-
res.writeHead(500, corsHeaders);
43-
return res.end(`Error : ${JSON.stringify({ error })}`);
36+
convert.stdin!.end();
37+
38+
res.setHeader("Content-Type", "image/png");
39+
40+
pipeline(convert.stdout!, res, (err) => {
41+
if (err) {
42+
console.error("Pipeline failed", err);
43+
res.status(500).send("Conversion failed");
4444
}
45-
res.writeHead(200, { ...corsHeaders, "Content-Type": "image/png" });
46-
return res.end(buffer);
4745
});
4846
};
4947

api/index.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { OptionalAuthMiddleware } from "~dm-services/auth/util";
99
import type { SessionUser } from "~dm-types/SessionUser";
1010

1111
import * as generateDefaultEdge from "./generateDefaultEdge";
12+
import { instrument } from "@socket.io/admin-ui";
13+
1214
import browse from "./services/browse";
1315
import imageInfo from "./services/image-info";
1416
import save from "./services/save";
@@ -62,29 +64,36 @@ app.get("/upload", (req, res) => {
6264
}
6365
});
6466

65-
app.get("/default-edge", (req, res) => {
66-
if (req.method === "OPTIONS") {
67-
generateDefaultEdge.options(req, res);
68-
} else if (req.method === "GET") {
69-
multer({
70-
dest: "/tmp/",
71-
limits: {
72-
fileSize: 3 * 1024 * 1024,
73-
files: 1,
74-
},
75-
}).array("files");
76-
generateDefaultEdge.get(req, res);
77-
} else {
78-
res.writeHead(405);
67+
app.get(
68+
"/default-edge/edges/:countrycode/gen/:magazinecode([^.]+).:issuenumber([^.]+).:extension(svg|png)",
69+
(req, res) => {
70+
if (req.method === "OPTIONS") {
71+
generateDefaultEdge.options(req, res);
72+
} else if (req.method === "GET") {
73+
multer({
74+
dest: "/tmp/",
75+
limits: {
76+
fileSize: 3 * 1024 * 1024,
77+
files: 1,
78+
},
79+
}).array("files");
80+
generateDefaultEdge.get(req, res);
81+
} else {
82+
res.writeHead(405);
83+
}
7984
}
80-
});
85+
);
8186
const httpServer = createServer(app);
8287
const io = new ServerWithUser(httpServer, {
8388
cors: {
8489
origin: "*",
8590
},
8691
});
8792

93+
instrument(io, {
94+
auth: false,
95+
});
96+
8897
httpServer.listen(port);
8998
console.log(`WebSocket open on port ${port}`);
9099

api/nodemon.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

api/package.json

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
{
22
"name": "~edgecreator-api",
3-
"pnpm": {
4-
"supportedArchitectures": {
5-
"os": [
6-
"linux",
7-
"darwin",
8-
"current"
9-
],
10-
"cpu": [
11-
"x64",
12-
"arm64"
13-
]
14-
}
15-
},
163
"type": "module",
174
"version": "1.0.0",
18-
"files": [
19-
"dist",
20-
"node_modules"
21-
],
225
"scripts": {
236
"clean": "rm -rf dist",
247
"build": "rm -rf dist && tsc && tsc-alias",
258
"postbuild": "cp -r ../../../packages/prisma-clients/dist/client_* dist/packages/prisma-clients",
26-
"dev": "nodemon"
9+
"dev": "concurrently -n bun,tsc \"bun run --hot index.ts\" \"tsc --noEmit --watch\""
2710
},
2811
"dependencies": {
29-
"@sentry/node": "^7.116.0",
12+
"@sentry/node": "^7.114.0",
13+
"@socket.io/admin-ui": "^0.5.1",
3014
"@vueuse/integrations": "^10.9.0",
31-
"axios": "^1.7.0",
15+
"axios": "^1.6.8",
3216
"cloudinary": "^1.41.3",
3317
"cors": "^2.8.5",
3418
"dotenv": "^16.4.5",
@@ -38,7 +22,6 @@
3822
"jsonwebtoken": "^9.0.2",
3923
"multer": "1.4.5-lts.1",
4024
"node-base64-image": "^2.0.6",
41-
"sharp": "^0.33.4",
4225
"socket.io": "^4.7.5",
4326
"socket.io-client": "^4.7.5",
4427
"universal-cookie": "^4.0.4",
@@ -60,10 +43,11 @@
6043
"@types/jsonwebtoken": "^9.0.6",
6144
"@types/multer": "^1.4.11",
6245
"@types/node": "^18.19.33",
63-
"nodemon": "^3.1.0",
46+
"concurrently": "^8.2.2",
6447
"rollup-plugin-node-globals": "^1.4.0",
6548
"rollup-plugin-tsc-alias": "^1.1.2",
66-
"tsc-alias": "^1.8.10",
49+
"tsc-alias": "^1.8.9",
50+
"tsx": "^4.10.1",
6751
"typescript": "^5.4.5"
6852
}
6953
}

0 commit comments

Comments
 (0)